@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
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Override grep: anchored `LINE#HASH│` results feed edit without a re-read.
3
- * Scope and paths live in grep-scope; ripgrep events in grep-search; output and
4
- * byte budgets in grep-output; TUI presentation in grep-render. This module
5
- * owns the tool schema, render wiring, and execution order.
3
+ * Scope and paths live in grep-scope; search requests and ripgrep events in
4
+ * grep-search; the rg process layer in rg-process; result formatting in
5
+ * grep-output; TUI presentation in grep-render. This module owns the tool
6
+ * schema, the production backend, render wiring, and execution order.
6
7
  * @module pi-hashline-edit/pi
7
8
  */
8
9
 
@@ -25,93 +26,108 @@ import {
25
26
  import { searchMatches, type GrepBackend, type SearchScope } from "./grep-search.ts";
26
27
  export type { GrepBackend } from "./grep-search.ts";
27
28
  import { toDisplayLines } from "./grep-render.ts";
28
- import { resolveIgnoreCase, runRg, runRgPaths, type SearchModes } from "./rg-line-filter.ts";
29
+ import { probeRegex, resolveIgnoreCase, runRgPaths, type SearchModes } from "./rg-process.ts";
29
30
  import { runRgTextView } from "./rg-text-view.ts";
30
- import { integerRange, POSITIVE_SAFE_INTEGER } from "./schema.ts";
31
+ import { GREP_CONTEXT_RANGE, POSITIVE_SAFE_INTEGER } from "./schema.ts";
31
32
  import { throwIfCancelled } from "./error-text.ts";
33
+ import type { HashlineEditConfig } from "./config.ts";
32
34
 
33
- const DEFAULT_LIMIT = 100;
34
- const GREP_CONTEXT_MAX = 20;
35
-
36
- const grepOverrideSchema = Type.Object(
37
- {
38
- pattern: Type.Union(
39
- [Type.String({ minLength: 1 }), Type.Array(Type.String({ minLength: 1 }), { minItems: 1 })],
40
- {
41
- description:
42
- "String or array of strings (an array matches any of them). Regex syntax is ripgrep's Rust regex, not JavaScript: no lookaround or backreferences; ^ and $ match at line boundaries.",
43
- },
44
- ),
45
- path: Type.Optional(
46
- Type.Union(
35
+ /** Grep parameters; descriptions state the configured defaults. */
36
+ function createGrepSchema({ defaultLimit, defaultContext }: HashlineEditConfig["grep"]) {
37
+ return Type.Object(
38
+ {
39
+ pattern: Type.Union(
47
40
  [Type.String({ minLength: 1 }), Type.Array(Type.String({ minLength: 1 }), { minItems: 1 })],
48
41
  {
49
42
  description:
50
- "Existing file or directory, or an array of them; omit to search the working directory. Wildcards are not expanded; use glob.",
43
+ "String or array of strings (an array matches any of them). Regex syntax is ripgrep's Rust regex, not JavaScript: no lookaround or backreferences; ^ and $ match at line boundaries.",
51
44
  },
52
45
  ),
53
- ),
54
- glob: Type.Optional(
55
- Type.Union(
56
- [Type.String({ minLength: 1 }), Type.Array(Type.String({ minLength: 1 }), { minItems: 1 })],
57
- {
46
+ path: Type.Optional(
47
+ Type.Union(
48
+ [
49
+ Type.String({ minLength: 1 }),
50
+ Type.Array(Type.String({ minLength: 1 }), { minItems: 1 }),
51
+ ],
52
+ {
53
+ description:
54
+ "Existing file or directory, or an array of them; omit to search the working directory. Wildcards are not expanded; use glob.",
55
+ },
56
+ ),
57
+ ),
58
+ glob: Type.Optional(
59
+ Type.Union(
60
+ [
61
+ Type.String({ minLength: 1 }),
62
+ Type.Array(Type.String({ minLength: 1 }), { minItems: 1 }),
63
+ ],
64
+ {
65
+ description:
66
+ "Filename glob, or an ordered array of them; prefix exclusions with !, e.g. ['*.ts', '!**/*.test.ts'].",
67
+ },
68
+ ),
69
+ ),
70
+ literal: Type.Optional(
71
+ Type.Boolean({
58
72
  description:
59
- "Filename glob, or an ordered array of them; prefix exclusions with !, e.g. ['*.ts', '!**/*.test.ts'].",
60
- },
73
+ "true: match the text literally. false: ripgrep Rust regex. Omitted: regex when the pattern has metacharacters; a single invalid pattern falls back to a literal search of the whole string, an invalid array fails.",
74
+ }),
61
75
  ),
62
- ),
63
- literal: Type.Optional(
64
- Type.Boolean({
65
- description:
66
- "true: match the text literally. false: ripgrep Rust regex. Omitted: regex when the pattern has metacharacters; a single invalid pattern falls back to a literal search of the whole string, an invalid array fails.",
67
- }),
68
- ),
69
- ignoreCase: Type.Optional(
70
- Type.Boolean({
71
- description:
72
- "true ignores case, false matches case; omitted uses smart-case for the whole query. Inline regex flags still apply.",
73
- }),
74
- ),
75
- multiline: Type.Optional(
76
- Type.Boolean({
77
- description:
78
- "Match across lines (default false); every line a match touches is anchored. The . wildcard does not match newlines; use \\n or (?s).",
79
- }),
80
- ),
81
- context: Type.Optional(
82
- Type.Number({
83
- ...integerRange(0, GREP_CONTEXT_MAX),
84
- description: `Anchored lines shown before and after each match (0-${GREP_CONTEXT_MAX}, default 0); display only, not matched.`,
85
- }),
86
- ),
87
- limit: Type.Optional(
88
- Type.Number({
89
- ...POSITIVE_SAFE_INTEGER,
90
- description: "Maximum matching lines, across all files (default 100).",
91
- }),
92
- ),
93
- outputMode: Type.Optional(
94
- Type.Union([Type.Literal("content"), Type.Literal("files"), Type.Literal("count")], {
95
- description:
96
- '"content" (default): anchored lines; "files": paths; "count": matching lines per file and total. All modes share limit.',
97
- }),
98
- ),
99
- },
100
- { additionalProperties: false },
101
- );
102
- type GrepTool = ToolDefinition<typeof grepOverrideSchema, { incomplete?: true } | undefined>;
76
+ ignoreCase: Type.Optional(
77
+ Type.Boolean({
78
+ description:
79
+ "true ignores case, false matches case; omitted uses smart-case for the whole query. Inline regex flags still apply.",
80
+ }),
81
+ ),
82
+ multiline: Type.Optional(
83
+ Type.Boolean({
84
+ description:
85
+ "Match across lines (default false); every line a match touches is anchored. The . wildcard does not match newlines; use \\n or (?s).",
86
+ }),
87
+ ),
88
+ context: Type.Optional(
89
+ Type.Number({
90
+ ...GREP_CONTEXT_RANGE,
91
+ description: `Anchored lines shown before and after each match (${GREP_CONTEXT_RANGE.minimum}-${GREP_CONTEXT_RANGE.maximum}, default ${defaultContext}); display only, not matched.`,
92
+ }),
93
+ ),
94
+ limit: Type.Optional(
95
+ Type.Number({
96
+ ...POSITIVE_SAFE_INTEGER,
97
+ description: `Maximum matching lines, across all files (default ${defaultLimit}).`,
98
+ }),
99
+ ),
100
+ outputMode: Type.Optional(
101
+ Type.Union([Type.Literal("content"), Type.Literal("files"), Type.Literal("count")], {
102
+ description:
103
+ '"content" (default): anchored lines; "files": paths; "count": matching lines per file and total. All modes share limit.',
104
+ }),
105
+ ),
106
+ },
107
+ { additionalProperties: false },
108
+ );
109
+ }
110
+ type GrepSchema = ReturnType<typeof createGrepSchema>;
111
+ type GrepTool = ToolDefinition<GrepSchema, { incomplete?: true } | undefined>;
103
112
 
104
113
  /** Build the production grep override (a ToolDefinition fragment for registerTool). */
105
- export function makeGrepOverride(cwd: string) {
106
- return makeGrepOverrideWithBackend(cwd, {});
114
+ export function makeGrepOverride(cwd: string, config: HashlineEditConfig) {
115
+ return makeGrepOverrideWithBackend(cwd, config, {});
107
116
  }
108
117
 
109
118
  /** @internal — build a grep override with deterministic process backends for tests. */
110
- export function makeGrepOverrideWithBackend(cwd: string, overrides: Partial<GrepBackend>) {
119
+ export function makeGrepOverrideWithBackend(
120
+ cwd: string,
121
+ config: HashlineEditConfig,
122
+ overrides: Partial<GrepBackend>,
123
+ ) {
124
+ const { hashLen } = config;
125
+ const grepSchema = createGrepSchema(config.grep);
111
126
  const backend: GrepBackend = {
112
- runRg: runRgTextView,
127
+ search: runRgTextView,
113
128
  runRgPaths,
114
129
  resolveIgnoreCase,
130
+ probeRegex,
115
131
  ...overrides,
116
132
  };
117
133
 
@@ -129,12 +145,12 @@ export function makeGrepOverrideWithBackend(cwd: string, overrides: Partial<Grep
129
145
  "In grep, use multiline:true for cross-line matches and outputMode files or count when only paths or counts are needed.",
130
146
  "Copy grep anchors directly into edit; read the full line before rewriting from a partial preview.",
131
147
  ],
132
- parameters: grepOverrideSchema,
148
+ parameters: grepSchema,
133
149
 
134
150
  renderShell: "default" as const,
135
151
 
136
152
  renderCall(
137
- args: Static<typeof grepOverrideSchema>,
153
+ args: Static<GrepSchema>,
138
154
  theme: Parameters<NonNullable<GrepTool["renderCall"]>>[1],
139
155
  ) {
140
156
  const rawPattern = args?.pattern;
@@ -175,17 +191,17 @@ export function makeGrepOverrideWithBackend(cwd: string, overrides: Partial<Grep
175
191
 
176
192
  async execute(
177
193
  _toolCallId: string,
178
- params: Static<typeof grepOverrideSchema>,
194
+ params: Static<GrepSchema>,
179
195
  signal: AbortSignal | undefined,
180
196
  _onUpdate: Parameters<GrepTool["execute"]>[3],
181
197
  ) {
182
198
  throwIfCancelled(signal);
183
- const anchors = createAnchorFormatter();
199
+ const anchors = createAnchorFormatter(hashLen);
184
200
  const warnings: string[] = [];
185
201
 
186
202
  const patterns = toArray(params.pattern).map(normalizeLineEndings);
187
- const effectiveLimit = params.limit ?? DEFAULT_LIMIT;
188
- const context = params.context ?? 0;
203
+ const effectiveLimit = params.limit ?? config.grep.defaultLimit;
204
+ const context = params.context ?? config.grep.defaultContext;
189
205
  const rgPath = bundledRgPath;
190
206
  const outputMode: "content" | "files" | "count" = params.outputMode ?? "content";
191
207
  const multiline = params.multiline ?? false;