@dexto/tools-filesystem 1.16.6 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/blocked-path-check.cjs +62 -0
  2. package/dist/blocked-path-check.d.ts +11 -0
  3. package/dist/blocked-path-check.d.ts.map +1 -0
  4. package/dist/blocked-path-check.js +27 -0
  5. package/dist/blocked-path-check.test.cjs +117 -0
  6. package/dist/blocked-path-check.test.d.ts +2 -0
  7. package/dist/blocked-path-check.test.d.ts.map +1 -0
  8. package/dist/blocked-path-check.test.js +116 -0
  9. package/dist/directory-approval.integration.test.cjs +12 -11
  10. package/dist/directory-approval.integration.test.js +12 -11
  11. package/dist/edit-file-tool.cjs +48 -27
  12. package/dist/edit-file-tool.d.ts +2 -1
  13. package/dist/edit-file-tool.d.ts.map +1 -1
  14. package/dist/edit-file-tool.js +48 -27
  15. package/dist/edit-file-tool.test.cjs +161 -30
  16. package/dist/edit-file-tool.test.js +161 -30
  17. package/dist/file-size.cjs +47 -0
  18. package/dist/file-size.d.ts +12 -0
  19. package/dist/file-size.d.ts.map +1 -0
  20. package/dist/file-size.js +22 -0
  21. package/dist/glob-files-tool.cjs +9 -4
  22. package/dist/glob-files-tool.d.ts +2 -1
  23. package/dist/glob-files-tool.d.ts.map +1 -1
  24. package/dist/glob-files-tool.js +9 -4
  25. package/dist/glob-files-tool.test.cjs +72 -4
  26. package/dist/glob-files-tool.test.js +72 -4
  27. package/dist/grep-content-tool.cjs +4 -1
  28. package/dist/grep-content-tool.d.ts +2 -1
  29. package/dist/grep-content-tool.d.ts.map +1 -1
  30. package/dist/grep-content-tool.js +4 -1
  31. package/dist/grep-content-tool.test.cjs +5 -4
  32. package/dist/grep-content-tool.test.js +5 -4
  33. package/dist/index.d.cts +12 -5
  34. package/dist/path-validator.cjs +26 -1
  35. package/dist/path-validator.d.ts.map +1 -1
  36. package/dist/path-validator.js +26 -1
  37. package/dist/path-validator.test.cjs +75 -0
  38. package/dist/path-validator.test.js +75 -0
  39. package/dist/read-file-tool.cjs +14 -2
  40. package/dist/read-file-tool.d.ts +2 -1
  41. package/dist/read-file-tool.d.ts.map +1 -1
  42. package/dist/read-file-tool.js +14 -2
  43. package/dist/read-file-tool.test.cjs +54 -5
  44. package/dist/read-file-tool.test.js +54 -5
  45. package/dist/test-helpers.cjs +28 -0
  46. package/dist/test-helpers.d.ts +4 -0
  47. package/dist/test-helpers.d.ts.map +1 -0
  48. package/dist/test-helpers.js +4 -0
  49. package/dist/tool-factory-config.d.ts +2 -2
  50. package/dist/tool-factory.cjs +9 -5
  51. package/dist/tool-factory.d.ts.map +1 -1
  52. package/dist/tool-factory.js +9 -5
  53. package/dist/workspace-paths.cjs +5 -1
  54. package/dist/workspace-paths.d.ts.map +1 -1
  55. package/dist/workspace-paths.js +5 -1
  56. package/dist/write-file-tool.cjs +8 -1
  57. package/dist/write-file-tool.d.ts +2 -1
  58. package/dist/write-file-tool.d.ts.map +1 -1
  59. package/dist/write-file-tool.js +8 -1
  60. package/dist/write-file-tool.test.cjs +18 -17
  61. package/dist/write-file-tool.test.js +18 -17
  62. package/package.json +4 -3
@@ -21,12 +21,13 @@ __export(edit_file_tool_exports, {
21
21
  createEditFileTool: () => createEditFileTool
22
22
  });
23
23
  module.exports = __toCommonJS(edit_file_tool_exports);
24
+ var import_blocked_path_check = require("./blocked-path-check.js");
24
25
  var import_node_crypto = require("node:crypto");
25
26
  var import_zod = require("zod");
26
27
  var import_diff = require("diff");
27
28
  var import_tools = require("@dexto/core/tools");
28
29
  var import_errors = require("@dexto/core/errors");
29
- var import_error_codes = require("./error-codes.js");
30
+ var import_file_size = require("./file-size.js");
30
31
  var import_directory_approval = require("./directory-approval.js");
