Skip to content

fix(#703): strip the ambient ANTHROPIC_API_KEY on evidence, keep it when it is the only credential - #704

Closed
fdaviddpt wants to merge 3 commits into
mainfrom
fix/703
Closed

fdaviddpt wants to merge 3 commits into
mainfrom
fix/703

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #703. Supersedes #693 — the diagnosis and the original patch are @Mega-Therion's, and their commit is the first of the two here, unchanged. What this adds is the condition the review asked for.

Why the strip could not stay unconditional

ANTHROPIC_API_KEY out-ranks a claude.ai login in the CLI's credential order, so an ambient key becomes the nested summarizer's credential and an exhausted one fails every background save while the operator's interactive sessions carry on working. That is #703, and dropping the key fixes it.

But authenticating the CLI with ANTHROPIC_API_KEY alone — no login, no setup-token — is a normal, documented setup, and for those installs a strip leaves the child with no credential at all: every save failing, silently, in the same shape that took the reporter days to trace. The fix and the bug are the same outage pointed at different people.

So the key is dropped on evidence:

what is visible ambient key
CLAUDE_CODE_OAUTH_TOKEN from the host stripped
a configured haiku.oauth_token / REMEMBER_OAUTH_TOKEN stripped
a login at ~/.claude/.credentials.json (honours CLAUDE_CONFIG_DIR) stripped
nothing else kept — it is the only credential there is

The limit, stated rather than papered over

A claude.ai login held in the macOS Keychain is invisible to this process, and probing the keychain would prompt the operator and read a secret we have no business reading. So auto keeps the key for those operators — today's behaviour, not a new failure — and they say so once:

{ "haiku": { "anthropic_api_key": "strip" } }

auto (default), keep, strip. An unrecognised value falls back to auto and says so in the daily log, rather than grading silently as one of the two behaviours.

Discoverability

The other half of #703 is that nothing named the variable. A failure that looks like a credential failure now carries it into the RuntimeError, which save-session.sh surfaces to hook-errors.log — where the operator is already looking, instead of the daily log alone (#694):

claude exited 1: Credit balance is too low -- note that ANTHROPIC_API_KEY was set in this
environment and was passed to the nested CLI, where it OUT-RANKS a claude.ai login; if that
key is exhausted or wrong, this is what failed. Set `haiku.anthropic_api_key` to "strip" in
config.json to keep it out of the summarizer, or "keep" to silence this note (#703)

The marker list is deliberately narrow. A hint that fires on every failure points unrelated outages at an innocent variable, which is the misdirection this issue is about.

Tests

Every strip case has its keep twin, because this is a repo rule and here it is load-bearing: an unconditional strip and an unconditional keep would each pass half the suite.

  • strips when the host supplied CLAUDE_CODE_OAUTH_TOKEN / when an OAuth token is configured / when a login file exists
  • keeps when it is the only credential — the twin that fails against @Mega-Therion's original patch, and against any future refactor that strips again
  • policy strip overrides an invisible login; policy keep overrides the strip; an unrecognised value warns and falls back
  • the failure hint fires on a credit error with the key set, and does not fire with no key set (negative control) or on a prompt-length failure with the key set (wrong-cause control)

The fixture relocates HOME, REMEMBER_DIR and the token env vars: without it the decision under test is the developer's own login, and the result flips between a laptop and CI.

pytest: 2496 passed, 58 skipped locally (macOS). The Windows and Linux legs are reasoned, not observed, from here — nothing in this change is platform-specific beyond expanduser, which the fixture pins on both shapes via HOME and USERPROFILE.

Docs: docs/configuration.md gets the row, config.example.json ships "anthropic_api_key": "auto", and tests/test_config_contract.py records that this key is read in Python rather than through config().

🤖 Generated with Claude Code

[AI-generated]

Mega-Therion and others added 2 commits September 14, 2026 11:58
ANTHROPIC_API_KEY, when present anywhere in the host environment, takes
precedence over the operator's claude.ai login for the nested claude -p
call the summarizer spawns -- the CLI resolves credentials in that
order. A key with an exhausted or unfunded balance then fails every
background summarize silently: save-session.sh --force exits 1 at
session end with "Credit balance is too low", while the operator's own
interactive sessions keep working fine off the login, so nothing about
the failure points at this var without reading the child's own stderr.

Reproduced in production: repeated "save-session.sh --force exited 1"
warnings traced via the daily narrative log to exactly this cause,
compounded once by a genuine rate limit and separately by this auth
routing issue. ANTHROPIC_API_KEY was never a "parent session" marker
the way CLAUDE_CODE_* is; _child_env() now strips it for the same
reason it already strips CLAUDE_PROJECT_DIR -- the child should
authenticate as if launched fresh, not inherit a credential the
operator did not choose for this subprocess.

Adds test_call_haiku_strips_anthropic_api_key alongside the existing
parent-session-stripping test. 80/80 tests pass.
…hen it is the only credential

@Mega-Therion diagnosed this and the strip itself is theirs; this commit makes
it conditional, which is what the review asked for.

An unconditional strip does not remove the reported outage, it relocates it:
authenticating the CLI with ANTHROPIC_API_KEY alone is normal and documented,
and for those installs dropping the key leaves the nested claude -p with no
credential at all -- every background save failing silently, the same shape
that took the reporter days to trace.

So the key is now dropped only when another credential is visible
(CLAUDE_CODE_OAUTH_TOKEN, a configured OAuth token, or a login at
~/.claude/.credentials.json) and kept when it is the only one there is. A
login held in an OS keychain is invisible to this process, so auto keeps the
key for those operators -- todays behaviour, not a new failure -- and the new
haiku.anthropic_api_key config key (auto|keep|strip) is how they overrule it.
An unrecognised value falls back to auto and says so, rather than grading
silently as one of the two behaviours.

A failure that looks like a credential failure now names the variable and the
config key in the RuntimeError, so it reaches hook-errors.log where the
operator is already looking rather than the daily log alone (#694). The marker
list is deliberately narrow: a hint on every failure would point unrelated
outages at an innocent variable.

Tests pair every strip case with its keep twin -- an unconditional strip and
an unconditional keep each fail here -- and the failure hint has both a
negative control (no key set) and a wrong-cause control (key set, prompt-length
failure).

Co-Authored-By: Max <noreply>
CodeQL was right: the warning quoted the rejected config value, and this keys
NAME invites an operator to paste an actual API key into it. A typo would then
have written that credential to the daily log in clear text -- a leaked secret
on disk as the punishment for a misspelling.

The warning now names the length and type, the legal values and the fallback,
and says explicitly that the value is withheld and why. The test asserts the
absence (a pasted key-shaped value must not appear) and gains its positive
control: a recognised value warns about nothing, so warns-on-bad-value cannot
silently become warns-on-everything.

Co-Authored-By: Max <noreply>
@fdaviddpt

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate. This existed only because pushing to the author's own branch was blocked at the time; the retry succeeded, so the identical commits now sit on #693, where they belong — @Mega-Therion reported the bug, diagnosed it and wrote the first commit, and their PR should be the one that lands.

Same two commits, same tests, no work lost. Continue at #693.

[AI-generated]

@fdaviddpt fdaviddpt closed this Sep 16, 2026
@fdaviddpt
fdaviddpt deleted the fix/703 branch September 16, 2026 07:14
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.

An ambient ANTHROPIC_API_KEY out-ranks the operator's login for the nested summarizer, and an exhausted one fails every background save silently

2 participants