Repository navigation
fix(survival): don't treat an unreachable balance API as death - #422
Open
hfakih1212-dev wants to merge 1 commit into
Open
hfakih1212-dev wants to merge 1 commit into
hfakih1212-dev wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
getFinancialState()returnscreditsCents: -1as a sentinel when the credits API fails and no cached balance exists (loop.ts— "No cache available -- return conservative non-zero sentinel").But
SURVIVAL_THRESHOLDSdefines:So that sentinel, passed to
getSurvivalTier(), resolves to"dead"— even thoughgetSurvivalTier'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_UNREACHABLEbranch is commented:…and sets low-compute mode. But the tier is recomputed from the same
-1further down, immediately before the inference call:Since every entry in
STATIC_MODEL_BASELINEhastierMinimumof"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: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_UNKNOWNsentinel andresolveSurvivalTier(), which maps unknown →"low_compute"and otherwise delegates unchanged togetSurvivalTier(). The sentinel is also named at its producer and its existing consumer so the coupling is explicit.Scope
Only the
-1sentinel changes behaviour. A genuine negative balance still resolves todead, and every other threshold is identical:-1deadlow_compute-500deaddead0criticalcritical11low_computelow_compute51normalnormal501highhighOther
getSurvivalTier()call sites are unaffected:loop.ts:438/:473sit inside theelsewherecreditsCents !== -1, andheartbeat/tick-context.tsandsurvival/monitor.tsboth default to0on fetch failure rather than producing the sentinel.Testing
tsc --noEmitcleanfinancial.test.ts+loop.test.ts: 47/47 pass🤖 Generated with Claude Code