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): a transport that throws ends the delivery instead of stranding it

enese3d850a25afb
diff --git a/src/renderer/terminal/command-delivery.test.ts b/src/renderer/terminal/command-delivery.test.ts
index fb81f7d91..545cee6a0 100644
--- a/src/renderer/terminal/command-delivery.test.ts
+++ b/src/renderer/terminal/command-delivery.test.ts
@@ -147,6 +147,68 @@ describe('deliverCommand', () => {
expect(ends).toBe(1)
})
+ // ── A transport that throws ────────────────────────────────────────────────────────────
+ // `io.write` is unguarded all the way down to the relay client's `ws.send`, which throws
+ // InvalidStateError while the socket is still CONNECTING. Every one of these used to STRAND the
+ // delivery: `done` stayed false, so `onSettled` never fired and whoever awaited it (the in-place
+ // restart) waited forever — node locked out, bulk run hung, no summary.
+
+ /** Throws from write() on the nth write (1-based), succeeds otherwise. */
+ function throwingIo(failOn: (d: string, n: number) => boolean) {
+ const writes: string[] = []
+ let cb: ((chunk: string) => void) | undefined
+ return {
+ writes,
+ emit: (chunk: string) => cb?.(chunk),
+ io: {
+ write: (d: string) => {
+ writes.push(d)
+ if (failOn(d, writes.length))
+ throw new DOMException('Still in CONNECTING state.', 'InvalidStateError')
+ },
+ onData: (fn: (chunk: string) => void) => {
+ cb = fn
+ return () => {
+ cb = undefined
+ }
+ }
+ }
+ }
+ }
+
+ it('settles the delivery when the retry write throws, instead of stranding it', () => {
+ const f = throwingIo((d) => d === '\x15')
+ let ends = 0
+ deliverCommand(f.io, CMD, () => (ends += 1))
+ vi.advanceTimersByTime(VERIFY_TIMEOUT_MS) // echo never came → kill-line → throw
+ expect(ends).toBe(1)
+ expect(vi.getTimerCount()).toBe(0) // no retry chain left behind
+ vi.advanceTimersByTime(VERIFY_TIMEOUT_MS * DELIVERY_ATTEMPTS)
+ expect(f.writes.filter((w) => w === '\r')).toHaveLength(0) // nothing more reaches the transport
+ })
+
+ it('settles — and leaves no live retry chain — when the very first write throws', () => {
+ const f = throwingIo((_d, n) => n === 1)
+ let ends = 0
+ expect(() => deliverCommand(f.io, CMD, () => (ends += 1))).toThrow('CONNECTING')
+ // The caller sees the failure (the restart counts it), but the delivery is OVER: no timer that
+ // would rewrite `claude --resume <sid>` into the pane seconds later, under whatever the user
+ // typed next.
+ expect(ends).toBe(1)
+ expect(vi.getTimerCount()).toBe(0)
+ vi.advanceTimersByTime(VERIFY_TIMEOUT_MS * DELIVERY_ATTEMPTS)
+ expect(f.writes).toEqual([CMD])
+ })
+
+ it('does not let a throwing Enter escape into the echo listener', () => {
+ const f = throwingIo((d) => d === '\r')
+ let ends = 0
+ deliverCommand(f.io, CMD, () => (ends += 1))
+ expect(() => f.emit(CMD)).not.toThrow() // the throw would surface inside the PTY data callback
+ expect(ends).toBe(1) // the line was written and verified; only Enter was lost
+ expect(vi.getTimerCount()).toBe(0)
+ })
+
it('ignores echo arriving after submit (no double Enter)', () => {
const f = fakeIo()
deliverCommand(f.io, CMD)
diff --git a/src/renderer/terminal/command-delivery.ts b/src/renderer/terminal/command-delivery.ts
index a67b21e08..40d604a28 100644
--- a/src/renderer/terminal/command-delivery.ts
+++ b/src/renderer/terminal/command-delivery.ts
@@ -13,7 +13,9 @@ 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
-const KILL_LINE = '\x15' // Ctrl-U — clear the pending input line before a rewrite
+/** 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'
// CSI (\x1b[...X), OSC (\x1b]...BEL|ST) and single-char ESC sequences.
// eslint-disable-next-line no-control-regex
@@ -57,12 +59,37 @@ export function deliverCommand(io: DeliveryIo, cmd: string, onSettled?: () => vo
unsub?.()
onSettled?.()
}
+ /**
+ * Every write goes through here. `io.write` is unguarded all the way down to the relay client's
+ * `ws.send`, which throws InvalidStateError while the socket is still CONNECTING — and a throw
+ * used to STRAND the delivery: it killed the retry callback before `submit()`, so `done` stayed
+ * false, `onSettled` never fired, and whoever awaited it (the in-place restart) waited forever —
+ * that node locked out of restarts for the rest of the app's run and the bulk loop hung with no
+ * summary. A transport that cannot be written to ENDS the delivery instead: `finish()` first
+ * (clearing the retry chain, so no rewrite lands in the pane seconds later, spliced under
+ * whatever the user typed meanwhile), then report.
+ *
+ * `propagate` is for the FIRST write only, the one still on the caller's stack: there the throw
+ * is the caller's answer — the restart counts it as a failure and tells the user to check the
+ * pane. Later writes happen on timers and inside the PTY data callback, where a throw has
+ * nowhere to go but the transport itself, so they are contained.
+ */
+ const write = (data: string, propagate = false): boolean => {
+ try {
+ io.write(data)
+ return true
+ } catch (e) {
+ finish()
+ if (propagate) throw e
+ return false
+ }
+ }
// Close the delivery BEFORE writing Enter: an io whose write echoes back synchronously (the
// in-place restart choreography feeds one) would otherwise re-enter the listener below while
// the tail still matches, and submit forever.
const submit = (): void => {
finish()
- io.write('\r')
+ write('\r')
}
const tryOnce = (): void => {
if (done) return
@@ -76,10 +103,10 @@ export function deliverCommand(io: DeliveryIo, cmd: string, onSettled?: () => vo
submit() // fail-open: unverified submit beats a never-launched agent
return
}
- io.write(KILL_LINE)
+ if (!write(KILL_LINE)) return // transport gone — the delivery is over, not stuck
tryOnce()
}, VERIFY_TIMEOUT_MS)
- io.write(cmd)
+ write(cmd, attempt === 1)
}
unsub = io.onData((chunk) => {