Repository navigation
Retry an attach herdr refused over a read in progress ("terminal ended" after a Codex answer) - #45
Merged
Conversation
herdr 0.9 refuses `herdr terminal attach` while a read of the same terminal is in flight. It prints "terminal attach failed: terminal … has a read in progress; retry" and exits 1. This server reads panes all the time (prompt polls, the Codex transcript match reads 400 lines), so an attach could race one of its own reads. That happens most on a phone coming back from the background: the socket reconnects and re-attaches while the chat re-reads the prompt and the conversation at once. The refusal was reported as pty-exit, and the chat showed "terminal ended". The attach is now started again for the same attachment and clients, after 50 ms, 100 ms, … up to five times, when its own output carries that refusal. Past the last retry it still ends as before. Checked with headless Chrome on an iPhone viewport over a live Codex pane, dropping the socket and returning the page repeatedly: - before: "terminal ended" in 3 of 4 runs - after: 0 of 6, with herdr refusing at least one attach in every run Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Five quick retries (about 0.75 s) were not enough. The read that makes herdr refuse is its alt-screen read: a pane.read of more lines than an idle alt-screen agent shows (the Codex transcript match asks for 400). herdr scrolls the agent's history back with wheel events to collect them, for up to 15 s plus 5 s to restore (src/server/alt_screen_read.rs), and refuses every attach meanwhile. - On a lab Codex pane, the 400-line read took about 0.9 s. - On the user's pane, herdr's log shows six attaches refused over a second right after Codex finished an answer. - Refused attaches are retried with backoff (50 ms, doubling, at most 500 ms) until 20 s after the first refusal. The limit is an option, attachRetryForMs, for tests. - A fresh attach's first bytes are held for 100 ms. A refused attach prints herdr's setup, teardown and message within milliseconds, and those bytes are now dropped instead of painted into the clients' terminal. The last refusal, when the retries run out, is still shown, since it says why the terminal ended. - The attach_conflict check reads the attach's own output, not the replay buffer. Checked with a real 400-line read of an idle lab Codex pane (about 0.9 s), attaching 20 ms after it started, three rounds: attached every time once the read finished, and no refusal text reached the client. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
devswha
approved these changes
Sep 25, 2026
devswha
left a comment
Owner
There was a problem hiding this comment.
Thanks, a clean diagnosis and a careful fix. The retry keys on the attach's own output, backs off up to herdr's longest read, and stops once the attachment is gone. The hold keeps herdr's refusal off the screen, and attach_conflict now reads the attach's own output too. Between retries, input and resize on the exited pty are dropped quietly, which is fine for a sub-second window. I merged this onto main together with #43 and #44: bun test passes 344/344, test:ui passes 8/8 and output-browser-qa passes. A possible follow-up: the Codex transcript match could read only visible lines, which would avoid herdr's alt-screen read altogether.
devswha
added a commit
that referenced
this pull request
Oct 1, 2026
Release v0.3.39: moves the Unreleased notes under 0.3.39 and bumps `package.json` and `herdr-plugin.toml`. In this release, all fixes: a password sent right after a reconnect (#244), typing and keys outliving their connection (#235), panning a terminal whose grid is not the browser's own on a phone (#241), omo's question form in the chat (#237 by @nahwan-kim), and a held pane resumed by a late herdr answer plus a new shell taken for OmO (#243). No remote bundle change: `REMOTE_BUNDLE_VERSION` stays 9. Changelog housekeeping that was waiting for a release: - Compare links at the bottom now cover 0.3.31 to 0.3.39 (they stopped at 0.3.30), and `[Unreleased]` compares from v0.3.39. - Five references that were a bare `#N` are full PR links with their author: #164 is the issue PR #199 fixed, #154 the issue PR #155 closed, and #45 and #6 are PRs. No other wording of released entries changed. `bun scripts/release-notes.ts 0.3.39` prints the notes and exits 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On a Codex pane, the chat often showed "terminal ended" right after an answer arrived. The agent
and the pane were fine.
The attach had died with herdr's own message:
herdr 0.9 refuses
herdr terminal attachwhile an alt-screen read of the same terminal is inprogress (
pending_alt_screen_readsinsrc/server/headless.rs). herdr starts such a read whenall of these hold:
pane.readasks for more lines than the screen shows,It then scrolls the agent's history back with wheel events to collect the lines, for up to 15 s
plus 5 s to restore (
src/server/alt_screen_read.rs).The Codex transcript match reads 400 recent lines. So once Codex finishes an answer (idle), a
poll that lands while the pane is unattached starts that read. A phone coming back from the
background is the typical case: the socket reconnects and the chat re-reads the conversation.
Every attach during the read was refused, the refusal was reported as
pty-exit, and the chatshowed "terminal ended".
Measured on a lab Codex pane: the 400-line read took about 0.9 s and refused every attach
meanwhile. On my own Codex pane, herdr's log shows six attaches refused over a second right
after an answer.
Change
again for the same attachment and clients. Retries back off from 50 ms, doubling up to
500 ms, until 20 s after the first refusal, which is herdr's longest such read. The retry
spawns at the attachment's current grid. Past that, it ends as before (
pty-exit), withherdr's message shown.
teardown and message within milliseconds, so those bytes are dropped rather than painted
into the terminal.
attach_conflictis detected from the attach's own output rather than the replay buffer.createServer({ attachRetryForMs })lets tests shorten the limit.An established attach is not affected by reads. I ran 40 rounds of 8 parallel
pane.readcallsagainst an attached pane and nothing ended. Only the attach itself races.
Verification
server/output.contract.test.ts, with a stand-inHERDR_WEB_HERDR_BINthat refuses the first N attaches with herdr's exact message:
echoround-trips), with nopty-exit, and no refusal text reaches the client.attachRetryForMs: 800: onepty-exit, with herdr's last messageshown.
bun run typecheck,bun test(332 pass),bun scripts/ui-regression.ts(8/8)attaches 20 ms later. In 3 of 3 rounds, it attached as soon as the read finished (about
0.9 s), with no refusal text on screen.
A possible follow-up, not in this PR: the 400-line read makes herdr scroll an idle, unattached
Codex pane's history with wheel events on every such poll. Reading only the screen for a pane
this server is about to attach might be gentler.
🤖 Generated with Claude Code