# PROP: Hybrid Rust+Python Architecture -- Selective Rust Core via PyO3

## Source
- **URL**: https://developers.redhat.com/articles/2025/09/15/why-some-agentic-ai-developers-are-moving-code-python-rust
- **URL**: https://thenewstack.io/combining-rust-and-python-for-high-performance-ai-systems/
- **URL**: https://github.com/nearai/ironclaw (4.1k stars, Rust agent runtime)
- **Author**: Red Hat Developer (article), NearAI (IronClaw)
- **Date**: 2025-09 (Red Hat), 2026-02 (IronClaw launch)

## Summary
The successful pattern in AI tooling is hybrid: Python for orchestration/AI/plugins, Rust for performance-critical core. Pydantic-core achieved 17x speedup via Rust rewrite. Ruff achieved 10-100x over Python linters. The common pattern is: Python surface -> Rust core -> Python bindings via PyO3. A full Rust rewrite of iEvo would lose the Python AI ecosystem (Pydantic AI, LiteLLM, sentence-transformers, httpx) and dramatically slow feature velocity.

## Relevance to iEvo
iEvo is a Python CLI (Typer + Textual + Rich). The question is whether to rewrite in Rust. The answer from industry evidence is: NO full rewrite, YES selective Rust components via PyO3.

## Proposed Change

### What to rewrite in Rust (high value, justified):
1. **PTY bridge** (`src/ievo/core/pty_bridge.rs`): Performance-critical, needs sub-ms latency for transparent passthrough. portable-pty (2.5M downloads, Rust) is the foundation. Build a thin PyO3 wrapper around it.
2. **Credential vault** (`src/ievo/core/vault.rs`): Security-critical code benefits from Rust's memory safety. Replace current RSA+AES Python implementation with age encryption via rage (Rust) + pyrage (PyO3 bindings, 1.3.0, MIT).
3. **Streaming JSON parser** (`src/ievo/core/json_stream.rs`): For parsing tool call streams from Claude/Codex APIs. Rust serde_json + simd-json are 10-100x faster than Python ijson.

### What to keep in Python (high value, no benefit from Rust):
- CLI framework (Typer) -- no performance need
- TUI (Textual) -- Python-only framework
- AI orchestration (Pydantic AI, LiteLLM) -- Python ecosystem
- Pipeline engine -- logic-heavy, not compute-heavy
- Plugin system -- needs dynamic loading, Python excels
- Memory/RAG (sqlite-vec) -- already C-native extension
- Agent definitions (.md parsing) -- trivial performance need

### What IronClaw teaches us:
IronClaw (4.1k stars) is a full Rust agent runtime. It is impressive but:
- Requires PostgreSQL + pgvector (heavy infra vs our sqlite-vec)
- No Python AI ecosystem access (no Pydantic AI, no LiteLLM)
- Development velocity: 303 commits vs iEvo's rapid iteration in Python
- Right approach for "always-on cloud agents", wrong for "local CLI tool"
Learn from IronClaw's security patterns (WASM sandboxing, credential protection), don't copy its full-Rust approach.

### Build approach:
- Phase 1: PyO3 Rust extension crate (`ievo-core`) with PTY bridge
- Phase 2: Add credential vault to the Rust crate
- Phase 3: Add streaming JSON parser
- Ship as manylinux/macosx wheels alongside the Python package

## Affected Components
- New: `rust/` directory with Cargo workspace
- New: `ievo-core` PyO3 crate (PTY, vault, JSON stream)
- Modified: `pyproject.toml` (maturin build backend for Rust extension)
- Modified: `src/ievo/core/docker.py` (use Rust PTY bridge)

## Scores
- Applicability: 4/5
- Effort: high
- Impact: speed, reliability, security
