dsh-awesome-optimizer 3.1.0 → 3.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.
@@ -6,6 +6,11 @@ import { ToolExecutionPipeline } from '@deepseek-ai/dsh-tools/pipeline';
6
6
  import { normalizePath } from './runtime-policy.js';
7
7
  import { buildEditFailureHint } from './edit-recovery.js';
8
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';
9
14
 
10
15
  export const FIND_AND_READ_TOOL = 'find_and_read';
11
16
  export const EDIT_BATCH_TOOL = 'edit_batch';
@@ -20,6 +25,9 @@ export const CompositeToolsConfig = z.object({
20
25
  defaultContextLines: z.number().step(1).min(0).max(25).default(5),
21
26
  maxFiles: z.number().step(1).min(1).max(12).default(5),
22
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),
23
31
  maxBatchEdits: z.number().step(1).min(1).max(25).default(10),
24
32
  maxLineChars: z.number().step(1).min(100).max(50000).default(1500),
25
33
  maxTotalBytes: z.number().step(1).min(512).max(1024 * 1024).default(24576),
@@ -31,10 +39,23 @@ export const CompositeToolsConfig = z.object({
31
39
  * or structured `meta.matches` into flat `{ path, lineNumber, line }` records.
32
40
  */
33
41
  export function parseGrepOutput(text, meta) {
34
- if (meta && Array.isArray(meta.matches)) {
35
- return meta.matches
36
- .filter(m => m && typeof m.path === 'string' && Number.isInteger(m.lineNumber) && m.lineNumber >= 1)
37
- .map(m => ({ path: m.path, lineNumber: m.lineNumber, line: String(m.line ?? '') }));
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 ?? '') }));
38
59
  }
