Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

daemon(manager): clear nonCleanEndMarker on force_exit so it can't leak onto the next turn

Gusd065208e8e28
diff --git a/src/daemon/src/manager/managerRuntime.test.ts b/src/daemon/src/manager/managerRuntime.test.ts
index 7624ee4d0..d453699d6 100644
--- a/src/daemon/src/manager/managerRuntime.test.ts
+++ b/src/daemon/src/manager/managerRuntime.test.ts
@@ -2081,6 +2081,65 @@ describe("AgentProcessManager — onFsmTransition trace (observability, zero beh
}
});
+ it("force_exit clears the killed_stalled marker so it does NOT leak onto the next reborn turn's turn_end", async () => {
+ // Regression: terminate_stalled sets nonCleanEndMarker=killed_stalled. If the
+ // kill's stop() produces no exit, stoppingStuck escalates force_exit — whose
+ // teardown must clear the marker. Otherwise the leaked marker is consumed by
+ // the NEXT (healthy, reborn) turn's turn_end and mislabels it killed_stalled
+ // (a forensic lie that corrupted our codex-wedge trace read).
+ vi.useFakeTimers();
+ try {
+ let now = 0;
+ const recs: Record<string, unknown>[] = [];
+ // stop() never drives an exit → the terminate_stalled kill wedges in
+ // `stopping` → stoppingStuck escalates force_exit (the path that leaks).
+ const stopSpy = vi.fn();
+ const session = fakeSession();
+ session.stop = stopSpy;
+ const mgr = new AgentProcessManager({
+ driverFor: () => fakeDriver("codex"), // per_turn — same stall setup as the killed_stalled test above
+ baseContextFor: () => ({ workingDirectory: "/tmp", agentId: "a1", standingPrompt: "", config: {} as LaunchContext["config"], credentialProxy: {} as LaunchContext["credentialProxy"] }),
+ sessionFactory: () => session,
+ now: () => now,
+ tickIntervalMs: 5,
+ staleThresholdMs: 100,
+ stoppingStuckThresholdMs: 100,
+ onFsmTransition: ((r: Record<string, unknown>) => recs.push(r)) as never,
+ });
+ mgr.start();
+ mgr.register("a1");
+ mgr.deliver("a1", { seq: 1, text: "hi" });
+ session.startResolver?.();
+ await Promise.resolve();
+ session.fire("runtime_event", { kind: "session_init", sessionId: "s1" });
+ // Turn is in flight (turnActive) and makes NO progress → terminate_stalled
+ // sets killed_stalled marker. (Same trigger as the killed_stalled test.)
+ now = 200;
+ await vi.advanceTimersByTimeAsync(10);
+ expect(recs.find((r) => Array.isArray(r.effects) && (r.effects as string[]).includes("terminate_stalled"))).toBeTruthy();
+ // stop() produced no exit → sit in stopping past stoppingStuck → force_exit.
+ now = 500;
+ await vi.advanceTimersByTimeAsync(10);
+ expect(recs.find((r) => Array.isArray(r.effects) && (r.effects as string[]).includes("force_exit"))).toBeTruthy();
+
+ // force_exit dispatched a synthetic exit → respawn. The reborn turn runs
+ // and ends CLEANLY. Its turn_end must NOT inherit the leaked marker.
+ session.startResolver?.();
+ await Promise.resolve();
+ recs.length = 0;
+ session.fire("runtime_event", { kind: "session_init", sessionId: "s2" });
+ session.fire("runtime_event", { kind: "turn_end" });
+ const turnEnd = recs.find((r) => r.event === "turn_end" && r.agentId === "a1");
+ expect(turnEnd).toBeTruthy();
+ // The whole point: marker was cleared at force_exit, so this healthy turn
+ // is a CLEAN end — no leaked killed_stalled.
+ expect(turnEnd!.terminationCause).toBeUndefined();
+ expect(turnEnd!.endReason).not.toBe("errored");
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it("a VOLUNTARY idle-timeout stop (which also flips status→stopping) does NOT produce a tagged turn_end (red line 2 — cause, not bare status)", async () => {
// Cecilia's "don't treat 'not seen' as 'won't happen'": idle-hibernation's
// `stop(idle_timeout)` flips status→stopping too, but it is a CLEAN end. We
diff --git a/src/daemon/src/manager/managerRuntime.ts b/src/daemon/src/manager/managerRuntime.ts
index a6733cdba..ae3b6598f 100644
--- a/src/daemon/src/manager/managerRuntime.ts
+++ b/src/daemon/src/manager/managerRuntime.ts
@@ -1312,6 +1312,15 @@ export class AgentProcessManager {
if (this.sessions.get(effect.agentId) === session) this.sessions.delete(effect.agentId);
this.liveSessions.delete(effect.agentId);
if (this.activeSpawnState.get(effect.agentId) === state) this.activeSpawnState.delete(effect.agentId);
+ // Clear the killed_stalled marker set at (1249) — this force_exit IS its
+ // trailing terminal event. The `session.on("exit")` clear (~:1569) can't
+ // do it on this path: force_exit set `torndown` (the late real exit
+ // bails at the torndown early-return) AND deleted activeSpawnState (its
+ // `=== state` guard then fails). Without this, the leaked marker gets
+ // consumed by the NEXT healthy reborn turn's turn_end (~:1842) and
+ // mislabels that turn `killed_stalled` in the trace/audit — a forensic
+ // lie, not a behavior bug, but it corrupts post-force_exit trace reads.
+ this.nonCleanEndMarker.delete(effect.agentId);
this.opts.onAgentLocallyStopped?.({ agentId: effect.agentId, reason: "terminate_stalled" });
// (3) Synthetic `exit` → the normal onExit recovery (drain-respawn or
// settle idle; enterStable clears stoppingSince). Universal backstop: