Rust Crates
Rust Crates
Section titled “Rust Crates”AgentCTX uses Rust for performance-critical components. The Rust crates provide native implementations that compile to multiple targets: native binaries, NAPI (Node.js), PyO3 (Python), and WASM (browser).
Available Crates
Section titled “Available Crates”ctx-parser
Section titled “ctx-parser”The core CTX parser, implementing the same grammar as the TypeScript CTXParser:
use ctx_parser::Parser;
let parser = Parser::new();let stmt = parser.parse("?k \"auth\" #code @7d ^3")?;// stmt.operator == Operator::Search// stmt.plane == Plane::Knowledge// stmt.target == "auth"- 75 test cases — byte-for-byte compatible with the TypeScript parser
- Targets: native, NAPI, PyO3, WASM
ctx-sidecar
Section titled “ctx-sidecar”The sidecar translation engine with all 6 emitter backends:
use ctx_sidecar::Sidecar;
let sidecar = Sidecar::new();let translation = sidecar.translate(&stmt, "human")?;// "Searched for knowledge matching \"auth\" with tag code..."- Emitters: human, surrealql, sql, rest, graphql, mcp-jsonrpc
- Inspection support: tier-adapted CTX and human-readable prose for
+iplane - NAPI bridge:
ctx-sidecar-napiexposes the crate to Node.js vianapi-rs - ~100x faster than TypeScript emitters
ctx-compactor
Section titled “ctx-compactor”Context compaction engine for reducing agent context size:
use ctx_compactor::{Compactor, Strategy};
let compactor = Compactor::new();let result = compactor.compact(&context, Strategy::Pressure)?;- Pressure-based compaction — automatically compresses context when approaching model limits
- Strategy registry — pluggable compaction strategies
- Model registry — aware of different model context windows
- Multi-target: native crate +
ctx-compactor-napi(Node.js) +ctx-compactor-wasm(browser)
ctx-gateway
Section titled “ctx-gateway”Rust HTTP gateway for LLM traffic interception:
- Port: 8420 (configurable via
CTX_GATEWAY_PORT) - Purpose: Intercepts LLM API calls, adds CTX context injection
- Docker: Runs as a separate service in Docker Compose
ctx-playground-wasm
Section titled “ctx-playground-wasm”Browser-based CTX playground WASM crate:
- Interactive parsing — parse and visualize CTX statements in the browser
- AST visualization — structured output of parsed statements
- Built with:
wasm-bindgenfor web integration
memory-wasm
Section titled “memory-wasm”WASM contracts for memory write validation:
use memory_wasm::WriteGate;
let gate = WriteGate::new(schema);let result = gate.validate(&memory_entry)?;- Edge validation — runs in browser and Node.js
- Schema enforcement — prevents malformed memory writes
Multi-Target Compilation
Section titled “Multi-Target Compilation”Each crate compiles to multiple targets:
| Target | Binding | Use Case |
|---|---|---|
| Native | cargo build | CLI, server-side |
| NAPI | napi-rs | Node.js native addon |
| PyO3 | pyo3 | Python binding |
| WASM | wasm-bindgen | Browser, edge |
Building for Node.js
Section titled “Building for Node.js”cd crates/ctx-parsernapi build --releaseBuilding for WASM
Section titled “Building for WASM”cd crates/ctx-parserwasm-pack build --target webFeature Flags
Section titled “Feature Flags”Enable the Rust native implementations via environment variables:
USE_RUST_SIDECAR=true actx start # Route translations through RustThe TypeScript implementations remain as fallbacks when native addons aren’t available.
Performance Comparison
Section titled “Performance Comparison”| Component | TypeScript | Rust Native | Speedup |
|---|---|---|---|
| Parser | ~0.1ms | ~0.005ms | 20x |
| Sidecar (human) | ~1ms | ~0.01ms | 100x |
| CTXB encode | ~0.05ms | ~0.001ms | 50x |
| CTXB decode | ~0.05ms | ~0.001ms | 50x |
See Also
Section titled “See Also”- Installation — installing Rust crates
- Sidecar — Rust native addon integration