From 3c4b03ba2b24e44eecf98281832da74c457849ea Mon Sep 17 00:00:00 2001 From: Tarsis Fernandes Date: Tue, 3 Nov 2026 11:26:28 -0500 Subject: [PATCH] fix(inference): keep custom ANTHROPIC_BASE_URL in nested claude child env The billing-safety scrub in the three spawn sites (Inference.ts, AgentJob.ts jobEnv, CarrierProbe.ts runChild) unconditionally deleted ANTHROPIC_BASE_URL and both API keys. On installs whose carrier is a custom base URL (local LLM proxy, gateway), the scrubbed child targets api.anthropic.com with no credentials and every nested call 401s. Scrub the keys only when the default Anthropic API is the target; a configured ANTHROPIC_BASE_URL is the principal's explicit carrier and is kept. Behavior without one is unchanged. The spawn_env_scrubbed log now reports what was actually scrubbed. --- LifeOS/install/LIFEOS/TOOLS/AgentJob.ts | 8 ++++++-- LifeOS/install/LIFEOS/TOOLS/CarrierProbe.ts | 14 ++++++++++---- LifeOS/install/LIFEOS/TOOLS/Inference.ts | 20 +++++++++++++------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/LifeOS/install/LIFEOS/TOOLS/AgentJob.ts b/LifeOS/install/LIFEOS/TOOLS/AgentJob.ts index f597b6064d..454af04cc8 100644 --- a/LifeOS/install/LIFEOS/TOOLS/AgentJob.ts +++ b/LifeOS/install/LIFEOS/TOOLS/AgentJob.ts @@ -210,7 +210,11 @@ function loadDotEnv(): Record { function jobEnv(job: Job, extra: Record = {}): NodeJS.ProcessEnv { const env: Record = { ...loadDotEnv(), ...process.env }; - delete env.CLAUDECODE; delete env.ANTHROPIC_API_KEY; delete env.ANTHROPIC_AUTH_TOKEN; delete env.ANTHROPIC_BASE_URL; + delete env.CLAUDECODE; + // A custom ANTHROPIC_BASE_URL is the principal's explicit carrier (local LLM + // proxy, gateway) and is kept; the key scrub only guards Anthropic billing, + // which is unreachable through a custom base URL. + if (!env.ANTHROPIC_BASE_URL) { delete env.ANTHROPIC_API_KEY; delete env.ANTHROPIC_AUTH_TOKEN; } env.LIFEOS_NOTIFICATION_CHANNEL = "headless"; env.LIFEOS_ROOT = LIFEOS_ROOT; env.AGENTJOB_ID = job.id; @@ -345,7 +349,7 @@ async function runDa(job: Job, prompt: string, env: NodeJS.ProcessEnv, timeoutMs ...claudeToolFlags(job, LIFEOS_ROOT, stateDir), "--append-system-prompt", JOB_SOUL, ]; - log(job.id, "spawn_env_scrubbed", { scrubbed: ["CLAUDECODE", "ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL"], channel: "headless", model: args[2] }); + log(job.id, "spawn_env_scrubbed", { scrubbed: ["CLAUDECODE", ...(env.ANTHROPIC_BASE_URL ? [] : ["ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN"])], channel: "headless", model: args[2] }); return await new Promise((resolve) => { const proc = spawn(resolveClaudeBin(), args, { cwd: ensureJobCwd(), env, stdio: ["pipe", "pipe", "pipe"] }); let out = "", err = ""; diff --git a/LifeOS/install/LIFEOS/TOOLS/CarrierProbe.ts b/LifeOS/install/LIFEOS/TOOLS/CarrierProbe.ts index 7a58d85541..ea94446fbd 100644 --- a/LifeOS/install/LIFEOS/TOOLS/CarrierProbe.ts +++ b/LifeOS/install/LIFEOS/TOOLS/CarrierProbe.ts @@ -111,15 +111,21 @@ function runChild(): Promise<{ envelope: Record | null; raw: st // Nested-session guard: hooks run inside Claude Code's environment. delete env.CLAUDECODE; // BILLING: subscription via OAuth — both keys outrank the OAuth token in - // Anthropic's precedence chain, and bun auto-loads ~/.claude/.env. Scrub. - delete env.ANTHROPIC_API_KEY; - delete env.ANTHROPIC_AUTH_TOKEN; + // Anthropic's precedence chain, and bun auto-loads ~/.claude/.env. Scrub, + // but only against the default Anthropic API: a configured + // ANTHROPIC_BASE_URL is the principal's explicit carrier (local LLM proxy, + // gateway), and scrubbing it strands the child on api.anthropic.com 401s. + if (!env.ANTHROPIC_BASE_URL) { + delete env.ANTHROPIC_API_KEY; + delete env.ANTHROPIC_AUTH_TOKEN; + } // Headless subprocess: never the desktop voice channel (2026-08-14 leak). // Unset AND `desktop` collapse to headless (public issue #1975, @emarcelcom). env.LIFEOS_NOTIFICATION_CHANNEL = !env.LIFEOS_NOTIFICATION_CHANNEL || env.LIFEOS_NOTIFICATION_CHANNEL === 'desktop' ? 'headless' : env.LIFEOS_NOTIFICATION_CHANNEL; - delete env.ANTHROPIC_BASE_URL; + // ANTHROPIC_BASE_URL is kept when set: the billing scrub above only runs + // against the default Anthropic API, where a stray key could bill per-token. const prompt = 'Use your agent-dispatch tool (named Agent or Task) exactly once: ' + diff --git a/LifeOS/install/LIFEOS/TOOLS/Inference.ts b/LifeOS/install/LIFEOS/TOOLS/Inference.ts index dbc9a86a6f..cb0a15002a 100755 --- a/LifeOS/install/LIFEOS/TOOLS/Inference.ts +++ b/LifeOS/install/LIFEOS/TOOLS/Inference.ts @@ -460,9 +460,17 @@ async function inferenceAttempt(options: InferenceOptions, modelOverride?: strin // puts BOTH ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN above CLAUDE_CODE_OAUTH_TOKEN, // so either one in env will silently override OAuth. Bun auto-loads ~/.claude/.env // into child processes, and some MCP/plugin setups export ANTHROPIC_AUTH_TOKEN — - // either path leaks subscription work onto API-key billing. Scrub both. - delete env.ANTHROPIC_API_KEY; - delete env.ANTHROPIC_AUTH_TOKEN; + // either path leaks subscription work onto API-key billing. Scrub both, but + // only when the default Anthropic API is the target: a configured + // ANTHROPIC_BASE_URL is the principal's explicit carrier (local LLM proxy, + // gateway), no Anthropic billing is reachable from it, and scrubbing would + // strand the child on api.anthropic.com with no credentials — every nested + // call 401s and retries until timeout (2026-11-03 local-LLM install, memory + // reviewer parse-failed on api_error_status=401). + if (!env.ANTHROPIC_BASE_URL) { + delete env.ANTHROPIC_API_KEY; + delete env.ANTHROPIC_AUTH_TOKEN; + } // Headless subprocess: never the desktop voice channel, even when spawned // from a terminal session whose TERM the child would inherit. Explicit // channels (imessage, ...) pass through. (2026-08-14 scheduled voice leak.) @@ -472,10 +480,8 @@ async function inferenceAttempt(options: InferenceOptions, modelOverride?: strin env.LIFEOS_NOTIFICATION_CHANNEL = !env.LIFEOS_NOTIFICATION_CHANNEL || env.LIFEOS_NOTIFICATION_CHANNEL === 'desktop' ? 'headless' : env.LIFEOS_NOTIFICATION_CHANNEL; - // Also scrub ANTHROPIC_BASE_URL: a local proxy (e.g. a LiteLLM gateway) would - // still be targeted after the child loses its credentials above, so every - // nested call fails auth (401) and retries until timeout. Force real Anthropic API. - delete env.ANTHROPIC_BASE_URL; + // ANTHROPIC_BASE_URL is kept when set: the billing scrub above only runs + // against the default Anthropic API, where a stray key could bill per-token. // The CLI finds its subscription login in the macOS Keychain by USER. A workflow step or launchd job may hand the // child only PATH and HOME, and then every call answers "Not logged in" (a scheduled judge call // can fail this way on its first run).