JuliusBrussee/caveman · error
cave_subagent_definition_cycle
cave_subagent_definition_cycle
Error message
cave_subagent_definition_cycle
What it means
Thrown by visit in validateAgentGraph when the same AgentDefinition object reference appears twice on the current active validation path — i.e. the subagent graph contains a cycle. Depth limiting alone would not catch cycles, so an explicit active-set detects them.
Solutions
- Break the cycle: remove the subagent tool that points back up the chain.
- Deep-clone definitions if intentional reuse across branches is desired (cycles are per-object-reference on one path; sibling reuse is fine).
- Refactor mutual composition into a flat structure where both agents are siblings under a common parent.
Example fix
// before agentA.tools.push(subagentTool(agentB)); agentB.tools.push(subagentTool(agentA)); // cycle // after agentA.tools.push(subagentTool(agentB)); // agentB no longer references agentA
Defensive patterns
Strategy: validation
Validate before calling
function hasCycle(def: AgentDefinition, active = new Set<AgentDefinition>()): boolean {
if (active.has(def)) return true;
active.add(def);
const cyc = (def.tools ?? []).some(t => t.runtime?.kind === "subagent" && hasCycle(t.runtime.definition, active));
active.delete(def);
return cyc;
}
if (hasCycle(root)) throw new Error("subagent graph contains a cycle"); Try / catch
try {
validateAgentGraph(root);
} catch (e) {
if (e instanceof Error && e.message === "cave_subagent_definition_cycle") {
console.error("Subagent graph is cyclic; remove the back-reference or deep-clone shared definitions");
} else throw e;
} Prevention
- Model agent composition as a DAG — never let a child reference an ancestor.
- Deep-clone definitions when reusing the same object across branches of one path.
- Run cycle detection on generated graphs before validation.
When it happens
Trigger: Defining agent A whose tools include a subagent tool referencing agent B, and B (directly or transitively) references A again via runtime.definition — the same object re-entering visit while still active.
Common situations: Programmatically generated definitions where a parent auto-wires its children and a child points back to the parent, config referencing shared agent objects that mutually compose, or accidental reuse of one definition object at multiple levels of one path.
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
- cave_agent_definition_invalid
- cave_subagent_depth_limit
- caveman agent: duplicate tool name
- caveman agent: invalid agent id
- caveman agent: tool prefix cave_ is reserved by framework
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/635044dc316e4828.
Report an issue: GitHub.
Appendix: source
Thrown at packages/agent/src/definition-graph.ts:23
);
export function validateAgentGraph(root: AgentDefinition): void {
// Memoize per inherited containment posture: the same child definition
// reached under a sandbox-required ancestor must be re-checked, not skipped.
const visited = [new Set<AgentDefinition>(), new Set<AgentDefinition>()];
const active = new Set<AgentDefinition>();
const visit = (
definition: AgentDefinition,
depth: number,
sandboxRequired: boolean,
): void => {
if (!definition || definition.kind !== "agent" ||
!Array.isArray(definition.tools)) {
throw new Error("cave_agent_definition_invalid");
}
if (depth > 8) throw new Error("cave_subagent_depth_limit");
if (active.has(definition)) throw new Error("cave_subagent_definition_cycle");
// Host mode is an opt-in the root makes for itself. A descendant cannot use
// it to run closures outside an ancestor's required containment.
if (sandboxRequired && definition.sandbox === "host") {
throw new Error("cave_host_sandbox_nested_under_required");
}
const memo = visited[sandboxRequired ? 1 : 0]!;
if (memo.has(definition)) return;
active.add(definition);
const childSandboxRequired = sandboxRequired ||
definition.sandbox === "required";
const names = new Set<string>();
for (const declared of definition.tools) {
if (!declared || declared.kind !== "tool" ||
typeof declared.name !== "string") {
throw new Error("cave_tool_definition_invalid");
}
if (names.has(declared.name)) throw new Error("cave_duplicate_tool_name");
names.add(declared.name);View on GitHub (pinned to 3ee70a1026)