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.

feat(codex): per-machine system-identity store, once-guarded

Claude Code45a195c5e7b3
diff --git a/src/renderer/state/systemCodexAccount.test.ts b/src/renderer/state/systemCodexAccount.test.ts
new file mode 100644
index 000000000..77de1f0e5
--- /dev/null
+++ b/src/renderer/state/systemCodexAccount.test.ts
@@ -0,0 +1,77 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+const systemIdentity = vi.fn()
+
+/** Fresh store per test — the once-guard is module state, so we re-import after resetModules. */
+async function freshStore() {
+ vi.resetModules()
+ vi.stubGlobal('window', {
+ nodeTerminal: { codexAccounts: { systemIdentity } }
+ })
+ return (await import('./systemCodexAccount')).useSystemCodexAccount
+}
+
+beforeEach(() => {
+ systemIdentity.mockReset()
+})
+afterEach(() => {
+ vi.unstubAllGlobals()
+})
+
+describe('useSystemCodexAccount.ensure', () => {
+ it('reads the local system identity exactly once per session', async () => {
+ systemIdentity.mockResolvedValue({ email: 'me@mac' })
+ const store = await freshStore()
+ store.getState().ensure()
+ store.getState().ensure()
+ await vi.waitFor(() => expect(store.getState().email).toBe('me@mac'))
+ // Once-guard: the second ensure() must not fire a second read.
+ expect(systemIdentity).toHaveBeenCalledTimes(1)
+ expect(systemIdentity).toHaveBeenCalledWith()
+ })
+
+ it('fails closed to null when the read rejects (no fabrication)', async () => {
+ systemIdentity.mockRejectedValue(new Error('no login'))
+ const store = await freshStore()
+ store.getState().ensure()
+ await vi.waitFor(() => expect(store.getState().loaded).toBe(true))
+ expect(store.getState().email).toBeNull()
+ })
+})
+
+describe('useSystemCodexAccount.ensureRemote', () => {
+ it('probes each host once and asks with that host connection', async () => {
+ systemIdentity.mockResolvedValue({ email: 'me@box' })
+ const store = await freshStore()
+ store.getState().ensureRemote('u@box', 'proj-1')
+ store.getState().ensureRemote('u@box', 'proj-1') // in-flight guard
+ await vi.waitFor(() => expect(store.getState().remoteEmails['u@box']).toBe('me@box'))
+ store.getState().ensureRemote('u@box', 'proj-1') // already-resolved guard
+ expect(systemIdentity).toHaveBeenCalledTimes(1)
+ expect(systemIdentity).toHaveBeenCalledWith({ projectId: 'proj-1' })
+ })
+
+ it('records a resolved-but-null host and never re-probes it', async () => {
+ systemIdentity.mockResolvedValue({ email: null })
+ const store = await freshStore()
+ store.getState().ensureRemote('u@box', 'proj-1')
+ await vi.waitFor(() => expect(Object.hasOwn(store.getState().remoteEmails, 'u@box')).toBe(true))
+ expect(store.getState().remoteEmails['u@box']).toBeNull()
+ store.getState().ensureRemote('u@box', 'proj-1')
+ expect(systemIdentity).toHaveBeenCalledTimes(1)
+ })
+
+ it('keeps different hosts in separate slots', async () => {
+ systemIdentity.mockImplementation(async (ctx: { projectId: string }) =>
+ ctx.projectId === 'p-a' ? { email: 'a@box' } : { email: 'b@box' }
+ )
+ const store = await freshStore()
+ store.getState().ensureRemote('a@box', 'p-a')
+ store.getState().ensureRemote('b@box', 'p-b')
+ await vi.waitFor(() => {
+ expect(store.getState().remoteEmails['a@box']).toBe('a@box')
+ expect(store.getState().remoteEmails['b@box']).toBe('b@box')
+ })
+ expect(systemIdentity).toHaveBeenCalledTimes(2)
+ })
+})
diff --git a/src/renderer/state/systemCodexAccount.ts b/src/renderer/state/systemCodexAccount.ts
new file mode 100644
index 000000000..77e2cce81
--- /dev/null
+++ b/src/renderer/state/systemCodexAccount.ts
@@ -0,0 +1,62 @@
+// S6 PR 8 — the discovered identity of the SYSTEM Codex account (`~/.codex`), per machine.
+// Based on @Corvin's #112 (`src/renderer/state/systemCodexAccount.ts`).
+//
+// The system account is implicit (no CodexAccount record), so its email is resolved lazily and
+// once per machine: `ensure()` for this Mac, `ensureRemote(host, projectId)` for a connected SSH
+// host. Every read is fail-closed — no login / unreachable host / read error resolves to `null`,
+// NEVER a fabricated identity (a machine panel with no discoverable system login shows no email
+// rather than borrowing another machine's).
+
+import { create } from 'zustand'
+
+interface SystemCodexAccountState {
+ /** This Mac's system Codex login email, or null (unknown / not logged in). */
+ email: string | null
+ /** Once-guard: the local read fires a single time per app session. */
+ loaded: boolean
+ /** Per-host system login email; a key present with value null means "read, not logged in". */
+ remoteEmails: Record<string, string | null>
+ /** Per-host in-flight guard so a host is probed once even while its read is pending. */
+ remoteLoading: Record<string, boolean>
+ ensure(): void
+ ensureRemote(host: string, projectId: string): void
+}
+
+export const useSystemCodexAccount = create<SystemCodexAccountState>((set, get) => ({
+ email: null,
+ loaded: false,
+ remoteEmails: {},
+ remoteLoading: {},
+ ensure() {
+ if (get().loaded) return
+ set({ loaded: true })
+ void window.nodeTerminal.codexAccounts
+ .systemIdentity()
+ .then((identity) => set({ email: identity?.email ?? null }))
+ .catch(() => {})
+ },
+ ensureRemote(host, projectId) {
+ // Once per host: skip if a read is in flight OR already completed (even to a null email — a
+ // resolved "not logged in" must not re-trigger a probe every render).
+ if (get().remoteLoading[host] || Object.hasOwn(get().remoteEmails, host)) return
+ set((state) => ({
+ remoteLoading: { ...state.remoteLoading, [host]: true }
+ }))
+ void window.nodeTerminal.codexAccounts
+ .systemIdentity({ projectId })
+ .then((identity) =>
+ set((state) => ({
+ remoteEmails: {
+ ...state.remoteEmails,
+ [host]: identity?.email ?? null
+ },
+ remoteLoading: { ...state.remoteLoading, [host]: false }
+ }))
+ )
+ .catch(() =>
+ set((state) => ({
+ remoteLoading: { ...state.remoteLoading, [host]: false }
+ }))
+ )
+ }
+}))