Repository navigation
Preserve reader intent across asynchronous navigation - #919
Conversation
Capture navigation ownership before hydration or materialization and pass it through nested reveals. Preserve synchronous focus handoffs without renewing the original input generation, and let newer input cancel delayed tab scrolling. Exercise stale and untouched arrivals in the real browser, including mobile tray handoffs and lazy data hydration. Align the existing shadow-root animation assertion with the canonical element diagnostic.
leaf-agent
left a comment
There was a problem hiding this comment.
activateRevision gates the carry's focus and caret restore on the same predicate as the scroll restore, so a reader who scrolls or types while a slow install settles keeps the words in their field but is left standing on document.body. Those two restores answer different questions: the scroll landmark is a navigation, and yielding it to newer input is the point of this change; the caret is a return to the node the install itself removed, and the reader is on body only because the patch took their field away. Newer input is not a reason to leave them there — their next keystrokes reach the page's key scope instead of the field they were writing in.
currentIntent() already admits at === document.body; it is the input-generation term that cancels this. The inline suggestion splits the two conditions at that one site. If the carry's inner-box scrolls should still yield to newer navigation, the split belongs inside carry.js's returned landing instead, where the focus and the positions are already separate lines — that call is yours.
Everything else in the pass held up: reveal's event.detail.mayReveal has exactly one dispatcher, lf-tabs' fallback: active matches the only element followLocation can focus on the "history" path (#targetForLocation() === panel), and the touchstart addition to reader-intent.js covers the types the removed navigationIntent listener carried. The 22 cases named in the description pass here, as do the four carry and refused-reopen cases the split in restoreCarry touches — and they still pass with the suggestion applied, which also closes the case below.
Reproduction
Against LIVE_KEYS_APPARATUS, whose named apparatus sits inside the widget LIVE_KEYS_APPARATUS_REWRITTEN replaces whole:
- Click
#lk-note, typehalf a thought,setSelectionRange(4, 4). - Route-hold
**/vendor/agentic-mermaid.esm.js, then stamp a revision that isLIVE_KEYS_APPARATUS_REWRITTENplus an<lf-diagram id="late">, so presentation waits on the held renderer. - While it is held, one
page.mouse.wheel(0, 120)— or onepage.keyboard.press("x"); either reproduces. - Release the route and wait for revision 2.
#lk-note has value half a thought, and document.activeElement is BODY with the caret gone. The same case passes on the merge base 5c355332fb23dfeca5279e344ccd034c89fdec03, where restoreCarry ran unconditionally. Without step 3 it passes on this head too.
Carry focus and caret with values before waiting for presentation. Preserve focused descendants while reading-layout constructs its wrappers, so reload upgrades do not break ongoing typing. Keep only scroll correction delayed and cancellable.
leaf-agent
left a comment
There was a problem hiding this comment.
restoreCarry no longer runs unconditionally. currentIntent.handoff(…) runs its callback only while current() holds, so the carried values, checked state and disclosures now sit behind the same navigation predicate as the scroll correction. Those are not navigation: a draft the reader typed into a field the patch is about to remove has to cross with that field whatever they have done since, which is what the unconditional call on the merge base guaranteed and what the function's own comment still claims — "Restore values, disclosures, focus and caret in the turn that replaces their nodes". handoff reads focused() at that instant, after patchTree has already removed and inserted nodes; where the patch leaves focus anywhere other than document.body or inside the pre-patch source — a connecting custom element that focuses, say — it returns false without running the move at all, and the reader's field silently reverts to authored text with nothing later to put it back. Per Stage in CLAUDE.md, a guard earns its place by having a useful response to a reachable condition; discarding the draft is not one.
The same decline leaves restoreCarryScroll unassigned, because handoff does not re-baseline source when it bails. Focus falling back to document.body before presentation — the patch removing whatever had taken it — makes currentIntent() read true again, and the call then throws inside activateRevision, abandoning the comparison restore, the update notice and the document publication that follow it.
Only the handoff itself — adopting the focus the restore just placed, without letting it count as newer input — belongs behind the predicate, which is what the inline suggestion leaves there. If the empty move reads wrong at the call site, an adopt() beside handoff in reader-intent.js that re-baselines source without running one says the same thing more directly.
leaf-agent
left a comment
There was a problem hiding this comment.
The open question from the earlier reply on this head — whether a runtime-owned focus transfer should cancel the reader's intent, and who owns the answer — now has a tested answer, so I'm posting it rather than leaving the call unsupported.
Gating handoff's decline on the input generation alone, and re-baselining source even when the move is skipped, is enough: it adopts the focus the runtime left instead of reading it as the reader moving on. The inline suggestion is that change. With it applied, the carried inner scroll comes back at 220 instead of 0, focus still lands on the arriving element, and the draft, checked state and disclosure still cross. tests/test_render_navigation.py and tests/test_render_widgets.py pass whole — 353 cases, run on the merge of ead233d4 with main, including the carried-apparatus case this PR adds. That is the whole diff to reader-intent.js; nothing in carry.js or the restoreCarry call site moves.
Worth stating plainly, since it bears on whether this is a package-author surface: the same arrangement keeps the offset at 220 on the merge base 254fdd45b3c05b795bd0e73d6360bebe3d6a0994. The base restored unconditionally, so it also restored over a reader who had deliberately scrolled away — which is the behaviour this PR is right to change. The narrower loss is the one left: a widget focusing on connect, with no reader input at all, yields the reader's position as if they had.
How the three numbers were measured
Appended to tests/test_render_navigation.py, against READING_REGIONS_PAGE with a draft field and an inner-scrolled box added before #left-head:
page.locator("#reading-draft").fill("kept draft")
page.evaluate("() => { document.getElementById('reading-log').scrollTop = 220 }")
page.evaluate("""() => {
customElements.define('page-focus-transfer', class extends HTMLElement {
connectedCallback() { document.getElementById('right-head').focus(); }
});
}""")
revised = source.replace(
'<p id="left-start">', '<p>Added context.</p><p id="left-start">'
).replace("</main>", "<page-focus-transfer></page-focus-transfer></main>")
stamp_page(serve.page_dir, revised, "Update reading with a focus handoff")
wait_for_revision(page, 2)#reading-log's scrollTop after the install: 0 on this head, 220 on the merge base, 220 with the suggestion applied. Dropping page-focus-transfer from revised gives 220 on this head too, which isolates the transfer as the cause. carry.js and reader-intent.js are unchanged between ead233d4 and d25200887ecbdbfb623900a0140327dc6525761b, and the base merge's only version.js edit is in the posture-transition path, so the reading carries across the merge.
leaf-agent
left a comment
There was a problem hiding this comment.
One contract point on the new handoff, not a live defect — I agree with the caller audit: each of the three asks/view.js sites verifies mayArrive() with no await between that check and its handoff, so none of them can reach the skip, and restoreCarry discards the return outright.
What the skip changes is what the return means. With the focus term gone from the entry guard, a declined move still falls through to source = focused(), and return current() then recomputes the location term against the node focus has just landed on — so the return reports available() && retained === intent and nothing about whether move() ran. The Ask sites read it the other way (if (!mayArrive.handoff(() => setOpenTray(null))) return false, ahead of the reveal that would otherwise land behind the tray), and so does the example packages.md hands a package author: currentIntent.handoff(() => changeSurface()). A surface change is the one move that must not be dropped while its caller is told it went through, and the paragraph now describes the skip as though every move were a focus move.
Reporting the skip is one line and leaves the carry path alone: source is still re-baselined on every path, so currentIntent() at presentation still reads true and the new #reading-inner assertion still holds. uv run pytest tests/test_render_navigation.py -n0 -k "apparatus or intent or restoration" is 18 green with that suggestion applied, on this head merged with main. If the primitive should keep its current shape instead, then the paragraph is the half to change — either way the two want to say the same thing.
…the gesture (#982) "Keep the reader's place" had been fixed one surface at a time (#919, #922, #937, #946, #954, #955, #956, #971). The thread list had a good hold that no other surface could use. The Page Map restored its raw `scrollTop`, so a group arriving above the view pushed the rows the reader was on down by that group's height. The margin card relied on native anchoring. The panel's visible band was measured three different ways. Separately, `reveal()` took its reader intent when it was called, so an async caller that forgot to pass one got a late capture, and `lf-diff` moved focus and scroll after a network wait no matter what the reader had done since. ## What changed - **`runtime/reader-place.js`**: `placeKeeper(scroller, {items, identity})` holds one reference node's content offset through a mutation. It picks the reference from the pointer, then focus, then the visible band. It suspends `overflow-anchor` while the hold lasts, hands the place to whatever node a render put under the same identity, and follows a running fold. The thread list (keyed by thread id), the Page Map's `renderSheet` (keyed by `data-lf-map-key`), and the margin card's re-render of the same thread (keyed by event id) all use it. A node with an empty identity hands the place to the next candidate and is never matched to another node that also lacks one. - **`geometry.js`**: `visibleBand` (the shown band minus stuck `.lf-pinned` covers) and `landingBand` (minus the declared `scroll-padding`). Read acknowledgement, the thread card's being-read check, conversation landing, and anchor travel now read these instead of measuring for themselves. - **Reader intent**: `reveal(el, intent)` now requires the intent retained at the gesture and throws without one. `lf-diff`'s `stepHunk` and `nextUnreviewed` capture intent before they load a file, and land only if that intent still stands. `references/packages.md` states the contract. `scroll` is still not a cancelling input: the runtime's own landings fire it. `reader-intent.js` now lists the input that begins each way a reader can scroll. A probe confirmed that pressing a scrollbar fires `pointerdown` on the scroller. - **The #954 ordering rule is now the interface**: `chrome-layout.js` exports `takeShell(key)` in place of a frame that took a callback. A surface owner therefore has no way to render inside the frame that native anchoring depends on. ## Behavior that moved - With no pointer or focus to lead, the thread list's hold now uses the topmost visible card, which is what its header said. Before, it used the second. - A card entirely under a stuck heading no longer counts as visible. - The Page Map keeps the rows the reader was looking at, not a scroll offset. ## Tests - `tests/runtime/reader-place.test.mjs` covers candidate order, the clamp-aware correction, band insets, the identity hand-across, and an empty identity that must not hand the place across. Each hand-across case was checked by disabling the hand-across and watching the case fail. - `test_chrome_contracts.py` (everyday) gets a new Page Map continuity test. It fails against the base `page-map-dialog.js` (row moved 36px). The file also takes `test_taking_the_panels_strip_leaves_the_reader_on_the_same_words` from the nightly controls file. - `test_render_anchors.py` gets a nightly test: a hunk step still waiting on a file leaves a reader who has pressed elsewhere. It fails against the base `lf-diff.js`. - Local: `npm run test:runtime` 60 passed. A focused browser selection across margin, controls, widgets, projection, anchors, and chrome contracts: 142 passed. `test_render_navigation.py::test_pressing_a_page_mark_stands_in_the_thread_it_opens[long]` fails the same way on the unchanged base. > _This was written by Claude Code on behalf of max-sixty_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
An asynchronous arrival could capture reader intent after waiting for rendering or hydration, allowing older work to take focus or restore scrolling after the reader had moved elsewhere. Revision restoration, Ask materialization, lazy data links, and nested disclosure now retain the original intent throughout the operation and yield to newer input.
Nested reveals share that predicate, and synchronous panel or tray focus transfers preserve its original input generation. Anchor navigation uses the shared mechanism instead of maintaining a separate input tracker. Values and disclosures cross replacement unconditionally. Eligible fields receive focus and caret synchronously so typing can continue while renderers load; reading-layout also preserves focus while wrapping existing controls. Only scroll correction waits for presentation. The widget-author contract documents the navigation rule. Integration retains PR #946's native tab navigation and removal of its parallel scroll cache; the delayed-layout regression now verifies the native arrival and newer wheel movement.
Validation: 1,219 everyday tests and 22 focused browser cases passed before integrating main. After integrating main, all 1,276 everyday tests pass. After the focus repair, 34 runtime tests and 24 focused browser cases pass, including patch/reload, mid-string typing without refocusing, newer wheel input, explicit blank-page clicks, and untouched restoration. The new interrupted-navigation cases fail on the original code while untouched controls pass. A further 18 focused browser cases pass after separating mandatory value carry from focus ownership, including an arriving custom element that takes focus. Broader runs exposed stale native-accordion test assumptions: PR #917 handles the widget/chart cases, and a separate local follow-up covers bottom-clamped versus freely aligned long-thread arrivals.