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(power): defer the session-boundary edge to origin's, close the SessionStart gap with flush-time sync

enesaed81b02cbf5
diff --git a/src/core/agent-status-mirror.test.ts b/src/core/agent-status-mirror.test.ts
index bbb93e7df..44f2f34bd 100644
--- a/src/core/agent-status-mirror.test.ts
+++ b/src/core/agent-status-mirror.test.ts
@@ -1466,27 +1466,6 @@ describe('onNodeStateChange seam', () => {
expect('pendingId' in c).toBe(false)
}
})
-
- it('a session boundary mid-working fires a not-a-completion end (the exit the sweep cannot see)', () => {
- // SessionStart/SessionEnd reset the entry to idle, which takes it out of the stale sweep's
- // jurisdiction (isStaleWorking wants state === 'working') — so without this edge every
- // edge-driven consumer (Live Activity, keep-awake) would hold a phantom worker forever.
- const seen: NodeStateChange[] = []
- onNodeStateChange((c) => seen.push(c))
- recordAgentEvent(ev({ state: 'working', newTurn: true }))
- const before = _inboxSnapshot().events.length
- recordAgentEvent(ev({ kind: 'session', sessionPhase: 'end' }))
- const ends = seen.filter((c) => c.event === 'end')
- expect(ends).toHaveLength(1)
- expect(ends[0]).toMatchObject({ state: 'done', message: 'Stopped', interrupted: true })
- // Like the sweep: no inbox card — nothing finished, nothing to read.
- expect(_inboxSnapshot().events.length).toBe(before)
- // And it is guarded on prevState === 'working': a session boundary on an idle/done node
- // (the common clean-start case) must stay silent.
- seen.length = 0
- recordAgentEvent(ev({ kind: 'session', sessionPhase: 'start' }))
- expect(seen).toHaveLength(0)
- })
})
describe('onNodeNowChange seam', () => {
diff --git a/src/core/agent-status-mirror.ts b/src/core/agent-status-mirror.ts
index 92e0b93ef..c2def3e9b 100644
--- a/src/core/agent-status-mirror.ts
+++ b/src/core/agent-status-mirror.ts
@@ -1260,21 +1260,6 @@ function produceInboxFromState(
}
const baseEvent = { ts: now, nodeId, agentId: ev.agentId, sessionId: ev.sessionId }
const stateBase = { nodeId, agentId: ev.agentId, sessionId: ev.sessionId, ts: now }
- // A session boundary (SessionStart/SessionEnd) resets a `working` node to idle with no done
- // edge of its own (reduceEntry clears the state) — yet every edge-driven consumer (the phone's
- // Live Activity, keep-awake's power assertion) still believes the node is working, and the
- // stale sweep can never correct them: it only watches entries still marked `working`, and this
- // one just left that set. Fire the same not-a-completion end the sweep fires, so the seam sees
- // every exit. Like the sweep, deliberately NO inbox event — nothing finished.
- if (ev.kind === 'session' && prevState === 'working') {
- fireNodeStateChange({
- ...stateBase,
- event: 'end',
- state: 'done',
- message: 'Stopped',
- interrupted: true
- })
- }
// Working START edge (fresh turn / session open): a Live Activity begins here. reduceEntry's
// done-holdoff means a held-off late working keeps `nextState === 'done'`, so it never reaches
// here — no spurious 'start'.
diff --git a/src/core/keep-awake.test.ts b/src/core/keep-awake.test.ts
index 1f7a9df40..e3dc3a2a4 100644
--- a/src/core/keep-awake.test.ts
+++ b/src/core/keep-awake.test.ts
@@ -125,4 +125,24 @@ describe('keep-awake tracker', () => {
expect(calls.stop).toBe(1)
expect(tracker.holding()).toBe(false)
})
+
+ it('sync() converges to the mirror set — the SessionStart silent-reset story', () => {
+ // A SessionStart mid-working resets the mirror entry to idle WITHOUT an edge (upstream keeps
+ // it silent to protect the new turn's Live Activity), which also disarms the stale sweep.
+ // The flush-time sync is what releases the assertion then (the reset schedules the flush).
+ const { tracker, calls } = harness({ remote: (id) => id.startsWith('remote-') })
+ tracker.onChange(edge('a', 'start', 'working'))
+ expect(tracker.holding()).toBe(true)
+ tracker.sync([]) // the mirror no longer believes anything is working
+ expect(calls.stop).toBe(1)
+ expect(tracker.holding()).toBe(false)
+ // And the inverse: a restored-at-boot working entry seeds a hold with the same gates.
+ tracker.sync(['b', 'remote-1'])
+ expect(calls.start).toBe(2)
+ expect(tracker.holding()).toBe(true)
+ // The remote id was filtered: releasing 'b' alone must release the assertion.
+ tracker.onChange(edge('b', 'end', 'done'))
+ expect(calls.stop).toBe(2)
+ expect(tracker.holding()).toBe(false)
+ })
})
diff --git a/src/core/keep-awake.ts b/src/core/keep-awake.ts
index 245ca9b85..be571054e 100644
--- a/src/core/keep-awake.ts
+++ b/src/core/keep-awake.ts
@@ -13,9 +13,13 @@
// stale sweep (`sweepStaleWorking`, shared/agents/stale.ts WORKING_STALE_MS), which fires a
// synthetic `end` edge per silent node through the SAME onNodeStateChange seam this tracker
// folds — worst case the assertion outlives the last real work by that window, never forever.
-// A session boundary (SessionStart/SessionEnd) mid-`working` is the one exit the sweep cannot
-// see — it resets the mirror entry to idle, out of the sweep's jurisdiction — so the mirror
-// fires the same synthetic end on that path itself (produceInboxFromState's session guard).
+// A session boundary mid-`working` resets the mirror entry to idle, out of the sweep's
+// jurisdiction: a session END fires its own 'Ended' edge (produceInboxFromState's mid-turn
+// branch), while a SessionStart is deliberately silent upstream (ending there would kill the
+// activity of the turn just beginning) — that one gap, and any future silent reset, is closed
+// by `sync()`, which the shell calls on every mirror flush to make the tracker converge to
+// the mirror's own `working` set — and the silent reset is itself an event, so it schedules
+// the very flush (WRITE_DEBOUNCE_MS) that releases the assertion moments later.
//
// Electron-free (src/core): the actual powerSaveBlocker lives behind the injected `blocker` seam
// (template: session-budget.ts); the thin shell in src/main/keep-awake.ts binds it.
@@ -50,6 +54,10 @@ export interface KeepAwakeTracker {
onChange(c: KeepAwakeEdge): void
/** Re-evaluate after a settings change (live toggle). */
refresh(): void
+ /** Converge to the mirror's authoritative `working` node ids (boot seed + every flush).
+ * Covers every reset the edge stream never reports — loadPersisted restores, the SessionStart
+ * silent reset — with the same isRemoteNode/enabled gates as a live edge. */
+ sync(workingNodeIds: string[]): void
/** Currently holding the assertion? (tests + future UI) */
holding(): boolean
/** Release + clear (quit path). */
@@ -99,6 +107,11 @@ export function createKeepAwake(opts: KeepAwakeOpts): KeepAwakeTracker {
refresh(): void {
reconcile()
},
+ sync(workingNodeIds: string[]): void {
+ working.clear()
+ for (const id of workingNodeIds) if (!opts.isRemoteNode(id)) working.add(id)
+ reconcile()
+ },
holding(): boolean {
return held
},
diff --git a/src/main/index.ts b/src/main/index.ts
index 519500402..78fc36834 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -1334,14 +1334,14 @@ app.whenReady().then(async () => {
isRemoteNode: (nodeId) => workspaceStore.sshProjectIdForNode(nodeId) !== undefined
})
onNodeStateChange((c) => keepAwake?.onChange(c))
- // Seed from the restored mirror: an app relaunch (auto-update, crash) does not stop a
- // tmux-backed run, and loadPersisted deliberately restores its `working` entry WITHOUT firing
- // edges — so without this the machine would go unprotected until the run's next turn boundary.
- // The tracker applies the same isRemoteNode/enabled gates as a live edge, and an entry that is
- // in fact gone is released by the stale sweep's synthetic end within its next tick.
- for (const { nodeId } of workingNodes()) {
- keepAwake.onChange({ nodeId, event: 'start', state: 'working' })
- }
+ // Converge to the mirror on two occasions the edge stream cannot cover:
+ // - boot: an app relaunch (auto-update, crash) does not stop a tmux-backed run, and
+ // loadPersisted deliberately restores its `working` entry WITHOUT firing edges;
+ // - every mirror flush: a SessionStart mid-working resets the entry silently (upstream keeps
+ // that edge-free to protect the new turn's Live Activity), which also disarms the stale
+ // sweep — but the reset schedules a flush itself, so this sync releases moments later.
+ keepAwake.sync(workingNodes().map((w) => w.nodeId))
+ onMirrorFlush(() => keepAwake?.sync(workingNodes().map((w) => w.nodeId)))
// Advertise launch settings to the mobile companion through the mirror. The provider is
// consulted at every flush (heartbeat ≤60s), so a settings change propagates without extra
// plumbing. Caps arrive async: re-flush once the memoized probe answers.