@criogaid/pi-hashline-edit 0.10.0 → 0.12.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 (103) hide show
  1. package/README.md +21 -7
  2. package/README.zh-CN.md +22 -8
  3. package/package.json +7 -6
  4. package/src/core/AGENTS.md +2 -2
  5. package/src/core/apply.test.ts +4 -4
  6. package/src/core/apply.ts +3 -3
  7. package/src/core/character-set.test.ts +2 -3
  8. package/src/core/character-set.ts +1 -1
  9. package/src/core/ranges.test.ts +2 -1
  10. package/src/core/ranges.ts +1 -1
  11. package/src/core/regex.ts +6 -2
  12. package/src/core/replace.test.ts +51 -0
  13. package/src/core/replace.ts +10 -4
  14. package/src/core/types.ts +1 -1
  15. package/src/index.ts +9 -9
  16. package/src/integration/config-registration.test.ts +8 -10
  17. package/src/integration/fusion-dispatch.test.ts +2 -2
  18. package/src/integration/grep-errors.test.ts +3 -3
  19. package/src/integration/grep-flags.test.ts +5 -5
  20. package/src/integration/grep-js-completeness.test.ts +12 -11
  21. package/src/integration/grep-line-candidates.test.ts +5 -3
  22. package/src/integration/grep-matrix.test.ts +5 -4
  23. package/src/integration/grep-order.test.ts +4 -4
  24. package/src/integration/grep-path-matrix.test.ts +5 -4
  25. package/src/integration/grep-prefilter-coverage.test.ts +17 -5
  26. package/src/integration/grep-process-limits.test.ts +2 -4
  27. package/src/integration/grep-regex.test.ts +10 -10
  28. package/src/integration/grep-rg.test.ts +93 -48
  29. package/src/integration/grep-scan-budget.test.ts +8 -8
  30. package/src/integration/grep-translation.test.ts +8 -7
  31. package/src/integration/large-text.test.ts +5 -5
  32. package/src/integration/model-budget-lifecycle.test.ts +9 -9
  33. package/src/integration/mutation-file-types.testing.ts +4 -4
  34. package/src/integration/npm-package.test.ts +12 -10
  35. package/src/integration/npm-package.testing.ts +151 -4
  36. package/src/integration/publication-read-changes.test.ts +54 -54
  37. package/src/integration/read-changes.test.ts +4 -4
  38. package/src/integration/replace-worker-boundary.test.ts +1 -1
  39. package/src/integration/report-replay.test.ts +9 -6
  40. package/src/integration/utf8-errors.test.ts +4 -4
  41. package/src/pi/AGENTS.md +13 -12
  42. package/src/pi/action-fusion.test.ts +125 -105
  43. package/src/pi/action-fusion.testing.ts +38 -0
  44. package/src/pi/action-fusion.ts +9 -21
  45. package/src/pi/argument-diagnostics.ts +4 -3
  46. package/src/pi/argument-format.test.ts +19 -18
  47. package/src/pi/argument-validation.test.ts +281 -139
  48. package/src/pi/argument-validation.ts +21 -31
  49. package/src/pi/budgets.ts +1 -4
  50. package/src/pi/codemode.test.ts +7 -2
  51. package/src/pi/config.test.ts +2 -1
  52. package/src/pi/config.ts +8 -3
  53. package/src/pi/diagnostic-buffer.test.ts +23 -13
  54. package/src/pi/edit-tool.ts +48 -63
  55. package/src/pi/error-text.ts +7 -2
  56. package/src/pi/execute.test.ts +178 -157
  57. package/src/pi/failure-context.test.ts +5 -4
  58. package/src/pi/failure-context.ts +1 -1
  59. package/src/pi/failure-path.test.ts +37 -37
  60. package/src/pi/file-commit.test.ts +47 -19
  61. package/src/pi/file-commit.ts +36 -9
  62. package/src/pi/fusion-card.test.ts +22 -21
  63. package/src/pi/fusion-card.ts +9 -5
  64. package/src/pi/grep-candidates.test.ts +18 -20
  65. package/src/pi/grep-candidates.ts +13 -17
  66. package/src/pi/grep-matrix.test.ts +3 -3
  67. package/src/pi/grep-order.test.ts +12 -11
  68. package/src/pi/grep-output.ts +45 -17
  69. package/src/pi/grep-reread.ts +7 -2
  70. package/src/pi/grep-rg.testing.ts +70 -0
  71. package/src/pi/grep-scope.ts +10 -10
  72. package/src/pi/grep-search.ts +8 -11
  73. package/src/pi/grep-tool.ts +40 -53
  74. package/src/pi/grep.test.ts +141 -116
  75. package/src/pi/model-budgets.test.ts +2 -1
  76. package/src/pi/mutation-outcome.testing.ts +0 -1
  77. package/src/pi/mutation-result.test.ts +8 -8
  78. package/src/pi/mutation-result.ts +1 -1
  79. package/src/pi/mutation-runner.ts +10 -6
  80. package/src/pi/output-budgets.test.ts +53 -31
  81. package/src/pi/pi.test.ts +18 -13
  82. package/src/pi/read-tool.ts +13 -18
  83. package/src/pi/read.test.ts +67 -41
  84. package/src/pi/registration.test.ts +25 -19
  85. package/src/pi/render.test.ts +94 -32
  86. package/src/pi/render.ts +7 -4
  87. package/src/pi/replace-regex.ts +1 -1
  88. package/src/pi/replace-tool.ts +6 -16
  89. package/src/pi/replace.test.ts +45 -34
  90. package/src/pi/report-failure.testing.ts +9 -1
  91. package/src/pi/report-schema.ts +225 -86
  92. package/src/pi/report.test.ts +81 -87
  93. package/src/pi/report.ts +35 -22
  94. package/src/pi/result-safety.test.ts +121 -100
  95. package/src/pi/rg-process.test.ts +2 -2
  96. package/src/pi/rg-process.ts +9 -15
  97. package/src/pi/rg-regex.test.ts +13 -0
  98. package/src/pi/rg-regex.ts +2 -2
  99. package/src/pi/schema.ts +23 -0
  100. package/src/pi/tool-factories.testing.ts +59 -0
  101. package/src/pi/write-tool.ts +9 -15
  102. package/src/pi/write.test.ts +30 -31
  103. package/src/testing/run-tests.ts +1 -1
@@ -2,13 +2,12 @@
2
2
  * Override grep: valid UTF-8 results feed edit; malformed UTF-8 uses non-editable previews.
3
3
  * grep-scope admits paths, grep-candidates selects candidates with rg, grep-search matches
4
4
  * them with native JavaScript (core/search), grep-output states the payload and every report
5
- * fact, and grep-render draws the card. This module owns the schema, backend wiring, and
6
- * execution order.
5
+ * fact, and grep-render draws the card. This module owns the schema and execution order.
7
6
  * @module pi-hashline-edit/pi
8
7
  */
9
8
 
10
9
  import type { ToolDefinition } from "@earendil-works/pi-coding-agent";
11
- import { Type, type Static, type TLiteral } from "typebox";
10
+ import { Type, type Static } from "typebox";
12
11
  import { Text } from "@earendil-works/pi-tui";
13
12
  import { renderOutputPreview, renderToolError, reportLines, splitReport } from "./render.ts";
14
13
  import { createAnchorFormatter } from "./anchor-format.ts";
@@ -16,21 +15,32 @@ import { GREP_OUTPUT_MODES, grepOutput, type GrepOutputMode } from "./grep-outpu
16
15
  import { filterExplicitFilesByGlob, resolveSearchPaths } from "./grep-scope.ts";
17
16
  import { searchFiles } from "./grep-search.ts";
18
17
  import { toDisplayLines } from "./grep-render.ts";
19
- import { runRgLines, runRgPaths, type GrepBackend } from "./rg-process.ts";
20
18
  import { smartIgnoreCase } from "../core/search-analysis.ts";
21
19
  import { checkRegexArguments } from "../core/regex.ts";
22
20
  import { DEFAULT_GREP_FLAGS } from "../core/search.ts";
23
- import { GREP_CONTEXT_RANGE, POSITIVE_SAFE_INTEGER, STRICT_SAMPLING } from "./schema.ts";
21
+ import {
22
+ GREP_CONTEXT_RANGE,
23
+ literalUnion,
24
+ POSITIVE_SAFE_INTEGER,
25
+ STRICT_SAMPLING,
26
+ } from "./schema.ts";
24
27
  import { throwIfCancelled } from "./error-text.ts";
