Entroly 0.18.0: Building a 5,300-line Rust Engine for AI Context Optimization #42
juyterman1000
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Entroly is an open-source context optimization engine for AI coding tools. The core is a single Rust crate (
entroly-core, ~5,300 lines) exposed to Python via PyO3/maturin. v0.18.0 shipped this week with gzip-compressed index persistence, 415 Rust unit tests, and several architectural lessons worth sharing.This post covers the Rust-specific implementation details: what worked, what surprised us, and the patterns that emerged from building a high-performance data plane that Python calls into via FFI.
This article was written with AI assistance.
Architecture: Why Rust for the Hot Path
Entroly's Python layer handles orchestration (MCP server, HTTP proxy, CLI). The Rust core owns everything latency-sensitive:
The boundary is a single
#[pyclass]struct with ~40#[pymethods]. PyO3's GIL management is straightforward once you accept that every method returning aPyObjectneedsPython::with_gil.Pattern 1: REINFORCE with EMA Baseline (PRISM)
The core optimization loop uses policy-gradient RL to learn which scoring weights produce useful context selections. The key insight: we don't need a neural network. Four scalar weights with REINFORCE + an EMA baseline work remarkably well.
The
apply_prism_rl_updatecomputes per-weight policy gradients and applies them with a temperature-scaled learning rate. A 5D variant adds a resonance dimension for pairwise fragment interaction learning.What we learned: The EMA baseline is critical. Without it, gradient variance makes the weights oscillate wildly. With it, convergence happens in ~50 feedback events.
Pattern 2: SimHash for Near-Duplicate Detection
AI coding tools frequently re-ingest the same file with minor edits. We use SimHash (Charikar's locality-sensitive hash) to detect near-duplicates in O(1):
Two fragments are near-duplicates if their Hamming distance is <= 3 bits (out of 64). This catches copy-paste variations, reformatted code, and minor edits — without the overhead of embedding models.
What we learned: The 3-bit threshold was tuned empirically. At 2 bits, we missed too many real duplicates. At 4 bits, we started collapsing genuinely different files that happened to share common boilerplate.
Pattern 3: Gzip Persistence with Backward-Compat Magic-Byte Detection
v0.18.0 fixed a latent bug:
persist_indexwas writing plain JSON to a file namedindex.json.gz. The fix usesflate2::write::GzEncoderfor actual compression, andload_indexdetects the format by inspecting the first two bytes:What we learned: The
#[cfg(unix)]guard forset_permissions(0o600)is essential — the index file contains ingested source code and must not be world-readable. On Windows, this is a no-op. The atomic temp-file-then-rename pattern prevents corruption if the process is killed mid-write.Pattern 4: Entropy-Gated Semantic Caching (EGSC)
The cache uses Thompson Sampling for admission control — a technique borrowed from multi-armed bandits. Each cache entry has a Beta distribution quality model. Admission decisions sample from this distribution rather than using a fixed threshold:
The frequency sketch is a Count-Min Sketch (4 rows x 256 columns) that estimates query popularity without storing the queries themselves. Cache invalidation uses dual signals: content hash (MD5 of the fragment body) and a monotonic generation counter.
Pattern 5: Submodular Knapsack with Greedy (1-1/e) Guarantee
Context selection is a submodular maximization problem under a cardinality (token budget) constraint. We use the standard greedy algorithm with lazy evaluations:
The marginal gain function combines four PRISM-weighted signals (recency, frequency, semantic relevance, entropy) plus optional resonance and causal bonuses.
Testing: 415 Rust Tests
The test suite covers:
All 415 tests pass in 1.6 seconds on a single core.
Numbers
On Entroly's own codebase (433 files, 1M tokens):
Links
pip install entrolyAll reactions