Skip to content

Keep a retained editor in the document through a diff's new patch and a file editor's move - #2570

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

max-sixty merged 4 commits into
mainfrom
agent-a674375c69b238cce

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

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

max-sixty and others added 3 commits October 12, 2026 03:21
… a file editor's move

Review items 68 and 69 of the Lit follow-up round.

68: lf-diff took a thread outlet down while preparing a thread placement, before core
had moved the comment box out of it, so a new patch blurred the box the user was
writing in, and a focus hold put them back. A patch that changed the commented line
dropped the outlet earlier still, in the file's re-render. Now:

- The placement callback's context carries `onCommit(run)`, which runs once the
  placement commits (thread/surfaces.js). lf-diff removes the outlets it stopped
  returning only there, so the composer has gone home or to its new seat first.
- A file's re-render keeps every outlet in the document: beside its datum's line
  where that is drawn again, else beside the line before it. A datum drawn anew
  takes its outlet along with `move`.
- The file hold's outlet loop is gone, since no re-render drops an outlet.
- A disconnect retires every outlet and resets the grid spans, rather than leaving
  empty outlets behind for a reconnection.

The new-patch test now asserts zero blurs, for a patch that changes another hunk and
one that changes the commented line itself.

69: lf-file-editor kept its view and buffer through a move, since whileConnected
follows the arrival batch, but `moveBefore` collapsed CodeMirror's native selection,
so the next key landed at the start of the file. It now defines
`connectedMoveCallback` and puts each view's recorded selection back as leaf-text
does.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…t of a replaced row

A hunk added above kept lines gave them indices other kept gutter rows still held, so
a lookup after the first re-key found the wrong row and dropped two gutter rows; the
rows are now read by their old index before any is re-keyed. A file that becomes a
rename moves its line outlets out of the old row before replacing it, and a re-render
lays the outlets it kept out again at once.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

@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 new file-editor move test fails the browser health gate. Its setChildren(main, [...main.children].reverse()) call reorders the page without a user gesture. In a focused run, the caret and typing assertions passed, but teardown reported eight passive layout shifts, including the focused editor moving 92 px. That conflicts with skills/leaf/assets/AGENTS.md’s “Stability” rule that reading content stays put without a gesture, and CI will run this new test. If the intended case is a revision reordering sections, the page needs to keep the reader’s place through that move. If it is a user-requested reorder, the fixture should make that gesture explicit before the move. The current test leaves the PR’s claimed revision path unverified and the gate red.

…ng on screen shifts