31
32
  var import_workspace_paths = require("./workspace-paths.js");
32
33
  const previewContentHashCache = /* @__PURE__ */ new Map();
@@ -39,6 +40,9 @@ const EditFileInputSchema = import_zod.z.object({
39
40
  new_string: import_zod.z.string().describe("Replacement text"),
40
41
  replace_all: import_zod.z.boolean().optional().default(false).describe("Replace all occurrences (default: false, requires unique match)")
41
42
  }).strict();
43
+ function replaceText(content, oldString, newString, replaceAll) {
44
+ return replaceAll ? content.split(oldString).join(newString) : content.replace(oldString, () => newString);
45
+ }
42
46
  function generateDiffPreview(filePath, originalContent, newContent) {
43
47
  const unified = (0, import_diff.createPatch)(filePath, originalContent, newContent, "before", "after", {
44
48
  context: 3
@@ -54,7 +58,7 @@ function generateDiffPreview(filePath, originalContent, newContent) {
54
58
  deletions
55
59
  };
56
60
  }
57
- function createEditFileTool(getFileSystemService) {
61
+ function createEditFileTool(getFileSystemService, blockedPathCheck, maxFileSize) {
58
62
  return (0, import_tools.defineTool)({
59
63
  id: "edit_file",
60
64
  aliases: ["edit"],
@@ -80,26 +84,24 @@ function createEditFileTool(getFileSystemService) {
80
84
  */
81
85
  preview: async (input, context) => {
82
86
  const { file_path, old_string, new_string, replace_all } = input;
83
- const resolvedFileSystemService = await getFileSystemService(context);
84
- const { path: resolvedPath } = (0, import_directory_approval.resolveFilePath)(
85
- resolvedFileSystemService.getWorkingDirectory(),
87
+ const handle = await openWorkspace(context, "edit_file");
88
+ const workspacePath = (0, import_workspace_paths.toWorkspaceRelativePath)(
89
+ "edit_file",
90
+ handle.context.path,
86
91
  file_path
87
92
  );
88
93
  try {
89
- let originalContent;
90
- try {
91
- const originalFile = await resolvedFileSystemService.readFile(resolvedPath);
92
- originalContent = originalFile.content;
93
- } catch (error) {
94
- if (error instanceof import_errors.DextoRuntimeError && error.code === import_error_codes.FileSystemErrorCode.INVALID_PATH) {
95
- const originalFile = await resolvedFileSystemService.readFileForToolPreview(
96
- resolvedPath
97
- );
98
- originalContent = originalFile.content;
99
- } else {
100
- throw error;
101
- }
102
- }
94
+ await (0, import_blocked_path_check.assertPathNotBlocked)(
95
+ blockedPathCheck,
96
+ handle.context.path,
97
+ workspacePath,
98
+ context.logger
99
+ );
100
+ const originalContent = await (0, import_file_size.readTextWithinMaxFileSize)(
101
+ handle.files,
102
+ workspacePath,
103
+ maxFileSize
104
+ );
103
105
  if (context.toolCallId) {
104
106
  previewContentHashCache.set(
105
107
  context.toolCallId,
@@ -112,29 +114,34 @@ function createEditFileTool(getFileSystemService) {
112
114
  throw import_tools.ToolError.validationFailed(
113
115
  "edit_file",
114
116
  `String found ${occurrences} times in file. Set replace_all=true to replace all, or provide more context to make old_string unique.`,
115
- { file_path: resolvedPath, occurrences }
117
+ { file_path, occurrences }
116
118
  );
117
119
  }
118
120
  }
119
- const newContent = replace_all ? originalContent.split(old_string).join(new_string) : originalContent.replace(old_string, new_string);
121
+ const newContent = replaceText(
122
+ originalContent,
123
+ old_string,
124
+ new_string,
125
+ replace_all
126
+ );
120
127
  if (originalContent === newContent) {
121
128
  throw import_tools.ToolError.validationFailed(
122
129
  "edit_file",
123
130
  `String not found in file: "${old_string.slice(0, 50)}${old_string.length > 50 ? "..." : ""}"`,
124
131
  {
125
- file_path: resolvedPath,
132
+ file_path,
126
133
  old_string_preview: old_string.slice(0, 100)
127
134
  }
128
135
  );
129
136
  }
130
- return generateDiffPreview(resolvedPath, originalContent, newContent);
137
+ return generateDiffPreview(file_path, originalContent, newContent);
131
138
  } catch (error) {
132
139
  if (error instanceof import_errors.DextoRuntimeError && error.code === import_tools.ToolErrorCode.VALIDATION_FAILED) {
133
140
  throw error;
134
141
  }
135
142
  if (error instanceof import_errors.DextoRuntimeError) {
136
143
  throw import_tools.ToolError.validationFailed("edit_file", error.message, {
137
- file_path: resolvedPath,
144
+ file_path,
138
145
  originalErrorCode: error.code
139
146
  });
140
147
  }
@@ -150,7 +157,17 @@ function createEditFileTool(getFileSystemService) {
150
157
  handle.context.path,
151
158
  file_path
152
159
  );
153
- let currentContent = await handle.files.readText(workspacePath);
160
+ await (0, import_blocked_path_check.assertPathNotBlocked)(
161
+ blockedPathCheck,
162
+ handle.context.path,
163
+ workspacePath,
164
+ context.logger
165
+ );
166
+ let currentContent = await (0, import_file_size.readTextWithinMaxFileSize)(
167
+ handle.files,
168
+ workspacePath,
169
+ maxFileSize
170
+ );
154
171
  const toolCallId = context.toolCallId;
155
172
  if (toolCallId) {
156
173
  const expectedHash = previewContentHashCache.get(toolCallId);
@@ -158,7 +175,11 @@ function createEditFileTool(getFileSystemService) {
158
175
  } else {
159
176
  previewContentHashCache.delete(toolCallId);
160
177
  try {
161
- currentContent = await handle.files.readText(workspacePath);
178
+ currentContent = await (0, import_file_size.readTextWithinMaxFileSize)(
179
+ handle.files,
180
+ workspacePath,
181
+ maxFileSize
182
+ );
162
183
  } catch {
163
184
  throw import_tools.ToolError.fileModifiedSincePreview("edit_file", file_path);
164
185
  }
@@ -186,7 +207,7 @@ function createEditFileTool(getFileSystemService) {
186
207
  { file_path, occurrences }
187
208
  );
188
209
  }
189
- const newContent = replace_all ? currentContent.split(old_string).join(new_string) : currentContent.replace(old_string, new_string);
210
+ const newContent = replaceText(currentContent, old_string, new_string, replace_all);
190
211
  await handle.files.writeFile(workspacePath, newContent);
191
212
  const _display = generateDiffPreview(file_path, currentContent, newContent);
192
213
  return {
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * Internal tool for editing files by replacing text (requires approval)
5
5
  */
6
+ import { type BlockedPathCheck } from './blocked-path-check.js';
6
7
  import { z } from 'zod';
7
8
  import type { Tool } from '@dexto/core/tools';
8
9
  import type { FileSystemServiceGetter } from './file-tool-types.js';
@@ -15,6 +16,6 @@ declare const EditFileInputSchema: z.ZodObject<{
15
16
  /**
16
17
  * Create the edit_file internal tool with directory approval support
17
18
  */
18
- export declare function createEditFileTool(getFileSystemService: FileSystemServiceGetter): Tool<typeof EditFileInputSchema>;
19
+ export declare function createEditFileTool(getFileSystemService: FileSystemServiceGetter, blockedPathCheck: BlockedPathCheck, maxFileSize: number): Tool<typeof EditFileInputSchema>;
19
20
  export {};
20
21
  //# sourceMappingURL=edit-file-tool.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"edit-file-tool.d.ts","sourceRoot":"","sources":["../src/edit-file-tool.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB,OAAO,KAAK,EAAmB,IAAI,EAAwB,MAAM,mBAAmB,CAAC;AACrF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAmBpE,QAAA,MAAM,mBAAmB;;;;;kBAaZ,CAAC;AA0Bd;;GAEG;AACH,wBAAgB,kBAAkB,CAC9B,oBAAoB,EAAE,uBAAuB,GAC9C,IAAI,CAAC,OAAO,mBAAmB,CAAC,CA6LlC"}
1
+ {"version":3,"file":"edit-file-tool.d.ts","sourceRoot":"","sources":["../src/edit-file-tool.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAwB,KAAK,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAEtF,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB,OAAO,KAAK,EAAmB,IAAI,EAAwB,MAAM,mBAAmB,CAAC;AACrF,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAmBpE,QAAA,MAAM,mBAAmB;;;;;kBAaZ,CAAC;AAyCd;;GAEG;AACH,wBAAgB,kBAAkB,CAC9B,oBAAoB,EAAE,uBAAuB,EAC7C,gBAAgB,EAAE,gBAAgB,EAClC,WAAW,EAAE,MAAM,GACpB,IAAI,CAAC,OAAO,mBAAmB,CAAC,CAsMlC"}
@@ -1,3 +1,4 @@
1
+ import { assertPathNotBlocked } from "./blocked-path-check.js";
1
2
  import { createHash } from "node:crypto";
2
3
  import { z } from "zod";
3
4
  import { createPatch } from "diff";
@@ -10,7 +11,7 @@ import {
10
11
  truncateForHeader
11
12
  } from "@dexto/core/tools";
12
13
  import { DextoRuntimeError } from "@dexto/core/errors";
13
- import { FileSystemErrorCode } from "./error-codes.js";
14
+ import { readTextWithinMaxFileSize } from "./file-size.js";
14
15
  import { createDirectoryAccessApprovalHandlers, resolveFilePath } from "./directory-approval.js";
15
16
  import { toWorkspaceRelativePath } from "./workspace-paths.js";
16
17
  const previewContentHashCache = /* @__PURE__ */ new Map();
@@ -23,6 +24,9 @@ const EditFileInputSchema = z.object({
23
24
  new_string: z.string().describe("Replacement text"),
24
25
  replace_all: z.boolean().optional().default(false).describe("Replace all occurrences (default: false, requires unique match)")
25
26
  }).strict();
27
+ function replaceText(content, oldString, newString, replaceAll) {
28
+ return replaceAll ? content.split(oldString).join(newString) : content.replace(oldString, () => newString);
29
+ }
26
30
  function generateDiffPreview(filePath, originalContent, newContent) {
27
31
  const unified = createPatch(filePath, originalContent, newContent, "before", "after", {
28
32
  context: 3
@@ -38,7 +42,7 @@ function generateDiffPreview(filePath, originalContent, newContent) {
38
42
  deletions
39
43
  };
40
44
  }
41
- function createEditFileTool(getFileSystemService) {
45
+ function createEditFileTool(getFileSystemService, blockedPathCheck, maxFileSize) {
42
46
  return defineTool({
43
47
  id: "edit_file",
44
48
  aliases: ["edit"],
@@ -64,26 +68,24 @@ function createEditFileTool(getFileSystemService) {
64
68
  */
65
69
  preview: async (input, context) => {
66
70
  const { file_path, old_string, new_string, replace_all } = input;
67
- const resolvedFileSystemService = await getFileSystemService(context);
68
- const { path: resolvedPath } = resolveFilePath(
69
- resolvedFileSystemService.getWorkingDirectory(),
71
+ const handle = await openWorkspace(context, "edit_file");
72
+ const workspacePath = toWorkspaceRelativePath(
73
+ "edit_file",
74
+ handle.context.path,
70
75
  file_path
71
76
  );
72
77
  try {
73
- let originalContent;
74
- try {
75
- const originalFile = await resolvedFileSystemService.readFile(resolvedPath);
76
- originalContent = originalFile.content;
77
- } catch (error) {
78
- if (error instanceof DextoRuntimeError && error.code === FileSystemErrorCode.INVALID_PATH) {
79
- const originalFile = await resolvedFileSystemService.readFileForToolPreview(
80
- resolvedPath
81
- );
82
- originalContent = originalFile.content;
83
- } else {
84
- throw error;
85
- }
86
- }
78
+ await assertPathNotBlocked(
79
+ blockedPathCheck,
80
+ handle.context.path,
81
+ workspacePath,
82
+ context.logger
83
+ );
84
+ const originalContent = await readTextWithinMaxFileSize(
85
+ handle.files,
86
+ workspacePath,
87
+ maxFileSize
88
+ );
87
89
  if (context.toolCallId) {
88
90
  previewContentHashCache.set(
89
91
  context.toolCallId,
@@ -96,29 +98,34 @@ function createEditFileTool(getFileSystemService) {
96
98
  throw ToolError.validationFailed(
97
99
  "edit_file",
98
100
  `String found ${occurrences} times in file. Set replace_all=true to replace all, or provide more context to make old_string unique.`,
99
- { file_path: resolvedPath, occurrences }
101
+ { file_path, occurrences }
100
102
  );
101
103
  }
102
104
  }
103
- const newContent = replace_all ? originalContent.split(old_string).join(new_string) : originalContent.replace(old_string, new_string);
105
+ const newContent = replaceText(
106
+ originalContent,
107
+ old_string,
108
+ new_string,
109
+ replace_all
110
+ );
104
111
  if (originalContent === newContent) {
105
112
  throw ToolError.validationFailed(
106
113
  "edit_file",
107
114
  `String not found in file: "${old_string.slice(0, 50)}${old_string.length > 50 ? "..." : ""}"`,
108
115
  {
109
- file_path: resolvedPath,
116
+ file_path,
110
117
  old_string_preview: old_string.slice(0, 100)
111
118
  }
112
119
  );
113
120
  }
114
- return generateDiffPreview(resolvedPath, originalContent, newContent);
121
+ return generateDiffPreview(file_path, originalContent, newContent);
115
122
  } catch (error) {
116
123
  if (error instanceof DextoRuntimeError && error.code === ToolErrorCode.VALIDATION_FAILED) {
117
124
  throw error;
118
125
  }
119
126
  if (error instanceof DextoRuntimeError) {
120
127
  throw ToolError.validationFailed("edit_file", error.message, {
121
- file_path: resolvedPath,
128
+ file_path,
122
129
  originalErrorCode: error.code
123
130
  });
124
131
  }
@@ -134,7 +141,17 @@ function createEditFileTool(getFileSystemService) {
134
141
  handle.context.path,
135
142
  file_path
136
143
  );
137
- let currentContent = await handle.files.readText(workspacePath);
144
+ await assertPathNotBlocked(
145
+ blockedPathCheck,
146
+ handle.context.path,
147
+ workspacePath,
148
+ context.logger
149
+ );
150
+ let currentContent = await readTextWithinMaxFileSize(
151
+ handle.files,
152
+ workspacePath,
153
+ maxFileSize
154
+ );
138
155
  const toolCallId = context.toolCallId;
139
156
  if (toolCallId) {
140
157
  const expectedHash = previewContentHashCache.get(toolCallId);
@@ -142,7 +159,11 @@ function createEditFileTool(getFileSystemService) {
142
159
  } else {
143
160
  previewContentHashCache.delete(toolCallId);
144
161
  try {
145
- currentContent = await handle.files.readText(workspacePath);
162
+ currentContent = await readTextWithinMaxFileSize(
163
+ handle.files,
164
+ workspacePath,
165
+ maxFileSize
166
+ );
146
167
  } catch {
147
168
  throw ToolError.fileModifiedSincePreview("edit_file", file_path);
148
169
  }
@@ -170,7 +191,7 @@ function createEditFileTool(getFileSystemService) {
170
191
  { file_path, occurrences }
171
192
  );
172
193
  }
173
- const newContent = replace_all ? currentContent.split(old_string).join(new_string) : currentContent.replace(old_string, new_string);
194
+ const newContent = replaceText(currentContent, old_string, new_string, replace_all);
174
195
  await handle.files.writeFile(workspacePath, newContent);
175
196
  const _display = generateDiffPreview(file_path, currentContent, newContent);
176
197
  return {
@@ -22,10 +22,13 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
22
  mod
23
23
  ));
24
24
  var import_vitest = require("vitest");
25
+ var import_test_helpers = require("./test-helpers.js");
25
26
  var path = __toESM(require("node:path"), 1);
26
27
  var fs = __toESM(require("node:fs/promises"), 1);
27
28
  var os = __toESM(require("node:os"), 1);
29
+ var import_blocked_path_check = require("./blocked-path-check.js");
28
30
  var import_edit_file_tool = require("./edit-file-tool.js");
31
+ var import_error_codes = require("./error-codes.js");
29
32
  var import_filesystem_service = require("./filesystem-service.js");
30
33
  var import_core = require("@dexto/core");
31
34
  var import_core2 = require("@dexto/core");
@@ -125,7 +128,7 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
125
128
  const getFileSystemService = import_vitest.vi.fn(async () => {
126
129
  throw new Error("edit_file execute must not use FileSystemService");
127
130
  });
128
- const tool = (0, import_edit_file_tool.createEditFileTool)(getFileSystemService);
131
+ const tool = (0, import_edit_file_tool.createEditFileTool)(getFileSystemService, import_test_helpers.allowAllPaths, 10 * 1024 * 1024);
129
132
  const parsedInput = tool.inputSchema.parse({
130
133
  file_path: "workspace.txt",
131
134
  old_string: "world",
@@ -163,7 +166,7 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
163
166
  }
164
167
  }))
165
168
  };
166
- const tool = (0, import_edit_file_tool.createEditFileTool)(import_vitest.vi.fn());
169
+ const tool = (0, import_edit_file_tool.createEditFileTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths, 10 * 1024 * 1024);
167
170
  await tool.execute(
168
171
  tool.inputSchema.parse({
169
172
  file_path: path.join(tempDir, "workspace.txt"),
@@ -201,7 +204,7 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
201
204
  }
202
205
  }))
203
206
  };
204
- const tool = (0, import_edit_file_tool.createEditFileTool)(import_vitest.vi.fn());
207
+ const tool = (0, import_edit_file_tool.createEditFileTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths, 10 * 1024 * 1024);
205
208
  await (0, import_vitest.expect)(
206
209
  tool.execute(
207
210
  tool.inputSchema.parse({
@@ -221,7 +224,11 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
221
224
  (0, import_vitest.expect)(writeFile).not.toHaveBeenCalled();
222
225
  });
223
226
  (0, import_vitest.it)("should describe edit calls with an Edit header", () => {
224
- const tool = (0, import_edit_file_tool.createEditFileTool)(async () => fileSystemService);
227
+ const tool = (0, import_edit_file_tool.createEditFileTool)(
228
+ async () => fileSystemService,
229
+ import_test_helpers.allowAllPaths,
230
+ 10 * 1024 * 1024
231
+ );
225
232
  const header = tool.presentation?.describeHeader?.(
226
233
  tool.inputSchema.parse({
227
234
  file_path: "snake.py",
@@ -236,8 +243,12 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
236
243
  });
237
244
  });
238
245
  (0, import_vitest.describe)("File Modification Detection", () => {
239
- (0, import_vitest.it)("should generate preview for files outside config-allowed roots (preview read only)", async () => {
240
- const tool = (0, import_edit_file_tool.createEditFileTool)(async () => fileSystemService);
246
+ (0, import_vitest.it)("refuses to preview a file outside the workspace", async () => {
247
+ const tool = (0, import_edit_file_tool.createEditFileTool)(
248
+ async () => fileSystemService,
249
+ import_test_helpers.allowAllPaths,
250
+ 10 * 1024 * 1024
251
+ );
241
252
  const previewFn = tool.presentation?.preview;
242
253
  (0, import_vitest.expect)(previewFn).toBeDefined();
243
254
  const rawExternalDir = await fs.mkdtemp(
@@ -247,30 +258,130 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
247
258
  const externalFile = path.join(externalDir, "external.txt");
248
259
  try {
249
260
  await fs.writeFile(externalFile, "hello world");
250
- const toolCallId = "preview-outside-roots";
251
- const parsedInput = tool.inputSchema.parse({
252
- file_path: externalFile,
253
- old_string: "world",
254
- new_string: "universe"
255
- });
256
- const preview = await previewFn(
257
- parsedInput,
258
- createToolContext(mockLogger, { toolCallId })
259
- );
260
- (0, import_vitest.expect)(preview).toBeDefined();
261
- (0, import_vitest.expect)(preview?.type).toBe("diff");
262
- if (preview?.type === "diff") {
263
- (0, import_vitest.expect)(preview.title).toBe("Update file");
264
- (0, import_vitest.expect)(preview.filename).toBe(externalFile);
265
- } else {
266
- import_vitest.expect.fail("Expected diff preview");
267
- }
261
+ await (0, import_vitest.expect)(
262
+ previewFn(
263
+ tool.inputSchema.parse({
264
+ file_path: externalFile,
265
+ old_string: "world",
266
+ new_string: "universe"
267
+ }),
268
+ createToolContext(mockLogger, { toolCallId: "preview-outside-roots" })
269
+ )
270
+ ).rejects.toMatchObject({ code: import_core.ToolErrorCode.VALIDATION_FAILED });
268
271
  } finally {
269
272
  await fs.rm(externalDir, { recursive: true, force: true });
270
273
  }
271
274
  });
275
+ (0, import_vitest.it)("refuses to preview a blocked file", async () => {
276
+ const blockedCheck = (0, import_blocked_path_check.createBlockedPathCheck)({
277
+ allowedPaths: ["."],
278
+ blockedPaths: [".env"],
279
+ blockedExtensions: [],
280
+ maxFileSize: 1024,
281
+ enableBackups: false,
282
+ backupRetentionDays: 7
283
+ });
284
+ await fs.mkdir(path.join(tempDir, "sub"));
285
+ await fs.writeFile(path.join(tempDir, "sub", ".env"), "SECRET=1");
286
+ const tool = (0, import_edit_file_tool.createEditFileTool)(import_vitest.vi.fn(), blockedCheck, 1024);
287
+ await (0, import_vitest.expect)(
288
+ tool.presentation.preview(
289
+ tool.inputSchema.parse({
290
+ file_path: "sub/.env",
291
+ old_string: "SECRET=1",
292
+ new_string: "SECRET=2"
293
+ }),
294
+ createToolContext(mockLogger)
295
+ )
296
+ ).rejects.toMatchObject({ code: import_core.ToolErrorCode.VALIDATION_FAILED });
297
+ });
298
+ (0, import_vitest.it)("refuses to preview or edit a file larger than maxFileSize", async () => {
299
+ await fs.writeFile(path.join(tempDir, "big.txt"), "x".repeat(11));
300
+ const tool = (0, import_edit_file_tool.createEditFileTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths, 10);
301
+ const input = tool.inputSchema.parse({
302
+ file_path: "big.txt",
303
+ old_string: "x",
304
+ new_string: "y",
305
+ replace_all: true
306
+ });
307
+ await (0, import_vitest.expect)(
308
+ tool.presentation.preview(input, createToolContext(mockLogger))
309
+ ).rejects.toMatchObject({ code: import_core.ToolErrorCode.VALIDATION_FAILED });
310
+ await (0, import_vitest.expect)(tool.execute(input, createToolContext(mockLogger))).rejects.toMatchObject({
311
+ code: import_error_codes.FileSystemErrorCode.FILE_TOO_LARGE
312
+ });
313
+ await (0, import_vitest.expect)(fs.readFile(path.join(tempDir, "big.txt"), "utf-8")).resolves.toBe(
314
+ "x".repeat(11)
315
+ );
316
+ });
317
+ (0, import_vitest.it)("checks the size before reading when previewing and editing", async () => {
318
+ const readText = import_vitest.vi.fn(async () => "x".repeat(11));
319
+ const readFileSize = import_vitest.vi.fn(async () => 11);
320
+ const writeFile = import_vitest.vi.fn(async () => void 0);
321
+ const services = {
322
+ ...createWorkspaceServices(tempDir),
323
+ workspaceManager: {
324
+ open: async () => ({
325
+ context: { id: "w", path: tempDir, createdAt: 1, lastActiveAt: 1 },
326
+ capabilities: ["files"],
327
+ files: {
328
+ readFile: readText,
329
+ readText,
330
+ readFileSize,
331
+ glob: import_vitest.vi.fn(async () => []),
332
+ writeFile,
333
+ listFiles: import_vitest.vi.fn(async () => [])
334
+ }
335
+ })
336
+ }
337
+ };
338
+ const tool = (0, import_edit_file_tool.createEditFileTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths, 10);
339
+ const input = tool.inputSchema.parse({
340
+ file_path: "big.txt",
341
+ old_string: "x",
342
+ new_string: "y",
343
+ replace_all: true
344
+ });
345
+ await (0, import_vitest.expect)(
346
+ tool.presentation.preview(input, createToolContext(mockLogger, { services }))
347
+ ).rejects.toMatchObject({ code: import_core.ToolErrorCode.VALIDATION_FAILED });
348
+ await (0, import_vitest.expect)(
349
+ tool.execute(input, createToolContext(mockLogger, { services }))
350
+ ).rejects.toMatchObject({ code: import_error_codes.FileSystemErrorCode.FILE_TOO_LARGE });
351
+ (0, import_vitest.expect)(readFileSize).toHaveBeenCalledTimes(2);
352
+ (0, import_vitest.expect)(readText).not.toHaveBeenCalled();
353
+ (0, import_vitest.expect)(writeFile).not.toHaveBeenCalled();
354
+ });
355
+ import_vitest.it.each([
356
+ ["$&", "cost $& now"],
357
+ ["$1", "cost $1 now"],
358
+ ["$$", "cost $$ now"],
359
+ ["$'", "cost $' now"],
360
+ ["$`", "cost $` now"]
361
+ ])("writes %s from new_string as literal text", async (token, expected) => {
362
+ await fs.writeFile(path.join(tempDir, "price.txt"), "cost PRICE now");
363
+ const tool = (0, import_edit_file_tool.createEditFileTool)(import_vitest.vi.fn(), import_test_helpers.allowAllPaths, 10 * 1024 * 1024);
364
+ const input = tool.inputSchema.parse({
365
+ file_path: "price.txt",
366
+ old_string: "PRICE",
367
+ new_string: token
368
+ });
369
+ const preview = await tool.presentation.preview(input, createToolContext(mockLogger));
370
+ (0, import_vitest.expect)(preview).toMatchObject({ type: "diff" });
371
+ if (preview?.type === "diff") {
372
+ (0, import_vitest.expect)(preview.unified).toContain(`+${expected}`);
373
+ }
374
+ await tool.execute(input, createToolContext(mockLogger));
375
+ await (0, import_vitest.expect)(fs.readFile(path.join(tempDir, "price.txt"), "utf-8")).resolves.toBe(
376
+ expected
377
+ );
378
+ });
272
379
  (0, import_vitest.it)("should succeed when file is not modified between preview and execute", async () => {
273
- const tool = (0, import_edit_file_tool.createEditFileTool)(async () => fileSystemService);
380
+ const tool = (0, import_edit_file_tool.createEditFileTool)(
381
+ async () => fileSystemService,
382
+ import_test_helpers.allowAllPaths,
383
+ 10 * 1024 * 1024
384
+ );
274
385
  const previewFn = tool.presentation?.preview;
275
386
  (0, import_vitest.expect)(previewFn).toBeDefined();
276
387
  const testFile = path.join(tempDir, "test.txt");
@@ -297,7 +408,11 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
297
408
  (0, import_vitest.expect)(content).toBe("hello universe");
298
409
  });
299
410
  (0, import_vitest.it)("should fail when file is modified between preview and execute", async () => {
300
- const tool = (0, import_edit_file_tool.createEditFileTool)(async () => fileSystemService);
411
+ const tool = (0, import_edit_file_tool.createEditFileTool)(
412
+ async () => fileSystemService,
413
+ import_test_helpers.allowAllPaths,
414
+ 10 * 1024 * 1024
415
+ );
301
416
  const previewFn = tool.presentation?.preview;
302
417
  (0, import_vitest.expect)(previewFn).toBeDefined();
303
418
  const testFile = path.join(tempDir, "test.txt");
@@ -328,7 +443,11 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
328
443
  (0, import_vitest.expect)(content).toBe("hello world - user added this");
329
444
  });
330
445
  (0, import_vitest.it)("should detect file modification with correct error code", async () => {
331
- const tool = (0, import_edit_file_tool.createEditFileTool)(async () => fileSystemService);
446
+ const tool = (0, import_edit_file_tool.createEditFileTool)(
447
+ async () => fileSystemService,
448
+ import_test_helpers.allowAllPaths,
449
+ 10 * 1024 * 1024
450
+ );
332
451
  const previewFn = tool.presentation?.preview;
333
452
  (0, import_vitest.expect)(previewFn).toBeDefined();
334
453
  const testFile = path.join(tempDir, "test.txt");
@@ -357,7 +476,11 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
357
476
  }
358
477
  });
359
478
  (0, import_vitest.it)("should work without toolCallId (no modification check)", async () => {
360
- const tool = (0, import_edit_file_tool.createEditFileTool)(async () => fileSystemService);
479
+ const tool = (0, import_edit_file_tool.createEditFileTool)(
480
+ async () => fileSystemService,
481
+ import_test_helpers.allowAllPaths,
482
+ 10 * 1024 * 1024
483
+ );
361
484
  const previewFn = tool.presentation?.preview;
362
485
  (0, import_vitest.expect)(previewFn).toBeDefined();
363
486
  const testFile = path.join(tempDir, "test.txt");
@@ -380,7 +503,11 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
380
503
  }
381
504
  });
382
505
  (0, import_vitest.it)("should clean up hash cache after successful execution", async () => {
383
- const tool = (0, import_edit_file_tool.createEditFileTool)(async () => fileSystemService);
506
+ const tool = (0, import_edit_file_tool.createEditFileTool)(
507
+ async () => fileSystemService,
508
+ import_test_helpers.allowAllPaths,
509
+ 10 * 1024 * 1024
510
+ );
384
511
  const previewFn = tool.presentation?.preview;
385
512
  (0, import_vitest.expect)(previewFn).toBeDefined();
386
513
  const testFile = path.join(tempDir, "test.txt");
@@ -410,7 +537,11 @@ function resolveWorkspacePath(workspaceRoot, filePath) {
410
537
  (0, import_vitest.expect)(content).toBe("hello galaxy");
411
538
  });
412
539
  (0, import_vitest.it)("should clean up hash cache after failed execution", async () => {
413
- const tool = (0, import_edit_file_tool.createEditFileTool)(async () => fileSystemService);
540
+ const tool = (0, import_edit_file_tool.createEditFileTool)(
541
+ async () => fileSystemService,
542
+ import_test_helpers.allowAllPaths,
543
+ 10 * 1024 * 1024
544
+ );
414
545
  const previewFn = tool.presentation?.preview;
415
546
  (0, import_vitest.expect)(previewFn).toBeDefined();
416
547
  const testFile = path.join(tempDir, "test.txt");