{"record":{"id":"5d65ee48e2271c48","repo":"JuliusBrussee/caveman","slug":"cave-context-body-missing-id","errorCode":null,"errorMessage":"cave_context_body_missing:${id}","messagePattern":"cave_context_body_missing:(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":2588,"sourceCode":"  ];\n  for (const [slot, used] of slots) {\n    if (used > plan.budgets[slot]) throw new Error(`cave_${slot}_budget_exceeded`);\n  }\n}\n\nexport function assembleSystemPrompt(\n  definition: AgentDefinition,\n  lowered: LoweredContext,\n  bodies: ReadonlyMap<string, Uint8Array> = lowered.bodies,\n): string {\n  const decoder = new TextDecoder();\n  const required = [\"agent.instructions\", ...definition.contexts.map((item) => item.id)];\n  const parts: string[] = [];\n  for (const id of required) {\n    const segment = lowered.ir.segments.find((item) => item.id === id);\n    if (!segment) throw new Error(`cave_context_segment_missing:${id}`);\n    const body = bodies.get(segment.bodyHandle);\n    if (!body) throw new Error(`cave_context_body_missing:${id}`);\n    const text = decoder.decode(body);\n    parts.push(id === \"agent.instructions\" ? text : `<cave-context id=${JSON.stringify(id)}>\\n${text}\\n</cave-context>`);\n  }\n  if (definition.output) {\n    parts.push(`<cave-output max_tokens=${definition.output.maxTokens}>Return output matching declared schema when present.</cave-output>`);\n  }\n  if (definition.memory) {\n    parts.push(\"<cave-memory>Use cave_memory_search before relying on prior-session facts. Use cave_memory_remember only for durable facts the user intended to retain.</cave-memory>\");\n  }\n  return parts.join(\"\\n\\n\");\n}\n\nasync function applyEfficiencyPlan(\n  lowered: LoweredContext,\n  plan: CavePlan | undefined,\n  engineBin: string | undefined,\n  signal?: AbortSignal,\n): Promise<AppliedPlan> {","sourceCodeStart":2570,"sourceCodeEnd":2606,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/packages/agent/src/runtime.ts#L2570-L2606","documentation":"Thrown by assembleSystemPrompt when the segment exists in lowered.ir.segments but its bodyHandle has no entry in the bodies map. The segment metadata and the body bytes are carried separately; this error means metadata arrived without content.","triggerScenarios":"lowered.bodies is missing the Uint8Array for a segment's bodyHandle — e.g. bodies were filtered, serialized/deserialized losing binary payloads, or a custom bodies map was passed (the third parameter overrides lowered.bodies) without all handles.","commonSituations":"Passing a custom bodies map (e.g. a transformed or cached copy) that omits handles; cloning/serializing LoweredContext and dropping the bodies map; a lowering bug that registered a handle but never stored bytes.","solutions":["If passing the optional bodies parameter, ensure it contains every bodyHandle referenced by ir.segments","Re-lower the context to rebuild a consistent (segments, bodies) pair","Audit any post-lowering transformation that filters or replaces bodies"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"function allBodiesPresent(lowered: LoweredContext, bodies = lowered.bodies): boolean {\n  return lowered.ir.segments.every((s) => bodies.has(s.bodyHandle));\n}","typeGuard":"function isContextBodyMissing(e: unknown): string | null {\n  if (!(e instanceof Error)) return null;\n  const m = /^cave_context_body_missing:(.+)$/.exec(e.message);\n  return m ? m[1] : null;\n}","tryCatchPattern":"if (!allBodiesPresent(lowered)) {\n  lowered = await lowerContexts(definition);\n}\nawait assembleSystemPrompt(definition, lowered);","preventionTips":["When serializing LoweredContext, ship the bodies map alongside segments","If overriding the bodies parameter, diff handle sets first","Re-lower rather than patching partial contexts"],"tags":["context","lowering","bodies","configuration"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}