Skip to content

fix(lynx): refuse first-screen event-channel collisions before touching PAPI (#84) - #94

Merged
Huxpro merged 1 commit into
new-lynxfrom
fix/lynx-first-screen-event-collision
Aug 19, 2026
Merged

Huxpro merged 1 commit into
new-lynxfrom
fix/lynx-first-screen-event-collision

Conversation

@Huxpro

@Huxpro Huxpro commented Aug 19, 2026

Copy link
Copy Markdown
Owner

Summary

A main-thread worklet and a background listener cannot share one native channel:
whichever is installed second silently supersedes the other. The staged path
runs assertNoMainThreadEventCollision over the batch's final host set during
prepare, so such a host is refused at zero PAPI cost.

applyLynxFirstScreenDirect has no prepare stage and skipped the check
entirely. The page painted, applyProps installed the worklet, and
installNativeEvent overwrote the same channel with the background token on the
very next statement — leaving the main-thread handler silently dead, with its
activation retained for the page's lifetime.

Reported as #84, which the audit thread ranked first: reachable from ordinary
.tsrx, tsrx-tsc clean, and stack-introduced.

Why

The pattern that hits it — a background handler plus a main-thread handler for
immediate feedback — is exactly what the collision guard was written to reject.

It also made the diagnosis inconsistent within one app. The same page
hard-fails with a clear conflict error as soon as it contains a native <list>,
because firstScreenTreeHasList makes the direct applier decline and
main-thread.ts:1906 falls through to the staged path. So whether an authoring
mistake was reported or swallowed depended on whether an unrelated part of the
page used a list.

Changes

  • packages/lynx/src/core/host-driver.ts
    • assertNoMainThreadEventCollisionForTypes is the shared rejection;
      assertNoMainThreadEventCollision keeps its map-shaped signature and
      delegates, so both appliers raise the identical error.
    • applyLynxFirstScreenDirect pre-walks the tree and refuses before mutating.
  • packages/lynx/tests/first-screen-direct.test.ts: two regressions.
  • docs/lynx-specialized-target-l0.md §8 and the committed FCP session record.

The pre-walk is bounded rather than unconditional: only a host the batch gave a
background listener can collide, so it consults eventsByHost per node, and a
page with no background listeners at all skips the walk entirely. It runs after
the native-<list> decline and the batch envelope checks, so declined trees
still reach the staged path exactly as before.

Refusing before mutating is the point, not an implementation detail. The
direct applier mutates as it walks, so a check inside the walk would leave a
half-painted page — the state the staged path never produces, and the state that
makes the missing throw-side fallback at main-thread.ts:1906 unsafe to add.

Validation

Red-green — each break applied, suite run, break reverted:

deliberate break result
remove the collision pre-walk entirely 1 failed / 7 passed
check only roots, not descendants 1 failed / 7 passed
run the pre-walk after mutation instead of before 1 failed / 7 passed
(restored) 8 passed

The first regression asserts the exact error text and that the refusal cost
nothing: the page has no children and nothing flushed. The second pins that the
walk does not over-reject — a main-thread handler and a background listener on
different native channels on the same host is not a collision.

Measurement

The pre-walk adds a traversal to the first-screen path, so it has to be paid for.
Mount-create FCP@10000, same window, fresh page per sample, cells alternating
AB/BA, n=5, quiet host (1-minute load 0.33). base is a preserved build of this
PR's parent — the iterative walk without the pre-walk. Committed as
benchmarks/lynx-table/prototype/results/fcp-10000-collision-check.md.

cell median ms min–max
base (no pre-walk) 1798.7 1738.8–1815.8
candidate (pre-walk) 1752.0 1701.8–2107.5
octane-direct (L0 architecture floor) 1102.2 1093.9–1260.3

Ratio 0.974×, ranges overlapping: no measurable cost. The benchmark app puts
a bindtap on every row, so this is the loaded case for the walk — 10,000 hosts
with listeners, not the skip path.

Absolute milliseconds here are not comparable with the depth-fix window on
the parent PR (this window's 5- and 15-minute load averages were higher); only
the within-window ratio is a claim. Raw bundle: +219 B.

Risk / follow-ups

Provenance

  • An agent produced this diff (agent-authored)

Generated by Claude Code

…ng PAPI

The staged path runs `assertNoMainThreadEventCollision` over the batch's final
host set during prepare, so a host carrying both a main-thread worklet and a
background listener on one native channel is refused at zero PAPI cost. The
direct applier has no prepare stage and skipped the check entirely: the page
painted, `applyProps` installed the worklet, and `installNativeEvent`
immediately overwrote the same channel with the background token, leaving the
main-thread handler silently dead with its activation retained for the page's
lifetime.

The behaviour was also inconsistent within one app. The same page hard-fails
with a clear conflict error as soon as it contains a native `<list>`, because
`firstScreenTreeHasList` makes the direct applier decline and the caller falls
through to the staged path — so whether an authoring mistake was reported
depended on whether an unrelated part of the page used a list (#84).

The direct applier now pre-walks the tree and refuses before mutating. Only
hosts the batch gave a background listener can collide, so the walk checks
those and skips entirely when the page has no background listeners. The
rejection is the same shared assertion, so both appliers report the same error.

Red-green: removing the pre-walk, checking only roots instead of descendants,
and moving the check after mutation each turn the regression red.

Same-window mount-create FCP@10000, fresh page per sample, AB/BA, n=5, quiet
host: 1798.7 ms (1738.8-1815.8) without the pre-walk versus 1752.0 ms
(1701.8-2107.5) with it, architecture floor 1102.2 ms. The ranges overlap, so
the added traversal has no measurable cost. Raw bundle grows 219 B.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RsC3U39kUuLKajZANeTVy
@Huxpro
Huxpro changed the base branch from fix/lynx-first-screen-depth to new-lynx August 19, 2026 18:30
@Huxpro
Huxpro marked this pull request as ready for review August 19, 2026 18:30
@Huxpro
Huxpro merged commit 0b7d78b into new-lynx Aug 19, 2026
3 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.

1 participant