Skip to content

perf(remote): drop the tool bodies a phone never renders from the live lane - #870

Merged
huichen merged 1 commit into
mainfrom
perf/lean-live-bodies
Sep 26, 2026
Merged

huichen merged 1 commit into
mainfrom
perf/lean-live-bodies

Conversation

@huichen

@huichen huichen commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

The lean feed dropped the two content streams but forwarded every other event verbatim — so a write still shipped the whole file it was about to write, and an edit the text it replaced. On the live lane this was exactly what the history page had already stopped doing (#846). A usage event still carried the provider's full report where the client reads a single number out of it.

What changed (remote_host::lean)

  • tool_start/toolcall_start: for write and edit, keep only the argument keys a tool row's label is built from (path / file_path / filePath); drop the rest. shell keeps its command (it is the row's label), read keeps its few bytes, and a tool this build does not know keeps everything — nothing here can tell which of its arguments a future client reads. Both argument shapes are handled (object, and the JSON string the agent has also emitted), preserving the shape and returning the payload borrowed when there is no body to drop.
  • usage: keep completion_tokens (plus the output_tokens alias the client also reads) and drop the provider's other counters, the credit cost and the stop reason.

Both are gated by lean_events_v1, so an undeclared client is unaffected.

Why this is safe

  • The client builds a tool row's label through targetFromArgs, which reads only path/command-family keys; the file bodies were never rendered.
  • Mobile sums only completion_tokens out of a usage event (liveApply.usageOutputTokens), and it passes preferEndTokens: true, so the per-call sum is the fallback for a run with no agent_end. The other counters and credit_cost feed the desktop footer, whose source is the agent, not this lane (the desktop never subscribes to p.{pair}.evt.* — it only publishes).
  • Verified on real data that the fallback is exact: per-call completion_tokens sum = 181,170 = agent_end.usage.output_tokens for the same run.

Measured (three heaviest completed runs, shipping trims, real journals)

run coalesced lane before after saved
1 764,398 B 607,798 B -20.5%
2 767,459 B 586,864 B -23.5%
3 441,876 B 369,437 B -16.4%

Lane composition now: per-tool-call metadata 46–53%, outcomes 15–18%, usage 12–14%, thinking boundaries 16–17%, visible text 2–6%.

Tests

Three new unit tests (tool_start_drops_the_bodies_only_write_and_edit_carry, tool_start_filters_a_stringified_argument_object, usage_keeps_only_the_counter_a_client_reads) covering: path survives, identity/phase survive, the file_path alias, the stringified shape staying a string, shell/read/unknown left byte-identical, a body-less payload returned borrowed, and the usage alias.

Falsified: with both trims short-circuited to Cow::Borrowed, exactly the three new tests fail and the other twelve pass.

Desktop backend: 1324 passed / 0 failed; cargo fmt --check and cargo clippy --all-targets -D warnings clean. The lean render fixtures (mobile/shot/mock/fixtures/render-lean-*) regenerate to the committed bytes — they were authored to the post-trim shape, so the fixed point holds and no fixture changed.

Not done here (measured, needs a decision)

  • command — 14.4% of the lane. It is the shell row's label, and a live row cannot be fetched back today (toolFromPayload never sets toolCallId, and ToolRow requires it plus runId to be fetchable). Making it fetchable is small, but until a row is tapped it would show no command at all during the run. Fetching would work if we do it: the assistant entry (with the tool-call arguments) is persisted before execute_tools runs.
  • Thinking boundaries — 16–17%, but their payload is 48 B each ({"type":"thinking_start","block_id":"reasoning"}); the cost is the per-event envelope, not the marker. Dropping them would remove the thinking rows.
  • The envelope itself is now ~56% of the lane: eventId is always the empty string on every event (27 KB/run, 3.2%) and is read by no client, and sessionId/runId/timestamp repeat per event. That is an envelope-shape change, so it needs its own decision.

