vercel/ai · error · Error

Failed to resolve sandbox default working directory: expecte

Error message

Failed to resolve sandbox default working directory: expected an absolute path, got ${JSON.stringify(cwd)}.

What it means

Thrown by resolveSandboxDefaultWorkingDirectory when `pwd` succeeds but its output is not a POSIX absolute path. The harness requires an absolute working directory to resolve relative paths for sandbox commands; a relative or empty cwd indicates a broken or non-standard sandbox environment. The JSON-stringified cwd is included to reveal whitespace or unexpected output.

Source

Thrown at packages/harness/src/utils/resolve-sandbox-default-working-directory.ts:28

  readonly abortSignal?: AbortSignal;
}): Promise<string> {
  if ('defaultWorkingDirectory' in sandboxSession) {
    return sandboxSession.defaultWorkingDirectory;
  }

  const result = await sandboxSession.run({
    command: 'pwd',
    abortSignal,
  });
  if (result.exitCode !== 0) {
    throw new Error(
      `Failed to resolve sandbox default working directory (exit ${result.exitCode}): ${result.stderr || result.stdout}`,
    );
  }

  const cwd = result.stdout.trim();
  if (!posix.isAbsolute(cwd)) {
    throw new Error(
      `Failed to resolve sandbox default working directory: expected an absolute path, got ${JSON.stringify(cwd)}.`,
    );
  }
  return cwd === '/' ? cwd : cwd.replace(/\/+$/, '');
}

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Fix the sandbox environment so `pwd` prints only a POSIX absolute path (remove profile scripts echoing to stdout).
  2. Implement `defaultWorkingDirectory` on the sandbox session to bypass the `pwd` probe.
  3. Use a Linux/POSIX sandbox image so cwd is an absolute POSIX path.
  4. Check `pwd` output manually inside the sandbox to see what is actually returned.

Example fix

// before
// ~/.bashrc prints: echo "Welcome to sandbox"
const cwd = await resolveSandboxDefaultWorkingDirectory({ sandboxSession }); // 'expected an absolute path, got "Welcome to sandbox"'
// after
// remove echo from ~/.bashrc, or:
const session = { ...sandboxSession, defaultWorkingDirectory: '/root' };
Defensive patterns

Strategy: validation

Validate before calling

const probe = await sandboxSession.run({ command: 'pwd' });
const cwd = probe.stdout.trim();
if (probe.exitCode !== 0 || !require('node:path').posix.isAbsolute(cwd)) {
  throw new Error(`Sandbox pwd output is not absolute: ${JSON.stringify(cwd)}`);
}

Try / catch

try {
  const cwd = await resolveSandboxDefaultWorkingDirectory({ sandboxSession });
} catch (error) {
  if (/expected an absolute path/.test((error as Error).message)) {
    // fall back to a known-good directory
    return '/root';
  }
  throw error;
}

Prevention

When it happens

Trigger: `sandboxSession.run({ command: 'pwd' })` returns exitCode 0 but `result.stdout.trim()` is empty, relative (e.g. `home/user`), a Windows-style path, or polluted with extra output (e.g. a shell printing a banner before `pwd`).

Common situations: Custom sandbox `run` implementations that mix command output with log/banner text; sandboxes on non-POSIX filesystems; misconfigured shell profiles echoing to stdout; Windows-based sandbox runtimes.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/418bcad6baf44d69. Report an issue: GitHub.