Skip to content

fix(survival): don't treat an unreachable balance API as death - #422

Open
hfakih1212-dev wants to merge 1 commit into
Conway-Research:mainfrom
hfakih1212-dev:fix/unknown-balance-not-dead
Open

hfakih1212-dev wants to merge 1 commit into
Conway-Research:mainfrom
hfakih1212-dev:fix/unknown-balance-not-dead

Conversation

@hfakih1212-dev

Copy link
Copy Markdown

Problem

getFinancialState() returns creditsCents: -1 as a sentinel when the credits API fails and no cached balance exists (loop.ts — "No cache available -- return conservative non-zero sentinel").

But SURVIVAL_THRESHOLDS defines:

dead: -1, // negative balance = truly dead

So that sentinel, passed to getSurvivalTier(), resolves to "dead" — even though getSurvivalTier's own docstring says "Only negative balance (API-confirmed debt) = dead", and an unreachable API is not confirmed debt.

The agent loop already intends the opposite. The API_UNREACHABLE branch is commented:

// api_unreachable: creditsCents === -1 means API failed with no cache.
// Do NOT kill the agent; continue in low-compute mode and retry next tick.

…and sets low-compute mode. But the tier is recomputed from the same -1 further down, immediately before the inference call:

const survivalTier = getSurvivalTier(financial.creditsCents);

Since every entry in STATIC_MODEL_BASELINE has tierMinimum of "critical" or higher, a "dead" tier leaves no eligible model, so inference is skipped altogether.

Observed behaviour

With a reachable inference provider and no actual debt, every turn logs 0 tools, 0 tokens:

[API_UNREACHABLE] Balance API unreachable, continuing in low-compute mode.
[THINK] Routing inference (tier: dead, model: ...)...
Turn 01M4EHFY4Z30ETNDKVTRF8EGYA: 0 tools, 0 tokens
...
[IDLE] 10 consecutive idle turns with no work. Entering sleep.

The automaton boots, runs its heartbeat, cycles turns and sleeps — but never thinks. One value was serving as both "unknown" and "confirmed debt".

Fix

Add a named BALANCE_UNKNOWN sentinel and resolveSurvivalTier(), which maps unknown → "low_compute" and otherwise delegates unchanged to getSurvivalTier(). The sentinel is also named at its producer and its existing consumer so the coupling is explicit.

Scope

Only the -1 sentinel changes behaviour. A genuine negative balance still resolves to dead, and every other threshold is identical:

cents before after
-1 dead low_compute
-500 dead dead
0 critical critical
11 low_compute low_compute
51 normal normal
501 high high

Other getSurvivalTier() call sites are unaffected: loop.ts:438/:473 sit inside the else where creditsCents !== -1, and heartbeat/tick-context.ts and survival/monitor.ts both default to 0 on fetch failure rather than producing the sentinel.

Testing

  • tsc --noEmit clean
  • financial.test.ts + loop.test.ts: 47/47 pass
  • Boundary values verified against the table above

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant