High-performance filesystem operations for AI agents
Project description
Agent-Gear
High-performance filesystem operations for AI agents, powered by Rust.
Features
- Stateful Indexing: In-memory file tree with LRU-cached glob patterns
- Batch I/O: Parallel file read/write with smart threshold optimization
- High-Performance Search: Grep with parallel search and mmap for large files
- Atomic Writes: Safe file modifications with temp-fsync-rename pattern
- Fast Writes: Optional non-atomic mode for 30x faster writes
- Large File Support: Efficient line-based reading for logs and large files
- Async Support: Full async/await API with
AsyncFileSystem - File Watching: Real-time filesystem monitoring with event debouncing
Installation
pip install agent-gear
For development:
git clone https://github.com/anthropics/agent-gear
cd agent-gear
pip install maturin
maturin develop --release
Quick Start
from agent_gear import FileSystem
with FileSystem("/path/to/project") as fs:
fs.wait_ready()
# List and glob
all_files = fs.list("**/*")
py_files = fs.glob("**/*.py")
# Search with grep
results = fs.grep("TODO", "**/*.py")
for r in results:
print(f"{r.file}:{r.line_number}: {r.content}")
# Read files
content = fs.read_file("main.py")
batch = fs.read_batch(py_files[:10])
# Large file: read specific lines
lines = fs.read_lines("app.log", start_line=1000, count=100)
# Write files
fs.write_file("output.txt", content) # Atomic (safe)
fs.write_file_fast("temp.txt", content) # Fast (30x faster)
# Edit in place
fs.edit_replace("config.py", "DEBUG = False", "DEBUG = True")
Async Usage
from agent_gear import AsyncFileSystem
import asyncio
async def main():
async with AsyncFileSystem("/path/to/project") as fs:
await fs.wait_ready()
# All operations are async
files = await fs.list("**/*.py")
results = await fs.grep("TODO", "**/*.py")
# Concurrent operations
results = await asyncio.gather(
fs.list("**/*.py"),
fs.grep("def main"),
fs.read_file("README.md"),
)
asyncio.run(main())
API Reference
FileSystem
class FileSystem:
def __init__(self, root: str, auto_watch: bool = True) -> None: ...
# Listing
def list(self, pattern: str = "**/*", only_files: bool = True) -> list[str]: ...
def glob(self, pattern: str) -> list[str]: ...
# Reading
def read_file(self, path: str, encoding: str = "utf-8") -> str: ...
def read_batch(self, paths: list[str]) -> dict[str, str]: ...
def read_lines(self, path: str, start_line: int = 0, count: int | None = None) -> list[str]: ...
def read_file_range(self, path: str, offset: int, limit: int) -> str: ...
# Writing
def write_file(self, path: str, content: str) -> bool: ... # Atomic
def write_file_fast(self, path: str, content: str) -> bool: ... # Fast
def edit_replace(self, path: str, old_text: str, new_text: str, strict: bool = True) -> bool: ...
# Searching
def grep(self, query: str, glob_pattern: str = "**/*",
case_sensitive: bool = False, max_results: int = 1000) -> list[SearchResult]: ...
# Metadata & Control
def get_metadata(self, path: str) -> FileMetadata: ...
def is_ready(self) -> bool: ...
def is_watching(self) -> bool: ...
def refresh(self) -> None: ...
def close(self) -> None: ...
AsyncFileSystem
Same API as FileSystem, but all I/O methods are async.
Data Classes
class SearchResult:
file: str # Relative file path
line_number: int # Line number (1-indexed)
content: str # Matching line content
class FileMetadata:
size: int # File size in bytes
mtime: float # Modification time (Unix timestamp)
is_dir: bool # Is directory
is_binary: bool # Is binary file
Performance
Benchmark on 3000 files (polyglot project, 20 repeated queries):
| Operation | Agent-Gear | Stdlib | Speedup |
|---|---|---|---|
| List all | 1.75ms | 3.5ms | 2.0x |
| *Glob .py | 1.84ms | 5.1ms | 2.8x |
| *Glob .ts | 2.01ms | 5.0ms | 2.5x |
| Grep TODO | 4.2ms | 7.8ms | 1.9x |
| Write (fast) | 0.14ms | 4.5ms (atomic) | 33x |
| Async (3 ops) | 3.9ms | 8.0ms (seq) | 2.0x |
Overall: 1.9x faster | Index: ~100ms (breaks even in 1 query round)
When to Use Agent-Gear
- Repeated queries: Index pays off after just 1-2 query rounds
- Large codebases: 1000+ files show 1.5-3x speedup
- Glob-heavy workflows: Cached patterns, 2-3x faster
- Grep searches: Parallel + mmap, 2x faster
- Async I/O: Concurrent operations with 2x speedup
Run benchmarks:
python benchmarks/benchmark.py # Single mode, 500 files
python benchmarks/benchmark.py --mode repeated # Repeated queries
python benchmarks/benchmark.py --mode all --files 3000 # Full test
Architecture
agent_gear/
├── _rust_core.so # Rust extension (PyO3)
└── __init__.py # Python wrapper + AsyncFileSystem
src/
├── lib.rs # PyO3 module entry
└── fs/
├── mod.rs # FileSystem pyclass
├── index.rs # DashMap-based file index + LRU glob cache
├── searcher.rs # Parallel grep with mmap
├── io.rs # Batch read/write with threshold optimization
├── atomic.rs # Atomic write (temp-fsync-rename)
└── watcher.rs # File watching with debouncing
Development
Prerequisites
- Python 3.12+
- Rust 1.75+
- maturin
Build & Test
# Development build
maturin develop --release
# Run tests
pytest tests/python -v
cargo test
# Run benchmarks
cargo bench
python benchmarks/benchmark.py
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agent_gear-0.1.0.tar.gz.
File metadata
- Download URL: agent_gear-0.1.0.tar.gz
- Upload date:
- Size: 90.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
630bad834e0eb08e45ecbb41ad8944f45c77cc5af1a3ea54ac27dbff268da841
|
|
| MD5 |
d8285e6dab28ee2f2f51fa544faaa950
|
|
| BLAKE2b-256 |
c3c00f2d468cc2eb976da6de6ef91ef3472de894fcc2921fc0221d1996406e1b
|
File details
Details for the file agent_gear-0.1.0-cp312-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: agent_gear-0.1.0-cp312-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 1.2 MB
- Tags: CPython 3.12+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ef4dc9c095e7f24a4c4dd819d466f23d2cdbb5620c0517291ee7cb4afdee8de
|
|
| MD5 |
8301e20c3192d660603add7636639664
|
|
| BLAKE2b-256 |
223fdc92210ae58f4e8e132f0a528f7a26a767b48e8616f66541bb33f5b55d04
|