dsh-taskbridge 1.5.0 → 1.5.1
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/CHANGELOG.md +18 -0
- package/README.md +9 -8
- package/codex/dsh-taskbridge.toml +107 -12
- package/docs/INTRODUCTION.md +2 -2
- package/docs/INTRODUCTION.zh.md +2 -2
- package/docs/codex-integration.md +9 -13
- package/docs/plugin-market.md +1 -1
- package/docs/releases/1.5.1.zh.md +70 -0
- package/docs/runtime-compatibility.md +4 -4
- package/docs/silent-wait.md +75 -5
- package/docs/taskbridge-upgrade.md +1 -1
- package/docs/worker-permissions.md +1 -1
- package/lib/client.js +295 -103
- package/package.json +7 -3
- package/src/client/api.js +289 -34
- package/src/client/index.jsx +151 -67
- package/src/codex/installer.js +236 -11
- package/src/http/execution-plane.js +48 -5
- package/src/index.d.ts +62 -0
- package/src/index.js +18 -5
- package/src/jobs/activity.js +92 -9
- package/src/jobs/continuation-identity.js +283 -278
- package/src/jobs/engine.js +165 -19
- package/src/jobs/history.js +43 -0
- package/src/mcp/metrics.js +366 -343
- package/src/mcp/server.js +127 -9
- package/src/mcp/wait.js +77 -4
- package/src/runtime/agent-runner.js +165 -0
- package/src/runtime/sandbox.js +363 -358
- package/src/runtime/session-placement.js +1207 -1194
- package/src/version.js +121 -99
- package/src/workspace/worktrees.js +70 -4
package/src/mcp/metrics.js
CHANGED
|
@@ -1,343 +1,366 @@
|
|
|
1
|
-
// Minimal, honest accounting for the MCP surface.
|
|
2
|
-
//
|
|
3
|
-
// What this module is, in one sentence: a bounded census of what this MCP PROCESS did —
|
|
4
|
-
// which tools were exercised, how many waits it ran and why they returned, how many
|
|
5
|
-
// per-call permission decisions were applied, and how many bytes of tool text it handed
|
|
6
|
-
// back — with each post-decision wait tied to the decision that led into it.
|
|
7
|
-
//
|
|
8
|
-
// What it deliberately is NOT:
|
|
9
|
-
//
|
|
10
|
-
// * **Not a wake-up counter.** `delegate_wait` is one MCP call the host chose to make; a
|
|
11
|
-
// count of calls, observations or returns says how much the PROGRAM waited, never how
|
|
12
|
-
// many times a model was woken. The host's own model turns, token usage and cost are
|
|
13
|
-
// not visible to this process at all, so they are reported as `not-integrated` rather
|
|
14
|
-
// than as `0` — a zero would be a measurement this code never took.
|
|
15
|
-
// * **Not a content store.** Every counter is a number, a duration, a closed-vocabulary
|
|
16
|
-
// reason or a byte size. No tool arguments, no reasoning text, no answer body and no
|
|
17
|
-
// credential is retained, so no projection of this module can leak one.
|
|
18
|
-
// * **Not persistent.** The census lives for the life of the MCP process and is reported
|
|
19
|
-
// through one read-only call and the exit diagnostics line.
|
|
20
|
-
//
|
|
21
|
-
// Two counting rules are load-bearing:
|
|
22
|
-
//
|
|
23
|
-
// 1. **Totals are cumulative; the sample lists are bounded.** The `recent*` arrays are
|
|
24
|
-
// the last N entries so a caller can audit individual waits. Every total is summed at
|
|
25
|
-
// record time, so a long session's numbers never shrink when the sample rotates.
|
|
26
|
-
// 2. **Byte counts are of the FINAL tool text.** They are measured by the server on the
|
|
27
|
-
// bounded, redacted `content[0].text` with `Buffer.byteLength(..., 'utf8')`, after
|
|
28
|
-
// `boundedJson` has decided what the host receives — never `String#length`, which
|
|
29
|
-
// counts UTF-16 units and would misreport Chinese text and emoji, and never a size the
|
|
30
|
-
// answer carries about itself.
|
|
31
|
-
|
|
32
|
-
/** The closed vocabulary of what one wait returned, mirrored from the wait loop. */
|
|
33
|
-
export const METRIC_WAIT_REASONS = Object.freeze([
|
|
34
|
-
'attention', 'permission', 'question', 'ready', 'failed', 'cancelled', 'interrupted', 'wait-limit', 'refused',
|
|
35
|
-
]);
|
|
36
|
-
|
|
37
|
-
/** Outcomes of one decision, as the receipt states them. */
|
|
38
|
-
export const METRIC_DECISION_STATES = Object.freeze(['applied', 'unknown', 'refused']);
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* The decision kind that records an ordinary QUESTION answer.
|
|
42
|
-
*
|
|
43
|
-
* Kept apart from `allow-once`/`reject` because answering a question authorizes nothing:
|
|
44
|
-
* counting it in the permission buckets would report a refusal that never happened, or an
|
|
45
|
-
* authorization that does not exist.
|
|
46
|
-
*/
|
|
47
|
-
export const QUESTION_ANSWER_KIND = 'question-answer';
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Facts this process cannot observe, and therefore must not report as zero.
|
|
51
|
-
*
|
|
52
|
-
* The host model's own turns, token usage and cost happen in the host process. This one
|
|
53
|
-
* only ever sees the tool calls the host decided to make.
|
|
54
|
-
*/
|
|
55
|
-
export const NOT_INTEGRATED = 'not-integrated';
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Create the census for one MCP server instance.
|
|
59
|
-
*
|
|
60
|
-
* Every entry point is defensive: this module is fed by the server's hot paths, and a
|
|
61
|
-
* malformed value must degrade one counter rather than throw inside an answer path.
|
|
62
|
-
* @param {object} [options] - `now` (clock seam), `limit` (how many samples to keep).
|
|
63
|
-
* @returns {object} census with `recordToolCall`, `recordReturn`, `recordWait`,
|
|
64
|
-
* `recordDecision`, `snapshot`, `diagnosticsLine`.
|
|
65
|
-
*/
|
|
66
|
-
export function createCallMetrics(options = {}) {
|
|
67
|
-
const now = typeof options.now === 'function' ? options.now : () => Date.now();
|
|
68
|
-
const limit = Number.isInteger(options.limit) && options.limit > 0 ? options.limit : 64;
|
|
69
|
-
|
|
70
|
-
/** Cumulative tool calls by name. */
|
|
71
|
-
const toolCalls = new Map();
|
|
72
|
-
/** Cumulative wait returns by reason. */
|
|
73
|
-
const waitReturns = new Map();
|
|
74
|
-
/** Cumulative decision outcomes by state, and by decision kind for the applied ones. */
|
|
75
|
-
const decisionStates = new Map();
|
|
76
|
-
const appliedDecisions = new Map();
|
|
77
|
-
/** Question answers, kept out of the permission buckets. */
|
|
78
|
-
const questionAnswerStates = new Map();
|
|
79
|
-
|
|
80
|
-
/** Cumulative totals — summed at record time, never derived from the samples. */
|
|
81
|
-
let returns = 0;
|
|
82
|
-
let returnBytes = 0;
|
|
83
|
-
let unmeasuredReturns = 0;
|
|
84
|
-
let waits = 0;
|
|
85
|
-
let waitedTotalMs = 0;
|
|
86
|
-
let waitObservations = 0;
|
|
87
|
-
let decisions = 0;
|
|
88
|
-
let waitsAfterDecisions = 0;
|
|
89
|
-
let waitQuestionsAnswered = 0;
|
|
90
|
-
let questionAnswers = 0;
|
|
91
|
-
|
|
92
|
-
/** Bounded audit samples, newest last. */
|
|
93
|
-
const recentWaits = [];
|
|
94
|
-
const recentDecisions = [];
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Count one value in a bounded map.
|
|
98
|
-
* @param {Map<string, number>} map - counter map.
|
|
99
|
-
* @param {string} key - closed-vocabulary key.
|
|
100
|
-
* @returns {void}
|
|
101
|
-
*/
|
|
102
|
-
const bump = (map, key) => {
|
|
103
|
-
map.set(key, (map.get(key) ?? 0) + 1);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* A non-negative integer, or 0.
|
|
108
|
-
* @param {unknown} value - candidate.
|
|
109
|
-
* @returns {number} bounded count.
|
|
110
|
-
*/
|
|
111
|
-
const count = (value) => (Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0);
|
|
112
|
-
|
|
113
|
-
return {
|
|
114
|
-
/**
|
|
115
|
-
* Count one tool invocation, whatever its outcome.
|
|
116
|
-
* @param {string} tool - tool name.
|
|
117
|
-
* @returns {void}
|
|
118
|
-
*/
|
|
119
|
-
recordToolCall(tool) {
|
|
120
|
-
if (typeof tool === 'string' && tool !== '') bump(toolCalls, tool);
|
|
121
|
-
},
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Count one tool TEXT returned to the host, and its size.
|
|
125
|
-
*
|
|
126
|
-
* Called exactly once per `tools/call`, from the server, with the final
|
|
127
|
-
* `content[0].text` — the bounded, redacted text the client parses. `null` means no
|
|
128
|
-
* text was produced at all (which is a fact worth counting, not zero bytes).
|
|
129
|
-
* @param {string | null} text - the answer text, or null.
|
|
130
|
-
* @returns {void}
|
|
131
|
-
*/
|
|
132
|
-
recordReturn(text) {
|
|
133
|
-
returns += 1;
|
|
134
|
-
if (typeof text !== 'string') {
|
|
135
|
-
// Stated as unmeasured rather than counted as 0 bytes: "no text" and "empty text"
|
|
136
|
-
// are different, and neither is a size this code guessed.
|
|
137
|
-
unmeasuredReturns += 1;
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
let bytes;
|
|
141
|
-
try {
|
|
142
|
-
bytes = Buffer.byteLength(text, 'utf8');
|
|
143
|
-
} catch {
|
|
144
|
-
// A lone surrogate cannot be encoded; the text is not a size this run can state.
|
|
145
|
-
unmeasuredReturns += 1;
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
returnBytes += bytes;
|
|
149
|
-
},
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Record one wait's own accounting.
|
|
153
|
-
*
|
|
154
|
-
* One flat summary shape, whether the wait followed a decision or not: the wait loop
|
|
155
|
-
* reports `waitId`/`afterDecisionId`/`taskId`/`reason`/`durationMs`/counts and nothing
|
|
156
|
-
* else, so a plain wait and a post-decision wait are counted by the same rule.
|
|
157
|
-
* @param {object} summary - the wait loop's summary.
|
|
158
|
-
* @returns {void}
|
|
159
|
-
*/
|
|
160
|
-
recordWait(summary) {
|
|
161
|
-
const known = METRIC_WAIT_REASONS.includes(summary?.reason);
|
|
162
|
-
const reason = known ? summary.reason : 'refused';
|
|
163
|
-
const durationMs = count(summary?.durationMs);
|
|
164
|
-
const waitId = typeof summary?.waitId === 'string' ? summary.waitId : null;
|
|
165
|
-
const afterDecisionId = typeof summary?.afterDecisionId === 'string' ? summary.afterDecisionId : null;
|
|
166
|
-
waits += 1;
|
|
167
|
-
waitedTotalMs += durationMs;
|
|
168
|
-
waitObservations += count(summary?.observationRequests);
|
|
169
|
-
bump(waitReturns, reason);
|
|
170
|
-
if (afterDecisionId !== null) waitsAfterDecisions += 1;
|
|
171
|
-
if (afterDecisionId !== null && reason === 'permission') waitQuestionsAnswered += 1;
|
|
172
|
-
recentWaits.push({
|
|
173
|
-
waitId,
|
|
174
|
-
afterDecisionId,
|
|
175
|
-
taskId: typeof summary?.taskId === 'string' ? summary.taskId : null,
|
|
176
|
-
reason,
|
|
177
|
-
durationMs,
|
|
178
|
-
observationRequests: count(summary?.observationRequests),
|
|
179
|
-
heartbeatsConsumed: count(summary?.heartbeatsConsumed),
|
|
180
|
-
settledRequestObservations: count(summary?.settledRequestObservations),
|
|
181
|
-
waitReturns: count(summary?.waitReturns),
|
|
182
|
-
at: now(),
|
|
183
|
-
});
|
|
184
|
-
if (recentWaits.length > limit) recentWaits.shift();
|
|
185
|
-
},
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Record one ACTUAL permission decision and how far it got.
|
|
189
|
-
*
|
|
190
|
-
* Only the decision path calls this, and only with what the receipt could prove:
|
|
191
|
-
* `applied` for a confirmed decision, `unknown` when the call failed without an answer,
|
|
192
|
-
* `refused` when the harness said no. A refused or unconfirmed attempt is counted as an
|
|
193
|
-
* attempt and never as an authorization.
|
|
194
|
-
*
|
|
195
|
-
* The `applied` field of the recorded sample is the SAME three-valued fact the permission
|
|
196
|
-
* receipt publishes, not a boolean derived from the state label: `true` for `applied`,
|
|
197
|
-
* `false` for `refused`, and `null` for `unknown`. An unconfirmed attempt whose sample
|
|
198
|
-
* said `false` would contradict both the receipt (`applied: null`) and the cumulative
|
|
199
|
-
* `decisions.unknown` counter, and a reader could take "not applied" for a fact nobody
|
|
200
|
-
* established.
|
|
201
|
-
* @param {object} entry - `taskId`, `requestId`, `decision`, `fingerprint`, `applied`,
|
|
202
|
-
* `confirmation`, optional `receiptId`, `at`.
|
|
203
|
-
* @returns {void}
|
|
204
|
-
*/
|
|
205
|
-
recordDecision(entry) {
|
|
206
|
-
const state = METRIC_DECISION_STATES.includes(entry?.confirmation)
|
|
207
|
-
? entry.confirmation
|
|
208
|
-
: (entry?.applied === true ? 'applied' : entry?.applied === false ? 'refused' : 'unknown');
|
|
209
|
-
// A QUESTION ANSWER is not a permission decision: it grants nothing, so it is counted
|
|
210
|
-
// on its own axis and NEVER touches the permission buckets (its verb is neither
|
|
211
|
-
// allow-once nor reject, which would otherwise have been reported as a refusal).
|
|
212
|
-
if (entry?.decision === QUESTION_ANSWER_KIND) {
|
|
213
|
-
questionAnswers += 1;
|
|
214
|
-
bump(questionAnswerStates, state);
|
|
215
|
-
recentDecisions.push({
|
|
216
|
-
receiptId: typeof entry?.receiptId === 'string' ? entry.receiptId : null,
|
|
217
|
-
taskId: typeof entry?.taskId === 'string' ? entry.taskId : null,
|
|
218
|
-
requestId: typeof entry?.requestId === 'string' ? entry.requestId : null,
|
|
219
|
-
decision: QUESTION_ANSWER_KIND,
|
|
220
|
-
fingerprint: null,
|
|
221
|
-
applied: state === 'applied' ? true : state === 'refused' ? false : null,
|
|
222
|
-
state,
|
|
223
|
-
at: Number.isFinite(entry?.at) ? entry.at : now(),
|
|
224
|
-
});
|
|
225
|
-
if (recentDecisions.length > limit) recentDecisions.shift();
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
const decision = entry?.decision === 'allow-once' || entry?.decision === 'reject' ? entry.decision : null;
|
|
229
|
-
decisions += 1;
|
|
230
|
-
bump(decisionStates, state);
|
|
231
|
-
if (state === 'applied' && decision !== null) bump(appliedDecisions, decision);
|
|
232
|
-
recentDecisions.push({
|
|
233
|
-
receiptId: typeof entry?.receiptId === 'string' ? entry.receiptId : null,
|
|
234
|
-
taskId: typeof entry?.taskId === 'string' ? entry.taskId : null,
|
|
235
|
-
requestId: typeof entry?.requestId === 'string' ? entry.requestId : null,
|
|
236
|
-
decision,
|
|
237
|
-
// A digest of the arguments the decision was bound to, never the arguments.
|
|
238
|
-
fingerprint: typeof entry?.fingerprint === 'string' ? entry.fingerprint : null,
|
|
239
|
-
// Three-valued on purpose: unknown is `null`, never `false`.
|
|
240
|
-
applied: state === 'applied' ? true : state === 'refused' ? false : null,
|
|
241
|
-
state,
|
|
242
|
-
at: Number.isFinite(entry?.at) ? entry.at : now(),
|
|
243
|
-
});
|
|
244
|
-
if (recentDecisions.length > limit) recentDecisions.shift();
|
|
245
|
-
},
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* The census, as a bounded read-only answer.
|
|
249
|
-
*
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
//
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
1
|
+
// Minimal, honest accounting for the MCP surface.
|
|
2
|
+
//
|
|
3
|
+
// What this module is, in one sentence: a bounded census of what this MCP PROCESS did —
|
|
4
|
+
// which tools were exercised, how many waits it ran and why they returned, how many
|
|
5
|
+
// per-call permission decisions were applied, and how many bytes of tool text it handed
|
|
6
|
+
// back — with each post-decision wait tied to the decision that led into it.
|
|
7
|
+
//
|
|
8
|
+
// What it deliberately is NOT:
|
|
9
|
+
//
|
|
10
|
+
// * **Not a wake-up counter.** `delegate_wait` is one MCP call the host chose to make; a
|
|
11
|
+
// count of calls, observations or returns says how much the PROGRAM waited, never how
|
|
12
|
+
// many times a model was woken. The host's own model turns, token usage and cost are
|
|
13
|
+
// not visible to this process at all, so they are reported as `not-integrated` rather
|
|
14
|
+
// than as `0` — a zero would be a measurement this code never took.
|
|
15
|
+
// * **Not a content store.** Every counter is a number, a duration, a closed-vocabulary
|
|
16
|
+
// reason or a byte size. No tool arguments, no reasoning text, no answer body and no
|
|
17
|
+
// credential is retained, so no projection of this module can leak one.
|
|
18
|
+
// * **Not persistent.** The census lives for the life of the MCP process and is reported
|
|
19
|
+
// through one read-only call and the exit diagnostics line.
|
|
20
|
+
//
|
|
21
|
+
// Two counting rules are load-bearing:
|
|
22
|
+
//
|
|
23
|
+
// 1. **Totals are cumulative; the sample lists are bounded.** The `recent*` arrays are
|
|
24
|
+
// the last N entries so a caller can audit individual waits. Every total is summed at
|
|
25
|
+
// record time, so a long session's numbers never shrink when the sample rotates.
|
|
26
|
+
// 2. **Byte counts are of the FINAL tool text.** They are measured by the server on the
|
|
27
|
+
// bounded, redacted `content[0].text` with `Buffer.byteLength(..., 'utf8')`, after
|
|
28
|
+
// `boundedJson` has decided what the host receives — never `String#length`, which
|
|
29
|
+
// counts UTF-16 units and would misreport Chinese text and emoji, and never a size the
|
|
30
|
+
// answer carries about itself.
|
|
31
|
+
|
|
32
|
+
/** The closed vocabulary of what one wait returned, mirrored from the wait loop. */
|
|
33
|
+
export const METRIC_WAIT_REASONS = Object.freeze([
|
|
34
|
+
'attention', 'permission', 'question', 'ready', 'failed', 'cancelled', 'interrupted', 'wait-limit', 'refused',
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
/** Outcomes of one decision, as the receipt states them. */
|
|
38
|
+
export const METRIC_DECISION_STATES = Object.freeze(['applied', 'unknown', 'refused']);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The decision kind that records an ordinary QUESTION answer.
|
|
42
|
+
*
|
|
43
|
+
* Kept apart from `allow-once`/`reject` because answering a question authorizes nothing:
|
|
44
|
+
* counting it in the permission buckets would report a refusal that never happened, or an
|
|
45
|
+
* authorization that does not exist.
|
|
46
|
+
*/
|
|
47
|
+
export const QUESTION_ANSWER_KIND = 'question-answer';
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Facts this process cannot observe, and therefore must not report as zero.
|
|
51
|
+
*
|
|
52
|
+
* The host model's own turns, token usage and cost happen in the host process. This one
|
|
53
|
+
* only ever sees the tool calls the host decided to make.
|
|
54
|
+
*/
|
|
55
|
+
export const NOT_INTEGRATED = 'not-integrated';
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Create the census for one MCP server instance.
|
|
59
|
+
*
|
|
60
|
+
* Every entry point is defensive: this module is fed by the server's hot paths, and a
|
|
61
|
+
* malformed value must degrade one counter rather than throw inside an answer path.
|
|
62
|
+
* @param {object} [options] - `now` (clock seam), `limit` (how many samples to keep).
|
|
63
|
+
* @returns {object} census with `recordToolCall`, `recordReturn`, `recordWait`,
|
|
64
|
+
* `recordDecision`, `snapshot`, `diagnosticsLine`.
|
|
65
|
+
*/
|
|
66
|
+
export function createCallMetrics(options = {}) {
|
|
67
|
+
const now = typeof options.now === 'function' ? options.now : () => Date.now();
|
|
68
|
+
const limit = Number.isInteger(options.limit) && options.limit > 0 ? options.limit : 64;
|
|
69
|
+
|
|
70
|
+
/** Cumulative tool calls by name. */
|
|
71
|
+
const toolCalls = new Map();
|
|
72
|
+
/** Cumulative wait returns by reason. */
|
|
73
|
+
const waitReturns = new Map();
|
|
74
|
+
/** Cumulative decision outcomes by state, and by decision kind for the applied ones. */
|
|
75
|
+
const decisionStates = new Map();
|
|
76
|
+
const appliedDecisions = new Map();
|
|
77
|
+
/** Question answers, kept out of the permission buckets. */
|
|
78
|
+
const questionAnswerStates = new Map();
|
|
79
|
+
|
|
80
|
+
/** Cumulative totals — summed at record time, never derived from the samples. */
|
|
81
|
+
let returns = 0;
|
|
82
|
+
let returnBytes = 0;
|
|
83
|
+
let unmeasuredReturns = 0;
|
|
84
|
+
let waits = 0;
|
|
85
|
+
let waitedTotalMs = 0;
|
|
86
|
+
let waitObservations = 0;
|
|
87
|
+
let decisions = 0;
|
|
88
|
+
let waitsAfterDecisions = 0;
|
|
89
|
+
let waitQuestionsAnswered = 0;
|
|
90
|
+
let questionAnswers = 0;
|
|
91
|
+
|
|
92
|
+
/** Bounded audit samples, newest last. */
|
|
93
|
+
const recentWaits = [];
|
|
94
|
+
const recentDecisions = [];
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Count one value in a bounded map.
|
|
98
|
+
* @param {Map<string, number>} map - counter map.
|
|
99
|
+
* @param {string} key - closed-vocabulary key.
|
|
100
|
+
* @returns {void}
|
|
101
|
+
*/
|
|
102
|
+
const bump = (map, key) => {
|
|
103
|
+
map.set(key, (map.get(key) ?? 0) + 1);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A non-negative integer, or 0.
|
|
108
|
+
* @param {unknown} value - candidate.
|
|
109
|
+
* @returns {number} bounded count.
|
|
110
|
+
*/
|
|
111
|
+
const count = (value) => (Number.isFinite(value) && value > 0 ? Math.trunc(value) : 0);
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
/**
|
|
115
|
+
* Count one tool invocation, whatever its outcome.
|
|
116
|
+
* @param {string} tool - tool name.
|
|
117
|
+
* @returns {void}
|
|
118
|
+
*/
|
|
119
|
+
recordToolCall(tool) {
|
|
120
|
+
if (typeof tool === 'string' && tool !== '') bump(toolCalls, tool);
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Count one tool TEXT returned to the host, and its size.
|
|
125
|
+
*
|
|
126
|
+
* Called exactly once per `tools/call`, from the server, with the final
|
|
127
|
+
* `content[0].text` — the bounded, redacted text the client parses. `null` means no
|
|
128
|
+
* text was produced at all (which is a fact worth counting, not zero bytes).
|
|
129
|
+
* @param {string | null} text - the answer text, or null.
|
|
130
|
+
* @returns {void}
|
|
131
|
+
*/
|
|
132
|
+
recordReturn(text) {
|
|
133
|
+
returns += 1;
|
|
134
|
+
if (typeof text !== 'string') {
|
|
135
|
+
// Stated as unmeasured rather than counted as 0 bytes: "no text" and "empty text"
|
|
136
|
+
// are different, and neither is a size this code guessed.
|
|
137
|
+
unmeasuredReturns += 1;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
let bytes;
|
|
141
|
+
try {
|
|
142
|
+
bytes = Buffer.byteLength(text, 'utf8');
|
|
143
|
+
} catch {
|
|
144
|
+
// A lone surrogate cannot be encoded; the text is not a size this run can state.
|
|
145
|
+
unmeasuredReturns += 1;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
returnBytes += bytes;
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Record one wait's own accounting.
|
|
153
|
+
*
|
|
154
|
+
* One flat summary shape, whether the wait followed a decision or not: the wait loop
|
|
155
|
+
* reports `waitId`/`afterDecisionId`/`taskId`/`reason`/`durationMs`/counts and nothing
|
|
156
|
+
* else, so a plain wait and a post-decision wait are counted by the same rule.
|
|
157
|
+
* @param {object} summary - the wait loop's summary.
|
|
158
|
+
* @returns {void}
|
|
159
|
+
*/
|
|
160
|
+
recordWait(summary) {
|
|
161
|
+
const known = METRIC_WAIT_REASONS.includes(summary?.reason);
|
|
162
|
+
const reason = known ? summary.reason : 'refused';
|
|
163
|
+
const durationMs = count(summary?.durationMs);
|
|
164
|
+
const waitId = typeof summary?.waitId === 'string' ? summary.waitId : null;
|
|
165
|
+
const afterDecisionId = typeof summary?.afterDecisionId === 'string' ? summary.afterDecisionId : null;
|
|
166
|
+
waits += 1;
|
|
167
|
+
waitedTotalMs += durationMs;
|
|
168
|
+
waitObservations += count(summary?.observationRequests);
|
|
169
|
+
bump(waitReturns, reason);
|
|
170
|
+
if (afterDecisionId !== null) waitsAfterDecisions += 1;
|
|
171
|
+
if (afterDecisionId !== null && reason === 'permission') waitQuestionsAnswered += 1;
|
|
172
|
+
recentWaits.push({
|
|
173
|
+
waitId,
|
|
174
|
+
afterDecisionId,
|
|
175
|
+
taskId: typeof summary?.taskId === 'string' ? summary.taskId : null,
|
|
176
|
+
reason,
|
|
177
|
+
durationMs,
|
|
178
|
+
observationRequests: count(summary?.observationRequests),
|
|
179
|
+
heartbeatsConsumed: count(summary?.heartbeatsConsumed),
|
|
180
|
+
settledRequestObservations: count(summary?.settledRequestObservations),
|
|
181
|
+
waitReturns: count(summary?.waitReturns),
|
|
182
|
+
at: now(),
|
|
183
|
+
});
|
|
184
|
+
if (recentWaits.length > limit) recentWaits.shift();
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Record one ACTUAL permission decision and how far it got.
|
|
189
|
+
*
|
|
190
|
+
* Only the decision path calls this, and only with what the receipt could prove:
|
|
191
|
+
* `applied` for a confirmed decision, `unknown` when the call failed without an answer,
|
|
192
|
+
* `refused` when the harness said no. A refused or unconfirmed attempt is counted as an
|
|
193
|
+
* attempt and never as an authorization.
|
|
194
|
+
*
|
|
195
|
+
* The `applied` field of the recorded sample is the SAME three-valued fact the permission
|
|
196
|
+
* receipt publishes, not a boolean derived from the state label: `true` for `applied`,
|
|
197
|
+
* `false` for `refused`, and `null` for `unknown`. An unconfirmed attempt whose sample
|
|
198
|
+
* said `false` would contradict both the receipt (`applied: null`) and the cumulative
|
|
199
|
+
* `decisions.unknown` counter, and a reader could take "not applied" for a fact nobody
|
|
200
|
+
* established.
|
|
201
|
+
* @param {object} entry - `taskId`, `requestId`, `decision`, `fingerprint`, `applied`,
|
|
202
|
+
* `confirmation`, optional `receiptId`, `at`.
|
|
203
|
+
* @returns {void}
|
|
204
|
+
*/
|
|
205
|
+
recordDecision(entry) {
|
|
206
|
+
const state = METRIC_DECISION_STATES.includes(entry?.confirmation)
|
|
207
|
+
? entry.confirmation
|
|
208
|
+
: (entry?.applied === true ? 'applied' : entry?.applied === false ? 'refused' : 'unknown');
|
|
209
|
+
// A QUESTION ANSWER is not a permission decision: it grants nothing, so it is counted
|
|
210
|
+
// on its own axis and NEVER touches the permission buckets (its verb is neither
|
|
211
|
+
// allow-once nor reject, which would otherwise have been reported as a refusal).
|
|
212
|
+
if (entry?.decision === QUESTION_ANSWER_KIND) {
|
|
213
|
+
questionAnswers += 1;
|
|
214
|
+
bump(questionAnswerStates, state);
|
|
215
|
+
recentDecisions.push({
|
|
216
|
+
receiptId: typeof entry?.receiptId === 'string' ? entry.receiptId : null,
|
|
217
|
+
taskId: typeof entry?.taskId === 'string' ? entry.taskId : null,
|
|
218
|
+
requestId: typeof entry?.requestId === 'string' ? entry.requestId : null,
|
|
219
|
+
decision: QUESTION_ANSWER_KIND,
|
|
220
|
+
fingerprint: null,
|
|
221
|
+
applied: state === 'applied' ? true : state === 'refused' ? false : null,
|
|
222
|
+
state,
|
|
223
|
+
at: Number.isFinite(entry?.at) ? entry.at : now(),
|
|
224
|
+
});
|
|
225
|
+
if (recentDecisions.length > limit) recentDecisions.shift();
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const decision = entry?.decision === 'allow-once' || entry?.decision === 'reject' ? entry.decision : null;
|
|
229
|
+
decisions += 1;
|
|
230
|
+
bump(decisionStates, state);
|
|
231
|
+
if (state === 'applied' && decision !== null) bump(appliedDecisions, decision);
|
|
232
|
+
recentDecisions.push({
|
|
233
|
+
receiptId: typeof entry?.receiptId === 'string' ? entry.receiptId : null,
|
|
234
|
+
taskId: typeof entry?.taskId === 'string' ? entry.taskId : null,
|
|
235
|
+
requestId: typeof entry?.requestId === 'string' ? entry.requestId : null,
|
|
236
|
+
decision,
|
|
237
|
+
// A digest of the arguments the decision was bound to, never the arguments.
|
|
238
|
+
fingerprint: typeof entry?.fingerprint === 'string' ? entry.fingerprint : null,
|
|
239
|
+
// Three-valued on purpose: unknown is `null`, never `false`.
|
|
240
|
+
applied: state === 'applied' ? true : state === 'refused' ? false : null,
|
|
241
|
+
state,
|
|
242
|
+
at: Number.isFinite(entry?.at) ? entry.at : now(),
|
|
243
|
+
});
|
|
244
|
+
if (recentDecisions.length > limit) recentDecisions.shift();
|
|
245
|
+
},
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The census, as a bounded read-only answer.
|
|
249
|
+
*
|
|
250
|
+
* The audit SAMPLE is capped well below the internal ring buffer: a long session
|
|
251
|
+
* (hundreds of waits and decisions) must still be able to ANSWER, and the sample
|
|
252
|
+
* lists were the one part `boundedJson` cannot shrink — without this cap they
|
|
253
|
+
* grew the whole answer past the tool-text bound and the caller received
|
|
254
|
+
* `MCP_RESULT_OVER_BUDGET` instead of even the totals. The cap keeps only the
|
|
255
|
+
* MOST RECENT entries and STATES how many were omitted, so a reader can always
|
|
256
|
+
* tell a partial view from the full history; the cumulative counters above are
|
|
257
|
+
* exact regardless of the sample.
|
|
258
|
+
* @returns {object} snapshot with no content and no credentials.
|
|
259
|
+
*/
|
|
260
|
+
snapshot() {
|
|
261
|
+
/** How many recent entries the published sample keeps, per list. */
|
|
262
|
+
const sampleLimit = 8;
|
|
263
|
+
const keptWaits = recentWaits.slice(-sampleLimit);
|
|
264
|
+
const keptDecisions = recentDecisions.slice(-sampleLimit);
|
|
265
|
+
return {
|
|
266
|
+
schemaVersion: 1,
|
|
267
|
+
// What each counter MEANS, stated where the numbers are, so no reader can take
|
|
268
|
+
// `waitReturns` for a number of model wake-ups.
|
|
269
|
+
definitions: {
|
|
270
|
+
mcpToolCalls: 'tool invocations the HOST made on this MCP connection, by tool name',
|
|
271
|
+
returnedText: 'answers this process handed back, and their UTF-8 bytes as handed to the transport',
|
|
272
|
+
waitReturns: 'wait answers by reason; one per host call, NOT a model wake-up',
|
|
273
|
+
waitObservations: 'internal bounded observations issued by the wait loop; program-internal',
|
|
274
|
+
decisions: 'permission decisions by outcome; only `applied` authorized anything',
|
|
275
|
+
questionAnswers: 'ordinary question answers by outcome; they authorize NOTHING',
|
|
276
|
+
waitsAfterDecisions: 'waits that continued inside the same call as a permission decision',
|
|
277
|
+
waitQuestionsAnswered: 'post-decision waits that returned ANOTHER pending permission request',
|
|
278
|
+
hostModelResponses: 'host model turns; cannot be observed from this process',
|
|
279
|
+
hostTokenUsage: 'host token usage; cannot be observed from this process',
|
|
280
|
+
},
|
|
281
|
+
hostObservability: {
|
|
282
|
+
hostModelResponses: NOT_INTEGRATED,
|
|
283
|
+
hostTokenUsage: NOT_INTEGRATED,
|
|
284
|
+
hostCost: NOT_INTEGRATED,
|
|
285
|
+
reason: 'the host model runs in its own process; this server only sees the tool calls it receives',
|
|
286
|
+
},
|
|
287
|
+
tools: Object.fromEntries([...toolCalls.entries()].sort(([a], [b]) => a.localeCompare(b))),
|
|
288
|
+
returnedText: {
|
|
289
|
+
count: returns,
|
|
290
|
+
// Bytes of the final bounded, redacted tool text. Not a delivery receipt: the
|
|
291
|
+
// transport accepted it, which is not the same as the host having read it.
|
|
292
|
+
bytes: returnBytes,
|
|
293
|
+
unmeasured: unmeasuredReturns,
|
|
294
|
+
unit: 'utf8-bytes-of-serialized-tool-text',
|
|
295
|
+
},
|
|
296
|
+
waits: {
|
|
297
|
+
count: waits,
|
|
298
|
+
totalMs: waitedTotalMs,
|
|
299
|
+
observations: waitObservations,
|
|
300
|
+
reasons: Object.fromEntries([...waitReturns.entries()].sort(([a], [b]) => a.localeCompare(b))),
|
|
301
|
+
},
|
|
302
|
+
decisions: {
|
|
303
|
+
// Cumulative, not derived from the samples below: a 65th decision still counts.
|
|
304
|
+
count: decisions,
|
|
305
|
+
applied: decisionStates.get('applied') ?? 0,
|
|
306
|
+
unknown: decisionStates.get('unknown') ?? 0,
|
|
307
|
+
refused: decisionStates.get('refused') ?? 0,
|
|
308
|
+
allowed: appliedDecisions.get('allow-once') ?? 0,
|
|
309
|
+
rejected: appliedDecisions.get('reject') ?? 0,
|
|
310
|
+
},
|
|
311
|
+
// Ordinary question answers, reported apart from permission decisions because they
|
|
312
|
+
// authorize nothing. A question answer is never counted as a refusal.
|
|
313
|
+
questionAnswers: {
|
|
314
|
+
count: questionAnswers,
|
|
315
|
+
applied: questionAnswerStates.get('applied') ?? 0,
|
|
316
|
+
unknown: questionAnswerStates.get('unknown') ?? 0,
|
|
317
|
+
refused: questionAnswerStates.get('refused') ?? 0,
|
|
318
|
+
},
|
|
319
|
+
correlation: {
|
|
320
|
+
waitsAfterDecisions,
|
|
321
|
+
waitQuestionsAnswered,
|
|
322
|
+
// The bounded sample is what makes the counts auditable: one identifier per wait,
|
|
323
|
+
// its reason, its duration, and the decision it followed. Published with an
|
|
324
|
+
// explicit omission statement, so a partial sample is never mistaken for the
|
|
325
|
+
// full history. The omission counts are measured against the CUMULATIVE
|
|
326
|
+
// histories (waits; decisions PLUS question answers, which share one sample
|
|
327
|
+
// list), never against the internal ring buffer — so the statement is exactly
|
|
328
|
+
// "how many of the recorded facts this answer does not show", even on a
|
|
329
|
+
// session shorter than the ring.
|
|
330
|
+
recentWaits: keptWaits.map((entry) => ({ ...entry })),
|
|
331
|
+
recentDecisions: keptDecisions.map((entry) => ({ ...entry })),
|
|
332
|
+
omitted: {
|
|
333
|
+
waits: waits - keptWaits.length,
|
|
334
|
+
decisions: (decisions + questionAnswers) - keptDecisions.length,
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
},
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* One bounded line for the exit diagnostics.
|
|
342
|
+
*
|
|
343
|
+
* Counts and byte totals only. Printed when the process ends, so an operator who never
|
|
344
|
+
* called `delegate_metrics` still learns how much this MCP process did — and that the
|
|
345
|
+
* host's own model activity is NOT part of it.
|
|
346
|
+
* @returns {string} single line, no task content.
|
|
347
|
+
*/
|
|
348
|
+
diagnosticsLine() {
|
|
349
|
+
const reasons = [...waitReturns.entries()].map(([key, value]) => `${key}=${value}`).join(',') || 'none';
|
|
350
|
+
const total = [...toolCalls.values()].reduce((sum, value) => sum + value, 0);
|
|
351
|
+
return `dsh-delegate-mcp: calls=${total} returns=${returns} returnBytes=${returnBytes} `
|
|
352
|
+
+ `waits=${waits} returns(${reasons}) decisions=${decisions} applied=${decisionStates.get('applied') ?? 0} `
|
|
353
|
+
+ `waitsAfterDecisions=${waitsAfterDecisions} hostModelResponses=${NOT_INTEGRATED}`;
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Build one stable identifier for a call chain.
|
|
360
|
+
* @param {string} prefix - `wait` or `decision`.
|
|
361
|
+
* @param {number} sequence - per-server counter.
|
|
362
|
+
* @returns {string} identifier.
|
|
363
|
+
*/
|
|
364
|
+
export function correlationId(prefix, sequence) {
|
|
365
|
+
return `${prefix}-${Number.isInteger(sequence) ? sequence : 0}-${Date.now().toString(36)}`;
|
|
366
|
+
}
|