jules-orchestrator-kit 0.22.9 → 0.23.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/bin/agentctl.mjs +1 -1
- package/package.json +1 -1
- package/src/dag-engine.mjs +23 -1
- package/src/engine.mjs +46 -3
- package/src/mcp-progress.mjs +158 -0
- package/src/mcp.mjs +36 -2
- package/src/security.mjs +3 -1
- package/src/telemetry.mjs +275 -0
package/bin/agentctl.mjs
CHANGED
|
@@ -14,7 +14,7 @@ const command = args[0];
|
|
|
14
14
|
|
|
15
15
|
function printHelp() {
|
|
16
16
|
console.log(`
|
|
17
|
-
🚀 agentctl v0.
|
|
17
|
+
🚀 agentctl v0.23.0 — Universal Agent Orchestrator & Safety Gatekeeper
|
|
18
18
|
|
|
19
19
|
Usage: agentctl <command> [options]
|
|
20
20
|
|
package/package.json
CHANGED
package/src/dag-engine.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { readFileSync, existsSync } from "node:fs";
|
|
3
|
+
import { appendTelemetry } from "./telemetry.mjs";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Custom error thrown when a circular dependency is detected in the DAG.
|
|
@@ -200,8 +201,17 @@ export class DagExecutor {
|
|
|
200
201
|
* @param {Object} [options]
|
|
201
202
|
* @param {number} [options.concurrency]
|
|
202
203
|
* @param {number} [options.taskTimeout]
|
|
204
|
+
* @param {string} [options.root]
|
|
205
|
+
* @param {Object} [options.progressBus]
|
|
206
|
+
* @param {string} [options.progressToken]
|
|
203
207
|
*/
|
|
204
|
-
async execute({
|
|
208
|
+
async execute({
|
|
209
|
+
concurrency = this.concurrency,
|
|
210
|
+
taskTimeout = this.taskTimeout,
|
|
211
|
+
root,
|
|
212
|
+
progressBus,
|
|
213
|
+
progressToken,
|
|
214
|
+
} = {}) {
|
|
205
215
|
this.isExecuting = true;
|
|
206
216
|
const { dependents } = this.validateDag();
|
|
207
217
|
|
|
@@ -265,6 +275,7 @@ export class DagExecutor {
|
|
|
265
275
|
}
|
|
266
276
|
|
|
267
277
|
running.add(taskId);
|
|
278
|
+
if (root) appendTelemetry(root, "dag_task_started", { taskId });
|
|
268
279
|
|
|
269
280
|
(async () => {
|
|
270
281
|
try {
|
|
@@ -291,6 +302,17 @@ export class DagExecutor {
|
|
|
291
302
|
running.delete(taskId);
|
|
292
303
|
completed.add(taskId);
|
|
293
304
|
|
|
305
|
+
if (root) appendTelemetry(root, "dag_task_completed", { taskId });
|
|
306
|
+
if (progressBus && progressToken) {
|
|
307
|
+
const pct = Math.round((completed.size / this.tasks.size) * 100);
|
|
308
|
+
progressBus.reportProgress(
|
|
309
|
+
progressToken,
|
|
310
|
+
pct,
|
|
311
|
+
100,
|
|
312
|
+
`Task ${taskId} completed (${completed.size}/${this.tasks.size})`
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
294
316
|
for (const depNode of dependents.get(taskId)) {
|
|
295
317
|
inDegree.set(depNode, inDegree.get(depNode) - 1);
|
|
296
318
|
}
|
package/src/engine.mjs
CHANGED
|
@@ -8,6 +8,7 @@ import { recordVerifyRun, readVerifyRuns, flakyVerdict } from "./flaky-ledger.mj
|
|
|
8
8
|
import { readdirSync, readFileSync, renameSync, existsSync } from "node:fs";
|
|
9
9
|
import { join } from "node:path";
|
|
10
10
|
import { createHash } from "node:crypto";
|
|
11
|
+
import { appendTelemetry } from "./telemetry.mjs";
|
|
11
12
|
|
|
12
13
|
export { recordVerifyRun, readVerifyRuns, flakyVerdict, sanitizeUntrustedData };
|
|
13
14
|
|
|
@@ -100,8 +101,12 @@ export async function gate(opts = {}) {
|
|
|
100
101
|
const root = opts.root || process.cwd();
|
|
101
102
|
const config = opts.config || loadConfig(root);
|
|
102
103
|
const base = opts.base || config.baseBranch || "main";
|
|
104
|
+
const progressBus = opts.progressBus;
|
|
105
|
+
const progressToken = opts.progressToken;
|
|
103
106
|
const phases = [];
|
|
104
107
|
|
|
108
|
+
appendTelemetry(root, "gate_started", { base });
|
|
109
|
+
|
|
105
110
|
let files = [];
|
|
106
111
|
let bytes = 0;
|
|
107
112
|
let diffStr = "";
|
|
@@ -112,6 +117,7 @@ export async function gate(opts = {}) {
|
|
|
112
117
|
diffStr = diffText(root, base);
|
|
113
118
|
} catch (err) {
|
|
114
119
|
phases.push({ phase: "git_resolution", ok: false, error: err.message });
|
|
120
|
+
appendTelemetry(root, "gate_finished", { ok: false, code: 1, error: err.message });
|
|
115
121
|
return { ok: false, code: 1, phases, error: err.message };
|
|
116
122
|
}
|
|
117
123
|
|
|
@@ -142,7 +148,13 @@ export async function gate(opts = {}) {
|
|
|
142
148
|
});
|
|
143
149
|
|
|
144
150
|
phases.push({ phase: "scope", ok: scopeResult.ok, violations: scopeResult.violations });
|
|
151
|
+
appendTelemetry(root, "gate_phase", { phase: "scope", ok: scopeResult.ok });
|
|
152
|
+
if (progressBus && progressToken) {
|
|
153
|
+
progressBus.reportProgress(progressToken, 25, 100, "Phase 1/4: Scope Guard verification complete");
|
|
154
|
+
}
|
|
155
|
+
|
|
145
156
|
if (!scopeResult.ok) {
|
|
157
|
+
appendTelemetry(root, "gate_finished", { ok: false, code: 3 });
|
|
146
158
|
return { ok: false, code: 3, phases };
|
|
147
159
|
}
|
|
148
160
|
|
|
@@ -150,14 +162,26 @@ export async function gate(opts = {}) {
|
|
|
150
162
|
const limitBytes = (config.limits.diffKb || 75) * 1024;
|
|
151
163
|
const payloadOk = bytes <= limitBytes;
|
|
152
164
|
phases.push({ phase: "payload", ok: payloadOk, bytes, limitBytes });
|
|
165
|
+
appendTelemetry(root, "gate_phase", { phase: "payload", ok: payloadOk });
|
|
166
|
+
if (progressBus && progressToken) {
|
|
167
|
+
progressBus.reportProgress(progressToken, 50, 100, "Phase 2/4: Diff Payload Governor check complete");
|
|
168
|
+
}
|
|
169
|
+
|
|
153
170
|
if (!payloadOk) {
|
|
171
|
+
appendTelemetry(root, "gate_finished", { ok: false, code: 5 });
|
|
154
172
|
return { ok: false, code: 5, phases };
|
|
155
173
|
}
|
|
156
174
|
|
|
157
175
|
// Phase 3: Diff Secret Scanner
|
|
158
176
|
const secretResult = scanDiff(diffStr);
|
|
159
177
|
phases.push({ phase: "secrets", ok: secretResult.ok, findings: secretResult.findings });
|
|
178
|
+
appendTelemetry(root, "gate_phase", { phase: "secrets", ok: secretResult.ok });
|
|
179
|
+
if (progressBus && progressToken) {
|
|
180
|
+
progressBus.reportProgress(progressToken, 75, 100, "Phase 3/4: Diff Secret Scanner check complete");
|
|
181
|
+
}
|
|
182
|
+
|
|
160
183
|
if (!secretResult.ok) {
|
|
184
|
+
appendTelemetry(root, "gate_finished", { ok: false, code: 6 });
|
|
161
185
|
return { ok: false, code: 6, phases };
|
|
162
186
|
}
|
|
163
187
|
|
|
@@ -195,6 +219,8 @@ export async function gate(opts = {}) {
|
|
|
195
219
|
flakyVerdictResult = flakyVerdict(runs);
|
|
196
220
|
if (flakyVerdictResult.verdict === "QUARANTINED") {
|
|
197
221
|
phases.push({ phase: "verify", ok: false, testResult, buildResult, flakyVerdict: flakyVerdictResult });
|
|
222
|
+
appendTelemetry(root, "gate_phase", { phase: "verify", ok: false, quarantined: true });
|
|
223
|
+
appendTelemetry(root, "gate_finished", { ok: false, code: 8 });
|
|
198
224
|
return { ok: false, code: 8, phases, flakyVerdict: flakyVerdictResult };
|
|
199
225
|
}
|
|
200
226
|
}
|
|
@@ -208,16 +234,25 @@ export async function gate(opts = {}) {
|
|
|
208
234
|
const verifyOk = testResult.ok && buildResult.ok;
|
|
209
235
|
const failingCmd = !testResult.ok ? testResult : !buildResult.ok ? buildResult : null;
|
|
210
236
|
phases.push({ phase: "verify", ok: verifyOk, testResult, buildResult });
|
|
237
|
+
appendTelemetry(root, "gate_phase", { phase: "verify", ok: verifyOk });
|
|
238
|
+
if (progressBus && progressToken) {
|
|
239
|
+
progressBus.reportProgress(progressToken, 100, 100, "Phase 4/4: Automated Test & Build Verification complete");
|
|
240
|
+
}
|
|
211
241
|
|
|
212
242
|
if (!verifyOk && opts.fix) {
|
|
213
|
-
const repairs = await repair(failingCmd, { config, root, signal: opts.signal });
|
|
243
|
+
const repairs = await repair(failingCmd, { config, root, signal: opts.signal, progressBus, progressToken });
|
|
244
|
+
const finalOk = repairs.ok;
|
|
245
|
+
const finalCode = repairs.ok ? 0 : 4;
|
|
246
|
+
appendTelemetry(root, "gate_finished", { ok: finalOk, code: finalCode, repaired: true });
|
|
214
247
|
if (repairs.ok) {
|
|
215
248
|
return { ok: true, code: 0, phases, repairs };
|
|
216
249
|
}
|
|
217
250
|
return { ok: false, code: 4, phases, repairs };
|
|
218
251
|
}
|
|
219
252
|
|
|
220
|
-
|
|
253
|
+
const code = verifyOk ? 0 : 4;
|
|
254
|
+
appendTelemetry(root, "gate_finished", { ok: verifyOk, code });
|
|
255
|
+
return { ok: verifyOk, code, phases };
|
|
221
256
|
}
|
|
222
257
|
|
|
223
258
|
/**
|
|
@@ -280,6 +315,8 @@ export async function repair(failure, opts = {}) {
|
|
|
280
315
|
const config = opts.config || loadConfig(root);
|
|
281
316
|
const maxRetries = config.limits.repairAttempts || 3;
|
|
282
317
|
const provider = opts.provider || createProvider(config.provider, config);
|
|
318
|
+
const progressBus = opts.progressBus;
|
|
319
|
+
const progressToken = opts.progressToken;
|
|
283
320
|
const attempts = [];
|
|
284
321
|
const breaker = opts.circuitBreaker || new OODACircuitBreaker({
|
|
285
322
|
windowSize: Math.max(maxRetries + 1, 6),
|
|
@@ -292,6 +329,10 @@ export async function repair(failure, opts = {}) {
|
|
|
292
329
|
breaker.observe(initialFingerprint);
|
|
293
330
|
|
|
294
331
|
for (let n = 1; n <= maxRetries; n++) {
|
|
332
|
+
if (progressBus && progressToken) {
|
|
333
|
+
progressBus.reportProgress(progressToken, Math.round((n / maxRetries) * 100), 100, `OODA Repair attempt ${n}/${maxRetries}`);
|
|
334
|
+
}
|
|
335
|
+
|
|
295
336
|
const repairPrompt = buildRepairPrompt(currentFailure, n, config);
|
|
296
337
|
let session;
|
|
297
338
|
try {
|
|
@@ -306,13 +347,15 @@ export async function repair(failure, opts = {}) {
|
|
|
306
347
|
);
|
|
307
348
|
} catch (err) {
|
|
308
349
|
attempts.push({ n, ok: false, error: err.message });
|
|
350
|
+
appendTelemetry(root, "ooda_repair_attempt", { attempt: n, ok: false, error: err.message });
|
|
309
351
|
break;
|
|
310
352
|
}
|
|
311
353
|
|
|
312
354
|
attempts.push({ n, session, ok: true });
|
|
355
|
+
appendTelemetry(root, "ooda_repair_attempt", { attempt: n, ok: true });
|
|
313
356
|
|
|
314
357
|
// Re-verify after repair attempt
|
|
315
|
-
const gateRes = await gate({ root, config, fix: false });
|
|
358
|
+
const gateRes = await gate({ root, config, fix: false, progressBus, progressToken });
|
|
316
359
|
if (gateRes.ok) {
|
|
317
360
|
breaker.reset();
|
|
318
361
|
return { ok: true, attempts, finalStatus: "PASSED" };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { writeMcpFrame } from "./mcp.mjs";
|
|
2
|
+
|
|
3
|
+
export const MAX_PROGRESS_MESSAGE_LENGTH = 240;
|
|
4
|
+
export const DEFAULT_PROGRESS_COALESCE_MS = 150;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* MCP Progress & Event Streaming Bus with 150ms window coalescing and backpressure safety.
|
|
8
|
+
*/
|
|
9
|
+
export class ProgressBus {
|
|
10
|
+
/**
|
|
11
|
+
* @param {import("node:stream").Writable} [output=process.stdout]
|
|
12
|
+
* @param {Object} [opts={}]
|
|
13
|
+
* @param {number} [opts.coalesceMs=150]
|
|
14
|
+
*/
|
|
15
|
+
constructor(output = process.stdout, opts = {}) {
|
|
16
|
+
this.output = output || process.stdout;
|
|
17
|
+
this.coalesceMs = opts.coalesceMs ?? DEFAULT_PROGRESS_COALESCE_MS;
|
|
18
|
+
this.pending = new Map(); // token -> { timer, latestPayload }
|
|
19
|
+
this.queue = [];
|
|
20
|
+
this.isWriting = false;
|
|
21
|
+
this.isDraining = false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Internal queue flusher with stream backpressure handling.
|
|
26
|
+
* @private
|
|
27
|
+
*/
|
|
28
|
+
async _flushQueue() {
|
|
29
|
+
this.isWriting = true;
|
|
30
|
+
while (this.queue.length > 0) {
|
|
31
|
+
if (this.isDraining) {
|
|
32
|
+
await new Promise((resolve) => {
|
|
33
|
+
if (!this.output || typeof this.output.once !== "function") {
|
|
34
|
+
return resolve();
|
|
35
|
+
}
|
|
36
|
+
this.output.once("drain", resolve);
|
|
37
|
+
});
|
|
38
|
+
this.isDraining = false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const frame = this.queue.shift();
|
|
42
|
+
const ok = writeMcpFrame(this.output, frame);
|
|
43
|
+
if (!ok) {
|
|
44
|
+
this.isDraining = true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
this.isWriting = false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Sends a framed JSON-RPC payload to output via authorized writeMcpFrame.
|
|
52
|
+
* @param {Object} payload
|
|
53
|
+
*/
|
|
54
|
+
async sendFrame(payload) {
|
|
55
|
+
const frameStr = JSON.stringify(payload) + "\n";
|
|
56
|
+
this.queue.push(frameStr);
|
|
57
|
+
if (!this.isWriting) {
|
|
58
|
+
await this._flushQueue();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Reports progress for an active progressToken.
|
|
64
|
+
* Coalesces rapid intermediate updates within a 150ms window (latest-wins).
|
|
65
|
+
*
|
|
66
|
+
* @param {string} progressToken
|
|
67
|
+
* @param {number} progress
|
|
68
|
+
* @param {number|null} [total=null]
|
|
69
|
+
* @param {string|null} [message=null]
|
|
70
|
+
*/
|
|
71
|
+
reportProgress(progressToken, progress, total = null, message = null) {
|
|
72
|
+
if (!progressToken) return;
|
|
73
|
+
|
|
74
|
+
let cappedMessage = undefined;
|
|
75
|
+
if (message !== null && message !== undefined) {
|
|
76
|
+
const msgStr = String(message);
|
|
77
|
+
cappedMessage =
|
|
78
|
+
msgStr.length > MAX_PROGRESS_MESSAGE_LENGTH
|
|
79
|
+
? msgStr.slice(0, MAX_PROGRESS_MESSAGE_LENGTH)
|
|
80
|
+
: msgStr;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const params = {
|
|
84
|
+
progressToken,
|
|
85
|
+
progress,
|
|
86
|
+
};
|
|
87
|
+
if (total !== null && total !== undefined) params.total = total;
|
|
88
|
+
if (cappedMessage !== undefined) params.message = cappedMessage;
|
|
89
|
+
|
|
90
|
+
const payload = {
|
|
91
|
+
jsonrpc: "2.0",
|
|
92
|
+
method: "notifications/progress",
|
|
93
|
+
params,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const isComplete = total !== null && total !== undefined && progress >= total;
|
|
97
|
+
const existing = this.pending.get(progressToken);
|
|
98
|
+
|
|
99
|
+
if (isComplete) {
|
|
100
|
+
if (existing?.timer) clearTimeout(existing.timer);
|
|
101
|
+
this.pending.delete(progressToken);
|
|
102
|
+
this.sendFrame(payload);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (!existing) {
|
|
107
|
+
// First update in window: send immediately
|
|
108
|
+
this.sendFrame(payload);
|
|
109
|
+
const timer = setTimeout(() => {
|
|
110
|
+
const current = this.pending.get(progressToken);
|
|
111
|
+
if (current?.latestPayload) {
|
|
112
|
+
this.sendFrame(current.latestPayload);
|
|
113
|
+
}
|
|
114
|
+
this.pending.delete(progressToken);
|
|
115
|
+
}, this.coalesceMs);
|
|
116
|
+
|
|
117
|
+
this.pending.set(progressToken, { timer, latestPayload: null });
|
|
118
|
+
} else {
|
|
119
|
+
// Intermediate update: save latest payload
|
|
120
|
+
existing.latestPayload = payload;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Emits an MCP log notification frame (notifications/message).
|
|
126
|
+
* Never calls raw console.log.
|
|
127
|
+
*
|
|
128
|
+
* @param {string} [level="info"] "info" | "warning" | "error" | "debug"
|
|
129
|
+
* @param {string|Object} [data=""]
|
|
130
|
+
* @param {string} [logger="jules-orchestrator"]
|
|
131
|
+
*/
|
|
132
|
+
log(level = "info", data = "", logger = "jules-orchestrator") {
|
|
133
|
+
const payload = {
|
|
134
|
+
jsonrpc: "2.0",
|
|
135
|
+
method: "notifications/message",
|
|
136
|
+
params: {
|
|
137
|
+
level,
|
|
138
|
+
logger,
|
|
139
|
+
data: typeof data === "string" ? data : JSON.stringify(data),
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
return this.sendFrame(payload);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Flushes all pending progress timers and ensures output queue is completely drained.
|
|
147
|
+
*/
|
|
148
|
+
async flush() {
|
|
149
|
+
for (const [token, item] of this.pending.entries()) {
|
|
150
|
+
if (item.timer) clearTimeout(item.timer);
|
|
151
|
+
if (item.latestPayload) {
|
|
152
|
+
this.sendFrame(item.latestPayload);
|
|
153
|
+
}
|
|
154
|
+
this.pending.delete(token);
|
|
155
|
+
}
|
|
156
|
+
await this._flushQueue();
|
|
157
|
+
}
|
|
158
|
+
}
|
package/src/mcp.mjs
CHANGED
|
@@ -4,10 +4,12 @@ import { gate, dispatch } from "./engine.mjs";
|
|
|
4
4
|
import { classifyRiskTier } from "./risk.mjs";
|
|
5
5
|
import { checkDailyBudget, lockStatus } from "./state.mjs";
|
|
6
6
|
import { reapOrphanedIntents, reapStaleMutexDirs } from "./journal.mjs";
|
|
7
|
+
import { readTelemetry } from "./telemetry.mjs";
|
|
8
|
+
import { ProgressBus } from "./mcp-progress.mjs";
|
|
7
9
|
|
|
8
10
|
export const MCP_SERVER_INFO = {
|
|
9
11
|
name: "jules-orchestrator-kit",
|
|
10
|
-
version: "0.
|
|
12
|
+
version: "0.23.0",
|
|
11
13
|
};
|
|
12
14
|
|
|
13
15
|
export const MAX_MCP_FRAME_SIZE = 4 * 1024 * 1024; // 4 MB memory safety ceiling
|
|
@@ -136,6 +138,16 @@ export const MCP_TOOLS = [
|
|
|
136
138
|
properties: {},
|
|
137
139
|
},
|
|
138
140
|
},
|
|
141
|
+
{
|
|
142
|
+
name: "telemetry_tail",
|
|
143
|
+
description: "Query the last N telemetry events from the orchestrator telemetry spine.",
|
|
144
|
+
inputSchema: {
|
|
145
|
+
type: "object",
|
|
146
|
+
properties: {
|
|
147
|
+
limit: { type: "number", description: "Number of telemetry events to retrieve (default 50, max 500)" },
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
},
|
|
139
151
|
];
|
|
140
152
|
|
|
141
153
|
export async function handleMcpRequest(request, opts = {}) {
|
|
@@ -260,6 +272,25 @@ export async function handleMcpRequest(request, opts = {}) {
|
|
|
260
272
|
};
|
|
261
273
|
}
|
|
262
274
|
|
|
275
|
+
if (toolName === "telemetry_tail") {
|
|
276
|
+
if (args.limit !== undefined && (typeof args.limit !== "number" || args.limit < 1)) {
|
|
277
|
+
return {
|
|
278
|
+
jsonrpc: "2.0",
|
|
279
|
+
id,
|
|
280
|
+
error: { code: -32602, message: "Invalid parameters: 'limit' must be a positive number" },
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
const limit = Math.min(args.limit || 50, 500);
|
|
284
|
+
const events = readTelemetry(root, limit);
|
|
285
|
+
return {
|
|
286
|
+
jsonrpc: "2.0",
|
|
287
|
+
id,
|
|
288
|
+
result: {
|
|
289
|
+
content: [{ type: "text", text: JSON.stringify(events, null, 2) }],
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
263
294
|
return {
|
|
264
295
|
jsonrpc: "2.0",
|
|
265
296
|
id,
|
|
@@ -361,11 +392,14 @@ export function startMcpServer(input = process.stdin, output = process.stdout, o
|
|
|
361
392
|
isolateMcpStdout(output, opts);
|
|
362
393
|
}
|
|
363
394
|
|
|
395
|
+
const progressBus = opts.progressBus || new ProgressBus(output, opts);
|
|
364
396
|
const decoder = new McpFrameDecoder({ maxFrameSize: opts.maxFrameSize || MAX_MCP_FRAME_SIZE });
|
|
365
397
|
|
|
366
398
|
decoder.on("data", async (request) => {
|
|
367
399
|
try {
|
|
368
|
-
const
|
|
400
|
+
const progressToken = request?.params?._meta?.progressToken || request?.params?.progressToken;
|
|
401
|
+
const requestOpts = { ...opts, progressBus, progressToken };
|
|
402
|
+
const response = await handleMcpRequest(request, requestOpts);
|
|
369
403
|
if (response !== null && response !== undefined) {
|
|
370
404
|
writeMcpFrame(output, JSON.stringify(response) + "\n");
|
|
371
405
|
}
|
package/src/security.mjs
CHANGED
|
@@ -64,7 +64,9 @@ export function safeAtomicWrite(filePath, content, options = {}) {
|
|
|
64
64
|
const fd = openSync(tempFile, "wx", mode);
|
|
65
65
|
try {
|
|
66
66
|
writeSync(fd, content, null, encoding);
|
|
67
|
-
|
|
67
|
+
if (options.sync !== false) {
|
|
68
|
+
fsyncSync(fd);
|
|
69
|
+
}
|
|
68
70
|
} finally {
|
|
69
71
|
closeSync(fd);
|
|
70
72
|
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import {
|
|
2
|
+
openSync,
|
|
3
|
+
writeSync,
|
|
4
|
+
closeSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
existsSync,
|
|
7
|
+
statSync,
|
|
8
|
+
readdirSync,
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { resolveRoot } from "./config.mjs";
|
|
13
|
+
import { getStateDir, withVfsMutex } from "./state.mjs";
|
|
14
|
+
import { safeAtomicWrite } from "./security.mjs";
|
|
15
|
+
|
|
16
|
+
export const MAX_TELEMETRY_SEGMENT_BYTES = 8 * 1024 * 1024; // 8 MB log segment ceiling
|
|
17
|
+
export const TELEMETRY_GENESIS_HASH = "0".repeat(64);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Returns telemetry file paths for a given date string sorted by segment index.
|
|
21
|
+
*/
|
|
22
|
+
function getTelemetrySegmentFiles(stateDir, dateStr) {
|
|
23
|
+
if (!existsSync(stateDir)) return [];
|
|
24
|
+
const prefix = `telemetry-${dateStr}`;
|
|
25
|
+
const files = readdirSync(stateDir).filter(
|
|
26
|
+
(f) => f.startsWith(prefix) && f.endsWith(".jsonl")
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
return files
|
|
30
|
+
.map((name) => {
|
|
31
|
+
let index = 0;
|
|
32
|
+
if (name !== `${prefix}.jsonl`) {
|
|
33
|
+
const match = name.match(/telemetry-[0-9]{4}-[0-9]{2}-[0-9]{2}-([0-9]+)\.jsonl$/);
|
|
34
|
+
if (match) index = parseInt(match[1], 10);
|
|
35
|
+
}
|
|
36
|
+
return { name, path: join(stateDir, name), index };
|
|
37
|
+
})
|
|
38
|
+
.sort((a, b) => a.index - b.index);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Performs a cold scan of telemetry jsonl files to recover the last valid hash and active segment index.
|
|
43
|
+
*/
|
|
44
|
+
function coldScanTelemetry(stateDir, dateStr) {
|
|
45
|
+
const segments = getTelemetrySegmentFiles(stateDir, dateStr);
|
|
46
|
+
if (segments.length === 0) {
|
|
47
|
+
return { hash: TELEMETRY_GENESIS_HASH, segment: 0 };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
51
|
+
const seg = segments[i];
|
|
52
|
+
if (existsSync(seg.path)) {
|
|
53
|
+
try {
|
|
54
|
+
const content = readFileSync(seg.path, "utf-8");
|
|
55
|
+
const lines = content.split("\n").filter(Boolean);
|
|
56
|
+
for (let j = lines.length - 1; j >= 0; j--) {
|
|
57
|
+
try {
|
|
58
|
+
const entry = JSON.parse(lines[j]);
|
|
59
|
+
if (entry && typeof entry.hash === "string") {
|
|
60
|
+
return { hash: entry.hash, segment: seg.index };
|
|
61
|
+
}
|
|
62
|
+
} catch (_) {}
|
|
63
|
+
}
|
|
64
|
+
} catch (_) {}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { hash: TELEMETRY_GENESIS_HASH, segment: 0 };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Appends a structured telemetry event to .agent/state/telemetry-<date>.jsonl with SHA-256 hash chaining.
|
|
73
|
+
* Uses .head atomic cache for O(1) constant-time appends.
|
|
74
|
+
*
|
|
75
|
+
* @param {string} [rootOrOpts]
|
|
76
|
+
* @param {string} kind
|
|
77
|
+
* @param {Object} [fields={}]
|
|
78
|
+
* @returns {Object} Appended telemetry record
|
|
79
|
+
*/
|
|
80
|
+
export function appendTelemetry(rootOrOpts = resolveRoot(), kind = "event", fields = {}) {
|
|
81
|
+
const root = typeof rootOrOpts === "string" ? rootOrOpts : resolveRoot();
|
|
82
|
+
const stateDir = getStateDir(root);
|
|
83
|
+
const mutexDir = join(stateDir, ".telemetry.mutex");
|
|
84
|
+
|
|
85
|
+
return withVfsMutex(mutexDir, () => {
|
|
86
|
+
const dateStr = new Date().toISOString().split("T")[0];
|
|
87
|
+
const headPath = join(stateDir, `telemetry-${dateStr}.head`);
|
|
88
|
+
|
|
89
|
+
let prevHash = TELEMETRY_GENESIS_HASH;
|
|
90
|
+
let activeSegmentIndex = 0;
|
|
91
|
+
let headValid = false;
|
|
92
|
+
|
|
93
|
+
if (existsSync(headPath)) {
|
|
94
|
+
try {
|
|
95
|
+
const headContent = readFileSync(headPath, "utf-8");
|
|
96
|
+
const headObj = JSON.parse(headContent);
|
|
97
|
+
if (headObj && typeof headObj.hash === "string") {
|
|
98
|
+
prevHash = headObj.hash;
|
|
99
|
+
activeSegmentIndex = typeof headObj.segment === "number" ? headObj.segment : 0;
|
|
100
|
+
headValid = true;
|
|
101
|
+
}
|
|
102
|
+
} catch (_) {}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!headValid) {
|
|
106
|
+
const recovered = coldScanTelemetry(stateDir, dateStr);
|
|
107
|
+
prevHash = recovered.hash;
|
|
108
|
+
activeSegmentIndex = recovered.segment;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let activeFileName =
|
|
112
|
+
activeSegmentIndex === 0
|
|
113
|
+
? `telemetry-${dateStr}.jsonl`
|
|
114
|
+
: `telemetry-${dateStr}-${activeSegmentIndex}.jsonl`;
|
|
115
|
+
let activeFilePath = join(stateDir, activeFileName);
|
|
116
|
+
|
|
117
|
+
if (existsSync(activeFilePath)) {
|
|
118
|
+
try {
|
|
119
|
+
const stat = statSync(activeFilePath);
|
|
120
|
+
if (stat.size >= MAX_TELEMETRY_SEGMENT_BYTES) {
|
|
121
|
+
activeSegmentIndex += 1;
|
|
122
|
+
activeFileName = `telemetry-${dateStr}-${activeSegmentIndex}.jsonl`;
|
|
123
|
+
activeFilePath = join(stateDir, activeFileName);
|
|
124
|
+
}
|
|
125
|
+
} catch (_) {}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const ts = new Date().toISOString();
|
|
129
|
+
const payloadWithoutHash = { v: 1, ts, kind, ...fields, prevHash };
|
|
130
|
+
const hash = createHash("sha256").update(JSON.stringify(payloadWithoutHash)).digest("hex");
|
|
131
|
+
const entry = { ...payloadWithoutHash, hash };
|
|
132
|
+
|
|
133
|
+
const fd = openSync(activeFilePath, "a");
|
|
134
|
+
try {
|
|
135
|
+
writeSync(fd, JSON.stringify(entry) + "\n", "utf-8");
|
|
136
|
+
} finally {
|
|
137
|
+
closeSync(fd);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
safeAtomicWrite(
|
|
141
|
+
headPath,
|
|
142
|
+
JSON.stringify({ v: 1, hash, segment: activeSegmentIndex, ts }),
|
|
143
|
+
{ sync: false }
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
return entry;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Reads telemetry records across segments for a target date (or current date if omitted).
|
|
152
|
+
* Returns array of telemetry objects in chronological order.
|
|
153
|
+
*
|
|
154
|
+
* @param {string} [rootOrOpts]
|
|
155
|
+
* @param {number} [limit=50]
|
|
156
|
+
* @param {string} [targetDateStr]
|
|
157
|
+
* @returns {Array<Object>}
|
|
158
|
+
*/
|
|
159
|
+
export function readTelemetry(rootOrOpts = resolveRoot(), limit = 50, targetDateStr = null) {
|
|
160
|
+
const root = typeof rootOrOpts === "string" ? rootOrOpts : resolveRoot();
|
|
161
|
+
const stateDir = getStateDir(root);
|
|
162
|
+
const dateStr = targetDateStr || new Date().toISOString().split("T")[0];
|
|
163
|
+
|
|
164
|
+
const segments = getTelemetrySegmentFiles(stateDir, dateStr);
|
|
165
|
+
if (segments.length === 0) return [];
|
|
166
|
+
|
|
167
|
+
const allEntries = [];
|
|
168
|
+
for (const seg of segments) {
|
|
169
|
+
if (existsSync(seg.path)) {
|
|
170
|
+
try {
|
|
171
|
+
const raw = readFileSync(seg.path, "utf-8");
|
|
172
|
+
const lines = raw.split("\n").filter(Boolean);
|
|
173
|
+
for (const line of lines) {
|
|
174
|
+
try {
|
|
175
|
+
const entry = JSON.parse(line);
|
|
176
|
+
allEntries.push(Object.freeze(entry));
|
|
177
|
+
} catch (_) {}
|
|
178
|
+
}
|
|
179
|
+
} catch (_) {}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (allEntries.length <= limit) {
|
|
184
|
+
return allEntries;
|
|
185
|
+
}
|
|
186
|
+
return allEntries.slice(allEntries.length - limit);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Verifies the cryptographic SHA-256 hash-chain integrity of telemetry records for a date.
|
|
191
|
+
*
|
|
192
|
+
* @param {string} [rootOrOpts]
|
|
193
|
+
* @param {string} [targetDateStr]
|
|
194
|
+
* @returns {Object}
|
|
195
|
+
*/
|
|
196
|
+
export function verifyTelemetryIntegrity(rootOrOpts = resolveRoot(), targetDateStr = null) {
|
|
197
|
+
const root = typeof rootOrOpts === "string" ? rootOrOpts : resolveRoot();
|
|
198
|
+
const stateDir = getStateDir(root);
|
|
199
|
+
const dateStr = targetDateStr || new Date().toISOString().split("T")[0];
|
|
200
|
+
|
|
201
|
+
const segments = getTelemetrySegmentFiles(stateDir, dateStr);
|
|
202
|
+
if (segments.length === 0) {
|
|
203
|
+
return { ok: true, count: 0, lastHash: TELEMETRY_GENESIS_HASH };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let expectedPrevHash = TELEMETRY_GENESIS_HASH;
|
|
207
|
+
let totalCount = 0;
|
|
208
|
+
|
|
209
|
+
for (const seg of segments) {
|
|
210
|
+
if (!existsSync(seg.path)) continue;
|
|
211
|
+
try {
|
|
212
|
+
const raw = readFileSync(seg.path, "utf-8");
|
|
213
|
+
const lines = raw.split("\n").filter(Boolean);
|
|
214
|
+
for (let i = 0; i < lines.length; i++) {
|
|
215
|
+
totalCount++;
|
|
216
|
+
let obj;
|
|
217
|
+
try {
|
|
218
|
+
obj = JSON.parse(lines[i]);
|
|
219
|
+
} catch (err) {
|
|
220
|
+
return { ok: false, segment: seg.name, line: i + 1, error: "TORN_WRITE_CORRUPTION", detail: err.message };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (!obj.hash || !obj.prevHash) {
|
|
224
|
+
return { ok: false, segment: seg.name, line: i + 1, error: "MISSING_HASH_FIELDS" };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (obj.prevHash !== expectedPrevHash) {
|
|
228
|
+
return {
|
|
229
|
+
ok: false,
|
|
230
|
+
segment: seg.name,
|
|
231
|
+
line: i + 1,
|
|
232
|
+
error: "BROKEN_PREV_HASH",
|
|
233
|
+
expected: expectedPrevHash,
|
|
234
|
+
actual: obj.prevHash,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const { hash, ...rest } = obj;
|
|
239
|
+
const recomputed = createHash("sha256").update(JSON.stringify(rest)).digest("hex");
|
|
240
|
+
if (recomputed !== hash) {
|
|
241
|
+
return {
|
|
242
|
+
ok: false,
|
|
243
|
+
segment: seg.name,
|
|
244
|
+
line: i + 1,
|
|
245
|
+
error: "CORRUPTED_ENTRY_HASH",
|
|
246
|
+
expected: recomputed,
|
|
247
|
+
actual: hash,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
expectedPrevHash = hash;
|
|
251
|
+
}
|
|
252
|
+
} catch (err) {
|
|
253
|
+
return { ok: false, count: totalCount, error: err.message };
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const headPath = join(stateDir, `telemetry-${dateStr}.head`);
|
|
258
|
+
if (existsSync(headPath)) {
|
|
259
|
+
try {
|
|
260
|
+
const headObj = JSON.parse(readFileSync(headPath, "utf-8"));
|
|
261
|
+
if (headObj.hash !== expectedPrevHash) {
|
|
262
|
+
return {
|
|
263
|
+
ok: false,
|
|
264
|
+
error: "HEAD_HASH_MISMATCH",
|
|
265
|
+
expected: expectedPrevHash,
|
|
266
|
+
actual: headObj.hash,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
} catch (_) {
|
|
270
|
+
return { ok: false, error: "CORRUPTED_HEAD_FILE" };
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return { ok: true, count: totalCount, lastHash: expectedPrevHash };
|
|
275
|
+
}
|