{"record":{"id":"50b217e204af87cd","repo":"JuliusBrussee/caveman","slug":"caveman-agent-run-ended-without-terminal-evidence","errorCode":null,"errorMessage":"caveman agent: run ended without terminal evidence","messagePattern":"caveman agent: run ended without terminal evidence","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/agent/src/runtime.ts","lineNumber":1001,"sourceCode":"  options: InternalRunOptions,\n  executionContext: InternalExecutionContext,\n): Promise<RunResult> {\n  let final: RunResult | undefined;\n  for await (const event of streamAgentWithOptions(\n    definition,\n    input,\n    options,\n    executionContext,\n  )) {\n    if (event.type === \"run_end\") final = event.result;\n    if (event.type === \"run_error\") {\n      // The ledger is not lost on the throwing path either: the partial receipt\n      // rides on a typed `cause` so a caller that only awaits the promise can\n      // still read what was spent before the failure.\n      throw new CavemanRunError(event.code, event.message, event.receipt);\n    }\n  }\n  if (!final) throw new Error(\"caveman agent: run ended without terminal evidence\");\n  return final;\n}\n\nexport function streamAgent(\n  definition: AgentDefinition,\n  input: string,\n  options: RunOptions = {},\n): AsyncGenerator<CavemanRunEvent> {\n  rejectInternalRunOptions(options);\n  return streamAgentWithOptions(\n    definition,\n    input,\n    options,\n    rootExecutionContext(definition, options.maxCostUsd, options),\n  );\n}\n\nfunction streamAgentWithOptions(","sourceCodeStart":983,"sourceCodeEnd":1019,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L983-L1019","documentation":"runAgentWithOptions drives the event stream from streamAgentInternal and reduces it to a single final RunResult. If the generator completes without emitting a terminal event (result, error, or budget stop), this invariant throw fires: the run ended with no terminal evidence, so there is no result to return. It is an internal consistency guard — a well-formed run always emits exactly one terminal event.","triggerScenarios":"The internal event generator (streamAgentInternal) returns/completes without yielding a terminal CavemanRunEvent — e.g. an upstream abort or generator short-circuit path that exits the loop before any `final`-setting event is yielded.","commonSituations":"Runtime bugs or a transport aborting the async generator mid-run; calls via runAgent/runAgentInternal/runSubagent where the underlying stream closes cleanly but never terminated the run logically. End users should never see this directly; it usually indicates an interrupted or buggy execution path.","solutions":["Retry the run; a transient abort of the event generator is the usual cause.","Check that no caller-supplied transport (custom streamFn/fetch) can silently close the stream without yielding a terminal event.","If reproducible, report a bug with the run's receipt/event log — this is an internal invariant violation (internal-invariant-violation family).","Upgrade the package; terminal-event emission guarantees have been hardened in later versions."],"exampleFix":"// before\ntry {\n  const result = await runAgent(def, input);\n} catch (e) {\n  // run ended without terminal evidence\n}\n// after\ntry {\n  const result = await runAgent(def, input);\n} catch (e) {\n  if (e instanceof CavemanRunError) {\n    // typed failure, e.receipt holds partial spend\n  } else {\n    // transient/invariant failure: retry with backoff\n    const result = await runAgent(def, input);\n  }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const result = await runAgent(def, input);\n} catch (err) {\n  if (err instanceof CavemanRunError) {\n    // typed failure: read err.receipt for partial spend\n  } else if (err.message.includes(\"terminal evidence\")) {\n    // invariant/transient: log and retry with backoff\n  } else {\n    throw err;\n  }\n}","preventionTips":["Use streamAgent directly if you need full event-stream visibility so a missing terminal event is observable.","Avoid custom transports that can close the event generator without a terminal event.","Keep the package updated; invariant violations warrant bug reports with event logs."],"tags":["agent-runtime","internal-invariant","async-generator"],"backgroundTag":"internal-invariant-violation","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"}