39
60
  if (typeof text !== 'string' || !text.trim() || /^No matches found\b/i.test(text.trim())) {
40
61
  return [];
@@ -113,20 +134,6 @@ function stripReadEnvelope(rawText) {
113
134
  return text.trimEnd();
114
135
  }
115
136
 
116
- function formatSliceText(sliceText, matchedLines, maxLineChars) {
117
- const matchedSet = new Set(matchedLines);
118
- const capped = truncateLongLines(sliceText, maxLineChars);
119
- return capped
120
- .split('\n')
121
- .map(line => {
122
- const m = line.match(/^(\d+):\s?(.*)$/);
123
- if (!m) return ` ${line}`;
124
- const lineNum = Number(m[1]);
125
- return `${matchedSet.has(lineNum) ? '>' : ' '} ${line}`;
126
- })
127
- .join('\n');
128
- }
129
-
130
137
  function countOccurrences(haystack, needle) {
131
138
  if (!needle) return 0;
132
139
  let count = 0;
@@ -140,12 +147,15 @@ function countOccurrences(haystack, needle) {
140
147
 
141
148
  async function readFileForPreflight(filePath, cwd, fsService, signal) {
142
149
  if (fsService) {
150
+ // A service failure is authoritative; never bypass sandbox or cancellation through local disk.
143
151
  if (typeof fsService.readFile === 'function') {
144
152
  try {
145
- const content = await fsService.readFile(filePath, { cwd, signal });
146
- if (typeof content === 'string') return { ok: true, content };
153
+ const content = await fsService.readFile(filePath, { cwd, signal, encoding: 'utf8' });
154
+ return typeof content === 'string' ? { ok: true, content } : null;
147
155
  } catch (err) {
148
- if (err?.code === 'ENOENT') return { ok: false, missing: true };
156
+ return err?.code === 'ENOENT' || err?.code === 'FS_NOT_FOUND'
157
+ ? { ok: false, missing: true }
158
+ : null;
149
159
  }
150
160
  }
151
161
  if (typeof fsService.readText === 'function') {
@@ -154,31 +164,23 @@ async function readFileForPreflight(filePath, cwd, fsService, signal) {
154
164
  ? await fsService.resolve(filePath, { cwd, signal })
155
165
  : filePath;
156
166
  const content = await fsService.readText(target, signal);
157
- if (typeof content === 'string') return { ok: true, content };
167
+ return typeof content === 'string' ? { ok: true, content } : null;
158
168
  } catch (err) {
159
- if (err?.code === 'ENOENT' || err?.code === 'FS_NOT_FOUND') return { ok: false, missing: true };
169
+ return err?.code === 'ENOENT' || err?.code === 'FS_NOT_FOUND'
170
+ ? { ok: false, missing: true }
171
+ : null;
160
172
  }
161
173
  }
174
+ return null;
162
175
  }
163
176
 
177
+ // Standalone hosts without an fs service may use local preflight inspection.
164
178
  try {
165
179
  const resolved = path.isAbsolute(filePath) ? filePath : path.resolve(cwd || process.cwd(), filePath);
166
180
  const content = await fs.readFile(resolved, 'utf8');
167
181
  return { ok: true, content };
168
182
  } catch (err) {
169
- if (err?.code === 'ENOENT') {
170
- if (fsService && typeof fsService.resolve === 'function' && typeof fsService.stat === 'function') {
171
- try {
172
- const target = await fsService.resolve(filePath, { cwd, signal });
173
- const stat = await fsService.stat(target, signal);
174
- if (stat) return null;
175
- } catch {
176
- // ignore
177
- }
178
- }
179
- return { ok: false, missing: true };
180
- }
181
- return null;
183
+ return err?.code === 'ENOENT' ? { ok: false, missing: true } : null;
182
184
  }
183
185
  }
184
186
 
@@ -221,6 +223,8 @@ function emptyStats() {
221
223
  findExecuted: 0,
222
224
  findPatternsSearched: 0,
223
225
  findWindowsRead: 0,
226
+ findLinesRequested: 0,
227
+ findReadBatches: 0,
224
228
  findFilesMatched: 0,
225
229
  editBatchesExecuted: 0,
226
230
  editsRequested: 0,
@@ -352,7 +356,7 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
352
356
  const rawPatterns = [
353
357
  ...(typeof args?.pattern === 'string' ? [args.pattern] : []),
354
358
  ...(Array.isArray(args?.patterns) ? args.patterns : [])
355
- ].map(p => (typeof p === 'string' ? p.trim() : '')).filter(Boolean);
359
+ ].filter(p => typeof p === 'string' && p.length > 0);
356
360
 
357
361
  const uniquePatterns = [...new Set(rawPatterns)];
358
362
  if (uniquePatterns.length === 0) {
@@ -396,45 +400,41 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
396
400
  return { pattern, result };
397
401
  }));
398
402
 
399
- const allGrepErrored = grepRuns.every(r => r.result.isError);
400
- if (allGrepErrored) {
401
- const errors = grepRuns.map(({ pattern, result }) => {
402
- const msg = result.error?.message
403
- ?? result.content?.filter(b => b.type === 'text').map(b => b.text).join('\n')
404
- ?? 'grep failed';
405
- return `[${pattern}] ${msg}`;
406
- }).join('\n');
407
- throw new HarnessError(errors, 'FIND_AND_READ_GREP_FAILED');
403
+ const runs = grepRuns.map(({ pattern, result }, index) => {
404
+ const text = (result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
405
+ const parsed = result.isError ? [] : parseGrepOutput(text, result.meta);
406
+ return { pattern, result, index, text, parsed, status: searchStatus(text, result.meta, parsed.length) };
407
+ });
408
+ const output = new FindOutput(config.maxTotalBytes);
409
+ appendSearchNotices(output, runs);
410
+ if (runs.every(run => run.result.isError)) {
411
+ throw new HarnessError(output.finish('All grep patterns failed.'), 'FIND_AND_READ_GREP_FAILED');
408
412
  }
409
413
 
410
414
  // Aggregate and deduplicate matches by normalized file path + line number
411
415
  const filesMap = new Map();
412
- const grepWarnings = [];
413
416
  let firstSeenOrder = 0;
414
417
 
415
- for (const { pattern, result } of grepRuns) {
416
- if (result.isError) {
417
- const msg = result.error?.message
418
- ?? result.content?.filter(b => b.type === 'text').map(b => b.text).join('\n')
419
- ?? 'grep error';
420
- grepWarnings.push(`Pattern "${pattern}" failed: ${msg}`);
421
- continue;
422
- }
423
- const text = (result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
424
- const parsed = parseGrepOutput(text, result.meta);
418
+ for (const { pattern, result, parsed } of runs) {
419
+ if (result.isError) continue;
425
420
  for (const m of parsed) {
426
421
  const norm = normalizePath(m.path, cwd) || m.path;
427
422
  let entry = filesMap.get(norm);
428
423
  if (!entry) {
429
424
  entry = {
430
425
  displayPath: m.path,
426
+ sortPath: norm,
427
+ linePatterns: new Map(),
431
428
  order: firstSeenOrder++,
432
429
  patterns: new Set(),
433
- lines: new Map()
430
+ lines: new Map(),
431
+ shown: new Set()
434
432
  };
435
433
  filesMap.set(norm, entry);
436
434
  }
437
435
  entry.patterns.add(pattern);
436
+ if (!entry.linePatterns.has(m.lineNumber)) entry.linePatterns.set(m.lineNumber, new Set());
437
+ entry.linePatterns.get(m.lineNumber).add(pattern);
438
438
  if (!entry.lines.has(m.lineNumber)) {
439
439
  entry.lines.set(m.lineNumber, m.line);
440
440
  }
@@ -442,8 +442,9 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
442
442
  }
443
443
 
444
444
  if (filesMap.size === 0) {
445
- const base = `No matches found for pattern(s): ${uniquePatterns.join(', ')}`;
446
- return grepWarnings.length > 0 ? `${base}\n\n${grepWarnings.join('\n')}` : base;
445
+ const incomplete = runs.some(run => run.result.isError || run.status.truncated);
446
+ output.appendClipped(`${incomplete ? 'No retained matches available' : 'No matches found'} for pattern(s): ${uniquePatterns.join(', ')}`);
447
+ return output.finish(output.reduced ? '[Query text truncated to fit byte budget.]' : '');
447
448
  }
448
449
 
449
450
  if (state) {
@@ -457,117 +458,55 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
457
458
  || (a.order - b.order)
458
459
  );
459
460
 
461
+ output.append(`Found retained matches across ${filesMap.size} file(s):`);
460
462
  const canRead = contextLines > 0 && ctx.tools.get('read', exec.agent) !== undefined;
461
463
  if (!canRead) {
462
- const sections = [];
463
- let usedBytes = 0;
464
- let omitted = 0;
465
- for (const file of rankedFiles) {
466
- const rows = [...file.lines.entries()]
467
- .sort((a, b) => a[0] - b[0])
468
- .map(([ln, lineText]) => ` ${ln}: ${truncateLongLines(lineText, config.maxLineChars)}`);
469
- const block = `=== ${file.displayPath} (${file.lines.size} matches) ===\n${rows.join('\n')}`;
470
- const bLen = Buffer.byteLength(block, 'utf8') + 2;
471
- if (usedBytes + bLen <= config.maxTotalBytes) {
472
- sections.push(block);
473
- usedBytes += bLen;
474
- } else {
475
- omitted++;
476
- }
477
- }
478
- if (omitted > 0) {
479
- sections.push(`[find_and_read budget: omitted ${omitted} additional matched file(s) to stay within maxTotalBytes=${config.maxTotalBytes}]`);
480
- }
481
- if (grepWarnings.length > 0) sections.push(grepWarnings.join('\n'));
482
- return sections.join('\n\n');
483
- }
484
-
485
- // Build merged windows per top file and select round-robin up to maxWindows
486
- const selectedFiles = rankedFiles.slice(0, maxFiles);
487
- const fileWindows = selectedFiles.map(file => ({
488
- file,
489
- windows: mergeMatchWindows([...file.lines.keys()], contextLines),
490
- cursor: 0
491
- }));
492
-
493
- const chosenTasks = [];
494
- let progress = true;
495
- while (chosenTasks.length < maxWindows && progress) {
496
- progress = false;
497
- for (const fw of fileWindows) {
498
- if (chosenTasks.length >= maxWindows) break;
499
- if (fw.cursor < fw.windows.length) {
500
- chosenTasks.push({ file: fw.file, win: fw.windows[fw.cursor++] });
501
- progress = true;
502
- }
503
- }
464
+ appendMatchListing(output, rankedFiles, config.maxLineChars);
465
+ return finishFindOutput(output, rankedFiles);
504
466
  }
505
467
 
506
- if (state) {
507
- state.findWindowsRead += chosenTasks.length;
508
- }
468
+ const candidates = rankedFiles.flatMap(file =>
469
+ splitWindows(mergeMatchWindows([...file.lines.keys()], contextLines), config.maxWindowLines)
470
+ .map(win => ({ file, win }))
471
+ );
472
+ const chosenTasks = selectReadWindows(candidates, { maxFiles, maxWindows, maxReadLines: config.maxReadLines });
509
473
 
510
- const readResults = await Promise.all(chosenTasks.map(async (task, idx) => {
474
+ // Complete a bounded batch before spending more of the output or I/O budget.
475
+ let cursor = 0;
476
+ while (cursor < chosenTasks.length && output.remaining() >= 128) {
511
477
  exec.signal.throwIfAborted();
512
- const limit = task.win.end - task.win.start + 1;
513
- const result = await dispatchSubCall(
514
- exec,
515
- 'read',
516
- { file_path: task.file.displayPath, offset: task.win.start, limit },
517
- `${exec.callId}:find-read-${idx}`
518
- );
519
- return { ...task, result };
520
- }));
521
-
522
- const renderedBlocks = [];
523
- const overflowIndex = [];
524
- let usedBytes = 0;
525
-
526
- for (const item of readResults) {
527
- let block;
528
- if (item.result.isError) {
529
- const msg = item.result.error?.message
530
- ?? item.result.content?.filter(b => b.type === 'text').map(b => b.text).join('\n')
531
- ?? 'Read failed';
532
- block = `=== ${item.file.displayPath}:${item.win.start}-${item.win.end} [READ ERROR] ===\n${msg}`;
533
- } else {
534
- const rawText = (item.result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
535
- const cleanText = stripReadEnvelope(rawText);
536
- const formatted = formatSliceText(cleanText, item.win.matchedLines, config.maxLineChars);
537
- block = `=== ${item.file.displayPath}:${item.win.start}-${item.win.end} ===\n${formatted}`;
538
- }
539
-
540
- const bytes = Buffer.byteLength(block, 'utf8') + 2;
541
- if (renderedBlocks.length === 0 || usedBytes + bytes <= config.maxTotalBytes) {
542
- renderedBlocks.push(block);
543
- usedBytes += bytes;
544
- } else {
545
- overflowIndex.push(`${item.file.displayPath}: L${item.win.matchedLines.join(', L')}`);
478
+ const slots = Math.min(config.readConcurrency, Math.max(1, Math.floor(output.remaining() / 1024)));
479
+ const batch = chosenTasks.slice(cursor, cursor + slots);
480
+ const previousBytes = output.bytes;
481
+ if (state) {
482
+ state.findReadBatches++;
483
+ state.findWindowsRead += batch.length;
484
+ state.findLinesRequested += batch.reduce((sum, task) => sum + task.win.end - task.win.start + 1, 0);
546
485
  }
547
- }
548
-
549
- // Index any windows/files skipped due to maxWindows or maxFiles
550
- for (const fw of fileWindows) {
551
- for (let i = fw.cursor; i < fw.windows.length; i++) {
552
- overflowIndex.push(`${fw.file.displayPath}: L${fw.windows[i].matchedLines.join(', L')}`);
486
+ const readResults = await Promise.all(batch.map(async (task, index) => {
487
+ exec.signal.throwIfAborted();
488
+ const result = await dispatchSubCall(
489
+ exec, 'read',
490
+ { file_path: task.file.displayPath, offset: task.win.start, limit: task.win.end - task.win.start + 1 },
491
+ `${exec.callId}:find-read-${cursor + index}`
492
+ );
493
+ return { ...task, result };
494
+ }));
495
+ exec.signal.throwIfAborted();
496
+ for (const item of readResults) {
497
+ if (item.result.isError) {
498
+ const message = item.result.error?.message
499
+ ?? (item.result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
500
+ output.appendClipped(`=== ${item.file.displayPath}:${item.win.start}-${item.win.end} [READ ERROR] ===\n${message}`);
501
+ } else {
502
+ const rawText = (item.result.content ?? []).filter(b => b.type === 'text').map(b => b.text).join('\n');
503
+ appendSlice(output, item, stripReadEnvelope(rawText), config.maxLineChars);
504
+ }
553
505
  }
506
+ cursor += batch.length;
507
+ if (output.bytes === previousBytes) break;
554
508
  }
555
- for (let i = maxFiles; i < rankedFiles.length; i++) {
556
- const f = rankedFiles[i];
557
- const lineList = [...f.lines.keys()].sort((a, b) => a - b).slice(0, 6);
558
- const suffix = f.lines.size > 6 ? `, ... (${f.lines.size} total)` : '';
559
- overflowIndex.push(`${f.displayPath}: L${lineList.join(', L')}${suffix}`);
560
- }
561
-
562
- const header = `Found matches across ${filesMap.size} file(s) (showing ${renderedBlocks.length} merged slice(s)):`;
563
- const parts = [header, ...renderedBlocks];
564
- if (overflowIndex.length > 0) {
565
- parts.push(`--- Additional matches (index only) ---\n${overflowIndex.join('\n')}`);
566
- }
567
- if (grepWarnings.length > 0) {
568
- parts.push(grepWarnings.join('\n'));
569
- }
570
- return parts.join('\n\n');
509
+ return finishFindOutput(output, rankedFiles);
571
510
  }
572
511
  }, { when: ({ agent }) => ctx.tools.get('grep', agent) !== undefined });
573
512
  }
@@ -668,6 +607,10 @@ export function applyCompositeTools(ctx, rawConfig = {}) {
668
607
  invalidEntries.set(index, 'Invalid edit entry: file_path, old_string, and new_string must be strings.');
669
608
  continue;
670
609
  }
610
+ if (!item.old_string || item.old_string === item.new_string) {
611
+ invalidEntries.set(index, 'Invalid edit entry: old_string must be non-empty and differ from new_string.');
612
+ continue;
613
+ }
671
614
  const pathKey = normalizePath(item.file_path, cwd) || item.file_path.trim();
672
615
  if (!editsByPath.has(pathKey)) editsByPath.set(pathKey, []);
673
616
  editsByPath.get(pathKey).push({ index, item });
@@ -0,0 +1,34 @@
1
+ import type { Context } from '@deepseek-ai/cordis';
2
+ import type z from '@deepseek-ai/schemastery';
3
+
4
+ export interface ContextCompactionOptions {
5
+ /** Enable the optimizer-owned bridge to the host compaction service. Default: true. */
6
+ enabled?: boolean;
7
+ /** Compact and retry provider-confirmed context-window overflows. Default: true. */
8
+ recoverOverflow?: boolean;
9
+ }
10
+
11
+ export interface ContextCompactionStats {
12
+ pressureAttempts: number;
13
+ pressureCompactions: number;
14
+ overflowAttempts: number;
15
+ overflowCompactions: number;
16
+ failures: number;
17
+ }
18
+
19
+ export interface ContextCompactionService {
20
+ getStats(): ContextCompactionStats;
21
+ }
22
+
23
+ export declare const CONTEXT_WINDOW_EXCEEDED_CODE: 'CONTEXT_WINDOW_EXCEEDED';
24
+ export declare const ContextCompactionConfig: z<ContextCompactionOptions>;
25
+ export declare function applyContextCompaction(
26
+ ctx: Context,
27
+ rawConfig?: ContextCompactionOptions
28
+ ): ContextCompactionService | null;
29
+ export declare const name: string;
30
+ export declare const inject: string[];
31
+ export declare const Config: z<ContextCompactionOptions>;
32
+ export declare function apply(ctx: Context, config?: ContextCompactionOptions): void;
33
+ declare const plugin: { name: typeof name; inject: typeof inject; Config: typeof Config; apply: typeof apply };
34
+ export default plugin;
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Opt into the host compaction seam without changing the official agent loop.
3
+ * This keeps long sessions below provider context pressure when the selected
4
+ * compaction backend is present, even if its own automatic listeners are off.
5
+ */
6
+ import z from '@deepseek-ai/schemastery';
7
+
8
+ export const CONTEXT_WINDOW_EXCEEDED_CODE = 'CONTEXT_WINDOW_EXCEEDED';
9
+
10
+ export const ContextCompactionConfig = z.object({
11
+ enabled: z.boolean().default(true),
12
+ recoverOverflow: z.boolean().default(true)
13
+ });
14
+
15
+ function compactService(ctx) {
16
+ try {
17
+ const service = ctx.get('compaction');
18
+ return service && typeof service.compactIfNeeded === 'function' ? service : null;
19
+ } catch {
20
+ return null;
21
+ }
22
+ }
23
+
24
+ function log(ctx, level, message) {
25
+ try { ctx.logger?.[level]?.(`[optimizer-context-compaction] ${message}`); } catch {}
26
+ }
27
+
28
+ export function applyContextCompaction(ctx, rawConfig = {}) {
29
+ const config = ContextCompactionConfig(rawConfig);
30
+ if (!config.enabled) return null;
31
+
32
+ let disposed = false;
33
+ let pressureAttempts = 0;
34
+ let pressureCompactions = 0;
35
+ let overflowAttempts = 0;
36
+ let overflowCompactions = 0;
37
+ let failures = 0;
38
+
39
+ ctx.on('dispose', () => { disposed = true; });
40
+
41
+ ctx.on('agent/pre-step', async ({ agent, signal }, next) => {
42
+ if (!disposed && !signal?.aborted) {
43
+ const compaction = compactService(ctx);
44
+ if (compaction) {
45
+ pressureAttempts++;
46
+ try {
47
+ const result = await compaction.compactIfNeeded(agent, 'pressure', signal);
48
+ if (result) pressureCompactions++;
49
+ } catch (error) {
50
+ if (!signal?.aborted) {
51
+ failures++;
52
+ log(ctx, 'warn', `pressure check failed: ${error instanceof Error ? error.message : String(error)}`);
53
+ }
54
+ }
55
+ }
56
+ }
57
+ return next();
58
+ });
59
+
60
+ if (config.recoverOverflow) {
61
+ ctx.on('agent/request-error', async ({ agent, failure, signal }, next) => {
62
+ if (
63
+ disposed || signal?.aborted || failure?.code !== CONTEXT_WINDOW_EXCEEDED_CODE
64
+ ) return next();
65
+
66
+ const compaction = compactService(ctx);
67
+ if (!compaction) return next();
68
+
69
+ const generation = agent.session?.surface?.replaceGeneration;
70
+ overflowAttempts++;
71
+ try {
72
+ const result = await compaction.compactIfNeeded(agent, 'context-overflow', signal);
73
+ const advanced = Number.isFinite(generation)
74
+ && agent.session?.surface?.replaceGeneration > generation;
75
+ if (result || advanced) overflowCompactions++;
76
+ if (!signal?.aborted && advanced) return { kind: 'retry' };
77
+ } catch (error) {
78
+ if (!signal?.aborted) {
79
+ failures++;
80
+ log(ctx, 'warn', `overflow recovery failed: ${error instanceof Error ? error.message : String(error)}`);
81
+ if (Number.isFinite(generation) && agent.session?.surface?.replaceGeneration > generation) {
82
+ overflowCompactions++;
83
+ return { kind: 'retry' };
84
+ }
85
+ }
86
+ }
87
+ return next();
88
+ });
89
+ }
90
+
91
+ return {
92
+ getStats() {
93
+ return {
94
+ pressureAttempts,
95
+ pressureCompactions,
96
+ overflowAttempts,
97
+ overflowCompactions,
98
+ failures
99
+ };
100
+ }
101
+ };
102
+ }
103
+
104
+ export const name = 'agent-optimizer-context-compaction';
105
+ export const inject = ['agent'];
106
+ export const Config = ContextCompactionConfig;
107
+ export function apply(ctx, config = {}) {
108
+ const service = applyContextCompaction(ctx, config);
109
+ if (service) ctx.provide('contextCompaction', service);
110
+ }
111
+ export default { name, inject, Config, apply };
@@ -15,7 +15,7 @@ export const GUIDANCE = [
15
15
  'This efficiency guidance NEVER overrides the user, correctness, permission checks, required verification, or cancellation.'
16
16
  ].join('\n');
17
17
 
18
- export const REMINDER = 'Accelerate execution: batch remaining actions and conclude if criteria are met; NEVER omit verification.';
18
+ export const REMINDER = 'Batch remaining actions; conclude only if criteria are met; NEVER omit verification.';
19
19
 
20
20
  export const ConvergenceConfig = z.object({
21
21
  enabled: z.boolean().default(true),
@@ -146,7 +146,7 @@ export function applyConvergence(ctx, raw = {}) {
146
146
  source: {
147
147
  kind: SOURCE,
148
148
  form: 'notice',
149
- summary: 'Accelerate execution and conclude when criteria are met.'
149
+ summary: 'Batch remaining actions; conclude only if criteria are met.'
150
150
  }
151
151
  });
152
152
  return { ...decision, messages: [...decision.messages, reminder] };
@@ -312,8 +312,8 @@ export function applyCustomPrompt(ctx, rawConfig = {}) {
312
312
 
313
313
  const manager = new CustomPromptManager(config);
314
314
 
315
- // If placement is 'suffix' or 'section', register section directly with systemPrompt service
316
- if ((config.placement === 'suffix' || config.placement === 'section') && ctx.systemPrompt?.section) {
315
+ // For suffix placement, register the section directly with the systemPrompt service
316
+ if (config.placement === 'suffix' && ctx.systemPrompt?.section) {
317
317
  ctx.systemPrompt.section({
318
318
  name: config.sectionName,
319
319
  order: config.sectionOrder,
@@ -325,7 +325,7 @@ export function applyCustomPrompt(ctx, rawConfig = {}) {
325
325
  });
326
326
  }
327
327
 
328
- // Also hook into waterfall 'system-prompt/assemble' for prefix or replace-persona-suffix
328
+ // Hook into the waterfall for both supported placement modes.
329
329
  ctx.on('system-prompt/assemble', (assembly, context) => {
330
330
  const wsRoot = resolveWorkspaceRoot(ctx, context);
331
331
  const customText = manager.getEffectivePrompt(wsRoot);
@@ -386,6 +386,7 @@ async function readTargetForRecovery(filePath, cwd, fsService, signal, maxInspec
386
386
  if (!filePath || typeof filePath !== 'string') return null;
387
387
 
388
388
  if (fsService) {
389
+ // A service failure is authoritative; never bypass sandbox or cancellation through local disk.
389
390
  if (typeof fsService.readFile === 'function') {
390
391
  try {
391
392
  const content = await fsService.readFile(filePath, { cwd, signal, encoding: 'utf8' });
@@ -404,11 +405,13 @@ async function readTargetForRecovery(filePath, cwd, fsService, signal, maxInspec
404
405
  if (typeof content !== 'string') return null;
405
406
  return Buffer.byteLength(content, 'utf8') <= maxInspectBytes ? content : null;
406
407
  } catch {
407
- // Fall through to disk
408
+ return null;
408
409
  }
409
410
  }
411
+ return null;
410
412
  }
411
413
 
414
+ // Standalone hosts without an fs service may use a bounded local inspection.
412
415
  try {
413
416
  const resolved = path.isAbsolute(filePath) ? filePath : path.resolve(cwd || process.cwd(), filePath);
414
417
  const stat = await fs.stat(resolved);