Business operating system for Claude Code — 57 skills, 21 agents, smart daemon. Unified inbox (WhatsApp/Email/Slack/Telegram), autonomous PR merge, full-AWS monitoring, revenue (Stripe+RevenueCat), e-commerce (Shopify), marketing (Klaviyo/Meta/GA4), voice (Bland/ElevenLabs), APM (Datadog/NewRelic/OTEL), YOLO mode.
Shell agent-teamsai-agentai-automationanthropicautonomous-agentawsbusiness-operationsclaudeclaude-codeclaude-code-plugindevopsecommerceinbox-zeroklaviyomarketingmcpmorning-briefingpr-automationshopifyterminal
fix(rotation): remove duplicate API_KEY/AUTH_TOKEN/OAUTH_TOKEN unset in bg-respawn atomic fallback (#698)
Aurora Capital626b8cff3216
diff --git a/claude-ops/scripts/account-rotation/bg-respawn.mjs b/claude-ops/scripts/account-rotation/bg-respawn.mjsindex 041ef4cc..71b131fd 100644--- a/claude-ops/scripts/account-rotation/bg-respawn.mjs+++ b/claude-ops/scripts/account-rotation/bg-respawn.mjs@@ -1,3 +1,4 @@+import { loadClaudeHarnessEnv } from './claude-harness-env.mjs';// ── Background-session respawn after rotation ────────────────────────────────// Daemon-hosted `claude --bg` sessions have no TTY, so the /login-injection// path in rotate.mjs can never reach them. They also never register in the@@ -19,29 +20,16 @@//// Used by rotate.mjs (post-rotation) and daemon.mjs (periodic deferred sweep).-import {- readFileSync,- readdirSync,- writeFileSync,- unlinkSync,- statSync,- existsSync,- openSync,- readSync,- closeSync,- mkdirSync,- rmSync,-} from 'fs';+import { readFileSync, readdirSync, writeFileSync, appendFileSync, unlinkSync, statSync, existsSync } from 'fs';import { execFileSync, execSync } from 'child_process';import { join, dirname } from 'path';import { homedir } from 'os';import { fileURLToPath } from 'url';-import { extractAccessToken, recordSessionLease, pickAccountForSession, readVaultToken } from './session-router.mjs';-import { applyOAuthEnv, applyBedrockEnv, scrubBedrockEnv } from './provider-env.mjs';+import { getTokenForSession, extractAccessToken, readLeases, recordSessionLease } from './session-router.mjs';+import { applyCrsSessionSettings, readRouteState } from './route-state.mjs';const __dirname = dirname(fileURLToPath(import.meta.url));const CONFIG_PATH = join(__dirname, 'config.json');-const STATE_PATH = join(__dirname, 'state.json');function readConfig() {try {@@ -51,23 +39,71 @@ function readConfig() {}}-function readState() {+const SESSIONS_DIR = join(homedir(), '.claude', 'sessions');+const RESPAWNED_MARKER = (id) => `/tmp/claude-rotation-respawned-${id}`;+const DEFERRED_MARKER = (id) => `/tmp/claude-respawn-deferred-${id}`;+const RESPAWN_THROTTLE_MS = 10 * 60_000; // never respawn the same session twice in 10 min+const BUSY_FORCE_AFTER_MS = 90 * 60_000; // busy this long after rotation → respawn anyway++// Dashboard marker: present ⇔ at least one session was last respawned to DIRECT+// because CRS was unhealthy. Lets fleet-status explain a "direct" session as+// "relay-down" rather than a silent/unexplained desync. Cleared whenever a+// session successfully routes via a healthy relay.+const CRS_RELAY_DOWN_MARKER = '/tmp/claude-crs-relay-down';+const HEALTH_WATCH_STATE = join(__dirname, 'crs-health-watch.state.json');+const HEALTH_STATE_FRESH_MS = 90_000; // trust the health-watch verdict if <90s old (it ticks every 60s)++// TTY hygiene helper: on abnormal paths or exit, attempt to restore sane mode so+// background errors or claude subprocs do not leave ^C ^[ etc. in a raw-mode TUI.+function resetTty() {try {- return JSON.parse(readFileSync(STATE_PATH, 'utf8'));+ execSync('stty sane 2>/dev/null || true', { stdio: 'ignore', timeout: 800 });+ } catch {}+}+process.once('exit', resetTty);++// Cheap CRS health read: prefer the recent crs-health-watch verdict (no network),+// fall back to a 3s /health curl when the state file is missing/stale. Returns+// boolean healthy. Keeps respawn fast and avoids hammering the relay per-session.+function crsHealthyCheap() {+ try {+ const st = JSON.parse(readFileSync(HEALTH_WATCH_STATE, 'utf8'));+ const mtime = statSync(HEALTH_WATCH_STATE).mtimeMs;+ if (Date.now() - mtime < HEALTH_STATE_FRESH_MS) {+ // mode 'crs' with down=0 ⇒ relay solid; mode 'fail-closed' ⇒ relay down.+ if (st.mode === 'fail-closed') return false;+ if (st.mode === 'crs' && (st.down || 0) === 0) return true;+ }+ } catch {}+ // Stale/absent state → authoritative live probe.+ try {+ const hc = execFileSync(+ 'curl',+ ['-sf', '-o', '/dev/null', '-w', '%{http_code}', '--max-time', '3', 'http://127.0.0.1:3005/health'],+ { encoding: 'utf8', timeout: 5000 },+ ).trim();+ return hc === '200';} catch {- return { activeAccount: null, accounts: {} };+ return false;}}-function accountKey(a) {- return a.label || a.email;-}+// Graceful rotation-over stagger: respawn bg sessions ONE AT A TIME with this gap+// so the fleet doesn't all re-auth onto the new account in the same instant and+// stampede it into a rate-limit. Default 5s; tune via+// CLAUDE_ROTATION_SESSION_STAGGER_MS (0 disables). Kept in sync with rotate.mjs.+const SESSION_STAGGER_MS = (() => {+ const v = parseInt(process.env.CLAUDE_ROTATION_SESSION_STAGGER_MS ?? '', 10);+ return Number.isFinite(v) && v >= 0 ? v : 5000;+})();-const SESSIONS_DIR = join(homedir(), '.claude', 'sessions');-const RESPAWNED_MARKER = (id) => `/tmp/claude-rotation-respawned-${id}`;-const DEFERRED_MARKER = (id) => `/tmp/claude-respawn-deferred-${id}`;-const RESPAWN_THROTTLE_MS = 10 * 60_000; // never respawn the same session twice in 10 min-const BUSY_FORCE_AFTER_MS = 90 * 60_000; // busy this long after rotation → respawn anyway+// Synchronous sleep (this module is sync end-to-end; respawn uses execSync).+function sleepSync(ms) {+ if (ms <= 0) return;+ try {+ execSync(`sleep ${(ms / 1000).toFixed(2)}`, { timeout: ms + 5000 });+ } catch {}+}let _claudeBin = null;function claudeBin() {@@ -112,47 +148,6 @@ function markerFresh(path, maxAgeMs) {}}-// /loop sessions are idle BY DESIGN between iterations, and their loop timer-// (CronCreate/ScheduleWakeup) is session-only — a respawn wipes it and the-// loop never fires again (observed 2026-06-12: logmonitor 41e6eaf0 killed-// every ~10min by the idle-respawn pass, 15m loop never survived to fire).-// Treat them like busy: defer, force-respawn only after BUSY_FORCE_AFTER_MS.-export function isLoopSession(id) {- let st;- try {- st = JSON.parse(readFileSync(join(homedir(), '.claude', 'jobs', String(id), 'state.json'), 'utf8'));- } catch {- return false;- }- if (/^\/loop\b/.test(st.detail || '')) return true;- // `detail` is the live narration line and gets overwritten — the durable- // signal is a /loop invocation (or autonomous-loop sentinel) in the- // transcript. Scan the tail only (loops re-arm near the end).- const transcript =- st.linkScanPath ||- (st.sessionId &&- join(- homedir(),- '.claude',- 'projects',- String(st.cwd || st.originCwd || '').replace(/\//g, '-'),- `${st.sessionId}.jsonl`,- ));- if (!transcript || !existsSync(transcript)) return false;- try {- const size = statSync(transcript).size;- const TAIL = 1024 * 1024; // 1MB tail- const fd = openSync(transcript, 'r');- const buf = Buffer.alloc(Math.min(TAIL, size));- readSync(fd, buf, 0, buf.length, Math.max(0, size - buf.length));- closeSync(fd);- const tail = buf.toString('utf8');- return tail.includes('<<autonomous-loop') || tail.includes('<command-name>/loop</command-name>');- } catch {- return false;- }-}-/** Enumerate live daemon-hosted bg sessions from ~/.claude/sessions/*.json. */export function listLiveBgSessions() {const out = [];@@ -180,62 +175,38 @@ export function listLiveBgSessions() {return out;}-// ── Post-swap continuation injection (F4) ───────────────────────────────────-// `claude respawn <id>` re-execs a session with the transcript preserved but the-// agent does NOT auto-resume its task — it just idles waiting for input. A-// continuation turn nudges it to pick up where it left off.-//-// HAZARD: a `claude --resume <sid>` turn that overlaps the respawn (or another-// --resume on the same sid) corrupts session state. We could NOT confirm a-// fully corruption-safe mechanism in this environment, so per spec this is-// DEFAULT-OFF behind CLAUDE_ROTATION_INJECT_CONTINUATION=1. When enabled we-// guard three ways: (a) only after doRespawn has resolved the NEW pid (respawn-// settled), (b) a per-sid lock dir so two daemons/ticks can't double-inject,-// (c) a short settle delay. Even so, treat as experimental until a maintainer-// validates it against a live fleet.-const INJECT_CONTINUATION_ENABLED = process.env.CLAUDE_ROTATION_INJECT_CONTINUATION === '1';-const CONTINUATION_PROMPT =- 'Your session was hot-swapped from Bedrock to OAuth to stop metered spend. ' +- 'Continue your previous work from where you left off.';--/**- * Inject a one-shot continuation turn into a freshly-respawned session.- * No-op unless CLAUDE_ROTATION_INJECT_CONTINUATION=1. Per-sid lock prevents- * concurrent --resume (which corrupts state). Best-effort; never throws.- * @param {string} sid session/job id- * @param {(m:string)=>void} log- */-export function injectContinuation(sid, log) {- if (!INJECT_CONTINUATION_ENABLED) return;- const lockDir = `/tmp/claude-continuation-lock-${sid}`;- try {- // Atomic mkdir lock — fails if another inject is already in flight for this sid.- mkdirSync(lockDir);- } catch {- log(`[bg-respawn] continuation inject for ${sid} skipped — lock held`);- return;- }- try {- // Brief settle so the re-exec'd session is fully attached before --resume.- try {- execSync('sleep 2');- } catch {}- execFileSync(claudeBin(), ['--resume', String(sid), CONTINUATION_PROMPT], {- timeout: 30_000,- stdio: 'ignore',- });- log(`[bg-respawn] injected continuation turn into ${sid}`);- } catch (e) {- log(`[bg-respawn] continuation inject for ${sid} failed: ${e.message?.slice(0, 80)}`);- } finally {- try {- rmSync(lockDir, { recursive: true, force: true });- } catch {}+export function doRespawn(session, log, opts = {}) {+ const route = readRouteState();+ if (route.mode === 'fail-closed') {+ // OPT-IN (2026-06-22, CRS-connection auto-recovery): a connection-wedged+ // session must be respawnable DURING a confirmed CRS outage (fail-closed is+ // exactly that state). In fail-closed, settings.json already has the CRS env+ // stripped, and the FINAL ATOMIC NORMALIZATION below strips any leaked CRS+ // base/token — so the respawn boots cleanly on direct keychain OAuth (the+ // intended "fall back to local OAuth"). Without the opt-in, keep refusing so+ // no existing caller (rotate.mjs post-rotation, daemon deferred sweep) changes+ // behavior. crs-health-watch's rotate-magic keeps the keychain account fresh.+ if (!opts.allowFailClosedDirect) {+ log(+ `[bg-respawn] route=fail-closed; refusing to respawn session ${session.id} without healthy CRS OAuth or confirmed Bedrock`,+ );+ return false;+ }+ log(+ `[bg-respawn] route=fail-closed + allowFailClosedDirect — respawning session ${session.id} onto direct keychain OAuth (CRS-connection recovery)`,+ );}-}-export function doRespawn(session, log) {+ // (resetTty is defined at module scope with process handler)const stateFile = join(homedir(), '.claude', 'jobs', String(session.id), 'state.json');+ let state = null;+ if (existsSync(stateFile)) {+ try {+ state = JSON.parse(readFileSync(stateFile, 'utf8'));+ } catch {+ state = null;+ }+ }if (!existsSync(stateFile)) {log(`[bg-respawn] session ${session.id} (pid ${session.pid}) has no saved state (spare) — terminating process to refresh`,@@ -257,51 +228,268 @@ export function doRespawn(session, log) {try {const childEnv = { ...process.env };- let routingKey = null;try {const config = readConfig();- const state = readState();- // Re-resolve routing before respawn so stale bedrock leases and unleased- // sessions pick up OAuth after rotation instead of inheriting Bedrock env.- routingKey = pickAccountForSession(session.id, config, state);+ const tokenJson = getTokenForSession(session.id, config);+ const token = tokenJson ? extractAccessToken(tokenJson) : null;+ if (token) {+ childEnv.CLAUDE_CODE_OAUTH_TOKEN = token;+ delete childEnv.CLAUDE_CODE_USE_BEDROCK;+ delete childEnv.ANTHROPIC_MODEL;+ log(`[bg-respawn] Injecting CLAUDE_CODE_OAUTH_TOKEN for session ${session.id}`);+ } else {+ // BEDROCK FALLBACK DISABLED (2026-06-13): the prior else-branch injected+ // CLAUDE_CODE_USE_BEDROCK + the unservable `anthropic.claude-fable-5`,+ // which stranded sessions ("Claude Fable 5 is currently unavailable").+ // With no per-session token, respawn on the global keychain instead —+ // never boot a child onto Bedrock. Strip any inherited Bedrock env too.+ delete childEnv.CLAUDE_CODE_USE_BEDROCK;+ delete childEnv.ANTHROPIC_MODEL;+ log(+ `[bg-respawn] no per-session token for ${session.id}; respawning on global keychain (bedrock fallback disabled)`,+ );+ }+ } catch (err) {+ log(`[bg-respawn] Failed to retrieve session token: ${err.message}`);+ }- let tokenJson = null;- if (routingKey && routingKey !== 'bedrock') {- const account = config.accounts.find((a) => accountKey(a) === routingKey);- if (account) tokenJson = readVaultToken(account);+ // CRS relay allowlist: override base URL + auth for specific sessions (scoped partial cutover).+ // Rollback: delete crs-allowlist.json + respawn listed sessions (removes CRS env, falls back to keychain).+ //+ // ATOMIC INVARIANT (2026-06-14, root-caused 401 loop): ANTHROPIC_BASE_URL and the cr_+ // relay token MUST be set together or not at all. If base→CRS but the Bearer token is a+ // real OAuth token (non-cr_ prefix), CRS rejects every request with+ // {"error":"Invalid API key","message":"Invalid API key format"} → HTTP 401+ // which Claude Code surfaces as "API Error: 401 Invalid API key format" and retries+ // forever. The earlier bug: the cr_ key lived in a job tmp dir that got cleaned up, so the+ // readFileSync threw, the catch logged "non-fatal", and the session respawned with CRS base+ // URL + real OAuth token → infinite loop. Defenses below:+ // 1. Default key path is now a STABLE location (not job tmp).+ // 2. On ANY failure to obtain a cr_-prefixed key, we DELETE ANTHROPIC_BASE_URL from+ // childEnv so the session falls back to direct Anthropic — never CRS-with-wrong-token.+ const CRS_ALLOWLIST_PATH = join(__dirname, 'crs-allowlist.json');+ let crsActive = false; // true ⇔ a VALID base+cr_ pair was applied (the only state in which CRS routing is allowed)+ let crsBaseUrl = 'http://127.0.0.1:3005/api';+ let crsRelayKey = '';+ try {+ if (existsSync(CRS_ALLOWLIST_PATH)) {+ const al = JSON.parse(readFileSync(CRS_ALLOWLIST_PATH, 'utf8'));+ crsBaseUrl = al.baseUrl || crsBaseUrl;+ // CARVE-OUT (2026-06-15): explicit per-session exclusions that MUST stay+ // off CRS even under the global default — orchestrator 0d298397, app-store+ // sonnet build e35cdd60, driver 0b465ce3. The global default lives in+ // settings.json env, so an excluded session would otherwise inherit+ // base=CRS+cr_ at startup; we strip the inherited CRS base here and leave+ // crsActive=false so it falls back to keychain/direct (the final atomic+ // normalization below then drops any leaked cr_ token too). Reversible:+ // remove the id from al.exclude.+ const excluded = Array.isArray(al.exclude) && al.exclude.includes(String(session.id));+ if (excluded) {+ delete childEnv.ANTHROPIC_BASE_URL;+ delete childEnv.ANTHROPIC_API_BASE;+ log(+ `[bg-respawn] CRS-allowlist: session ${session.id} is an explicit carve-out (al.exclude) — forcing OFF CRS, stripping inherited CRS base variables (keychain/direct)`,+ );+ }+ // GLOBAL CRS (2026-06-14, Sam directive "by default all sessions route there, new AND existing"):+ // al.global===true routes EVERY respawned session via CRS, not just the listed cohort.+ // Downstream health-gate + cr_-key validity + atomic invariant still apply, so a global+ // session falls back to direct auth if CRS is unhealthy or the key is bad. Reversible: set global:false.+ if (+ !excluded &&+ (al.global === true || (Array.isArray(al.sessions) && al.sessions.includes(String(session.id))))+ ) {+ const crKeyPath = join(homedir(), '.claude', 'crs-keys', 'claude-cli.env');+ let crKey = '';+ try {+ crKey = loadClaudeHarnessEnv({ path: crKeyPath }).CRS_API_KEY;+ } catch {+ crKey = '';+ }+ // HEALTH GATE (2026-06-14, refined 2026-06-15): never route a session+ // onto a DEAD relay. A valid cr_ key is necessary but not sufficient —+ // if CRS is unhealthy, injecting the CRS base wedges the session on an+ // unreachable upstream. crsHealthyCheap() prefers the recent+ // crs-health-watch verdict (no network) and falls back to a 3s /health+ // curl. When down, fall through to strip-base direct-auth + drop the+ // dashboard relay-down marker so the "direct" session is explained.+ const crsHealthy = crsHealthyCheap();+ if (crKey.startsWith('cr_') && crsHealthy) {+ // Both together + relay alive — the only safe state.+ childEnv.ANTHROPIC_BASE_URL = crsBaseUrl;+ childEnv.ANTHROPIC_API_BASE = crsBaseUrl;+ childEnv.ANTHROPIC_AUTH_TOKEN = crKey;+ childEnv.CLAUDE_CODE_OAUTH_TOKEN = crKey;+ // Keep API_KEY=cr_ so Claude hits CRS (unsetting broke routing).+ childEnv.ANTHROPIC_API_KEY = crKey;+ crsRelayKey = crKey;+ crsActive = true;+ try {+ unlinkSync(CRS_RELAY_DOWN_MARKER);+ } catch {} // relay healthy → clear stale marker+ log(`[bg-respawn] CRS-allowlist: routing session ${session.id} via relay ${childEnv.ANTHROPIC_BASE_URL}`);+ } else if (crKey.startsWith('cr_') && !crsHealthy) {+ // Valid key but relay DOWN → fail safe to direct auth (do not wedge).+ delete childEnv.ANTHROPIC_BASE_URL;+ delete childEnv.ANTHROPIC_API_BASE;+ try {+ writeFileSync(+ CRS_RELAY_DOWN_MARKER,+ JSON.stringify({ ts: Date.now(), reason: 'relay-down', lastSession: String(session.id) }),+ );+ } catch {}+ log(+ `[bg-respawn] CRS-allowlist: session ${session.id} listed but CRS unhealthy — stripping ANTHROPIC_BASE_URL, falling back to direct auth (marker: ${CRS_RELAY_DOWN_MARKER})`,+ );+ } else {+ // Key missing/malformed → MUST NOT leave base URL pointing at CRS with a non-cr_ token.+ delete childEnv.ANTHROPIC_BASE_URL;+ delete childEnv.ANTHROPIC_API_BASE;+ log(+ `[bg-respawn] CRS-allowlist: session ${session.id} listed but cr_ key invalid/missing at ${crKeyPath} — stripping ANTHROPIC_BASE_URL, falling back to direct auth`,+ );+ }+ }}- let token = tokenJson ? extractAccessToken(tokenJson) : null;- if (!token && routingKey !== 'bedrock' && state.activeAccount) {- const active = config.accounts.find((a) => accountKey(a) === state.activeAccount);- if (active) {- tokenJson = readVaultToken(active);- token = tokenJson ? extractAccessToken(tokenJson) : null;+ } catch (err) {+ // Allowlist parse failure must also be fail-safe: never route to CRS half-configured.+ delete childEnv.ANTHROPIC_BASE_URL;+ delete childEnv.ANTHROPIC_API_BASE;+ log(`[bg-respawn] CRS allowlist read failed (fail-safe: stripped ANTHROPIC_BASE_URL): ${err.message}`);+ }++ // ── FINAL ATOMIC NORMALIZATION (2026-06-14, both-directions hardening) ────+ // The earlier guard only covered the allowlist-injection path. Two real+ // regressions slipped past it for spare-worker / --settings respawns:+ // (a) lone cr_ key (no CRS base) → sent to direct api.anthropic.com →+ // "401 Invalid API key format" (Anthropic rejects cr_).+ // (b) base=CRS (leaked via --settings crs-session-settings.json or an+ // inherited daemon env) + a real keychain OAuth token (which childEnv+ // injection sets, OVERRIDING the settings-file cr_ token) → the relay+ // rejects the non-cr_ token: "🔒 Invalid API key format from 172.18.0.1".+ // Invariant enforced here, unconditionally, as the LAST word on env:+ // base==CRS XNOR token==cr_ (both, or neither — never one).+ // crsActive is the ONLY sanctioned "both" path; anything else is forced to+ // direct: strip the CRS base AND replace any cr_ token with the keychain one.+ {+ const crsBasePattern =+ /127\.0\.0\.1:(3000|3002|3005|8091|18091)|100\.87\.53\.96:8091|:(3000|3002|3005|8091|18091)\/api/;+ const baseIsCrs =+ crsBasePattern.test(String(childEnv.ANTHROPIC_BASE_URL || '')) ||+ crsBasePattern.test(String(childEnv.ANTHROPIC_API_BASE || ''));+ const tokIsCr = [+ childEnv.CLAUDE_CODE_OAUTH_TOKEN,+ childEnv.ANTHROPIC_AUTH_TOKEN,+ childEnv.ANTHROPIC_API_KEY,+ ].some((token) => String(token || '').startsWith('cr_'));+ if (crsActive) {+ const crKey = String(childEnv.ANTHROPIC_API_KEY || '').startsWith('cr_')+ ? childEnv.ANTHROPIC_API_KEY+ : String(childEnv.ANTHROPIC_AUTH_TOKEN || '').startsWith('cr_')+ ? childEnv.ANTHROPIC_AUTH_TOKEN+ : childEnv.CLAUDE_CODE_OAUTH_TOKEN;+ if (String(crKey || '').startsWith('cr_')) {+ childEnv.ANTHROPIC_API_KEY = crKey;+ childEnv.ANTHROPIC_AUTH_TOKEN = crKey;+ childEnv.CLAUDE_CODE_OAUTH_TOKEN = crKey;}}- if (token) {- // Scrub ALL Bedrock vars (USE_BEDROCK, AWS_*, hardcoded ANTHROPIC_MODEL)- // before setting the OAuth token. Leaving ANTHROPIC_MODEL=anthropic.claude-fable-5- // or AWS_* in env makes the OAuth session emit invalid model ids / keep paying- // for Bedrock. Model resets to the subscription default catalog.- applyOAuthEnv(childEnv, token);- log(`[bg-respawn] Injecting CLAUDE_CODE_OAUTH_TOKEN for session ${session.id} (Bedrock vars scrubbed)`);- } else if (routingKey === 'bedrock') {- applyBedrockEnv(childEnv);- log(`[bg-respawn] Injecting Bedrock fallback env for session ${session.id}`);- } else {- scrubBedrockEnv(childEnv);- delete childEnv.CLAUDE_CODE_OAUTH_TOKEN;- log(`[bg-respawn] Scrubbed Bedrock vars for session ${session.id} (no per-session token)`);+ if (!crsActive && (baseIsCrs || tokIsCr)) {+ if (baseIsCrs) {+ delete childEnv.ANTHROPIC_BASE_URL; // → falls back to default direct api.anthropic.com+ delete childEnv.ANTHROPIC_API_BASE;+ log(+ `[bg-respawn] ATOMIC: session ${session.id} not CRS-active but base→CRS leaked — stripped CRS base variables (direct)`,+ );+ }+ if (tokIsCr) {+ // Never let a lone cr_ relay key reach the direct API. Prefer the keychain token.+ let kc = null;+ try {+ const config = readConfig();+ const tokenJson = getTokenForSession(session.id, config);+ kc = tokenJson ? extractAccessToken(tokenJson) : null;+ } catch {}+ if (kc) {+ delete childEnv.ANTHROPIC_API_KEY;+ delete childEnv.ANTHROPIC_AUTH_TOKEN;+ childEnv.CLAUDE_CODE_OAUTH_TOKEN = kc;+ log(`[bg-respawn] ATOMIC: session ${session.id} had lone cr_ key — swapped to keychain OAuth (direct)`);+ } else {+ delete childEnv.ANTHROPIC_API_KEY; // fall through to keychain credentials file+ delete childEnv.ANTHROPIC_AUTH_TOKEN;+ delete childEnv.CLAUDE_CODE_OAUTH_TOKEN;+ log(+ `[bg-respawn] ATOMIC: session ${session.id} had lone cr_ key, no per-session token — deleted (keychain file fallback)`,+ );+ }+ }+ }+ }++ // ── RESPAWN-FLAG RECONCILIATION (durable: base+token can't separate) ─────+ // `--settings crs-session-settings.json` injects ANTHROPIC_BASE_URL=CRS at+ // process startup, which childEnv stripping cannot undo. Make the allowlist+ // the SOLE authority for CRS routing: add the CRS settings flag iff crsActive,+ // remove it otherwise. Persisted to state.json so `claude respawn` re-reads it.+ try {+ const flags = Array.isArray(state.respawnFlags) ? [...state.respawnFlags] : null;+ if (flags) {+ const isCrsSettingsVal = (v) => typeof v === 'string' && v.endsWith('crs-session-settings.json');+ const hasCrsSettings = flags.some((f, i) => f === '--settings' && isCrsSettingsVal(flags[i + 1]));+ let mutated = false;+ if (!crsActive && hasCrsSettings) {+ for (let i = flags.length - 1; i >= 0; i--) {+ if (flags[i] === '--settings' && isCrsSettingsVal(flags[i + 1])) {+ flags.splice(i, 2);+ mutated = true;+ }+ }+ log(+ `[bg-respawn] RECONCILE: session ${session.id} not CRS-active — removed --settings crs-session-settings.json from respawnFlags`,+ );+ } else if (crsActive && !hasCrsSettings) {+ const settingsPath = join(homedir(), '.claude', 'crs-session-settings.json');+ applyCrsSessionSettings({ baseUrl: crsBaseUrl, key: crsRelayKey });+ flags.push('--settings', settingsPath);+ mutated = true;+ log(+ `[bg-respawn] RECONCILE: session ${session.id} CRS-active — added --settings ${settingsPath} to respawnFlags`,+ );+ } else if (crsActive) {+ applyCrsSessionSettings({ baseUrl: crsBaseUrl, key: crsRelayKey });+ }+ if (mutated) {+ state.respawnFlags = flags;+ writeFileSync(stateFile, JSON.stringify(state, null, 2));+ }}} catch (err) {- log(`[bg-respawn] Failed to retrieve session token: ${err.message}`);+ log(`[bg-respawn] RECONCILE: failed to reconcile respawnFlags for ${session.id}: ${err.message}`);}- execFileSync(claudeBin(), ['respawn', String(session.id)], {- timeout: 30_000,- stdio: 'pipe',- env: childEnv,- });+ let respawnOut = '';+ try {+ respawnOut = execFileSync(claudeBin(), ['respawn', String(session.id)], {+ timeout: 30_000,+ stdio: 'pipe',+ env: childEnv,+ }).toString();+ } catch (e) {+ // Capture any output the binary emitted on failure (model errors etc) to our log, not controlling tty.+ try {+ respawnOut = (e.stdout || '') + (e.stderr || '');+ } catch {}+ log(`[bg-respawn] respawn exec note for ${session.id}: ${String(e.message || e).slice(0, 120)}`);+ // Do not re-throw; we still want to mark and continue fleet hygiene.+ }+ if (respawnOut && respawnOut.length > 0) {+ // Persist any chatter from the respawn to dedicated log to avoid TUI pollution.+ try {+ const LOGP = join(__dirname, 'bg-respawn.out.log');+ appendFileSync(LOGP, `[${new Date().toISOString()}] ${session.id}: ${respawnOut.slice(0, 2000)}\n`);+ } catch {}+ }try {writeFileSync(RESPAWNED_MARKER(session.id), String(Date.now()));} catch {}@@ -310,9 +498,11 @@ export function doRespawn(session, log) {} catch {}log(`[bg-respawn] respawned bg session ${session.id} (pid ${session.pid}, was ${session.status})`);- // Synchronously resolve new PID and persist the lease only after respawn succeeds+ // Synchronously resolve new PID and update the leasetry {- if (routingKey) {+ const leases = readLeases();+ const lease = leases[session.id];+ if (lease) {let newPid = null;for (let i = 0; i < 20; i++) {try {@@ -327,11 +517,7 @@ export function doRespawn(session, log) {}if (newPid) {log(`[bg-respawn] Updated lease for session ${session.id} with new PID ${newPid}`);- recordSessionLease(session.id, routingKey, newPid);- // F4: nudge the re-exec'd session to resume its task (default-off).- // Only here, after the new pid is resolved (respawn has settled), and- // only for sessions with saved state (spares returned earlier).- injectContinuation(session.id, log);+ recordSessionLease(session.id, lease.accountKey, newPid);} else {log(`[bg-respawn] Warning: could not resolve new PID for session ${session.id}`);}@@ -343,6 +529,7 @@ export function doRespawn(session, log) {return true;} catch (e) {log(`[bg-respawn] respawn ${session.id} failed: ${e.message?.slice(0, 80)}`);+ resetTty();return false;}}@@ -364,16 +551,17 @@ export function respawnBgSessions(log = () => {}) {log(`[bg-respawn] ${s.id} respawned <10min ago — skipping`);continue;}- if (s.status === 'busy' || isLoopSession(s.id)) {+ if (s.status === 'busy') {try {if (!existsSync(DEFERRED_MARKER(s.id))) writeFileSync(DEFERRED_MARKER(s.id), String(Date.now()));} catch {}deferred++;- log(- `[bg-respawn] ${s.id} ${s.status === 'busy' ? 'busy' : 'has an armed /loop'} — deferred (daemon sweep will retry, force after 90min)`,- );+ log(`[bg-respawn] ${s.id} busy — deferred (daemon sweep will retry, force after 90min)`);continue;}+ // Graceful rotation-over: space respawns so the fleet doesn't all re-auth+ // onto the new account at once. Gap only between real respawns.+ if (respawned > 0) sleepSync(SESSION_STAGGER_MS);if (doRespawn(s, log)) respawned++;}return { respawned, deferred };@@ -412,13 +600,15 @@ export function sweepDeferredRespawns(log = () => {}) {return 0;}})();- const protectedNow = s.status === 'busy' || isLoopSession(id);- if (!protectedNow || deferredAgo > BUSY_FORCE_AFTER_MS) {- if (protectedNow) {+ if (s.status !== 'busy' || deferredAgo > BUSY_FORCE_AFTER_MS) {+ if (s.status === 'busy') {log(- `[bg-respawn] ${id} still ${s.status === 'busy' ? 'busy' : 'looping'} after ${Math.round(deferredAgo / 60000)}min — force-respawning before token expiry`,+ `[bg-respawn] ${id} still busy after ${Math.round(deferredAgo / 60000)}min — force-respawning before token expiry`,);}+ // Graceful rotation-over: space respawns so a deferred batch doesn't all+ // re-auth onto the new account in the same instant.+ if (handled > 0) sleepSync(SESSION_STAGGER_MS);if (doRespawn(s, log)) handled++;}}diff --git a/claude-ops/scripts/account-rotation/claude-harness-env.mjs b/claude-ops/scripts/account-rotation/claude-harness-env.mjsnew file mode 100644index 00000000..b8458412--- /dev/null+++ b/claude-ops/scripts/account-rotation/claude-harness-env.mjs@@ -0,0 +1,77 @@+import { lstatSync, readFileSync, statSync } from 'node:fs';+import { homedir } from 'node:os';+import { join } from 'node:path';++const ALLOWED_KEYS = new Set([+ 'ANTHROPIC_BASE_URL',+ 'ANTHROPIC_API_BASE',+ 'ANTHROPIC_AUTH_TOKEN',+ 'ANTHROPIC_API_KEY',+ 'CLAUDE_CODE_OAUTH_TOKEN',+ 'CRS_API_KEY',+ 'CRS_HARNESS_NAME',+]);+const REQUIRED_KEYS = new Set([+ 'ANTHROPIC_BASE_URL',+ 'ANTHROPIC_API_BASE',+ 'ANTHROPIC_AUTH_TOKEN',+ 'CLAUDE_CODE_OAUTH_TOKEN',+ 'CRS_API_KEY',+ 'CRS_HARNESS_NAME',+]);+const ALLOWED_BASES = new Set(['http://127.0.0.1:3005/api', 'http://127.0.0.1:8091/api']);+const TOKEN_RE = /^cr_[A-Za-z0-9._~-]+$/;+const HARNESS_RE = /^[A-Za-z0-9._-]+$/;++function invalid() {+ return new Error('Claude CRS harness env validation failed');+}++export function claudeHarnessEnvPath(home = homedir()) {+ return join(home, '.claude', 'crs-keys', 'claude-cli.env');+}++export function loadClaudeHarnessEnv(options = {}) {+ const path = options.path || claudeHarnessEnvPath(options.home);+ let mode;+ let source;+ try {+ if (lstatSync(path).isSymbolicLink()) throw invalid();+ mode = statSync(path).mode & 0o777;+ source = readFileSync(path, 'utf8');+ } catch {+ throw invalid();+ }+ if (mode !== 0o600) throw invalid();++ const values = {};+ for (const rawLine of source.split(/\r?\n/)) {+ if (!rawLine || rawLine.startsWith('#')) continue;+ const separator = rawLine.indexOf('=');+ if (separator <= 0) throw invalid();+ const name = rawLine.slice(0, separator);+ const value = rawLine.slice(separator + 1);+ if (!ALLOWED_KEYS.has(name) || Object.hasOwn(values, name)) throw invalid();+ values[name] = value;+ }+ if ([...REQUIRED_KEYS].some((name) => !Object.hasOwn(values, name))) {+ throw invalid();+ }+ // API_KEY optional in file; when present must match the harness cr_ key.+ if (Object.hasOwn(values, 'ANTHROPIC_API_KEY') && values.ANTHROPIC_API_KEY !== values.CRS_API_KEY) {+ throw invalid();+ }+ if (+ !ALLOWED_BASES.has(values.ANTHROPIC_BASE_URL) ||+ values.ANTHROPIC_API_BASE !== values.ANTHROPIC_BASE_URL ||+ !TOKEN_RE.test(values.ANTHROPIC_AUTH_TOKEN) ||+ values.CLAUDE_CODE_OAUTH_TOKEN !== values.ANTHROPIC_AUTH_TOKEN ||+ values.CRS_API_KEY !== values.ANTHROPIC_AUTH_TOKEN ||+ !HARNESS_RE.test(values.CRS_HARNESS_NAME)+ ) {+ throw invalid();+ }+ // Always surface API_KEY=cr_ so launch paths do not strip the only usable CRS credential.+ values.ANTHROPIC_API_KEY = values.CRS_API_KEY;+ return values;+}diff --git a/claude-ops/scripts/account-rotation/launch-claude.mjs b/claude-ops/scripts/account-rotation/launch-claude.mjsnew file mode 100755index 00000000..5826ef40--- /dev/null+++ b/claude-ops/scripts/account-rotation/launch-claude.mjs@@ -0,0 +1,259 @@+#!/usr/bin/env node+import { loadClaudeHarnessEnv } from './claude-harness-env.mjs';+import { readFileSync, existsSync } from 'node:fs';+import { spawnSync } from 'node:child_process';+import { join, dirname } from 'node:path';+import { fileURLToPath } from 'node:url';++// ADMISSION_GATE 2026-07-13+import { spawnSync as __admSpawnSync } from 'node:child_process';+function __admissionAllow(args) {+ if (process.env.ADMISSION_FORCE === '1') return;+ const cmd = args.find((a) => a && !String(a).startsWith('-')) || '';+ const control = new Set([+ 'agents',+ 'attach',+ 'auth',+ 'config',+ 'daemon',+ 'doctor',+ 'help',+ 'kill',+ 'logs',+ 'mcp',+ 'plugin',+ 'plugins',+ 'rm',+ 'respawn',+ 'status',+ 'stop',+ 'update',+ ]);+ if (control.has(cmd)) return;+ try {+ __admSpawnSync('bash', [`${process.env.HOME}/.claude/scripts/admission-write.sh`], { stdio: 'ignore' });+ } catch {}+ const r = __admSpawnSync('bash', [`${process.env.HOME}/.claude/scripts/admission-gate.sh`], { encoding: 'utf8' });+ if (r.status === 3) {+ console.error('ADMISSION: over_ceiling — launch-claude.mjs refusing model spawn');+ process.exit(3);+ }+}++const __dirname = dirname(fileURLToPath(import.meta.url));+const CONFIG_PATH = join(__dirname, 'config.json');+const STATE_PATH = join(__dirname, 'state.json');+const ROUTER_PATH = join(__dirname, 'session-router.mjs');+const home = process.env.HOME || '';+const CLAUDE_SETTINGS_PATH = join(home, '.claude', 'settings.json');+const REAL_CLAUDE_BIN = join(home, '.npm-global/lib/node_modules/@anthropic-ai/claude-code/bin/claude.exe');+const defaultCandidates =+ process.platform === 'win32'+ ? [+ process.env.CLAUDE_REAL_BIN,+ process.env.CLAUDE_BIN,+ join(home, '.npm-global/bin/claude.exe'),+ join(home, '.local/bin/claude'),+ ]+ : [+ process.env.CLAUDE_REAL_BIN,+ process.env.CLAUDE_BIN,+ REAL_CLAUDE_BIN,+ join(home, '.npm-global/bin/claude'),+ join(home, '.local/bin/claude'),+ '/usr/local/bin/claude',+ 'claude',+ ];+const CLAUDE_BIN = (() => {+ for (const candidate of defaultCandidates) {+ if (candidate && existsSync(candidate)) {+ return candidate;+ }+ }+ return process.platform === 'win32' ? 'claude.exe' : 'claude';+})();+const rawArgs = process.argv.slice(2);+__admissionAllow(rawArgs);++const CONTROL_SUBCOMMANDS = new Set([+ 'agents',+ 'attach',+ 'auth',+ 'config',+ 'daemon',+ 'doctor',+ 'help',+ 'kill',+ 'logs',+ 'mcp',+ 'plugin',+ 'plugins',+ 'rm',+ 'respawn',+ 'status',+ 'stop',+ 'update',+]);++function firstCommand(args) {+ for (let i = 0; i < args.length; i += 1) {+ const arg = args[i];+ if (!arg.startsWith('-')) return arg;+ if (+ [+ '--add-dir',+ '--agent',+ '--append-system-prompt',+ '--cwd',+ '--effort',+ '--model',+ '--name',+ '--output-format',+ '--permission-mode',+ '--resume',+ '--settings',+ ].includes(arg)+ ) {+ i += 1;+ }+ }+ return null;+}++function shouldBypassPermissions(args) {+ if (+ args.includes('--remote-control') ||+ args.includes('--version') ||+ args.includes('-v') ||+ args.includes('--help') ||+ args.includes('-h')+ ) {+ return false;+ }+ const cmd = firstCommand(args);+ if (cmd && CONTROL_SUBCOMMANDS.has(cmd)) return false;+ return true;+}++function withBypassPermissions(args) {+ if (!shouldBypassPermissions(args)) return args;+ const next = [...args];+ if (!next.includes('--dangerously-skip-permissions')) {+ next.push('--dangerously-skip-permissions');+ }+ if (!next.includes('--permission-mode')) {+ next.push('--permission-mode', 'bypassPermissions');+ }+ return next;+}++const args = withBypassPermissions(rawArgs);+const isModelLaunch = shouldBypassPermissions(rawArgs);+const settingsEnv = (() => {+ try {+ return JSON.parse(readFileSync(CLAUDE_SETTINGS_PATH, 'utf8'))?.env || {};+ } catch {+ return {};+ }+})();+const baseEnv = { ...process.env, ...settingsEnv };+for (const key of [+ 'OPENAI_API_KEY',+ 'OPENROUTER_API_KEY',+ 'GOOGLE_API_KEY',+ 'GEMINI_API_KEY',+ 'GOOGLE_GENERATIVE_AI_API_KEY',+ 'GROQ_API_KEY',+ 'COHERE_API_KEY',+ 'CEREBRAS_API_KEY',+ 'DEEPINFRA_API_KEY',+ 'PERPLEXITY_API_KEY',+ 'PPLX_API_KEY',+ 'REPLICATE_API_KEY',+ 'TOGETHER_API_KEY',+ 'XAI_API_KEY',+]) {+ delete baseEnv[key];+}+delete baseEnv.CLAUDE_CODE_USE_BEDROCK;+delete baseEnv.ANTHROPIC_MODEL;++if (isModelLaunch) {+ let harnessEnv;+ try {+ harnessEnv = loadClaudeHarnessEnv({ home });+ } catch (error) {+ console.error(error.message);+ process.exit(2);+ }+ Object.assign(baseEnv, harnessEnv);+ // Keep API_KEY=cr_ (derived from harness). Stripping it left BASE→CRS with no key.+ baseEnv.ANTHROPIC_API_KEY = baseEnv.ANTHROPIC_API_KEY || baseEnv.CRS_API_KEY || baseEnv.ANTHROPIC_AUTH_TOKEN;+} else {+ for (const key of [+ 'ANTHROPIC_BASE_URL',+ 'ANTHROPIC_API_BASE',+ 'ANTHROPIC_API_KEY',+ 'ANTHROPIC_AUTH_TOKEN',+ 'CLAUDE_CODE_OAUTH_TOKEN',+ 'CRS_API_KEY',+ 'CRS_HARNESS_NAME',+ ]) {+ delete baseEnv[key];+ }+}++function readJson(path) {+ try {+ return JSON.parse(readFileSync(path, 'utf8'));+ } catch {+ return null;+ }+}++function fallback() {+ const result = spawnSync(CLAUDE_BIN, args, {+ env: baseEnv,+ stdio: 'inherit',+ });+ process.exit(result.status ?? 1);+}++const config = readJson(CONFIG_PATH);+const state = readJson(STATE_PATH);+if (!config || !state) {+ fallback();+}++let spawnWithAccount;+try {+ ({ spawnWithAccount } = await import(ROUTER_PATH));+} catch (err) {+ console.error(`claude-rotate-launcher: failed to load session-router (${err.message})`);+ fallback();+}++if (typeof spawnWithAccount !== 'function') {+ fallback();+}++let session;+try {+ session = spawnWithAccount(args, config, state, { env: baseEnv, detached: false, stdio: 'inherit' });+} catch (err) {+ console.error(`claude-rotate-launcher: spawnWithAccount failed (${err.message})`);+ fallback();+}++const proc = session?.proc;+if (!proc || typeof proc.once !== 'function') {+ fallback();+}++proc.once('exit', (code) => {+ process.exit(code ?? 1);+});++proc.once('error', () => {+ fallback();+});diff --git a/claude-ops/scripts/account-rotation/route-state.mjs b/claude-ops/scripts/account-rotation/route-state.mjsindex 0242ecae..4661facb 100644--- a/claude-ops/scripts/account-rotation/route-state.mjs+++ b/claude-ops/scripts/account-rotation/route-state.mjs@@ -1,12 +1,52 @@-import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'fs';+import { loadClaudeHarnessEnv } from './claude-harness-env.mjs';+import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'fs';+import { execFileSync } from 'child_process';import { dirname, join } from 'path';import { homedir, hostname } from 'os';-export const ROUTE_MODES = new Set(['crs-oauth', 'bedrock-confirmed', 'fail-closed']);+export const ROUTE_MODES = new Set(['crs-oauth', 'fail-closed']);++// CRS base⇄token desync guard (the safety net for every settings/overlay write).+// INVARIANT: ANTHROPIC_BASE_URL points at the CRS relay XNOR a cr_-prefixed+// relay key in ANTHROPIC_API_KEY. Auth-token fields are reserved for the+// launcher-created child environment and must not persist in settings.+const CRS_BASE_RE = /127\.0\.0\.1:(3000|3002|3005|8091|18091)|100\.87\.53\.96:8091|:(3000|3002|3005|8091|18091)\/api/;++function currentCrsToken(env = {}) {+ return isCrsToken(env.ANTHROPIC_API_KEY) ? env.ANTHROPIC_API_KEY : '';+}++export function isCrsBase(baseUrl) {+ return !!baseUrl && CRS_BASE_RE.test(String(baseUrl));+}++export function isCrsToken(token) {+ return String(token || '').startsWith('cr_');+}++export function assertCrsInvariant(env, where = 'crs-env') {+ const e = env && typeof env === 'object' ? env : {};+ const baseIsCrs = isCrsBase(e.ANTHROPIC_BASE_URL) || isCrsBase(e.ANTHROPIC_API_BASE);+ const tokIsCr = isCrsToken(currentCrsToken(e));+ if (baseIsCrs !== tokIsCr) {+ throw new Error(+ `[${where}] CRS base-token desync (fail-closed, refusing write): ` ++ `base=${baseIsCrs ? 'CRS' : 'non-CRS'} token=${tokIsCr ? 'cr_' : 'non-cr_'}. ` ++ 'ANTHROPIC_BASE_URL and CRS token vars must be set together or not at all.',+ );+ }+ if (isCrsToken(e.ANTHROPIC_AUTH_TOKEN) || isCrsToken(e.CLAUDE_CODE_OAUTH_TOKEN)) {+ throw new Error(`[${where}] refusing to persist cr_ relay key in auth-token fields`);+ }+ return env;+}const STATE_PATH = join(homedir(), '.claude', 'claude-routing-state.json');const SETTINGS_PATH = join(homedir(), '.claude', 'settings.json');+const CRS_SESSION_SETTINGS_PATH = join(homedir(), '.claude', 'crs-session-settings.json');const CRKEY_PATH = join(homedir(), '.claude', 'scripts', 'account-rotation', '.crkey');+const FALLBACK_MARKER = join(homedir(), '.claude', 'scripts', 'account-rotation', 'crs-fallback-active');+const HEALTH_WATCH_STATE = join(homedir(), '.claude', 'scripts', 'account-rotation', 'crs-health-watch.state.json');const MODEL_KEYS = ['ANTHROPIC_MODEL',@@ -19,7 +59,12 @@ const MODEL_KEYS = [const BEDROCK_KEYS = ['CLAUDE_CODE_USE_BEDROCK', 'AWS_BEDROCK_REGION'];-const ANTHROPIC_DIRECT_KEYS = ['ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_TOKEN'];+const ANTHROPIC_DIRECT_KEYS = [+ 'ANTHROPIC_API_BASE',+ 'ANTHROPIC_API_KEY',+ 'ANTHROPIC_AUTH_TOKEN',+ 'CLAUDE_CODE_OAUTH_TOKEN',+];function nowIso() {return new Date().toISOString();@@ -33,31 +78,64 @@ function readJson(path, fallback) {}}+function readSettingsForUpdate() {+ let settings;+ try {+ settings = JSON.parse(readFileSync(SETTINGS_PATH, 'utf8'));+ } catch (error) {+ throw new Error(`refusing settings update: cannot read valid JSON at ${SETTINGS_PATH}: ${error.message}`);+ }+ const hooks = settings?.hooks;+ let commands = 0;+ if (hooks && typeof hooks === 'object' && !Array.isArray(hooks)) {+ for (const groups of Object.values(hooks)) {+ if (!Array.isArray(groups)) continue;+ for (const group of groups) {+ if (!group || typeof group !== 'object' || !Array.isArray(group.hooks)) continue;+ commands += group.hooks.filter(+ (entry) => entry && typeof entry.command === 'string' && entry.command.trim(),+ ).length;+ }+ }+ }+ if (commands === 0) {+ throw new Error(`refusing settings update: hooks are missing or empty at ${SETTINGS_PATH}`);+ }+ return settings;+}+function readSettingsEnv() {return readJson(SETTINGS_PATH, { env: {} }).env || {};}function defaultCrsConfig() {const env = readSettingsEnv();- const baseUrl = process.env.CRS_BASE_URL || env.ANTHROPIC_BASE_URL || 'http://127.0.0.1:3000/api';+ const defaultBase = process.platform === 'darwin' ? 'http://127.0.0.1:8091/api' : 'http://127.0.0.1:3005/api';+ const inheritedBase = isCrsBase(env.ANTHROPIC_BASE_URL) ? env.ANTHROPIC_BASE_URL : null;+ const baseUrl = process.env.CRS_BASE_URL || inheritedBase || defaultBase;const healthUrl = process.env.CRS_HEALTH_URL || baseUrl.replace(/\/api\/?$/, '/health');return {baseUrl,healthUrl,- authority: 'fra-primary',+ authority: process.platform === 'darwin' ? 'mac-local-primary' : 'dev-us',};}-function writeJsonAtomic(path, data) {+function writeJsonAtomic(path, data, defaultMode = null) {mkdirSync(dirname(path), { recursive: true });const tmp = `${path}.tmp.${process.pid}`;writeFileSync(tmp, `${JSON.stringify(data, null, 2)}\n`);+ let mode = defaultMode;+ try {+ mode = statSync(path).mode & 0o777;+ } catch {}+ if (mode !== null) chmodSync(tmp, mode);renameSync(tmp, path);}function crKey() {try {- return readFileSync(CRKEY_PATH, 'utf8').trim();+ return loadClaudeHarnessEnv().CRS_API_KEY;} catch {return '';}@@ -110,52 +188,20 @@ export function setRouteMode(mode, options = {}) {updatedBy: options.updatedBy || process.env.USER || 'unknown',};if (options.crs) next.crs = options.crs;- if (mode === 'bedrock-confirmed') {- if (!options.confirmMetered) {- throw new Error('Bedrock is metered AWS usage; pass confirmMetered=true to activate a TTL-scoped confirmation');- }- const confirmedAt = nowIso();- next.bedrockConfirmation = {- confirmedAt,- expiresAt: new Date(Date.now() + ttlMinutes * 60_000).toISOString(),- reason,- confirmedBy: options.updatedBy || process.env.USER || 'unknown',- };- } else {- next.bedrockConfirmation = null;- }- const prev = readRouteState();- const merged = {- ...prev,- ...next,- updatedAt: nowIso(),- host: hostname(),- };- if (!ROUTE_MODES.has(merged.mode)) {- throw new Error(`invalid route mode: ${merged.mode}`);- }- applyRouteToSettings(merged, options);- writeJsonAtomic(STATE_PATH, merged);- return merged;+ next.bedrockConfirmation = null;+ const state = writeRouteState(next);+ applyRouteToSettings(state, options);+ return state;}export function applyRouteToSettings(state = readRouteState(), options = {}) {- const settings = readJson(SETTINGS_PATH, {});+ const settings = readSettingsForUpdate();const env = settings.env && typeof settings.env === 'object' ? { ...settings.env } : {};for (const key of [...MODEL_KEYS, ...ANTHROPIC_DIRECT_KEYS]) delete env[key];delete settings.model;delete settings.availableModels;- if (state.mode === 'bedrock-confirmed') {- if (!bedrockConfirmationActive(state)) {- throw new Error('Bedrock route lacks an active TTL confirmation');- }- env.CLAUDE_CODE_USE_BEDROCK = '1';- env.AWS_BEDROCK_REGION = options.region || env.AWS_REGION || process.env.AWS_BEDROCK_REGION || 'us-east-1';- env.AWS_REGION = env.AWS_BEDROCK_REGION;- delete env.ANTHROPIC_BASE_URL;- delete env.CLAUDE_CODE_OAUTH_TOKEN;- } else if (state.mode === 'crs-oauth') {+ if (state.mode === 'crs-oauth') {for (const key of BEDROCK_KEYS) delete env[key];env.AWS_REGION = env.AWS_REGION || process.env.AWS_REGION || 'us-east-1';const key = crKey();@@ -163,23 +209,66 @@ export function applyRouteToSettings(state = readRouteState(), options = {}) {throw new Error(`CRS relay key missing or invalid at ${CRKEY_PATH}`);}env.ANTHROPIC_BASE_URL = state.crs?.baseUrl || defaultCrsConfig().baseUrl;- env.CLAUDE_CODE_OAUTH_TOKEN = key;+ env.ANTHROPIC_API_BASE = env.ANTHROPIC_BASE_URL;+ env.ANTHROPIC_API_KEY = key;+ delete env.ANTHROPIC_AUTH_TOKEN;+ delete env.CLAUDE_CODE_OAUTH_TOKEN;} else {for (const key of BEDROCK_KEYS) delete env[key];delete env.ANTHROPIC_BASE_URL;+ delete env.ANTHROPIC_AUTH_TOKEN;delete env.CLAUDE_CODE_OAUTH_TOKEN;+ delete env.ANTHROPIC_API_KEY;}settings.env = env;+ assertCrsInvariant(env, `applyRouteToSettings:${state.mode}`);writeJsonAtomic(SETTINGS_PATH, settings);return settings;}+export function assertCrsSessionInvariant(env, where = 'crs-session-env') {+ const e = env && typeof env === 'object' ? env : {};+ const baseUrl = String(e.ANTHROPIC_BASE_URL || '');+ const apiBase = String(e.ANTHROPIC_API_BASE || '');+ const authToken = String(e.ANTHROPIC_AUTH_TOKEN || '');+ const oauthToken = String(e.CLAUDE_CODE_OAUTH_TOKEN || '');+ if (+ !isCrsBase(baseUrl) ||+ baseUrl !== apiBase ||+ !isCrsToken(authToken) ||+ authToken !== oauthToken ||+ e.ANTHROPIC_API_KEY !== undefined+ ) {+ throw new Error(`[${where}] invalid CRS model-child auth contract; refusing settings write`);+ }+ return env;+}++export function applyCrsSessionSettings(options = {}) {+ const settings = readJson(CRS_SESSION_SETTINGS_PATH, {});+ const env = settings.env && typeof settings.env === 'object' ? { ...settings.env } : {};+ const key = options.key || crKey();+ if (!key.startsWith('cr_')) {+ throw new Error(`CRS relay key missing or invalid at ${CRKEY_PATH}`);+ }+ const baseUrl = options.baseUrl || readRouteState().crs?.baseUrl || defaultCrsConfig().baseUrl;+ env.ANTHROPIC_BASE_URL = baseUrl;+ env.ANTHROPIC_API_BASE = baseUrl;+ env.ANTHROPIC_AUTH_TOKEN = key;+ env.CLAUDE_CODE_OAUTH_TOKEN = key;+ delete env.ANTHROPIC_API_KEY;+ settings.env = env;+ assertCrsSessionInvariant(env, 'applyCrsSessionSettings');+ writeJsonAtomic(CRS_SESSION_SETTINGS_PATH, settings, 0o600);+ return settings;+}+export function settingsRouteSnapshot() {const settings = readJson(SETTINGS_PATH, { env: {} });const env = settings.env || {};const base = String(env.ANTHROPIC_BASE_URL || '');- const token = String(env.CLAUDE_CODE_OAUTH_TOKEN || '');+ const token = currentCrsToken(env);const bedrock = env.CLAUDE_CODE_USE_BEDROCK === '1';return {settingsPath: SETTINGS_PATH,@@ -201,3 +290,55 @@ export function routeStatus() {settings: settingsRouteSnapshot(),};}++export function probeCrsHealth(state = readRouteState()) {+ const healthUrl = process.env.CRS_HEALTH_URL || state.crs?.healthUrl || defaultCrsConfig().healthUrl;+ try {+ const code = execFileSync('curl', ['-sf', '-o', '/dev/null', '-w', '%{http_code}', '--max-time', '3', healthUrl], {+ encoding: 'utf8',+ timeout: 5000,+ }).trim();+ return code === '200';+ } catch {+ return false;+ }+}++export function clearCrsFallbackMarker() {+ try {+ if (existsSync(FALLBACK_MARKER)) unlinkSync(FALLBACK_MARKER);+ } catch {}+}++function resetHealthWatchCounters() {+ try {+ writeJsonAtomic(HEALTH_WATCH_STATE, {+ down: 0,+ up: 0,+ inferenceDown: 0,+ lastInferenceHealAt: 0,+ mode: 'crs',+ });+ } catch {}+}++export function restoreCrsRouteIfHealthy(options = {}) {+ const state = readRouteState();+ if (state.mode !== 'fail-closed') {+ return { restored: false, reason: 'not-fail-closed', mode: state.mode };+ }+ if (!probeCrsHealth(state)) {+ return { restored: false, reason: 'crs-unhealthy', mode: state.mode };+ }+ try {+ setRouteMode('crs-oauth', {+ reason: options.reason || 'CRS live probe healthy',+ updatedBy: options.updatedBy || 'crs-route-recovery',+ });+ clearCrsFallbackMarker();+ resetHealthWatchCounters();+ return { restored: true, reason: 'crs-oauth', mode: 'crs-oauth' };+ } catch (e) {+ return { restored: false, reason: e.message || String(e), mode: state.mode };+ }+}diff --git a/claude-ops/scripts/account-rotation/session-router.mjs b/claude-ops/scripts/account-rotation/session-router.mjsindex 3ec1429c..f49f3ff7 100644--- a/claude-ops/scripts/account-rotation/session-router.mjs+++ b/claude-ops/scripts/account-rotation/session-router.mjs@@ -35,16 +35,17 @@* time. It never touches the global "Claude Code-credentials" keychain entry.*/-import { readFileSync, writeFileSync, existsSync } from 'fs';+import { readFileSync, writeFileSync, existsSync, mkdirSync, renameSync } from 'fs';import { join, dirname } from 'path';import { fileURLToPath } from 'url';import { spawnSync, spawn } from 'child_process';-import { applyOAuthEnv, applyBedrockEnv } from './provider-env.mjs';+import { cachedUtilizationMax } from './rotation-policy.mjs';const __dirname = dirname(fileURLToPath(import.meta.url));const LEASES_PATH = join(__dirname, 'session-leases.json');const IS_LINUX = process.platform === 'linux';const LEASE_TTL_MS = 30 * 60_000; // 30 min — sessions that die without cleanup are GC'd+const DIRECT_OAUTH_SETTINGS_DIR = join(__dirname, '.settings-overrides');// ── Lease store ───────────────────────────────────────────────────────────────@@ -91,7 +92,7 @@ export function pruneLeases(leases) {// ── Account key helper (mirrors rotate.mjs / daemon.mjs) ──────────────────────-function accountKey(a) {+export function accountKey(a) {return a.label || a.email;}@@ -162,8 +163,11 @@ export function extractAccessToken(tokenJson) {* @returns {string|null} accountKey, or null to use global keychain*/export function pickAccountForSession(sessionId, config, state) {- // Always route if we're called- if (sessionId === 'app-releaser' || String(sessionId).includes('releaser')) return 'bedrock';+ // BEDROCK FALLBACK DISABLED (2026-06-13): Bedrock served the hardcoded+ // `anthropic.claude-fable-5`, which Bedrock cannot serve ("Claude Fable 5 is+ // currently unavailable") → sessions flipped to Bedrock wedged permanently.+ // The releaser→bedrock special-case is removed; releaser sessions now route to+ // a normal OAuth account like everything else.const leases = readLeases();const pruned = pruneLeases(leases);if (pruned) {@@ -195,7 +199,7 @@ export function pickAccountForSession(sessionId, config, state) {let bestLeaseCount = Infinity;let bestUtil = Infinity;const SORT_WEIGHT_PCT = 2; // Anti-dogpiling weight per active lease for selection sorting only- const MAX_CONCURRENT_PER_ACCOUNT = 2; // Strict limit to prevent Anthropic "session limit"+ const MAX_CONCURRENT_PER_ACCOUNT = 4; // Sam 2026-07-20: raised from 2 to 4 to maximize fleet throughput. Real ceiling is the Anthropic API quota-reached response, not a soft heuristic.for (const a of config.accounts) {if (a.disabled === true) continue;@@ -210,16 +214,21 @@ export function pickAccountForSession(sessionId, config, state) {if (leasesCount >= MAX_CONCURRENT_PER_ACCOUNT) continue;const cached = state.accounts?.[key]?.lastUtilization;- const util = cached?.pct ?? 50; // unknown = assume 50%+ const util = cachedUtilizationMax(cached) ?? 50; // unknown = assume 50%const maxUtil = a.maxUtilPercent || config.rateLimits.destinationMaxUtilPercent;- // Only lease if ACTUAL utilization is strictly below the account's cap (e.g. 98% or 50% account cap)+ // Only lease if ACTUAL utilization is strictly below the account's cap.if (util < maxUtil) {- // Add a tiny weight to the actual utilization to stagger concurrent spawns (anti-dogpiling)+ // COOLEST-ACCOUNT-FIRST: sort PRIMARILY by utilization (plus a small+ // per-lease anti-dogpile weight), NOT by lease count. The old sort keyed+ // on fewest-leases first, so a 97%-but-0-lease account beat a 0%-but-1-lease+ // account → sessions were leased onto near-exhausted accounts and 429'd+ // almost immediately even while cooler accounts sat idle. The strict+ // MAX_CONCURRENT_PER_ACCOUNT cap above still bounds dogpiling. (Sam 2026-06-13)const sortedUtil = util + leasesCount * SORT_WEIGHT_PCT;- if (leasesCount < bestLeaseCount || (leasesCount === bestLeaseCount && sortedUtil < bestUtil)) {- bestLeaseCount = leasesCount;+ if (sortedUtil < bestUtil) {bestUtil = sortedUtil;+ bestLeaseCount = leasesCount;bestAccount = a;}}@@ -227,27 +236,116 @@ export function pickAccountForSession(sessionId, config, state) {if (bestAccount) return accountKey(bestAccount);- // Bedrock is METERED/paid. owner directive (2026-06-14): "Bedrock should never be- // in use if any /rotate OAuth account has tokens available." The primary loop- // above can fall through to Bedrock when every account is over its util cap or- // at the concurrency cap — but a usable (non-expired) OAuth token is still- // strictly preferable to paying for Bedrock. Second pass: pick the OAuth account- // with the lowest known utilization that still has a valid token, ignoring the- // soft util/concurrency caps. Only return 'bedrock' if ZERO accounts have a- // usable token (true last resort).- let fallbackAccount = null;- let fallbackUtil = Infinity;+ // No account is under both the util cap AND the concurrency cap. Bedrock+ // fallback is DISABLED (unservable model stranded the fleet), so instead of+ // returning 'bedrock' we fall back to the COOLEST token-valid OAuth account,+ // ignoring the concurrency cap. An over-leased OAuth account still serves+ // requests; an unservable Bedrock model does not. Per-account egress IPs+ // (Phase 1) are the real fix for the concurrency/IP-reputation ceiling.+ let coolest = null;+ let coolestUtil = Infinity;for (const a of config.accounts) {if (a.disabled === true) continue;const tokenJson = readVaultToken(a);if (!tokenJson || tokenExpired(tokenJson)) continue;- const util = state.accounts?.[accountKey(a)]?.lastUtilization?.pct ?? 50;- if (util < fallbackUtil) {- fallbackUtil = util;- fallbackAccount = a;+ const util = cachedUtilizationMax(state.accounts?.[accountKey(a)]?.lastUtilization) ?? 50;+ if (util < coolestUtil) {+ coolestUtil = util;+ coolest = a;+ }+ }+ // null → caller uses the global keychain (existing behavior); never bedrock.+ return coolest ? accountKey(coolest) : null;+}++const CRS_BASE_RE = /127\.0\.0\.1:(3000|3002|3005|8091|18091)|100\.87\.53\.96:8091|:(3000|3002|3005|8091|18091)\/api/;++/**+ * ~/.claude/settings.json's env block carries a fleet-wide, autofixer-enforced+ * ANTHROPIC_BASE_URL/ANTHROPIC_API_BASE pinned to CRS (see crs-sync-update.sh+ * "checking ANTHROPIC_BASE_URL parity" autofix — it reverts removal of these+ * keys within minutes, by design). Claude Code re-applies settings.json's env+ * block internally at startup regardless of what env the spawning process set,+ * which silently re-points a direct-OAuth account-rotation session at CRS and+ * breaks it (CRS rejects the raw sk-ant-oat01 token: 401 Invalid API key format).+ *+ * Rather than fight the autofixer on the shared settings.json, generate a+ * settings copy with just those two keys stripped and point THIS spawn at it+ * via --settings, only when we've actually assigned a direct (non-cr_) OAuth+ * token. CRS-paired sessions are untouched and keep using the real settings.json.+ */+function buildDirectOauthSettingsOverride() {+ try {+ // --settings loads ADDITIONAL settings merged on top of the base+ // settings.json (confirmed empirically: "--help" says "load additional+ // settings from", and omitting a key does NOT unset the base file's+ // value — deleting keys here is a no-op). To actually neutralize the+ // CRS pin for this one spawn, explicitly override both keys to "".+ // Also blank CRS API-key fields. settings.json env injects+ // ANTHROPIC_API_KEY=cr_* fleet-wide; if we only clear BASE_URL, Claude Code+ // dual-auths (OAuth + external API key) and surfaces "Invalid API key".+ const overrideDoc = {+ env: {+ ANTHROPIC_BASE_URL: '',+ ANTHROPIC_API_BASE: '',+ ANTHROPIC_API_KEY: '',+ ANTHROPIC_AUTH_TOKEN: '',+ CRS_API_KEY: '',+ },+ };+ if (!existsSync(DIRECT_OAUTH_SETTINGS_DIR)) {+ mkdirSync(DIRECT_OAUTH_SETTINGS_DIR, { recursive: true });+ }+ const outPath = join(DIRECT_OAUTH_SETTINGS_DIR, 'direct-oauth.settings.json');+ const tmpPath = `${outPath}.${process.pid}.tmp`;+ writeFileSync(tmpPath, JSON.stringify(overrideDoc, null, 2));+ renameSync(tmpPath, outPath); // atomic replace — safe under concurrent spawns+ return outPath;+ } catch {+ return null;+ }+}++export function enforceDirectOrCrsPairing(childEnv) {+ const crsBase =+ CRS_BASE_RE.test(String(childEnv.ANTHROPIC_BASE_URL || '')) ||+ CRS_BASE_RE.test(String(childEnv.ANTHROPIC_API_BASE || ''));++ // CRS pairing: keep API_KEY=cr_ (and mirror onto AUTH/OAUTH). Do not strip API_KEY.+ if (String(childEnv.ANTHROPIC_API_KEY || '').startsWith('cr_')) {+ if (+ !String(childEnv.CLAUDE_CODE_OAUTH_TOKEN || '').startsWith('cr_') &&+ !String(childEnv.ANTHROPIC_AUTH_TOKEN || '').startsWith('cr_')+ ) {+ childEnv.CLAUDE_CODE_OAUTH_TOKEN = childEnv.ANTHROPIC_API_KEY;+ childEnv.ANTHROPIC_AUTH_TOKEN = childEnv.ANTHROPIC_API_KEY;}}- return fallbackAccount ? accountKey(fallbackAccount) : 'bedrock';++ const crToken = String(+ childEnv.CLAUDE_CODE_OAUTH_TOKEN || childEnv.ANTHROPIC_AUTH_TOKEN || childEnv.ANTHROPIC_API_KEY || '',+ ).startsWith('cr_');+ if (crsBase && crToken) {+ const crKey = String(+ childEnv.ANTHROPIC_API_KEY || childEnv.ANTHROPIC_AUTH_TOKEN || childEnv.CLAUDE_CODE_OAUTH_TOKEN || '',+ );+ if (crKey.startsWith('cr_')) {+ childEnv.ANTHROPIC_API_KEY = crKey;+ childEnv.ANTHROPIC_AUTH_TOKEN = crKey;+ childEnv.CLAUDE_CODE_OAUTH_TOKEN = crKey;+ }+ }+ if (crsBase && !crToken) {+ delete childEnv.ANTHROPIC_BASE_URL;+ delete childEnv.ANTHROPIC_API_BASE;+ delete childEnv.ANTHROPIC_AUTH_TOKEN;+ }+ if (!crsBase && crToken) {+ delete childEnv.CLAUDE_CODE_OAUTH_TOKEN;+ delete childEnv.ANTHROPIC_AUTH_TOKEN;+ delete childEnv.ANTHROPIC_API_KEY;+ }+ return childEnv;}/**@@ -305,30 +403,57 @@ export function getTokenForSession(sessionId, config) {export function spawnWithAccount(args, config, state, opts = {}) {const sessionId = opts.sessionId || `session-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;const childEnv = { ...(opts.env || process.env) };+ enforceDirectOrCrsPairing(childEnv);let assignedKey = null;- if (true) {+ const crsBaseConfigured =+ CRS_BASE_RE.test(String(childEnv.ANTHROPIC_BASE_URL || '')) ||+ CRS_BASE_RE.test(String(childEnv.ANTHROPIC_API_BASE || ''));+ const crsTokenConfigured = String(childEnv.CLAUDE_CODE_OAUTH_TOKEN || childEnv.ANTHROPIC_AUTH_TOKEN || '').startsWith(+ 'cr_',+ );++ // CRS-route the bg daemon and any explicitly CRS-configured process: skip+ // per-account OAuth injection so children keep settings.json's cr_ relay key+ // -> CRS instead of replacing it with a direct OAuth account token.+ const isDaemon = Array.isArray(args) && args.includes('daemon');+ const usePerAccountRouting = !(isDaemon || (crsBaseConfigured && crsTokenConfigured));++ if (usePerAccountRouting) {const key = pickAccountForSession(sessionId, config, state);- if (key === 'bedrock') {- applyBedrockEnv(childEnv);- assignedKey = 'bedrock';- } else if (key) {+ // BEDROCK FALLBACK DISABLED (2026-06-13): pickAccountForSession never returns+ // 'bedrock' anymore. Defensively strip any inherited Bedrock env so a child+ // never boots on the unservable model. key===null → global keychain.+ delete childEnv.CLAUDE_CODE_USE_BEDROCK;+ delete childEnv.ANTHROPIC_MODEL;+ delete childEnv.ANTHROPIC_API_KEY;+ if (key && key !== 'bedrock' && !isDaemon) {const account = config.accounts.find((a) => accountKey(a) === key);if (account) {const tokenJson = readVaultToken(account);const accessToken = tokenJson ? extractAccessToken(tokenJson) : null;if (accessToken) {- // Scrub Bedrock vars (incl. hardcoded ANTHROPIC_MODEL + AWS_*) before- // setting the OAuth token, so the session can't keep paying for Bedrock.- applyOAuthEnv(childEnv, accessToken);+ childEnv.CLAUDE_CODE_OAUTH_TOKEN = accessToken;assignedKey = key;}}}}- const proc = spawn('claude', args, {+ enforceDirectOrCrsPairing(childEnv);++ const directOauthToken =+ childEnv.CLAUDE_CODE_OAUTH_TOKEN && !String(childEnv.CLAUDE_CODE_OAUTH_TOKEN).startsWith('cr_');+ let spawnArgs = args;+ if (directOauthToken && !childEnv.ANTHROPIC_BASE_URL && !args.includes('--settings')) {+ const overridePath = buildDirectOauthSettingsOverride();+ if (overridePath) {+ spawnArgs = [...args, '--settings', overridePath];+ }+ }++ const proc = spawn(childEnv.CLAUDE_REAL_BIN || 'claude', spawnArgs, {env: childEnv,detached: opts.detached ?? false,stdio: opts.stdio ?? 'inherit',