Skip to content

fix(terminal): attach again when a page put away comes back to a stopped output - #968

Merged
devswha merged 15 commits into
mainfrom
fix/recover-hidden-output-stall
Oct 13, 2026
Merged

devswha merged 15 commits into
mainfrom
fix/recover-hidden-output-stall

Conversation

@devswha

@devswha devswha commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

Problem

A phone put away while its pane stays attached comes back to "Terminal output stopped because this device could not keep up." and a dead terminal for the whole PC. The pane stays attached in two cases: a message queued from this tab (queueWaits), or a mirrored pane (fixedGridRef). Its frozen page sends no pty-ack, so the server's 2 s stall check closes it with 4008. Unlocking the phone does not recover it. The only ways out are the banner's Reconnect link, which reloads the whole app, or picking another pane. This is stability-audit findings connection-2, history-2 and moshi-2.

Cause

  • On a 4008 close, HerdrSocket sets its stopped flag and schedules no reconnect. wake() refuses while the output is stopped (intended: no automatic reconnect loop).
  • Only attach() clears that flag. PaneTerminal.back() calls resume(), which attaches, only for a released pane. A pane that kept its attach only gets refit(), which sends a resize into a closed socket.
  • A frozen page often reads the server's close only after it resumes, after visibilitychange and back() have already run. Also, the 2 s stall clock can still run out while the page catches up on the ACKs it held back. So checking visibility when the close arrives is not enough.

Fix

  • src/lib/ws.ts:
    • New getters outputStopped and stoppedWhileHidden. The private field is renamed to stopped.
    • A 4008 counts as the hidden page's in three cases: it came while the page was hidden; it came while the page had been hidden and not yet shown; or it came within SHOWN_GRACE_MS (5 s) of the page being shown again.
    • A page that connects while hidden counts as put away until it is first shown.
    • The attach() that clears the stop also clears the mark and uses up the grace period. A second stall on the new connection while the page is in use is therefore final.
    • wake() still refuses a stopped output, as src/AGENTS.md requires.
  • src/components/PaneTerminal.tsx:
    • New reattach(). It clears stalledRef, the notice and ended, runs the resume() path (stream-order \x1bc reset, unwatch if watching, socket.attach), and restores disableStdin. The attach reconnects, and the connection's open replays every attach remembered for that PC.
    • It runs once when the page is back in use. It is called from back() when the close was read while the page was hidden. It is called from the output_stalled handler, in a microtask, when the close is read after the page was shown.
    • A 4008 while the page stays in use keeps today's behaviour: notice, no automatic reconnect.
  • The banner's Reconnect <a href="?machine=&pane="> (a full page reload) is now a <button> that runs the same in-place reattach(). The label is unchanged, so no i18n entries were added.
  • Docs: docs/terminal-flow-control.md step 5 and the QA paragraph, and the 4008 rule in src/AGENTS.md.
  • Changelog fragment changelog.d/recover-hidden-output-stall.md.

Tests

  • src/lib/ws.test.ts: four new cases. Each one fails without the fix, checked by stashing ws.ts.
    • A 4008 while hidden is marked. Showing the page reconnects nothing by itself. An attach after showing clears outputStopped, reconnects, and replays both remembered attaches.
    • A 4008 read 1 s after the page is shown again is marked. After the re-attach, a second 4008 while in use is final (stoppedWhileHidden false).
    • A 4008 long after the page came back is final. A later wake() and a hide/show do not reconnect, even 60 s later.
    • A page that connected while hidden counts as put away until it is first shown.
  • scripts/output-browser-qa.ts, real herdr, xterm and Chromium, run with bun run check run: passed.
    • The existing visible-stall flow now clicks the Reconnect button and still asserts no automatic reconnect loop.
    • A new scenario fakes visibilityState/hasFocus, puts the page away with ACKs held and the pane still attached (desktop, release-when-away off), and waits for the 4008. It then shows the page and asserts that the pane attaches again with no click, the notice goes away, and exactly one reconnect happened.
    • With the two recovery branches disabled, the new scenario times out at "shown page attaches again by itself".
    • It covers only the order where the close is read while the page is hidden. The other order is covered by the unit tests. This script is not part of CI's lanes.
  • bun run typecheck: passed. bun run test:unit: 3091 pass, 0 fail, 8 skip; this ran before the small follow-up commit, after which ws.test.ts and typecheck were run again and passed. check full/concurrent were not run.

Notes

Review follow-up (cc86059, 0303ac0)

These fix the probe and held-draft code this branch carries from #967 (#967 itself was not updated).

