Skip to content

fix(octane): retire a moved early host binding when hydration replaces it - #1748

Merged
trueadm merged 2 commits into
mainfrom
fix/displaced-host-lease
Oct 5, 2026
Merged

trueadm merged 2 commits into
mainfrom
fix/displaced-host-lease

Conversation

@trueadm

@trueadm trueadm commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #1745.

A host bound with adoptBindings can be moved elsewhere inside the same root while its boundary is pending. On resume, hydration no longer finds the host at its cursor. It reports a recoverable mismatch and renders its own host at the original site. Before this change, the early lease was never matched, and commit-time retirement (retireDetachedBindingLeases) only retired leases whose host had left the container. The moved host therefore stayed subscribed, and later source notifications kept writing to it.

The fix takes the issue's "retire before the replacement commits" option:

  • dom-bindings.ts / dom-binding-handoff.ts: a scalar host lease gains an optional displaced(parent, node) check. It reports whether the renderer reached the host's server site (directly after the same authored neighbour, skipping renderer sidecars) and found another node or the end of the parent there. Nested component hosts are framed by their own block markers, so that neighbour identifies one instance.
  • runtime.ts: when a host-presentation component hydrates and no lease matches positionally, beginPresentationHydration looks for a displaced lease with the same id. It registers that lease's retirement on the attempt's capture. Committing the replacement retires the early owner (cleanup runs, listeners and subscriptions stop), just as an accepted handoff would. A discarded or re-suspended attempt leaves the early owner live. The moved DOM is never touched.

The unmoved control is unchanged: the binding still hands off in place. An invalid host that is still at the cursor still refuses takeover as before.

docs/deferred-hydration.md documents the new contract.

Validation

  • pnpm format:check (scoped pnpm format:files:check on every changed file passed)

  • pnpm typecheck (scoped pnpm typecheck:files on the changed source and test files passed)

  • pnpm test (not run locally; left to CI)

  • targeted tests: vitest run --project octane packages/octane/tests/binding-handoff-displaced-host.test.ts, 10/10 passing in dev and prod compiles:

    • the issue's scenario: the first boundary's handoff cleanup moves the second host into an <aside>;
    • a host moved directly while its boundary is pending, where a resumed attempt suspends again: the early owner stays live until the replacement commits;
    • a host moved out of a dormant <Hydrate> boundary that a later render activates;
    • an external clone left at the original site, which hydration adopts instead;
    • the unmoved control.

    Each moved case asserts that the cleanup ran once, a later notification no longer writes to the moved element, the moved element stays where it was moved, and a later root render updates only the renderer's host.

  • Pre-fix failure: with the runtime call disabled, all 8 moved cases fail (cleanup never runs, the moved host takes the late notification) and both controls pass. Removing the discarded-attempt guard fails only the re-suspend case in both modes.

Risk / follow-ups

  • The new path runs only when a root has bindingLeases and a host-presentation component hydrates without a positional lease. It is a cold loop over that root's leases. No bundle-size app includes beginPresentationHydration; the early scalar adopter grows by one small closure.
  • Overlap: Hydration can loop when an early host binding is followed by stale server content #1743 (hydration loop with an early host binding plus stale server content) is being fixed in a separate session in the presentation retry path. This PR only touches the no-lease branch of beginPresentationHydration. Whichever lands second rebases.

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 deferred hydration and early-binding retirement in edge cases where DOM moves during pending boundaries; incorrect matching could retire the wrong lease or leave stale writers.

Overview
Fixes stale early binding subscriptions when a scalar host is moved inside the root while its hydration boundary is still pending. On resume, hydration treats the host as missing at the original site, reports a recoverable mismatch, and renders a replacement at that site—but the displaced host’s lease could stay active, so later source notifications kept updating the moved DOM.

Scalar host leases now expose an optional displaced(parent, node) check (server site identified via the same authored sibling, skipping renderer sidecars). When host presentation hydration finds no positional lease, supersedeDisplacedHost looks for a displaced lease with the same id and schedules retire() on commit of the replacement—matching accepted handoff cleanup without touching the moved element. Discarded or re-suspended attempts leave the early owner live.

Deferred hydration docs describe this contract. New integration tests cover moved hosts (including re-suspend, dormant <Hydrate> activation, and an external clone at the site). Staged-DOM checks whitelist parentNode reads in the new runtime path.

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

…s it

A host leased through adoptBindings can be moved elsewhere inside the root
while its boundary is pending. On resume, hydration no longer finds the host
at the cursor, reports a recoverable mismatch, and renders its own host at
the original site. The early lease was never matched, and commit-time
retirement only checked container containment, so the moved host kept its
subscription and later notifications wrote to it.

The host lease now reports whether the renderer reached its original site
(after the same authored neighbour) without finding it there. When a
host-presentation component hydrates without a positional lease, it looks
for such a displaced lease and retires it when the replacement's capture
commits. A discarded or re-suspended attempt leaves the early owner live,
and the moved DOM is never touched.

Fixes #1745

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:33
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

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

Compares d7ee29b77, the merge commit's first parent with fb9ca7136 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

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 2.900 2.892 ±0.0% −1.5% … +3.0% ⚪ within noise
octane-tsrx replace 6.508 6.492 +1.5% −1.5% … +3.7% ⚪ within noise
octane-tsrx add 2.525 2.533 ±0.0% ±0.0% … +3.8% ⚪ within noise
octane-tsrx update 0.123 0.119 −0.7% −2.1% … +0.7% ⚪ within noise
octane-tsrx select 0.005 0.005 −0.5% −2.1% … +2.1% ⚪ within noise
octane-tsrx swap 0.035 0.034 +0.4% −0.8% … +1.7% ⚪ within noise
octane-tsrx remove 0.019 0.020 −0.0% −1.2% … +2.2% ⚪ within noise
octane-tsrx runlots 25.533 26.167 +3.6% +0.0% … +6.8% ⚪ within noise
octane-tsrx select_lots 0.006 0.007 −0.6% −3.3% … +4.9% ⚪ within noise
octane-tsrx clear 49.383 47.683 −2.2% −13.5% … +6.9% ⚪ within noise
octane-jsx run 4.617 4.350 −2.0% −4.2% … +2.1% ⚪ within noise
octane-jsx replace 5.983 6.100 +2.5% −4.0% … +6.8% ⚪ within noise
octane-jsx add 3.217 3.125 ±0.0% −3.1% … +0.0% ⚪ within noise
octane-jsx update 0.270 0.260 −0.4% −1.5% … +1.1% ⚪ within noise
octane-jsx select 0.138 0.145 +0.2% −0.5% … +1.7% ⚪ within noise
octane-jsx swap 0.243 0.240 −0.2% −2.4% … +2.8% ⚪ within noise
octane-jsx remove 0.194 0.203 +2.1% ±0.0% … +12.9% ⚪ within noise
octane-jsx runlots 32.800 32.750 +1.4% −1.8% … +3.4% ⚪ within noise
octane-jsx select_lots 3.340 3.382 −0.8% −8.5% … +4.3% ⚪ within noise
octane-jsx clear 47.192 48.017 +1.9% −1.3% … +6.8% ⚪ 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,760 +441 (+0.1%) 🔴
octane-tsrx full_fw_gzip 174,423 174,555 +132 (+0.1%) 🔴
octane-tsrx full_fw_brotli 147,294 147,335 +41 (+0.0%) 🔴
octane-jsx full_fw_raw 569,319 569,760 +441 (+0.1%) 🔴
octane-jsx full_fw_gzip 174,423 174,555 +132 (+0.1%) 🔴
octane-jsx full_fw_brotli 147,294 147,335 +41 (+0.0%) 🔴

50 other measured values are unchanged.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@trueadm
trueadm merged commit 5041f00 into main Oct 5, 2026
50 of 56 checks passed
@trueadm
trueadm deleted the fix/displaced-host-lease branch October 5, 2026 10:05
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.

A moved early-bound host can keep publishing after its pending boundary hydrates a replacement

1 participant