Skip to content

fix(octane): stop hydration looping when stale server content follows an early-bound host - #1751

Merged
trueadm merged 3 commits into
mainfrom
fix/early-binding-extra-tail-hydration
Oct 5, 2026
Merged

trueadm merged 3 commits into
mainfrom
fix/early-binding-extra-tail-hydration

Conversation

@trueadm

@trueadm trueadm commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #1743.

When an early host binding (adoptBindings) was installed and the server node directly after its host was stale content, hydrateRoot returned but the next act() never did. Hydration now behaves as it does when no binding is installed: it removes the stale content, reports one recoverable error, keeps the adopted host, hands the binding off, and runs the layout effect.

Root cause. The host lease's valid() in dom-bindings.ts records the host's server neighbors as a site fingerprint. Mismatch recovery removed the stale neighbor, so at commit currentPresentation() saw an invalid lease. commitRootRenders then rolled back the attempt, which put the stale node back, and called retryPresentations(). The root retry queued adopt() in a microtask, the next attempt removed the node again, and the cycle repeated forever. Because each retry is a microtask, Vitest's timeout never fired either.

This happened on two recovery paths, which I confirmed on main:

  • settleClaim (stale content at the end of the host component's range), as in the issue's repro;
  • finishRoot (a root-level or in-place host, or one inside @if, @for or a fragment, followed by a stale root remainder).

Changes

  • HydrationCapability.save(parent) is the documented hook that runs before mismatch recovery changes a parent's children. It now adds parent to a new repairedServerParents WeakSet. That WeakSet lives in dom-binding-claims.ts, the module already shared by early bindings and hydration.

  • The host lease's valid() checks each side with adjacent(recorded, previous). That check passes in two cases:

    • the neighbor on that side is still the recorded one;
    • the recorded neighbor has been removed from the DOM and the host's parent is a repaired parent.

    The ancestry, document, data-octane-bindings and still-attached-neighbor checks are unchanged. The helper is allocated only when a lease is requested.

  • A first version tolerated any detached neighbor. It let an externally damaged site, such as a removed range marker, through the hydrateRoot gate and into a new loop. Tying the exemption to the recovery hook keeps the gate strict.

  • The lease honors a repair mark only while a hydration holds it (owner set) or after that hydration has retired it. hydrateRoot checks the lease before claiming it, so a repair left from an earlier hydration of the same container cannot excuse a later external change (raised by Bugbot). The runtime change stays a single line in save(), inside the hydrate-root byte budget.

Validation

  • pnpm format:check: not run repo-wide. pnpm format:files:check passes on the changed files.
  • pnpm typecheck: not run repo-wide. pnpm typecheck:files on the changed files reports no new errors. The three behavior-root.test.ts diagnostics it prints are unchanged lines that also exist on main.
  • pnpm test: left to CI.
  • targeted tests (--project octane): behavior-root.test.ts, binding-handoff.test.ts and behavior-handoff-bundle.test.ts, 339 passing. The new cases also pass under --project octane-prod.
  • New regression in behavior-root.test.ts: dev and prod × nested and root host × {matching, stale, removed by something other than hydration}. It asserts that there is no uncaught error, exactly one recoverable error for stale content, that the stale node is removed, the button and its child keep their identity, the layout effect runs once, the early source unsubscribes once and a later early publish does not write, and that a later root.render updates the same button. A render watchdog turns a non-converging hydration into a failed assertion instead of a hung worker.
  • Added a regression for a repair that outlives its hydration: the first hydration repairs the container, the root unmounts, a new lease's neighbor is then removed externally, and the next hydrateRoot must refuse with Fix user-app starter verifier CI timeout #77. It fails without the claim condition.
  • The test fails before the fix: the 4 stale cases trip the watchdog. Removing the repairedServerParents condition fails the 4 "removed" cases, because hydrateRoot no longer refuses with Fix user-app starter verifier CI timeout #77. The existing reorder refusal ('propagates a rejected external range readiness…') still fails if neighbor adjacency is dropped.
  • I also checked by hand: stale text, two stale siblings, stale content before the host (which rebuilds the host), stale content inside the host, and @if/@for/fragment/in-place root hosts. With the early binding, each now ends the same way as the no-binding control.

Risk / follow-ups

  • Recovery that inserts or moves a node next to an adopted host still invalidates the lease. I found no realistic trigger with intact markers. External damage to a host's markers is refused at the hydrateRoot gate, as before.
  • A commit-time presentation invalidation that the attempt itself causes still retries without a convergence bound. This PR removes the known triggers but adds no general guard.
  • Coordinated with the session working A moved early-bound host can keep publishing after its pending boundary hydrates a replacement #1745 (a moved early-bound host keeps publishing). That fix relies on the ancestry and still-attached-neighbor checks, which this PR leaves unchanged. Whichever PR lands second rebases.
  • I found two hydration-recovery defects that also occur without bindings. Both are out of scope here: a stale element before a component range leaves the old server range on screen next to the rebuilt one, and a missing range close marker throws a TypeError.

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 hydration mismatch recovery interaction with early binding lease validation—a sensitive commit/handoff path—but the scope is narrow and heavily regression-tested.

Overview
Fixes an infinite hydration retry loop when an early adoptBindings host sits next to stale server DOM that mismatch recovery removes during hydrateRoot.

Hydration now records parents touched by mismatch recovery in a new repairedServerParents WeakSet (HydrationCapability.save). Early host binding leases treat a recorded neighbor that was detached as still valid when that neighbor was removed as part of recovery under a repaired parent—matching the no–early-binding path: drop stale nodes, emit one recoverable error, keep the adopted host, and complete handoff. Any other neighbor change (including manual removal before/during lease) still refuses the lease (error #77).

Adds behavior-root coverage for nested/root placement, matching vs stale vs externally removed neighbors (dev/prod), plus a case where an earlier hydration repair must not excuse a later external DOM change.

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

… an early-bound host

An early host binding records its host's server neighbors as a site
fingerprint. When hydration mismatch recovery removed stale server content
beside the host, the lease looked moved at commit. The commit rolled back,
which restored the stale node, and retried in a microtask, forever.

Hydration's save() hook, which runs before recovery changes a parent's
children, now marks that parent. A host whose recorded neighbor was
removed from a repaired parent keeps its site. Any other neighbor change
still refuses the lease at hydrateRoot.

Fixes #1743

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 5, 2026
@trueadm
trueadm marked this pull request as ready for review October 5, 2026 07:50
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 2 value(s) increased within budget
  • 🔴 bundle-reachability: 6 value(s) increased within budget

Compares d7ee29b77, the merge commit's first parent with cd39c6ca2 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-tsrx select_lots 0.006 0.006 −5.2% −6.7% … −4.1% 🟢 faster
All 20 timed operations (19 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 3.117 3.042 −0.0% −3.4% … +1.7% ⚪ within noise
octane-tsrx replace 5.583 5.983 ±0.0% −1.8% … +4.0% ⚪ within noise
octane-tsrx add 2.525 2.333 −3.8% −4.2% … ±0.0% ⚪ within noise
octane-tsrx update 0.122 0.119 −1.7% −3.0% … −1.1% ⚪ within noise
octane-tsrx select 0.005 0.005 ±0.0% −1.3% … +0.8% ⚪ within noise
octane-tsrx swap 0.033 0.033 ±0.0% −1.8% … +1.8% ⚪ within noise
octane-tsrx remove 0.019 0.018 −3.5% −7.0% … +1.1% ⚪ within noise
octane-tsrx runlots 28.442 26.358 −0.5% −5.3% … +3.2% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 −5.2% −6.7% … −4.1% 🟢 faster
octane-tsrx clear 56.150 58.192 +2.9% −0.4% … +6.6% ⚪ within noise
octane-jsx run 4.142 4.000 −2.4% −4.3% … +2.5% ⚪ within noise
octane-jsx replace 9.817 9.608 −1.6% −5.7% … +1.5% ⚪ within noise
octane-jsx add 4.092 3.917 −2.5% −6.9% … ±0.0% ⚪ within noise
octane-jsx update 0.298 0.295 −0.3% −2.8% … +1.2% ⚪ within noise
octane-jsx select 0.136 0.139 +1.8% ±0.0% … +4.1% ⚪ within noise
octane-jsx swap 0.237 0.242 −1.6% −2.2% … −0.3% ⚪ within noise
octane-jsx remove 0.194 0.191 −0.6% −1.5% … ±0.0% ⚪ within noise
octane-jsx runlots 35.450 36.125 +1.3% −1.6% … +4.4% ⚪ within noise
octane-jsx select_lots 1.685 1.725 +0.9% −3.3% … +5.5% ⚪ within noise
octane-jsx clear 45.192 43.800 −0.8% −2.4% … +0.7% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

target metric base head Δ
octane-tsrx bindings_js_gzip 240,865 240,853 −12 (−0.0%) 🟢
octane-tsrx bindings_js_brotli 205,129 204,952 −177 (−0.1%) 🟢
octane-tsrx bindings_app_gzip 121,729 121,717 −12 (−0.0%) 🟢
octane-tsrx bindings_app_brotli 101,815 101,638 −177 (−0.2%) 🟢
octane-tsrx full_fw_raw 569,319 569,344 +25 (+0.0%) 🔴
octane-tsrx full_fw_gzip 174,423 174,415 −8 (−0.0%) 🟢
octane-tsrx full_fw_brotli 147,294 147,240 −54 (−0.0%) 🟢
octane-jsx full_fw_raw 569,319 569,344 +25 (+0.0%) 🔴
octane-jsx full_fw_gzip 174,423 174,415 −8 (−0.0%) 🟢
octane-jsx full_fw_brotli 147,294 147,240 −54 (−0.0%) 🟢

50 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
hydrate-root raw 173,788 173,813 +25 (+0.0%) 🔴
hydrate-root gzip 55,158 55,160 +2 (+0.0%) 🔴
hydrate-root brotli 48,123 48,183 +60 (+0.1%) 🔴
deferred-hydration raw 237,306 237,331 +25 (+0.0%) 🔴
deferred-hydration gzip 75,000 75,017 +17 (+0.0%) 🔴
deferred-hydration brotli 64,807 64,840 +33 (+0.1%) 🔴

99 other measured values are unchanged.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Lease exemption outlives the hydration
    • Journaled the repairedServerParents.add(parent) call in save() for both root hydration rollback (journalUndo) and speculative attempt rollback (this.undo), matching the existing remember() pattern, so rolled-back attempts no longer leave stale exemptions; commit-time cleanup was not added because the exemption is still needed when render cleanups validate the lease during spliceOffscreenCapture.

Create PR

Or push these changes by commenting:

@cursor push 8d4923de30
Preview (8d4923de30)
diff --git a/packages/octane/src/runtime.ts b/packages/octane/src/runtime.ts
--- a/packages/octane/src/runtime.ts
+++ b/packages/octane/src/runtime.ts
@@ -19420,7 +19420,14 @@
 	 */
 	save(parent: Node): void {
 		// An early-bound host losing a server neighbor here has not moved.
-		repairedServerParents.add(parent);
+		// Journal the exemption so a rolled-back attempt does not leave a stale
+		// entry that a later hydrateRoot on the same container could misread.
+		if (!repairedServerParents.has(parent)) {
+			repairedServerParents.add(parent);
+			const cleanup = () => repairedServerParents.delete(parent);
+			if (inRootHydrationAttempt()) journalUndo(cleanup);
+			if (this.speculative) (this.undo ??= []).push(cleanup);
+		}
 		if (inRootHydrationAttempt()) journalRootChildren(parent);
 		if (!this.speculative || (this.saved ??= new Set()).has(parent)) return;
 		this.saved.add(parent);

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 272fd15. Configure here.

Comment thread packages/octane/src/runtime.ts Resolved
trueadm and others added 2 commits October 5, 2026 09:09
…them

A parent repaired by an earlier hydration stayed marked, so a later
hydrateRoot on the same container treated an externally removed host
neighbor as a repair and accepted the lease. hydrateRoot now clears a
leased host's parent before validating the lease.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The hydrateRoot reset added a native parentNode read outside the
staged-DOM inventory and pushed hydrate-root past its byte budget. Scope
the exemption in the lease instead: a repaired parent excuses a detached
neighbor only while a hydration holds the lease or after it retired it.
The hydrateRoot gate runs before the claim, so it stays strict, and the
runtime keeps only the one-line mark in HydrationCapability.save().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

Hydration can loop when an early host binding is followed by stale server content

1 participant