Skip to content

feat(python): expose filesystem and realfs mount APIs - #768

Merged
chaliy merged 1 commit into
everruns:mainfrom
h0rv:robby/python-fs-exposure
Mar 23, 2026
Merged

chaliy merged 1 commit into
everruns:mainfrom
h0rv:robby/python-fs-exposure

Conversation

@h0rv

@h0rv h0rv commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

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

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

bash = Bash(
    mount_text=[('/config/app.conf', 'debug=true\n')],
    mount_real_readwrite_at=[('/host/workspace', '/workspace')],
)

Live mount methods

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 FileSystem class and exposes it via Bash.fs() / BashTool.fs(), with byte-safe VFS operations (read/write/stat/etc).
  • Adds constructor mount options and “live” mount methods for Bash and BashTool, and enables the Rust realfs feature 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.

Comment thread crates/bashkit-python/src/lib.rs Resolved
Comment thread crates/bashkit-python/src/lib.rs Resolved
Comment thread crates/bashkit-python/src/lib.rs Resolved
Comment thread crates/bashkit-python/src/lib.rs Resolved
@chaliy

chaliy commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

@h0rv can you pls reabse on top of latest main? There was an issue in CI.

@h0rv
h0rv force-pushed the robby/python-fs-exposure branch from a29ceaa to ed6854f Compare March 20, 2026 01:13
@chaliy

chaliy commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

@h0rv this is a nice addition, would you be able to address comments from Copilot?

Also, do you really need mount_*, I am little hesitant to expose them to the public surface. I think they would require some rework on rust side.

@h0rv

h0rv commented Mar 20, 2026

Copy link
Copy Markdown
Contributor Author

@chaliy Yes, working on addressing comments. And I will reconsider the mount_* API.

@h0rv
h0rv requested a review from chaliy March 20, 2026 19:35
@h0rv

h0rv commented Mar 20, 2026

Copy link
Copy Markdown
Contributor Author

@chaliy updated fs() to stay live across mount_*() and `reset()

@chaliy

chaliy commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

@h0rv I am trying to process this PR, but I am not ready to merge mount_* functions, I added MountFs - #784 , can you look if this works for you?

@h0rv
h0rv force-pushed the robby/python-fs-exposure branch from 4a0f6a1 to 68055f3 Compare March 23, 2026 15:44

@chaliy chaliy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 .pyi docstring for fs() (e.g., "Returns a live handle — each call resolves against the current interpreter")
  • Optionally adding a snapshot() method that returns a Static handle 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 error
  • FileSystem.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 chaliy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chaliy
chaliy merged commit 3cd4ce4 into everruns:main Mar 23, 2026
13 checks passed
chaliy added a commit that referenced this pull request Mar 23, 2026
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
yonas pushed a commit to yonasBSD/bashkit that referenced this pull request Mar 24, 2026
…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
chaliy pushed a commit that referenced this pull request May 30, 2026
## 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.
chaliy added a commit that referenced this pull request May 30, 2026
…#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants