Repository navigation
feat: remember per backend whether a fork's change of effort keeps the prompt cache - #5907
Conversation
…e prompt cache The hooks module learned from each process's first turn-end check whether a fork at another effort than the session's rewrites the conversation to the cache, and forgot it when the process ended, so on a backend without per-message effort every session start or resume paid one rewrite to learn it again. A turn-end check's usage now stores that answer in `$.store`, keyed by the model and the variables that pick the route (`CLAUDE_CODE_USE_*`, the base URLs, `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`), which name the backend without deciding for it. Where the stored answer is a rewrite, every fork on that backend goes at the session's effort from its first. The answer lapses after seven days; then a check in a conversation of at most 20 messages asks at the changed effort again, and drops the answer if the cache held. A check counts as a rewrite only while the main thread's last request began within the cache's five-minute lifetime, since an expired entry reads the same in usage. Behavior change: this replaces #5905's gate, which forked at the session's effort whenever `CLAUDE_CODE_USE_VERTEX` or `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` was set. That copied Claude Code's own rule and couldn't see a proxy or a model without per-message effort, so #5906 asks for the usage to decide. Under those variables the first session's first check now asks at `low` once, and every later session on that backend forks at the session's effort from the first; #5905's test of the variables is replaced by tests of the stored answer. The stand-in Claude Code driver gains a file-backed `$.store` and a clock tests can move on. Closes #5906
leaf-agent
left a comment
There was a problem hiding this comment.
The store key can exceed what $.store accepts, and the backends this PR targets are the likeliest to hit that. When it does, every turn-end check rewrites the cache again.
…ses a key over 256 characters A Bedrock ARN or a long proxy base URL made the per-backend key exceed Claude Code's limit, so every get and set threw and every turn-end check rewrote the cache. The answers now live in one map under `fork-effort`. The test driver enforces the same key limit.
|
@leaf-agent The expiry probe also runs on thread-reply forks, where
|
…learn A thread's reply fork can't renew or drop a stored rewrite, since its usage decides nothing, so asking again there paid a rewrite on every reply until a turn-end check ran. After a stored rewrite lapses, reply forks now keep the session's effort; only the turn-end check asks at the changed effort again.
|
Fixed in 43d61de: only a fork that learns from its usage (the turn-end check) asks at the changed effort once a stored rewrite lapses, and every other fork keeps the session's effort. The new test |
… here, not from 2.1.325 2.1.322, 2.1.323 and 2.1.325 all refuse main's module (claude plugin validate), so #5907's loop broke the module for every session since it landed.
…rom the main thread's usage (#5947) Claude Code refuses to load a hooks module whose `$.env.get` takes anything but a literal name, and #5907 read the route variables in a loop. Since #5907 landed, Leaf's Claude Code module hasn't loaded in any session: no band, no turn-end check, no thread-reply fork. `claude plugin validate` refuses main's module at 2.1.322, 2.1.323 and 2.1.325. `readRoute` now spells each name out. The claude-code journey passes with this branch, where it failed at setup on main (`hooks module did not load`). #5952 adds that validation to CI and should merge after this. The PR also tightens the effort learning from #5892/#5907, which keeps turn-end checks from rewriting the prompt cache on Vertex: - **Rewrite test.** A check at a changed effort now counts as a rewrite when it reads less from the cache than the main thread's last request left there (read + creation from the `turn.step` result, recorded as `request.cached` in each fork's diagnostics). Before, the test was "wrote more than it read", which misses a rewrite of a conversation shorter than the system prompt and tools the fork still reads (28k tokens on Vertex in #5904's repro). In 103 real checks where the cache held, plus 12 in the journey, every fork read at least `cached`. Only answered checks count: a refused fork reports zero reads. - **Lapse.** A stored answer was asked again only in a conversation of at most 20 messages. In practice that never happened: of 902 real checks, none came before message 46. So a stored answer never lapsed. Now the first check after the 7-day lapse asks at low whatever the size. Where the backend still lacks per-message effort, that costs one conversation rewrite per backend per week, about $1.50–3 at 300k tokens. That number is a call to weigh. The module's stale `session.append` door names, which 2.1.325's `claude plugin validate` reports, are left to #5816. Max asked whether the Vertex caching issue could be fixed. Codex reviewed the change; its two findings, the refused fork and a step racing the fork, are fixed here. `tests/process/test_interact_claude_code_module.py` passes (18); the new cases fail on main. > _This was written by Claude Code on behalf of max-sixty_
…nd daily at the newest release (#5952) CI now fails wherever Claude Code would refuse to load `hooks/claude-code.ts`. `pnpm run check:hooks` also runs `claude plugin validate .` from a pinned `@anthropic-ai/claude-code` (2.1.325), on every run. The daily scheduled `test` job then runs the same check again after installing the newest Claude Code and Pi. A release that refuses the module, or changes the API `hooks/pi.ts` uses, then turns the daily run red for tend within a day, instead of waiting for Dependabot's weekly bump of the pin while users' Claude Code updates itself. Until now nothing in CI asked Claude Code: the module's tests run in a Node stand-in, and `hooks/tsconfig.json` leaves the module out. That gap let #5907's `$.env.get(name)` loop through. Claude Code refuses a module whose `$.env.get` takes anything but a string literal (2.1.305 already did), so every session that loaded the module from an install since #5907 has had no band, no turn-end clerk and no thread forks. The refusal shows only in Claude Code's debug log, so all Max saw was an empty band in a session he resumed today. #5947, now on main, spells each name out, so the check passes on this branch; before it merged, the check failed in CI with exactly that refusal. The nightly harness scan in `running-tend` now reads Pi's releases too, since `hooks/pi.ts` depends on Pi's API. The package's postinstall copies its platform binary into place, so `pnpm-workspace.yaml` allows its build; pnpm hard-links the 347 MB binary from its store, so a worktree adds no copy. Max asked why the band was missing, then for the nightly check against each harness's newest release, and for Pi in the nightly scan. Checks: `pnpm run check:hooks` failed in CI with only that refusal before #5947 and passes locally after it; the newest-release step's commands, rehearsed locally with #5947's module, installed Claude Code 2.1.325 and Pi 1.5.2 and passed; `pnpm run test:browser` and pre-commit pass. The `test` job's one failure, `test_receipts.py::test_ack_rearm_reports_when_its_only_page_transfers_after_selection`, is inherited: a watchdog warning (`InotifyEmitter.queue_events() called when the thread is inactive`) on a waiter's stderr broke the test's JSON parse, in code this PR doesn't touch. > _This was written by Claude Code on behalf of max-sixty_
When Claude Code didn't load Leaf's hooks module into a session holding a page, Leaf's Stop hook now tells the user, once per Claude Code process. Claude Code refuses a module it can't validate and reports the refusal only in its debug log. When it refused `hooks/claude-code.ts` after #5907, sessions lost their band, turn-end clerk and thread forks, and all Max saw was the band gone. The signal comes from the pages follower, which only the module starts: it now records the Claude Code process it runs in. A Stop that ends the turn of a session holding a page, in a process no follower has recorded, prints a `systemMessage`. It says what is off, gives the `claude plugin validate <plugin root>` command that says why, and suggests `/reload-plugins` once an update fixes it. The notice records the process too, so it shows once. To weigh: the notice prints only on a Stop that ends the turn, since a Stop that continues it already prints its own output. A turn that ends within about a second of its own first claim could warn wrongly, because the follower for that claim may not have started yet; the harness docstring says so. Max asked for this after the missing band. On Claude Code 2.1.325, a throwaway mod showed that the module's processes have the Claude Code process as their parent, the pid a hook's `CLAUDE_PID` names, and that Claude Code parses a Stop hook's `systemMessage`. Tests mark the module as running for the sessions they hook. The new test fails without the notice. `test_hooks.py`, `test_stop_guard.py` and `test_forks.py` pass. `test_session_lifecycle.py`'s server-start tests flake under load with and without this change (3 runs each). > _This was written by Claude Code on behalf of max-sixty_
…kend reads the cache (#5960) Leaf's two conversation forks (the turn-end check and the thread-reply line) no longer ask `$.model.fork` for `low` effort. They think at the session's effort, which reads the prompt cache on every backend, and their prompts ask them to answer straight away without thinking it over, as Claude Code's `ModelForkRequest.effort` docs suggest. That removes the per-backend learning from #5892/#5907/#5947 (route variables, a stored answer in `$.store`, a weekly lapse, a cache-warmth window) and the `effort` plumbing through `clerk.py`, `forks.py` and their diagnostics. A TODO in `runFork` marks the switch back to `low` once Claude Code carries a change of effort per message on every backend without an env var. What it costs, from `leaf-dev eval 'turn-end-check*' 'fork-reply*' --base` (one sample per case, xhigh with the quick-answer request against the low fork): - **Turn-end check:** 15/15 passed, as low did. It wrote 1.62× low's output (95% 1.26–2.09) and took 1.23× its time (1.10–1.38). The check runs off the turn's path. - **Reply line:** 7/7 passed, against low's 6/7. It wrote 1.49× the output (0.91–2.44) and took 1.09× the time (0.94–1.25), about 4 s a line. To weigh: the turn-end check is roughly a quarter slower than at `low`. In exchange, Vertex and proxy sessions stop paying a conversation rewrite to learn their backend. Max asked for the effort change to go in favour of the prompt. The claude-code journey passes, and Codex reviewed it; its two findings, an unused driver clock and a stale comment, are fixed. `tests/process/test_interact_claude_code_module.py`, `tests/model/test_clerk.py` and `tests/model/test_forks.py` pass. > _This was written by Claude Code on behalf of max-sixty_
The hooks module learned from a process's first turn-end check whether a fork at another effort than the session's rewrites the conversation to the prompt cache, and lost that when the process ended. So on a backend without per-message effort, every session start or resume paid one rewrite (about $1.50–$3.00 at 300k tokens) to learn it again. #5905, merged minutes ago, avoided that for
CLAUDE_CODE_USE_VERTEXandCLAUDE_CODE_DISABLE_EXPERIMENTAL_BETASby reading those variables. #5906 asks instead for the usage to decide, since the variables copy Claude Code's own rule and can't see a proxy or a model without per-message effort. This PR does that and removes #5905's gate.What
runForkinhooks/claude-code.tsdoes now:{at}in$.storeunder one key,fork-effort, whose value maps each backend to its answer ($.storerefuses a key over 256 characters, which a Bedrock ARN or a proxy URL can exceed). A backend is the model plus the route variables read through$.env.get: theCLAUDE_CODE_USE_*routes, theirANTHROPIC_*_BASE_URLs, andCLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS. Those variables only name the backend; they don't decide the answer.lowagain. A thread-reply fork keeps the session's effort, since its usage can't renew or drop the answer. If the cache held, it deletes the answer, so a backend that gains per-message effort is found again cheaply. In a larger conversation the stale answer keeps holding.usage, so this keeps that misreading out of the store.The stand-in driver (
tests/claude_code_driver.mjs) gains a file-backed$.storeshared across driven processes and an{"advance": ms}clock line. New tests cover: a second session on the same backend forks atxhighfrom its first check; a session withCLAUDE_CODE_USE_VERTEXset gets its own key and asks atlow; an answer three days old holds; an eight-day-old answer asks again in a 12-message conversation but not in a 400-message one; a check ten minutes after the request stores nothing; repeated thread replies against an eight-day-old answer keep the session's effort and leave the store unchanged until a turn-end check asks again; and a route longer than a store key still keeps its answer (the stand-in enforces Claude Code's 256-character key limit). On the previous module, four of these five cases fail.test_interact_claude_code_module.pypasses (17 tests), and pre-commit passes over the changed files.The thresholds (seven days, 20 messages, five minutes) are judgment calls the issue left open. Message count stands in for conversation size because the module sees no token count before it forks. The cache saving still rests on the Vertex measurements in #5904, because CI can't reach Vertex.
Closes #5906 — automated triage