@criogaid/pi-hashline-edit 0.9.0 → 0.10.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 (110) hide show
  1. package/README.md +9 -10
  2. package/README.zh-CN.md +8 -9
  3. package/THIRD_PARTY_NOTICES.md +0 -6
  4. package/package.json +1 -1
  5. package/src/core/AGENTS.md +4 -5
  6. package/src/core/apply.test.ts +96 -271
  7. package/src/core/apply.ts +62 -169
  8. package/src/core/case-fold.ts +8 -67
  9. package/src/core/character-set.ts +4 -4
  10. package/src/core/errors.test.ts +23 -7
  11. package/src/core/errors.ts +32 -5
  12. package/src/core/hash.test.ts +8 -16
  13. package/src/core/hash.ts +3 -23
  14. package/src/core/lines.ts +2 -4
  15. package/src/core/regex.ts +3 -4
  16. package/src/core/replace.test.ts +47 -1
  17. package/src/core/replace.ts +19 -13
  18. package/src/core/search-analysis.test.ts +24 -27
  19. package/src/core/search-analysis.ts +34 -65
  20. package/src/core/search-matrix.testing.ts +0 -2
  21. package/src/core/search.ts +1 -1
  22. package/src/core/text.test.ts +5 -5
  23. package/src/core/text.ts +5 -12
  24. package/src/core/types.ts +7 -43
  25. package/src/integration/fusion-dispatch.test.ts +2 -5
  26. package/src/integration/grep-flags.test.ts +0 -14
  27. package/src/integration/grep-js-completeness.test.ts +34 -8
  28. package/src/integration/grep-line-candidates.test.ts +4 -8
  29. package/src/integration/grep-matrix.test.ts +3 -7
  30. package/src/integration/grep-path-matrix.test.ts +5 -5
  31. package/src/integration/grep-prefilter-coverage.test.ts +3 -7
  32. package/src/integration/grep-process-limits.test.ts +78 -0
  33. package/src/integration/grep-rg.test.ts +118 -20
  34. package/src/integration/grep-scan-budget.test.ts +2 -2
  35. package/src/integration/grep-translation.test.ts +12 -27
  36. package/src/integration/large-text.test.ts +1 -47
  37. package/src/integration/model-budget-lifecycle.test.ts +2 -1
  38. package/src/integration/publication-read-changes.test.ts +123 -45
  39. package/src/integration/replace-worker-boundary.test.ts +21 -11
  40. package/src/integration/report-replay.test.ts +20 -22
  41. package/src/pi/AGENTS.md +15 -15
  42. package/src/pi/action-fusion.test.ts +164 -296
  43. package/src/pi/action-fusion.ts +148 -111
  44. package/src/pi/anchor-format.test.ts +9 -9
  45. package/src/pi/anchor-format.ts +6 -8
  46. package/src/pi/argument-diagnostics.ts +21 -25
  47. package/src/pi/argument-error.ts +2 -1
  48. package/src/pi/argument-validation.test.ts +129 -157
  49. package/src/pi/argument-validation.ts +5 -8
  50. package/src/pi/budgets.ts +5 -4
  51. package/src/pi/config.test.ts +5 -2
  52. package/src/pi/config.ts +4 -6
  53. package/src/pi/diagnostic-buffer.test.ts +12 -1
  54. package/src/pi/diagnostic-buffer.ts +1 -2
  55. package/src/pi/directory-sync.test.ts +0 -1
  56. package/src/pi/edit-tool.ts +46 -156
  57. package/src/pi/error-text.ts +4 -8
  58. package/src/pi/execute.test.ts +83 -196
  59. package/src/pi/failure-context.test.ts +144 -126
  60. package/src/pi/failure-context.ts +44 -93
  61. package/src/pi/failure-path.test.ts +168 -141
  62. package/src/pi/file-commit.test.ts +110 -53
  63. package/src/pi/file-commit.ts +90 -137
  64. package/src/pi/file-snapshot.test.ts +3 -5
  65. package/src/pi/fusion-card.test.ts +4 -1
  66. package/src/pi/fusion-card.ts +16 -24
  67. package/src/pi/grep-candidates.test.ts +2 -101
  68. package/src/pi/grep-candidates.ts +23 -69
  69. package/src/pi/grep-matrix.test.ts +2 -2
  70. package/src/pi/grep-order.test.ts +4 -4
  71. package/src/pi/grep-output.ts +62 -96
  72. package/src/pi/grep-render.ts +1 -1
  73. package/src/pi/grep-reread.ts +92 -0
  74. package/src/pi/grep-scope.ts +40 -42
  75. package/src/pi/grep-search.ts +22 -31
  76. package/src/pi/grep-tool.ts +55 -82
  77. package/src/pi/grep.test.ts +80 -45
  78. package/src/pi/model-budgets.test.ts +25 -23
  79. package/src/pi/model-budgets.ts +6 -38
  80. package/src/pi/mutation-result.test.ts +44 -35
  81. package/src/pi/mutation-result.ts +51 -20
  82. package/src/pi/mutation-runner.ts +126 -70
  83. package/src/pi/output-budgets.test.ts +36 -14
  84. package/src/pi/path.ts +1 -1
  85. package/src/pi/read-tool.ts +5 -5
  86. package/src/pi/regex-worker-contract.ts +3 -13
  87. package/src/pi/regex-worker.ts +1 -10
  88. package/src/pi/registration.test.ts +64 -36
  89. package/src/pi/render.test.ts +38 -28
  90. package/src/pi/render.ts +59 -63
  91. package/src/pi/replace-regex.ts +14 -2
  92. package/src/pi/replace-tool.ts +20 -82
  93. package/src/pi/replace-worker.ts +1 -1
  94. package/src/pi/replace.test.ts +9 -5
  95. package/src/pi/report-cause.ts +3 -8
  96. package/src/pi/report-failure.testing.ts +1 -2
  97. package/src/pi/report-schema.ts +62 -81
  98. package/src/pi/report.test.ts +171 -25
  99. package/src/pi/report.ts +33 -87
  100. package/src/pi/result-safety.test.ts +149 -176
  101. package/src/pi/rg-process.test.ts +77 -63
  102. package/src/pi/rg-process.ts +48 -64
  103. package/src/pi/rg-regex.ts +40 -400
  104. package/src/pi/schema.ts +12 -0
  105. package/src/pi/text-stream.test.ts +0 -21
  106. package/src/pi/text-stream.ts +14 -21
  107. package/src/pi/write-tool.ts +33 -62
  108. package/src/pi/write.test.ts +1 -3
  109. package/src/testing/regex-translation-corpus.ts +12 -12
  110. package/src/pi/replace-worker-contract.ts +0 -10
