Skip to content

fix: align delegated discrete event commit boundary with React batchedUpdates - #927

Merged
domgan-oai merged 3 commits into
mainfrom
fix/discrete-event-flush-parity
Sep 2, 2026
Merged

domgan-oai merged 3 commits into
mainfrom
fix/discrete-event-flush-parity

Conversation

@trueadm

@trueadm trueadm commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Align Octane's delegated-event commit boundary with React's batchedUpdates: the outermost dispatch of a discrete event flushes synchronously only when a controlled value/checked host armed a state restore during that dispatch; every other handler update stays in the microtask batch.
  • Script-dispatched events (dispatchEvent, click(), requestSubmit() from outside code) no longer publish a commit mid-dispatch that React would publish only after the dispatching script yields. Browser-dispatched events are observationally unchanged: the microtask checkpoint runs before the next native listener and before the default action.

Why

  • Outside event systems (native capture/bubble listeners, controllers that dispatch programmatically and then inspect the DOM) observed committed Octane DOM where a React root — or a nested React root — still shows pre-commit DOM. React 19's batchedUpdates$1 (react-dom-client) flushes sync work at the end of the outermost event only when a controlled restore is pending; verified empirically in this repo's jsdom and in real Chromium.
  • The previous conformance test pinned the unconditional sync flush as React parity; that was React 17 legacy-mode behavior.

Changes

  • packages/octane/src/runtime.ts: maybeFlushDiscrete is gated on pendingRestores.length > 0; comments on DISCRETE_EVENTS, flushSync, the flush re-entrancy guard, and the capture-only fallback state the actual policy.
  • New packages/octane/tests/discrete-dispatch-commit-timing.test.ts (+ _fixtures/dispatch-commit-timing.tsrx) with an inline React 19 oracle; conformance/scheduler-priority.test.ts rewritten to the React policy.
  • Real-browser tests/browser/event-boundaries: new commit-timing probe for Octane (_fixtures/event-commit-timing.tsrx) and a hand-written React counterpart — a trusted click commits before a later native document listener runs; a script click commits only after the dispatching script yields; both renderers agree.
  • 12 other test files adjusted: bare dispatch followed by a synchronous assertion now goes through act()/flushSync(); contract tests for async actions, nested roots, react-hosted islands, and the view-transition in-flight path re-pinned to the React-consistent behavior.
  • packages/octane/src/runtime.ts setValue: controlled value updates write the DOM property before syncing the value attribute (React updateInput order, and Octane's own mount order). The new timing made this reachable: a script click on a reset button that also clears state now commits after the native reset, which leaves the control non-dirty; attribute-first moved the live value and skipped the property write, leaving it non-dirty where React marks it dirty (user-event's value tracker relies on that write — this is what broke the tanstack-form reset in onSubmit case). Regression ResetForm in discrete-dispatch-commit-timing for Octane and the React 19 oracle; red on the old order (expected 'drift' to be '').
  • Test rig packages/octane/tests/_helpers.ts: flushEffects()/nextPaint() now flushSync(() => {}) before draining passive effects, so the deterministic "browser settles" stand-in includes the microtask commit a script-dispatched event leaves queued. That keeps binding tests written as el.click(); flushEffects(); expect(DOM) (rainbowkit, tanstack-hotkeys) valid without per-site edits.
  • Six more tests re-pinned explicitly after the first CI run: devtools-runtime.test.tsrx (script click commits in the microtask, subscriber notified then), signals-rendering.test.ts (event-owned batch settled before observing the DOM), colorful hydration settle() (commit, then passive effects), the usehooks-ts package-boundary consumer (flushSync(() => button.click())), the drei Html click, and the colorful adapted pointer/touch helpers, which now commit per simulated event the way upstream's act-wrapped fireEvent does (two script dispatches in one task no longer coalesce into one onChange).
  • Audit ledgers: refreshed the pinned sha256 of testing-library/tests/differential.test.ts and of the drei Html test (plus the runtime-evidence.json that pins it); pnpm react-parity:validate passes.
  • docs/differences-from-react.md scheduling section; changeset (octane patch).

Validation

  • pnpm format:check
  • pnpm typecheck — targeted: tsgo --noEmit -p packages/octane/tsconfig.json and -p packages/testing-library/tsconfig.json (the full script was not run)
  • pnpm test — full root script on the pushed head after the CI follow-up: 2543 test files / 27,850 tests pass across every vitest project (the first CI run exposed 19 tests in 8 files outside the initially targeted projects, and the local full run three more; all now pass).
  • targeted tests: the new commit-timing suite fails 6/17 on the previous policy and passes 17/17 on the new one; the browser probe passes for both renderers in Chromium
  • pnpm react-parity:validate (manifests + evidence hashes current) and pnpm sync (no generated changes)
  • benchmark node benchmarks/event-delegation/run.mjs octane-tsrx 10 — baseline 4.80 ms (p95 6.70) vs candidate 4.38 ms (p95 7.10), all correctness and deterministic work gates pass (this burst exercises the unchanged controlled-restore path, so it is the semantic control; no speed claim is made)

