Skip to content

fix(hydration): remove the server tail after a component root rebuilt over its range - #1614

Merged
trueadm merged 10 commits into
mainfrom
claude/adoring-noether-cad2cc
Oct 3, 2026
Merged

trueadm merged 10 commits into
mainfrom
claude/adoring-noether-cad2cc

Conversation

@trueadm

@trueadm trueadm commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When hydration rebuilds a component's single root over a server node that does not match it, the rest of the server content in that component's range now goes too. Before this fix, it stayed on screen after the rebuilt root, and nothing reported it.

Repro (pre-existing on main, no suspension needed): a lite Para (<p>) adopts the range the server rendered for BoldThenEm (<b> + <em>tail</em>) in the other @if arm. The section read <p>p</p><em>tail</em><em>e</em>. It now reads <p>p</p><em>e</em>. The following Em's server <em> keeps its identity, and the mismatch is reported once.

Cause. Only a successful adoption recorded where a range's content ends (claimRoots, from adopt()). When the range's own template rebuilt the range's first node instead, nothing recorded it. settleClaim then fell back to its last-slot heuristic and left the tail in place. This affected every claim owner: componentSlotLite, and renderClaimed in both componentSlotImpl and childSlot.

Fix (packages/octane/src/runtime.ts):

  • adopt()'s single-root mismatch path calls claimRoots(cursor, null) when the rebuilt node is the open claim's first node. claimRoots' owner check limits this to the range's own content: the owning block, a call inheriting the range, or the lite component that adopted it. It then records that range's start marker (rebuiltRange). A markerless branch's claim (fix(hydration): end a markerless branch arm after every root its template adopted #1622's claimOwner) has no range of its own, so it records none.
  • When that rebuilt root commits (rebuiltAt), it takes the rest of the range with it (takeRangeTail), while the cursor still rests right after the node it replaced. This is quiet: the rebuild already reported the mismatch, as @if/@switch arms already do for a rebuilt root's tail. Because it happens at commit, every slot kind and every resume path is covered: componentSlotLite, componentSlotImpl, childSlot, a @try retry, and a <Hydrate> resume (fix(hydration): resume a suspended child inside a rebuilt component fragment #1598).
  • Review history: earlier heads swept from settleClaim and from each slot kind's resume path. Bugbot found three resume gaps that way: a hole whose call rebuilt its root, an inheriting call in a full slot, and a hole or return value. The commit-time take replaced all of them.
    • The RebuiltHoleBranch and StateRebuiltHoleBranch tests check that a resumed fragment keeps the static root it adopted after a hole whose call rebuilt its root.
    • InheritedRootBranch, RebuiltHoleValueBranch and ReturnBranch check that the tail goes for every slot kind.
    • The MarkerlessRebuilt control covers fix(hydration): end a markerless branch arm after every root its template adopted #1622's markerless claim. Without the claimOwner guard, the merge would throw NotFoundError there.

Design note: a broader version, sweeping whenever the cursor rests on a rebuilt root's tail, deleted a fragment template's adopted static element after a hole whose call was rebuilt in place. The fragment's static roots do not move the cursor. The HoleThenElement control pins main's correct behavior there.

Validation

  • pnpm format:check: not run. Ran pnpm format:files:check on the changed files, which passes.
  • pnpm typecheck: not run. Ran pnpm typecheck:files on runtime.ts and both tests and .tsrx fixtures, which passes.
  • pnpm test: not run locally. CI covers it.
  • targeted tests:
    • New tests/hydration/rebuilt-root-tail.test.ts: 66 cases × octane + octane-prod, run under dev compile, prod compile, and prod compile with prod runtime.
      • The defect: 8 shapes (lite arm, full component with hooks, dynamic identity, dynamic template, renderable hole, conditional return, list item, inherited call), plus a suspended @try retry.
      • Three controls: a later call's own range, a template element after a hole, and a markerless branch whose rebuilt root precedes a later call's node. Each keeps the claimed node.
      • Matching renders: no reports and no removals.
    • Pre-fix: 27 failures. Mutations, each verified red:
      • no claim on rebuild: 27 fail;
      • no commit-time take: 17 fail;
      • claim without the owner check: ClaimedTail fails;
      • the broader sweep: HoleThenElement fails;
      • rebuiltRange recorded for a markerless claim: MarkerlessRebuilt fails (it throws NotFoundError);
      • a resume sweep without the range guard (earlier design): RebuiltHoleBranch or StateRebuiltHoleBranch fails;
      • no resume handling: fix(hydration): resume a suspended child inside a rebuilt component fragment #1598's RebuiltRootBranch and the new InheritedRootBranch fail.
    • On the merge with main at 8e90048: rebuilt-root-tail, rebuilt-fragment-suspended-child, branch-arm-claim-tail, in-place-tail and markerless-arm-retry in octane, 232 pass. Before that merge, rebuilt-root-tail, rebuilt-fragment-suspended-child and in-place-tail × octane + octane-prod: 262 pass.
    • 35 related hydration files (adopted-, branch-arm-, in-place-, mismatch-, rebuilt-, arm-, range/component-range, lite-host-mismatch) in octane: 1,385 pass, on an earlier head (5e57a13). Not re-run on this head because of local machine load; CI covers it.
  • Bundle, measured on an earlier head (the PR bench reports the current head): node benchmarks/bundle-size/run-minimal.mjs hydrate-root
    • baseline (main 6ea582e): 169,643 raw / 53,945 gzip / 47,135 brotli
    • candidate: 169,761 / 53,966 / 47,104
    • delta: +118 B raw, +21 B gzip, −31 B brotli
  • Hot path: no per-render or per-settle cost. The claim and the commit-time take run only on a single-root mismatch.

Risk / follow-ups

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
Touches core hydration mismatch recovery and DOM removal in runtime.ts; wrong range boundaries could drop claimed nodes or leave stale markup, though broad tests and control cases target those edges.

Overview
When mismatch recovery rebuilds a component’s single root over the first server node in that component’s claim range, hydration now drops the rest of the server markup inside that same range (e.g. trailing nodes from the server’s other component) at commit time, without a second mismatch report. Siblings after the range still reuse their server nodes.

The runtime tracks whether the rebuilt root is the range’s whole content (rebuiltRange + claimRoots on the mismatch path in adopt()), then takeRangeTail in rebuiltAt() removes stale server nodes between the replaced node and the range end when nothing else has claimed them. That covers suspending retries and <Hydrate> resumes; fragments that only rebuild a hole and still adopt a static sibling are unchanged.

Adds rebuilt-root-tail coverage (many slot shapes, suspension, and controls for claimed tails / markerless claims) and extends suspended-child hydration fixtures for inherited roots, holes, returns, and inline suspend paths. Patch changeset for octane.

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

… over its range

When a range's own single-root template did not match the range's first
server node, adopt() rebuilt the root, but only a successful adoption
recorded where the range's content ends (claimRoots). settleClaim then fell
back to its last-slot heuristic and left the rest of the server range on
screen, unreported.

The single-root mismatch path now records the claim when it rebuilds the
open claim's first node, under claimRoots' existing owner check, and
settleClaim removes that tail with sweepRebuiltTail, without a second
report. A lite component replayed by a <Hydrate> resume (#1598) sweeps the
same tail before stepping past its range.

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 2, 2026
@github-actions
github-actions Bot marked this pull request as draft October 2, 2026 22:21
@trueadm
trueadm marked this pull request as ready for review October 2, 2026 22:21

@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.

Autofix Details

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

  • ✅ Fixed: Replay sweep deletes adopted siblings
    • Cleared rebuiltTail in settleClaim's else branch (when from !== rebuiltTail) so that a stale tail from a first render that settled without sweeping does not cause the lite replay path's unconditional sweepRebuiltTail call to delete an adopted sibling.

Create PR

Or push these changes by commenting:

@cursor push bc16fb1d1a
Preview (bc16fb1d1a)
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
@@ -19482,6 +19482,8 @@
 		if (from === this.rebuiltTail && from !== null) {
 			this.sweepRebuiltTail(end);
 			if (this.node === end) return;
+		} else {
+			this.rebuiltTail = null;
 		}
 		if (from === undefined) {
 			// A slot that adopted a server node without a range parks the cursor

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

Comment thread packages/octane/src/runtime.ts Resolved
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🟡 js-framework: 1 timed operation(s) possibly slower
  • 🔴 bundle-reachability: 7 value(s) increased

Compares 27c2a12da with a1e08efa8 on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

target operation base head Δ noise
octane-tsrx runlots 31.14 42.24 +35.6% ±28% 🟡 slower
All 20 timed operations (19 within noise)
target operation base head Δ noise
octane-tsrx run 3.79 3.64 −4.0% ±22% ⚪ within noise
octane-tsrx replace 8.79 8.97 +2.0% ±33% ⚪ within noise
octane-tsrx add 3.68 3.58 −2.7% ±23% ⚪ within noise
octane-tsrx update 0.39 0.45 +15.4% ±24% ⚪ within noise
octane-tsrx select 0.17 0.19 +11.8% ±88% ⚪ within noise
octane-tsrx swap 0.40 0.45 +12.5% ±33% ⚪ within noise
octane-tsrx remove 0.41 0.48 +17.1% ±60% ⚪ within noise
octane-tsrx runlots 31.14 42.24 +35.6% ±28% 🟡 slower
octane-tsrx select_lots 0.27 0.31 +14.8% ±43% ⚪ within noise
octane-tsrx clear 45.51 49.38 +8.5% ±10% ⚪ within noise
octane-jsx run 3.78 3.81 +0.8% ±14% ⚪ within noise
octane-jsx replace 7.97 8.58 +7.7% ±25% ⚪ within noise
octane-jsx add 3.27 3.36 +2.8% ±8% ⚪ within noise
octane-jsx update 0.52 0.70 +34.6% ±47% ⚪ within noise
octane-jsx select 0.44 0.43 −2.3% ±48% ⚪ within noise
octane-jsx swap 0.73 0.70 −4.1% ±47% ⚪ within noise
octane-jsx remove 0.63 0.78 +23.8% ±38% ⚪ within noise
octane-jsx runlots 30.53 32.37 +6.0% ±15% ⚪ within noise
octane-jsx select_lots 2.43 2.40 −1.2% ±24% ⚪ within noise
octane-jsx clear 47.18 48.24 +2.2% ±14% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

target metric base head Δ
hydrate-root raw 171,116 171,573 +457 (+0.3%) 🔴
hydrate-root gzip 54,299 54,419 +120 (+0.2%) 🔴
hydrate-root brotli 47,409 47,471 +62 (+0.1%) 🔴
deferred-hydration raw 234,555 235,012 +457 (+0.2%) 🔴
deferred-hydration gzip 74,202 74,316 +114 (+0.2%) 🔴
deferred-hydration brotli 64,069 64,137 +68 (+0.1%) 🔴
binding-base-ui-esbuild brotli 59,025 59,000 −25 (−0.0%) 🟢
binding-radix-esbuild gzip 68,697 68,690 −7 (−0.0%) 🟢
binding-radix-esbuild brotli 59,585 59,607 +22 (+0.0%) 🔴

96 other measured values are unchanged.

…t was rebuilt

The <Hydrate> resume path swept the rebuilt root's server tail whenever one
was pending. When the rebuilt root came from a call in a fragment's leading
hole, that tail was the fragment's own adopted static root, so the resume
deleted it. The single-root mismatch path now records the scope whose
range's whole content the rebuilt root is (claimRoots' owner check), and a
lite replay sweeps only for that scope.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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.

Stale Bugbot comment from a previous run.

Comment thread packages/octane/src/runtime.ts Resolved
trueadm and others added 2 commits October 3, 2026 01:05
… component slots too

A <Hydrate> resume of a wrapper whose only call inherits its range left the
rebuilt root's server tail. The rebuild recorded the inheriting call's
scope, but the wrapper's slot is what completes on the resume, and that
wrapper renders in a full component slot, which never swept. The rebuild
now records the start marker of the range whose whole content it is
(rebuiltRange), and both the lite replay and componentSlotImpl's
post-render step sweep when their range starts there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r-cad2cc

# Conflicts:
#	packages/octane/src/runtime.ts

@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.

Autofix Details

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

  • ✅ Fixed: Child-slot resume skips tail sweep
    • Added rebuiltRange/sweepRebuiltTail checks and cursor advancement after renderBlock in both same-identity early-return paths of childSlot, matching the pattern componentSlotImpl already applies after every render.

Create PR

Or push these changes by commenting:

@cursor push d79e18cc0f
Preview (d79e18cc0f)
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
@@ -37103,6 +37103,11 @@
 			state.block.body = comp;
 			renderBlock(state.block);
 			state.currentComp = comp;
+			if (hydration !== null && !state.borrowed && state.end !== null) {
+				const rebuilt = hydration.rebuiltRange;
+				if (rebuilt !== null && rebuilt === state.start) hydration.sweepRebuiltTail(state.end);
+				hydration.node = getNextSibling(state.end);
+			}
 			return;
 		}
 		if (state.block !== null && comp === state.currentComp) {
@@ -37128,6 +37133,11 @@
 			if (state.block.props !== props) journalRootProperty(state.block, 'props', state.block.props);
 			state.block.props = props;
 			renderBlock(state.block);
+			if (hydration !== null && !state.borrowed && state.end !== null) {
+				const rebuilt = hydration.rebuiltRange;
+				if (rebuilt !== null && rebuilt === state.start) hydration.sweepRebuiltTail(state.end);
+				hydration.node = getNextSibling(state.end);
+			}
 			return;
 		}
 		const signalInstanceKey =

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

Comment thread packages/octane/src/runtime.ts Resolved
trueadm and others added 3 commits October 3, 2026 01:41
A <Hydrate> resume completes a range through whichever slot owns it, and
each slot kind needed its own sweep of a rebuilt root's server tail. The
root that is its range's whole content now takes the rest of that range
when it commits (rebuiltAt), while the cursor still rests right after the
node it replaces, so every slot kind and every resume path is covered.
The sweeps in settleClaim, componentSlotLite's replay and componentSlotImpl
are gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…closing range

#1622 lets claimRoots accept a markerless branch's claim (claimOwner), which
has no open marker of its own. A rebuilt root that is such a branch's
content recorded the node before it as its range's start, which can be an
enclosing range's open marker. The commit-time take then removed the nodes
after the branch up to that range's end, including a later call's server
node (NotFoundError). Only a claim on a marker range records rebuiltRange.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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 is ON. A cloud agent has been kicked off to fix the reported issue. You can view the agent here.

Reviewed by Cursor Bugbot for commit 1ca7bda. Configure here.

Comment thread packages/octane/src/runtime.ts Resolved
trueadm and others added 2 commits October 3, 2026 01:59
… it commits

A <Hydrate> resume claims the range again when the component that rebuilt
its single root suspended before committing it (a use() in setup, or in the
template after the clone), so the root still takes its range's tail when
the resume commits it. Pin that for lite and full slots.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cursor

cursor Bot commented Oct 3, 2026

Copy link
Copy Markdown

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

  • ✅ Fixed: Resume leaves inherited rebuilt tail
    • The suspendedClaim for an inherited call's wrapper block was never consumed on Hydrate resume because findSuspendedHydrateBlock returned null; fixed by checking for suspendedClaims in componentSlotImpl's re-render path and explicitly sweeping the rebuilt tail after re-rendering the block.

Create PR

Or push these changes by commenting:

@cursor push 27a76c90e8
Preview (27a76c90e8)
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
@@ -19378,6 +19378,20 @@
 		return this.unframedClaims?.get(block);
 	}
 