@@ -7,8 +7,9 @@
7
7
  */
8
8
 
9
9
  import { StringDecoder } from "node:string_decoder";
10
+ import { rgPath } from "@vscode/ripgrep";
10
11
  import { DiagnosticBuffer } from "./diagnostic-buffer.ts";
11
- import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
12
+ import { spawn, type ChildProcessByStdio } from "node:child_process";
12
13
  import type { Readable } from "node:stream";
13
14
  import { HashlineError } from "../core/errors.ts";
14
15
  import { throwIfCancelled } from "./error-text.ts";
@@ -20,29 +21,10 @@ import { formatByteSize } from "./budgets.ts";
20
21
  * matching stays parallel in grep-search's workers.
21
22
  */
22
23
  export const COMMON_RG_ARGS = ["--no-config", "--color=never", "--sort=path"];
24
+ /** Leave room for rg's arguments and paths on hosts with small command-line limits. */
25
+ export const MAX_RG_PATTERN_UNITS = 24 * 1024;
23
26
  export const MAX_RG_RECORD_BYTES = 16 * 1024 * 1024;
24
27
  const RG_RECORD_LIMIT_MESSAGE = `ripgrep output record exceeds ${formatByteSize(MAX_RG_RECORD_BYTES)}.`;
25
- /**
26
- * A ripgrep process or output-protocol failure. A finished run contributes its
27
- * exit code and, as the cause, its stderr.
28
- */
29
- function ripgrepFailure(
30
- message: string,
31
- run?: { readonly code: number | null; readonly stderr: string } | { readonly error: unknown },
32
- ): HashlineError<"RIPGREP_FAILED"> {
33
- if (run === undefined) return new HashlineError("RIPGREP_FAILED", message);
34
- if ("error" in run) return new HashlineError("RIPGREP_FAILED", message, { cause: run.error });
35
- const stderr = run.stderr.trim();
36
- return new HashlineError("RIPGREP_FAILED", message, {
37
- facts: { exitCode: run.code },
38
- ...(stderr ? { cause: stderr } : {}),
39
- });
40
- }
41
-
42
- /** rg reported a path that is not valid UTF-8; tools cannot address it. */
43
- function unsupportedPath(): HashlineError {
44
- return new HashlineError("UNSUPPORTED_PATH", "Non-UTF-8 search paths are not supported.");
45
- }
46
28
 
47
29
  /** Bounded ripgrep stderr retains the beginning and end with an omission notice. */
48
30
  export const MAX_RG_STDERR_BYTES = 64 * 1024;
@@ -54,7 +36,7 @@ interface ExitResult {
54
36
  }
55
37
 
56
38
  interface RunningProcess {
57
- child: ChildProcessWithoutNullStreams;
39
+ child: ChildProcessByStdio<null, Readable, Readable>;
58
40
  done: Promise<ExitResult>;
59
41
  kill(): void;
60
42
  }
