Skip to content

Refuse plain moves of placed nodes, and keep an editor's caret through a native move - #2545

Merged
max-sixty merged 4 commits into
mainfrom
agent-aa59d82fedcd90841
Oct 12, 2026
Merged

max-sixty merged 4 commits into
mainfrom
agent-aa59d82fedcd90841

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Review item 52 of the Lit follow-up round: a lint rule now refuses re-parenting a placed node with a plain insertion, every site it finds goes through move(), and a focused comment box keeps its caret through a native move, which it lost in Chrome on main.

The rule (architecture/moves, eslint.config.mjs)

  • insertBefore and moveBefore are refused outside dom-move.js, unless the node is made in the same expression (createElement, cloneNode, ...).
  • append, prepend, before, after, appendChild, replaceChildren and replaceWith are refused only where a condition around the call asks where the node stands: its parent or a sibling, or whether something contains it. The variants, measured on main over the runtime and packages:
Variant for the append family Sites flagged Real re-parents
every call with a non-literal argument 282 mostly new nodes
minus nodes made in the expression, a local binding, or el/offer 159 mostly new nodes
a holdFocus in the same function 9 4
a condition reads where the node stands (chosen) 12 11

The 12th, in lf-trace, appended its fresh replacement under the old name and now names it.

  • The rule sits beside architecture/placements and has a row in skills/leaf/assets/AGENTS.md's owner table. tests/runtime/moves-rule.test.mjs pins what it refuses and admits.

What the rule found, fixed through move() or setChildren

  • 20 insertBefore sites and 11 guarded appends across the runtime, the annotation overlay and the default widgets.
  • The key chip's seat and a failed diff file's body use setChildren. The diff's replaceChildren re-inserted the file's summary, which blurred it if focused.
  • Moves that held focus themselves now call move(): the margin rows' lane, carrier and motion-layer moves (moveHolding, moveHost and the rows' move option are deleted), the inline contributions' host, and the floating composer's motion frames.

Behaviour change: a focused leaf-text kept focus but lost its caret on a native move. moveBefore collapses the selection inside the field's closed shadow tree, and CodeMirror read it back a few frames later as a caret at the start, so the next keystroke landed there. The field's reparent hold restored the caret on the first selectionchange, which came before CodeMirror's read. leaf-text now defines connectedMoveCallback, so a native move is no disconnection, and puts its recorded selection back as it lands (LeafEditorView.placeSelection, split out of focus()). The hold and the test that called connectedCallback directly are deleted.

Response-bar test. test_the_comment_box_keeps_the_user_typing_as_it_takes_its_seat_again folds and reopens a diff file while the user writes a line comment, so the bar is re-seated in the line's outlet, then types. With moveBefore it fails on main (the words land at the start, 3 of 3 runs) and with the pre-#2509 append seat (the editor blurs). Without moveBefore, the hold path keeps focus and caret.

Lines (git diff --numstat) Added Removed
Hand-written source, excluding tests, vendored bundles and Markdown 223 154
of which the lint rule 125 0
Tests 117 34

Checks run: eslint over the runtime and packages, pre-commit on the changed files, pnpm run test:runtime, and the focused browser tests for the response bar, retained moves, diff outlets and seating, code copy, the margin's lane and focus cases, floating composers and the touched widgets.

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

max-sixty and others added 3 commits October 12, 2026 00:23
…h a native move

A lint rule, `architecture/moves`, refuses the runtime's and packages' own
insertions of a node that may already stand somewhere, so they go through
`move()` (runtime/dom-move.js) or `setChildren`, which keep the user's
focus, caret and native state in the node.

What the rule refuses:
- `insertBefore` and `moveBefore` outside dom-move.js, unless the node is
  made in the same expression (`createElement`, `cloneNode`, ...).
