@criogaid/pi-hashline-edit 0.5.1 → 0.6.1

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 (53) hide show
  1. package/README.md +51 -15
  2. package/package.json +6 -6
  3. package/src/core/apply.test.ts +66 -16
  4. package/src/core/apply.ts +36 -19
  5. package/src/core/lines.ts +3 -3
  6. package/src/index.ts +2 -0
  7. package/src/integration/forget-grep.test.ts +98 -0
  8. package/src/integration/fusion-dispatch.test.ts +343 -0
  9. package/src/integration/grep-rg.test.ts +269 -33
  10. package/src/integration/mutation-file-types.test.ts +44 -0
  11. package/src/pi/action-fusion.test.ts +71 -128
  12. package/src/pi/action-fusion.ts +83 -45
  13. package/src/pi/argument-validation.test.ts +136 -0
  14. package/src/pi/argument-validation.ts +102 -0
  15. package/src/pi/budgets.ts +4 -1
  16. package/src/pi/config.test.ts +1 -0
  17. package/src/pi/config.ts +4 -0
  18. package/src/pi/directory-sync.test.ts +1 -1
  19. package/src/pi/edit-tool.ts +55 -36
  20. package/src/pi/execute.test.ts +185 -645
  21. package/src/pi/extension-loading.test.ts +34 -0
  22. package/src/pi/extension-loading.testing.ts +15 -0
  23. package/src/pi/failure-context.test.ts +4 -3
  24. package/src/pi/failure-path.test.ts +3 -3
  25. package/src/pi/file-commit.test.ts +24 -17
  26. package/src/pi/file-commit.ts +36 -9
  27. package/src/pi/file-snapshot.test.ts +24 -0
  28. package/src/pi/forget-tool.ts +268 -0
  29. package/src/pi/forget.test.ts +343 -0
  30. package/src/pi/forget.testing.ts +273 -0
  31. package/src/pi/grep-output.ts +57 -28
  32. package/src/pi/grep-search.ts +35 -12
  33. package/src/pi/grep-tool.ts +28 -9
  34. package/src/pi/grep.test.ts +173 -152
  35. package/src/pi/mutation-result.test.ts +23 -0
  36. package/src/pi/mutation-runner.ts +8 -4
  37. package/src/pi/path.ts +16 -2
  38. package/src/pi/pi.test.ts +3 -2
  39. package/src/pi/read-tool.ts +58 -26
  40. package/src/pi/read.test.ts +361 -0
  41. package/src/pi/registration.test.ts +115 -2
  42. package/src/pi/render.test.ts +45 -14
  43. package/src/pi/replace-tool.ts +18 -27
  44. package/src/pi/replace.test.ts +8 -107
  45. package/src/pi/result-safety.test.ts +123 -149
  46. package/src/pi/rg-match-bytes.ts +95 -0
  47. package/src/pi/rg-process.test.ts +40 -59
  48. package/src/pi/rg-text-view.ts +52 -5
  49. package/src/pi/tool-call.testing.ts +50 -0
  50. package/src/pi/tool-prompts.ts +15 -0
  51. package/src/pi/write-tool.ts +17 -26
  52. package/src/pi/write.test.ts +70 -61
  53. package/src/pi/e2e-scenarios.test.ts +0 -361
@@ -15,7 +15,8 @@ import {
15
15
  writeFile,
16
16
  } from "node:fs/promises";
17
17
  import { tmpdir } from "node:os";
18
- import { join } from "node:path";
18
+ import { dirname, join } from "node:path";
19
+ import { pathToFileURL } from "node:url";
19
20
  import { rgPath } from "@vscode/ripgrep";
20
21
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
21
22
  import { makeGrepOverrideWithBackend } from "../pi/grep-tool.ts";
@@ -23,11 +24,19 @@ import { makeEditOverride } from "../pi/edit-tool.ts";
23
24
  import { makeReadOverride } from "../pi/read-tool.ts";
24
25
  import { makeWriteOverride } from "../pi/write-tool.ts";
25
26
  import { makeReplaceTool } from "../pi/replace-tool.ts";
