Repository navigation
feat(python): expose filesystem and realfs mount APIs - #768
Conversation
There was a problem hiding this comment.
Pull request overview
This PR expands the bashkit Python bindings to expose BashKit’s virtual filesystem (byte-safe I/O + metadata) and the Rust “mount” configuration surface (including real host mounts via realfs), bringing Python closer to the Rust API capabilities.
Changes:
- Adds a Python
FileSystemclass and exposes it viaBash.fs()/BashTool.fs(), with byte-safe VFS operations (read/write/stat/etc). - Adds constructor mount options and “live” mount methods for
BashandBashTool, and enables the Rustrealfsfeature in the Python crate. - Updates Python package exports, type stubs, README docs, and adds initial pytest coverage for the new surfaces.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
crates/bashkit-python/src/lib.rs |
Implements FileSystem PyO3 class, exposes fs() handles, and adds mount configuration + methods on Bash/BashTool. |
crates/bashkit-python/tests/test_bashkit.py |
Adds pytest coverage for fs byte roundtrip, text mounts, and realfs readwrite mounts. |
crates/bashkit-python/bashkit/_bashkit.pyi |
Adds typing stubs for FileSystem, fs() accessors, and mount options/methods. |
crates/bashkit-python/bashkit/__init__.py |
Re-exports FileSystem from the extension module. |
crates/bashkit-python/README.md |
Documents direct filesystem access and real host mount options for Python users. |
crates/bashkit-python/Cargo.toml |
Enables bashkit’s realfs feature for the Python crate. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@h0rv can you pls reabse on top of latest main? There was an issue in CI. |
a29ceaa to
ed6854f
Compare
|
@h0rv this is a nice addition, would you be able to address comments from Copilot? Also, do you really need |
|
@chaliy Yes, working on addressing comments. And I will reconsider the |
|
@chaliy updated |
69920d5 to
4a0f6a1
Compare
4a0f6a1 to
68055f3
Compare
chaliy
left a comment
There was a problem hiding this comment.
Re-review: feat(python): expose filesystem and realfs mount APIs
Good work — the Python FS surface is well-shaped, tests are solid, and the FileSystemHandle::Live design correctly tracks interpreter resets. A few items to address before merge:
1. Mount config parsing is duplicated (~50 lines) — extract helper
PyBash::new() and BashTool::new() have near-identical mount config collection chains. Extract to a shared function:
fn parse_mount_configs(
mount_text: Option<Vec<(String, String)>>,
mount_readonly_text: Option<Vec<(String, String)>>,
mount_real_readonly: Option<Vec<String>>,
mount_real_readonly_at: Option<Vec<(String, String)>>,
mount_real_readwrite: Option<Vec<String>>,
mount_real_readwrite_at: Option<Vec<(String, String)>>,
) -> (Vec<MountedTextConfig>, Vec<RealMountConfig>) {
// ... shared logic
}This removes ~50 lines of duplication and a maintenance hazard.
2. realfs feature is now always-on for the Python crate
Cargo.toml unconditionally enables realfs. This means every Python wheel ships with host filesystem access capability. This is the intended design, but worth a one-line comment in the Cargo.toml explaining why (Python callers need mount_real_* APIs).
3. Per-operation mutex acquisition on FileSystemHandle::Live
Every FS method on a live handle locks the Bash mutex via inner.resolve().await. For bulk operations (e.g., reading 100 files), this means 100 mutex acquisitions. This is correct for consistency (the handle always sees latest state post-reset), but consider:
- Documenting this tradeoff in the
.pyidocstring forfs()(e.g., "Returns a live handle — each call resolves against the current interpreter") - Optionally adding a
snapshot()method that returns aStatichandle for batch reads without repeated locking
Not a blocker, but worth a doc note.
4. Missing error-case tests
The positive-path coverage is good. Consider adding:
- Write to a readonly text mount → expect error
unmount()a path that was never mounted → expect errorFileSystem.real()with a nonexistent host path → expect error- FS operations on paths with invalid characters
5. Minor: BashTool doesn't forward python/external_* to constructor
PyBash supports python, external_functions, external_handler kwargs. BashTool doesn't. Unrelated to this PR, but the mount params were added to both — should the Python/external params also be on BashTool? If intentionally omitted, a comment would help.
6. Close #782
Issue #782 (created just now) requests exactly what #784 already delivered (Rust-side Bash::mount/unmount with Arc<MountableFs>). It should be closed as already implemented, with this PR completing the Python surface.
Summary
The design is sound — MountableFs as outermost layer, live handle tracking, clean Python API. The main ask is deduplicating the mount config parsing. Rest are suggestions.
chaliy
left a comment
There was a problem hiding this comment.
LGTM — approving with the understanding that the items from the previous review (mount config dedup, error-case tests, live handle doc note) will be addressed in follow-ups.
Follow-up to #768: - Extract shared parse_mount_configs() helper used by both PyBash and BashTool - Add comment explaining why realfs feature is always-on - Document live handle semantics on fs() (per-op interpreter lock) - Add error-case tests: unmount nonexistent, write to readonly, bad host path, read/stat missing files
…everruns#788) ## Summary Follow-up to everruns#768 addressing review feedback: - **Extract `parse_mount_configs()` helper** — removes ~50 lines of duplicated mount config collection between `PyBash::new()` and `BashTool::new()` - **Document `realfs` always-on** — one-line comment in Cargo.toml explaining why - **Document live handle semantics** — `fs()` docstrings in both Rust and `.pyi` stubs note per-op interpreter lock behavior - **Add error-case tests** — unmount nonexistent path, write to readonly mount, `FileSystem.real()` with bad host path, read/stat missing files ## Test plan - [ ] `cargo clippy -p bashkit-python --all-targets -- -D warnings` passes - [ ] `cargo fmt --check -p bashkit-python` passes - [ ] `ruff check crates/bashkit-python && ruff format --check crates/bashkit-python` passes - [ ] `pytest crates/bashkit-python/tests/ -v` passes (requires `maturin develop`) - [ ] CI green
## Summary
- expose a Python `FileSystem` handle via `Bash.fs()` and
`BashTool.fs()`
- expose byte-safe VFS operations from Python (`read_file`,
`write_file`, `append_file`, `mkdir`, `remove`, `stat`, `read_dir`,
`exists`, `rename`, `copy`, `symlink`, `chmod`, `read_link`)
- expose the existing Rust mount APIs on the Python side for both
constructors and live instances:
- `mount_text`
- `mount_readonly_text`
- `mount_real_readonly`
- `mount_real_readonly_at`
- `mount_real_readwrite`
- `mount_real_readwrite_at`
- enable the `realfs` feature for the Python crate so these bindings
match the Rust surface
- update stubs, package exports, README, and tests
## Why
The Rust core already exposes byte-level filesystem access and real host
mounts, but the Python bindings only exposed command execution.
That made Python agent integrations much more text-oriented than the
core runtime and blocked workflows that need:
- binary-safe file IO
- direct VFS inspection/manipulation
- mounting a host workspace into the sandbox from Python
This PR closes that gap by exposing the corresponding filesystem and
mount APIs that already exist in Rust.
## API shape
### New filesystem handle
```python
from bashkit import Bash
bash = Bash()
fs = bash.fs()
fs.mkdir('/data', recursive=True)
fs.write_file('/data/blob.bin', b'\x00\x01hello')
assert fs.read_file('/data/blob.bin') == b'\x00\x01hello'
```
### Constructor mount options
```python
bash = Bash(
mount_text=[('/config/app.conf', 'debug=true\n')],
mount_real_readwrite_at=[('/host/workspace', '/workspace')],
)
```
### Live mount methods
```python
bash.mount_real_readwrite_at('/host/workspace', '/workspace')
```
## Validation
### Passed locally
- `cargo fmt --all --check`
- `cargo clippy --all-targets --all-features -- -D warnings`
- `RUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-features`
- `cargo test --features realfs -p bashkit --test realfs_tests -p
bashkit-cli`
- `cargo test --features failpoints --test security_failpoint_tests --
--test-threads=1`
- `PROPTEST_CASES=50 cargo test --test proptest_security --
--test-threads=1`
- `cargo +nightly fuzz build` (from `crates/bashkit/`)
- `uvx ruff check crates/bashkit-python`
- `uvx ruff format --check crates/bashkit-python`
- `mypy crates/bashkit-python/bashkit/ --ignore-missing-imports`
- `pytest crates/bashkit-python/tests/test_bashkit.py -q`
- Python examples: `bash_basics.py`, `k8s_orchestrator.py`
- Rust examples including `python_external_functions`,
`realfs_readonly`, `realfs_readwrite`
- Python wheel build + `twine check`
### Existing local failures on macOS that reproduce on current upstream
`main`
These appear unrelated to this change and reproduce in a fresh clone of
upstream:
- `cargo test --features http_client` fails in `bash_comparison_tests`
- `cargo vet --locked` reports unvetted `zerocopy` / `zerocopy-derive`
- Python security test
`tests/test_python_security.py::TestResourceLimits::test_fork_bomb_blocked`
segfaults under local macOS/Python 3.9
Given that, I focused local validation on the changed Python/Rust
surfaces plus the existing CI-relevant build/lint/test paths I could run
deterministically here.
…#788) ## Summary Follow-up to #768 addressing review feedback: - **Extract `parse_mount_configs()` helper** — removes ~50 lines of duplicated mount config collection between `PyBash::new()` and `BashTool::new()` - **Document `realfs` always-on** — one-line comment in Cargo.toml explaining why - **Document live handle semantics** — `fs()` docstrings in both Rust and `.pyi` stubs note per-op interpreter lock behavior - **Add error-case tests** — unmount nonexistent path, write to readonly mount, `FileSystem.real()` with bad host path, read/stat missing files ## Test plan - [ ] `cargo clippy -p bashkit-python --all-targets -- -D warnings` passes - [ ] `cargo fmt --check -p bashkit-python` passes - [ ] `ruff check crates/bashkit-python && ruff format --check crates/bashkit-python` passes - [ ] `pytest crates/bashkit-python/tests/ -v` passes (requires `maturin develop`) - [ ] CI green
Summary
FileSystemhandle viaBash.fs()andBashTool.fs()read_file,write_file,append_file,mkdir,remove,stat,read_dir,exists,rename,copy,symlink,chmod,read_link)mount_textmount_readonly_textmount_real_readonlymount_real_readonly_atmount_real_readwritemount_real_readwrite_atrealfsfeature for the Python crate so these bindings match the Rust surfaceWhy
The Rust core already exposes byte-level filesystem access and real host mounts, but the Python bindings only exposed command execution.
That made Python agent integrations much more text-oriented than the core runtime and blocked workflows that need:
This PR closes that gap by exposing the corresponding filesystem and mount APIs that already exist in Rust.
API shape
New filesystem handle
Constructor mount options
Live mount methods
Validation
Passed locally
cargo fmt --all --checkcargo clippy --all-targets --all-features -- -D warningsRUSTDOCFLAGS='-D warnings' cargo doc --no-deps --all-featurescargo test --features realfs -p bashkit --test realfs_tests -p bashkit-clicargo test --features failpoints --test security_failpoint_tests -- --test-threads=1PROPTEST_CASES=50 cargo test --test proptest_security -- --test-threads=1cargo +nightly fuzz build(fromcrates/bashkit/)uvx ruff check crates/bashkit-pythonuvx ruff format --check crates/bashkit-pythonmypy crates/bashkit-python/bashkit/ --ignore-missing-importspytest crates/bashkit-python/tests/test_bashkit.py -qbash_basics.py,k8s_orchestrator.pypython_external_functions,realfs_readonly,realfs_readwritetwine checkExisting local failures on macOS that reproduce on current upstream
mainThese appear unrelated to this change and reproduce in a fresh clone of upstream:
cargo test --features http_clientfails inbash_comparison_testscargo vet --lockedreports unvettedzerocopy/zerocopy-derivetests/test_python_security.py::TestResourceLimits::test_fork_bomb_blockedsegfaults under local macOS/Python 3.9Given that, I focused local validation on the changed Python/Rust surfaces plus the existing CI-relevant build/lint/test paths I could run deterministically here.