From c8df5403b2ecb1a8623689cf50df73834cd9138f Mon Sep 17 00:00:00 2001 From: Jinwoo-H Date: Mon, 27 Apr 2026 20:38:44 -0400 Subject: [PATCH] fix(orchestration): harden push-on-idle delivery against stale PTY Move markAsRead after the delayed Enter is confirmed, re-check leaf.writable before the second write, and wrap in try/catch. If the terminal closes during the 500ms paste delay, messages stay unread and will be re-delivered on the next idle transition. Co-authored-by: Orca --- src/main/runtime/orca-runtime.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index b7f3fc989..b221afcff 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -1923,15 +1923,29 @@ export class OrcaRuntimeService { const payload = formatMessagesForInjection(unread) const wrote = this.ptyController?.write(leaf.ptyId, payload) ?? false - if (wrote) { - this._orchestrationDb.markAsRead(unread.map((m) => m.id)) - // Why: Claude Code treats large single PTY writes as paste events and - // swallows a \r included in the same write. Send Enter separately after - // a delay so the agent processes the pasted message first. - setTimeout(() => { - this.ptyController?.write(leaf.ptyId, '\r') - }, 500) + if (!wrote) { + return } + + // Why: Claude Code treats large single PTY writes as paste events and + // swallows a \r included in the same write. Send Enter separately after + // a delay so the agent processes the pasted message first. Mark messages + // as read only after \r is confirmed, so failed deliveries stay queued. + const ptyId = leaf.ptyId + setTimeout(() => { + try { + if (!leaf.writable) { + return + } + const submitted = this.ptyController?.write(ptyId, '\r') ?? false + if (submitted) { + this._orchestrationDb?.markAsRead(unread.map((m) => m.id)) + } + } catch { + // Terminal may have closed during the delay — messages stay unread + // and will be re-delivered on the next idle transition. + } + }, 500) } private resolveWaiter(waiter: TerminalWaiter, result: RuntimeTerminalWait): void {