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(chat): ask the kernel before typing into a codex pane

reviewcfe24b899830
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 79538197b..6a7a94251 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -393,6 +393,7 @@ import {
import { useProjects } from '../state/projects'
import { useAgentStatus } from '../state/agentStatus'
import { hostChatSend, hostChatSession, hostChatStatus } from '../lib/hostChatQuery'
+import { chatPaneRefusal } from '../lib/chatPaneGate'
import { useLaunchDelivery } from '../state/launchDelivery'
import { useBrowserLease, drivingNodeIds } from '../state/browserLease'
import { useTerminalFocus } from '../state/terminalFocus'
@@ -10005,7 +10006,13 @@ export function Canvas() {
void hostChatSend(q, {
getStatus: (id) => useAgentStatus.getState().byId[id],
sendText: (id, text) => api.pty.sendText(id, text),
- now: Date.now
+ now: Date.now,
+ // The kernel's say on whether the agent is still in the pane (codex announces no quit).
+ paneRefusal: (id, agentId) =>
+ chatPaneRefusal(agentId, id, {
+ paneOwner: (n) => api.pty.paneOwner(n),
+ customAgents: useSettings.getState().settings.customAgents
+ })
}).then((out) => api.sendHostChatReply({ requestId: q.requestId, kind: 'send', ...out }))
})
// eslint-disable-next-line react-hooks/exhaustive-deps
diff --git a/src/renderer/lib/chatPaneGate.test.ts b/src/renderer/lib/chatPaneGate.test.ts
new file mode 100644
index 000000000..476156613
--- /dev/null
+++ b/src/renderer/lib/chatPaneGate.test.ts
@@ -0,0 +1,63 @@
+// The chat send's second gate: for an agent whose hooks never announce a session end (codex,
+// opencode), the kernel must prove the agent owns the pane before text + Enter is typed — or a
+// message sent after the user `/quit` codex runs as a SHELL command.
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { chatPaneRefusal, chatPaneRefusalReason, chatPaneRefusalToast } from './chatPaneGate'
+import { setCustomAgentBaseResolver } from '@shared/agents/config'
+import type { PaneOwner } from '@shared/agents/pane-owner-predicate'
+
+afterEach(() => setCustomAgentBaseResolver(null))
+
+// MEASURED (codex-cli 0.156.1 in a real tmux pane, 2026-09-29): the npm shim and the native binary
+// it spawns share the pane's foreground process group.
+const CODEX_PANE: PaneOwner = {
+ panePid: 100,
+ tty: '/dev/pts/31',
+ command: 'node',
+ argv: ['node /usr/bin/codex', '/usr/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex'],
+ pids: [201550, 201574]
+}
+const SHELL_PANE: PaneOwner = { panePid: 100, tty: '/dev/pts/31', command: 'bash', argv: ['-bash'], pids: [100] }
+
+describe('chatPaneRefusal', () => {
+ it('lets a send through when codex owns the pane', async () => {
+ expect(await chatPaneRefusal('codex', 'n1', { paneOwner: async () => CODEX_PANE })).toBeNull()
+ })
+
+ it('refuses when a SHELL owns the pane — codex quit, and no hook said so', async () => {
+ expect(await chatPaneRefusal('codex', 'n1', { paneOwner: async () => SHELL_PANE })).toBe('exited')
+ })
+
+ it('refuses when the pane cannot be read (unknown is never upgraded to agent)', async () => {
+ expect(await chatPaneRefusal('codex', 'n1', { paneOwner: async () => null })).toBe('unverified')
+ expect(await chatPaneRefusal('codex', 'n1', { paneOwner: async () => { throw new Error('ssh') } })).toBe('unverified')
+ })
+
+ it('an agent whose hooks DO report a session end is not probed at all (unchanged path)', async () => {
+ const paneOwner = vi.fn(async () => SHELL_PANE)
+ for (const id of ['claude', 'grok']) expect(await chatPaneRefusal(id, 'n1', { paneOwner })).toBeNull()
+ expect(paneOwner).not.toHaveBeenCalled()
+ })
+
+ it('a custom agent on the codex harness is probed for the binary its launch command runs', async () => {
+ setCustomAgentBaseResolver((id) => (id === 'custom:cx' ? 'codex' : undefined))
+ const customAgents = [{ id: 'custom:cx', launchCmd: '', baseAgent: 'codex' }]
+ expect(await chatPaneRefusal('custom:cx', 'n1', { paneOwner: async () => CODEX_PANE, customAgents })).toBeNull()
+ expect(await chatPaneRefusal('custom:cx', 'n1', { paneOwner: async () => SHELL_PANE, customAgents })).toBe('exited')
+ // No definition to name its binary from: unverifiable, so refused.
+ expect(await chatPaneRefusal('custom:cx', 'n1', { paneOwner: async () => CODEX_PANE })).toBe('unverified')
+ })
+})
+
+describe('what a refusal says', () => {
+ it('maps onto the phone\'s locked reason union', () => {
+ expect(chatPaneRefusalReason('exited')).toBe('exited')
+ expect(chatPaneRefusalReason('unverified')).toBe('unavailable')
+ })
+ it('names the agent and never claims a cause it did not measure', () => {
+ expect(chatPaneRefusalToast('exited', 'Codex')).toBe('Codex is no longer running in this terminal — the message was not sent.')
+ expect(chatPaneRefusalToast('unverified', 'Codex')).toBe(
+ 'Could not confirm Codex is running in this terminal — the message was not sent.'
+ )
+ })
+})
diff --git a/src/renderer/lib/chatPaneGate.ts b/src/renderer/lib/chatPaneGate.ts
new file mode 100644
index 000000000..8326f04a0
--- /dev/null
+++ b/src/renderer/lib/chatPaneGate.ts
@@ -0,0 +1,51 @@
+// The chat send's KERNEL gate — the second half of "may the ⌘M composer / the phone type into this
+// pane?", after `chatSendRefusal` (chatSendGate.ts).
+//
+// `chatSendRefusal` reads the agent-status store, and for most agents that is enough: a CLI that
+// quits announces it (SessionEnd → `sessionEnded`), so a shell-owned pane is refused. Codex and
+// opencode announce NO session end (see SESSION_END_CAPABLE in shared/agents/config.ts): after a
+// deliberate `/quit` their last hook state is still `done`, and the store reads "at its prompt". A
+// chat message sent then is pasted into the SHELL with an Enter — it runs as a command. So for those
+// agents the pane's foreground process group is asked (`isAgentPane`, the same positive, three-
+// valued predicate the wake and delivery gates use) right before the send.
+//
+// Agents that DO report a session end are not probed: their path is byte-identical to before.
+import { capabilityAgentId, reportsSessionEnd } from '@shared/agents/config'
+import { binariesFor, isAgentPane, type PaneOwner } from '@shared/agents/pane-owner-predicate'
+import type { ChatSendReason } from '@shared/mobile-chat'
+
+/** `exited` = the kernel answered and the agent is not in the pane; `unverified` = the pane could not
+ * be read (unknown is a refusal, never a licence). `null` = send. */
+export type ChatPaneRefusal = 'exited' | 'unverified' | null
+
+export async function chatPaneRefusal(
+ agentId: string,
+ nodeId: string,
+ deps: {
+ paneOwner(nodeId: string): Promise<PaneOwner | null>
+ /** The settings' custom agents, so a `custom:<id>` on the codex harness can be named. */
+ customAgents?: readonly { id: string; launchCmd: string; baseAgent?: string }[]
+ }
+): Promise<ChatPaneRefusal> {
+ if (reportsSessionEnd(capabilityAgentId(agentId))) return null
+ let owner: PaneOwner | null
+ try {
+ owner = await deps.paneOwner(nodeId)
+ } catch {
+ return 'unverified'
+ }
+ const verdict = isAgentPane(owner, agentId, binariesFor(agentId, deps.customAgents))
+ return verdict === 'agent' ? null : verdict === 'not-agent' ? 'exited' : 'unverified'
+}
+
+/** The phone's reason (its union is locked — `mobile-chat.ts`): an unreadable pane is `unavailable`. */
+export function chatPaneRefusalReason(r: Exclude<ChatPaneRefusal, null>): ChatSendReason {
+ return r === 'exited' ? 'exited' : 'unavailable'
+}
+
+/** The desktop's toast for a refused send. It states what was measured, nothing more. */
+export function chatPaneRefusalToast(r: Exclude<ChatPaneRefusal, null>, agentLabel: string): string {
+ return r === 'exited'
+ ? `${agentLabel} is no longer running in this terminal — the message was not sent.`
+ : `Could not confirm ${agentLabel} is running in this terminal — the message was not sent.`
+}
diff --git a/src/renderer/lib/hostChatQuery.test.ts b/src/renderer/lib/hostChatQuery.test.ts
index da5e77095..f230e8ae3 100644
--- a/src/renderer/lib/hostChatQuery.test.ts
+++ b/src/renderer/lib/hostChatQuery.test.ts
@@ -40,7 +40,7 @@ describe('hostChatSend', () => {
sendText: ReturnType<typeof vi.fn> = vi.fn(async () => true),
now = 1000
) =>
- hostChatSend(base, { getStatus: () => status as never, sendText: sendText as never, now: () => now }).then((r) => ({
+ hostChatSend(base, { getStatus: () => status as never, sendText: sendText as never, now: () => now, paneRefusal: async () => null }).then((r) => ({
r,
sendText
}))
@@ -70,7 +70,7 @@ describe('hostChatSend', () => {
})
it('refuses a node whose agent is unknown everywhere (no process can be proven in the pane)', async () => {
const sendText = vi.fn(async () => true as const)
- const r = await hostChatSend({ ...base, agentId: undefined }, { getStatus: () => ({ state: 'done' }), sendText, now: () => 1000 })
+ const r = await hostChatSend({ ...base, agentId: undefined }, { getStatus: () => ({ state: 'done' }), sendText, now: () => 1000, paneRefusal: async () => null })
expect(r).toEqual({ result: 'refused', reason: 'exited' })
expect(sendText).not.toHaveBeenCalled()
})
@@ -78,7 +78,7 @@ describe('hostChatSend', () => {
const sendText = vi.fn(async () => true as const)
const r = await hostChatSend(
{ ...base, agentId: undefined },
- { getStatus: () => ({ state: 'done', agentId: 'claude' }), sendText, now: () => 1000 }
+ { getStatus: () => ({ state: 'done', agentId: 'claude' }), sendText, now: () => 1000, paneRefusal: async () => null }
)
expect(r).toEqual({ result: 'sent' })
})
@@ -94,4 +94,30 @@ describe('hostChatSend', () => {
expect((await run({ state: 'done' }, vi.fn(async () => { throw new Error('x') }))).r).toEqual({ result: 'unconfirmed' })
expect((await run({ state: 'done' }, vi.fn(async () => 'yes' as never))).r).toEqual({ result: 'refused', reason: 'failed' })
})
+ it('asks the KERNEL gate last, with the resolved agent id, and refuses what it refuses — nothing typed', async () => {
+ // codex announces no session end: after a `/quit` the store still reads `done`, and only the
+ // pane's foreground process group can tell that a shell would receive the text.
+ for (const [refusal, reason] of [['exited', 'exited'], ['unverified', 'unavailable']] as const) {
+ const sendText = vi.fn(async () => true as const)
+ const paneRefusal = vi.fn(async () => refusal)
+ const r = await hostChatSend({ ...base, agentId: 'codex' }, { getStatus: () => ({ state: 'done' }), sendText, now: () => 1000, paneRefusal })
+ expect(r).toEqual({ result: 'refused', reason })
+ expect(paneRefusal).toHaveBeenCalledWith('n1', 'codex')
+ expect(sendText).not.toHaveBeenCalled()
+ }
+ })
+ it('a kernel gate that throws refuses (unavailable) rather than typing', async () => {
+ const sendText = vi.fn(async () => true as const)
+ const r = await hostChatSend(
+ { ...base, agentId: 'codex' },
+ { getStatus: () => ({ state: 'done' }), sendText, now: () => 1000, paneRefusal: async () => { throw new Error('x') } }
+ )
+ expect(r).toEqual({ result: 'refused', reason: 'unavailable' })
+ expect(sendText).not.toHaveBeenCalled()
+ })
+ it('the store gate refuses FIRST — no pane probe for a working agent', async () => {
+ const paneRefusal = vi.fn(async () => null)
+ await hostChatSend({ ...base, agentId: 'codex' }, { getStatus: () => ({ state: 'working' }), sendText: vi.fn(), now: () => 1000, paneRefusal })
+ expect(paneRefusal).not.toHaveBeenCalled()
+ })
})
diff --git a/src/renderer/lib/hostChatQuery.ts b/src/renderer/lib/hostChatQuery.ts
index 2158cdb81..1784d73c7 100644
--- a/src/renderer/lib/hostChatQuery.ts
+++ b/src/renderer/lib/hostChatQuery.ts
@@ -12,6 +12,7 @@ import type { ChatSendOutcome, HostChatQuery, RendererChatStatus } from '@shared
import type { TextDeliveryResult } from '@shared/text-delivery'
import type { AgentNodeStatus } from '../state/agentStatus'
import { chatSendRefusal } from './chatSendGate'
+import { chatPaneRefusalReason, type ChatPaneRefusal } from './chatPaneGate'
type StatusSlice = Pick<AgentNodeStatus, 'state' | 'held' | 'hibernated' | 'paused' | 'dropped' | 'sessionEnded' | 'agentId'>
@@ -30,6 +31,12 @@ export interface HostChatSendDeps {
getStatus(nodeId: string): Partial<StatusSlice> | undefined
sendText(nodeId: string, text: string): Promise<TextDeliveryResult>
now(): number
+ /**
+ * The KERNEL gate (`chatPaneRefusal`, chatPaneGate.ts): for an agent that announces no session end
+ * (codex), is the agent really in the pane? Required, not optional — a shell that forgot it would
+ * type the phone's text into whatever shell a quit codex left behind, and that must not compile.
+ */
+ paneRefusal(nodeId: string, agentId: string): Promise<ChatPaneRefusal>
}
export async function hostChatSend(
@@ -45,6 +52,14 @@ export async function hostChatSend(
const refusal = chatSendRefusal(agentId, st)
if (refusal !== null) return { result: 'refused', reason: refusal }
if (st.held) return { result: 'refused', reason: 'dialog' }
+ // Last, because it is the one gate that costs a round trip (a remote pane is read over ssh).
+ let pane: ChatPaneRefusal
+ try {
+ pane = await deps.paneRefusal(q.nodeId, agentId)
+ } catch {
+ pane = 'unverified'
+ }
+ if (pane) return { result: 'refused', reason: chatPaneRefusalReason(pane) }
try {
const r = await deps.sendText(q.nodeId, q.text)
if (r === true) return { result: 'sent' }
diff --git a/src/renderer/nodes/ChatPanel.codexSend.test.tsx b/src/renderer/nodes/ChatPanel.codexSend.test.tsx
new file mode 100644
index 000000000..5129cac0a
--- /dev/null
+++ b/src/renderer/nodes/ChatPanel.codexSend.test.tsx
@@ -0,0 +1,100 @@
+// @vitest-environment jsdom
+import { act } from 'react'
+import { createRoot, type Root } from 'react-dom/client'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { useAgentStatus } from '../state/agentStatus'
+import type { PaneOwner } from '@shared/agents/pane-owner-predicate'
+
+/**
+ * The ⌘M composer on a CODEX node asks the kernel before it types (lib/chatPaneGate.ts): codex
+ * announces no session end, so after a `/quit` the store still reads `done` while a shell owns the
+ * pane, and `sendText` would run the message as a command. This pins the GLUE: the probe runs at
+ * send time, a shell-owned pane is refused with a toast and nothing is typed.
+ */
+
+;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+
+const { sendText, paneOwner, session } = vi.hoisted(() => {
+ const sendText = vi.fn(async (_id: string, _text: string) => true as const)
+ const paneOwner = vi.fn(async (_id: string): Promise<PaneOwner | null> => null)
+ const session = {
+ api: {
+ chat: { readTranscript: async () => ({ messages: [], found: true }) },
+ pty: { sendText, paneOwner }
+ }
+ }
+ return { sendText, paneOwner, session }
+})
+vi.mock('../session/session', () => ({ useSession: () => session }))
+
+import { ChatPanel } from './ChatPanel'
+
+const NODE = 'n-chat-codex'
+const CODEX: PaneOwner = { panePid: 1, tty: '/dev/pts/1', command: 'node', argv: ['node /usr/bin/codex'], pids: [2] }
+const SHELL: PaneOwner = { panePid: 1, tty: '/dev/pts/1', command: 'bash', argv: ['-bash'], pids: [1] }
+let host: HTMLDivElement
+let root: Root
+let toasts: string[]
+const onToast = (e: Event): void => {
+ toasts.push((e as CustomEvent<{ message: string }>).detail.message)
+}
+
+async function mount(agentId: string): Promise<HTMLTextAreaElement> {
+ await act(async () => {
+ root.render(<ChatPanel nodeId={NODE} sessionId="01a0c9f4-2b7e-7c31-9d52-5e8a1f3b6c20" agentId={agentId} />)
+ })
+ return host.querySelector('textarea') as HTMLTextAreaElement
+}
+async function send(ta: HTMLTextAreaElement, text: string): Promise<void> {
+ const setter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value')!.set!
+ await act(async () => {
+ setter.call(ta, text)
+ ta.dispatchEvent(new Event('input', { bubbles: true }))
+ })
+ await act(async () => {
+ ta.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true }))
+ })
+}
+
+beforeEach(() => {
+ sendText.mockClear()
+ paneOwner.mockReset()
+ toasts = []
+ window.addEventListener('nodeterm:toast', onToast)
+ useAgentStatus.setState((s) => ({ byId: { ...s.byId, [NODE]: { state: 'done' } as (typeof s.byId)[string] } }))
+ host = document.createElement('div')
+ document.body.appendChild(host)
+ root = createRoot(host)
+})
+afterEach(async () => {
+ await act(async () => root.unmount())
+ host.remove()
+ window.removeEventListener('nodeterm:toast', onToast)
+ useAgentStatus.setState((s) => {
+ const byId = { ...s.byId }
+ delete byId[NODE]
+ return { byId }
+ })
+})
+
+describe('ChatPanel — codex send asks the kernel', () => {
+ it('types the message when codex owns the pane', async () => {
+ paneOwner.mockResolvedValue(CODEX)
+ await send(await mount('codex'), 'run the tests')
+ expect(paneOwner).toHaveBeenCalledWith(NODE)
+ expect(sendText).toHaveBeenCalledWith(NODE, 'run the tests')
+ })
+
+ it('refuses, with a toast, when a SHELL owns the pane though the store still reads done', async () => {
+ paneOwner.mockResolvedValue(SHELL)
+ await send(await mount('codex'), 'rm -rf build')
+ expect(sendText).not.toHaveBeenCalled()
+ expect(toasts).toEqual(['Codex is no longer running in this terminal — the message was not sent.'])
+ })
+
+ it('claude is not probed (its hooks announce a quit)', async () => {
+ await send(await mount('claude'), 'hello')
+ expect(paneOwner).not.toHaveBeenCalled()
+ expect(sendText).toHaveBeenCalled()
+ })
+})
diff --git a/src/renderer/nodes/ChatPanel.tsx b/src/renderer/nodes/ChatPanel.tsx
index 2bc3a415b..e1b16f8d7 100644
--- a/src/renderer/nodes/ChatPanel.tsx
+++ b/src/renderer/nodes/ChatPanel.tsx
@@ -5,6 +5,7 @@ import { useAgentStatus } from '../state/agentStatus'
import { useSession } from '../session/session'
import { chipFor } from '../lib/keybindingOverrides'
import { chatComposerPlaceholder, chatSendRefusal } from '../lib/chatSendGate'
+import { chatPaneRefusal, chatPaneRefusalToast } from '../lib/chatPaneGate'
import { chatAgentLabel, isNearBottom, shouldFollowOnLoad, toolCardTitle } from '../lib/chatPanel'
import { useSettings } from '../state/settings'
import {
@@ -632,6 +633,17 @@ export function ChatPanel({
// Read the store at SEND time, not the render-time values: a PermissionRequest (or an Eco
// hibernation) that landed between the last render and this keypress must still block.
if (!text || chatSendRefusal(agentId, useAgentStatus.getState().byId[nodeId] ?? {}) !== null) return
+ // The kernel's say (chatPaneGate.ts): an agent that announces no quit (codex) may have left a
+ // SHELL in the pane while the store still reads `done` — typed there, the message would run.
+ const pane = await chatPaneRefusal(agentId, nodeId, {
+ paneOwner: (n) => api.pty.paneOwner(n),
+ customAgents: useSettings.getState().settings.customAgents
+ })
+ if (pane) {
+ const message = chatPaneRefusalToast(pane, chatAgentLabel(agentId, useSettings.getState().settings.customAgents))
+ window.dispatchEvent(new CustomEvent('nodeterm:toast', { detail: { kind: 'error', message } }))
+ return
+ }
const ok = await api.pty.sendText(nodeId, text)
if (ok === 'pasted-not-submitted') {
window.dispatchEvent(new CustomEvent('nodeterm:toast', { detail: { kind: 'error', message: TEXT_NOT_SUBMITTED } }))