Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(agents): clear the background-task stamp only from `done`

enes79ea85b24f06
diff --git a/docs/superpowers/plans/2026-08-10-ram-optimization.md b/docs/superpowers/plans/2026-08-10-ram-optimization.md
index 6140352f5..e8bc88ed0 100644
--- a/docs/superpowers/plans/2026-08-10-ram-optimization.md
+++ b/docs/superpowers/plans/2026-08-10-ram-optimization.md
@@ -856,6 +856,7 @@ describe('planHibernation', () => {
5. Kanban card modal co-attached while hibernation fires (modal viewer sees the exit; acceptable? decide + document).
6. Managed-account node (CLAUDE_CONFIG_DIR) resumes into the right account dir.
7. A `/loop` node (ScheduleWakeup) and a `/cron` node stay untouched through several sweeps while eligible-looking (done + offscreen + idle) — the `recurring` guard holds end-to-end, and their next scheduled wakeup fires on time.
+8. A node with a running **background shell task** (`Bash` with `run_in_background: true`) is not hibernated by an enabled Eco sweep; after the task completes and its turn runs, hibernation happens one idle window later. (Background-task guard — `docs/superpowers/specs/2026-08-12-background-task-hibernation-guard-design.md`.)
---
diff --git a/docs/superpowers/specs/2026-08-12-background-task-hibernation-guard-design.md b/docs/superpowers/specs/2026-08-12-background-task-hibernation-guard-design.md
index 42eed838e..c80f42a82 100644
--- a/docs/superpowers/specs/2026-08-12-background-task-hibernation-guard-design.md
+++ b/docs/superpowers/specs/2026-08-12-background-task-hibernation-guard-design.md
@@ -27,7 +27,7 @@ Both shells get this for free: normalize is shared, and the raw listeners forwar
Transient `backgroundTaskAt?: number` per node. **Not persisted** — same rationale as `lastEventAt`: after an app relaunch Eco is structurally inert until the node takes a turn, so a persisted stamp would only assert staleness with confidence.
- **Set:** Canvas's `agent:status` listener stamps `Date.now()` on a `background-task` event (a dedicated store action in house style, bail-when-unchanged not needed — re-stamping on every launch is correct).
-- **Clear:** in `setState`'s transition branch, only at a turn **START** (`done`/`undefined` → `working`; `blocked`/`waiting` → `working` is a mid-turn resumption and KEEPS the stamp). Rationale: a background task's completion queues a `<task-notification>` into the parent transcript, which re-invokes the agent — a turn starts — `working` fires. Clearing on `done` would be wrong: a turn can end while the task still runs. And clearing on *every* `working` transition would be wrong too: a background `Bash` whose command needs approval runs UserPromptSubmit(working) → PreToolUse(stamp) → PermissionRequest(blocked) → approve → PostToolUse(working), so the resumption edge would clear the stamp milliseconds after it was set, for exactly the task this guard exists for.
+- **Clear:** in `setState`'s transition branch, only at a turn **START** — `done` → `working`, and nothing else. `blocked`/`waiting` → `working` is a mid-turn resumption and KEEPS the stamp; so does `undefined` → `working`, because an unknown previous state is reachable mid-turn (a renderer reload starts with an empty table; `sweepStaleWorking` blanks a working entry) and is therefore no evidence of a turn start. Rationale: a background task's completion queues a `<task-notification>` into the parent transcript, which re-invokes the agent — a turn starts — `working` fires. Clearing on `done` would be wrong: a turn can end while the task still runs. And clearing on *every* `working` transition would be wrong too: a background `Bash` whose command needs approval runs UserPromptSubmit(working) → PreToolUse(stamp) → PermissionRequest(blocked) → approve → PostToolUse(working), so the resumption edge would clear the stamp milliseconds after it was set, for exactly the task this guard exists for.
### 3. Consumers
@@ -40,14 +40,15 @@ Transient `backgroundTaskAt?: number` per node. **Not persisted** — same ratio
- **App relaunch:** stamp is gone (transient) — consistent, because Eco cannot act until a turn happens, and any turn's `working` would have cleared the stamp anyway.
- **A task reporting back while the node sits `waiting`/`blocked`:** the clear needs a turn START, so a `<task-notification>` that lands while the session holds a question open (`waiting`/`blocked` → `working` is a resumption) leaves the stamp in place until the turn after that. Bounded and in the fail-safe direction — and such a node is never a hibernation candidate anyway, since `planHibernation` requires `state === 'done'`.
- **Manual task kill without a turn:** the stamp stays until the next turn — the node simply isn't hibernated. Fail-safe direction (memory not reclaimed; work never killed).
+- **A stamp set while the node's state is unknown** (renderer reload, or `sweepStaleWorking` having blanked the entry): since only `done` → `working` clears, the completing `<task-notification>` turn is a resumption from `undefined` and does NOT clear — the stamp survives until the turn AFTER that, i.e. the node's hibernation is deferred by one more turn. Accepted: the cost is memory reclaimed one turn late, whereas clearing on `undefined` would delete the guard for a task that is still running.
### 5. Testing
- Normalize: a `PreToolUse` Bash payload with `run_in_background: true` yields the event; without the field (or with `false`, or another tool) yields nothing — mutation-sensitive both ways.
-- Store: stamp set on event; cleared on transition to `working`; NOT cleared on `done`/`waiting`; never persisted (extend the existing persistence tests).
+- Store: stamp set on event; cleared on a turn start (`done` → `working`); NOT cleared on `done`/`waiting`, on a `blocked`/`waiting` resumption, or from an unknown state; never persisted (extend the existing persistence tests).
- Adapter/policy: `liveBackgroundTask` row in `hibernationCandidates` + exclusion row in `planHibernation` (the oldest-node-surfaces mutation pattern already used there).
- Bulk plan: a background-task candidate lands in `skipped.working` and not in `runnable`.
-- Device checklist (append to PR #130's list as item 9): a node with a running background shell is not hibernated by an enabled Eco sweep; after the task completes and its turn runs, hibernation happens one idle window later.
+- Device checklist: appended as **item 8** to the tracked Eco list in `docs/superpowers/plans/2026-08-10-ram-optimization.md` (Phase 5, which has 7 items) — a node with a running background shell is not hibernated by an enabled Eco sweep; after the task completes and its turn runs, hibernation happens one idle window later.
### 6. Surfaces
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 7ab99e6cb..27dc8739f 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -7197,6 +7197,9 @@ export function Canvas() {
nodes: nodesRef.current
.filter((n) => n.type === 'terminal')
.map((n) => ({ id: n.id, agentId: createdAgentId(n.data) })),
+ // Pass the store entries WHOLE: every optional field here (backgroundTaskAt,
+ // lastEventAt, loop) is read by the policy; narrowing this to a hand-picked literal
+ // would silently kill those guards with the suite still green.
statusById: useAgentStatus.getState().byId,
// Any card that has not finished pins its parent — see the adapter's header.
subagents: Object.values(useAgentNodes.getState().byId).map((v) => ({
diff --git a/src/renderer/state/agentStatus.test.ts b/src/renderer/state/agentStatus.test.ts
index ffbbfa271..d1fa9b0b4 100644
--- a/src/renderer/state/agentStatus.test.ts
+++ b/src/renderer/state/agentStatus.test.ts
@@ -120,15 +120,17 @@ describe('background-task stamp (Eco / bulk-restart guard)', () => {
expect(useAgentStatus.getState().byId[id]?.backgroundTaskAt).toBeTypeOf('number')
})
- // Only a turn START clears the stamp. A `working` arriving from blocked/waiting is a MID-TURN
- // RESUMPTION — the same turn picking back up — and clearing there would drop the guard for
- // exactly the task it exists for (see the approval walk-through below).
- it('clears only on a turn start (from done/idle), never on a mid-turn resumption', () => {
+ // Only a turn START — a `working` arriving from `done` — clears the stamp. Everything else keeps
+ // it. blocked/waiting → working is a MID-TURN RESUMPTION (the same turn picking back up; see the
+ // approval walk-through below), and an UNKNOWN previous state is not evidence of a turn start
+ // at all: it is what a renderer reload or the stale-working sweeper leaves behind MID-TURN, so
+ // clearing there would delete the stamp for a task that is still running.
+ it('clears only on a turn start (done → working), never on a resumption or from an unknown state', () => {
for (const { from, cleared } of [
{ from: 'blocked' as const, cleared: false },
{ from: 'waiting' as const, cleared: false },
- { from: 'done' as const, cleared: true },
- { from: undefined, cleared: true }
+ { from: undefined, cleared: false },
+ { from: 'done' as const, cleared: true }
]) {
const id = nid()
const s = useAgentStatus.getState()
diff --git a/src/renderer/state/agentStatus.ts b/src/renderer/state/agentStatus.ts
index 7ab521115..6be1636ca 100644
--- a/src/renderer/state/agentStatus.ts
+++ b/src/renderer/state/agentStatus.ts
@@ -355,25 +355,34 @@ export function createAgentStatusSession(
// the sweep) exempts that session from Eco for good.
// `done` deliberately does NOT clear it — a hibernated node's last known state IS done,
// and a late Stop POST arriving after the exit would undo the hibernation we just did.
- // A background task's guard is dropped at the START OF THE NEXT TURN — and only there.
//
- // Not on `done`: that is the launching turn ending while the task runs on, which is
+ // ---- a different field, and the opposite rule ----
+ //
+ // The BACKGROUND-TASK guard is dropped at the START OF THE NEXT TURN — `done` → `working`,
+ // and nothing else.
+ //
+ // Not on `done` itself: that is the launching turn ending while the task runs on, which is
// precisely the window Eco / the bulk restart would kill it in. A turn start is safe
// because Claude delivers a finished background task back as a <task-notification>, whose
// own turn is exactly this `working` — so by the time one begins, the task has reported.
//
- // And not on EVERY `working` transition, because `blocked`/`waiting` → `working` is a
- // MID-TURN RESUMPTION, not a turn start. A background Bash whose command needs approval
- // runs UserPromptSubmit(working) → PreToolUse(stamp) → PermissionRequest(blocked) →
- // approve → PostToolUse(working): that last edge would clear the stamp milliseconds after
- // it was set, for exactly the task this guard exists for. A turn start comes from `done`
- // or from no known state at all.
+ // Not on EVERY `working` transition either, because `blocked`/`waiting` → `working` is a
+ // MID-TURN RESUMPTION. A background Bash whose command needs approval runs
+ // UserPromptSubmit(working) → PreToolUse(stamp) → PermissionRequest(blocked) → approve →
+ // PostToolUse(working): that last edge would clear the stamp milliseconds after it was
+ // set, for exactly the task this guard exists for.
+ //
+ // And NOT from an unknown previous state, which is the same hole from the other side:
+ // `undefined` is reachable MID-TURN — a renderer reload starts with an empty table, and
+ // `sweepStaleWorking` blanks a working entry after the stale window — so post-reload a
+ // background launch would stamp an entry with no state, and the very next tool event's
+ // `working` would read as a turn start and delete it. Requiring `done` makes the miss
+ // fail SAFE: every real turn ends Stop → `done`, so the clear still happens, at most one
+ // turn late.
//
// Deliberately NOT keyed on `newTurn`: the <task-notification> prompt is explicitly not
// flagged as one (see normalizeClaude), so the intended clear would never fire.
- if (state === 'working' && (prev.state === 'done' || prev.state === undefined)) {
- next.backgroundTaskAt = undefined
- }
+ if (state === 'working' && prev.state === 'done') next.backgroundTaskAt = undefined
const alive = state === 'working' || state === 'blocked' || state === 'waiting'
if (alive && prev.hibernated) {
next.hibernated = undefined