JuliusBrussee/caveman · error

cave_sandbox_source_staging_root_required

cave_sandbox_source_staging_root_required

Error message

cave_sandbox_source_staging_root_required

What it means

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.

Solutions

  1. Fix the caller so `sourceFiles` contains at least one resolved path before calling `sandboxSourceReadFlags`
  2. Verify the staging/source collection glob or copy step actually produced files (check for silent empty results from the snapshot step)
  3. If empty input is legitimate, guard before the call: return [] early when `sourceFiles.length === 0`

Example fix

// before
const flags = sandboxSourceReadFlags(collectedSources, stagingRoot);
// after
const flags = collectedSources.length === 0 ? [] : sandboxSourceReadFlags(collectedSources, stagingRoot);
Defensive patterns

Strategy: validation

Validate before calling

if (sourceFiles.length === 0) throw new Error('no source files collected for sandbox read flags');
const flags = sandboxSourceReadFlags(sourceFiles, stagingRoot);

Type guard

function hasSources(files: readonly string[]): files is [string, ...string[]] { return files.length > 0; }

Try / catch

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; }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/d1c86549241711cd. Report an issue: GitHub.

Appendix: source

Thrown at packages/agent/src/runtime.ts:4887

    await rm(workspace, { recursive: true, force: true });
  }
}

/**
 * Above this many per-file `--allow-fs-read` flags, collapse the staged source
 * files to their common ancestor directory. A large project would
 * otherwise blow the OS argument limit (E2BIG) and the tool could not spawn at
 * all. The collapse is safe here: `sourceFiles` are paths inside the per-run
 * STAGED COPY, which already contains only the reachable source graph — never
 * the real project root with its .env and credentials.
 */
const SANDBOX_FS_READ_FLAG_THRESHOLD = 1024;

function commonAncestorDir(paths: readonly string[]): string {
  const dirs = paths.map((path) => resolve(dirname(path)));
  const first = dirs[0];
  if (first === undefined) {
    throw new Error("cave_sandbox_source_staging_root_required");
  }
  let ancestor = first;
  while (dirs.some((path) => escapesRoot(relative(ancestor, path)))) {
    const parent = dirname(ancestor);
    if (parent === ancestor) {
      throw new Error("cave_sandbox_source_read_root_refused");
    }
    ancestor = parent;
  }
  if (dirname(ancestor) === ancestor) {
    throw new Error("cave_sandbox_source_read_root_refused");
  }
  return ancestor;
}

export function sandboxSourceReadFlags(
  sourceFiles: readonly string[],
  stagingRoot?: string,

View on GitHub (pinned to 3ee70a1026)