Risk / follow-ups

  • Consumer tests that asserted committed state immediately after a bare dispatchEvent need act()/flushSync(), as with React. @octanejs/testing-library's fireEvent is already flushSync-wrapped and is unaffected.
  • Deliberately not in this PR: touchstart/touchmove root listeners remain passive: false (React 17+ registers them passive), and continuous events still have no separate priority lane.
  • pnpm typecheck:files on test files that import .tsrx fixtures reports TS2307; this is pre-existing (tests are excluded from the octane tsconfig).

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


Note

Medium Risk
Changes core event scheduling and controlled-input DOM writes—behavioral breaking for tests or integrations that assumed every discrete event committed synchronously, though it matches React and is heavily regression-tested.

Overview
Aligns Octane’s delegated discrete-event scheduling with React 19 batchedUpdates: the outermost click/input/etc. dispatch only sync-flushes when a controlled value/checked host armed a state restore; ordinary handler updates stay in the microtask batch, so script dispatches (dispatchEvent, click(), requestSubmit()) no longer commit mid-dispatch while React would commit after the script yields. Native listeners and nested roots therefore see the same pre-commit DOM as under React; browser-dispatched events stay observationally similar because the microtask runs before later listeners.

Controlled value commits now write the DOM property before syncing the value attribute (React updateInput order), fixing reset-button + controlled-input cases where attribute-first left the control non-dirty.

Adds discrete-dispatch-commit-timing tests with a React 19 oracle, real-browser commit-timing probes, and re-pins many suites to use act()/flushSync() where bare dispatches used to assume synchronous commits. Test helpers (flushEffects/nextPaint) flush the microtask queue first; docs and a patch changeset document the behavior shift.

Reviewed by Cursor Bugbot for commit 0364327. Bugbot is set up for automated code reviews on this repo. Configure here.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

…dUpdates

The outermost delegated dispatch of a discrete event now flushes synchronously
only when a controlled `value`/`checked` host armed a state restore during that
dispatch; every other handler update stays in the microtask batch, matching
React 19's `batchedUpdates`. Script-dispatched events (`dispatchEvent`,
`click()`, `requestSubmit()`) therefore no longer publish a commit mid-dispatch
that React would publish only after the dispatching script yields, while
browser-dispatched events are unaffected because the microtask checkpoint runs
before the next native listener and the default action.

Adds a commit-timing suite with an inline React 19 oracle, a real-browser
probe for trusted versus script clicks on both renderers, re-pins the
conformance tests that encoded the previous boundary, and documents the
contract in differences-from-react.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review September 2, 2026 00:07
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Sep 2, 2026
trueadm and others added 2 commits September 2, 2026 01:54
…ts in the test rig

The first CI run of the restore-gated event boundary exposed 19 tests in 8
files that only run in projects outside the local matrix (octane-profile,
octane-signals*, rainbowkit, tanstack-hotkeys, colorful, usehooks-ts), and
the local full run two more (colorful adapted pointer probes, drei Html) plus
a genuine runtime ordering defect the new timing made reachable.

Runtime: `setValue` now writes the DOM property before syncing the value
attribute on updates, as on mount and as React's updateInput does. When the
commit lands after a reset button's default action (any script-dispatched
click), the native reset has left the control NON-DIRTY, so the old
attribute-first write moved the live value itself, skipped the property write,
and left the control non-dirty where React marks it dirty. Regression:
`ResetForm` in discrete-dispatch-commit-timing (Octane + React 19 oracle);
the test fails on the previous order with `expected 'drift' to be ''`.

Tests:
- _helpers.ts: flushEffects()/nextPaint() flushSync the queued commit before
  draining passive effects, so the deterministic "browser settles" stand-in
  includes the microtask commit a script-dispatched event leaves behind.
- devtools-runtime, signals-rendering, colorful hydration, usehooks-ts
  package-boundary, drei Html: pin the new timing explicitly.
- colorful adapted pointer/touch helpers dispatch inside flushSync, so each
  simulated event commits on its own like upstream's act-wrapped fireEvent.
- Audit ledgers: refresh the pinned hashes of testing-library's
  differential.test.ts and drei's html.test.ts / runtime-evidence.json (lint
  and all four React-parity shards failed on the mismatch).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@domgan-oai
domgan-oai merged commit 34ba45e into main Sep 2, 2026
30 checks passed
@domgan-oai
domgan-oai deleted the fix/discrete-event-flush-parity branch September 2, 2026 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants