Add SandboxFileSystem.create_file
RI
4mo ago+274+274+012345678901234567890123456789-0-0-0123456789
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
strandbytescontents - 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 -qsdk/.venv/bin/python -m ruff check sdk/src/beta9/abstractions/sandbox.py sdk/tests/test_sandbox_filesystem.pypython3 -m compileall -q sdk/src/beta9/abstractions/sandbox.py sdk/tests/test_sandbox_filesystem.pygit 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()(andfs.aio.create_file()) creates files fromstr/byteswith optional encoding and filemode. - Safer writes: normalizes POSIX paths, can create missing parents (
parents=True), and refuses to overwrite unlessoverwrite=True. - Clear errors:
FileExistsErrorfor existing files without overwrite;SandboxFileSystemErrorfor RPC failures; validates paths and contents. - Tests added for strings/bytes, parent creation order, overwrite rules, and async delegation.
- New