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
perf(identity): materialise a node token once, not on every persist
Claude Code94e835ace3a8
diff --git a/src/core/agents/node-token-service.test.ts b/src/core/agents/node-token-service.test.tsindex 603e9b9e8..280aa6849 100644--- a/src/core/agents/node-token-service.test.ts+++ b/src/core/agents/node-token-service.test.ts@@ -137,3 +137,40 @@ describe('node token service — case-folding node id collisions', () => {expect(nodeIdFoldKey('KELVIN')).toBe(nodeIdFoldKey('kelvin')) // U+212A KELVIN SIGN})})++/**+ * `refreshNodeTokens` is wired to `onPersist`, which fires on every canvas load AND save, so a+ * ~375-node workspace paid ~2000 synchronous syscalls on the main thread per save to rewrite bytes+ * that cannot change. The inode is the assertion rather than mtime: the writer is tmp+rename, so a+ * real rewrite always lands a NEW inode (the tmp is allocated while the old file still exists),+ * which no clock resolution can blur.+ */+describe('node token service — writes once, not on every persist', () => {+ const ino = (id: string) => fs.statSync(path.join(nodeTokenDir(), id)).ino++ it('does not touch the file again for an id already materialised this run', async () => {+ hookServer.setNodeAuthSecret(await loadOrCreateNodeAuthSecret())+ initNodeTokens({ canvases: () => canvases })+ const before = canvases.flatMap((c) => c.nodes).map((n) => [n.id, ino(n.id)] as const)+ refreshNodeTokens() // what every save costs+ ensureNodeToken('node-1') // ...and what every spawn costs+ for (const [id, was] of before) expect(ino(id), id).toBe(was)+ })++ it('rewrites anyway under `force` — the boot sweep re-asserts what is on disk', async () => {+ hookServer.setNodeAuthSecret(await loadOrCreateNodeAuthSecret())+ ensureNodeToken('node-1')+ const was = ino('node-1')+ ensureNodeToken('node-1', { force: true })+ expect(ino('node-1')).not.toBe(was)+ })++ it('re-materialises a swept id, so a deleted-then-recreated node is not left on legacy', async () => {+ hookServer.setNodeAuthSecret(await loadOrCreateNodeAuthSecret())+ ensureNodeToken('node-1')+ sweepNodeToken('node-1')+ expect(fs.existsSync(path.join(nodeTokenDir(), 'node-1'))).toBe(false)+ ensureNodeToken('node-1')+ expect(fs.existsSync(path.join(nodeTokenDir(), 'node-1'))).toBe(true)+ })+})diff --git a/src/core/agents/node-token-service.ts b/src/core/agents/node-token-service.tsindex 2809a6478..d6409bd8b 100644--- a/src/core/agents/node-token-service.ts+++ b/src/core/agents/node-token-service.ts@@ -1,6 +1,6 @@import { hookServer } from './hook-server'import { nodeAuthToken, isSafeNodeId } from './node-auth-token'-import { writeNodeTokenFile, sweepNodeTokenFile } from './node-token-files'+import { writeNodeTokenFile, sweepNodeTokenFile, materialisedNodes } from './node-token-files'type Canvases = () => Array<{ nodes: Array<{ id: string }> }>let canvases: Canvases = () => []@@ -75,7 +75,16 @@ function canvasNodeIds(): string[] {return ids}-function materialiseOne(secret: Buffer, nodeId: string): void {+/**+ * `materialisedNodes()` is exactly the right key for the short-circuit: it holds the ids this RUN+ * has written a readable file for, and the token is DERIVED from (secret, nodeId) — neither input+ * can change without a restart — so a second write produces byte-identical content. `force` is the+ * boot sweep's door past it, for a run that wants the files re-asserted regardless.+ *+ * `sweepNodeTokenFile` deletes from that Set, so a deleted-and-recreated node re-materialises.+ */+function materialiseOne(secret: Buffer, nodeId: string, force: boolean): void {+ if (!force && materialisedNodes().has(nodeId)) returnconst token = nodeAuthToken(secret, nodeId)if (token) writeNodeTokenFile(nodeId, token)}@@ -89,10 +98,10 @@ function materialiseOne(secret: Buffer, nodeId: string): void {*/export function initNodeTokens(deps: { canvases: Canvases }): void {canvases = deps.canvases- refreshNodeTokens()+ refreshNodeTokens({ force: true })}-export function ensureNodeToken(nodeId: string): void {+export function ensureNodeToken(nodeId: string, opts?: { force?: boolean }): void {const secret = hookServer.nodeAuthSecretOrNull()if (!secret) return // legacy everywhere, write nothing// The spawn path must apply the same refusal as the sweep, or a hostile pair would simply be@@ -104,14 +113,21 @@ export function ensureNodeToken(nodeId: string): void {for (const id of group) sweepNodeTokenFile(id)return}- materialiseOne(secret, nodeId)+ materialiseOne(secret, nodeId, opts?.force ?? false)}export function sweepNodeToken(nodeId: string): void {sweepNodeTokenFile(nodeId)}-export function refreshNodeTokens(): void {+/**+ * Wired to `onPersist`, which fires on every canvas LOAD and every canvas SAVE. Rewriting every+ * token for every node in every project there cost a ~375-node workspace ~2000 synchronous syscalls+ * on the Electron main thread per save — plus one `console.warn` per node per save when the token+ * dir is unwritable — for a value that is derived and cannot have changed. `materialiseOne` skips+ * anything already on disk this run, so the steady state is a scan and no I/O at all.+ */+export function refreshNodeTokens(opts?: { force?: boolean }): void {const secret = hookServer.nodeAuthSecretOrNull()if (!secret) returnconst ids = canvasNodeIds()@@ -124,6 +140,6 @@ export function refreshNodeTokens(): void {}for (const id of new Set(ids)) {if (colliding.has(id)) continue- materialiseOne(secret, id)+ materialiseOne(secret, id, opts?.force ?? false)}}diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.tsindex f496cb94a..32158c741 100644--- a/src/core/pty-manager.ts+++ b/src/core/pty-manager.ts@@ -3032,9 +3032,15 @@ export class PtyManager {// re-binds a record immediately. Worth stating because the two intents share this line: only// `delete` means "gone for good".forgetCodexThreadIdentitiesForNode(persistKey)- // The node's per-node capability goes with it, same hook, same reason. A recycle re-mints on the- // next cold launch, so sweeping there is harmless.- sweepNodeToken(persistKey)+ // The node's per-node capability goes with it — but ONLY on a delete, unlike the two above.+ // The token is derived from the NODE id, and a recycle keeps the node: the file on disk stays+ // exactly correct across a worktree move, so sweeping it would delete a valid credential and+ // open a window (kill → respawn → first hook event) in which the node cannot prove itself.+ // Under the trust-on-first-proof latch that window is not merely a downgrade to `legacy` — a+ // node that has already proven itself and then presents nothing is refused. Re-minting right+ // after the sweep would close most of it, but it depends on respawn ordering and still leaves a+ // gap; not sweeping leaves none, and there is nothing stale to clean up.+ if (intent === 'delete') sweepNodeToken(persistKey)if (sshRemote) {// Remote (ssh-project) node: end the REMOTE session.const ssh = findSsh()