Skip to content

test(terminal): make the arrows and clicks browser check deterministic - #2219

Merged
devswha merged 3 commits into
mainfrom
fix/terminal-arrows-clicks-flake
Oct 28, 2026
Merged

devswha merged 3 commits into
mainfrom
fix/terminal-arrows-clicks-flake

Conversation

@devswha

@devswha devswha commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Problem

scripts/terminal-arrows-clicks-regression.ts (Browser regression, other scripts) failed in the first CI attempt of PRs that touch no terminal code, and passed on rerun:

Two causes:

  1. An arrow typed right after a letter. A plain arrow goes through herdr's pane.send_keys RPC, around the attach pty the letter was just typed into. herdr offers no barrier for bytes in that pty, so server/core/input.ts holds the arrow KEY_SETTLE_MS (40 ms) after the last pty keystroke (TYPED_SETTLE_MS after an ESC). fix(terminal): arrows reach full-screen programs, and a click reaches one that reads the mouse #634 documents this as a known limit: "the 40 ms settle is a margin, not a delivery acknowledgement". A stall longer than that on a busy runner (6 browser workers, Node PTY host) lets the arrow arrive first, and both failures in the letter/arrow sections are that. herdr 0.9.5 still has no barrier and no report of the program's cursor-key mode (checked its CLI and the generated API), so the product cannot make this exact. The check asserted an order the product only reaches by a margin.
  2. The first click after the pane opens. useTerminalMount hands a press on as a click only when term.modes.mouseTrackingMode is on at mousedown. The check clicked right after input-ready, and the attach's first output, which turns xterm's mouse reporting on, can be parsed after that. The press was then a plain selection and nothing was sent.

Change (test only)

  • Letters, then arrows. The check reads each letter before pressing the arrow that follows it, in the five a ← b rounds, after Escape, and after each letter between held repeats. An arrow followed at once by a letter stays back to back: the lane queues typing that arrives while a key waits, so that order is exact.
  • New server/core/key-settle.test.ts. It drives createInputLane with a fake attach and proves the hold that the browser check no longer races. The key goes out at least KEY_SETTLE_MS after a letter typed into the pty, and TYPED_SETTLE_MS after an ESC. A letter typed while the key waits goes behind it through herdr, not into the pty.
  • No fixed waits for "nothing arrives". Each Bun.sleep(400) is replaced by mark(): it types z after the action and waits until the program has read it and its frame is reported. Anything the action sent went out first on the same socket and pty (or herdr queue, on a mirror), so what came before the mark is all it sent. The file's entry leaves scripts/fixed-sleeps.json (9 → 0). The only sleep left is the autorepeat pacing, marked // fixed wait.
  • Clicks wait for .xterm.enable-mouse-events, the state the page reads to decide that a press is a click.

Evidence

Mutations, each run through the browser check and the unit test, then restored:

