Budget guard (#66)
Opt-in, off when runtime.budget is absent. runtime.budget = { maxCalls?, maxCallsPerTool?: {tool: int}, maxCost?, costFn?(tool, input) -> number }. Limits must be positive integers (maxCost: positive finite number), otherwise create() throws. costFn is host code and never comes from JSON.
Pipeline order: resolve -> enabled -> approval -> budget.reserve -> dispatch -> budget.commit -> envelope.
- Reserve is synchronous before dispatch, so concurrent calls cannot overspend.
- Exceeding any limit returns
BUDGET_EXCEEDED(recoverable: false) withmeta.callsUsed/meta.callsRemaining(the latter only whenmaxCallsis set). - A dispatched call counts even if the tool fails.
TOOL_DISABLED, unknown tools andAPPROVAL_*outcomes never reach reserve. - A
costFnthat throws or returns a negative/non-finite number fails closed withBUDGET_EXCEEDED. - State lives in the
PolicyStateStorekeyed bypolicyScope(never the spoofable inputsessionId), underbudget:calls,budget:cost,budget:tool:<name>.kv.*andstate.checkpointcannot reset it. - Retries and cache hits do not consume extra budget (see retry and idempotency).
Usage: WarTools.budgetUsage() (Node), budget_usage() (Python), BudgetUsage() (.NET) return {callsUsed, callsRemaining?, costUsed}. Successful and denied envelopes carry meta.callsUsed / meta.callsRemaining when a budget is configured.