{"record":{"id":"a9e00a5356cf0e53","repo":"JuliusBrussee/caveman","slug":"cave-untrusted-tool-definition","errorCode":"cave_untrusted_tool_definition","errorMessage":"cave_untrusted_tool_definition:${declared.name}","messagePattern":"cave_untrusted_tool_definition:(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/definition-graph.ts","lineNumber":46,"sourceCode":"    }\n    const memo = visited[sandboxRequired ? 1 : 0]!;\n    if (memo.has(definition)) return;\n    active.add(definition);\n    const childSandboxRequired = sandboxRequired ||\n      definition.sandbox === \"required\";\n    const names = new Set<string>();\n    for (const declared of definition.tools) {\n      if (!declared || declared.kind !== \"tool\" ||\n          typeof declared.name !== \"string\") {\n        throw new Error(\"cave_tool_definition_invalid\");\n      }\n      if (names.has(declared.name)) throw new Error(\"cave_duplicate_tool_name\");\n      names.add(declared.name);\n      if (declared.name.startsWith(\"cave_\")) {\n        throw new Error(`cave_reserved_tool_name:${declared.name}`);\n      }\n      if (typeof Reflect.get(declared, TOOL_IMPLEMENTATION_SOURCE) !== \"string\") {\n        throw new Error(`cave_untrusted_tool_definition:${declared.name}`);\n      }\n      if (declared.runtime?.kind !== \"subagent\") continue;\n      const child = declared.runtime.definition as AgentDefinition;\n      visit(child, depth + 1, childSandboxRequired);\n    }\n    active.delete(definition);\n    memo.add(definition);\n  };\n\n  visit(root, 0, false);\n}\n\n/**\n * True when any agent in the graph opts into host mode.\n *\n * Lock eligibility is a property of the whole graph, not of its root: a host\n * subagent runs its tool closures in the host process just as a host root does,\n * so its evidence shows no containment either. Follows the same subagent edges","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/definition-graph.ts#L28-L64","documentation":"Thrown by visit in validateAgentGraph when a tool declaration lacks a trusted implementation source: Reflect.get(declared, TOOL_IMPLEMENTATION_SOURCE) is not a string. Tool implementations must be registered through the library's tool factory, which stamps a private symbol proving the implementation came from trusted code; arbitrary object literals claiming to be tools are rejected.","triggerScenarios":"Hand-constructing a plain object { kind: \"tool\", name: \"x\", ... } without the TOOL_IMPLEMENTATION_SOURCE symbol, deserializing tools from JSON (symbols cannot survive serialization), or copying a tool with a spread that drops symbol-keyed properties.","commonSituations":"Loading tool definitions from config/DB, sharing tools across process boundaries, or using object spread/Object.assign to build modified copies of tools — all of which lose the private symbol.","solutions":["Create tools with the library's official tool factory/helper so the trusted-source symbol is attached.","Re-wrap copied tools through the factory instead of spreading plain objects.","Do not serialize/deserialize tool definitions across boundaries; pass the live factory-created objects.","If a custom implementation wrapper is required, use the library's extension point to register a trusted source string."],"exampleFix":"// before\nconst tool = { kind: \"tool\", name: \"search\", run: async () => /* ... */ };\n// after\nconst tool = defineTool({ name: \"search\", run: async () => /* ... */ }); // factory stamps TOOL_IMPLEMENTATION_SOURCE","handlingStrategy":"validation","validationCode":"import { TOOL_IMPLEMENTATION_SOURCE } from \"@caveman/agent\";\nfunction isTrustedTool(t: ToolDefinition): boolean {\n  return typeof Reflect.get(t, TOOL_IMPLEMENTATION_SOURCE) === \"string\";\n}\ndefinition.tools.forEach(t => { if (!isTrustedTool(t)) throw new Error(`tool ${t.name} lacks a trusted implementation source`); });","typeGuard":"function isTrustedToolDefinition(v: unknown): v is ToolDefinition {\n  return typeof v === \"object\" && v !== null &&\n    typeof Reflect.get(v, TOOL_IMPLEMENTATION_SOURCE) === \"string\";\n}","tryCatchPattern":"try {\n  validateAgentGraph(def);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith(\"cave_untrusted_tool_definition\")) {\n    console.error(`Tool was not created by the library factory: ${e.message}; rebuild it with defineTool()`);\n  } else throw e;\n}","preventionTips":["Always create tools with the library's tool factory so the trust symbol is stamped.","Never build tool objects by hand or restore them from JSON — symbols do not survive serialization.","Avoid spread/Object.assign copies of tool definitions; they drop symbol-keyed metadata.","Pass live factory-created tool objects across modules instead of serialized copies."],"tags":["security","tool-definition","trust"],"backgroundTag":"untrusted-tool-definition","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"}