Skip to content

Release a replaced widget's lifetime before its replacement starts - #2573

Merged
max-sixty merged 2 commits into
mainfrom
agent-ab183802b624f2a4b
Oct 12, 2026
Merged

max-sixty merged 2 commits into
mainfrom
agent-ab183802b624f2a4b

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

A revision that replaces a primary Thread reader with another one no longer throws "The page already has a Thread presentation". The thread draft, its selection and the frozen widgets carry across to the replacement again. This fixes review item #70: test_primary_reader_revision_replaces_owner_without_losing_thread_draft was failing on main.

Cause

Bisect over main (781f2da..6b1210e, running only that test) names #2503 (d57bf19) as the first bad commit; its parent bb47f5f passes. #2503 moved lf-conversation-workspace's registrations off connectedCallback/disconnectedCallback and onto whileConnected. whileConnected's connect runs synchronously, but watchOwner decides a departure only at the next arrival mutation batch. patchTree evicts the replaced element and then inserts its replacement in the same task, so the replacement's start ran while the evicted element still held its registration. The single-holder registries (thread/destination.js register, thread/presentation.js registerPrimary) then refused it. dom-move.js appears in the stack only because it is the insertion that connects the element. #2545, #2552 and #2565 did not cause it; #2552's PR notes that the test already failed without it.

Fix

This is fixed in the lifetime primitive, not in each registry. Two changes in arrivals.js:

  • The offer that watchArrivals returns now reads the pending mutation records (takeRecords) before it decides its element. A decision made now therefore never runs ahead of a departure already made. watchOwner offers every owner as it connects, so whatever left before has released first.
  • offerMany now decides every departure in a batch before any arrival in it. Each mutation record lists its added nodes before its removed ones, so before this, an owner returning through the batch (old.replaceWith(returning)) started before old released.

A move within one batch still keeps its resources. An element that is removed, and then re-added after another owner connected in between, now leaves and comes back, which is what disconnectedCallback did before #2503. The arrivals.js header and module-authoring.md ("Connection lifetimes") now state this rule. The single-holder checks in the registries stay as they are, since they guard a real invariant: two primary readers standing at once.

Why CI didn't catch it

The test is in the nightly tier (tests/cost_tiers.json: it costs more than 8 s on CI's runner). A pull request's gate adds later-tier tests only when the change edits their own lines (--changed-since), and #2503 did not edit this one. The last scheduled run started on 2026-10-11 at 10:53 UTC, before #2503 landed, so no nightly run has executed the test since the break. The three most recent scheduled runs (10-09, 10-10 and 10-11) were all cancelled, so the nightly tier has not reported for three days.

Measured (item #70)

  • Hand-written source: arrivals.js +26 / −16. Of that, code is +15 / −12 and the header comment is +11 / −4. The Markdown change is excluded.
  • Tests: +35 / −0. That is one Node runtime test, an owner replaced within one batch releases before its replacement starts, with two cases. The case of a new owner fails on main with "already held". The case of a returning owner fails the same way with only the takeRecords change applied. Both pass here.

Validation

  • The failing browser test passes.
  • pnpm run test:runtime passes (368 tests).
  • 55 focused primary-reader, presentation, draft and workspace tests in test_render_application_boundary.py and test_render_threads.py pass, across all tiers.
  • I ran 332 lifetime, return, mirror, carry, workspace, reading-region and code-copy tests across tests/, in all tiers (-k "returns or returning or lifecycle or lifetime or disconnect or mirror or revision_replaces or retains or carry or primary_reader or workspace or external_link or reading_region or code_copy"). All pass except test_render_anchors.py::test_code_copy_enter_leaves_nested_links_usable, which this change does not cause. Under -n 4 that test fails 2 runs in 6 with copyAtPresentation == 2, both with main's arrivals.js and with this branch's.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 2 commits October 12, 2026 03:36
A revision that replaces a primary Thread reader with another one no
longer throws "The page already has a Thread presentation", and the
thread draft, selection and frozen widgets carry to the replacement
again.

#2503 moved widget registrations from connectedCallback and
disconnectedCallback onto whileConnected. Its connect runs
synchronously, but a departure is decided at the next arrival mutation
batch. patchTree evicts the old element and then inserts its
replacement within one task, so the replacement's start ran while the
evicted element still held its registration, and any registration only
one element may hold refused it.

watchArrivals' explicit offer now reads the pending mutation records
before deciding its element, so a decision made now never runs ahead of
a departure already made. Every owner connects through that offer, so
whatever left before it has released first. A move within one batch
still keeps its resources; an element removed, then re-added after
another owner has connected in between, now leaves and returns, as it
did under disconnectedCallback.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
An owner returning through a mutation batch, as in old.replaceWith(R),
was connected before the element it replaced released, because each
record lists its added nodes before its removed ones. offerMany now
decides every departure in the batch first, so the release-first rule
holds for returning owners too, not only for new ones.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 0a39c67 into main Oct 12, 2026
3 checks passed
@max-sixty
max-sixty deleted the agent-ab183802b624f2a4b branch October 12, 2026 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants