Skip to content

fix(lynx): two unbounded main-to-background replies - #233

Merged
Huxpro merged 8 commits into
new-lynxfrom
fix/lynx-first-screen-wire-cliff
Aug 29, 2026
Merged

Huxpro merged 8 commits into
new-lynxfrom
fix/lynx-first-screen-wire-cliff

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 28, 2026 •

Copy link
Copy Markdown
Owner

Summary

Two unbounded main-to-background replies, found by the same investigation and
fixed here. Both are the main thread answering a question with the whole page
when a constant would do, and both are invisible to every count the benchmark
harness reports.

  1. At 30k rows the "wire-free" first screen ships 42 MB to the background #231 — the main-ready reply carried the entire first-tree description to
    communicate one bit. A 30,000-row page shipped ~37 MB to say "a first screen
    was painted here."
  2. The create residual: attribute it, tax-test it, then extend compiled programs to steady-state mounts (E-train) #230 — succeeding at the compact acknowledgement disqualified the next
    one, so every create after the first answered with one handle per host:
    70,000 handles, 17.4 MB, for the benchmark's second Create 10,000 rows.

They are independent defects in the same direction (MTS→BTS), and they are the
only two anomalies in that direction across the whole benchmark coordinate
system.

Why

#231: a boolean shipped as a tree

The background needs one bit out of the main-ready reply: a first screen was
painted here, so the first background batch must preserve its IDs. transport.ts
computed exactly that — message.firstTree !== undefined — and nothing in
packages/lynx/src ever read a node out of the snapshot it tested. Below this
change the only way to state the fact was to attach the whole description, so a
correlated reply was O(painted tree), and protocol.ts then validated it node by
node on receipt.

#231 reported this as a 30k scale cliff introduced by the fork. Its own run file
refutes both halves, and the evidence is on the issue:

  • Upstream v0.1.47 ships 36,960,349 B on the same workload in 2 of 5 reps. The
    issue's upstream = 1,688 B column is the median of a bimodal sample.
  • Per rep it is 1.7 KB or 37 MB on the same build at the same scale, so it is a
    race, not a threshold. wireToMtsBytes moves with it positionally and without
    exception: every rep that received the correlated main-ready-request while
    the main thread still held the first-tree journal shipped ~37 MB back.

Row count matters only because the ~8 s paint at 30k holds that window open. The
unbounded reply was always there.

#230: compact-v1 worked exactly once per root

main-thread.ts picks the acknowledgement encoding from a host count that
preparation records. Preparation records that count only while it is itself
driving the compact path — and taking that path installs a
LynxDenseHostRecordStore as state.records (host-driver.ts:6955), which
incrementalCompactCandidate rejects because it asks for a Map
(host-driver.ts:6417). So the second identical run on the same root arrives
with no prepared count, and the non-provisional branch read that absence as "not
compact."

Read out of a browser trace, single variable:

Create #1  prep: … recordsMap=TRUE  incrementalCandidate=true  compactHostCount=70000
           ack:  ack=compact-v1 preparedCount=70000    handleDelta=70000 → COMPACT
Create #2  prep: … recordsMap=FALSE incrementalCandidate=false compactHostCount=undefined
           ack:  ack=compact-v1 preparedCount=undefined handleDelta=70000 → LEGACY

That accounts for every shape of the measurement on #230: only create is
anomalous, because only create is a single large mount-template-run arriving
on the non-provisional path; it is deterministic 7/7, because the store leaves
Map on sample 1 and never returns; and 70,000 handles for 10,000 rows at 7
hosts each is the 17,420,700 B the issue reports.

The count is a property of the batch, so the fix recomputes it rather than
letting the container the driver happens to be holding records in decide a wire
encoding.

Changes

#231

  • packages/lynx/src/core/protocol.ts: new
    LYNX_FIRST_TREE_PRESENCE_READY_REQUEST_BASE rung (2 ** 45) and
    firstTreePainted?: 1 on LynxMainReadyReply, rung-gated and mutually
    exclusive with firstTree in assertReady.
  • packages/lynx/src/main-thread.ts: dispatchReady picks the encoding from the
    peer's rung; every downstream branch now turns on whether the reply announces a
    first tree rather than on which encoding carried it.
  • packages/lynx/src/core/transport.ts: the background announces at the new rung
    and reads either encoding.
  • packages/lynx/tests/first-screen-ready-wire.test.ts (new): the wire-byte
    budget.