25
28
  import { reportResult, runTool, type ReportDetails } from "./report.ts";
26
- import type { HashlineEditConfig } from "./config.ts";
29
+ import type { HashlineEditConfig, ToolEnvironment } from "./config.ts";
27
30
  import { createArgumentPreparer, type ReportArgumentIssue } from "./argument-validation.ts";
28
- import { defaultOutputBudgets, type OutputBudgetProvider } from "./budgets.ts";
29
31
 
30
32
  /** A list field: arrays only, because strict sampling cannot take a string-or-array union. */
31
33
  const nonEmptyStrings = (description: string) =>
32
34
  Type.Array(Type.String({ minLength: 1 }), { minItems: 1, description });
33
35
 
36
+ /** What each output mode returns; argument diagnostics list these beside the values. */
37
+ const OUTPUT_MODE_MEANING: Record<GrepOutputMode, string> = {
38
+ content:
39
+ "Default. Matches and context, with anchors for valid UTF-8 or plain previews otherwise.",
40
+ files: "Paths of the files with a match; use it when only paths are needed.",
41
+ count: "Matching lines per file and in total; use it when only counts are needed.",
42
+ };
43
+
34
44
  /** Grep parameters; descriptions state the configured defaults. */
35
45
  function createGrepSchema({ defaultLimit, defaultContext }: HashlineEditConfig["grep"]) {
36
46
  return Type.Object(
@@ -78,21 +88,11 @@ function createGrepSchema({ defaultLimit, defaultContext }: HashlineEditConfig["
78
88
  limit: Type.Optional(
79
89
  Type.Number({
80
90
  ...POSITIVE_SAFE_INTEGER,
81
- description: `Maximum matching lines, across all files (default ${defaultLimit}).`,
91
+ description: `Maximum matching lines, across all files (default ${defaultLimit}). Every outputMode counts them the same way, so files and count stop at the same cutoff as content.`,
82
92
  }),
83
93
  ),
84
94
  outputMode: Type.Optional(
85
- // TypeBox infers a union's static type only from a tuple.
86
- Type.Union(
87
- GREP_OUTPUT_MODES.map((mode) => Type.Literal(mode)) as [
88
- TLiteral<GrepOutputMode>,
89
- ...TLiteral<GrepOutputMode>[],
90
- ],
91
- {
92
- description:
93
- '"content" (default): matches/context with anchors for valid UTF-8 or plain previews otherwise; "files": paths; "count": matching lines per file and total. Use files or count when only paths or counts are needed. limit counts matching lines in every mode, so files and count stop at the same cutoff as content.',
94
- },
95
- ),
95
+ literalUnion(GREP_OUTPUT_MODES, (mode) => OUTPUT_MODE_MEANING[mode]),
96
96
  ),
97
97
  },
98
98
  { additionalProperties: false },
@@ -108,41 +108,23 @@ function streamedList(value: string | readonly string[] | null | undefined): rea
108
108
  /** Validate JavaScript syntax once; the search never rejects a pattern V8 accepts. */
109
109
  function checkGrepArguments(args: unknown, report: ReportArgumentIssue): void {
110
110
  const { pattern, literal, flags } = (args ?? {}) as Record<string, unknown>;
111
- if (
112
- !Array.isArray(pattern) ||
113
- typeof literal !== "boolean" ||
114
- (flags !== undefined && typeof flags !== "string")
115
- )
116
- return;
117
- const fields = pattern.map((value, index) => [`pattern[${index}]`, value] as const);
118
- checkRegexArguments("flags", flags ?? DEFAULT_GREP_FLAGS, literal, fields, report);
119
- }
120
-
121
- /** Build the production grep override (a ToolDefinition fragment for registerTool). */
122
- export function makeGrepOverride(
123
- cwd: string,
124
- config: HashlineEditConfig,
125
- budgets: OutputBudgetProvider = defaultOutputBudgets,
126
- ) {
127
- return makeGrepOverrideWithBackend(cwd, config, {}, budgets);
111
+ if (flags !== undefined && typeof flags !== "string") return;
112
+ const fields = Array.isArray(pattern)
113
+ ? pattern.map((value, index) => [`pattern[${index}]`, value] as const)
114
+ : [];
115
+ checkRegexArguments("flags", flags ?? DEFAULT_GREP_FLAGS, literal !== false, fields, report);
128
116
  }
129
117
 
130
- /** @internal — build a grep override with deterministic process backends for tests. */
131
- export function makeGrepOverrideWithBackend(
132
- cwd: string,
133
- config: HashlineEditConfig,
134
- overrides: Partial<GrepBackend>,
135
- budgets: OutputBudgetProvider = defaultOutputBudgets,
136
- ) {
118
+ /** Build the grep override (a ToolDefinition fragment for registerTool). */
119
+ export function makeGrepOverride({ cwd, config, budgets }: ToolEnvironment) {
137
120
  const { hashLen } = config;
138
121
  const grepSchema = createGrepSchema(config.grep);
139
- const backend: GrepBackend = { runRgPaths, runRgLines, ...overrides };
140
122
 
141
123
  return {
142
124
  name: "grep" as const,
143
125
  label: "grep",
144
126
  description:
145
- "Search file contents. Content mode returns LINE#HASH edit anchors for valid UTF-8 and plain line numbers without anchors otherwise; files/count modes return paths or matching-line counts. NUL-containing files are skipped. Directory searches respect ignore rules and skip .git and linked directories, even beneath a directory named in path; to search an ignored file or one of those directories, name it itself in path. Use grep instead of rg or grep in a shell to search files; use the shell only to filter another command's output.",
127
+ "Search file contents. Content mode returns LINE#HASH edit anchors for valid UTF-8 and plain line numbers without anchors otherwise; files/count modes return paths or matching-line counts. NUL-containing files are skipped. Directory searches respect ignore rules and skip .git and linked directories, even beneath a directory named in path; to search an ignored file or one of those directories, name it itself in path. Use this tool instead of running rg or grep in a shell to search files; use the shell only to filter another command's output.",
146
128
  promptSnippet: "Search file contents",
147
129
  promptGuidelines: [
148
130
  "When you need several independent searches, send them as separate grep calls in one response; they run in parallel.",
@@ -180,13 +162,17 @@ export function makeGrepOverrideWithBackend(
180
162
  ) {
181
163
  if (isPartial) return new Text(theme.fg("warning", "Searching…"), 0, 0);
182
164
  if (context.isError) return renderToolError(result, "grep", theme, expanded);
183
- const { content, report } = splitReport(result);
165
+ const { content, report } = splitReport(result, "grep");
184
166
  const payload = content[0];
167
+ const mode = context.args?.outputMode ?? "content";
168
+ const lines =
169
+ payload?.type !== "text"
170
+ ? []
171
+ : mode === "content"
172
+ ? toDisplayLines(payload.text, theme)
173
+ : payload.text.split("\n").map((line) => theme.fg("toolOutput", line));
185
174
  return renderOutputPreview(
186
- [
187
- ...(payload?.type === "text" ? toDisplayLines(payload.text, theme) : []),
188
- ...(report ? reportLines(report, theme) : []),
189
- ],
175
+ [...lines, ...(report ? reportLines(report, theme) : [])],
190
176
  expanded,
191
177
  theme,
192
178
  );
@@ -218,16 +204,15 @@ export function makeGrepOverrideWithBackend(
218
204
  };
219
205
  const globs = params.glob ?? [];
220
206
  const paths = await resolveSearchPaths(cwd, params.path);
221
- const admitted = await filterExplicitFilesByGlob(backend, globs, paths, signal);
207
+ const admitted = await filterExplicitFilesByGlob(globs, paths, signal);
222
208
  const { warnings: searchWarnings, ...search } = await searchFiles({
223
- backend,
224
209
  scope: { globs, searchPaths: admitted.searchPaths },
225
210
  query,
226
211
  limit: effectiveLimit,
227
212
  timeoutMs: config.grep.regexTimeoutMs,
228
213
  signal,
229
214
  });
230
- const { payload, facts } = await grepOutput({
215
+ const { payload, facts, next } = await grepOutput({
231
216
  cwd,
232
217
  search,
233
218
  outputMode,
@@ -243,6 +228,8 @@ export function makeGrepOverrideWithBackend(
243
228
  return reportResult(
244
229
  { tool: "grep" as const, path: params.path, ...facts },
245
230
  payload === undefined ? [] : [{ type: "text" as const, text: payload }],
231
+ undefined,
232
+ next,
246
233
  );
247
234
  });
248
235
  },