Skip to content

fix: fork at the session's effort from the first under Vertex or disabled experimental betas - #5905

Merged
leaf-agent merged 1 commit into
mainfrom
fix/issue-5904
Nov 12, 2026
Merged

leaf-agent merged 1 commit into
mainfrom
fix/issue-5904

Conversation

@leaf-agent

Copy link
Copy Markdown
Collaborator

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

…t experimental betas

Claude Code sends no per-message effort under CLAUDE_CODE_USE_VERTEX or
CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS (2.1.321), so a fork at another effort
than the session's writes the conversation to the cache again. The hooks module
learned that only from the first turn-end check's usage (#5891), which cost one
rewritten conversation per session start or resume. It now reads both variables
from Claude Code's environment with the session's process, and where either is
set every fork of the session goes at the session's effort. A proxy that drops
per-message effort shows in no variable, so the usage check stays for it.

Behavior change: under those variables the turn-end check and thread-reply forks
never ask for `low`.

Closes #5904
@leaf-agent leaf-agent changed the title Fork at the session's effort from the first under Vertex or disabled experimental betas fix: fork at the session's effort from the first under Vertex or disabled experimental betas Nov 12, 2026
@leaf-agent
leaf-agent merged commit 7f89955 into main Nov 12, 2026
6 checks passed
@leaf-agent
leaf-agent deleted the fix/issue-5904 branch November 12, 2026 20:12
leaf-agent added a commit that referenced this pull request Nov 12, 2026
…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 added a commit that referenced this pull request Nov 12, 2026
…e prompt cache (#5907)

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_VERTEX` and
`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` by 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 `runFork` in `hooks/claude-code.ts` does now:

- **Stores the answer.** A turn-end check at a changed effort whose
usage shows more written than read records `{at}` in `$.store` under one
key, `fork-effort`, whose value maps each backend to its answer
(`$.store` refuses 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`: the `CLAUDE_CODE_USE_*` routes, their
`ANTHROPIC_*_BASE_URL`s, and `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`.
Those variables only name the backend; they don't decide the answer.
- **Uses it from the first fork.** Where the stored answer is a rewrite,
every fork on that backend goes at the session's effort, the
thread-reply fork included.
- **Lets it lapse.** After seven days, a turn-end check in a
conversation of at most 20 messages asks at `low` again. 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.
- **Ignores a cold cache.** A check counts only when the main thread's
last request began within five minutes, the API's default cache
lifetime. An expired entry looks the same in `usage`, so this keeps that
misreading out of the store.

The stand-in driver (`tests/claude_code_driver.mjs`) gains a file-backed
`$.store` shared across driven processes and an `{"advance": ms}` clock
line. New tests cover: a second session on the same backend forks at
`xhigh` from its first check; a session with `CLAUDE_CODE_USE_VERTEX`
set gets its own key and asks at `low`; 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.py` passes (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

---------

Co-authored-by: leaf-agent <[email protected]>
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's change of effort rewrites the prompt cache on backends without per-message effort, such as Vertex

1 participant