akemon 0.2.24 → 0.2.25

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.
@@ -13,6 +13,9 @@ import { readFile, writeFile, mkdir } from "fs/promises";
13
13
  import { join, dirname, isAbsolute } from "path";
14
14
  import { callAgent } from "./relay-client.js";
15
15
  import { SIG, sig } from "./types.js";
16
+ import { updateMetrics, pushExecMs } from "./metrics.js";
17
+ import { sendFailureEvent } from "./relay-client.js";
18
+ import { resolveEngineConfig, } from "./engine-routing.js";
16
19
  export const LLM_ENGINES = new Set(["claude", "codex", "opencode", "gemini", "raw"]);
17
20
  // ---------------------------------------------------------------------------
18
21
  // EnginePeripheral
@@ -26,6 +29,27 @@ export class EnginePeripheral {
26
29
  bus = null;
27
30
  /** Last execution trace (for error reporting) */
28
31
  lastTrace = [];
32
+ /** Active CLI child processes — tracked so SIGTERM handler can kill them. */
33
+ activeChildren = new Set();
34
+ /**
35
+ * Send SIGKILL to all active child process groups. Called during daemon shutdown.
36
+ *
37
+ * NOTE: sends SIGKILL directly (no SIGTERM grace) — safe for stateless
38
+ * request/response CLIs. Must change to SIGTERM+3s+SIGKILL when Batch 5.1
39
+ * persistent-session mode lands (sessions need graceful teardown).
40
+ */
41
+ killAllChildren() {
42
+ for (const child of this.activeChildren) {
43
+ if (!child.pid)
44
+ continue;
45
+ console.log(`[engine] shutdown: killing pgid=-${child.pid}`);
46
+ try {
47
+ process.kill(-child.pid, "SIGKILL");
48
+ }
49
+ catch { }
50
+ }
51
+ this.activeChildren.clear();
52
+ }
29
53
  constructor(config) {
30
54
  this.config = config;
31
55
  this.id = `engine:${config.engine}`;
@@ -69,22 +93,32 @@ export class EnginePeripheral {
69
93
  // ---------------------------------------------------------------------------
70
94
  // Unified engine runner
71
95
  // ---------------------------------------------------------------------------
72
- async runEngine(task, allowAll, extraAllowedTools, signal) {
73
- const { engine, model, workdir } = this.config;
74
- if (engine === "raw") {
75
- return this.runRawEngine(task);
96
+ async runEngine(task, allowAll, extraAllowedTools, signal, origin, routing) {
97
+ const entry = resolveEngineConfig(routing, origin);
98
+ const cfg = entry ? applyRoutingEntry(this.config, entry) : this.config;
99
+ if (origin && entry) {
100
+ console.log(`[engine] using ${cfg.engine}${cfg.model ? `/${cfg.model}` : ""} (origin=${origin})`);
101
+ }
102
+ const t0 = Date.now();
103
+ try {
104
+ if (cfg.engine === "raw") {
105
+ return await this.runRawEngine(task, cfg);
106
+ }
107
+ const cmd = buildEngineCommand(cfg.engine, cfg.model, allowAll ?? cfg.allowAll, extraAllowedTools);
108
+ return await runCommand(cmd.cmd, cmd.args, task, cfg.workdir, cmd.stdinMode, signal, this.activeChildren);
109
+ }
110
+ finally {
111
+ pushExecMs(Date.now() - t0);
76
112
  }
77
- const cmd = buildEngineCommand(engine, model, allowAll ?? this.config.allowAll, extraAllowedTools);
78
- return runCommand(cmd.cmd, cmd.args, task, workdir, cmd.stdinMode, signal);
79
113
  }
80
114
  // ---------------------------------------------------------------------------
81
115
  // Raw engine: OpenAI-compatible API with tool call loop
82
116
  // ---------------------------------------------------------------------------
83
- async runRawEngine(task) {
84
- const apiUrl = (this.config.rawApiUrl || "http://localhost:11434/v1") + "/chat/completions";
85
- const modelName = this.config.model || "gemma4:4b";
86
- const maxRounds = this.config.rawMaxRounds || 20;
87
- const apiKey = this.config.rawApiKey || "";
117
+ async runRawEngine(task, cfg = this.config) {
118
+ const apiUrl = (cfg.rawApiUrl || "http://localhost:11434/v1") + "/chat/completions";
119
+ const modelName = cfg.model || "gemma4:4b";
120
+ const maxRounds = cfg.rawMaxRounds || 20;
121
+ const apiKey = cfg.rawApiKey || "";
88
122
  console.log(`[raw] Task:\n${task}`);
89
123
  const trace = [];
90
124
  this.lastTrace = trace;
@@ -332,6 +366,25 @@ export const RAW_TOOLS = [
332
366
  // ---------------------------------------------------------------------------
333
367
  // CLI engine helpers (shared, non-class)
334
368
  // ---------------------------------------------------------------------------
369
+ /**
370
+ * Build a local EngineConfig copy that merges in a routing entry's overrides.
371
+ * Resolves rawApiKeyEnv → rawApiKey from environment at call time.
372
+ * Never mutates the base config.
373
+ */
374
+ function applyRoutingEntry(base, entry) {
375
+ const override = { engine: entry.engine };
376
+ if (entry.model !== undefined)
377
+ override.model = entry.model ?? undefined;
378
+ if (entry.rawApiUrl !== undefined)
379
+ override.rawApiUrl = entry.rawApiUrl;
380
+ if (entry.rawMaxRounds !== undefined)
381
+ override.rawMaxRounds = entry.rawMaxRounds;
382
+ if (entry.allowAll !== undefined)
383
+ override.allowAll = entry.allowAll;
384
+ if (entry.rawApiKeyEnv)
385
+ override.rawApiKey = process.env[entry.rawApiKeyEnv] ?? "";
386
+ return { ...base, ...override };
387
+ }
335
388
  function buildEngineCommand(engine, model, allowAll, extraAllowedTools) {
336
389
  switch (engine) {
337
390
  case "claude": {
@@ -364,7 +417,7 @@ function buildEngineCommand(engine, model, allowAll, extraAllowedTools) {
364
417
  return { cmd: engine, args: [], stdinMode: true };
365
418
  }
366
419
  }
367
- function runCommand(cmd, args, task, cwd, stdinMode = true, signal) {
420
+ function runCommand(cmd, args, task, cwd, stdinMode = true, signal, activeChildren) {
368
421
  return new Promise((resolve, reject) => {
369
422
  const { CLAUDECODE, ...cleanEnv } = process.env;
370
423
  const finalArgs = stdinMode ? args : [...args, task];
@@ -373,23 +426,32 @@ function runCommand(cmd, args, task, cwd, stdinMode = true, signal) {
373
426
  cwd,
374
427
  env: cleanEnv,
375
428
  stdio: [stdinMode ? "pipe" : "ignore", "pipe", "pipe"],
429
+ detached: true, // child becomes process-group leader; enables pgid kill
376
430
  });
377
- // Abort → SIGTERM, then SIGKILL after a grace period so a hung engine can't
378
- // hold the slot past the caller's deadline.
431
+ if (activeChildren) {
432
+ activeChildren.add(child);
433
+ updateMetrics({ engine_children_active: activeChildren.size });
434
+ }
435
+ // Abort → SIGTERM to process group, then SIGKILL after grace period.
436
+ // Using -pid (negative) sends the signal to the entire process group,
437
+ // so any sub-forks spawned by the CLI are also terminated.
379
438
  let aborted = false;
380
439
  const onAbort = () => {
381
- if (aborted)
440
+ if (aborted || !child.pid)
382
441
  return;
383
442
  aborted = true;
384
- console.log(`[${cmd}] aborted, killing pid=${child.pid}`);
443
+ console.log(`[${cmd}] aborted, killing pgid=-${child.pid}`);
444
+ sendFailureEvent("engine_abort", cmd, "engine subprocess aborted via signal");
385
445
  try {
386
- child.kill("SIGTERM");
446
+ process.kill(-child.pid, "SIGTERM");
387
447
  }
388
448
  catch { }
389
- setTimeout(() => { try {
390
- child.kill("SIGKILL");
391
- }
392
- catch { } }, 3000).unref();
449
+ setTimeout(() => {
450
+ try {
451
+ process.kill(-child.pid, "SIGKILL");
452
+ }
453
+ catch { }
454
+ }, 3000).unref();
393
455
  };
394
456
  if (signal) {
395
457
  if (signal.aborted)
@@ -412,6 +474,11 @@ function runCommand(cmd, args, task, cwd, stdinMode = true, signal) {
412
474
  });
413
475
  child.on("close", (code, killSignal) => {
414
476
  signal?.removeEventListener("abort", onAbort);
477
+ if (activeChildren) {
478
+ activeChildren.delete(child);
479
+ updateMetrics({ engine_children_active: activeChildren.size });
480
+ }
481
+ child.unref();
415
482
  console.log(`[${cmd}] exit=${code}${killSignal ? ` signal=${killSignal}` : ""} stdout=${stdout.length}b stderr=${stderr.length}b`);
416
483
  if (stderr)
417
484
  console.log(`[${cmd}] stderr:\n${stderr}`);
@@ -431,6 +498,11 @@ function runCommand(cmd, args, task, cwd, stdinMode = true, signal) {
431
498
  });
432
499
  child.on("error", (err) => {
433
500
  signal?.removeEventListener("abort", onAbort);
501
+ if (activeChildren) {
502
+ activeChildren.delete(child);
503
+ updateMetrics({ engine_children_active: activeChildren.size });
504
+ }
505
+ child.unref();
434
506
  reject(err);
435
507
  });
436
508
  });
@@ -19,11 +19,55 @@
19
19
  * - low — background enrichment (platform tasks, script activities,
20
20
  * long-term, identity compression)
21
21
  */
22
+ import { updateMetrics } from "./metrics.js";
22
23
  const PRIORITY_RANK = { high: 3, normal: 2, low: 1 };
24
+ /** Max simultaneous user_manual tasks allowed to hold or wait for a slot.
25
+ * Prevents more than this many claude CLI processes from queuing up. */
26
+ const DEFAULT_MAX_USER_MANUAL = 2;
23
27
  export class EngineQueue {
24
28
  busy = false;
25
29
  busySince = 0;
26
30
  waiters = [];
31
+ // User-manual concurrency gate
32
+ maxUserManualSlots;
33
+ userManualActive = 0;
34
+ userManualQueue = [];
35
+ constructor(maxUserManualSlots = DEFAULT_MAX_USER_MANUAL) {
36
+ this.maxUserManualSlots = maxUserManualSlots;
37
+ }
38
+ /** Acquire a user_manual slot before joining the engine queue.
39
+ * Callers MUST call releaseUserManualSlot() in a finally block. */
40
+ acquireUserManualSlot(deadlineMs) {
41
+ if (this.userManualActive < this.maxUserManualSlots) {
42
+ this.userManualActive++;
43
+ return Promise.resolve();
44
+ }
45
+ return new Promise((resolve, reject) => {
46
+ let timerRef;
47
+ const entry = {
48
+ resolve: () => { clearTimeout(timerRef); resolve(); },
49
+ reject: (err) => { clearTimeout(timerRef); reject(err); },
50
+ };
51
+ timerRef = setTimeout(() => {
52
+ const idx = this.userManualQueue.indexOf(entry);
53
+ if (idx >= 0)
54
+ this.userManualQueue.splice(idx, 1);
55
+ entry.reject(new Error("User manual slot timeout"));
56
+ }, deadlineMs);
57
+ this.userManualQueue.push(entry);
58
+ });
59
+ }
60
+ /** Release a user_manual slot and wake the next waiter. */
61
+ releaseUserManualSlot() {
62
+ const next = this.userManualQueue.shift();
63
+ if (next) {
64
+ // Transfer slot to the next waiter (active count unchanged)
65
+ next.resolve();
66
+ }
67
+ else {
68
+ this.userManualActive = Math.max(0, this.userManualActive - 1);
69
+ }
70
+ }
27
71
  /** Wait up to `deadlineMs` for the slot, then take it. */
28
72
  acquire(priority, deadlineMs) {
29
73
  if (!this.busy) {
@@ -39,12 +83,15 @@ export class EngineQueue {
39
83
  reject,
40
84
  timer: setTimeout(() => {
41
85
  const idx = this.waiters.indexOf(waiter);
42
- if (idx >= 0)
86
+ if (idx >= 0) {
43
87
  this.waiters.splice(idx, 1);
88
+ updateMetrics({ engine_queue_depth: this.waiters.length });
89
+ }
44
90
  reject(new Error(`Engine busy timeout (${Math.round(deadlineMs / 60000)} min)`));
45
91
  }, deadlineMs),
46
92
  };
47
93
  this.waiters.push(waiter);
94
+ updateMetrics({ engine_queue_depth: this.waiters.length });
48
95
  });
49
96
  }
50
97
  /** Release the slot and hand it to the best waiter, if any. */
@@ -58,6 +105,7 @@ export class EngineQueue {
58
105
  this.waiters.splice(this.waiters.indexOf(next), 1);
59
106
  clearTimeout(next.timer);
60
107
  this.busySince = Date.now();
108
+ updateMetrics({ engine_queue_depth: this.waiters.length });
61
109
  next.resolve();
62
110
  }
63
111
  /** Take the slot synchronously (used by MCP fast-path when !isBusy). */
@@ -0,0 +1,99 @@
1
+ import { describe, it } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { EngineQueue } from "./engine-queue.js";
4
+ // Helpers
5
+ const tick = () => new Promise((r) => setImmediate(r));
6
+ async function sleep(ms) {
7
+ return new Promise((r) => setTimeout(r, ms));
8
+ }
9
+ describe("EngineQueue", () => {
10
+ it("free slot: acquire resolves immediately and isBusy becomes true", async () => {
11
+ const q = new EngineQueue();
12
+ assert.equal(q.isBusy(), false);
13
+ await q.acquire("high", 1000);
14
+ assert.equal(q.isBusy(), true);
15
+ q.release();
16
+ assert.equal(q.isBusy(), false);
17
+ });
18
+ it("tryAcquire: succeeds when free, returns false when busy", () => {
19
+ const q = new EngineQueue();
20
+ assert.equal(q.tryAcquire(), true);
21
+ assert.equal(q.isBusy(), true);
22
+ assert.equal(q.tryAcquire(), false);
23
+ q.release();
24
+ });
25
+ it("priority ordering: high waiter beats normal when slot is released", async () => {
26
+ const q = new EngineQueue();
27
+ await q.acquire("high", 1000); // take the slot
28
+ const order = [];
29
+ const p1 = q.acquire("normal", 2000).then(() => { order.push("normal"); q.release(); });
30
+ await tick();
31
+ const p2 = q.acquire("high", 2000).then(() => { order.push("high"); q.release(); });
32
+ await tick();
33
+ assert.equal(q.queueDepth(), 2);
34
+ q.release(); // hand off to highest-priority waiter
35
+ await Promise.all([p1, p2]);
36
+ assert.deepEqual(order, ["high", "normal"]);
37
+ });
38
+ it("FIFO within same priority: earlier enqueuer wins", async () => {
39
+ const q = new EngineQueue();
40
+ await q.acquire("high", 1000);
41
+ const order = [];
42
+ const p1 = q.acquire("normal", 2000).then(() => { order.push("first"); q.release(); });
43
+ await sleep(5); // ensure different enqueuedAt timestamps
44
+ const p2 = q.acquire("normal", 2000).then(() => { order.push("second"); q.release(); });
45
+ await tick();
46
+ q.release();
47
+ await Promise.all([p1, p2]);
48
+ assert.deepEqual(order, ["first", "second"]);
49
+ });
50
+ it("deadline timeout: waiter is removed and rejects with busy-timeout error", async () => {
51
+ const q = new EngineQueue();
52
+ await q.acquire("high", 1000); // hold the slot
53
+ let caught = null;
54
+ const p = q.acquire("low", 30).catch((e) => { caught = e; });
55
+ await sleep(60); // let the 30ms deadline fire
56
+ assert.equal(q.queueDepth(), 0, "waiter must be removed after timeout");
57
+ await p;
58
+ assert.ok(caught !== null && typeof caught === "object", "should have rejected with an Error");
59
+ const msg = caught.message;
60
+ assert.ok(msg.includes("Engine busy timeout"), msg);
61
+ q.release();
62
+ });
63
+ it("release with no waiters makes slot free", () => {
64
+ const q = new EngineQueue();
65
+ assert.equal(q.tryAcquire(), true);
66
+ q.release();
67
+ assert.equal(q.isBusy(), false);
68
+ assert.equal(q.heldMs(), 0);
69
+ });
70
+ it("queueDepth tracks waiters correctly", async () => {
71
+ const q = new EngineQueue();
72
+ await q.acquire("high", 1000);
73
+ assert.equal(q.queueDepth(), 0);
74
+ const p1 = q.acquire("normal", 2000);
75
+ await tick();
76
+ assert.equal(q.queueDepth(), 1);
77
+ const p2 = q.acquire("low", 2000);
78
+ await tick();
79
+ assert.equal(q.queueDepth(), 2);
80
+ q.release(); // hand to normal (higher priority)
81
+ await tick();
82
+ assert.equal(q.queueDepth(), 1);
83
+ const holder = await p1; // p1 resolved — release it
84
+ void holder; // suppress unused warning
85
+ q.release();
86
+ await p2;
87
+ q.release();
88
+ assert.equal(q.queueDepth(), 0);
89
+ });
90
+ it("heldMs: returns 0 when free, positive when busy", async () => {
91
+ const q = new EngineQueue();
92
+ assert.equal(q.heldMs(), 0);
93
+ await q.acquire("high", 1000);
94
+ await sleep(10);
95
+ assert.ok(q.heldMs() >= 10, `heldMs should be >= 10, got ${q.heldMs()}`);
96
+ q.release();
97
+ assert.equal(q.heldMs(), 0);
98
+ });
99
+ });
@@ -0,0 +1,52 @@
1
+ /**
2
+ * engine-routing.ts — pure helpers for origin-based engine selection.
3
+ *
4
+ * Three exported pure functions (each independently unit-tested):
5
+ * resolveEngineConfig — picks which engine/model to use for a given origin
6
+ * deriveChildOrigin — returns the origin a child/sub-task should carry
7
+ * downgradeForRetry — downgrades any origin to "retry" when a task retries
8
+ */
9
+ /**
10
+ * Resolve which engine routing entry to use for a given origin.
11
+ *
12
+ * Lookup order:
13
+ * 1. routing[origin] (exact match)
14
+ * 2. routing.default (fallback)
15
+ * 3. null (no routing configured → caller uses base engine config)
16
+ *
17
+ * Backward-compatible: if routing is undefined/null, returns null, meaning the
18
+ * caller should use whatever engine is already in the base EngineConfig.
19
+ */
20
+ export function resolveEngineConfig(routing, origin) {
21
+ if (!routing)
22
+ return null;
23
+ if (origin) {
24
+ const exact = routing[origin];
25
+ if (exact)
26
+ return exact;
27
+ }
28
+ return routing.default ?? null;
29
+ }
30
+ /**
31
+ * Derive the origin that a child task should carry.
32
+ *
33
+ * "Human contamination" rule: human intent does NOT cross agent boundaries.
34
+ * Regardless of what the parent's origin is, any task spawned for/from another
35
+ * agent is always "platform" on the receiving side.
36
+ *
37
+ * Example: user_manual order → agent A calls agent B via MCP →
38
+ * agent B's resulting order has origin "platform", not "user_manual".
39
+ */
40
+ export function deriveChildOrigin(_parentOrigin) {
41
+ return "platform";
42
+ }
43
+ /**
44
+ * Downgrade the origin when a task enters the retry path.
45
+ *
46
+ * Retries must not consume the subscription CLI budget even if the original
47
+ * task was user_manual. Downgrading to "retry" lets the routing table send
48
+ * them to a cheaper API engine.
49
+ */
50
+ export function downgradeForRetry(_origin) {
51
+ return "retry";
52
+ }
@@ -0,0 +1,122 @@
1
+ import assert from "node:assert/strict";
2
+ import { describe, it } from "node:test";
3
+ import { resolveEngineConfig, deriveChildOrigin, downgradeForRetry, } from "./engine-routing.js";
4
+ // ---------------------------------------------------------------------------
5
+ // resolveEngineConfig
6
+ // ---------------------------------------------------------------------------
7
+ describe("resolveEngineConfig", () => {
8
+ const claudeEntry = { engine: "claude", model: "claude-opus-4-5" };
9
+ const rawEntry = { engine: "raw", rawApiUrl: "https://api.deepseek.com/v1", model: "deepseek-chat", rawApiKeyEnv: "DEEPSEEK_API_KEY" };
10
+ const defaultEntry = { engine: "raw", rawApiUrl: "https://api.anthropic.com/v1", model: "claude-haiku-4-5", rawApiKeyEnv: "ANTHROPIC_API_KEY" };
11
+ it("returns exact origin entry when routing has that origin", () => {
12
+ const routing = {
13
+ user_manual: claudeEntry,
14
+ platform: rawEntry,
15
+ default: defaultEntry,
16
+ };
17
+ const result = resolveEngineConfig(routing, "user_manual");
18
+ assert.deepEqual(result, claudeEntry);
19
+ });
20
+ it("returns platform entry for platform origin", () => {
21
+ const routing = {
22
+ user_manual: claudeEntry,
23
+ platform: rawEntry,
24
+ default: defaultEntry,
25
+ };
26
+ const result = resolveEngineConfig(routing, "platform");
27
+ assert.deepEqual(result, rawEntry);
28
+ });
29
+ it("falls back to default when origin not in routing", () => {
30
+ const routing = {
31
+ user_manual: claudeEntry,
32
+ default: defaultEntry,
33
+ };
34
+ // self_cycle not in routing → fallback to default
35
+ const result = resolveEngineConfig(routing, "self_cycle");
36
+ assert.deepEqual(result, defaultEntry);
37
+ });
38
+ it("falls back to default when origin is undefined", () => {
39
+ const routing = { default: defaultEntry };
40
+ const result = resolveEngineConfig(routing, undefined);
41
+ assert.deepEqual(result, defaultEntry);
42
+ });
43
+ it("returns null when routing is undefined (backward-compat: use base config)", () => {
44
+ const result = resolveEngineConfig(undefined, "user_manual");
45
+ assert.equal(result, null);
46
+ });
47
+ it("returns null when routing is null", () => {
48
+ const result = resolveEngineConfig(null, "user_manual");
49
+ assert.equal(result, null);
50
+ });
51
+ it("returns null when routing has no matching entry and no default", () => {
52
+ const routing = { user_manual: claudeEntry };
53
+ // self_cycle not in routing, no default
54
+ const result = resolveEngineConfig(routing, "self_cycle");
55
+ assert.equal(result, null);
56
+ });
57
+ it("returns null when routing is empty object and origin is undefined", () => {
58
+ const result = resolveEngineConfig({}, undefined);
59
+ assert.equal(result, null);
60
+ });
61
+ it("retry origin resolves to its own routing entry when configured", () => {
62
+ const retryEntry = { engine: "raw", rawApiUrl: "https://api.deepseek.com/v1", model: "deepseek-chat" };
63
+ const routing = {
64
+ user_manual: claudeEntry,
65
+ retry: retryEntry,
66
+ default: defaultEntry,
67
+ };
68
+ const result = resolveEngineConfig(routing, "retry");
69
+ assert.deepEqual(result, retryEntry);
70
+ });
71
+ it("retry origin falls back to default when no retry entry configured", () => {
72
+ const routing = {
73
+ user_manual: claudeEntry,
74
+ default: defaultEntry,
75
+ };
76
+ const result = resolveEngineConfig(routing, "retry");
77
+ assert.deepEqual(result, defaultEntry);
78
+ });
79
+ it("reflection origin resolves correctly", () => {
80
+ const reflEntry = { engine: "raw", model: "gemma3:4b" };
81
+ const routing = { reflection: reflEntry, default: defaultEntry };
82
+ const result = resolveEngineConfig(routing, "reflection");
83
+ assert.deepEqual(result, reflEntry);
84
+ });
85
+ });
86
+ // ---------------------------------------------------------------------------
87
+ // downgradeForRetry
88
+ // ---------------------------------------------------------------------------
89
+ describe("downgradeForRetry", () => {
90
+ const origins = ["user_manual", "self_cycle", "platform", "retry", "reflection"];
91
+ it("always returns 'retry' regardless of input", () => {
92
+ for (const origin of origins) {
93
+ assert.equal(downgradeForRetry(origin), "retry", `downgradeForRetry(${origin}) should be 'retry'`);
94
+ }
95
+ });
96
+ it("user_manual + isRetry=true → 'retry' (not user_manual)", () => {
97
+ // This is the spec's explicit test case for the downgrade rule
98
+ const original = "user_manual";
99
+ const downgraded = downgradeForRetry(original);
100
+ assert.equal(downgraded, "retry");
101
+ assert.notEqual(downgraded, "user_manual");
102
+ });
103
+ });
104
+ // ---------------------------------------------------------------------------
105
+ // deriveChildOrigin
106
+ // ---------------------------------------------------------------------------
107
+ describe("deriveChildOrigin", () => {
108
+ const origins = ["user_manual", "self_cycle", "platform", "retry", "reflection"];
109
+ it("always returns 'platform' regardless of parent", () => {
110
+ for (const origin of origins) {
111
+ assert.equal(deriveChildOrigin(origin), "platform", `deriveChildOrigin(${origin}) should be 'platform'`);
112
+ }
113
+ });
114
+ it("user_manual parent does NOT propagate to child (anti-contamination rule)", () => {
115
+ const child = deriveChildOrigin("user_manual");
116
+ assert.equal(child, "platform");
117
+ assert.notEqual(child, "user_manual");
118
+ });
119
+ it("self_cycle parent → child is platform, not self_cycle", () => {
120
+ assert.equal(deriveChildOrigin("self_cycle"), "platform");
121
+ });
122
+ });
@@ -12,6 +12,22 @@ import { callAgent } from "./relay-client.js";
12
12
  import { loadConversation, appendRound, buildLLMContext, resolveConvId, loadProductContext, appendProductLog } from "./context.js";
13
13
  import { biosPath, loadBioState, saveBioState, localNow, bioStatePromptModifier, feedHunger, appendBioEvent, SHOP_ITEMS, loadAgentConfig, } from "./self.js";
14
14
  // ---------------------------------------------------------------------------
15
+ // Shared call_agent handler — used by both createMcpServer and createMcpProxyServer
16
+ // ---------------------------------------------------------------------------
17
+ async function handleCallAgent(agentName, target, task) {
18
+ console.log(`[call_agent] ${agentName} → ${target}: ${task.slice(0, 80)}`);
19
+ try {
20
+ const result = await callAgent(target, task);
21
+ return { content: [{ type: "text", text: result }] };
22
+ }
23
+ catch (err) {
24
+ return {
25
+ content: [{ type: "text", text: `[error] Failed to call agent "${target}": ${err.message}` }],
26
+ isError: true,
27
+ };
28
+ }
29
+ }
30
+ // ---------------------------------------------------------------------------
15
31
  // createMcpServer
16
32
  // ---------------------------------------------------------------------------
17
33
  export function createMcpServer(opts, deps) {
@@ -150,21 +166,7 @@ ${productPrefix}${contextPrefix}Current task: ${task}`;
150
166
  server.tool("call_agent", "Synchronous call to another agent. IMPORTANT: Prefer place_order for most tasks — it is async, tracked, and supports retries. Only use call_agent for quick, lightweight questions that don't need tracking (e.g. 'what is your specialty?'). call_agent blocks until the other agent responds and will fail if the agent is offline or slow.", {
151
167
  agent: z.string().describe("Name of the target agent to call"),
152
168
  task: z.string().describe("Task to send to the target agent"),
153
- }, async ({ agent: target, task }) => {
154
- console.log(`[call_agent] ${agentName} → ${target}: ${task.slice(0, 80)}`);
155
- try {
156
- const result = await callAgent(target, task);
157
- return {
158
- content: [{ type: "text", text: result }],
159
- };
160
- }
161
- catch (err) {
162
- return {
163
- content: [{ type: "text", text: `[error] Failed to call agent "${target}": ${err.message}` }],
164
- isError: true,
165
- };
166
- }
167
- });
169
+ }, ({ agent: target, task }) => handleCallAgent(agentName, target, task));
168
170
  // Discovery tool
169
171
  server.tool("list_agents", "List available agents on the relay. Use this to discover agents you can collaborate with via place_order.", {
170
172
  tag: z.string().optional().describe("Filter by tag (e.g. 'translation', 'code')"),
@@ -377,14 +379,7 @@ export function createMcpProxyServer(proxy, agentName) {
377
379
  server.setRequestHandler(CallToolRequestSchema, async (request) => {
378
380
  const { name, arguments: toolArgs } = request.params;
379
381
  if (name === "call_agent") {
380
- console.log(`[call_agent] ${agentName} → ${toolArgs?.agent}: ${String(toolArgs?.task).slice(0, 80)}`);
381
- try {
382
- const result = await callAgent(toolArgs?.agent, toolArgs?.task);
383
- return { content: [{ type: "text", text: result }] };
384
- }
385
- catch (err) {
386
- return { content: [{ type: "text", text: `[error] ${err.message}` }], isError: true };
387
- }
382
+ return handleCallAgent(agentName, toolArgs?.agent, toolArgs?.task);
388
383
  }
389
384
  // Forward to child MCP server
390
385
  console.log(`[mcp-proxy] → ${name}(${JSON.stringify(toolArgs).slice(0, 100)})`);
@@ -164,6 +164,7 @@ Output ONLY a JSON object:`;
164
164
  context,
165
165
  question,
166
166
  priority: "normal",
167
+ origin: "self_cycle",
167
168
  });
168
169
  if (!result.success) {
169
170
  console.log(`[memory] Digestion compute failed: ${result.error}`);
@@ -235,6 +236,7 @@ ${unsummarized.map(i => `- [${i.ts}] who: ${i.who}, doing: ${i.doing}`).join("\n
235
236
  question: `Write a personality summary (2-4 paragraphs) that captures who you are.
236
237
  Reply ONLY with the summary text, no JSON, no markdown headers.`,
237
238
  priority: "low",
239
+ origin: "self_cycle",
238
240
  });
239
241
  if (compressResult.success && compressResult.response?.trim()) {
240
242
  const lastEntry = unsummarized[unsummarized.length - 1];
@@ -0,0 +1,30 @@
1
+ /**
2
+ * metrics.ts — module-scope metrics state container.
3
+ *
4
+ * Each module writes to metricsState at key events.
5
+ * relay-client reads getMetrics() every 30s and sends it to relay.
6
+ */
7
+ const startTime = Date.now();
8
+ const metricsState = {
9
+ agentName: "",
10
+ uptime_ms: 0,
11
+ engine_children_active: 0,
12
+ engine_queue_depth: 0,
13
+ engine_last_exec_ms: [],
14
+ task_executing: 0,
15
+ task_pending_retries: 0,
16
+ bio: { hunger: 0, energy: 0, mood: "" },
17
+ };
18
+ export function getMetrics() {
19
+ return { ...metricsState, uptime_ms: Date.now() - startTime };
20
+ }
21
+ export function updateMetrics(patch) {
22
+ Object.assign(metricsState, patch);
23
+ }
24
+ /** Append an exec duration (ms) to the ring buffer, keeping last 10. */
25
+ export function pushExecMs(ms) {
26
+ metricsState.engine_last_exec_ms.push(ms);
27
+ if (metricsState.engine_last_exec_ms.length > 10) {
28
+ metricsState.engine_last_exec_ms.shift();
29
+ }
30
+ }