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
feat(browser): @refs are scoped to a node AND a navigation generation
enes4ab260ddf861
diff --git a/src/main/browser-refs.test.ts b/src/main/browser-refs.test.tsnew file mode 100644index 000000000..9a7cf245a--- /dev/null+++ b/src/main/browser-refs.test.ts@@ -0,0 +1,90 @@+import { describe, it, expect } from 'vitest'+import { RefTable, refStaleMessage } from './browser-refs'++const items = () => [+ { ref: 0, tag: 'A', text: 'Home' },+ { ref: 1, tag: 'BUTTON', text: 'Delete account' },+ { ref: 2, tag: 'A', text: 'Next page' }+]++describe('RefTable — @refs are scoped to a node AND a navigation generation', () => {+ it('--read map mints @1…@n bound to (nodeId, gen)', () => {+ const t = new RefTable()+ const { gen, labels } = t.mint('browser-1', 0, items())+ expect(gen).toBe(0)+ expect(labels).toEqual(['@1', '@2', '@3'])+ const r = t.resolve('browser-1', 0, '@2')+ expect(r.ok).toBe(true)+ if (r.ok) expect(r.item).toMatchObject({ tag: 'BUTTON', text: 'Delete account' })+ })++ it('a stale ref is a REFUSAL, not a re-resolution — this is a correctness property first', () => {+ // A stale ref that silently re-resolves is how an agent clicks "Delete account" while meaning+ // "Next page".+ const t = new RefTable()+ t.mint('browser-1', 0, items())+ // Page.frameNavigated bumps the generation and invalidates EVERY ref.+ t.bumpGeneration('browser-1')+ const r = t.resolve('browser-1', 0, '@2')+ expect(r.ok).toBe(false)+ if (!r.ok) {+ expect(r.stale).toBe(true)+ // It is the design's exact sentence, and it never carries a re-resolved item.+ expect(r.message).toBe(+ '@2 is not on the current page (the page navigated since the last --read map; re-read it)'+ )+ expect(r).not.toHaveProperty('item')+ }+ })++ it('Runtime.executionContextsCleared bumps the generation too (same single invalidation path)', () => {+ const t = new RefTable()+ t.mint('browser-1', 0, items())+ // Both Page.frameNavigated and Runtime.executionContextsCleared are wired to bumpGeneration.+ t.bumpGeneration('browser-1')+ t.bumpGeneration('browser-1')+ expect(t.currentGeneration('browser-1')).toBe(2)+ expect(t.resolve('browser-1', 0, '@1').ok).toBe(false)+ })++ it('a bump DROPS the old items, so a ref does not resolve even at the NEW generation', () => {+ // Without clearing, an agent that already knows the new gen could resolve a handle that+ // described the page BEFORE it navigated — the exact "@ref is silently stale" bug.+ const t = new RefTable()+ t.mint('browser-1', 0, items())+ t.bumpGeneration('browser-1') // now on gen 1, no fresh map read yet+ const r = t.resolve('browser-1', 1, '@1')+ expect(r.ok).toBe(false)+ if (!r.ok) expect(r.message).not.toContain('Delete account')+ })++ it('after a fresh --read map at the new generation, its refs resolve again', () => {+ const t = new RefTable()+ t.mint('browser-1', 0, items())+ t.bumpGeneration('browser-1') // now on gen 1+ t.mint('browser-1', 1, items()) // re-read map at gen 1+ expect(t.resolve('browser-1', 1, '@3').ok).toBe(true)+ // The old-generation ref is still refused.+ expect(t.resolve('browser-1', 0, '@3').ok).toBe(false)+ })++ it('the exact stale sentence is exported and reusable', () => {+ expect(refStaleMessage('@7')).toBe(+ '@7 is not on the current page (the page navigated since the last --read map; re-read it)'+ )+ })++ it('a ref from ANOTHER node never resolves', () => {+ const t = new RefTable()+ t.mint('browser-1', 0, items())+ expect(t.resolve('browser-2', 0, '@1').ok).toBe(false)+ })++ it('a label that was never minted at the current generation is refused (not a guess)', () => {+ const t = new RefTable()+ t.mint('browser-1', 0, items())+ const r = t.resolve('browser-1', 0, '@99')+ expect(r.ok).toBe(false)+ if (!r.ok) expect(r.stale).toBe(false) // present generation, but no such ref — not a nav-stale+ })+})diff --git a/src/main/browser-refs.ts b/src/main/browser-refs.tsnew file mode 100644index 000000000..de225f115--- /dev/null+++ b/src/main/browser-refs.ts@@ -0,0 +1,103 @@+/**+ * `@ref` bookkeeping for `--read map` / `--read text`. A ref (`@7`) is a HANDLE the agent later spends+ * on a click or a describe, and its whole safety rests on two scopes:+ * 1. it belongs to ONE node — a ref minted on browser-1 never resolves on browser-2;+ * 2. it belongs to ONE navigation generation — the moment the page navigates, every ref from the+ * previous map is dead.+ *+ * A stale ref is a REFUSAL, never a best-effort re-resolution. A ref that silently re-resolves after a+ * navigation is how an agent clicks "Delete account" while meaning "Next page" — so this is a+ * correctness property before it is a security one. The generation is bumped by BOTH Page.frameNavigated+ * and Runtime.executionContextsCleared (PR 7 wires those events to {@link RefTable.bumpGeneration});+ * both funnel through the one invalidation path so neither can be forgotten independently.+ *+ * Main-side (its inputs come from CDP), never Server Edition.+ */++export type RefItem = Record<string, unknown>++export interface MintResult {+ /** The generation these refs are stamped with (echoes the `gen` passed in). */+ gen: number+ /** `['@1', '@2', …]`, in item order. */+ labels: string[]+}++export type RefResolution =+ | { ok: true; label: string; gen: number; item: RefItem }+ | { ok: false; stale: boolean; message: string }++/** The design's exact sentence for a ref killed by a navigation. Exported so PR 7's reply path and+ * the tests share one string — the wording is a contract (it tells the agent to re-read the map). */+export const refStaleMessage = (label: string): string =>+ `${label} is not on the current page (the page navigated since the last --read map; re-read it)`++/** For a label that is simply not in the current map (out of range, or a node that never read a map).+ * Distinct from stale: nothing navigated, the handle was just never minted here. */+export const refUnknownMessage = (label: string): string =>+ `${label} is not a ref from the last --read map on this page`++interface NodeRefs {+ gen: number+ items: Map<string, RefItem>+}++export class RefTable {+ private readonly byNode = new Map<string, NodeRefs>()++ /** The generation the node is currently on (0 before anything is minted or bumped). */+ currentGeneration(nodeId: string): number {+ return this.byNode.get(nodeId)?.gen ?? 0+ }++ /**+ * Mint `@1…@n` for a fresh `--read map`, stamped with `gen`. A mint REPLACES the node's table (a new+ * read supersedes the old one entirely) and establishes `gen` as the current generation.+ */+ mint(nodeId: string, gen: number, items: readonly RefItem[]): MintResult {+ const map = new Map<string, RefItem>()+ const labels: string[] = []+ for (let i = 0; i < items.length; i++) {+ const label = `@${i + 1}`+ labels.push(label)+ map.set(label, items[i])+ }+ this.byNode.set(nodeId, { gen, items: map })+ return { gen, labels }+ }++ /**+ * Resolve a ref for a node at the generation the agent read it on. Returns the item ONLY when the+ * node's current generation still equals `gen` AND the label was minted then. Anything else is a+ * refusal — a stale refusal (naming the navigation) when the generation moved on, an unknown refusal+ * when the label was never a ref here. NEVER a re-resolution against a different generation's map.+ */+ resolve(nodeId: string, gen: number, label: string): RefResolution {+ const entry = this.byNode.get(nodeId)+ const current = entry?.gen ?? 0+ if (!entry || gen !== current) {+ return { ok: false, stale: true, message: refStaleMessage(label) }+ }+ const item = entry.items.get(label)+ if (!item) {+ return { ok: false, stale: false, message: refUnknownMessage(label) }+ }+ return { ok: true, label, gen: current, item }+ }++ /**+ * The page navigated (or its execution contexts were cleared): advance the generation and drop every+ * ref. The single invalidation path — both CDP events call this, so a ref cannot outlive the page it+ * described.+ */+ bumpGeneration(nodeId: string): number {+ const nextGen = this.currentGeneration(nodeId) + 1+ this.byNode.set(nodeId, { gen: nextGen, items: new Map() })+ return nextGen+ }++ /** Forget a node entirely (its guest is gone / owner released). */+ forget(nodeId: string): void {+ this.byNode.delete(nodeId)+ }+}