Skip to content

feat(lynx): adopt a painted native list into the background root - #112

Merged
Huxpro merged 4 commits into
new-lynxfrom
feat/lynx-adopt-native-list
Aug 21, 2026
Merged

Huxpro merged 4 commits into
new-lynxfrom
feat/lynx-adopt-native-list

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 20, 2026

Copy link
Copy Markdown
Owner

Summary

Stacked on #111. Issue #66 Phase C, C2.

Adoption refused every tree holding a native <list>, so a feed — the app shape
a synchronous first screen exists for — was the one shape that never got one. It
is adoptable now: a <list> page paints on the main thread and the background
root adopts that paint, including the list.

What made it look impossible was a measurement nobody had taken. At first
screen a native list has no cells at all.
The platform materializes a row
through componentAtIndex when it needs one, so a 1,000-row feed paints exactly
one node — the <list> itself — and every row is a live record with node === null. I confirmed this with a probe before designing anything: physicalCells: 0, only the <list> host attached.

That collapses the problem twice.

  • What is painted is one node, so transfer moves one node. The rows have no
    physical identity to hand over, because they have no physical identity yet.
  • The state that cannot be serialized never crosses the wire.
    LynxNativeListState — cells by native sign, recycle pools, retained items,
    counters, and the three recycling callbacks — is main-local. The background
    only ever tests message.firstTree !== undefined. So adoption moves the
    state by reference
    instead of describing it.

Only the callbacks cannot come along: they close over the container adoption is
about to empty, and would materialize cells into a retired source. They are
rebound against the target through bindNativeListCallbacks (extracted in #111
for exactly this) and handed over with listPAPI.updateCallbacks — the same seam
applyListUpdate and disposeNativeListState already use.

No wire format changes

My first design said format: 2, carrying the rows in the snapshot. That was
wrong twice, and I corrected it publicly on the issue before implementing:

  • A background peer that validates every inbound message would reject the whole
    main-ready reply and never boot. validateLynxBackgroundInboundMessage
    runs unconditionally on receive, and assertFirstTreeSnapshot fails a
    format !== 1.
  • It is also unnecessary. No peer reads a node out of the snapshot.

So the snapshot keeps format: 1 and its contract of one painted physical node
each — which is what nativeId means and what the validator enforces — and the
unpainted rows live in the main-local journal beside it, along with one entry per
list.

What adoption has to prove for a list

  1. The painted nodes agree — unchanged.
  2. The list's items agree. The main thread already wrote update-list-info onto
    the node being adopted; listUpdates is what the background would have
    written onto a node it created itself.
  3. The platform has not materialized a cell since capture. That is a recycling
    epoch — enterCount + leaveCount + createdCells + reusedCells — read at
    capture and re-read at adoption. A list that woke up in between holds physical
    state the captured tree does not describe, so the page repairs.

A list with no rows at all is an ordinary page, not a disagreement: its
commit carries no list update, because there is nothing to insert.

Capture now declines only a list that had already materialized a cell.

The Phase C opener's pre-check is deleted here

