dsh-awesome-optimizer 2.1.0 → 3.3.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 (42) hide show
  1. package/README.md +29 -35
  2. package/README.zh.md +29 -35
  3. package/cache-diagnostics.md +19 -0
  4. package/cordis.patch.yml +14 -2
  5. package/outline-benchmark.md +37 -0
  6. package/outline-benchmark.windows.json +184 -0
  7. package/package.json +15 -2
  8. package/scripts/benchmark-outline.mjs +100 -0
  9. package/scripts/benchmark.mjs +33 -26
  10. package/scripts/typecheck.mjs +37 -18
  11. package/src/ast-outline-config.yml +1 -0
  12. package/src/ast-outline.js +143 -0
  13. package/src/cache-diagnostics.js +218 -0
  14. package/src/code-lexical.js +131 -0
  15. package/src/code-outline.js +171 -0
  16. package/src/composite-tools.d.ts +36 -8
  17. package/src/composite-tools.js +538 -194
  18. package/src/convergence.js +3 -3
  19. package/src/custom-prompt.d.ts +7 -1
  20. package/src/custom-prompt.js +53 -18
  21. package/src/edit-recovery.d.ts +59 -0
  22. package/src/edit-recovery.js +524 -0
  23. package/src/failure-recovery.js +1 -1
  24. package/src/file-mutation-policy.js +0 -8
  25. package/src/find-read-output.js +175 -0
  26. package/src/find-read-ranking.js +63 -0
  27. package/src/index.d.ts +34 -2
  28. package/src/index.js +29 -5
  29. package/src/outline-cache.js +36 -0
  30. package/src/persistent-eval/daemon-pool.js +3 -2
  31. package/src/persistent-eval/index.d.ts +35 -8
  32. package/src/persistent-eval/index.js +51 -16
  33. package/src/persistent-eval/stdio-runner.js +209 -30
  34. package/src/persistent-eval/workers/node_worker.mjs +42 -31
  35. package/src/persistent-eval/workers/py_worker.py +47 -24
  36. package/src/prompt-linter.js +43 -26
  37. package/src/read-guard.d.ts +80 -0
  38. package/src/read-guard.js +336 -0
  39. package/src/runtime-policy.d.ts +1 -0
  40. package/src/runtime-policy.js +35 -15
  41. package/src/telemetry.d.ts +4 -0
  42. package/src/telemetry.js +27 -8
@@ -1,24 +1,248 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
1
3
  import z from '@deepseek-ai/schemastery';
2
4
  import { HarnessError } from '@deepseek-ai/dsh-llm';
3
5
  import { ToolExecutionPipeline } from '@deepseek-ai/dsh-tools/pipeline';
6
+ import { normalizePath } from './runtime-policy.js';
7
+ import { buildEditFailureHint } from './edit-recovery.js';
8
+ import { truncateLongLines } from './read-guard.js';
9
+ import { selectReadWindows } from './find-read-ranking.js';
10
+ import {
11
+ FindOutput, appendSearchNotices, searchStatus, splitWindows,
12
+ appendSlice, appendMatchListing, finishFindOutput
13
+ } from './find-read-output.js';
4
14
 
5
- export const READ_BATCH_TOOL = 'read_batch';
15
+ export const FIND_AND_READ_TOOL = 'find_and_read';
6
16
  export const EDIT_BATCH_TOOL = 'edit_batch';
7
17
 
18
+ export { truncateLongLines };
19
+
8
20
  export const CompositeToolsConfig = z.object({
9
21
  enabled: z.boolean().default(true),
10
- readBatch: z.boolean().default(true),
22
+ findAndRead: z.boolean().default(true),
11
23
  editBatch: z.boolean().default(true),
12
- maxBatchFiles: z.number().step(1).min(1).max(25).default(10),
24
+ maxPatterns: z.number().step(1).min(1).max(10).default(5),
25
+ defaultContextLines: z.number().step(1).min(0).max(25).default(5),
26
+ maxFiles: z.number().step(1).min(1).max(12).default(5),
27
+ maxWindows: z.number().step(1).min(1).max(25).default(10),
28
+ maxWindowLines: z.number().step(1).min(1).max(1000).default(120),
29
+ maxReadLines: z.number().step(1).min(1).max(10000).default(600),
30
+ readConcurrency: z.number().step(1).min(1).max(8).default(3),
13
31
  maxBatchEdits: z.number().step(1).min(1).max(25).default(10),
14
- defaultLimitPerFile: z.number().step(1).min(1).max(2000).default(400)
32
+ maxLineChars: z.number().step(1).min(100).max(50000).default(1500),
33
+ maxTotalBytes: z.number().step(1).min(512).max(1024 * 1024).default(24576),
34
+ atomicEdits: z.boolean().default(false)
15
35
  });
16
36
 
