Skip to content

fix(hydration): activate ready independent islands one per task - #1866

Merged
domgan-oai merged 1 commit into
mainfrom
fix/island-activation-pacing
Oct 7, 2026
Merged

domgan-oai merged 1 commit into
mainfrom
fix/island-activation-pacing

Conversation

@trueadm

@trueadm trueadm commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This is step 4 of #1864, from the investigation comment. Independent <Hydrate> islands that become ready together now activate one per task, not back to back in one microtask checkpoint.

Why

Each island activation (adopt → renderBlock → commitRootRenders) is one synchronous block. Ready islands used to activate back to back inside one checkpoint, so the browser could not deliver input or paint until every one of them had finished. These paths converge in practice:

  • repeated instances of one island component share one module promise and one style promise;
  • one pending shared stylesheet releases every waiter in its single load event;
  • the on-demand strategies chunk, or one IntersectionObserver callback with the default 600px margin, releases several islands together;
  • a back/forward-cache pageshow → resume() re-arms every unfinished island in one loop, and their modules are already evaluated.

Changes

  • Gate: registerIndependentHydrationIsland sends each activation through a per-document gate in activate. The gate sits after the module resolves and before intents.splice(0) / replayReady = true. It lives in this public function rather than in app-core codegen, so custom hosts that call the function directly get it too.
    • The first island to reach the gate in a task activates at once, so a lone island has no added latency.
    • Each later island gets a task of its own: scheduler.postTask if present, otherwise a one-shot MessageChannel, otherwise setTimeout.
    • Islands holding captured interaction intents take the next turn ahead of automatic ones. Urgency is read whenever a turn is chosen, so input captured while an island waits promotes it.
  • Waiting is still loading:
    • replayReady stays false, so input keeps joining intents. Setting it before the yield would send input to a hold the activator has not registered yet, and the input would be dropped.
    • The active latch prevents a second chain.
    • pause() and dispose still cancel. After the wait, run re-checks disposed || generation !== attempt and then runs the native-intent expiry filter.
    • A stale or expired turn does not use up the task; the gate moves to the next waiter.
    • An activator error still reaches onError, and the next island still gets its turn.
  • Docs:
    • docs/deferred-hydration.md gains an "Activation pacing" subsection.
    • docs/ssr.md (islands-only routes) links to it.
  • Changeset: octane patch.

Not done: forwarding the IdleDeadline. The brief marked it optional, and I left it out:

  • each requestIdleCallback callback already runs as its own task;
  • by the time a module that was not cached resolves, the deadline is stale;
  • the gate already paces islands that converge after an idle trigger.

Pacing within an idle period belongs to the shared host budget (G3).

Validation

  • New: packages/octane/tests/hydration/independent-island-activation-pacing.test.ts (5 tests).

    • MessageChannel ordering. Four load islands share one module. A MessageChannel task is armed before the module resolves. The first island hydrates in the readying task, and the armed task runs before the last island hydrates.
    • One per task, input first. scheduler.postTask is stubbed to hold tasks, which makes this deterministic. Exactly one island activates per task, and the interaction island with a captured click goes ahead of earlier load islands. A second click on that island while it waits is replayed (intents = click, click).
    • Pause, dispose, resume. Pausing or disposing a waiting island cancels it without using up its task. Resume starts a new attempt.
    • Errors. A throwing activator reports through onError, and the next island still activates.
    • bfcache restore. pagehide before the modules resolve, then pageshow: the restore resumes three islands, and they activate one per task.
  • Mutation checks: each one turned the named tests red.

    Mutation Tests that failed
    gate removed 4 of 5 (all except the error test)
    no urgency input-first
    replayReady set before the yield input-first; pause
    no stale re-check pause
    a stale turn uses up the task pause
    errors escape run error test (and the next test, through the stuck gate)
  • Existing island suites (unchanged): 11 files and 271 tests pass in --project octane. They are the new file plus independent-island, independent-island-strategies, document-lifecycle, early-independent-intent, early-control-handoff, independent-parent, independent-root-fallback, deferred-hydration-styles, independent-island-signal-identity, and form-submission. initial-document-signals passes in --project octane-signals (38).

  • pnpm typecheck:files on the changed source and test

  • pnpm format:files:check on every changed file; pnpm sync was clean

  • pnpm format:check, pnpm typecheck, pnpm test: not run locally. The machine is shared, so CI runs the full suites.

Bundle cost: esbuild minify of hydration/independent-island.ts with the strategies chunk external goes from 20,845 to 21,539 B minified (+694) and from 7,531 to 7,790 B gzip (+259). Only pages that register independent islands load this chunk, and no bundle-size budget covers it.

Risk / follow-ups

  • Extra task of latency: an island whose module arrives in a fresh task, while the previous island's release task is still queued, waits for that release task. That is at most one task.
  • Async activators: the gate paces the activator's synchronous call. A custom activator that returns a promise and hydrates after its own await is not paced for that later work. The in-repo activators (createIndependentHydrateActivator, the dom-binding island activator) are synchronous.
  • Task posters: the runtime now has a fifth ad-hoc task poster. The investigation's shared poster (G1–G3) should absorb this one when it lands. The poster is kept here so the island bootstrap never reaches runtime.ts.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


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


