workspace-mutating-tool-call.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. isWorkspaceMutatingToolCall,
  4. isWorldTouchingToolCall,
  5. WORKSPACE_MUTATING_SIDE_EFFECT_SCOPES,
  6. } from "../src/tools/contract.js";
  7. // 「第一笔写入」的判定(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md「Amend-resume」):
  8. // 执行器上载荷、driver 读载荷,两边用的就是这一个函数。
  9. describe("isWorkspaceMutatingToolCall", () => {
  10. it("only workspace, git and system scopes touch the workspace", () => {
  11. expect([...WORKSPACE_MUTATING_SIDE_EFFECT_SCOPES].sort()).toEqual([
  12. "git",
  13. "system",
  14. "workspace",
  15. ]);
  16. for (const scope of ["workspace", "git", "system"] as const) {
  17. expect(isWorkspaceMutatingToolCall({ readOnly: false, sideEffectScope: scope }), scope).toBe(
  18. true,
  19. );
  20. }
  21. for (const scope of ["none", "network", "session", "userInteraction"] as const) {
  22. expect(isWorkspaceMutatingToolCall({ readOnly: false, sideEffectScope: scope }), scope).toBe(
  23. false,
  24. );
  25. }
  26. });
  27. it("a read-only call never counts, even in a mutating scope (Bash `ls` is scope none but the rule holds regardless)", () => {
  28. expect(isWorkspaceMutatingToolCall({ readOnly: true, sideEffectScope: "workspace" })).toBe(
  29. false,
  30. );
  31. expect(isWorkspaceMutatingToolCall({ readOnly: true, sideEffectScope: "system" })).toBe(false);
  32. });
  33. it("an undeclared scope is treated as a write (an event from before the flags existed, or a tool that never said)", () => {
  34. expect(isWorkspaceMutatingToolCall({})).toBe(true);
  35. expect(isWorkspaceMutatingToolCall({ readOnly: false })).toBe(true);
  36. expect(isWorkspaceMutatingToolCall({ readOnly: undefined, sideEffectScope: undefined })).toBe(
  37. true,
  38. );
  39. });
  40. });
  41. // 「碰过外部世界」的判定(同一份 spec):决定一条缓存条目是不是纯的。
  42. describe("isWorldTouchingToolCall", () => {
  43. it("only the protocol scopes are exempt: handing a result back or asking is not touching the world", () => {
  44. for (const scope of ["session", "userInteraction"] as const) {
  45. expect(isWorldTouchingToolCall({ sideEffectScope: scope }), scope).toBe(false);
  46. }
  47. for (const scope of ["none", "workspace", "git", "system", "network"] as const) {
  48. expect(isWorldTouchingToolCall({ sideEffectScope: scope }), scope).toBe(true);
  49. }
  50. });
  51. it("a read counts: Read declares scope none, yet its answer depends on the workspace", () => {
  52. expect(isWorldTouchingToolCall({ sideEffectScope: "none" })).toBe(true);
  53. // 与「写」的判定分工:读不关门,但它让条目不再是纯的。
  54. expect(isWorkspaceMutatingToolCall({ readOnly: true, sideEffectScope: "none" })).toBe(false);
  55. });
  56. it("an undeclared scope counts as touching (conservative, same as the write predicate)", () => {
  57. expect(isWorldTouchingToolCall({})).toBe(true);
  58. });
  59. });