Skip to content

Retry an attach herdr refused over a read in progress ("terminal ended" after a Codex answer) - #45

Merged
devswha merged 2 commits into
devswha:mainfrom
Yoonwoo-Ha:fix/attach-read-in-progress
Sep 25, 2026
Merged

devswha merged 2 commits into
devswha:mainfrom
Yoonwoo-Ha:fix/attach-read-in-progress

Conversation

@Yoonwoo-Ha

@Yoonwoo-Ha Yoonwoo-Ha commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

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: server shut down: terminal attach failed: terminal term_… has a read in progress; retry

herdr 0.9 refuses herdr terminal attach while an alt-screen read of the same terminal is in
progress (pending_alt_screen_reads in src/server/headless.rs). herdr starts such a read when
all of these hold:

  • a pane.read asks for more lines than the screen shows,
  • the terminal is an idle agent in the alternate screen with mouse reporting (Codex),
  • the terminal is not attached.

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 chat
showed "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

  • When an attach exits non-zero and its own output carries that refusal, it is started
    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), with
    herdr's message shown.
  • A fresh attach's first bytes are held for 100 ms. A refused attach prints herdr's setup,
    teardown and message within milliseconds, so those bytes are dropped rather than painted
    into the terminal.
  • attach_conflict is 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.read calls
against an attached pane and nothing ended. Only the attach itself races.

Verification

  • New contract tests in server/output.contract.test.ts, with a stand-in HERDR_WEB_HERDR_BIN
    that refuses the first N attaches with herdr's exact message:
    • Refused twice: the third attach streams the pane (an echo round-trips), with no
      pty-exit, and no refusal text reaches the client.
    • Always refused, with attachRetryForMs: 800: one pty-exit, with herdr's last message
      shown.
    • Both tests fail on current main.
  • bun run typecheck, bun test (332 pass), bun scripts/ui-regression.ts (8/8)
  • Real herdr, lab Codex pane idle after a 300-line answer. A 400-line read starts, and a client
    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.
  • Headless Chrome on an iPhone 13 viewport, with the socket dropped and the page brought back:
    • main: "terminal ended" in 3 of 4 runs
    • this branch: 0 of 6

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

Yoonwoo-Ha and others added 2 commits September 25, 2026 20:15
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]>
@Yoonwoo-Ha Yoonwoo-Ha changed the title Retry an attach herdr refused over a read in progress ("terminal ended" on reconnect) Retry an attach herdr refused over a read in progress ("terminal ended" after a Codex answer) Sep 25, 2026

@devswha devswha left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
devswha merged commit cd39e39 into devswha:main Sep 25, 2026
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.
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.

2 participants