Note

Medium Risk
Changes core independent-island activation timing on the client hydration path; incorrect pacing could delay interactivity or mishandle captured input, though behavior is covered by new tests and preserves existing pause/dispose semantics.

Overview
Independent <Hydrate> islands that become ready in the same microtask checkpoint (shared module load, one stylesheet load, IO callback, or bfcache resume) no longer hydrate back-to-back in one synchronous stretch. registerIndependentHydrationIsland now routes post-module activation through a per-document gate: the first ready island still runs immediately in that task; each additional island waits for its own task via scheduler.postTask, MessageChannel, or setTimeout(0).

Islands with captured interaction intents are scheduled ahead of automatic load/idle/visible triggers. While an island waits, it stays in the loading path (replayReady stays false) so input keeps appending to intents; pause, dispose, and stale attempts skip without consuming a turn. Activator errors still hit onError and the queue advances.

Docs add an Activation pacing subsection under deferred hydration and link it from islands-only SSR notes. A new Vitest suite exercises task ordering, input priority, pause/dispose/resume, errors, and bfcache restore.

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

Independent islands that become ready together (repeated instances sharing
one module, one stylesheet load or IntersectionObserver callback, a
back/forward-cache restore) each hydrated synchronously in the same
microtask checkpoint, so input and paint waited for all of them.

`registerIndependentHydrationIsland` now passes each activation through a
per-document gate after the module resolves and before replays are taken.
The first island to arrive in a task activates at once; each later one
gets its own task (scheduler.postTask, then MessageChannel, then
setTimeout), and islands holding captured input go first. A waiting island
is still loading: input keeps joining its replay queue, pause and dispose
cancel it without using up a task, and the native-intent expiry filter runs
after the wait.

Step 4 of #1864.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 7, 2026
@trueadm
trueadm marked this pull request as ready for review October 7, 2026 21:24
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🟡 js-framework: 1 timed operation(s) slower beyond ±3%

Compares 31724a3f9, the merge commit's first parent with 2efe15799 on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

target operation base ms head ms head/base 95% CI
octane-jsx clear 67.542 75.000 +9.0% +4.7% … +15.5% 🟡 slower
All 20 timed operations (19 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 4.433 4.183 −11.3% −16.5% … +7.6% ⚪ within noise
octane-tsrx replace 10.067 10.217 −0.2% −9.9% … +5.7% ⚪ within noise
octane-tsrx add 3.067 3.225 +3.5% −0.0% … +19.6% ⚪ within noise
octane-tsrx update 0.169 0.167 −0.9% −9.1% … +5.7% ⚪ within noise
octane-tsrx select 0.007 0.008 +2.2% −3.4% … +3.9% ⚪ within noise
octane-tsrx swap 0.040 0.043 +6.3% −0.3% … +11.6% ⚪ within noise
octane-tsrx remove 0.030 0.031 +4.8% −5.5% … +12.3% ⚪ within noise
octane-tsrx runlots 38.600 37.025 −0.9% −13.0% … +5.9% ⚪ within noise
octane-tsrx select_lots 0.008 0.008 +0.4% −2.8% … +3.3% ⚪ within noise
octane-tsrx clear 76.475 83.333 +0.2% −5.7% … +7.3% ⚪ within noise
octane-jsx run 3.825 3.858 −2.6% −4.9% … +3.8% ⚪ within noise
octane-jsx replace 10.317 11.967 +4.1% −1.4% … +10.1% ⚪ within noise
octane-jsx add 4.550 4.567 −2.4% −8.1% … +2.1% ⚪ within noise
octane-jsx update 0.386 0.369 +0.3% −5.3% … +9.1% ⚪ within noise
octane-jsx select 0.170 0.178 +0.3% −7.2% … +6.1% ⚪ within noise
octane-jsx swap 0.266 0.267 +2.4% −2.7% … +6.5% ⚪ within noise
octane-jsx remove 0.216 0.214 +0.8% −2.8% … +6.6% ⚪ within noise
octane-jsx runlots 33.633 33.467 +0.4% −4.4% … +3.6% ⚪ within noise
octane-jsx select_lots 1.631 1.682 +1.6% −2.4% … +4.9% ⚪ within noise
octane-jsx clear 67.542 75.000 +9.0% +4.7% … +15.5% 🟡 slower

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 60 measured values.

Public-import reachability bundles (bytes)

No changes across 111 measured values.

@trueadm

trueadm commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator Author

Agent-authored reply (Claude Code, for @trueadm).

No code change for the benchmark report's octane-jsx clear +9% wall-time flag:

  • This PR only touches packages/octane/src/hydration/independent-island.ts, the independent-island bootstrap. The js-framework app is a createRoot client and never loads it.
  • The report itself shows all 64 work counters unchanged and no byte change across the 60 app-bundle and 111 reachability values. The benchmarked code is therefore identical, so the timing difference is runner variance.
  • octane-tsrx clear is within noise in the same run.

@domgan-oai
domgan-oai merged commit 8fa3398 into main Oct 7, 2026
51 checks passed
@domgan-oai
domgan-oai deleted the fix/island-activation-pacing branch October 7, 2026 22:11
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