Repository navigation
perf(rpc): omit unset and empty optionals from history entries - #842
Merged
Merged
Conversation
A history page the phone downloads is mostly absent optionals: with the
current struct every entry spells out its six unset fields as `null`, and
every entry carries a `metadata` object that is usually empty. Measured on
the three heaviest real sessions (via the live agent, one serializer over
one dataset), removing them shrinks the page by 3.2-3.9%:
session1 11,423.9 -> 11,055.8 KiB (-368.1 KiB, 3.2%)
session2 4,254.3 -> 4,110.4 KiB (-143.9 KiB, 3.4%)
session3 3,215.1 -> 3,088.4 KiB (-126.7 KiB, 3.9%)
of which null optionals 2.4-3.0%, of which empty `metadata` 0.8-0.9%.
- `SessionEntryPayload`'s six optional fields now carry
`skip_serializing_if`, matching `MessageBlock`. Deserialization is
unchanged: a peer or cached page that still sends `null` decodes as
before. Every consumer reads these through an optional accessor
(`entry.usage?.outputTokens`, `obj.get("checkpoint")`), so an omitted
field and an explicit `null` are indistinguishable to it.
- The display projection drops `metadata` when it is an empty object.
The value is `SessionEntry.meta` with `run_id` already hoisted out, not
the raw `entries.metadata_json` column, whose `{meta,timestamp}` wrapper
is loader plumbing that never reaches the wire — the served value is
`{}` for 6,292 of a real page's 6,300 entries. Anything non-empty is kept
verbatim, because `attachments` lives there and is rendered on the phone.
Tests pin both directions of the metadata rule (the empty object goes, an
empty `attachments` list stays) and the serialization contract (unset
optionals are omitted, but explicit `null`s still deserialize).
huichen
enabled auto-merge (squash)
September 25, 2026 15:55
This was referenced Sep 25, 2026
huichen
added a commit
that referenced
this pull request
Sep 25, 2026
…ies (#849) * perf(rpc): omit unset usage/run subfields from history entries A `run` object rides nearly every history entry, and a completed run has only some members set, so the serializer spelled the rest out as `null`. Measured through the shipping serializer over three real sessions' `get_session_entries` dumps, those nulls are 0.66-0.75% of the page: session1 12,357,563 -> 12,275,730 B (-81,833: run.error x6,293, usage.cacheWriteTokens x1) session2 4,809,110 -> 4,777,095 B (-32,015: run.error x2,459, usage.cacheWriteTokens x2) session3 3,748,708 -> 3,720,576 B (-28,132: run.error x2,164) - `MessageUsage` and `MessageRun` now carry `skip_serializing_if`, matching `MessageBlock` / `SessionEntryPayload`. Deserialization is unchanged: an explicit `null` written by a peer or cached page decodes as before, which the new tests pin in both directions. - Every consumer reads these through optional access: the shared thread-projection `SessionEntry` declares each subfield optional-null and reads it under `?.` / `!= null` guards, Rust consumers only map Options to proto (or never read the fields). The one exact-shape expectation, the get_state parity fixture, moves to the new canonical output. - `tests/measure_usage_run_nulls.rs` is the ignored instrument that produced the numbers (and reproduces the "before" line on a base-commit checkout). * perf(rpc): stop writing a false isError on every tool result `MessageBlock::is_error` is set from the journal with `unwrap_or(false)`, so a tool result that is not an error carries an explicit `"isError": false` on the wire. No consumer can tell that from an absent flag — the phone reads `if (block.isError && …)` — so a history page was spending ~15 bytes on every tool result saying nothing happened: blocks with false before KiB after KiB saved 3,144 11,141.9 11,092.7 49.1 KiB (0.44%) 1,242 4,144.0 4,124.6 19.4 KiB (0.47%) 1,072 3,118.0 3,101.2 16.8 KiB (0.54%) That is ~1.35% of the trimmed page the phone actually receives, and it grows as a share as the other trims land. `true` is still written: it is the only value that carries information, and this pairs with recording the real outcome — once failures set it, a success sends no flag at all instead of a `false`. The predicate deliberately replaces `Option::is_none` rather than sitting beside it, because both uninformative spellings have to be skipped: with `is_false` alone, `None` would have started serializing as `null` again, undoing #842. A test for the unset case caught exactly that. Deserialization is unchanged: an explicit `false` or `null` from an older peer (or a cached page) still decodes to the same value. * test(agent): update the entries shape assertion and cover the failing case `get_session_entries_renders_roles_and_run_stats` asserted `blocks[0].isError == false`, which is the old canonical shape: a successful tool result no longer writes the flag, because every consumer already reads an absent `isError` as "not an error". Rather than only relaxing the assertion to "the key is absent", which would also pass if the flag were dropped unconditionally, the fixture now carries a second, failing tool call and asserts `isError == true` on it. That keeps the informative value covered at this layer, where before only the `false` case was. Caught by CI, not by my local scoping: I had run the consumer crates' modules closest to the change and missed this file. The full `future-agent` lib run (1876) is what actually covers it. * style(agent): rustfmt the entries shape test Missed rustfmt after editing the fixture; CI's workspace fmt check caught it. No behaviour change.
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.
Problem
A history page the phone downloads is mostly absent optionals. With the current
SessionEntryPayload, every entry writes its six unset fields out asnull, and every entry carries ametadataobject that is empty in practice:~68 bytes per entry of pure scaffolding. Nothing reads them: each consumer goes through an optional accessor (
entry.usage?.outputTokens,obj.get("checkpoint")), andprojection.tsonly ever touchesmetadata.attachments.Change
1. Six optional fields skip serialization when unset — matching what
MessageBlocknext door already does. Deserialization is untouched: a peer or a cached page that still spells them out asnulldecodes exactly as before (pinned by a test).2. The display projection drops
metadatawhen it is an empty object.Worth being precise about what that value is, because it is easy to reason about the wrong one: it is
SessionEntry.meta— the entry's own metadata object, withrun_idalready hoisted into the payload's own field — not the rawentries.metadata_jsoncolumn. That column looks like{"meta":{},"timestamp":…}(97% of a 31k-row journal), which would suggest ~57 bytes/entry of redundancy, but its outer wrapper is loader plumbing that never reaches the wire. Measured on a real 6,300-entry page, the served value is{}for 6,292 entries and{"attachments":…}for one.So the only redundant shape is the empty object, and the saving is its 14 bytes rather than 57. Anything non-empty is kept verbatim, because
attachmentslives there and is rendered — the phone shows the attached images and files.Measured effect
Fetched from the live unmodified agent via
get_session_entries, then the B1 rules applied to that same data, so the delta comes from one serializer over one dataset (the desktop re-serializes the agent's proto entries into the phone's JSON, which is the representation measured):Split: null optionals 2.4–3.0%, empty
metadata0.8–0.9%.Scope note
This is the safe, ungated half of the payload-trimming work, deliberately split from the half that needs a capability-gated rewrite layer. It touches only shapes that carry no information when absent, so an older client is unaffected and no
featuresnegotiation is involved.It is also a modest win — ~3.5% of a history page. The larger savings (tool arguments, tool-result text, reasoning text) are the other half and are not here:
entry.roleis read directly by the mobile projector and the TUI, so removing fields like that would break an unmodified client and needs a per-client rewrite in the desktop's history relay, which today is pure pass-through.Verification
future-rpc126 tests,future-agent1876,future-tui2095, desktop backend 1297 — all pass on the pinned 1.97 toolchain.cargo fmt --checkandcargo clippy --all-targets -D warningsclean.attachmentslist is kept) and the serialization contract (unset optionals are omitted, explicitnulls still deserialize).!is_empty()fails all three metadata tests; deleting oneskip_serializing_iffails "metadata must be omitted, not null".Two flakes were seen and confirmed unrelated by re-running: a
cli_smokesingleton-lock race infuture-tui(passes 3/3 on re-run) andprune_preview_cache_removes_expired_filesin the desktop suite (passes in isolation, and the full suite then passed twice, 1297/1297).