@@ -71,25 +53,32 @@ export interface GrepBackend {
71
53
  runRgLines: typeof runRgLines;
72
54
  }
73
55
 
74
- /** Surface incomplete search diagnostics without discarding confirmed matches. */
75
- export function recordSearchDiagnostics(result: RgRunResult, warnings: string[]): void {
76
- const message =
56
+ /** An incomplete run's diagnostic, surfaced without discarding confirmed matches. */
57
+ export function searchDiagnostic(result: RgRunResult): string | undefined {
58
+ return (
77
59
  result.stderr.trim() ||
78
60
  (!result.stopped && result.code !== 0 && result.code !== 1
79
61
  ? `ripgrep exited with code ${result.code}.`
80
- : "");
81
- if (message) warnings.push(message);
62
+ : undefined)
63
+ );
64
+ }
65
+
66
+ /** spawn reports a failure to start by throwing or by an error event, depending on the errno. */
67
+ function startFailure(cause: unknown): HashlineError {
68
+ return new HashlineError("RIPGREP_FAILED", "Failed to run ripgrep.", { cause });
82
69
  }
83
70
 
84
- function startRg(rgPath: string, args: readonly string[], signal?: AbortSignal): RunningProcess {
71
+ function startRg(args: readonly string[], signal?: AbortSignal): RunningProcess {
85
72
  throwIfCancelled(signal);
86
- const env = { ...process.env };
87
- delete env.RIPGREP_CONFIG_PATH;
88
- const child = spawn(rgPath, [...args], {
89
- stdio: ["pipe", "pipe", "pipe"],
90
- shell: false,
91
- env,
92
- });
73
+ let child: ChildProcessByStdio<null, Readable, Readable>;
74
+ try {
75
+ child = spawn(rgPath, [...args], {
76
+ stdio: ["ignore", "pipe", "pipe"],
77
+ shell: false,
78
+ });
79
+ } catch (cause) {
80
+ throw startFailure(cause);
81
+ }
93
82
  const stderr = new DiagnosticBuffer(MAX_RG_STDERR_BYTES);
94
83
  const stderrDecoder = new StringDecoder("utf8");
95
84
  let error: Error | undefined;
@@ -110,12 +99,6 @@ function startRg(rgPath: string, args: readonly string[], signal?: AbortSignal):
110
99
  child.on("error", (cause) => {
111
100
  error = cause;
112
101
  });
113
- child.stdin.on("error", (cause: NodeJS.ErrnoException) => {
114
- if (cause.code !== "EPIPE") {
115
- error = cause;
116
- kill();
117
- }
118
- });
119
102
  const done = new Promise<ExitResult>((resolve) => {
120
103
  child.on("close", (code) => {
121
104
  closed = true;
@@ -126,24 +109,25 @@ function startRg(rgPath: string, args: readonly string[], signal?: AbortSignal):
126
109
  });
127
110
  });
128
111
  signal?.addEventListener("abort", kill, { once: true });
129
- if (signal?.aborted) kill();
130
112
  return { child, done, kill };
131
113
  }
132
114
 
133
115
  async function* nulDelimitedRecords(stream: Readable): AsyncGenerator<Buffer> {
134
- let pending = Buffer.alloc(0);
116
+ let pending: Buffer = Buffer.alloc(0);
135
117
  for await (const value of stream) {
136
- const chunk = Buffer.isBuffer(value) ? value : Buffer.from(value);
118
+ const chunk = value as Buffer;
137
119
  // Fast path: no pending data — search the chunk directly.
138
120
  let buf = pending.length ? Buffer.concat([pending, chunk]) : chunk;
139
121
  pending = Buffer.alloc(0);
140
122
  let at: number;
141
123
  while ((at = buf.indexOf("\0")) !== -1) {
142
- if (at > MAX_RG_RECORD_BYTES) throw ripgrepFailure(RG_RECORD_LIMIT_MESSAGE);
124
+ if (at > MAX_RG_RECORD_BYTES)
125
+ throw new HashlineError("RIPGREP_FAILED", RG_RECORD_LIMIT_MESSAGE);
143
126
  yield buf.subarray(0, at);
144
127
  buf = buf.subarray(at + 1);
145
128
  }
146
- if (buf.length > MAX_RG_RECORD_BYTES) throw ripgrepFailure(RG_RECORD_LIMIT_MESSAGE);
129
+ if (buf.length > MAX_RG_RECORD_BYTES)
130
+ throw new HashlineError("RIPGREP_FAILED", RG_RECORD_LIMIT_MESSAGE);
147
131
  pending = buf;
148
132
  }
149
133
  if (pending.length) yield pending;
@@ -151,34 +135,31 @@ async function* nulDelimitedRecords(stream: Readable): AsyncGenerator<Buffer> {
151
135
 
152
136
  /** Run rg while read consumes its stdout; read returns false to stop rg early. */
153
137
  async function runRg(
154
- rgPath: string,
155
138
  args: readonly string[],
156
139
  signal: AbortSignal | undefined,
157
140
  read: (stdout: Readable) => Promise<boolean>,
158
141
  ): Promise<RgRunResult> {
159
- const process = startRg(rgPath, args, signal);
160
- process.child.stdin.end();
142
+ const rg = startRg(args, signal);
161
143
  try {
162
- const stopped = !(await read(process.child.stdout));
163
- if (stopped) process.kill();
164
- const result = await process.done;
144
+ const stopped = !(await read(rg.child.stdout));
145
+ if (stopped) rg.kill();
146
+ const result = await rg.done;
165
147
  throwIfCancelled(signal);
166
- if (result.error) throw ripgrepFailure("Failed to run ripgrep.", { error: result.error });
148
+ if (result.error) throw startFailure(result.error);
167
149
  return { code: result.code, stderr: result.stderr, stopped };
168
150
  } finally {
169
- process.kill();
170
- await process.done;
151
+ rg.kill();
152
+ await rg.done;
171
153
  }
172
154
  }
173
155
 
174
156
  /** @internal — read NUL-delimited UTF-8 paths from an owned rg process. */
175
157
  export function runRgPaths(
176
- rgPath: string,
177
158
  args: string[],
178
159
  signal: AbortSignal | undefined,
179
160
  onPath: (path: string) => boolean | Promise<boolean>,
180
161
  ): Promise<RgRunResult> {
181
- return runRg(rgPath, args, signal, async (stdout) => {
162
+ return runRg(args, signal, async (stdout) => {
182
163
  for await (const record of nulDelimitedRecords(stdout)) {
183
164
  throwIfCancelled(signal);
184
165
  if (!(await onPath(decodePath(record)))) return false;
@@ -187,9 +168,11 @@ export function runRgPaths(
187
168
  });
188
169
  }
189
170
 
171
+ /** A path rg reported; one that is not valid UTF-8 cannot be addressed by tools. */
190
172
  function decodePath(record: Buffer): string {
191
173
  const path = record.toString("utf8");
192
- if (!Buffer.from(path, "utf8").equals(record)) throw unsupportedPath();
174
+ if (!Buffer.from(path, "utf8").equals(record))
175
+ throw new HashlineError("UNSUPPORTED_PATH", "Non-UTF-8 search paths are not supported.");
193
176
  return path;
194
177
  }
195
178
 
@@ -210,13 +193,12 @@ export const LINE_LISTING_ARGS = [
210
193
  * numbers, or undefined once there are more than maxLines.
211
194
  */
212
195
  export function runRgLines(
213
- rgPath: string,
214
196
  args: string[],
215
197
  signal: AbortSignal | undefined,
216
198
  maxLines: number,
217
199
  onFile: (path: string, lines: readonly number[] | undefined) => boolean | Promise<boolean>,
218
200
  ): Promise<RgRunResult> {
219
- return runRg(rgPath, args, signal, async (stdout) => {
201
+ return runRg(args, signal, async (stdout) => {
220
202
  let path: string | undefined;
221
203
  let lines: number[] | undefined = [];
222
204
  let pending: Buffer = Buffer.alloc(0);
@@ -245,15 +227,17 @@ export function runRgLines(
245
227
  const colon = record.indexOf(58);
246
228
  const line = colon > 0 ? Number(record.subarray(0, colon).toString("latin1")) : NaN;
247
229
  if (!Number.isSafeInteger(line) || line < 1)
248
- throw ripgrepFailure("ripgrep printed an unexpected line record.");
230
+ throw new HashlineError("RIPGREP_FAILED", "ripgrep printed an unexpected line record.");
249
231
  if (lines.length < maxLines) lines.push(line);
250
232
  else lines = undefined;
251
233
  }
252
234
  }
253
235
  pending = buf.subarray(at);
254
- if (pending.length > MAX_RG_RECORD_BYTES) throw ripgrepFailure(RG_RECORD_LIMIT_MESSAGE);
236
+ if (pending.length > MAX_RG_RECORD_BYTES)
237
+ throw new HashlineError("RIPGREP_FAILED", RG_RECORD_LIMIT_MESSAGE);
255
238
  }
256
- if (pending.length) throw ripgrepFailure("ripgrep printed an incomplete line record.");
239
+ if (pending.length)
240
+ throw new HashlineError("RIPGREP_FAILED", "ripgrep printed an incomplete line record.");
257
241
  return path === undefined || flush();
258
242
  });
259
243
  }