Skip to content

perf(remote): trim the history page a phone does not read - #846

Merged
huichen merged 1 commit into
mainfrom
perf/lean-history-entries
Sep 25, 2026
Merged

huichen merged 1 commit into
mainfrom
perf/lean-history-entries

Conversation

@huichen

@huichen huichen commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Follows #844, which did this for the live event lane. The durable history read was still sending every reasoning body, every tool output and every tool-call argument.

Effect

Through the shipping trim over the three heaviest real sessions (scripts/measure-lean-history.py, which dumps a session's get_session_entries reply from the live read-only agent and runs the real Rust rewrite over it):

entries before after saved
6,300 11.74 MiB 3.86 MiB 67.1%
2,462 4.57 MiB 1.25 MiB 72.7%
2,166 3.56 MiB 1.12 MiB 68.4%

Measured per payload:

Trim Share of a page
reasoning body 25.6–32.2%
tool-result body 22.0–26.5%
tool-call arguments beyond the four a target can come from 12.3–17.8%

Why each one is a removal, not a fidelity trade

  • Tool-call arguments. targetFromArgs reads command for a shell call and path/file_path/filePath otherwise, and reads no other argument key for any tool name — an unrecognized name is treated as shell and still only reads command. Keeping exactly those four keys is therefore behaviour-preserving even for a tool this client has never seen.
  • Tool-result body. foldToolEntry reads toolCallId and isError and nothing else.
  • Reasoning body. Only rendered when its row is expanded.

Two properties that keep it safe

It never changes structure. Nothing adds, removes or reorders an entry or a block — entries keep their identity and count — so a page's nextOffset/hasMore/flush-cursor arithmetic and the client's gap-fill are untouched. (The event lane needed careful cursor handling for exactly this reason; here it is a field-level shrink.)

It runs before the byte budget, not after. The budget sheds whole oldest exchanges to fit one reply, so measuring the trimmed page is what lets it hold more of them per round trip.

Both history shapes are covered — the paged read the phone actually uses (before + limit) and the full read — because they are separate code paths. The first falsification attempt is what caught that: I made the trim unconditional on the paged path only, and the test still passed, because it was exercising the other one.

The client half

The reasoning row needed a matching change in the shared projection: it only produced a thinking segment when the block had text, which is precisely what a lean page no longer sends. It now projects the row from the block's presence — the same fix the live lane already received in #844. Without it, a lean history page would show no reasoning rows at all.

Verification

  • Desktop Rust: 1312 pass (two consecutive full-suite runs); cargo fmt --check + cargo clippy --all-targets -D warnings clean on pinned 1.97.
  • Mobile: 128 suites / 2052 pass; tsc --noEmit + eslint clean. Desktop TS (consumes the shared projection package): 145 files / 1255 pass; tsc + eslint clean.
  • 5 new unit tests for the rewrite (the trims, entry/block count preservation, fail-closed on unrecognized shapes, and that every key kept is one a target can be derived from), plus an end-to-end test through the real command path asserting the gating in all three states (undeclared → full, declared → trimmed, withdrawn → full again).
  • Falsified in three directions: unconditional trim fails the undeclared assertion; removing the trim fails the lean assertions; requiring text for a reasoning segment fails the history projection test; and narrowing targetFromArgs to path alone fails the new argument-key test — which is how I found that test was only covering one of the four keys.

Follow-ups, deliberately not in this PR

  • MessageUsage / MessageRun still serialize their unset sub-fields ("error":null on nearly every entry). Worth 79.9 KiB = 0.66% of a full page, 2.0% of a trimmed one. It belongs with perf(rpc): omit unset and empty optionals from history entries #842's class of change rather than here: every change in this PR is capability-gated, and mixing in an ungated field change would blur the rollback boundary.
  • The history failure state is already broken independently of this work: all 16,010 tool_result blocks in the journal have is_error NULL and none is true, so foldToolEntry has no failure signal to read and every history tool row renders as completed. Removing the output text does not change that. Fixing it means writing the flag (or an exit code) at record time.

The lean declaration covered the live lane; the durable history read was
still sending every reasoning body, every tool output and every tool-call
argument. Measured through the shipping trim over the three heaviest real
sessions (`scripts/measure-lean-history.py`):

  entries   before MiB   after MiB   saved
     6300        11.74        3.86   67.1%
     2462         4.57        1.25   72.7%
     2166         3.56        1.12   68.4%

Three payloads, each unread rather than merely unrendered:

- reasoning body (25.6-32.2% of a page). The block stays, so the row that
  says the model reasoned still appears.
- tool-result body (22.0-26.5%). `foldToolEntry` reads `toolCallId` and
  `isError` and nothing else.
- tool-call arguments beyond the four a target can come from
  (12.3-17.8%). `targetFromArgs` reads `command`, `path`, `file_path` or
  `filePath` and no other key for any tool name, so keeping exactly those
  four is behaviour-preserving even for a tool this client has never seen.

Nothing here adds, removes or reorders an entry or a block, so a page's
`nextOffset`/`hasMore`/flush-cursor arithmetic and the client's gap-fill are
untouched. It is applied *before* the page byte budget rather than after:
the budget sheds whole oldest exchanges to fit one reply, so measuring the
trimmed page is what lets it hold more of them per round trip.

Both history shapes are trimmed — the paged read the phone actually uses
(`before` + limit) and the full read — because they are separate code paths.

The reasoning row needed the client half of the same change: the shared
projection only produced a thinking segment when the block had text, which
is exactly what a lean page does not send. It now projects the row from the
block's presence, matching what the live lane already does.

Same declaration as the lean event lane: it is the same client generation,
because the reasoning row, the tool target and the tool outcome are precisely
the three things that generation reads differently.
@huichen
huichen enabled auto-merge (squash) September 25, 2026 17:25
@huichen
huichen merged commit beb050d into main Sep 25, 2026
12 checks passed
@huichen
huichen deleted the perf/lean-history-entries branch September 25, 2026 17:34
huichen added a commit that referenced this pull request Sep 25, 2026
…ll (#850)

* test(mobile): verify the lean lane through the real render surface

The lean lane's client half (#844/#846) is three reads: a reasoning row from
its boundary alone, a tool target from `tool_start`'s complete arguments, and
a tool outcome from `tool_end`'s structured fields. They are covered by unit
tests against the projector, but nothing had fed them to the screens the phone
actually draws.

This adds a render fixture set and five screenshot-harness scenarios:

- `scripts/screenshots/gen-lean-render-fixtures.mjs` authors the full-feed wire
  shapes (live lane and history page) and derives the lean ones through the
  shipping Rust trims (a new ignored test in `remote_host::lean`, driven the
  same way `measure-lean-history.py` drives its measurement) — so a capture
  cannot test a hand-trimmed paraphrase of the wire.
- `?leanRender=<mode>` feeds those slices to the real conversation screen; the
  projection, components, i18n and layout stay the shipped implementation.
- The scenarios assert the three reads plus the control: the same run as the
  full feed must not regress (reasoning body, `[exit: N]` outcome path), and
  the trimmed payloads must stay off screen.

The mid-run scenario reproduces a real bug in the client half, fixed in the
next commit: a bodyless reasoning slot after a tool call was swallowed by the
tool run's "hop over whitespace-only text".

* fix(thread-projection): keep a bodyless reasoning row after a tool call

`buildSegments` gathers a run of adjacent tool slots while hopping over
whitespace-only text between them. The hop tested `!current.text.trim()` for
every slot type, so a reasoning slot with no body — the normal case in the
lean feed, whose deltas are never published — read as whitespace and was
dropped: its row disappeared, and the tool calls on either side of it were
glued into one burst.

Only a text slot can be glue between tools. A bodyless reasoning row after a
tool call (the live tail of a run that thinks again, or between two calls) now
renders, with a regression test for both shapes.

* style(desktop): rustfmt the lean fixture generator

The generator test was added while working in the mobile harness, so the
desktop backend's rustfmt was never run on it. CI's
`cargo fmt --check --manifest-path desktop/src-tauri/Cargo.toml` caught it.

No behaviour change: rustfmt only.
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