workflow-draft-write-symlink-guard.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // ZP-20261110-04:Write/Edit 处理器对草稿目录内的目标,必须把工作目录作为 symlinkFreeBase 交给 fs 端口,
  2. // 由适配器在写入执行点逐级核验(见 adapters/tests/fs-workflow-draft-symlink.test.ts)。
  3. // 普通路径不带该字段,行为不变。
  4. import { describe, expect, it } from "vitest";
  5. import {
  6. createFileSystemError,
  7. type FileSystemPort,
  8. type FileSystemWriteTextRequest,
  9. } from "@zcode/contracts";
  10. import { editHandler } from "../src/tool/handlers/edit.js";
  11. import { writeHandler } from "../src/tool/handlers/write.js";
  12. import type { ToolExecutionContext } from "../src/tool/types.js";
  13. const WORKING_DIRECTORY = "/repo/project";
  14. describe("Write handler workflow draft symlink guard", () => {
  15. it("passes the working directory as symlinkFreeBase for an absolute draft path", async () => {
  16. const port = createRecordingPort();
  17. await writeHandler(
  18. { file_path: `${WORKING_DIRECTORY}/.zcode/workflow-drafts/nightly.dwf.ts`, content: "x" },
  19. contextWith(port),
  20. );
  21. expect(port.writes).toHaveLength(1);
  22. expect(port.writes[0]?.symlinkFreeBase).toBe(WORKING_DIRECTORY);
  23. });
  24. it("passes symlinkFreeBase for a relative draft path resolved against the working directory", async () => {
  25. const port = createRecordingPort();
  26. await writeHandler(
  27. { file_path: ".zcode/workflow-drafts/nightly.dwf.ts", content: "x" },
  28. contextWith(port),
  29. );
  30. expect(port.writes[0]?.symlinkFreeBase).toBe(WORKING_DIRECTORY);
  31. });
  32. it("does not pass symlinkFreeBase for an ordinary workspace path", async () => {
  33. const port = createRecordingPort();
  34. await writeHandler({ file_path: "src/index.ts", content: "x" }, contextWith(port));
  35. expect(port.writes).toHaveLength(1);
  36. expect("symlinkFreeBase" in (port.writes[0] ?? {})).toBe(false);
  37. });
  38. });
  39. // Edit 与 Write 同属草稿免确认工具,写入走 writeEditResult,接线需单独守住。
  40. // old_string 为空、文件不存在时走新建分支,直接进入写入。
  41. describe("Edit handler workflow draft symlink guard", () => {
  42. it("passes the working directory as symlinkFreeBase for a draft path", async () => {
  43. const port = createRecordingPort();
  44. await editHandler(
  45. { file_path: ".zcode/workflow-drafts/nightly.dwf.ts", old_string: "", new_string: "x" },
  46. contextWith(port),
  47. );
  48. expect(port.writes).toHaveLength(1);
  49. expect(port.writes[0]?.symlinkFreeBase).toBe(WORKING_DIRECTORY);
  50. });
  51. it("does not pass symlinkFreeBase for an ordinary workspace path", async () => {
  52. const port = createRecordingPort();
  53. await editHandler(
  54. { file_path: "src/index.ts", old_string: "", new_string: "x" },
  55. contextWith(port),
  56. );
  57. expect(port.writes).toHaveLength(1);
  58. expect("symlinkFreeBase" in (port.writes[0] ?? {})).toBe(false);
  59. });
  60. });
  61. function notFound(path: string): never {
  62. throw createFileSystemError({ code: "not_found", path, message: `File not found: ${path}` });
  63. }
  64. function createRecordingPort(): FileSystemPort & { writes: FileSystemWriteTextRequest[] } {
  65. const writes: FileSystemWriteTextRequest[] = [];
  66. // 只实现 Write/Edit 处理器用到的方法;读取和 stat 总是 not_found,让处理器直接进入写入。
  67. const port = {
  68. writes,
  69. async readTextFile(request: { path: string }) {
  70. notFound(request.path);
  71. },
  72. async stat(request: { path: string }) {
  73. notFound(request.path);
  74. },
  75. async writeTextFile(request: FileSystemWriteTextRequest) {
  76. writes.push(request);
  77. return { path: request.path, bytesWritten: request.content.length };
  78. },
  79. };
  80. return port as unknown as FileSystemPort & { writes: FileSystemWriteTextRequest[] };
  81. }
  82. function contextWith(fileSystemPort: FileSystemPort): ToolExecutionContext {
  83. return {
  84. toolCallId: "tool_test",
  85. traceId: "trace_test",
  86. spanId: "span_test",
  87. abortSignal: new AbortController().signal,
  88. fileSystemPort,
  89. readFileState: undefined,
  90. workingDirectory: WORKING_DIRECTORY,
  91. workspaceRoot: WORKING_DIRECTORY,
  92. sessionId: "sess_test",
  93. turnId: "turn_test",
  94. };
  95. }