firstScreenTreeIsUnadoptable (#110) answered "would capture refuse this tree?"
before painting. Capture refuses far less now, and the pre-check cannot predict
what it still refuses — cell materialization is a runtime event, not a property
of the record tree. Left in place it would decline exactly the pages that gained
a first screen. Its changeset is amended to say so; its measurement stands as the
record of what build-and-discard cost.

Measurement

Real installLynxMainThread first-screen path, against the lowering compiled
.tsrx produces: a template-created <view> shell wrapping <list> whose rows
arrive through a keyed @for, each row a <list-item> with three children. The
background thread is stood in for by a second render whose batch is dispatched
over the real ContextProxy as a commit. Arms by checking out the three changed
source files; 21 samples per pass after 3 warmups, each in a fresh environment;
Element PAPI traffic counted by wrapping every function-valued __* global and
asserted identical across a pass.

Every head sample was asserted to have actually adopted (ack.adoption === 'adopted', no diagnostics), so these are adoption timings, not repairs.

page arm first paint total main-thread PAPI
1,000 rows declines (base) 70.4 ms 70.4 ms 12
1,000 rows paints and adopts 42.8 ms 117.1 ms 21
50 rows declines (base) 5.6 ms 5.6 ms 12
50 rows paints and adopts 2.9 ms 7.1 ms 21

Medians of three passes each at 1,000 rows (base 70.4 / 70.2 / 71.4, head 42.8 /
41.5 / 43.7), two at 50.

I expected the compare walk to dominate the extra total work, and it does
not.
Timed directly at 1,000 rows:

phase median
synchronous paint 43.2 ms
commit — shared prepareLynxHostBatch walk 59.0 ms
commit — compareFirstTree 11.1 ms
commit — transferFirstTree 0.8 ms

The commit costs about the same in both arms (60.2 base, 70.9 head) because the
record walk runs either way — adoption skips the mutation, not the preparation.
Transfer is 0.8 ms because two nodes are physical. So the trade is: the page
appears after the paint instead of after the background's commit, and the main
thread pays the paint plus a 12 ms agreement check to make that happen.

Adoption's host traffic is constant, not per-row: 21 calls against 12 at both
50 and 1,000 rows, because an unscrolled list materializes no cells at any size.

Both figures understate the case. The harness dispatches the background commit
immediately, charging the base arm 3.6 ms (1,000 rows) or 0.2 ms (50) for "the
background produced its first commit", where a real page pays a bundle load and a
cold render on another thread — which the painting arm does not wait for at all.

The harness was temporary and is not in this diff; the numbers are in
docs/lynx-specialized-target-l0.md §8.

Red/green, including two breaks that came back green

Each break run against the full 24-test file:

deliberate break result
transfer keeps the source-bound callbacks 1 failed, 23 passed
drop the epoch check 1 failed, 23 passed
drop the sameListItems check 24 passed
install loop does not skip logical rows 1 failed, 23 passed
capture treats list rows as ordinary hosts 6 failed, 18 passed
transfer does not skip logical rows 1 failed, 23 passed
ownership assertion ignores logical rows 24 passed
restored 24 passed

Both green rows are declared rather than quietly kept, and neither is fixable by
adding a test:

The list-items check cannot fail alone. Every field of a
LynxListItemDescriptor comes from a row's id, its position among the list's
children, and four props — and the node walk already compares ids, child order,
and props, more strictly: an absent reuse-identifier and an empty one are one
descriptor but two prop sets. A row set that differs at all differs there first.
The check states the update-list-info agreement where adoption relies on it, and
becomes load-bearing the moment list metadata stops being a plain prop or row
props stop being compared one-for-one. A comment in the source says this. Note the
contract is still guarded — repairs when the background list holds different rows passes through the props comparison.

The ownership equality is unreachable from any test, and that is not new. I
checked instead of assuming: disabling the assertion entirely on the parent
commit
, in its original un-relaxed form, leaves all 434 lynx tests passing. It
is an internal invariant that only a driver bug can violate, and fabricating one
would mean writing to LynxHostState internals, which .rulesync/rules/testing.md
rules out. The design requirement it had to meet — that relaxing it must not
weaken it for non-list trees — is met by construction: it stays an exact count of
the logical rows the capture walk itself emitted
(ownedNodes.size !== records.size - logicalNodes.size), not an inequality.

A bug found by reasoning, then by a test

listUpdates.length !== journal.lists.size looked like the obvious agreement
check and is wrong: a list with no rows produces no prepared update at all. I
predicted it while writing the empty-feed case, wrote
adopts a native list that has no rows at all, watched it fail, and removed the
count equality. Absence is agreement here, not disagreement.

Tests

21 → 24 in packages/lynx/tests/first-screen.test.ts. Six decline tests are
replaced by tests of the new behavior:

  • paints a first screen holding a native list, with its rows still logical
  • adopts a painted native list, and the adopted list still materializes rows
  • adopts a native list that has no rows at all
  • repairs when the background list holds different rows
  • repairs when the platform materialized a row between capture and adoption
  • paints a native list whose rows come from a keyed loop
  • paints a native list the compiler lowered to a template

The two duplicate-item-key tests are kept verbatim: a malformed list must still
throw its diagnostic from the staged path rather than settling as a quiet skip.

One rewritten assertion is a stronger contract than the one it replaced. A
painted list now gates background startup until markFirstScreenSyncReady(),
where the declining version announced readiness immediately, so the test proves
the gating rather than the old immediate main-ready.

Validation

  • pnpm format:check — clean repo-wide
  • pnpm typecheck — clean (exit 0)
  • pnpm test — not run to completion in this container: the full run
    exceeded a 60-minute cap on the branch below. The reachable projects were
    run explicitly instead.
  • targeted tests: --project lynx — 32 files, 437 passed (parent: 434)
  • targeted tests: packages/lynx/tests/first-screen.test.ts — 24 passed
  • node benchmarks/bench.mjs --quick --ratios lynx-list — PASS, 0.012x
    eager (12 physical cells for 1,000 logical items, 988 reuses, 0 after
    teardown). This is the exit gate Execution plan (#58): complete L2, then drive L4→L6 — full context handoff #66 §5 names for Phase C. It exercises the
    list runtime, including the callback binding this change rebinds, but not
    the adoption path itself.

Per #66 §2.3 the rspeedy-plugin native-demo and packed-consumer failures
remain pre-existing in this container and were not chased.

Risk / follow-ups

  • transferFirstTree must stay free of Element PAPI calls before the rebind
    lands.
    Nothing above or below it calls the PAPI, so no native callback can
    interleave with the move; a flush, an insertBefore, or a setAttribute
    added there would open that window. A comment in the source says so.
  • Partially materialized capture is new ground. A list that materializes a
    cell before capture is declined, and one that materializes between capture and
    adoption repairs. Neither is adopted, deliberately: the physical subtree is
    keyed by a native sign, and nothing here can establish that it survives the
    window without a device to observe.
  • The compare walk visits every logical row. At 1,000 rows that is 11.1 ms
    proving agreement over records that own nothing physical. The load-bearing
    contract for a list page is narrower — the painted nodes agree, and the list's
    items agree — because an unpainted row's descendants materialize later from the
    background's own records. Narrowing the walk there is the change that would also
    make the items check load-bearing. Not taken here; it belongs with C3.
  • C3 (extending direct first-screen emission to lists) is unblocked by this: it
    was never independent of C2, since capture refused every list tree whatever
    painted it.
  • The template-lowered fixture is hand-written against verified compiler output
    rather than compiled in-process, because the lynx vitest project compiles
    .tsrx with the background renderers. If the main-thread lowering changes
    shape, it goes stale silently. Unchanged from perf(lynx): decline an unadoptable first screen before painting it #110.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

Adoption refused every tree holding a native `<list>`, so a feed — the app
shape a synchronous first screen exists for — was the one shape that never
got one. It is adoptable now.

What made it look impossible was a measurement nobody had taken: at first
screen a native list has no cells at all. The platform materializes a row
through `componentAtIndex` when it needs one, so a 1,000-row feed paints
exactly one node, the `<list>` itself, and every row is a live record with no
element behind it.

That collapses the problem twice. What is painted is one node, so transfer
moves one node. And the state that cannot be serialized — cells by native
sign, recycle pools, retained items, the three recycling callbacks — is
main-local and never crosses the wire, so adoption moves it by reference
rather than describing it. Only the callbacks cannot come along, because they
close over the container adoption is about to empty; they are rebound against
the target and installed through `updateCallbacks`.

No wire format changes. The snapshot keeps `format: 1` and its contract of one
painted physical node each; the unpainted rows live in the main-local journal
beside it, because widening the format would change a shape every peer
validates on receipt in order to describe nodes no peer reads.

Adoption of a list proves the painted nodes agree, the list's items agree, and
the platform has not materialized a cell since capture — the last through a
recycling epoch read at capture and re-read at adoption. Capture declines only
a list that had already materialized a cell, which is why the Phase C opener's
pre-check is removed here: it cannot predict that, and would decline exactly
the pages that gained a first screen.

Issue #66 Phase C, C2.

Huxpro commented Aug 20, 2026

Copy link
Copy Markdown
Owner Author

Adding a validation run that finished after the body was written, rather than editing the body around Bugbot's region.

--project octane --project octane-prod — 841 files, 12,435 passed, 4 skipped, 1 failed; 3 files failed. None of the three is reachable from this diff, which is confined to packages/lynx/src; the octane project does not import it. All three were run concurrently with the lynx suite and the lynx-list benchmark on this container, and all three are load-sensitive:

  • compiler/vite-integration.test.ts > discovers a parent package and routes raw dependency imports to the SSR runtime — Test timed out in 5000ms. Standalone on this same head it passes in 2.31 s, with a 7.61 s setup phase ahead of it; under load it crossed the 5 s cap. This is the first time I have seen this one.
  • react-hosted/web3-island.test.ts, once per project — Hook timed out in 10000ms building the rainbowkit hydration fixture, so its 2 tests are skipped rather than failing an assertion. Same threshold flake documented on perf(lynx): decline an unadoptable first screen before painting it #110, where three consecutive standalone runs passed.

I would rather report this than quietly re-run until it is green: the honest statement is that this container cannot run these three files reliably alongside other work, not that they are unaffected by the change — though the import graph says they are.


Generated by Claude Code

claude added 3 commits August 20, 2026 19:08
…pt-native-list

Carries the review fix "replay prepare-walk diagnostics before declining a
first screen" up the stack.

Conflict resolution: this branch retires `firstScreenTreeIsUnadoptable` and
its call site outright, because adopting a painted native list is what makes
the early decline unnecessary. The fix hardened the skip path the removal
deletes, so its hunks are dropped here rather than reapplied — but the
property it protects is kept, and is now proved by its own two regressions
running against the no-skip path: a colliding main-thread event prop and a
duplicated main-thread ref still throw from `render`, with a null snapshot.
Huxpro pushed a commit that referenced this pull request Aug 20, 2026
… paints

Before this branch a page holding a native <list> was declined by the direct
first-screen applier and rebuilt through the staged path, so prepare's final
validation raised `main-thread ref X is assigned to hosts N and M` with zero
PAPI calls spent. C3 makes such a page directly buildable, and the direct path
carried a pre-walk for only one of prepare's two non-list diagnostics: the
main-thread/background event collision. A duplicated ref therefore survived to
`installMainThreadRef`, which reports it as `is already mounted` — a weaker
message, raised mid-mount, after elements exist. That is the half-painted page
the pre-walk exists to prevent.

Extend the pre-walk to carry both diagnostics, with prepare's exact ref message
and its visibility rule. The walk is now unconditional: a background listener is
announced by the envelope, but a ref is announced by nothing but the host's own
props, and this applier is exported, so no envelope field may make the check
skippable. It is a second read-only pass over a tree the function already scans
for a <list>.

This is where the pre-check fix on new-lynx (e6b2898) lands once #112 retires
`firstScreenTreeIsUnadoptable`: the same two diagnostics, replayed from the one
place that still decides whether to paint.

Regressions: the two gate scenarios now also assert nothing was painted, and a
new one puts the duplicate on two rows of one list — rows own no element during
the first screen, so no mid-mount check can ever see it. Both ref scenarios were
verified red without the check (2 failed | 25 passed).

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
@Huxpro
Huxpro changed the base branch from refactor/lynx-list-callback-binding to new-lynx August 21, 2026 20:24
@Huxpro
Huxpro marked this pull request as ready for review August 21, 2026 20:24
@Huxpro
Huxpro merged commit 9a90bea into new-lynx Aug 21, 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