- `append`, `prepend`, `before`, `after`, `appendChild`, `replaceChildren`
  and `replaceWith` where a condition around the call asks where the node
  stands: its parent or a sibling, or whether something contains it.
  Measured on main: flagging every such call with a non-literal argument
  finds 304 sites; dropping nodes made in the same expression, in a local
  binding, or by `el`/`offer` still leaves 175, nearly all new nodes. The
  guarded form finds 13, and 12 are re-parents of a node already placed
  (the 13th, in lf-trace, appended its fresh replacement under the old
  name). Requiring a `holdFocus` in the same function instead finds 9, of
  which 4 are real.

Every finding now goes through `move()`, or `setChildren` for the key
chip's seat and a failed diff file's body (which re-inserted its summary).
Moves that bracketed an insertion with their own focus hold use `move()`
instead: the margin rows' lane and carrier moves (`moveHolding`,
`moveHost` and the rows' `move` option are gone), the inline
contributions' host, and the floating composer's motion frames.

Behaviour change: a focused `leaf-text` moved with `moveBefore` (Chrome)
kept focus but lost its caret. The move collapses the native selection in
its closed shadow tree, and CodeMirror read that back a few frames later
as a caret at the start, so the next keystroke landed there. The field's
reparent hold, which restored the caret on the first selectionchange, ran
before CodeMirror read the collapsed selection. `leaf-text` now defines
`connectedMoveCallback`, so a native move is no disconnection, and puts
its recorded selection back as it lands (`LeafEditorView.placeSelection`,
split out of `focus()`). The hold and its test, which called
`connectedCallback` directly, are deleted.

Tests: `test_the_comment_box_keeps_the_user_typing_as_it_takes_its_seat_again`
reseats the response bar into a diff outlet while the user types. With
`moveBefore` it fails on main (the typed words land at the start) and
with the bar's old `append` (the editor blurs); without it, the hold
path keeps focus and caret. `tests/runtime/moves-rule.test.mjs` pins what
the rule refuses and admits.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
leaf-agent previously approved these changes Oct 12, 2026
…odule per row

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 20ef69b into main Oct 12, 2026
3 checks passed
@max-sixty
max-sixty deleted the agent-aa59d82fedcd90841 branch October 12, 2026 10:02
leaf-agent added a commit that referenced this pull request Oct 12, 2026
The [main CI
run](https://github.com/max-sixty/leaf/actions/runs/38218813098) fails
three words-watch cases because their presenter fixtures return `null`
from `attach()`. The presentation scheduler now calls `handle.present()`
when it claims a paint, so those tests throw before exercising input
ownership. This changes the fixtures to return a minimal completion
ticket; the tests still assert that a passive paint cannot borrow a
click's permission to discard typed words.

[#2521](#2521) intentionally moved
completion-ticket creation into the presentation handle. The fixture had
not followed that contract. Before this change, the focused presenter
case timed out with `Cannot read properties of null (reading
'present')`; after it, all three affected presenter cases pass. The two
startup assertions from the same run pass on current main after #2547
and #2548. The remaining chip-seat write is reproduced on main and
passes on the open #2545 head, which owns that writer.

Verified with the three affected presenter cases and both startup cases,
run together: `5 passed`.

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Oct 12, 2026
… a file editor's move (#2570)

Items 68 and 69 of the Lit follow-up review. Both are cases where an
editor that stays in the page should survive a DOM move, and one of them
didn't. The numbers are the review's own, not GitHub issues.

## Item 68: the comment box on a diff line no longer blurs on a new
patch

**What changed.** A comment box being written on a diff line now keeps
focus through a new patch, and never fires a blur. Before, lf-diff
removed the thread outlet holding the box while it prepared the next
thread placement. That ran before core moved the box out at commit, so
the editor left the document, blurred once, and `restoreFab`'s focus
hold put the user back. A patch that changed the commented line itself
caused the same blur one step earlier: the file's re-render dropped the
outlet together with the old row.

**How.**
- `thread/surfaces.js`: the placement callback's context gains
`onCommit(run)`. Each registered function runs after the placement
commits, so by then core's composer and thread views are no longer in
outlets the widget stopped returning. A cancelled preparation never runs
its hooks.
- `lf-diff.js`: the widget removes unused outlets only in `onCommit`
(`retireThreadOutlets`). Between preparation and commit,
`layoutThreadOutlets` sizes the grid with them still counted. A file
re-render keeps every outlet in the document. Each one goes beside its
datum's line if that line is drawn again, otherwise beside the line
before it (`outletPlaces`). A datum that is drawn again takes its outlet
along with `move()` rather than a remove and recreate.
- With that, `holdFileFocus` drops its outlet loop, which only existed
because a re-render could take away the outlet the user was standing in.
`restoreFab`'s hold stays, because it covers a different case: the reset
hides the bar before moving it.
- A disconnect now removes every outlet and resets the grid spans.
Before, it left empty outlets behind for a later reconnection.
- Found along the way, and needed by the new outlet placement: when a
new patch added a hunk above lines it kept, the kept gutter rows were
looked up by old index *after* earlier rows had already taken new
indices. So a lookup could return the wrong row, and two gutter rows
dropped out of the column. Gutter rows are now read by their old index
before any is re-keyed.
- A re-render lays out the outlets it kept straight away, rather than
waiting for the next thread pass.
- `module-authoring.md` ("Widget-local Thread placement") documents
`onCommit` and the rule that a widget removes an outlet only at commit.

**Lines:** hand-written source +134 / −67 (surfaces.js +19/−2,
lf-diff.js +115/−65). Tests: +30 / −21.

**Test.**
`test_a_comment_being_written_on_a_diff_line_stays_in_hand_across_a_new_patch`
now asserts zero blurs and that the old line outlet is gone. It runs for
two patches: one that changes another hunk, and one that changes the
commented line. Both fail on `main` with one blur each. The first blurs
in `endThreadSurface`; the second blurs in `replaceFileContent`'s
`setChildren`.
`test_a_diff_refresh_keeps_a_selection_in_unchanged_lines` gains an
`earlier-hunk` case. Without the gutter fix, the widget fails with
"Pierre returned a diff line without its paired gutter row".

## Item 69: the file editor keeps its caret through a move

**What changed.** If the page moves an `lf-file-editor` while the user
is typing, the next key now lands at the caret. Two ways it can be
moved: a container re-placing it with `setChildren`, which the test
drives, or a revision that reorders its section, which I have only read
in the code. Both go through `move()`. On `main` it landed at the start
of the file. The issue expected the view and buffer to be destroyed on
disconnect. They are not: `whileConnected` follows the arrival batch, so
a move inside one batch is not a departure, and the view, buffer and
focus all survive. The real loss was the caret. `moveBefore` collapses
the native selection inside the CodeMirror view, and CodeMirror reads
that back as a caret at offset 0. That is the same failure #2545 fixed
for `leaf-text`. `lf-file-editor` now defines `connectedMoveCallback`,
which puts each view's recorded selection back
(`createEditor().placeSelection()`). This is the rule
module-authoring.md already states for any element holding a
`LeafEditorView`. A generic fix inside `move()` can't cover `leaf-text`,
whose selection is in a closed shadow root, so the forward stays with
each host. There are two hosts.

**Lines:** hand-written source +17 / −0. Tests: +39 / −0.

**Test.** `test_file_editor_keeps_the_user_typing_through_a_move` uses
`setChildren` to move the editor past an empty sibling while the caret
is mid-word, then types. The sibling is empty so that nothing on screen
moves. Without the callback, the text lands at the start of the file.
With it, the test passes.

## Checks

These all pass: `pnpm run test:runtime`; the diff and seat selections of
`test_render_threads.py`; `test_render_diff_controls.py`; the
diff-outlet tests in `test_render_anchors.py`,
`test_render_read_state.py` and `test_render_widgets.py`; the diff,
composer and comment-box selections of `test_render_widgets.py`,
`test_render_drafts.py`, `test_thread_response_continuity.py` and
`test_response_pointer.py`; `test_render_files.py`; and eslint and
prettier on the changed files.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 12, 2026
…2573)

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_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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