@criogaid/pi-hashline-edit 0.13.0 → 0.15.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 (152) hide show
  1. package/README.md +18 -12
  2. package/README.zh-CN.md +18 -12
  3. package/package.json +8 -1
  4. package/src/core/AGENTS.md +3 -1
  5. package/src/core/apply.ts +110 -117
  6. package/src/core/character-set.ts +2 -3
  7. package/src/core/copy-span.ts +15 -0
  8. package/src/core/errors.ts +24 -9
  9. package/src/core/lines.ts +71 -17
  10. package/src/core/ranges.ts +5 -4
  11. package/src/core/replace-schema.ts +3 -1
  12. package/src/core/replace.ts +50 -29
  13. package/src/core/search-analysis.ts +21 -22
  14. package/src/core/search.ts +15 -32
  15. package/src/core/types.ts +3 -0
  16. package/src/index.ts +47 -39
  17. package/src/pi/AGENTS.md +30 -19
  18. package/src/pi/action-fusion.ts +284 -211
  19. package/src/pi/argument-diagnostics.ts +50 -37
  20. package/src/pi/argument-error.ts +2 -2
  21. package/src/pi/argument-validation.ts +163 -66
  22. package/src/pi/budgets.ts +12 -0
  23. package/src/pi/diagnostic-buffer.ts +12 -11
  24. package/src/pi/edit-tool.ts +138 -115
  25. package/src/pi/error-text.ts +10 -2
  26. package/src/pi/failure-context.ts +24 -37
  27. package/src/pi/file-commit.ts +131 -164
  28. package/src/pi/file-read.ts +122 -31
  29. package/src/pi/fusion-arguments.ts +214 -0
  30. package/src/pi/fusion-card.ts +102 -60
  31. package/src/pi/grep-candidates.ts +35 -22
  32. package/src/pi/grep-output.ts +197 -190
  33. package/src/pi/grep-render.ts +22 -13
  34. package/src/pi/grep-reread.ts +7 -6
  35. package/src/pi/grep-scope.ts +31 -16
  36. package/src/pi/grep-search.ts +56 -47
  37. package/src/pi/grep-tool.ts +35 -33
  38. package/src/pi/grep-worker.ts +3 -4
  39. package/src/pi/mutation-diff.ts +238 -0
  40. package/src/pi/mutation-result.ts +102 -79
  41. package/src/pi/mutation-runner.ts +107 -84
  42. package/src/pi/native-bash.ts +41 -0
  43. package/src/pi/path.ts +59 -6
  44. package/src/pi/read-tool.ts +173 -138
  45. package/src/pi/regex-worker.ts +14 -8
  46. package/src/pi/render.ts +101 -54
  47. package/src/pi/replace-regex.ts +1 -1
  48. package/src/pi/replace-tool.ts +14 -7
  49. package/src/pi/report-cause.ts +34 -17
  50. package/src/pi/report-schema.ts +184 -85
  51. package/src/pi/report.ts +133 -64
  52. package/src/pi/rg-process.ts +68 -48
  53. package/src/pi/schema.ts +4 -0
  54. package/src/pi/text-stream.ts +81 -87
  55. package/src/pi/workspace-cwd.js +2 -0
  56. package/src/pi/workspace.ts +46 -0
  57. package/src/pi/write-tool.ts +19 -13
  58. package/src/core/apply.test.ts +0 -644
  59. package/src/core/case-fold.test.ts +0 -35
  60. package/src/core/character-set.test.ts +0 -97
  61. package/src/core/errors.test.ts +0 -39
  62. package/src/core/hash.test.ts +0 -64
  63. package/src/core/lines.test.ts +0 -63
  64. package/src/core/ranges.test.ts +0 -123
  65. package/src/core/replace.test.ts +0 -187
  66. package/src/core/search-analysis.test.ts +0 -210
  67. package/src/core/search-matrix.test.ts +0 -64
  68. package/src/core/search-matrix.testing.ts +0 -171
  69. package/src/core/search.test.ts +0 -132
  70. package/src/core/text.test.ts +0 -52
  71. package/src/integration/config-registration.test.ts +0 -291
  72. package/src/integration/fusion-dispatch.test.ts +0 -320
  73. package/src/integration/grep-errors.test.ts +0 -81
  74. package/src/integration/grep-flags.test.ts +0 -76
  75. package/src/integration/grep-js-completeness.test.ts +0 -278
  76. package/src/integration/grep-line-candidates.test.ts +0 -201
  77. package/src/integration/grep-matrix.test.ts +0 -118
  78. package/src/integration/grep-order.test.ts +0 -78
  79. package/src/integration/grep-path-matrix.test.ts +0 -128
  80. package/src/integration/grep-prefilter-coverage.test.ts +0 -204
  81. package/src/integration/grep-process-limits.test.ts +0 -122
  82. package/src/integration/grep-regex.test.ts +0 -412
  83. package/src/integration/grep-rg.test.ts +0 -1659
  84. package/src/integration/grep-scan-budget.test.ts +0 -111
  85. package/src/integration/grep-translation.test.ts +0 -199
  86. package/src/integration/large-text.test.ts +0 -70
  87. package/src/integration/model-budget-lifecycle.test.ts +0 -186
  88. package/src/integration/mutation-file-types.test.ts +0 -30
  89. package/src/integration/mutation-file-types.testing.ts +0 -33
  90. package/src/integration/npm-package.test.ts +0 -19
  91. package/src/integration/npm-package.testing.ts +0 -243
  92. package/src/integration/publication-read-changes.test.ts +0 -454
  93. package/src/integration/read-changes.test.ts +0 -98
  94. package/src/integration/replace-package.test.ts +0 -22
  95. package/src/integration/replace-package.testing.ts +0 -36
  96. package/src/integration/replace-worker-boundary.test.ts +0 -127
  97. package/src/integration/report-replay.test.ts +0 -91
  98. package/src/integration/utf8-errors.test.ts +0 -46
  99. package/src/pi/action-fusion.test.ts +0 -659
  100. package/src/pi/action-fusion.testing.ts +0 -38
  101. package/src/pi/anchor-format.test.ts +0 -124
  102. package/src/pi/argument-error.testing.ts +0 -46
  103. package/src/pi/argument-format.test.ts +0 -196
  104. package/src/pi/argument-validation.test.ts +0 -1007
  105. package/src/pi/codemode.test.ts +0 -101
  106. package/src/pi/config.test.ts +0 -204
  107. package/src/pi/diagnostic-buffer.test.ts +0 -115
  108. package/src/pi/directory-sync.test.ts +0 -57
  109. package/src/pi/execute.test.ts +0 -1257
  110. package/src/pi/extension-loading.test.ts +0 -14
  111. package/src/pi/extension-loading.testing.ts +0 -3
  112. package/src/pi/failure-context.test.ts +0 -370
  113. package/src/pi/failure-path.test.ts +0 -289
  114. package/src/pi/file-commit.test.ts +0 -628
  115. package/src/pi/file-read.test.ts +0 -29
  116. package/src/pi/file-snapshot.test.ts +0 -22
  117. package/src/pi/fusion-card-interaction.test.ts +0 -143
  118. package/src/pi/fusion-card.test.ts +0 -353
  119. package/src/pi/grep-candidates.test.ts +0 -26
  120. package/src/pi/grep-matrix.test.ts +0 -68
  121. package/src/pi/grep-order.test.ts +0 -447
  122. package/src/pi/grep-output.testing.ts +0 -24
  123. package/src/pi/grep-rg.testing.ts +0 -73
  124. package/src/pi/grep.test.ts +0 -1670
  125. package/src/pi/model-budgets.test.ts +0 -96
  126. package/src/pi/mutation-outcome.testing.ts +0 -20
  127. package/src/pi/mutation-result.test.ts +0 -96
  128. package/src/pi/output-budgets.test.ts +0 -221
  129. package/src/pi/pi.test.ts +0 -158
  130. package/src/pi/read-delegation.test.ts +0 -119
  131. package/src/pi/read.test.ts +0 -735
  132. package/src/pi/regex-worker.test.ts +0 -42
  133. package/src/pi/registration.test.ts +0 -549
  134. package/src/pi/render.test.ts +0 -841
  135. package/src/pi/replace.test.ts +0 -634
  136. package/src/pi/report-failure.testing.ts +0 -27
  137. package/src/pi/report.test.ts +0 -533
  138. package/src/pi/result-safety.test.ts +0 -597
  139. package/src/pi/rg-process.test.ts +0 -133
  140. package/src/pi/rg-regex.test.ts +0 -40
  141. package/src/pi/text-stream.test.ts +0 -281
  142. package/src/pi/tool-call.testing.ts +0 -61
  143. package/src/pi/tool-factories.testing.ts +0 -59
  144. package/src/pi/write.test.ts +0 -256
  145. package/src/testing/extension-loader.testing.ts +0 -24
  146. package/src/testing/installation.testing.ts +0 -77
  147. package/src/testing/process.testing.ts +0 -33
  148. package/src/testing/regex-host.testing.ts +0 -20
  149. package/src/testing/regex-translation-corpus.ts +0 -267
  150. package/src/testing/rg-line-listing.fixture.mjs +0 -7
  151. package/src/testing/run-tests.ts +0 -47
  152. package/src/testing/session.testing.ts +0 -127
