Skip to content

Batch terminal measurements when fitting REPL readings - #1352

Draft
taras wants to merge 3 commits into
mainfrom
codex/repl-batched-fitting
Draft

taras wants to merge 3 commits into
mainfrom
codex/repl-batched-fitting

Conversation

@taras

@taras taras commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Why

Opening a long REPL entry makes the terminal renderer measure each logical line separately at every step of wrapping. The work delays the first visible Output after selecting or submitting an entry.

What changes

The fitter sends width probes for several lines in the same renderer call. Each line keeps its own grapheme boundaries, search window, and indentation. The fitter limits batches with multiple search probes to 2,048 parts so they fit the smaller measuring cache on narrow terminals. Near the terminal width or when a batch refuses, the fitter measures lines individually in written order.

How to verify it

The Source reading tests compare fitted rows against the real renderer run one line at a time. The tests cover Unicode, cached and uncached plain text, wrapping near the terminal width, and refusal order.

In the dedicated 80×24 regression, fitting 1,000 wrapping lines took 41 measurement renders, down from about 12,000 on the base. The median time spent preparing that reading fell from 157 ms to 44 ms across seven runs.

ReplBench ran three paired macOS launches at each viewport with the same synthetic 1,000-line traceback. The medians below measure from input through the expected terminal text.

Viewport Action Before After
160×36 Select the long entry 444 ms 104 ms
160×36 Submit the traceback and show its Output 1,019 ms 335 ms
160×36 Send ten wheel-down events and verify the resulting viewport 148 ms 144 ms
160×36 Type one key in Draft 21 ms 21 ms
80×24 Submit the traceback and show its Output 330 ms 214 ms

Terminal frames

The styled frames match at all five checkpoints after accounting for the generated session ID. Captures use base 8c71b35b6 and head 1ceee0bf4.

Checkpoint Before After
First Output after submitting at 160×36 Baseline Output after submitting Candidate Output after submitting
Output after selecting Entry 1 at 160×36 Baseline Output after selection Candidate Output after selection
Output after ten wheel-down events Baseline Output after scrolling Candidate Output after scrolling
Draft after typing beside the long entry Baseline Draft after typing Candidate Draft after typing
First Output after submitting at 80×24 Baseline narrow Output Candidate narrow Output

Scope

This is the fitting optimization discussed alongside #1342. The Markdown presentation work is tracked in #1351.

Scope confirmation

  • Every changed file supports the fitting optimization.
  • The diff is limited to fitting and its regression tests.

Entry readings and the lines a plain fit store does not hold are cut in one search: each binary-search step for every line shares a measuring render, through the existing widths limits. A single line asks exactly what it asked before. Rooms that reach the measuring columns keep the one-line path, and a failure found while batching is discarded so the lines are cut again in written order, reporting the first failure as before. A cold 1,000-line wrapping entry goes from about 24,000 measuring renders to fewer than 150, with identical rows.
@taras

taras commented Nov 16, 2026

Copy link
Copy Markdown
Owner Author

Independent review from Claude Fable 5.2

Changes requested for 67fd0a5dbc1d7daff4eaf8c61397f6b4556ea376.

  • At 80×24, a cold 1,000-line wrapping reading can exceed Clay's measurement cache with the first search batch. The renderer rebuilds, refuses the batch, and the fitter falls back to measuring each line. Across seven runs the median went from 154 ms on base to 178 ms on this head, with about 12,000 measuring renders in both. The existing 160×36 render-count test misses this. Bound the search batches for the smaller cache and add an 80×24 render-count regression.
  • A 1,200-case differential check with the real renderer found zero differences in rows, cell widths, refusal messages, or store contents compared with base. The search preserves grapheme boundaries, refusal order, and cache ownership. The implementation reuses one search for individual rows and grouped probes and adds no public API.

@taras

taras commented Nov 16, 2026

Copy link
Copy Markdown
Owner Author

Independent review from Claude Fable 5.2

PASS for 1ceee0bf4d73b5db749408721b64c04befa1e443 against 8c71b35b64210e3206c15683accd28c9a73424f8.

The 2,048-part budget applies to grouped line-search probes. Individual measurements and finished row widths still use the existing behavior. At 80×24, the same 1,000-line fixture dropped from 12,004 measurement renders on the earlier PR head to 41 on this head. The median across seven isolated runs was 154 ms on base, 178 ms on the earlier head, and 47 ms with the correction. At 160×36, the 2,000-line fixture took 82 ms on this head, compared with 87 ms on the earlier head and 599 ms on base.

The real-renderer comparison found identical output, refusals, and retained cuts in 1,200 differential cases. Narrow and wide stress probes agreed with base, including the existing refusal in the finished-width pass for 2,000 non-wrapping lines at 80×24.

This branch has not been deployed

No deployments
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