Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(daemon): rescue silently-stalled gated agents + surface runtime stderr

Gus601557a891bc
diff --git a/src/daemon/src/manager/managerPolicy.test.ts b/src/daemon/src/manager/managerPolicy.test.ts
index a468a80ab..597c053d9 100644
--- a/src/daemon/src/manager/managerPolicy.test.ts
+++ b/src/daemon/src/manager/managerPolicy.test.ts
@@ -332,6 +332,36 @@ describe("reduceManager — tick: stall + idle hibernation", () => {
expect(r.state.agents.a.sessionId).toBe("sess-1"); // preserved for resume
});
+ it("terminates a stalled gated agent when inbox has queued work", () => {
+ let s = createInitialManagerState(100);
+ s = register(s, "a", PERSISTENT_GATED);
+ s = reduceManager(s, { type: "wake", agentId: "a", message: { text: "m1" }, nowMs: 0 }).state;
+ s = reduceManager(s, { type: "spawned", agentId: "a", nowMs: 0 }).state;
+ s = reduceManager(s, { type: "session", agentId: "a", sessionId: "sess-1" }).state;
+ // A second wake mid-turn is held (gated) and lands in the inbox.
+ s = reduceManager(s, { type: "wake", agentId: "a", message: { text: "m2" }, nowMs: 10 }).state;
+ expect(s.agents.a.inbox.length).toBe(1);
+ expect(s.agents.a.turnActive).toBe(true);
+
+ const r = reduceManager(s, { type: "tick", nowMs: 200 });
+ expect(r.effects).toEqual([{ type: "terminate_stalled", agentId: "a" }]);
+ expect(r.state.agents.a.status).toBe("stopping");
+ });
+
+ it("does NOT terminate a stalled gated agent whose inbox is empty", () => {
+ let s = createInitialManagerState(100);
+ s = register(s, "a", PERSISTENT_GATED);
+ s = reduceManager(s, { type: "wake", agentId: "a", message: { text: "m1" }, nowMs: 0 }).state;
+ s = reduceManager(s, { type: "spawned", agentId: "a", nowMs: 0 }).state;
+ // Inbox drained on spawn; no queued work.
+ expect(s.agents.a.inbox.length).toBe(0);
+ expect(s.agents.a.turnActive).toBe(true);
+
+ const r = reduceManager(s, { type: "tick", nowMs: 200 });
+ expect(r.effects).toEqual([]);
+ expect(r.state.agents.a.status).toBe("running");
+ });
+
it("idle timeout of 0 disables hibernation", () => {
let s = createInitialManagerState(100_000, 0);
s = register(s, "a", PERSISTENT_GATED);
diff --git a/src/daemon/src/manager/managerPolicy.ts b/src/daemon/src/manager/managerPolicy.ts
index e8210607a..67475e723 100644
--- a/src/daemon/src/manager/managerPolicy.ts
+++ b/src/daemon/src/manager/managerPolicy.ts
@@ -461,13 +461,20 @@ function onTick(state: ManagerState, nowMs: number): ReduceResult {
const a = agents[id];
// Stalled recovery: running, turn in flight, no progress past threshold.
+ // Gated runtimes are normally excluded from restart (their tool-batch/
+ // compaction/review boundaries do the flushing), BUT a silent gated turn
+ // with queued inbox has no other exit — every wake hits `gated_hold` and
+ // nothing frees it. Include gated when work is backing up so `onExit`'s
+ // drain-and-respawn can rescue it.
const stalled =
a.status === "running" &&
a.turnActive &&
nowMs - a.lastProgressAt >= state.staleThresholdMs &&
- // Only restartable runtimes (persistent+direct, or per-turn) — mirror daemon policy.
(a.caps.lifecycleKind === "per_turn" ||
- (a.caps.supportsStdinNotification && a.caps.busyDeliveryMode === "direct"));
+ (a.caps.supportsStdinNotification && a.caps.busyDeliveryMode === "direct") ||
+ (a.caps.supportsStdinNotification &&
+ a.caps.busyDeliveryMode === "gated" &&
+ a.inbox.length > 0));
if (stalled) {
agents[id] = { ...a, status: "stopping", idleSince: null };
effects.push({ type: "terminate_stalled", agentId: id });
diff --git a/src/daemon/src/manager/managerRuntime.ts b/src/daemon/src/manager/managerRuntime.ts
index 2c67d0752..730864fdb 100644
--- a/src/daemon/src/manager/managerRuntime.ts
+++ b/src/daemon/src/manager/managerRuntime.ts
@@ -923,6 +923,19 @@ export class AgentProcessManager {
}
this.onRuntimeEvent(agentId, e, driver.id);
});
+ // Only ChildProcessRuntimeSession emits `stderr`; SdkManagedSession
+ // doesn't, so this is harmless when the session is an SDK adapter.
+ // Runtime CLIs (Claude Code, Codex, etc.) write auth prompts, rate-limit
+ // errors, panics, and network hangs here — everything the daemon's
+ // stream-json parser will NEVER see. Without this line, a gated agent
+ // that goes silent is a black hole; with it, the underlying error lands
+ // in the main daemon log next to the `gated busy message held` line and
+ // the next incident is diagnosable.
+ session.on("stderr", (...args: unknown[]) => {
+ const raw = typeof args[0] === "string" ? args[0] : String(args[0] ?? "");
+ const text = raw.length > 2000 ? raw.slice(0, 2000) + "…" : raw;
+ this.log.warn("runtime stderr", { agentId, runtime: driver.id, text });
+ });
// Child-process `error` (ENOENT etc.) — Node EE emits this before or in
// parallel with `exit`. Without this subscriber the raw `error` would
// become an unhandled EE emit.