Repository navigation
Serve a resumed session's pages again at its first prompt - #5555
Merged
Merged
Conversation
A session that resumes holding a page whose server retired with it now gets the page back from its first prompt hook, which serves it as `leaf server start` would: it renews the layer, prepares the claim and the harness's delivery, and runs `claim_and_start` with the prepared claim (`reconnect.serve_again`). The selection is the one the reconnect notice used, so a stopped, released, persistent or preview page, and another session's page, are left alone. The agent hears only of a page that did not start, and why. Before this, the hook told the agent to run the command: of 18 Claude Code sessions told over 20 days, 6 did at their next tool call and 4 never did, while the user's tab had no server. The serve runs before the prompt opens its turn, so the turn joins the generation the claim starts. A claim made after the prompt, in a session whose process died without a SessionEnd, replaced the prompt's turn with a random one. It also runs before the hook reads the session's pages, so input left while the session was gone reaches that same turn. The hook's start is bounded at a quarter of the hook timeout, since a recorded port another process holds makes the bind retry for 90 s. The watch between turns no longer wakes the session when no page it watches has a server and revival failed. All 39 such wakes in 20 days arrived mid-turn from a prompt's watch, beside that prompt's own notice, and each started another; one session got 15 in two minutes. SessionStart now only asks for the skill to be re-read after a compaction. `HOOK_TIMEOUT_S` moves to `harness.py`, so the prompt hook can bound its start without importing the hook transport. Co-authored-by: Claude Opus 5.5 <[email protected]>
leaf-agent
approved these changes
Nov 5, 2026
max-sixty
added a commit
that referenced
this pull request
Nov 14, 2026
Some tests ran real work under a deadline the product sets, so their result depended on how fast the machine runs, against tests/AGENTS.md's "Results do not depend on execution speed". The suite's check, `test_a_wait_takes_the_suites_deadline`, reads only deadlines written in test source, so it missed every one. The prompt hook gives a resumed session's page server 5 s to start (`reconnect.SERVE_WITHIN`) and hands the turn its input inline only within 10 s of its own start (`hook_transport.CONFIRM_WITHIN`). A start takes about 1 s on an idle Mac and about 3 s on CI, over 5 s at the tail. On 14 Nov two CI runs ([#5863's](https://github.com/max-sixty/leaf/actions/runs/43627224592/job/132545728761) and [another a minute later](https://github.com/max-sixty/leaf/actions/runs/43627326086)) failed four `test_session_lifecycle.py` tests with "did not become ready". Across 324 CI runs since the tests landed in #5555, their call time shows no drift: every day's slowest run sits within half a second of the bound. An autouse fixture in `tests/interact_support.py` now gives both bounds `STATED_TIMEOUT` in every in-process test, replacing the one test that patched `CONFIRM_WITHIN` to 600 s for the same reason. The `port held` arm keeps 0.5 s, which holds at any speed because the held port means its start never becomes ready. An audit of every other product deadline a test runs work under, or shortens with `monkeypatch`, found three more: - Two presence tests count neighbor scans while the presence cache's 2 s age runs out; they now give it `STATED_TIMEOUT`. - The render gate's stopped-server journey cut `SERVED_TIMEOUT_MS` to 1.5 s for every probe and read in four renders; now only the stalled read is cut. - Three arms of the Codex desktop start test cut `START_TIMEOUT` to 0.2 s, so a late answer read as lost, the outcome those arms expect; now only the lost-answer arm cuts it. tests/AGENTS.md now says to give such a deadline `STATED_TIMEOUT` and shorten it only to 0 or where the work it bounds can never finish. Still exposed: `tests/journeys/test_session_lifecycle.py` runs the hook as a subprocess, which a fixture can't reach. Tests: under twelve busy loops on the Mac's efficiency cores, all six reconnect tests fail on `main` and pass here (one start took 40 s); with only `SERVE_WITHIN` raised, the resumed-session test fails on `CONFIRM_WITHIN` instead. The presence tests passed 6 of 6 under the same load. The edited journey, the hook, lifecycle, Codex adapter and suite-source tests, and the other hook-reaching model tests pass. Asked for by Max, reviewing a CI failure his session flagged. > _This was written by Claude Code on behalf of max-sixty_
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a session resumes holding a page whose server retired with it, its first prompt now takes the page back and serves it, as
leaf server startwould. Before this change the prompt hook told the agent to run that command, and the user's tab had no server until the agent did. The agent now hears only of a page that did not start, and why.Closes #4025
What changed
reconnect.serve_againkeeps the selectionreconnect.pyalready made: the session's own claim under this harness, an initialized page withoutpreview.json, an enabled session service, and ownership or server down. It excludes a stopped or released page, a persistent service, a preview and another session's page, as before. For each qualifying page it renews the layer (vendoring.renew_layer, asserver start's page argument does), prepares the claim and the harness's delivery, and runshosting.claim_and_startwith the prepared claim, the path a detached preview already takes, so the hook selects no checkout.state.ensure_session). This matters for a session whose process died without a SessionEnd: a claim made after the prompt replaces its turn with a random one. The serve also runs before the hook reads the session's pages, so input the user left while the session was gone reaches that same turn inline (tested).revive-refusedfinding (now "server was down and did not start again: …"). The prompt hook's start is bounded atHOOK_TIMEOUT_S / 4(5 s) through a newtimeoutonclaim_and_start. Without the bound, a page whose recorded port another process holds retries the bind forPORT_WAIT_SECONDS(90 s), well past the hook's 20 s timeout. I found that when the first version of the refusal test took 92 s.noticesadded to everycarry_turnoutput; and the watch's server-down wake, "server is not running; restart it withleaf server start …", a hand copy of theserver-downfinding's words.leaf waitstill ends with that finding.HOOK_TIMEOUT_Smoves fromhook_transport.pytoharness.py. It is a fact about each harness's hook registration, every hook already importsharness, andreconnectcan then derive its bound without importinghook_transport. The test that holds prompt hooks in sessions holding no page to cheap imports caught that import.session-lifetime.md("Serving again at the next prompt" replaces "Reconnect notices", plus the claim writer, the watch's ending and the adapter sentence), thehooks.py,hook_transport.py,cmd_claimandclaim_and_startdocstrings, and the "Hook notices" row indocs/how-it-works.html. No shipped instruction described the notice, soskills/leaf/is unchanged.Evidence
Transcripts. I scanned Claude Code transcripts modified in the last 21 days for hook-delivered notices, meaning
hook_additional_contextattachments rather than agents reading the source. 18 sessions got the reconnect notice, which matches the triage's count. 6 ranleaf server startat the next tool call, 4 within 2–5 calls, 4 after 15–197 calls, and 4 never.The watch's server-down wake reached 20 sessions 39 times. All 39 were dequeued as
absorbed_mid_turn, so every one came from the watch a prompt starts. That watch woke the turn it follows, whose own prompt hook carried the reconnect notice. Each absorbed wake ran another prompt hook, which started another watch, which woke again. One session (fewer-change-marks, 1 November) got 15 wakes and 14 notices in about two minutes, while a merge had broken its checkout. No server-down wake was ever dequeued between turns.A real resume.
.tmp/resume_probe.py(not committed) runsclaude -p --plugin-dir <this tree>under a throwaway home and state home. The first run serves a page. The second uses--resumeon that session, and its only instruction is to curl the page's URL. Each print session ends with SessionEnd, so the server retires between the two runs.leaf server start …"000(nothing listening)404, which is what a freshly started server answers for this probe's unchecked pageTiming (
.tmp/time-hook.sh,.tmp/time-steady.sh, Claude Code's registered prompt hook command with--if-claimed, throwaway state home, 1-minute load 23–33 on 18 cores):leaf server startfrom the CLI took 0.92–0.97 s, consistent with the triage's 0.94–1.37 s.Tests
tests/model/test_session_lifecycle.py: a resumed session's first prompt serves its page at the recorded port, in the prompt's turn, and hands that turn the waiting comment inline. It does this after a SessionEnd, after a crash (the old generation still live under a dead pid), and for a page at an older protocol, which it renews first. SessionStart serves nothing. As a control, moving the serve afterprompt_turnfails only the crash case, where the claim's turn becomes a random id. Another test covers a grace-period server that outlives its session: the prompt reclaims the page and keeps the server. A start refused for a page a newer Leaf initialized, or one past its bound while another process holds the port, is reported with its reason, and the next prompt serves the page once the cause is gone. The exclusion cases from the old notice test are kept (stop, persistent, preview, transferred, foreign harness, unreadable claim or service, missing service, uninitialized page, new session).tests/model/test_wait.py: the watch from a Stop or a prompt that cannot revive its only page ends without waking the session, and recordsserver-down.tests/journeys/test_session_lifecycle.py: the real hook, run as a subprocess throughbin/leaf, serves the page under the session's process. The claim namesCLAUDE_PID, not the hook's pid, and the server retires when the session ends again, across two outages.src/; the port-held case fails on the missing bound.test_hooks,test_hook_registration,test_stop_guard,test_activity,test_clerk,test_forks,interact_contract/test_layer_contract,test_codex_adapter,test_codex_app_server,test_claims,test_server_lifetime,process/test_bridge,process/test_hook_gate(536 passed), and theUserPromptSubmitandclaim_and_starttests intest_interact_layer,process/test_interact_server,journeys/test_hook_registration,journeys/test_stop_guardanddev/tests/model/test_interact_preview. One failure,dev/tests/model/test_interact_preview.py::test_preview_file_subscription_follows_atomic_replacements, fails the same way on base.uv run leaf-dev journey claude-code: all 12 steps passed (setupthroughquit). No step resumes a session, so this checks that the prompt hook and the watch still behave for a live session. The real resume is the probe above. During the run,hooks.pywas twice swapped for a few seconds with the order-flipped control described above, which behaves the same for a session whose page is served.Not verified
app,job): the hook prepares the claim from its own environment, as a Bash tool call does, and I only exercised thepidlifetime.Calls to review
leaf server startdoes. For a session no longer live, that renewal stops the service (hosting.restarting_server), and only the start re-enables it. A page whose start fails right after a renewal therefore waits for the agent's ownleaf server startinstead of the next prompt.