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
|
@@ -101,6 +101,45 @@ def serialize_result(result):
|
|
|
101
101
|
# Fall back to string representation
|
|
102
102
|
return result_repr, result_repr
|
|
103
103
|
|
|
104
|
+
class BoundedTextCapture(io.TextIOBase):
|
|
105
|
+
"""Capture UTF-8 output without retaining more than the configured byte limit."""
|
|
106
|
+
def __init__(self, max_bytes, label):
|
|
107
|
+
self.max_bytes = max(0, int(max_bytes))
|
|
108
|
+
self.label = label
|
|
109
|
+
self.value = ''
|
|
110
|
+
self.truncated = False
|
|
111
|
+
|
|
112
|
+
def write(self, text):
|
|
113
|
+
if self.truncated:
|
|
114
|
+
return len(text)
|
|
115
|
+
text = str(text)
|
|
116
|
+
candidate = self.value + text
|
|
117
|
+
if len(candidate.encode('utf-8')) <= self.max_bytes:
|
|
118
|
+
self.value = candidate
|
|
119
|
+
return len(text)
|
|
120
|
+
suffix = f"\n\n[Output truncated: {self.label} exceeded {self.max_bytes} bytes.]"
|
|
121
|
+
if len(suffix.encode('utf-8')) >= self.max_bytes:
|
|
122
|
+
suffix = "\n[Output truncated]"
|
|
123
|
+
suffix_bytes = len(suffix.encode('utf-8'))
|
|
124
|
+
available = max(0, self.max_bytes - len(self.value.encode('utf-8')) - suffix_bytes)
|
|
125
|
+
encoded = text.encode('utf-8')[:available]
|
|
126
|
+
while encoded and (encoded[-1] & 0xc0) == 0x80:
|
|
127
|
+
encoded = encoded[:-1]
|
|
128
|
+
self.value += encoded.decode('utf-8', errors='ignore')
|
|
129
|
+
if len((self.value + suffix).encode('utf-8')) <= self.max_bytes:
|
|
130
|
+
self.value += suffix
|
|
131
|
+
else:
|
|
132
|
+
encoded_value = self.value.encode('utf-8')[:self.max_bytes]
|
|
133
|
+
while encoded_value and (encoded_value[-1] & 0xc0) == 0x80:
|
|
134
|
+
encoded_value = encoded_value[:-1]
|
|
135
|
+
self.value = encoded_value.decode('utf-8', errors='ignore')
|
|
136
|
+
self.truncated = True
|
|
137
|
+
return len(text)
|
|
138
|
+
|
|
139
|
+
def getvalue(self):
|
|
140
|
+
return self.value
|
|
141
|
+
|
|
142
|
+
|
|
104
143
|
def print_user_traceback(ex, stream):
|
|
105
144
|
"""Hide worker frames without discarding user frames or exception metadata."""
|
|
106
145
|
diagnostic = traceback.TracebackException.from_exception(ex)
|
|
@@ -167,8 +206,11 @@ def main():
|
|
|
167
206
|
REAL_STDOUT.buffer.flush()
|
|
168
207
|
continue
|
|
169
208
|
|
|
170
|
-
|
|
171
|
-
|
|
209
|
+
max_output_bytes = req.get("maxOutputBytes", 8192)
|
|
210
|
+
if not isinstance(max_output_bytes, int) or max_output_bytes < 0:
|
|
211
|
+
max_output_bytes = 8192
|
|
212
|
+
captured_stdout = BoundedTextCapture(max_output_bytes, "stdout")
|
|
213
|
+
captured_stderr = BoundedTextCapture(max_output_bytes, "stderr")
|
|
172
214
|
old_stdout = sys.stdout
|
|
173
215
|
old_stderr = sys.stderr
|
|
174
216
|
sys.stdout = captured_stdout
|
package/src/read-guard.d.ts
CHANGED
|
@@ -8,15 +8,43 @@ export interface ReadGuardOptions {
|
|
|
8
8
|
maxOutlineEntries?: number;
|
|
9
9
|
maxOutlineBytes?: number;
|
|
10
10
|
maxInspectBytes?: number;
|
|
11
|
+
/** Parsed-outline LRU entries per session/backend; 0 disables caching. Default: 32. */
|
|
12
|
+
maxCacheEntries?: number;
|
|
13
|
+
/** Maximum serialized cache payload per session/backend. Default: 262144. */
|
|
14
|
+
maxCacheBytes?: number;
|
|
15
|
+
/** auto: parse TSX/JSX and known lexical gaps on demand; off disables the CLI. */
|
|
16
|
+
astOutline?: 'off' | 'auto' | 'always';
|
|
17
|
+
/** Executable name or absolute path; executed without a shell. Default: ast-grep. */
|
|
18
|
+
astGrepCommand?: string;
|
|
19
|
+
/** CLI deadline in milliseconds. Default: 750. */
|
|
20
|
+
astTimeoutMs?: number;
|
|
21
|
+
/** Maximum CLI stdout bytes. Default: 262144. */
|
|
22
|
+
astMaxOutputBytes?: number;
|
|
11
23
|
}
|
|
12
24
|
|
|
13
25
|
export interface ReadGuardStats {
|
|
14
26
|
inspected: number;
|
|
15
27
|
linesTruncated: number;
|
|
16
28
|
outlinesInjected: number;
|
|
29
|
+
/** Per-turn enabled lookups with a valid identity/version after successful preflight. */
|
|
30
|
+
outlineCacheLookups: number;
|
|
31
|
+
outlineCacheHits: number;
|
|
32
|
+
/** Includes absent, stale, and evicted entries, even if subsequent parsing fails. */
|
|
33
|
+
outlineCacheMisses: number;
|
|
34
|
+
/** Successful preflights without caching: disabled, unversioned, or local-only backend. */
|
|
35
|
+
outlineCacheBypasses: number;
|
|
36
|
+
/** Mutation events clearing an existing session cache group, including in-flight work. */
|
|
37
|
+
outlineCacheInvalidations: number;
|
|
38
|
+
/** Hits / lookups in [0, 1]; null when no lookup occurred. */
|
|
39
|
+
outlineCacheHitRate: number | null;
|
|
40
|
+
astAttempts: number;
|
|
41
|
+
astSucceeded: number;
|
|
42
|
+
astFallbacks: number;
|
|
43
|
+
astTimeouts: number;
|
|
17
44
|
}
|
|
18
45
|
|
|
19
46
|
export interface ReadGuardService {
|
|
47
|
+
/** Detached per-turn snapshot; caches may survive turn boundaries. */
|
|
20
48
|
getStats(session: unknown): ReadGuardStats;
|
|
21
49
|
}
|
|
22
50
|
|
package/src/read-guard.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import z from '@deepseek-ai/schemastery';
|
|
4
|
-
import {
|
|
4
|
+
import { formatOutlineEntries, supportsCodeOutline } from './code-outline.js';
|
|
5
|
+
import { buildAdaptiveOutline } from './ast-outline.js';
|
|
6
|
+
import { OutlineCache } from './outline-cache.js';
|
|
5
7
|
|
|
6
8
|
export const READ_GUARD_SOURCE = 'optimizer-read-guard';
|
|
7
9
|
|
|
@@ -11,7 +13,13 @@ export const ReadGuardConfig = z.object({
|
|
|
11
13
|
injectTailOutline: z.boolean().default(true),
|
|
12
14
|
maxOutlineEntries: z.number().step(1).min(3).max(60).default(20),
|
|
13
15
|
maxOutlineBytes: z.number().step(1).min(256).max(8192).default(1536),
|
|
14
|
-
maxInspectBytes: z.number().step(1).min(4096).max(4 * 1024 * 1024).default(512 * 1024)
|
|
16
|
+
maxInspectBytes: z.number().step(1).min(4096).max(4 * 1024 * 1024).default(512 * 1024),
|
|
17
|
+
maxCacheEntries: z.number().step(1).min(0).max(256).default(32),
|
|
18
|
+
maxCacheBytes: z.number().step(1).min(1024).max(8 * 1024 * 1024).default(256 * 1024),
|
|
19
|
+
astOutline: z.union(['off', 'auto', 'always']).default('auto'),
|
|
20
|
+
astGrepCommand: z.string().default('ast-grep'),
|
|
21
|
+
astTimeoutMs: z.number().step(1).min(1).max(5000).default(750),
|
|
22
|
+
astMaxOutputBytes: z.number().step(1).min(1024).max(1024 * 1024).default(256 * 1024)
|
|
15
23
|
});
|
|
16
24
|
|
|
17
25
|
export function truncateLongLines(text, maxLineChars = 1500) {
|
|
@@ -52,42 +60,89 @@ function detectTruncation(body, meta) {
|
|
|
52
60
|
return null;
|
|
53
61
|
}
|
|
54
62
|
|
|
55
|
-
async function readTargetSafely(filePath, cwd, fsService, signal,
|
|
63
|
+
async function readTargetSafely(filePath, cwd, fsService, signal, config, cache, stats) {
|
|
64
|
+
const { maxInspectBytes } = config;
|
|
56
65
|
if (signal?.aborted) return null;
|
|
66
|
+
const generation = cache?.generation;
|
|
57
67
|
|
|
58
68
|
if (fsService) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
69
|
+
// A service failure is authoritative; never retry through another backend.
|
|
70
|
+
try {
|
|
71
|
+
if (typeof fsService.resolve !== 'function' || typeof fsService.stat !== 'function') return null;
|
|
72
|
+
const target = await fsService.resolve(filePath, { cwd, signal });
|
|
73
|
+
const info = await fsService.stat(target, signal);
|
|
74
|
+
if (signal?.aborted || cache?.generation !== generation || info?.type !== 'file' || !Number.isFinite(info.size) || info.size < 0 || info.size > maxInspectBytes) return null;
|
|
75
|
+
const key = typeof target?.targetKey === 'string' && target.targetKey ? JSON.stringify([target.targetKey, path.extname(filePath).toLowerCase()]) : null;
|
|
76
|
+
const version = typeof info.version === 'string' && info.version ? info.version : null;
|
|
77
|
+
if (cache?.maxEntries > 0 && key && version) {
|
|
78
|
+
if (stats) stats.outlineCacheLookups++;
|
|
79
|
+
const cached = cache.get(key, version);
|
|
80
|
+
if (cached) {
|
|
81
|
+
if (stats) stats.outlineCacheHits++;
|
|
82
|
+
return cached;
|
|
64
83
|
}
|
|
65
|
-
|
|
66
|
-
|
|
84
|
+
if (stats) stats.outlineCacheMisses++;
|
|
85
|
+
} else if (stats) {
|
|
86
|
+
stats.outlineCacheBypasses++;
|
|
67
87
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
88
|
+
let content;
|
|
89
|
+
if (typeof fsService.readBytes === 'function') {
|
|
90
|
+
const bytes = await fsService.readBytes(target, signal, maxInspectBytes);
|
|
91
|
+
if (signal?.aborted || bytes.byteLength > maxInspectBytes) return null;
|
|
92
|
+
content = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
|
93
|
+
} else if (typeof fsService.streamText === 'function') {
|
|
94
|
+
const chunks = [];
|
|
95
|
+
let size = 0;
|
|
96
|
+
for await (const chunk of await fsService.streamText(target, signal)) {
|
|
97
|
+
if (signal?.aborted || typeof chunk !== 'string') return null;
|
|
98
|
+
size += Buffer.byteLength(chunk, 'utf8');
|
|
99
|
+
if (size > maxInspectBytes) return null;
|
|
100
|
+
chunks.push(chunk);
|
|
101
|
+
}
|
|
102
|
+
content = chunks.join('');
|
|
103
|
+
} else if (typeof fsService.readText === 'function') {
|
|
104
|
+
// Compatibility with older services: only after a successful size preflight.
|
|
105
|
+
content = await fsService.readText(target, signal);
|
|
106
|
+
} else {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
if (signal?.aborted || cache?.generation !== generation || typeof content !== 'string' || Buffer.byteLength(content, 'utf8') > maxInspectBytes) return null;
|
|
110
|
+
const outline = await buildAdaptiveOutline(content, filePath, config, signal, stats);
|
|
111
|
+
if (signal?.aborted || cache?.generation !== generation) return null;
|
|
112
|
+
if (key && version) {
|
|
113
|
+
const after = await fsService.stat(target, signal);
|
|
114
|
+
if (signal?.aborted || cache?.generation !== generation || after?.type !== 'file' || after.version !== version || after.size !== info.size) return null;
|
|
115
|
+
cache?.set(key, version, outline);
|
|
79
116
|
}
|
|
117
|
+
return outline;
|
|
118
|
+
} catch {
|
|
119
|
+
return null;
|
|
80
120
|
}
|
|
81
121
|
}
|
|
82
122
|
|
|
123
|
+
// Standalone hosts without an fs service still get a bounded local read.
|
|
124
|
+
let handle;
|
|
83
125
|
try {
|
|
84
126
|
const resolved = path.isAbsolute(filePath) ? filePath : path.resolve(cwd || process.cwd(), filePath);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (signal?.aborted) return null;
|
|
88
|
-
|
|
127
|
+
handle = await fs.open(resolved, 'r');
|
|
128
|
+
const stat = await handle.stat();
|
|
129
|
+
if (!stat.isFile() || stat.size > maxInspectBytes || signal?.aborted || cache?.generation !== generation) return null;
|
|
130
|
+
if (stats) stats.outlineCacheBypasses++;
|
|
131
|
+
const buffer = Buffer.alloc(maxInspectBytes + 1);
|
|
132
|
+
let size = 0;
|
|
133
|
+
while (size < buffer.length) {
|
|
134
|
+
if (signal?.aborted) return null;
|
|
135
|
+
const { bytesRead } = await handle.read(buffer, size, buffer.length - size, null);
|
|
136
|
+
if (!bytesRead) break;
|
|
137
|
+
size += bytesRead;
|
|
138
|
+
}
|
|
139
|
+
if (signal?.aborted || cache?.generation !== generation || size > maxInspectBytes) return null;
|
|
140
|
+
const outline = await buildAdaptiveOutline(new TextDecoder('utf-8', { fatal: true }).decode(buffer.subarray(0, size)), filePath, config, signal, stats);
|
|
141
|
+
return signal?.aborted || cache?.generation !== generation ? null : outline;
|
|
89
142
|
} catch {
|
|
90
143
|
return null;
|
|
144
|
+
} finally {
|
|
145
|
+
await handle?.close().catch(() => {});
|
|
91
146
|
}
|
|
92
147
|
}
|
|
93
148
|
|
|
@@ -95,29 +150,85 @@ async function readTargetSafely(filePath, cwd, fsService, signal, maxInspectByte
|
|
|
95
150
|
* Protect direct `read` calls against giant minified lines and blind large-file truncation
|
|
96
151
|
* by enforcing single-line caps and appending a compact structural outline of unread tail lines.
|
|
97
152
|
*/
|
|
153
|
+
function emptyStats() {
|
|
154
|
+
return {
|
|
155
|
+
inspected: 0, linesTruncated: 0, outlinesInjected: 0,
|
|
156
|
+
outlineCacheLookups: 0, outlineCacheHits: 0, outlineCacheMisses: 0,
|
|
157
|
+
outlineCacheBypasses: 0, outlineCacheInvalidations: 0,
|
|
158
|
+
astAttempts: 0, astSucceeded: 0, astFallbacks: 0, astTimeouts: 0
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
98
162
|
export function applyReadGuard(ctx, rawConfig = {}) {
|
|
99
163
|
const config = ReadGuardConfig(rawConfig);
|
|
100
164
|
if (!config.enabled) return null;
|
|
101
165
|
|
|
102
166
|
const statsBySession = new WeakMap();
|
|
167
|
+
const cachesBySession = new WeakMap();
|
|
168
|
+
const disposedSessions = new WeakSet();
|
|
169
|
+
let disposed = false;
|
|
170
|
+
const anonymousSession = {};
|
|
171
|
+
const localBackend = {};
|
|
172
|
+
const allCaches = new Set();
|
|
173
|
+
const clearCaches = session => {
|
|
174
|
+
const caches = cachesBySession.get(session ?? anonymousSession);
|
|
175
|
+
for (const cache of caches?.values() ?? []) { cache.clear(); allCaches.delete(cache); }
|
|
176
|
+
cachesBySession.delete(session ?? anonymousSession);
|
|
177
|
+
return Boolean(caches?.size);
|
|
178
|
+
};
|
|
179
|
+
const cacheFor = (session, backend) => {
|
|
180
|
+
const key = session ?? anonymousSession;
|
|
181
|
+
let caches = cachesBySession.get(key);
|
|
182
|
+
if (!caches) {
|
|
183
|
+
caches = new Map();
|
|
184
|
+
cachesBySession.set(key, caches);
|
|
185
|
+
}
|
|
186
|
+
let cache = caches.get(backend ?? localBackend);
|
|
187
|
+
if (!cache) {
|
|
188
|
+
cache = new OutlineCache(config.maxCacheEntries, config.maxCacheBytes);
|
|
189
|
+
caches.set(backend ?? localBackend, cache);
|
|
190
|
+
allCaches.add(cache);
|
|
191
|
+
}
|
|
192
|
+
return cache;
|
|
193
|
+
};
|
|
194
|
+
ctx.on('dispose', () => {
|
|
195
|
+
disposed = true;
|
|
196
|
+
for (const cache of allCaches) cache.clear();
|
|
197
|
+
allCaches.clear();
|
|
198
|
+
});
|
|
103
199
|
const statsFor = session => {
|
|
104
|
-
if (!session) return null;
|
|
200
|
+
if (disposed || !session || disposedSessions.has(session)) return null;
|
|
105
201
|
let s = statsBySession.get(session);
|
|
106
202
|
if (!s) {
|
|
107
|
-
s =
|
|
203
|
+
s = emptyStats();
|
|
108
204
|
statsBySession.set(session, s);
|
|
109
205
|
}
|
|
110
206
|
return s;
|
|
111
207
|
};
|
|
112
208
|
|
|
209
|
+
ctx.on('session/disposed', session => {
|
|
210
|
+
if (!session) return;
|
|
211
|
+
disposedSessions.add(session);
|
|
212
|
+
clearCaches(session);
|
|
213
|
+
statsBySession.delete(session);
|
|
214
|
+
});
|
|
113
215
|
ctx.on('session/event', (session, event) => {
|
|
114
|
-
if (event?.type === 'turn/start') {
|
|
115
|
-
statsBySession.set(session,
|
|
216
|
+
if (!disposed && session && !disposedSessions.has(session) && event?.type === 'turn/start') {
|
|
217
|
+
statsBySession.set(session, emptyStats());
|
|
116
218
|
}
|
|
117
219
|
});
|
|
118
220
|
|
|
119
221
|
ctx.on('tools/post-execute', async (exec, result, next) => {
|
|
120
222
|
const decision = await next();
|
|
223
|
+
const session = exec.agent?.session;
|
|
224
|
+
if (disposed || (session && disposedSessions.has(session))) return decision;
|
|
225
|
+
// Invalidate conservatively even for failed/partially successful composite mutations.
|
|
226
|
+
if (['write', 'edit', 'edit_batch', 'bash', 'eval_node', 'eval_python'].includes(exec?.name)) {
|
|
227
|
+
if (clearCaches(session)) {
|
|
228
|
+
const stats = statsFor(session);
|
|
229
|
+
if (stats) stats.outlineCacheInvalidations++;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
121
232
|
if (
|
|
122
233
|
decision?.kind === 'block' ||
|
|
123
234
|
exec?.name !== 'read' ||
|
|
@@ -131,7 +242,6 @@ export function applyReadGuard(ctx, rawConfig = {}) {
|
|
|
131
242
|
const baseBlocks = decision?.content ?? result.content;
|
|
132
243
|
if (!Array.isArray(baseBlocks) || baseBlocks.length === 0) return decision;
|
|
133
244
|
|
|
134
|
-
const session = exec.agent?.session;
|
|
135
245
|
const stats = statsFor(session);
|
|
136
246
|
if (stats) stats.inspected++;
|
|
137
247
|
|
|
@@ -154,7 +264,7 @@ export function applyReadGuard(ctx, rawConfig = {}) {
|
|
|
154
264
|
// 2. Blind-read tail outline injection (only when caller omitted both offset & limit and hit truncation)
|
|
155
265
|
const args = exec.arguments ?? {};
|
|
156
266
|
const isBlindRead = args.offset === undefined && args.limit === undefined && typeof args.file_path === 'string' && args.file_path.trim().length > 0;
|
|
157
|
-
if (config.injectTailOutline && isBlindRead) {
|
|
267
|
+
if (config.injectTailOutline && isBlindRead && supportsCodeOutline(args.file_path)) {
|
|
158
268
|
const firstTextIdx = updatedBlocks.findIndex(b => b.type === 'text' && typeof b.text === 'string');
|
|
159
269
|
if (firstTextIdx !== -1) {
|
|
160
270
|
const { prefix, body, suffix } = splitEnvelope(updatedBlocks[firstTextIdx].text);
|
|
@@ -170,16 +280,16 @@ export function applyReadGuard(ctx, rawConfig = {}) {
|
|
|
170
280
|
try {
|
|
171
281
|
return ctx.get('fs');
|
|
172
282
|
} catch {
|
|
173
|
-
|
|
283
|
+
// An unavailable/denied service is not permission to bypass it.
|
|
284
|
+
return {};
|
|
174
285
|
}
|
|
175
286
|
})();
|
|
176
287
|
|
|
177
|
-
const
|
|
178
|
-
if (
|
|
179
|
-
const totalLines =
|
|
288
|
+
const parsed = await readTargetSafely(args.file_path, cwd, fsService, exec.signal, config, cacheFor(session, fsService), stats);
|
|
289
|
+
if (parsed !== null && !exec.signal?.aborted && !disposed && !(session && disposedSessions.has(session))) {
|
|
290
|
+
const { totalLines } = parsed;
|
|
180
291
|
if (totalLines > trunc.endLine) {
|
|
181
|
-
const outline =
|
|
182
|
-
filePath: args.file_path,
|
|
292
|
+
const outline = formatOutlineEntries(parsed.entries, {
|
|
183
293
|
startLine: trunc.endLine + 1,
|
|
184
294
|
endLine: totalLines,
|
|
185
295
|
maxEntries: config.maxOutlineEntries,
|
|
@@ -207,7 +317,11 @@ export function applyReadGuard(ctx, rawConfig = {}) {
|
|
|
207
317
|
|
|
208
318
|
return {
|
|
209
319
|
getStats(session) {
|
|
210
|
-
|
|
320
|
+
const stats = statsFor(session) ?? emptyStats();
|
|
321
|
+
return {
|
|
322
|
+
...stats,
|
|
323
|
+
outlineCacheHitRate: stats.outlineCacheLookups ? stats.outlineCacheHits / stats.outlineCacheLookups : null
|
|
324
|
+
};
|
|
211
325
|
}
|
|
212
326
|
};
|
|
213
327
|
}
|
package/src/telemetry.d.ts
CHANGED
|
@@ -4,10 +4,13 @@ import type { RuntimePolicySummary } from './runtime-policy.d.ts';
|
|
|
4
4
|
import type { FileMutationPolicySummary } from './file-mutation-policy.d.ts';
|
|
5
5
|
import type { FailureRecoveryStats } from './failure-recovery.d.ts';
|
|
6
6
|
import type { CompositeToolsStats } from './composite-tools.d.ts';
|
|
7
|
+
import type { ReadGuardStats } from './read-guard.d.ts';
|
|
7
8
|
|
|
8
9
|
export interface TelemetryOptions {
|
|
9
10
|
enabled?: boolean;
|
|
10
11
|
trackStreams?: boolean;
|
|
12
|
+
/** Opt-in HTTP diagnostics JSONL path; absolute, bounded to 8 MiB. */
|
|
13
|
+
cacheDiagnosticsPath?: string;
|
|
11
14
|
onTurnSummary?: (summary: TurnTelemetrySummary, session: unknown) => void;
|
|
12
15
|
}
|
|
13
16
|
|
|
@@ -42,6 +45,7 @@ export interface TurnTelemetrySummary {
|
|
|
42
45
|
fileMutationPolicy: FileMutationPolicySummary | null;
|
|
43
46
|
failureRecovery: FailureRecoveryStats | null;
|
|
44
47
|
compositeTools: CompositeToolsStats | null;
|
|
48
|
+
readGuard: ReadGuardStats | null;
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
export interface TurnComparisonDelta {
|
package/src/telemetry.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
+
import { CacheDiagnostics } from './cache-diagnostics.js';
|
|
2
3
|
import z from '@deepseek-ai/schemastery';
|
|
3
4
|
import { SOURCE as CONVERGENCE_SOURCE } from './convergence.js';
|
|
4
5
|
import { RECOVERY_SOURCE } from './failure-recovery.js';
|
|
5
6
|
export const TelemetryConfig = z.object({
|
|
6
7
|
enabled: z.boolean().default(true),
|
|
7
8
|
trackStreams: z.boolean().default(true),
|
|
8
|
-
onTurnSummary: z.any()
|
|
9
|
+
onTurnSummary: z.any(),
|
|
10
|
+
cacheDiagnosticsPath: z.string()
|
|
9
11
|
});
|
|
10
12
|
|
|
11
13
|
function computeQuantile(sortedNumbers, p) {
|
|
@@ -85,16 +87,12 @@ export class AgentTelemetryManager {
|
|
|
85
87
|
this.sessionStates = new WeakMap();
|
|
86
88
|
this.sessionsById = new Map();
|
|
87
89
|
this.activeSession = null;
|
|
88
|
-
this.
|
|
90
|
+
this.previousHistories = new WeakMap();
|
|
89
91
|
}
|
|
90
92
|
|
|
91
93
|
#digestMessage(message) {
|
|
92
94
|
if (message && typeof message === 'object') {
|
|
93
|
-
|
|
94
|
-
if (cached !== undefined) return cached;
|
|
95
|
-
const digest = createHash('sha256').update(JSON.stringify(message)).digest('hex');
|
|
96
|
-
this.messageDigestCache.set(message, digest);
|
|
97
|
-
return digest;
|
|
95
|
+
return createHash('sha256').update(JSON.stringify(message)).digest('hex');
|
|
98
96
|
}
|
|
99
97
|
return createHash('sha256').update(String(message)).digest('hex');
|
|
100
98
|
}
|
|
@@ -114,7 +112,6 @@ export class AgentTelemetryManager {
|
|
|
114
112
|
recoveryNotices: 0,
|
|
115
113
|
turnOutcome: null,
|
|
116
114
|
streamAttempts: [],
|
|
117
|
-
previousHistory: null,
|
|
118
115
|
prefixChanges: 0
|
|
119
116
|
};
|
|
120
117
|
}
|
|
@@ -177,6 +174,7 @@ export class AgentTelemetryManager {
|
|
|
177
174
|
}
|
|
178
175
|
|
|
179
176
|
onSessionDisposed(session) {
|
|
177
|
+
if (session) this.previousHistories.delete(session);
|
|
180
178
|
if (session?.id !== undefined && this.sessionsById.get(session.id) === session) {
|
|
181
179
|
this.sessionsById.delete(session.id);
|
|
182
180
|
}
|
|
@@ -192,10 +190,10 @@ export class AgentTelemetryManager {
|
|
|
192
190
|
|
|
193
191
|
if (Array.isArray(options?.messages)) {
|
|
194
192
|
const history = options.messages.map(message => this.#digestMessage(message));
|
|
195
|
-
if (
|
|
193
|
+
if (this.previousHistories.get(session)?.some((msg, idx) => history[idx] !== msg)) {
|
|
196
194
|
state.prefixChanges++;
|
|
197
195
|
}
|
|
198
|
-
|
|
196
|
+
this.previousHistories.set(session, history);
|
|
199
197
|
}
|
|
200
198
|
|
|
201
199
|
const attempt = {
|
|
@@ -252,7 +250,8 @@ export class AgentTelemetryManager {
|
|
|
252
250
|
runtimePolicy: null,
|
|
253
251
|
fileMutationPolicy: null,
|
|
254
252
|
failureRecovery: null,
|
|
255
|
-
compositeTools: null
|
|
253
|
+
compositeTools: null,
|
|
254
|
+
readGuard: null
|
|
256
255
|
};
|
|
257
256
|
}
|
|
258
257
|
|
|
@@ -310,7 +309,8 @@ export class AgentTelemetryManager {
|
|
|
310
309
|
runtimePolicy: this.ctx?.get?.('runtimePolicy')?.getSummary(targetSession, state.turn) ?? null,
|
|
311
310
|
fileMutationPolicy: this.ctx?.get?.('fileMutationPolicy')?.getSummary(targetSession, state.turn) ?? null,
|
|
312
311
|
failureRecovery: this.ctx?.get?.('failureRecovery')?.getStats(targetSession) ?? null,
|
|
313
|
-
compositeTools: this.ctx?.get?.('compositeTools')?.getStats(targetSession) ?? null
|
|
312
|
+
compositeTools: this.ctx?.get?.('compositeTools')?.getStats(targetSession) ?? null,
|
|
313
|
+
readGuard: this.ctx?.get?.('readGuard')?.getStats(targetSession) ?? null
|
|
314
314
|
};
|
|
315
315
|
}
|
|
316
316
|
}
|
|
@@ -320,6 +320,14 @@ export function applyTelemetry(ctx, rawConfig = {}) {
|
|
|
320
320
|
if (!config.enabled) return null;
|
|
321
321
|
|
|
322
322
|
const manager = new AgentTelemetryManager(ctx, config);
|
|
323
|
+
if (config.cacheDiagnosticsPath) {
|
|
324
|
+
const warn = reason => { try { ctx.logger?.warn?.(`Cache diagnostics disabled: ${reason}`); } catch {} };
|
|
325
|
+
try {
|
|
326
|
+
const diagnostics = new CacheDiagnostics(config.cacheDiagnosticsPath, { onStop: warn });
|
|
327
|
+
ctx.on('llm/stream', (options, next) => diagnostics.wrap(options?.sessionId, next));
|
|
328
|
+
ctx.on('dispose', () => diagnostics.dispose());
|
|
329
|
+
} catch { warn('initialization-failed'); }
|
|
330
|
+
}
|
|
323
331
|
|
|
324
332
|
ctx.on('session/event', (session, event) => {
|
|
325
333
|
manager.onSessionEvent(session, event);
|