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(accounts): account-aware transcript resolution

enes6f5794250f49
diff --git a/src/main/claude-accounts-core.test.ts b/src/main/claude-accounts-core.test.ts
index 3acca9ffc..62ba5184c 100644
--- a/src/main/claude-accounts-core.test.ts
+++ b/src/main/claude-accounts-core.test.ts
@@ -5,7 +5,8 @@ import {
AUTH_ENV_STRIP,
accountTmuxEnvArgs,
parseLoginCapture,
- isSupportedClaudeVersion
+ isSupportedClaudeVersion,
+ transcriptRootFor
} from './claude-accounts-core'
describe('accountConfigDir', () => {
@@ -67,6 +68,19 @@ describe('parseLoginCapture', () => {
})
})
+describe('transcriptRootFor', () => {
+ it('defaults to the system ~/.claude/projects when no account', () => {
+ expect(transcriptRootFor('/Users/x', null)).toBe('/Users/x/.claude/projects')
+ expect(transcriptRootFor('/Users/x', '/ud', undefined)).toBe('/Users/x/.claude/projects')
+ })
+ it('uses the account config dir + projects when an account id is given', () => {
+ expect(transcriptRootFor('/Users/x', '/ud', 'a1')).toBe('/ud/claude-accounts/a1/projects')
+ })
+ it('rejects account ids that could traverse out of the root', () => {
+ expect(() => transcriptRootFor('/Users/x', '/ud', '../evil')).toThrow()
+ })
+})
+
describe('isSupportedClaudeVersion', () => {
it('accepts 2.1+ and rejects older', () => {
expect(isSupportedClaudeVersion('2.1.0 (Claude Code)')).toBe(true)
diff --git a/src/main/claude-accounts-core.ts b/src/main/claude-accounts-core.ts
index afe63723d..39369b0e5 100644
--- a/src/main/claude-accounts-core.ts
+++ b/src/main/claude-accounts-core.ts
@@ -11,6 +11,22 @@ export function accountConfigDir(userDataPath: string, accountId: string): strin
return path.join(userDataPath, 'claude-accounts', accountId)
}
+/**
+ * Transcript root for a session lookup: an account's `projects` dir under its config dir, or
+ * the system default `~/.claude/projects` when no account. Pure path math (no fs) — the impure
+ * wrapper in transcript-reader.ts feeds `os.homedir()` / `app.getPath('userData')`. Reuses
+ * `accountConfigDir`'s id validation so a bad account id can never escape the accounts root.
+ */
+export function transcriptRootFor(
+ homeDir: string,
+ userDataPath: string | null,
+ accountId?: string
+): string {
+ return accountId
+ ? path.join(accountConfigDir(userDataPath ?? '', accountId), 'projects')
+ : path.join(homeDir, '.claude', 'projects')
+}
+
/**
* Claude Code ≥ 2.1 scopes its macOS Keychain service name per config dir:
* 'Claude Code-credentials-' + first 8 hex chars of sha256(CLAUDE_CONFIG_DIR).
diff --git a/src/main/handoff/index.ts b/src/main/handoff/index.ts
index b2434da39..d28864723 100644
--- a/src/main/handoff/index.ts
+++ b/src/main/handoff/index.ts
@@ -13,7 +13,7 @@ import { locateClaude, locateCodex, locateGemini } from './locate'
export type HandoffResult = { filePath: string } | { error: string }
type Renderer = (raw: string) => string
-type Locator = (sessionId: string) => Promise<string | undefined>
+type Locator = (sessionId: string, accountId?: string) => Promise<string | undefined>
const RENDERERS: Record<string, Renderer> = {
claude: renderClaudeTranscript,
@@ -40,14 +40,15 @@ export async function buildHandoff(opts: {
agentId: string
sourceNodeId: string
cwd?: string
+ accountId?: string
}): Promise<HandoffResult> {
- const { sessionId, agentId, sourceNodeId, cwd } = opts
+ const { sessionId, agentId, sourceNodeId, cwd, accountId } = opts
if (!sessionId || !SESSION_ID_RE.test(sessionId)) return { error: 'No valid session id to transfer.' }
const render = RENDERERS[agentId]
const locate = LOCATORS[agentId]
if (!render || !locate) return { error: `Transfer is not supported from ${agentId}.` }
- const src = await locate(sessionId)
+ const src = await locate(sessionId, accountId)
if (!src) return { error: "Couldn't find the source conversation transcript." }
let raw: string
diff --git a/src/main/handoff/locate.ts b/src/main/handoff/locate.ts
index 14699e28d..40deb034b 100644
--- a/src/main/handoff/locate.ts
+++ b/src/main/handoff/locate.ts
@@ -6,13 +6,15 @@ import path from 'path'
import { resolveTranscriptPath } from '../transcript-reader'
// claude: ~/.claude/projects/<proj>/<sessionId>.jsonl — already implemented (searches all
-// project dirs for the exact <sessionId>.jsonl).
-export function locateClaude(sessionId: string): Promise<string | undefined> {
- return resolveTranscriptPath(sessionId)
+// project dirs for the exact <sessionId>.jsonl). `accountId` scopes to a managed account's
+// transcript root (default `~/.claude`).
+export function locateClaude(sessionId: string, accountId?: string): Promise<string | undefined> {
+ return resolveTranscriptPath(sessionId, accountId)
}
// codex: ~/.codex/sessions/YYYY/MM/DD/rollout-<ts>-<sessionId>.jsonl — walk the tree and
-// match a .jsonl filename containing the sessionId.
+// match a .jsonl filename containing the sessionId. Managed accounts are Claude-only, so the
+// codex/gemini locators ignore accountId (present only to satisfy the shared Locator type).
export async function locateCodex(sessionId: string): Promise<string | undefined> {
const root = path.join(os.homedir(), '.codex', 'sessions')
const stack = [root]
diff --git a/src/main/index.ts b/src/main/index.ts
index 080857a42..ff0bb2505 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -267,8 +267,8 @@ app.whenReady().then(async () => {
ptyManager.captureSession(persistKey, full)
)
- ipcMain.handle(IPC.ptyReadSessionName, (_e, sessionId: string) =>
- readSessionName(sessionId ?? '')
+ ipcMain.handle(IPC.ptyReadSessionName, (_e, sessionId: string, accountId?: string) =>
+ readSessionName(sessionId ?? '', accountId)
)
ipcMain.on(IPC.appCloseWindow, () => BrowserWindow.getFocusedWindow()?.close())
@@ -531,8 +531,14 @@ app.whenReady().then(async () => {
})
ipcMain.handle(
IPC.handoffBuild,
- (_e, sessionId: string, agentId: string, sourceNodeId: string, cwd: string | undefined) =>
- buildHandoff({ sessionId, agentId, sourceNodeId, cwd })
+ (
+ _e,
+ sessionId: string,
+ agentId: string,
+ sourceNodeId: string,
+ cwd: string | undefined,
+ accountId: string | undefined
+ ) => buildHandoff({ sessionId, agentId, sourceNodeId, cwd, accountId })
)
// Chat nodes: one long-lived Claude Agent SDK query per node, bridged over chat:* IPC.
ipcMain.handle(IPC.chatEnsure, (_e, nodeId: string, opts) => chatDriver.ensure(nodeId, opts))
diff --git a/src/main/transcript-reader.ts b/src/main/transcript-reader.ts
index 9af3f8919..efe333b5d 100644
--- a/src/main/transcript-reader.ts
+++ b/src/main/transcript-reader.ts
@@ -5,6 +5,18 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import type { TranscriptLine, ChatMessage, ChatPart } from '../shared/types'
+import { transcriptRootFor } from './claude-accounts-core'
+
+// Transcript root for a managed account (its `projects` dir) or the system default
+// (`~/.claude/projects` when accountId is undefined — bit-for-bit the old behavior). Impure
+// wrapper over the pure `transcriptRootFor`: `electron.app` is required lazily (and only for the
+// account branch) so this module — and its vitest test — stays electron-free.
+function transcriptRoot(accountId?: string): string {
+ const userData = accountId
+ ? (require('electron') as typeof import('electron')).app.getPath('userData')
+ : null
+ return transcriptRootFor(os.homedir(), userData, accountId)
+}
// Only read the last ~5 MB of a transcript so a very large session can't block the main
// process. The older head is dropped silently (search is most useful on recent context).
@@ -186,10 +198,17 @@ export const SESSION_ID_RE = /^[0-9a-fA-F-]{8,64}$/
// and the renderer polls readSessionName every few seconds PER agent node — without the
// cache each poll re-scanned every project dir under ~/.claude/projects (heavy Claude users
// have hundreds). readSessionName drops the entry if the cached path stops being readable.
+// Cache key includes the account: a session id is globally unique in practice, but keying by
+// id alone would let a wrong-account hit return a still-existing path and silently read the
+// wrong root after an account remove/re-add. Undefined account → `:` prefix (system default).
const transcriptPathCache = new Map<string, string>()
-export async function resolveTranscriptPath(sessionId: string): Promise<string | undefined> {
+export async function resolveTranscriptPath(
+ sessionId: string,
+ accountId?: string
+): Promise<string | undefined> {
if (!SESSION_ID_RE.test(sessionId)) return undefined
- const cached = transcriptPathCache.get(sessionId)
+ const cacheKey = `${accountId ?? ''}:${sessionId}`
+ const cached = transcriptPathCache.get(cacheKey)
if (cached) {
// One cheap access() per hit (vs the O(project-dirs) scan below): heals a stale entry for
// EVERY caller (chat/search/context-ensure too, not just the title poll) if the transcript
@@ -198,10 +217,10 @@ export async function resolveTranscriptPath(sessionId: string): Promise<string |
await fs.promises.access(cached)
return cached
} catch {
- transcriptPathCache.delete(sessionId)
+ transcriptPathCache.delete(cacheKey)
}
}
- const root = path.join(os.homedir(), '.claude', 'projects')
+ const root = transcriptRoot(accountId)
let dirs: string[]
try {
dirs = await fs.promises.readdir(root)
@@ -212,7 +231,7 @@ export async function resolveTranscriptPath(sessionId: string): Promise<string |
const p = path.join(root, d, `${sessionId}.jsonl`)
try {
await fs.promises.access(p)
- transcriptPathCache.set(sessionId, p)
+ transcriptPathCache.set(cacheKey, p)
return p
} catch {
/* keep looking */
@@ -271,10 +290,13 @@ export function pickSessionName(text: string): string | null {
// multiple Claude nodes in one folder would all resolve to the same newest transcript and adopt
// each other's names. Returns null until the node's own sessionId is known.
const TITLE_TAIL_BYTES = 128 * 1024
-export async function readSessionName(sessionId: string): Promise<string | null> {
+export async function readSessionName(
+ sessionId: string,
+ accountId?: string
+): Promise<string | null> {
if (!sessionId) return null
// Stale cache entries are healed inside resolveTranscriptPath (access-checked per hit).
- const p = await resolveTranscriptPath(sessionId)
+ const p = await resolveTranscriptPath(sessionId, accountId)
if (!p) return null
const tail = await readSmallTail(p, TITLE_TAIL_BYTES)
if (!tail) return null
@@ -288,10 +310,13 @@ export async function readSessionName(sessionId: string): Promise<string | null>
// reattaching to a session this app instance didn't spawn. (Encoding leaves no '/', so it
// can't traverse.) Limitation: multiple Claude nodes in the SAME cwd resolve to the same
// newest transcript — the sessionId path above is preferred when known for that reason.
-export async function transcriptPathForCwd(cwd: string): Promise<string | undefined> {
+export async function transcriptPathForCwd(
+ cwd: string,
+ accountId?: string
+): Promise<string | undefined> {
if (!cwd) return undefined
const encoded = cwd.replace(/[/.]/g, '-')
- const dir = path.join(os.homedir(), '.claude', 'projects', encoded)
+ const dir = path.join(transcriptRoot(accountId), encoded)
let entries: string[]
try {
entries = await fs.promises.readdir(dir)
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 6fe31219a..de4c381cb 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -53,7 +53,8 @@ const api: NodeTerminalApi = {
capture: (persistKey, full) => ipcRenderer.invoke(IPC.ptyCapture, persistKey, full),
readScrollback: (persistKey) => ipcRenderer.invoke(IPC.ptyReadScrollback, persistKey),
sendText: (persistKey, text) => ipcRenderer.invoke(IPC.ptySendText, persistKey, text),
- readSessionName: (sessionId) => ipcRenderer.invoke(IPC.ptyReadSessionName, sessionId),
+ readSessionName: (sessionId, accountId) =>
+ ipcRenderer.invoke(IPC.ptyReadSessionName, sessionId, accountId),
onData: (sessionId, listener) => {
const channel = IPC.ptyData(sessionId)
const handler = (_e: unknown, data: string) => listener(data)
@@ -343,8 +344,8 @@ const api: NodeTerminalApi = {
ipcRenderer.invoke(IPC.remoteClientFsWrite, connectionId, path, content)
},
handoff: {
- build: (sessionId, agentId, sourceNodeId, cwd) =>
- ipcRenderer.invoke(IPC.handoffBuild, sessionId, agentId, sourceNodeId, cwd)
+ build: (sessionId, agentId, sourceNodeId, cwd, accountId) =>
+ ipcRenderer.invoke(IPC.handoffBuild, sessionId, agentId, sourceNodeId, cwd, accountId)
},
contextLink: {
setLinks: (map) => ipcRenderer.invoke(IPC.contextLinkSetLinks, map)
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 5a48ed05f..652779b16 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -1840,7 +1840,8 @@ export function Canvas() {
sessionId,
sourceAgentId,
sourceNodeId,
- source.data.cwd
+ source.data.cwd,
+ source.data.accountId
)
if ('error' in res) {
setConfirm({ message: res.error, onConfirm: () => setConfirm(null) })
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index fc93cfbb0..5fe1a29d9 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -637,7 +637,7 @@ export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasN
let timer: ReturnType<typeof setTimeout> | undefined
const sync = async () => {
if (!titleAutoRef.current || editingTitleRef.current) return
- const name = await window.nodeTerminal.pty.readSessionName(sid)
+ const name = await window.nodeTerminal.pty.readSessionName(sid, data.accountId)
if (cancelled) return
if (name) delayMs = 15000
if (
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 4d32e0c93..afa48377e 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -203,8 +203,9 @@ export interface PtyApi {
sendText(persistKey: string, text: string): Promise<boolean>
/** The agent session's display name (`/rename` name, else auto name) read from its transcript,
* resolved strictly by sessionId; null if unknown. Keeps a node title in sync with the
- * `/resume` name (e.g. after resume) without cross-contaminating same-folder sessions. */
- readSessionName(sessionId: string): Promise<string | null>
+ * `/resume` name (e.g. after resume) without cross-contaminating same-folder sessions.
+ * `accountId` scopes the lookup to a managed Claude account's transcript root (default `~/.claude`). */
+ readSessionName(sessionId: string, accountId?: string): Promise<string | null>
/** Listens for PTY output. Returns an unsubscribe function. */
onData(sessionId: string, listener: (data: string) => void): () => void
/** Fires when the PTY process exits. Returns an unsubscribe function. */
@@ -820,7 +821,8 @@ export interface HandoffApi {
sessionId: string,
agentId: string,
sourceNodeId: string,
- cwd: string | undefined
+ cwd: string | undefined,
+ accountId?: string
): Promise<HandoffResult>
}