dsh-awesome-optimizer 0.0.0-stage → 1.7.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/LICENSE +21 -0
- package/README.md +226 -2
- package/README.zh.md +227 -0
- package/cordis.patch.yml +42 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +112 -4
- package/scripts/benchmark.mjs +113 -0
- package/scripts/typecheck.mjs +57 -0
- package/src/composite-tools.d.ts +37 -0
- package/src/composite-tools.js +417 -0
- package/src/convergence.d.ts +33 -0
- package/src/convergence.js +173 -0
- package/src/custom-prompt.d.ts +67 -0
- package/src/custom-prompt.js +366 -0
- package/src/failure-recovery.d.ts +66 -0
- package/src/failure-recovery.js +289 -0
- package/src/file-mutation-policy.d.ts +44 -0
- package/src/file-mutation-policy.js +298 -0
- package/src/index.d.ts +102 -0
- package/src/index.js +99 -0
- package/src/prompt-linter.d.ts +44 -0
- package/src/prompt-linter.js +332 -0
- package/src/runtime-policy.d.ts +72 -0
- package/src/runtime-policy.js +511 -0
- package/src/telemetry.d.ts +79 -0
- package/src/telemetry.js +333 -0
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import z from '@deepseek-ai/schemastery';
|
|
4
|
+
|
|
5
|
+
export const POLICY_SOURCE = 'optimizer-runtime-policy';
|
|
6
|
+
|
|
7
|
+
export const RuntimePolicyConfig = z.object({
|
|
8
|
+
mode: z.union(['off', 'observe', 'enforce']).default('enforce'),
|
|
9
|
+
noProgressLimit: z.number().step(1).min(1).default(2),
|
|
10
|
+
recoveryAttemptLimit: z.number().step(1).min(1).max(1).default(1),
|
|
11
|
+
minLargeWriteBytes: z.number().step(1).min(1).default(8192),
|
|
12
|
+
maxShellCommandBytes: z.number().step(1).min(256).default(4096),
|
|
13
|
+
maxInlineScriptBytes: z.number().step(1).min(128).default(512),
|
|
14
|
+
disallowChainedNetworkCommands: z.boolean().default(true)
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Normalize path separators and Windows path casing for stable tool identities.
|
|
19
|
+
*/
|
|
20
|
+
export function normalizePath(filePath, cwd = '') {
|
|
21
|
+
if (typeof filePath !== 'string' || !filePath.trim()) return '';
|
|
22
|
+
let normalized = filePath.trim().replace(/\\/g, '/');
|
|
23
|
+
const normalizedCwd = typeof cwd === 'string' ? cwd.trim().replace(/\\/g, '/') : '';
|
|
24
|
+
if (normalizedCwd && !path.posix.isAbsolute(normalized) && !/^[a-zA-Z]:\//.test(normalized)) {
|
|
25
|
+
normalized = path.posix.join(normalizedCwd, normalized);
|
|
26
|
+
}
|
|
27
|
+
const isWindowsPath = /^[a-zA-Z]:\//.test(normalized) || /^\/\/[^/]+\//.test(normalized);
|
|
28
|
+
if (/^[a-zA-Z]:\//.test(normalized)) {
|
|
29
|
+
normalized = normalized[0].toLowerCase() + normalized.slice(1);
|
|
30
|
+
}
|
|
31
|
+
normalized = path.posix.normalize(normalized);
|
|
32
|
+
if (isWindowsPath && filePath.trim().replace(/\\/g, '/').startsWith('//') && !normalized.startsWith('//')) {
|
|
33
|
+
normalized = `/${normalized}`;
|
|
34
|
+
}
|
|
35
|
+
if (normalized.length > 1 && normalized.endsWith('/') && !/^[a-z]:\/$/.test(normalized)) {
|
|
36
|
+
normalized = normalized.replace(/\/+$/, '');
|
|
37
|
+
}
|
|
38
|
+
if (normalized === '.') return '';
|
|
39
|
+
return isWindowsPath ? normalized.toLowerCase() : normalized;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Size bucket to ensure fingerprint captures payload scale without storing payload bytes.
|
|
44
|
+
*/
|
|
45
|
+
export function sizeBucket(bytes) {
|
|
46
|
+
if (!bytes || bytes <= 0) return '0';
|
|
47
|
+
if (bytes < 1024) return '<1K';
|
|
48
|
+
if (bytes < 8192) return '1K-8K';
|
|
49
|
+
if (bytes < 65536) return '8K-64K';
|
|
50
|
+
if (bytes < 524288) return '64K-512K';
|
|
51
|
+
return '>512K';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Generate a short, deterministic fingerprint string for a tool call.
|
|
56
|
+
*/
|
|
57
|
+
export function computeFingerprint({ name, targetPath, opClass, errorCode, isError, byteLength }) {
|
|
58
|
+
const parts = [
|
|
59
|
+
name ?? '',
|
|
60
|
+
opClass ?? 'generic',
|
|
61
|
+
normalizePath(targetPath),
|
|
62
|
+
isError ? (errorCode ?? 'ERROR') : 'OK',
|
|
63
|
+
sizeBucket(byteLength)
|
|
64
|
+
];
|
|
65
|
+
return createHash('sha256').update(parts.join('|')).digest('hex').slice(0, 16);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function stableValue(value, cwd = '', key = '') {
|
|
69
|
+
if (typeof value === 'string') {
|
|
70
|
+
if (['file_path', 'path', 'targetPath', 'workdir', 'cwd'].includes(key)) {
|
|
71
|
+
return normalizePath(value, cwd);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
bytes: Buffer.byteLength(value, 'utf8'),
|
|
75
|
+
hash: createHash('sha256').update(value).digest('hex').slice(0, 16)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (value === null || typeof value !== 'object') return value;
|
|
79
|
+
if (Array.isArray(value)) return value.map(item => stableValue(item, cwd));
|
|
80
|
+
return Object.fromEntries(Object.keys(value).sort().map(childKey => [childKey, stableValue(value[childKey], cwd, childKey)]));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function invocationKey(name, args, classified, cwd) {
|
|
84
|
+
const identity = {
|
|
85
|
+
name,
|
|
86
|
+
opClass: classified.opClass,
|
|
87
|
+
targetPath: normalizePath(classified.targetPath, cwd),
|
|
88
|
+
byteLength: classified.byteLength,
|
|
89
|
+
args: stableValue(args, cwd)
|
|
90
|
+
};
|
|
91
|
+
return createHash('sha256').update(JSON.stringify(identity)).digest('hex').slice(0, 16);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function classifyToolCall(name, args = {}, cwd = '') {
|
|
95
|
+
let targetPath = '';
|
|
96
|
+
let opClass = 'generic';
|
|
97
|
+
let byteLength = 0;
|
|
98
|
+
|
|
99
|
+
if (name === 'write' || name === 'retry_failed_write') {
|
|
100
|
+
opClass = 'write';
|
|
101
|
+
targetPath = typeof args.file_path === 'string' ? args.file_path : '';
|
|
102
|
+
if (typeof args.content === 'string') {
|
|
103
|
+
byteLength = Buffer.byteLength(args.content, 'utf8');
|
|
104
|
+
}
|
|
105
|
+
} else if (name === 'edit') {
|
|
106
|
+
opClass = 'edit';
|
|
107
|
+
targetPath = typeof args.file_path === 'string' ? args.file_path : '';
|
|
108
|
+
const newLen = typeof args.new_string === 'string' ? Buffer.byteLength(args.new_string, 'utf8') : 0;
|
|
109
|
+
const oldLen = typeof args.old_string === 'string' ? Buffer.byteLength(args.old_string, 'utf8') : 0;
|
|
110
|
+
byteLength = newLen + oldLen;
|
|
111
|
+
} else if (name === 'read' || name === 'read_image') {
|
|
112
|
+
opClass = 'read';
|
|
113
|
+
targetPath = typeof args.file_path === 'string' ? args.file_path : '';
|
|
114
|
+
} else if (name === 'read_batch') {
|
|
115
|
+
opClass = 'read';
|
|
116
|
+
const items = Array.isArray(args.files) ? args.files : [];
|
|
117
|
+
targetPath = items.map(item => (typeof item?.file_path === 'string' ? item.file_path : '')).filter(Boolean).join(',');
|
|
118
|
+
} else if (name === 'glob' || name === 'grep') {
|
|
119
|
+
opClass = 'search';
|
|
120
|
+
targetPath = typeof args.path === 'string' ? args.path : '';
|
|
121
|
+
} else if (name === 'bash') {
|
|
122
|
+
opClass = 'shell';
|
|
123
|
+
targetPath = typeof args.workdir === 'string' ? args.workdir : (cwd || '');
|
|
124
|
+
if (typeof args.command === 'string') {
|
|
125
|
+
byteLength = Buffer.byteLength(args.command, 'utf8');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { name, opClass, targetPath, byteLength };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Core runtime policy manager managing state, fingerprinting, and pre-dispatch guards.
|
|
134
|
+
*/
|
|
135
|
+
export class RuntimePolicyManager {
|
|
136
|
+
constructor(config = {}) {
|
|
137
|
+
this.config = RuntimePolicyConfig(config);
|
|
138
|
+
this.sessionStates = new WeakMap();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#createTurnState(turn, cwd = '') {
|
|
142
|
+
return {
|
|
143
|
+
turn,
|
|
144
|
+
step: 0,
|
|
145
|
+
cwd,
|
|
146
|
+
fingerprintCounts: new Map(),
|
|
147
|
+
consecutiveFingerprints: new Map(),
|
|
148
|
+
lastFingerprint: null,
|
|
149
|
+
lastInvocationKey: null,
|
|
150
|
+
blockedInvocationKey: null,
|
|
151
|
+
consecutiveInvocationCount: 0,
|
|
152
|
+
maxConsecutiveRepeats: 0,
|
|
153
|
+
lastRecord: null,
|
|
154
|
+
deniedTokens: new Set(),
|
|
155
|
+
toolExecutions: 0,
|
|
156
|
+
failedToolExecutions: 0,
|
|
157
|
+
policyDenials: 0,
|
|
158
|
+
largeWrites: 0,
|
|
159
|
+
edits: 0,
|
|
160
|
+
writeBytes: 0,
|
|
161
|
+
editBytes: 0,
|
|
162
|
+
repeatedFingerprintHits: 0,
|
|
163
|
+
noProgressHits: 0,
|
|
164
|
+
shellPayloadBlocks: 0,
|
|
165
|
+
noProgressFingerprints: new Set()
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
stateFor(session, turn) {
|
|
170
|
+
if (!session) return null;
|
|
171
|
+
let state = this.sessionStates.get(session);
|
|
172
|
+
const targetTurn = turn ?? state?.turn ?? 1;
|
|
173
|
+
if (!state || state.turn !== targetTurn) {
|
|
174
|
+
state = this.#createTurnState(targetTurn, state?.cwd ?? '');
|
|
175
|
+
this.sessionStates.set(session, state);
|
|
176
|
+
}
|
|
177
|
+
return state;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
onTurnStart(session, turn) {
|
|
181
|
+
const existing = this.sessionStates.get(session);
|
|
182
|
+
this.sessionStates.set(session, this.#createTurnState(turn, existing?.cwd ?? ''));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
onTurnEnd(_session) {
|
|
186
|
+
// Keep state available for post-turn telemetry inspection; WeakMap releases with session.
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
onStepStart(session, turn, step) {
|
|
190
|
+
const state = this.stateFor(session, turn);
|
|
191
|
+
if (state) state.step = step;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
onCwdChange(session, newCwd) {
|
|
195
|
+
const state = this.sessionStates.get(session);
|
|
196
|
+
if (!state) return;
|
|
197
|
+
state.cwd = newCwd;
|
|
198
|
+
state.lastFingerprint = null;
|
|
199
|
+
state.lastInvocationKey = null;
|
|
200
|
+
state.blockedInvocationKey = null;
|
|
201
|
+
state.consecutiveInvocationCount = 0;
|
|
202
|
+
state.lastRecord = null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
recordExecution(session, exec, result) {
|
|
206
|
+
if (!session || !exec) return null;
|
|
207
|
+
const existing = this.sessionStates.get(session);
|
|
208
|
+
const state = existing ?? this.stateFor(session, 1);
|
|
209
|
+
if (!state) return null;
|
|
210
|
+
if (exec.token !== undefined && state.deniedTokens.delete(exec.token)) {
|
|
211
|
+
return {
|
|
212
|
+
record: state.lastRecord,
|
|
213
|
+
decision: {
|
|
214
|
+
kind: 'deny',
|
|
215
|
+
blockedPreExecute: true
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const classified = classifyToolCall(exec.name, exec.arguments, state.cwd);
|
|
221
|
+
const { name, opClass, targetPath, byteLength } = classified;
|
|
222
|
+
const key = invocationKey(name, exec.arguments ?? {}, classified, state.cwd);
|
|
223
|
+
const isError = Boolean(result?.isError);
|
|
224
|
+
const errorMessage = result?.error?.message;
|
|
225
|
+
const errorCode = result?.error?.info?.code ?? (isError
|
|
226
|
+
? `ERROR:${createHash('sha256').update(typeof errorMessage === 'string' ? errorMessage : '').digest('hex').slice(0, 16)}`
|
|
227
|
+
: 'OK');
|
|
228
|
+
|
|
229
|
+
const fp = computeFingerprint({
|
|
230
|
+
name,
|
|
231
|
+
targetPath: normalizePath(targetPath, state.cwd),
|
|
232
|
+
opClass,
|
|
233
|
+
errorCode,
|
|
234
|
+
isError,
|
|
235
|
+
byteLength
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
state.toolExecutions++;
|
|
239
|
+
if (isError) state.failedToolExecutions++;
|
|
240
|
+
|
|
241
|
+
if (opClass === 'write') {
|
|
242
|
+
if (byteLength >= this.config.minLargeWriteBytes) state.largeWrites++;
|
|
243
|
+
state.writeBytes += byteLength;
|
|
244
|
+
} else if (opClass === 'edit') {
|
|
245
|
+
state.edits++;
|
|
246
|
+
state.editBytes += byteLength;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const previousCount = state.fingerprintCounts.get(fp) ?? 0;
|
|
250
|
+
state.fingerprintCounts.set(fp, previousCount + 1);
|
|
251
|
+
|
|
252
|
+
const isConsecutive = state.lastFingerprint === fp;
|
|
253
|
+
const consecutiveCount = (isConsecutive ? (state.consecutiveFingerprints.get(fp) ?? 0) : 0) + 1;
|
|
254
|
+
state.consecutiveFingerprints.set(fp, consecutiveCount);
|
|
255
|
+
state.maxConsecutiveRepeats = Math.max(state.maxConsecutiveRepeats, consecutiveCount);
|
|
256
|
+
state.lastFingerprint = fp;
|
|
257
|
+
const failureKey = isError ? `${key}:${fp}` : null;
|
|
258
|
+
state.consecutiveInvocationCount = failureKey
|
|
259
|
+
? (state.lastInvocationKey === failureKey ? state.consecutiveInvocationCount + 1 : 1)
|
|
260
|
+
: 0;
|
|
261
|
+
state.lastInvocationKey = failureKey;
|
|
262
|
+
|
|
263
|
+
if (previousCount > 0) {
|
|
264
|
+
state.repeatedFingerprintHits++;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (isError && state.consecutiveInvocationCount >= this.config.noProgressLimit) {
|
|
268
|
+
state.noProgressHits++;
|
|
269
|
+
state.noProgressFingerprints.add(fp);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const record = {
|
|
273
|
+
step: state.step,
|
|
274
|
+
callId: exec.callId,
|
|
275
|
+
name,
|
|
276
|
+
opClass,
|
|
277
|
+
targetPath: normalizePath(targetPath, state.cwd),
|
|
278
|
+
byteLength,
|
|
279
|
+
isError,
|
|
280
|
+
errorCode,
|
|
281
|
+
fingerprint: fp,
|
|
282
|
+
invocationKey: key,
|
|
283
|
+
failureKey,
|
|
284
|
+
repeated: previousCount > 0,
|
|
285
|
+
consecutive: consecutiveCount
|
|
286
|
+
};
|
|
287
|
+
state.lastRecord = record;
|
|
288
|
+
if (!isError || key !== state.blockedInvocationKey) state.blockedInvocationKey = null;
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
record,
|
|
292
|
+
decision: {
|
|
293
|
+
kind: this.config.mode === 'off' ? 'allow' : 'observe',
|
|
294
|
+
fingerprint: fp,
|
|
295
|
+
repeated: previousCount > 0,
|
|
296
|
+
consecutive: consecutiveCount
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
evaluatePreExecute(session, exec) {
|
|
302
|
+
if (this.config.mode !== 'enforce' || !session || !exec) return { kind: 'allow' };
|
|
303
|
+
const state = this.stateFor(session);
|
|
304
|
+
if (!state) return { kind: 'allow' };
|
|
305
|
+
const classified = classifyToolCall(exec.name, exec.arguments, state.cwd);
|
|
306
|
+
const { name, opClass, targetPath } = classified;
|
|
307
|
+
const key = invocationKey(name, exec.arguments ?? {}, classified, state.cwd);
|
|
308
|
+
if (state.blockedInvocationKey === key) {
|
|
309
|
+
state.policyDenials++;
|
|
310
|
+
if (exec.token !== undefined) state.deniedTokens.add(exec.token);
|
|
311
|
+
return {
|
|
312
|
+
kind: 'deny',
|
|
313
|
+
reason: '[runtime-policy] Execution blocked: the same tool invocation was already denied after repeated identical failures.'
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Guard against oversized shell commands or large embedded scripts (anti-spawn ENAMETOOLONG on Windows)
|
|
318
|
+
if (name === 'bash' && exec.arguments && typeof exec.arguments.command === 'string') {
|
|
319
|
+
const cmd = exec.arguments.command;
|
|
320
|
+
const cmdBytes = Buffer.byteLength(cmd, 'utf8');
|
|
321
|
+
const isHeredoc = /(?:cat|type)\s*<<\s*['"]?EOF/i.test(cmd);
|
|
322
|
+
const isInlineScript = /(?:python|python3|node|pwsh|powershell)\s+(?:-c|-e|-Command)\s+['"][\s\S]+/i.test(cmd);
|
|
323
|
+
|
|
324
|
+
const maxBytes = this.config.maxShellCommandBytes ?? 4096;
|
|
325
|
+
const maxScriptBytes = this.config.maxInlineScriptBytes ?? 512;
|
|
326
|
+
|
|
327
|
+
if (cmdBytes > maxBytes) {
|
|
328
|
+
state.shellPayloadBlocks++;
|
|
329
|
+
state.policyDenials++;
|
|
330
|
+
if (exec.token !== undefined) state.deniedTokens.add(exec.token);
|
|
331
|
+
return {
|
|
332
|
+
kind: 'deny',
|
|
333
|
+
reason: JSON.stringify({
|
|
334
|
+
code: 'EMBEDDED_SHELL_PAYLOAD_TOO_LARGE',
|
|
335
|
+
reason: `Shell command size (${cmdBytes} bytes) exceeds safe limit of ${maxBytes} bytes (causes spawn ENAMETOOLONG on Windows).`,
|
|
336
|
+
nextAction: "Write the script or deliverable to a file using native 'write' first, then execute it by file path.",
|
|
337
|
+
retryable: false
|
|
338
|
+
})
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if ((isHeredoc && cmdBytes > 256) || (isInlineScript && cmdBytes > maxScriptBytes)) {
|
|
343
|
+
state.shellPayloadBlocks++;
|
|
344
|
+
state.policyDenials++;
|
|
345
|
+
if (exec.token !== undefined) state.deniedTokens.add(exec.token);
|
|
346
|
+
return {
|
|
347
|
+
kind: 'deny',
|
|
348
|
+
reason: JSON.stringify({
|
|
349
|
+
code: 'EMBEDDED_SHELL_PAYLOAD_TOO_LARGE',
|
|
350
|
+
reason: `Large script or deliverable embedded in shell argument (${cmdBytes} bytes). NEVER embed complete scripts or files in shell commands.`,
|
|
351
|
+
nextAction: "Write a reusable script or deliverable using native 'write' first, then execute it by file path.",
|
|
352
|
+
retryable: false
|
|
353
|
+
})
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Guard against blocked file:// URLs in playwright-cli
|
|
358
|
+
const isPlaywrightFileUrl = /\bplaywright-cli\s+(?:open|goto)\s+['"]?file:/i.test(cmd);
|
|
359
|
+
if (isPlaywrightFileUrl) {
|
|
360
|
+
state.shellPayloadBlocks++;
|
|
361
|
+
state.policyDenials++;
|
|
362
|
+
if (exec.token !== undefined) state.deniedTokens.add(exec.token);
|
|
363
|
+
return {
|
|
364
|
+
kind: 'deny',
|
|
365
|
+
reason: JSON.stringify({
|
|
366
|
+
code: 'PLAYWRIGHT_FILE_PROTOCOL_BLOCKED',
|
|
367
|
+
reason: "Playwright CLI blocks access to the 'file:' protocol.",
|
|
368
|
+
nextAction: "Serve static HTML files via a local HTTP server (e.g. node http.createServer or npx serve) and access 'http://127.0.0.1:<port>' instead.",
|
|
369
|
+
retryable: false
|
|
370
|
+
})
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Guard against dangerous network/credential command chaining on Windows
|
|
375
|
+
if (this.config.disallowChainedNetworkCommands) {
|
|
376
|
+
const hasCompoundOperator = /(&&|;)/.test(cmd);
|
|
377
|
+
const hasChainedNetworkAction = /(?:^|[\s;&|])(?:npm\s+publish|gh\s+(?:pr|issue)\s+create)\b/i.test(cmd);
|
|
378
|
+
if (hasCompoundOperator && hasChainedNetworkAction) {
|
|
379
|
+
state.shellPayloadBlocks++;
|
|
380
|
+
state.policyDenials++;
|
|
381
|
+
if (exec.token !== undefined) state.deniedTokens.add(exec.token);
|
|
382
|
+
return {
|
|
383
|
+
kind: 'deny',
|
|
384
|
+
reason: JSON.stringify({
|
|
385
|
+
code: 'CHAINED_NETWORK_COMMAND',
|
|
386
|
+
reason: "Chaining network/credential-bound commands ('npm publish') with other commands causes Windows process handle hanging and 120s timeouts.",
|
|
387
|
+
nextAction: "Separate into independent steps: execute local operations first, then run network command ('npm publish') standalone.",
|
|
388
|
+
retryable: false
|
|
389
|
+
})
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const lastRecord = state.lastRecord;
|
|
396
|
+
const candidateFingerprint = computeFingerprint({
|
|
397
|
+
name,
|
|
398
|
+
targetPath: normalizePath(targetPath, state.cwd),
|
|
399
|
+
opClass,
|
|
400
|
+
errorCode: lastRecord?.errorCode,
|
|
401
|
+
isError: true,
|
|
402
|
+
byteLength: classified.byteLength
|
|
403
|
+
});
|
|
404
|
+
if (lastRecord?.isError && lastRecord.failureKey === `${key}:${candidateFingerprint}`) {
|
|
405
|
+
const normTarget = normalizePath(targetPath, state.cwd);
|
|
406
|
+
if (lastRecord.name === name && lastRecord.opClass === opClass && lastRecord.targetPath === normTarget) {
|
|
407
|
+
const consecutive = state.consecutiveInvocationCount;
|
|
408
|
+
if (consecutive >= this.config.noProgressLimit) {
|
|
409
|
+
state.blockedInvocationKey = key;
|
|
410
|
+
state.noProgressHits++;
|
|
411
|
+
state.policyDenials++;
|
|
412
|
+
state.noProgressFingerprints.add(lastRecord.fingerprint);
|
|
413
|
+
if (exec.token !== undefined) state.deniedTokens.add(exec.token);
|
|
414
|
+
return {
|
|
415
|
+
kind: 'deny',
|
|
416
|
+
reason: JSON.stringify({
|
|
417
|
+
code: 'NO_PROGRESS',
|
|
418
|
+
reason: `Tool '${name}' on '${normTarget || 'target'}' failed ${consecutive} times consecutively without progress (limit: ${this.config.noProgressLimit}).`,
|
|
419
|
+
nextAction: 'Inspect the prior error and change the command, arguments, or target before retrying.',
|
|
420
|
+
retryable: false
|
|
421
|
+
})
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return { kind: 'allow' };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
getSummary(session, turn) {
|
|
430
|
+
const state = this.stateFor(session, turn);
|
|
431
|
+
if (!state) {
|
|
432
|
+
return {
|
|
433
|
+
toolExecutions: 0,
|
|
434
|
+
failedToolExecutions: 0,
|
|
435
|
+
policyDenials: 0,
|
|
436
|
+
largeWrites: 0,
|
|
437
|
+
edits: 0,
|
|
438
|
+
writeBytes: 0,
|
|
439
|
+
editBytes: 0,
|
|
440
|
+
repeatedFingerprintHits: 0,
|
|
441
|
+
uniqueFingerprints: 0,
|
|
442
|
+
maxConsecutiveRepeats: 0,
|
|
443
|
+
noProgressHits: 0,
|
|
444
|
+
shellPayloadBlocks: 0,
|
|
445
|
+
uniqueNoProgressFingerprints: 0
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return {
|
|
450
|
+
toolExecutions: state.toolExecutions,
|
|
451
|
+
failedToolExecutions: state.failedToolExecutions,
|
|
452
|
+
policyDenials: state.policyDenials ?? 0,
|
|
453
|
+
largeWrites: state.largeWrites,
|
|
454
|
+
edits: state.edits,
|
|
455
|
+
writeBytes: state.writeBytes,
|
|
456
|
+
editBytes: state.editBytes,
|
|
457
|
+
repeatedFingerprintHits: state.repeatedFingerprintHits,
|
|
458
|
+
uniqueFingerprints: state.fingerprintCounts.size,
|
|
459
|
+
maxConsecutiveRepeats: state.maxConsecutiveRepeats,
|
|
460
|
+
noProgressHits: state.noProgressHits ?? 0,
|
|
461
|
+
shellPayloadBlocks: state.shellPayloadBlocks ?? 0,
|
|
462
|
+
uniqueNoProgressFingerprints: state.noProgressFingerprints?.size ?? 0
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Apply the runtime policy plugin to the Cordis context.
|
|
469
|
+
*/
|
|
470
|
+
export function applyRuntimePolicy(ctx, rawConfig = {}) {
|
|
471
|
+
const config = RuntimePolicyConfig(rawConfig);
|
|
472
|
+
if (config.mode === 'off') return null;
|
|
473
|
+
|
|
474
|
+
const manager = new RuntimePolicyManager(config);
|
|
475
|
+
|
|
476
|
+
ctx.on('session/event', (session, event) => {
|
|
477
|
+
if (event.type === 'turn/start') {
|
|
478
|
+
manager.onTurnStart(session, event.data.turn);
|
|
479
|
+
} else if (event.type === 'turn/end') {
|
|
480
|
+
manager.onTurnEnd(session);
|
|
481
|
+
} else if (event.type === 'step/start') {
|
|
482
|
+
manager.onStepStart(session, event.data.turn, event.data.step);
|
|
483
|
+
} else if (event.type === 'working-directory/change') {
|
|
484
|
+
manager.onCwdChange(session, event.data.cwd);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
ctx.on('tools/pre-execute', async (exec, next) => {
|
|
489
|
+
if (!exec.agent) return next();
|
|
490
|
+
const decision = manager.evaluatePreExecute(exec.agent.session, exec);
|
|
491
|
+
if (decision.kind === 'deny') {
|
|
492
|
+
return decision;
|
|
493
|
+
}
|
|
494
|
+
return next();
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
ctx.on('tools/result', (exec, result) => {
|
|
498
|
+
if (!exec.agent) return;
|
|
499
|
+
manager.recordExecution(exec.agent.session, exec, result);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
return manager;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export const name = 'agent-optimizer-runtime-policy';
|
|
506
|
+
export const Config = RuntimePolicyConfig;
|
|
507
|
+
export function apply(ctx, config = {}) {
|
|
508
|
+
const manager = applyRuntimePolicy(ctx, config);
|
|
509
|
+
if (manager) ctx.provide('runtimePolicy', manager);
|
|
510
|
+
}
|
|
511
|
+
export default { name, Config, apply };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type z from '@deepseek-ai/schemastery';
|
|
3
|
+
import type { RuntimePolicySummary } from './runtime-policy.d.ts';
|
|
4
|
+
import type { FileMutationPolicySummary } from './file-mutation-policy.d.ts';
|
|
5
|
+
import type { FailureRecoveryStats } from './failure-recovery.d.ts';
|
|
6
|
+
import type { CompositeToolsStats } from './composite-tools.d.ts';
|
|
7
|
+
|
|
8
|
+
export interface TelemetryOptions {
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
trackStreams?: boolean;
|
|
11
|
+
onTurnSummary?: (summary: TurnTelemetrySummary, session: unknown) => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface TurnTelemetrySummary {
|
|
15
|
+
turn: number;
|
|
16
|
+
steps: number;
|
|
17
|
+
modelRequests: number;
|
|
18
|
+
toolCalls: number;
|
|
19
|
+
ptcSubCalls: number;
|
|
20
|
+
toolBatches: number;
|
|
21
|
+
maxBatchSize: number;
|
|
22
|
+
parallelismRatio: number;
|
|
23
|
+
failedToolCalls: number;
|
|
24
|
+
reminders: number;
|
|
25
|
+
recoveryNotices: number;
|
|
26
|
+
prefixChanges: number;
|
|
27
|
+
wallClockMs: number;
|
|
28
|
+
modelStreamMs: number;
|
|
29
|
+
firstTokenMs: {
|
|
30
|
+
n: number;
|
|
31
|
+
p50: number | null;
|
|
32
|
+
p95: number | null;
|
|
33
|
+
};
|
|
34
|
+
usage: {
|
|
35
|
+
inputTokens: number;
|
|
36
|
+
outputTokens: number;
|
|
37
|
+
cacheReadTokens: number;
|
|
38
|
+
cacheWriteTokens: number;
|
|
39
|
+
};
|
|
40
|
+
turnOutcome: string | null;
|
|
41
|
+
runtimePolicy: RuntimePolicySummary | null;
|
|
42
|
+
fileMutationPolicy: FileMutationPolicySummary | null;
|
|
43
|
+
failureRecovery: FailureRecoveryStats | null;
|
|
44
|
+
compositeTools: CompositeToolsStats | null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface TurnComparisonDelta {
|
|
48
|
+
modelRequests: { baseline: number; candidate: number; delta: number };
|
|
49
|
+
toolCalls: { baseline: number; candidate: number; delta: number };
|
|
50
|
+
parallelismRatio: { baseline: number; candidate: number; delta: number };
|
|
51
|
+
failedToolCalls: { baseline: number; candidate: number; delta: number };
|
|
52
|
+
wallClockMs: { baseline: number; candidate: number; delta: number };
|
|
53
|
+
inputTokens: { baseline: number; candidate: number; delta: number };
|
|
54
|
+
outputTokens: { baseline: number; candidate: number; delta: number };
|
|
55
|
+
prefixChanges: { baseline: number; candidate: number };
|
|
56
|
+
bothCompleted: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export declare const TelemetryConfig: z<TelemetryOptions>;
|
|
60
|
+
export declare function compareTurnSummaries(
|
|
61
|
+
baseline: Partial<TurnTelemetrySummary>,
|
|
62
|
+
candidate: Partial<TurnTelemetrySummary>
|
|
63
|
+
): TurnComparisonDelta;
|
|
64
|
+
|
|
65
|
+
export declare class AgentTelemetryManager {
|
|
66
|
+
readonly config: TelemetryOptions;
|
|
67
|
+
constructor(ctx: Context, config?: TelemetryOptions);
|
|
68
|
+
onSessionEvent(session: unknown, event: any): void;
|
|
69
|
+
onSessionDisposed(session: unknown): void;
|
|
70
|
+
wrapStream(options: any, next: () => AsyncIterable<any>): AsyncIterable<any>;
|
|
71
|
+
getSummary(session?: unknown, turn?: number): TurnTelemetrySummary;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export declare function applyTelemetry(ctx: Context, rawConfig?: TelemetryOptions): AgentTelemetryManager | null;
|
|
75
|
+
export declare const name: string;
|
|
76
|
+
export declare const Config: z<TelemetryOptions>;
|
|
77
|
+
export declare function apply(ctx: Context, config?: TelemetryOptions): void;
|
|
78
|
+
declare const plugin: { name: typeof name; Config: typeof Config; apply: typeof apply };
|
|
79
|
+
export default plugin;
|