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): create the deferred-respawn marker atomically (#707)
Aurora Capital406e18c79fd6
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 323ade3c..2a57f354 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -3,7 +3,9 @@ on:pull_request:branches: [dev, main]push:- branches: [dev]+ # main is included so formatting and checks are verified after a merge, not+ # only on the pull request. Without it, drift can land on main unnoticed.+ branches: [dev, main]concurrency:diff --git a/claude-ops/scripts/account-rotation/bg-respawn.mjs b/claude-ops/scripts/account-rotation/bg-respawn.mjsindex 71b131fd..bf1ad78a 100644--- a/claude-ops/scripts/account-rotation/bg-respawn.mjs+++ b/claude-ops/scripts/account-rotation/bg-respawn.mjs@@ -553,7 +553,10 @@ export function respawnBgSessions(log = () => {}) {}if (s.status === 'busy') {try {- if (!existsSync(DEFERRED_MARKER(s.id))) writeFileSync(DEFERRED_MARKER(s.id), String(Date.now()));+ // 'wx' creates the marker in one step, so a second process cannot slip in+ // between a check and the write. EEXIST just means the marker is already+ // there, which is the same outcome we wanted. mode keeps it owner-only.+ writeFileSync(DEFERRED_MARKER(s.id), String(Date.now()), { flag: 'wx', mode: 0o600 });} catch {}deferred++;log(`[bg-respawn] ${s.id} busy — deferred (daemon sweep will retry, force after 90min)`);diff --git a/claude-ops/scripts/account-rotation/claude-stack.mjs b/claude-ops/scripts/account-rotation/claude-stack.mjsindex b97eb3c3..649bee1e 100644--- a/claude-ops/scripts/account-rotation/claude-stack.mjs+++ b/claude-ops/scripts/account-rotation/claude-stack.mjs@@ -99,8 +99,7 @@ function accountRotationStatus() {function status() {const route = routeStatus();- const crsHealthUrl =- route.state.crs?.healthUrl || process.env.CRS_HEALTH_URL || 'http://127.0.0.1:3005/health';+ const crsHealthUrl = route.state.crs?.healthUrl || process.env.CRS_HEALTH_URL || 'http://127.0.0.1:3005/health';return {ok: !route.settings.mixedProvider,route,diff --git a/claude-ops/scripts/account-rotation/crs-401-refresher.mjs b/claude-ops/scripts/account-rotation/crs-401-refresher.mjsindex 35d59976..bc34f8c6 100644--- a/claude-ops/scripts/account-rotation/crs-401-refresher.mjs+++ b/claude-ops/scripts/account-rotation/crs-401-refresher.mjs@@ -71,7 +71,11 @@ const C = cfg.crs || {};const num = (v, d) => (v === undefined || v === null || v === '' || Number.isNaN(+v) ? d : +v);function truthy(v) {- return ['1', 'true', 'yes', 'on'].includes(String(v ?? '').trim().toLowerCase());+ return ['1', 'true', 'yes', 'on'].includes(+ String(v ?? '')+ .trim()+ .toLowerCase(),+ );}const ENABLED = truthy(process.env.CRS_TOKEN_REFRESH_ENABLED) || C.tokenRefreshEnabled === true;@@ -160,8 +164,8 @@ function authoritativeRateLimited(a, nowMs) {const reason = status.reason || a.rateLimitReason;return Boolean((status.isRateLimited === true || a.rateLimitStatus?.isRateLimited === 'true') &&- end > nowMs &&- String(reason || '').includes('authoritative_reset'),+ end > nowMs &&+ String(reason || '').includes('authoritative_reset'),);}@@ -169,8 +173,9 @@ function hardHeld(a, nowMs) {const status = String(a.status || '');if (['blocked', 'auth_repair', 'error'].includes(status)) return true;return (- futureMs(a.rateLimitEndAt || a.rateLimitStatus?.rateLimitEndAt || a.rateLimitStatus?.resetAt || a.weeklyRateLimitEndAt) >- nowMs+ futureMs(+ a.rateLimitEndAt || a.rateLimitStatus?.rateLimitEndAt || a.rateLimitStatus?.resetAt || a.weeklyRateLimitEndAt,+ ) > nowMs);}@@ -207,7 +212,9 @@ async function refreshAccount(auth, a) {const delayMs = retryAfterHdr? Math.min(15 * 60_000, Math.max(2_000, parseInt(retryAfterHdr, 10) * 1000 + 2000)): 30_000;- log(` ${a.name}: refresh 429 (attempt ${attempt}/${MAX_RETRIES}) — waiting ${Math.round(delayMs / 1000)}s (Retry-After=${retryAfterHdr ?? 'n/a'})...`);+ log(+ ` ${a.name}: refresh 429 (attempt ${attempt}/${MAX_RETRIES}) — waiting ${Math.round(delayMs / 1000)}s (Retry-After=${retryAfterHdr ?? 'n/a'})...`,+ );await new Promise((r2) => setTimeout(r2, delayMs));continue;}@@ -219,7 +226,8 @@ async function refreshAccount(auth, a) {body = txt;}const ok = r.status === 200 && (body?.success === true || body?.data?.success === true || body?.data?.accessToken);- const err = body?.message || body?.error?.message || body?.error || (typeof body === 'string' ? body.slice(0, 120) : '');+ const err =+ body?.message || body?.error?.message || body?.error || (typeof body === 'string' ? body.slice(0, 120) : '');if (r.status === 200 || attempt === MAX_RETRIES) {return { ok, status: r.status, err, retryAfter: lastRetryAfter };}@@ -290,7 +298,9 @@ async function tick() {if (authLimited) {if (a.schedulable !== false) {const off = await setSchedulable(auth, a, false);- log(`${a.name}: authoritative rate limit until ${a.rateLimitStatus?.rateLimitEndAt || a.rateLimitEndAt} — ${off ? 'SIDELINED' : 'sideline FAILED'}`);+ log(+ `${a.name}: authoritative rate limit until ${a.rateLimitStatus?.rateLimitEndAt || a.rateLimitEndAt} — ${off ? 'SIDELINED' : 'sideline FAILED'}`,+ );} else {log(`${a.name}: authoritative rate limit already sidelined`);}@@ -329,7 +339,9 @@ async function tick() {const expired = !exp || exp <= nowMs + REFRESH_WINDOW_MS;if (expired && a.schedulable !== false) {const off = await setSchedulable(auth, a, false);- log(`${a.name}: refresh FAILED (HTTP ${res.status}: ${res.err}) — dead token, ${off ? 'SIDELINED' : 'sideline FAILED'}, needs full re-auth`);+ log(+ `${a.name}: refresh FAILED (HTTP ${res.status}: ${res.err}) — dead token, ${off ? 'SIDELINED' : 'sideline FAILED'}, needs full re-auth`,+ );} else {log(`${a.name}: refresh FAILED (HTTP ${res.status}: ${res.err}) — needs full re-auth`);}diff --git a/claude-ops/scripts/account-rotation/crs-429-cooldown.mjs b/claude-ops/scripts/account-rotation/crs-429-cooldown.mjsindex 0827d883..71ed9666 100644--- a/claude-ops/scripts/account-rotation/crs-429-cooldown.mjs+++ b/claude-ops/scripts/account-rotation/crs-429-cooldown.mjs@@ -74,7 +74,11 @@ const cfg = loadRotationConfig();const C = cfg.crs || {};function truthy(v) {- return ['1', 'true', 'yes', 'on'].includes(String(v ?? '').trim().toLowerCase());+ return ['1', 'true', 'yes', 'on'].includes(+ String(v ?? '')+ .trim()+ .toLowerCase(),+ );}const ENABLED = truthy(process.env.CRS_COOLDOWN_ENABLED) || C.cooldownEnabled === true;@@ -318,7 +322,12 @@ async function main() {main().catch((e) => {const message = e?.message || String(e);- if (message === 'fetch failed' || message.includes('ECONNREFUSED') || message.includes('ECONNRESET') || message.includes('ETIMEDOUT')) {+ if (+ message === 'fetch failed' ||+ message.includes('ECONNREFUSED') ||+ message.includes('ECONNRESET') ||+ message.includes('ETIMEDOUT')+ ) {log('WARN transient CRS fetch failure; skipping this tick');process.exit(0);}diff --git a/claude-ops/scripts/account-rotation/crs-pool-config.mjs b/claude-ops/scripts/account-rotation/crs-pool-config.mjsindex 6d02db0b..419e707d 100644--- a/claude-ops/scripts/account-rotation/crs-pool-config.mjs+++ b/claude-ops/scripts/account-rotation/crs-pool-config.mjs@@ -107,7 +107,16 @@ export function credentialStoreCandidates() {return [process.env.OPS_CREDENTIAL_STORE,join(homedir(), '.claude', 'plugins', 'cache', 'ops-marketplace', 'ops', 'current', 'lib', 'credential-store.sh'),- join(homedir(), '.claude', 'plugins', 'marketplaces', 'ops-marketplace', 'claude-ops', 'lib', 'credential-store.sh'),+ join(+ homedir(),+ '.claude',+ 'plugins',+ 'marketplaces',+ 'ops-marketplace',+ 'claude-ops',+ 'lib',+ 'credential-store.sh',+ ),join(pluginRoot, 'lib', 'credential-store.sh'),].filter(Boolean);}diff --git a/claude-ops/scripts/account-rotation/daemon.mjs b/claude-ops/scripts/account-rotation/daemon.mjsindex 37bf7029..9bb1c68d 100644--- a/claude-ops/scripts/account-rotation/daemon.mjs+++ b/claude-ops/scripts/account-rotation/daemon.mjs@@ -1567,7 +1567,17 @@ async function checkSessionLeaseRotations(config, state) {} else {try {const marker = `/tmp/claude-respawn-deferred-${s.id}`;- if (!existsSync(marker)) writeFileSync(marker, String(Date.now()));+ // 'wx' creates the marker in one step, so a second process cannot slip in+ // between a check and the write. EEXIST just means the marker is already+ // there, which is the same outcome we wanted. mode keeps it owner-only.+ try {+ writeFileSync(marker, String(Date.now()), { flag: 'wx', mode: 0o600 });+ } catch (err) {+ // EEXIST means another sweep already wrote the marker, so carry on and log.+ // Anything else is a real write failure: fall out to the outer catch so we+ // don't claim a deferral the sweep has no marker to act on.+ if (err.code !== 'EEXIST') throw err;+ }log(`[session-router] Session ${s.id} is ${s.status === 'busy' ? 'busy' : 'a /loop session'} — deferred respawn (sweep handles it).`,);@@ -1591,7 +1601,10 @@ async function checkSessionLeaseRotations(config, state) {} else {try {const marker = `/tmp/claude-respawn-deferred-${s.id}`;- if (!existsSync(marker)) writeFileSync(marker, String(Date.now()));+ // 'wx' creates the marker in one step, so a second process cannot slip in+ // between a check and the write. EEXIST just means the marker is already+ // there, which is the same outcome we wanted. mode keeps it owner-only.+ writeFileSync(marker, String(Date.now()), { flag: 'wx', mode: 0o600 });} catch {}}return; // Rotate at most one session per tick to stagger@@ -1622,7 +1635,17 @@ async function checkSessionLeaseRotations(config, state) {} else {try {const marker = `/tmp/claude-respawn-deferred-${s.id}`;- if (!existsSync(marker)) writeFileSync(marker, String(Date.now()));+ // 'wx' creates the marker in one step, so a second process cannot slip in+ // between a check and the write. EEXIST just means the marker is already+ // there, which is the same outcome we wanted. mode keeps it owner-only.+ try {+ writeFileSync(marker, String(Date.now()), { flag: 'wx', mode: 0o600 });+ } catch (err) {+ // EEXIST means another sweep already wrote the marker, so carry on and log.+ // Anything else is a real write failure: fall out to the outer catch so we+ // don't claim a deferral the sweep has no marker to act on.+ if (err.code !== 'EEXIST') throw err;+ }log(`[session-router] Session ${s.id} is busy or looping. Deferred rotation to idle state.`);} catch {}}