{"record":{"id":"e61b63e941b67b5f","repo":"JuliusBrussee/caveman","slug":"cave-budget-release-exceeds-max","errorCode":"cave_budget_release_exceeds_max","errorMessage":"cave_budget_release_exceeds_max","messagePattern":"cave_budget_release_exceeds_max","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/budget.ts","lineNumber":338,"sourceCode":"   * pre-flight validation at a developer-controlled checkpoint, so the caller\n   * asked for something the contract cannot grant.\n   */\n  release(amount: number, reason: string): BudgetTranche {\n    if (!Number.isFinite(amount) || amount <= 0) {\n      throw new Error(\"cave_budget_release_invalid\");\n    }\n    if (this.denomination === \"tokens\" && !Number.isSafeInteger(amount)) {\n      throw new Error(\"cave_budget_release_invalid\");\n    }\n    if (typeof reason !== \"string\" || reason.trim() === \"\") {\n      throw new Error(\"cave_budget_release_reason_required\");\n    }\n    if (this.revokedFlag) throw new Error(\"cave_budget_revoked\");\n    // A breached ledger is dead. Releasing into it would record a tranche and\n    // raise an escalation for money that can never be spent, and would read on\n    // the receipt as a run that was still being funded after it went past cap.\n    if (this.breachedFlag) throw new Error(\"cave_budget_cap_breached\");\n    if (amount > this.releasable()) throw new Error(\"cave_budget_release_exceeds_max\");\n    this.releasedAmount += amount;\n    const tranche: BudgetTranche = Object.freeze({\n      amount,\n      reason,\n      atCall: this.callIndex,\n    });\n    this.trancheLog.push(tranche);\n    return tranche;\n  }\n\n  /**\n   * Hold `amount` against the ledger. Returns `undefined` when it does not fit,\n   * which is the caller's signal to clamp, compact, or stop — never to proceed.\n   */\n  reserve(amount: number, outputTokenCap: number): BudgetReservation | undefined {\n    const held = this.hold(amount, outputTokenCap);\n    if (held !== undefined) this.callIndex++;\n    return held;","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/budget.ts#L320-L356","documentation":"`BudgetMeter.release` throws `cave_budget_release_exceeds_max` when the requested amount exceeds `releasable()` — `max - releasedAmount`, the budget that could still be released without breaching `max`. This is pre-flight validation at a developer-controlled checkpoint: the caller asked for a tranche the contract cannot grant, so the library refuses instead of allowing the release (releases can top a staged budget up to `max`, never past it — a release never causes a breach). Nothing is mutated when this throws.","triggerScenarios":"Calling `meter.release(amount, reason)` where `amount > meter.releasable()`; summing staged tranches to more than the configured `maxUsd`/`maxTokens` (e.g. initial 2 + three 1.5 releases against max 5); an `onBudgetExhausted` handler returning `{ release }` larger than what remains under `max`; releasing the full original amount again after earlier tranches were already granted.","commonSituations":"Configured `budget.initialUsd` plus checkpoint top-ups exceeding `maxUsd`; off-by-one or unit mistakes (tokens vs USD, whole vs fractional amounts) in a controller; hard-coded release amounts that ignore how much was already released; concurrent controller code paths each releasing without checking the remaining headroom.","solutions":["Clamp the release to the remaining headroom: `meter.release(Math.min(amount, meter.releasable()), reason);`","Check before releasing: `const head = meter.releasable(); if (amount <= head) meter.release(amount, reason);`","Raise `maxUsd`/`maxTokens` in `RunOptions.budget` if the staged plan genuinely needs more total budget.","Fix the controller's arithmetic so cumulative releases (initial tranche included) never exceed `max`."],"exampleFix":"// before\nmeter.release(2.0, \"checkpoint top-up\"); // max=5, 4.5 already released\n\n// after\nmeter.release(Math.min(2.0, meter.releasable()), \"checkpoint top-up\");","handlingStrategy":"validation","validationCode":"const headroom = meter.releasable();\nif (amount <= headroom) {\n  meter.release(amount, reason);\n} else {\n  meter.release(headroom, reason); // or skip entirely\n}","typeGuard":null,"tryCatchPattern":"try {\n  meter.release(amount, reason);\n} catch (e) {\n  if (e instanceof Error && e.message === \"cave_budget_release_exceeds_max\") {\n    const headroom = meter.releasable();\n    if (headroom > 0) meter.release(headroom, reason);\n  } else throw e;\n}","preventionTips":["Clamp every release to Math.min(requested, meter.releasable()).","Verify initialUsd/initialTokens plus all planned top-ups sum to at most maxUsd/maxTokens.","Track released tranches (meter.tranches) in controller logic instead of assuming amounts are still available.","Watch units: token-denominated ledgers need safe integers; USD uses fractional dollars."],"tags":["budget","limits","validation"],"backgroundTag":"value-out-of-range","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}