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.
- package/README.md +26 -20
- package/README.zh.md +26 -20
- package/cache-diagnostics.md +19 -0
- package/cordis.patch.yml +4 -0
- package/outline-benchmark.md +37 -0
- package/outline-benchmark.windows.json +184 -0
- package/package.json +11 -2
- package/scripts/benchmark-outline.mjs +100 -0
- package/src/ast-outline-config.yml +1 -0
- package/src/ast-outline.js +143 -0
- package/src/cache-diagnostics.js +218 -0
- package/src/code-lexical.js +131 -0
- package/src/code-outline.js +126 -153
- package/src/composite-tools.d.ts +8 -0
- package/src/composite-tools.js +105 -162
- package/src/context-compaction.d.ts +34 -0
- package/src/context-compaction.js +111 -0
- package/src/convergence.js +2 -2
- package/src/custom-prompt.js +3 -3
- package/src/edit-recovery.js +4 -1
- package/src/find-read-output.js +175 -0
- package/src/find-read-ranking.js +63 -0
- package/src/index.d.ts +8 -0
- package/src/index.js +10 -2
- package/src/outline-cache.js +36 -0
- package/src/persistent-eval/daemon-pool.js +21 -0
- package/src/persistent-eval/index.js +18 -10
- package/src/persistent-eval/stdio-runner.js +46 -14
- package/src/persistent-eval/workers/node_worker.mjs +44 -18
- package/src/persistent-eval/workers/py_worker.py +44 -2
- package/src/read-guard.d.ts +28 -0
- package/src/read-guard.js +152 -38
- package/src/telemetry.d.ts +4 -0
- package/src/telemetry.js +20 -12
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { truncateLongLines } from './read-guard.js';
|
|
3
|
+
|
|
4
|
+
const byteLength = text => Buffer.byteLength(text, 'utf8');
|
|
5
|
+
|
|
6
|
+
export function clipUtf8(text, maxBytes, suffix = ' ... [truncated]') {
|
|
7
|
+
if (byteLength(text) <= maxBytes) return text;
|
|
8
|
+
if (maxBytes <= byteLength(suffix)) return '';
|
|
9
|
+
const buffer = Buffer.from(text, 'utf8');
|
|
10
|
+
let end = maxBytes - byteLength(suffix);
|
|
11
|
+
while (end > 0 && (buffer[end] & 0xc0) === 0x80) end--;
|
|
12
|
+
return buffer.subarray(0, end).toString('utf8') + suffix;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Reserve room for recovery indexes and a final omission notice. */
|
|
16
|
+
export class FindOutput {
|
|
17
|
+
constructor(maxBytes) {
|
|
18
|
+
this.maxBytes = maxBytes;
|
|
19
|
+
this.reserve = Math.min(2048, Math.floor(maxBytes / 3));
|
|
20
|
+
this.parts = [];
|
|
21
|
+
this.bytes = 0;
|
|
22
|
+
this.reduced = false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
remaining() {
|
|
26
|
+
return Math.max(0, this.maxBytes - this.reserve - this.bytes - (this.parts.length ? 2 : 0));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
append(text) {
|
|
30
|
+
if (!text) return true;
|
|
31
|
+
if (byteLength(text) > this.remaining()) return false;
|
|
32
|
+
this.bytes += byteLength(text) + (this.parts.length ? 2 : 0);
|
|
33
|
+
this.parts.push(text);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
appendClipped(text, cap = this.remaining()) {
|
|
38
|
+
const clipped = clipUtf8(text, Math.min(cap, this.remaining()));
|
|
39
|
+
if (clipped !== text) this.reduced = true;
|
|
40
|
+
return this.append(clipped);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
finish(note = '') {
|
|
44
|
+
this.reserve = 0;
|
|
45
|
+
if (note) this.appendClipped(note);
|
|
46
|
+
return this.parts.join('\n\n');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function searchStatus(text, meta, retainedCount) {
|
|
51
|
+
const header = text.match(/^Found (\d+) of (\d+) matches\s*$/m);
|
|
52
|
+
const recovery = text.match(/^\(?Full grep result stored at:[^\r\n]+/m)?.[0]?.replace(/\)$/, '')
|
|
53
|
+
?? text.match(/^\(?The complete result could not be saved[^\r\n]+/m)?.[0]?.replace(/\)$/, '')
|
|
54
|
+
?? '';
|
|
55
|
+
const total = header ? Number(header[2]) : (Number.isFinite(meta?.total) ? meta.total : undefined);
|
|
56
|
+
const truncated = Boolean(header || meta?.truncated || (total !== undefined && total > retainedCount));
|
|
57
|
+
return { truncated, total, retained: header ? Number(header[1]) : retainedCount, recovery };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function appendSearchNotices(output, runs) {
|
|
61
|
+
const incomplete = runs.filter(run => run.result.isError || run.status.truncated);
|
|
62
|
+
if (!incomplete.length) return;
|
|
63
|
+
const summary = incomplete.map(run => `#${run.index + 1} ${run.result.isError ? 'failed' : 'truncated'}`).join(', ');
|
|
64
|
+
output.append(`Search incomplete: ${summary}. Counts below cover retained matches only.`);
|
|
65
|
+
const cap = Math.floor(Math.min(4096, output.remaining() / 2) / incomplete.length);
|
|
66
|
+
for (const run of incomplete) {
|
|
67
|
+
let detail;
|
|
68
|
+
if (run.result.isError) {
|
|
69
|
+
const message = run.result.error?.message || run.text || 'grep failed';
|
|
70
|
+
detail = `Pattern #${run.index + 1} failed: ${message}`;
|
|
71
|
+
} else {
|
|
72
|
+
const counts = run.status.total === undefined ? 'partial results' : `${run.status.retained} of ${run.status.total} matches`;
|
|
73
|
+
const recovery = run.status.recovery.includes('could not be saved')
|
|
74
|
+
? 'spill could not be saved; narrow search.'
|
|
75
|
+
: (run.status.recovery || 'narrow search.');
|
|
76
|
+
detail = `#${run.index + 1}: ${counts}; ${recovery}`;
|
|
77
|
+
}
|
|
78
|
+
output.appendClipped(detail, cap);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Split chained merges before dispatch; each returned window contains a hit. */
|
|
83
|
+
export function splitWindows(windows, maxLines) {
|
|
84
|
+
const bounded = [];
|
|
85
|
+
for (const window of windows) {
|
|
86
|
+
for (let start = window.start; start <= window.end; start += maxLines) {
|
|
87
|
+
const end = Math.min(window.end, start + maxLines - 1);
|
|
88
|
+
const matchedLines = window.matchedLines.filter(line => line >= start && line <= end);
|
|
89
|
+
if (matchedLines.length) bounded.push({ start, end, matchedLines });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return bounded;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function appendSlice(output, task, cleanText, maxLineChars) {
|
|
96
|
+
const matched = new Set(task.win.matchedLines);
|
|
97
|
+
const rows = truncateLongLines(cleanText, maxLineChars).split('\n').map(text => {
|
|
98
|
+
const line = text.match(/^(\d+):\s?(.*)$/);
|
|
99
|
+
const number = line ? Number(line[1]) : undefined;
|
|
100
|
+
return { number, text: `${matched.has(number) ? '>' : ' '} ${text}` };
|
|
101
|
+
});
|
|
102
|
+
const header = `=== ${task.file.displayPath}:${task.win.start}-${task.win.end} ===`;
|
|
103
|
+
const full = `${header}\n${rows.map(row => row.text).join('\n')}`;
|
|
104
|
+
if (output.append(full)) {
|
|
105
|
+
for (const row of rows) if (matched.has(row.number)) task.file.shown.add(row.number);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
output.reduced = true;
|
|
110
|
+
const note = ' ... [context omitted to fit byte budget]';
|
|
111
|
+
let block = header;
|
|
112
|
+
const shown = [];
|
|
113
|
+
for (const row of rows.filter(row => matched.has(row.number))) {
|
|
114
|
+
const available = output.remaining() - byteLength(block) - byteLength(note) - 2;
|
|
115
|
+
const text = clipUtf8(row.text, available);
|
|
116
|
+
if (!text || !text.startsWith(`> ${row.number}:`)) break;
|
|
117
|
+
block += `\n${text}`;
|
|
118
|
+
shown.push(row.number);
|
|
119
|
+
if (text !== row.text) break;
|
|
120
|
+
}
|
|
121
|
+
if (shown.length && output.append(`${block}\n${note}`)) {
|
|
122
|
+
for (const line of shown) task.file.shown.add(line);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function appendMatchListing(output, files, maxLineChars) {
|
|
127
|
+
for (const file of files) {
|
|
128
|
+
const header = `=== ${file.displayPath} (${file.lines.size} matches) ===`;
|
|
129
|
+
let block = header;
|
|
130
|
+
const shown = [];
|
|
131
|
+
for (const [line, text] of [...file.lines.entries()].sort((a, b) => a[0] - b[0])) {
|
|
132
|
+
const row = ` ${line}: ${truncateLongLines(text, maxLineChars)}`;
|
|
133
|
+
const available = output.remaining() - byteLength(block) - 1;
|
|
134
|
+
const clipped = clipUtf8(row, available);
|
|
135
|
+
if (!clipped || !clipped.startsWith(` ${line}:`)) break;
|
|
136
|
+
block += `\n${clipped}`;
|
|
137
|
+
shown.push(line);
|
|
138
|
+
if (clipped !== row) { output.reduced = true; break; }
|
|
139
|
+
}
|
|
140
|
+
if (shown.length && output.append(block)) {
|
|
141
|
+
for (const line of shown) file.shown.add(line);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function compactLines(lines) {
|
|
147
|
+
const runs = [];
|
|
148
|
+
for (const line of lines) {
|
|
149
|
+
const last = runs.at(-1);
|
|
150
|
+
if (last && last[1] + 1 === line) last[1] = line;
|
|
151
|
+
else runs.push([line, line]);
|
|
152
|
+
}
|
|
153
|
+
const text = runs.slice(0, 6).map(([start, end]) => start === end ? `L${start}` : `L${start}-${end}`).join(', ');
|
|
154
|
+
return `${text}${runs.length > 6 ? ', ...' : ''}`;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function finishFindOutput(output, files) {
|
|
158
|
+
output.reserve = Math.min(128, Math.floor(output.maxBytes / 5));
|
|
159
|
+
let missing = 0;
|
|
160
|
+
let unindexed = 0;
|
|
161
|
+
let hasIndex = false;
|
|
162
|
+
for (const file of files) {
|
|
163
|
+
const lines = [...file.lines.keys()].filter(line => !file.shown.has(line)).sort((a, b) => a - b);
|
|
164
|
+
if (!lines.length) continue;
|
|
165
|
+
missing += lines.length;
|
|
166
|
+
const row = `${file.displayPath}: ${compactLines(lines)}`;
|
|
167
|
+
const block = `${hasIndex ? '' : '--- Additional matches (index only) ---\n'}${row}`;
|
|
168
|
+
if (output.append(block)) hasIndex = true;
|
|
169
|
+
else unindexed++;
|
|
170
|
+
}
|
|
171
|
+
const note = missing || output.reduced
|
|
172
|
+
? `[find_and_read: ${missing} code match(es), ${unindexed} file index(es) omitted; narrow search or read by line.]`
|
|
173
|
+
: '';
|
|
174
|
+
return output.finish(note);
|
|
175
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** Choose bounded windows by marginal pattern coverage, then diverse information per line. */
|
|
2
|
+
export function selectReadWindows(candidates, { maxFiles, maxWindows, maxReadLines }) {
|
|
3
|
+
const pending = [...candidates];
|
|
4
|
+
const chosen = [];
|
|
5
|
+
const coverage = new Map();
|
|
6
|
+
const files = new Set();
|
|
7
|
+
let lines = 0;
|
|
8
|
+
while (pending.length && chosen.length < maxWindows && lines < maxReadLines) {
|
|
9
|
+
let best;
|
|
10
|
+
for (let index = 0; index < pending.length; index++) {
|
|
11
|
+
const candidate = pending[index];
|
|
12
|
+
if (!files.has(candidate.file) && files.size >= maxFiles) continue;
|
|
13
|
+
const win = fitWindow(candidate, maxReadLines - lines, coverage);
|
|
14
|
+
const patterns = patternsFor(candidate.file, win.matchedLines);
|
|
15
|
+
const gain = [...patterns].filter(pattern => !coverage.has(pattern)).length;
|
|
16
|
+
const value = [...patterns].reduce((sum, pattern) => sum + 1 / (1 + (coverage.get(pattern) ?? 0)), 0);
|
|
17
|
+
const cost = win.end - win.start + 1;
|
|
18
|
+
const score = [gain, value / Math.sqrt(cost), files.has(candidate.file) ? 0 : 1, Math.min(10, win.matchedLines.length) / cost];
|
|
19
|
+
const contender = { index, file: candidate.file, win, patterns, score };
|
|
20
|
+
if (!best || compare(contender, best) < 0) best = contender;
|
|
21
|
+
}
|
|
22
|
+
if (!best) break;
|
|
23
|
+
pending.splice(best.index, 1);
|
|
24
|
+
chosen.push({ file: best.file, win: best.win });
|
|
25
|
+
files.add(best.file);
|
|
26
|
+
lines += best.win.end - best.win.start + 1;
|
|
27
|
+
for (const pattern of best.patterns) coverage.set(pattern, (coverage.get(pattern) ?? 0) + 1);
|
|
28
|
+
}
|
|
29
|
+
return chosen;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function compare(a, b) {
|
|
33
|
+
for (let i = 0; i < a.score.length; i++) {
|
|
34
|
+
if (a.score[i] !== b.score[i]) return b.score[i] - a.score[i];
|
|
35
|
+
}
|
|
36
|
+
const pathA = a.file.sortPath ?? a.file.displayPath;
|
|
37
|
+
const pathB = b.file.sortPath ?? b.file.displayPath;
|
|
38
|
+
return (pathA < pathB ? -1 : pathA > pathB ? 1 : 0) || a.win.start - b.win.start;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function patternsFor(file, lines) {
|
|
42
|
+
const patterns = new Set();
|
|
43
|
+
for (const line of lines) for (const pattern of file.linePatterns.get(line) ?? []) patterns.add(pattern);
|
|
44
|
+
return patterns;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function fitWindow({ file, win }, remaining, coverage) {
|
|
48
|
+
if (win.end - win.start + 1 <= remaining) return win;
|
|
49
|
+
// A small final line budget must retain the most informative hits, not just the first hit.
|
|
50
|
+
let best;
|
|
51
|
+
for (const line of win.matchedLines) {
|
|
52
|
+
const start = Math.max(win.start, Math.min(line, win.end - remaining + 1));
|
|
53
|
+
const end = start + remaining - 1;
|
|
54
|
+
const matchedLines = win.matchedLines.filter(hit => hit >= start && hit <= end);
|
|
55
|
+
const patterns = patternsFor(file, matchedLines);
|
|
56
|
+
const gain = [...patterns].filter(pattern => !coverage.has(pattern)).length;
|
|
57
|
+
const value = [...patterns].reduce((sum, pattern) => sum + 1 / (1 + (coverage.get(pattern) ?? 0)), 0);
|
|
58
|
+
if (!best || gain > best.gain || (gain === best.gain && value > best.value)) {
|
|
59
|
+
best = { start, end, matchedLines, gain, value };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return best;
|
|
63
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type { PersistentEvalOptions, PersistentEvalService } from './persistent-
|
|
|
9
9
|
import type { ConvergenceOptions } from './convergence.d.ts';
|
|
10
10
|
import type { CustomPromptOptions } from './custom-prompt.d.ts';
|
|
11
11
|
import type { TelemetryOptions } from './telemetry.d.ts';
|
|
12
|
+
import type { ContextCompactionOptions, ContextCompactionService } from './context-compaction.d.ts';
|
|
12
13
|
|
|
13
14
|
export interface AgentOptimizerConfig {
|
|
14
15
|
enabled?: boolean;
|
|
@@ -24,6 +25,7 @@ export interface AgentOptimizerConfig {
|
|
|
24
25
|
convergence?: ConvergenceOptions;
|
|
25
26
|
customPrompt?: CustomPromptOptions;
|
|
26
27
|
telemetry?: TelemetryOptions;
|
|
28
|
+
contextCompaction?: ContextCompactionOptions;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
declare module '@deepseek-ai/dsh-llm' {
|
|
@@ -122,6 +124,12 @@ export {
|
|
|
122
124
|
type TurnTelemetrySummary,
|
|
123
125
|
type TurnComparisonDelta
|
|
124
126
|
} from './telemetry.d.ts';
|
|
127
|
+
export {
|
|
128
|
+
applyContextCompaction,
|
|
129
|
+
ContextCompactionConfig,
|
|
130
|
+
type ContextCompactionOptions,
|
|
131
|
+
type ContextCompactionService
|
|
132
|
+
} from './context-compaction.d.ts';
|
|
125
133
|
export {
|
|
126
134
|
EVAL_PYTHON_TOOL,
|
|
127
135
|
EVAL_NODE_TOOL,
|
package/src/index.js
CHANGED
|
@@ -10,6 +10,7 @@ import { applyPersistentEval, PersistentEvalConfig, EVAL_PYTHON_TOOL, EVAL_NODE_
|
|
|
10
10
|
import { applyConvergence, ConvergenceConfig, SOURCE as CONVERGENCE_SOURCE, GUIDANCE, REMINDER } from './convergence.js';
|
|
11
11
|
import { applyCustomPrompt, CustomPromptConfig, CustomPromptManager, SECTION_NAME as CUSTOM_PROMPT_SECTION, ENVIRONMENT_FACTS_SECTION, formatEnvironmentFacts, detectPythonEnv, resetPythonCacheForTesting } from './custom-prompt.js';
|
|
12
12
|
import { applyTelemetry, TelemetryConfig, AgentTelemetryManager, compareTurnSummaries } from './telemetry.js';
|
|
13
|
+
import { applyContextCompaction, ContextCompactionConfig } from './context-compaction.js';
|
|
13
14
|
|
|
14
15
|
import { lintPrompt, fixPrompt, lintPromptFile, RULE_IDS as PROMPT_LINT_RULES } from './prompt-linter.js';
|
|
15
16
|
|
|
@@ -29,7 +30,8 @@ export const Config = z.object({
|
|
|
29
30
|
persistentEval: PersistentEvalConfig.default({}),
|
|
30
31
|
convergence: ConvergenceConfig.default({}),
|
|
31
32
|
customPrompt: CustomPromptConfig.default({}),
|
|
32
|
-
telemetry: TelemetryConfig.default({})
|
|
33
|
+
telemetry: TelemetryConfig.default({}),
|
|
34
|
+
contextCompaction: ContextCompactionConfig.default({})
|
|
33
35
|
});
|
|
34
36
|
|
|
35
37
|
/**
|
|
@@ -86,6 +88,10 @@ export function apply(ctx, raw = {}) {
|
|
|
86
88
|
const telemetryConfig = raw.telemetry ?? config.telemetry ?? {};
|
|
87
89
|
const telemetryManager = applyTelemetry(ctx, telemetryConfig);
|
|
88
90
|
if (telemetryManager) ctx.provide('agentTelemetry', telemetryManager);
|
|
91
|
+
|
|
92
|
+
const compactionConfig = raw.contextCompaction ?? config.contextCompaction ?? {};
|
|
93
|
+
const compactionManager = applyContextCompaction(ctx, compactionConfig);
|
|
94
|
+
if (compactionManager) ctx.provide('contextCompaction', compactionManager);
|
|
89
95
|
}
|
|
90
96
|
|
|
91
97
|
export {
|
|
@@ -134,7 +140,9 @@ export {
|
|
|
134
140
|
lintPromptFile,
|
|
135
141
|
PROMPT_LINT_RULES,
|
|
136
142
|
GUIDANCE,
|
|
137
|
-
REMINDER
|
|
143
|
+
REMINDER,
|
|
144
|
+
applyContextCompaction,
|
|
145
|
+
ContextCompactionConfig
|
|
138
146
|
};
|
|
139
147
|
|
|
140
148
|
export default { name, inject, Config, apply };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Bounded LRU of parsed symbols, scoped by session and filesystem backend. */
|
|
2
|
+
export class OutlineCache {
|
|
3
|
+
constructor(maxEntries, maxBytes) {
|
|
4
|
+
this.maxEntries = maxEntries;
|
|
5
|
+
this.maxBytes = maxBytes;
|
|
6
|
+
this.entries = new Map();
|
|
7
|
+
this.bytes = 0;
|
|
8
|
+
this.generation = 0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get(key, version) {
|
|
12
|
+
const entry = this.entries.get(key);
|
|
13
|
+
if (!entry) return null;
|
|
14
|
+
if (entry.version !== version) { this.delete(key); return null; }
|
|
15
|
+
this.entries.delete(key);
|
|
16
|
+
this.entries.set(key, entry);
|
|
17
|
+
return entry.value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
set(key, version, value) {
|
|
21
|
+
this.delete(key);
|
|
22
|
+
const bytes = Buffer.byteLength(JSON.stringify(value), 'utf8') + Buffer.byteLength(key, 'utf8') + Buffer.byteLength(version, 'utf8');
|
|
23
|
+
if (!this.maxEntries || bytes > this.maxBytes) return;
|
|
24
|
+
while (this.entries.size >= this.maxEntries || this.bytes + bytes > this.maxBytes) this.delete(this.entries.keys().next().value);
|
|
25
|
+
this.entries.set(key, { version, value, bytes });
|
|
26
|
+
this.bytes += bytes;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
delete(key) {
|
|
30
|
+
const entry = this.entries.get(key);
|
|
31
|
+
if (entry) this.bytes -= entry.bytes;
|
|
32
|
+
this.entries.delete(key);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
clear() { this.entries.clear(); this.bytes = 0; this.generation++; }
|
|
36
|
+
}
|
|
@@ -20,6 +20,8 @@ export class DaemonPool {
|
|
|
20
20
|
constructor(config = {}) {
|
|
21
21
|
this.config = config;
|
|
22
22
|
this.runnersBySession = new Map();
|
|
23
|
+
this.disposedKeys = new Set();
|
|
24
|
+
this.isDestroyed = false;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
_getKey(session) {
|
|
@@ -27,8 +29,20 @@ export class DaemonPool {
|
|
|
27
29
|
return session.id || session;
|
|
28
30
|
}
|
|
29
31
|
|
|
32
|
+
_assertSessionActive(key) {
|
|
33
|
+
if (this.isDestroyed) throw new Error('Daemon pool has been destroyed.');
|
|
34
|
+
if (this.disposedKeys.has(key)) throw new Error('Session workers have been destroyed.');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
reviveSession(session) {
|
|
38
|
+
if (this.isDestroyed) return false;
|
|
39
|
+
this.disposedKeys.delete(this._getKey(session));
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
30
43
|
getPythonRunner(session) {
|
|
31
44
|
const key = this._getKey(session);
|
|
45
|
+
this._assertSessionActive(key);
|
|
32
46
|
let sessionMap = this.runnersBySession.get(key);
|
|
33
47
|
if (!sessionMap) {
|
|
34
48
|
sessionMap = {};
|
|
@@ -50,6 +64,7 @@ export class DaemonPool {
|
|
|
50
64
|
|
|
51
65
|
getNodeRunner(session) {
|
|
52
66
|
const key = this._getKey(session);
|
|
67
|
+
this._assertSessionActive(key);
|
|
53
68
|
let sessionMap = this.runnersBySession.get(key);
|
|
54
69
|
if (!sessionMap) {
|
|
55
70
|
sessionMap = {};
|
|
@@ -77,6 +92,8 @@ export class DaemonPool {
|
|
|
77
92
|
* @param {boolean} [options.node=true]
|
|
78
93
|
*/
|
|
79
94
|
async prewarm(session, { python = true, node = true } = {}) {
|
|
95
|
+
const key = this._getKey(session);
|
|
96
|
+
if (this.isDestroyed || this.disposedKeys.has(key)) return;
|
|
80
97
|
const promises = [];
|
|
81
98
|
if (python) {
|
|
82
99
|
try {
|
|
@@ -99,6 +116,7 @@ export class DaemonPool {
|
|
|
99
116
|
|
|
100
117
|
destroySession(session) {
|
|
101
118
|
const key = this._getKey(session);
|
|
119
|
+
this.disposedKeys.add(key);
|
|
102
120
|
const sessionMap = this.runnersBySession.get(key);
|
|
103
121
|
if (!sessionMap) return;
|
|
104
122
|
|
|
@@ -112,9 +130,12 @@ export class DaemonPool {
|
|
|
112
130
|
}
|
|
113
131
|
|
|
114
132
|
destroyAll() {
|
|
133
|
+
if (this.isDestroyed) return;
|
|
134
|
+
this.isDestroyed = true;
|
|
115
135
|
for (const [key, sessionMap] of this.runnersBySession.entries()) {
|
|
116
136
|
if (sessionMap.python) sessionMap.python.destroy();
|
|
117
137
|
if (sessionMap.node) sessionMap.node.destroy();
|
|
138
|
+
this.disposedKeys.add(key);
|
|
118
139
|
}
|
|
119
140
|
this.runnersBySession.clear();
|
|
120
141
|
}
|
|
@@ -79,6 +79,7 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
|
|
|
79
79
|
});
|
|
80
80
|
|
|
81
81
|
const workingDirectories = new Map();
|
|
82
|
+
let disposed = false;
|
|
82
83
|
const sessionKey = session => session?.id || session || '__global__';
|
|
83
84
|
function getWorkingDirectory(session) {
|
|
84
85
|
const key = sessionKey(session);
|
|
@@ -94,28 +95,32 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
|
|
|
94
95
|
pool.destroySession(session);
|
|
95
96
|
}
|
|
96
97
|
function destroy() {
|
|
98
|
+
if (disposed) return;
|
|
99
|
+
disposed = true;
|
|
97
100
|
workingDirectories.clear();
|
|
98
101
|
pool.destroyAll();
|
|
99
102
|
}
|
|
103
|
+
function prewarmSession(session) {
|
|
104
|
+
if (disposed) return;
|
|
105
|
+
void pool.prewarm(session, { python: config.python, node: config.node });
|
|
106
|
+
}
|
|
100
107
|
|
|
101
|
-
// Clean up session workers when
|
|
108
|
+
// Clean up session workers when sessions end. The official session/event stream
|
|
109
|
+
// is the single lifecycle source; avoid duplicate turn/start prewarm listeners.
|
|
102
110
|
ctx.on('session/event', (session, event) => {
|
|
103
|
-
if (event?.type === 'session/destroy' || event?.type === 'session/close') {
|
|
111
|
+
if (event?.type === 'session/destroy' || event?.type === 'session/close' || event?.type === 'session/disposed') {
|
|
104
112
|
destroySession(session);
|
|
113
|
+
} else if (event?.type === 'session/create' || event?.type === 'session/start') {
|
|
114
|
+
pool.reviveSession(session);
|
|
115
|
+
prewarmSession(session);
|
|
105
116
|
} else if (event?.type === 'working-directory/change') {
|
|
106
117
|
workingDirectories.set(sessionKey(session), event.data.cwd);
|
|
107
|
-
} else if (event?.type === 'turn/start'
|
|
108
|
-
|
|
118
|
+
} else if (event?.type === 'turn/start') {
|
|
119
|
+
prewarmSession(session);
|
|
109
120
|
}
|
|
110
121
|
});
|
|
111
122
|
|
|
112
123
|
ctx.on('session/disposed', destroySession);
|
|
113
|
-
|
|
114
|
-
// Pre-warm on turn/start event so worker processes are already hot during model inference
|
|
115
|
-
ctx.on('turn/start', (turn) => {
|
|
116
|
-
const session = turn?.agent?.session ?? turn?.session;
|
|
117
|
-
pool.prewarm(session, { python: config.python, node: config.node });
|
|
118
|
-
});
|
|
119
124
|
// Clean up all daemon workers when plugin context is disposed
|
|
120
125
|
if (typeof ctx.effect === 'function') {
|
|
121
126
|
ctx.effect(() => destroy);
|
|
@@ -152,6 +157,7 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
|
|
|
152
157
|
},
|
|
153
158
|
isConcurrencySafe: () => false,
|
|
154
159
|
async execute(args, exec) {
|
|
160
|
+
if (disposed) throw new Error('Persistent eval service has been disposed.');
|
|
155
161
|
exec?.signal?.throwIfAborted();
|
|
156
162
|
const session = exec?.agent?.session ?? exec?.session;
|
|
157
163
|
const runner = pool.getPythonRunner(session);
|
|
@@ -199,6 +205,7 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
|
|
|
199
205
|
},
|
|
200
206
|
isConcurrencySafe: () => false,
|
|
201
207
|
async execute(args, exec) {
|
|
208
|
+
if (disposed) throw new Error('Persistent eval service has been disposed.');
|
|
202
209
|
exec?.signal?.throwIfAborted();
|
|
203
210
|
const session = exec?.agent?.session ?? exec?.session;
|
|
204
211
|
const runner = pool.getNodeRunner(session);
|
|
@@ -220,6 +227,7 @@ export function applyPersistentEval(ctx, rawConfig = {}) {
|
|
|
220
227
|
pool,
|
|
221
228
|
config,
|
|
222
229
|
prewarm(session) {
|
|
230
|
+
if (disposed) return Promise.resolve();
|
|
223
231
|
return pool.prewarm(session, { python: config.python, node: config.node });
|
|
224
232
|
},
|
|
225
233
|
destroy
|
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import readline from 'node:readline';
|
|
3
3
|
import process from 'node:process';
|
|
4
|
+
|
|
5
|
+
const OUTPUT_TRUNCATION_SUFFIX = '\n\n[Output truncated: output exceeded the configured byte limit.]';
|
|
6
|
+
|
|
7
|
+
function byteLength(value) {
|
|
8
|
+
return Buffer.byteLength(value, 'utf8');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function truncateUtf8(value, maxBytes, suffix = OUTPUT_TRUNCATION_SUFFIX) {
|
|
12
|
+
const text = typeof value === 'string' ? value : String(value ?? '');
|
|
13
|
+
if (byteLength(text) <= maxBytes) return text;
|
|
14
|
+
if (maxBytes <= 0) return '';
|
|
15
|
+
let effectiveSuffix = suffix;
|
|
16
|
+
if (byteLength(effectiveSuffix) >= maxBytes) effectiveSuffix = '\n[Output truncated]';
|
|
17
|
+
const suffixBytes = byteLength(effectiveSuffix);
|
|
18
|
+
const source = Buffer.from(text, 'utf8');
|
|
19
|
+
if (suffixBytes >= maxBytes) {
|
|
20
|
+
let end = Math.min(maxBytes, source.length);
|
|
21
|
+
while (end > 0 && (source[end] & 0xc0) === 0x80) end--;
|
|
22
|
+
return source.subarray(0, end).toString('utf8');
|
|
23
|
+
}
|
|
24
|
+
const contentBytes = maxBytes - suffixBytes;
|
|
25
|
+
let end = Math.min(contentBytes, source.length);
|
|
26
|
+
while (end > 0 && (source[end] & 0xc0) === 0x80) end--;
|
|
27
|
+
return source.subarray(0, end).toString('utf8') + effectiveSuffix;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function appendUtf8Bounded(current, chunk, maxBytes) {
|
|
31
|
+
const text = typeof chunk === 'string' ? chunk : String(chunk ?? '');
|
|
32
|
+
const remaining = Math.max(0, maxBytes - byteLength(current));
|
|
33
|
+
if (!remaining) return current;
|
|
34
|
+
return current + truncateUtf8(text, remaining, '');
|
|
35
|
+
}
|
|
4
36
|
/**
|
|
5
37
|
* Convert an aborted signal's reason into an Error suitable for rejection
|
|
6
38
|
* and DSH cancellation classification.
|
|
@@ -123,7 +155,11 @@ export class StdioWorkerRunner {
|
|
|
123
155
|
if (this.child !== child) return;
|
|
124
156
|
// Worker-level unhandled stderr logging (not during task execution)
|
|
125
157
|
if (this.currentTask) {
|
|
126
|
-
|
|
158
|
+
const before = this.currentTask.stderrChunk || '';
|
|
159
|
+
this.currentTask.stderrChunk = appendUtf8Bounded(before, chunk, this.maxOutputBytes);
|
|
160
|
+
if (byteLength(String(chunk)) > Math.max(0, this.maxOutputBytes - byteLength(before))) {
|
|
161
|
+
this.currentTask.stderrTruncated = true;
|
|
162
|
+
}
|
|
127
163
|
}
|
|
128
164
|
});
|
|
129
165
|
|
|
@@ -187,19 +223,14 @@ export class StdioWorkerRunner {
|
|
|
187
223
|
}
|
|
188
224
|
|
|
189
225
|
_truncateAndResolve(task, response) {
|
|
190
|
-
let stdout = response.stdout || '';
|
|
191
|
-
let stderr = (response.stderr || '') + (task.stderrChunk || '');
|
|
192
|
-
|
|
193
226
|
const limit = this.maxOutputBytes;
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
response.stdout = stdout;
|
|
202
|
-
response.stderr = stderr;
|
|
227
|
+
response.stdout = truncateUtf8(response.stdout || '', limit,
|
|
228
|
+
`\n\n[Output truncated: stdout exceeded ${limit} bytes. If full payload is required, write it to a file directly.]`);
|
|
229
|
+
const stderrText = `${response.stderr || ''}${task.stderrChunk || ''}`;
|
|
230
|
+
const stderrSuffix = `\n\n[Output truncated: stderr exceeded ${limit} bytes.]`;
|
|
231
|
+
response.stderr = task.stderrTruncated
|
|
232
|
+
? truncateUtf8(`${stderrText}${stderrSuffix}`, limit, stderrSuffix)
|
|
233
|
+
: truncateUtf8(stderrText, limit, stderrSuffix);
|
|
203
234
|
task.resolve(response);
|
|
204
235
|
}
|
|
205
236
|
|
|
@@ -342,7 +373,8 @@ export class StdioWorkerRunner {
|
|
|
342
373
|
action: options.action || 'eval',
|
|
343
374
|
cwd: options.cwd,
|
|
344
375
|
code: code || '',
|
|
345
|
-
reset: Boolean(options.reset)
|
|
376
|
+
reset: Boolean(options.reset),
|
|
377
|
+
maxOutputBytes: this.maxOutputBytes
|
|
346
378
|
};
|
|
347
379
|
|
|
348
380
|
return new Promise((resolve, reject) => {
|
|
@@ -132,6 +132,37 @@ async function executeInContext(code, context) {
|
|
|
132
132
|
return syncScript.runInContext(context, { breakOnSigint: true });
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
function byteLength(value) {
|
|
136
|
+
return Buffer.byteLength(value, 'utf8');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function createBoundedCapture(maxBytes, label) {
|
|
140
|
+
const suffix = `\n\n[Output truncated: ${label} exceeded ${maxBytes} bytes.]`;
|
|
141
|
+
let value = '';
|
|
142
|
+
let truncated = false;
|
|
143
|
+
return {
|
|
144
|
+
write(chunk) {
|
|
145
|
+
if (truncated) return;
|
|
146
|
+
const text = typeof chunk === 'string' ? chunk : String(chunk ?? '');
|
|
147
|
+
if (byteLength(value) + byteLength(text) <= maxBytes) {
|
|
148
|
+
value += text;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const effectiveSuffix = byteLength(suffix) >= maxBytes ? '\n[Output truncated]' : suffix;
|
|
152
|
+
const suffixBytes = byteLength(effectiveSuffix);
|
|
153
|
+
const available = Math.max(0, maxBytes - byteLength(value) - suffixBytes);
|
|
154
|
+
const bytes = Buffer.from(text, 'utf8');
|
|
155
|
+
let end = Math.min(available, bytes.length);
|
|
156
|
+
while (end > 0 && (bytes[end] & 0xc0) === 0x80) end--;
|
|
157
|
+
value += bytes.subarray(0, end).toString('utf8');
|
|
158
|
+
if (byteLength(value) + suffixBytes <= maxBytes) value += effectiveSuffix;
|
|
159
|
+
else value = Buffer.from(value, 'utf8').subarray(0, maxBytes).toString('utf8');
|
|
160
|
+
truncated = true;
|
|
161
|
+
},
|
|
162
|
+
get value() { return value; }
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
135
166
|
function sendFrame(payload) {
|
|
136
167
|
let line;
|
|
137
168
|
try {
|
|
@@ -193,23 +224,18 @@ function main() {
|
|
|
193
224
|
return;
|
|
194
225
|
}
|
|
195
226
|
|
|
196
|
-
|
|
197
|
-
|
|
227
|
+
const maxOutputBytes = Number.isInteger(req.maxOutputBytes) && req.maxOutputBytes >= 0
|
|
228
|
+
? req.maxOutputBytes
|
|
229
|
+
: 8192;
|
|
230
|
+
const stdoutCapture = createBoundedCapture(maxOutputBytes, 'stdout');
|
|
231
|
+
const stderrCapture = createBoundedCapture(maxOutputBytes, 'stderr');
|
|
198
232
|
|
|
199
|
-
// Intercept console inside context
|
|
233
|
+
// Intercept console inside context with byte-bounded UTF-8 buffers.
|
|
200
234
|
const customConsole = {
|
|
201
|
-
log: (...args) =>
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
stdoutBuf += util.format(...args) + '\n';
|
|
206
|
-
},
|
|
207
|
-
warn: (...args) => {
|
|
208
|
-
stderrBuf += util.format(...args) + '\n';
|
|
209
|
-
},
|
|
210
|
-
error: (...args) => {
|
|
211
|
-
stderrBuf += util.format(...args) + '\n';
|
|
212
|
-
}
|
|
235
|
+
log: (...args) => stdoutCapture.write(util.format(...args) + '\n'),
|
|
236
|
+
info: (...args) => stdoutCapture.write(util.format(...args) + '\n'),
|
|
237
|
+
warn: (...args) => stderrCapture.write(util.format(...args) + '\n'),
|
|
238
|
+
error: (...args) => stderrCapture.write(util.format(...args) + '\n')
|
|
213
239
|
};
|
|
214
240
|
CURRENT_CONTEXT.console = customConsole;
|
|
215
241
|
|
|
@@ -236,7 +262,7 @@ function main() {
|
|
|
236
262
|
} catch (err) {
|
|
237
263
|
success = false;
|
|
238
264
|
errorMsg = err instanceof Error ? err.stack || err.message : String(err);
|
|
239
|
-
|
|
265
|
+
stderrCapture.write(errorMsg + '\n');
|
|
240
266
|
}
|
|
241
267
|
|
|
242
268
|
const elapsedMs = Number((performance.now() - startTime).toFixed(3));
|
|
@@ -245,8 +271,8 @@ function main() {
|
|
|
245
271
|
sendFrame({
|
|
246
272
|
id: reqId,
|
|
247
273
|
success,
|
|
248
|
-
stdout:
|
|
249
|
-
stderr:
|
|
274
|
+
stdout: stdoutCapture.value,
|
|
275
|
+
stderr: stderrCapture.value,
|
|
250
276
|
result: success ? result : null,
|
|
251
277
|
resultRepr: success ? resultRepr : null,
|
|
252
278
|
error: success ? null : errorMsg,
|