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 7 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, llm
- 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-client-wasm
Section titled “ctx-client-wasm”Unified WASM client bundle — parser + sidecar + compactor + Ed25519 signing:
- Single import — includes all core CTX functionality in one WASM module
- Target: browser (WASM)
- Used by: AgentCTX playground and web components
ctx-sentinel
Section titled “ctx-sentinel”L1/L3 consensus evaluation — threshold checks + decay scoring:
- Alignment enforcement — detects divergence, ungroundedness, sandbagging
- Decay scoring — memory decay based on activity and relevance
- Export:
#[no_mangle]functions for FFI integration
ctx-crypto
Section titled “ctx-crypto”Ed25519 keypair generation, signing, and verification:
- Constant-time operations — resistant to timing attacks
- Key derivation — hierarchical deterministic key derivation
- Used by: identity system, sidecar signing, trust verification
ctx-security
Section titled “ctx-security”Security utilities — zero-trust auth, bounds checking, anti-injection:
- RBAC enforcement — role-based access control for CTX operations
- OIDC discovery — OpenID Connect integration for enterprise
- Bridge authentication — edge auth bridge for multi-tenant deployments
ctx-surrealism
Section titled “ctx-surrealism”SurrealDB WASM plugin — fn::ctx_* functions for Pro/Team tiers:
- Database-native CTX functions — CTX parsing and translation inside SurrealDB
- WASM plugin — runs inside SurrealDB server
- Pro/Team feature — available with SurrealDB Pro or Team license
ctx-llm-adapter
Section titled “ctx-llm-adapter”LLM adapter — translates SurrealQL, GraphQL, REST, SQL, MCP JSON-RPC, LLM, and human English from CTX ASTs:
- Multi-protocol translation — converts CTX ASTs to various target protocols
- LLM provider integration — unified interface for different LLM providers
- Used by: sovereign executor for LLM-driven subagents
ctx-sovereign
Section titled “ctx-sovereign”Sovereign Engine — autonomous lifecycle orchestration, dispatch, and agent loop:
- Subagent management — Overseer, Gatekeeper, Scribe, Archivist personas
- Dispatch lifecycle — full lifecycle from dispatch creation to completion
- Pressure monitoring — pressure-based dispatch throttling
- Token budgeting — per-dispatch token budget enforcement
ctx-sovereign-napi
Section titled “ctx-sovereign-napi”N-API bridge for ctx-sovereign — exposes sovereign pressure monitor and executor to Node.js:
- Pressure assessment — assess pressure across dispatch records
- Executor bridge — run sovereign dispatches from Node.js
- Used by: edge proxy for dispatch orchestration
ctx-edge-proxy
Section titled “ctx-edge-proxy”Standalone Edge Proxy — high-performance routing and host-agnostic edge deployments:
- Low-latency CTX routing — Rust-native routing with minimal overhead
- Gateway replacement — replaces Node.js gateway middleware with compiled Rust
- Deployment: runs as standalone service or Cloudflare Worker
ctx-broker
Section titled “ctx-broker”Cloudflare Worker middleware pipeline — the single entry point for all agent CTX interactions:
- Middleware pipeline — composable middleware for CTX processing
- Tool dispatch — routes CTX statements to appropriate plane handlers
- Replaces
ctx-gateway(archived) — updated architecture for broker-based routing
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