Skip to content

bug: .env-resident ANTHROPIC_API_KEY shadows subscription auth in the per-spawn execution env (#1999), defeating SUB-003 auto-switch #2114

Closed

Description

@vybe

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() (sends remove_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:

  1. bug: credential injects mirror .env into 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 of INITIAL_ENV + .env + RUNTIME_OVERRIDES, with .env parsed fresh at every spawn. This is the intended security behavior (removed credentials stop applying) — but it also means .env became a new authoritative source for ANTHROPIC_API_KEY.
  2. lifecycle.py pops ANTHROPIC_API_KEY from Config.Env at container creation for subscription-backed agents, and the SUB-003 hot-reload passes remove_api_key=False with 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 .env file, which lives on the persistent workspace volume and survives every recreate.
  3. Result: a stale ANTHROPIC_API_KEY in .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.environ mutation, 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.
  4. 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)

Reproduction Steps

  1. Create a subscription-backed agent (valid CLAUDE_CODE_OAUTH_TOKEN, no ANTHROPIC_API_KEY in Config.Env) on a post-bug: credential injects mirror .env into the agent-server long-lived process env — a removed key keeps reaching every execution, invisible to /proc and docker exec #1999 base image.
  2. Inject an ANTHROPIC_API_KEY belonging to an account with no API credits into the agent's .env (any write path: credentials inject, agent editing its own file).
  3. Trigger any execution → fails with "Credit balance is too low" despite the valid subscription.
  4. 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:

  1. SUB-003 hot-reload should send remove_api_key=True for subscription-backed agents. The None force-unset runtime override is exactly the semantics bug: credential injects mirror .env into 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.
  2. Spawn-side guard: in build_execution_env(), when a CLAUDE_CODE_OAUTH_TOKEN runtime override is active (subscription auth is the explicit, most recent signal), drop ANTHROPIC_API_KEY from the .env merge — mirrors the existing "an explicit hot-reload wins" precedence rationale.
  3. Classifier/observability: when an auth-class failure occurs while a fresh OAuth override is in place AND .env carries ANTHROPIC_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. The env_drift report added by bug: credential injects mirror .env into 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

Related

Activity

  1. added
    priority-p1
    type-bug
    theme-reliability
    status-ready
    on Aug 11, 2026
  2. vybe commented on Aug 11, 2026

    @vybe
    ContributorAuthor

    /claim

  3. added
    status-in-progress
    on Aug 11, 2026
  4. self-assigned this
    on Aug 11, 2026
  5. github-actions commented on Aug 11, 2026

    @github-actions

    Claimed by @vybe. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/2114-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #2114
  6. removed
    status-ready
    on Aug 11, 2026
  7. added
    status-in-dev
    and removed
    status-in-progress
    on Aug 12, 2026
  8. removed
    status-in-dev
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p1theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions