Skip to content

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).

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

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 +i plane
  • NAPI bridge: ctx-sidecar-napi exposes the crate to Node.js via napi-rs
  • ~100x faster than TypeScript emitters

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)

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

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-bindgen for web integration

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

Each crate compiles to multiple targets:

TargetBindingUse Case
Nativecargo buildCLI, server-side
NAPInapi-rsNode.js native addon
PyO3pyo3Python binding
WASMwasm-bindgenBrowser, edge
Terminal window
cd crates/ctx-parser
napi build --release
Terminal window
cd crates/ctx-parser
wasm-pack build --target web

Enable the Rust native implementations via environment variables:

Terminal window
USE_RUST_SIDECAR=true actx start # Route translations through Rust

The TypeScript implementations remain as fallbacks when native addons aren’t available.

ComponentTypeScriptRust NativeSpeedup
Parser~0.1ms~0.005ms20x
Sidecar (human)~1ms~0.01ms100x
CTXB encode~0.05ms~0.001ms50x
CTXB decode~0.05ms~0.001ms50x