Skip to content

perf(lynx): let a commit say whether it announced its public instances - #130

Merged
Huxpro merged 3 commits into
new-lynxfrom
perf/lynx-selector-on-create
Aug 21, 2026
Merged

Huxpro merged 3 commits into
new-lynxfrom
perf/lynx-selector-on-create

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Summary

The demand rule from #129 now applies to every mount path, not only native
<list> cells — and a commit, rather than the session, carries the promise that
makes it safe.

rows element nodes mounted selector writes before after announced
1,000 4,000 4,000 0 0
10,000 40,000 40,000 0 0

The last column is why zero is correct rather than lossy: the app holds no
ref, no host lifecycle, and no main-thread callback, so the background
announces no public instance and nothing can ever query those nodes.

Why

#129 left four eager install sites and called the remaining prize unmeasured,
turning on "how much of a first screen mounts through templates." Measuring it
first changed the answer.

I added a nodes-ref install counter and a wire-regime summary to the
lynx-table chassis and ran the same 1,000 rows two ways:

  • as the first commit (rows present at mount): the whole 7,042-node tree
    cost 28 installs. The first screen was already almost free.
  • as a later commit (the "Create 1,000 rows" tap): the identical
    mount-template-run cost 4,000.

So the prize was never on the first screen. It was on every ordinary commit
after it, and it scales with the tree: 4 writes per row, 40,000 at 10k.

The session flag was not the fact the main thread needed

The obvious generalization — read the negotiated lazyPublicInstances
capability instead of the per-commit instances flag — is unsound, and it broke
the gallery example's auto-scroll: <list ref={galleryRef}> mounted with no
selector, so useEffect's invoke('autoScroll') failed with native code 2 and
the list never moved. web-host.test.ts caught it; instrumenting the Web host
gave the sequence exactly:

emit      lazy=false  refBit=true  drafts=379  withRef=1  announced=0
setCaps   lazy=true
container announces=true
bind      type=list   wanted=false announces=true   → skipped

A background composes its first batch before the main-ready reply granting
the capability reaches it, and dispatches it after. So that batch names none of
its hosts however the session was negotiated, while the main thread — reading
the reply it had already sent — believed every host would be announced. This is
the exact failure protocol.ts already warned about above
lynxLazyPublicInstancesNegotiated: "a main thread that skips the selector while
the background skips the announcement strips refs with no error anywhere."

The same compose/dispatch split is a latent defect in #129 as shipped:
deferPublicInstances reads the negotiated capability at dispatch, so a
compact template-only first batch composed before the reply could go out flagged
instances: lazy-v1 while carrying no announcements — and lose its refs the
same silent way. This PR fixes that too.

Changes

  • protocol.ts — new optional commit field announces: 'announced-v1',
    present only when the batch was composed while the capability was already live.
    Validated for shape; an older main thread never negotiates the capability, so
    it never receives the field.
  • transport.ts — the capability is captured once at compose time and used
    for both the new flag and deferPublicInstances, instead of being re-read at
    dispatch.
  • main-thread.ts — the container's announce mode comes from the commit, and
    latches on: FIFO delivery means no un-announced commit can follow an announced
    one. Two new rejections: announces from a peer that did not negotiate, and
    instances on a commit that never announced.
  • host-driver.ts — one bindNodesRefSelector(state, record) decides every
    path that hands a host a physical node: install if the host already asked, or
    if the commit announces nothing. The native-list paths call it instead of
    repeating the condition, and ordinary create, recreate, and
    mount-template-run use it too. The per-commit deferral stays layered above
    it, because prepareBatch({ lazyPublicInstances: true }) is reachable without
    the announcement through the driver API.
  • Two sites stay eager by construction, now with the reason written down:
    first-screen direct emission paints before any peer exists, and first-tree
    adoption must overwrite what that container wrote — its root id can equal the
    accepted root's, so a skipped install would leave a node answering an address
    that names a different host.
  • benchmarks/lynx-table — the fake Element PAPI counts nodes-ref
    installs and clears and the selectable nodes it creates; the wire snapshot
    keeps each commit's ack/instances/announces and the one ready reply's
    capabilities. run.mjs reports create_ref_installs_* against a new
    eager-selector-model reference target, prints 0/4000 refs on the run line,
    and records the negotiated regime in meta.rows_*. An install count is
    unreadable without knowing whether the commit announced at all.
  • ratios.json — create_ref_installs_1k and _10k at maxRatio 0.01.
    Not zero, because an app that does hold refs legitimately pays for those
    nodes; what it catches is a return to installing on every node, which reads as
    1.00x.

Validation

  • pnpm format:check
  • pnpm typecheck
  • pnpm test — 2191/2196 files, 21041/21057 tests. Five failing files, none
    from this diff, each checked in isolation on a quiet host:
    drei upstream-e2e (container cannot fetch potsdamer_platz_1k.hdr),
    dropzone probes/hydration and rspeedy-plugin native-demo (both
    already recorded as pre-existing here), octane-events-browser
    suspense-hydration > restores focused input … ([0, 0] vs [2, 9],
    reproduces in isolation, headless selection restore), and
    octane-events-browser native-change (12 page.waitForFunction
    timeouts under full-run load — 12/12 pass in isolation).
    rspeedy-plugin-browser web-host passes.
  • targeted: vitest run --project lynx — 32 files, 445 tests, all pass
  • targeted: vitest run packages/rspeedy-plugin-octane/tests/browser/web-host.test.ts
    — 3/3 pass (this is the suite that caught the regression; it fails on the
    intermediate commit and passes on the base)
  • targeted: node benchmarks/bench.mjs --quick --ratios --only lynx-table —
    13/13 applicable guards pass, create=1 (1000r, 0/4000 refs)

