@criogaid/pi-hashline-edit 0.7.0 → 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 (118) hide show
  1. package/README.md +74 -222
  2. package/README.zh-CN.md +162 -0
  3. package/package.json +2 -1
  4. package/src/core/AGENTS.md +1 -1
  5. package/src/core/apply.test.ts +172 -134
  6. package/src/core/apply.ts +28 -30
  7. package/src/core/case-fold.test.ts +11 -7
  8. package/src/core/case-fold.ts +2 -6
  9. package/src/core/character-set.ts +14 -0
  10. package/src/core/hash.test.ts +1 -25
  11. package/src/core/hash.ts +0 -5
  12. package/src/core/lines.ts +25 -7
  13. package/src/core/ranges.test.ts +41 -9
  14. package/src/core/ranges.ts +11 -4
  15. package/src/core/regex.ts +36 -6
  16. package/src/core/replace-schema.ts +18 -11
  17. package/src/core/replace.test.ts +90 -0
  18. package/src/core/replace.ts +6 -5
  19. package/src/core/search-analysis.test.ts +9 -0
  20. package/src/core/search-analysis.ts +35 -31
  21. package/src/core/search.ts +1 -1
  22. package/src/core/types.ts +5 -7
  23. package/src/index.ts +2 -4
  24. package/src/integration/config-registration.test.ts +16 -8
  25. package/src/integration/fusion-dispatch.test.ts +41 -22
  26. package/src/integration/grep-flags.test.ts +6 -13
  27. package/src/integration/grep-js-completeness.test.ts +41 -31
  28. package/src/integration/grep-order.test.ts +78 -0
  29. package/src/integration/grep-path-matrix.test.ts +4 -3
  30. package/src/integration/grep-regex.test.ts +13 -16
  31. package/src/integration/grep-rg.test.ts +127 -87
  32. package/src/integration/grep-scan-budget.test.ts +27 -5
  33. package/src/integration/large-text.test.ts +3 -4
  34. package/src/integration/model-budget-lifecycle.test.ts +15 -6
  35. package/src/integration/mutation-file-types.test.ts +0 -1
  36. package/src/integration/mutation-file-types.testing.ts +1 -1
  37. package/src/integration/npm-package.testing.ts +6 -32
  38. package/src/integration/publication-read-changes.test.ts +58 -26
  39. package/src/integration/replace-package.testing.ts +4 -1
  40. package/src/integration/replace-worker-boundary.test.ts +34 -24
  41. package/src/integration/report-replay.test.ts +2 -2
  42. package/src/integration/utf8-errors.test.ts +2 -2
  43. package/src/pi/AGENTS.md +12 -13
  44. package/src/pi/action-fusion.test.ts +71 -216
  45. package/src/pi/action-fusion.ts +37 -74
  46. package/src/pi/anchor-format.test.ts +16 -0
  47. package/src/pi/anchor-format.ts +7 -3
  48. package/src/pi/argument-diagnostics.ts +1 -1
  49. package/src/pi/argument-error.testing.ts +7 -4
  50. package/src/pi/argument-error.ts +5 -7
  51. package/src/pi/argument-format.test.ts +5 -16
  52. package/src/pi/argument-validation.test.ts +59 -25
  53. package/src/pi/argument-validation.ts +4 -5
  54. package/src/pi/budgets.ts +5 -7
  55. package/src/pi/config.test.ts +15 -5
  56. package/src/pi/config.ts +6 -6
  57. package/src/pi/diagnostic-buffer.test.ts +2 -3
  58. package/src/pi/diagnostic-buffer.ts +2 -2
  59. package/src/pi/edit-tool.ts +9 -12
  60. package/src/pi/execute.test.ts +80 -149
  61. package/src/pi/failure-context.test.ts +61 -16
  62. package/src/pi/failure-context.ts +12 -9
  63. package/src/pi/failure-path.test.ts +115 -50
  64. package/src/pi/file-commit.test.ts +30 -46
  65. package/src/pi/file-commit.ts +11 -40
  66. package/src/pi/file-revision.ts +25 -0
  67. package/src/pi/fusion-card-interaction.test.ts +143 -0
  68. package/src/pi/fusion-card.test.ts +193 -37
  69. package/src/pi/fusion-card.ts +186 -116
  70. package/src/pi/grep-candidates.ts +25 -27
  71. package/src/pi/grep-matrix.test.ts +1 -1
  72. package/src/pi/grep-order.test.ts +323 -0
  73. package/src/pi/grep-output.testing.ts +24 -0
  74. package/src/pi/grep-output.ts +62 -69
  75. package/src/pi/grep-render.ts +4 -1
  76. package/src/pi/grep-scope.ts +1 -1
  77. package/src/pi/grep-search.ts +85 -56
  78. package/src/pi/grep-tool.ts +17 -48
  79. package/src/pi/grep.test.ts +270 -169
  80. package/src/pi/mutation-outcome.testing.ts +2 -3
  81. package/src/pi/mutation-result.test.ts +26 -19
  82. package/src/pi/mutation-result.ts +25 -40
  83. package/src/pi/mutation-runner.ts +9 -17
  84. package/src/pi/output-budgets.test.ts +25 -16
  85. package/src/pi/path.ts +3 -1
  86. package/src/pi/pi.test.ts +4 -5
  87. package/src/pi/read-tool.ts +32 -80
  88. package/src/pi/read.test.ts +145 -92
  89. package/src/pi/regex-worker-contract.ts +5 -23
  90. package/src/pi/regex-worker.ts +4 -10
  91. package/src/pi/registration.test.ts +54 -49
  92. package/src/pi/render.test.ts +196 -93
  93. package/src/pi/render.ts +32 -22
  94. package/src/pi/replace-regex.ts +3 -5
  95. package/src/pi/replace-tool.ts +27 -26
  96. package/src/pi/replace-worker-contract.ts +3 -12
  97. package/src/pi/replace.test.ts +144 -226
  98. package/src/pi/report-cause.ts +23 -19
  99. package/src/pi/report-failure.testing.ts +2 -2
  100. package/src/pi/report-schema.ts +55 -43
  101. package/src/pi/report.test.ts +138 -71
  102. package/src/pi/report.ts +40 -43
  103. package/src/pi/result-safety.test.ts +157 -117
  104. package/src/pi/rg-process.test.ts +36 -10
  105. package/src/pi/rg-process.ts +9 -3
  106. package/src/pi/rg-regex.test.ts +17 -0
  107. package/src/pi/rg-regex.ts +28 -22
  108. package/src/pi/text-stream.test.ts +35 -36
  109. package/src/pi/text-stream.ts +14 -14
  110. package/src/pi/tool-call.testing.ts +17 -7
  111. package/src/pi/write-tool.ts +9 -11
  112. package/src/pi/write.test.ts +43 -75
  113. package/src/testing/regex-translation-corpus.ts +16 -0
  114. package/src/testing/rg-line-listing.fixture.mjs +1 -1
  115. package/src/integration/forget-grep.test.ts +0 -148
  116. package/src/pi/forget-tool.ts +0 -367
  117. package/src/pi/forget.test.ts +0 -630
  118. package/src/pi/forget.testing.ts +0 -201
