Skip to content

Restart at the next prompt a page server another installed copy of Leaf started - #6765

Merged
max-sixty merged 6 commits into
mainfrom
agent-a15bb68a7a0fcb9ab
Nov 22, 2026
Merged

max-sixty merged 6 commits into
mainfrom
agent-a15bb68a7a0fcb9ab

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

When a session's prompt hook runs an installed copy of Leaf and a page the session holds is served by another installed copy, the hook now restarts that server under its own copy, at the same address, before the turn opens. A session moves to a newer copy at /reload-plugins, a background job's wake or a resume, and until now its pages' servers kept the old copy until someone ran leaf server start, so the user's tab could draw what the agent's page check never checked.

The hook reuses leaf server start's test (now hosting.other_serving_leaf), its check that this copy can serve the page, and its restart (hosting.restarting_server, which now takes the hook's deadline). A server a checkout started, and every server when the hook runs from a checkout, is left alone. A restart takes the old server down first, so it begins only while 5 s of the hook's 10 s deadline remain (reconnect.RESTART_NEEDS); later ones wait for the next prompt.

Worth weighing:

  • The test is "differs", as leaf server start's is, so a session still on an older copy that takes a newer copy's page with leaf page claim moves it back, where the older copy can serve it.
  • Each restart adds about 2–3 s to that prompt. Where the runtime changed, open tabs reload, which keeps drafts but drops focus and caret, as any server start restart does today; this now happens once per update.
  • Every prompt from an installed copy reads each held page's claim, service record and lease, about 0.5 ms a page; a checkout's hook reads nothing more.

The maintainer chose this on the Leaf page about keeping a session on one version of Leaf (open item #12). A fresh-context reviewer read the diff; its findings are fixed. I didn't run the agent journey: it loads this tree with --plugin-dir, a checkout, where the new pass returns at once.

Tests: new test_a_prompt_restarts_only_a_server_another_installed_copy_started (model; removing any guard, the deadline margin or the serve-then-restart order fails it) and test_a_prompt_restarts_a_server_another_installed_copy_started (journey, a real restart at the same URL), plus the serve-again, server start, session-lifecycle, server-lifetime and hook-import tests, all passing.

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

leaf-agent
leaf-agent previously approved these changes Nov 22, 2026
@max-sixty
max-sixty merged commit 65cd90c into main Nov 22, 2026
6 checks passed
@max-sixty
max-sixty deleted the agent-a15bb68a7a0fcb9ab branch November 22, 2026 22:05
max-sixty added a commit that referenced this pull request Nov 23, 2026
…asks for (#6778)

When a page's server restarts under a Leaf whose browser runtime
differs, the open tab reloads itself (`reloadNow` in `layer-client.js`),
and since #6765 that happens once per Leaf update for every page a
session holds, often seconds after the user sent something from the tab.
That reload dropped the user's place: a margin card or the panel's view
closed under the reply being written in it, the panel's field lost focus
and caret, a reader below a disclosure they had opened landed 1800px
off, and the panel's Threads and On Me lists closed. Keys the user typed
on with as the tab reloaded ran Leaf's single-key commands in the new
document; probed at a key every 60ms, they opened another thread, showed
Go-to hints and resolved a thread in the log, and a Space typed first
scrolled the page and turned on design mode. Words typed as the server
restarted put the browser's "Changes you made may not be saved" prompt
in front of the reload.

Every reload Leaf asks for now goes through `reloadDocument`, and the
document writes, as it goes (`pagehide`), the one-use handoff a revision
whose executable changed already used (`handOff` in `version.js`), so no
second restore mechanism is added. What the handoff carries grows where
the reload showed gaps: the reader a reply was written in (`readerOf`),
so a reply typed in the panel's thread view comes back there rather than
in the thread's margin card, and the panel's open list with its row and
scroll (`captureCurrentLocation` in `floating-panel.js`).

Keys typed through the reload go into the restored box. prepaint reads
the handoff once for a reload of its own address (`lfStorage.handoff`),
so the bootstrap knows before any module loads that a place is coming
back. Until the new document has handed it back, no key is ready
(`placeHandedBack`) and the held keys replay only afterwards, wherever
focus is. Meanwhile a key the hold keeps, or a bare modifier such as a
capital's Shift, is no input, so it doesn't stop the hand-back, while a
key that acts, such as Tab or PageDown, does, and drops the held keys.
Where the user was writing (`typing` in the handoff), a Space starts a
held run as a letter does, and the hold takes keys whatever the
single-key preference. `drafts.js` skips its leave prompt only for the
`beforeunload` Leaf's reload raises (`takeLeafUnload`), so after a
page's own prompt keeps the document, a later departure asks again.

To weigh: the handoff and the panel's location also serve a revision's
fresh document and a Back to a working place (`history.js`), so those
now reopen a panel list and return a panel reply to the panel too.
Ordinary loads keep counting held keys: probed against `main`, a load at
a URL fragment and a Back to the page with keys typed during startup
land and act the same; uncounting held keys there let an unsent
comment's box reopen under the keys and lost some of them, so it applies
only to Leaf's reloads. Still lost: a disclosure with no `id`, and focus
on a widget's generated control with no `data-lf-carry`, since
`carry.js` matches by authored id.

Probed on `main` and here with `leaf-dev probe --base` on the real path:
card reply, panel thread reply, panel page comment, passage comment, a
just-sent reply in card or panel, Threads on a row, On Me scrolled, a
scrolled reader with an open disclosure, and typing through the reload
(a capital first, a Space first, into a passage comment, with single-key
commands off) each come back as they stood, the typing cases also in
Firefox; on `main` each lost its place. Three fresh-context reviews used
the change and read the diff, and Tend's review found the Shift case;
their findings are fixed here.

Tests:
`test_a_reload_into_another_runtime_keeps_the_user_where_they_were`
(card, card at a version's address, panel),
`..._keeps_the_panel_s_list_open` (Threads, On Me),
`test_keys_typed_through_the_reload_go_into_the_reply_it_hands_back` (a
capital first, a Space first, a passage comment, single-key commands
off),
`test_words_typed_as_the_server_restarts_go_with_the_reload_unasked` and
`test_a_page_s_own_prompt_the_user_stays_at_leaves_leaf_asking_before_they_leave`
fail without the change, except the passage-comment case, which guards a
loss that depends on the replay's timing. The 24 startup key tests, the
28 tests that press Back, 27 revision-continuity tests, `pnpm run
test:runtime` and `pnpm run check:browser` pass.

> _This was written by Claude Code on behalf of max-sixty_
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