17
37
  /**
18
- * Register safe composite tools that collapse predictable multi-step operations
19
- * (read_batch for reading, edit_batch for literal revisions) into a single model round-trip
20
- * while routing every sub-call through ToolExecutionPipeline so all sandbox policies,
21
- * permissions, and file-observation hooks remain enforced.
38
+ * Parse DSH grouped grep output (`<path>\nLine N: text`), ripgrep `path:line:text`,
39
+ * or structured `meta.matches` into flat `{ path, lineNumber, line }` records.
40
+ */
41
+ export function parseGrepOutput(text, meta) {
42
+ if (meta && (Array.isArray(meta.matches) || Array.isArray(meta.files))) {
43
+ const structured = [
44
+ ...(Array.isArray(meta.matches) ? meta.matches : []),
45
+ ...(Array.isArray(meta.files) ? meta.files.flatMap(file =>
46
+ Array.isArray(file?.matches) ? file.matches.map(match => ({ ...match, path: file.path })) : []
47
+ ) : [])
48
+ ];
49
+ // Meta has its own retention cap; union with rendered text rather than hiding text-only hits.
50
+ const matches = [...structured, ...parseGrepOutput(text)];
51
+ const seen = new Set();
52
+ return matches.filter(m => {
53
+ if (!m || typeof m.path !== 'string' || !Number.isInteger(m.lineNumber) || m.lineNumber < 1) return false;
54
+ const key = JSON.stringify([m.path, m.lineNumber]);
55
+ if (seen.has(key)) return false;
56
+ seen.add(key);
57
+ return true;
58
+ }).map(m => ({ path: m.path, lineNumber: m.lineNumber, line: String(m.line ?? '') }));
59
+ }
60
+ if (typeof text !== 'string' || !text.trim() || /^No matches found\b/i.test(text.trim())) {
61
+ return [];
62
+ }
63
+
64
+ const matches = [];
65
+ let currentFile = '';
66
+ for (const rawLine of text.replace(/\r\n/g, '\n').split('\n')) {
67
+ const trimmed = rawLine.trim();
68
+ if (!trimmed) {
69
+ currentFile = '';
70
+ continue;
71
+ }
72
+ if (/^Found \d+ (?:of \d+ )?match(?:es)?$/i.test(trimmed)) continue;
73
+ if (/^\((?:Full grep result stored at:|The complete result could not be saved)/.test(trimmed)) continue;
74
+
75
+ // 1. Official DSH grouped line format: `Line 42: ...`
76
+ const groupedLine = rawLine.match(/^Line (\d+):\s?(.*)$/);
77
+ if (groupedLine && currentFile) {
78
+ matches.push({
79
+ path: currentFile,
80
+ lineNumber: Number(groupedLine[1]),
81
+ line: groupedLine[2]
82
+ });
83
+ continue;
84
+ }
85
+
86
+ // 2. Ripgrep flat format: `path:42:...` (supports Windows `C:\...`)
87
+ const flatLine = rawLine.match(/^((?:[A-Za-z]:)?[^:\r\n]+):(\d+):(.*)$/);
88
+ if (flatLine) {
89
+ matches.push({
90
+ path: flatLine[1],
91
+ lineNumber: Number(flatLine[2]),
92
+ line: flatLine[3]
93
+ });
94
+ continue;
95
+ }
96
+
97
+ // 3. Otherwise treat as current file group header
98
+ currentFile = trimmed;
99
+ }
100
+ return matches;
101
+ }
102
+
103
+ /**
104
+ * Merge nearby 1-based line numbers into disjoint `[start, end]` read windows.
105
+ */
106
+ export function mergeMatchWindows(lineNumbers, contextLines = 5, adjacentGap = 2) {
107
+ if (!Array.isArray(lineNumbers) || lineNumbers.length === 0) return [];
108
+ const sorted = [...new Set(lineNumbers.filter(n => Number.isInteger(n) && n >= 1))].sort((a, b) => a - b);
109
+ if (sorted.length === 0) return [];
110
+
111
+ const windows = [];
112
+ for (const line of sorted) {
113
+ const start = Math.max(1, line - contextLines);
114
+ const end = line + contextLines;
115
+ const prev = windows[windows.length - 1];
116
+ if (prev && start <= prev.end + adjacentGap) {
117
+ prev.end = Math.max(prev.end, end);
118
+ prev.matchedLines.push(line);
119
+ } else {
120
+ windows.push({ start, end, matchedLines: [line] });
121
+ }
122
+ }
123
+ return windows;
124
+ }
125
+
126
+ function stripReadEnvelope(rawText) {
127
+ if (typeof rawText !== 'string') return '';
128
+ let text = rawText;
129
+ const envelope = text.match(/^[\s\S]*?<content>\n([\s\S]*)\n<\/content>[\s\S]*$/);
130
+ if (envelope) {
131
+ text = envelope[1];
132
+ }
133
+ text = text.replace(/\n\n\((?:Output capped\.|Showing lines |End of file )[^)]*\)\s*$/, '');
134
+ return text.trimEnd();
135
+ }
136
+
137
+ function countOccurrences(haystack, needle) {
138
+ if (!needle) return 0;
139
+ let count = 0;
140
+ let pos = haystack.indexOf(needle);
141
+ while (pos !== -1) {
142
+ count++;
143
+ pos = haystack.indexOf(needle, pos + needle.length);
144
+ }
145
+ return count;
146
+ }
147
+
148
+ async function readFileForPreflight(filePath, cwd, fsService, signal) {
149
+ if (fsService) {
150
+ if (typeof fsService.readFile === 'function') {
151
+ try {
152
+ const content = await fsService.readFile(filePath, { cwd, signal });
153
+ if (typeof content === 'string') return { ok: true, content };
154
+ } catch (err) {
155
+ if (err?.code === 'ENOENT') return { ok: false, missing: true };
156
+ }
157
+ }
158
+ if (typeof fsService.readText === 'function') {
159
+ try {
160
+ const target = typeof fsService.resolve === 'function'
161
+ ? await fsService.resolve(filePath, { cwd, signal })
162
+ : filePath;
163
+ const content = await fsService.readText(target, signal);
164
+ if (typeof content === 'string') return { ok: true, content };
165
+ } catch (err) {
166
+ if (err?.code === 'ENOENT' || err?.code === 'FS_NOT_FOUND') return { ok: false, missing: true };
167
+ }
168
+ }
169
+ }
170
+
171
+ try {
172
+ const resolved = path.isAbsolute(filePath) ? filePath : path.resolve(cwd || process.cwd(), filePath);
173
+ const content = await fs.readFile(resolved, 'utf8');
174
+ return { ok: true, content };
175
+ } catch (err) {
176
+ if (err?.code === 'ENOENT') {
177
+ if (fsService && typeof fsService.resolve === 'function' && typeof fsService.stat === 'function') {
178
+ try {
179
+ const target = await fsService.resolve(filePath, { cwd, signal });
180
+ const stat = await fsService.stat(target, signal);
181
+ if (stat) return null;
182
+ } catch {
183
+ // ignore
184
+ }
185
+ }
186
+ return { ok: false, missing: true };
187
+ }
188
+ return null;
189
+ }
190
+ }
191
+
192
+ function simulateEditsOnContent(initialContent, items) {
193
+ let simulated = initialContent;
194
+ for (const { index, item } of items) {
195
+ if (!item.old_string) {
196
+ return {
197
+ ok: false,
198
+ failedIndex: index,
199
+ reason: `Pre-flight failed at edit #${index + 1} (${item.file_path}): old_string must be non-empty.`
200
+ };
201
+ }
202
+ const matches = countOccurrences(simulated, item.old_string);
203
+ if (matches === 0) {
204
+ const hint = buildEditFailureHint(simulated, item.old_string, { filePath: item.file_path });
205
+ return {
206
+ ok: false,
207
+ failedIndex: index,
208
+ reason: `Pre-flight failed at edit #${index + 1} (${item.file_path}): old_string not found (no changes written to this file).\n\n${hint}`
209
+ };
210
+ }
211
+ if (!item.replace_all && matches > 1) {
212
+ const hint = buildEditFailureHint(simulated, item.old_string, { filePath: item.file_path });
213
+ return {
214
+ ok: false,
215
+ failedIndex: index,
216
+ reason: `Pre-flight failed at edit #${index + 1} (${item.file_path}): old_string matched ${matches} locations; set replace_all=true or provide more context.\n\n${hint}`
217
+ };
218
+ }
219
+ simulated = item.replace_all
220
+ ? simulated.split(item.old_string).join(item.new_string)
221
+ : simulated.replace(item.old_string, item.new_string);
222
+ }
223
+ return { ok: true };
224
+ }
225
+
226
+ function emptyStats() {
227
+ return {
228
+ findExecuted: 0,
229
+ findPatternsSearched: 0,
230
+ findWindowsRead: 0,
231
+ findLinesRequested: 0,
232
+ findReadBatches: 0,
233
+ findFilesMatched: 0,
234
+ editBatchesExecuted: 0,
235
+ editsRequested: 0,
236
+ editsSucceeded: 0,
237
+ editsFailed: 0
238
+ };
239
+ }
240
+
241
+ /**
242
+ * Register precision composite tools:
243
+ * - `find_and_read`: collapses multi-pattern grep + merged-window context reads into 1 round-trip
244
+ * - `edit_batch`: sequential multi-edit with pre-flight simulation & recovery hints
245
+ * Every sub-call routes through ToolExecutionPipeline so sandbox, permissions, and file observations hold.
22
246
  */
