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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(status): proof and `restored` move with the state, or not at all
enes4522bbcf6637
diff --git a/src/core/agent-status-mirror.test.ts b/src/core/agent-status-mirror.test.tsindex 7c325a9a9..5191eb9af 100644--- a/src/core/agent-status-mirror.test.ts+++ b/src/core/agent-status-mirror.test.ts@@ -1785,6 +1785,18 @@ describe('reduceEntry records whether the state transition was verified', () =>expect(b.stateVerified).toBe(true)})+ it('a VERIFIED session boundary still drops the proof — idle is not a proven state', () => {+ // The `false` at that call site is explicit, so it needs a case where `ev.verified` is true+ // and the answer is still false; without this, passing `ev.verified === true` there would be+ // an untested equivalent. What a SessionStart proves is that a token holder started a+ // session, not that the node is idle-and-accounted-for, and gate 2 reads `done` anyway.+ const a = reduceEntry(undefined, ev({ state: 'done', verified: true }), 1000)+ const b = reduceEntry(a, ev({ kind: 'session', sessionPhase: 'start', verified: true }), 2000)+ expect(b.state).toBeUndefined()+ expect(b.stateVerified).toBe(false)+ expect(b.verifiedAt).toBe(1000)+ })+it('a session boundary drops the proof with the state it was about', () => {// SessionStart/End reset the node to idle. A `stateVerified: true` left standing beside a// state of `undefined` would assert proof about a state that no longer exists.@@ -1796,6 +1808,43 @@ describe('reduceEntry records whether the state transition was verified', () =>})})+describe('the request_user_input hold carries its own evidence', () => {+ // MEASURED on the first version of this PR: a verified `waiting` at t=1000 followed by a+ // TOKENLESS `done` produced { state: 'waiting', stateVerified: true, updatedAt: 2000 }. The hold+ // was the one branch that wrote `next.state` without co-writing the proof, so the label survived+ // an event that never presented a token — and since /hook/* is fail-open by contract, any caller+ // can replay that event forever, refreshing `updatedAt` so the entry never expires either.+ it('a TOKENLESS done that gets held to waiting un-proves the entry', () => {+ const a = reduceEntry(undefined, ev({ state: 'waiting', awaitingInput: true, verified: true }), 1000)+ expect(a.stateVerified).toBe(true)+ const b = reduceEntry(a, ev({ state: 'done', verified: false }), 2000)+ expect(b.state).toBe('waiting')+ expect(b.stateVerified).toBe(false)+ expect(b.verifiedAt).toBe(1000) // "we once saw proof" is still true+ })++ it('stays false however many times the tokenless event is replayed', () => {+ let e = reduceEntry(undefined, ev({ state: 'waiting', awaitingInput: true, verified: true }), 1000)+ for (let i = 0; i < 98; i++) e = reduceEntry(e, ev({ state: 'done', verified: false }), 2000 + i)+ expect(e.state).toBe('waiting')+ expect(e.stateVerified).toBe(false)+ })++ it('and a VERIFIED held done proves it — the flag tracks the event, not the branch', () => {+ const a = reduceEntry(undefined, ev({ state: 'waiting', awaitingInput: true, verified: false }), 1000)+ const b = reduceEntry(a, ev({ state: 'done', verified: true }), 2000)+ expect(b.state).toBe('waiting')+ expect(b.stateVerified).toBe(true)+ expect(b.verifiedAt).toBe(2000)+ })++ it('carries the client stamp on that edge too', () => {+ const a = reduceEntry(undefined, ev({ state: 'waiting', awaitingInput: true, clientRevision: 3 }), 1000)+ const b = reduceEntry(a, ev({ state: 'done' }), 2000)+ expect(b.clientRevision).toBeUndefined() // the held event carried no stamp — say so+ })+})+describe('a restored entry is never proof', () => {let dir = ''beforeEach(() => {@@ -1826,19 +1875,56 @@ describe('a restored entry is never proof', () => {expect(_snapshot().n1?.stateVerified).toBe(false)})- it('the first live event clears `restored`', () => {+ it('the first live event that COMMITS a state clears `restored`', () => {+ // `newTurn` matters here and the first draft of this test omitted it: at now=5 against a+ // restored `done` stamped 0, a bare `working` is inside the done-holdoff, commits nothing, and+ // therefore — correctly — leaves the entry restored. A genuine UserPromptSubmit is what+ // actually replaces the state that came off disk.const a = { state: 'done', stateVerified: false, restored: true, updatedAt: 0 } as MirrorEntry- expect(reduceEntry(a, ev({ state: 'working', verified: true }), 5).restored).toBeUndefined()+ const b = reduceEntry(a, ev({ state: 'working', newTurn: true, verified: true }), 5)+ expect(b.restored).toBeUndefined()+ expect(b.state).toBe('working')})- it('even an event that changes nothing clears it — the entry is live evidence now', () => {- // A context/usage event is not a state transition, but it IS this run's traffic from that- // node. What `restored` means is "nothing has been heard from this node since boot".- const a = { state: 'done', restored: true, updatedAt: 0 } as MirrorEntry- expect(reduceEntry(a, ev({ kind: 'context' } as never), 5).restored).toBeUndefined()+ // THIS ASSERTION USED TO SAY THE OPPOSITE, and it was wrong in the direction that matters.+ // `restored` means "this entry's STATE came off disk at boot" — that is what its docblock says+ // and what gate 2 will read it as. The first version cleared it on ANY event, so a `context`+ // event left `restored` gone while `state` and `updatedAt` were still the six-hour-old on-disk+ // ones: a future `!restored && state === 'done'` would then admit a stale restored `done` after+ // one tool event. Not exploitable while `stateVerified` is the real input, but a loaded gun.+ it('an event that commits NO state leaves it restored — a context event is not a state', () => {+ const a = { state: 'done', restored: true, updatedAt: 111 } as MirrorEntry+ const b = reduceEntry(a, ev({ kind: 'context' } as never), 999)+ expect(b.restored).toBe(true)+ expect(b.state).toBe('done')+ expect(b.updatedAt).toBe(111) // still the on-disk stamp — nothing about the state is new+ })++ it('a HELD-OFF late working leaves it restored — the state is still the restored one', () => {+ const a = { state: 'done', restored: true, updatedAt: 111 } as MirrorEntry+ expect(reduceEntry(a, ev({ state: 'working' }), 111 + 500).restored).toBe(true)+ })++ it('the idle RESCUE leaves it restored — it returns before committing anything', () => {+ const a = { state: 'done', restored: true, updatedAt: 111 } as MirrorEntry+ expect(reduceEntry(a, ev({ state: 'done', idle: true }), 999).restored).toBe(true)+ })++ it('a session boundary clears it — that DOES commit a state (idle)', () => {+ const a = { state: 'done', restored: true, updatedAt: 111 } as MirrorEntry+ const b = reduceEntry(a, ev({ kind: 'session', sessionPhase: 'start' }), 999)+ expect(b.restored).toBeUndefined()+ expect(b.state).toBeUndefined()+ })++ it('the request_user_input hold clears it — it commits `waiting`', () => {+ const a = { state: 'waiting', awaitingInput: true, restored: true, updatedAt: 111 } as MirrorEntry+ const b = reduceEntry(a, ev({ state: 'done' }), 999)+ expect(b.state).toBe('waiting')+ expect(b.restored).toBeUndefined()})- it('a restored `stateVerified` never survives to disk either', () => {+ it('buildFile\'s allowlist keeps neither field on disk (its sibling above covers the restore)', () => {const file = path.join(dir, 'agent-status.json')fs.writeFileSync(file,diff --git a/src/core/agent-status-mirror.ts b/src/core/agent-status-mirror.tsindex cd5130c83..e93f4855b 100644--- a/src/core/agent-status-mirror.ts+++ b/src/core/agent-status-mirror.ts@@ -347,10 +347,31 @@ export function reduceEntry(now: number): MirrorEntry {const next: MirrorEntry = prev ? { ...prev } : { updatedAt: now }- // ANY event is this run's traffic from this node, so the entry is no longer "restored and- // unheard-from". Done before every branch — including the ones that return early — because what- // the flag means is "nothing has been heard since boot", not "the state is still the restored one".- delete next.restored+ /**+ * Commit a state onto `next` — and everything that must move WITH it. One function rather than+ * the same four lines at each branch, because the alternative was measured: of the three branches+ * that commit a state, the `request_user_input` hold set `next.state` and nothing else. A+ * verified `waiting` therefore kept `stateVerified: true` after a TOKENLESS `done` re-asserted+ * it — replayable indefinitely by any caller, since `/hook/*` is fail-open by contract, with+ * `updatedAt` advancing each time so the entry never even expired. A rule three call sites have+ * to remember is a rule two of them keep.+ *+ * `proof` is the evidence for THIS transition, passed in rather than read off `ev` so the one+ * caller that means something different has to say so out loud.+ */+ const commitState = (state: AgentState | undefined, proof: boolean): void => {+ next.state = state+ next.updatedAt = now+ next.stateVerified = proof+ if (proof) next.verifiedAt = now+ next.clientRevision = ev.clientRevision+ // The entry's STATE now comes from this run, so it is no longer the one restored off disk.+ // Cleared HERE and only here: an event that commits no state — a context/usage event, a+ // held-off late `working`, the idle rescue — leaves a restored `done` exactly as restored as+ // it was. `restored` means "this state came off disk", not "we have heard something since+ // boot", and gate 2 will read it as the former.+ delete next.restored+ }// Identity is captured off ANY event (mirrors the renderer's per-event setSessionId +// agentId threading). agentId is always present on a NormalizedAgentEvent.if (ev.agentId) next.agentId = ev.agentId@@ -370,8 +391,9 @@ export function reduceEntry(if (ev.awaitingInput) {next.awaitingInput = true} else if (prev?.awaitingInput && ev.state === 'done' && !ev.interrupted) {- next.state = 'waiting'- next.updatedAt = now+ // The HELD state is still a state this event committed — the entry says `waiting` because+ // THIS POST arrived — so its evidence is this POST's evidence, not the ask's from before.+ commitState('waiting', ev.verified === true)return next} else {next.awaitingInput = undefined@@ -385,27 +407,20 @@ export function reduceEntry(!ev.newTurn &&prev?.state === 'done' &&now - (prev.updatedAt ?? 0) < DONE_HOLDOFF_MS- if (!heldOff) {- next.state = ev.state- next.updatedAt = now- // Set on the SAME edge the state is set on, and only there: a context/usage event carrying a- // verified flag says nothing about how the current state arrived, and a held-off working did- // not change the state whose proof this describes.- next.stateVerified = ev.verified === true- if (ev.verified === true) next.verifiedAt = now- // Same edge, and ASSIGNED rather than merged: an event with no stamp is a report that this- // node is running a script that cannot send one, which is exactly the state a stale entry- // would hide.- next.clientRevision = ev.clientRevision- }+ // Evidence is written on the SAME edge the state is, and only there: a context/usage event+ // carrying a verified flag says nothing about how the current state arrived, and a held-off+ // working did not change the state whose proof this describes. `clientRevision` is ASSIGNED+ // rather than merged — an event with no stamp is a report that this node is running a script+ // that cannot send one, which is exactly what a stale entry would hide.+ if (!heldOff) commitState(ev.state, ev.verified === true)} else if (ev.kind === 'session') {// SessionStart / SessionEnd both reset the node to idle (renderer: setState(id, undefined)).- next.state = undefined+ // The proof goes with the state it was about, and `false` is passed EXPLICITLY rather than+ // `ev.verified`: this commits idle, and "the idle was verified" is not a claim worth making.+ // `verifiedAt` stays — "this node has proven itself at least once" survives a session boundary+ // and is what makes a refusal retryable.+ commitState(undefined, false)next.awaitingInput = undefined- next.updatedAt = now- // The proof went with the state it was about. `verifiedAt` stays — "this node has proven- // itself at least once" survives a session boundary and is what makes a refusal retryable.- next.stateVerified = false}// subagent-start / subagent-end / recurring: identity captured above, main state untouched.return nextdiff --git a/src/core/agents/hook-verified-parity.test.ts b/src/core/agents/hook-verified-parity.test.tsindex f41f77dd7..c2f080178 100644--- a/src/core/agents/hook-verified-parity.test.ts+++ b/src/core/agents/hook-verified-parity.test.ts@@ -16,6 +16,7 @@ import { mkdtempSync, rmSync, readFileSync } from 'node:fs'import { tmpdir } from 'node:os'import { join, resolve } from 'node:path'import { hookServer } from './hook-server'+import { reduceEntry } from '../agent-status-mirror'import { nodeAuthToken } from './node-auth-token'import { initPlatform, resetPlatformForTests } from '../platform'import { fakePlatform } from '../platform-fake'@@ -137,7 +138,12 @@ describe('both shells register a 4-arg raw listener', () => {const shells = ['src/main/index.ts', 'src/server/agent-status.ts']/** Source with comments removed — a comment that mentions `meta.verified` (both shells have one,- * saying why they do NOT read it) is documentation, not a branch. */+ * saying why they do NOT read it) is documentation, not a branch.+ *+ * KNOWN BLIND SPOT, deliberately not fixed with a hand-rolled lexer: this also truncates at a+ * `//` inside a string literal, so a real branch sharing a line with a URL would be invisible.+ * The cost of missing that is a parity check that passes; the cost of a bespoke JS tokenizer+ * here is a guard nobody trusts. If it ever matters, put the branch on its own line. */const code = (rel: string): string =>readFileSync(join(root, rel), 'utf8').replace(/\/\/.*|\/\*[\s\S]*?\*\//g, '')@@ -162,14 +168,23 @@ describe('both shells register a 4-arg raw listener', () => {it('the src/core mirror is the consumer, on both shells', () => {// The gate reads MirrorEntry.stateVerified. It is only true evidence if the reducer that- // writes it runs wherever hook events land — i.e. if both shells feed the same mirror.- for (const rel of shells.concat(['src/server/index.ts'])) {+ // writes it runs wherever hook events land — i.e. if both shells feed the same mirror. The+ // SHELL ENTRYPOINTS are what must import it; `server/agent-status.ts` is a module the server+ // entrypoint wires up, so it is deliberately not in this list (an earlier version iterated+ // `shells` and then `continue`d past it, which checked two files while reading like three).+ for (const rel of ['src/main/index.ts', 'src/server/index.ts']) {const src = readFileSync(join(root, rel), 'utf8')- if (rel === 'src/server/agent-status.ts') continueexpect(src, `${rel} does not import the status mirror`).toMatch(/agent-status-mirror/)}- const mirror = readFileSync(join(root, 'src/core/agent-status-mirror.ts'), 'utf8')- expect(mirror).toContain('next.stateVerified = ev.verified === true')+ // Asserted by RUNNING the reducer, not by grepping it: the first version of this line matched+ // an exact source string and went red on a pure refactor of the same behaviour, which is a+ // guard that trains people to edit the guard.+ const proven = reduceEntry(+ undefined,+ { kind: 'state', state: 'done', nodeId: 'n1', agentId: 'claude', verified: true } as NormalizedAgentEvent,+ 1+ )+ expect(proven.stateVerified).toBe(true)})for (const rel of shells) {diff --git a/src/core/agents/hooks/managed-script.ts b/src/core/agents/hooks/managed-script.tsindex 4abccc45d..3e5252e00 100644--- a/src/core/agents/hooks/managed-script.ts+++ b/src/core/agents/hooks/managed-script.ts@@ -107,6 +107,10 @@ export function buildManagedScript('# ENDPOINT FILE and therefore reports the SERVER\'s protocol version, never the client\'s.','# Deliberately set HERE, outside nt_pick_fallback\'s clearing block: sock/port/token-dir belong','# to whichever endpoint we adopt, but the revision is a property of this file on this disk.',+ '# (The failover SOURCES the endpoint file it adopts, so a file carrying an nt_client_rev line',+ '# would overwrite this. Not defended against and not worth defending: every candidate is a',+ '# 0600 file under our own $HOME, so writing one already means being us — at which point the',+ '# script itself is editable. Noted so the next reader does not have to re-derive it.)',`nt_client_rev=${MANAGED_SCRIPT_REVISION}`,'# The PER-NODE capability. The endpoint file (v2) advertises the directory; the token itself is','# one file in it named for THIS node id — a lookup by name, never a scan, so a session can only',