fix(desktop,host-service): retry terminal create-on-attach after pty-daemon stall instead of dead tab

Merged
KI
3mo ago
+884-47

Summary

  • A new terminal opened while the pty-daemon is unresponsive no longer dies permanently at the 15s daemon open timeout — the host now classifies daemon-unavailable as a transient, attach-retryable failure (new DaemonUnavailableError → retryable WS close) and the renderer's existing partysocket backoff keeps redialing until the daemon heals.
  • Existing-session attaches recover the same way.

Why / Context

User report (1.17.0→1.20.0, #ext-vori): typing in a Claude Code TUI or opening a Terminal hangs 10–15s+; new tabs stay dead. Reproduced 1:1: any pty-daemon stall ≥15s at create time produced a permanently dead tab — transport closed, never retried after the daemon recovered. The reporter's own host-service log contains the matching daemon list: timed out after 5000ms / daemon unreachable line. The "10–15s" in the report is this exact timeout.

How It Works

daemon open/list timeouts and daemon-connection failures during WS attach now map to a retryable close code instead of a terminal one; the renderer transport treats it like any transient disconnect (capped backoff with jitter, already in place). Create-on-attach idempotence (inflightCreates + session-row check) prevents duplicate sessions across retries.

Manual QA (end-to-end, dev desktop over CDP, screenshots captured)

  • BEFORE: SIGSTOP dev pty-daemon → Cmd+T → dead at 15s, still dead 60s after SIGCONT.
  • AFTER: identical steps → attached 0.25s after SIGCONT; typed echo hi echoed.
  • Healthy daemon: create in 151ms (baseline 275ms — no regression).
  • 3-tab spam → exactly 3 daemon sessions; renderer reload mid-retry → same terminal id recovers, exactly 1 session.

Testing

  • New terminal.daemon-stall-retry.node-test.ts (3 tests) + DaemonUnavailableError unit test — verified they fail with the transient classification mutated off.
  • create-on-attach (8/8) and adoption suites pass; bun run typecheck and bun run lint exit 0.

Risks / Rollout

Retry storms are bounded by the existing partysocket backoff cap. Known follow-ups (pre-existing, not touched here): terminal.create-on-attach.node-test.ts leaks a handle (reproduces on baseline); HTTP POST /terminal/sessions callers could honor the new 503.


Summary by cubic

Retries terminal create-on-attach and existing-session attaches when the pty-daemon is unreachable or stalls, avoiding permanently dead tabs after the 15s daemon-open timeout. Previously these errors terminated the transport; now the host marks them transient, the WS closes with 1013 and code "attach-retryable", and the renderer keeps retrying the same terminal id.

Key changes

  • Host-service classifies connection-level daemon failures as transient via DaemonUnavailableError (connect failure/timeout, handshake timeout, request timeout, disconnect, dead-socket writes); handshake rejections and daemon bootstrap failures remain permanent.
  • WS attach sends { code: "attach-retryable" } and closes with 1013 for transient errors; renderer keeps retrying the same terminal id, preserves existing-session rows, and skips agent teardown or respawn. POST /terminal/sessions returns 503 while the daemon is unreachable.
  • Renderer transport does not terminate on "attach-retryable"; counts every connection that closes without ever reaching attached, clears the stored daemon message when the failure mode changes, and surfaces the diagnosis from the close after max(streak, partysocket.retryCount) >= 10. Logs once per cycle; the header stays amber "Reconnecting…" while auto-retrying and turns red only when the transport is terminated.
  • Close reasons are truncated to <=123 UTF-8 bytes on character boundaries to prevent ws.close() RangeError.
  • makeDaemonPty.write swallows dead-socket throws so input handlers don’t escape uncaught.
  • Tests cover unresponsive-daemon wedge, connect-refused and dead-socket typing, handshake-reject permanency, multibyte truncation, and attach-retry diagnosis; DaemonUnavailableError is exported from DaemonClient/index.ts.

Rollout

  • Treat 503 responses from POST /terminal/sessions as retryable.

Summary by CodeRabbit

  • Bug Fixes
    • Improved terminal recovery when the host daemon is temporarily unavailable.
    • Terminal attachments now retry without closing the transport or creating duplicate sessions.
    • Existing terminal sessions are preserved during daemon interruptions.
    • REST and WebSocket clients now receive clear, retryable unavailable responses.
    • Improved retry diagnosis with clearer failure reasons after repeated attachment failures.
    • Connection indicators remain in a reconnecting state while recovery is possible.
    • WebSocket close messages are safely limited without breaking multi-byte characters.