Calybris Core
Deterministic proof-carrying decision core for systems that must explain and replay why an action was allowed, substituted, or rejected.
Not an LLM framework. Not an exchange or strategy engine. A domain-neutral primitive:
candidate + policy constraints → decision + digests + optional WAL + budget proof
#![forbid(unsafe_code)] · unit/proptest/Loom/Miri coverage · Apache-2.0
Two Reference Use Cases
| Use case | What Calybris does |
|---|---|
| LLM routing | Select / substitute / reject models under budget, risk, quality, latency |
| Pre-trade guard | Admit / reject candidate orders under exposure, risk, and latency limits |
Calybris is not an exchange, market data feed, colocation stack, or alpha engine. It is a deterministic pre-trade decision kernel — integer-only constraints, replay verification, and fixed-point conservation proofs.
When to use it
Use Calybris when a service has to make the same decision twice and prove it got the same answer:
- route an LLM request under budget, latency, provider, and quality constraints
- reject or substitute a candidate action before it crosses a risk boundary
- write an auditable decision record to a tamper-evident WAL
- reconcile budget state with fixed-point conservation proofs
Do not use it as a hosted API, trading strategy, exchange adapter, web framework, or model orchestration platform. Calybris is the deterministic core you put behind those systems.
Try it locally
Use as a dependency
use BudgetEngine;
use ;
use *;
use ;
let models = vec!;
let snapshot = try_new?;
let input = KernelInput ;
let decision = snapshot.prescribe;
assert_eq!;
assert!;
let budget = new;
budget.ensure_tenant;
let proof: ConservationProof = prove_conservation?;
assert_eq!;
Kernel-only (no WAL):
Architecture
kernel— Integer-only decision kernel (~115ns/decision).prescribe_with_traceexposes per-constraint rejection counts.verify— Policy + input + decision digests, full replay,DigestDecodeErroron public API.finance— Ledger digest,FinancialCertificate,ConservationProof,prove_conservation,certify_snapshot.wal— Tamper-evident hash chain,append_verified_audited(fail-closed),replay_audited_wal.budget— CAS reserve/commit/release. Conservation holds after completed ops:remaining + reserved + committed_lifetime == initial. Loom + Miri in CI.proof—ProofEnvelope: single struct binding policy + input + decision digests + WAL position + budget proof.config— RuntimeEngineConfigwith builder pattern, validation, and budget integration (ensure_tenant).builder—InputBuilder,ModelBuilder,PolicyBuilderwithBuildError(config + policy + catalog size enforcement).persistence— fsync-backed snapshot save/load,checkpoint_with_wal,recovery_planwith WAL high-watermark.async_wal(featureasync) — Tokio-based non-blocking WAL with HMAC, chain validation, configurable sync.instrument(featureobservability) — Structuredtracingspans for prescribe, verify, budget, WAL.
Audit Pipeline
prescribe → verify_decision → append_verified_audited → replay_audited_wal (fail-closed)
↓ ↓
calypol1 / calyinp1 / calydcn1 ProofEnvelope (optional)
Financial layer & policy
Fixed-point i64 microcents (1 cent = 1,000,000). No f64.
committed_microcents— lifetime cumulative spend (monotonic; never decreases)reserved_microcents— active holds awaiting commit/releasetop_up_tenant— add funds without resetting lifetime spendrestore_from_snapshot— exclusive-recovery restore from frozenBudgetSnapshotverify_conservation— audit/reconciliation path (full snapshot)PolicySnapshot::utility_for_model— per-model utility (not prescribe winner/runner-up)
budget.ensure_tenant;
budget.top_up_tenant;
let proof = prove_conservation?;
let cert = certify_ledger;
assert!;
| Policy API | Use |
|---|---|
PolicySnapshot::try_new |
Production — validates catalog + BPS (MAX_BPS, etc.) |
PolicySnapshot::new_unchecked |
Tests / fuzz only — never serve without explicit validate() |
PolicySnapshot::new |
Deprecated alias for new_unchecked |
Feature Flags
| Feature | What it adds | Dependencies |
|---|---|---|
wal (default) |
Hash-chained WAL, HMAC-SHA256, audited append | serde, hmac, subtle |
async |
Tokio-based async WAL | wal + tokio |
observability |
Structured tracing spans/events | tracing |
full |
All of the above | — |
Builder Ergonomics (v0.4.0)
use EngineConfig;
use ;
let config = new
.latency_penalty
.hard_risk_limit
.default_exposure_cap;
let snapshot = new
.epochs
.model
.model
.build?;
let input = new
.tokens
.business_value
.risk
.minimum_quality
.build;
let decision = snapshot.prescribe;
Persistence & Recovery
use ;
// Checkpoint budget state alongside WAL position (fsync-backed)
let snap = checkpoint_with_wal?;
// After crash: figure out what needs replay
let plan = recovery_plan?;
println!;
// Restore from last checkpoint
let fresh = new;
restore?;
Proof Envelope
use ProofEnvelopeBuilder;
let envelope = new
.wal
.budget
.build;
assert!; // replay + WAL + budget all present
Examples
Tests & CI
cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test --all-features
cargo test --no-default-features
RUSTFLAGS='--cfg loom' LOOM_MAX_PREEMPTIONS=3 cargo test --test budget_loom
cargo +nightly miri test --lib --all-features # see docs/MIRI.md for CI filters
cargo doc --no-deps
Extensive test coverage across unit, property-based (proptest), 7 Loom exhaustive concurrency, and Miri UB detection targets. Feature matrix CI: default, no-default-features, async, full. See CI for the current test count.
Integration contract
Calybris verifies decisions and conservation proofs — it does not auto-invoke verify_decision in your hot path. You must call it at audit boundaries:
prescribe → verify_decision → (optional WAL / prove_conservation)
Use append_verified_audited (not append_audited) at production boundaries — it verifies before writing. See docs/AUDIT_GUIDE.md.
For fail-closed audit boundaries, use the verified helpers:
use verified_audit_bundle;
let bundle = verified_audit_bundle?;
assert!;
With the wal feature enabled, append_verified_audited verifies before writing. Invalid or tampered decisions do not enter the log:
use WalWriter;
let mut wal = open?;
wal.append_verified_audited?;
External audit
Invariant docs, adversarial tests, Loom, Miri, and supply-chain checks are in place for third-party review. A paid external audit is still your responsibility — see docs/AUDIT_GUIDE.md §7.
Security Posture
#![forbid(unsafe_code)]— zero unsafe blockscargo-audit+cargo-denyin CI- Miri on nightly — UB detection for all lib tests
- 7 Loom exhaustive concurrency tests for budget operations
- HMAC-SHA256 keyed tamper-evident WAL with constant-time comparison (
subtle) - Fail-closed
append_verified_audited— invalid decisions never enter the log - fsync-backed snapshot persistence with atomic rename
- Feature matrix CI:
default,no-default-features,async,full
What This Crate Is Not
- Exchange gateway, market data, or order lifecycle
- Thompson Sampling / adaptive routing
- HTTP API server
See emirhuseyin.tech/engine for the full proprietary stack.
License
Apache-2.0. See LICENSE.