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(session-host): make the echo head sticky, so a bounded window cannot unsee it

Erik Zwep565a67e8562f
diff --git a/src/session-host/session.test.ts b/src/session-host/session.test.ts
index 4fb74a1d9..58e75b1a3 100644
--- a/src/session-host/session.test.ts
+++ b/src/session-host/session.test.ts
@@ -542,3 +542,74 @@ describe('HostSession emulator output watermarks', () => {
await expect(session.serialize()).resolves.toBe('good')
})
})
+
+describe('HostSession launch echo verification', () => {
+ // The launch line this leg actually carries: long enough that head and tail windows
+ // (ECHO_EDGE_CHARS = 24 each) do not overlap.
+ const COMMAND = 'claude --permission-mode auto --session-id 0f9a1c3e-5b7d-4e21-9a86-1d2c3b4a5e6f'
+
+ function deliveryProc(): { value: IPty; written: string[]; emit: (chunk: string) => void } {
+ let sink: ((d: string) => void) | undefined
+ const written: string[] = []
+ return {
+ value: {
+ pid: 42,
+ pause: vi.fn(),
+ resume: vi.fn(),
+ resize: vi.fn(),
+ write: (d: string) => {
+ written.push(d)
+ },
+ onData: (cb: (d: string) => void) => {
+ sink = cb
+ return { dispose: vi.fn() }
+ },
+ onExit: () => ({ dispose: vi.fn() })
+ } as unknown as IPty,
+ written,
+ emit: (chunk: string) => sink?.(chunk)
+ }
+ }
+
+ function deliver(session: HostSession, command: string): Promise<void> {
+ return (
+ session as unknown as { deliverVerifiedCommand(c: string): Promise<void> }
+ ).deliverVerifiedCommand(command)
+ }
+
+ it('submits when the head scrolled out of the bounded window before the tail arrived', async () => {
+ const proc = deliveryProc()
+ const session = new HostSession('echo-window', SPAWN, 100, {
+ proc: proc.value,
+ term: inertTerm()
+ })
+ const delivered = deliver(session, COMMAND)
+
+ // The shell echoes the head, then a full-line ZLE redraw pushes more than the window
+ // (max(command.length + 256, 512)) of visible characters through before the tail lands.
+ // Without a sticky head the two substrings never coexist in `echoed`, all three attempts
+ // fail, and this leg REFUSES the launch — the node never starts.
+ proc.emit(COMMAND.slice(0, 32))
+ proc.emit('x'.repeat(700))
+ proc.emit(COMMAND.slice(-32))
+
+ await expect(delivered).resolves.toBeUndefined()
+ expect(proc.written.at(-1)).toBe('\r')
+ })
+
+ it('still requires the head: a tail-only echo does not submit', async () => {
+ const proc = deliveryProc()
+ const session = new HostSession('echo-tail-only', SPAWN, 100, {
+ proc: proc.value,
+ term: inertTerm()
+ })
+ // Left pending on purpose: this asserts the NON-event, and the retry timer is unref'd.
+ void deliver(session, COMMAND).catch(() => {})
+
+ proc.emit('x'.repeat(700))
+ proc.emit(COMMAND.slice(-32))
+
+ // submit() is synchronous inside the data handler, so its absence is observable now.
+ expect(proc.written).not.toContain('\r')
+ })
+})
diff --git a/src/session-host/session.ts b/src/session-host/session.ts
index 8454231f0..f4a35d954 100644
--- a/src/session-host/session.ts
+++ b/src/session-host/session.ts
@@ -530,6 +530,13 @@ export class HostSession {
let done = false
let attempt = 0
let echoed = ''
+ // The head is STICKY, because `echoed` below is a bounded window. A tail match is safe under
+ // that truncation by construction — the tail is what the window keeps — but a head is not:
+ // once a window's worth of bytes has arrived after it, the head is evicted and the two
+ // substrings need never coexist in the buffer even though both were genuinely echoed. This
+ // leg REFUSES on exhausted retries rather than failing open, so an evicted head would not
+ // merely retry, it would stop the node from starting.
+ let sawHead = false
let timer: ReturnType<typeof setTimeout> | undefined
let dataSub: { dispose(): void } | undefined
let exitSub: { dispose(): void } | undefined
@@ -563,6 +570,7 @@ export class HostSession {
if (done) return
attempt++
echoed = ''
+ sawHead = false
timer = setTimeout(() => {
if (done) return
if (attempt >= DELIVERY_ATTEMPTS) {
@@ -580,16 +588,16 @@ export class HostSession {
}
dataSub = this.proc.onData((chunk) => {
if (done) return
- echoed = (echoed + cleanEcho(chunk)).slice(-Math.max(command.length + 256, 512))
// 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()
+ const merged = echoed + cleanEcho(chunk)
+ // Tested BEFORE the window is trimmed, so a head that arrived in an earlier chunk still
+ // counts once it has scrolled out. Same memory bound as before.
+ sawHead ||= merged.includes(command.slice(0, ECHO_EDGE_CHARS))
+ echoed = merged.slice(-Math.max(command.length + 256, 512))
+ if (sawHead && echoed.includes(command.slice(-ECHO_EDGE_CHARS))) submit()
})
exitSub = this.proc.onExit(() =>
finish(new Error('session-host terminal exited during launch delivery'))