{"record":{"id":"5349a2655435a92c","repo":"vercel/ai","slug":"failed-to-resolve-sandbox-default-working-director","errorCode":null,"errorMessage":"Failed to resolve sandbox default working directory (exit ${result.exitCode}): ${result.stderr || result.stdout}","messagePattern":"Failed to resolve sandbox default working directory \\(exit (.+?)\\): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/harness/src/utils/resolve-sandbox-default-working-directory.ts","lineNumber":21,"sourceCode":"import type { HarnessV1NetworkSandboxSession } from '../v1';\n\nexport async function resolveSandboxDefaultWorkingDirectory({\n  sandboxSession,\n  abortSignal,\n}: {\n  readonly sandboxSession: HarnessV1NetworkSandboxSession | SandboxSession;\n  readonly abortSignal?: AbortSignal;\n}): Promise<string> {\n  if ('defaultWorkingDirectory' in sandboxSession) {\n    return sandboxSession.defaultWorkingDirectory;\n  }\n\n  const result = await sandboxSession.run({\n    command: 'pwd',\n    abortSignal,\n  });\n  if (result.exitCode !== 0) {\n    throw new Error(\n      `Failed to resolve sandbox default working directory (exit ${result.exitCode}): ${result.stderr || result.stdout}`,\n    );\n  }\n\n  const cwd = result.stdout.trim();\n  if (!posix.isAbsolute(cwd)) {\n    throw new Error(\n      `Failed to resolve sandbox default working directory: expected an absolute path, got ${JSON.stringify(cwd)}.`,\n    );\n  }\n  return cwd === '/' ? cwd : cwd.replace(/\\/+$/, '');\n}\n","sourceCodeStart":3,"sourceCodeEnd":34,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/harness/src/utils/resolve-sandbox-default-working-directory.ts#L3-L34","documentation":"Thrown by resolveSandboxDefaultWorkingDirectory in packages/harness/src/utils/resolve-sandbox-default-working-directory.ts when the `pwd` command executed inside the sandbox session exits non-zero. The harness needs the sandbox's default working directory before running commands, and this failure means the sandbox shell could not even report its cwd. The stderr/stdout of the failed command is embedded in the message to help diagnose the sandbox-side problem.","triggerScenarios":"Calling prepareSandboxForHarness (or any code path reaching resolveSandboxDefaultWorkingDirectory) with a sandbox session that does not expose a `defaultWorkingDirectory` property, where `sandboxSession.run({ command: 'pwd' })` returns exitCode !== 0 — e.g. the sandbox shell fails to start, `pwd` is unavailable in a minimal image, or the session is already terminated.","commonSituations":"Custom sandbox implementations whose `run` fails on shell startup (bad image, missing shell); sandboxes where the working directory was deleted; sessions aborted mid-run; minimal containers lacking coreutils.","solutions":["Inspect the stderr/stdout embedded in the error to see why `pwd` failed inside the sandbox.","Verify the sandbox image/VM is healthy and has a POSIX shell with `pwd` available.","Implement `defaultWorkingDirectory` on your sandbox session so the harness can skip the `pwd` probe entirely.","Check that the sandbox session is running/not aborted before calling harness setup."],"exampleFix":"// before\nconst session = createCustomSandbox({ image: 'scratch' }); // no shell\nconst cwd = await resolveSandboxDefaultWorkingDirectory({ sandboxSession: session });\n// after\nconst session = createCustomSandbox({ image: 'debian:slim', defaultWorkingDirectory: '/workspace' });\nconst cwd = await resolveSandboxDefaultWorkingDirectory({ sandboxSession: session });","handlingStrategy":"try-catch","validationCode":"// Pre-check: does the sandbox expose a working directory?\nif (!('defaultWorkingDirectory' in sandboxSession)) {\n  const probe = await sandboxSession.run({ command: 'command -v pwd && pwd' });\n  if (probe.exitCode !== 0) throw new Error('Sandbox cannot run pwd: ' + probe.stderr);\n}","typeGuard":"function hasDefaultWorkingDirectory(s: object): s is { defaultWorkingDirectory: string } {\n  return 'defaultWorkingDirectory' in s && typeof (s as any).defaultWorkingDirectory === 'string';\n}","tryCatchPattern":"try {\n  const cwd = await resolveSandboxDefaultWorkingDirectory({ sandboxSession });\n} catch (error) {\n  // message includes exit code + stderr; inspect, then fail fast or recreate sandbox\n  throw new Error(`Sandbox unusable, recreate the session: ${(error as Error).message}`);\n}","preventionTips":["Provide `defaultWorkingDirectory` on your sandbox session to skip the pwd probe.","Use a sandbox image with a POSIX shell and coreutils.","Verify the session is running before harness setup.","Smoke-test the sandbox with a trivial `run({ command: 'true' })` first."],"tags":["sandbox","shell","harness","command-failed"],"backgroundTag":"sandbox-command-exit-nonzero","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}