Fix:

  1. (high) Hiding the page during a probe released waiting sends on an unverified socket. endProbe() now records whether the probe was answered; probeSettled() lets a waiting submit, pending action or secret go only after a pong. A probe cancelled by hiding (or ended by the connection) settles them as disconnected, and the secret is cleared on that path. The secret still lives in the closure while it waits for the pong; that is inherent to waiting, and it is never sent unanswered.
  2. (high) Ctrl+Enter under modifyOtherKeys (ESC[27;5;13~) passed the held-draft guard and committed the partial line. New pure heldInputAction() in src/lib/draft.ts decides by meaning: Enter itself (captured before the remap), any encoded Enter (CSI 27;m;13~, CSI 13;m u), any line break and chords are blocked with the existing "Not sent: send or discard the held input first." note. The input line (sendTerminalLine) and the composer (submitComposerMessage, new held_input code in submitNote) also refuse while a draft is held.
  3. (medium) The pong returned before handlers ran, so a view that stored inputReady=false during the probe stayed disabled. The probe's pong now goes on to handlers after the state is updated, and PaneTerminal's 1 s poll also re-reads canInput() (covers the probe start, which has no frame).
  4. (medium) Remembered ping support could drop an older bridge after a reconnect. A snapshot that leaves out ping now ends a probe in flight as answered (trusted as before) without counting a drop. A separate deadline for a snapshot that never comes was left out: on such a socket the probe's own two pings still decide.
  5. (medium) Backspace now edits the held draft (eraseFromDraft, whole code points). A paste the draft cannot keep whole (line breaks) is refused with the same note instead of being lost silently. Line breaks are not held: Send types the draft with sendInput, so a held newline would run lines.
  6. (low, not changed) Typing during a probe goes to the held draft. Buffering keys and flushing them on the pong would queue input typed into an unverified socket, against the never-queue rule in src/AGENTS.md; holding only after the first ping times out would send on a possibly dead socket for the first deadline, which defeats the probe. Left as a known UX trade-off for the maintainer to decide.

Tests:

  • src/lib/ws.test.ts: three new cases (hidden page refuses waiting submit/pending action/secret, then a fresh probe's pong sends a new submit; the pong reaches handlers with canInput() true; a snapshot without ping ends a remembered probe and the connection is kept past 30 s). All three fail with ws.ts stashed.
  • src/lib/draft.test.ts: heldInputAction table (encoded Ctrl+Enter, CR/LF/Alt+Enter, Backspace, single- and multi-line pastes, pass-through controls) and eraseFromDraft.
  • scripts/terminal-input-regression.ts: with a held draft, the pane sends ESC[>4;2m, Ctrl+Enter shows the note and sends nothing, Backspace edits the draft.
  • bun run typecheck passed; bun run test:unit 3099 pass, 0 fail, 8 skip; bun run build then bun run check run bun scripts/ui-regression.ts passed (all PASS, exit 0). check full/concurrent not run.

Merge-session review (d0b66cc..HEAD)

Fixed:

  1. (low) A probe the hidden page cancelled while a submit, pending action or secret still waited for the snapshot let the frame go out unverified (probeSettled() only counted a probe whose timer was still running). Requests now read a probe generation (probeMark()) before the snapshot wait; a probe in flight then, or started during the wait, counts even if it already ended, and only its pong lets the request go. Test: src/lib/ws.test.ts "refuses a submit, a pending action and a secret whose probe the hidden page cancelled while they waited for the snapshot" (fails without the fix).
  2. (low) held_input was missing from submitNotTyped(), so Send now on a local held queue row refused for a held terminal draft turned the row "uncertain" (Copy only). Added, with a case in src/lib/compose.test.ts.
  3. (medium, merge coordination with fix(terminal): apply Backspace and tell left-out line breaks in held input #956) heldInputAction() now blocks line breaks, encoded Enter and pasted tabs before it asks applyToDraft(), so its answer no longer changes when fix(terminal): apply Backspace and tell left-out line breaks in held input #956 makes applyToDraft() fold breaks into a space. eraseFromDraft() now follows fix(terminal): apply Backspace and tell left-out line breaks in held input #956's Backspace rule: it leaves a truncated draft alone and returns EMPTY_DRAFT when erased to nothing. Checked with a throwaway no-commit merge of fix(terminal): apply Backspace and tell left-out line breaks in held input #956 into this head: src/lib/draft.test.ts passes (30/30).

Policy kept: Enter beside a held draft is refused with "Not sent: send or discard the held input first." (this PR), not folded into a space with the left-out notice (#956). #956's left-out path still applies to the chunk that starts a draft.

For the restack onto main once #956 is merged:

Deferred:

  • Item 6 of the review follow-up above (typing during a probe goes to the held draft) is still a maintainer decision.

Checks: bun run check fast passed (typecheck, build, unit 3101 pass / 0 fail / 8 skip). CI=1 bun run check run --build bun scripts/ui-regression.ts passed (terminal input, held input and Safari replay steps included).

🤖 Generated with Claude Code

devswha and others added 7 commits October 13, 2026 21:31
A WebSocket that stays in CONNECTING (a phone waking on a dead network,
a tunnel that drops the handshake) had no deadline, so no new attempt
ever started. wake() also returned when no reconnect timer was set,
which is exactly the state of a page that froze before its socket's
close event ran.

Close and retry an attempt still CONNECTING after 8 s, ending it on the
client side without waiting for a close event. wake() now ends a dead
socket whose close event is pending and connects at once; an open or
connecting socket is left alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hakes connect

- wake() leaves a CLOSING socket to its close event, so an output-stalled
  (4008) close still stops output and a submit-result that arrives during
  the closing handshake is still delivered.
- A CLOSED socket wake() ends early is remembered: if its late close event
  says 4008, the replacement connection is closed and output stops.
- The connect deadline doubles with each attempt given up (8 s, 16 s, capped
  at 30 s) and starts over once an attempt opens, so a remote PC whose
  upgrade waits up to 15 s for the machine relay still connects.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n it does not answer

A socket can read OPEN while its TCP path is gone (Wi-Fi to cellular, a NAT entry expired while
the phone slept). No close event comes for minutes, so the terminal froze while the header said
connected, and typed keys went into the dead socket instead of the held draft.

- protocol: ClientMessage `ping {id}`, ServerMessage `pong {id}`, ServerFeature "ping".
- server: answers ping with pong for any role, lists "ping" in its snapshot; MachineRelay
  already forwards both unchanged, so a remote PC's own snapshot decides.
- demo transport: lists "ping" and answers it.
- HerdrSocket: tracks lastFrameAt; on a bridge listing "ping" it probes an OPEN socket on
  wake (visible, pageshow, online) and after 20 s without a frame while visible. Any frame ends
  the probe; none within 3 s drops the socket (disconnect handlers, submits settle) and connects
  at once with retries reset. While a probe waits, canInput() and submit() refuse, so typing goes
  to the held draft. New ConnectionState (`state`) for the next change to build on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ing in a held draft

Review follow-ups on the liveness probe:
- a held draft now takes all later typing until Send or Discard, so a probe that
  ends mid-command can no longer split it ("echo " held, "rm -rf build\r" run);
  chords are refused with a note and the input line's Enter is refused
- only the pong of a ping in flight ends a probe; buffered frames and stray pongs
  no longer re-enable input on a dead socket
- submit, pendingAction and sendSecret wait out a probe (before sending, after the
  snapshot wait) instead of refusing or skipping it
- a socket dropped by a probe is kept in endedEarly (now a WeakSet), so a late
  4008 still stops output
- hiding the page cancels a probe; a deadline that fires late after a freeze pings
  again instead of dropping
- the ping feature is remembered across reconnects, so a socket whose snapshot
  never comes is probed too
- a second ping before a drop, and each probe drop doubles the per-ping deadline
  (cap 12 s) until a pong

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Text, pastes, Backspace and line breaks still join the held draft, but Ctrl+C,
Escape, Tab and arrows go to the program as before, so the Stop button and an
interrupt keep working while input is held.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ped output

A pane that kept its attach while the page was hidden (a queued message, a
mirrored pane) got a 4008 overload close from its frozen page, and coming back
never re-attached: back() only refit, wake() refuses a stopped output, and the
banner's Reconnect reloaded the whole page.

HerdrSocket now exposes outputStopped and stoppedWhileHidden: a 4008 that came
while the page was hidden, or within 5 s of it being shown again (a frozen page
reads the close after it resumes), counts as the page's freeze. PaneTerminal
attaches again in place once when the page is back in use, from back() or from
the stall notice itself when it is read after back() ran. A 4008 while the page
stays in use stays final. The banner's Reconnect is now a button that does the
same in-place re-attach.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ac9cc4b4-856f-4acc-905d-412a40dd0a2f
📥 Commits

Reviewing files that changed from the base of the PR and between 8246e19 and fe28bce.

📒 Files selected for processing (7)
  • changelog.d/recover-hidden-output-stall.md
  • docs/terminal-flow-control.md
  • scripts/output-browser-qa.ts
  • src/AGENTS.md
  • src/components/PaneTerminal.tsx
  • src/lib/ws.test.ts
  • src/lib/ws.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Output-stalled panes can now reconnect automatically once when the page returns to use, if the connection stopped while the page was hidden or shortly after it became visible.
    • The Reconnect button now reconnects the selected pane in place without reloading the page.
    • Stops that occur while the page remains in use still require user action, and repeated stops won’t trigger an automatic reconnect loop.

The WebSocket client now tracks whether output stopped while the page was hidden or shortly after it became visible. PaneTerminal can reattach in place after a qualifying stop. Tests and documentation cover automatic and button-triggered recovery.

Changes: Hidden-page output-stall recovery

Layer / File(s) Summary
Classify hidden-page output stops
src/lib/ws.ts, src/lib/ws.test.ts
HerdrSocket records page visibility and classifies 4008 stops received while hidden or within five seconds after showing. A later attach reconnects and replays attachments; wake does not reconnect. Tests cover these conditions.
Reattach the pane in place
src/components/PaneTerminal.tsx, scripts/output-browser-qa.ts, docs/terminal-flow-control.md, src/AGENTS.md, changelog.d/recover-hidden-output-stall.md
PaneTerminal reattaches after a qualifying stop or when the Reconnect button is used. Browser QA checks automatic reattachment and the button. Documentation describes the recovery behavior and its conditions.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: wouldu-pres

Merge Risk: ⚪ Minimal · up to fe28b

The reported chat Reconnect inconsistency does not occur on the output-stalled path. No actionable merge-blocking issue remains after normal checks.

Pre-merge checks · Passed 4 · Failed 1

Failed Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 4 files. (3 skipped: 3… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: reattaching a terminal after a hidden page returns to a stopped output state.
Description check Passed The description clearly documents the problem, cause, fix, scope, tests, validation results, known limitations, and stacking context. It uses equivalent sections instead of the template headings, but …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 4 files. (3 skipped: 3 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

devswha and others added 2 commits October 13, 2026 22:59
…er beside a held draft

- A probe cancelled by hiding the page no longer releases the submits, pending actions and
  secrets that waited on it: only an answered probe sends them; the others settle as
  disconnected and the secret is cleared.
- The probe's pong is passed on to handlers, and PaneTerminal's 1 s poll re-reads canInput(),
  so the key bar and the waiting banner follow the probe.
- A snapshot without "ping" ends a probe sent on remembered ping support, so an older bridge
  after a reconnect is not dropped.
- heldInputAction blocks Enter in any encoding (modifyOtherKeys Ctrl+Enter included), chords and
  multi-line pastes with a note; Backspace edits the held draft; the input line and the composer
  refuse to send beside a held draft.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
devswha and others added 4 commits October 13, 2026 23:26
…its snapshot wait

A submit, pending action or secret reads the probe generation before it
waits for the snapshot. A probe in flight then, or started during the
wait, now counts even when the hidden page already cancelled it, so the
frame goes out only after that probe's pong.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The composer refuses a send while the terminal holds input before any of
it reaches the socket. Listing held_input in submitNotTyped keeps a
refused queued row held (Send now stays) instead of marking it uncertain.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ine breaks

heldInputAction now blocks line breaks, encoded Enter and pasted tabs
before it asks applyToDraft whether the chunk is text, so the decision
does not change when applyToDraft learns to fold breaks into a space
(#956). Backspace no longer edits a draft that lost text for want of
room, the same rule #956 gives applyToDraft.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Matches #956's Backspace rule, so whatever else the empty draft carries
is reset with the text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A regression then fails at once instead of waiting out the submit
timeout under fake timers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
devswha added a commit that referenced this pull request Oct 13, 2026
…input (#956)

## Problem

In direct typing, input the terminal cannot take yet (socket down, or
the pane not input-ready after a phone resume) is held for review in the
"Input held until the terminal is ready" banner. The held text did not
match what was typed:

- `lss`, Backspace, ` -la` was held as `lss -la`: the deleted character
came back.
- `ls`, Enter, `pwd` was held as `lspwd`.
- A multi-line paste, raw (`a\rb`) or bracketed
(`\x1b[200~a\nb\x1b[201~`), vanished whole with no notice.

## Cause

`applyToDraft` (src/lib/draft.ts) kept a chunk only if it held no C0/C1
control at all and otherwise returned the draft unchanged with no flag.
DEL, BS, CR and LF were all treated like arrows and xterm's automatic
reports.

## Fix

- A leading run of DEL / BS in a chunk deletes that many code points
from the held text (`Array.from`, so an emoji or Hangul syllable is
never split). Text after the deletions in the same chunk is folded in as
usual.
- A chunk (or the inside of a bracketed paste) with CR/LF keeps its
text, with a single space where each run of line breaks was, and sets a
new `leftOut` flag. The held text never contains a line break, so Send
still never presses Enter.
- The banner shows a new notice when `leftOut` is set: "An Enter or line
break was left out: Send never presses Enter." (ko/ja/zh, and tr since
that locale lists the neighbouring strings). `draftIsEmpty` counts the
flag, and `restoreDraft` reads it back (anything but `true` reads as
false, so older records restore unchanged).
- An Enter or a run of line breaks with no text held changes nothing (no
empty notice, nothing stored).
- A tab inside a bracketed paste is held as a space (a typed Tab stays
out: it is completion). A paste, or a multi-line chunk, that still has
other controls sets `truncated` as well, so the notice says text was
left out instead of implying it was kept.
- Once `truncated` is set, Backspace no longer edits the held text: it
may be meant for the chunk that was dropped.
- Other typed controls (arrows, Tab, Ctrl keys, CPR/focus/mouse reports)
stay silent, as before.

## Tests

- `src/lib/draft.test.ts`: `ab` + `\x7f` -> `a`; `\b` likewise; empty +
`\x7f` -> unchanged; surrogate/Hangul deletion; `\x7f\x7f\x7fthe`
replaces; `ls`,`\r`,`pwd` -> `ls pwd` + `leftOut`; `a\rb` and
`\x1b[200~a\nb\x1b[201~` -> `a b` + `leftOut`; restore of the flag; bare
Enter / empty paste -> `EMPTY_DRAFT`; bracketed `a\tb` -> `a b`; raw
`a\tb\nc` and bracketed `a\x1bb` -> `truncated`; Backspace after a loss
keeps the held text.
- `scripts/terminal-input-regression.ts` (via `ui-regression.ts`): in
the not-ready state, a bare Enter shows no left-out notice, `b` +
Backspace removes the `b`, Enter after text shows the notice, and after
reload both the held text (`a `) and the notice are back; Send sends `a
`.
- Ran: `bun run typecheck` (pass), `HERDR_TEST_MODE=unit bun test
./src/lib/draft.test.ts` (23 pass; 5 fail on the first commit's
draft.ts), `bun run test:unit` (3076 pass, 0 fail), `bun run build` +
`bun run check run bun scripts/ui-regression.ts` (all PASS).

## Notes

- Design choice: the space where a line break was is a character the
user never typed. It keeps `ls` Enter `pwd` readable as two words
instead of `lspwd`, and the notice says a break was left out. The
alternative (keep `\n` in the draft and have Send deliver it as a
bracketed paste) was not taken: it changes what Send writes and depends
on the program's paste mode.
- A lone Enter with nothing held shows nothing, as before. The flag is
for Enter/line breaks only, not the "special keys dropped" count that
was removed earlier (#561).
- Known limits, left out on purpose (the banner shows the held text
before Send): Backspace right after a break deletes the space that
stands for it (fixing it needs new stored state); arrows that move the
cursor, Ctrl+U, Ctrl+W, Alt+Backspace and Cmd+Backspace are not
followed; a chunk with DEL after text is dropped whole; a raw
single-line chunk with a control is still dropped silently, since it
cannot be told from typing. The changelog names only the Backspace key.
- Overlap: #951 reworks the held-input banner into an activity row and
reads `draft.truncated` there. Whichever merges second needs to carry
the `leftOut` notice into the new row. No open PR touches
`src/lib/draft.ts`.
- Source: mobile-terminal stability audit finding input-3.

## Merge-session review

Fixed in ca92b54:
- Medium: a typed ESC CR (physical Shift+Enter or Alt+Enter) went
through the line-break branch and set both `truncated` and `leftOut`, so
the draft showed the "too long" notice and later Backspace stopped
editing it. A typed chunk containing ESC is now treated as a key
sequence again: neither held nor told. Tests: `\x1b\r` / `\x1b\n` ->
`EMPTY_DRAFT`; `ls`, `\x1b\r`, `\x7f`, `x` -> `lx` with no flags (both
failed at f9b902c).
- Low: Backspace that erased all held text kept `leftOut`, leaving an
empty banner with only the line-break notice (and saving it). Erasing
everything held now returns the empty draft. Test: `ls`, `\r`, three
`\x7f` -> `EMPTY_DRAFT` (failed at f9b902c).
- Low: the truncated notice blamed length even when a paste with control
characters set it. The string is now "Some input could not be held and
was left out." in en/ko/ja/zh/tr; `src/lib/i18n.test.ts` checks every
locale has it. The changelog fragment was updated to match.

Deferred:
- Coordination with #968 (stacked on #967): on restack after this PR
merges, #968's `eraseFromDraft` expectations need `leftOut: false`, its
Backspace erase should reuse this PR's ERASE path instead of a second
implementation, and `heldInputAction` must check line breaks and ESC CR
before its `applyToDraft(...) !== EMPTY_DRAFT` "hold" test if blocking
stays the contract (update src/AGENTS.md to match). Its existing
`"line1\nline2"` / `"\x1b\r"` -> `block` tests catch a naive merge. Not
a defect in this PR alone.
- #951 overlap: the truncated notice's English key changed, so whichever
of #951 and this PR merges second must use the new key.

Checks: `bun run check fast` (typecheck, build, 3078 unit pass), `CI=1
bun run check run --build bun scripts/terminal-input-regression.ts`
(PASS).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Base automatically changed from fix/ws-liveness-probe to main October 13, 2026 16:26
devswha added a commit that referenced this pull request Oct 13, 2026
… ping (#967)

## Problem
A WebSocket can still report `OPEN` after its TCP path is gone, for
example when a phone moves from Wi-Fi to cellular or wakes on a changed
network. No close event arrives for tens of seconds to minutes. In that
time:
- the terminal stops updating while the header still says connected, and
no reconnecting banner shows;
- keys go to `rawSend` on the dead socket and `sendInput()` returns
true, so PaneTerminal never keeps them as a held draft and they are
lost;
- a composer send waits the full 90 s submit timeout;
- `wake()` (visibilitychange / pageshow / online) leaves an `OPEN`
socket alone.

## Cause
`HerdrSocket` treats `readyState === OPEN` as proof that the connection
is alive. The protocol has no app-level ping. Bun's own pings and idle
timeout only tell the server about a dead peer, and the browser never
sees them.

## Fix
- **Protocol** (`shared/protocol.ts`): new `ClientMessage` `ping {id}`,
`ServerMessage` `pong {id}` and `ServerFeature` `"ping"`. The doc
comments on both frames are new.
- **Server** (`server/index.ts`): answers `ping` with `pong` for any
role and lists `"ping"` in its snapshot. A malformed id gets an
`invalid_ping` error frame, not a pong.
- **Remote relay:** no code change. `MachineRelay` already forwards
frames as they are, so the remote PC's own snapshot decides whether the
client pings. The contract test proves the pass-through.
- **Demo** (`site/demo/transport.ts`): lists `"ping"` and answers it the
way the server does.
- **Client** (`src/lib/ws.ts`):
- Tracks `lastFrameAt`. On a bridge whose last snapshot listed `"ping"`,
it probes an `OPEN` socket on wake (visible, pageshow, online) and after
20 s without a frame while the page is visible (checked every 5 s). The
flag is remembered across reconnects, so a socket whose snapshot never
arrives (a relay path that dies right after the upgrade, a failed
snapshot) is probed too.
- Only the `pong` of a ping in flight ends a probe. Any frame still
postpones the idle probe, but a frame the browser buffered before a
sleep, or a pong with another id, does not prove the path is alive. The
probe ends before handlers run, so `canInput()` already reads true for
them.
- No pong within 3 s sends a second ping; no pong to that drops the
socket on the client side: `socket = null`, the disconnect handlers run,
submits settle as `disconnected`, `close()` is called, and `connect()`
runs at once with retries reset. The open handler then replays the role
and attaches. Each probe that ends in a drop doubles the per-ping
deadline (cap 12 s) until a pong comes back, so a slow relayed link is
not dropped over and over.
- A dropped socket is kept in `endedEarly` (now a `WeakSet`, shared with
`wake()`), so a 4008 close that arrives after the drop still stops
output and closes the replacement. A socket that is `CLOSING` when the
deadline hits is left to its close event (same rule as #954's `wake()`).
- Hiding the page cancels a probe; showing it starts a fresh one. A
deadline that fires more than 2 s late (the page was frozen and the
timer ran before the queued pong) pings again instead of dropping.
- While a probe waits, `canInput()` refuses, so typing goes to the held
draft. `submit()`, `pendingAction()` and `sendSecret()` wait the probe
out after their snapshot wait and right before sending; if the socket is
dropped they settle as `disconnected` having sent nothing (the secret is
still cleared). The composer no longer reports offline during a routine
probe.
- New explicit `ConnectionState` (`idle | connecting | open | probing |
waiting | stopped | closed`), exposed as `state`. `connected` still
reads the socket itself, so the banner does not flash on every tab
switch.
- **Held draft** (`src/components/PaneTerminal.tsx`): once input is held
for a pane, all later typing joins the draft until Send or Discard, also
after the terminal is ready again. Before this, keys refused during a
probe (or a disconnect) were held while the next keys after the pong
went straight to the pty: `echo ` held and `rm -rf build⏎` run. Text,
pastes, Backspace and anything with a line break join the draft; Enter
is left out of it, a sticky-modifier chord is refused with a note (new
string, translated in ko/ja/zh/tr), and the input line's Enter is
refused while a draft is held. Other control keys (Ctrl+C, Escape from
the Stop button, Tab, arrows) still reach the program, so an interrupt
never waits on the draft.
- `src/AGENTS.md`: the WS client row records the probe rules, and a new
row records the held-draft rule.

## Tests
- `src/lib/ws.test.ts` (fake socket and fake timers):
- an `OPEN` socket with no pong gets a second ping at 3 s and is dropped
at 6 s. The disconnect handler runs once, a pending submit settles as
`disconnected`, a new socket starts with no backoff and its open replays
role and attach at the saved geometry, and the late close of the old
socket changes nothing;
- during a probe, `sendInput` and `sendKeys` are refused; a buffered
`pane-status` frame and a pong with another id leave it probing; the
matching pong restores input; a second wake sends no second ping;
  - a socket that only delivers stale frames during a probe is dropped;
- `submit`, `pendingAction(steer)` and `sendSecret` started during a
probe send nothing until the pong, then each sends once;
- a submit already waiting on its snapshot when `wake()` runs, and a
secret, settle as `disconnected` with nothing sent when the probe drops
the socket;
- a 4008 close after a probe drop still stops output and closes the
replacement;
- a deadline that runs out while the page is frozen pings again, and the
pre-freeze pong no longer counts;
- hiding the page cancels the probe (no drop in 60 s), showing it starts
a fresh one;
- a reconnected socket with no snapshot is probed when the previous
snapshot listed `ping`;
  - a probe drop doubles the next deadline, and a pong resets it;
  - plus the earlier idle-ping, no-flag and `CLOSING` tests.
- The 10 new or changed tests fail with the previous `ws.ts` of this
branch (checked by restoring it).
- `scripts/terminal-input-regression.ts`: after the terminal becomes
ready with `a` held, Enter and `b` are typed: the draft reads `ab` and
nothing reaches the pane; Escape still reaches the pane with the draft
kept; Send types `ab` once with no Enter. It fails without the
PaneTerminal change (checked by restoring it and rebuilding).
- `server/ping.contract.test.ts` (integration, real herdr): the snapshot
lists `ping`; ping gets a pong with the same id, also for an observe
role; a malformed id gets `invalid_ping`; ping and pong pass through
`MachineRelay`. Not rerun for the review fixes (no server change).
- `bun run typecheck`: pass.
- `HERDR_TEST_MODE=unit bun test ./src/lib/ws.test.ts`: 49 pass.
- `bun run test:unit`: 3088 pass, 0 fail, 8 skip (includes the #949
convention guards).
- `bun run check run --build bun scripts/terminal-input-regression.ts`:
pass.
- `scripts/ui-contexts-regression.ts` passed on the first revision; not
rerun after the review fixes.
- No browser test of a real half-open socket: Playwright cannot hold a
socket half-open, so the unit tests cover that part.

## Notes
- **Stacked on #954; retarget to main after it merges.** It builds on
#954's connect deadline, `wake()` rules and `SOCKET_CLOSING` handling in
`ws.ts`.
- **Release:** `shared/protocol.ts` and `server/index.ts` are inputs to
the remote bundle, so the release must raise `REMOTE_BUNDLE_VERSION`.
The release guard from #948 (`scripts/release-guards.ts`) refuses to tag
otherwise. It is not bumped here. Until a remote PC updates, its
snapshot does not list `ping`, so it is never probed and behaves as
before.
- **Trade-off:** a key typed within one round trip of a probe start
lands in the held draft with Send/Discard rather than being sent, and
later typing joins it until the user picks one. Sending it later on its
own would break the never-queue rule.
- **Left out on purpose:**
- In-flight submits still settle as `disconnected` on a probe drop
rather than waiting for the close event: a half-open socket may not
close for minutes, and the server never types a message more than 45 s
after it arrived.
- No "checking connection" UI and no state-change event for probe start
and end. `inputReady` drives the "Waiting for terminal input…" banner
and the key bar, so a sub-second probe on a healthy link would flash
both on every wake. The held-draft banner already shows the case the
user can act on, and the composer no longer reports offline. The
remaining gap: PaneTerminal recomputes `inputReady` only when a message
arrives.
- The `ping` flag survives a reconnect: if a PC is downgraded to a
bridge without `ping`, the first probe after the reconnect costs one
drop before the new snapshot clears the flag.
- `resume()` in PaneTerminal still resets the screen before the attach
answers (render-scroll-1's second half).
- **Overlaps:**
- #956 edits the held-draft code (`applyToDraft` Backspace and line
breaks) and the same lines of `scripts/terminal-input-regression.ts`: a
textual conflict, and the test expectation changes with it: after #956
an Enter in the draft becomes a space plus a notice, so the held and
sent text become `a b` instead of `ab`. Never an Enter either way.
- #932 edits the same `submit()` guard area in `ws.ts`, plus
`protocol.ts` and the demo transport.
- #951, #936 and #859 add to the protocol unions, the demo switch and
`server/index.ts`'s message switch. These are textual merges with no
conflict in meaning.
- #962, #964 and #966 touch PaneTerminal's key bar and input line, not
the onData draft path.
  - #952 owns `server/machine-relay.ts`. Nothing changed there.
- From a mobile-terminal stability audit: connection-1, render-scroll-1,
lifecycle-1, history-1, moshi-1.

## Merge-session review
Fixed (each with a regression test that fails without the fix):
- **Hiding the page answered the probe** (`ws.ts`): a submit, pending
action or secret waiting on a probe was sent on an unchecked socket once
the page hid. A hidden page now pauses the probe (`probePaused`): input
stays refused, waiters keep waiting, and showing the page pings again.
Test: `keeps a submit and a secret waiting when the page hides during a
probe...`; the existing hide test now expects `probing` and refused
input while hidden.
- **Replacing a CLOSING socket left its probe unresolved** (`ws.ts`):
`connect()` now ends a non-null previous socket (CLOSING, or CLOSED
before its close event) first, so its probe and waiting submits settle,
the disconnect handlers run, and a late 4008 from it still stops output
(`endedEarly`). Test: `settles a submit waiting on a probe when a
CLOSING socket is replaced...`.
- **Ctrl+Enter under modifyOtherKeys got past the held-draft guard**
(`PaneTerminal.tsx`): a modified Enter is decided on before its rewrite
to `CSI 27;5;13~` and refused like a chord, with the held-input note.
Test: `terminal-input-regression` turns on modifyOtherKeys and presses
Ctrl+Enter beside a held draft.
- **A paste with a tab went to the pty and a multi-line paste was
dropped in silence** (`PaneTerminal.tsx`): while a draft is held, a
paste the draft cannot take is refused with the held-input note. Test:
paste `a\tb` and `x\ny` beside a held draft; nothing reaches the pane,
and the multi-line paste is told or held, never dropped in silence
(written so it still holds once #956 folds line breaks into the draft).
- **The input line said "disconnected" and its non-empty line got past
the draft rule** (`TerminalInput.tsx`, `PaneTerminal.tsx`): `onEnter`
returns `boolean | string` and the held-input reason is shown;
`sendTerminalLine` refuses beside a held draft too and keeps the text.
Test: empty Enter and `ls` + Enter on the input line beside a held draft
show the held-input note and send nothing.
- `src/AGENTS.md` rows and the changelog fragment updated to match.

Deferred (unchanged from the builder's notes, including its review
finding 8): the items under "Left out on purpose" above, such as
PaneTerminal recomputing `inputReady` only when a message arrives.

Notes for the merge step:
- #968 is stacked on this branch and does not carry these two commits;
it picks them up when restacked onto main after this PR squash-merges.
- #956 overlap: the `terminal-input-regression` Send step now checks the
draft's own text, so it holds whether #956 shows `ab` or `a b`.

Checks: `bun run check fast` pass; `scripts/ui-regression.ts` (terminal
input, Ctrl+Enter, held-draft lanes) pass;
`scripts/sticky-modifiers-regression.ts` pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
#967 landed with its own review fixes, so this keeps main's version of the
probe and held-draft code and drops the parallel #967 fixes this branch
carried. Only this PR's own change remains: re-attach a pane whose output
stopped while the page was put away.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@devswha
devswha merged commit a338f8b into main Oct 13, 2026
9 checks passed
@devswha
devswha deleted the fix/recover-hidden-output-stall branch October 13, 2026 16:36
devswha added a commit that referenced this pull request Oct 13, 2026
…969)

## Problem

CI on main at eace9f0 (run 38429338201, job 115409436687, self-hosted
runner, `HERDR_TEST_SHARDS=4`) failed
`server/claude-session.contract.test.ts:200` "lists a Claude pane's
subagents, counts the running ones and pushes the count without a
status": expected `[{ id: "a1", status: "running" }]`, received `[]`, in
120 ms. The test after it in the file and every later test passed.

## Root cause: a race in the test, not a regression

- The server's snapshot path starts `claudeAgents.refresh()` without
waiting for it. If `claudePaneSession` answers null once for the pane
(herdr's `agent.get` / `pane.process_info` busy or late),
`ClaudeSubagentStatus.ensure` records `path: null`. It then skips a new
lookup until `refreshMs` (5 s) has passed, and that includes the lookup
`/api/pane/omo-tasks` makes when asked. So an ask inside that window
answers `[]` fast, on purpose. The client retries.
- The test asked once and treated that answer as final. Its comment
("before the server has had a snapshot") was already wrong: `beforeAll`
waits for `statusReady`, and another test runs before this one.
- Reproduced locally by making the first two `claudePaneSession` calls
answer null. The result matched CI exactly: the same assertion, `[]`,
107 ms, and the other 9 tests green. With this change the same injection
passes.
- No file in the lookup or subagent path changed in #952–#968. A later
main run with the same code (38430538375) passed integration. This is
the only time this test has failed in the recent runs. The new factor is
the self-hosted 4-shard runner (#873). The log does not show which herdr
call was late in CI. What is shown is that one null lookup gives exactly
this failure.
- 30 local runs (serial, and 6 in parallel on separate herdr sessions)
and 3 full `HERDR_TEST_SHARDS=4` integration runs were all green before
the fix.

## Change (test only)

- Moved the bounded `until` poll (100 × 100 ms, allowed by the
fixed-sleeps ratchet) to file scope. The plan test and this test both
use it.
- Poll the subagent list until `a1` reads running, then keep the
existing `toMatchObject` assertions.
- Wait for the `background_tasks === 1` frame starting from the socket's
first frame, after the poll. Its 10 s window now starts once discovery
is done, and a count pushed while the list was polled still counts.
- Test timeout raised from 15 s to 25 s to cover the 5 s lookup cache on
the slow path.

Not changed: an explicit ask after a failed background lookup still
waits out the 5 s cache. That is the intended product behaviour. If an
explicit ask should look again at once, that is a separate follow-up.

## Checks

- `bun run check fast`: ok
- `bun test server/claude-session.contract.test.ts` on an isolated herdr
session: 10 pass, with and without the injected lookup failure

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant