Repository navigation
Description
Summary
Since #1999 (f744f349, "rebuild the execution env per spawn so a removed .env key stops applying"), build_execution_env() merges the agent's ~/.env file into the environment of every spawned runtime subprocess. On a subscription-backed agent whose .env happens to contain a stale ANTHROPIC_API_KEY, every claude spawn now receives that key — and Claude Code prefers an API key over CLAUDE_CODE_OAUTH_TOKEN — so all executions hard-fail (e.g. "Credit balance is too low" when the key's account has no credits) despite a valid, working subscription token. SUB-003 auto-switch then cycles the agent through every registered subscription without effect, mis-attributes the identical failure to each one, and fills the 2h skip-list until it reports "no viable alternative subscription is available" while every subscription is actually healthy.
Component
Agent Runtime (agent-server execution_env.py) + Backend (SUB-003 subscription_auto_switch.py)
Priority
P1
Error
Execution failed with no output (exit code 1): Credit balance is too low. To resolve: (1) add credits to your Anthropic account at console.anthropic.com, or (2) assign a subscription token with available usage in Settings -> Subscriptions.
Every execution fails within seconds of spawn (auth rejection, not task failure), regardless of which subscription is assigned.
Location
- File:
docker/base-image/agent_server/services/execution_env.py—build_execution_env()(precedence: container baseline →.env→ runtime overrides → extra) - File:
src/backend/services/subscription_auto_switch.py—_hot_reload_subscription_token()(sendsremove_api_key=False) - File:
docker/base-image/agent_server/routers/credentials.py—/api/credentials/reload-token
Root Cause
Interaction between #1999 and #1089/SUB-003:
- bug: credential injects mirror
.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 makes the execution env a pure function ofINITIAL_ENV + .env + RUNTIME_OVERRIDES, with.envparsed fresh at every spawn. This is the intended security behavior (removed credentials stop applying) — but it also means.envbecame a new authoritative source forANTHROPIC_API_KEY. lifecycle.pypopsANTHROPIC_API_KEYfrom Config.Env at container creation for subscription-backed agents, and the SUB-003 hot-reload passesremove_api_key=Falsewith the comment "subscription-backed agents never carry ANTHROPIC_API_KEY in env (popped at create time, lifecycle.py)". That assumption was true for Config.Env, but nothing cleans the.envfile, which lives on the persistent workspace volume and survives every recreate.- Result: a stale
ANTHROPIC_API_KEYin.env(from an old credential push, an agent writing its own file, an export that captured it) silently shadows the subscription token on every spawn. Nothing in the switch path can remove it:os.environmutation, the runtime override, and the writable-layer token file were all verified delivering the correct working OAuth token — and the next spawn still authenticated with the dead API key. - Secondary defect: the resulting auth failures are recorded as rate-limit/auth events against whichever subscription is currently assigned, so healthy subscriptions get skip-listed for 2h each. After one rotation the agent reports "no viable alternative subscription is available" — the operator-facing signal actively points away from the real cause.
Field Observation (sanitized)
- Subscription-backed agent, ~68 scheduled executions/day, 100% success for 2 weeks.
- An
ANTHROPIC_API_KEYfor an out-of-credits account had been sitting in its~/.envsince a credential push 2 weeks earlier. (It briefly caused identical failures on the day it was written — pre-bug: credential injects mirror.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 ghost-env behavior — then went dormant after a recreate.) - The day the fleet's agents were rebuilt onto the post-bug: credential injects mirror
.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 base image, every execution began failing in seconds with "Credit balance is too low". - SUB-003 rotated through all 4 registered subscriptions (each switch logged
restart_result: 'hot_reloaded'); the persisted override token after the last switch was byte-identical (sha256) to the token a healthy peer agent was succeeding with in the same minute. The next spawn still failed. - Removing
ANTHROPIC_API_KEYfrom the agent's.envresolves it (per bug: credential injects mirror.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999's own design, the removal takes effect at the next spawn).
Reproduction Steps
- Create a subscription-backed agent (valid
CLAUDE_CODE_OAUTH_TOKEN, noANTHROPIC_API_KEYin Config.Env) on a post-bug: credential injects mirror.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 base image. - Inject an
ANTHROPIC_API_KEYbelonging to an account with no API credits into the agent's.env(any write path: credentials inject, agent editing its own file). - Trigger any execution → fails with "Credit balance is too low" despite the valid subscription.
- Observe SUB-003 switching subscriptions with successful hot-reloads and no effect; after one rotation, "no viable alternative subscription is available".
Suggested Fix
Layered; (1) is the minimal correct fix:
- SUB-003 hot-reload should send
remove_api_key=Truefor subscription-backed agents. TheNoneforce-unset runtime override is exactly the semantics bug: credential injects mirror.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 built for this ("a plain dict merge cannot express force-unset") — it suppresses the.env-sourced key at spawn time without touching the file. - Spawn-side guard: in
build_execution_env(), when aCLAUDE_CODE_OAUTH_TOKENruntime override is active (subscription auth is the explicit, most recent signal), dropANTHROPIC_API_KEYfrom the.envmerge — mirrors the existing "an explicit hot-reload wins" precedence rationale. - Classifier/observability: when an auth-class failure occurs while a fresh OAuth override is in place AND
.envcarriesANTHROPIC_API_KEY, emit an operator notification ("API key shadowing subscription auth") instead of recording the failure against the subscription — prevents healthy subscriptions from being skip-listed and surfaces the actual cause. Theenv_driftreport added by bug: credential injects mirror.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 already exposes the raw signal.
Environment
- Regression first present:
f744f349(bug: credential injects mirror.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999), post-v0.8.5 dev - Observed on: dev @
f1809975, prod compose, agents rebuilt onto the bug: credential injects mirror.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 base image the same day - Any agent whose
.envcontains a staleANTHROPIC_API_KEYwill hit this on its next rebuild against a post-bug: credential injects mirror.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 base image
Related
- bug: credential injects mirror
.envinto the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 (per-spawn execution env rebuild — the regression vector) - feat: credential rotation via hot-reload, not container recreate #1089 (subscription token hot-reload — the mechanism defeated)
- bug: SUB-003 auto-switch has no per-agent lock — concurrent 429s race the restart #799 (per-agent switch lock — switch serialization, working as designed here)
docker/base-image/agent_server/services/execution_env.pysrc/backend/services/subscription_auto_switch.py
Activity
vybe commentedon Aug 11, 2026
/claim
github-actions commentedon Aug 11, 2026
Claimed by @vybe. Label
status-in-progressadded.Next steps:
feature/2114-<short-slug>Fixes #2114fix(subscriptions): stop .env-resident ANTHROPIC_API_KEY shadowing su…
fix(workspace): roster shows container availability honestly — never …
fix(subscriptions): a rejected token is not a rate limit (#2354)
fix(workspace): thread titles authenticate as the agent, not as the i…
fix(credentials): subscription agents import a committed .credentials…
fix(agent-runtime): drop the startup credentials auto-import that cal…