23
247
  export function applyCompositeTools(ctx, rawConfig = {}) {
24
248
  const config = CompositeToolsConfig(rawConfig);
@@ -31,16 +255,7 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
31
255
  if (!session) return null;
32
256
  let state = statsBySession.get(session);
33
257
  if (!state) {
34
- state = {
35
- batchesExecuted: 0,
36
- filesRequested: 0,
37
- filesSucceeded: 0,
38
- filesFailed: 0,
39
- editBatchesExecuted: 0,
40
- editsRequested: 0,
41
- editsSucceeded: 0,
42
- editsFailed: 0
43
- };
258
+ state = emptyStats();
44
259
  statsBySession.set(session, state);
45
260
  }
46
261
  return state;
@@ -48,56 +263,91 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
48
263
 
49
264
  ctx.on('session/event', (session, event) => {
50
265
  if (event.type === 'turn/start') {
51
- statsBySession.set(session, {
52
- batchesExecuted: 0,
53
- filesRequested: 0,
54
- filesSucceeded: 0,
55
- filesFailed: 0,
56
- editBatchesExecuted: 0,
57
- editsRequested: 0,
58
- editsSucceeded: 0,
59
- editsFailed: 0
60
- });
266
+ statsBySession.set(session, emptyStats());
61
267
  }
62
268
  });
63
269
 
64
- if (config.readBatch) {
270
+ async function dispatchSubCall(exec, toolName, subArgs, subCallId) {
271
+ const session = exec.agent?.session;
272
+ const log = {
273
+ rootCallId: exec.rootCallId,
274
+ parentCallId: exec.callId,
275
+ subCallId,
276
+ name: toolName,
277
+ arguments: subArgs
278
+ };
279
+ if (session?.append) {
280
+ session.append('tool/ptc-dispatch-start', log);
281
+ }
282
+ const prepared = await pipeline.prepare({
283
+ callId: subCallId,
284
+ rootCallId: exec.rootCallId,
285
+ parent: exec.token,
286
+ name: toolName,
287
+ arguments: subArgs,
288
+ agent: exec.agent,
289
+ toolSet: exec.toolSet,
290
+ signal: exec.signal
291
+ });
292
+ const outcome = prepared.kind === 'dispatch' ? await pipeline.dispatch(prepared.exec) : prepared;
293
+ const result = await pipeline.complete(prepared.exec, outcome);
294
+ if (session?.append) {
295
+ session.append('tool/ptc-dispatch', {
296
+ ...log,
297
+ isError: result.isError,
298
+ content: result.content,
299
+ ...(result.error?.info ? { error: result.error.info } : {}),
300
+ ...(result.meta === undefined ? {} : { meta: result.meta })
301
+ });
302
+ }
303
+ for (const context of result.additionalContexts ?? []) {
304
+ exec.deferContext(context);
305
+ }
306
+ return result;
307
+ }
308
+
309
+ if (config.findAndRead) {
65
310
  ctx.tools.register({
66
- name: READ_BATCH_TOOL,
311
+ name: FIND_AND_READ_TOOL,
67
312
  description: [
68
- `Read multiple UTF-8 text files (up to ${config.maxBatchFiles}) in a single tool call to avoid sequential single-file round trips.`,
69
- 'Each entry dispatches through the native read tool pipeline with full sandbox and policy checks.',
70
- 'Can be called alongside glob or grep in the same turn.'
313
+ `Search code with 1 to ${config.maxPatterns} regex patterns and return merged, line-numbered context slices around matches in a single round-trip.`,
314
+ 'Replaces sequential grep -> read loops while capping total output bytes.',
315
+ 'Set contextLines=0 for a deduplicated match-only listing.'
71
316
  ].join(' '),
72
317
  parameters: {
73
318
  type: 'object',
74
319
  additionalProperties: false,
75
- required: ['files'],
76
320
  properties: {
77
- files: {
321
+ pattern: {
322
+ type: 'string',
323
+ description: 'Single ripgrep regex pattern to search for.'
324
+ },
325
+ patterns: {
78
326
  type: 'array',
79
327
  minItems: 1,
80
- maxItems: config.maxBatchFiles,
81
- description: `List of file read specifications (1 to ${config.maxBatchFiles} files).`,
82
- items: {
83
- type: 'object',
84
- additionalProperties: false,
85
- required: ['file_path'],
86
- properties: {
87
- file_path: {
88
- type: 'string',
89
- description: 'Path to read, resolved by the filesystem backend.'
90
- },
91
- offset: {
92
- type: 'number',
93
- description: '1-based first line to return. Defaults to 1.'
94
- },
95
- limit: {
96
- type: 'number',
97
- description: `Maximum number of lines to return. Defaults to ${config.defaultLimitPerFile}.`
98
- }
99
- }
100
- }
328
+ maxItems: config.maxPatterns,
329
+ items: { type: 'string' },
330
+ description: `1 to ${config.maxPatterns} ripgrep regex patterns searched in parallel and aggregated.`
331
+ },
332
+ path: {
333
+ type: 'string',
334
+ description: 'Directory or file to search. Defaults to the session workspace.'
335
+ },
336
+ include: {
337
+ type: 'string',
338
+ description: 'One positive glob filter for files (e.g. "*.ts", "*.{js,jsx}").'
339
+ },
340
+ contextLines: {
341
+ type: 'number',
342
+ description: `Lines before and after each match (0-25). Defaults to ${config.defaultContextLines}.`
343
+ },
344
+ maxFiles: {
345
+ type: 'number',
346
+ description: `Maximum files to return code slices for (1-12). Defaults to ${config.maxFiles}.`
347
+ },
348
+ maxWindows: {
349
+ type: 'number',
350
+ description: `Maximum merged context windows to read (1-25). Defaults to ${config.maxWindows}.`
101
351
  }
102
352
  }
103
353
  },
@@ -108,108 +358,162 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
108
358
  isConcurrencySafe: () => true,
109
359
  async execute(args, exec) {
110
360
  exec.signal.throwIfAborted();
111
- const files = Array.isArray(args?.files) ? args.files : [];
112
- if (files.length === 0) {
113
- throw new HarnessError('read_batch requires at least one file entry in "files".', 'INVALID_READ_BATCH_ARGS');
361
+ const rawPatterns = [
362
+ ...(typeof args?.pattern === 'string' ? [args.pattern] : []),
363
+ ...(Array.isArray(args?.patterns) ? args.patterns : [])
364
+ ].filter(p => typeof p === 'string' && p.length > 0);
365
+
366
+ const uniquePatterns = [...new Set(rawPatterns)];
367
+ if (uniquePatterns.length === 0) {
368
+ throw new HarnessError('find_and_read requires a non-empty "pattern" or "patterns" array.', 'INVALID_FIND_AND_READ_ARGS');
114
369
  }
115
- if (files.length > config.maxBatchFiles) {
370
+ if (uniquePatterns.length > config.maxPatterns) {
116
371
  throw new HarnessError(
117
- `read_batch received ${files.length} files, exceeding maxBatchFiles (${config.maxBatchFiles}).`,
118
- 'READ_BATCH_LIMIT_EXCEEDED'
372
+ `find_and_read received ${uniquePatterns.length} patterns, exceeding maxPatterns (${config.maxPatterns}).`,
373
+ 'FIND_AND_READ_LIMIT_EXCEEDED'
119
374
  );
120
375
  }
121
376
 
377
+ const contextLines = typeof args?.contextLines === 'number' && Number.isFinite(args.contextLines)
378
+ ? Math.max(0, Math.min(25, Math.floor(args.contextLines)))
379
+ : config.defaultContextLines;
380
+ const maxFiles = typeof args?.maxFiles === 'number' && Number.isFinite(args.maxFiles)
381
+ ? Math.max(1, Math.min(12, Math.floor(args.maxFiles)))
382
+ : config.maxFiles;
383
+ const maxWindows = typeof args?.maxWindows === 'number' && Number.isFinite(args.maxWindows)
384
+ ? Math.max(1, Math.min(25, Math.floor(args.maxWindows)))
385
+ : config.maxWindows;
386
+
122
387
  const session = exec.agent?.session;
123
388
  const state = stateFor(session);
389
+ const cwdService = ctx.get ? ctx.get('workingDirectory') : undefined;
390
+ const cwd = cwdService?.get?.(session) ?? '';
391
+
124
392
  if (state) {
125
- state.batchesExecuted++;
126
- state.filesRequested += files.length;
393
+ state.findExecuted++;
394
+ state.findPatternsSearched += uniquePatterns.length;
127
395
  }
128
396
 
129
- const subResults = await Promise.all(files.map(async (item, index) => {
397
+ const grepRuns = await Promise.all(uniquePatterns.map(async (pattern, idx) => {
130
398
  exec.signal.throwIfAborted();
131
- if (!item || typeof item.file_path !== 'string' || !item.file_path.trim()) {
132
- if (state) state.filesFailed++;
133
- return {
134
- filePath: String(item?.file_path ?? `<invalid-${index}>`),
135
- isError: true,
136
- text: 'Invalid file_path: must be a non-empty string.'
137
- };
138
- }
139
- const readArgs = {
140
- file_path: item.file_path,
141
- ...(typeof item.offset === 'number' ? { offset: item.offset } : {}),
142
- limit: typeof item.limit === 'number' ? item.limit : config.defaultLimitPerFile
143
- };
144
- const subCallId = `${exec.callId}:read-batch-${index}`;
145
- const log = {
146
- rootCallId: exec.rootCallId,
147
- parentCallId: exec.callId,
148
- subCallId,
149
- name: 'read',
150
- arguments: readArgs
151
- };
152
- if (session?.append) {
153
- session.append('tool/ptc-dispatch-start', log);
154
- }
155
- const prepared = await pipeline.prepare({
156
- callId: subCallId,
157
- rootCallId: exec.rootCallId,
158
- parent: exec.token,
159
- name: 'read',
160
- arguments: readArgs,
161
- agent: exec.agent,
162
- toolSet: exec.toolSet,
163
- signal: exec.signal
164
- });
165
- const outcome = prepared.kind === 'dispatch' ? await pipeline.dispatch(prepared.exec) : prepared;
166
- const result = await pipeline.complete(prepared.exec, outcome);
167
- if (session?.append) {
168
- session.append('tool/ptc-dispatch', {
169
- ...log,
170
- isError: result.isError,
171
- content: result.content,
172
- ...(result.error?.info ? { error: result.error.info } : {}),
173
- ...(result.meta === undefined ? {} : { meta: result.meta })
174
- });
175
- }
176
- for (const context of result.additionalContexts ?? []) {
177
- exec.deferContext(context);
178
- }
179
- if (result.isError) {
180
- if (state) state.filesFailed++;
181
- const msg = result.error?.message
182
- ?? result.content?.filter(b => b.type === 'text').map(b => b.text).join('\n')
183
- ?? 'Read failed';
184
- return {
185
- filePath: item.file_path,
186
- isError: true,
187
- text: `[ERROR] ${msg}`
188
- };
189
- }
190
- if (state) state.filesSucceeded++;
191
- const text = (result.content ?? [])
192
- .filter(block => block.type === 'text')
193
- .map(block => block.text)
194
- .join('\n');
195
- return {
196
- filePath: item.file_path,
197
- isError: false,
198
- text
399
+ const grepArgs = {
400
+ pattern,
401
+ ...(typeof args?.path === 'string' && args.path.trim() ? { path: args.path } : {}),
402
+ ...(typeof args?.include === 'string' && args.include.trim() ? { include: args.include } : {})
199
403
  };
404
+ const result = await dispatchSubCall(exec, 'grep', grepArgs, `${exec.callId}:find-grep-${idx}`);
405
+ return { pattern, result };
200
406
  }));
201
407
 
202
- const allFailed = subResults.every(entry => entry.isError);
203
- const rendered = subResults
204
- .map(entry => `=== [${entry.isError ? 'ERROR' : 'FILE'}] ${entry.filePath} ===\n${entry.text}`)
205
- .join('\n\n');
408
+ const runs = grepRuns.map(({ pattern, result }, index) => {
409
+ const text = (result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
410
+ const parsed = result.isError ? [] : parseGrepOutput(text, result.meta);
411
+ return { pattern, result, index, text, parsed, status: searchStatus(text, result.meta, parsed.length) };
412
+ });
413
+ const output = new FindOutput(config.maxTotalBytes);
414
+ appendSearchNotices(output, runs);
415
+ if (runs.every(run => run.result.isError)) {
416
+ throw new HarnessError(output.finish('All grep patterns failed.'), 'FIND_AND_READ_GREP_FAILED');
417
+ }
206
418
 
207
- if (allFailed) {
208
- throw new HarnessError(rendered, 'READ_BATCH_ALL_FAILED');
419
+ // Aggregate and deduplicate matches by normalized file path + line number
420
+ const filesMap = new Map();
421
+ let firstSeenOrder = 0;
422
+
423
+ for (const { pattern, result, parsed } of runs) {
424
+ if (result.isError) continue;
425
+ for (const m of parsed) {
426
+ const norm = normalizePath(m.path, cwd) || m.path;
427
+ let entry = filesMap.get(norm);
428
+ if (!entry) {
429
+ entry = {
430
+ displayPath: m.path,
431
+ sortPath: norm,
432
+ linePatterns: new Map(),
433
+ order: firstSeenOrder++,
434
+ patterns: new Set(),
435
+ lines: new Map(),
436
+ shown: new Set()
437
+ };
438
+ filesMap.set(norm, entry);
439
+ }
440
+ entry.patterns.add(pattern);
441
+ if (!entry.linePatterns.has(m.lineNumber)) entry.linePatterns.set(m.lineNumber, new Set());
442
+ entry.linePatterns.get(m.lineNumber).add(pattern);
443
+ if (!entry.lines.has(m.lineNumber)) {
444
+ entry.lines.set(m.lineNumber, m.line);
445
+ }
446
+ }
209
447
  }
210
- return rendered;
448
+
449
+ if (filesMap.size === 0) {
450
+ const incomplete = runs.some(run => run.result.isError || run.status.truncated);
451
+ output.appendClipped(`${incomplete ? 'No retained matches available' : 'No matches found'} for pattern(s): ${uniquePatterns.join(', ')}`);
452
+ return output.finish(output.reduced ? '[Query text truncated to fit byte budget.]' : '');
453
+ }
454
+
455
+ if (state) {
456
+ state.findFilesMatched += filesMap.size;
457
+ }
458
+
459
+ // Rank files by distinct pattern coverage first, then match count, then discovery order
460
+ const rankedFiles = [...filesMap.values()].sort((a, b) =>
461
+ (b.patterns.size - a.patterns.size)
462
+ || (Math.min(10, b.lines.size) - Math.min(10, a.lines.size))
463
+ || (a.order - b.order)
464
+ );
465
+
466
+ output.append(`Found retained matches across ${filesMap.size} file(s):`);
467
+ const canRead = contextLines > 0 && ctx.tools.get('read', exec.agent) !== undefined;
468
+ if (!canRead) {
469
+ appendMatchListing(output, rankedFiles, config.maxLineChars);
470
+ return finishFindOutput(output, rankedFiles);
471
+ }
472
+
473
+ const candidates = rankedFiles.flatMap(file =>
474
+ splitWindows(mergeMatchWindows([...file.lines.keys()], contextLines), config.maxWindowLines)
475
+ .map(win => ({ file, win }))
476
+ );
477
+ const chosenTasks = selectReadWindows(candidates, { maxFiles, maxWindows, maxReadLines: config.maxReadLines });
478
+
479
+ // Complete a bounded batch before spending more of the output or I/O budget.
480
+ let cursor = 0;
481
+ while (cursor < chosenTasks.length && output.remaining() >= 128) {
482
+ exec.signal.throwIfAborted();
483
+ const slots = Math.min(config.readConcurrency, Math.max(1, Math.floor(output.remaining() / 1024)));
484
+ const batch = chosenTasks.slice(cursor, cursor + slots);
485
+ const previousBytes = output.bytes;
486
+ if (state) {
487
+ state.findReadBatches++;
488
+ state.findWindowsRead += batch.length;
489
+ state.findLinesRequested += batch.reduce((sum, task) => sum + task.win.end - task.win.start + 1, 0);
490
+ }
491
+ const readResults = await Promise.all(batch.map(async (task, index) => {
492
+ exec.signal.throwIfAborted();
493
+ const result = await dispatchSubCall(
494
+ exec, 'read',
495
+ { file_path: task.file.displayPath, offset: task.win.start, limit: task.win.end - task.win.start + 1 },
496
+ `${exec.callId}:find-read-${cursor + index}`
497
+ );
498
+ return { ...task, result };
499
+ }));
500
+ exec.signal.throwIfAborted();
501
+ for (const item of readResults) {
502
+ if (item.result.isError) {
503
+ const message = item.result.error?.message
504
+ ?? (item.result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
505
+ output.appendClipped(`=== ${item.file.displayPath}:${item.win.start}-${item.win.end} [READ ERROR] ===\n${message}`);
506
+ } else {
507
+ const rawText = (item.result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
508
+ appendSlice(output, item, stripReadEnvelope(rawText), config.maxLineChars);
509
+ }
510
+ }
511
+ cursor += batch.length;
512
+ if (output.bytes === previousBytes) break;
513
+ }
514
+ return finishFindOutput(output, rankedFiles);
211
515
  }
212
- }, { when: ({ agent }) => ctx.tools.get('read', agent) !== undefined });
516
+ }, { when: ({ agent }) => ctx.tools.get('grep', agent) !== undefined });
213
517
  }
214
518
 
215
519
  if (config.editBatch) {
@@ -225,6 +529,10 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
225
529
  additionalProperties: false,
226
530
  required: ['edits'],
227
531
  properties: {
532
+ atomic: {
533
+ type: 'boolean',
534
+ description: `When true, pre-flight validate all edits and abort without writing if any edit fails. Defaults to ${config.atomicEdits}.`
535
+ },
228
536
  edits: {
229
537
  type: 'array',
230
538
  minItems: 1,
@@ -282,6 +590,7 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
282
590
  );
283
591
  }
284
592
 
593
+ const isAtomic = typeof args?.atomic === 'boolean' ? args.atomic : config.atomicEdits;
285
594
  const session = exec.agent?.session;
286
595
  const state = stateFor(session);
287
596
  if (state) {
@@ -289,16 +598,99 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
289
598
  state.editsRequested += edits.length;
290
599
  }
291
600
 
601
+ const fsService = ctx.get ? ctx.get('fs') : undefined;
602
+ const cwdService = ctx.get ? ctx.get('workingDirectory') : undefined;
603
+ const cwd = cwdService?.get?.(session) ?? '';
604
+
605
+ const editsByPath = new Map();
606
+ const preflightBlockedPaths = new Map();
607
+ const invalidEntries = new Map();
608
+
609
+ for (let index = 0; index < edits.length; index++) {
610
+ const item = edits[index];
611
+ if (!item || typeof item.file_path !== 'string' || !item.file_path.trim() || typeof item.old_string !== 'string' || typeof item.new_string !== 'string') {
612
+ invalidEntries.set(index, 'Invalid edit entry: file_path, old_string, and new_string must be strings.');
613
+ continue;
614
+ }
615
+ if (!item.old_string || item.old_string === item.new_string) {
616
+ invalidEntries.set(index, 'Invalid edit entry: old_string must be non-empty and differ from new_string.');
617
+ continue;
618
+ }
619
+ const pathKey = normalizePath(item.file_path, cwd) || item.file_path.trim();
620
+ if (!editsByPath.has(pathKey)) editsByPath.set(pathKey, []);
621
+ editsByPath.get(pathKey).push({ index, item });
622
+ }
623
+
624
+ for (const [pathKey, group] of editsByPath.entries()) {
625
+ const shouldPreflight = isAtomic || group.length > 1;
626
+ if (!shouldPreflight) continue;
627
+ if (group.some(({ item }) => typeof item.sandbox_permissions === 'string')) continue;
628
+
629
+ const probe = await readFileForPreflight(group[0].item.file_path, cwd, fsService, exec.signal);
630
+ if (!probe) continue;
631
+ if (!probe.ok && probe.missing) {
632
+ preflightBlockedPaths.set(
633
+ pathKey,
634
+ `Pre-flight failed (${group[0].item.file_path}): target file does not exist (no changes written).`
635
+ );
636
+ continue;
637
+ }
638
+ if (probe.ok) {
639
+ const sim = simulateEditsOnContent(probe.content, group);
640
+ if (!sim.ok) {
641
+ preflightBlockedPaths.set(pathKey, sim.reason);
642
+ }
643
+ }
644
+ }
645
+
646
+ if (isAtomic && (invalidEntries.size > 0 || preflightBlockedPaths.size > 0)) {
647
+ if (state) state.editsFailed += edits.length;
648
+ const reasons = [];
649
+ for (const [index, msg] of invalidEntries.entries()) {
650
+ reasons.push(`=== [ERROR] ${String(edits[index]?.file_path ?? `<invalid-${index}>`)} ===\n[ERROR] ${msg}`);
651
+ }
652
+ for (const [, reason] of preflightBlockedPaths.entries()) {
653
+ reasons.push(`=== [ERROR] Pre-flight ===\n[ERROR] ${reason}`);
654
+ }
655
+ throw new HarnessError(
656
+ `${reasons.join('\n\n')}\n\n[Atomic edit_batch aborted before any file was modified.]`,
657
+ 'EDIT_BATCH_PREFLIGHT_FAILED'
658
+ );
659
+ }
660
+
661
+ const failedPaths = new Set(preflightBlockedPaths.keys());
292
662
  const subResults = [];
663
+
293
664
  for (let index = 0; index < edits.length; index++) {
294
665
  exec.signal.throwIfAborted();
295
666
  const item = edits[index];
296
- if (!item || typeof item.file_path !== 'string' || !item.file_path.trim() || typeof item.old_string !== 'string' || typeof item.new_string !== 'string') {
667
+ if (invalidEntries.has(index)) {
297
668
  if (state) state.editsFailed++;
298
669
  subResults.push({
299
670
  filePath: String(item?.file_path ?? `<invalid-${index}>`),
300
671
  isError: true,
301
- text: 'Invalid edit entry: file_path, old_string, and new_string must be strings.'
672
+ text: `[ERROR] ${invalidEntries.get(index)}`
673
+ });
674
+ continue;
675
+ }
676
+
677
+ const pathKey = normalizePath(item.file_path, cwd) || item.file_path.trim();
678
+ if (preflightBlockedPaths.has(pathKey)) {
679
+ if (state) state.editsFailed++;
680
+ subResults.push({
681
+ filePath: item.file_path,
682
+ isError: true,
683
+ text: `[ERROR] ${preflightBlockedPaths.get(pathKey)}`
684
+ });
685
+ continue;
686
+ }
687
+
688
+ if (failedPaths.has(pathKey)) {
689
+ if (state) state.editsFailed++;
690
+ subResults.push({
691
+ filePath: item.file_path,
692
+ isError: true,
693
+ text: `[ERROR] Skipped edit #${index + 1} after an earlier failure on ${item.file_path} to prevent partial corruption.`
302
694
  });
303
695
  continue;
304
696
  }
@@ -312,47 +704,10 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
312
704
  ...(typeof item.sandbox_permissions === 'string' ? { sandbox_permissions: item.sandbox_permissions } : {})
313
705
  };
314
706
 
315
- const subCallId = `${exec.callId}:edit-batch-${index}`;
316
- const log = {
317
- rootCallId: exec.rootCallId,
318
- parentCallId: exec.callId,
319
- subCallId,
320
- name: 'edit',
321
- arguments: editArgs
322
- };
323
- if (session?.append) {
324
- session.append('tool/ptc-dispatch-start', log);
325
- }
326
-
327
- const prepared = await pipeline.prepare({
328
- callId: subCallId,
329
- rootCallId: exec.rootCallId,
330
- parent: exec.token,
331
- name: 'edit',
332
- arguments: editArgs,
333
- agent: exec.agent,
334
- toolSet: exec.toolSet,
335
- signal: exec.signal
336
- });
337
- const outcome = prepared.kind === 'dispatch' ? await pipeline.dispatch(prepared.exec) : prepared;
338
- const result = await pipeline.complete(prepared.exec, outcome);
339
-
340
- if (session?.append) {
341
- session.append('tool/ptc-dispatch', {
342
- ...log,
343
- isError: result.isError,
344
- content: result.content,
345
- ...(result.error?.info ? { error: result.error.info } : {}),
346
- ...(result.meta === undefined ? {} : { meta: result.meta })
347
- });
348
- }
349
-
350
- for (const context of result.additionalContexts ?? []) {
351
- exec.deferContext(context);
352
- }
353
-
707
+ const result = await dispatchSubCall(exec, 'edit', editArgs, `${exec.callId}:edit-batch-${index}`);
354
708
  if (result.isError) {
355
709
  if (state) state.editsFailed++;
710
+ failedPaths.add(pathKey);
356
711
  const msg = result.error?.message
357
712
  ?? result.content?.filter(b => b.type === 'text').map(b => b.text).join('\n')
358
713
  ?? 'Edit failed';
@@ -391,18 +746,7 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
391
746
  return {
392
747
  getStats(session) {
393
748
  const s = session ? statsBySession.get(session) : null;
394
- return s
395
- ? { ...s }
396
- : {
397
- batchesExecuted: 0,
398
- filesRequested: 0,
399
- filesSucceeded: 0,
400
- filesFailed: 0,
401
- editBatchesExecuted: 0,
402
- editsRequested: 0,
403
- editsSucceeded: 0,
404
- editsFailed: 0
405
- };
749
+ return s ? { ...s } : emptyStats();
406
750
  }
407
751
  };
408
752
  }