firstTreePainted: 1 is the same shape every other optional reply key already
uses, and for the same reason: a peer below the rung validates against an exact
key set and would reject a key it has never heard of.

The description route is kept, not approximated away. A peer below the rung
still receives the snapshot, because presence is the key for it — sending the
bit alone would tell it no first screen happened at all, and it would rebuild the
page over the command path with fresh IDs. Both skew directions are therefore
safe: an old main thread sends firstTree to a new background, which still reads
it; a new main thread sends firstTree to an old background, which never sees
the new key.

The main thread's own cost goes with it. firstTree.snapshot is built on first
read (#148), and the presence path never reads it, so a first screen that hands
off this way never allocates a description of itself either.

#230

  • packages/lynx/src/main-thread.ts: the non-provisional branch of the
    acknowledgement gate gets the same recompute fallback the provisional branch
    already had.
  • packages/lynx/tests/block-first-screen.test.ts: a regression that drives two
    compact-eligible runs on one root, separated by a clear, and reads main's own
    replies off the wire — the second must still negotiate compact-v1 and carry
    no handles.

The safety net immediately below the gate is unchanged and does the work that
makes this sound: a recomputed count that disagrees with what preparation
actually produced is re-validated against prepared.handleDelta — the lengths
must match and every delta must be a generation-1 create — and falls back to
legacy if it does not.

No check was deleted. In particular this does not remove the
state.records instanceof Map test in host-driver.ts, which guards a real cast
at :6951 where the prefix for the next dense store is copied. Why a record
store's representation gets to decide a wire encoding at all is a real question,
and it is deliberately left for its own issue rather than smuggled in here.

Benchmarks

  • benchmarks/lynx-table/{workload.ts,first-screen-selectors.mjs}: the
    first-screen selector suite now measures main-to-background wire bytes against
    a 4,096 B budget.

    That budget does not guard At 30k rows the "wire-free" first screen ships 42 MB to the background #231's description, and the code now says so in
    place.
    Both of its arms render from the background, so main never captures a
    first tree and has no description to send: measured on the fixed build it reads
    468 B at 1,000 rows, 470 B at 30,000, and the same 470 B with the background
    dropped below the presence rung. What it does bound is the rest of the
    handshake — that answering a request and acknowledging a commit stays flat
    however large the page gets. The description's own budget lives in
    packages/lynx/tests/first-screen-ready-wire.test.ts, where a main-thread paint
    exists to produce one. An earlier revision of this body described the bench
    budget as if it caught At 30k rows the "wire-free" first screen ships 42 MB to the background #231; 73882702a corrects the claim rather than the
    check.

Measured effect

create, one window, one machine, three entries including upstream v0.1.47,
--reps 7, quiet box (#39 protocol). Before/after are the same tree built
minutes apart.

Wire, MTS→BTS — the direction both defects live in:

upstream before after
create@1,000 725 B 1,721,694 B (2,375×) 719 B (0.992×)
create@10,000 732 B 17,420,700 B (23,799×) 727 B (0.993×)
startup@30,000 — 36,960,444 B 470 B (at 120,029 selectable nodes)

The command stream (BTS→MTS) is untouched: 350,885 B against upstream's
350,774 B, 1.000×.

Latency and CPU (ms, median ±CI95, n=7):

metric scale upstream before after
latency @1,000 144 ±15 211 ±17 (1.461×) 164 ±10 (1.134×)
latency @10,000 1,540 ±97 2,470 ±90 (1.604×) 2,268 ±164 (1.473×)
mtsCpu @10,000 1,391 ±88 2,162 ±67 (1.554×) 1,905 ±79 (1.370×)
btsCpu @10,000 163 ±22 963 ±107 (5.927×) 536 ±203 (3.296×)

So the acknowledgement was most of the residual at 1,000 rows (71% of it) and a
real but minority slice at 10,000 (22%). It does not close #230, and the
remaining ~728 ms is reported there rather than claimed here.

Validation

  • pnpm format:check — via pnpm format:files on every changed path

  • pnpm typecheck — clean

  • pnpm test — 2,443 of 2,448 files, 26,718 of 26,725 tests pass, on a
    quiet box. Five files fail and none are this change's:

    file shape
    packages/drei/tests/browser/upstream-e2e.browser.test.ts carried base-red, same test
    packages/octane/tests/browser/suspense-hydration/suspense-hydration.test.ts carried base-red, same test
    packages/rspeedy-plugin-octane/tests/native-demo.test.ts carried base-red, same test
    packages/octane/tests/react-hosted/web3-island.test.ts beforeAll hook timed out at 10,000 ms
    packages/octane/tests/hydration/deferred-hydration.test.ts a vi.waitFor never converged

    The last two are deadline failures under full-suite load, not assertions about
    behavior: both pass in isolation on this head, and the diff touches nothing
    outside packages/lynx, benchmarks/lynx-table, and two changesets.

    A first full-suite run on this same head is deliberately not the one
    reported here. It overlapped the benchmark above — four Vitest workers and
    three Chromium instances on one box — and produced six extra deadline failures
    of the same shape. It is discarded, not averaged in.

  • targeted tests: whole packages/lynx project green (45 files, 686 tests).
    Both new regressions red-verified by reverting only the product change:
    first-screen-ready-wire.test.ts by breaking announcesByPresence (2 of 3
    cases fail), the The create residual: attribute it, tax-test it, then extend compiled programs to steady-state mounts (E-train) #230 case by restoring the old gate expression (it then
    reports the legacy encoding and a populated handles).

  • node first-screen-selectors.mjs at rows=1,000 and rows=30,000, both
    handshake arms: 468 B and 470 B against the 4,096 B budget.

The #231 unit budget asserts the reply is byte-identical at 50 and 400 rows and
under 512 B, and proves it can fail by driving a peer below the rung and watching
the same reply grow past 4×; a companion case asserts that peer still receives
all 401 nodes. first-screen.test.ts's end-to-end adoption scenario now runs
through the presence encoding and still asserts preserved node identity and
replayed events — that scenario is the evidence the handoff is unchanged, and the
assertion it used to make about the description now says so in place.

Risk / follow-ups

Provenance

  • An agent produced this diff (agent-authored)

The background reads one bit out of `main-ready`: a first screen was
painted here, so the first background batch must preserve its IDs.
`transport.ts` computed exactly that — `message.firstTree !== undefined`
— and nothing in the package ever read a node out of the snapshot it
tested. Below this change the only way to state the fact was to attach
the whole first-tree description, so a correlated reply was O(painted
tree): a 30,000-row page shipped a ~37 MB structured clone that
`protocol.ts` then validated node by node on receipt, to carry a
boolean.

The reply now carries `firstTreePainted: 1`, published only to a
background whose readiness request was tagged at the new
`LYNX_FIRST_TREE_PRESENCE_READY_REQUEST_BASE`. That is the shape every
other optional reply key already uses, and for the same reason: a peer
below the rung validates against an exact key set and would reject a
key it has never heard of.

The description route is kept rather than approximated away. A peer
below the rung is still sent the snapshot, because presence *is* the
key for it — the bit alone would tell it no first screen happened at
all, and it would rebuild the page over the command path with fresh
IDs. Both skew directions are therefore safe. The two spellings are
mutually exclusive on the wire, so a receiver never has to decide which
one wins.

The main thread's own cost goes with it: the description is built on
first read, and the presence path never reads it, so a first screen
that hands off this way never allocates a description of itself either.

Regressions: a unit budget asserting the reply is byte-identical at 50
and 400 rows and under 512 B, a companion case driving a peer below the
rung and asserting it still receives all 401 nodes, and a 4,096 B
main-to-background budget in the lynx-table first-screen selector
suite, which until now reported only counts and so could not see this.

Refs #231

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
Huxpro and others added 2 commits August 28, 2026 10:49
…t catch

The budget added with the #231 fix claimed its job was "to catch a payload
that scales with the tree". It cannot: both arms of this harness render from
the background, so the main thread applies commands and never captures a
first tree, and there is no description on the wire for the budget to bound.
Measured on the fixed build it reads 468 B at 1,000 rows, 470 B at 30,000,
and the same 470 B with the background dropped below the presence rung —
identical because neither arm has a description to send either way.

The check still bounds something real, so it stays: the rest of the handshake
must not become a function of the page by any route. The comment now says
that is its scope, and names where the description's own budget lives —
first-screen-ready-wire.test.ts, which drives a real main-thread paint, pins
the reply byte-identical across an 8x tree and under 512 B at the presence
rung, and proves that budget can fail by dropping a peer below it.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
… the first

Issue #230 measured 17.4 MB of MTS->BTS acknowledgement on the benchmark's
`create 10000`, deterministic across 7 samples, where upstream spends 732 B.
Reading the decision out of a real browser run showed the first create is
compact and every later one is not, with one input differing: preparation
records no host count from the second run onwards.

Taking the compact path installs the driver's dense record store, and the
incremental-compact candidate test asks for `state.records instanceof Map`.
Against the dense store it is false, so `compactHostCount` comes back
undefined -- and the main thread's acknowledgement gate had a recompute
fallback only on its provisional branch. The non-provisional branch read the
missing count as "not compact" and replied with one handle per host: 70,000
of them for 10,000 rows. Succeeding at compact was what disqualified the
next one.

The count is a property of the batch, so recompute it on both branches rather
than let the container the driver happens to be holding records in decide a
wire encoding. The check immediately below still re-validates a count that
disagrees with preparation against the prepared handle deltas -- length must
match and every delta must be a generation-1 create -- so a recomputed count
that does not describe what was prepared still falls back to legacy.

Measured on the bench app built from this tree, same machine, one window,
seven samples per arm: create@10000 median 17,420,754 B before and 782 B
after; `heapMtsAfterClear` 17,850,904 B before and 17,863,780 B after, so
this does not touch the retention tracked in #231.

The regression drives the real main thread behind a painted first screen,
fills a range, clears it, and fills it again, asserting the second run is
still acknowledged with `compact-v1`. It fails on the old gate.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
@Huxpro Huxpro changed the title fix(lynx): state the painted first screen as a fact, not a description fix(lynx): two unbounded main-to-background replies Aug 28, 2026
@Huxpro
Huxpro marked this pull request as ready for review August 28, 2026 13:37
@Huxpro
Huxpro marked this pull request as draft August 28, 2026 13:41
claude and others added 5 commits August 29, 2026 00:16
Reconciles the upstream v0.1.47 sync stack with the merged C8-D8 train:
union .prettierignore, regenerate-later audit stamps, and a semantic merge
of collectFirstScreenEvents keeping the train's merged-order program
announcement while threading the sync's component-scope/native-list flags
and template-range branch through the node helper.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YHfFsy9CLaRovTFH8pTGyY
The merged pnpm-lock.yaml hashes differently from either parent, so every
react-parity manifest and the user-app corpus carry regenerated
lockfileSha256/overlayLockfileHash stamps from their own generators.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YHfFsy9CLaRovTFH8pTGyY
Three defects and two cleanups from the gate review of the merged tree:

- expandRecordsRunTeardown collected its removes and destroys with one
  spread call, so a run past the engine's ~125k-argument ceiling died in
  RangeError at batch-prepare — exactly the run sizes the explicit-records
  fallback exists for. Appended in loops; red-verified at 17,000 rows of
  width 7.
- expandRunTeardown tested the dense store's removed set with an absolute
  host id where every other reader uses store-relative offsets, silently
  kicking intact runs off the certified expansion whenever an aliasing
  offset was retired. delete() also blanks nodes[offset], so the wrong
  unit could only decline, never wrongly certify.
- Dropped the identical-armed slot ternary and the unreachable __proto__
  assignment branch in main-renderer; forbiddenProp declines __proto__
  before either loop can reach an assignment.

A reviewed hidden-program template-selection divergence was verified
unreachable: a first screen holding a compiled program never builds a
command batch (the programs guard throws first), and program nodes are
the only visibility carriers the template walk does not narrow.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YHfFsy9CLaRovTFH8pTGyY
…test wire

Two corrections to the merge resolution, caught by the full run:

- The react-parity audit records were resolved to the train side, but the
  sync regenerated them against upstream v0.1.47 — the tree the merge
  actually contains — so the parity contract tests read stale execution
  records (tsconfig lanes pointing at pre-sync paths). Restored the sync
  side for every conflicted audit and restamped lockfileSha256 against
  the merged lockfile.

- The new first-screen wire and compact-ack tests were written against
  the pre-codec fork base and put raw objects on the ContextProxy; since
  the transport owns encoding (#164), such a message never decodes and
  the reply never fires. They now wire/unwire through the shared fixture,
  and the reply-cost budgets measure the encoded string that actually
  crosses instead of a re-print.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YHfFsy9CLaRovTFH8pTGyY
@Huxpro
Huxpro changed the base branch from chore/sync-upstream-v0-1-47 to new-lynx August 29, 2026 03:06
@Huxpro
Huxpro marked this pull request as ready for review August 29, 2026 03:06
@Huxpro
Huxpro merged commit 8938c12 into new-lynx Aug 29, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants