Skip to content

Serve a resumed session's pages again at its first prompt - #5555

Merged
max-sixty merged 1 commit into
mainfrom
grind/4025
Nov 5, 2026
Merged

max-sixty merged 1 commit into
mainfrom
grind/4025

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

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 start would. 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

  • The prompt hook serves the page again. reconnect.serve_again keeps the selection reconnect.py already made: the session's own claim under this harness, an initialized page without preview.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, as server start's page argument does), prepares the claim and the harness's delivery, and runs hosting.claim_and_start with the prepared claim, the path a detached preview already takes, so the hook selects no checkout.
  • It runs before the prompt opens its turn. The claim then starts the generation the prompt's turn joins (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).
  • A failure is one line in "Leaf needs attention:", worded by the revive-refused finding (now "server was down and did not start again: …"). The prompt hook's start is bounded at HOOK_TIMEOUT_S / 4 (5 s) through a new timeout on claim_and_start. Without the bound, a page whose recorded port another process holds retries the bind for PORT_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.
  • Removed: the reconnect notice text; SessionStart's reconnect notice (SessionStart now only asks for the skill to be re-read after a compaction, and opens no turn, so it serves nothing); the late lifecycle check notices added to every carry_turn output; and the watch's server-down wake, "server is not running; restart it with leaf server start …", a hand copy of the server-down finding's words. leaf wait still ends with that finding.
  • HOOK_TIMEOUT_S moves from hook_transport.py to harness.py. It is a fact about each harness's hook registration, every hook already imports harness, and reconnect can then derive its bound without importing hook_transport. The test that holds prompt hooks in sessions holding no page to cheap imports caught that import.
  • Contract prose: session-lifetime.md ("Serving again at the next prompt" replaces "Reconnect notices", plus the claim writer, the watch's ending and the adapter sentence), the hooks.py, hook_transport.py, cmd_claim and claim_and_start docstrings, and the "Hook notices" row in docs/how-it-works.html. No shipped instruction described the notice, so skills/leaf/ is unchanged.

Evidence

Transcripts. I scanned Claude Code transcripts modified in the last 21 days for hook-delivered notices, meaning hook_additional_context attachments rather than agents reading the source. 18 sessions got the reconnect notice, which matches the triage's count. 6 ran leaf server start at 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) runs claude -p --plugin-dir <this tree> under a throwaway home and state home. The first run serves a page. The second uses --resume on 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.

base (47f5a8f) this branch
resumed prompt's hook context "Your Leaf page needs reconnecting: … leaf server start …" none
curl from the resumed turn 000 (nothing listening) 404, which is what a freshly started server answers for this probe's unchecked page
claim after the resumed turn old generation, no turn new generation, the resumed prompt's turn and pid

Timing (.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 start from the CLI took 0.92–0.97 s, consistent with the triage's 0.94–1.37 s.
  • A resumed prompt that serves the page takes 1.03–1.24 s on this branch. On base it takes 0.15–0.18 s, prints the notice, and leaves the server down.
  • A prompt with nothing to serve has interleaved medians of 0.290 and 0.270 s on this branch, against 0.302 and 0.275 s on base.

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 after prompt_turn fails 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 records server-down.
  • tests/journeys/test_session_lifecycle.py: the real hook, run as a subprocess through bin/leaf, serves the page under the session's process. The claim names CLAUDE_PID, not the hook's pid, and the server retires when the session ends again, across two outages.
  • Each new test fails on base src/; the port-held case fails on the missing bound.
  • Ran: those three files, plus 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 the UserPromptSubmit and claim_and_start tests in test_interact_layer, process/test_interact_server, journeys/test_hook_registration, journeys/test_stop_guard and dev/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 (setup through quit). 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.py was 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

  • No Codex or Pi journey was run. For Codex, the prompt hook now prepares delivery, which starts the delivery adapter from inside a hook. I haven't run that path. The selection covered Codex and Pi before, so they get the same serve.
  • The Claude desktop app and background-job lifetimes (app, job): the hook prepares the claim from its own environment, as a Bash tool call does, and I only exercised the pid lifetime.

Calls to review

  • The watch's server-down ending no longer wakes the session at all, including a watch that follows a turn's end. Between turns, a page whose revival failed now waits for the next prompt instead of the agent being woken. Over 20 days no such wake came between turns.
  • SessionStart serves nothing, so a Codex session resumed without a prompt (no prompt hook) gets its page back at its first prompt, where it used to get the notice at start.
  • The hook renews a page at an older protocol before serving it, as leaf server start does. 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 own leaf server start instead of the next prompt.

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

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]>
@max-sixty
max-sixty merged commit 0f19761 into main Nov 5, 2026
5 checks passed
@max-sixty
max-sixty deleted the grind/4025 branch November 5, 2026 22:03
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_
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.

Consider automatic Leaf recovery on resume

2 participants