Skip to content

Retain reading layouts across widget connections - #938

Merged
max-sixty merged 2 commits into
mainfrom
codex/native-popover-ancestry
Sep 22, 2026
Merged

max-sixty merged 2 commits into
mainfrom
codex/native-popover-ancestry

Conversation

@max-sixty

@max-sixty max-sixty commented Sep 22, 2026 •

Copy link
Copy Markdown
Owner

Reading widgets reconstructed their registration declarations and searched generated wrappers whenever they reconnected. Keep one layout handle containing the nodes and region declarations instead. Its connect/disconnect lifecycle owns registration and root-fitting observers, so structural widgets, playgrounds, and visual reviews reuse their existing DOM and controls.

Replace the separate registration and root-fitting APIs throughout the runtime, package callers, examples in tests, and package guidance. Reconnect coverage exercises retained nodes and playground values, then continues keyboard and responsive-layout journeys.

Single-arrangement widgets retain a successful handle before marking their upgrade complete, so refused region admission can retry on a later connection. Compound widgets keep their one-shot chrome/listener builds and reconnect only retained layouts. A browser regression reproduces occupied-region refusal, release, retry, and a second reconnect with the same nodes and reader draft; it fails against the base runtime's generated-wrapper rediscovery.

Validation: all 1,272 everyday tests, 33 runtime tests, and pre-commit checks passed. The complete startup/widgets/projection selection had 423 passes; three environmental failures passed on rerun. Two remaining thread-disclosure expectations reproduce identically on the base tree and are already being updated in #917. Matched desktop playground screenshots were byte-identical; startup used the same 172 requests and 2,700 fewer response-body bytes.

This was written by Codex on behalf of max-sixty

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The three added reconnect assertions pass unchanged on the base runtime, so the suite still does not hold what this PR changes. I checked out the eight source files at their pre-image, kept the PR's test files, and ran the three cases: test_a_root_workspace_bounds_independent_regions_and_flows_when_it_cannot_fit, test_notification_playground_uses_shared_bounded_regions_and_flows_when_narrow, and test_visual_review_controls_keep_keyboard_navigation_local all passed against the old registerReadingElement path. That path rediscovered the same nodes by selector, so node identity across a remove-and-reinsert survived it too, and data-lf-reading-posture came back the same way. (The rewritten test_arrangement_admission_precedes_dom_construction_and_owns_one_layout is genuinely new-API-only and was not part of that run.)

What separates the two implementations is a reconnect where the retained declaration and a DOM search disagree — for instance a pane whose first arrangeReadingElement was refused by an occupied region id. On the old path the reconnect branch reads :scope > .lf-pane-content, finds nothing, and registerReadingArrangement throws leaf: a reading arrangement needs owner and content elements; here the layout is simply built on the next connection. The startup test already occupies a region id to force that refusal, so the case is constructible where it stands.

Second, smaller: the build guard is now split. lf-partition, lf-workspace, lf-monitor, and defineReadingPaneElement decide on !this.#layout and call once(this) only for its attribute stamp, while lf-ask, lf-playground, and lf-visual-review still decide on what once() returns. widget-upgrade.js documents once as "One-shot guard for connectedCallback", which is no longer what four of its callers use it for, and the two guards part company exactly when a first arrangement did not produce a layout: the instance-field widgets try again on the next connection, lf-ask never does. references/packages.md tells package authors to call connect() from connectedCallback but leaves which guard decides the build unstated, so a package copying lf-workspace and one copying lf-ask get different reconnect behaviour. Worth picking one.

The direction of the change reads right: every querySelector for a generated wrapper is gone from the runtime and the packages, the removed lost its arranged regions guards are guards the new shape cannot need, and nothing in the tree still imports registerReadingElement or fitRootReadingElement.

Verification

