Skip to content

fix: fork at the session's effort once a fork's usage shows a cache rewrite - #5892

Merged
max-sixty merged 2 commits into
mainfrom
fix/issue-5891
Nov 12, 2026
Merged

max-sixty merged 2 commits into
mainfrom
fix/issue-5891

Conversation

@leaf-agent

@leaf-agent leaf-agent commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Where Claude Code's per-message effort doesn't reach the model, a turn-end check or thread-reply fork at low from a session at another effort writes the conversation to the prompt cache again rather than reading it. That happens behind a proxy that rewrites mid-conversation system messages, on Vertex, or with CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS set. Each such check is billed as a cache write of the whole conversation. The issue measured about $4 rather than $0.08 for one check on a 400k-token conversation on Fable 5.2.

The hooks module can't tell those setups apart, so it reads the usage of each turn-end check (clerk in hooks/claude-code.ts). Once a check writes more of its prompt to the cache than it reads, every later fork in that session (turn-end checks and thread replies) runs at the session's own effort (the main thread's last turn.step effort), and the cache serves that effort. Only the turn-end check decides, because it forks right after the main thread's request while the cache is warm. A thread-reply fork can run in an idle session whose cache has expired, where it would write its whole prompt at any effort. In a working setup a low fork reads almost everything from the cache, so it keeps running at clerk.EFFORT / forks.EFFORT as before. A session therefore pays for at most one rewrite instead of one per check. The headers of clerk.py and forks.py no longer say that the effort always leaves the cache whole.

Verification: a new test in tests/process/test_interact_claude_code_module.py drives the module through the Claude Code stand-in. The first fork reports rewrite-shaped usage, and the test checks that the second fork is sent at the session's xhigh. Before the change it fails with ['low', 'low'] == ['low', 'xhigh'], and after it it passes, along with the existing fork tests and tsc over hooks/. I couldn't reproduce the billing itself from CI because it needs a live Claude Code session in one of the three setups. The turn_end_check and fork_reply diagnostics already record the effort each fork was sent at, so a real session shows the switch.

The underlying behavior belongs to Claude Code: a changed top-level output_config.effort invalidates the cached messages when the per-message effort is missing. This change limits what that costs Leaf.

Closes #5891 — automated triage

…ewrite

Where Claude Code's per-message effort doesn't reach the model (a proxy
that rewrites mid-conversation system messages, Vertex, or
CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS), a fork at low effort from a
session at another effort writes the conversation to the prompt cache
again instead of reading it. The hooks module now watches each fork's
usage: once one writes more of its prompt than it reads, later forks of
the session go at the session's own effort, which the cache serves.

Behavior change: forks no longer always run at clerk.EFFORT /
forks.EFFORT; after a rewrite they run at the session's effort.

Closes #5891

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback can trip in setups where the cache works, and once it trips it stays on for the whole session. A thread-reply fork (forkReply) runs whenever the session's lane is live, idle sessions included. When the user comments after the main thread's cache has expired, that fork writes its whole prompt to the cache, as it would at any effort, and sets rewrote. From then on, every turn-end check and thread line in that session runs at the session's effort, which gives up clerk.EFFORT's measured 0.45× time on the path where nothing was wrong. The PR description calls this cost "only low effort's speed advantage", but this is the common working path, not just the rewrite case.

The turn-end check doesn't have that ambiguity. It forks right after the main thread's request, so the cache is warm, and a write larger than the read there can only be the effort rewrite. I'll push a change that judges the cache from the clerk's forks only. Thread forks still follow the flag once a turn-end check sets it, which happens at the end of the session's first turn.

A thread-reply fork runs whenever the session's lane is live, idle sessions
included, so it can meet a cache that expired and write its whole prompt at any
effort. Judging the rewrite from it would switch every later fork of a working
session to the session's effort. The turn-end check forks just after the main
thread's request, while the cache is warm, so its usage alone sets the switch.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit dbc46b5 into main Nov 12, 2026
6 checks passed
@max-sixty
max-sixty deleted the fix/issue-5891 branch November 12, 2026 09:25
leaf-agent added a commit that referenced this pull request Nov 12, 2026
…bled experimental betas (#5905)

Under `CLAUDE_CODE_USE_VERTEX` or
`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`, Claude Code 2.1.321 sends no
per-message effort, so a fork at `low` from a session at another effort
writes the conversation to the cache again (#5891, #5904). Until now the
hooks module found that out only from the first turn-end check's usage,
which cost one rewritten conversation per session start or resume, and
left the thread-reply fork at `low` until then. The module now reads
both variables from Claude Code's environment (`$.env.get`) when it
first learns the session's process, and where either is set (to a value
such as `1` or `true`) every fork of the session goes at the session's
own effort, from the first. A proxy that rewrites mid-conversation
system messages shows in no variable, so the usage-based fallback from
#5892 stays for that case.


`test_a_backend_without_per_message_effort_forks_at_the_sessions_effort`,
parametrized over both variables, sets the variable in the stand-in
Claude Code's environment and checks that the first turn-end fork goes
at the session's `xhigh`. It failed with `low` before the change and
passes after it; the rest of `test_interact_claude_code_module.py` and
`pnpm run check` pass. The cache saving itself rests on the issue's
Vertex measurements; CI can't run against Vertex.

Behavior change: under either variable, the turn-end check and
thread-reply forks never ask for `low`, so they lose `low`'s shorter
output and time there, which the issue accepts as the price of keeping
the cache. Bedrock isn't included: nothing here establishes whether it
carries per-message effort, and the usage check still covers it if it
doesn't.

Closes #5904 — automated triage

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Nov 14, 2026
…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_
max-sixty added a commit that referenced this pull request Nov 14, 2026
…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_
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.

A fork at low effort rewrites the conversation to the prompt cache where per-message effort does not reach the model

2 participants