JuliusBrussee/caveman · error · Error
cave_delegate_depth_exceeded
cave_delegate_depth_exceeded
Error message
cave_delegate_depth_exceeded
What it means
The delegate MCP server refuses to start a nested delegate worker when the environment variable CAVEMAN_DELEGATE_CHILD=1 is set, throwing 'cave_delegate_depth_exceeded'. Each worker sets this variable, so if the worker itself receives a cave_delegate tool call, it means delegation is recursing; the server caps it at one level to prevent infinite delegate loops.
Solutions
- Remove any cave_delegate calls from the delegated task text so the worker does not attempt to delegate again
- Clear CAVEMAN_DELEGATE_CHILD only if you genuinely intend a new top-level session (not inside a worker)
- Restructure the workflow so nested work is done directly by the worker instead of via delegation
- If the flag is set unexpectedly, inspect the parent process env for an exported CAVEMAN_DELEGATE_CHILD
Example fix
// before: task instructs recursion
runWorker("use cave_delegate to investigate ...")
// after: worker does the work directly
runWorker("investigate the failing test and report findings") Defensive patterns
Strategy: try-catch
Validate before calling
function canDelegate() {
return process.env.CAVEMAN_DELEGATE_CHILD !== '1';
}
if (!canDelegate()) throw new Error('already inside a delegated worker; do not call cave_delegate again'); Try / catch
try { await callTool('cave_delegate', { task }); } catch (e) {
if (e.message === 'cave_delegate_depth_exceeded') {
// we are a child worker: perform the work directly instead of delegating
return doWorkLocally(task);
} throw e;
} Prevention
- Strip cave_delegate instructions from task text passed to workers
- Check CAVEMAN_DELEGATE_CHILD before offering the tool in client UIs
- Do not export CAVEMAN_DELEGATE_CHILD in shells where you run top-level sessions
- Design tasks so the worker completes work itself rather than re-delegating
When it happens
Trigger: Invoking the cave_delegate tool from within a session that is already a cave_delegate child worker (CAVEMAN_DELEGATE_CHILD=1 in its environment), e.g. an agent task that itself calls cave_delegate.
Common situations: An agent prompt that tells the sub-agent it may delegate, recursive task definitions, or reusing a worker environment where the flag was incorrectly persisted/exported.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- MCP changes require the ownership transaction
- : MCP ownership journal failed; native config may already…
- caveman-cloud MCP changed after setup; refusing destructive…
- caveman-cloud MCP postflight mismatch
- caveman-cloud MCP registration failed exact postflight
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/b5ece32a4b43ceae.
Report an issue: GitHub.
Appendix: source
Thrown at agents/delegate/caveman-delegate-mcp.mjs:141
},
required: ["task"],
},
};
async function handle(msg) {
const { id, method, params } = msg;
if (method === "initialize") {
return {
protocolVersion: params?.protocolVersion || PROTOCOL_FALLBACK,
capabilities: { tools: { listChanged: false } },
serverInfo: { name: "caveman-delegate", version: "0.1.0" },
};
}
if (method === "tools/list") return { tools: [TOOL] };
if (method === "ping") return {};
if (method === "tools/call") {
if (params?.name !== TOOL.name) throw new Error(`unknown tool: ${params?.name}`);
if (process.env.CAVEMAN_DELEGATE_CHILD === "1") throw new Error("cave_delegate_depth_exceeded");
const task = params?.arguments?.task;
if (!task || typeof task !== "string") throw new Error("cave_delegate_missing_task");
const r = await runWorker(task, params?.arguments?.cwd);
const lines = [r.out || "(worker produced no output)"];
if (r.usage) {
const u = r.usage;
let usageLine = `delegate usage (measured, provider-reported): input ${u.input + u.cacheRead + u.cacheWrite} (cacheRead ${u.cacheRead}, cacheWrite ${u.cacheWrite}), output ${u.output}`;
if (u.cost > 0) usageLine += `, cost $${u.cost.toFixed(4)}`;
lines.push("---", usageLine);
}
if (r.code !== 0) {
lines.push("---", `worker exit ${r.code}: ${r.err.slice(-500)}`);
return { content: [{ type: "text", text: lines.join("\n") }], isError: true };
}
return { content: [{ type: "text", text: lines.join("\n") }] };
}
if (method?.startsWith("notifications/")) return undefined;
throw Object.assign(new Error(`method not found: ${method}`), { code: -32601 });View on GitHub (pinned to 3ee70a1026)