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(delivery): centralize shell line-clear sequence and make renderer host-aware

abhirupone9d6e1402bda7
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index a4190dd1a..098466dd8 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -109,10 +109,9 @@ import {
import {
cleanEcho,
deliverCommand,
- KILL_LINE,
- WINDOWS_KILL_LINE,
type DeliveryIo
} from '../terminal/command-delivery'
+import { terminalKillLine } from '../terminal/terminal-kill-line'
import {
RESUME_MISS_WINDOW_MS,
detectsResumeMiss,
@@ -1565,10 +1564,14 @@ export function TerminalNode({
!remoteSession &&
(data.cwd as string | undefined) !== parentWtPath
const getTerminalKillLine = (): string => {
- const isWindows =
- (corePlatformRef.current === 'win32' || (isWindowsPlatform() && session.source === 'local')) &&
- !remoteSession
- return isWindows ? WINDOWS_KILL_LINE : KILL_LINE
+ return terminalKillLine({
+ source: session.source,
+ browserRuntime: isBrowserRuntime(),
+ viewerWindows: isWindowsPlatform(),
+ corePlatform: corePlatformRef.current,
+ remoteSession,
+ shell: data.shell || useSettings.getState().settings.defaultShell || undefined
+ })
}
const status = useAgentStatus((s) => s.byId[id])
/**
diff --git a/src/renderer/terminal/agent-restart.ts b/src/renderer/terminal/agent-restart.ts
index 8a5d5cb09..60e457cc8 100644
--- a/src/renderer/terminal/agent-restart.ts
+++ b/src/renderer/terminal/agent-restart.ts
@@ -205,7 +205,10 @@ export async function performExitPhase(d: {
// what command-delivery.ts already relies on for its rewrites. Each agent added to that table
// inherits this assumption; only a device check retires it, per agent. If a TUI binds Ctrl-U to
// something else this becomes one stray keystroke before the exit command — no worse than
- // today's blind write. Belongs in the manual test matrix.
+ // CRITICAL LOAD-BEARING SPLIT: This line-clear writes into a pane owned by an AGENT TUI, not
+ // a shell. A lone Escape (\x1b) into a live agent is the user-interrupt gesture (cancels turns/thinking),
+ // whereas \x15 is the safe line-clear attempt. Keep \x15 here even on Windows; WINDOWS_KILL_LINE
+ // (\x1b) is strictly for shell panes (command delivery retry and hibernation wake).
d.io.write(KILL_LINE)
// opencode's TUI does not submit when text and CR arrive in the same input burst
// (batched-input handling). Measured on 1.18.18-1.18.25, Linux, tmux, isolated socket:
diff --git a/src/renderer/terminal/command-delivery.test.ts b/src/renderer/terminal/command-delivery.test.ts
index 34541840c..6b44787ed 100644
--- a/src/renderer/terminal/command-delivery.test.ts
+++ b/src/renderer/terminal/command-delivery.test.ts
@@ -58,6 +58,11 @@ describe('cleanEcho', () => {
const noisy = '\x1b[1;32mprompt\x1b[0m \x1b]0;title\x07ec' + '\r\n' + 'ho text\x1b[K'
expect(cleanEcho(noisy)).toBe('prompt echo text')
})
+
+ it('strips PSReadLine CSI erase sequences (e.g. \\x1b[9X)', () => {
+ const psreadlineErase = '\x1b[18X' + CMD
+ expect(cleanEcho(psreadlineErase)).toBe(CMD)
+ })
})
describe('echoedIntact', () => {
diff --git a/src/renderer/terminal/command-delivery.ts b/src/renderer/terminal/command-delivery.ts
index 11ee38ade..decc146b9 100644
--- a/src/renderer/terminal/command-delivery.ts
+++ b/src/renderer/terminal/command-delivery.ts
@@ -15,12 +15,11 @@
// @shared/canonical-line.
import { fitsLaunchLine } from '@shared/canonical-line'
+import { KILL_LINE, WINDOWS_KILL_LINE } from '@shared/shell-kill-line'
export const VERIFY_TIMEOUT_MS = 2000
export const DELIVERY_ATTEMPTS = 3
-export const KILL_LINE = '\x15'
-/** Escape — clear the pending input line in Windows shells (PowerShell, cmd.exe). */
-export const WINDOWS_KILL_LINE = '\x1b'
+export { KILL_LINE, WINDOWS_KILL_LINE }
export interface DeliverCommandOptions {
killLine?: string
diff --git a/src/renderer/terminal/terminal-kill-line.test.ts b/src/renderer/terminal/terminal-kill-line.test.ts
new file mode 100644
index 000000000..6f8de103d
--- /dev/null
+++ b/src/renderer/terminal/terminal-kill-line.test.ts
@@ -0,0 +1,97 @@
+import { describe, expect, it } from 'vitest'
+import { KILL_LINE, WINDOWS_KILL_LINE } from '@shared/shell-kill-line'
+import { terminalKillLine, type TerminalKillLineFacts } from './terminal-kill-line'
+
+const facts = (overrides: Partial<TerminalKillLineFacts> = {}): TerminalKillLineFacts => ({
+ source: 'local',
+ browserRuntime: false,
+ viewerWindows: false,
+ corePlatform: null,
+ remoteSession: false,
+ shell: undefined,
+ ...overrides
+})
+
+describe('terminalKillLine', () => {
+ it('uses the local desktop viewer only where viewer and host are the same machine', () => {
+ expect(terminalKillLine(facts({ viewerWindows: true }))).toBe(WINDOWS_KILL_LINE)
+ expect(terminalKillLine(facts({ viewerWindows: false }))).toBe(KILL_LINE)
+ })
+
+ it('uses the Server Edition host rather than the browser OS', () => {
+ // Windows browser pointed at a Linux server must NOT write Escape into bash
+ expect(
+ terminalKillLine(
+ facts({ source: 'local', browserRuntime: true, viewerWindows: true, corePlatform: 'linux' })
+ )
+ ).toBe(KILL_LINE)
+ // Non-Windows browser pointed at a Windows Server Edition writes Escape
+ expect(
+ terminalKillLine(
+ facts({ source: 'local', browserRuntime: true, viewerWindows: false, corePlatform: 'win32' })
+ )
+ ).toBe(WINDOWS_KILL_LINE)
+ })
+
+ it('uses the relay host rather than the relay guest OS', () => {
+ expect(
+ terminalKillLine(facts({ source: 'relay', viewerWindows: true, corePlatform: 'darwin' }))
+ ).toBe(KILL_LINE)
+ expect(
+ terminalKillLine(facts({ source: 'relay', viewerWindows: false, corePlatform: 'win32' }))
+ ).toBe(WINDOWS_KILL_LINE)
+ })
+
+ it('treats a remote session as POSIX even when desktop core is Windows', () => {
+ expect(
+ terminalKillLine(facts({ viewerWindows: true, corePlatform: 'win32', remoteSession: true }))
+ ).toBe(KILL_LINE)
+ })
+
+ it('fails closed to KILL_LINE (\\x15) when core platform read has not completed on server or relay', () => {
+ expect(
+ terminalKillLine(facts({ source: 'local', browserRuntime: true, viewerWindows: true }))
+ ).toBe(KILL_LINE)
+ expect(terminalKillLine(facts({ source: 'server', viewerWindows: true }))).toBe(KILL_LINE)
+ expect(terminalKillLine(facts({ source: 'relay', viewerWindows: false }))).toBe(KILL_LINE)
+ })
+
+ it('respects shell executable on Windows platform (e.g. git-bash uses \\x15)', () => {
+ expect(
+ terminalKillLine(
+ facts({
+ viewerWindows: true,
+ corePlatform: 'win32',
+ shell: 'C:\\Program Files\\Git\\bin\\bash.exe'
+ })
+ )
+ ).toBe(KILL_LINE)
+ expect(
+ terminalKillLine(
+ facts({
+ viewerWindows: true,
+ corePlatform: 'win32',
+ shell: 'bash'
+ })
+ )
+ ).toBe(KILL_LINE)
+ expect(
+ terminalKillLine(
+ facts({
+ viewerWindows: true,
+ corePlatform: 'win32',
+ shell: 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe'
+ })
+ )
+ ).toBe(WINDOWS_KILL_LINE)
+ expect(
+ terminalKillLine(
+ facts({
+ viewerWindows: true,
+ corePlatform: 'win32',
+ shell: 'pwsh.exe'
+ })
+ )
+ ).toBe(WINDOWS_KILL_LINE)
+ })
+})
diff --git a/src/renderer/terminal/terminal-kill-line.ts b/src/renderer/terminal/terminal-kill-line.ts
new file mode 100644
index 000000000..2fb414b9c
--- /dev/null
+++ b/src/renderer/terminal/terminal-kill-line.ts
@@ -0,0 +1,44 @@
+import type { SessionSource } from '../session/session'
+import {
+ KILL_LINE,
+ shellKillLineSequence
+} from '@shared/shell-kill-line'
+
+export interface TerminalKillLineFacts {
+ /** Which core owns the active tab's filesystem and process lifecycle. */
+ source: SessionSource
+ /** True for the Server Edition's websocket-backed browser tab (currently source `local`). */
+ browserRuntime: boolean
+ /** The browser/window's OS. It is authoritative only for the local desktop core. */
+ viewerWindows: boolean
+ /** `process.platform` reported by the core; null means it was not observed yet. */
+ corePlatform: string | null
+ /** True if this terminal targets an SSH project or remote session node. */
+ remoteSession: boolean
+ /** Optional shell configured on the node or resolved default. */
+ shell?: string | null
+}
+
+/**
+ * Choose the line-clear sequence for a terminal node (Ctrl-U vs Escape).
+ *
+ * A browser's OS is not evidence about a Server Edition or relay host: a Windows browser
+ * pointed at a Linux Server Edition must not write Escape into bash/readline (where Escape is
+ * the meta-prefix). When the core platform has not been observed yet, fail closed to `\x15`.
+ * Remote sessions (SSH project or standalone SSH) target POSIX environments and use `\x15`.
+ */
+export function terminalKillLine(facts: TerminalKillLineFacts): string {
+ if (facts.remoteSession) return KILL_LINE
+
+ const platform =
+ facts.corePlatform ??
+ (facts.source === 'local' && !facts.browserRuntime
+ ? facts.viewerWindows
+ ? 'win32'
+ : 'posix'
+ : null)
+
+ if (!platform) return KILL_LINE
+
+ return shellKillLineSequence(undefined, facts.shell, platform)
+}
diff --git a/src/session-host/session.ts b/src/session-host/session.ts
index 061ce2bf6..1a70204f3 100644
--- a/src/session-host/session.ts
+++ b/src/session-host/session.ts
@@ -16,36 +16,13 @@ const LAUNCH_READINESS_POLL_MS = 100
const VERIFY_TIMEOUT_MS = 2_000
const ECHO_EDGE_CHARS = 24
const DELIVERY_ATTEMPTS = 3
-export const KILL_LINE = '\x15'
-export const WINDOWS_KILL_LINE = '\x1b'
+import {
+ KILL_LINE,
+ WINDOWS_KILL_LINE,
+ shellKillLineSequence
+} from '../shared/shell-kill-line'
-export function shellKillLineSequence(
- dialect?: SessionHostShellDialect,
- shellExecutableName?: string,
- platform: NodeJS.Platform = process.platform
-): string {
- if (dialect === 'pwsh' || dialect === 'windows-powershell' || dialect === 'cmd') {
- return WINDOWS_KILL_LINE
- }
- if (dialect === 'posix') {
- return KILL_LINE
- }
- const exe = (shellExecutableName ?? '').toLowerCase()
- if (
- exe === 'powershell' ||
- exe === 'powershell.exe' ||
- exe === 'pwsh' ||
- exe === 'pwsh.exe' ||
- exe === 'cmd' ||
- exe === 'cmd.exe'
- ) {
- return WINDOWS_KILL_LINE
- }
- if (exe === 'bash' || exe === 'bash.exe' || exe === 'zsh' || exe === 'sh' || exe === 'fish') {
- return KILL_LINE
- }
- return platform === 'win32' ? WINDOWS_KILL_LINE : KILL_LINE
-}
+export { KILL_LINE, WINDOWS_KILL_LINE, shellKillLineSequence }
const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
// eslint-disable-next-line no-control-regex
diff --git a/src/shared/shell-kill-line.test.ts b/src/shared/shell-kill-line.test.ts
new file mode 100644
index 000000000..eb4042e42
--- /dev/null
+++ b/src/shared/shell-kill-line.test.ts
@@ -0,0 +1,59 @@
+import { describe, expect, it } from 'vitest'
+import {
+ KILL_LINE,
+ WINDOWS_KILL_LINE,
+ shellKillLineSequence
+} from './shell-kill-line'
+
+describe('shellKillLineSequence', () => {
+ it('returns \\x1b for Windows PowerShell, pwsh, and cmd dialects', () => {
+ expect(shellKillLineSequence('windows-powershell')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence('pwsh')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence('cmd')).toBe(WINDOWS_KILL_LINE)
+ })
+
+ it('returns \\x15 for posix dialect even on Windows platform', () => {
+ expect(shellKillLineSequence('posix', undefined, 'win32')).toBe(KILL_LINE)
+ expect(shellKillLineSequence('posix', 'bash.exe', 'win32')).toBe(KILL_LINE)
+ })
+
+ it('infers sequence from bare shell executable name when dialect is omitted', () => {
+ expect(shellKillLineSequence(undefined, 'powershell.exe')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'powershell')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'pwsh.exe')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'pwsh')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'cmd.exe')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'cmd')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'bash.exe')).toBe(KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'bash')).toBe(KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'zsh')).toBe(KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'sh')).toBe(KILL_LINE)
+ expect(shellKillLineSequence(undefined, 'fish')).toBe(KILL_LINE)
+ })
+
+ it('infers sequence from full executable path with forward and backward slashes', () => {
+ expect(shellKillLineSequence(undefined, 'C:\\Program Files\\PowerShell\\7\\pwsh.exe')).toBe(
+ WINDOWS_KILL_LINE
+ )
+ expect(shellKillLineSequence(undefined, 'C:\\Windows\\System32\\cmd.exe')).toBe(
+ WINDOWS_KILL_LINE
+ )
+ expect(shellKillLineSequence(undefined, 'C:\\Program Files\\Git\\bin\\bash.exe')).toBe(
+ KILL_LINE
+ )
+ expect(shellKillLineSequence(undefined, '/usr/local/bin/zsh')).toBe(KILL_LINE)
+ expect(shellKillLineSequence(undefined, '/bin/bash')).toBe(KILL_LINE)
+ })
+
+ it('lets dialect override executable name', () => {
+ expect(shellKillLineSequence('posix', 'powershell.exe', 'win32')).toBe(KILL_LINE)
+ expect(shellKillLineSequence('pwsh', 'bash.exe', 'linux')).toBe(WINDOWS_KILL_LINE)
+ })
+
+ it('falls back based on host platform when dialect and shell executable are unknown', () => {
+ expect(shellKillLineSequence(undefined, undefined, 'win32')).toBe(WINDOWS_KILL_LINE)
+ expect(shellKillLineSequence(undefined, undefined, 'darwin')).toBe(KILL_LINE)
+ expect(shellKillLineSequence(undefined, undefined, 'linux')).toBe(KILL_LINE)
+ expect(shellKillLineSequence(undefined, undefined, null)).toBe(KILL_LINE)
+ })
+})
diff --git a/src/shared/shell-kill-line.ts b/src/shared/shell-kill-line.ts
new file mode 100644
index 000000000..029a1c8ee
--- /dev/null
+++ b/src/shared/shell-kill-line.ts
@@ -0,0 +1,63 @@
+/**
+ * LINE-CLEAR SEQUENCES FOR SHELLS VS AGENT TUIS.
+ *
+ * Line clearing before command delivery (command-delivery.ts) or after delivery refusal
+ * targets an interactive SHELL (PowerShell, cmd.exe, bash, zsh).
+ *
+ * - POSIX shells (bash, zsh, dash) bind `\x15` (Ctrl-U) to kill the pending line in readline/ZLE.
+ * - Windows shells (PowerShell, cmd.exe) bind `\x1b` (Escape) to clear the line. In PowerShell,
+ * `\x15` is not bound by default; typing `\x15` leaves junk on the prompt and mangles retried commands.
+ *
+ * CRITICAL LOAD-BEARING SPLIT:
+ * `WINDOWS_KILL_LINE` (`\x1b`) is ONLY for shells. A live AGENT TUI (Claude Code, Codex, etc.) treats
+ * a lone `\x1b` as the INTERRUPT gesture (cancelling turns / thinking), not a line-clear gesture!
+ * Therefore, agent exit phases (`performExitPhase` in `agent-restart.ts`) write into panes owned by
+ * agent TUIs and MUST continue to write `KILL_LINE` (`\x15`), NEVER `WINDOWS_KILL_LINE` (`\x1b`).
+ */
+
+/** Ctrl-U — line-kill sequence in POSIX shells (readline/ZLE) and default agent TUI line clear. */
+export const KILL_LINE = '\x15'
+
+/** Escape — line-clear sequence in Windows native shells (PowerShell, cmd.exe). */
+export const WINDOWS_KILL_LINE = '\x1b'
+
+export type ShellKillLineDialect = 'posix' | 'pwsh' | 'windows-powershell' | 'cmd'
+
+/**
+ * Determine the appropriate line-clear sequence for a shell given its dialect, executable name,
+ * and host platform.
+ *
+ * If a dialect is specified ('pwsh', 'windows-powershell', 'cmd'), it takes precedence.
+ * If dialect is 'posix' (including git-bash / MSYS on Windows), returns `\x15`.
+ * If dialect is omitted, the executable name is checked (e.g. bash.exe vs pwsh.exe), stripping directory paths.
+ * Finally, falls back to the host platform ('win32' -> Escape, others -> Ctrl-U).
+ * Unobserved or unknown platforms fail closed to `KILL_LINE` ('\x15').
+ */
+export function shellKillLineSequence(
+ dialect?: ShellKillLineDialect | string | null,
+ shellExecutableName?: string | null,
+ platform: NodeJS.Platform | string | null = typeof process !== 'undefined' ? process.platform : null
+): string {
+ if (dialect === 'pwsh' || dialect === 'windows-powershell' || dialect === 'cmd') {
+ return WINDOWS_KILL_LINE
+ }
+ if (dialect === 'posix') {
+ return KILL_LINE
+ }
+ const raw = shellExecutableName ?? ''
+ const exe = raw.replace(/^.*[/\\]/, '').toLowerCase()
+ if (
+ exe === 'powershell' ||
+ exe === 'powershell.exe' ||
+ exe === 'pwsh' ||
+ exe === 'pwsh.exe' ||
+ exe === 'cmd' ||
+ exe === 'cmd.exe'
+ ) {
+ return WINDOWS_KILL_LINE
+ }
+ if (exe === 'bash' || exe === 'bash.exe' || exe === 'zsh' || exe === 'sh' || exe === 'fish') {
+ return KILL_LINE
+ }
+ return platform === 'win32' ? WINDOWS_KILL_LINE : KILL_LINE
+}