Skip to content

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.

fix(rotation): block Bedrock fallback + preserve CRS routing on mode flips (#589)

Aurora Capitala2e52cbaf9e7
diff --git a/claude-ops/scripts/account-rotation/__tests__/claude-settings-mode-crs.test.mjs b/claude-ops/scripts/account-rotation/__tests__/claude-settings-mode-crs.test.mjs
new file mode 100644
index 00000000..ca7f5baa
--- /dev/null
+++ b/claude-ops/scripts/account-rotation/__tests__/claude-settings-mode-crs.test.mjs
@@ -0,0 +1,119 @@
+/**
+ * claude-settings-mode CRS-awareness tests (node --test).
+ *
+ * Run: node --test claude-ops/scripts/account-rotation/__tests__/claude-settings-mode-crs.test.mjs
+ *
+ * Guards the money-leak fix: a CRS-routed box (cr_ token + ANTHROPIC_BASE_URL)
+ * must never be flipped onto metered Bedrock, and an OAuth-mode flip must keep
+ * the CRS relay pair intact (a cr_ token with no base URL 401s the relay).
+ *
+ * These functions read/write ~/.claude/settings.json via $HOME, so each test
+ * points HOME at a fresh temp dir. persistBedrockClaudeSettings on a CRS box
+ * throws BEFORE any `aws` exec, so no AWS mocking is needed for that path.
+ */
+
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+import { persistBedrockClaudeSettings, clearHardcodedModelsForOAuthClaudeSettings } from '../claude-settings-mode.mjs';
+
+const CRS_BASE = 'http://127.0.0.1:3005/api';
+const CRS_TOKEN = 'cr_deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef';
+
+function withTempHome(initialEnv) {
+ const home = mkdtempSync(join(tmpdir(), 'crs-settings-test-'));
+ mkdirSync(join(home, '.claude'), { recursive: true });
+ writeFileSync(join(home, '.claude', 'settings.json'), JSON.stringify({ env: initialEnv }, null, 2));
+ const prevHome = process.env.HOME;
+ process.env.HOME = home;
+ return {
+ readEnv() {
+ return JSON.parse(readFileSync(join(home, '.claude', 'settings.json'), 'utf8')).env || {};
+ },
+ cleanup() {
+ process.env.HOME = prevHome;
+ rmSync(home, { recursive: true, force: true });
+ },
+ };
+}
+
+test('clearHardcodedModelsForOAuth: preserves CRS relay pair across Bedrock→OAuth flip', () => {
+ const ctx = withTempHome({
+ CLAUDE_CODE_USE_BEDROCK: '1',
+ AWS_REGION: 'us-east-1',
+ ANTHROPIC_MODEL: 'us.anthropic.claude-sonnet-4-6',
+ ANTHROPIC_BASE_URL: CRS_BASE,
+ CLAUDE_CODE_OAUTH_TOKEN: CRS_TOKEN,
+ });
+ try {
+ clearHardcodedModelsForOAuthClaudeSettings();
+ const env = ctx.readEnv();
+ assert.equal(env.ANTHROPIC_BASE_URL, CRS_BASE, 'CRS base URL restored');
+ assert.equal(env.CLAUDE_CODE_OAUTH_TOKEN, CRS_TOKEN, 'CRS token retained');
+ assert.equal('CLAUDE_CODE_USE_BEDROCK' in env, false, 'Bedrock flag cleared');
+ assert.equal('ANTHROPIC_MODEL' in env, false, 'hardcoded model cleared');
+ assert.equal('AWS_REGION' in env, false, 'AWS region cleared');
+ } finally {
+ ctx.cleanup();
+ }
+});
+
+test('clearHardcodedModelsForOAuth: non-CRS box still strips ANTHROPIC_BASE_URL (regression)', () => {
+ const ctx = withTempHome({
+ CLAUDE_CODE_USE_BEDROCK: '1',
+ ANTHROPIC_BASE_URL: 'https://some-proxy.example/api',
+ CLAUDE_CODE_OAUTH_TOKEN: 'sk-ant-oat-not-a-relay-token',
+ ANTHROPIC_MODEL: 'us.anthropic.claude-sonnet-4-6',
+ });
+ try {
+ clearHardcodedModelsForOAuthClaudeSettings();
+ const env = ctx.readEnv();
+ assert.equal('ANTHROPIC_BASE_URL' in env, false, 'non-cr_ base URL stripped (raw OAuth)');
+ assert.equal('CLAUDE_CODE_USE_BEDROCK' in env, false);
+ } finally {
+ ctx.cleanup();
+ }
+});
+
+test('persistBedrockClaudeSettings: refuses to flip a CRS-routed box onto Bedrock', () => {
+ const ctx = withTempHome({
+ ANTHROPIC_BASE_URL: CRS_BASE,
+ CLAUDE_CODE_OAUTH_TOKEN: CRS_TOKEN,
+ });
+ try {
+ assert.throws(() => persistBedrockClaudeSettings('us-east-1'), /CRS-routed/, 'throws on CRS box');
+ const env = ctx.readEnv();
+ // settings must be untouched — no Bedrock vars written, CRS pair intact.
+ assert.equal('CLAUDE_CODE_USE_BEDROCK' in env, false, 'no Bedrock flag written');
+ assert.equal(env.ANTHROPIC_BASE_URL, CRS_BASE, 'CRS base URL untouched');
+ assert.equal(env.CLAUDE_CODE_OAUTH_TOKEN, CRS_TOKEN, 'CRS token untouched');
+ } finally {
+ ctx.cleanup();
+ }
+});
+
+test('persistBedrockClaudeSettings: CLAUDE_ALLOW_BEDROCK_OVER_CRS=1 overrides the refusal', () => {
+ const ctx = withTempHome({
+ ANTHROPIC_BASE_URL: CRS_BASE,
+ CLAUDE_CODE_OAUTH_TOKEN: CRS_TOKEN,
+ });
+ const prev = process.env.CLAUDE_ALLOW_BEDROCK_OVER_CRS;
+ const prevSkip = process.env.BEDROCK_SKIP_RESOLVE;
+ process.env.CLAUDE_ALLOW_BEDROCK_OVER_CRS = '1';
+ process.env.BEDROCK_SKIP_RESOLVE = '1'; // avoid live `aws bedrock` calls in CI
+ try {
+ persistBedrockClaudeSettings('us-east-1');
+ const env = ctx.readEnv();
+ assert.equal(env.CLAUDE_CODE_USE_BEDROCK, '1', 'override allows Bedrock flip');
+ assert.equal('ANTHROPIC_BASE_URL' in env, false, 'base URL dropped when overridden onto Bedrock');
+ } finally {
+ if (prev === undefined) delete process.env.CLAUDE_ALLOW_BEDROCK_OVER_CRS;
+ else process.env.CLAUDE_ALLOW_BEDROCK_OVER_CRS = prev;
+ if (prevSkip === undefined) delete process.env.BEDROCK_SKIP_RESOLVE;
+ else process.env.BEDROCK_SKIP_RESOLVE = prevSkip;
+ ctx.cleanup();
+ }
+});
diff --git a/claude-ops/scripts/account-rotation/claude-settings-mode.mjs b/claude-ops/scripts/account-rotation/claude-settings-mode.mjs
index 401149d5..75e08691 100644
--- a/claude-ops/scripts/account-rotation/claude-settings-mode.mjs
+++ b/claude-ops/scripts/account-rotation/claude-settings-mode.mjs
@@ -15,6 +15,25 @@ function claudeSettingsPath() {
return join(process.env.HOME || '', '.claude', 'settings.json');
}
+// ── CRS (Claude Relay Service) routing awareness ───────────────────────────────
+// When a box routes through CRS, ANTHROPIC_BASE_URL points at the local relay
+// (e.g. http://127.0.0.1:3005/api) and CLAUDE_CODE_OAUTH_TOKEN holds a static
+// `cr_…` relay token. Both must travel together — a `cr_` token with no base URL
+// 401s. The Bedrock/OAuth settings mutators below were written for the raw-OAuth /
+// Bedrock world and would silently strip ANTHROPIC_BASE_URL, stranding a CRS box.
+// These helpers detect that routing and snapshot/restore the pair so a mode flip
+// never breaks (or money-leaks past) a CRS-routed fleet.
+function isCrsToken(t) {
+ return typeof t === 'string' && t.startsWith('cr_');
+}
+
+/** True when settings.env is wired for CRS relay routing (cr_ token + base URL). */
+function detectCrsRouting(env) {
+ const token = env.CLAUDE_CODE_OAUTH_TOKEN;
+ const base = env.ANTHROPIC_BASE_URL;
+ return isCrsToken(token) && typeof base === 'string' && base.length > 0 ? { token, base } : null;
+}
+
function readSettings() {
try {
return JSON.parse(readFileSync(claudeSettingsPath(), 'utf8'));
@@ -228,6 +247,19 @@ export function resolveBedrockClaudeModelIds(region = 'us-east-1') {
/** Match use-bedrock.sh: Bedrock env + strip top-level model lists (OAuth catalog ids break Bedrock). */
export function persistBedrockClaudeSettings(region = 'us-east-1') {
+ const s0 = readSettings();
+ const env0 = s0.env && typeof s0.env === 'object' ? s0.env : {};
+ // Defense-in-depth behind the activateBedrockFallback kill-switch: a CRS-routed
+ // box must NEVER be flipped onto metered Bedrock — the free relay pool already
+ // multiplexes every Max account. Refuse loudly so callers' try/catch logs a
+ // failure instead of silently claiming "Bedrock active" + money-leaking.
+ // Override with CLAUDE_ALLOW_BEDROCK_OVER_CRS=1 if ever genuinely intended.
+ if (detectCrsRouting(env0) && process.env.CLAUDE_ALLOW_BEDROCK_OVER_CRS !== '1') {
+ throw new Error(
+ 'refusing Bedrock flip: box is CRS-routed (cr_ token + ANTHROPIC_BASE_URL). ' +
+ 'CRS pool already covers capacity; Bedrock is metered. Set CLAUDE_ALLOW_BEDROCK_OVER_CRS=1 to override.',
+ );
+ }
const { primary, small, opus, fable } = resolveBedrockClaudeModelIds(region);
const aws = resolveWorkingAwsEnv(region);
const s = readSettings();
@@ -262,6 +294,12 @@ export function persistBedrockClaudeSettings(region = 'us-east-1') {
export function clearHardcodedModelsForOAuthClaudeSettings() {
const s = readSettings();
const env = s.env && typeof s.env === 'object' ? { ...s.env } : {};
+ // Preserve CRS relay routing across the flip: a `cr_` token + ANTHROPIC_BASE_URL
+ // must survive together. The generic clear below strips ANTHROPIC_BASE_URL (it
+ // assumes raw OAuth, where base-url must be absent), which on a CRS box would
+ // leave the cr_ token pointing at api.anthropic.com → 401, killing the relay.
+ // Snapshot the pair before clearing, then restore it.
+ const crs = detectCrsRouting(env);
for (const k of [
'CLAUDE_CODE_USE_BEDROCK',
'AWS_BEDROCK_REGION',
@@ -278,6 +316,11 @@ export function clearHardcodedModelsForOAuthClaudeSettings() {
]) {
delete env[k];
}
+ if (crs) {
+ // Restore the relay pair so the box keeps routing through CRS, not raw OAuth.
+ env.ANTHROPIC_BASE_URL = crs.base;
+ env.CLAUDE_CODE_OAUTH_TOKEN = crs.token;
+ }
s.env = env;
delete s.model;
delete s.availableModels;
diff --git a/claude-ops/scripts/account-rotation/rotate.mjs b/claude-ops/scripts/account-rotation/rotate.mjs
index dff33c8c..6dc51231 100644
--- a/claude-ops/scripts/account-rotation/rotate.mjs
+++ b/claude-ops/scripts/account-rotation/rotate.mjs
@@ -743,6 +743,22 @@ function stopClaudeDaemon() {
// Cannot mutate env of an already-running session — user must start a new
// one with `source ~/.claude/scripts/account-rotation/use-bedrock.sh`.
async function activateBedrockFallback(reason, dryRun = false) {
+ // Kill-switch — mirror daemon.mjs activateBedrockFallbackFromDaemon (disabled
+ // 2026-05-08, Max/CRS plan handles capacity; Bedrock is metered AWS = $600-1.2k/mo
+ // bleed). BLOCKED by default; set CLAUDE_DISABLE_BEDROCK_FALLBACK=0 to re-enable.
+ // Authoritative here regardless of caller so the rotate path can never silently
+ // flip a CRS/Max-routed box onto metered Bedrock (the bedrockOnExhausted opt
+ // defaults ON, unlike the daemon — this is the backstop).
+ if (process.env.CLAUDE_DISABLE_BEDROCK_FALLBACK !== '0') {
+ log(`[bedrock-fallback] BLOCKED by CLAUDE_DISABLE_BEDROCK_FALLBACK (reason was: ${reason})`);
+ if (!dryRun) {
+ notify(
+ 'Bedrock Fallback BLOCKED',
+ 'Kill-switch active — Max/CRS-only mode. Wait for reset window or rotate manually.',
+ );
+ }
+ return false;
+ }
const region = process.env.AWS_BEDROCK_REGION || 'us-east-1';
const result = checkBedrockAvailable(region);
const sentinel = join(process.env.HOME || '', '.claude', '.bedrock-fallback.json');