@@ -1,1007 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import fs, { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
3
- import { syncBuiltinESMExports } from "node:module";
4
- import { tmpdir } from "node:os";
5
- import { join } from "node:path";
6
- import { test } from "node:test";
7
- import { createActionFusionExecutor } from "./action-fusion.ts";
8
- import { makeMockGrep } from "./grep-rg.testing.ts";
9
- import { commandCompletion } from "./action-fusion.testing.ts";
10
- import { callTool, createToolContext } from "./tool-call.testing.ts";
11
- import { argumentError, rejectsArgument } from "./argument-error.testing.ts";
12
- import { POSITIVE_SAFE_INTEGER } from "./schema.ts";
13
- import { reportOf } from "./report.ts";
14
- import { createAnchorFormatter } from "./anchor-format.ts";
15
- import { computeLineHash } from "../core/hash.ts";
16
-
17
- // Load candidate-dependent helpers only after grep-rg.testing registers the module mock.
18
- const { grepCounts } = await import("./grep-output.testing.ts");
19
-
20
- const {
21
- DEFAULT_CONFIG,
22
- makeReadOverride,
23
- makeGrepOverride,
24
- makeEditOverride,
25
- makeReplaceTool,
26
- makeWriteOverride,
27
- } = await import("./tool-factories.testing.ts");
28
-
29
- test('grep receives flags: "i" → rejects INVALID_ARGUMENTS before searching', async () => {
30
- let searches = 0;
31
- const discover = async () => {
32
- searches++;
33
- return { code: 1, stderr: "", stopped: false };
34
- };
35
- const tool = makeMockGrep(process.cwd(), DEFAULT_CONFIG, {
36
- runRgPaths: discover,
37
- runRgLines: discover,
38
- });
39
- await assert.rejects(
40
- callTool(tool, { pattern: ["text"], literal: true, flags: "i" }),
41
- (error: unknown) => {
42
- const report = argumentError(error);
43
- assert.equal(report.error, "INVALID_ARGUMENTS");
44
- assert.deepEqual(
45
- report.issues.map((issue) => issue.field),
46
- ["flags"],
47
- );
48
- assert.match(report.issues[0].reason, /not allowed/);
49
- return true;
50
- },
51
- );
52
- assert.equal(searches, 0);
53
- });
54
-
55
- const invalidCommand = { command: "", timeout: 0 };
56
- const anchorHash = "A".repeat(DEFAULT_CONFIG.hashLen);
57
- const cases = [
58
- {
59
- name: "read",
60
- make: (cwd: string, _fusion: ReturnType<typeof createActionFusionExecutor>) =>
61
- makeReadOverride(cwd, DEFAULT_CONFIG),
62
- args: { offset: -1, limit: 0 },
63
- fields: ["offset", "limit"],
64
- },
65
- {
66
- name: "grep",
67
- make: (cwd: string, _fusion: ReturnType<typeof createActionFusionExecutor>) =>
68
- makeGrepOverride(cwd, DEFAULT_CONFIG),
69
- args: { pattern: [], literal: true, limit: 0, context: -1 },
70
- fields: ["pattern", "limit", "context"],
71
- },
72
- {
73
- name: "edit",
74
- make: (cwd: string, fusion: ReturnType<typeof createActionFusionExecutor>) =>
75
- makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
76
- args: {
77
- steps: [
78
- { action: "replace", anchor: `1#${anchorHash}`, lines: [] },
79
- { action: "append", lines: ["bad\0", "bad\ud800"] },
80
- { action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, end: "1#A" },
81
- { action: "append", lines: [] },
82
- { action: "replace", anchor: `3#${anchorHash}`, end: `1#${anchorHash}`, lines: ["text"] },
83
- { action: "delete", anchor: `4#${anchorHash}`, end: `2#${anchorHash}` },
84
- ],
85
- then_run: invalidCommand,
86
- },
87
- fields: [
88
- "steps[0].lines",
89
- "steps[1].lines[0]",
90
- "steps[1].lines[1]",
91
- "steps[2].anchor",
92
- "steps[2].end",
93
- "steps[3].lines",
94
- "steps[4].end",
95
- "steps[5].end",
96
- "then_run.command",
97
- "then_run.timeout",
98
- ],
99
- reasons: [
100
- { field: "steps[4].end", reason: /line 1 is before anchor line 3/ },
101
- { field: "steps[5].end", reason: /line 2 is before anchor line 4/ },
102
- ],
103
- },
104
- {
105
- name: "replace",
106
- make: (cwd: string, fusion: ReturnType<typeof createActionFusionExecutor>) =>
107
- makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
108
- args: {
109
- replacements: [
110
- { find: "(", replace: "bad\0", literal: false },
111
- { find: "[", replace: "bad\ud800", literal: false },
112
- ],
113
- then_run: invalidCommand,
114
- },
115
- fields: [
116
- "replacements[0].replace",
117
- "replacements[0].find",
118
- "replacements[1].replace",
119
- "replacements[1].find",
120
- "then_run.command",
121
- "then_run.timeout",
122
- ],
123
- },
124
- {
125
- name: "write",
126
- make: (cwd: string, fusion: ReturnType<typeof createActionFusionExecutor>) =>
127
- makeWriteOverride(cwd, fusion),
128
- args: { content: "bad\0", mode: "invalid", then_run: invalidCommand },
129
- fields: ["content", "mode", "then_run.command", "then_run.timeout"],
130
- },
131
- ] as const;
132
-
133
- for (const scenario of cases) {
134
- test(`${scenario.name} receives independent argument errors → reports them together without effects`, async (t) => {
135
- const cwd = await mkdtemp(join(tmpdir(), "hashline-arguments-"));
136
- t.after(() => rm(cwd, { recursive: true, force: true }));
137
- const path = join(cwd, "target.txt");
138
- const original = "original\n";
139
- await writeFile(path, original);
140
- let commandRuns = 0;
141
- const fusion = createActionFusionExecutor(() => {});
142
- const ctx = {
143
- ...(await createToolContext(cwd, t)),
144
- executeTool: async () => {
145
- commandRuns++;
146
- return commandCompletion({ content: [{ type: "text", text: "ran" }], details: undefined });
147
- },
148
- };
149
- const tool = scenario.make(cwd, fusion);
150
- const args: unknown = JSON.parse(JSON.stringify({ path, ...scenario.args, unexpected: true }));
151
- const snapshot = structuredClone(args);
152
- const open = t.mock.method(fs, "open");
153
- syncBuiltinESMExports();
154
- try {
155
- await assert.rejects(callTool(tool, args, { ctx }), (error: unknown) => {
156
- const result = argumentError(error);
157
- assert.deepEqual(
158
- new Set(result.issues.map((issue) => issue.field)),
159
- new Set([...scenario.fields, "unexpected"]),
160
- );
161
- if ("reasons" in scenario) {
162
- for (const expected of scenario.reasons) {
163
- const issue = result.issues.find((issue) => issue.field === expected.field);
164
- assert.ok(issue);
165
- assert.match(issue.reason, expected.reason);
166
- }
167
- }
168
- return true;
169
- });
170
- assert.equal(open.mock.callCount(), 0);
171
- } finally {
172
- t.mock.restoreAll();
173
- syncBuiltinESMExports();
174
- }
175
- assert.deepEqual(args, snapshot);
176
- assert.equal(await readFile(path, "utf8"), original);
177
- assert.equal(commandRuns, 0);
178
- });
179
- }
180
-
181
- test("blank then_run command with Fusion → reports exactly one command issue", async (t) => {
182
- const cwd = process.cwd();
183
- const fusion = createActionFusionExecutor(() => {});
184
- const mutations = [
185
- {
186
- name: "edit",
187
- tool: makeEditOverride(cwd, DEFAULT_CONFIG, fusion),
188
- args: { steps: [{ action: "append", lines: ["text"] }] },
189
- },
190
- {
191
- name: "replace",
192
- tool: makeReplaceTool(cwd, DEFAULT_CONFIG, fusion),
193
- args: { replacements: [{ find: "old", replace: "new", literal: true }] },
194
- },
195
- {
196
- name: "write",
197
- tool: makeWriteOverride(cwd, fusion),
198
- args: { content: "text", mode: "create" },
199
- },
200
- ] as const;
201
- for (const scenario of mutations) {
202
- for (const command of ["", " "]) {
203
- await t.test(
204
- `${scenario.name}/${JSON.stringify(command)} → rejects one command issue`,
205
- async () => {
206
- await assert.rejects(
207
- callTool(scenario.tool, {
208
- path: "unused.txt",
209
- ...scenario.args,
210
- then_run: { command },
211
- }),
212
- (error: unknown) => {
213
- const result = argumentError(error);
214
- assert.equal(result.issues.length, 1, JSON.stringify(result));
215
- assert.equal(result.issues[0].field, "then_run.command");
216
- return true;
217
- },
218
- );
219
- },
220
- );
221
- }
222
- }
223
- });
224
-
225
- test("replace flags → accept V8 modes and reject invalid flags before patterns or file reads", async (t) => {
226
- const cwd = await mkdtemp(join(tmpdir(), "hashline-flags-"));
227
- t.after(() => rm(cwd, { recursive: true, force: true }));
228
- const path = join(cwd, "missing.txt");
229
- const tools = [
230
- {
231
- tool: makeReplaceTool(cwd, DEFAULT_CONFIG),
232
- args: (find: string, literal: boolean, flags: string) => ({
233
- replacements: [
234
- { literal: true, find: "valid", replace: "first" },
235
- { find, replace: "second", literal, flags },
236
- ],
237
- }),
238
- field: "replacements[1].flags",
239
- },
240
- ];
241
- for (const { tool, args, field } of tools) {
242
- for (const literal of [false, true]) {
243
- for (const flags of ["v", "uv", "x"]) {
244
- await t.test(`${tool.name}/${literal ? "literal" : "regex"}/${flags}`, async () => {
245
- const input = { path, ...args(flags === "v" ? "a" : "(", literal, flags) };
246
- if (flags === "v") {
247
- const result = await callTool(tool, input);
248
- assert.equal(reportOf(result, tool.name)?.error, "PATH_NOT_FOUND");
249
- } else {
250
- await assert.rejects(callTool(tool, input), (error: unknown) => {
251
- const report = argumentError(error);
252
- assert.equal(report.issues.length, 1);
253
- assert.equal(report.issues[0].field, field);
254
- assert.match(report.issues[0].reason, /Invalid flags supplied to RegExp constructor/);
255
- return true;
256
- });
257
- }
258
- });
259
- }
260
- }
261
- }
262
- });
263
-
264
- test("invalid regex pattern → explains literal recovery without changing the requested mode", async (t) => {
265
- const cwd = await mkdtemp(join(tmpdir(), "hashline-regex-recovery-"));
266
- t.after(() => rm(cwd, { recursive: true, force: true }));
267
- const path = join(cwd, "missing.txt");
268
- const cases = [
269
- {
270
- tool: makeGrepOverride(cwd, DEFAULT_CONFIG),
271
- args: (find: string, literal: boolean) => ({ pattern: [find], literal }),
272
- field: "pattern[0]",
273
- },
274
- {
275
- tool: makeReplaceTool(cwd, DEFAULT_CONFIG),
276
- args: (find: string, literal: boolean) => ({
277
- replacements: [{ find, literal, replace: "updated" }],
278
- }),
279
- field: "replacements[0].find",
280
- },
281
- ];
282
- for (const { tool, args, field } of cases) {
283
- await t.test(tool.name, async () => {
284
- const input = { path, ...args("call(", false) };
285
- const original = structuredClone(input);
286
- await assert.rejects(callTool(tool, input), (error: unknown) => {
287
- const { issues } = argumentError(error);
288
- assert.equal(issues.length, 1);
289
- assert.equal(issues[0].field, field);
290
- assert.match(issues[0].reason, /Invalid regular expression/);
291
- assert.match(issues[0].fix ?? "", /literal.*true.*otherwise.*regex/);
292
- return true;
293
- });
294
- assert.deepEqual(input, original);
295
- for (const corrected of [args("call(", true), args("call\\(", false)]) {
296
- const result = await callTool(tool, { path, ...corrected });
297
- assert.equal(reportOf(result, tool.name)?.error, "PATH_NOT_FOUND");
298
- }
299
- await assert.rejects(readFile(path), { code: "ENOENT" });
300
- });
301
- }
302
- });
303
-
304
- const editFields = ["anchor", "end", "lines"] as const;
305
- type EditField = (typeof editFields)[number];
306
- const fieldValues = {
307
- anchor: `1#${anchorHash}`,
308
- end: `2#${anchorHash}`,
309
- lines: ["text"],
310
- };
311
- const actionCases: readonly {
312
- action: string;
313
- required: readonly EditField[];
314
- optional: readonly EditField[];
315
- expected: string;
316
- }[] = [
317
- {
318
- action: "replace",
319
- required: ["anchor", "lines"],
320
- optional: ["end"],
321
- expected: "text\nbeta\ngamma\n",
322
- },
323
- { action: "delete", required: ["anchor"], optional: ["end"], expected: "beta\ngamma\n" },
324
- {
325
- action: "insert_after",
326
- required: ["anchor", "lines"],
327
- optional: [],
328
- expected: "alpha\ntext\nbeta\ngamma\n",
329
- },
330
- {
331
- action: "insert_before",
332
- required: ["anchor", "lines"],
333
- optional: [],
334
- expected: "text\nalpha\nbeta\ngamma\n",
335
- },
336
- { action: "append", required: ["lines"], optional: [], expected: "alpha\nbeta\ngamma\ntext\n" },
337
- { action: "prepend", required: ["lines"], optional: [], expected: "text\nalpha\nbeta\ngamma\n" },
338
- ];
339
-
340
- for (const scenario of actionCases) {
341
- test(`${scenario.action} field matrix → requires its fields and rejects fields it does not take`, async (t) => {
342
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
343
- const fields = schemaProperties(tool).steps?.items?.properties;
344
- const choice = fields?.action?.anyOf?.find((option) => option.const === scenario.action);
345
- assert.ok(choice?.description);
346
- const description = choice.description;
347
- const valid = {
348
- action: scenario.action,
349
- ...Object.fromEntries(scenario.required.map((field) => [field, fieldValues[field]])),
350
- };
351
- for (const field of scenario.required) {
352
- for (const value of [undefined, null]) {
353
- await t.test(
354
- `${field} is ${value === null ? "null" : "missing"} → reports the required field`,
355
- async () => {
356
- const edit: Record<string, unknown> = Object.fromEntries(
357
- Object.entries(valid).filter(([key]) => key !== field),
358
- );
359
- if (value === null) edit[field] = null;
360
- const args = { path: "unused.txt", steps: [edit] };
361
- await assert.rejects(callTool(tool, args), (error: unknown) => {
362
- assert.deepEqual(argumentError(error).issues, [
363
- {
364
- field: `steps[0].${field}`,
365
- reason: `is required for "${scenario.action}"`,
366
- fix: fields?.[field]?.description,
367
- },
368
- ]);
369
- return true;
370
- });
371
- },
372
- );
373
- }
374
- }
375
- for (const field of editFields.filter(
376
- (field) => !scenario.required.includes(field) && !scenario.optional.includes(field),
377
- )) {
378
- await t.test(`${field} is supplied → reports only that forbidden field`, async () => {
379
- await assert.rejects(
380
- callTool(tool, {
381
- path: "unused.txt",
382
- steps: [{ ...valid, [field]: fieldValues[field] }],
383
- }),
384
- (error: unknown) => {
385
- assert.deepEqual(argumentError(error).issues, [
386
- {
387
- field: `steps[0].${field}`,
388
- reason: "is not allowed",
389
- fix: `"${scenario.action}": ${description}`,
390
- },
391
- ]);
392
- assert.match(description, /Takes /);
393
- return true;
394
- },
395
- );
396
- });
397
- }
398
- });
399
- }
400
-
401
- test("edit action is missing → reports choices without guessing field permissions", async () => {
402
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
403
- const choices = schemaProperties(tool).steps?.items?.properties?.action?.anyOf ?? [];
404
- assert.deepEqual(
405
- choices.map((choice) => choice.const),
406
- actionCases.map(({ action }) => action),
407
- );
408
- await assert.rejects(
409
- callTool(tool, {
410
- path: "unused.txt",
411
- steps: [{ anchor: `1#${anchorHash}`, lines: ["valid"] }],
412
- }),
413
- (error: unknown) => {
414
- const { issues } = argumentError(error);
415
- assert.equal(issues.length, 1);
416
- const issue = issues[0];
417
- assert.equal(issue.field, "steps[0].action");
418
- assert.equal(issue.reason, "is required");
419
- for (const choice of choices) {
420
- assert.ok(choice.description);
421
- inOrder(issue.fix ?? "", [JSON.stringify(choice.const), choice.description]);
422
- }
423
- return true;
424
- },
425
- );
426
- });
427
-
428
- test("optional edit fields and then_run are null with Fusion → publishes bytes without rewriting caller arguments", async (t) => {
429
- const cwd = await mkdtemp(join(tmpdir(), "hashline-null-"));
430
- t.after(() => rm(cwd, { recursive: true, force: true }));
431
- const path = join(cwd, "target.txt");
432
- let commandRuns = 0;
433
- const fusion = createActionFusionExecutor(() => {});
434
- const ctx = {
435
- ...(await createToolContext(cwd, t)),
436
- executeTool: async () => {
437
- commandRuns++;
438
- return commandCompletion({ content: [{ type: "text", text: "ran" }], details: undefined });
439
- },
440
- };
441
- const tool = makeEditOverride(cwd, DEFAULT_CONFIG, fusion);
442
- for (const scenario of actionCases) {
443
- await t.test(`${scenario.action} → accepts null optional fields`, async () => {
444
- await writeFile(path, "alpha\nbeta\ngamma\n");
445
- const edit = {
446
- action: scenario.action,
447
- ...Object.fromEntries(editFields.map((field) => [field, null])),
448
- ...Object.fromEntries(
449
- scenario.required.map((field) => [
450
- field,
451
- field === "lines" ? ["text"] : token(1, "alpha"),
452
- ]),
453
- ),
454
- };
455
- const args = { path, steps: [edit], then_run: null };
456
- const snapshot = structuredClone(args);
457
- const result = await callTool(tool, args, { ctx });
458
- assert.equal(result.isError, undefined);
459
- assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
460
- assert.equal(reportOf(result, "edit")?.then_run, undefined);
461
- assert.deepEqual(await readFile(path), Buffer.from(scenario.expected));
462
- assert.deepEqual(args, snapshot);
463
- });
464
- }
465
- assert.equal(commandRuns, 0);
466
- });
467
-
468
- test("null optional fields → grep applies defaults without filename filters", async (t) => {
469
- const cwd = await mkdtemp(join(tmpdir(), "hashline-grep-null-"));
470
- t.after(() => rm(cwd, { recursive: true, force: true }));
471
- const path = join(cwd, "target.txt");
472
- const config = DEFAULT_CONFIG;
473
- await writeFile(path, "x\n".repeat(config.grep.defaultLimit + 1));
474
- const rgCalls: (readonly string[])[] = [];
475
- const discover = async (
476
- args: readonly string[],
477
- onPath: (path: string) => boolean | Promise<boolean>,
478
- ) => {
479
- rgCalls.push(args);
480
- await onPath(path);
481
- return { code: 0, stderr: "", stopped: false };
482
- };
483
- const tool = makeMockGrep(cwd, config, {
484
- runRgPaths: (args, _signal, onPath) => discover(args, onPath),
485
- runRgLines: (args, _signal, _maxLines, onFile) =>
486
- discover(args, (file) => onFile(file, undefined)),
487
- });
488
- const result = await callTool(tool, {
489
- pattern: "x",
490
- literal: true,
491
- glob: null,
492
- limit: null,
493
- context: null,
494
- });
495
- assert.equal(result.isError, undefined);
496
- assert.equal(grepCounts(result).matches, config.grep.defaultLimit);
497
- assert.equal(reportOf(result, "grep")?.matchLimit, config.grep.defaultLimit);
498
- assert.ok(rgCalls.length > 0);
499
- for (const args of rgCalls) {
500
- const globs = args.flatMap((arg, index) => (arg === "--glob" ? [args[index + 1]] : []));
501
- // The built-in .git exclusion remains; null adds no filename filter.
502
- assert.deepEqual(globs, ["!.git"]);
503
- }
504
- });
505
-
506
- test("edit batch has 64 or 65 operations → publishes the maximum or rejects the oversized batch unchanged", async (t) => {
507
- const cwd = await mkdtemp(join(tmpdir(), "hashline-edit-limit-"));
508
- t.after(() => rm(cwd, { recursive: true, force: true }));
509
- const path = join(cwd, "target.txt");
510
- const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
511
- const maxEdits: unknown = Reflect.get(tool.parameters.properties.steps, "maxItems");
512
- assert.ok(typeof maxEdits === "number");
513
- assert.equal(maxEdits, 64);
514
- const lines = Array.from({ length: maxEdits + 2 }, (_, i) => `line ${i + 1}`);
515
- const original = Buffer.from(`${lines.join("\n")}\n`);
516
- for (const count of [maxEdits, maxEdits + 1]) {
517
- await t.test(
518
- `${count} steps → ${count === maxEdits ? "publishes distinct deletions" : `rejects steps above max${maxEdits}`}`,
519
- async () => {
520
- await writeFile(path, original);
521
- const args = {
522
- path,
523
- steps: lines
524
- .slice(0, count)
525
- .map((line, i) => ({ action: "delete", anchor: token(i + 1, line) })),
526
- };
527
- const snapshot = structuredClone(args);
528
- if (count === maxEdits) {
529
- const result = await callTool(tool, args);
530
- assert.equal(result.isError, undefined);
531
- assert.equal(reportOf(result, "edit")?.publication, "PUBLISHED");
532
- assert.deepEqual(await readFile(path), Buffer.from(`${lines.slice(count).join("\n")}\n`));
533
- } else {
534
- await assert.rejects(callTool(tool, args), (error: unknown) => {
535
- const report = argumentError(error);
536
- assert.equal(report.issues.length, 1);
537
- assert.equal(report.issues[0].field, "steps");
538
- assert.match(
539
- report.issues[0].reason,
540
- new RegExp(`must not have more than ${maxEdits} items`),
541
- );
542
- return true;
543
- });
544
- assert.deepEqual(await readFile(path), original);
545
- }
546
- assert.deepEqual(args, snapshot);
547
- },
548
- );
549
- }
550
- });
551
-
552
- test("edit item is not an object → report the item type once", async () => {
553
- for (const item of [null, 42, "bad", [], true]) {
554
- await assert.rejects(
555
- callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
556
- path: "unused.txt",
557
- steps: [item],
558
- }),
559
- (error: unknown) => {
560
- const result = argumentError(error);
561
- assert.equal(result.issues.length, 1);
562
- return rejectsArgument("steps[0]", /object/)(error);
563
- },
564
- );
565
- }
566
- });
567
-
568
- test("grep union value has one applicable type → report only its constraint", async () => {
569
- for (const pattern of ["", [], [""]]) {
570
- await assert.rejects(
571
- callTool(makeGrepOverride(process.cwd(), DEFAULT_CONFIG), { pattern, literal: true }),
572
- (error: unknown) => {
573
- const result = argumentError(error);
574
- assert.equal(result.issues.length, 1);
575
- assert.match(result.issues[0].field, /^pattern(?:\[0\])?$/);
576
- assert.doesNotMatch(result.issues[0].reason, /anyOf/);
577
- return true;
578
- },
579
- );
580
- }
581
- });
582
-
583
- test("unknown field names contain punctuation, numeric keys or newlines → quote property keys without treating them as array indices", async () => {
584
- for (const key of ["odd.\nfield/~", "0", "01", "a/b", "a~b", 'quote"field']) {
585
- await assert.rejects(
586
- callTool(makeReadOverride(process.cwd(), DEFAULT_CONFIG), {
587
- path: "unused.txt",
588
- [key]: true,
589
- }),
590
- (error: unknown) => {
591
- const result = argumentError(error);
592
- assert.equal(result.issues.length, 1);
593
- assert.equal(result.issues[0].field, `[${JSON.stringify(key)}]`);
594
- return true;
595
- },
596
- );
597
- }
598
- });
599
-
600
- test("one field exhausts native diagnostics → label the limit and retain another field", async () => {
601
- await assert.rejects(
602
- callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
603
- path: "",
604
- steps: Array.from({ length: 30 }, () => ({ action: "unknown" })),
605
- }),
606
- (error: unknown) => {
607
- const result = argumentError(error);
608
- assert.ok(result.issues.some((issue) => issue.field === "path"));
609
- assert.ok(result.issues.some((issue) => issue.field === "steps[0].action"));
610
- assert.equal(result.schemaLimited, true);
611
- return true;
612
- },
613
- );
614
- });
615
-
616
- test("one field violates independent constraints → combine its reasons without losing either", async () => {
617
- await assert.rejects(
618
- callTool(makeEditOverride(process.cwd(), DEFAULT_CONFIG), {
619
- path: "unused.txt",
620
- steps: [{ action: "delete", anchor: `${Number.MAX_SAFE_INTEGER + 1}#A` }],
621
- }),
622
- (error: unknown) => {
623
- const result = argumentError(error);
624
- assert.equal(result.issues.length, 1);
625
- return rejectsArgument("steps[0].anchor", /safe integer[\s\S]*hash characters/)(error);
626
- },
627
- );
628
- await assert.rejects(
629
- callTool(makeReadOverride(process.cwd(), DEFAULT_CONFIG), { path: "unused.txt", offset: 0.5 }),
630
- (error: unknown) => {
631
- const result = argumentError(error);
632
- assert.equal(result.issues.length, 1);
633
- assert.match(
634
- result.issues[0].reason,
635
- new RegExp(`multiple of ${POSITIVE_SAFE_INTEGER.multipleOf}`),
636
- );
637
- assert.match(result.issues[0].reason, new RegExp(`>= ${POSITIVE_SAFE_INTEGER.minimum}`));
638
- return true;
639
- },
640
- );
641
- });
642
-
643
- test("edit receives a forbidden field with the wrong type → reports the field prohibition", async () => {
644
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
645
- await assert.rejects(
646
- callTool(tool, {
647
- path: "unused.txt",
648
- steps: [{ action: "append", lines: ["text"], anchor: {} }],
649
- }),
650
- (error: unknown) => {
651
- const { issues } = argumentError(error);
652
- assert.equal(issues.length, 1);
653
- assert.equal(issues[0].field, "steps[0].anchor");
654
- assert.equal(issues[0].reason, "is not allowed");
655
- assert.ok(issues[0].fix);
656
- return true;
657
- },
658
- );
659
- });
660
-
661
- test("edit fields contain semantic errors → checks only fields accepted by their action", async (t) => {
662
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
663
- for (const scenario of actionCases) {
664
- const valid = {
665
- action: scenario.action,
666
- ...Object.fromEntries(scenario.required.map((field) => [field, fieldValues[field]])),
667
- };
668
- for (const field of editFields) {
669
- const accepted = scenario.required.includes(field) || scenario.optional.includes(field);
670
- const values: readonly (readonly [unknown, RegExp])[] =
671
- field === "lines"
672
- ? [
673
- [[], /empty/],
674
- [["bad\0"], /NUL/],
675
- [["bad\ud800"], /surrogate|UTF-8/],
676
- ]
677
- : [
678
- [`${Number.MAX_SAFE_INTEGER + 1}#${anchorHash}`, /safe integer/],
679
- ["1#A", /hash characters/],
680
- ];
681
- for (const [value, reason] of values) {
682
- await t.test(
683
- `${scenario.action}/${field}/${reason.source} → ${accepted ? "reports value error" : "reports only forbidden field"}`,
684
- async () => {
685
- const edit = { ...valid, [field]: value };
686
- await assert.rejects(
687
- callTool(tool, { path: "unused.txt", steps: [edit] }),
688
- (error: unknown) => {
689
- const report = argumentError(error);
690
- assert.equal(report.issues.length, 1, JSON.stringify(report));
691
- const issue = report.issues[0];
692
- assert.equal(
693
- issue.field,
694
- `steps[0].${field}${accepted && field === "lines" && Array.isArray(value) && value.length ? "[0]" : ""}`,
695
- );
696
- if (accepted) {
697
- assert.match(issue.reason, reason);
698
- if (field === "lines" && Array.isArray(value) && value.length === 0) {
699
- assert.match(
700
- issue.fix ?? "",
701
- scenario.action === "replace"
702
- ? /"action":"delete"/
703
- : /supply at least one line/,
704
- );
705
- }
706
- } else {
707
- assert.equal(issue.reason, "is not allowed");
708
- assert.match(issue.fix ?? "", new RegExp(`^"${scenario.action}":.*Takes `));
709
- }
710
- return true;
711
- },
712
- );
713
- },
714
- );
715
- }
716
- }
717
- }
718
- });
719
-
720
- /** Tool schema properties, so expected fix text comes from the schema instead of the test. */
721
- interface SchemaNode {
722
- readonly description?: string;
723
- readonly const?: unknown;
724
- readonly anyOf?: readonly SchemaNode[];
725
- readonly properties?: Readonly<Record<string, SchemaNode>>;
726
- readonly items?: SchemaNode;
727
- }
728
-
729
- const schemaProperties = (tool: {
730
- readonly parameters: unknown;
731
- }): Readonly<Record<string, SchemaNode>> =>
732
- (tool.parameters as { properties: Readonly<Record<string, SchemaNode>> }).properties;
733
-
734
- const inOrder = (fix: string, pieces: readonly string[]): void => {
735
- let cursor = -1;
736
- for (const piece of pieces) {
737
- const at = fix.indexOf(piece);
738
- assert.ok(at > cursor, `missing or out of order: ${piece} in ${fix}`);
739
- cursor = at;
740
- }
741
- };
742
-
743
- test("missing required parameter → fix states the parameter's role from its schema description", async () => {
744
- let discoveries = 0;
745
- const discover = async () => {
746
- discoveries++;
747
- return { code: 1, stderr: "", stopped: false };
748
- };
749
- const grep = makeMockGrep(process.cwd(), DEFAULT_CONFIG, {
750
- runRgPaths: discover,
751
- runRgLines: discover,
752
- });
753
- const write = makeWriteOverride(process.cwd());
754
- const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
755
- const cases = [
756
- {
757
- name: "grep literal",
758
- tool: grep,
759
- args: { pattern: "text" },
760
- field: "literal",
761
- verify: (fix: string) => assert.equal(fix, schemaProperties(grep).literal?.description),
762
- },
763
- {
764
- name: "write mode",
765
- tool: write,
766
- args: { path: "unused.txt", content: "text" },
767
- field: "mode",
768
- verify: (fix: string) => {
769
- const [create, overwrite] = schemaProperties(write).mode?.anyOf ?? [];
770
- assert.ok(create?.description && overwrite?.description);
771
- inOrder(fix, ['"create"', create.description, '"overwrite"', overwrite.description]);
772
- },
773
- },
774
- ] as const;
775
- for (const scenario of cases) {
776
- await assert.rejects(callTool(scenario.tool, scenario.args), (error: unknown) => {
777
- const result = argumentError(error);
778
- const issue = result.issues.find((candidate) => candidate.field === scenario.field);
779
- assert.ok(issue, JSON.stringify(result.issues));
780
- assert.match(issue.reason, /is required/);
781
- assert.ok(issue.fix, JSON.stringify(result.issues));
782
- scenario.verify(issue.fix);
783
- return true;
784
- });
785
- }
786
- assert.equal(discoveries, 0);
787
- });
788
-
789
- test("two operations miss the same required field → the shared fix appears once", async () => {
790
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
791
- await assert.rejects(
792
- callTool(tool, { path: "unused.txt", steps: [{ action: "replace" }, { action: "replace" }] }),
793
- (error: unknown) => {
794
- const result = argumentError(error);
795
- const anchors = result.issues.filter((issue) => issue.field.endsWith(".anchor"));
796
- assert.equal(anchors.length, 2);
797
- assert.equal(anchors.filter((issue) => issue.fix).length, 1);
798
- const fixes = result.issues.flatMap((issue) => (issue.fix ? [issue.fix] : []));
799
- assert.equal(new Set(fixes).size, fixes.length, JSON.stringify(fixes));
800
- const description = schemaProperties(tool).steps?.items?.properties?.anchor?.description;
801
- assert.ok(description);
802
- assert.ok(fixes.includes(description));
803
- return true;
804
- },
805
- );
806
- });
807
-
808
- test("out-of-choice value → reason lists values and fix explains each choice", async (t) => {
809
- const cwd = process.cwd();
810
- const cases = [
811
- {
812
- tool: makeEditOverride(cwd, DEFAULT_CONFIG),
813
- field: "steps[0].action",
814
- values: ["unknown", null, 42, {}],
815
- args: (action: unknown) => ({
816
- path: "unused.txt",
817
- steps: [{ action, anchor: `1#${anchorHash}`, lines: ["valid"] }],
818
- }),
819
- choices: [
820
- ["replace", "Replace the cited line(s)"],
821
- ["delete", "Delete the cited line(s)"],
822
- ["insert_after", "Insert lines after the anchor line"],
823
- ["insert_before", "Insert lines before the anchor line"],
824
- ["append", "Add lines at the end"],
825
- ["prepend", "Add lines at the start"],
826
- ],
827
- },
828
- {
829
- tool: makeWriteOverride(cwd),
830
- field: "mode",
831
- values: ["unknown"],
832
- args: (mode: unknown) => ({ path: "unused.txt", content: "ok", mode }),
833
- choices: [
834
- ["create", "Fail if the target already exists."],
835
- ["overwrite", "Fail if the target does not exist."],
836
- ],
837
- },
838
- {
839
- tool: makeGrepOverride(cwd, DEFAULT_CONFIG),
840
- field: "outputMode",
841
- values: ["unknown"],
842
- args: (outputMode: unknown) => ({ pattern: "text", literal: true, outputMode }),
843
- choices: [
844
- ["content", "Matches and context", "anchors", "plain previews"],
845
- ["files", "Paths of the files", "only paths"],
846
- ["count", "Matching lines per file", "total", "only counts"],
847
- ],
848
- },
849
- ] as const;
850
- for (const scenario of cases) {
851
- await t.test(`${scenario.tool.name}/${scenario.field}`, async () => {
852
- for (const value of scenario.values) {
853
- await assert.rejects(callTool(scenario.tool, scenario.args(value)), (error: unknown) => {
854
- const { issues } = argumentError(error);
855
- assert.equal(issues.length, 1, JSON.stringify(issues));
856
- const issue = issues[0];
857
- assert.equal(issue.field, scenario.field);
858
- assert.equal(
859
- issue.reason,
860
- `expected one of: ${scenario.choices.map(([value]) => JSON.stringify(value)).join(", ")}`,
861
- );
862
- assert.ok(issue.fix, JSON.stringify(issues));
863
- inOrder(
864
- issue.fix,
865
- scenario.choices.flatMap(([value, ...meaning]) => [JSON.stringify(value), ...meaning]),
866
- );
867
- return true;
868
- });
869
- }
870
- });
871
- }
872
- });
873
-
874
- const acceptedFields = (shape: SchemaNode): string =>
875
- `Accepted fields: ${Object.keys(shape.properties ?? {}).join(", ")}.`;
876
-
877
- test("unknown field is supplied → fix lists only the flat object's accepted fields", async () => {
878
- const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
879
- const grep = makeGrepOverride(process.cwd(), DEFAULT_CONFIG);
880
- for (const scenario of [
881
- {
882
- tool: edit,
883
- args: { path: "unused.txt", steps: [{ action: "append", lines: ["text"], op: "append" }] },
884
- field: "steps[0].op",
885
- fix: "Accepted fields: action, anchor, end, lines.",
886
- },
887
- {
888
- tool: grep,
889
- args: { pattern: "text", literal: true, bogus: 1 },
890
- field: "bogus",
891
- fix: acceptedFields({ properties: schemaProperties(grep) }),
892
- },
893
- ]) {
894
- await assert.rejects(callTool(scenario.tool, scenario.args), (error: unknown) => {
895
- assert.deepEqual(argumentError(error).issues, [
896
- { field: scenario.field, reason: "is not allowed", fix: scenario.fix },
897
- ]);
898
- return true;
899
- });
900
- }
901
- });
902
-
903
- test("value mismatches the field pattern → fix states the field description from the schema", async () => {
904
- const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
905
- const fields = schemaProperties(edit).steps?.items?.properties;
906
- const cases = [
907
- {
908
- name: "line element with a newline",
909
- args: { path: "unused.txt", steps: [{ action: "append", lines: ["a\nb"] }] },
910
- field: "steps[0].lines[0]",
911
- fix: fields?.lines?.description,
912
- },
913
- {
914
- name: "anchor without the line-hash shape",
915
- args: { path: "unused.txt", steps: [{ action: "replace", anchor: "abc", lines: ["text"] }] },
916
- field: "steps[0].anchor",
917
- fix: fields?.anchor?.description,
918
- },
919
- ] as const;
920
- for (const scenario of cases) {
921
- await assert.rejects(callTool(edit, scenario.args), (error: unknown) => {
922
- const result = argumentError(error);
923
- const issue = result.issues.find((candidate) => candidate.field === scenario.field);
924
- assert.ok(issue, JSON.stringify(result.issues));
925
- assert.match(issue.reason, /^must match pattern/);
926
- assert.equal(issue.fix, scenario.fix, JSON.stringify(result.issues));
927
- return true;
928
- });
929
- }
930
- });
931
-
932
- test("two unknown fields in one object → the shared fix appears once", async () => {
933
- const edit = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
934
- await assert.rejects(
935
- callTool(edit, {
936
- path: "unused.txt",
937
- steps: [{ action: "append", lines: ["text"], zzz: 1, yyy: 2 }],
938
- }),
939
- (error: unknown) => {
940
- const result = argumentError(error);
941
- assert.equal(result.issues.length, 2, JSON.stringify(result.issues));
942
- assert.ok(result.issues.every((issue) => issue.reason === "is not allowed"));
943
- assert.ok(result.issues[0].fix, JSON.stringify(result.issues));
944
- assert.equal(result.issues[1].fix, undefined);
945
- return true;
946
- },
947
- );
948
- });
949
-
950
- test("value has the wrong type → the reason carries no fix", async () => {
951
- await assert.rejects(
952
- callTool(makeReadOverride(process.cwd(), DEFAULT_CONFIG), { path: "unused.txt", limit: "abc" }),
953
- (error: unknown) => {
954
- const result = argumentError(error);
955
- assert.equal(result.issues.length, 1);
956
- assert.equal(result.issues[0].field, "limit");
957
- assert.match(result.issues[0].reason, /must be number/);
958
- assert.equal(result.issues[0].fix, undefined);
959
- return true;
960
- },
961
- );
962
- });
963
-
964
- function token(line: number, content: string): string {
965
- const hashLen = DEFAULT_CONFIG.hashLen;
966
- return createAnchorFormatter(hashLen).reference(line, computeLineHash(line, content, hashLen));
967
- }
968
-
969
- test("20 unknown keys including a slash → bounded issues retain one accepted-fields fix", async () => {
970
- const tool = makeReadOverride(process.cwd(), DEFAULT_CONFIG);
971
- const keys = Array.from({ length: 20 }, (_, index) =>
972
- index === 0 ? "unknown/key" : `unknown${index}`,
973
- );
974
- await assert.rejects(
975
- callTool(tool, { path: "unused.txt", ...Object.fromEntries(keys.map((key) => [key, true])) }),
976
- (error: unknown) => {
977
- const result = argumentError(error);
978
- assert.equal(result.schemaLimited, true);
979
- assert.ok(result.issues.length >= 8);
980
- assert.ok(result.issues.some((issue) => issue.field.includes("unknown/key")));
981
- for (const issue of result.issues) {
982
- assert.equal(issue.reason, "is not allowed");
983
- }
984
- assert.equal(result.issues[0].fix, acceptedFields({ properties: schemaProperties(tool) }));
985
- assert.ok(result.issues.slice(1).every((issue) => issue.fix === undefined));
986
- return true;
987
- },
988
- );
989
- });
990
-
991
- test("bounded additional-property diagnostics → preserve exact keys despite pointer-like spelling", async () => {
992
- const extras = Object.fromEntries([
993
- ...["a/b", "a", "a~1b"].map((key) => [key, true]),
994
- ...Array.from({ length: 20 }, (_, index) => [`extra${index}`, true]),
995
- ]);
996
- const tool = makeEditOverride(process.cwd(), DEFAULT_CONFIG);
997
- await assert.rejects(
998
- callTool(tool, { path: "unused.txt", steps: [{ action: "append", lines: ["ok"], ...extras }] }),
999
- (error: unknown) => {
1000
- const report = argumentError(error);
1001
- assert.equal(report.schemaLimited, true);
1002
- const fields = report.issues.map((issue) => issue.field);
1003
- assert.deepEqual(fields.slice(0, 3), ['steps[0]["a/b"]', "steps[0].a", 'steps[0]["a~1b"]']);
1004
- return true;
1005
- },
1006
- );
1007
- });