Mutation Result
typing ignores a waiting key (paneQueues.has(...) || stop → stop) browser FAIL an arrow between two letters arrives between them; unit FAIL (letter behind key)
no settle before a key unit FAIL (both hold tests); the browser check passes, since it now waits for the letter
forwardClick dropped browser FAIL timed out: left click received as press and release
link clicks replayed to the program (!onLink removed) browser FAIL a click on a link sends the program no button, actual \x1b[<0;7;1M\x1b[<0;7;1m: the mark catches bytes that do arrive
each arrow sent twice browser FAIL in application cursor mode

Reproduction. A pty stall longer than the settle, simulated with KEY_SETTLE_MS = 0 on 4 pinned busy cores: main's check failed 1 of 5 runs with CI's exact assertion, an arrow between two letters arrives between them. This branch's check passed 5 of 5.

Under load, main's check and this one alternating (pr-policy's flake rule), each run through dori heavy and bun run check run:

  • 12 pairs while a bun run check fast loop ran in another checkout: this branch 12/12, main 12/12.
  • 20 pairs pinned with taskset to 4 cores alongside 6 CPU hogs, to imitate a 4-core runner: this branch 20/20, main 20/20.

This PC's spare cores rarely reproduce the CI stall unaided, so the pass counts show the check holds up under load. The mutations and the stall reproduction above are what show it can still fail.

Review

Test-only, so the omo reviewer alone (notes/pr-policy.md): omo-native-code-reviewer APPROVE, with no CRITICAL or HIGH findings. Its MEDIUM finding was that the unit test ran with its own settle values, so a lowered KEY_SETTLE_MS or TYPED_SETTLE_MS would have slipped through. Fixed in the second commit: the two constants are now exported from server/index.ts (no behaviour change), and the test runs the lane with the shipped values against herdr's measured floors (a ~2 ms pty pipeline and a ~150 ms ESC hold). KEY_SETTLE_MS = 0 and TYPED_SETTLE_MS = 100 each fail it. The review's LOW findings are also fixed: a timed-out wait prints what was read, the mark checks that nothing came after it, and the check confirms that nothing follows the Escape and ArrowUp. The browser check also logs, without asserting, whether an arrow pressed right after a letter outran the margin. A third LOW (the queue test names the send_text path) is kept on purpose, because that path is intended.

bun run check fast: green on 39c26af (typecheck, build, unit tests and actionlint).

…not arrive in the arrows and clicks check

The check raced the server's 40 ms key settle, a margin herdr gives no
barrier for (#634), and clicked before xterm reported the mouse. It now
waits for each letter before the arrow after it, waits for mouse
reporting before clicking, and proves nothing arrived by typing a mark
instead of sleeping. server/core/key-settle.test.ts proves the settle.
…n a wait times out

The settle test now runs the lane with the values the server ships,
exported from server/index.ts, against herdr's measured ESC hold and pty
pipeline, so a lowered or zeroed settle fails it. The browser check
prints what it read when a wait times out, checks nothing follows the
Escape and ArrowUp, and logs (without asserting) whether an arrow pressed
right after a letter outran the settle margin.
@coderabbitai

coderabbitai Bot commented Oct 28, 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 11 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 b257be8 and e160d43.

Files selected for processing: 5
  • changelog.d/terminal-arrows-clicks-deterministic.md
  • scripts/fixed-sleeps.json
  • scripts/terminal-arrows-clicks-regression.ts
  • server/core/key-settle.test.ts
  • server/index.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.

…step

Its first letter goes through the attach pty and the rest may go through
herdr, so a mark typed after them could return before a stalled letter,
which then broke the Escape and ArrowUp assertion. Wait for all five bytes,
in any order, instead.
@devswha

devswha commented Oct 28, 2026

Copy link
Copy Markdown
Owner Author

Fixed in e160d43. The merge pass's MEDIUM (the at-once diagnostic) is confirmed: its a goes through the attach pty, while the arrow, b and a mark can all go through herdr's queue. So a stall past KEY_SETTLE_MS returned the mark before a, and the late a broke the Escape/ArrowUp assertion. The diagnostic now waits for all five of its bytes, in any order, and logs their order. It types no mark, so nothing of it can reach the next step.

I checked every other mark() call for the same mix: in each, the action and its mark take the same path. The lane chooses the pane queue or the pty by whether a key is waiting, and between an action and its mark that queue only drains (no key is pressed in between).

Verified at e160d43: bun run typecheck passed; the browser check passed once, then 5 of 5 with KEY_SETTLE_MS = 0 pinned to 4 busy cores, where the diagnostic logged 'in order' each time. The test changed only, so no other review was run.

@devswha

devswha commented Oct 28, 2026

Copy link
Copy Markdown
Owner Author

Merging e160d43: it makes the arrows-and-clicks browser check deterministic, the check that flaked on #2192. On 39c26af both reviewers reproduced one race: the optional diagnostic's marker, sent over the RPC lane, could return before its first letter arrived over the attach PTY, breaking the next assertion. e160d43 reads all five diagnostic bytes before continuing. Codex passes that change: the delayed-delivery scenario and all 120 byte orderings pass, the wait is bounded, and no fixed sleep was added. omo passed the rest of the PR. server/index.ts only exports two existing constants, so behaviour is unchanged. The required checks are green, and CodeRabbit did not review this head. Notes go to a follow-up issue.

@devswha
devswha merged commit 6863c39 into main Oct 28, 2026
19 checks passed
@devswha
devswha deleted the fix/terminal-arrows-clicks-flake branch October 28, 2026 08:00
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