Repository navigation
perf(remote,mobile): bound a run's first paint by bytes and rows; keep the shared walk whole - #1603
Merged
Merged
Conversation
…er bookkeeping Opening a conversation must not cost a turn's whole body, and it did. The history read completes a page back to the question its reply answers (`history::read_page`, `ExchangeStart::Complete`), and that walk had no byte bound: one turn of 1,113 rows / 10.35 MB became a 10.35 MB first paint on every open. Measured 2026-11-13 on the reported conversation: six serial Agent page reads (222/179/217/193/260/33 rows) assembled into one reply, 10.35 MB raw and 5.22 MB after the lean trim, with ~24 s from the first command to the end of the run replay over a relay. The walk now stops at `FIRST_PAGE_BYTE_BUDGET` (512 KiB). That value is the host's own reply budget (`wire_limits::BACKWARD_HISTORY_PAGE_BYTES`), not a second opinion: it is what a history reply already promises to fit, and the host cannot keep that promise for a single over-budget turn because it may only defer whole exchanges. A turn bigger than the budget on its own therefore comes back as the newest rows that fit, with `truncated: true` and a cursor that still reaches the rest. Same conversation after: 33 rows, 0.52 MB raw, 0.29 MB lean, one Agent read instead of six. The lean page also drops `blocks[].provider_metadata`, which is provider bookkeeping for re-sending a turn (a thinking signature, a cache hint) and not display: the only occurrence of the name in the front ends is the type declaration `providerMetadata?: unknown` in `packages/thread-projection/src/events.ts` (nothing reads it), and `git grep provider_metadata -- host/src desktop/src-tauri/src tui/src` is empty. It was 1.77 MB of that 5.22 MB page. Tests pin all of it: a turn larger than the budget comes back as its tail (the `<= FIRST_PAGE_BYTE_BUDGET` assertion is what a reverted tail fails, and the constant itself is pinned so widening it is a failing test), an exchange that fits is still completed to its question, and the lean trim keeps every one of 1,113 rows, every block and their order - a sequence comparison, not a picture. Not done here, deliberately: the phone's cold-open backfill still fills to three exchanges, so it still reads the rest of a huge turn one bounded page at a time; and a page that already starts at its question but is over budget is left whole - cutting it mid-exchange is a row-level trim that changes what a reader sees, and that waits until a run is actually being truncated in the field.
Opening a conversation reads its tail page and, when that page was cut by the
reply budget, backfills until the window holds three exchanges. A conversation
that is streaming has a turn in flight, and one turn is one exchange: the pages
above it hold that same turn's rows, so the count never reaches three and the
walk only ends when it has crossed the whole run — one round trip per page, all
of it before the transcript can be drawn.
Measured on this machine against the real store (2026-11-13), session
`8ac7afd8` (1,712 display rows, one run owning 1,176 of them, 70%):
pages read 10 -> 2
rows moved 1,500 -> 300
get_session_entries
round trips 10 -> 2
agent-side read ~330 ms -> ~66 ms (measured mean 33 ms per page read for
this session's 1,057 logged page reads)
The agent-side column is what the Agent logged; the part the reader feels on a
phone is the page count multiplied by the relay's round-trip time, so the ten
sequential reads were the seconds in "opening a streaming conversation takes
seconds".
Two bounds, both falsified by a test that counts reads:
- a page that adds no exchange stops the walk once the window already holds one
(the guard). It must not stop before that: a window with no user row projects
to nothing, so the earlier the guard fires the more it would erase — the rule
is stated as "a page that adds no exchange AND the window holds an exchange".
- at most two extra pages, because an exchange spans its user row and the steps
after it: the first page cannot separate "this turn continues above" from
"the previous turn starts above", and the second page is what shows the page
above is still the same turn. Past that the window is already short by the
run's own length and every further page is a round trip the reader waits on.
Measured: 2 rather than the 10 the unbounded walk spends; raising it spends
the round trip, and zero drops the backfill the rule exists for.
A window that stopped short is reported, not drawn silently: the controller
publishes `historyBackfillTruncated` (derived, so it clears once the window
holds the exchanges) and the timeline's existing "load older" affordance says
"earlier messages aren't all loaded — tap to load them" instead of the generic
label. The rows above stay reachable through the paging cursor, which the stop
never moves.
Scope: the client's stop decision only. `BACKWARD_HISTORY_PAGE_BYTES`, the page
assembly and `provider_metadata` belong to the host-side change that landed as
`perf/first-paint-budget`; nothing here touches them.
Verification: mobile 166 suites / 3,176 tests green (including four new
assertions here), `packages/remote-client` 32 suites / 706 tests, thread
projection 453 tests, `tsc --noEmit` and eslint clean. Each of the three knobs
was falsified: dropping the guard takes the long-run case from 2 reads to 3,
setting the cap to zero takes the cap case from 3 to 1, and dropping the
"window holds an exchange" rule takes the unanchored case from 3 to 2.
…nning Opening a conversation whose run is in flight asked the host for that run from index 0 (`sinceIdx: -1`) and then paid one relay round trip per 1,000 events for it. Measured on the reported conversation (2026-11-13 14:04:20.5-14:04:27.6): one `get_run_snapshot` (1 ms) plus ten `get_events_since` pages (62-170 ms each server-side), ~10,000 events, ~6.6 s of wall clock before the newest screen was on screen. The read now starts at the run's newest screen: `get_state` already reports `activeRun.lastEventIdx`, so the boundary is that index minus one client page, and the page it returns is trimmed to 512 KiB before it leaves (an event count is a guess about sizes; one tool event can outweigh a thousand token deltas). The page says where its feed begins (`startIdx`), and `hasMore`/`nextSinceIdx` are unchanged, so a client that wants more still gets it. The same run today: one round trip, the last 1,000 events, 0.42 MB instead of 12 round trips and 5.11 MB. What the first paint skips is not lost and is not a hole: those are the run's earliest rows, which the conversation's own history pages still carry, so scrolling up reaches them. `the_head_a_first_paint_skips_is_still_reachable_through_history` pins exactly that. Not touched here, deliberately: the history page's own bounds (the completion walk's 512 KiB and `truncated`), the client's backfill loop, and `preferSnapshot` (a snapshot bootstrap still folds the whole run; that is a separate question). Reverse proof, in the suite: `forcing_the_old_start_serves_the_run_from_its_beginning` drives the same handler with the old start forced through a test-only switch and shows the unbounded read; flipping the shipping code back to "no boundary" makes `a_whole_run_replay_starts_at_the_runs_tail` fail on `startIdx` (measured: `left: Null, right: Number(4001)`). Every assertion is bytes, events, round trips or the `sinceIdx` the Agent was actually asked for - no screenshots. Numbers, sources and reproduction steps (including why the "after" elapsed time is computed rather than measured on this machine: the running Agent's version does not match this branch's client, and an isolated instance needs a 3 GB `agent.db` copy) are in `docs/testing/remote-first-paint-budgets-2026-11-13.md`, which also carries the history-page measurements that landed in `perf/first-paint-budget` so a PR body can quote both from the repository.
The 512 KiB bound added last time weighed the Agent's raw page, and the page a learner actually receives is not that page: the lean lane drops reasoning bodies, tool-result bodies, tool arguments, the shell envelope and provider bookkeeping. On the reported conversation's last turn those are 86% of the bytes - 12.99 MB raw, 1.75 MB sent - so the budget was buying rows nobody would ever see. `read_page_with` now takes the caller's weight, and the host weighs a row the way `lean_entries` will leave it (`page_weight_as_sent`). A client that did not declare the lane keeps the raw weight, so nothing changes for it. The same change adds a second, independent bound: at most 300 rows in a first paint. Bytes are a transfer cost and rows are a render cost - a thousand rows of two hundred bytes each move cheaply and are still a thousand row models to build - so a page stops at whichever bound comes first. `PageBudget::FIRST_PAINT` holds both numbers. Measured on the reported conversation (same store, field-level model): | | rows in the first paint | bytes sent | scroll-ups to read that turn | | --- | ---: | ---: | ---: | | before (512 KiB of raw) | 33 | 0.29 MB | ~50 | | after (512 KiB sent + 300 rows) | 300 (row bound) | ~0.35 MB | ~6 | Tests pin both properties: `a_first_paint_is_bounded_by_rows_as_well_as_bytes` (a thousand tiny rows stop at the row bound), `a_cheaper_weight_fits_more_rows_in_the_same_budget` (a lean-weighted page holds more than twice the rows of a raw-weighted one), `a_lean_row_weighs_what_the_trim_leaves` and `a_client_without_the_lane_is_weighed_by_its_own_bytes` (the weight is the trim's, and only for a client that declared it). Side effects, stated rather than discovered later: 1. The first paint holds more rows than it did, so the client builds more row models - and it is bounded by the second number, which is why 300 was chosen instead of leaving rows unbounded. 2. Filling 512 KiB of sent bytes can cost the host one or two more Agent slices locally (about 1 MB each on this store). That is loopback gRPC, not the relay. 3. Nothing on the wire exceeds 512 KiB, and an undeclared client behaves exactly as before. Numbers, the field breakdown they come from and the reproduction SQL are in `docs/testing/remote-first-paint-budgets-2026-11-13.md` (section 4).
huichen
enabled auto-merge (squash)
November 13, 2026 15:27
`read_page` is the shared walk behind the CLI's page read and the agent's own paging contract test (`a_backward_exchange_larger_than_the_budget_is_complete_before_projection`). Routing it through `PageBudget::FIRST_PAINT` cut those readers at 512 KiB / 300 rows and set `truncated` — a flag neither of them reads — so the CLI's `get_session_entries_page` served a turn's tail where it asked for the turn, and `Rust * test / agent` went red on the agent-side test that pins the completed exchange (measured locally: 3070 passed, 1 failed before this commit). The budget is a *first paint's*, and bounding is something a caller opts into: the host passes it explicitly through `read_page_with` (weighing a row the way its lean lane ships it) and the terminal's first page goes through `read_page_from`. `read_page` passes `PageBudget::UNBOUNDED`, which is what a reader with no screen to paint asked for. `the_shared_walk_completes_an_exchange_larger_than_a_first_paint` pins the completed read, and flips to failing on the second fetch if the bound comes back.
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.
Opening a conversation whose run is long or in flight used to cost the whole of it before anything was on screen: one turn of 1,113 rows / 10.35 MB became a 10.35 MB first paint, and an in-flight run was replayed from index 0 at one relay round trip per 1,000 events. These commits bound the first read at both ends — a first page bounded by the bytes actually sent and by rows, and a run read that starts at its newest screen — and stop the phone's tail backfill from walking a run it is currently streaming. The last one repairs a regression those bounds introduced in a shared walk that the CLI and the agent's own paging contract test also go through.
Commits
FIRST_PAGE_BYTE_BUDGET(512 KiB, the host's ownwire_limits::BACKWARD_HISTORY_PAGE_BYTES); an over-budget turn comes back as its newest rows withtruncated: trueand a cursor to the rest. The lean page also dropsblocks[].provider_metadata, which no front end reads (1.77 MB of that 5.22 MB page).get_statealready reportsactiveRun.lastEventIdx, so the replay starts one client page below it and is trimmed to 512 KiB before it leaves; the page reportsstartIdx, andhasMore/nextSinceIdxare unchanged. One round trip and 0.42 MB instead of 12 round trips and 5.11 MB. What the first paint skips is not a hole: those are the run's earliest rows, which the conversation's own history pages still carry.read_page_withnow takes the caller's weight and the host weighs a row aslean_entrieswill leave it; a client that did not declare the lane keeps the raw weight. A second, independent bound of 300 rows joins it — bytes are a transfer cost, rows are a render cost — and a page stops at whichever comes first: 300 rows in ~0.35 MB against 33 rows before, about 6 scroll-ups instead of 50 to read that turn.read_pageis the shared walk behind the CLI'sget_session_entries_pageand the agent's paging contract test. Routing it throughPageBudget::FIRST_PAINTcut those readers at the budget and set atruncatedflag neither of them reads, so the CLI served a turn's tail where it asked for the turn and the agent's contract test hung instead of completing — the threeRust * test / agentjobs of run 43452736670 sat in that step until the 60-minute job timeout. Bounding is now something a caller opts into (read_page_withfrom the host,read_page_fromfor the terminal's first page);read_pagepassesPageBudget::UNBOUNDED.Verification
Local run on the frozen tip
9747363d1, in a detached worktree, rustc pinned to 1.99.1 (rust-toolchain.toml),ulimit -n 8192,CARGO_TARGET_DIRshared with the main checkout:cargo fmt --all -- --check— cleancargo check -p future-rpc -p future-host -p future-remote-server— cleancargo clippy -p future-rpc -p future-host -p future-remote-server --all-targets -- -D warnings— cleancargo test -p future-rpc -p future-remote-server— 210 passed (5 ignored) + 269 + 7 + 6 passedcargo test -p future-host --lib— 2246 passed, 0 failed, 15 ignoredcargo test -p future-agent(the CI command line,--no-fail-fast) — lib 3071 passed / 0 failed, 10 integration targets green--test-threads=4, three times in a row — 111 s and 112 s greencargo test -p future-rpc --lib— 272 passedcargo fmt -p future-rpc -- --check+cargo clippy -p future-rpc --all-targets -- -D warnings— cleancargo check -p future-cli -p future-tui -p future-host --all-targets— cleanmobilenpx tsc --noEmit— clean;npx jest src/remote src/features/chat— 89 suites / 1860 tests passedmake check-docs— 418 docs OK, 0 pairs pending bilingualization, provider matrix 13 rowsThe new assertions are bytes, event counts, round trips, rows and the
sinceIdxthe Agent was actually asked for.forcing_the_old_start_serves_the_run_from_its_beginningdrives the same handler with the old unbounded start;a_first_paint_is_bounded_by_rows_as_well_as_bytesanda_cheaper_weight_fits_more_rows_in_the_same_budgetpin the two bounds;the_shared_walk_completes_an_exchange_larger_than_a_first_paintfails on its second fetch if the unbounded read comes back. Reverting the shipping code makesa_whole_run_replay_starts_at_the_runs_tailfail onstartIdx.User-visible changes
truncated, with a cursor that still reaches the rest; before, the whole turn was assembled before anything showed. Revert76643912c.16794b34b.d7b375293.d79d885d8.8be0819d1.No migration, no default or setting change, no permission or privacy change.
Notes
Measurements, sources and reproduction steps (including why the "after" elapsed time is computed rather than measured on this machine) are in
docs/testing/remote-first-paint-budgets-2026-11-13.md.