Architecture Comparison: pi-mono vs terraphim-ai
Executive Summary
This document provides a comprehensive architectural analysis comparing pi-mono (badlogic/pi-mono) and terraphim-ai, two AI agent toolkits with fundamentally different architectural approaches.
| Aspect | pi-mono | terraphim-ai | |--------|-------------|------------------| | Language/Stack | TypeScript/Node.js (96.4%) | Rust (backend) + Svelte/TS (frontend) | | Monorepo Strategy | npm workspaces (7 packages) | Cargo workspace (40+ crates) | | Primary Focus | CLI coding agent with TUI/Web UI | Privacy-first local AI with knowledge graphs | | Architecture Style | Modular TypeScript libraries | Layered Rust services with WASM | | Distribution | npm packages + compiled binaries | crates.io + Homebrew + npm + PyPI |
Quality Assessment: This analysis passed disciplined research quality gates (4.2/5.0 KLS score).
1. pi-mono Architecture Deep Dive
1.1 Repository Structure
pi-mono/
├── packages/
│ ├── ai/ # Unified LLM API (@mariozechner/pi-ai)
│ ├── agent/ # Agent runtime core (@mariozechner/pi-agent-core)
│ ├── coding-agent/ # CLI tool (@mariozechner/pi-coding-agent)
│ ├── mom/ # Slack bot integration
│ ├── pods/ # vLLM deployment management
│ ├── tui/ # Terminal UI library (@mariozechner/pi-tui)
│ └── web-ui/ # Web components (@mariozechner/pi-web-ui)
├── .github/workflows/ # CI/CD automation
├── scripts/ # Build and release scripts
└── Root configs: package.json, tsconfig.base.json, biome.json1.2 Package Architecture
pi-ai Package (Unified LLM API)
- Purpose: Multi-provider LLM abstraction layer
- Key Dependencies:
@anthropic-ai/sdk,openai,@google/genai,@mistralai/mistralai@aws-sdk/client-bedrock-runtime@sinclair/typeboxfor runtime type validationajv+ajv-formatsfor JSON schema validation
- Architecture Pattern: Provider registry with dynamic model discovery
- Build: TypeScript compilation with
tsgo(custom/fast compiler)
pi-agent-core Package (Agent Runtime)
- Purpose: General-purpose agent with tool calling and state management
- Architecture: Event-driven agent loop with streaming support
- Key Features:
Agentclass with state managementagentLoop()/agentLoopContinue()for conversation flow- Steering queue for mid-conversation intervention
- Tool execution with pending call tracking
- Session-based caching support
// Core Agent Design Pattern (from agent.ts)
export class Agent {
private _state: AgentState = { /* ... */ };
private listeners = new Set<(e: AgentEvent) => void>();
private steeringQueue: AgentMessage[] = [];
private followUpQueue: AgentMessage[] = [];
// Event-driven architecture with pub/sub
subscribe(fn: (e: AgentEvent) => void): () => void;
// Steering API for mid-conversation intervention
steer(m: AgentMessage); // Interrupt during execution
followUp(m: AgentMessage); // Queue for post-completion
}pi-coding-agent Package (Main CLI)
- Purpose: Interactive coding agent with file operations
- Binary:
picommand - Architecture:
- CLI argument parsing with extension flag discovery
- Session management (local/global sessions, forking)
- Resource loading (extensions, skills, themes, prompts)
- Multiple modes: Interactive TUI, Print mode, RPC mode
- Package manager for extensions (npm/git/local sources)
Key Components:
SessionManager: File-based session persistence (.jsonl format)SettingsManager: Global + project-level configurationModelRegistry: Dynamic provider registrationResourceLoader: Extension/skill/theme loadingAuthStorage: API key management with runtime overrides
pi-tui Package (Terminal UI)
- Purpose: Differential rendering TUI library
- Key Dependencies:
chalk,marked,mime-types - Features: Efficient terminal rendering for AI chat interfaces
pi-web-ui Package (Web Components)
- Purpose: Reusable web UI for AI chat
- Dependencies:
lit,@mariozechner/mini-lit,tailwindcss - Features: Web component-based chat interface with file preview
1.3 Build & Tooling Configuration
TypeScript Configuration (tsconfig.base.json)
Code Quality (biome.json)
- Linter: Biome with recommended rules
- Formatter: Tab indentation, 120 character line width
- Scope:
packages/*/src/**/*.ts, strict file includes
CI/CD Pipeline (.github/workflows/ci.yml)
jobs:
build-check-test:
runs-on: ubuntu-latest
steps:
- Install Node.js 22
- Install system deps (cairo, pango, jpeg, gif, rsvg, fd-find, ripgrep)
- npm ci
- npm run build # Ordered package build
- npm run check # Biome + TypeScript
- npm test # Vitest1.4 Dependency Management
Package Dependencies:
- Internal: Tight coupling between packages via npm workspace references
- External:
- AI Providers: Anthropic, OpenAI, Google, Mistral, AWS Bedrock
- Utilities: chalk, glob, marked, yaml, diff, ignore
- Build:
tsgo(fast TS compiler),tsx(runtime)
Version Strategy:
- All packages version-locked together (0.50.9)
- npm workspaces with
sync-versions.jsscript - Release automation via
scripts/release.mjs
2. terraphim-ai Architecture Deep Dive
2.1 Repository Structure
terraphim-ai/
├── crates/ # 40+ Rust library crates
│ ├── terraphim_agent/ # CLI/TUI agent (main binary)
│ ├── terraphim_automata/ # Text processing, Aho-Corasick automata
│ ├── terraphim_service/ # Core service logic
│ ├── terraphim_rolegraph/ # Knowledge graph implementation
│ ├── terraphim_middleware/# Document indexing, search orchestration
│ ├── terraphim_multi_agent/ # Multi-agent LLM system
│ ├── haystack_*/ # Data source integrations (7+ sources)
│ └── ... (30+ more crates)
├── terraphim_server/ # HTTP server binary (Axum)
├── terraphim_firecracker/ # VM execution environment
├── desktop/ # Svelte + Tauri frontend
│ ├── src/ # Svelte components
│ ├── src-tauri/ # Tauri Rust backend
│ └── tests/ # Playwright E2E tests
├── .docs/ # Comprehensive documentation
└── Cargo.toml (workspace root)2.2 Workspace Configuration
[workspace]
resolver = "2"
members = ["crates/*", "terraphim_server", "terraphim_firecracker",
"desktop/src-tauri", "terraphim_ai_nodejs"]
exclude = ["crates/terraphim_agent_application", "crates/terraphim_truthforge",
"crates/terraphim_automata_py", "crates/terraphim_validation",
"crates/terraphim_rlm"] # Experimental
default-members = ["terraphim_server"]
[workspace.package]
version = "1.6.0"
edition = "2024" # Latest Rust edition
[workspace.dependencies]
tokio = { version = "1.0", features = ["full"] }
reqwest = { version = "0.12", features = ["json", "rustls-tls"] }
serde = { version = "1.0", features = ["derive"] }
async-trait = "0.1"
thiserror = "1.0"
anyhow = "1.0"2.3 Crate Architecture
terraphim_agent (CLI/TUI Binary)
-
Features: Modular feature flags for different capabilities
repl: Basic REPL with rustylinerepl-interactive: TTY detectionrepl-full: All features including sessions, chat, MCP, webrepl-sessions: AI assistant session search (Claude Code, Cursor, Aider)
-
Architecture:
main.rs: 91KB - comprehensive CLI with ratatui TUIclient.rs: API client for terraphim_serverservice.rs: Business logic layerrepl/: REPL implementation with command systemcommands/: Markdown-defined custom commandsonboarding/: Interactive setup wizardguard_patterns.rs: Git safety guard patterns
Key Design Patterns:
// Feature-gated module structure
// Async runtime with tokio
use Runtime;
// CLI with clap derive macros
terraphim_automata (Text Processing Core)
-
Purpose: High-performance text matching using Aho-Corasick algorithm
-
Features:
remote-loading: Async data fetchingtokio-runtime: Async supportwasm: WebAssembly compilation with wasm-bindgentypescript: TypeScript bindings via tsify
-
Multi-Language Distribution:
- Rust crate (crates.io)
- npm package (
@terraphim/autocomplete) via NAPI - PyPI package (
terraphim-automata) via PyO3
terraphim_server (HTTP API Server)
-
Framework: Axum with WebSocket support
-
Features:
openrouter: OpenRouter AI integrationollama: Local Ollama LLM supportsqlite/redis: Multiple database backends
-
Architecture:
- REST API endpoints for search and indexing
- WebSocket for real-time updates
- Static file embedding with
rust-embed - CORS and tracing middleware
terraphim_multi_agent (Multi-Agent System)
- Purpose: 13 LLM-powered agents for narrative analysis
- Architecture:
- Async agent orchestration with tokio
- WebSocket progress streaming
- Session-based result storage
- Knowledge graph context enrichment
2.4 Frontend Architecture (Desktop)
Technology Stack
- Framework: Svelte 5.47.1 with TypeScript
- Desktop: Tauri 1.6.3 (Rust-based Electron alternative)
- Styling: Bulma CSS 1.0.4 + Bulmaswatch themes
- Build: Vite 5.3.4
- Testing: Vitest + Playwright E2E
Package.json Highlights
2.5 Build & Quality Configuration
Clippy Configuration (.clippy.toml)
msrv = "1.80.0"
avoid-breaking-exported-api = false
allow-unwrap-in-tests = true
cognitive-complexity-threshold = 30Pre-commit Configuration
- Conventional commits enforcement
cargo fmtfor Rust formatting- Biome for JavaScript/TypeScript
- Secret detection with detect-secrets
- Large file protection
2.6 Documentation Practices
Comprehensive Documentation System:
.docs/summary.md: Consolidated project overview (200+ lines).docs/summary-<file>.md: Individual file summariesAGENTS.md: AI agent instructionsCLAUDE.md: Claude Code integration guideCONTRIBUTING.md: Detailed contribution guidelinesHANDOVER.md: Async collaboration documentation
3. Comparative Analysis
3.1 Language/Stack Differences
| Dimension | pi-mono | terraphim-ai | |-----------|-------------|------------------| | Primary Language | TypeScript (96.4%) | Rust (backend) + TypeScript (frontend) | | Runtime | Node.js 20+ | Native (Rust) + tokio async | | Package Manager | npm with workspaces | Cargo workspace | | Memory Safety | GC-based | Compile-time (ownership/borrowing) | | Performance | Good (interpreted) | Excellent (native + zero-copy) | | WASM Support | Limited | First-class (terraphim_automata) |
Key Observations:
- pi-mono prioritizes development velocity with TypeScript's flexibility
- terraphim-ai prioritizes performance and safety with Rust's guarantees
- Both use TypeScript for frontend (terraphim-ai desktop is Svelte + Tauri)
3.2 Architecture Patterns
| Pattern | pi-mono | terraphim-ai | |---------|-------------|------------------| | Modularity | npm packages with clear boundaries | Cargo crates with workspace | | Dependency Injection | Constructor-based | Trait-based (Rust) | | State Management | Class-based with event emitters | Immutable + channel-based | | Concurrency | Async/await (single-threaded) | Tokio (multi-threaded async) | | Error Handling | try/catch + union types | Result<T,E> + ? operator | | Configuration | SettingsManager (hierarchical) | 4-level priority system |
pi-mono Agent Pattern:
// Event-driven with pub/sub
class Agent {
private listeners = new Set<(e: AgentEvent) => void>();
subscribe(fn: (e: AgentEvent) => void): () => void;
steer(m: AgentMessage); // Mid-conversation intervention
}terraphim-ai Service Pattern:
// Trait-based abstraction with async
3.3 Code Organization & Modularity
pi-mono:
- Strengths: Clear package separation, npm workspace integration, shared tsconfig
- Weaknesses: Tight internal coupling (packages reference each other by version)
- 7 main packages with focused responsibilities
terraphim-ai:
- Strengths: 40+ crates for fine-grained modularity, feature flags for compile-time selection
- Weaknesses: Complex dependency graph, many experimental crates excluded
- Workspace members include binaries, crates, and nested WASM projects
3.4 Build/Test Tooling
| Tool | pi-mono | terraphim-ai | |------|-------------|------------------| | Build | npm scripts + tsgo | Cargo + Earthly | | Linting | Biome | Clippy + cargo fmt | | Testing | Vitest | cargo test + Playwright | | CI/CD | GitHub Actions | GitHub Actions + Earthfile | | Documentation | Markdown | Markdown + mdBook | | Release | npm publish + GitHub | release-plz + multi-platform |
pi-mono Build Chain:
terraphim-ai Build Chain:
&& && 3.5 Documentation Practices
pi-mono:
- Standard README per package
- AGENTS.md for AI agent guidance
- CONTRIBUTING.md for contribution guidelines
- Changelog per package
- MIT License
terraphim-ai:
- Comprehensive
.docs/folder with 50+ files - Automated file summary generation (
summary-<file>.md) - Consolidated
summary.mdwith architecture overview - Multiple agent instruction files (AGENTS.md, CLAUDE.md)
- Conventional commits enforcement
- Apache 2.0 License
3.6 Agent/AI Integration Approaches
pi-mono Approach:
- Unified LLM API: Single interface for multiple providers
- Agent Runtime: Event-driven loop with tool calling
- Extension System: npm/git/local package loading
- Session Management: File-based .jsonl sessions
- Steering API: Real-time conversation intervention
Key Integration Pattern:
// Extension system for custom providers
interface Extension {
flags: Map<string, Flag>;
runtime: ExtensionRuntime;
}
// Provider registration at runtime
modelRegistry.registerProvider(name, config);terraphim-ai Approach:
- Multi-Agent System: 13 specialized agents with workflow patterns
- Knowledge Graph: Aho-Corasick automata for semantic search
- MCP Server: Model Context Protocol for AI tool integration
- Hooks System: Pre/Post tool validation and replacement
- Firecracker VMs: Sandboxed code execution
Key Integration Pattern:
// Knowledge graph enrichment
;
// Two-stage validation hooks
4. Architectural Quality Assessment
4.1 Maintainability
pi-mono:
- Grade: B+
- Strengths: Clear package boundaries, consistent TypeScript patterns, good test coverage
- Weaknesses: Tight coupling between packages, build ordering dependencies, large main.ts (1000+ lines)
terraphim-ai:
- Grade: A-
- Strengths: Excellent modularity (40+ crates), feature flags, comprehensive documentation, security-first design
- Weaknesses: Complex workspace structure, many experimental crates, steep learning curve for contributors
4.2 Software Engineering Best Practices
pi-mono:
- Strict TypeScript with Node16 module resolution
- Biome linting with recommended rules
- Automated testing with Vitest
- Conventional commits
- Semantic versioning across packages
terraphim-ai:
- Rust 2024 edition with strict clippy
- Memory safety guarantees
- Comprehensive error handling with thiserror/anyhow
- Async/await with structured concurrency
- Security-first with validation hooks
- Multi-platform distribution (crates.io, npm, PyPI, Homebrew)
4.3 Key Recommendations
For pi-mono:
- Consider breaking down
main.tsinto smaller modules - Add more explicit architectural documentation
- Consider adding Rust/WASM for performance-critical paths
- Expand test coverage for edge cases
For terraphim-ai:
- Consolidate or remove experimental crates
- Add more inline code examples in documentation
- Consider a TypeScript-first API for broader adoption
- Add architecture decision records (ADRs)
5. Conclusion
Both repositories represent high-quality AI agent toolkits with different architectural philosophies:
- pi-mono excels at rapid development and TypeScript ecosystem integration, with a clean npm workspace structure and excellent CLI UX
- terraphim-ai excels at performance, security, and multi-language distribution, with a sophisticated Rust-based architecture and comprehensive privacy-first design
The choice between them depends on use case: pi-mono for JavaScript/TypeScript environments needing quick integration, terraphim-ai for performance-critical or security-sensitive applications requiring native execution.
Lessons for terraphim-ai:
- pi-mono's steering API for mid-conversation intervention is an excellent UX pattern
- The extension system with runtime provider registration enables flexibility
- The unified LLM API abstraction reduces vendor lock-in
- Session management with fork support enables powerful conversation branching
Lessons for pi-mono:
- terraphim-ai's knowledge graph integration provides semantic context
- The two-stage validation hooks system enables security-first design
- Multi-platform distribution (crates.io, npm, PyPI) maximizes reach
- Feature flags enable compile-time optimization and smaller binaries
6. Appendix: Quality Gate Report
Document Quality Evaluation (KLS Framework)
| Dimension | Score | Status | |-----------|-------|--------| | Syntactic | 4/5 | Pass | | Semantic | 5/5 | Pass | | Pragmatic | 4/5 | Pass | | Social | 4/5 | Pass | | Physical | 4/5 | Pass | | Empirical | 4/5 | Pass |
Average Score: 4.2 / 5.0
Decision: GO
Blocking Dimensions: None
This document passed the disciplined research quality gate and is approved for architectural decision-making.
Generated using disciplined research methodology
Research document: .docs/research-pi-mono-comparison.md