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(terminal): verify both ends of the launch line, not just its tail

Erik Zwep801163125588
diff --git a/src/renderer/terminal/command-delivery.test.ts b/src/renderer/terminal/command-delivery.test.ts
index 545cee6a0..bd5feb7a2 100644
--- a/src/renderer/terminal/command-delivery.test.ts
+++ b/src/renderer/terminal/command-delivery.test.ts
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
DELIVERY_ATTEMPTS,
+ KILL_LINE,
VERIFY_TIMEOUT_MS,
cleanEcho,
deliverCommand,
@@ -64,6 +65,15 @@ describe('echoedIntact', () => {
it('does not match a truncated echo (flush ate the tail)', () => {
expect(echoedIntact(CMD.slice(0, -6), CMD)).toBe(false)
})
+ it('does not match an echo that lost its head (rc file read ate the first chars)', () => {
+ expect(echoedIntact(`% ${CMD.slice(1)}`, CMD)).toBe(false)
+ })
+ it('tolerates redraw text interleaved mid-line — the command need not be contiguous', () => {
+ // A ZLE re-wrap can splice printable prompt text into the echoed line. Both ENDS are still
+ // there, so this must verify: on the session-host leg a false negative refuses the launch.
+ const mid = Math.floor(CMD.length / 2)
+ expect(echoedIntact(`% ${CMD.slice(0, mid)}% ${CMD.slice(mid)}`, CMD)).toBe(true)
+ })
})
describe('deliverCommand', () => {
@@ -80,6 +90,19 @@ describe('deliverCommand', () => {
expect(f.writes).toEqual([CMD, '\r'])
})
+ it('does not submit a line whose head the shell never echoed (#556)', () => {
+ const f = fakeIo()
+ deliverCommand(f.io, CMD)
+ // An rc file reading the same tty during startup (oh-my-zsh's update prompt) swallows the
+ // first character, so the echo is missing its head while the TAIL is fully intact.
+ f.emit(`% ${CMD.slice(1)}`)
+ expect(f.writes).toEqual([CMD]) // no '\r': submitting here runs a mangled command
+ // The rewrite lands after the rc prompt consumed its answer, and then verifies.
+ vi.advanceTimersByTime(VERIFY_TIMEOUT_MS)
+ f.emit(`% ${CMD}`)
+ expect(f.writes).toEqual([CMD, KILL_LINE, CMD, '\r'])
+ })
+
it('kills the line and rewrites when the echo never completes, then succeeds', () => {
const f = fakeIo()
deliverCommand(f.io, CMD)
diff --git a/src/renderer/terminal/command-delivery.ts b/src/renderer/terminal/command-delivery.ts
index 40d604a28..d8016de8e 100644
--- a/src/renderer/terminal/command-delivery.ts
+++ b/src/renderer/terminal/command-delivery.ts
@@ -3,16 +3,13 @@
// rc/ZLE setup resets the tty with a FLUSH that can eat part of the queued line, and a
// mangled line submitted anyway strands the shell at `quote>` (field report: 3 spawned
// team agents, none started, each needed a manual `'` + Enter). So: write WITHOUT Enter,
-// wait until the shell has echoed the tail of the line back, THEN submit. A verify timeout
+// wait until the shell has echoed both ENDS of the line back, THEN submit. A verify timeout
// kills the pending line (Ctrl-U) and rewrites; the LAST attempt submits unverified —
// fail-open, a terminal whose echo we can't recognize must never block the launch (that
// worst case is exactly the pre-fix behavior).
export const VERIFY_TIMEOUT_MS = 2000
export const DELIVERY_ATTEMPTS = 3
-/** Long enough to be unambiguous in the echo stream, short enough that a ZLE wrap/redraw
- * sequence interleaved mid-line rarely lands inside the matched window. */
-export const ECHO_TAIL_CHARS = 24
/** Ctrl-U — clear the pending input line before a rewrite. Exported because the in-place restart
* choreography clears the line the same way before typing its exit command (agent-restart.ts). */
export const KILL_LINE = '\x15'
@@ -28,9 +25,33 @@ export function cleanEcho(chunk: string): string {
return chunk.replace(ESC_SEQ, '').replace(/[\r\n]/g, '')
}
-/** Has the shell echoed the full line? Tail-match: the head is polluted by the prompt. */
+/** Long enough to be unambiguous in the echo stream, short enough that a ZLE wrap/redraw
+ * sequence interleaved mid-line rarely lands inside either matched window. */
+export const ECHO_EDGE_CHARS = 24
+
+/**
+ * Has the shell echoed the line? BOTH ENDS must appear, each as its own substring match.
+ *
+ * Tail-only (the original) was blind to a lost HEAD: an rc file that reads the same tty during
+ * startup swallows leading characters — oh-my-zsh's update prompt does exactly this — and the
+ * shell then echoes a line whose tail is fully intact, so verification passed and Enter
+ * submitted a mangled command the shell ran as garbage (#556).
+ *
+ * Requiring the WHOLE command contiguously would catch that, but it is too strict: a ZLE
+ * redraw can interleave printable prompt text inside the echoed line, so the cleaned stream
+ * need not contain the command in one piece. That matters asymmetrically — the renderer would
+ * merely stall and fail open, but the session-host leg clears the line and REFUSES the launch,
+ * turning a cosmetic false negative into a node that never starts.
+ *
+ * Two edge windows keep both properties: a truncated head or tail fails, while junk in the
+ * middle (or before the line) is tolerated. Commands shorter than 2×ECHO_EDGE_CHARS simply
+ * compare as a whole, which is the strictest form and safe at that length.
+ */
export function echoedIntact(cleanedSoFar: string, cmd: string): boolean {
- return cleanedSoFar.includes(cmd.slice(-ECHO_TAIL_CHARS))
+ return (
+ cleanedSoFar.includes(cmd.slice(0, ECHO_EDGE_CHARS)) &&
+ cleanedSoFar.includes(cmd.slice(-ECHO_EDGE_CHARS))
+ )
}
export interface DeliveryIo {
diff --git a/src/session-host/session.ts b/src/session-host/session.ts
index eb9e215ce..8454231f0 100644
--- a/src/session-host/session.ts
+++ b/src/session-host/session.ts
@@ -14,8 +14,8 @@ const SHELL_QUIET_CAP_MS = 1_500
const POST_LAUNCH_READY_CAP_MS = 5_000
const LAUNCH_READINESS_POLL_MS = 100
const VERIFY_TIMEOUT_MS = 2_000
+const ECHO_EDGE_CHARS = 24
const DELIVERY_ATTEMPTS = 3
-const ECHO_TAIL_CHARS = 24
const KILL_LINE = '\x15'
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
@@ -581,7 +581,15 @@ export class HostSession {
dataSub = this.proc.onData((chunk) => {
if (done) return
echoed = (echoed + cleanEcho(chunk)).slice(-Math.max(command.length + 256, 512))
- if (echoed.includes(command.slice(-ECHO_TAIL_CHARS))) submit()
+ // BOTH ends, not just the tail — see echoedIntact in the renderer's command-delivery for
+ // why a tail match let a head-truncated command through (#556), and why requiring the
+ // whole command contiguously would be too strict on this leg in particular: the retry
+ // exhaustion below REFUSES the launch rather than failing open.
+ if (
+ echoed.includes(command.slice(0, ECHO_EDGE_CHARS)) &&
+ echoed.includes(command.slice(-ECHO_EDGE_CHARS))
+ )
+ submit()
})
exitSub = this.proc.onExit(() =>
finish(new Error('session-host terminal exited during launch delivery'))