@criogaid/pi-hashline-edit 0.6.1 → 0.7.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 (148) hide show
  1. package/README.md +161 -304
  2. package/THIRD_PARTY_NOTICES.md +80 -0
  3. package/package.json +10 -6
  4. package/src/core/AGENTS.md +20 -0
  5. package/src/core/apply.test.ts +189 -60
  6. package/src/core/apply.ts +110 -74
  7. package/src/core/case-fold.test.ts +40 -0
  8. package/src/core/case-fold.ts +81 -0
  9. package/src/core/character-set.test.ts +103 -0
  10. package/src/core/character-set.ts +170 -0
  11. package/src/core/errors.test.ts +23 -0
  12. package/src/core/errors.ts +70 -4
  13. package/src/core/lines.ts +7 -0
  14. package/src/core/regex.ts +21 -0
  15. package/src/core/replace-schema.ts +34 -0
  16. package/src/core/replace.ts +37 -54
  17. package/src/core/search-analysis.test.ts +199 -0
  18. package/src/core/search-analysis.ts +375 -0
  19. package/src/core/search-matrix.test.ts +64 -0
  20. package/src/core/search-matrix.testing.ts +183 -0
  21. package/src/core/search-schema.ts +18 -0
  22. package/src/core/search.test.ts +145 -0
  23. package/src/core/search.ts +119 -0
  24. package/src/core/text.test.ts +16 -8
  25. package/src/core/text.ts +26 -6
  26. package/src/core/types.ts +16 -11
  27. package/src/index.ts +25 -15
  28. package/src/integration/config-registration.test.ts +30 -24
  29. package/src/integration/forget-grep.test.ts +52 -2
  30. package/src/integration/fusion-dispatch.test.ts +21 -105
  31. package/src/integration/grep-errors.test.ts +50 -0
  32. package/src/integration/grep-flags.test.ts +280 -0
  33. package/src/integration/grep-js-completeness.test.ts +208 -0
  34. package/src/integration/grep-line-candidates.test.ts +233 -0
  35. package/src/integration/grep-matrix.test.ts +121 -0
  36. package/src/integration/grep-path-matrix.test.ts +132 -0
  37. package/src/integration/grep-prefilter-coverage.test.ts +189 -0
  38. package/src/integration/grep-regex.test.ts +410 -0
  39. package/src/integration/grep-rg.test.ts +220 -402
  40. package/src/integration/grep-scan-budget.test.ts +89 -0
  41. package/src/integration/grep-translation.test.ts +206 -0
  42. package/src/integration/large-text.test.ts +57 -1
  43. package/src/integration/model-budget-lifecycle.test.ts +128 -0
  44. package/src/integration/mutation-file-types.test.ts +12 -25
  45. package/src/integration/mutation-file-types.testing.ts +33 -0
  46. package/src/integration/npm-package.test.ts +17 -0
  47. package/src/integration/npm-package.testing.ts +123 -0
  48. package/src/integration/publication-read-changes.test.ts +344 -0
  49. package/src/integration/read-changes.test.ts +98 -0
  50. package/src/integration/replace-package.test.ts +17 -36
  51. package/src/integration/replace-package.testing.ts +33 -0
  52. package/src/integration/replace-worker-boundary.test.ts +107 -0
  53. package/src/integration/report-replay.test.ts +90 -0
  54. package/src/integration/utf8-errors.test.ts +46 -0
  55. package/src/pi/AGENTS.md +55 -0
  56. package/src/pi/action-fusion.test.ts +253 -103
  57. package/src/pi/action-fusion.ts +116 -290
  58. package/src/pi/anchor-format.ts +9 -0
  59. package/src/pi/argument-diagnostics.ts +478 -0
  60. package/src/pi/argument-error.testing.ts +43 -0
  61. package/src/pi/argument-error.ts +78 -0
  62. package/src/pi/argument-format.test.ts +208 -0
  63. package/src/pi/argument-validation.test.ts +742 -45
  64. package/src/pi/argument-validation.ts +55 -57
  65. package/src/pi/budgets.ts +45 -16
  66. package/src/pi/codemode.test.ts +96 -0
  67. package/src/pi/config.test.ts +14 -10
  68. package/src/pi/config.ts +21 -7
  69. package/src/pi/diagnostic-buffer.test.ts +52 -16
  70. package/src/pi/diagnostic-buffer.ts +11 -14
  71. package/src/pi/directory-sync.test.ts +4 -1
  72. package/src/pi/edit-tool.ts +100 -93
  73. package/src/pi/error-text.ts +39 -16
  74. package/src/pi/execute.test.ts +362 -315
  75. package/src/pi/extension-loading.test.ts +7 -27
  76. package/src/pi/extension-loading.testing.ts +2 -14
  77. package/src/pi/failure-context.test.ts +220 -88
  78. package/src/pi/failure-context.ts +161 -180
  79. package/src/pi/failure-path.test.ts +85 -88
  80. package/src/pi/file-commit.test.ts +78 -45
  81. package/src/pi/file-commit.ts +218 -151
  82. package/src/pi/file-read.test.ts +29 -0
  83. package/src/pi/file-read.ts +60 -0
  84. package/src/pi/file-snapshot.test.ts +1 -1
  85. package/src/pi/forget-tool.ts +189 -90
  86. package/src/pi/forget.test.ts +309 -22
  87. package/src/pi/forget.testing.ts +41 -113
  88. package/src/pi/fusion-card.test.ts +40 -14
  89. package/src/pi/fusion-card.ts +84 -50
  90. package/src/pi/grep-candidates.test.ts +127 -0
  91. package/src/pi/grep-candidates.ts +193 -0
  92. package/src/pi/grep-matrix.test.ts +83 -0
  93. package/src/pi/grep-output.ts +181 -138
  94. package/src/pi/grep-render.ts +28 -25
  95. package/src/pi/grep-scope.ts +61 -53
  96. package/src/pi/grep-search.ts +299 -242
  97. package/src/pi/grep-tool.ts +150 -140
  98. package/src/pi/grep-worker.mjs +4 -0
  99. package/src/pi/grep-worker.ts +14 -0
  100. package/src/pi/grep.test.ts +631 -632
  101. package/src/pi/model-budgets.test.ts +43 -0
  102. package/src/pi/model-budgets.ts +44 -0
  103. package/src/pi/mutation-outcome.testing.ts +5 -5
  104. package/src/pi/mutation-result.test.ts +21 -74
  105. package/src/pi/mutation-result.ts +74 -87
  106. package/src/pi/mutation-runner.ts +83 -75
  107. package/src/pi/output-budgets.test.ts +166 -0
  108. package/src/pi/path.ts +7 -0
  109. package/src/pi/read-tool.ts +151 -137
  110. package/src/pi/read.test.ts +39 -33
  111. package/src/pi/regex-worker-contract.ts +73 -0
  112. package/src/pi/regex-worker.ts +101 -0
  113. package/src/pi/registration.test.ts +111 -92
  114. package/src/pi/render.test.ts +193 -19
  115. package/src/pi/render.ts +137 -29
  116. package/src/pi/replace-regex.ts +11 -44
  117. package/src/pi/replace-tool.ts +38 -145
  118. package/src/pi/replace-worker-contract.ts +19 -0
  119. package/src/pi/replace-worker.ts +12 -8
  120. package/src/pi/replace.test.ts +93 -160
  121. package/src/pi/report-cause.ts +99 -0
  122. package/src/pi/report-failure.testing.ts +20 -0
  123. package/src/pi/report-schema.ts +512 -0
  124. package/src/pi/report.test.ts +276 -0
  125. package/src/pi/report.ts +288 -0
  126. package/src/pi/result-safety.test.ts +122 -427
  127. package/src/pi/rg-process.test.ts +78 -79
  128. package/src/pi/rg-process.ts +124 -177
  129. package/src/pi/rg-regex.test.ts +50 -0
  130. package/src/pi/rg-regex.ts +709 -0
  131. package/src/pi/text-stream.test.ts +67 -38
  132. package/src/pi/text-stream.ts +96 -75
  133. package/src/pi/tool-call.testing.ts +3 -39
  134. package/src/pi/write-tool.ts +45 -34
  135. package/src/pi/write.test.ts +45 -37
  136. package/src/testing/extension-loader.testing.ts +24 -0
  137. package/src/testing/installation.testing.ts +77 -0
  138. package/src/testing/process.testing.ts +33 -0
  139. package/src/testing/regex-host.testing.ts +20 -0
  140. package/src/testing/regex-translation-corpus.ts +308 -0
  141. package/src/testing/rg-line-listing.fixture.mjs +7 -0
  142. package/src/testing/run-tests.ts +47 -0
  143. package/src/testing/session.testing.ts +127 -0
  144. package/src/pi/rg-line-ranges.test.ts +0 -66
  145. package/src/pi/rg-line-ranges.ts +0 -75
  146. package/src/pi/rg-match-bytes.ts +0 -95
  147. package/src/pi/rg-text-view.ts +0 -192
  148. package/src/pi/tool-prompts.ts +0 -15
