Skip to content

perf(rpc): omit unset and empty optionals from history entries - #842

Merged
huichen merged 1 commit into
mainfrom
perf/remote-payload-trim
Sep 25, 2026
Merged

huichen merged 1 commit into
mainfrom
perf/remote-payload-trim

Conversation

@huichen

@huichen huichen commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Problem

A history page the phone downloads is mostly absent optionals. With the current SessionEntryPayload, every entry writes its six unset fields out as null, and every entry carries a metadata object that is empty in practice:

"runId":null,"metadata":{},"usage":null,"run":null,"session":null,"checkpoint":null

~68 bytes per entry of pure scaffolding. Nothing reads them: each consumer goes through an optional accessor (entry.usage?.outputTokens, obj.get("checkpoint")), and projection.ts only ever touches metadata.attachments.

Change

1. Six optional fields skip serialization when unset — matching what MessageBlock next door already does. Deserialization is untouched: a peer or a cached page that still spells them out as null decodes exactly as before (pinned by a test).

2. The display projection drops metadata when 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, with run_id already hoisted into the payload's own field — not the raw entries.metadata_json column. 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 attachments lives 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):

before after saved
session1 (6,300 entries) 11,423.9 KiB 11,055.8 KiB 368.1 KiB (3.2%)
session2 (2,462 entries) 4,254.3 KiB 4,110.4 KiB 143.9 KiB (3.4%)
session3 (2,166 entries) 3,215.1 KiB 3,088.4 KiB 126.7 KiB (3.9%)

Split: null optionals 2.4–3.0%, empty metadata 0.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 features negotiation 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.role is 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-rpc 126 tests, future-agent 1876, future-tui 2095, desktop backend 1297 — all pass on the pinned 1.97 toolchain. cargo fmt --check and cargo clippy --all-targets -D warnings clean.
  • New tests pin both directions of the metadata rule (the empty object is dropped; an empty attachments list is kept) and the serialization contract (unset optionals are omitted, explicit nulls still deserialize).
  • Both were falsified: flipping the metadata predicate to !is_empty() fails all three metadata tests; deleting one skip_serializing_if fails "metadata must be omitted, not null".

Two flakes were seen and confirmed unrelated by re-running: a cli_smoke singleton-lock race in future-tui (passes 3/3 on re-run) and prune_preview_cache_removes_expired_files in the desktop suite (passes in isolation, and the full suite then passed twice, 1297/1297).

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
huichen enabled auto-merge (squash) September 25, 2026 15:55
@huichen
huichen merged commit f52b0c1 into main Sep 25, 2026
12 checks passed
@huichen
huichen deleted the perf/remote-payload-trim branch September 25, 2026 16:04
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.
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