26
- import { COMMON_RG_ARGS, probeRegex, resolveIgnoreCase, runRg } from "../pi/rg-process.ts";
27
+ import {
28
+ COMMON_RG_ARGS,
29
+ MAX_RG_RECORD_BYTES,
30
+ probeRegex,
31
+ resolveIgnoreCase,
32
+ runRg,
33
+ } from "../pi/rg-process.ts";
34
+ import { GREP_MAX_LINE_LENGTH } from "../pi/budgets.ts";
27
35
  import { runRgTextView } from "../pi/rg-text-view.ts";
28
36
  import { computeLineHash } from "../core/hash.ts";
29
37
  import { callTool } from "../pi/tool-call.testing.ts";
30
38
  import { DEFAULT_CONFIG } from "../pi/config.ts";
39
+ import { canonicalPath } from "../pi/path.ts";
31
40
 
32
41
  const invoke = (
33
42
  tool: any,
@@ -286,7 +295,7 @@ test("real rg requires an explicit mode and separates exact text from regex", as
286
295
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
287
296
  await assert.rejects(
288
297
  invoke(tool, "missing-mode", { pattern: "foo" }),
289
- /Validation failed for tool "grep":\n {2}- literal: /,
298
+ /Validation failed[\s\S]*literal:/,
290
299
  );
291
300
  for (const pattern of ["(?i)^foo$", "(?P<name>foo)$"]) {
292
301
  const result = await invoke(tool, "regex", { pattern, literal: false });
@@ -303,11 +312,11 @@ test("real rg requires an explicit mode and separates exact text from regex", as
303
312
  assert.equal(missing.content[0].text, "No matches found");
304
313
  await assert.rejects(
305
314
  invoke(tool, "invalid-regex", { pattern: "queueTool(", literal: false }),
306
- /regex parse error[\s\S]*set literal:true to search the text exactly/,
315
+ /regex parse error[\s\S]*literal:true/,
307
316
  );
308
317
  await assert.rejects(
309
318
  invoke(tool, "invalid-array", { pattern: ["queueTool(", "\\bfoo\\b"], literal: false }),
310
- /regex parse error[\s\S]*set literal:true to search the text exactly/,
319
+ /regex parse error[\s\S]*literal:true/,
311
320
  );
312
321
  } finally {
313
322
  await rm(directory, { recursive: true, force: true });
@@ -398,7 +407,7 @@ test("shared rg runner stops and cleans up after limits, callback failures, and
398
407
  );
399
408
  await assert.rejects(
400
409
  probeRegex(join(directory, "missing-rg"), ["needle"], false),
401
- /^Error: Failed to run ripgrep: /,
410
+ /Failed to run ripgrep/,
402
411
  );
403
412
  } finally {
404
413
  await rm(directory, { recursive: true, force: true });
@@ -562,7 +571,13 @@ test("real rg reports unsupported lookarounds and backreferences under Rust rege
562
571
  for (const pattern of ["foo(?=bar)", "(foo)\\1", ["plain", "foo(?=bar)"]]) {
563
572
  await assert.rejects(
564
573
  invoke(tool, "0", { pattern, literal: false }, undefined, undefined),
565
- /regex parse error[\s\S]*no lookaround or backreferences; rewrite the pattern, or use replace/,
574
+ (error: Error) => {
575
+ assert.match(error.message, /regex parse error/);
576
+ assert.match(error.message, /no lookaround or backreferences/);
577
+ assert.match(error.message, /rewrite.*pattern/);
578
+ assert.match(error.message, /use replace/);
579
+ return true;
580
+ },
566
581
  );
567
582
  }
568
583
  const literal = await invoke(tool, "0", { pattern: "foo(?=bar)", literal: true });
@@ -572,7 +587,7 @@ test("real rg reports unsupported lookarounds and backreferences under Rust rege
572
587
  invoke(tool, "0", { pattern: "foo(", literal: false }, undefined, undefined),
573
588
  (error: Error) =>
574
589
  /regex parse error/.test(error.message) &&
575
- /set literal:true to search the text exactly/.test(error.message) &&
590
+ /literal:true/.test(error.message) &&
576
591
  !/lookaround/.test(error.message),
577
592
  );
578
593
  } finally {
@@ -926,11 +941,11 @@ test("default grep rejects file changes and propagates cancellation", async () =
926
941
  test("owned rg rejects an oversized JSONL record", async () => {
927
942
  const directory = await mkdtemp(join(tmpdir(), "hl-grep-record-limit-"));
928
943
  try {
929
- await writeFile(join(directory, "large.txt"), `needle${"x".repeat(17 * 1024 * 1024)}\n`);
944
+ await writeFile(join(directory, "large.txt"), `needle${"x".repeat(MAX_RG_RECORD_BYTES + 1)}\n`);
930
945
  const tool = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
931
946
  await assert.rejects(
932
947
  invoke(tool, "0", { pattern: "needle", literal: true }, undefined, undefined),
933
- /record exceeds 16 MiB/,
948
+ new RegExp(`record exceeds ${MAX_RG_RECORD_BYTES / 1024 ** 2} MiB`),
934
949
  );
935
950
  } finally {
936
951
  await rm(directory, { recursive: true, force: true });
@@ -989,14 +1004,18 @@ test("tools share physical lines and anchors across text representations", async
989
1004
  result.content[0].text.split("\n").filter((line: string) => /^\d+#/.test(line));
990
1005
  try {
991
1006
  const file = join(directory, "fixture.txt");
992
- for (const before of [
1007
+ for (const [index, before] of [
993
1008
  "a\nold\n",
994
1009
  "a\r\nold\r\n",
995
1010
  "\uFEFFa\r\nold\nkeep\r\n",
996
1011
  "a\rb\nold\n",
997
1012
  "a\rb\nold",
998
- ]) {
999
- await call(makeWriteOverride(directory), { path: file, content: before });
1013
+ ].entries()) {
1014
+ await call(makeWriteOverride(directory), {
1015
+ path: file,
1016
+ content: before,
1017
+ mode: index === 0 ? "create" : "overwrite",
1018
+ });
1000
1019
  assert.equal(await readFile(file, "utf8"), before);
1001
1020
  const read = await call(makeReadOverride(directory, DEFAULT_CONFIG), { path: file });
1002
1021
  const grep = await call(makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {}), {
@@ -1058,7 +1077,11 @@ test("long-line previews preserve full-line anchors across literal and Rust rege
1058
1077
  assert.ok(row.startsWith(`1#${computeLineHash(1, long, 4)}│[partial, columns `));
1059
1078
  assert.ok(row.includes("NEEDLE"));
1060
1079
  assert.equal(Buffer.from(row).toString("utf8"), row);
1061
- assert.ok(output.includes(`3#${computeLineHash(3, context, 4)}│[partial, columns 1-499]`));
1080
+ assert.ok(
1081
+ output.includes(
1082
+ `3#${computeLineHash(3, context, 4)}│[partial, columns 1-${GREP_MAX_LINE_LENGTH - 1}]`,
1083
+ ),
1084
+ );
1062
1085
  assert.match(output, /anchors hash full lines/);
1063
1086
  }
1064
1087
  } finally {
@@ -1074,7 +1097,7 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1074
1097
  const write = makeWriteOverride(directory);
1075
1098
  const read = makeReadOverride(directory, DEFAULT_CONFIG);
1076
1099
  const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1077
- await invoke(write, "write", { path, content: before }, undefined, undefined, {
1100
+ await invoke(write, "write", { path, content: before, mode: "create" }, undefined, undefined, {
1078
1101
  cwd: directory,
1079
1102
  } as ExtensionContext);
1080
1103
  assert.equal(await readFile(path, "utf8"), before);
@@ -1123,9 +1146,16 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1123
1146
 
1124
1147
  const expected = "\uFEFFhead\r\nA\r\nB\r\nC\nstand\rCR\r\r\nlast";
1125
1148
  for (const mode of ["edit", "literal", "regex"]) {
1126
- await invoke(write, "reset", { path, content: before }, undefined, undefined, {
1127
- cwd: directory,
1128
- } as ExtensionContext);
1149
+ await invoke(
1150
+ write,
1151
+ "reset",
1152
+ { path, content: before, mode: "overwrite" },
1153
+ undefined,
1154
+ undefined,
1155
+ {
1156
+ cwd: directory,
1157
+ } as ExtensionContext,
1158
+ );
1129
1159
  if (mode === "edit") {
1130
1160
  await invoke(
1131
1161
  makeEditOverride(directory, DEFAULT_CONFIG),
@@ -1167,9 +1197,16 @@ test("all text tools share logical CRLF matching, anchors, and mutation separato
1167
1197
  assert.equal(await readFile(path, "utf8"), expected, mode);
1168
1198
  }
1169
1199
  // Whole-file write remains the explicit representation boundary, including EOL conversion.
1170
- await invoke(write, "convert", { path, content: "alpha\nbeta\n" }, undefined, undefined, {
1171
- cwd: directory,
1172
- } as ExtensionContext);
1200
+ await invoke(
1201
+ write,
1202
+ "convert",
1203
+ { path, content: "alpha\nbeta\n", mode: "overwrite" },
1204
+ undefined,
1205
+ undefined,
1206
+ {
1207
+ cwd: directory,
1208
+ } as ExtensionContext,
1209
+ );
1173
1210
  assert.equal(await readFile(path, "utf8"), "alpha\nbeta\n");
1174
1211
  } finally {
1175
1212
  await rm(directory, { recursive: true, force: true });
@@ -1184,7 +1221,7 @@ test("visible source escapes remain readable and searchable while real CRLF stay
1184
1221
  await invoke(
1185
1222
  makeWriteOverride(directory),
1186
1223
  "write",
1187
- { path, content: code },
1224
+ { path, content: code, mode: "create" },
1188
1225
  undefined,
1189
1226
  undefined,
1190
1227
  { cwd: directory } as ExtensionContext,
@@ -1249,11 +1286,9 @@ test("normalized grep batches retain original paths and ignore unmatched invalid
1249
1286
  undefined,
1250
1287
  );
1251
1288
  assert.equal((result.content[0].text.match(/file \d+\.txt: 1/g) ?? []).length, 70);
1252
- assert.doesNotMatch(
1253
- result.content[0].text,
1254
- /Search incomplete|invalid\.txt|UNSUPPORTED_ENCODING/,
1255
- );
1289
+ assert.doesNotMatch(result.content[0].text, /invalid\.txt|UNSUPPORTED_ENCODING/);
1256
1290
  assert.doesNotMatch(result.content[0].text, /hashline-grep-/);
1291
+ assert.equal(result.details?.incomplete, undefined);
1257
1292
  } finally {
1258
1293
  await rm(directory, { recursive: true, force: true });
1259
1294
  }
@@ -1289,10 +1324,8 @@ test("NUL files are silently skipped across search scopes and output modes", asy
1289
1324
  undefined,
1290
1325
  );
1291
1326
  assert.match(result.content[0].text, /valid\.txt/);
1292
- assert.doesNotMatch(
1293
- result.content[0].text,
1294
- /binary\.txt|UNSUPPORTED_TEXT|Search incomplete/,
1295
- );
1327
+ assert.doesNotMatch(result.content[0].text, /binary\.txt|UNSUPPORTED_TEXT/);
1328
+ assert.equal(result.details?.incomplete, undefined);
1296
1329
  }
1297
1330
  }
1298
1331
  assert.deepEqual(await readFile(binary), source);
@@ -1321,8 +1354,9 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
1321
1354
  undefined,
1322
1355
  );
1323
1356
  assert.match(result.content[0].text, /1│before\n2│needle �\n3│after/);
1324
- assert.match(result.content[0].text, /Invalid UTF-8.*cannot be used as edit anchors/);
1325
- assert.doesNotMatch(result.content[0].text, /\d+#|Search incomplete/);
1357
+ assert.match(result.content[0].text, /Invalid UTF-8.*cannot.*edit anchors/);
1358
+ assert.doesNotMatch(result.content[0].text, /\d+#/);
1359
+ assert.equal(result.details?.incomplete, undefined);
1326
1360
  assert.deepEqual(await readFile(invalid), source);
1327
1361
  }
1328
1362
  await writeFile(join(directory, "valid.txt"), "needle\n");
@@ -1345,7 +1379,7 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
1345
1379
  );
1346
1380
  assert.match(result.content[0].text, /invalid\.txt/);
1347
1381
  assert.match(result.content[0].text, /valid\.txt/);
1348
- assert.doesNotMatch(result.content[0].text, /Search incomplete/);
1382
+ assert.equal(result.details?.incomplete, undefined);
1349
1383
  }
1350
1384
  const multiline = await invoke(
1351
1385
  grep,
@@ -1369,3 +1403,205 @@ test("invalid UTF-8 matches use plain previews while valid files retain edit anc
1369
1403
  await rm(directory, { recursive: true, force: true });
1370
1404
  }
1371
1405
  });
1406
+
1407
+ test("grep paths with input shorthand or literal separators still edit the searched file", async (t) => {
1408
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-path-roundtrip-"));
1409
+ try {
1410
+ const cases = [
1411
+ { name: "@config.txt", other: "config.txt" },
1412
+ { name: "space\u00a0file.txt", other: "space file.txt" },
1413
+ ...(process.platform === "win32"
1414
+ ? []
1415
+ : [{ name: "dir\\target.txt", other: "dir/target.txt" }]),
1416
+ ];
1417
+ for (const { name, other } of cases) {
1418
+ await t.test(name, async () => {
1419
+ const file = join(directory, name);
1420
+ const otherFile = join(directory, other);
1421
+ await mkdir(dirname(otherFile), { recursive: true });
1422
+ await writeFile(file, "needle\n");
1423
+ await writeFile(otherFile, "needle\n");
1424
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1425
+ let content = "";
1426
+ let returnedPath = "";
1427
+ for (const outputMode of ["content", "files", "count"] as const) {
1428
+ const result = await invoke(grep, "path", {
1429
+ path: pathToFileURL(file).href,
1430
+ pattern: "needle",
1431
+ literal: true,
1432
+ outputMode,
1433
+ });
1434
+ const text = result.content[0].text;
1435
+ const firstLine = text.split("\n")[0];
1436
+ returnedPath =
1437
+ outputMode === "content"
1438
+ ? firstLine.slice(0, firstLine.lastIndexOf(" · "))
1439
+ : outputMode === "count"
1440
+ ? firstLine.slice(0, firstLine.lastIndexOf(": "))
1441
+ : firstLine;
1442
+ assert.equal(canonicalPath(directory, returnedPath), file, outputMode);
1443
+ if (outputMode === "content") content = text;
1444
+ }
1445
+ const anchor = content.match(/(1#[0-9A-Z]+)│needle/)?.[1];
1446
+ assert.ok(anchor);
1447
+ await invoke(makeEditOverride(directory, DEFAULT_CONFIG), "edit", {
1448
+ path: returnedPath,
1449
+ edits: [{ op: "replace", anchor, body: ["updated"] }],
1450
+ });
1451
+ assert.equal(await readFile(file, "utf8"), "updated\n");
1452
+ assert.equal(await readFile(otherFile, "utf8"), "needle\n");
1453
+ });
1454
+ }
1455
+ } finally {
1456
+ await rm(directory, { recursive: true, force: true });
1457
+ }
1458
+ });
1459
+
1460
+ test("literal CRLF queries use the corresponding UTF-8 and raw-byte search views", async (t) => {
1461
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-raw-crlf-"));
1462
+ try {
1463
+ await writeFile(join(directory, "valid.txt"), "alpha\r\nbeta\r\n");
1464
+ await writeFile(
1465
+ join(directory, "invalid.txt"),
1466
+ Buffer.concat([Buffer.from("alpha\r\nbeta\r\n"), Buffer.from([0xff])]),
1467
+ );
1468
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {});
1469
+ for (const outputMode of ["content", "files", "count"] as const) {
1470
+ await t.test(outputMode, async () => {
1471
+ const result = await invoke(grep, "crlf", {
1472
+ path: directory,
1473
+ pattern: "alpha\r\nbeta",
1474
+ literal: true,
1475
+ multiline: true,
1476
+ outputMode,
1477
+ });
1478
+ assert.match(result.content[0].text, /(?:^|\n)valid\.txt/);
1479
+ assert.match(result.content[0].text, /(?:^|\n)invalid\.txt/);
1480
+ if (outputMode === "content") {
1481
+ assert.match(result.content[0].text, /1│alpha\n2│beta/);
1482
+ assert.match(result.content[0].text, /1#[0-9A-Z]+│alpha\n2#[0-9A-Z]+│beta/);
1483
+ } else if (outputMode === "count") {
1484
+ assert.match(result.content[0].text, /Total: 4 matches in 2 files/);
1485
+ }
1486
+ assert.equal(result.details?.incomplete, undefined);
1487
+ const lf = await invoke(grep, "lf", {
1488
+ path: directory,
1489
+ pattern: "alpha\nbeta",
1490
+ literal: true,
1491
+ multiline: true,
1492
+ outputMode,
1493
+ });
1494
+ assert.match(lf.content[0].text, /valid\.txt/);
1495
+ assert.doesNotMatch(lf.content[0].text, /invalid\.txt/);
1496
+ });
1497
+ }
1498
+ } finally {
1499
+ await rm(directory, { recursive: true, force: true });
1500
+ }
1501
+ });
1502
+
1503
+ test("raw-byte matches changed before revision acquisition are never confirmed by lossy previews", async (t) => {
1504
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-raw-revision-"));
1505
+ try {
1506
+ const file = join(directory, "invalid.txt");
1507
+ for (const separator of ["\n", "\r\n"]) {
1508
+ for (const mode of [false, true, "limited", "eof"] as const) {
1509
+ const multiline = mode !== false;
1510
+ for (const outputMode of ["content", "files", "count"] as const) {
1511
+ await t.test(`${JSON.stringify(separator)} ${mode} ${outputMode}`, async () => {
1512
+ const source = (byte: number) =>
1513
+ Buffer.concat([
1514
+ Buffer.from(`before${separator}${"x".repeat(64 * 1024)}needle`),
1515
+ Buffer.from([byte]),
1516
+ Buffer.from(`${separator}after${separator}`),
1517
+ ]);
1518
+ await writeFile(file, source(0xff));
1519
+ const changedSource =
1520
+ mode === "eof"
1521
+ ? Buffer.concat([source(0xff), Buffer.from(`tail${separator}`)])
1522
+ : source(0xfe);
1523
+ let changed = false;
1524
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
1525
+ search(path, request, signal, onLine) {
1526
+ return runRgTextView(path, request, signal, async (...event) => {
1527
+ if (!changed && JSON.parse(event[0]).type === "match") {
1528
+ changed = true;
1529
+ await writeFile(file, changedSource);
1530
+ }
1531
+ return onLine(...event);
1532
+ });
1533
+ },
1534
+ });
1535
+ const boundary = separator === "\r\n" ? "\\r\\n" : "\\n";
1536
+ await assert.rejects(
1537
+ invoke(grep, "changed", {
1538
+ path: file,
1539
+ pattern:
1540
+ mode === "limited"
1541
+ ? `before${boundary}(?-u:[^\\r\\n]*\\xFF)`
1542
+ : mode === "eof"
1543
+ ? `(?-u:\\xFF${boundary}after${boundary})\\z`
1544
+ : `(?-u:\\xFF${multiline ? `${boundary}after` : ""})`,
1545
+ literal: false,
1546
+ ignoreCase: false,
1547
+ multiline,
1548
+ ...(mode === "limited" || mode === "eof" ? { limit: 1 } : {}),
1549
+ outputMode,
1550
+ }),
1551
+ /File changed during search/,
1552
+ );
1553
+ assert.equal(changed, true);
1554
+ assert.deepEqual(await readFile(file), changedSource);
1555
+ });
1556
+ }
1557
+ }
1558
+ }
1559
+ } finally {
1560
+ await rm(directory, { recursive: true, force: true });
1561
+ }
1562
+ });
1563
+
1564
+ test("raw files lost before summary verification leave only confirmed counts and an incomplete notice", async (t) => {
1565
+ const directory = await mkdtemp(join(tmpdir(), "hl-grep-raw-summary-"));
1566
+ try {
1567
+ const invalid = join(directory, "invalid.txt");
1568
+ const valid = join(directory, "valid.txt");
1569
+ for (const outputMode of ["files", "count"] as const) {
1570
+ for (const keepValid of [true, false]) {
1571
+ await t.test(`${outputMode} ${keepValid}`, async () => {
1572
+ await writeFile(invalid, Buffer.concat([Buffer.from("needle "), Buffer.from([0xff])]));
1573
+ await writeFile(valid, "needle\n");
1574
+ const grep = makeGrepOverrideWithBackend(directory, DEFAULT_CONFIG, {
1575
+ async search(...args) {
1576
+ const result = await runRgTextView(...args);
1577
+ await rm(invalid);
1578
+ return result;
1579
+ },
1580
+ });
1581
+ const result = invoke(grep, "summary", {
1582
+ path: keepValid ? [invalid, valid] : invalid,
1583
+ pattern: "needle",
1584
+ literal: true,
1585
+ limit: keepValid ? 2 : 1,
1586
+ outputMode,
1587
+ });
1588
+ if (!keepValid) {
1589
+ await assert.rejects(result, /No matches could be displayed\.[\s\S]*Search incomplete/);
1590
+ return;
1591
+ }
1592
+ const summary = await result;
1593
+ assert.equal(summary.details?.incomplete, true);
1594
+ const shown = summary.content[0].text.split("\n\n[")[0];
1595
+ assert.equal(
1596
+ shown,
1597
+ outputMode === "files" ? "valid.txt" : "valid.txt: 1\nTotal: 1 match in 1 file",
1598
+ );
1599
+ assert.match(summary.content[0].text, /2 matches limit reached/);
1600
+ assert.match(summary.content[0].text, /Could not read .*invalid\.txt/);
1601
+ });
1602
+ }
1603
+ }
1604
+ } finally {
1605
+ await rm(directory, { recursive: true, force: true });
1606
+ }
1607
+ });
@@ -0,0 +1,44 @@
1
+ import assert from "node:assert/strict";
2
+ import { execFile as execFileCallback } from "node:child_process";
3
+ import { mkdtemp, rm, symlink } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { test } from "node:test";
7
+ import { promisify } from "node:util";
8
+
9
+ const execFile = promisify(execFileCallback);
10
+
11
+ test("FIFO mutation targets → reject without waiting for a writer, including through a symlink", {
12
+ skip: process.platform === "win32" ? "POSIX FIFO behavior" : false,
13
+ }, async (t) => {
14
+ const directory = await mkdtemp(join(tmpdir(), "hashline-fifo-"));
15
+ t.after(() => rm(directory, { recursive: true, force: true }));
16
+ const fifo = join(directory, "pipe");
17
+ await execFile("mkfifo", [fifo]);
18
+ await symlink("pipe", join(directory, "alias"));
19
+ const moduleUrl = (path: string) => JSON.stringify(new URL(path, import.meta.url).href);
20
+ // Isolate a blocking open so a regression cannot leave the test runner's I/O threads stuck.
21
+ const script = `
22
+ import assert from "node:assert/strict";
23
+ import { join } from "node:path";
24
+ import { makeEditOverride } from ${moduleUrl("../pi/edit-tool.ts")};
25
+ import { makeReplaceTool } from ${moduleUrl("../pi/replace-tool.ts")};
26
+ import { DEFAULT_CONFIG } from ${moduleUrl("../pi/config.ts")};
27
+ import { callTool } from ${moduleUrl("../pi/tool-call.testing.ts")};
28
+ const directory = process.argv[1];
29
+ const cases = [
30
+ [makeEditOverride(directory, DEFAULT_CONFIG), { edits: [{ op: "append", body: ["after"] }] }],
31
+ [makeReplaceTool(directory, DEFAULT_CONFIG), { replacements: [{ find: "before", replace: "after" }] }],
32
+ ];
33
+ for (const path of ["pipe", "alias"]) {
34
+ for (const [tool, args] of cases) {
35
+ await assert.rejects(callTool(tool, { path: join(directory, path), ...args }),
36
+ error => /not a regular file/.test(error.message) && error.publication === "NOT_PUBLISHED");
37
+ }
38
+ }
39
+ `;
40
+ await execFile(process.execPath, ["--input-type=module", "--eval", script, directory], {
41
+ timeout: 10_000,
42
+ killSignal: "SIGKILL",
43
+ });
44
+ });