@@ -0,0 +1,208 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+ import { openTestSession } from "../testing/session.testing.ts";
4
+ import { registerForgetTool } from "./forget-tool.ts";
5
+ import { makeEditOverride } from "./edit-tool.ts";
6
+ import { makeReadOverride } from "./read-tool.ts";
7
+ import { makeGrepOverride } from "./grep-tool.ts";
8
+ import { makeWriteOverride } from "./write-tool.ts";
9
+ import { makeReplaceTool } from "./replace-tool.ts";
10
+ import { DEFAULT_CONFIG } from "./config.ts";
11
+ import { DEFAULT_OUTPUT_BUDGETS } from "./budgets.ts";
12
+ import { callTool } from "./tool-call.testing.ts";
13
+ import { argumentError } from "./argument-error.testing.ts";
14
+
15
+ const cwd = process.cwd();
16
+
17
+ test("all six tools receive invalid arguments → return field diagnostics without an argument copy or execution", async (t) => {
18
+ const { session } = await openTestSession(t, {
19
+ tools: ["forget"],
20
+ configure(pi) {
21
+ registerForgetTool(pi);
22
+ },
23
+ });
24
+ const forget = session.getToolDefinition("forget");
25
+ assert.ok(forget);
26
+ const cases = [
27
+ {
28
+ tool: makeEditOverride(cwd, DEFAULT_CONFIG),
29
+ args: { path: "unused", edits: [{ op: "append", body: [] }] },
30
+ },
31
+ { tool: makeReadOverride(cwd, DEFAULT_CONFIG), args: { path: "unused", limit: 0 } },
32
+ { tool: makeGrepOverride(cwd, DEFAULT_CONFIG), args: { pattern: "", literal: true } },
33
+ { tool: makeWriteOverride(cwd), args: { path: "unused", content: "bad\0", mode: "create" } },
34
+ {
35
+ tool: makeReplaceTool(cwd, DEFAULT_CONFIG),
36
+ args: { path: "unused", replacements: [{ find: "a", replace: "bad\0" }] },
37
+ },
38
+ { tool: forget, args: { ids: [] } },
39
+ ];
40
+ for (const { tool, args } of cases) {
41
+ let executed = false;
42
+ await assert.rejects(
43
+ callTool(
44
+ {
45
+ ...tool,
46
+ execute: async () => {
47
+ executed = true;
48
+ return { content: [] };
49
+ },
50
+ },
51
+ args,
52
+ ),
53
+ (error: unknown) => {
54
+ const result = argumentError(error);
55
+ assert.equal(result.tool, tool.name);
56
+ assert.ok(result.issues.length > 0);
57
+ assert.equal(Object.hasOwn(result, "arguments"), false);
58
+ assert.equal(Object.hasOwn(result, "argumentsOmitted"), false);
59
+ return true;
60
+ },
61
+ );
62
+ assert.equal(executed, false);
63
+ }
64
+ });
65
+
66
+ test("Pi prepares a singleton operation → semantic and schema paths resolve to the prepared array", async () => {
67
+ const tool = makeEditOverride(cwd, DEFAULT_CONFIG);
68
+ await assert.rejects(
69
+ callTool(tool, { path: "unused", edits: { op: "insert_after", body: [] } }),
70
+ (error: unknown) => {
71
+ const result = argumentError(error);
72
+ assert.deepEqual(
73
+ result.issues.map((issue) => issue.field),
74
+ ["edits[0].body", "edits[0].anchor"],
75
+ );
76
+ return true;
77
+ },
78
+ );
79
+ await assert.rejects(
80
+ callTool(makeReadOverride(cwd, DEFAULT_CONFIG), { path: "unused", offset: "1", limit: 0 }),
81
+ (error: unknown) => {
82
+ assert.deepEqual(
83
+ argumentError(error).issues.map((issue) => issue.field),
84
+ ["limit"],
85
+ );
86
+ return true;
87
+ },
88
+ );
89
+ });
90
+
91
+ test("JSON diagnostics exceed the budget → retain whole opening and closing issues and label omissions", async () => {
92
+ const edits = Array.from({ length: DEFAULT_OUTPUT_BUDGETS.blockBytes }, () => ({
93
+ op: "append",
94
+ body: [],
95
+ }));
96
+ await assert.rejects(
97
+ callTool(makeEditOverride(cwd, DEFAULT_CONFIG), { path: "", edits }),
98
+ (error: unknown) => {
99
+ const result = argumentError(error);
100
+ assert.ok(
101
+ error instanceof Error &&
102
+ Buffer.byteLength(error.message) <= DEFAULT_OUTPUT_BUDGETS.blockBytes,
103
+ );
104
+ assert.equal(result.issues[0].field, "edits[0].body");
105
+ assert.equal(result.issues.at(-1)?.field, "path");
106
+ assert.ok((result.omittedIssues ?? 0) > 0);
107
+ assert.equal(result.issues.length + (result.omittedIssues ?? 0), edits.length + 1);
108
+ return true;
109
+ },
110
+ );
111
+ });
112
+
113
+ test("a reason contains an oversized anchor → keep its cause and recovery instruction inside valid JSON", async () => {
114
+ const anchor = `${"9".repeat(DEFAULT_OUTPUT_BUDGETS.blockBytes)}#${"A".repeat(DEFAULT_CONFIG.hashLen)}`;
115
+ await assert.rejects(
116
+ callTool(makeEditOverride(cwd, DEFAULT_CONFIG), {
117
+ path: "unused",
118
+ edits: [{ op: "delete", anchor }],
119
+ }),
120
+ (error: unknown) => {
121
+ const result = argumentError(error);
122
+ assert.ok(
123
+ error instanceof Error &&
124
+ Buffer.byteLength(error.message) <= DEFAULT_OUTPUT_BUDGETS.blockBytes,
125
+ );
126
+ assert.equal(result.issues.length, 1);
127
+ assert.equal(result.issues[0].field, "edits[0].anchor");
128
+ assert.match(result.issues[0].reason, /^line number/);
129
+ assert.match(result.issues[0].fix ?? "", /Copy a complete.*latest tool result/);
130
+ assert.doesNotMatch(result.issues[0].reason, /Read|grep|Copy/);
131
+ return true;
132
+ },
133
+ );
134
+ });
135
+
136
+ test("cyclic arguments → retain the field diagnostic", async () => {
137
+ const args: { path: string; self?: unknown } = { path: "unused" };
138
+ args.self = args;
139
+ await assert.rejects(callTool(makeReadOverride(cwd, DEFAULT_CONFIG), args), (error: unknown) => {
140
+ const result = argumentError(error);
141
+ assert.equal(result.issues[0].field, "self");
142
+ assert.match(result.issues[0].reason, /not allowed/);
143
+ return true;
144
+ });
145
+ });
146
+
147
+ test("Pi cannot clone an argument → report the original cause in the same JSON envelope", async () => {
148
+ await assert.rejects(
149
+ callTool(makeReadOverride(cwd, DEFAULT_CONFIG), { path: "unused", extra: () => undefined }),
150
+ (error: unknown) => {
151
+ const result = argumentError(error);
152
+ assert.equal(result.issues[0].field, "$");
153
+ assert.match(result.issues[0].reason, /clone/i);
154
+ return true;
155
+ },
156
+ );
157
+ });
158
+
159
+ test("a non-finite numeric argument → reports its field without JSON encoding the value", async () => {
160
+ await assert.rejects(
161
+ callTool(makeReadOverride(cwd, DEFAULT_CONFIG), { path: "unused", limit: Infinity }),
162
+ (error: unknown) => {
163
+ const result = argumentError(error);
164
+ assert.equal(result.issues[0].field, "limit");
165
+ return true;
166
+ },
167
+ );
168
+ });
169
+
170
+ test("a property path exceeds the JSON budget → omit the whole issue and keep another field", async () => {
171
+ await assert.rejects(
172
+ callTool(makeReadOverride(cwd, DEFAULT_CONFIG), {
173
+ path: "",
174
+ ['"中🙂'.repeat(DEFAULT_OUTPUT_BUDGETS.blockBytes)]: true,
175
+ }),
176
+ (error: unknown) => {
177
+ const result = argumentError(error);
178
+ assert.ok(
179
+ error instanceof Error &&
180
+ Buffer.byteLength(error.message) <= DEFAULT_OUTPUT_BUDGETS.blockBytes,
181
+ );
182
+ assert.equal(result.issues.length, 1);
183
+ assert.equal(result.issues[0].field, "path");
184
+ assert.equal(result.omittedIssues, 1);
185
+ return true;
186
+ },
187
+ );
188
+ });
189
+
190
+ test("a large valid argument → leaves the diagnostic budget for invalid fields", async () => {
191
+ await assert.rejects(
192
+ callTool(makeReadOverride(cwd, DEFAULT_CONFIG), {
193
+ path: '"中🙂'.repeat(DEFAULT_OUTPUT_BUDGETS.blockBytes),
194
+ limit: 0,
195
+ }),
196
+ (error: unknown) => {
197
+ const result = argumentError(error);
198
+ assert.ok(
199
+ error instanceof Error &&
200
+ Buffer.byteLength(error.message) <= DEFAULT_OUTPUT_BUDGETS.blockBytes,
201
+ );
202
+ assert.equal(result.issues.length, 1);
203
+ assert.equal(result.issues[0].field, "limit");
204
+ assert.equal(result.omittedIssues, undefined);
205
+ return true;
206
+ },
207
+ );
208
+ });