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(accounts): an observation whose account was removed degrades to its config dir
freshjuicez8a920879074d
diff --git a/src/renderer/components/AccountChip.test.tsx b/src/renderer/components/AccountChip.test.tsxindex 181eb5130..9c5a22f05 100644--- a/src/renderer/components/AccountChip.test.tsx+++ b/src/renderer/components/AccountChip.test.tsx@@ -144,6 +144,33 @@ describe('AccountChip (D6 visibility through the live stores)', () => {root.unmount()})+ it('flips a linked chip back to unlinked the moment the account is REMOVED', () => {+ // The other half of the smoke test: the store still holds `{known:true, accountId}` from+ // before the unlink. Without the resolver the chip read "Unknown account" and the dir was+ // gone from Settings → Detected, so there was no way back to Link without a new turn.+ const observedLinked: ObservedClaudeAccount = {+ configDir: '/home/me/.claude-2',+ accountId: 'a2',+ known: true+ }+ useAgentStatus.setState({ byId: { n9: { unread: false, account: observedLinked } } })+ const { host, root } = render({ observed: observedLinked })+ expect(chipEl(host)?.textContent).toBe('second')+ expect(chipEl(host)?.className).toContain('node-account-chip--linked')+ act(() => {+ useSettings.setState({+ settings: {+ ...DEFAULT_SETTINGS,+ claudeAccounts: accounts.filter((a) => a.id !== 'a2') // unlinked in Settings+ }+ })+ })+ expect(chipEl(host)?.textContent).toBe('.claude-2')+ expect(chipEl(host)?.className).toContain('node-account-chip--unlinked')+ expect(chipEl(host)?.getAttribute('title')).toContain('Settings → Accounts')+ root.unmount()+ })+it('follows the node’s own account over the observed one', () => {// Launch identity wins: the env is what it is, whatever a later observation says.const { host, root } = render({ accountId: 'a1', observed: CLAUDE_2 })diff --git a/src/renderer/components/AccountChip.tsx b/src/renderer/components/AccountChip.tsxindex 57f041408..7c9a33971 100644--- a/src/renderer/components/AccountChip.tsx+++ b/src/renderer/components/AccountChip.tsx@@ -59,7 +59,7 @@ export function useAccountChip(const multiple = useAgentStatus((s) => hasMultipleAccountKeys(s.byId, dataAccountId, accounts))// The system account's email is looked up lazily and only when something will show it — a node// whose account is entirely unknown renders no chip, so it must not trigger the fetch.- const known = accountKey(dataAccountId, observed) !== null+ const known = accountKey(dataAccountId, observed, accounts) !== nulluseEffect(() => {if (known) useSystemAccount.getState().ensure()}, [known])diff --git a/src/renderer/components/settings/sections/AccountsSection.tsx b/src/renderer/components/settings/sections/AccountsSection.tsxindex bc9314ec4..b9376601d 100644--- a/src/renderer/components/settings/sections/AccountsSection.tsx+++ b/src/renderer/components/settings/sections/AccountsSection.tsx@@ -238,7 +238,7 @@ export function AccountsSection({ isActive }: { isActive: boolean }): React.JSX.const detectedDirs = useAgentStatus((s) =>// NUL-joined, not newline-joined: a path may legally contain a newline, and splitting one back// into two rows would offer the user a dir that does not exist.- unlinkedConfigDirs(s.byId, accounts.map((a) => a.configDir)).join('\u0000')+ unlinkedConfigDirs(s.byId, accounts).join('\u0000'))const detected = detectedDirs ? detectedDirs.split('\u0000') : []diff --git a/src/renderer/lib/accountChip.test.ts b/src/renderer/lib/accountChip.test.tsindex e9b6d2d06..92eb9adcb 100644--- a/src/renderer/lib/accountChip.test.ts+++ b/src/renderer/lib/accountChip.test.ts@@ -40,7 +40,8 @@ describe('effectiveAccountId (D5)', () => {})it('falls back to a KNOWN observed account for a node created without one', () => {- expect(effectiveAccountId(undefined, managed)).toBe('a1')+ // The account list is what keeps a known observation known — see `resolveObserved`.+ expect(effectiveAccountId(undefined, managed, [acct()])).toBe('a1')})it('resolves to nothing for the system account and for an unknown dir', () => {@@ -61,7 +62,7 @@ describe('accountKey (D6)', () => {it('keys a managed/linked account by id, from either source', () => {expect(accountKey('a1', undefined)).toBe('a1')- expect(accountKey(undefined, managed)).toBe('a1')+ expect(accountKey(undefined, managed, [acct()])).toBe('a1')})it('keys an unlinked dir by its path', () => {@@ -104,7 +105,7 @@ describe('hasMultipleAccountKeys (the selector form)', () => {// The store only holds observed accounts, so a managed node that has posted no hook yet is// only visible through its own `data.accountId`.expect(hasMultipleAccountKeys({ n1: { account: system } }, 'a1')).toBe(true)- expect(hasMultipleAccountKeys({ n1: { account: managed } }, 'a1')).toBe(false)+ expect(hasMultipleAccountKeys({ n1: { account: managed } }, 'a1', [acct()])).toBe(false)})})@@ -209,23 +210,25 @@ describe('accountChipFor (D6 visibility, D7 unlinked naming)', () => {describe('unlinkedConfigDirs (Settings → Accounts “Detected config dirs”)', () => {it('lists each unknown dir once, sorted, and never a known one', () => {expect(- unlinkedConfigDirs({- n1: { account: unlinked },- n2: { account: unlinked }, // same dir, two panes- n3: { account: observed({ configDir: '/home/me/.claude-3', known: false }) },- n4: { account: system }, // known: not a candidate- n5: { account: managed },- n6: {}- })+ unlinkedConfigDirs(+ {+ n1: { account: unlinked },+ n2: { account: unlinked }, // same dir, two panes+ n3: { account: observed({ configDir: '/home/me/.claude-3', known: false }) },+ n4: { account: system }, // known: not a candidate+ n5: { account: managed },+ n6: {}+ },+ [acct()] // …and `managed`'s account still exists, so it stays known+ )).toEqual(['/home/me/.claude-2', '/home/me/.claude-3'])})it('drops a dir that is already linked', () => {- expect(unlinkedConfigDirs({ n1: { account: unlinked } }, ['/home/me/.claude-2'])).toEqual([])- // …and tolerates the undefined `configDir` every managed account has.- expect(unlinkedConfigDirs({ n1: { account: unlinked } }, [undefined])).toEqual([- '/home/me/.claude-2'- ])+ const byId = { n1: { account: unlinked } }+ expect(unlinkedConfigDirs(byId, [acct({ configDir: '/home/me/.claude-2' })])).toEqual([])+ // …and tolerates the undefined `configDir` every MANAGED account has.+ expect(unlinkedConfigDirs(byId, [acct()])).toEqual(['/home/me/.claude-2'])})})@@ -248,10 +251,10 @@ describe('resolveObserved (a dir linked since the observation)', () => {expect(resolveObserved(unlinked, [acct()])).toBe(unlinked) // a MANAGED account has no configDir})- it('never downgrades a known observation', () => {- // Core classified these against the managed layout; settings cannot outrank that.+ it('leaves a known observation alone while its account is still listed', () => {+ // Core classified these against the managed layout; a dir match cannot outrank that.expect(resolveObserved(system, [linkedAcct])).toBe(system)- expect(resolveObserved(managed, [linkedAcct])).toBe(managed)+ expect(resolveObserved(managed, [linkedAcct, acct()])).toBe(managed)})})@@ -308,13 +311,15 @@ describe('linking flows through every reader of the observation', () => {it('drops the dir from the detected list the moment it is linked', () => {const byId = { n1: { account: unlinked } }- expect(unlinkedConfigDirs(byId, ['/home/me/.claude-2/'])).toEqual([]) // trailing slash- expect(unlinkedConfigDirs(byId, ['/home/me/.claude-9'])).toEqual(['/home/me/.claude-2'])+ expect(unlinkedConfigDirs(byId, [acct({ configDir: '/home/me/.claude-2/' })])).toEqual([]) // trailing slash+ expect(unlinkedConfigDirs(byId, [acct({ configDir: '/home/me/.claude-9' })])).toEqual([+ '/home/me/.claude-2'+ ])})it('excludes a Windows-shaped linked dir whatever its case', () => {const byId = { n1: { account: observed({ configDir: 'C:\\Users\\Me\\.claude-2', known: false }) } }- expect(unlinkedConfigDirs(byId, ['c:/users/me/.claude-2'])).toEqual([])+ expect(unlinkedConfigDirs(byId, [acct({ configDir: 'c:/users/me/.claude-2' })])).toEqual([])})})@@ -327,3 +332,95 @@ describe('the system chip has nothing to truncate', () => {expect(chip?.tooltip).toContain('System account')})})++// ── Follow-up 2: the account was UNLINKED (or removed) after the observation ───────────────────+// The store still holds `{known:true, accountId}` from before. Left alone it chipped "Unknown+// account" and the dir vanished from Settings → Detected, so there was no way back to Link.+describe('resolveObserved (an account that has since gone away)', () => {+ const observedLinked: ObservedClaudeAccount = {+ configDir: '/home/me/.claude-2',+ accountId: 'lnk',+ known: true+ }++ it('degrades to the bare dir when nothing carries that id any more', () => {+ expect(resolveObserved(observedLinked, [])).toEqual({+ configDir: '/home/me/.claude-2',+ accountId: null,+ known: false+ })+ expect(resolveObserved(observedLinked, [acct({ id: 'other' })])).toEqual({+ configDir: '/home/me/.claude-2',+ accountId: null,+ known: false+ })+ })++ it('keeps the observation while the account is still listed — pending included', () => {+ const live = [acct({ id: 'lnk', configDir: '/home/me/.claude-2' })]+ expect(resolveObserved(observedLinked, live)).toBe(observedLinked)+ // A row still finishing `claude /login` is present, not gone.+ expect(resolveObserved(observedLinked, [acct({ id: 'lnk', pending: true })])).toBe(observedLinked)+ })++ it('re-adopts the dir when it is linked again under a NEW id', () => {+ // Unlink → Link mints a fresh account; the DIR is what carries identity across that gap.+ expect(resolveObserved(observedLinked, [acct({ id: 'lnk2', configDir: '/home/me/.claude-2/' })]))+ .toEqual({ configDir: '/home/me/.claude-2', accountId: 'lnk2', known: true })+ })++ it('leaves a dangling id alone when there is no dir to fall back to', () => {+ // A MANAGED observation whose dir the classifier did not report: nothing better to say.+ const noDir: ObservedClaudeAccount = { configDir: '', accountId: 'gone', known: true }+ expect(resolveObserved(noDir, [])).toBe(noDir)+ })++ it('never touches the system account', () => {+ expect(resolveObserved(system, [])).toBe(system)+ })+})++describe('unlinking flows through every reader of the observation', () => {+ const observedLinked: ObservedClaudeAccount = {+ configDir: '/home/me/.claude-2',+ accountId: 'lnk',+ known: true+ }+ const live = [acct({ id: 'lnk', label: 'second', email: undefined, configDir: '/home/me/.claude-2' })]++ it('falls back to the dashed dir chip instead of "Unknown account"', () => {+ expect(accountChipFor({ observed: observedLinked, accounts: live, multiple: false })).toEqual({+ short: 'second',+ tooltip: 'second',+ kind: 'linked'+ })+ expect(accountChipFor({ observed: observedLinked, accounts: [], multiple: false })).toEqual({+ short: '.claude-2',+ tooltip: 'Unlinked Claude config dir /home/me/.claude-2 — link it in Settings → Accounts',+ kind: 'unlinked'+ })+ })++ it('puts the dir back in the detected list, ready to link again', () => {+ const byId = { n1: { account: observedLinked } }+ expect(unlinkedConfigDirs(byId, live)).toEqual([])+ expect(unlinkedConfigDirs(byId, [])).toEqual(['/home/me/.claude-2'])+ })++ it('keys the pane by path again and gives the readers nothing', () => {+ expect(accountKey(undefined, observedLinked, [])).toBe('ext:/home/me/.claude-2')+ expect(effectiveAccountId(undefined, observedLinked, [])).toBeUndefined()+ // …so its transcripts are read as the system account's, not as a removed account's.+ expect(effectiveAccountId(undefined, observedLinked, live)).toBe('lnk')+ })++ it('leaves a NODE created under a removed account on the legacy Unknown-account chip', () => {+ // `data.accountId` is a different fact — that binding really is dangling, and saying so is+ // correct. Only the OBSERVATION degrades.+ expect(accountChipFor({ dataAccountId: 'lnk', accounts: [], multiple: false })).toEqual({+ short: 'Unknown account',+ tooltip: 'Unknown account',+ kind: 'managed'+ })+ })+})diff --git a/src/renderer/lib/accountChip.ts b/src/renderer/lib/accountChip.tsindex 228e0bac4..3b448682a 100644--- a/src/renderer/lib/accountChip.ts+++ b/src/renderer/lib/accountChip.ts@@ -15,6 +15,12 @@ import { accountChipLabel, systemAccountDisplay } from '../state/workspace'* Everything here is pure and display/read-only. An observed account is a LABEL (see the* `ObservedClaudeAccount` doc comment in shared/types): it may decide what we SHOW and whose* transcripts we READ, never what is permitted — so nothing in this module is a gate.+ *+ * PASS THE LIVE `accounts` LIST AT EVERY CALL SITE. It is optional only so the parameter could be+ * added without touching every caller, and the default `[]` is not "don't resolve" — it means "no+ * accounts exist", which is a real state (the user just unlinked their only one) and makes+ * `resolveObserved` degrade every observed id to its bare dir. Omitting it therefore reads as+ * "every account was removed", which is exactly wrong in the one case it matters.*//** The account key for the system default (`~/.claude`), which has no `ClaudeAccount` record. */@@ -73,20 +79,41 @@ export function configDirsMatch(a: string | undefined | null, b: string | undefi/*** Re-resolve an observation against the CURRENT account list.*- * The hook server classifies `transcript_path` at POST time, so an observation captured before the- * user linked its dir is frozen as `{ known: false }` — and nothing else would ever revisit it: a- * quiet pane may not post another hook for hours. Linking is a settings edit, so the renderer is- * the only place that can notice, and this is where it does. Everything else in this module goes- * through it, so the chip, the keys and the readers cannot disagree about the same dir.+ * The hook server classifies `transcript_path` at POST time, so an observation is frozen as of the+ * account list that existed then — and nothing else would ever revisit it: a quiet pane may not+ * post another hook for hours. Linking and unlinking are settings edits, so the renderer is the+ * only place that can notice, and this is where it does. Everything else in this module goes+ * through it, so the chip, the keys, the detected list and the readers cannot disagree about the+ * same dir.+ *+ * Two directions, and both are about the DIR, which is the one fact that outlives the record:+ * - unknown dir + an account now claiming it ⇒ that account (the link case);+ * - a known id that no longer exists ⇒ back to the bare dir, `known: false` (the unlink+ * or remove case). Without this the pane showed "Unknown account" and its dir vanished from+ * Settings → Detected, so there was no way back to Link short of waiting for the next turn.+ * A re-link under a NEW id is picked up by the same dir match, hence one code path.*- * Only ever UPGRADES an unknown dir to a linked account (never the reverse): a `known` observation- * was classified by core against the managed layout and stays authoritative.+ * The system account (`known` with `accountId: null`) is never touched: it has no record to lose.+ * `data.accountId` is likewise untouched — a NODE created under a removed account still reads+ * "Unknown account", which is correct: that binding really is dangling.*/export function resolveObserved(observed: ObservedClaudeAccount | undefined,accounts: readonly ClaudeAccount[] = []): ObservedClaudeAccount | undefined {- if (!observed || observed.known || !observed.configDir) return observed+ if (!observed) return observed+ if (observed.known) {+ if (!observed.accountId) return observed // the system default — nothing to lose+ if (accounts.some((a) => a.id === observed.accountId)) return observed+ // The id is gone (unlinked/removed since the observation). Fall back to the dir — unless there+ // is none, in which case there is nothing better to say than the id we already have.+ if (!observed.configDir) return observed+ const relinked = accounts.find((a) => configDirsMatch(a.configDir, observed.configDir))+ return relinked+ ? { ...observed, accountId: relinked.id, known: true }+ : { ...observed, accountId: null, known: false }+ }+ if (!observed.configDir) return observedconst match = accounts.find((a) => configDirsMatch(a.configDir, observed.configDir))return match ? { ...observed, accountId: match.id, known: true } : observed}@@ -188,22 +215,21 @@ export function hasMultipleAccountKeys(** Derived from OBSERVATIONS only: a dir gets in here because a session posted a hook from it, so* the list is a record of what actually ran, and nothing here reads the filesystem (a forged POST- * must not make us stat anything). `linkedDirs` is compared through `configDirsMatch` — the SAME- * comparison `resolveObserved` uses, so a dir can never be both "already linked" (hidden from the- * chip) and "detected" (offered for linking).+ * must not make us stat anything).+ *+ * Membership is decided by `resolveObserved` — the SAME resolution the chip uses — so a dir can+ * never be both "belongs to an account" (chipped as linked) and "detected" (offered for linking),+ * in either direction: linking removes it from this list at once, and unlinking puts it back at+ * once, with no hook event in between.*/export function unlinkedConfigDirs(byId: Record<string, { account?: ObservedClaudeAccount }>,- linkedDirs: readonly (string | undefined)[] = []+ accounts: readonly ClaudeAccount[] = []): string[] {- const already = linkedDirs- .map((d) => normalizeConfigDirForCompare(d))- .filter((d) => d.length > 0)const dirs = new Set<string>()for (const entry of Object.values(byId)) {- const a = entry.account+ const a = resolveObserved(entry.account, accounts)if (!a || a.known || !a.configDir) continue- if (already.includes(normalizeConfigDirForCompare(a.configDir))) continuedirs.add(a.configDir)}// Sorted so the list does not reshuffle itself as unrelated nodes come and go.