From 540813b84ab3dd3b95609deb6a3f218bcd222c21 Mon Sep 17 00:00:00 2001 From: hfakih1212-dev Date: Thu, 8 Oct 2026 23:20:18 +0300 Subject: [PATCH] fix(survival): don't treat an unreachable balance API as death getFinancialState() returns creditsCents: -1 as a sentinel when the credits API fails and no cached balance exists. SURVIVAL_THRESHOLDS defines dead: -1 ("negative balance = truly dead"), so passing that sentinel to getSurvivalTier() yields "dead". The agent loop already intends the opposite. The API_UNREACHABLE branch is commented "Do NOT kill the agent; continue in low-compute mode and retry next tick" and sets low-compute mode -- but the tier is then recomputed from the same -1 further down, before the inference call: const survivalTier = getSurvivalTier(financial.creditsCents); Because every entry in the model registry has tierMinimum "critical" or higher, a "dead" tier leaves no eligible model, so inference is skipped entirely. Observed effect: every turn logs "0 tools, 0 tokens", the loop accumulates idle turns and sleeps, and the automaton never thinks -- with a reachable inference provider and no actual debt. One value was serving as both "unknown" and "confirmed debt". Introduce a named BALANCE_UNKNOWN sentinel and resolveSurvivalTier(), which maps unknown to "low_compute" and otherwise delegates unchanged to getSurvivalTier(). An unreachable billing API is an infrastructure failure, not evidence the agent is out of money. Only the -1 sentinel changes behaviour; a genuine negative balance still resolves to "dead" and every other threshold is untouched: -1 dead -> low_compute -500 dead -> dead 0 critical -> critical 11 low_compute -> low_compute 51 normal -> normal 501 high -> high financial.test.ts and loop.test.ts pass (47/47); tsc --noEmit clean. Co-Authored-By: Claude Opus 5 --- src/agent/loop.ts | 8 ++++---- src/conway/credits.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/agent/loop.ts b/src/agent/loop.ts index aad14278a..dd62de4cd 100644 --- a/src/agent/loop.ts +++ b/src/agent/loop.ts @@ -35,7 +35,7 @@ import { executeTool, } from "./tools.js"; import { sanitizeInput } from "./injection-defense.js"; -import { getSurvivalTier } from "../conway/credits.js"; +import { getSurvivalTier, resolveSurvivalTier, BALANCE_UNKNOWN } from "../conway/credits.js"; import { getUsdcBalance } from "../conway/x402.js"; import { claimInboxMessages, @@ -431,7 +431,7 @@ export async function runAgentLoop( // Check survival tier // api_unreachable: creditsCents === -1 means API failed with no cache. // Do NOT kill the agent; continue in low-compute mode and retry next tick. - if (financial.creditsCents === -1) { + if (financial.creditsCents === BALANCE_UNKNOWN) { log(config, "[API_UNREACHABLE] Balance API unreachable, continuing in low-compute mode."); inference.setLowComputeMode(true); } else { @@ -596,7 +596,7 @@ export async function runAgentLoop( pendingInput = undefined; // ── Inference Call (via router when available) ── - const survivalTier = getSurvivalTier(financial.creditsCents); + const survivalTier = resolveSurvivalTier(financial.creditsCents); log(config, `[THINK] Routing inference (tier: ${survivalTier}, model: ${inference.getDefaultModel()})...`); const inferenceTools = toolsToInferenceFormat(tools); @@ -977,7 +977,7 @@ async function getFinancialState( // No cache available -- return conservative non-zero sentinel logger.error("Balance API failed, no cache available"); return { - creditsCents: -1, + creditsCents: BALANCE_UNKNOWN, usdcBalance: -1, lastChecked: new Date().toISOString(), }; diff --git a/src/conway/credits.ts b/src/conway/credits.ts index ba9f63e1e..e1f8b77b1 100644 --- a/src/conway/credits.ts +++ b/src/conway/credits.ts @@ -43,6 +43,25 @@ export function getSurvivalTier(creditsCents: number): SurvivalTier { return "dead"; } +/** + * Sentinel used when the balance API is unreachable and no cached balance + * exists. It is deliberately NOT a real balance: "unknown" must never be + * read as the API-confirmed debt that getSurvivalTier() treats as "dead". + */ +export const BALANCE_UNKNOWN = -1; + +/** + * Map a possibly-unknown balance to a survival tier. + * + * When the balance is unknown the automaton degrades to "low_compute" + * instead of dying: an unreachable billing API is an infrastructure + * failure, not evidence that the agent is out of money. Retried next tick. + */ +export function resolveSurvivalTier(creditsCents: number): SurvivalTier { + if (creditsCents === BALANCE_UNKNOWN) return "low_compute"; + return getSurvivalTier(creditsCents); +} + /** * Format a credit amount for display. */