diff --git a/CHANGELOG.md b/CHANGELOG.md index e6732a06..bb252d14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ Anything that changes what the dashboard, the admin or a widget can do gets a li ### Added - **GitHub rows open on the Mac.** Open a saved run, notification or pull request from a widget without marking it read. +- **cmux session navigation.** A Claude card selects its cmux panel through the bundled CLI when the running process identifies both workspace and surface and socket access allows it. Otherwise it brings cmux forward. - **Local Docker and AI models.** Registry widgets can show up to 200 local containers and observed Ollama or MLX models. Fremkit refuses remote Docker contexts, distinguishes a missing CLI from a stopped daemon, and keeps the container list when stats fail. - **The Uptime tile shows time since boot.** The reading comes from macOS and remains unavailable when the system cannot provide it. - **Borrow the Edge display.** Hide or show the dashboard from the helper menu without quitting it. Hidden state survives display rearrangements, and the mouse fence pauses while the Edge is borrowed. Touch and data collection continue; the dashboard reappears after a helper restart. diff --git a/docs/claude-code.md b/docs/claude-code.md index 9ffb5325..8e373407 100644 --- a/docs/claude-code.md +++ b/docs/claude-code.md @@ -41,10 +41,10 @@ Only the tool name and a short summary reach Fremkit. Tool inputs and outputs ar never stored. The hook also adds a `client` object saying where the session lives: the terminal application's -bundle id, `TERM_PROGRAM`, the pid of the `claude` process, its tty, and — under Orca — the pane -and tab it runs in. It is the only way anything can know: the server otherwise sees a session id -and a working directory, and two sessions in two panes of the same folder are the same pair of -strings. It is what makes the ↗ on a card work. +bundle id, `TERM_PROGRAM`, the pid of the `claude` process, its tty, and the Orca pane +handle when present. For cmux, the server reads workspace and surface ids from the live +process. The server otherwise sees only a session id and a working directory, which cannot +distinguish two panes of the same folder. These details make the ↗ on a card work. That object stays on the server. The dashboard is told which *kind* of application it is and what to call it (`Orca`, `Terminal`, `iTerm2`, `VS Code`), never the pane key or the tty. @@ -61,10 +61,17 @@ corner. What happens depends on the application: | Client | What is raised | |---|---| | Orca | the exact pane, through `orca terminal switch --terminal ` | +| cmux | the exact panel through the bundled `cmux focus-panel` CLI when the socket permits external clients; otherwise the application | | Terminal.app, iTerm2 | the tab whose tty matches, through AppleScript — macOS asks once to allow it | | VS Code | the window already open on that folder | | anything else | the application, by its bundle id | +The helper starts Fremkit outside cmux. With cmux socket control set to `cmuxOnly`, the +`focus-panel` command is denied and touching a card brings cmux forward without selecting +its panel. Exact panel selection requires a socket mode that allows the helper's process, +such as `allowAll`; that mode expands local socket access. Keep `cmuxOnly` if you prefer the +restricted mode and use the application fallback. + A session whose hook never said where it lives — one Fremkit found by scanning processes, or one from before this existed — shows no arrow at all. diff --git a/server/src/claude/focus.ts b/server/src/claude/focus.ts index 3d21e898..5a72fd38 100644 --- a/server/src/claude/focus.ts +++ b/server/src/claude/focus.ts @@ -51,12 +51,15 @@ export async function clientOfProcess(pid: number, reader: Reader = read): Promi const bundleId = pick('__CFBundleIdentifier') const program = pick('TERM_PROGRAM') const terminal = pick('ORCA_TERMINAL_HANDLE') + const workspace = pick('CMUX_WORKSPACE_ID') + const surface = pick('CMUX_SURFACE_ID') const tty = await ttyOfProcess(pid, reader) - if (!bundleId && !program && !terminal) return undefined + if (!bundleId && !program && !terminal && !workspace) return undefined return { ...(bundleId && BUNDLE_RE.test(bundleId) ? { bundleId } : {}), ...(program ? { program } : {}), pid, + ...(workspace && surface && UUID_RE.test(workspace) && UUID_RE.test(surface) ? { cmux: { workspace, surface } } : {}), ...(tty ? { tty } : {}), ...(terminal && ORCA_TERMINAL_RE.test(terminal) ? { orca: { terminal } } : {}), } @@ -100,6 +103,9 @@ export async function appOfProcess(pid: number, reader: Reader = read): Promise< const ORCA_CLI = '/Applications/Orca.app/Contents/Resources/bin/orca' const ORCA_BUNDLE = 'com.stablyai.orca' const VSCODE_BUNDLE = 'com.microsoft.VSCode' +const CMUX_BUNDLE = 'com.cmuxterm.app' +const CMUX_CLI = '/Applications/cmux.app/Contents/Resources/bin/cmux' +const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i /** A bundle id, as `open -b` will take it. */ const BUNDLE_RE = /^[A-Za-z0-9.-]{1,120}$/ @@ -184,11 +190,44 @@ export async function focusClient( reader: Reader = read, ): Promise { try { + // Hooks written before cmux support already identify the app. Read the live process once + // to recover its panel and reuse its client details for the no-hook fallback below. + const livePid = pid ?? client?.pid + let live: SessionClient | undefined + if (livePid && Number.isSafeInteger(livePid) && livePid > 1 && + (!client?.bundleId || client.bundleId.toLowerCase() === CMUX_BUNDLE)) { + live = await clientOfProcess(livePid, reader) + } + if (client?.bundleId?.toLowerCase() === CMUX_BUNDLE && live?.bundleId && + live.bundleId.toLowerCase() !== CMUX_BUNDLE) { + client = { ...client, bundleId: live.bundleId, cmux: undefined } + } + // A terminal launched from cmux can inherit its bundle id and panel IDs. + // TERM_PROGRAM identifies the actual host in that case. + const program = (live?.program ?? client?.program)?.toLowerCase() + const otherProgramBundle = program === 'vscode' ? VSCODE_BUNDLE + : program === 'iterm.app' ? 'com.googlecode.iterm2' + : program === 'apple_terminal' ? 'com.apple.Terminal' : undefined + if (otherProgramBundle && client?.bundleId?.toLowerCase() === CMUX_BUNDLE) { + client = { ...client, bundleId: otherProgramBundle, program, cmux: undefined } + } + const panel = live?.cmux ?? client?.cmux + // A shell launched inside cmux may pass CMUX_* to an unrelated client. The bundle must + // still identify cmux, and an explicit Orca pane takes precedence. + if (panel && UUID_RE.test(panel.workspace) && UUID_RE.test(panel.surface) && + !otherProgramBundle && (live?.bundleId ?? client?.bundleId)?.toLowerCase() === CMUX_BUNDLE && + !live?.orca?.terminal && !client?.orca?.terminal) { + try { + await runner(CMUX_CLI, ['focus-panel', '--panel', panel.surface, '--workspace', panel.workspace]) + await runner('/usr/bin/open', ['-b', CMUX_BUNDLE]) + return { ok: true, how: 'pane' } + } catch { return await focusApp(CMUX_BUNDLE, runner) } + } if (!client?.bundleId && pid) { // No report from a hook: the process's own environment says the same things the hook // would have — the exact Orca pane, the terminal, the tty. Failing that, the process tree // still says which application owns it. - const fromEnv = await clientOfProcess(pid, reader) + const fromEnv = livePid === pid ? live : await clientOfProcess(pid, reader) if (fromEnv && (fromEnv.orca?.terminal || fromEnv.program)) { client = fromEnv } else { diff --git a/server/src/claude/tracker.ts b/server/src/claude/tracker.ts index d5b9b4fb..0ae44846 100644 --- a/server/src/claude/tracker.ts +++ b/server/src/claude/tracker.ts @@ -20,6 +20,7 @@ export interface SessionClient { pid?: number tty?: string terminalSession?: string + cmux?: { workspace: string; surface: string } orca?: { pane?: string; tab?: string; terminal?: string } } diff --git a/server/test/claude-focus.test.ts b/server/test/claude-focus.test.ts index 438a87ea..64f92c65 100644 --- a/server/test/claude-focus.test.ts +++ b/server/test/claude-focus.test.ts @@ -168,3 +168,72 @@ describe('the focus command', () => { await expect(provider(new ClaudeTracker()).commands!.focus({}, { loopback: true })).rejects.toThrow() }) }) + + +describe('cmux session navigation', () => { + const workspace = '85F7E39D-4EC1-43B6-ADA7-CDCDF7A354EB' + const surface = '22C82604-948E-469D-B2B4-849070102A18' + it('uses the live process panel even when the hook already named cmux', async () => { + const calls: string[][] = [] + const reader = async (_cmd: string, args: string[]) => args[0] === 'eww' + ? `claude TERM_PROGRAM=ghostty __CFBundleIdentifier=com.cmuxterm.app CMUX_WORKSPACE_ID=${workspace} CMUX_SURFACE_ID=${surface}` : 'ttys003' + const result = await focusClient({ bundleId: 'com.cmuxterm.app', program: 'ghostty', pid: 123 }, '/x', + async (cmd, args) => { calls.push([cmd, ...args]) }, 123, reader) + expect(result).toEqual({ ok: true, how: 'pane' }) + expect(calls).toEqual([ + ['/Applications/cmux.app/Contents/Resources/bin/cmux', 'focus-panel', '--panel', surface, '--workspace', workspace], + ['/usr/bin/open', '-b', 'com.cmuxterm.app'], + ]) + }) + it.each([ + ['vscode', 'com.microsoft.VSCode', ['/usr/bin/open', '-b', 'com.microsoft.VSCode', '/project']], + ['iTerm.app', 'com.googlecode.iterm2', ['/usr/bin/open', '-b', 'com.googlecode.iterm2']], + ['Apple_Terminal', 'com.apple.Terminal', ['/usr/bin/open', '-b', 'com.apple.Terminal']], + ])('honours %s over inherited cmux IDs', async (program, _bundle, expected) => { + const calls: string[][] = [] + const reader = async (_cmd: string, args: string[]) => args[0] === 'eww' + ? `claude TERM_PROGRAM=${program} __CFBundleIdentifier=com.cmuxterm.app CMUX_WORKSPACE_ID=${workspace} CMUX_SURFACE_ID=${surface}` : 'ttys003' + const result = await focusClient({ bundleId: 'com.cmuxterm.app', pid: 123 }, '/project', + async (cmd, args) => { calls.push([cmd, ...args]) }, 123, reader) + expect(result.ok).toBe(true) + expect(calls.some(call => call[0].endsWith('/cmux'))).toBe(false) + expect(calls.at(-1)).toEqual(expected) + }) + it('falls back to fronting cmux when selecting the panel fails', async () => { + const calls: string[][] = [] + const reader = async (_cmd: string, args: string[]) => args[0] === 'eww' + ? `claude __CFBundleIdentifier=com.cmuxterm.app CMUX_WORKSPACE_ID=${workspace} CMUX_SURFACE_ID=${surface}` : 'ttys003' + const result = await focusClient({ bundleId: 'com.cmuxterm.app' }, '/x', async (cmd, args) => { + calls.push([cmd, ...args]) + if (cmd.endsWith('/cmux')) throw new Error('panel closed') + }, 123, reader) + expect(result).toEqual({ ok: true, how: 'app' }) + expect(calls.at(-1)).toEqual(['/usr/bin/open', '-b', 'com.cmuxterm.app']) + }) + it('does not route an inherited cmux handle ahead of an Orca pane', async () => { + const calls: string[][] = [] + const reader = async (_cmd: string, args: string[]) => args[0] === 'eww' + ? `claude __CFBundleIdentifier=com.cmuxterm.app ORCA_TERMINAL_HANDLE=term_123 CMUX_WORKSPACE_ID=${workspace} CMUX_SURFACE_ID=${surface}` : 'ttys003' + const result = await focusClient({ bundleId: 'com.stablyai.orca', orca: { terminal: 'term_123' } }, '/x', + async (cmd, args) => { calls.push([cmd, ...args]) }, 123, reader) + expect(result).toEqual({ ok: true, how: 'pane' }) + expect(calls[0]?.[0]).toContain('/orca') + expect(calls.some(call => call[0].endsWith('/cmux'))).toBe(false) + }) + it('uses the live client when an unrelated process inherited CMUX variables', async () => { + const calls: string[][] = [] + const reader = async (_cmd: string, args: string[]) => args[0] === 'eww' + ? `code __CFBundleIdentifier=com.microsoft.VSCode CMUX_WORKSPACE_ID=${workspace} CMUX_SURFACE_ID=${surface}` : 'ttys003' + const result = await focusClient({ bundleId: 'com.cmuxterm.app' }, '/project', + async (cmd, args) => { calls.push([cmd, ...args]) }, 123, reader) + expect(result).toEqual({ ok: true, how: 'app' }) + expect(calls).toEqual([['/usr/bin/open', '-b', 'com.microsoft.VSCode', '/project']]) + }) + it('never puts invalid process handles into a link', async () => { + const calls: string[][] = [] + const reader = async () => 'CMUX_WORKSPACE_ID=--bad CMUX_SURFACE_ID=../../evil __CFBundleIdentifier=com.cmuxterm.app' + expect(await focusClient({ bundleId: 'com.cmuxterm.app' }, '/x', async (cmd, args) => { calls.push([cmd, ...args]) }, 123, reader)) + .toEqual({ ok: true, how: 'app' }) + expect(calls).toEqual([['/usr/bin/open', '-b', 'com.cmuxterm.app']]) + }) +})