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(pane): isAgentPane — ask the kernel what IS running, not a field for what should be

enes8933cecc7d2f
diff --git a/src/shared/agents/pane-owner-predicate.test.ts b/src/shared/agents/pane-owner-predicate.test.ts
new file mode 100644
index 000000000..350185e06
--- /dev/null
+++ b/src/shared/agents/pane-owner-predicate.test.ts
@@ -0,0 +1,88 @@
+import { describe, it, expect } from 'vitest'
+import { isAgentPane, AGENT_BINARIES } from './pane-owner-predicate'
+import { isShellCommand } from './pane'
+import { BUILTIN_AGENT_IDS, AGENT_CONFIG } from './config'
+
+const owner = (argv: string[], command = 'node') => ({
+ panePid: 1,
+ tty: '/dev/pts/1',
+ command,
+ argv
+})
+
+describe('isAgentPane', () => {
+ it('recognises the expected agent binary in the foreground group', () => {
+ expect(isAgentPane(owner(['node /usr/local/bin/claude --resume x']), 'claude')).toBe('agent')
+ expect(isAgentPane(owner(['/opt/homebrew/bin/codex']), 'codex')).toBe('agent')
+ expect(isAgentPane(owner(['gemini']), 'gemini')).toBe('agent')
+ })
+
+ it('recognises it anywhere in the group, not only as the leader', () => {
+ // A wrapper leads the group; the CLI is a child of it. Both are in the foreground group and
+ // both will receive what we write, so either one proving the agent is present is enough.
+ expect(
+ isAgentPane(owner(['/bin/sh -c claude --resume x', 'node /usr/local/bin/claude --resume x']), 'claude')
+ ).toBe('agent')
+ })
+
+ it('refuses a DIFFERENT agent — a claude message must not land in a codex pane', () => {
+ expect(isAgentPane(owner(['node /usr/local/bin/codex']), 'claude')).toBe('not-agent')
+ expect(isAgentPane(owner(['node /usr/local/bin/claude']), 'codex')).toBe('not-agent')
+ })
+
+ // The two holes the old negative predicate had, closed:
+ it('refuses a shell OUTSIDE the seven-name allowlist', () => {
+ for (const sh of ['nu', 'pwsh', 'xonsh', 'elvish', 'ssh host', 'python3 -i']) {
+ const cmd = sh.split(' ')[0]
+ // The old gate's exact reading: not one of seven shell names, therefore "an agent".
+ expect(isShellCommand(cmd)).toBe(false)
+ expect(isAgentPane(owner([sh], cmd), 'claude')).toBe('not-agent')
+ }
+ })
+
+ it('refuses a pane whose CLI exited even though agentId is still persisted', () => {
+ expect(isAgentPane(owner(['-zsh'], 'zsh'), 'claude')).toBe('not-agent')
+ expect(isAgentPane(owner(['/bin/bash'], 'bash'), 'claude')).toBe('not-agent')
+ })
+
+ it('answers unknown — never `agent` — when the read failed', () => {
+ expect(isAgentPane(null, 'claude')).toBe('unknown')
+ expect(isAgentPane(undefined, 'claude')).toBe('unknown')
+ expect(isAgentPane(owner([]), 'claude')).toBe('unknown')
+ })
+
+ it('is not fooled by an argument that merely mentions the agent', () => {
+ expect(isAgentPane(owner(['vim /etc/claude.conf']), 'claude')).toBe('not-agent')
+ expect(isAgentPane(owner(['grep -r claude .']), 'claude')).toBe('not-agent')
+ expect(isAgentPane(owner(['tail -f /var/log/claude.log']), 'claude')).toBe('not-agent')
+ // The nastiest shape: a shell whose -c string names the agent. `-c` is an option, not a script
+ // path, so interpreter resolution must not reach past it.
+ expect(isAgentPane(owner(["sh -c 'grep claude .'"]), 'claude')).toBe('not-agent')
+ })
+
+ it('resolves an interpreter only when what follows is a script, never an option', () => {
+ expect(isAgentPane(owner(['node /home/u/.local/share/npm/bin/claude']), 'claude')).toBe('agent')
+ expect(isAgentPane(owner(['bun /opt/bin/codex mcp']), 'codex')).toBe('agent')
+ expect(isAgentPane(owner(['node --inspect claude']), 'claude')).toBe('not-agent')
+ })
+
+ it('answers unknown for a custom agent whose binary nothing can name', () => {
+ // `custom:<uuid>` has no entry, and guessing one would be the exact class of mistake this
+ // module replaces. The caller refuses, retryably, rather than admitting the pane.
+ expect(isAgentPane(owner(['node /opt/bin/whatever']), 'custom:abc')).toBe('unknown')
+ })
+
+ it('accepts a caller-supplied binary list for a custom agent', () => {
+ expect(isAgentPane(owner(['node /opt/bin/whatever']), 'custom:abc', ['whatever'])).toBe('agent')
+ expect(isAgentPane(owner(['node /opt/bin/other']), 'custom:abc', ['whatever'])).toBe('not-agent')
+ })
+
+ it('covers every built-in agent, so a new one cannot be silently unverifiable', () => {
+ for (const id of BUILTIN_AGENT_IDS) {
+ expect(AGENT_BINARIES[id], `${id} has no binary list`).toBeTruthy()
+ // The list must still name the process the launcher actually starts.
+ expect(AGENT_BINARIES[id]).toContain(AGENT_CONFIG[id].expectedProcess)
+ expect(isAgentPane(owner([`/usr/local/bin/${AGENT_CONFIG[id].expectedProcess}`]), id)).toBe('agent')
+ }
+ })
+})
diff --git a/src/shared/agents/pane-owner-predicate.ts b/src/shared/agents/pane-owner-predicate.ts
index 045f62f70..aef2a6bec 100644
--- a/src/shared/agents/pane-owner-predicate.ts
+++ b/src/shared/agents/pane-owner-predicate.ts
@@ -35,3 +35,89 @@ export interface PaneOwner {
* caller may retry. It must never be upgraded to `agent`.
*/
export type AgentPaneVerdict = 'agent' | 'not-agent' | 'unknown'
+
+/**
+ * The binary names each built-in agent's CLI actually runs as. Mirrors `AGENT_CONFIG[x].launchCmd`
+ * / `.expectedProcess`, kept as its own list so a launch-command change (a wrapper, a flag) cannot
+ * silently widen or narrow a SECURITY predicate. A custom agent (`custom:<uuid>`) has no entry —
+ * see `isAgentPane`'s `unknown` for what that means.
+ */
+export const AGENT_BINARIES: Record<string, readonly string[]> = {
+ claude: ['claude'],
+ codex: ['codex'],
+ gemini: ['gemini'],
+ opencode: ['opencode'],
+ grok: ['grok']
+}
+
+/**
+ * Interpreters that are followed by the script they run. `node /usr/local/bin/claude` is the shape
+ * every npm-installed agent CLI actually has on this host (measured), so argv[0]'s basename alone
+ * would answer `node` for every one of them.
+ *
+ * Resolution applies ONLY when the next token is not an option: `sh -c 'grep -r claude .'` must not
+ * resolve to `grep`, and `python3 -i` must not resolve to anything. An interpreter whose next token
+ * starts with `-` keeps its own name.
+ */
+const INTERPRETERS = new Set(['node', 'nodejs', 'bun', 'deno', 'python', 'python3', 'ruby', 'perl'])
+
+/** basename, with a login shell's leading `-` stripped (`-zsh` → `zsh`). */
+function baseName(token: string): string {
+ const noDash = token.replace(/^-/, '')
+ return noDash.split('/').pop() ?? ''
+}
+
+/**
+ * The binary a command line actually runs, as a bare name. Splits on whitespace only — this is a
+ * `ps` line, already tokenised by the kernel's argv, and a quoted path with a space in it is not
+ * worth a shell-grammar parser here: the failure mode is a name that matches nothing, i.e. a
+ * refusal, which is the safe direction.
+ */
+function effectiveBinary(commandLine: string): string {
+ const tokens = commandLine.trim().split(/\s+/).filter(Boolean)
+ if (tokens.length === 0) return ''
+ const head = baseName(tokens[0])
+ if (!INTERPRETERS.has(head)) return head
+ const next = tokens[1]
+ if (!next || next.startsWith('-')) return head
+ return baseName(next)
+}
+
+/**
+ * POSITIVE and kernel-truth, replacing `!isShellCommand(pane)`, which was neither.
+ *
+ * The old gate asked a persisted field what the pane SHOULD be running. `agentId` is durable and
+ * outlives the CLI, so a plain terminal whose claude exited weeks ago still read as an agent — and
+ * `isShellCommand` is a SEVEN-NAME allowlist (zsh/bash/sh/fish/dash/ksh/tcsh), so `nu`, `pwsh`,
+ * `xonsh`, `ssh` and `python` all fell through its negation and were admitted as agents. Text
+ * delivered to any of those EXECUTES, because sendText appends Enter.
+ *
+ * `unknown` is a third answer, not a shade of `not-agent`, because the two need different replies:
+ * `unknown` means the probe failed and the caller may retry; `not-agent` means the kernel answered
+ * and the answer was no.
+ *
+ * Matched on the BASENAME of argv[0] (after interpreter resolution), never on a substring of the
+ * command line — `vim /etc/claude.conf` and `grep -r claude .` are not claude panes.
+ *
+ * `binaries` overrides the built-in table for a custom agent whose binary the caller knows. Without
+ * it an agent id with no entry answers `unknown`: we cannot name what should be there, so we cannot
+ * claim to have seen it.
+ *
+ * `isShellCommand` is deliberately NOT deleted — its existing callers (the restart exit poll, the
+ * reconnect resync) ask a genuinely different question ("has the CLI let go of the pane?") for
+ * which a shell allowlist is the right shape.
+ */
+export function isAgentPane(
+ owner: PaneOwner | null | undefined,
+ expected: string,
+ binaries?: readonly string[]
+): AgentPaneVerdict {
+ if (!owner) return 'unknown'
+ if (!owner.argv || owner.argv.length === 0) return 'unknown'
+ const wanted = binaries ?? AGENT_BINARIES[expected]
+ if (!wanted || wanted.length === 0) return 'unknown'
+ for (const line of owner.argv) {
+ if (wanted.includes(effectiveBinary(line))) return 'agent'
+ }
+ return 'not-agent'
+}
diff --git a/src/shared/agents/pane.ts b/src/shared/agents/pane.ts
index 7812f6b77..c068693ad 100644
--- a/src/shared/agents/pane.ts
+++ b/src/shared/agents/pane.ts
@@ -2,6 +2,16 @@
// renderer (agent-restart's exit poll) and the main process (the reconnect resync) ask the same
// question of the same `#{pane_current_command}` answer.
+// NOT a way to ask "is this an agent pane?". `!isShellCommand(cmd)` was that gate once, and it is
+// wrong in both directions: the list below is SEVEN names, so `nu`, `pwsh`, `xonsh`, `elvish`,
+// `ssh` and `python` all negate to "agent", and `#{pane_current_command}` answers `node` for every
+// npm-installed agent CLI anyway. That question is now `isAgentPane`
+// (`./pane-owner-predicate.ts`), which reads the pane's foreground process group from the kernel.
+//
+// This one stays, unchanged, because its callers ask a genuinely different question — "has the CLI
+// let go of the pane?" — for which a shell allowlist IS the right shape: an unrecognised command is
+// simply "not yet", and the poll goes round again.
+
/** Foreground commands that mean "the CLI is gone, a shell owns the pane". Login shells
* report as '-zsh'; tmux may report a full path. */
const SHELLS = new Set(['zsh', 'bash', 'sh', 'fish', 'dash', 'ksh', 'tcsh'])