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 on a turn START only

enes7539ccdb3c6a
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index df94c3e62..1ab959f0e 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -7112,7 +7112,8 @@ export function Canvas() {
case 'background-task':
// A background shell task runs INSIDE the CLI process, so the `/exit` Eco hibernation
// and the bulk restart type would kill it silently. Stamp the node so both skip it.
- // Cheap: the write touches one entry and nothing subscribes to `backgroundTaskAt`.
+ // The write mints a new entry object, so whole-map subscribers (minimap, the node) do
+ // re-render — once per background launch, which is rarer than any state event.
cs.markBackgroundTask(e.nodeId)
break
case 'recurring':
diff --git a/src/renderer/state/agentStatus.test.ts b/src/renderer/state/agentStatus.test.ts
index ef996b601..ffbbfa271 100644
--- a/src/renderer/state/agentStatus.test.ts
+++ b/src/renderer/state/agentStatus.test.ts
@@ -109,7 +109,7 @@ describe('interrupt inference (Esc/Ctrl-C with no final hook)', () => {
})
describe('background-task stamp (Eco / bulk-restart guard)', () => {
- it('markBackgroundTask stamps; only a transition TO working clears it', () => {
+ it('markBackgroundTask stamps, and a non-working transition never clears it', () => {
const id = nid()
const s = useAgentStatus.getState()
s.setState(id, 'done', 'claude')
@@ -118,9 +118,45 @@ describe('background-task stamp (Eco / bulk-restart guard)', () => {
// done → waiting is a transition, but the task is still running: the guard must hold.
s.setState(id, 'waiting', 'claude')
expect(useAgentStatus.getState().byId[id]?.backgroundTaskAt).toBeTypeOf('number')
- // A turn START is what clears it — the completed task's <task-notification> precedes it.
- s.setState(id, 'working', 'claude')
- expect(useAgentStatus.getState().byId[id]?.backgroundTaskAt).toBeUndefined()
+ })
+
+ // 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', () => {
+ 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 }
+ ]) {
+ const id = nid()
+ const s = useAgentStatus.getState()
+ if (from) s.setState(id, from, 'claude')
+ s.markBackgroundTask(id)
+ // A `done` predecessor needs the holdoff to lapse, or the working event is dropped whole.
+ if (from === 'done') vi.advanceTimersByTime(DONE_HOLDOFF_MS + 500)
+ useAgentStatus.getState().setState(id, 'working', 'claude')
+ const label = String(from)
+ expect(useAgentStatus.getState().byId[id].state, label).toBe('working')
+ const stamp = useAgentStatus.getState().byId[id].backgroundTaskAt
+ if (cleared) expect(stamp, label).toBeUndefined()
+ else expect(stamp, label).toBeTypeOf('number')
+ }
+ })
+
+ // The scenario the predicate exists for: a background Bash whose command needs approval runs
+ // UserPromptSubmit(working) → PreToolUse(stamp) → PermissionRequest(blocked) → approve →
+ // PostToolUse(working). That last edge IS a transition, and clearing on it would drop the guard
+ // milliseconds after the stamp was set, while the task runs on.
+ it('a background task that needed approval keeps its stamp across the approve edge', () => {
+ const id = nid()
+ const s = useAgentStatus.getState()
+ s.setState(id, 'working', 'claude', true) // UserPromptSubmit — the turn starts
+ s.markBackgroundTask(id) // PreToolUse Bash, run_in_background
+ s.setState(id, 'blocked', 'claude') // PermissionRequest
+ s.setState(id, 'working', 'claude') // approved, the turn resumes
+ expect(useAgentStatus.getState().byId[id].backgroundTaskAt).toBeTypeOf('number')
})
it('stamps a node with no entry yet (the task can precede any state event)', () => {
diff --git a/src/renderer/state/agentStatus.ts b/src/renderer/state/agentStatus.ts
index de63aae87..7ab521115 100644
--- a/src/renderer/state/agentStatus.ts
+++ b/src/renderer/state/agentStatus.ts
@@ -355,13 +355,25 @@ 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, not when the node
- // goes idle: Claude delivers a finished background task back as a <task-notification>,
- // whose own turn is exactly the `working` below — so by the time a turn begins, whatever
- // the task was doing has been reported. Clearing on `done` instead would drop the guard
- // the instant the launching turn ended, i.e. while the task is still running, which is
- // precisely the window Eco / the bulk restart would kill it in.
- if (state === 'working') next.backgroundTaskAt = undefined
+ // 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
+ // 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.
+ //
+ // 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
+ }
const alive = state === 'working' || state === 'blocked' || state === 'waiting'
if (alive && prev.hibernated) {
next.hibernated = undefined
diff --git a/src/shared/agents/normalize.test.ts b/src/shared/agents/normalize.test.ts
index fa56a667f..3585e122f 100644
--- a/src/shared/agents/normalize.test.ts
+++ b/src/shared/agents/normalize.test.ts
@@ -165,8 +165,10 @@ describe('normalizeClaude — background shell tasks', () => {
})
// The mutation guard: dropping the `tool_name` check, the `ev` check, or matching truthily
- // instead of `=== true` each flips exactly one of these rows.
- it('foreground Bash, false/absent flag, PostToolUse, and other tools stay generic working', () => {
+ // instead of `=== true` each flips exactly one of these rows. The truthy-but-not-`true` row is
+ // what pins the strict compare — the two boolean rows below are both falsy, so on their own they
+ // would pass a `!!p.tool_input?.run_in_background` implementation too.
+ it('foreground Bash, false/absent/truthy-non-true flags, PostToolUse and other tools stay generic working', () => {
for (const payload of [
{ hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: 'ls' } },
{
@@ -174,6 +176,17 @@ describe('normalizeClaude — background shell tasks', () => {
tool_name: 'Bash',
tool_input: { command: 'ls', run_in_background: false }
},
+ {
+ hook_event_name: 'PreToolUse',
+ tool_name: 'Bash',
+ // A hand-rolled/forwarded payload could carry 1 or "true"; only a real boolean counts.
+ tool_input: { command: 'ls', run_in_background: 1 }
+ },
+ {
+ hook_event_name: 'PreToolUse',
+ tool_name: 'Bash',
+ tool_input: { command: 'ls', run_in_background: 'true' }
+ },
{
hook_event_name: 'PostToolUse',
tool_name: 'Bash',