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