Skip to content

fix(terminal): hold a resized attach until herdr draws its current grid (#2289) - #2324

Merged
devswha merged 3 commits into
mainfrom
fix/attach-hold-resize
Oct 29, 2026
Merged

devswha merged 3 commits into
mainfrom
fix/attach-hold-resize

Conversation

@devswha

@devswha devswha commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Change

Closes #2289, a follow-up to #2255. An attach resized while it started no longer shows a screen at the wrong size first, and no longer takes input before the program has the current size.

Before:

  • The early release compared the requested size with the start size only, so a resize back to the start size released on herdr's redraw for the size in between.
  • The 100 ms hold's deadline released the old-size screen while herdr's redraw was still on its way.

Now (server/core/terminal-process.ts, server/core/terminals.ts):

  • The attach counts the pty's resizes. Once resized, it settles only on the latest whole herdr screen since the latest resize, drawn at the pty's current size.
  • What counts as a whole screen (screenGrid): one synchronized update (ESC[?2026h … ESC[?2026l) that moves to the start of every row from 1 to the last.
    • The width is read only from rows of plain ASCII. herdr pads each row to the width, and Bun.stringWidth and herdr count some marks (Devanagari) differently.
    • Each row is measured from its first move, so herdr's own cursor placement at column 1 is not taken for an extra row.
  • Which screens can cancel a match: only a whole screen at a size the pty actually had, either the start size or one it was resized to. A diff of the top rows says nothing either way.
  • When the redraw is late: after the hold's own 100 ms, a resized attach waits for that redraw for up to ATTACH_RESIZE_REDRAW_MS (1 s), then lets the bytes go as before, so no attach stays held.
  • Size cap: any hold also lets go past 256 KiB of held output (ATTACH_HOLD_MAX_BYTES, counted in UTF-8 bytes). Held output goes out as one frame, and a frame over a client's 1 MiB output window ended the client as stalled.
  • Scanning cost: each read is looked at once. An update that has not ended is kept as its reads, and they are joined only when its end comes.
  • Unchanged: attaches that were not resized, herdr's refusal handling from perf(terminal): paint an attach's screen without the 100 ms hold (#1642) #2255, --takeover, herdr focus, RPC connections and drafts.

What herdr sends

Captured from herdr 0.9.5 through the PTY host, against the isolated ovis-test session:

  • The first screen is ESC[?2026h, then ESC[2J, then every row moved to with ESC[r;1H and padded to the width.
  • A resize is followed by herdr's redraw about 50 ms later, in the same shape without the clear.
  • Two resizes back to back (91x27, then 80x24) give one redraw, at the final size.
  • A resize before herdr's attach client starts gives no redraw: its first screen is already at the final size.

The test helpers produce those captures byte for byte.

Validation

  • Unit tests through the real attach lifecycle (server/core/terminals.test.ts: the PTY mocked, a client that acknowledges its output, fake timers):

    • a resize back to the start size;
    • a redraw later than the hold;
    • a screen queued from before a resize round trip, followed by a redraw at the size in between;
    • the redraw in reads cut every 5 bytes;
    • the 1 s fallback;
    • a diff of the top row right after the redraw;
    • a busy pane over the cap, both during the hold and after it.

    On 010a44dd (main) the first two fail. Every later case fails on the commit before the fix it covers.

  • server/core/terminal-process.test.ts checks screenGrid on the captured bytes:

    • the cursor at column 1;
    • partial updates;
    • plain rows of different widths;
    • Devanagari.
  • Mutations: each one is caught by the test named after it. They are: a later screen at any size cancels a match; screens at sizes the pty had no longer cancel one; the cap applies only during the redraw wait; widths are read with Bun.stringWidth; and an update not yet ended is dropped.

  • Real-surface QA: a real createServer (port 0, a mkdtemp state dir) on the isolated ovis-test herdr, with one WebSocket client.

    • The client attaches at 80x24, then resizes during the hold, 0 to 90 ms later. Either it goes to 91x27, or it goes to 91x27 and back to 80x24 30 ms later.
    • The check: the last whole screen in the output released with input-ready is at the requested size.
    • This PR: 20/20. Input was ready within 319 ms.
    • main: 16/20. The four misses are the "back" ordering, where the first screen was at 91x27.
  • Scan cost: a 1 MB redraw at 1000x1000 in 1 KiB reads took 1,288 ms with a rescan of everything held, against 47 ms when each read is looked at once. That comes from a scratch benchmark.

  • Terminal contract tests: 262 pass, 0 fail (attach-held, input-membership, keys-lifecycle, output, pending-input, reattach, submit, take-over, terminal-insert, watch).

  • bun run check fast: green.

Reviews

An omo code reviewer and Codex (gpt-6.1-sol, fast, xhigh) reviewed each round adversarially.

  • Round 1:

    • omo approved, with one MEDIUM: each read rescanned all held output. That is fixed.
    • Codex raised four MEDIUMs, all fixed:
      • the width of spacing marks;
      • a queued screen from before a round trip;
      • a rescan of a fragmented update;
      • over 1 MiB released as one frame, which ended the client as stalled.
  • Round 2: omo raised a HIGH and Codex a MEDIUM, both the cursor at column 1. Codex also raised a MEDIUM for a top-row diff masking the redraw, and a MEDIUM for the cap starting only after the hold. All are fixed.

  • Codex's own reproductions on the final head: 11 of 12 pass. The one left is a single read over 1 MiB, which cannot occur: the Rust PTY host reads at most 64 KiB (READ_CHUNK), and the Node host is bounded by its pipe.

  • Round 3: omo checked the final delta and approved it, with no CRITICAL, HIGH or MEDIUM.

Known limits (LOW, not reproduced against herdr)

  • Size cap and a pane another bridge held: the cap lets go early even for that pane, which otherwise keeps the whole hold. If more than 256 KiB arrives in the 100 ms right after the other bridge lets go, attach-resumed comes early and a take-over click that was waiting is spent. The frame size has to win then anyway (omo round 3).
  • A diff of only an old grid's rows: after a resize that changes only the row count (80x24 to 80x30), a full-width diff of rows 1-24 in the same read as the redraw reads as the old grid. The attach then waits the 1 s fallback. Output is never wrong (omo round 3).
  • No committed test for the cap without a resize: the early release of an attach that was not resized, with a later refusal still not painted, was checked with a scratch test only.
  • The same screen before and after a round trip: a first screen herdr drew before a resize there and back looks the same as its redraw for the final size. If its bytes reach the server after both resizes, it settles the attach. The screen is at the right size; the only exposure is input in the few ms between the program's two resize signals.
  • Inherited rescans: a held screen that contains herdr's own words ("terminal attach") makes attachScreenStarted scan everything held on each read until the hold ends. That path is from perf(terminal): paint an attach's screen without the 100 ms hold (#1642) #2255 and is bounded by the hold.

Release

server/ goes into the remote bundle, so the release that ships this raises REMOTE_BUNDLE_VERSION.

…id (#2289)

A resize since an attach started kept the hold only while the requested
grid differed from the start grid, so a resize back to the start size let
the first screen go out on herdr's redraw for the size in between. The
hold's 100 ms deadline also sent the old-size screen when herdr's redraw
for the new grid came later.

The attach now counts the pty's resizes and, once resized, settles only on
a whole herdr screen at the pty's current grid that began after the latest
resize (one synchronized update drawing every row, the first filled to the
width, as herdr 0.9.5 draws it). Past the hold it waits for that redraw for
up to ATTACH_RESIZE_REDRAW_MS (1 s), then lets the bytes go as before.

Closes #2289
… screen

Review follow-ups for #2289:
- Read a screen's width from its plain ASCII rows only: herdr and
  Bun.stringWidth count spacing marks differently, so a stale grid could
  pass and a current one wait out the fallback.
- Settle on the latest whole screen since the latest resize, not the
  first that matches: a read can bring herdr's first screen, queued from
  before a resize round trip, followed by the redraw for the grid between.
- Keep an update that has not ended as its reads and look for its end in
  each new read only: a large screen in small reads no longer rescans its
  growing prefix (1 MB in 1 KiB reads: 1288 ms -> 47 ms).
- Stop waiting for the redraw past 256 KiB of held output: it goes out as
  one frame, and over a client's 1 MiB output window that ended a healthy
  client as stalled.
…sor at column 1

Review follow-ups for #2289:
- Measure each row from its first move only: herdr places the cursor
  before its update ends, and at column 1 that is one more move to a
  row's start with no text, which made a valid redraw no screen.
- Let only a screen at a grid the pty had (its start grid or one it was
  resized to) cancel a match: a diff repainting the top rows right after
  the redraw kept the attach waiting for the fallback.
- Apply the held-output cap for the whole hold, counted in UTF-8 bytes,
  and forget an open update once the hold releases.
@coderabbitai

coderabbitai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 7 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 353029c and f18aec3.

Files selected for processing: 5
  • changelog.d/attach-hold-resize-redraw.md
  • server/core/terminal-process.test.ts
  • server/core/terminal-process.ts
  • server/core/terminals.test.ts
  • server/core/terminals.ts
  • 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

devswha commented Oct 29, 2026

Copy link
Copy Markdown
Owner Author

CodeRabbit did not review this head: it hit its review limit. These are the reviews that stand in for it, with how each finding was resolved. Each reviewer was given an adversarial, read-only brief and ran unit tests only.

Round 1: fe91ffe8 (now f5ce1b84)

  • omo-native-code-reviewer: APPROVE, with one MEDIUM.
    • M1: each read rescanned all held output, so the cost grew quadratically. Fixed: the scan is incremental. 240 reads went from 1,508 ms to 26 ms.
    • LOW:
      • A quick resize there and back: herdr does redraw at the final size (captured), so this does not reproduce.
      • A screen herdr drew before a resize round trip that looks the same as the redraw cannot be told apart from it. This is documented as a known limit.
      • Two test-only notes.
  • Codex (gpt-6.1-sol, fast, xhigh): CHANGES, with four MEDIUMs, all fixed in c9982893:
    1. Spacing marks: Bun.stringWidth and herdr disagree on them, so a stale grid could match. The width is now read only from plain ASCII rows.
    2. Queued first screen: a first screen queued from before a round trip was taken for the redraw. Now the latest whole screen decides.
    3. Fragmented update: a large update arriving in small reads was rescanned on every read, which was quadratic. An open update is now kept as its reads and joined once it ends.
    4. Over 1 MiB released as one frame: sendOutput ended a healthy ACK client as stalled. Held output is now capped.

Round 2: 877d5357 (now c9982893)

  • omo: CHANGES, with one HIGH.
    • The HIGH: herdr's cursor placement at column 1 is one more ESC[r;1H with no text, so a valid redraw was taken for no screen. Fixed in f18aec33: each row is measured from its first move only.
    • LOW: the cap counted characters; it now counts UTF-8 bytes. An open update stayed in memory after release; it is now cleared.
  • Codex: CHANGES, with three MEDIUMs, all fixed in f18aec33:
    1. Cursor at column 1: the same issue as omo's HIGH.
    2. A top-row diff right after the redraw cancelled the match. Now only a screen at a grid the pty had (its start grid or one it was resized to) can cancel one.
    3. The cap applied only after the 100 ms hold. It now applies for the whole hold.
    • Declined: a single read over 1 MiB. The PTY host reads at most 64 KiB (READ_CHUNK in crates/pty-sidecar/src/main.rs; the Node host is bounded by its pipe), so it cannot occur.
    • Codex's own reproductions on the final code: 11 of 12 pass, and the remaining one is that single read.

Round 3: 64d97c13 (now f18aec33)

  • omo: APPROVE. No CRITICAL, HIGH or MEDIUM. Its three LOWs are listed under Known limits in the PR body.

Every fix has a test that fails on the commit before it, and each was checked with a targeted mutation.

@devswha

devswha commented Oct 29, 2026

Copy link
Copy Markdown
Owner Author

Merging f18aec3 (closes #2289). herdr draws an attach's first screen at the grid the attach started at and redraws for a new grid about 50 ms after a resize, so an attach resized before its first frame could show the old-size screen. An attach resized since it started is now held until herdr's whole screen at the pty's current grid arrives (recognised from one synchronized update's rows; only an update that began after the latest resize counts), capped at 1 s and 256 KiB, after which the bytes go out anyway, so no attach stays held. The lane's evidence: both orderings reproduced on main and fixed; three rounds of omo + Codex with every HIGH and MEDIUM fixed, omo APPROVE on the final; check fast green; terminal contract 262/0; real-server QA showed a right-sized first screen 20/20 against 16/20 on main. The merge pass read the diff.

@devswha
devswha merged commit b73c74f into main Oct 29, 2026
27 of 29 checks passed
@devswha
devswha deleted the fix/attach-hold-resize branch October 29, 2026 05:07
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.

Attach hold: a resize back to the start size, or a late resize redraw, can release an old-size frame (follow-up to #2255)

1 participant