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.

fix(grok): route the session-name sweep by agent, through one shared resolver

enesd163ad9e2a3d
diff --git a/src/core/agent-session-name.test.ts b/src/core/agent-session-name.test.ts
new file mode 100644
index 000000000..0efdbc9a2
--- /dev/null
+++ b/src/core/agent-session-name.test.ts
@@ -0,0 +1,39 @@
+// The ROUTER, tested against both real readers — the point being that neither one is ever asked
+// about the other's storage. See the provenance note in grok-session.test.ts for the fixture story.
+import { afterAll, describe, expect, it } from 'vitest'
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs'
+import { tmpdir } from 'os'
+import path from 'path'
+import { readAgentSessionName } from './agent-session-name'
+import { rememberGrokSessionDir, forgetGrokSession } from './grok-session'
+
+const root = mkdtempSync(path.join(tmpdir(), 'agent-session-name-'))
+afterAll(() => rmSync(root, { recursive: true, force: true }))
+
+describe('readAgentSessionName', () => {
+ it("routes grok to grok's session metadata", async () => {
+ const dir = path.join(root, 'gs1')
+ mkdirSync(dir, { recursive: true })
+ writeFileSync(path.join(dir, 'summary.json'), JSON.stringify({ generated_title: 'Grok named it' }))
+ rememberGrokSessionDir('gs1', dir)
+ expect(await readAgentSessionName('gs1', undefined, 'grok')).toBe('Grok named it')
+ // The account id is claude's concept; passing one must not change grok's answer.
+ expect(await readAgentSessionName('gs1', 'acct-2', 'grok')).toBe('Grok named it')
+ forgetGrokSession('gs1')
+ })
+
+ it('sends everything else to the claude transcript reader', async () => {
+ // No transcript exists for this id under any root, so the honest answer is null — the assertion
+ // that matters is that it did NOT come back with the grok session's name below.
+ rememberGrokSessionDir('shared-id', path.join(root, 'gs1'))
+ expect(await readAgentSessionName('shared-id', undefined, 'claude')).toBeNull()
+ expect(await readAgentSessionName('shared-id')).toBeNull()
+ expect(await readAgentSessionName('shared-id', undefined, 'codex')).toBeNull()
+ forgetGrokSession('shared-id')
+ })
+
+ it('answers null for an empty session id without asking either reader', async () => {
+ expect(await readAgentSessionName('', undefined, 'grok')).toBeNull()
+ expect(await readAgentSessionName('')).toBeNull()
+ })
+})
diff --git a/src/core/agent-session-name.ts b/src/core/agent-session-name.ts
new file mode 100644
index 000000000..aa9676336
--- /dev/null
+++ b/src/core/agent-session-name.ts
@@ -0,0 +1,35 @@
+// ONE routing rule for "what is this session's own display name?" — the `/rename` name a node title
+// adopts, per agent.
+//
+// It exists as its own module because the rule has THREE consumers already (the desktop's
+// `IPC.ptyReadSessionName` handler for the mounted node's poll, and the session-name sweep that
+// refreshes the agent-status mirror in BOTH shells), and a routing rule kept in three places is the
+// exact drift this branch has been bitten by twice. Neither reader may ever search the other's tree:
+// claude → a transcript `.jsonl` under `~/.claude/projects` (or a managed account's root)
+// grok → `summary.json` in the session directory a hook told us about (core/grok-session.ts)
+// Anything else has no readable session name; the claude reader answers null for it, which is what
+// every pre-grok caller already got.
+import { readSessionName } from './transcript-reader'
+import { readGrokSessionName } from './grok-session'
+
+/**
+ * The session's display name, or null when it cannot be resolved.
+ *
+ * `agentId` is TRAILING and optional so every pre-grok caller is unchanged — omitted means claude's
+ * transcript reader, the only reader that existed. `accountId` is claude's (managed accounts are
+ * Claude-only), and grok ignores it.
+ *
+ * Routing MATTERS beyond correctness: claude's resolver scans `~/.claude/projects` when its cache
+ * misses, and a grok session id can never be found there — so an unrouted grok node would pay that
+ * scan on every poll, forever, for a guaranteed null.
+ */
+export function readAgentSessionName(
+ sessionId: string,
+ accountId?: string,
+ agentId?: string
+): Promise<string | null> {
+ if (!sessionId) return Promise.resolve(null)
+ return agentId === 'grok'
+ ? readGrokSessionName(sessionId)
+ : readSessionName(sessionId, accountId)
+}
diff --git a/src/core/session-name-sweep.test.ts b/src/core/session-name-sweep.test.ts
index 9c5586358..fb1b1e781 100644
--- a/src/core/session-name-sweep.test.ts
+++ b/src/core/session-name-sweep.test.ts
@@ -7,22 +7,27 @@ function deps(
names?: Record<string, string | null>
nodes?: Record<string, { accountId?: string; titleAuto?: boolean }>
throwFor?: string
+ /** The real `supports` is RENAME_CAPABLE, which now holds grok too. */
+ supportsGrok?: boolean
} = {}
) {
const published: [string, string][] = []
- const resolve = vi.fn(async (sessionId: string) => {
+ const calls: [string, string | undefined, string | undefined][] = []
+ const resolve = vi.fn(async (sessionId: string, accountId?: string, agentId?: string) => {
+ calls.push([sessionId, accountId, agentId])
if (opts.throwFor === sessionId) throw new Error('transient')
return opts.names?.[sessionId] ?? null
})
return {
published,
resolve,
+ calls,
d: {
entries: () => entries,
node: (id: string) => opts.nodes?.[id],
resolve,
publish: (nodeId: string, name: string) => published.push([nodeId, name]),
- supports: (agentId?: string) => agentId === 'claude'
+ supports: (agentId?: string) => agentId === 'claude' || (opts.supportsGrok ? agentId === 'grok' : false)
}
}
}
@@ -72,7 +77,30 @@ describe('sweepSessionNames', () => {
nodes: { n1: { accountId: 'acct-2' } }
})
await sweepSessionNames(d)
- expect(resolve).toHaveBeenCalledWith('s1', 'acct-2')
+ expect(resolve).toHaveBeenCalledWith('s1', 'acct-2', 'claude')
+ })
+
+ it('tells the resolver WHICH AGENT it is resolving', async () => {
+ // The resolver routes per agent (core/agent-session-name.ts): grok's name is in its own session
+ // metadata, claude's in a transcript. Not threading `agentId` does not merely lose grok's name —
+ // it sends every grok node through claude's resolver, which scans ~/.claude/projects for an id
+ // that can never be there, once a minute per node, forever.
+ const { d, calls, published } = deps(
+ [
+ { nodeId: 'g1', sessionId: 'gs1', agentId: 'grok' },
+ { nodeId: 'c1', sessionId: 'cs1', agentId: 'claude' }
+ ],
+ { names: { gs1: 'grok named it', cs1: 'claude named it' }, supportsGrok: true }
+ )
+ expect(await sweepSessionNames(d)).toBe(2)
+ expect(calls).toEqual([
+ ['gs1', undefined, 'grok'],
+ ['cs1', undefined, 'claude']
+ ])
+ expect(published).toEqual([
+ ['g1', 'grok named it'],
+ ['c1', 'claude named it']
+ ])
})
it('one failing read never stops the pass', async () => {
diff --git a/src/core/session-name-sweep.ts b/src/core/session-name-sweep.ts
index 53b59f038..8c8d73ddc 100644
--- a/src/core/session-name-sweep.ts
+++ b/src/core/session-name-sweep.ts
@@ -29,11 +29,15 @@ export interface SessionNameSweepDeps {
/** The node's persisted record — `accountId` scopes the transcript root; `titleAuto === false`
* means the user named this node by hand, so its session name is not what should be shown. */
node: (nodeId: string) => { accountId?: string; titleAuto?: boolean } | undefined
- /** `readSessionName` — resolves the agent's own session name, local or remote. */
- resolve: (sessionId: string, accountId?: string) => Promise<string | null>
+ /** Resolves the agent's own session name, local or remote — `core/agent-session-name.ts`, which
+ * routes per agent (claude reads a transcript, grok its session metadata). `agentId` is trailing
+ * and optional so a resolver that only knows claude still satisfies the type. It is not optional
+ * in PRACTICE: without it a grok entry would be resolved by claude's reader, which scans
+ * `~/.claude/projects` for an id that can never be there — once a minute, per node, forever. */
+ resolve: (sessionId: string, accountId?: string, agentId?: string) => Promise<string | null>
/** Publish a changed name into the mirror. */
publish: (nodeId: string, name: string) => void
- /** Which agents carry a resolvable session name (RENAME_CAPABLE — claude today). */
+ /** Which agents carry a resolvable session name (RENAME_CAPABLE — claude and grok). */
supports: (agentId?: string) => boolean
}
@@ -51,7 +55,7 @@ export async function sweepSessionNames(deps: SessionNameSweepDeps): Promise<num
if (node?.titleAuto === false) continue
let name: string | null = null
try {
- name = await deps.resolve(e.sessionId, node?.accountId)
+ name = await deps.resolve(e.sessionId, node?.accountId, e.agentId)
} catch {
continue // transient read failure — try again next pass
}
diff --git a/src/main/index.ts b/src/main/index.ts
index 69a7c53f3..878c6b4bc 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -1,6 +1,6 @@
import { join, resolve, posix } from 'path'
import { startSessionNameSweep, displayNodeTitle } from '../core/session-name-sweep'
-import { readSessionName as readSessionNameForSweep } from '../core/transcript-reader'
+import { readAgentSessionName } from '../core/agent-session-name'
import { canRename, type AgentId } from '@shared/agents/config'
import { readFile } from 'fs/promises'
import { homedir, hostname } from 'os'
@@ -74,13 +74,8 @@ import { createSubagentTail } from '../core/subagent-tail'
import { createContextTail, type TaskNotification } from '../core/context-tail'
import { grokRawFields, isAsyncSubagentLaunch, type NormalizedAgentEvent } from '../shared/agents/normalize'
import { grokSessionDir, grokSessionsDir } from '../core/agents/grok-paths'
+import { forgetGrokSession, rememberGrokSessionDir } from '../core/grok-session'
import {
- forgetGrokSession,
- readGrokSessionName,
- rememberGrokSessionDir
-} from '../core/grok-session'
-import {
- readSessionName,
setRemoteTranscriptReader,
TITLE_TAIL_BYTES,
SESSION_ID_RE
@@ -552,16 +547,14 @@ app.whenReady().then(async () => {
ptyManager.captureSession(persistKey, full)
)
- // The reader is selected by the NODE's agent, so neither reader ever searches the other's tree:
- // grok keeps its session name in its own metadata (summary.json, in a directory derived from the
- // hook's cwd + sessionId), claude keeps it in a transcript .jsonl. `agentId` is a TRAILING
- // optional argument, so every pre-grok caller resolves through the claude reader unchanged.
+ // The reader is selected by the NODE's agent (core/agent-session-name.ts — the one copy of that
+ // rule, shared with the sweep below and with the Server Edition), so neither reader ever searches
+ // the other's tree. `agentId` is a TRAILING optional argument, so every pre-grok caller resolves
+ // through the claude reader unchanged.
corePlatform.handle(
IPC.ptyReadSessionName,
(sessionId: string, accountId?: string, agentId?: string) =>
- agentId === 'grok'
- ? readGrokSessionName(sessionId ?? '')
- : readSessionName(sessionId ?? '', accountId)
+ readAgentSessionName(sessionId ?? '', accountId, agentId)
)
ipcMain.on(IPC.appCloseWindow, () => BrowserWindow.getFocusedWindow()?.close())
@@ -848,7 +841,10 @@ app.whenReady().then(async () => {
const n = workspaceStore.getNode(nodeId)
return n ? { accountId: n.accountId, titleAuto: n.titleAuto } : undefined
},
- resolve: (sessionId, accountId) => readSessionNameForSweep(sessionId, accountId),
+ // Same router the IPC handler above uses — the sweep sees every RENAME_CAPABLE agent, so
+ // resolving a grok node through claude's reader would scan ~/.claude/projects once a minute
+ // for an id that can never be there.
+ resolve: readAgentSessionName,
publish: setNodeSessionName,
supports: (agentId) => !!agentId && canRename(agentId as AgentId)
})
diff --git a/src/server/index.ts b/src/server/index.ts
index 1b4e5f6bb..becd608e4 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -1,5 +1,5 @@
import fs from 'fs'
-import { readSessionName } from '../core/transcript-reader'
+import { readAgentSessionName } from '../core/agent-session-name'
import { startSessionNameSweep, displayNodeTitle } from '../core/session-name-sweep'
import { canRename, type AgentId } from '@shared/agents/config'
import path from 'path'
@@ -241,7 +241,10 @@ export async function startServer(
const n = workspaceStore.getNode(nodeId)
return n ? { accountId: n.accountId, titleAuto: n.titleAuto } : undefined
},
- resolve: (sessionId, accountId) => readSessionName(sessionId, accountId),
+ // The per-agent router (core/agent-session-name.ts), same as the desktop's sweep and its
+ // ptyReadSessionName handler: a grok node's name is in its session metadata, and resolving it
+ // through claude's reader would scan ~/.claude/projects once a minute for a guaranteed miss.
+ resolve: readAgentSessionName,
publish: setNodeSessionName,
supports: (agentId) => !!agentId && canRename(agentId as AgentId)
})