+	/** Consume a suspended adopted-range claim for `block` and sweep its rebuilt tail after re-render. */
+	resumeSuspendedClaim(block: Block): boolean {
+		const claim = this.suspendedClaims?.get(block);
+		if (claim === undefined) return false;
+		this.suspendedClaims!.delete(block);
+		const tail = this.rebuiltTail;
+		renderBlock(block);
+		if (tail !== null && this.rebuiltTail === tail) {
+			this.node = tail;
+			this.sweepRebuiltTail(block.endMarker!);
+		}
+		return true;
+	}
+
 	/**
 	 * Resume `source`, the block that a preserved activation suspended in. Inside
 	 * a component whose unframed render suspended, it renders without adopting,
@@ -32994,8 +33008,13 @@
 		if (state.block.props !== renderProps)
 			journalRootProperty(state.block, 'props', state.block.props);
 		state.block.props = renderProps;
-		if (claim === undefined) renderBlock(state.block);
-		else hydration!.renderUnframed(renderBlock, state.block, state.block, claim);
+		if (claim === undefined) {
+			if (hydration !== null && hydration.resumeSuspendedClaim(state.block)) {
+				// The block re-rendered and its rebuilt tail was swept.
+			} else {
+				renderBlock(state.block);
+			}
+		} else hydration!.renderUnframed(renderBlock, state.block, state.block, claim);
 	}
 	// Hydration: advance the cursor PAST this component's adopted range so the next
 	// sibling adopts from the right node. The body itself doesn't reliably leave the

diff --git a/packages/octane/tests/hydration/_fixtures/rebuilt-fragment-suspended-child.tsrx b/packages/octane/tests/hydration/_fixtures/rebuilt-fragment-suspended-child.tsrx
--- a/packages/octane/tests/hydration/_fixtures/rebuilt-fragment-suspended-child.tsrx
+++ b/packages/octane/tests/hydration/_fixtures/rebuilt-fragment-suspended-child.tsrx
@@ -178,6 +178,30 @@
 	</div>
 }
 
+function ParaCall(props: { leaf: Promise<string> }) @{
+	<Para leaf={props.leaf} />
+}
+
+export function RebuiltRootInheritedBranch(props: { server?: boolean; leaf: Promise<string> }) @{
+	<div>
+		<Hydrate split={false} when={load()}>
+			<section>
+				@if (props.server) {
+					<>
+						<BoldThenEm />
+						<Em />
+					</>
+				} @else {
+					<>
+						<ParaCall leaf={props.leaf} />
+						<Em />
+					</>
+				}
+			</section>
+		</Hydrate>
+	</div>
+}
+
 function BoldThenItalic() @{
 	<>
 		<b class="server">{'server'}</b>

diff --git a/packages/octane/tests/hydration/rebuilt-fragment-suspended-child.test.ts b/packages/octane/tests/hydration/rebuilt-fragment-suspended-child.test.ts
--- a/packages/octane/tests/hydration/rebuilt-fragment-suspended-child.test.ts
+++ b/packages/octane/tests/hydration/rebuilt-fragment-suspended-child.test.ts
@@ -217,6 +217,26 @@
 		expect(warnings()).toHaveLength(dev ? 1 : 0);
 	});
 
+	it('completes a child of a rebuilt root inside an inherited call, as when its data is ready', async () => {
+		const name = 'RebuiltRootInheritedBranch';
+		render(name, { server: true, leaf: fulfilled('unused') });
+		await hydrate(name, { leaf: fulfilled('z') });
+		const ready = markup(section());
+		root!.unmount();
+		recoverable = [];
+		errSpy.mockClear();
+
+		render(name, { server: true, leaf: fulfilled('unused') });
+		const leaf = pending();
+		await hydrate(name, { leaf: leaf.promise });
+		await act(async () => leaf.resolve('z'));
+
+		expect(markup(section().querySelector('p')!)).toBe('<em>z</em><s>s</s>');
+		expect(markup(section())).toBe(ready);
+		expect(recoverable).toEqual([expect.stringMatching(MISMATCH)]);
+		expect(warnings()).toHaveLength(dev ? 1 : 0);
+	});
+
 	it('keeps the root a fragment adopted after a hole whose call rebuilt its root, as when its data is ready', async () => {
 		const name = 'RebuiltHoleBranch';
 		render(name, { server: true, leaf: fulfilled('unused') });

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

@trueadm

trueadm commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author

The Bugbot Autofix above (branch cursor/resume-inherited-rebuilt-tail-8fb0, based on 7ad462b) is superseded and shouldn't be merged.

The "Resume leaves inherited rebuilt tail" finding was fixed in 9c2f392. That approach was then replaced by the commit-time take in ca0c638: a rebuilt root that is its range's whole content takes the range's tail in rebuiltAt, so no per-slot resume sweep is needed.

The Autofix's RebuiltRootInheritedBranch scenario is the same shape as this PR's InheritedRootBranch resume test, which passes on the current head (ead75c8).

🤖 Generated with Claude Code

@trueadm
trueadm merged commit 90c209e into main Oct 3, 2026
31 checks passed
@trueadm
trueadm deleted the claude/adoring-noether-cad2cc branch October 3, 2026 01:17
trueadm added a commit that referenced this pull request Oct 3, 2026
Resolve the conflict with #1614: its rebuilt-range tail removal
(rebuiltRange / takeRangeTail) now runs inside HydrationCapability.insertRoot,
which replaced rebuiltAt. A null from takeRangeTail still appends.

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.

1 participant