{"record":{"id":"beb1304df299df8c","repo":"JuliusBrussee/caveman","slug":"cave-budget-escalation-result-invalid","errorCode":"cave_budget_escalation_result_invalid","errorMessage":"cave_budget_escalation_result_invalid","messagePattern":"cave_budget_escalation_result_invalid","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":1632,"sourceCode":"        restorableBytes: restorableRequestBytes(\n          conversationOriginals,\n          instructions,\n          originalInstructions,\n        ),\n      });\n      let decided: NextCallDecision;\n      try {\n        decided = plan();\n        // Escalation gets exactly one attempt per exhaustion: the handler either\n        // funds the next call or it does not, and a handler that keeps releasing\n        // slivers must not turn one stop into an unbounded loop.\n        if (decided.action === \"stop\" && decided.reason === \"budget_exhausted\" &&\n            budgetMeter !== undefined && typeof options.onBudgetExhausted === \"function\") {\n          const outcome = await options.onBudgetExhausted(budgetExhaustionContext(budgetMeter));\n          if (outcome !== \"stop\") {\n            if (!isRecord(outcome) || typeof outcome.release !== \"number\" ||\n                typeof outcome.reason !== \"string\") {\n              throw new Error(\"cave_budget_escalation_result_invalid\");\n            }\n            budgetMeter.release(outcome.release, outcome.reason);\n            decided = plan();\n          }\n        }\n      } catch (error) {\n        // Pi answers a thrown streamFn with a synthesized terminal error turn,\n        // which would otherwise replace this cause with a generic provider\n        // failure. Record it first so the run reports what actually went wrong.\n        ladderFailure ??= error instanceof Error ? error : new Error(String(error));\n        throw ladderFailure;\n      }\n      if (decided.action === \"stop\") {\n        // Pi answers a thrown streamFn with a synthesized zero-usage error turn,\n        // so record the refusal before throwing or the stop becomes the usage\n        // failure that turn produces.\n        stopReason = decided.reason;\n        refusalPending = true;","sourceCodeStart":1614,"sourceCodeEnd":1650,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L1614-L1650","documentation":"When the budget ladder decides to stop for budget_exhausted, the library calls options.onBudgetExhausted and expects either \"stop\" or an object { release: number, reason: string }. Any other return shape is rejected with this error, since an invalid escalation result cannot be applied to the budget meter.","triggerScenarios":"Providing an onBudgetExhausted callback that returns something other than \"stop\" or a well-formed release object — e.g. returning undefined, true, or { release: \"5\" } with a non-number release.","commonSituations":"Async callback whose return value is wrapped in a Promise resolved to undefined, a callback refactored to log without returning, or returning a release amount as a string from config.","solutions":["Return exactly \"stop\" or { release: <number>, reason: <string> } from onBudgetExhausted","Await the callback and return its value instead of letting it fall through to undefined","Add a return-type annotation/union so TypeScript enforces the allowed outcomes"],"exampleFix":"// before\nonBudgetExhausted: async (ctx) => { log(ctx); }\n// after\nonBudgetExhausted: async (ctx) => ({ release: 2.0, reason: \"approved top-up\" })","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isEscalationOutcome(v: unknown): v is \"stop\" | { release: number; reason: string } {\n  return v === \"stop\" || (typeof v === \"object\" && v !== null && typeof (v as any).release === \"number\" && typeof (v as any).reason === \"string\");\n}","tryCatchPattern":"try { await streamAgent(opts); } catch (e) { if (e.message === \"cave_budget_escalation_result_invalid\") { /* fix onBudgetExhausted return value */ } else throw e; }","preventionTips":["Always return \"stop\" or { release, reason } — never leave the callback without a return","Type the callback as (ctx) => \"stop\" | { release: number; reason: string }","Avoid wrapping the return value in extra async transforms that drop it"],"tags":["callback-contract","budget-escalation","validation"],"backgroundTag":"invalid-argument-value","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"}