Two oracles in background-root.test.ts, both over the real transport:

  • "addresses only the hosts a ref asked for, including ones a later commit
    creates"
    — a ref'd host and a ref'd row created by a later commit each
    address their own node through createLynxNodesRefSelector, the row's handle
    still drives setNativeProps, and #summary, #rows, #healthy, #details
    carry no attribute. Checked against two broken implementations: installing
    eagerly regardless goes red on the "carries no attribute" assertions; letting
    ensure-public-instance record the request without installing goes red on
    both addressing assertions, which pins the ordering that makes a fresh create
    safe.
  • "addresses a ref the background asked for before the main thread was there" —
    renders the root, then installs the main thread, then awaits. Reverting the
    container flag to the session-level capability turns it red, which is the
    gallery failure in one jsdom test.

One existing test changed for a reason worth naming: native-event-delivery
read #target's octane-ref attribute to discover the live root id for a
forged event token. Under this contract that node has no attribute — correctly,
since the fixture holds no ref. It now reads main.activeIdentity()!.root,
which is what it actually wanted.

Risk / follow-ups

  • These numbers are the best case. A row carrying a ref is announced and
    pays one write per announced node. The saving is per node nobody queries.
  • A root's first commit still installs eagerly when it is composed before
    the handshake completes — it announces nothing, so eager is the only sound
    answer. Closing that needs the background either to compose after readiness or
    to re-announce once the capability lands; both are separate slices, and both
    trade against first-paint overlap. The bench harness is synchronous end to
    end, so all 87 of its commits announce and it does not exercise this.
  • A batch composed before the handshake no longer carries instances: lazy-v1
    either, since both flags come from the same compose-time capability. That
    gives up deferred handle deltas on exactly the batches that were claiming them
    falsely, and is the price of the flag being true.
  • Untouched and now documented as such: first-screen direct emission and
    first-tree adoption. Both are bounded by the shell rather than the content —
    28 nodes for a 1,000-row screen here — and adoption cannot defer without
    clearing, which costs the same write.

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

claude added 2 commits August 21, 2026 13:38
A `nodes-ref` selector is now installed only for hosts the peer announced,
on ordinary create, recreate, and template-run as well as native list cells.

Measuring first inverted the question #129 left open. The same 1,000 rows
mounted as the first commit went out compact + lazy and cost 28 selector
writes for 7,042 nodes; mounted by a later commit the identical
mount-template-run went out eager and cost 4,000. Two of the run's 87
commits were compact, because transport.ts sets `instances: lazy-v1` only
on a compact first or post-first-tree commit — and the main thread was
reading that per-commit flag to answer a session-level question. The
session had negotiated lazyPublicInstances and stayed there throughout.

So the prize was never the first screen. It was every ordinary commit
after it, at four writes per row: 4,000 at 1k rows and 40,000 at 10k,
against zero `ensure-public-instance` announcements.

One `bindNodesRefSelector` now decides every path that hands a host a
physical node. The per-commit deferral stays layered above it, since the
driver API admits `prepareBatch({ lazyPublicInstances: true })` without
the container flag. First-screen direct emission and first-tree adoption
stay eager by construction, with the reason written down: the first
screen paints before a peer exists, and adoption must overwrite what that
container wrote because its root id can equal the accepted root's.

The lynx-table chassis counts installs, clears, and selectable nodes, and
records the regime every commit went out under — an install count is
unreadable without knowing whether the peer was announcing at all.
`create_ref_installs_{1k,10k}` guard it against a new eager-selector-model
reference at 0.01x; both measure 0.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
The demand rule from #129 covered only native list cells. Extending it to
ordinary create, recreate, and template-run mounts drops the benchmark table
from 4,000 nodes-ref selector writes to 0 at 1,000 rows, and from 40,000 to 0
at 10,000.

Reading the session's negotiated lazyPublicInstances capability to decide it
is unsound. A background composes its first batch before the main-ready reply
granting the capability reaches it, and dispatches it after, so that batch
names none of the hosts it will query however the session was negotiated. The
main thread, reading the reply it had already sent, skipped their selectors:
the gallery example's <list ref> mounted unaddressable and its auto-scroll
invoke failed with native code 2.

Commits now carry the promise themselves in a new optional `announces` field,
stamped from the capability captured when the batch was composed. The main
thread latches its container into demand installs on the first commit that
makes it, which is sound because delivery is FIFO. `instances` is decided from
the same compose-time capability rather than re-read at dispatch, closing the
same silent ref loss for a compact template-only first batch.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
Resolved `host-driver.ts` in the first-screen walk: new-lynx moved the
create-side work into `emitHostNode` and added the `insideList` early exit
for rows that own no element yet, so the eager `ensureNodesRefSelector`
call this branch introduced now lives in that helper, carrying its
rationale with it.

Co-Authored-By: Claude <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
@Huxpro
Huxpro changed the base branch from perf/lynx-nodes-ref-on-demand to new-lynx August 21, 2026 22:18
@Huxpro
Huxpro marked this pull request as ready for review August 21, 2026 22:18
@Huxpro
Huxpro merged commit b4eb02b 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