@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
@@ -1,26 +1,18 @@
1
1
  import { Type, type Static } from "typebox";
2
- import type { AgentToolResult, AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
2
+ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
3
3
  import { Text } from "@earendil-works/pi-tui";
4
4
  import {
5
5
  createWriteToolDefinition,
6
- type ExtensionToolContext,
7
6
  type Theme,
8
7
  type ToolRenderResultOptions,
9
8
  } from "@earendil-works/pi-coding-agent";
10
- import {
11
- ACTION_FUSION_GUIDELINES,
12
- withThenRunDescription,
13
- withThenRunSchema,
14
- type ThenRunInput,
15
- } from "./action-fusion.ts";
16
9
  import { commitFile } from "./file-commit.ts";
17
- import { executeMutation, type ActionFusionExecutor } from "./mutation-runner.ts";
10
+ import { mutationToolBase, type ActionFusionExecutor } from "./mutation-runner.ts";
18
11
  import { throwIfCancelled } from "./error-text.ts";
19
- import { createArgumentPreparer } from "./argument-validation.ts";
20
12
  import { defaultOutputBudgets, type OutputBudgetProvider } from "./budgets.ts";
21
13
  import { unwritableTextError } from "../core/text.ts";
22
- import { reportLines, renderToolError, splitReport } from "./render.ts";
23
- import type { MutationResultDetails } from "./mutation-result.ts";
14
+ import { MUTATION_CARD_SHOWN, reportLines, renderToolError, splitReport } from "./render.ts";
15
+ import type { MutationOutcome } from "./mutation-result.ts";
24
16
  import type { ReportDetails } from "./report.ts";
25
17
 
26
18
  const writeSchema = Type.Object(
@@ -37,12 +29,11 @@ const writeSchema = Type.Object(
37
29
  { additionalProperties: false },
38
30
  );
39
31
 
40
- type WriteParams = Static<typeof writeSchema> & { then_run?: ThenRunInput };
32
+ type WriteParams = Static<typeof writeSchema>;
41
33
  /** write shows the built-in card; its report states every fact. */
42
34
  type WriteMutationDetails = object;
43
- type WriteDetails = MutationResultDetails<WriteMutationDetails>;
44
- /** The write card's call line shows the content; the command card owns then_run. */
45
- const WRITE_CARD_SHOWN = new Set(["publication", "created", "then_run", "progressError"]);
35
+ /** The write card's call line shows the content. */
36
+ const WRITE_CARD_SHOWN = new Set([...MUTATION_CARD_SHOWN, "publication", "created"]);
46
37
  type WriteRenderContext = Parameters<
47
38
  NonNullable<ReturnType<typeof createWriteToolDefinition>["renderCall"]>
48
39
  >[2];
@@ -52,29 +43,38 @@ export function makeWriteOverride(
52
43
  fusion?: ActionFusionExecutor,
53
44
  budgets: OutputBudgetProvider = defaultOutputBudgets,
54
45
  ) {
55
- const parameters = withThenRunSchema(writeSchema, "write", fusion !== undefined);
56
46
  const builtin = createWriteToolDefinition(cwd);
57
47
  return {
58
- name: "write" as const,
59
- label: "write",
60
- description: withThenRunDescription(
61
- "Write a whole file exactly as supplied, including its line endings. Do not use write for partial changes to an existing file; use edit or replace. Choose create for a new file or overwrite for an existing file. Creates missing parent directories.",
62
- fusion !== undefined,
63
- ),
64
- promptSnippet: "Write complete file content to a path",
65
- promptGuidelines: [...(fusion ? ACTION_FUSION_GUIDELINES : [])],
66
- parameters,
67
- prepareArguments: createArgumentPreparer(
68
- "write",
69
- parameters,
70
- (args, report) => {
48
+ ...mutationToolBase({
49
+ tool: "write",
50
+ cwd,
51
+ fusion,
52
+ budgets,
53
+ schema: writeSchema,
54
+ description:
55
+ "Write a whole file exactly as supplied, including its line endings. Do not use write for partial changes to an existing file; use edit or replace. Choose create for a new file or overwrite for an existing file. Creates missing parent directories.",
56
+ promptSnippet: "Write complete file content to a path",
57
+ check: (args, report) => {
71
58
  const content = (args as { content?: unknown } | null)?.content;
72
59
  const unwritable = typeof content === "string" ? unwritableTextError(content) : undefined;
73
60
  if (unwritable) report("content", unwritable.message);
74
61
  },
75
- budgets,
76
- ),
77
- renderShell: "default" as const,
62
+ async run(
63
+ params,
64
+ { absolutePath, signal },
65
+ ): Promise<MutationOutcome<WriteMutationDetails, "write">> {
66
+ throwIfCancelled(signal);
67
+ const commit = await commitFile(absolutePath, params.content, {
68
+ mode: params.mode,
69
+ signal,
70
+ });
71
+ return {
72
+ commit,
73
+ details: {},
74
+ facts: commit.created ? { created: true } : {},
75
+ };
76
+ },
77
+ }),
78
78
  renderCall(args: WriteParams, theme: Theme, context: WriteRenderContext) {
79
79
  return builtin.renderCall!(args, theme, context);
80
80
  },
@@ -90,34 +90,5 @@ export function makeWriteOverride(
90
90
  if (facts.length) return new Text(facts.join("\n"), 0, 0);
91
91
  return builtin.renderResult!({ ...result, details: undefined }, options, theme, context);
92
92
  },
93
- async execute(
94
- toolCallId: string,
95
- params: WriteParams,
96
- signal: AbortSignal | undefined,
97
- onUpdate: AgentToolUpdateCallback<WriteDetails> | undefined,
98
- ctx: ExtensionToolContext,
99
- ) {
100
- return executeMutation<WriteParams, WriteMutationDetails, "write">(
101
- {
102
- tool: "write",
103
- cwd,
104
- fusion,
105
- budgets,
106
- async run(mutationParams, { absolutePath, signal }) {
107
- throwIfCancelled(signal);
108
- const commit = await commitFile(absolutePath, mutationParams.content, {
109
- mode: mutationParams.mode,
110
- signal,
111
- });
112
- return {
113
- commit,
114
- details: {},
115
- facts: commit.created ? { created: true } : {},
116
- };
117
- },
118
- },
119
- { toolCallId, params, signal, onUpdate, ctx },
120
- );
121
- },
122
93
  };
123
94
  }
@@ -43,8 +43,6 @@ test("write rejects unwritable content before accessing a missing target", async
43
43
  withTemp(async (dir) => {
44
44
  const file = join(dir, "missing.txt");
45
45
  const tool = makeWriteOverride(dir);
46
- const valid = { path: file, content: "ready\n", mode: "create" };
47
- assert.equal(tool.prepareArguments(valid), valid);
48
46
  for (const [content, expected] of [
49
47
  ["bad\0", rejectsArgument("content", /NUL/)],
50
48
  ["\ud800", rejectsArgument("content", /encoded losslessly/)],
@@ -236,7 +234,7 @@ test("write reports missing freshness when then_run removes the target", async (
236
234
  context(dir),
237
235
  );
238
236
  assert.equal(reportOf(result, "write")?.freshness, "missing");
239
- assert.equal(reportOf(result, "write")?.next, REREAD);
237
+ assert.equal(reportOf(result, "write")?.next, undefined);
240
238
  }));
241
239
 
242
240
  test("write preserves command failure and changed freshness", async () =>
@@ -29,7 +29,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
29
29
  flags: "",
30
30
  hits: ["b", "z"],
31
31
  misses: ["a", "A"],
32
- rejectCandidates: true,
32
+ rejectCandidates: false,
33
33
  source: local,
34
34
  },
35
35
  {
@@ -37,7 +37,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
37
37
  flags: "",
38
38
  hits: ["ab"],
39
39
  misses: ["ac", "ad"],
40
- rejectCandidates: true,
40
+ rejectCandidates: false,
41
41
  source: local,
42
42
  },
43
43
  {
@@ -45,7 +45,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
45
45
  flags: "",
46
46
  hits: ["b"],
47
47
  misses: ["a", "c"],
48
- rejectCandidates: true,
48
+ rejectCandidates: false,
49
49
  source: local,
50
50
  },
51
51
  {
@@ -53,7 +53,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
53
53
  flags: "",
54
54
  hits: ["ac", "ba"],
55
55
  misses: ["ab"],
56
- rejectCandidates: true,
56
+ rejectCandidates: false,
57
57
  source: local,
58
58
  },
59
59
  {
@@ -61,7 +61,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
61
61
  flags: "",
62
62
  hits: ["ac", "ba"],
63
63
  misses: ["ab"],
64
- rejectCandidates: true,
64
+ rejectCandidates: false,
65
65
  source: local,
66
66
  },
67
67
  {
@@ -69,7 +69,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
69
69
  flags: "u",
70
70
  hits: ["a", "ſ"],
71
71
  misses: ["k", "K", "K"],
72
- rejectCandidates: true,
72
+ rejectCandidates: false,
73
73
  source: local,
74
74
  },
75
75
  {
@@ -85,7 +85,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
85
85
  flags: "iu",
86
86
  hits: ["k ", "K "],
87
87
  misses: ["kk", "Kk"],
88
- rejectCandidates: true,
88
+ rejectCandidates: false,
89
89
  source: local,
90
90
  },
91
91
  {
@@ -93,7 +93,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
93
93
  flags: "",
94
94
  hits: ["\rb"],
95
95
  misses: ["ab"],
96
- rejectCandidates: true,
96
+ rejectCandidates: false,
97
97
  source: local,
98
98
  },
99
99
  {
@@ -141,7 +141,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
141
141
  flags: "",
142
142
  hits: ["catcat", "dogdog"],
143
143
  misses: ["catdog", "dogcat"],
144
- rejectCandidates: true,
144
+ rejectCandidates: false,
145
145
  source: local,
146
146
  },
147
147
  {
@@ -149,7 +149,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
149
149
  flags: "",
150
150
  hits: ["aa", "bcbc"],
151
151
  misses: ["abc", "bca"],
152
- rejectCandidates: true,
152
+ rejectCandidates: false,
153
153
  source: local,
154
154
  },
155
155
  {
@@ -165,7 +165,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
165
165
  flags: "v",
166
166
  hits: ["KsKs", "abab"],
167
167
  misses: ["Ksks", "abAB"],
168
- rejectCandidates: true,
168
+ rejectCandidates: false,
169
169
  source: local,
170
170
  },
171
171
  {
@@ -214,7 +214,7 @@ export const REGEX_TRANSLATION_CORPUS: readonly RegexWitness[] = [
214
214
  flags: "v",
215
215
  hits: [`prefix${"a".repeat(300)}suffix`, `prefix${"b".repeat(300)}suffix`],
216
216
  misses: ["wrong", "prefixwrong", "wrongsuffix"],
217
- rejectCandidates: true,
217
+ rejectCandidates: false,
218
218
  source: local,
219
219
  },
220
220
  {
@@ -1,10 +0,0 @@
1
- /** Replacement worker request and reply decoder; shared worker errors are checked by the host. */
2
- import { workerReplyDecoder } from "./regex-worker-contract.ts";
3
- import { REPLACEMENT_RESULT_SCHEMA, type Replacement } from "../core/replace-schema.ts";
4
-
5
- export interface ReplacementRequest {
6
- readonly source: string;
7
- readonly rules: readonly Replacement[];
8
- }
9
-
10
- export const decodeWorkerReply = workerReplyDecoder(REPLACEMENT_RESULT_SCHEMA);