SpendControl constructor's "nothing to lose" reasoning doesn't hold for a restart after a limit was already configured
Filing this as a gap description, in the spirit of https://github.com/BlockRunAI/ClawRouter/issues/329.
SpendControl's handling of UnreadableSpendPolicyError differs between the constructor and reloadLimits(), and the comment on the constructor path explains why:
The constructor can start fresh on an unreadable file because it has nothing to lose. A reload does: dropping the live limits here would widen what the agent may pay because a read failed. Keep enforcing what is already loaded and leave any refusal state alone.
That reasoning holds for a process that has genuinely never had a policy. It does not hold for a restart: at that point spending.json may already hold real limits from before, and the constructor has no way to tell "never configured" apart from "configured, but this read failed" (existsSync returning false is the only case that reaches return null with no error at all; anything else, including a transient permission or I/O failure on an existing file, becomes UnreadableSpendPolicyError). The constructor's own in-memory state is empty either way, so "nothing to lose" is true locally, but not true of the limit the operator already set.
I wrote a small test against SpendControl directly (injecting a SpendControlStorage that throws on load(), no real file I/O) to check this is real rather than theoretical:
- A fresh
SpendControlwhereload()throwsUnreadableSpendPolicyError:getPolicyFileError()isundefined,getLimits()is{}, andcheck(1_000_000)returnsallowed: true. - A
SpendControlthat first loads{ daily: 20 }successfully, then callsreloadLimits()against a storage that now throws the sameUnreadableSpendPolicyError:getLimits()still reports{ daily: 20 }, andcheck(1_000_000)returnsallowed: false.
Same underlying error, same previously-configured $20/day limit, opposite outcome depending only on whether the process happened to restart (clean slate) or stay up through a later read failure (reload). A deploy, crash, or container recycle is also when a transient file-permission or I/O glitch is most likely to show up, so the two tend to land together.
Possible fix shape, without changing the "don't fail closed just because a file briefly glitched" intent: have the constructor's catch block for UnreadableSpendPolicyError check whether spending.json exists at all (the existsSync result is already computed one layer down, inside FileSpendControlStorage.load()) rather than treating "doesn't exist" and "exists but failed to read" the same way the constructor does today. A file that exists but can't be read right now is evidence a policy probably was configured, even if this instance can't see what it said.