Repository navigation
fix(octane): retire a deletion's hosts before its cleanups run - #1627
Conversation
Deletion cleanups run parent first. A dialog that restored focus in its own layout cleanup started onBlur on an input owned by a child component the teardown had not reached yet, and the same happened for a value-hole input in a removed @for row: #1548 retired each Block only when its own unmount began. Replace #1548's per-host owner claim and per-Block epoch with #1549's DOM-root stamp, applied before any cleanup at every committed deletion: the deferred range removal, parked keyed rows, root.unmount, and a portal's own content. An inert list clear retires only the row holding the scheduler's captured focus, and parked rows are stamped only when focus was captured, so an unfocused bulk removal does no per-row work. Handler publication no longer writes an owner expando on every host. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Benchmark report
Compares js-framework: 1k/10k row operations (ms, lower is better)
All 20 timed operations (16 within noise)
Application bundle size (bytes)
7 other measured values are unchanged. Public-import reachability bundles (bytes)
35 other measured values are unchanged. |
…eardown The owned-list clear read row.parentNode directly; read it through domNode so the staged-DOM boundary check classifies it (lint checks). Stamp a portal's own range in unmountBlockInner rather than only in teardownPortalState, so a portal torn down through unmountSlot's portal branch, such as one detaching under a retained hydration parent, retires its content before its cleanups run too. A non-parkable list clear stamps each row before its cleanup under the same captured-focus rule as parked rows. Add a regression for a portal compiled in a template. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
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: List clear stamps rows too late
- Split batchClearItems into two passes—first retire all rows' host ranges, then unmount—so an earlier row's cleanup cannot dispatch events on not-yet-retired hosts in later rows.
Or push these changes by commenting:
@cursor push b003c91ccb
Preview (b003c91ccb)
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
@@ -46519,13 +46519,21 @@
const retire = renderingFocus !== null || !inFlush;
TEARDOWN_DEPTH++;
try {
+ // Retire every row's hosts BEFORE any row's cleanups run: an earlier
+ // row's layout cleanup can move focus, which dispatches on a later row's
+ // hosts. If those hosts are not yet retired, the handler runs against a
+ // scope the clear is about to dispose.
+ if (retire) {
+ for (let b: Block | null = state.head; b !== null; b = b.nextSibling) {
+ retireHostRange(b.startMarker, b.endMarker);
+ }
+ }
// Dispose the items before their DOM leaves, like every other deletion:
// cleanups observe attached hosts. Walk the intrusive item chain (head →
// nextSibling) rather than the Map's iterator: zero allocation and a
// monomorphic pointer chase. Callers reset head/tail only AFTER this returns,
// so the chain still covers exactly the old items here.
for (let b: Block | null = state.head; b !== null; b = b.nextSibling) {
- if (retire) retireHostRange(b.startMarker, b.endMarker);
if (b.cleanups !== null || b.children !== null || b._slots !== null) {
unmountBlock(b, false);
} else {You can send follow-ups to the cloud agent here.
… cleanup batchClearItems stamped each row just before that row's own teardown, so an earlier row's cleanup that moved focus could still start onBlur on a later row the same clear was removing. Stamp the whole span between the list's markers first, under the same captured-focus rule as parked rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…gment owner unmounts Stamping every portal Block's range in unmountBlockInner also stamped a foreign portal range parked between a Fragment owner's markers, so a click on that still-mounted portal in the same task started no handler (conformance/fragment-future-children). Retire a portal's range in teardownPortalState again: a portal compiled into a template routes its events through a host element inside its owner, which the enclosing deletion has already retired before any cleanup runs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every transactional blockless de-opt removal now commits through deferRootRange, which retires its nodes before their cleanup runs, so the stamp detachDeoptTreeRefs added for #1549 repeats it. Removing it returns detachDeoptTreeRefs to its single recursive form and keeps root-chained-jsx within the budget main tightened to 84,352 raw bytes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Bugbot Autofix prepared fixes for both issues found in the latest run.
Or push these changes by commenting: Preview (0ff26217f3)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
@@ -4107,6 +4107,8 @@
if ((STAGED_DOM?.view(node) ?? node).parentNode !== parent)
(STAGED_DOM?.view(parent) ?? parent).insertBefore(node, anchor);
}
+ // Retire the outgoing nodes before any cleanup can move focus off them.
+ nodes.forEach(retireEventHostTree);
const previousSuppression = REF_DETACH_SUPPRESSION;
REF_DETACH_SUPPRESSION = suppressedRefs;
try {
@@ -12367,6 +12369,8 @@
// Portal DOM lives in a FOREIGN target — the root-level batched clear
// never reaches it, so portals must always self-detach individually.
const childDetach = k === 'portalSlotSlot' ? true : detachDom;
+ // Its DOM lies outside every enclosing deletion's range: retire it here.
+ if (k === 'portalSlotSlot' && val.block) retireHostRange(val.start, val.end);
if (val.block) unmountBlock(val.block, childDetach);
if (k === 'portalSlotSlot' && val.target) {
unregisterPortalEventRange(val.target, val);
@@ -46516,6 +46520,12 @@
}
TEARDOWN_DEPTH++;
try {
+ // Retire the focused row before any cleanup can blur it: only removing a
+ // focused row dispatches focusout during a synchronous batch clear.
+ const p = domNode(state.start).parentNode!;
+ let row: Node | null | undefined = (renderingFocus as FocusSelectionSnapshot | null)?.focused;
+ while (row != null && row.parentNode !== p) row = row.parentNode ?? (row as ShadowRoot).host;
+ if (row != null) retireEventHostTree(row);
// Dispose the items before their DOM leaves, like every other deletion:
// cleanups observe attached hosts. Walk the intrusive item chain (head →
// nextSibling) rather than the Map's iterator: zero allocation and a
@@ -46534,7 +46544,6 @@
b.disposed = true;
}
}
- const p = domNode(state.start).parentNode!;
if (domNode(state.start).previousSibling === null && getNextSibling(state.end) === null) {
// forBlock owns the parent — nuke everything in one DOM op, then re-add markers.
(STAGED_DOM?.view(p as Element) ?? (p as Element)).textContent = '';You can send follow-ups to the cloud agent here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: De-opt teardown no longer retires hosts
- Restored the root-level
retireEventHostTree(node)call insidedetachDeoptTreeRefsfor teardown paths (out === null), ensuring non-transactional removals (clearChildContent immediate sweep, clone() adoption, unmountSlot hostNode teardown, and reconciler else-branches) retire hosts before detach so focusout cannot fire on a non-retired node.
- Restored the root-level
Or push these changes by commenting:
@cursor push 34762d6738
Preview (34762d6738)
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
@@ -44438,7 +44438,11 @@
ownerScope?: Scope,
uncommitted: UncommittedRefAttaches | null = null,
activityRefs: WeakMap<Element | FragmentInstance, ActivityRefState> | null = null,
+ // Only the removed subtree's root carries the retirement stamp.
+ root: boolean = true,
): void {
+ // Teardown runs just before every blockless de-opt removal detaches `node`.
+ if (root && out === null) retireEventHostTree(node);
// No de-opt descriptor ref was ever stamped → nothing to detach or collect
// anywhere; skip the subtree scan. (Monotone flag — see noteDeoptRef.)
if (!DEOPT_REFS_STAMPED) return;
@@ -44472,7 +44476,7 @@
c = nodeAfterPortalRange(c, rangeEnd);
continue;
}
- detachDeoptTreeRefs(c, out, shouldDetach, ownerScope, uncommitted, activityRefs);
+ detachDeoptTreeRefs(c, out, shouldDetach, ownerScope, uncommitted, activityRefs, false);
c = getNextSibling(c);
}
}You can send follow-ups to the cloud agent here.
Reviewed by Cursor Bugbot for commit 29ba34b. Configure here.
| uncommitted: UncommittedRefAttaches | null = null, | ||
| activityRefs: WeakMap<Element | FragmentInstance, ActivityRefState> | null = null, | ||
| ): void { | ||
| // Teardown runs just before every blockless de-opt removal detaches `node`. |
There was a problem hiding this comment.
De-opt teardown no longer retires hosts
Medium Severity
detachDeoptTreeRefs no longer stamps a host before teardown detach. Immediate blockless removals — value-hole hosts outside a root transaction, clearChildContent's non-deferred sweep, and clone() adoption replaceChild/replaceChildren — can dispatch focusout at a host that still starts its handler.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit 29ba34b. Configure here.
There was a problem hiding this comment.
Intentional trade-off in 29ba34b, pending a maintainer decision, so I'm leaving this open. Restoring the hook costs about 27 raw bytes on root-chained-jsx, which is past the 84,352 budget #1637 set; main alone measures 84,344.
What is actually exposed without it:
- Non-transaction branches. The 'outside a root transaction' value-hole removals and
clearChildContent's immediate sweep only run without a root transaction, and createRoot renders always open one. Their transactional twins commit throughdeferRootRange, which stamps the same nodes before the cleanup runs. clone(). It replaces mismatched server nodes during hydration adoption, before Octane publishes any handler on them, so no handler can start.- Reachable gap. Two render-time de-opt removals bypass
deferRootRange: a mapped item switching from a host to a component (mappedDeoptItemBody), and thedeoptItemBodytransfer fallback. Both are listed in the PR's risk section.
If the budget can move one 32-byte step, the hook goes back as it was.
🤖 Addressed by Claude Code
|
Not adopting the Bugbot Autofix branch (
🤖 Addressed by Claude Code |



Summary
#1548 and #1549 were two mechanisms for one defect (#1472): a focused host's removal, or a deletion cleanup that moves focus, dispatches
focusoutsynchronously at hosts being torn down. This keeps only #1549's DOM-root stamp and applies it where a deletion is committed, before any of its cleanups run. The stamp therefore covers child components the teardown has not reached yet.That fixes E1 from the 2026-10-02 review. Deletion cleanups run parent first, so under #1548's per-Block epoch:
onBluron an input owned by a child<Field>;@forrow did the same when an earlier component in the row moved focus.Removed (#1548):
Block.retired, the$$eventOwnerwrite on every handler publication and its prototype seed, the four inline stamps, the dispatch owner check, andDeferredLayoutDriver.retireHosts.Stamp sites. Root transactions defer deletions into several commit forms. Some
detachDom=falseteardowns retain their DOM (hydration retries, adoption), so stamping by Block markers at the outermost teardown is not safe. Each committed removal stamps its own saved nodes instead:deferRootRangecommit stamps itsnodes;unmountParkedItemstamps the row, only when the scheduler captured focus for this pass (or when called outside a flush). An unfocused bulk removal does no per-row work.root.unmountstamps the container's content;teardownPortalStatestamps a portal's own range: its DOM sits outside every enclosing range, and a returned portal's logical parent is the live host. A portal compiled into a template routes its events through a host element inside its owner, which the enclosing deletion has already stamped. (Stamping every portal Block inunmountBlockInnerinstead also stamped an independent portal parked inside a Fragment owner's markers.)The
deferRootUnmountcommit is deliberately unstamped: its nodes already left the document (with any focus) during render, or the caller clears them at render time.Kept (#1549): the
$$retiredEpochstamp, the per-task window, the outermost-stamp path scan, the tag-swap stamp and the ViewTransition staging.stampRetiredHostTreeis merged intoretireEventHostTree.Removed (#1549): the blockless de-opt hook in
detachDeoptTreeRefs(and itsdetachDeoptSubtreeRefssplit). Every transactional blockless removal commits throughdeferRootRange, which now stamps its nodes before their cleanup. The removal also keeps root-chained-jsx within the 84,352-byte raw budget main set in #1637.Also:
docs/differences-from-react.mdnow mentions deletion cleanups that move focus, and a changeset is added.Size
Measured locally against main 0d91b93, which is two hydration commits behind the current base; the gated budgets were re-checked on the current base.
Full applications, framework chunk (
bundle-size/run.mjs, octane targets):Minimal scenarios (
run-minimal.mjs):The review estimated −80…−150 gzip per app. In practice the per-commit-site stamps cost about what #1548's claim did, so the result is about −75 raw and roughly flat gzip/brotli (within ±60). Handler-free bundles never paid for the removed claim, so they grow slightly.
CI-gated budgets (
--budgets root-static-local root-chained-jsx hooks-state prop-attributes context) pass on the current base. root-chained-jsx is 84,347 / 27,875 / 24,838 against 84,352 / 27,904 / 24,864 at 29ba34b, with main at #1637. Main alone measures 84,344 raw there.Timing (for CI to confirm, not measured locally):
clearon js-framework rows should return to its pre-fix(octane): skip retired hosts when removal dispatches focusout #1548 cost: one focus lookup per clear instead of an epoch store per row.The ViewTransition effect-cleanup call gate should be unchanged. An instrumented local count showed 0 extra calls for an unfocused 100-row removal and an unfocused 1,000-row clear of effectful rows, and 1 call per removed row when something is focused.
Validation
pnpm format:check(scopedpnpm format:files:checkon changed files: clean)pnpm typecheck(scopedpnpm typecheck:fileson changed files: clean)pnpm test(not run locally per machine-load rules; CI covers it)octaneandoctane-prod:retired-event-hosts,discrete-flush-reentrancy,portal-events,view-transition-*,focus-*,for-batch-clear,keyed-removal-lifetime,deopt-*,portal-*,unmount-detach: 37 files / 435 tests in each project;tests/retired-event-hosts.test.ts. The first three fail on main in dev and prod:['field', 'parent']);['row', 'parent']);['field', 'parent']; fix(octane): skip retired hosts when removal dispatches focusout #1548 also missed this);tests/browser/retired-event-hostscover the three E1 orderings plus a 20-row inert list clear with a focused row. They were not run in a browser locally. I validated all 34 browser scenarios under a jsdom harness that emulates Chromium's synchronous blur-on-removal, in dev and prod. Under that harness the three new E1 scenarios fail on main, and disabling any one stamp site fails at least one scenario.Risk / follow-ups
onBlur; fix(octane): skip retired hosts when removal dispatches focusout #1548 had the same per-row order. Stamping the whole batch first fixes it (a jsdom test confirms:['row 2', 'parent']today). It costs about +20 gzip / +27 brotli in root-chained-jsx, which is past that bundle's committed budget, so it is left for a separate decision. The non-parkablebatchClearItemspath already stamps its whole span first.ScopeDisposedError.deferRootRangeno longer stamp: a mapped item switching from a host to a component, and the fallback when a moved de-opt host has no receiving slot. The same applies to the non-transactionelsebranches, which createRoot trees do not reach. Restoring the hook costs about 27 raw bytes, past that budget. Equivalent spellings move brotli by ±60, so a concurrent PR can tip it. The in-flight A7 work (closeNativeEventBatchgating) measures −133 brotli there.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.Note
Medium Risk
Changes teardown ordering and delegated focus/blur dispatch across lists, portals, and root unmount; behavior is heavily regression-tested but touches core commit paths.
Overview
Fixes #1472 by retiring removed DOM hosts for delegated events before any deletion cleanups run, so a parent layout cleanup that restores focus no longer runs
onBluron inputs owned by child components (or value-hole rows) that teardown has not reached yet.The PR drops block-level retirement (
Block.retired,$$eventOwneron handler publish,retireHosts) and relies on$$retiredEpochstamps on detached node roots at each committed removal path (deferRootRange, focused parked rows, root unmount, portal ranges, list clears). Dispatch skips handlers under the outermost stamp in the capture path; live siblings and ancestors still fire.Docs note deletion cleanups that move focus; new jsdom and Chromium scenarios cover dialog/portal/
@forfocus-restore orderings and focused list clears.Reviewed by Cursor Bugbot for commit 29ba34b. Bugbot is set up for automated code reviews on this repo. Configure here.