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.

feat(agents): background-task hook signal + transient stamp

enesf713b91831a5
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index a6e57b170..df94c3e62 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -7109,6 +7109,12 @@ export function Canvas() {
result: e.result
})
break
+ 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`.
+ cs.markBackgroundTask(e.nodeId)
+ break
case 'recurring':
if (e.recurringEnd) {
// The recurring job itself was removed (CronDelete) — take the card down.
diff --git a/src/renderer/state/agentStatus.persist.test.ts b/src/renderer/state/agentStatus.persist.test.ts
index f1ed167c1..9d82df980 100644
--- a/src/renderer/state/agentStatus.persist.test.ts
+++ b/src/renderer/state/agentStatus.persist.test.ts
@@ -189,6 +189,23 @@ describe('loop persistence (cron/schedule survive an app restart)', () => {
expect(saved.n5.state).toBeUndefined()
})
+ it('never persists `backgroundTaskAt` — a stale stamp would exempt a node from Eco forever', async () => {
+ // Same rationale as `lastEventAt`: after a relaunch nothing is running that we know of, and any
+ // turn's `working` would have cleared it anyway.
+ const store = memStorage()
+ vi.stubGlobal('localStorage', store)
+ const { useAgentStatus } = await import('./agentStatus')
+ useAgentStatus.getState().markBackgroundTask('n15')
+ expect(useAgentStatus.getState().byId['n15'].backgroundTaskAt).toBeTypeOf('number')
+ // The stamp alone is not durable, so it writes nothing at all…
+ expect(store.getItem('nodeterm.agentStatus')).toBeNull()
+ // …and it stays out of the file when a durable field does get written.
+ useAgentStatus.getState().setSessionId('n15', 'sess-2')
+ const saved = JSON.parse(store.getItem('nodeterm.agentStatus')!)
+ expect(saved.n15.sessionId).toBe('sess-2')
+ expect(saved.n15.backgroundTaskAt).toBeUndefined()
+ })
+
it('stamps `lastEventAt` on a state transition only (same-state refresh keeps the idle clock)', async () => {
vi.stubGlobal('localStorage', memStorage())
const { useAgentStatus } = await import('./agentStatus')
diff --git a/src/renderer/state/agentStatus.test.ts b/src/renderer/state/agentStatus.test.ts
index b9000d8ac..ef996b601 100644
--- a/src/renderer/state/agentStatus.test.ts
+++ b/src/renderer/state/agentStatus.test.ts
@@ -108,6 +108,29 @@ 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', () => {
+ const id = nid()
+ const s = useAgentStatus.getState()
+ s.setState(id, 'done', 'claude')
+ s.markBackgroundTask(id)
+ expect(useAgentStatus.getState().byId[id]?.backgroundTaskAt).toBeTypeOf('number')
+ // 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()
+ })
+
+ it('stamps a node with no entry yet (the task can precede any state event)', () => {
+ const id = nid()
+ useAgentStatus.getState().markBackgroundTask(id)
+ expect(useAgentStatus.getState().byId[id]?.backgroundTaskAt).toBeTypeOf('number')
+ expect(useAgentStatus.getState().byId[id]?.unread).toBe(false)
+ })
+})
+
describe('clearUnread — cross-surface ack vs. external (host-driven) clear', () => {
it('a normal clear of a done+unread node ACKs the read (dismisses the phone activity)', () => {
const acked: string[] = []
diff --git a/src/renderer/state/agentStatus.ts b/src/renderer/state/agentStatus.ts
index c3ae50575..de63aae87 100644
--- a/src/renderer/state/agentStatus.ts
+++ b/src/renderer/state/agentStatus.ts
@@ -52,6 +52,17 @@ export interface AgentNodeStatus {
* moment the app came back. Absent ⇒ unknown idle ⇒ never a hibernation candidate.
*/
lastEventAt?: number
+ /**
+ * When this node last launched a BACKGROUND shell task (Claude's `Bash` with
+ * `run_in_background: true`). Such a task lives inside the CLI process, so `/exit` — Eco
+ * hibernation and the bulk in-place restart both type it — kills it silently, with no output and
+ * no error. The stamp is what those two exclude on.
+ *
+ * TRANSIENT — never persisted, same rationale as `lastEventAt`: after a relaunch Eco is inert
+ * until a turn happens anyway, and any turn's `working` would have cleared this. A stale stamp
+ * restored from disk would exempt the node from Eco for good.
+ */
+ backgroundTaskAt?: number
/**
* The agent CLI was exited to reclaim its RAM ("Eco" mode) and its conversation is waiting to be
* resumed when the node is next viewed. PERSISTED beside unread/session/sessionId: the tmux
@@ -140,6 +151,9 @@ export interface AgentStatusStore {
/** Record what the pane settled to when this node's CLI let go of it (`null` = forget: a stale
* value must never permit a wake into a pane we did not measure). See `hibernatedPane`. */
setHibernatedPane(id: string, pane: string | null): void
+ /** Record that this node just launched a background shell task (see `backgroundTaskAt`).
+ * Transient — nothing is written to localStorage. */
+ markBackgroundTask(id: string): void
markUnread(id: string): void
/**
* Drop a node's unread flag. By default a clear of a FINISHED (done) node also ACKs the read
@@ -341,6 +355,13 @@ 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
const alive = state === 'working' || state === 'blocked' || state === 'waiting'
if (alive && prev.hibernated) {
next.hibernated = undefined
@@ -423,6 +444,14 @@ export function createAgentStatusSession(
return { byId }
}),
+ markBackgroundTask: (id) =>
+ set((s) => {
+ const prev = s.byId[id] ?? EMPTY
+ // Transient (see `backgroundTaskAt`) — no save(): a stamp restored from disk would exempt
+ // the node from Eco forever.
+ return { byId: { ...s.byId, [id]: { ...prev, backgroundTaskAt: Date.now() } } }
+ }),
+
markUnread: (id) =>
set((s) => {
const prev = s.byId[id] ?? EMPTY
diff --git a/src/shared/agents/normalize.test.ts b/src/shared/agents/normalize.test.ts
index c9fed5e2d..fa56a667f 100644
--- a/src/shared/agents/normalize.test.ts
+++ b/src/shared/agents/normalize.test.ts
@@ -150,6 +150,44 @@ describe('normalizeClaude — recurring (cron/schedule/loop)', () => {
})
})
+describe('normalizeClaude — background shell tasks', () => {
+ // A background shell task lives INSIDE the CLI process, so `/exit` (Eco hibernation, the bulk
+ // restart) kills it silently. This event is the stamp those two exclude on.
+ it('claude PreToolUse Bash with run_in_background=true is a background-task event', () => {
+ const e = normalizeClaude(
+ env({
+ hook_event_name: 'PreToolUse',
+ tool_name: 'Bash',
+ tool_input: { command: 'sleep 999', run_in_background: true }
+ })
+ )
+ expect(e?.kind).toBe('background-task')
+ })
+
+ // 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', () => {
+ for (const payload of [
+ { hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: 'ls' } },
+ {
+ hook_event_name: 'PreToolUse',
+ tool_name: 'Bash',
+ tool_input: { command: 'ls', run_in_background: false }
+ },
+ {
+ hook_event_name: 'PostToolUse',
+ tool_name: 'Bash',
+ tool_input: { command: 'ls', run_in_background: true }
+ },
+ { hook_event_name: 'PreToolUse', tool_name: 'Read', tool_input: { run_in_background: true } }
+ ]) {
+ const e = normalizeClaude(env(payload))
+ expect(e?.kind, JSON.stringify(payload)).toBe('state')
+ expect(e?.state, JSON.stringify(payload)).toBe('working')
+ }
+ })
+})
+
describe('normalizeClaude — permission signals', () => {
it('PermissionRequest → blocked', () => {
const e = normalizeClaude(env({ hook_event_name: 'PermissionRequest' }))
diff --git a/src/shared/agents/normalize.ts b/src/shared/agents/normalize.ts
index b3141d6e1..cf9dd3d03 100644
--- a/src/shared/agents/normalize.ts
+++ b/src/shared/agents/normalize.ts
@@ -7,7 +7,7 @@ export type AgentState = 'working' | 'waiting' | 'blocked' | 'done'
export interface NormalizedAgentEvent {
nodeId: string
agentId: AgentId
- kind: 'state' | 'subagent-start' | 'subagent-end' | 'recurring' | 'session'
+ kind: 'state' | 'subagent-start' | 'subagent-end' | 'recurring' | 'session' | 'background-task'
state?: AgentState
// done only: the turn ended because the user interrupted (Esc/Ctrl-C) — the renderer
// skips the completion alert/unread for these (the user was right there).
@@ -95,6 +95,8 @@ interface ClaudePayload {
prompt?: string
skill?: string
cron?: string
+ /** Bash only: the task was launched as a background shell (`run_in_background: true`). */
+ run_in_background?: boolean
}
tool_response?: {
status?: string
@@ -185,6 +187,14 @@ export function normalizeClaude(env: RawHookEnvelope): NormalizedAgentEvent | nu
}
}
}
+ // A background shell task lives INSIDE the CLI process: /exit kills it silently. This event
+ // is the stamp Eco hibernation and the bulk restart exclude on (see hibernation-policy /
+ // planBulkRestart). PreToolUse only, and `=== true` — an absent or false flag is a foreground
+ // command, which the generic "working" below already covers. Claude-only: no other dialect
+ // carries the field (closed set, CLAUDE.md agent rule 7).
+ if (ev === 'PreToolUse' && tool === 'Bash' && p.tool_input?.run_in_background === true) {
+ return { ...base, kind: 'background-task' }
+ }
// Any other tool use is just "working".
return { ...base, kind: 'state', state: 'working' }
}