User and Workdir

Updated at:

The user and working directory inside a template affect command execution, file permissions, and dependency installation paths. In agent scenarios, keep the working directory stable so commands do not rely on implicit paths.

Check the current user and directory

const result = await sandbox.commands.run("whoami && pwd");
console.log(result.stdout);

Python example:

result = sandbox.commands.run("whoami && pwd")
print(result.stdout)

Set the command working directory

await sandbox.files.makeDir("/tmp/project");
await sandbox.files.write("/tmp/project/main.py", "print('hello')\n");

const result = await sandbox.commands.run("python3 main.py", {
  cwd: "/tmp/project",
});

console.log(result.stdout.trim());

Python example:

sandbox.files.make_dir("/tmp/project")
sandbox.files.write("/tmp/project/main.py", "print('hello')\n")

result = sandbox.commands.run(
    "python3 main.py",
    cwd="/tmp/project",
)

print(result.stdout.strip())

Recommendations

  • Pin common tools and dependencies during template build.

  • Use an explicit cwd when running tasks.

  • Do not assume every image has the same default user, HOME directory, or permissions.