{"record":{"id":"3aad056e596d5cdd","repo":"JuliusBrussee/caveman","slug":"cave-recovery-handle-out-of-scope","errorCode":"cave_recovery_handle_out_of_scope","errorMessage":"cave_recovery_handle_out_of_scope","messagePattern":"cave_recovery_handle_out_of_scope","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":3041,"sourceCode":"}\n\nfunction recoveryTool(\n  handles: ReadonlySet<string>,\n  engineBin: string | undefined,\n  trace?: MutableTransformTrace[],\n): AgentTool<TSchema> {\n  return {\n    name: \"cave_retrieve\",\n    label: \"cave_retrieve\",\n    description: \"Recover exact content omitted by an active Caveman transform. Handles are scoped to this run.\",\n    parameters: Type.Object({\n      recovery_handle: Type.String(),\n      query: Type.Optional(Type.String()),\n    }),\n    executionMode: \"parallel\",\n    async execute(_toolCallId, params) {\n      const input = params as { recovery_handle: string; query?: string };\n      if (!handles.has(input.recovery_handle)) throw new Error(\"cave_recovery_handle_out_of_scope\");\n      const value = await engineRetrieve(input.recovery_handle, input.query, engineBin);\n      for (const item of trace ?? []) {\n        if (item.recoveryHandle === input.recovery_handle) item.recoveryUsed = true;\n      }\n      return {\n        content: [{ type: \"text\", text: new TextDecoder().decode(value) }],\n        details: { recovery: input.query ? \"query\" : \"exact\" },\n      };\n    },\n  };\n}\n\nfunction toolSchemaSearchTool(\n  handles: ReadonlySet<string>,\n  engineBin: string | undefined,\n  trace?: MutableTransformTrace[],\n): AgentTool<TSchema> {\n  return {","sourceCodeStart":3023,"sourceCodeEnd":3059,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L3023-L3059","documentation":"Thrown by the built-in recovery retrieval tool when the model supplies a recovery_handle that is not in the current run's registered handle set. Recovery handles are scoped to a single run/plan execution; a handle from a previous run or fabricated by the model is out of scope and rejected.","triggerScenarios":"The agent calls the recovery tool with a recovery_handle string that was never registered by this run's engine transforms — e.g. the model hallucinated an id, reused a handle from an earlier session, or the handle set was reset between calls.","commonSituations":"LLM inventing plausible-looking handles in multi-turn conversations; reusing transcripts/handles across runs; executing multiple plans concurrently where the tool is bound to the wrong run's handle set.","solutions":["Use a recovery_handle exactly as emitted by the current run's engine transform report.","Re-run the compaction/engine step if the original run's handles are gone — handles are not persistent.","Verify the tool's closure is bound to the correct run's handles set (not a stale or shared one).","Instruct the model (via tool description) that handles are single-run and must be copied verbatim from tool/engine output."],"exampleFix":"// before\nengineRetrieve(\"rcv-from-previous-run\", query, engineBin); // out of scope\n// after\nconst { handle } = await runEngineTransform(segment); // this run\nengineRetrieve(handle, query, engineBin);","handlingStrategy":"try-catch","validationCode":"const handle = String(params.recovery_handle ?? \"\");\nif (!handles.has(handle)) {\n  return { content: [{ type: \"text\", text: \"Invalid recovery_handle. Use a handle emitted by this run's compaction output.\" }] };\n}","typeGuard":"function isScopedHandle(handle: string, handles: Set<string>): boolean { return handles.has(handle); }","tryCatchPattern":"try {\n  const value = await engineRetrieve(input.recovery_handle, input.query, engineBin);\n} catch (err) {\n  if (err instanceof Error && err.message === \"cave_recovery_handle_out_of_scope\") {\n    // return a tool-error message to the model explaining handles are single-run\n  } else throw err;\n}","preventionTips":["Write a precise tool description stating handles are emitted per-run and must be copied verbatim.","Never persist or replay recovery handles across runs or sessions.","Bind the recovery tool's closure to exactly one run's handle set.","Return structured tool errors instead of throwing when the model supplies bad handles, so it can self-correct."],"tags":["recovery","handle","scope","agent-runtime","llm-tool"],"backgroundTag":"resource-not-found","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"}