Skip to content

perf(remote): send a phone only the event content it renders - #844

Merged
huichen merged 2 commits into
mainfrom
perf/lean-phone-payload
Sep 25, 2026
Merged

huichen merged 2 commits into
mainfrom
perf/lean-phone-payload

Conversation

@huichen

@huichen huichen commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Problem

A run's live lane costs 67–77 MB raw / 4.2–4.6 MB after coalescing, and almost all of it is content no phone surface shows:

  • thinking_delta — 57–84% of the raw bytes, a per-token stream. The phone renders a collapsed "思考/思考中" row, never the text.
  • tool_delta — 4–41%. It re-sends the accumulated argument snapshot per fragment; the row's target is already derivable from tool_start's complete tool_args.
  • tool_end — carries the tool's captured output, of which the phone reads only the exit code.

Change

A client that declares lean_events_v1 receives the same event lane with that content removed.

Event Full lane Lean lane
thinking_delta, tool_delta, toolcall_delta per-token content not published
tool_end / tool_result + captured output output dropped; exit_code, is_soft_fail, target_path, error kept
run_snapshot + folded snapshotEvents array dropped (the client only uses this as a resync signal)

Every other event is forwarded byte-for-byte, so a future event type cannot be silently reshaped. The per-token types are dropped on a slice compare before the queue, so the hot path never parses them; only the per-call types are parsed at all.

Measured effect

Through the shipping code over the three heaviest real runs — scripts/measure-live-lane.py now runs both lanes and prints the comparison:

full coalesced lean coalesced saved messages
top1 4.60 MB 0.76 MB 83.4% 8,713 → 1,335
top2 4.37 MB 0.77 MB 82.4% 8,472 → 1,442
top3 4.20 MB 0.44 MB 89.5% 8,689 → 925

Message count matters as much as bytes: a phone pays an AEAD open, a JSON parse and a radio wake per message, not per byte.

Why this is a removal, not a fidelity trade

Three properties do the work: thinking_start/thinking_end alone open and close a reasoning row; a tool's target already rides tool_start's complete tool_args; and a tool's outcome is on tool_end as structured fields.

The last one is why the client now reads exit_code instead of parsing an [exit: N] footer out of the output — and therefore why the feed is opt-in: a client that did not declare it still does all three from the text. The flag is cleared on every new connection rather than latched, so an older build on the same pairing cannot inherit the previous client's declaration.

Replay is trimmed too

Leaving get_events_since and the snapshot bootstrap alone would pull the dropped content straight back on the next reconcile, so both are rewritten as well. One ordering constraint is load-bearing and documented at the call site: a page's cursor fields are computed before the rewrite. Dropping an event must not move nextSinceIdx/watermark, or a client resuming from that cursor would re-fetch a range whose events are always dropped — an endless loop.

The folded projection that rides a replay page is treated differently on purpose: both the desktop and the client validate its events for length and ordering (and the desktop rejects an empty list), so its events keep their idx and only the text is blanked.

Verification

  • Desktop Rust: 1306 pass; cargo fmt --check + cargo clippy --all-targets -D warnings clean (pinned 1.97).
  • Mobile: 128 suites / 2047 pass (one pre-existing failure, below); tsc --noEmit + eslint clean.
  • Desktop TS (consumes the shared projection package): 145 files / 1255 tests pass; tsc + eslint clean. Confirmed the tests resolve @future-os/thread-projection to this worktree, not the main tree.
  • New tests: 7 unit tests for the rewrite rules, 2 integration tests over the real publisher lane (one asserting the undeclared lane is untouched), 5 mobile projection tests, and a new ignored measurement test.
  • All three behavioural assertions were falsified: short-circuiting the publisher's trim makes thinking_delta the first event on the wire; removing the structured exit_code read fails two tests; restoring the empty-text filter drops the reasoning row.

Known, pre-existing, not from this diff

  • useSkillRecommendation → "skips a skill already shown today" fails identically on clean main. The test seeds a UTC date (toISOString().slice(0,10)) while the hook uses the local day, so it only fails between 00:00 and 08:00 local. Worth a one-line fix in its own PR.
  • prune_preview_cache_removes_expired_files is flaky under full-suite load (passes in isolation; 3 full runs gave fail/pass/pass). preview_cache_dir() is one process-wide directory and a sibling test calls clear_preview_cache(), which remove_dir_alls it. A test-isolation bug, not a behaviour bug.
  • instance::tests::excludes_second_owner_and_releases_on_drop failed once in a full run and passes everywhere else.

Deliberately out of scope

Trimming history entries (get_session_entries: reasoning text, tool-result text, tool arguments → the four keys the client reads). That path needs a per-client field rewrite and touches entry.role, which the mobile projector and the TUI read directly, so it deserves its own PR.

A run's live lane costs 67-77 MB raw / 4.2-4.6 MB coalesced, and almost all
of it is content no phone surface shows: `thinking_delta` and `tool_delta`
are per-token streams, and `tool_end` carries the tool's captured output
that only its exit code is read from.

A client that declares `lean_events_v1` now receives the same events with
that content removed. Measured through the shipping code over the three
heaviest real runs (`scripts/measure-live-lane.py`):

  full coalesced   lean coalesced   saved    msgs
       4.60 MB         0.76 MB     83.4%    8,713 -> 1,335
       4.37 MB         0.77 MB     82.4%    8,472 -> 1,442
       4.20 MB         0.44 MB     89.5%    8,689 ->   925

- `thinking_delta`, `tool_delta`, `toolcall_delta` are not published. The
  per-token types are dropped on a slice compare, before the queue, so the
  hot path never parses them.
- `tool_end` / `tool_result` keep `exit_code`, `is_soft_fail`,
  `target_path` and `error`; only the captured output goes.
- `run_snapshot` drops its folded `snapshotEvents`, which the client never
  reads (it treats the event as a resync signal).

Every other event is forwarded byte-for-byte, so a future event type cannot
be silently reshaped.

Three properties make this a removal rather than a fidelity trade:
`thinking_start`/`thinking_end` alone open and close a reasoning row, a tool
target already rides `tool_start`'s complete `tool_args`, and a tool outcome
is on `tool_end` as structured fields. The client therefore reads `exit_code`
instead of parsing an `[exit: N]` footer out of the output — so the feed is
opt-in, and the flag is cleared on every new connection rather than latched,
because a client that did not declare it still does all three from the text.

The durable replay path is rewritten to match (`get_events_since` and the
snapshot bootstrap), or a reconnect would pull the dropped content back. A
page's cursor fields are computed *before* the rewrite: dropping an event
must not move `nextSinceIdx`, or a client resuming from it would re-fetch a
range whose events are always dropped. The folded projection riding a replay
page keeps its events and their `idx` — both the desktop and the client
reject an empty or reordered list — so only its text is blanked.
@huichen
huichen enabled auto-merge (squash) September 25, 2026 16:32
@huichen
huichen merged commit c173e64 into main Sep 25, 2026
12 checks passed
@huichen
huichen deleted the perf/lean-phone-payload branch September 25, 2026 16:40
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