Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): report agent activity while working, not idle

Gus115179dc94a7
diff --git a/src/daemon/src/daemon/createDaemon.test.ts b/src/daemon/src/daemon/createDaemon.test.ts
index 687e46bfb..96efbaa21 100644
--- a/src/daemon/src/daemon/createDaemon.test.ts
+++ b/src/daemon/src/daemon/createDaemon.test.ts
@@ -425,6 +425,90 @@ describe("createDaemon — logging", () => {
});
});
+describe("createDaemon — level-triggered activity heartbeat (2b: live-connection self-heal)", () => {
+ const originalFetch = global.fetch;
+ afterEach(() => {
+ global.fetch = originalFetch;
+ vi.useRealTimers();
+ });
+
+ it("re-asserts a running agent's current activity every heartbeat with NO intervening transition — recovery path for a dropped frame on a live socket", async () => {
+ vi.useFakeTimers();
+ global.fetch = vi.fn(async (url: string | URL) => {
+ const href = String(url);
+ if (href.includes("/enroll-agent")) return new Response(JSON.stringify({ runnerKey: "rk_1" }), { status: 200 });
+ return new Response(JSON.stringify({ bots: [] }), { status: 200 });
+ }) as unknown as typeof fetch;
+
+ // A persistent, stdin-capable driver stays `running` after session_init
+ // without emitting turn_end — so the agent sits in a STEADY working state,
+ // which is exactly the window 2b exercises.
+ const emitters: Array<EventEmitter> = [];
+ const persistentDriver = {
+ id: "codex",
+ lifecycle: { kind: "persistent", start: "immediate", exit: "natural", inFlightWake: "queue" } as never,
+ session: { recovery: "resume_or_fresh" } as never,
+ model: { detectedModelsVerifiedAs: "launchable", toLaunchSpec: () => ({ args: [] }) } as never,
+ supportsStdinNotification: true,
+ busyDeliveryMode: "direct",
+ probe: () => ({ status: "healthy" as const, version: "test" }),
+ spawn: async () => {
+ const proc = new EventEmitter() as unknown as { kill: () => void; stdin: unknown };
+ (proc as unknown as { kill: () => void }).kill = () => {};
+ emitters.push(proc as unknown as EventEmitter);
+ return { process: proc as never };
+ },
+ parseLine: () => [],
+ encodeStdinMessage: () => null,
+ buildSystemPrompt: () => "",
+ } as unknown as Driver;
+
+ const sockets: FakeSocket[] = [];
+ const daemon = await createDaemon({
+ machineKey: "cmk_hb",
+ serverUrl: "http://localhost:9999",
+ serverWsUrl: "ws://x",
+ webSocketFactory: factory(sockets) as any,
+ runtimeReport: [{ id: "codex" }],
+ driverFor: () => persistentDriver,
+ capabilities: [],
+ tickIntervalMs: 1_000_000, // park the stall/hibernation loop out of the way
+ });
+ sockets[0].emit("open");
+ sockets[0].emit("message", JSON.stringify({ type: "bot:added", botId: "bot_1", name: "Bot One", discriminator: "4821" }));
+ sockets[0].emit(
+ "message",
+ JSON.stringify({
+ type: "agent:wake",
+ agentId: "bot_1",
+ config: { version: 1, runtime: "codex", model: { kind: "default" }, mode: { kind: "default" } },
+ launchId: "l1",
+ unreadNotice: { kind: "unread_notice", channel: "/demo/general", latestSeq: 1 },
+ }),
+ );
+ // Let enroll + spawn resolve, then land the runtime handshake so the FSM
+ // reaches `running` (turnActive) — a steady working state.
+ await vi.advanceTimersByTimeAsync(50);
+ expect(emitters.length).toBeGreaterThan(0);
+ emitters[0].emit("runtime_event", { kind: "session_init", sessionId: "s1" });
+ await vi.advanceTimersByTimeAsync(1);
+
+ const activityFrames = () =>
+ sockets[0].sent.map((s) => JSON.parse(s)).filter((f: any) => f.type === "agent_activity");
+ const beforeCount = activityFrames().length;
+ expect(beforeCount).toBeGreaterThan(0); // the edge transition to running fired
+
+ // Advance ONE heartbeat with no further runtime events → the level-triggered
+ // re-assert must emit another running frame despite zero transitions.
+ await vi.advanceTimersByTimeAsync(5_000);
+ const after = activityFrames();
+ expect(after.length).toBeGreaterThan(beforeCount);
+ expect(after.at(-1)).toMatchObject({ type: "agent_activity", agentId: "bot_1", state: "running" });
+
+ await daemon.stop();
+ });
+});
+
describe("deriveAuditLogSubcommand", () => {
it("maps the CLI's bare /api/* pathnames to their subcommand suffix", () => {
expect(deriveAuditLogSubcommand("/api/send")).toBe("send");
diff --git a/src/daemon/src/daemon/createDaemon.ts b/src/daemon/src/daemon/createDaemon.ts
index d24a6c0cc..a825d0195 100644
--- a/src/daemon/src/daemon/createDaemon.ts
+++ b/src/daemon/src/daemon/createDaemon.ts
@@ -270,6 +270,19 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
typingHeartbeats.delete(agentId);
}
}
+ // Level-triggered activity re-assert (durability for the profile pill).
+ // `onAgentActivity` is edge-triggered, so an `agent_activity` frame dropped
+ // during a disconnect window strands the pill on a stale state with no
+ // recovery. This re-sends the manager's CURRENT derived activity
+ // unconditionally each heartbeat — independent of any transition diff — so a
+ // live, never-reconnecting agent's pill self-heals within one interval. The
+ // heartbeat only runs while the agent is in a starting/running-family state
+ // (torn down on idle/stopping), so it never wrongly asserts "working" for an
+ // idle agent. Idempotent on the ws-do side (rewrites the same preset).
+ function reassertAgentActivity(agentId: string): void {
+ const state = managerRef?.agentActivity(agentId);
+ if (state) channel.reportAgentActivity?.({ agentId, state });
+ }
function startTypingHeartbeat(agentId: string): void {
stopTypingHeartbeat(agentId);
// Immediate emit + every 5s while active — comfortably refreshes the
@@ -283,6 +296,7 @@ export async function createDaemon(opts: CreateDaemonOptions): Promise<RunningDa
for (const dmConversationId of typingTracker.snapshot(agentId)) {
channel.reportAgentTyping?.({ agentId, dmConversationId });
}
+ reassertAgentActivity(agentId);
}, TYPING_HEARTBEAT_MS);
timer.unref?.();
typingHeartbeats.set(agentId, timer);
diff --git a/src/daemon/src/manager/agentRouter.ts b/src/daemon/src/manager/agentRouter.ts
index 9425cca15..78523ea4a 100644
--- a/src/daemon/src/manager/agentRouter.ts
+++ b/src/daemon/src/manager/agentRouter.ts
@@ -184,6 +184,7 @@ export class AgentRouter {
this.opts.channel.onResync?.(() => ({
ready: this.buildReady(),
sessions: this.opts.manager.liveSessionReports() as AgentSessionReport[],
+ activities: this.opts.manager.liveAgentActivities(),
}));
await this.opts.channel.reportReady(this.buildReady());
}
diff --git a/src/daemon/src/manager/managerPolicy.test.ts b/src/daemon/src/manager/managerPolicy.test.ts
index 90a698902..627cd9b95 100644
--- a/src/daemon/src/manager/managerPolicy.test.ts
+++ b/src/daemon/src/manager/managerPolicy.test.ts
@@ -2,9 +2,13 @@ import { describe, it, expect } from "vitest";
import {
reduceManager,
createInitialManagerState,
+ isActivelyWorking,
type ManagerState,
+ type AgentState,
+ type AgentStatus,
type AgentRuntimeCaps,
} from "./managerPolicy";
+import { createInitialApmGatedSteeringState } from "../runtime/apmStateMachine";
const PERSISTENT_GATED: AgentRuntimeCaps = {
lifecycleKind: "persistent",
@@ -543,3 +547,38 @@ describe("reduceManager — onExit / onSpawned clear resetting", () => {
expect(r.state.agents.a.apm.outstandingToolUses).toBe(0);
});
});
+
+describe("isActivelyWorking — single source of truth for the profile pill", () => {
+ function agentWith(fields: { status: AgentStatus; turnActive: boolean; inbox: number }): AgentState {
+ return {
+ agentId: "a",
+ status: fields.status,
+ caps: PERSISTENT_GATED,
+ inbox: Array.from({ length: fields.inbox }, (_, i) => ({ seq: i, text: "m" })),
+ sessionId: null,
+ turnActive: fields.turnActive,
+ lastProgressAt: 0,
+ idleSince: null,
+ resetting: false,
+ apm: createInitialApmGatedSteeringState(),
+ };
+ }
+
+ it("running + turn in flight ⇒ working", () => {
+ expect(isActivelyWorking(agentWith({ status: "running", turnActive: true, inbox: 0 }))).toBe(true);
+ });
+
+ it("running + no turn + queued inbox ⇒ working (the reported bug: mid-task between turns)", () => {
+ expect(isActivelyWorking(agentWith({ status: "running", turnActive: false, inbox: 2 }))).toBe(true);
+ });
+
+ it("running + no turn + empty inbox ⇒ NOT working (genuinely idle, pre-hibernation)", () => {
+ expect(isActivelyWorking(agentWith({ status: "running", turnActive: false, inbox: 0 }))).toBe(false);
+ });
+
+ it("non-running states are never working, even with a queued inbox", () => {
+ for (const status of ["idle", "starting", "stopping"] as AgentStatus[]) {
+ expect(isActivelyWorking(agentWith({ status, turnActive: true, inbox: 3 }))).toBe(false);
+ }
+ });
+});
diff --git a/src/daemon/src/manager/managerPolicy.ts b/src/daemon/src/manager/managerPolicy.ts
index 91a60d5a6..90798f24b 100644
--- a/src/daemon/src/manager/managerPolicy.ts
+++ b/src/daemon/src/manager/managerPolicy.ts
@@ -81,6 +81,18 @@ export interface AgentState {
apm: ApmGatedSteeringState;
}
+/**
+ * An agent counts as actively working iff its process is `running` AND it has
+ * work in hand — a turn in flight, or queued inbox not yet drained into a turn.
+ * `running` alone is not enough: a persistent agent stays `running` for up to
+ * `idleTimeoutMs` after a turn ends with an empty inbox, which is genuinely
+ * idle. Single source of truth for both the activity derivation and the
+ * durability re-assert, so they can never disagree.
+ */
+export function isActivelyWorking(agent: AgentState): boolean {
+ return agent.status === "running" && (agent.turnActive || agent.inbox.length > 0);
+}
+
export interface ManagerState {
agents: Record<string, AgentState>;
/** Stall threshold: no progress for this long while running ⇒ terminate. */
diff --git a/src/daemon/src/manager/managerRuntime.test.ts b/src/daemon/src/manager/managerRuntime.test.ts
index 163b09bd4..0b8ffb050 100644
--- a/src/daemon/src/manager/managerRuntime.test.ts
+++ b/src/daemon/src/manager/managerRuntime.test.ts
@@ -620,6 +620,44 @@ describe("AgentProcessManager — onAgentActivity (derived activity reporting)",
]);
});
+ it("liveAgentActivities / agentActivity report the DERIVED state (running while a turn is in flight, idle once it ends) — the level-triggered source for resync + heartbeat re-assert", async () => {
+ const persistentDriver = {
+ ...fakeDriver("codex"),
+ lifecycle: { kind: "persistent", start: "immediate", exit: "natural", inFlightWake: "queue" } as never,
+ supportsStdinNotification: true,
+ busyDeliveryMode: "direct",
+ } 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,
+ });
+ mgr.register("a1");
+ // register alone: known but not running ⇒ idle.
+ expect(mgr.agentActivity("a1")).toBe("idle");
+
+ mgr.deliver("a1", { seq: 1, text: "hello" });
+ session.startResolver?.();
+ await Promise.resolve();
+ session.fire("runtime_event", { kind: "session_init", sessionId: "s1" }); // -> running, turnActive
+ expect(mgr.agentActivity("a1")).toBe("running");
+ expect(mgr.liveAgentActivities()).toEqual([{ agentId: "a1", state: "running" }]);
+
+ session.fire("runtime_event", { kind: "turn_end" }); // running, !turnActive, empty inbox -> idle
+ expect(mgr.agentActivity("a1")).toBe("idle");
+ expect(mgr.liveAgentActivities()).toEqual([{ agentId: "a1", state: "idle" }]);
+
+ // Unknown agent has no derivable activity.
+ expect(mgr.agentActivity("nope")).toBeNull();
+ });
+
it("a tick that stalls/hibernates two different agents at once fires onAgentActivity for both", async () => {
vi.useFakeTimers();
try {
diff --git a/src/daemon/src/manager/managerRuntime.ts b/src/daemon/src/manager/managerRuntime.ts
index d0c43a0ba..1ae09fbf3 100644
--- a/src/daemon/src/manager/managerRuntime.ts
+++ b/src/daemon/src/manager/managerRuntime.ts
@@ -19,6 +19,7 @@ import {
type AgentRuntimeCaps,
type AgentMsg,
type AgentState,
+ isActivelyWorking,
} from "./managerPolicy.js";
import type { Driver, LaunchContext, SdkDriverDeps } from "../types.js";
import { resolveLaunchFieldsOrDefault, type RuntimeConfig } from "../runtimeConfig.js";
@@ -814,6 +815,25 @@ export class AgentProcessManager {
}));
}
+ /**
+ * Current derived activity for every known agent, for control-plane resync
+ * and heartbeat re-assert. Level-triggered snapshot of the same
+ * `deriveActivity` the edge-triggered dispatch uses, so a dropped
+ * `agent_activity` frame can be recovered from the daemon's own truth.
+ */
+ liveAgentActivities(): Array<{ agentId: string; state: AgentActivityState }> {
+ return Object.entries(this.deriveActivitySnapshot(this.state)).map(([agentId, state]) => ({
+ agentId,
+ state,
+ }));
+ }
+
+ /** Current derived activity for one agent, or null if it isn't known. */
+ agentActivity(agentId: string): AgentActivityState | null {
+ const agent = this.state.agents[agentId];
+ return agent ? this.deriveActivity(agent) : null;
+ }
+
/* --------------------------------------------------------------- */
/* Core dispatch: reduce → apply effects */
/* --------------------------------------------------------------- */
@@ -843,12 +863,14 @@ export class AgentProcessManager {
/**
* `AgentState.status` alone doesn't mean "actively working" — a persistent
- * agent stays `"running"` (turnActive=false) for up to `idleTimeoutMs` after
- * a turn ends, before the tick loop finally stops it. Report "idle" the
- * moment the turn ends instead of waiting for that hibernation timeout.
+ * agent stays `"running"` for up to `idleTimeoutMs` after a turn ends, before
+ * the tick loop finally stops it. A `running` agent is "running" only while it
+ * has work in hand (`isActivelyWorking`: turn in flight OR queued inbox);
+ * otherwise it reads "idle" the moment the turn ends, without waiting for the
+ * hibernation timeout. `starting`/`stopping` pass through unchanged.
*/
private deriveActivity(agent: AgentState): AgentActivityState {
- if (agent.status === "running" && !agent.turnActive) return "idle";
+ if (agent.status === "running") return isActivelyWorking(agent) ? "running" : "idle";
return agent.status;
}
diff --git a/src/daemon/src/server/wsControlChannel.test.ts b/src/daemon/src/server/wsControlChannel.test.ts
index 5a1201d52..02fc08f0c 100644
--- a/src/daemon/src/server/wsControlChannel.test.ts
+++ b/src/daemon/src/server/wsControlChannel.test.ts
@@ -70,7 +70,7 @@ describe("WsControlChannel — resync on (re)connect", () => {
const { ch, sockets } = makeChannel();
const ready: HostReady = { runtimeReport: [{ id: "mock" }], runningAgents: ["a1"] };
const sessions: AgentSessionReport[] = [{ agentId: "a1", sessionId: "s1", launchId: "l1" }];
- ch.onResync(() => ({ ready, sessions }));
+ ch.onResync(() => ({ ready, sessions, activities: [] }));
ch.connect();
sockets[0].emit("open");
@@ -92,10 +92,42 @@ describe("WsControlChannel — resync on (re)connect", () => {
expect(f.some((x) => x.type === "agent_session" && x.agentId === "a1")).toBe(true);
});
+ it("2a — replays each live agent's current activity on (re)connect, recovering a frame dropped mid-disconnect", async () => {
+ const { ch, sockets } = makeChannel();
+ const ready: HostReady = { runtimeReport: [{ id: "mock" }], runningAgents: ["a1", "a2"] };
+ ch.onResync(() => ({
+ ready,
+ sessions: [{ agentId: "a1", sessionId: "s1", launchId: "l1" }],
+ activities: [
+ { agentId: "a1", state: "running" },
+ { agentId: "a2", state: "idle" },
+ ],
+ }));
+
+ ch.connect();
+ sockets[0].emit("open");
+
+ // Simulate the reported failure: while the socket was down, a1's `running`
+ // activity frame was dropped and never reached the server. On reconnect the
+ // daemon re-asserts its OWN current truth, so the pill self-heals.
+ sockets[0].emit("close");
+ await new Promise((r) => setTimeout(r, 10));
+ expect(sockets.length).toBe(2);
+ sockets[1].emit("open");
+
+ const f = sockets[1].frames();
+ expect(f.some((x) => x.type === "agent_activity" && x.agentId === "a1" && x.state === "running")).toBe(true);
+ expect(f.some((x) => x.type === "agent_activity" && x.agentId === "a2" && x.state === "idle")).toBe(true);
+ // Activity replay comes after ready + sessions.
+ const readyIdx = f.findIndex((x) => x.type === "ready");
+ const activityIdx = f.findIndex((x) => x.type === "agent_activity");
+ expect(readyIdx).toBeLessThan(activityIdx);
+ });
+
it("does NOT replay a stale ready/session if the resync provider's state changed", async () => {
const { ch, sockets } = makeChannel();
let running = ["a1"];
- ch.onResync(() => ({ ready: { runtimeReport: [{ id: "mock" }], runningAgents: running }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [{ id: "mock" }], runningAgents: running }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
@@ -125,7 +157,7 @@ describe("WsControlChannel — auth rejection", () => {
reconnect: { baseMs: 1, maxMs: 1 },
onAuthRejected: () => { authRejectedCalled = true; },
});
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
@@ -151,7 +183,7 @@ describe("WsControlChannel — auth rejection", () => {
},
reconnect: { baseMs: 1, maxMs: 1 },
});
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
@@ -171,7 +203,7 @@ describe("WsControlChannel — ready frame", () => {
runtimeReport: [{ id: "claude", version: "1.0.0" }],
runningAgents: [],
};
- ch.onResync(() => ({ ready, sessions: [] }));
+ ch.onResync(() => ({ ready, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
const frames = sockets[0].frames();
@@ -185,7 +217,7 @@ describe("WsControlChannel — ready frame", () => {
describe("WsControlChannel — wake/stop acks", () => {
it("sends agent_wake_ack when open", async () => {
const { ch, sockets } = makeChannel();
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
await ch.reportWakeAck({ agentId: "a1", launchId: "l1", status: "ok" });
@@ -198,7 +230,7 @@ describe("WsControlChannel — wake/stop acks", () => {
// is nothing to buffer for. A dropped ack while offline is fine: the
// server never addressed this wake attempt on this connection anyway.
const { ch, sockets } = makeChannel();
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
await ch.reportWakeAck({ agentId: "a1", launchId: "l_early", status: "ok" });
expect(sockets[0].frames().some((f) => f.type === "agent_wake_ack")).toBe(false);
@@ -210,7 +242,7 @@ describe("WsControlChannel — wake/stop acks", () => {
describe("WsControlChannel — agent activity reports", () => {
it("sends agent_activity when open", async () => {
const { ch, sockets } = makeChannel();
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
await ch.reportAgentActivity({ agentId: "a1", state: "running" });
@@ -219,7 +251,7 @@ describe("WsControlChannel — agent activity reports", () => {
it("no-ops (does not throw) when the socket isn't open", async () => {
const { ch, sockets } = makeChannel();
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
await expect(ch.reportAgentActivity({ agentId: "a1", state: "idle" })).resolves.toBeUndefined();
expect(sockets[0].frames().some((f) => f.type === "agent_activity")).toBe(false);
@@ -229,7 +261,7 @@ describe("WsControlChannel — agent activity reports", () => {
describe("WsControlChannel — bot audit event reports", () => {
it("sends a well-formed bot_audit_event frame when open", async () => {
const { ch, sockets } = makeChannel();
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
await ch.reportBotAuditEvent({
@@ -245,7 +277,7 @@ describe("WsControlChannel — bot audit event reports", () => {
it("no-ops when the socket isn't open", async () => {
const { ch, sockets } = makeChannel();
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
await expect(
ch.reportBotAuditEvent({
@@ -272,7 +304,7 @@ describe("WsControlChannel — HTTP 401s are non-terminal", () => {
reconnect: { baseMs: 1, maxMs: 1 },
onAuthRejected: () => { authRejectedCalls++; },
});
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
// Simulate 3 consecutive 401-then-close cycles. The channel MUST keep
@@ -301,7 +333,7 @@ describe("WsControlChannel — HTTP 401s are non-terminal", () => {
reconnect: { baseMs: 1, maxMs: 1 },
onAuthRejected: () => { authRejectedCalls++; },
});
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
@@ -347,7 +379,7 @@ describe("WsControlChannel — reconnect timer keeps the event loop alive", () =
},
reconnect: { baseMs: 50, maxMs: 50 },
});
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
@@ -371,6 +403,7 @@ describe("WsControlChannel — logging", () => {
ch.onResync(() => ({
ready: { runtimeReport: [{ id: "claude" }], runningAgents: ["a1"] },
sessions: [{ agentId: "a1", sessionId: "s1", launchId: "l1" }],
+ activities: [],
}));
ch.connect();
sockets[0].emit("open");
@@ -388,7 +421,7 @@ describe("WsControlChannel — logging", () => {
it("logs warn on close", async () => {
const logger = stubLogger();
const { ch, sockets } = makeChannel({ logger });
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
sockets[0].emit("close", 1006, "abnormal");
@@ -401,7 +434,7 @@ describe("WsControlChannel — logging", () => {
it("logs info on each scheduled reconnect with the computed delayMs", async () => {
const logger = stubLogger();
const { ch, sockets } = makeChannel({ logger, reconnect: { baseMs: 10, maxMs: 100 } });
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
sockets[0].emit("close");
@@ -414,7 +447,7 @@ describe("WsControlChannel — logging", () => {
it("logs error on AUTH_REJECTED", async () => {
const logger = stubLogger();
const { ch, sockets } = makeChannel({ logger });
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
sockets[0].emit("message", JSON.stringify({ type: "error", code: "AUTH_REJECTED" }));
@@ -432,7 +465,7 @@ describe("WsControlChannel — logging", () => {
now: () => now,
heartbeat: { pingIntervalMs: 10, pongTimeoutMs: 20 },
});
- ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [] }));
+ ch.onResync(() => ({ ready: { runtimeReport: [], runningAgents: [] }, sessions: [], activities: [] }));
ch.connect();
sockets[0].emit("open");
// Advance the injected clock past the pong deadline, then let the
diff --git a/src/daemon/src/server/wsControlChannel.ts b/src/daemon/src/server/wsControlChannel.ts
index 856e5dea9..d2de96ab9 100644
--- a/src/daemon/src/server/wsControlChannel.ts
+++ b/src/daemon/src/server/wsControlChannel.ts
@@ -124,7 +124,12 @@ type OutboundFrame =
| HostBotAuditEventFrame
| SessionErrorFrame;
-type ResyncProvider = () => { ready: HostReady; sessions: AgentSessionReport[] };
+type AgentActivityReport = { agentId: AgentId; state: AgentActivityState };
+type ResyncProvider = () => {
+ ready: HostReady;
+ sessions: AgentSessionReport[];
+ activities: AgentActivityReport[];
+};
function describeErr(err: unknown): string {
return err instanceof Error ? err.message : String(err);
@@ -306,10 +311,18 @@ export class WsControlChannel implements HostControlChannel {
*/
private resyncOnConnect(): void {
if (this.resyncProvider) {
- const { ready, sessions } = this.resyncProvider();
+ const { ready, sessions, activities } = this.resyncProvider();
this.sendFrame({ type: "ready", ...ready });
for (const s of sessions) this.sendFrame({ type: "agent_session", ...s });
- this.log.info("resync sent", { ready: ready.runtimeReport.length, sessions: sessions.length });
+ // Re-assert each live agent's current activity: `agent_activity` is
+ // edge-triggered, so a frame dropped during the disconnect window is
+ // otherwise lost forever, stranding the pill on a stale state.
+ for (const a of activities) this.sendFrame({ type: "agent_activity", ...a });
+ this.log.info("resync sent", {
+ ready: ready.runtimeReport.length,
+ sessions: sessions.length,
+ activities: activities.length,
+ });
}
for (const hook of this.resyncHooks) {
try {
diff --git a/src/shared/src/community-cli-contract.ts b/src/shared/src/community-cli-contract.ts
index 728391dbe..4029535d4 100644
--- a/src/shared/src/community-cli-contract.ts
+++ b/src/shared/src/community-cli-contract.ts
@@ -820,11 +820,18 @@ export interface HostControlChannel {
reportBotAuditEvent?(frame: HostBotAuditEventFrame): Promise<void>;
/**
* Register a resync provider invoked on every (re)connect: it returns the
- * host's current `ready` snapshot + live agent sessions, which the channel
- * re-sends so the server can recover this host's state after a drop. Optional
- * so the in-process `LocalControlChannel` (no reconnect) can omit it.
+ * host's current `ready` snapshot + live agent sessions + each live agent's
+ * current derived activity, which the channel re-sends so the server can
+ * recover this host's state after a drop. Activities are replayed because
+ * `agent_activity` is edge-triggered — a frame dropped mid-disconnect is
+ * otherwise lost, stranding the profile pill on a stale state. Optional so the
+ * in-process `LocalControlChannel` (no reconnect) can omit it.
*/
- onResync?(provider: () => { ready: HostReady; sessions: AgentSessionReport[] }): void;
+ onResync?(provider: () => {
+ ready: HostReady;
+ sessions: AgentSessionReport[];
+ activities: Array<{ agentId: AgentId; state: AgentActivityState }>;
+ }): void;
}
/** A live agent session the host replays to the server on (re)connect. */
diff --git a/src/shared/src/community/bot-activity-presets.ts b/src/shared/src/community/bot-activity-presets.ts
index b33383885..d595ad92d 100644
--- a/src/shared/src/community/bot-activity-presets.ts
+++ b/src/shared/src/community/bot-activity-presets.ts
@@ -34,6 +34,31 @@ export const RUNNING_PRESETS: BotActivityPreset[] = [
{ emoji: "🔥", text: "In the zone" },
];
+/**
+ * Every emoji+text pair the WS DO writes when translating an
+ * `AgentActivityState`. Lets a consumer tell "system-driven bot-activity pill
+ * this pipeline wrote" from "human/owner-set custom status" without a
+ * `system_driven` column. Single source of truth so the reconciler's stuck-idle
+ * check and the write path's overwrite guard can never drift from the presets
+ * actually emitted.
+ */
+export const BOT_ACTIVITY_STATUS_PAIRS: ReadonlyArray<BotActivityPreset> = [
+ BOT_ACTIVITY_PRESETS.idle,
+ BOT_ACTIVITY_PRESETS.starting,
+ BOT_ACTIVITY_PRESETS.stopping,
+ ...RUNNING_PRESETS,
+];
+
+/**
+ * True iff (emoji, text) is a known bot-activity preset — i.e. the activity
+ * pipeline owns this pill and may overwrite it. A null pair (no status set) and
+ * any owner-set custom status return false, so both are left untouched.
+ */
+export function isBotActivityStatus(emoji: string | null, text: string | null): boolean {
+ if (emoji === null && text === null) return false;
+ return BOT_ACTIVITY_STATUS_PAIRS.some((p) => p.emoji === emoji && p.text === text);
+}
+
/**
* Deterministic across process restarts and reconnects if the caller passes
* the same `seed`; random per-episode if it doesn't. `ws-do` passes
diff --git a/src/shared/src/db/queries/community/machine.ts b/src/shared/src/db/queries/community/machine.ts
index b14759f1a..8d9e52a5d 100644
--- a/src/shared/src/db/queries/community/machine.ts
+++ b/src/shared/src/db/queries/community/machine.ts
@@ -10,7 +10,7 @@ import {
import { user } from "../../schema";
import { communityUserProfile } from "../../community-schema";
import type { Database } from "../../index";
-import { BOT_ACTIVITY_PRESETS, RUNNING_PRESETS } from "../../../community/bot-activity-presets";
+import { BOT_ACTIVITY_PRESETS, isBotActivityStatus } from "../../../community/bot-activity-presets";
import { COMMUNITY_MACHINE_PAIR_TOKEN_TTL_MS } from "../../../constants";
import { isPresenceOnline } from "../../../utils/status";
import type {
@@ -384,24 +384,6 @@ export async function touchMachineHeartbeat(
// profile`, bots are written here by the WS DO on `agent_activity` frames.
// ---------------------------------------------------------------------------
-/**
- * All emoji+text pairs the WS DO writes when translating an
- * `AgentActivityState` — used by the reconciler below to distinguish
- * "system-driven bot status pill this DO wrote" from "human-driven status
- * pill the owner set" without adding a `system_driven` column.
- */
-const BOT_ACTIVITY_STATUS_PAIRS: ReadonlyArray<{ emoji: string; text: string }> = [
- BOT_ACTIVITY_PRESETS.idle,
- BOT_ACTIVITY_PRESETS.starting,
- BOT_ACTIVITY_PRESETS.stopping,
- ...RUNNING_PRESETS,
-];
-
-function isBotActivityStatus(emoji: string | null, text: string | null): boolean {
- if (emoji === null && text === null) return false;
- return BOT_ACTIVITY_STATUS_PAIRS.some((p) => p.emoji === emoji && p.text === text);
-}
-
/**
* Coarse safety net for `agent_activity` frames dropped mid-disconnect: for
* every bot bound to `machineId`, if the bot's persisted status currently
diff --git a/src/shared/src/index.ts b/src/shared/src/index.ts
index c15833e02..a4db78b31 100644
--- a/src/shared/src/index.ts
+++ b/src/shared/src/index.ts
@@ -461,6 +461,8 @@ export type { WakePayload, BuildUnreadWakeResult } from "./community/wake-dispat
export {
BOT_ACTIVITY_PRESETS,
RUNNING_PRESETS,
+ BOT_ACTIVITY_STATUS_PAIRS,
+ isBotActivityStatus,
pickBotActivityPreset,
} from "./community/bot-activity-presets";
export type { BotActivityPreset } from "./community/bot-activity-presets";
diff --git a/src/shared/test/community/bot-activity-presets.test.ts b/src/shared/test/community/bot-activity-presets.test.ts
new file mode 100644
index 000000000..8080838b9
--- /dev/null
+++ b/src/shared/test/community/bot-activity-presets.test.ts
@@ -0,0 +1,51 @@
+import { describe, it, expect } from "vitest";
+import {
+ BOT_ACTIVITY_PRESETS,
+ RUNNING_PRESETS,
+ BOT_ACTIVITY_STATUS_PAIRS,
+ isBotActivityStatus,
+ pickBotActivityPreset,
+} from "../../src/community/bot-activity-presets";
+
+describe("isBotActivityStatus — the shared write-path / reconciler guard", () => {
+ it("classifies EVERY emitted preset as a bot-activity status (no drift between the guard and what the pipeline writes)", () => {
+ // Guards Blair's over-reach concern: if a preset the pipeline emits isn't
+ // recognized here, the write path would freeze that pill as if it were a
+ // custom status. Every idle/starting/stopping preset and every running
+ // variant must be classified writable.
+ for (const state of ["idle", "starting", "stopping", "running"] as const) {
+ // Sample the whole running pool via deterministic seeds, not just one.
+ const seeds = state === "running" ? RUNNING_PRESETS.map((_, i) => i / RUNNING_PRESETS.length) : [0];
+ for (const seed of seeds) {
+ const p = pickBotActivityPreset(state, seed);
+ expect(isBotActivityStatus(p.emoji, p.text)).toBe(true);
+ }
+ }
+ // And the canonical pair list agrees.
+ for (const p of BOT_ACTIVITY_STATUS_PAIRS) {
+ expect(isBotActivityStatus(p.emoji, p.text)).toBe(true);
+ }
+ });
+
+ it("treats a null pair (no status set) as writable — not a custom status", () => {
+ expect(isBotActivityStatus(null, null)).toBe(false);
+ });
+
+ it("treats an owner-set custom status as NOT a bot-activity status (so the pipeline leaves it alone)", () => {
+ expect(isBotActivityStatus("🎨", "Painting")).toBe(false);
+ // A preset emoji with different text is still custom — both must match.
+ expect(isBotActivityStatus(BOT_ACTIVITY_PRESETS.idle.emoji, "On vacation")).toBe(false);
+ // A preset text with a different emoji is likewise custom.
+ expect(isBotActivityStatus("🏖️", BOT_ACTIVITY_PRESETS.idle.text)).toBe(false);
+ });
+
+ it("the pair list covers exactly the presets, so guard membership can't silently diverge", () => {
+ const expected = [
+ BOT_ACTIVITY_PRESETS.idle,
+ BOT_ACTIVITY_PRESETS.starting,
+ BOT_ACTIVITY_PRESETS.stopping,
+ ...RUNNING_PRESETS,
+ ];
+ expect(BOT_ACTIVITY_STATUS_PAIRS).toEqual(expected);
+ });
+});
diff --git a/src/ws-do/src/ws-durable.test.ts b/src/ws-do/src/ws-durable.test.ts
index db428cdf3..0584814c6 100644
--- a/src/ws-do/src/ws-durable.test.ts
+++ b/src/ws-do/src/ws-durable.test.ts
@@ -273,6 +273,23 @@ vi.mock("@alook/shared", () => {
{ emoji: "🚀", text: "On it" },
{ emoji: "🔥", text: "In the zone" },
],
+ // Real impl of the write-path/reconciler guard — mirrors the shared module
+ // over the presets emitted above (idle/starting/stopping + running pool).
+ isBotActivityStatus: (emoji: string | null, text: string | null) => {
+ if (emoji === null && text === null) return false
+ const pairs = [
+ { emoji: "💤", text: "Idle" },
+ { emoji: "🌀", text: "Waking up" },
+ { emoji: "🌙", text: "Wrapping up" },
+ { emoji: "⚡", text: "Working on it" },
+ { emoji: "🛠️", text: "Cooking" },
+ { emoji: "🧠", text: "Thinking hard" },
+ { emoji: "🔧", text: "Tinkering" },
+ { emoji: "🚀", text: "On it" },
+ { emoji: "🔥", text: "In the zone" },
+ ]
+ return pairs.some((p) => p.emoji === emoji && p.text === text)
+ },
queries: {
session: { getValidSession: (db: unknown, token: string) => mockGetValidSession(db, token) },
machineToken: {
@@ -1348,6 +1365,77 @@ describe("WebSocketDurableObject", () => {
expect(mockStubFetch).not.toHaveBeenCalled()
})
+ it("regression #3 — an owner-set custom status is NOT overwritten by a running frame (write-path guard); no DB write, no fan-out", async () => {
+ const { durable, store } = createDO()
+ store.set("community-machine-identity", {
+ userId: "u_1",
+ machineId: "cm_1",
+ credentialHash: "0".repeat(64),
+ })
+ mockGetBotBinding.mockResolvedValue({ machineId: "cm_1", runtime: "codex" })
+ // Prior status is a custom (non-preset) pair the owner set.
+ mockGetProfile.mockResolvedValue({ statusEmoji: "🎨", statusText: "Painting" })
+
+ const ws = createMockWebSocket()
+ ws.serializeAttachment({ type: "community-machine", machineId: "cm_1", userId: "u_1", authenticated: true })
+
+ const frame = JSON.stringify({ type: "agent_activity", agentId: "bot_1", state: "running" })
+ await durable.webSocketMessage(ws as any, frame)
+
+ // Guard skips the write entirely — custom status survives, even though the
+ // bot is working. This is what stops T6's heartbeat re-stomp every 5s.
+ expect(mockUpdateProfile).not.toHaveBeenCalled()
+ expect(mockStubFetch).not.toHaveBeenCalled()
+ })
+
+ it("regression #4 — a known idle preset still flips to running (guard didn't over-reach)", async () => {
+ const { durable, store } = createDO()
+ store.set("community-machine-identity", {
+ userId: "u_1",
+ machineId: "cm_1",
+ credentialHash: "0".repeat(64),
+ })
+ mockGetBotBinding.mockResolvedValue({ machineId: "cm_1", runtime: "codex" })
+ mockGetCoMemberUserIds.mockResolvedValue(["viewer-1"])
+ // Prior is the idle PRESET (pipeline-owned) — must remain writable.
+ mockGetProfile.mockResolvedValue({ statusEmoji: "💤", statusText: "Idle" })
+
+ const ws = createMockWebSocket()
+ ws.serializeAttachment({ type: "community-machine", machineId: "cm_1", userId: "u_1", authenticated: true })
+
+ const frame = JSON.stringify({ type: "agent_activity", agentId: "bot_1", state: "running" })
+ await durable.webSocketMessage(ws as any, frame)
+
+ expect(mockUpdateProfile).toHaveBeenCalledWith(expect.anything(), "bot_1", {
+ statusEmoji: "⚡",
+ statusText: "Working on it",
+ })
+ })
+
+ it("regression #4 — a known running preset still flips back to idle (guard didn't over-reach)", async () => {
+ const { durable, store } = createDO()
+ store.set("community-machine-identity", {
+ userId: "u_1",
+ machineId: "cm_1",
+ credentialHash: "0".repeat(64),
+ })
+ mockGetBotBinding.mockResolvedValue({ machineId: "cm_1", runtime: "codex" })
+ mockGetCoMemberUserIds.mockResolvedValue(["viewer-1"])
+ // Prior is a running PRESET — an idle frame must be allowed to overwrite it.
+ mockGetProfile.mockResolvedValue({ statusEmoji: "⚡", statusText: "Working on it" })
+
+ const ws = createMockWebSocket()
+ ws.serializeAttachment({ type: "community-machine", machineId: "cm_1", userId: "u_1", authenticated: true })
+
+ const frame = JSON.stringify({ type: "agent_activity", agentId: "bot_1", state: "idle" })
+ await durable.webSocketMessage(ws as any, frame)
+
+ expect(mockUpdateProfile).toHaveBeenCalledWith(expect.anything(), "bot_1", {
+ statusEmoji: "💤",
+ statusText: "Idle",
+ })
+ })
+
it("drops the frame with phase='binding_check' when getBotBinding throws — socket stays open, no write", async () => {
const { durable, store } = createDO()
store.set("community-machine-identity", {
diff --git a/src/ws-do/src/ws-durable.ts b/src/ws-do/src/ws-durable.ts
index 775458e4c..8eeb72179 100644
--- a/src/ws-do/src/ws-durable.ts
+++ b/src/ws-do/src/ws-durable.ts
@@ -14,6 +14,7 @@ import {
HostBotAuditEventFrameSchema,
pickBotActivityPreset,
RUNNING_PRESETS,
+ isBotActivityStatus,
isThread,
isForumPost,
} from "@alook/shared"
@@ -796,6 +797,16 @@ export class WebSocketDurableObject extends DurableObject<Env> {
const prior = await queries.communityUserProfile.getProfile(db, agentId)
const priorEmoji = prior?.statusEmoji ?? null
const priorText = prior?.statusText ?? null
+ // The activity pipeline only owns pills it wrote itself. If the bot
+ // carries an owner-set custom status (a non-preset pair), leave it —
+ // matches the reconciler's declared intent and stops the heartbeat
+ // re-assert from stomping a custom status every interval. A null pair
+ // (no status yet) is still writable.
+ if (
+ (priorEmoji !== null || priorText !== null) &&
+ !isBotActivityStatus(priorEmoji, priorText)
+ )
+ return
const priorIsRunning =
priorEmoji !== null &&
RUNNING_PRESETS.some((p) => p.emoji === priorEmoji && p.text === priorText)