Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix(daemon): don't leak wedged apm state or heartbeat-mask stall watchdog
Gus0c75fe1fc9a2
diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 8debbeac3..524e117b9 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -7531,6 +7531,9 @@ packages:resolution: {integrity: sha512-61A5ThoTiDG/C8s8UMZwSorAGwMJ0ERVGj2OjoW5pAalsNOg15+iQiPzrLJ4jhZ1HJzmC2PIHT2oEiH3R5fzNA==}+ resolution: {integrity: sha512-yQA4H19AmPEoMUeavPMDIe1higySl/gH/yaQrkT/s07Qp+7pp2hYz30N3z2l5BkjVkF9Ow6o0wjJamm2y7Sn0A==}+resolution: {integrity: sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==}engines: {node: '>=6'}@@ -7672,6 +7675,10 @@ packages:resolution: {integrity: sha512-KBDEIpLrvpv16pp3K0Fw+UCoZfopFjjgeB+0tA/aaThfEE74kKDLrgg603YvOWJyg3+WYtyq3xYsQWsIyZlPqQ==}engines: {node: ^10 || ^12 || >=14}+ resolution: {integrity: sha512-g50586zr4bZmwFiTlflMu8E0bDTb5I5gertgwAKmsdUlTQIhZtunzUlD1WSzwcVWPoAVpsrA6vlfCD7oXvRwgg==}+ engines: {node: ^10 || ^12 || >=14}+resolution: {integrity: sha512-dM0jVuXJPsDN6DvRpea484tCUaMiXWjuCn++HGTqUWzGDjv5tZkEZldAJ/UMlqRYGFrD/etByo4/xOuC/snX2A==}engines: {node: '>=20'}@@ -11534,7 +11541,7 @@ snapshots:commander: 14.0.3ignore: 7.0.6ora: 9.4.1- package-manager-detector: 1.6.0+ package-manager-detector: 1.8.0picocolors: 1.1.1prompts: 2.4.2tinyexec: 1.2.4@@ -12773,7 +12780,7 @@ snapshots:obug: 2.1.3std-env: 4.2.0tinyrainbow: 3.1.0- vitest: 4.1.10(@opentelemetry/[email protected])(@types/[email protected])(@vitest/[email protected])([email protected](@types/[email protected])([email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))+ vitest: 4.1.10(@opentelemetry/[email protected])(@types/[email protected])(@vitest/[email protected])([email protected](@types/[email protected])([email protected]))([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))'@vitest/[email protected]':dependencies:@@ -14214,8 +14221,8 @@ snapshots:'@next/eslint-plugin-next': 16.2.11eslint: 9.39.5([email protected])eslint-import-resolver-node: 0.3.10- eslint-import-resolver-typescript: 3.10.1([email protected])([email protected]([email protected]))+ eslint-import-resolver-typescript: 3.10.1([email protected]([email protected]([email protected])))([email protected]([email protected]))+ eslint-plugin-import: 2.32.0([email protected]([email protected]([email protected]([email protected])))([email protected]([email protected])))([email protected]([email protected]))eslint-plugin-jsx-a11y: 6.10.2([email protected]([email protected]))eslint-plugin-react: 7.37.5([email protected]([email protected]))eslint-plugin-react-hooks: 7.1.1([email protected]([email protected]))@@ -14237,7 +14244,7 @@ snapshots:transitivePeerDependencies:- supports-color+ [email protected]([email protected]([email protected]([email protected])))([email protected]([email protected])):dependencies:'@nolyfill/is-core-module': 1.0.39debug: 4.4.3@@ -14248,21 +14255,21 @@ snapshots:tinyglobby: 0.2.17unrs-resolver: 1.12.2optionalDependencies:+ eslint-plugin-import: 2.32.0([email protected]([email protected]([email protected]([email protected])))([email protected]([email protected])))([email protected]([email protected]))transitivePeerDependencies:- supports-color+ [email protected]([email protected])([email protected]([email protected]([email protected]([email protected])))([email protected]([email protected])))([email protected]([email protected])):dependencies:debug: 3.2.7optionalDependencies:eslint: 9.39.5([email protected])eslint-import-resolver-node: 0.3.10- eslint-import-resolver-typescript: 3.10.1([email protected])([email protected]([email protected]))+ eslint-import-resolver-typescript: 3.10.1([email protected]([email protected]([email protected])))([email protected]([email protected]))transitivePeerDependencies:- supports-color+ [email protected]([email protected]([email protected]([email protected]([email protected])))([email protected]([email protected])))([email protected]([email protected])):dependencies:'@rtsao/scc': 1.1.0array-includes: 3.1.9@@ -14273,7 +14280,7 @@ snapshots:doctrine: 2.1.0eslint: 9.39.5([email protected])eslint-import-resolver-node: 0.3.10- eslint-module-utils: 2.14.0([email protected])([email protected])([email protected]([email protected]))+ eslint-module-utils: 2.14.0([email protected])([email protected]([email protected]([email protected]([email protected])))([email protected]([email protected])))([email protected]([email protected]))hasown: 2.0.4is-core-module: 2.16.2is-glob: 4.0.3@@ -16936,6 +16943,8 @@ snapshots:+ [email protected]: {}+dependencies:callsites: 3.1.0@@ -17061,6 +17070,12 @@ snapshots:picocolors: 1.1.1source-map-js: 1.2.1+ dependencies:+ nanoid: 3.3.16+ picocolors: 1.1.1+ source-map-js: 1.2.1+@@ -18696,7 +18711,7 @@ snapshots:dependencies:lightningcss: 1.33.0picomatch: 4.0.5- postcss: 8.5.22+ postcss: 8.5.23rolldown: 1.0.0-rc.15tinyglobby: 0.2.17optionalDependencies:@@ -18712,7 +18727,7 @@ snapshots:dependencies:lightningcss: 1.33.0picomatch: 4.0.5- postcss: 8.5.22+ postcss: 8.5.23rolldown: 1.0.0-rc.15tinyglobby: 0.2.17optionalDependencies:@@ -18728,7 +18743,7 @@ snapshots:dependencies:lightningcss: 1.33.0picomatch: 4.0.5- postcss: 8.5.22+ postcss: 8.5.23rolldown: 1.0.0-rc.15tinyglobby: 0.2.17optionalDependencies:diff --git a/src/daemon/src/drivers/systemPrompt.ts b/src/daemon/src/drivers/systemPrompt.tsindex 0655461fe..71112c5d0 100644--- a/src/daemon/src/drivers/systemPrompt.ts+++ b/src/daemon/src/drivers/systemPrompt.ts@@ -154,12 +154,16 @@ function messagingSection(): string {"","### Sending & receiving","",+ "You can initiate conversations — send to any channel or DM someone directly. You're not " ++ "limited to replying. Use the same `message send` command whether you're replying or " ++ "starting a conversation.",+ "","- Reply where the message came from. Post results in the channel that owns the topic. " +"When uncertain, check history or DM the relevant people.",`- Short reply: \`${CLI} message send --target <ref> --text "brief reply"\`.`,`- Long or complicated: write body to a tmp file, then \`${CLI} message send --target <ref> --file ./temp_msg.md\`.`,"",- "### Channel refs & addressing",+ "### Channel refs","","Path-style refs:","",@@ -173,14 +177,52 @@ function messagingSection(): string {"| `/.dm/<peer>#N` | Message #N in a DM |","","Use the `channel` field from a received message as `--target`. For an in-thread reply, use " +- "the thread ref (`/<server>/<channel>/#N`). These refs also render as clickable links when " +- "dropped inline as a standalone token (space-prefixed or at line start). " +- "**Don't wrap them in backticks** — that kills the link. Use them to point at channels or " +- "threads instead of describing them.",+ "the thread ref (`/<server>/<channel>/#N`).",+ "",+ "Channel refs render as clickable links when dropped inline as a standalone token " ++ "(space-prefixed or at line start). **Don't wrap them in backticks** — that kills the link.",+ "",+ "Example:",+ "",+ "```bash",+ `${CLI} message send --target \"/.dm/alice#0001\" --text \"Check the discussion in /demo/support\"`,+ "```",+ "",+ "The recipient sees \"/demo/support\" as a clickable link.",+ "",+ "### Mentions",+ "",+ "To mention someone, use `@name#NNNN` format (e.g., `@alice#0001`). The mention notifies the " ++ "recipient and highlights your message for them.",+ "",+ "Example:",+ "",+ "```bash",+ `${CLI} message send --target \"/demo/general\" --text \"@alice#0001 Can you review this?\"`,+ "```",+ "",+ "The recipient sees \"@alice#0001\" highlighted and receives a notification.",+ "",+ "### Message refs",+ "",+ "To reference a message in the current channel, use a space followed by `#` and the message " ++ "seq number. The reference renders as a clickable pill that jumps to that message.",+ "",+ "Format requirements:",+ "- **Must have a space before `#`** (or be at line start)",+ "- Seq number: 1-6 digits",+ "- Channel-scoped: `#42` refers to message seq 42 in the current channel, not globally",+ "",+ "Example:",+ "",+ "```bash",+ `${CLI} message send --target \"/demo/general\" --text \"See my earlier comment in #42\"`,+ "```","",- "### Message shape",+ "In the above, \" #42\" (note the space before #) will render as a clickable pill. " ++ "Without the leading space (like \"issue#42\"), it stays plain text.","",- "Pulled messages:",+ "### Pulled messages","","```json",'{"seq": "#3", "channel": "/demo/general", "sender": "@gustavo#4821", "content": {"text": "hello"}, "time": "2026-06-01T12:00:00Z"}',@@ -221,15 +263,15 @@ function criticalRulesSection(): string {"screenshots, or workspace state are visible to anyone else — they aren't. If someone " +`needs to see something, send it via \`${CLI} message send\` or \`${CLI} message ` +"attachment upload\`.",- "- Never expose tokens, keys, or secrets; redact credential-like strings from tool output " ++ "- **Never expose tokens, keys, or secrets.** Redact credential-like strings from tool output " +"before sharing.",- "- Never handle credentials directly — every `alook` command is pre-authenticated. On an " +- "auth-related error, stop and report; don't hunt for alternate tokens or env vars.",+ "- **Match the sender's language.** When someone writes to you in Chinese, reply in Chinese. " ++ "When they write in English, reply in English. Don't talk past each other.","- **Channel alignment**: you can't send to a channel with unread messages. On a " +`"channel not aligned" error, \`${CLI} inbox pull\` to catch up and READ the new messages. ` +"Judge if your message is still needed or overlaps with what just landed. Adjust or skip; " +"don't mechanically resend.",- "- Finish in-flight work before stopping; don't leave anything half-handled. If a message " ++ "- **Finish in-flight work before stopping.** Don't leave anything half-handled. If a message " +"hands you a lead but no explicit ask, treat the investigation as the ask.",].join("\n");}@@ -298,7 +340,7 @@ function chaosAwarenessSection(): string {function workspaceMemorySection(): string {return [- "## Personality",+ "## Self-awareness","","Your cwd is a persistent workspace. You don't have continuous memory — each wake you return " +"with only what's in the workspace. Yesterday's you is gone; tomorrow's you won't remember " +diff --git a/src/daemon/src/manager/managerPolicy.test.ts b/src/daemon/src/manager/managerPolicy.test.tsindex 597c053d9..90a698902 100644--- a/src/daemon/src/manager/managerPolicy.test.ts+++ b/src/daemon/src/manager/managerPolicy.test.ts@@ -522,4 +522,24 @@ describe("reduceManager — onExit / onSpawned clear resetting", () => {expect(r.state.agents.a.status).toBe("idle");expect(r.state.agents.a.resetting).toBe(false);});++ it("onExit resets apm — a dead process's `compacting` flag doesn't leak into the respawn", () => {+ let s = createInitialManagerState();+ s = register(s, "a", PERSISTENT_GATED);+ s = reduceManager(s, { type: "wake", agentId: "a", message: { text: "m1" }, nowMs: 1 }).state;+ s = reduceManager(s, { type: "spawned", agentId: "a", nowMs: 2 }).state;+ // Enter compaction, then queue mid-turn work.+ s = reduceManager(s, { type: "runtime_signal", agentId: "a", kind: "compaction_started", nowMs: 3 }).state;+ s = reduceManager(s, { type: "wake", agentId: "a", message: { text: "m2" }, nowMs: 4 }).state;+ expect(s.agents.a.apm.compacting).toBe(true);+ expect(s.agents.a.apm.phase).toBe("compacting");+ expect(s.agents.a.inbox.length).toBe(1);++ // Stall-recovery kill → exit. Respawn should start with a fresh apm.+ const r = reduceManager(s, { type: "exit", agentId: "a" });+ expect(r.effects[0]).toMatchObject({ type: "spawn", agentId: "a" });+ expect(r.state.agents.a.apm.compacting).toBe(false);+ expect(r.state.agents.a.apm.phase).toBe("idle");+ expect(r.state.agents.a.apm.outstandingToolUses).toBe(0);+ });});diff --git a/src/daemon/src/manager/managerPolicy.ts b/src/daemon/src/manager/managerPolicy.tsindex 67475e723..91a60d5a6 100644--- a/src/daemon/src/manager/managerPolicy.ts+++ b/src/daemon/src/manager/managerPolicy.ts@@ -441,6 +441,10 @@ function onExit(state: ManagerState, agentId: string): ReduceResult {// top-level clear, the failure path would leave `resetting: true` and// silently reset-lock the agent for every subsequent wake.if (agent.resetting) agent.resetting = false;+ // Fresh process ⇒ fresh gated-steering horizon. Symmetric with `onTurnEnd`;+ // prevents a dead process's `compacting` / outstanding-tool-use flags from+ // silently carrying into the next spawn and re-blocking `onWake`.+ agent.apm = createInitialApmGatedSteeringState();// Per-turn: if more messages queued, immediately respawn for the next batch.if (agent.inbox.length > 0) {diff --git a/src/daemon/src/manager/managerRuntime.test.ts b/src/daemon/src/manager/managerRuntime.test.tsindex 3b83e8b64..2766c718e 100644--- a/src/daemon/src/manager/managerRuntime.test.ts+++ b/src/daemon/src/manager/managerRuntime.test.ts@@ -435,6 +435,46 @@ describe("AgentProcessManager — session race conditions", () => {}});+ it("internal_progress heartbeats do NOT refresh the stall watchdog — a compacting agent that only emits stream_event pings still gets terminated at the threshold", async () => {+ vi.useFakeTimers();+ try {+ let currentTime = 0;+ const logger = stubLogger();+ const { mgr, session } = makeManager({ logger, now: () => currentTime, tickIntervalMs: 5, staleThresholdMs: 100 });+ mgr.start();+ mgr.deliver("a1", { seq: 1, text: "hello" });+ session.startResolver?.();+ await Promise.resolve();+ session.fire("runtime_event", { kind: "session_init", sessionId: "s1" });++ // Simulate Claude going into compaction and emitting only heartbeats.+ // Without the fix these keep bumping lastProgressAt and the stall never+ // fires; with the fix they're ignored for stall purposes.+ currentTime = 50;+ session.fire("runtime_event", { kind: "compaction_started" });+ currentTime = 80;+ session.fire("runtime_event", { kind: "internal_progress", source: "claude_system", itemType: "stream_event" });+ currentTime = 150;+ session.fire("runtime_event", { kind: "internal_progress", source: "claude_system", itemType: "stream_event" });++ // 200ms since spawn (t=0), 120ms since compaction_started's real+ // progress event. Threshold is 100ms → watchdog must fire.+ currentTime = 200;+ await vi.advanceTimersByTimeAsync(10);++ expect(+ logger.calls.info.some(+ ([m, d]) =>+ m === "agent session ended" &&+ (d[0] as any).reason === "terminate_stalled" &&+ (d[0] as any).sessionId === "s1",+ ),+ ).toBe(true);+ } finally {+ vi.useRealTimers();+ }+ });+it("logs info on session ended with reason=stopped from the idle-hibernation tick", async () => {vi.useFakeTimers();try {diff --git a/src/daemon/src/manager/managerRuntime.ts b/src/daemon/src/manager/managerRuntime.tsindex 730864fdb..d1ab27ada 100644--- a/src/daemon/src/manager/managerRuntime.ts+++ b/src/daemon/src/manager/managerRuntime.ts@@ -1075,8 +1075,13 @@ export class AgentProcessManager {if (ev.kind === "text" && typeof ev.text === "string" && ev.text.length > 0) {this.opts.timeline?.appendResponseToLatest(agentId, ev.text);}- // Any event is progress for stall detection.- this.dispatch({ type: "progress", agentId, nowMs: this.now() });+ // Progress for stall detection — but content-free heartbeats+ // (Claude `stream_event` / `status`, Codex raw response items) don't+ // count. They keep firing during a wedged compaction and would mask+ // a real stall from `onTick`'s watchdog.+ if (ev.kind !== "internal_progress") {+ this.dispatch({ type: "progress", agentId, nowMs: this.now() });+ }// Forward every parsed event's kind for gated-steering phase tracking// (tool/compaction/review boundaries). No-ops in the reducer for kinds// it doesn't care about, aside from the diagnostics ring buffer.