Repository navigation
Ask a Claude Code session's own model through Leaf's module, not claude -p - #5835
Conversation
…ude -p`
A page server titled a Claude Code page's new threads, and answered widgets'
`generate`, by running `claude -p --model=haiku --json-schema …` from its own
process. Now it asks the claimant session's hooks module instead: it writes the
request among the session's files in the state home (`completions`), the session's
pages follower lists it, and the module makes the completion with
`$.model.complete` on the session's own client and hands the reply back through
`leaf hook --fork`. `ClaudeCodeHarness.inference_generator` is the one place that
changed, so titles and `/api/llm` both move, and `claude -p` leaves Leaf's product
code.
What changes for the user:
- Titles come sooner. On 80 real titles since 2026-11-09, `claude -p` took a median
2.5 s (p90 4.0 s): 0.2 s starting and exiting the process, and two model turns,
since `--json-schema` answers through a tool call. Through `$.model.complete`,
392 titles asked of real pages' threads took a median 1.5 s (p90 2.2 s), one turn
of about 300 input tokens where `claude -p` sent about 1,900. The claude-code
journey titled each of its threads in 1.3–2.0 s.
- The request uses the session's own credentials and provider, so it no longer
depends on a `claude` on the page server's PATH or on user-level settings alone.
- A Claude Code page's title and widget `generate` now need the claimant session's
Claude Code process running, since its module makes the call; a request nobody
answers fails after `inference.TIMEOUT` (60 s), and the agent still names the
thread with its reply.
`$.model.complete` takes no answer schema, so the schema goes at the end of the
system prompt and Python parses and validates the answer, as it did for Pi
(`inference.answering`, `inference.answered`, now shared). Told the schema that
way, Haiku answered 392 of 392 with JSON matching it; asked for a plain-text title,
it prefaced 4 with its reasoning about the request. The request asks for up to
64,000 tokens, claude-haiku-5-5's limit, since the call's default of 1,024 would cut
off a widget builder's answer; its time limit, five seconds inside the page
server's, ends a long one.
The pages follower now prints `{"pages": [...], "completions": [...]}`, listing a
session's requests apart from its pages, so a page the agent closed, which the band
leaves out, still gets its titles. It always waits on the session's requests, so a
follower without forks holds one native subscription, on the state home's
`sessions/`, which a forking follower already watched for its lane; it still
subscribes to none of its pages (#4813). The page server polls its request every
50 ms rather than subscribing, so it still holds none. Requests are session files,
which the session's retirement removes.
A follower moves to the Leaf delivering for its session at its next start, while
the module stays the copy Claude Code loaded, so a session whose module loaded
before this change reads the new output as an error until `/reload-plugins`: its
band goes blank, its forks stop, and each follower start shows the error. A new
session that selects a checkout from before this change sees the reverse. Leaf
keeps no compatibility with earlier runtimes (AGENTS.md, "Stage"), and either kind
of session loses its titles until it reloads anyway, since its module makes no
completions.
Tests: the title and `/api/llm` tests answer through a stand-in module
(`interact_support.claude_code_module`), the title snapshot records the whole
request the module is given, system prompt included, and
`test_interact_claude_code_module.py` titles a comment end to end through the real
follower, launcher and module, after a provider's 429 that the module reports and
the page server retries. `test_hooks.py` holds the follower to listing a request
while it lists no page, and to waking as one is written. Tests that read the
follower's output read its new shape.
leaf-agent
left a comment
There was a problem hiding this comment.
For a Claude Code claim whose lifetime is app or job, the claim can stay active while no Claude Code process runs, so titles and generate now fail where claude -p used to work. A page server builds the generator whenever the claim is active. For those lifetimes, lifetime_is_live checks only the desktop app's session record or the job's state.json, not a process. The ClaudeCodeHarness.lifetime docstring describes when the process is gone while the claim is still live:
- The desktop app stops every session to install an update once its window has been unfocused for ten minutes. That is what happens while the user is in the browser commenting on the page.
- The background-job daemon retires the worker about an hour after the job goes idle.
In both cases nothing runs the module or the follower. Each new thread then waits the full 60 s in completions.ask before it falls back to its opening words. A widget's generate rejects with a timeout after the same wait. A resumed job's follower may still list the request inside its deadline, but a desktop session doesn't resume until the user opens it.
The description's "needs the claimant's Claude Code process running" reads as if an active claim implied that, and for these two lifetimes it doesn't. Forks already handle this case: a message is held only while a follower holds the lane, and otherwise it is nudged. Completions could follow the same pattern. Every follower would hold a lease, forks or not (a non-forking follower takes no lane today), and ask would fail at once when nothing holds it. Whether to do that, or to keep claude -p as the fallback for a session with no follower, is your call. As the branch stands, those sessions lose titles and widget generation.
leaf-agent
left a comment
There was a problem hiding this comment.
The new completions.py header correctly states which claims outlive their Claude Code process. The verdict still waits on the choice the description leaves open under "Things to weigh": accept that desktop sessions stopped for an update, and idle background jobs, get no titles or generate for the 60 s deadline, or fall back to claude -p whenever no follower holds a lease. Once that's decided, nothing else in the diff stands in the way.
# Conflicts: # docs/threads.html
# Conflicts: # tests/claude_code_driver.mjs
# Conflicts: # tests/claude_code_driver.mjs
…e tools and ten tests they caught (#6412) Nothing in CI ran `dev/tests`, so main commits broke those tests without anyone seeing (#6374 fixed ten of them). This adds every developer-tool test, the Python and the Node ones, to the daily scheduled run, after the product suite, where a failure reaches Tend like any other failure on main. Pull requests, pushes to main and landing gates still don't run them. On the Ubuntu runner they took 8 minutes 43 seconds (235 passed, and the one macOS-only test skipped). Three of the 13 other failures were tools that had stopped working: - `leaf-dev preview` kept serving the runtime and layer its process first read, so an edit to `browser/leaf.js`, `browser/base.css` or a bundled package reached neither an open tab nor a fresh one. It also decided when to restart from its own copy of the runtime's identity, which had lost the kernel's stylesheets and the bundled packages, so editing those never restarted even a `--user` preview's server. The preview now reads the runtime's own identity, and replacing its in-process server first drops what the process holds of the tree (`runtime_selection.forget_tree`, which is new). - `leaf-dev site demo` stopped getting a title for its thread after #5835, because its own session has no hooks module to answer the request. It now answers with a staged title through the suite's stand-in for that module, which moves to `leaf_dev.claude_code_module`. - `leaf-dev trace-server` called a Playwright export that 1.65 removed. The other ten had stale expectations: #5964's stdout refusals, #4325's recovery words, #6154's recovery reload, the layer record's dropped `fingerprint`, and a preview identity that moved to About this page. Two more tests started a second Playwright driver on a worker thread that already held one. To weigh: the demo's thread title is now the same every run, rather than worded fresh by a model each time. Asked for by max-sixty, as a follow-up to #6374. Tests: all of `dev/tests` locally and, through a commit since reverted, on this PR's runner; the product modules the stand-in move and the layer change touch (225 passed). > _This was written by Claude Code on behalf of max-sixty_
Thread titles and widget
generateon a Claude Code page no longer shell out toclaude -pfrom the page server. The page server writes the request into the claimant session's files in the state home (src/leaf/completions.py). The session's pages follower lists it, Leaf's hooks module runs$.model.completewith the session's own client, and the reply comes back throughleaf hook --fork. Titles get faster. On the same 60 real thread requests, sent four at a time in one window:claude -p --json-schema(main)claude -p, schema in the prompt$.model.completethrough the moduleThe module row leaves out the relay through the follower. End to end, the claude-code journey titled each thread in 1.2–2.6 s.
$.model.completetakes no answer schema, so the schema goes at the end of the system prompt, and Python validates the answer. Haiku returned valid JSON for every one of 452 title requests. On the widget builder, every completed answer in the HTML arm parsed: 14 of 14, plus 1 request that hit the time limit. The Leaf-catalog arm uses a nested$defsschema, and there 57 of 60 parsed. The three failures dropped a key, left a bracket unclosed, and repeated the whole object. Each of these rejectsgeneraterather than reaching the widget.Things to weigh:
{"pages": [...], "completions": [...]}. A running session's module stays at the version it loaded, while its follower moves to the new Leaf. So after this lands, each live session shows an error at every follower start, and its band and forks stop, until/reload-plugins. A new session that selects a pre-change checkout hits the same error the other way round. Listing completions per page avoids that, but loses titles on a page the agent closed. Leaf's Stage rule chose the new shape. Any session that doesn't reload also loses its titles, since its module makes no completions.generatenow needs the claimant's Claude Code process running. Without one, the request fails after 60 s. An active claim doesn't guarantee a process. The desktop app stops its sessions to install an update after ten unfocused minutes, so those sessions get no titles until the user reopens them; meanwhile each thread shows its opening words, andgeneraterejects. A background job retires its worker after an hour idle. The comment's wake probably brings the job back inside the 60 s, but I haven't measured that. Max kept the module and accepted this gap. The alternative wasclaude -palone with the schema in the prompt (the table's middle row), which keeps working while a session is stopped and needs no relay. A fallback that keeps both transports would cost more than either.sessions/. It still holds none per page (fix: stop retaining native watchers for server maintenance #4813).Two follow-ups are filed: #5954 asks whether a fork of the session should title threads, and #5955 constrains the reply to the schema once
$.model.completetakes one.Max asked for this. An independent reviewer read it twice, and its findings are fixed in this branch. Focused tests pass, including the module end to end through a 429 and a retry, and so does the real
leaf-dev journey claude-code.