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): refuse sending after the agent CLI exited; derive the gate from agentProcessInPane

review0d464fbc02d3
diff --git a/CLAUDE.md b/CLAUDE.md
index f695e98eb..a1dc57ef7 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -2337,11 +2337,14 @@ command-bearing opens; this does not add a human-confirm dialog or change mobile
`renderer/lib/chatSendGate.ts`) — never in `waiting`/`blocked`, not just never in `working`:
PermissionRequest and AskUserQuestion both normalize to `waiting`, the pane then holds a TUI
select dialog this view does not show, and `sendText`'s Enter would ANSWER it ("Yes" is the
- default highlight). It also refuses a **hibernated / paused / dropped** node, whose `state` still
- reads `done` while a SHELL owns the pane — the message would run as a shell command; those flags
- rank above the state (`chatSendRefusal`). Everything is re-read from the store at send time, not
- only at render. Same trap as the in-place restart's `/exit`. The ↻ in the panel bar is the only
- reload for a session whose hooks never report `working`.
+ default highlight). It also refuses any node whose CLI has left the pane — hibernated, paused,
+ dropped, or **exited** (`/exit`/Ctrl+D: `state: undefined` + `sessionEnded`) — because a SHELL
+ owns it and the message would run as a shell command. That test is `agentProcessInPane`
+ (`terminal/live-work.ts`), the same single rule the memory levers use — never a second copy of
+ the flag set — and it ranks above the state (`chatSendRefusal`). Everything is re-read from the
+ store at send time, not only at render. Same trap as the in-place restart's `/exit`. The bar's ↻
+ reloads on demand (beside the empty state's Retry), since a session whose hooks never report
+ `working` never takes the turn-finish reload.
- **Subagent visualization** (agents in `SUBAGENT_CAPABLE`) — `subagent-start`/`subagent-end`
normalized events (from Claude's `PreToolUse`/`PostToolUse` on tool `Agent`/`Task`, correlated
by `tool_use_id`) drive a transient `state/agentNodes.ts` store. Claude launches subagents
diff --git a/src/renderer/lib/chatSendGate.test.ts b/src/renderer/lib/chatSendGate.test.ts
index 5b73726dd..501278558 100644
--- a/src/renderer/lib/chatSendGate.test.ts
+++ b/src/renderer/lib/chatSendGate.test.ts
@@ -1,40 +1,57 @@
import { describe, expect, it } from 'vitest'
+import { agentProcessInPane } from '../terminal/live-work'
import { canSendFromChat, chatComposerPlaceholder, chatSendRefusal } from './chatSendGate'
describe('chatSendRefusal / canSendFromChat', () => {
it('allows a finished turn and an unknown state (no hook knowledge keeps the historical behavior)', () => {
- expect(chatSendRefusal({ state: 'done' })).toBeNull()
- expect(chatSendRefusal({ state: undefined })).toBeNull()
- expect(chatSendRefusal({})).toBeNull()
- expect(canSendFromChat({ state: 'done' })).toBe(true)
- expect(canSendFromChat({})).toBe(true)
+ expect(chatSendRefusal('claude', { state: 'done' })).toBeNull()
+ expect(chatSendRefusal('claude', { state: undefined })).toBeNull()
+ expect(chatSendRefusal('claude', {})).toBeNull()
+ expect(canSendFromChat('claude', { state: 'done' })).toBe(true)
+ expect(canSendFromChat('claude', {})).toBe(true)
})
it('refuses while the agent is working', () => {
- expect(chatSendRefusal({ state: 'working' })).toBe('working')
- expect(canSendFromChat({ state: 'working' })).toBe(false)
+ expect(chatSendRefusal('claude', { state: 'working' })).toBe('working')
+ expect(canSendFromChat('claude', { state: 'working' })).toBe(false)
})
it('refuses while a TUI dialog is up: Enter would answer it', () => {
// PermissionRequest / AskUserQuestion normalize to `waiting`, a permission Notification to
// `blocked` — both are select dialogs where sendText's trailing Enter confirms the highlight.
- expect(chatSendRefusal({ state: 'waiting' })).toBe('dialog')
- expect(chatSendRefusal({ state: 'blocked' })).toBe('dialog')
+ expect(chatSendRefusal('claude', { state: 'waiting' })).toBe('dialog')
+ expect(chatSendRefusal('claude', { state: 'blocked' })).toBe('dialog')
})
it('refuses a hibernated node even though its state still reads done: a SHELL owns the pane', () => {
- expect(chatSendRefusal({ state: 'done', hibernated: true })).toBe('asleep')
- expect(canSendFromChat({ state: 'done', hibernated: true })).toBe(false)
- expect(chatSendRefusal({ hibernated: true })).toBe('asleep')
+ expect(chatSendRefusal('claude', { state: 'done', hibernated: true })).toBe('asleep')
+ expect(canSendFromChat('claude', { state: 'done', hibernated: true })).toBe(false)
+ expect(chatSendRefusal('claude', { hibernated: true })).toBe('asleep')
})
it('refuses a paused or dropped node for the same reason', () => {
- expect(chatSendRefusal({ state: 'done', paused: true })).toBe('paused')
- expect(chatSendRefusal({ state: 'done', dropped: true })).toBe('dropped')
+ expect(chatSendRefusal('claude', { state: 'done', paused: true })).toBe('paused')
+ expect(chatSendRefusal('claude', { state: 'done', dropped: true })).toBe('dropped')
+ })
+
+ it('refuses a node whose CLI announced its own exit (/exit, /quit, Ctrl+D): state is undefined, a shell owns the pane', () => {
+ // Canvas records a SessionEnd as `state: undefined` + `sessionEnded: true`; the undefined state
+ // alone would read as "unknown = allowed" and type the message into the bare shell.
+ expect(chatSendRefusal('claude', { state: undefined, sessionEnded: true })).toBe('exited')
+ expect(canSendFromChat('claude', { sessionEnded: true })).toBe(false)
+ })
+
+ it('derives the refusal from live-work agentProcessInPane, the ONE shell-owned-pane rule', () => {
+ // Every status agentProcessInPane calls "no CLI in the pane" must be refused here, and every
+ // one it calls "CLI present" must fall through to the state.
+ const statuses = [{}, { hibernated: true }, { paused: true }, { dropped: true }, { sessionEnded: true }]
+ for (const st of statuses) {
+ expect(canSendFromChat('claude', { ...st, state: 'done' })).toBe(agentProcessInPane('claude', st))
+ }
})
it('a shell-owned pane outranks a stale live state', () => {
- expect(chatSendRefusal({ state: 'working', hibernated: true })).toBe('asleep')
+ expect(chatSendRefusal('claude', { state: 'working', hibernated: true })).toBe('asleep')
})
})
@@ -68,13 +85,16 @@ describe('chatComposerPlaceholder', () => {
it('tells an asleep, paused or dropped node how to come back, by the chip it shows', () => {
expect(chatComposerPlaceholder({ ...base, refusal: 'asleep' })).toBe(
- 'Grok is asleep to save memory — click SLEEPING in the node header to resume it'
+ 'Grok is asleep to save memory — click SLEEPING in the header to resume it'
)
expect(chatComposerPlaceholder({ ...base, refusal: 'paused' })).toBe(
- 'Grok is paused — click PAUSED in the node header to resume it'
+ 'Grok is paused — click PAUSED in the header to resume it'
)
expect(chatComposerPlaceholder({ ...base, refusal: 'dropped' })).toBe(
- 'Grok is no longer running in this terminal — click DROPPED in the node header to resume it'
+ 'Grok is no longer running in this terminal — click DROPPED in the header to resume it'
+ )
+ expect(chatComposerPlaceholder({ ...base, refusal: 'exited' })).toBe(
+ 'Grok has exited — relaunch it in the terminal'
)
})
diff --git a/src/renderer/lib/chatSendGate.ts b/src/renderer/lib/chatSendGate.ts
index 47d6aac8b..fac60f1c5 100644
--- a/src/renderer/lib/chatSendGate.ts
+++ b/src/renderer/lib/chatSendGate.ts
@@ -1,4 +1,5 @@
import type { AgentState } from '@shared/agents/normalize'
+import { agentProcessInPane } from '../terminal/live-work'
/** The slice of a node's agent status the composer gate reads (`AgentNodeStatus` fits it). */
export interface ChatGateStatus {
@@ -6,10 +7,11 @@ export interface ChatGateStatus {
hibernated?: boolean
paused?: boolean
dropped?: boolean
+ sessionEnded?: boolean
}
/** Why the composer refuses to send; `null` = it may send. */
-export type ChatSendRefusal = 'working' | 'dialog' | 'asleep' | 'paused' | 'dropped' | null
+export type ChatSendRefusal = 'working' | 'dialog' | 'asleep' | 'paused' | 'dropped' | 'exited' | null
/**
* May the ⌘M chat composer type into the agent's pane right now — and if not, why?
@@ -22,27 +24,35 @@ export type ChatSendRefusal = 'working' | 'dialog' | 'asleep' | 'paused' | 'drop
* The pane then holds a select dialog the transcript view does not show, and Enter CONFIRMS the
* highlighted option — "Yes" by default. A chat message would silently grant a permission or
* pick an answer. Same trap CLAUDE.md documents for the in-place restart's `/exit`.
- * - **A SHELL** (hibernated / paused / dropped). Eco's hibernation exits the CLI and leaves the
- * node's `state` at `done`, so the state alone reads "sendable" — but the pane now belongs to
- * the login shell, and the message would be EXECUTED as a shell command. Pause and an
- * unaccounted CLI exit (DROPPED) leave the pane in the same shell. These flags therefore rank
- * ABOVE the state: a stale live state cannot vouch for a CLI that is known to be gone.
+ * - **A SHELL**: the CLI is no longer in the pane — Eco hibernated it, it was paused, it died
+ * unaccounted for (DROPPED), or it announced its own exit (`/exit`, `/quit`, Ctrl+D → SessionEnd,
+ * which Canvas records as `state: undefined` + `sessionEnded`). The message would be EXECUTED as
+ * a shell command. Hibernation even leaves `state` at `done`, and an exit leaves it undefined, so
+ * the state alone reads "sendable" either way; this check therefore ranks ABOVE the state.
*
- * No hook knowledge (`state` undefined, no flags — a custom agent, a session with no event yet in
- * this app run) is ALLOWED: unknown is not unsafe, and refusing it would take the composer away
+ * Whether the CLI is in the pane is NOT decided here: it is `agentProcessInPane`
+ * (`terminal/live-work.ts`), the one rule the memory levers already use. A second copy of that flag
+ * set would drift the day a new "CLI gone" flag lands. The specific kinds below only pick the
+ * sentence; an unrecognised "not in pane" still refuses, as the generic `exited`.
+ *
+ * No hook knowledge (`state` undefined and no flags — a custom agent, a session with no event yet
+ * in this app run) is ALLOWED: unknown is not unsafe, and refusing it would take the composer away
* from every such node. Only `done` is a positive "input box is up".
*/
-export function chatSendRefusal(s: ChatGateStatus): ChatSendRefusal {
- if (s.dropped) return 'dropped'
- if (s.paused) return 'paused'
- if (s.hibernated) return 'asleep'
+export function chatSendRefusal(agentId: string, s: ChatGateStatus): ChatSendRefusal {
+ if (!agentProcessInPane(agentId, s)) {
+ if (s.dropped) return 'dropped'
+ if (s.paused) return 'paused'
+ if (s.hibernated) return 'asleep'
+ return 'exited'
+ }
if (s.state === 'working') return 'working'
if (s.state === 'waiting' || s.state === 'blocked') return 'dialog'
return null
}
-export function canSendFromChat(s: ChatGateStatus): boolean {
- return chatSendRefusal(s) === null
+export function canSendFromChat(agentId: string, s: ChatGateStatus): boolean {
+ return chatSendRefusal(agentId, s) === null
}
/**
@@ -52,8 +62,9 @@ export function canSendFromChat(s: ChatGateStatus): boolean {
* A dialog points back at the terminal through the chord actually bound to the markdown/chat
* toggle (`chip`, from `chipFor('node.toggleMarkdown')`); `''` = unbound, and then the text names
* the action instead of promising a chord that never fires. A shell-owned pane names the header
- * chip that resumes it — the same SLEEPING / PAUSED / DROPPED chip whose click runs the wake,
- * visible above this panel. A write failure (`readonly`) outranks everything: no state change will
+ * chip that resumes it — the same SLEEPING / PAUSED / DROPPED chip whose click runs the wake, shown
+ * in the header of the canvas node and of the kanban card modal alike (hence "the header"). An
+ * exited CLI has no such chip: it is relaunched in the terminal itself. A write failure (`readonly`) outranks everything: no state change will
* make that session writable.
*/
export function chatComposerPlaceholder({
@@ -76,11 +87,13 @@ export function chatComposerPlaceholder({
? `${agentLabel} is waiting for an answer in the terminal — press ${chip} to answer there`
: `${agentLabel} is waiting for an answer in the terminal — switch back to the terminal to answer`
case 'asleep':
- return `${agentLabel} is asleep to save memory — click SLEEPING in the node header to resume it`
+ return `${agentLabel} is asleep to save memory — click SLEEPING in the header to resume it`
case 'paused':
- return `${agentLabel} is paused — click PAUSED in the node header to resume it`
+ return `${agentLabel} is paused — click PAUSED in the header to resume it`
case 'dropped':
- return `${agentLabel} is no longer running in this terminal — click DROPPED in the node header to resume it`
+ return `${agentLabel} is no longer running in this terminal — click DROPPED in the header to resume it`
+ case 'exited':
+ return `${agentLabel} has exited — relaunch it in the terminal`
case null:
return `Message ${agentLabel}… (Enter to send, Shift+Enter for a new line)`
}
diff --git a/src/renderer/nodes/ChatPanel.sendGate.test.tsx b/src/renderer/nodes/ChatPanel.sendGate.test.tsx
index 7328f9dff..ed23fbf87 100644
--- a/src/renderer/nodes/ChatPanel.sendGate.test.tsx
+++ b/src/renderer/nodes/ChatPanel.sendGate.test.tsx
@@ -36,7 +36,7 @@ let root: Root
function setAgentState(
state: 'working' | 'waiting' | 'blocked' | 'done' | undefined,
- extra: { hibernated?: boolean } = {}
+ extra: { hibernated?: boolean; sessionEnded?: boolean } = {}
): void {
useAgentStatus.setState((s) => ({
byId: { ...s.byId, [NODE]: { ...(s.byId[NODE] ?? {}), state, ...extra } as (typeof s.byId)[string] }
@@ -124,4 +124,22 @@ describe('ChatPanel send gate', () => {
})
expect(sendText).not.toHaveBeenCalled()
})
+
+ it('disables the composer after the CLI exited (/exit, Ctrl+D): state undefined, a SHELL owns the pane', async () => {
+ setAgentState(undefined, { sessionEnded: true })
+ const ta = await mount()
+ expect(ta.disabled).toBe(true)
+ expect(ta.placeholder).toMatch(/has exited/)
+ })
+
+ it('re-reads the exit at send time too', async () => {
+ setAgentState('done')
+ const ta = await mount()
+ await act(async () => type(ta, 'rm -rf build'))
+ await act(async () => {
+ setAgentState(undefined, { sessionEnded: true })
+ enter(ta)
+ })
+ expect(sendText).not.toHaveBeenCalled()
+ })
})
diff --git a/src/renderer/nodes/ChatPanel.tsx b/src/renderer/nodes/ChatPanel.tsx
index 5ecc78800..15e0135ee 100644
--- a/src/renderer/nodes/ChatPanel.tsx
+++ b/src/renderer/nodes/ChatPanel.tsx
@@ -101,6 +101,7 @@ export function ChatPanel({
const hibernated = useAgentStatus((s) => s.byId[nodeId]?.hibernated)
const paused = useAgentStatus((s) => s.byId[nodeId]?.paused)
const dropped = useAgentStatus((s) => s.byId[nodeId]?.dropped)
+ const sessionEnded = useAgentStatus((s) => s.byId[nodeId]?.sessionEnded)
const customAgents = useSettings((s) => s.settings.customAgents)
const msgsRef = useRef<HTMLDivElement>(null)
const prevState = useRef(state)
@@ -172,15 +173,15 @@ export function ChatPanel({
}
// Not just `working`: a TUI dialog (`waiting`/`blocked`) would be ANSWERED by sendText's Enter,
- // and a hibernated/paused/dropped node's pane is a SHELL that would execute the text.
- const refusal = chatSendRefusal({ state, hibernated, paused, dropped })
+ // and a pane whose CLI is gone (hibernated/paused/dropped/exited) is a SHELL that would execute it.
+ const refusal = chatSendRefusal(agentId, { state, hibernated, paused, dropped, sessionEnded })
const agentLabel = chatAgentLabel(agentId, customAgents)
const send = useCallback(async () => {
const text = input.trim()
// 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(useAgentStatus.getState().byId[nodeId] ?? {}) !== null) return
+ if (!text || chatSendRefusal(agentId, useAgentStatus.getState().byId[nodeId] ?? {}) !== null) 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 } }))
@@ -195,7 +196,7 @@ export function ChatPanel({
justSentRef.current = true
setMessages((m) => [...m, { role: 'user', parts: [{ kind: 'text', text }] }])
setInput('')
- }, [api, input, nodeId])
+ }, [api, input, nodeId, agentId])
const onKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
// Shift+Enter falls through to the textarea's own newline; an IME commit is not a send.