{"record":{"id":"d1c86549241711cd","repo":"JuliusBrussee/caveman","slug":"cave-sandbox-source-staging-root-required","errorCode":"cave_sandbox_source_staging_root_required","errorMessage":"cave_sandbox_source_staging_root_required","messagePattern":"cave_sandbox_source_staging_root_required","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":4887,"sourceCode":"    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)));\n  const first = dirs[0];\n  if (first === undefined) {\n    throw new Error(\"cave_sandbox_source_staging_root_required\");\n  }\n  let ancestor = first;\n  while (dirs.some((path) => escapesRoot(relative(ancestor, path)))) {\n    const parent = dirname(ancestor);\n    if (parent === ancestor) {\n      throw new Error(\"cave_sandbox_source_read_root_refused\");\n    }\n    ancestor = parent;\n  }\n  if (dirname(ancestor) === ancestor) {\n    throw new Error(\"cave_sandbox_source_read_root_refused\");\n  }\n  return ancestor;\n}\n\nexport function sandboxSourceReadFlags(\n  sourceFiles: readonly string[],\n  stagingRoot?: string,","sourceCodeStart":4869,"sourceCodeEnd":4905,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L4869-L4905","documentation":"This error means the sandbox source-read flag builder had no files to compute a common ancestor from. When more than 1024 source files need `--allow-fs-read` grants, the runtime collapses them into a single grant on their common ancestor directory; with an empty input list there is no first path to start from, so it throws. It exists to fail closed instead of granting a bogus or overly broad filesystem read permission.","triggerScenarios":"Calling `sandboxSourceReadFlags(sourceFiles, stagingRoot)` with an empty `sourceFiles` array AND the list large enough to need ancestor consolidation — i.e. `commonAncestorDir([])` is reached, which happens when the caller passes an empty array while also providing a stagingRoot path that takes the >1024-file branch (or a test calls `commonAncestorDir` directly with no paths).","commonSituations":"A staged source graph collected into an empty array (wrong glob, staging copy that matched nothing), a refactor that renamed the source-collection function so it now returns [], or unit tests calling `commonAncestorDir` with an empty list.","solutions":["Fix the caller so `sourceFiles` contains at least one resolved path before calling `sandboxSourceReadFlags`","Verify the staging/source collection glob or copy step actually produced files (check for silent empty results from the snapshot step)","If empty input is legitimate, guard before the call: return [] early when `sourceFiles.length === 0`"],"exampleFix":"// before\nconst flags = sandboxSourceReadFlags(collectedSources, stagingRoot);\n// after\nconst flags = collectedSources.length === 0 ? [] : sandboxSourceReadFlags(collectedSources, stagingRoot);","handlingStrategy":"validation","validationCode":"if (sourceFiles.length === 0) throw new Error('no source files collected for sandbox read flags');\nconst flags = sandboxSourceReadFlags(sourceFiles, stagingRoot);","typeGuard":"function hasSources(files: readonly string[]): files is [string, ...string[]] { return files.length > 0; }","tryCatchPattern":"try { const flags = sandboxSourceReadFlags(files, stagingRoot); } catch (e) { if (String((e as Error).message).includes('cave_sandbox_source_staging_root_required')) { /* empty source list — fix collection */ } else throw e; }","preventionTips":["Always assert the staged source list is non-empty before computing sandbox flags","Fail loudly in the staging/copy step when it produces zero files","Add a unit test for the empty-input path"],"tags":["sandbox","filesystem","internal-invariant"],"backgroundTag":"empty-required-field","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"}