Repository navigation
fix(lynx): refuse first-screen event-channel collisions before touching PAPI (#84) - #94
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
assertNoMainThreadEventCollisionover the batch's final host set duringprepare, so such a host is refused at zero PAPI cost.
applyLynxFirstScreenDirecthas no prepare stage and skipped the checkentirely. The page painted,
applyPropsinstalled the worklet, andinstallNativeEventoverwrote the same channel with the background token on thevery 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-tscclean, 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
firstScreenTreeHasListmakes the direct applier decline andmain-thread.ts:1906falls through to the staged path. So whether an authoringmistake was reported or swallowed depended on whether an unrelated part of the
page used a list.
Changes
packages/lynx/src/core/host-driver.tsassertNoMainThreadEventCollisionForTypesis the shared rejection;assertNoMainThreadEventCollisionkeeps its map-shaped signature anddelegates, so both appliers raise the identical error.
applyLynxFirstScreenDirectpre-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
eventsByHostper node, and apage with no background listeners at all skips the walk entirely. It runs after
the native-
<list>decline and the batch envelope checks, so declined treesstill 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:1906unsafe to add.Validation
Red-green — each break applied, suite run, break reverted:
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.
pnpm typecheckpnpm sync(no generated changes)pnpm format:fileson every changed pathpnpm vitest run --project lynx --project octane --project octane-prod—866 files / 12,798 tests passed
pnpm testrepo-wide (run on PR perf(lynx): classify host updates without building the prop patch (issue-66 A4) #92, where the only failures were fivebrowser cases that reproduce identically on the base commit)
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).
baseis a preserved build of thisPR's parent — the iterative walk without the pre-walk. Committed as
benchmarks/lynx-table/prototype/results/fcp-10000-collision-check.md.octane-direct(L0 architecture floor)Ratio 0.974×, ranges overlapping: no measurable cost. The benchmark app puts
a
bindtapon every row, so this is the loaded case for the walk — 10,000 hostswith 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
main-thread:bindtapalone on the shipped swiper shape as needing one probeto settle. That is untouched here — this PR only restores the refusal the
staged path already performs.
fallback at
main-thread.ts:1906needs (see the parent PR's follow-ups). Thepre-walk is one step toward a transactional direct applier, not the whole of
it: PAPI faults mid-walk remain unrecoverable.
Provenance
agent-authored)Generated by Claude Code