@@ -9,7 +9,8 @@
9
9
  * lines belongs to `edit`.
10
10
  *
11
11
  * - Both modes match the shared LF view and restore original line separators.
12
- * - Literal mode keeps `$` verbatim; regex mode expands JavaScript replacement tokens.
12
+ * - Each rule states `literal`: literal mode keeps `$` verbatim; regex mode expands
13
+ * JavaScript replacement tokens.
13
14
  * - `flags` adds regex flags in either mode; `g` is always forced so every
14
15
  * occurrence is replaced. `i` (case-insensitive), `m` (per-line ^/$),
15
16
  * `s` (dotall), `u` (unicode) all work.
@@ -32,8 +33,8 @@ import {
32
33
  import type { AgentToolUpdateCallback } from "@earendil-works/pi-agent-core";
33
34
  import { Type, type Static } from "typebox";
34
35
  import { applyReplacements } from "../core/replace.ts";
35
- import { buildRegex } from "../core/regex.ts";
36
- import { REPLACEMENTS_SCHEMA, REGEX_FLAGS_PATTERN } from "../core/replace-schema.ts";
36
+ import { checkRegexArguments } from "../core/regex.ts";
37
+ import { REPLACEMENTS_SCHEMA } from "../core/replace-schema.ts";
37
38
  import { runRegexReplacements } from "./replace-regex.ts";
38
39
  import { unwritableTextError } from "../core/text.ts";
39
40
  import {
@@ -57,7 +58,6 @@ import {
57
58
  type TextMutationDetails,
58
59
  } from "./mutation-runner.ts";
59
60
  import type { MutationResultDetails } from "./mutation-result.ts";
60
- import { errorMessage } from "../core/errors.ts";
61
61
  import { defaultOutputBudgets, type OutputBudgetProvider } from "./budgets.ts";
62
62
  import {
63
63
  argumentItems,
@@ -80,9 +80,6 @@ const replaceSchema = Type.Object(
80
80
  { additionalProperties: false },
81
81
  );
82
82
 
83
- function createReplaceSchema(actionFusion: boolean) {
84
- return withThenRunSchema(replaceSchema, "replace", actionFusion);
85
- }
86
83
  type ReplaceParams = Static<typeof replaceSchema> & { then_run?: ThenRunInput };
87
84
 
88
85
  /** Call-header line: `replace path — N rules`, plus `+N -N` once diff counts are known. */
@@ -95,26 +92,22 @@ function replaceHeader(args: ReplaceParams, theme: Theme, counts?: DiffCounts):
95
92
  return t;
96
93
  }
97
94
 
98
- /** Report unwritable replacement text and regex compile errors; Pi checks field shapes. */
95
+ /** Report unwritable replacement text, flags V8 rejects, and regex compile errors; Pi checks field shapes. */
99
96
  function checkReplaceArguments(args: unknown, report: ReportArgumentIssue): void {
100
97
  const rules = argumentItems((args as { replacements?: unknown } | null)?.replacements);
101
- const validFlags = new RegExp(REGEX_FLAGS_PATTERN);
102
98
  rules.forEach((rule, index) => {
103
- const { find, replace, regex, flags } = (rule ?? {}) as Record<string, unknown>;
99
+ const { find, replace, literal, flags } = (rule ?? {}) as Record<string, unknown>;
104
100
  const unwritable = typeof replace === "string" ? unwritableTextError(replace) : undefined;
105
101
  if (unwritable) report(`replacements[${index}].replace`, unwritable.message);
106
- if (
107
- regex === true &&
108
- typeof find === "string" &&
109
- find !== "" &&
110
- (flags === undefined || (typeof flags === "string" && validFlags.test(flags)))
111
- ) {
112
- try {
113
- buildRegex(find, true, flags);
114
- } catch (error) {
115
- report(`replacements[${index}].find`, errorMessage(error));
116
- }
117
- }
102
+ if (flags !== undefined && typeof flags !== "string") return;
103
+ const item = `replacements[${index}]`;
104
+ checkRegexArguments(
105
+ `${item}.flags`,
106
+ flags,
107
+ literal !== false,
108
+ [[`${item}.find`, find]],
109
+ report,
110
+ );
118
111
  });
119
112
  }
120
113
 
@@ -124,12 +117,12 @@ export function makeReplaceTool(
124
117
  fusion?: ActionFusionExecutor,
125
118
  budgets: OutputBudgetProvider = defaultOutputBudgets,
126
119
  ) {
127
- const parameters = createReplaceSchema(fusion !== undefined);
120
+ const parameters = withThenRunSchema(replaceSchema, "replace", fusion !== undefined);
128
121
  return {
129
122
  name: "replace" as const,
130
123
  label: "replace",
131
124
  description: withThenRunDescription(
132
- "Replace every match of one or more literal or JavaScript-regex rules in a file. Rules swap only their matched fragments in one call, without line anchors. Use edit instead for inserting, deleting, or rewriting whole lines. Overlapping matches or a rule with no match reject the whole call. Returns a mutation report.",
125
+ "Replace every match of one or more literal or JavaScript-regex rules in a file. Rules swap only their matched fragments in one call, without line anchors. Use edit instead for inserting, deleting, or rewriting whole lines. Overlapping matches or a rule with no match reject the whole call.",
133
126
  fusion !== undefined,
134
127
  ),
135
128
  promptSnippet: "Replace matching text across a file",
@@ -148,7 +141,15 @@ export function makeReplaceTool(
148
141
  theme: Theme,
149
142
  context: ReplaceRenderContext,
150
143
  ) {
151
- return renderMutationResult(result, options, theme, context, "Replacing…", replaceHeader);
144
+ return renderMutationResult(
145
+ result,
146
+ options,
147
+ theme,
148
+ context,
149
+ "Replacing…",
150
+ replaceHeader,
151
+ "replace",
152
+ );
152
153
  },
153
154
 
154
155
  async execute(
@@ -179,7 +180,7 @@ function runReplace(
179
180
  ) {
180
181
  return runTextMutation<"replace">(target, async (currentText) => {
181
182
  // The regex worker reports its own cancellation; a literal batch runs synchronously.
182
- const { text, count } = rules.some((rule) => rule.regex === true)
183
+ const { text, count } = rules.some((rule) => !rule.literal)
183
184
  ? await runRegexReplacements(currentText, rules, config.replace.regexTimeoutMs, target.signal)
184
185
  : applyReplacements(currentText, rules);
185
186
  return { text, facts: { matches: count } };
@@ -1,19 +1,10 @@
1
- /** Replacement-specific result shape; shared worker errors are checked by the host. */
2
- import { Type } from "typebox";
1
+ /** Replacement worker request and reply decoder; shared worker errors are checked by the host. */
3
2
  import { workerReplyDecoder } from "./regex-worker-contract.ts";
4
- import type { Replacement } from "../core/replace.ts";
3
+ import { REPLACEMENT_RESULT_SCHEMA, type Replacement } from "../core/replace-schema.ts";
5
4
 
6
5
  export interface ReplacementRequest {
7
6
  readonly source: string;
8
7
  readonly rules: readonly Replacement[];
9
8
  }
10
9
 
11
- export const decodeWorkerReply = workerReplyDecoder(
12
- Type.Object(
13
- {
14
- text: Type.String(),
15
- count: Type.Integer({ minimum: 1 }),
16
- },
17
- { additionalProperties: false },
18
- ),
19
- );
10
+ export const decodeWorkerReply = workerReplyDecoder(REPLACEMENT_RESULT_SCHEMA);