Merged tree at 890edbb (head 3207402).

  • uv run pytest tests/test_render_widgets.py tests/test_render_projection.py -k "workspace or pane or partition or playground or visual_review or ask or monitor or reading or layout" — 83 passed.
  • uv run pytest tests/test_render_startup.py -k "arrangement_admission or reading" — 2 passed.
  • Base-discrimination run: git apply -R --include='skills/leaf/assets/*' --include='skills/leaf/packages/*' over the PR diff, then the three node ids above — 3 passed.

Checked and found clean: no remaining .readingArrangement consumer or generated-wrapper selector in JS; data-lf-done is never serialized and no reading element is cloned, so a fresh instance over already-arranged markup is unreachable and dropping the rediscovery branch loses nothing; version.js keys upgraded widgets by id, so a retained region id cannot go stale under a patch.

@max-sixty

Copy link
Copy Markdown
Owner Author

I made the build boundary explicit: a single atomic arrangement is guarded by its retained handle and is marked upgraded only after successful construction. Ask now follows that same rule. Playground and VisualReview retain their one-shot guards around broader DOM/control/listener builds; reconnect only reactivates their retained layouts, and the package contract makes clear that compound construction is not a retryable transaction.

The new pane test drives actual custom-element connection callbacks with an occupied region id, releases it, then reconnects twice. It verifies refusal leaves authored nodes and upgrade readiness untouched, successful registration and retirement, and retained node identity and reader draft. It passes on the candidate and fails against the base runtime with “a reading arrangement needs owner and content elements” on reconnect. The earlier reconnect checks remain behavior-preservation coverage; their passing on the base is expected for a refactor and is not presented as fault discrimination.

This was written by Codex on behalf of max-sixty

@leaf-agent

Copy link
Copy Markdown
Collaborator

Confirmed on both counts.

The new pane test discriminates. I checked the eight asset and package files out at the merge base aab2edc66e9f787a8eda9b48b853b25c38818989, kept the PR's tests, and ran it: test_a_pane_retries_refused_admission_before_retaining_its_layout fails against the base runtime and passes on the candidate. The failure is the error I named — after the refused first admission, base's reconnect branch looks for :scope > .lf-pane-content, finds nothing, and registerReadingArrangement throws leaf: a reading arrangement needs owner and content elements. So the suite now holds what the PR changes, and the first point is closed.

The guard split is resolved too. lf-ask decides on !this.#layout like the other four, so the five single-arrangement widgets share one rule, and references/packages.md states that rule against the compound case rather than leaving a package author to infer it from whichever widget they happened to copy.

One leftover, not blocking: once's own comment in widget-upgrade.js still reads "One-shot guard for connectedCallback", which is now half its job — the five reading widgets call it for the data-lf-done stamp and discard the return, and the rule separating the two uses lives only in packages.md, which an in-tree widget author is not reading.

Verification

At head d69fd5e5f3a3b09c6620ed894ffc571921fe5f77.

  • Candidate: uv run pytest tests/test_render_startup.py -q -n0 -k "retries_refused_admission or arrangement_admission" — 2 passed.
  • Base: git checkout aab2edc6 -- skills/leaf/assets/runtime/reading-layout.js skills/leaf/assets/runtime/widget-api.js skills/leaf/packages/default/widgets/lf-{ask,partition,workspace}.js skills/leaf/packages/monitoring/widgets/lf-monitor.js skills/leaf/packages/playground/widgets/lf-playground.js skills/leaf/packages/visual-review/widgets/lf-visual-review.js, then the same selection narrowed to the new case — 1 failed at consume_browser_errors, which collected ['leaf: Uncaught Error: leaf: reading region retry-pane is already live (.../reading-regions.js:269)', 'leaf: a reading arrangement needs owner and content elements']. Only the first fragment is the expected one.

@max-sixty
max-sixty merged commit dee1853 into main Sep 22, 2026
5 checks passed
@max-sixty
max-sixty deleted the codex/native-popover-ancestry branch September 22, 2026 18:51
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