Add SandboxFileSystem.create_file

RI
4mo ago
+274-0

Summary

Fixes #1520.

Adds SandboxFileSystem.create_file() for creating sandbox files directly from in-memory text or bytes. This uses the existing sandbox upload RPC instead of shelling out in the sandbox.

The helper keeps writes explicit and safe:

  • normalizes POSIX sandbox paths
  • accepts str and bytes contents
  • supports file modes
  • optionally creates missing parent directories
  • refuses to overwrite existing files unless overwrite=True
  • exposes the same helper through instance.fs.aio.create_file()

Checks

  • sdk/.venv/bin/python -m pytest sdk/tests/test_sandbox_filesystem.py -q
  • sdk/.venv/bin/python -m ruff check sdk/src/beta9/abstractions/sandbox.py sdk/tests/test_sandbox_filesystem.py
  • python3 -m compileall -q sdk/src/beta9/abstractions/sandbox.py sdk/tests/test_sandbox_filesystem.py
  • git diff --check

Note: I initially tried uv run with the default interpreter, but local uv selected Python 3.14 and pydantic-core in this repo does not build against that PyO3 version. I reran the checks with Python 3.11, which is inside the package support range.


Summary by cubic

Add SandboxFileSystem.create_file() to write files in the sandbox from in-memory text or bytes using the upload RPC. Also adds fs.aio.create_file() and avoids shelling out for simple writes.

  • New Features
    • New SandboxFileSystem.create_file() (and fs.aio.create_file()) creates files from str/bytes with optional encoding and file mode.
    • Safer writes: normalizes POSIX paths, can create missing parents (parents=True), and refuses to overwrite unless overwrite=True.
    • Clear errors: FileExistsError for existing files without overwrite; SandboxFileSystemError for RPC failures; validates paths and contents.
    • Tests added for strings/bytes, parent creation order, overwrite rules, and async delegation.