The reversed sections moved the focused editor 92px without input, which the
browser health gate refuses.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 99ad3b3 into main Oct 12, 2026
3 checks passed
@max-sixty
max-sixty deleted the agent-a674375c69b238cce branch October 12, 2026 11:03
max-sixty added a commit that referenced this pull request Oct 12, 2026
…ng it (#2571)

## Why

`x-thread-surface` only gated `placeThreads`. A widget's module
registered its surface in `connectedCallback` and unregistered it in
`disconnectedCallback`, and `requireSurface` refused the call unless the
tag declared the key. The declaration added nothing the call did not, so
every surface widget carried the same registration code. The user
decided (#29) that the declaration should do the work.

## What changed

- **Registration from the declaration.** The runtime registers each
instance of a tag declaring `x-thread-surface` once its module has
defined it, and retires it when it leaves the page. This uses the
arrival lifetime every element owner already holds (`watchArrivals`), so
a move within one batch keeps the registration. The watch starts once
the registry that names the tags has loaded. It waits for a tag's
definition only while none stands, so an element whose constructor threw
is not retried; its module's failure report is the outcome.
- **The widget answers instead of registering.** On each reconciliation
pass the runtime calls the element's own `threadOutlets(targets,
{signal, collection, onCommit})`. The batch, the returned outlet array,
the commit-time work #2570 added (`onCommit`) and the failure handling
are those the callback had; `lf-diff` lays its outlets out in the method
and retires unused ones on commit.
- **A handle the widget reads.** `threadSurface(element)` from
`widget-api.js` gives `update()` and `open(datum, {origin})`. Before
registration, or after the widget leaves, `update` does nothing and
`open` returns `false`.
- **Removed.** `placeThreads` and `requireSurface` are gone.
`placePageThreads` stays for page-owned annotations.
- **Widgets and docs.** `lf-diff` and `lf-visual-review` implement
`threadOutlets` and no longer register or unregister; `lf-diff`'s outlet
map, which used that name, is now `threadSeats`. The `$keys`
description, `module-authoring.md` ("Widget-local Thread placement"),
`docs/registry.html` and the two eval cases that named `placeThreads`
describe the new contract.

## Behavior change

A package widget that declares `x-thread-surface` must implement
`threadOutlets`. Calling `placeThreads` now fails, because the export no
longer exists. A widget can no longer withdraw its surface while it
stands: to offer no seats, it returns `null` for each target. Tests
changed to match:

- The adapter-isolation test drops its two cases where the widget
unregistered itself.
- The widget-before-page and cohort tests withhold seats by returning
`null` outlets, instead of registering late or unregistering.

## Tests

- `pnpm run test:runtime` (369, including `thread-surfaces.test.mjs` for
the failed-constructor case) and `pnpm run check` pass.
- The thread-surface browser tests and #2570's retained-editor tests by
node id at `-n 2`: 52 passed. These cover the diff, visual-review,
read-state, startup adapter-failure, widget-before-page and cohort
tests.
- The registry contract tests and the `widget-api` export tests pass.

> _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
…ding it (#2580)

Item 72 of the Lit follow-up review. That review numbers its own items,
so #72 is not a GitHub issue.

## Item 72: a comment being written stays in hand when its outlet's
content goes

**What changed.** A comment being written on a diff line now keeps
focus, with no blur, when a change removes the line without a redraw
that would keep its outlet. There are four such changes: a new patch
that drops the line's file, the file's next deferred patch failing to
load, the source being cleared, and a patch the widget refuses
(`failSoft`). Before, each of them removed the thread outlet holding
core's composer before the thread placement committed. A dropped file
lost focus altogether, and the user was left on the page. In the other
three the box blurred once and `restoreFab`'s hold put the user back.
The same cause affected `lf-visual-review`: a new run that dropped the
case being commented on left the user on the next case's title, and a
cleared source blurred the box once. Both now keep the user typing too.

**How.** #2570 gave the placement an `onCommit` hook, so a widget
removes unused outlets only once core has moved the composer. Its hook
didn't cover a removal outside the placement, and the runtime had
nothing a widget could call first.
- `releaseOutlets(node)` (`thread/surfaces.js`, exported from
`widget-api`) moves core's composer out of every registered outlet under
`node` immediately, to where the next commit would put it. On every path
this covers, the composer's datum is gone, so the next commit puts it in
the same place and doesn't move it again. The move goes through
`move()`, which keeps focus. The released box is in the viewport.
- `lf-diff` calls it before dropping a file, before a file's error box
replaces its lines, before a cleared source empties its stage, and
before `failSoft`.
- `lf-visual-review` calls it for the cases a new run drops, at the top
of `#show`, and before `failSoft`.
- `failSoft` doesn't call it itself. The dependency gate forbids
`widget-upgrade.js` from importing the thread layer, because that import
closes a cycle through `markdown.js` and `projection/authored.js`. These
two are the only widgets that place threads.
- A release has to come before any `holdFocus` around the removal.
Otherwise that hold reads the user as having left and hands them to
Retry or to the case title. `module-authoring.md` ("Widget-local Thread
placement") states this next to `onCommit`.

**Not done here.** I didn't add a removal hook in `dom-children.js`.
`lf-visual-review` removes cases through Lit `repeat` rather than
`setChildren`, so the hook wouldn't reach it. A release made during
removal would also come after the widget's own focus hold. While the
floating bar is still being placed at home, that hold would not count it
as drawn. A revision that removes the whole widget while the user is
composing is deferred (`midComposition` in `version.js`). I checked
this: the revision was not applied within 120 s.

**Lines.** Hand-written source: +31 / −1 (surfaces.js +11, widget-api.js
+1, lf-diff.js +12/−1, lf-visual-review.js +7). Tests: +102 / −6.

**Tests.**
-
`test_a_comment_being_written_on_a_diff_line_stays_in_hand_across_a_new_patch`
runs four new cases: `file-dropped`, `file-refused`, `source-cleared`
and `patch-refused`. It now also checks that the box is in the viewport.
Each new case fails on `main`. `file-dropped` loses focus, and the other
three blur once. `patch-refused` was checked by disabling the release
before `failSoft`.
-
`test_a_comment_being_written_on_a_visual_case_stays_in_hand_as_the_case_goes`
has two cases, `case-dropped` and `source-cleared`. Both fail without
the release in `#show`.

## Checks

These pass: `npm run test:runtime`; the diff, visual-review and failure
selections of `test_render_threads.py`, `test_render_diff_controls.py`,
`test_render_anchors.py`, `test_render_widgets.py`,
`test_render_read_state.py`, `test_render_drafts.py`,
`test_thread_response_continuity.py`, `test_response_pointer.py` and
`test_render_visual_review_journey.py`; the widget-api export test; and
pre-commit.

> _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