{"record":{"id":"8c582e4f9cdffcf7","repo":"JuliusBrussee/caveman","slug":"cave-sandbox-tool-failed","errorCode":"cave_sandbox_tool_failed","errorMessage":"cave_sandbox_tool_failed","messagePattern":"cave_sandbox_tool_failed","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":4866,"sourceCode":"          reject(new Error(`cave_sandbox_failed:${redactSandboxError(Buffer.concat(stderr).toString(\"utf8\"))}`));\n          return;\n        }\n        reject(new Error(\"cave_sandbox_invalid_output\"));\n      });\n      combined.addEventListener(\"abort\", onAbort, { once: true });\n      if (combined.aborted) onAbort();\n      child.stdin.end(JSON.stringify({\n        entry: pathToFileURL(entryPath).href,\n        agentPath: executionContext.agentPath,\n        rootDefinitionSha256: executionContext.rootDefinitionSha256,\n        toolDefinitionSha256,\n        tool: toolName,\n        params,\n        allowSideEffects,\n        allowNetwork: profile?.network === true,\n      }));\n    });\n    if (!result.ok) throw new Error(result.code ?? \"cave_sandbox_tool_failed\");\n    return result.value;\n  } finally {\n    await rm(workspace, { recursive: true, force: true });\n  }\n}\n\n/**\n * Above this many per-file `--allow-fs-read` flags, collapse the staged source\n * files to their common ancestor directory. A large project would\n * otherwise blow the OS argument limit (E2BIG) and the tool could not spawn at\n * all. The collapse is safe here: `sourceFiles` are paths inside the per-run\n * STAGED COPY, which already contains only the reachable source graph — never\n * the real project root with its .env and credentials.\n */\nconst SANDBOX_FS_READ_FLAG_THRESHOLD = 1024;\n\nfunction commonAncestorDir(paths: readonly string[]): string {\n  const dirs = paths.map((path) => resolve(dirname(path)));","sourceCodeStart":4848,"sourceCodeEnd":4884,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L4848-L4884","documentation":"Sandboxed tool execution returns a result envelope; if the execution reports not-ok without a more specific code, the runtime throws cave_sandbox_tool_failed as the generic failure signal. It means the tool itself (or its sandbox harness) failed inside the temporary workspace — the workspace is then always cleaned up in the finally block.","triggerScenarios":"The sandbox result envelope has ok === false and result.code is undefined/empty when running a tool via the sandbox runner (runtime.ts:4866) — e.g. the tool process crashed without setting an error code, or the harness failed before assigning a specific code.","commonSituations":"Tool binary/script crashing (segfault, unhandled exception) without emitting a structured error code; timeout or OOM kill inside the sandbox leaving no code; harness-level failure (workspace staging issue) before the tool's own error mapping ran.","solutions":["Inspect the tool's own logs/output to find the underlying crash cause, since this generic code carries no detail.","Fix the tool so it exits with a structured error code (result.code) for its failure modes.","Retry the tool invocation if the cause was transient (OOM/timeout), possibly with smaller inputs.","Verify tool params and allowSideEffects/allowNetwork settings match what the tool expects before re-running."],"exampleFix":"// before: tool crashes with bare exit\ntoolMain();\n// after: emit a structured code the sandbox can surface\ntry {\n  await toolMain();\n} catch (e) {\n  process.exitCode = 1;\n  emitSandboxCode('cave_tool_invalid_params', String(e));\n}","handlingStrategy":"try-catch","validationCode":"if (!toolCommandHealthy(tool)) throw new Error('tool binary fails smoke test; fix before sandboxing');","typeGuard":"const sandboxResultOk = (r) => r !== undefined && r.ok === true && r.value !== undefined;","tryCatchPattern":"try {\n  return await runSandboxedTool(tool, params);\n} catch (e) {\n  if (e.message === 'cave_sandbox_tool_failed') {\n    logToolCrash(tool.name, params);\n    return null; // or retry with backoff\n  }\n  throw e;\n}","preventionTips":["Smoke-test tools outside the sandbox first.","Have tools emit structured error codes instead of crashing silently.","Watch for transient OOM/timeouts and add bounded retries."],"tags":["sandbox","tool-execution","crash","fail-closed"],"backgroundTag":"http-error-response","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"}