Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

chore(daemon): surface stoppingSince + sinceStoppingMs in the FSM trace (batch H)

Gus9c49ad895d23
diff --git a/src/daemon/src/manager/managerRuntime.test.ts b/src/daemon/src/manager/managerRuntime.test.ts
index d555c185e..a07b26d73 100644
--- a/src/daemon/src/manager/managerRuntime.test.ts
+++ b/src/daemon/src/manager/managerRuntime.test.ts
@@ -1817,4 +1817,50 @@ describe("AgentProcessManager — onFsmTransition trace (observability, zero beh
vi.useRealTimers();
}
});
+
+ it("carries stoppingSince + sinceStoppingMs — null while not stopping, populated once in `stopping` (batch H)", async () => {
+ vi.useFakeTimers();
+ try {
+ let now = 0;
+ const recs: Record<string, unknown>[] = [];
+ const persistentDriver = {
+ ...fakeDriver("codex"),
+ lifecycle: { kind: "persistent", start: "immediate", exit: "natural", inFlightWake: "queue" } as never,
+ } as Driver;
+ const session = fakeSession();
+ const mgr = new AgentProcessManager({
+ driverFor: () => persistentDriver,
+ baseContextFor: () => ({ workingDirectory: "/tmp", agentId: "a1", standingPrompt: "", config: {} as LaunchContext["config"], credentialProxy: {} as LaunchContext["credentialProxy"] }),
+ sessionFactory: () => session,
+ now: () => now,
+ tickIntervalMs: 5,
+ idleTimeoutMs: 50, // drive into `stopping` via idle-timeout
+ stoppingStuckThresholdMs: 1_000_000, // huge so it does NOT force_exit during this test — we just want the stopping snapshot
+ 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" });
+ session.fire("runtime_event", { kind: "turn_end" });
+
+ // While running/idle-not-stopping, the field is null.
+ const runningRec = recs.find((r) => r.status === "running");
+ expect(runningRec).toBeTruthy();
+ expect(runningRec!.stoppingSince).toBeNull();
+ expect(runningRec!.sinceStoppingMs).toBeNull();
+
+ recs.length = 0;
+ now = 100; // past idleTimeout=50 → idle-hibernation tick sets status=stopping, stoppingSince=100
+ await vi.advanceTimersByTimeAsync(10);
+ const stoppingRec = recs.find((r) => r.status === "stopping" && r.agentId === "a1");
+ expect(stoppingRec).toBeTruthy();
+ expect(stoppingRec!.stoppingSince).toBe(100);
+ expect(typeof stoppingRec!.sinceStoppingMs).toBe("number"); // now - 100, populated
+ } finally {
+ vi.useRealTimers();
+ }
+ });
});
diff --git a/src/daemon/src/manager/managerRuntime.ts b/src/daemon/src/manager/managerRuntime.ts
index fa914d810..43993254f 100644
--- a/src/daemon/src/manager/managerRuntime.ts
+++ b/src/daemon/src/manager/managerRuntime.ts
@@ -174,6 +174,7 @@ export interface ManagerRuntimeOpts {
idleSince: number | null;
resetting: boolean;
resettingSince: number | null;
+ stoppingSince: number | null;
apmPhase: string;
effects: string[];
nowMs: number;
@@ -181,6 +182,8 @@ export interface ManagerRuntimeOpts {
sinceProgressMs: number;
/** `nowMs - lastDeliverAt` (null if never delivered) — suspectedDeaf's half. */
sinceDeliverMs: number | null;
+ /** `nowMs - stoppingSince` (null unless in `stopping`) — stopping-stuck backstop clock. */
+ sinceStoppingMs: number | null;
}) => void;
/**
* Optional context-timeline recorder. When provided, the manager logs each
@@ -968,6 +971,7 @@ export class AgentProcessManager {
idleSince: a.idleSince,
resetting: a.resetting,
resettingSince: a.resettingSince,
+ stoppingSince: a.stoppingSince,
apmPhase: a.apm.phase,
effects: myEffects,
nowMs,
@@ -977,9 +981,13 @@ export class AgentProcessManager {
// wedged agent, lastProgressAt is being bumped by stray progress =
// the anchor is unusable — the exit-1 decision). `sinceDeliverMs` is
// suspectedDeaf's half (null when no deliver ever happened = its
- // blind spot). See plans/daemon-fsm-desync.md.
+ // blind spot). `sinceStoppingMs` is the stopping-stuck backstop's
+ // clock (null unless in `stopping`) — how close it is to `force_exit`,
+ // so a stopping-wedge is legible in the trace instead of inferred from
+ // a bare `status=stopping` streak. See plans/daemon-fsm-desync.md.
sinceProgressMs: nowMs - a.lastProgressAt,
sinceDeliverMs: a.lastDeliverAt === null ? null : nowMs - a.lastDeliverAt,
+ sinceStoppingMs: a.stoppingSince === null ? null : nowMs - a.stoppingSince,
});
};
const agentId = (event as { agentId?: string }).agentId;