@criogaid/pi-hashline-edit 0.4.0 → 0.5.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 (60) hide show
  1. package/README.md +116 -20
  2. package/package.json +3 -3
  3. package/src/core/apply.test.ts +194 -104
  4. package/src/core/apply.ts +6 -37
  5. package/src/core/errors.ts +9 -0
  6. package/src/core/hash.test.ts +8 -8
  7. package/src/core/hash.ts +5 -5
  8. package/src/{pi/replace-apply.ts → core/replace.ts} +4 -4
  9. package/src/core/text.ts +1 -3
  10. package/src/core/types.ts +4 -7
  11. package/src/index.ts +9 -11
  12. package/src/integration/config-registration.test.ts +206 -0
  13. package/src/integration/grep-rg.test.ts +73 -60
  14. package/src/integration/large-text.test.ts +9 -8
  15. package/src/integration/replace-package.test.ts +11 -7
  16. package/src/pi/action-fusion.test.ts +110 -34
  17. package/src/pi/action-fusion.ts +77 -114
  18. package/src/pi/anchor-format.test.ts +43 -22
  19. package/src/pi/anchor-format.ts +23 -8
  20. package/src/pi/budgets.ts +7 -2
  21. package/src/pi/config.test.ts +153 -40
  22. package/src/pi/config.ts +87 -41
  23. package/src/pi/e2e-scenarios.test.ts +12 -11
  24. package/src/pi/edit-tool.ts +20 -156
  25. package/src/pi/error-text.ts +10 -5
  26. package/src/pi/execute.test.ts +299 -175
  27. package/src/pi/failure-context.test.ts +7 -2
  28. package/src/pi/failure-context.ts +155 -8
  29. package/src/pi/failure-path.test.ts +33 -26
  30. package/src/pi/file-commit.ts +2 -1
  31. package/src/pi/fusion-card.ts +83 -2
  32. package/src/pi/grep-output.ts +26 -9
  33. package/src/pi/grep-render.ts +6 -5
  34. package/src/pi/grep-scope.ts +2 -15
  35. package/src/pi/grep-search.ts +29 -15
  36. package/src/pi/grep-tool.ts +94 -78
  37. package/src/pi/grep.test.ts +357 -320
  38. package/src/pi/mutation-outcome.testing.ts +22 -0
  39. package/src/pi/mutation-result.test.ts +51 -2
  40. package/src/pi/mutation-result.ts +62 -55
  41. package/src/pi/mutation-runner.ts +16 -33
  42. package/src/pi/pi.test.ts +10 -5
  43. package/src/pi/read-tool.ts +61 -33
  44. package/src/pi/registration.test.ts +1 -5
  45. package/src/pi/render.test.ts +16 -8
  46. package/src/pi/render.ts +4 -91
  47. package/src/pi/replace-regex.ts +54 -0
  48. package/src/pi/replace-tool.ts +24 -58
  49. package/src/pi/replace-worker.ts +2 -2
  50. package/src/pi/replace.test.ts +62 -58
  51. package/src/pi/result-safety.test.ts +124 -84
  52. package/src/pi/rg-line-ranges.ts +3 -2
  53. package/src/pi/{rg-line-filter.test.ts → rg-process.test.ts} +1 -1
  54. package/src/pi/{rg-line-filter.ts → rg-process.ts} +35 -4
  55. package/src/pi/rg-text-view.ts +17 -33
  56. package/src/pi/schema.ts +2 -10
  57. package/src/pi/write-tool.ts +1 -2
  58. package/src/pi/write.test.ts +10 -9
  59. package/src/core/index.ts +0 -14
  60. package/src/pi/state.ts +0 -26
@@ -10,8 +10,10 @@ import {
10
10
  runRg,
11
11
  runRgPaths,
12
12
  type RgRunResult,
13
- } from "./rg-line-filter.ts";
14
- import { errorMessage, throwIfCancelled } from "./error-text.ts";
13
+ } from "./rg-process.ts";
14
+ import { scopeArgs, type SearchRunner } from "./grep-search.ts";
15
+ import { errorMessage } from "../core/errors.ts";
16
+ import { searchChangedError, throwIfCancelled } from "./error-text.ts";
15
17
 
16
18
  /** CRLF snapshot batches flush after this many files or source bytes (README: 64 files / 8 MiB). */
17
19
  const SNAPSHOT_BATCH_FILES = 64;
@@ -30,7 +32,7 @@ async function writeLfSnapshot(source: string, destination: string, signal?: Abo
30
32
  },
31
33
  signal,
32
34
  );
33
- if (stats.hasNul) throw new Error("File changed during search; rerun the query.");
35
+ if (stats.hasNul) throw searchChangedError();
34
36
  if (pendingCr) await handle.writeFile(pendingCr);
35
37
  } finally {
36
38
  await handle.close();
@@ -38,30 +40,12 @@ async function writeLfSnapshot(source: string, destination: string, signal?: Abo
38
40
  }
39
41
 
40
42
  /** Search LF views in batches while reporting original paths and logical match offsets. */
41
- export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine) => {
42
- let boundary = args.length;
43
- const scope: string[] = [];
44
- const matcher: string[] = [];
45
- for (let i = 0; i < args.length; i++) {
46
- const arg = args[i];
47
- if (arg === "--") {
48
- boundary = i;
49
- break;
50
- }
51
- if (arg === "--glob") scope.push(arg, args[++i]);
52
- else if (["--hidden", "--no-ignore", "--follow"].includes(arg)) scope.push(arg);
53
- else {
54
- matcher.push(arg);
55
- if (arg === "-e") matcher.push(args[++i]);
56
- }
57
- }
58
- if (boundary === args.length || args[boundary + 1] === "-")
59
- return runRg(rgPath, args, signal, onLine);
43
+ export const runRgTextView: SearchRunner = async (rgPath, { matcher, scope }, signal, onLine) => {
60
44
  let directory: string | undefined;
61
45
  const result: RgRunResult = { code: 1, stderr: "", stopped: false };
62
46
  // LF-only files pass through to rg directly; only CRLF files need temp snapshots.
63
47
  const snapshots = new Map<string, string>();
64
- const searchPaths: string[] = [];
48
+ const batchPaths: string[] = [];
65
49
  let batchBytes = 0;
66
50
  const record = (run: RgRunResult) => {
67
51
  result.stderr = (result.stderr + run.stderr).slice(0, MAX_RG_STDERR_BYTES);
@@ -71,7 +55,7 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
71
55
  };
72
56
 
73
57
  const searchBatch = async (
74
- searchPaths: readonly string[],
58
+ batchPaths: readonly string[],
75
59
  rewritePaths: ReadonlyMap<string, string>,
76
60
  ) => {
77
61
  const searchArgs = [
@@ -80,7 +64,7 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
80
64
  "--no-ignore",
81
65
  "--hidden",
82
66
  "--",
83
- ...searchPaths,
67
+ ...batchPaths,
84
68
  ];
85
69
  const run = await runRg(rgPath, searchArgs, signal, async (line) => {
86
70
  const event = JSON.parse(line);
@@ -96,12 +80,12 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
96
80
  };
97
81
 
98
82
  const flush = async () => {
99
- if (!searchPaths.length) return !result.stopped;
83
+ if (!batchPaths.length) return !result.stopped;
100
84
  // Scope filtering belongs to the original paths. Snapshot names have no ignore/glob semantics.
101
- await searchBatch(searchPaths, snapshots);
85
+ await searchBatch(batchPaths, snapshots);
102
86
  for (const path of snapshots.keys()) await rm(path);
103
87
  snapshots.clear();
104
- searchPaths.length = 0;
88
+ batchPaths.length = 0;
105
89
  batchBytes = 0;
106
90
  return !result.stopped;
107
91
  };
@@ -114,11 +98,11 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
114
98
  try {
115
99
  const listArgs = [
116
100
  ...COMMON_RG_ARGS,
117
- ...scope,
101
+ ...scopeArgs(scope),
118
102
  "--files",
119
103
  "--null",
120
104
  "--",
121
- ...args.slice(boundary + 1),
105
+ ...scope.searchPaths,
122
106
  ];
123
107
  const listed = await runRgPaths(rgPath, listArgs, signal, async (path) => {
124
108
  throwIfCancelled(signal);
@@ -131,10 +115,10 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
131
115
  const snapshot = join(dir, String(snapshots.size));
132
116
  await writeLfSnapshot(original, snapshot, signal);
133
117
  snapshots.set(snapshot, original);
134
- searchPaths.push(snapshot);
118
+ batchPaths.push(snapshot);
135
119
  } else {
136
120
  // Binary files retain their raw bytes; confirmed content hits are rejected later.
137
- searchPaths.push(original);
121
+ batchPaths.push(original);
138
122
  }
139
123
  batchBytes += info.byteLength;
140
124
  } catch (error) {
@@ -144,7 +128,7 @@ export const runRgTextView: typeof runRg = async (rgPath, args, signal, onLine)
144
128
  return true;
145
129
  }
146
130
  return (
147
- (searchPaths.length < SNAPSHOT_BATCH_FILES && batchBytes < SNAPSHOT_BATCH_BYTES) ||
131
+ (batchPaths.length < SNAPSHOT_BATCH_FILES && batchBytes < SNAPSHOT_BATCH_BYTES) ||
148
132
  (await flush())
149
133
  );
150
134
  });
package/src/pi/schema.ts CHANGED
@@ -15,13 +15,5 @@ export function integerRange(minimum: number, maximum: number) {
15
15
  /** Positive line numbers, line counts, and match limits. */
16
16
  export const POSITIVE_SAFE_INTEGER = integerRange(1, Number.MAX_SAFE_INTEGER);
17
17
 
18
- /** Runtime counterpart of {@link integerRange} for values not validated by a schema. */
19
- export function isIntegerInRange(
20
- value: unknown,
21
- minimum: number,
22
- maximum: number,
23
- ): value is number {
24
- return (
25
- typeof value === "number" && Number.isInteger(value) && value >= minimum && value <= maximum
26
- );
27
- }
18
+ /** grep `context` parameter and its configured default. */
19
+ export const GREP_CONTEXT_RANGE = integerRange(0, 20);
@@ -91,8 +91,6 @@ export function makeWriteOverride(cwd: string, fusion?: ActionFusionExecutor) {
91
91
  {
92
92
  cwd,
93
93
  fusion,
94
- // Write results carry no anchors; stale revisions are reported by then_run.
95
- reportsAnchors: false,
96
94
  async run(mutationParams, { absolutePath, displayPath, signal }) {
97
95
  throwIfCancelled(signal, `before write; ${displayPath} was not changed.`);
98
96
  const result = await commitFile(absolutePath, mutationParams.content, {
@@ -100,6 +98,7 @@ export function makeWriteOverride(cwd: string, fusion?: ActionFusionExecutor) {
100
98
  signal,
101
99
  });
102
100
  return {
101
+ commit: result,
103
102
  result: postProcessMutation("write", result.publication, () => ({
104
103
  content: [
105
104
  {
@@ -4,9 +4,10 @@ import { join } from "node:path";
4
4
  import assert from "node:assert/strict";
5
5
  import { test } from "node:test";
6
6
  import { makeWriteOverride } from "./write-tool.ts";
7
- import { createActionFusionExecutor } from "./action-fusion.ts";
7
+ import { createActionFusionExecutor, THEN_RUN_STALE, THEN_RUN_SUCCEEDED } from "./action-fusion.ts";
8
8
  import { fileRevision } from "./file-commit.ts";
9
9
  import { callTool } from "./tool-call.testing.ts";
10
+ import { staleTargetNotice } from "./mutation-result.ts";
10
11
 
11
12
  const context = (cwd: string) => ({ cwd }) as any;
12
13
 
@@ -193,18 +194,18 @@ test("write reports changed freshness when then_run changes the target", async (
193
194
  return "checked";
194
195
  });
195
196
  const write = makeWriteOverride(dir, fusion) as any;
196
- const result = await write.execute(
197
- "changed",
197
+ const result = await callTool(
198
+ write,
198
199
  { path: "changed.txt", content: "mutation\n", then_run: { command: "check" } },
199
- undefined,
200
- undefined,
201
- context(dir),
200
+ { toolCallId: "changed", ctx: context(dir) },
202
201
  );
203
202
  const text = result.content.map((block: any) => block.text).join("\n");
204
203
  assert.doesNotMatch(text, /revision:|[0-9a-f]{64}/i);
205
- assert.doesNotMatch(text, /anchors are omitted/);
206
- assert.equal((text.match(/Re-read/g) ?? []).length, 1);
207
- assert.equal((text.match(/\[then_run:stale\]/g) ?? []).length, 1);
204
+ assert.deepEqual(result.content, [
205
+ { type: "text", text: "Created changed.txt." },
206
+ { type: "text", text: staleTargetNotice(THEN_RUN_STALE) },
207
+ { type: "text", text: `${THEN_RUN_SUCCEEDED}\nchecked` },
208
+ ]);
208
209
  assert.doesNotMatch(text, /Fresh anchors:|\b1#[0-9A-Z]+\b/);
209
210
  assert.equal(result.details.actionFusion.freshness, "changed");
210
211
  assert.deepEqual(
package/src/core/index.ts DELETED
@@ -1,14 +0,0 @@
1
- /**
2
- * Public API of the pi-hashline-edit core library.
3
- *
4
- * The pure hashline engine, with zero pi dependencies, runnable standalone via
5
- * `node --test`. The pi integration layer lives under `../pi/`.
6
- *
7
- * @module pi-hashline-edit/core
8
- */
9
-
10
- export * from "./types.ts";
11
- export { computeLineHash, hashFileLines } from "./hash.ts";
12
- export { splitLines, detectLineEnding, hasFinalNewline } from "./lines.ts";
13
- export { decodeUtf8, decodeEditableText } from "./text.ts";
14
- export { applyEdits } from "./apply.ts";
package/src/pi/state.ts DELETED
@@ -1,26 +0,0 @@
1
- /**
2
- * Session-level config holder.
3
- *
4
- * globalThis singleton (consistent with the repo's module-identity guidance).
5
- * Config is loaded when the extension registers and read by the tool
6
- * overrides.
7
- *
8
- * @module pi-hashline-edit/pi
9
- */
10
-
11
- import { DEFAULT_CONFIG, type HashlineEditConfig } from "./config.ts";
12
-
13
- const GLOBAL_KEY = "__piHashlineEdit";
14
-
15
- export interface HashlineEditState {
16
- config: HashlineEditConfig;
17
- }
18
-
19
- export function getState(): HashlineEditState {
20
- const g = globalThis as Record<string, unknown>;
21
- const existing = g[GLOBAL_KEY];
22
- if (existing) return existing as HashlineEditState;
23
- const state: HashlineEditState = { config: { ...DEFAULT_CONFIG } };
24
- g[GLOBAL_KEY] = state;
25
- return state;
26
- }