Skip to content

fix(anthropic-subscription): read the prompt-cache lifetime from loaded settings files (senpi#2988) - #6333

Merged
code-yeongyu merged 5 commits into
code-yeongyu:mainfrom
surlymochan:fix/2988-settings-env-cache-ttl
Nov 10, 2026
Merged

code-yeongyu merged 5 commits into
code-yeongyu:mainfrom
surlymochan:fix/2988-settings-env-cache-ttl

Conversation

@surlymochan

@surlymochan surlymochan commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes senpi#2988 item 4 (settings-file lifetime invisible to the pin). The one-shot pin only saw process env and query-options env, so a FORCE_PROMPT_CACHING_5M / CLAUDE_CODE_PROMPT_CACHE_TTL set in a loaded settings file was invisible: on a managed lane the pin defaulted to 1h while Claude Code used the settings 5m, and the 1h breakpoint could then be rejected by the Messages API (the exact failure senpi#2982 fixed for explicit env).

  • New claude-settings-env.ts: resolves the effective settings env with the SDK's own resolveSettings merge engine (same files, lookup rules and precedence as the CLI), for exactly the sources the attempt loads (explicit settingSources, or user/project in preset-append ambient mode). Resolution failure falls back to the old pin behavior instead of failing the turn.
  • pinOneShotPromptCacheTtl takes the settings env as a fourth input. Precedence: explicit env > settings env > lane default. The pin still writes the final lifetime into query-options env, so what Claude Code receives matches the breakpoint.
  • stream.ts resolves the settings env once per stream and passes it to the pin only for attempts whose settingSources are non-empty (managed lanes load none).

Items 1 (ambient lane breakpoint) and 2 (wide tool turns) of #2988 stay open; this PR covers item 4 only.

Evidence

  • RED: new regression file test/suite/regressions/2988-anthropic-subscription-settings-env-cache-ttl.test.ts failed before the fix (missing module import).
  • GREEN: 11/11 pass in that file (pin table incl. ambient + settings TTL, reader with injected resolver, failure fallback).
  • Neighbors: 40/40 pass — 2982-anthropic-subscription-one-shot-cache-ttl, 2982-anthropic-subscription-append-only-prompt-capture, and the three cold-seed suites.
  • bun run check (root): clean — oxlint 0 warnings, fmt clean, tsc clean in both projects, install locks up to date.
  • No live-provider QA in this PR; tests are offline (fake resolver, no real credentials, no network).

Notes

  • CLA: the CLA for this repo is pending the user's own signature; not signed here, please do not treat this PR as CLA-complete.
  • Change fragment (changes/unreleased/<PR#>.md) to follow once the PR number is assigned.

Summary by cubic

Makes the one-shot prompt-cache pin read the effective env block of loaded settings files, so its breakpoint matches the lifetime Claude Code actually applies. Previously a FORCE_PROMPT_CACHING_5M/CLAUDE_CODE_PROMPT_CACHE_TTL in a settings file was invisible: managed lanes pinned 1h while Claude Code used the settings 5m, and the 1h breakpoint could be rejected by the Messages API.

  • New claude-settings-env.ts resolves the effective settings env with the SDK's resolveSettings merge engine, matching the CLI's files, lookup rules, and precedence; resolution failures fall back to the old pin behavior.
  • Precedence is now settings env > explicit env (options/process) > lane default, since Claude Code writes each settings source's env over the process env on an SDK spawn.
  • The pin only receives settings env for attempts whose settingSources are non-empty; managed lanes load none, so nothing changes there.
  • The lazy SDK module also loads resolveSettings, and a change fragment documents the fix.

Written for commit 460d1a5. Summary will update on new commits.

View guided diff

…ed settings files (senpi#2988)

The one-shot pin only saw process env and query options env, so a
FORCE_PROMPT_CACHING_5M / CLAUDE_CODE_PROMPT_CACHE_TTL set in a loaded
settings file was invisible: on a managed lane the pin defaulted to 1h
while Claude Code used the settings 5m, and the 1h breakpoint could be
rejected. Resolve the effective settings env with the SDK's own
resolveSettings merge engine for the sources the attempt loads, and let
it sit below explicit env and above the lane default in the pin.
@code-yeongyu

Copy link
Copy Markdown
Owner

Thanks @surlymochan. Review at efab453dab: changes requested for one medium point. Formatting, lint and types are clean, and it merges cleanly with main.

MEDIUM: the precedence is the reverse of Claude Code's. prompt-cache-ttl.ts:29 and :44-47 let options.env and the process env win over the settings env. In the bundled Claude Code CLI, each settings source's env is written over the process env (Object.assign(process.env, this.filterSettingsEnv(...))), and launch-env keys are only protected when the session is host-orchestrated (a desktop host, or CLAUDE_CODE_ENVIRONMENT_KIND=byoc). An SDK spawn is neither, so for us the settings value should win, and the TTL senpi computes should match the one Claude Code actually applies. A test where both the process env and a settings file set the TTL would pin the order.

LOW (non-blocking):

  • The managed policy layer is never read on the managed lanes: stream.ts:133-137 passes nothing when settingSources is empty and claude-settings-env.ts:49 returns early, but the CLI still applies policySettings env there (the SDK docs say [] still reads the managed-settings tier). The "managed lanes load none" comment is inaccurate; fine as a follow-up.
  • 2988-...test.ts:16: the title template has 4 placeholders but each row has 5 values, so the titles print the wrong columns.
  • No test covers the source selection in resolveQuerySettingsEnv (claude-settings-env.ts:46-50) or the per-attempt settingSources check (stream.ts:134-137).
  • stream.ts:101 runs resolveSettings on every stream, including resident sessions where the pin never runs; skipping it when useResidentSession is true would avoid an unneeded plutil/reg.exe spawn.

@code-yeongyu

Copy link
Copy Markdown
Owner

Concretely, for the medium point: with the process or options.env TTL set to 1h and a loaded settings file set to 5m, Claude Code runs at 5m while senpi pins a 1h breakpoint, which is the same rejection #2982 fixed. Row 9 of 2988-anthropic-subscription-settings-env-cache-ttl.test.ts currently expects "1h" for that case, so it locks in the wrong answer.

Suggested fix: put settingsEnv[CLAUDE_CODE_PROMPT_CACHE_TTL] ahead of options.env and the process env in the chain (FORCE_PROMPT_CACHING_5M still wins first), flip row 9, and update the docblock plus the fragment's "Precedence is explicit env, then settings env" line. (This reading comes from the minified CLI, not a live run; if you've seen different behaviour live, say so and we'll re-check.)

…he one-shot cache pin

Address review on senpi#6333: Claude Code writes each settings source's env over the process env, and launch-env keys are only protected for host-orchestrated sessions, so on an SDK spawn the settings lifetime is what Claude Code applies. Flip the pin precedence to settings env, then options/process env, then the lane default; flip the process-1h/settings-5m test row to 5m and add a settings-over-options row; fix the it.each title template (4 placeholders for 5 values); cover the empty source-selection branches of resolveQuerySettingsEnv; update the docblock and the change fragment.
@surlymochan

Copy link
Copy Markdown
Contributor Author

Addressed in de9d58b:

  • MEDIUM (precedence): flipped — settings env now wins over options.env and the process env (FORCE_PROMPT_CACHING_5M still wins first). The process-1h/settings-5m row now expects 5m, and I added a settings-over-options.env row (options 1h + settings 5m → 5m). Docblock and change fragment updated to the new order. I have not seen different behaviour live, so I took your minified-CLI reading.
  • LOW (test titles): fixed — the template now has 5 placeholders for the 5 row values.
  • LOW (source-selection coverage): added tests for the empty branches of resolveQuerySettingsEnv (explicit [] and the default full mode both resolve to nothing, no merge runs). The preset-append positive selection is still unpinned: the SDK merge engine isn't stubbable through overrideSdkBoundary (it doesn't cover resolveSettings), so covering it would need a test-only seam — leaving that as a follow-up.
  • LOW (per-attempt settingSources check in stream.ts): no new test — it would need a full stream-harness run; leaving as a follow-up.
  • LOW (managed-policy comment, stream.ts:101 resident skip): left as follow-ups, as you suggested.

Verification: 2988-...test.ts 13 passed (the 2 flipped/new rows fail on the pre-fix pin with expected '1h' to be '5m'); root bun run check exit 0; fragment compile --dry-run OK.

@code-yeongyu

Copy link
Copy Markdown
Owner

Thanks @surlymochan, the precedence fix is right. Re-review at de9d58b2f9: approve (non-blocking LOW notes). The settings value now wins over the environment as in Claude Code, row 9 is flipped, and the docblock and fragment are corrected.

Remaining LOWs, fine as follow-ups:

  • An invalid CLAUDE_CODE_PROMPT_CACHE_TTL in a loaded settings file (e.g. "10m") is skipped by the pin, which then writes 1h into options.env; Claude Code lays the settings value over that, so the pin no longer controls the lifetime on that lane.
  • The managed policy tier is still never read on managed lanes (claude-settings-env.ts:49, stream.ts:133-137).
  • Tests still don't cover the preset-append default (["user","project"]), an explicit source list, or the per-attempt settingSources gate.
  • resolveQuerySettingsEnv still runs on every stream, including resident sessions that never pin (stream.ts:101, :128).

The branch was 35 commits behind main, so I merged main into it (no conflicts) as 3670476. Next: the full suite, then merge on green; I'll post the merge SHA here.

@code-yeongyu
code-yeongyu merged commit 20fab91 into code-yeongyu:main Nov 10, 2026
11 checks passed
@code-yeongyu

Copy link
Copy Markdown
Owner

Merged into main as 20fab9139b (reviewed head 460d1a57f7, after a main merge). Thanks @surlymochan! The prompt-cache lifetime now follows the loaded settings files the way Claude Code does. It ships with the next senpi release.

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.

2 participants