…e lane

The lean feed dropped the two content streams but forwarded every other event
verbatim, so a `write` still shipped the whole file it was about to write and
an `edit` the text it replaced -- on the live lane, exactly as the history page
had already stopped doing. A usage event still carried the provider's full
report where the client reads one number out of it.

`tool_start` now keeps only the argument keys a tool row's label is built from
(`path`/`file_path`/`filePath`) for `write` and `edit`; `shell` keeps its
`command` (it *is* the label) and `read` its few bytes, and a tool this build
does not know keeps everything, because nothing here can tell which of its
arguments a future client reads. `usage` keeps `completion_tokens` -- the one
number the phone sums into a settled reply's footer, with `agent_end` as its
documented fallback -- and drops the counters and credit cost that only the
desktop footer shows (the desktop reads the agent, not this lane).

Measured on the three heaviest completed runs, through the shipping trims:

| run | coalesced lane before | after | saved |
|---|---:|---:|---:|
| 1 | 764,398 B | 607,798 B | -20.5% |
| 2 | 767,459 B | 586,864 B | -23.5% |
| 3 | 441,876 B | 369,437 B | -16.4% |

What the lane carries now is per-tool-call metadata (46-53%), outcomes (15-18%),
usage (12-14%), thinking boundaries (16-17%) and visible text (2-6%).
@huichen
huichen enabled auto-merge (squash) September 26, 2026 07:48
@huichen
huichen merged commit 8119fe5 into main Sep 26, 2026
12 checks passed
@huichen
huichen deleted the perf/lean-live-bodies branch September 26, 2026 07:56
huichen added a commit that referenced this pull request Sep 26, 2026
The live lane still streamed every shell call's command, the largest single
thing it had left (~18%). The history page dropped it long ago and posts the
client to `get_tool_call_args` when a row is opened; this does the same on the
live lane, which is what the row's identity is now for.

Client: a live tool activity carries `toolCallId` and the run it streamed under,
the same pair the persisted projection attaches. Without them the row would be
permanently blank once its target is gone -- `ToolRow` only offers the fetch
when it has both.

Trim: `shell` keeps nothing but its identity on `tool_start`. `read` keeps its
path (the row's own label) and `write`/`edit` the path alone, as before; a tool
this build does not know still keeps everything.

The price, measured rather than assumed: the client exempts a bare
`grep`/`diff`/`test` exiting 1 from being shown as a failure, and its predicate
reads the command. The agent sends the same verdict as `is_soft_fail` on the
outcome (both predicates agree, and the field has been on the wire since
2026-08-19), so the exemption normally needs nothing else. A row from an agent
predating that field now reads as failed instead. Over 20,332 real shell results
every `exit_code: 1` without the flag was a genuine failure -- a piped or `&&`
chain, or a program outside the soft-fail set -- so the shape this costs is one
the running agent does not produce. The render checks assert the price
explicitly, and the full-feed control shows the other reading side by side.

Measured through `scripts/measure/measure-live-lane.py` on the three heaviest
completed runs:

| | events | undeclared | coalesced | lean before | lean now | this PR |
|---|---:|---:|---:|---:|---:|---:|
| 1 | 174,791 | 77.21 MB | 5.33 MB | 379,915 B | 188,061 B | -50.5% |
| 2 | 166,685 | 73.19 MB | 5.07 MB | 390,848 B | 214,189 B | -45.2% |
| 3 | 154,907 | 66.96 MB | 4.92 MB | 269,414 B | 132,513 B | -50.8% |

The lean lane no longer carries a shell command at all; `tool_start` is still
its largest remaining type (~30%), but that is the envelope plus each call's
identity. Cumulative from the untrimmed lane: -99.75%.

Also refreshes CONNECTION.md's ladder, which #870 and #873 had left two
generations stale (it still quoted 0.82/0.82/0.47 MB), and records that the
measurement now builds each event's real `eventId`.
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