JuliusBrussee/caveman · error
cave_recovery_handle_out_of_scope
cave_recovery_handle_out_of_scope
Error message
cave_recovery_handle_out_of_scope
What it means
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.
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.
Example fix
// before
engineRetrieve("rcv-from-previous-run", query, engineBin); // out of scope
// after
const { handle } = await runEngineTransform(segment); // this run
engineRetrieve(handle, query, engineBin); Defensive patterns
Strategy: try-catch
Validate before calling
const handle = String(params.recovery_handle ?? "");
if (!handles.has(handle)) {
return { content: [{ type: "text", text: "Invalid recovery_handle. Use a handle emitted by this run's compaction output." }] };
} Type guard
function isScopedHandle(handle: string, handles: Set<string>): boolean { return handles.has(handle); } Try / catch
try {
const value = await engineRetrieve(input.recovery_handle, input.query, engineBin);
} catch (err) {
if (err instanceof Error && err.message === "cave_recovery_handle_out_of_scope") {
// return a tool-error message to the model explaining handles are single-run
} else throw err;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- engine_changed_bytes_without_recovery
- caveman-cloud MCP changed during interrupted setup…
- caveman-cloud MCP changed during interrupted removal…
- MCP config or ownership journal changed during interrupted…
- MCP transaction failed and safe recovery was blocked: …
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/3aad056e596d5cdd.
Report an issue: GitHub.
Appendix: source
Thrown at packages/agent/src/runtime.ts:3041
}
function recoveryTool(
handles: ReadonlySet<string>,
engineBin: string | undefined,
trace?: MutableTransformTrace[],
): AgentTool<TSchema> {
return {
name: "cave_retrieve",
label: "cave_retrieve",
description: "Recover exact content omitted by an active Caveman transform. Handles are scoped to this run.",
parameters: Type.Object({
recovery_handle: Type.String(),
query: Type.Optional(Type.String()),
}),
executionMode: "parallel",
async execute(_toolCallId, params) {
const input = params as { recovery_handle: string; query?: string };
if (!handles.has(input.recovery_handle)) throw new Error("cave_recovery_handle_out_of_scope");
const value = await engineRetrieve(input.recovery_handle, input.query, engineBin);
for (const item of trace ?? []) {
if (item.recoveryHandle === input.recovery_handle) item.recoveryUsed = true;
}
return {
content: [{ type: "text", text: new TextDecoder().decode(value) }],
details: { recovery: input.query ? "query" : "exact" },
};
},
};
}
function toolSchemaSearchTool(
handles: ReadonlySet<string>,
engineBin: string | undefined,
trace?: MutableTransformTrace[],
): AgentTool<TSchema> {
return {View on GitHub (pinned to 3ee70a1026)