Skip to content

research(nightly): semantic-query-cache — cosine-similarity cache for agent memory workloads (ADR-298) - #811

Draft
ruvnet wants to merge 1 commit into
mainfrom
research/nightly/2026-08-09-semantic-query-cache
Draft

research(nightly): semantic-query-cache — cosine-similarity cache for agent memory workloads (ADR-298)#811
ruvnet wants to merge 1 commit into
mainfrom
research/nightly/2026-08-09-semantic-query-cache

Conversation

@ruvnet

@ruvnet ruvnet commented Aug 9, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds crates/ruvector-semantic-cache: a zero-dependency Rust crate implementing a semantic cache layer that maps (query_vector → result_ids) and detects cache hits via cosine similarity, enabling AI agents to skip redundant vector searches when they re-ask semantically identical questions with slightly different wording.
  • Two production-relevant backends: LinearScanCache (O(C·D), optimal for C < 1 000) and ShardedCache (6-bit multi-probe LSH, O(7·C/64·D), suitable for C up to ~50 000), both implementing the QueryCache trait.
  • Real benchmark results (N=10 000 base vectors, D=128, 50 clusters, 500 queries, k=10, threshold=0.92): LinearCache 90.0% hit rate / 9.5× speedup; ShardedCache 85.6% hit rate / 6.7× speedup — all 6 acceptance criteria PASS.
  • Adds docs/adr/ADR-298-semantic-query-cache.md and full nightly research document under docs/research/nightly/2026-08-09-semantic-query-cache/.

What changed

New crate: crates/ruvector-semantic-cache

File Purpose
src/lib.rs QueryCache trait, NoCache baseline, dot() / normalize() utilities
src/linear.rs LinearScanCache: exhaustive cosine scan with FIFO eviction and TTL
src/sharded.rs ShardedCache: 6-bit LSH projection, multi-probe (primary + 6 one-bit-flip neighbors)
src/metrics.rs CacheStats with Cell<u64> interior mutability (single-threaded / WASM-safe)
src/dataset.rs Deterministic clustered dataset generator (LCG RNG, Box-Muller Gaussian, brute-force top-k)
src/bin/benchmark.rs Three-variant benchmark with real acceptance thresholds; exits 1 on failure

Design choices

  • Pre-normalized vectors: cosine similarity reduces to a dot product — no sqrt per comparison.
  • TTL via monotonic u64 tick counter: avoids SystemTime so the crate compiles to WASM without shims.
  • Recall tradeoff documented: cached hits return results from a prior similar query, not the exact query; mean recall ≈ 0.744 at 90% hit rate is inherent and documented as an acceptance floor (≥ 0.60), not a target.
  • No external dependencies: pure Rust stdlib; compiles alongside all existing RuVector crates without dependency conflicts.

Documentation

  • docs/adr/ADR-298-semantic-query-cache.md — context, decision, consequences, alternatives, benchmark evidence, failure modes, security considerations, migration path.
  • docs/research/nightly/2026-08-09-semantic-query-cache/README.md — full research document with SOTA survey, forward-looking thesis, ecosystem fit analysis, architecture diagram, performance math, and next steps.
  • docs/research/nightly/2026-08-09-semantic-query-cache/gist.md — SEO-optimized public article with comparison against 9 vector databases and practical + exotic application tables.

Test plan

  • cargo test -p ruvector-semantic-cache — 21 tests, 0 failures
  • cargo build --release -p ruvector-semantic-cache — clean build, 0 warnings (except dead_code on public API surface)
  • cargo run --release -p ruvector-semantic-cache --bin benchmark — all 6 acceptance criteria PASS:
    • LinearCache hit rate ≥ 80%: PASS (90.0%)
    • ShardedCache hit rate ≥ 70%: PASS (85.6%)
    • LinearCache mean recall ≥ 60%: PASS (0.744)
    • ShardedCache mean recall ≥ 60%: PASS (0.757)
    • LinearCache speedup ≥ 3×: PASS (9.50×)
    • ShardedCache speedup ≥ 3×: PASS (6.65×)

Generated by Claude Code

…arded backends (ADR-298)

Implements a zero-dependency Rust semantic cache layer for RuVector agent
memory workloads. Caches (query_vector → result_ids) pairs and detects
cache hits via cosine similarity (dot product on pre-normalized vectors),
tolerating natural-language rephrasing that would defeat exact-match caches.

Two production-relevant backends:
- LinearScanCache: O(C·D) exhaustive scan, optimal for C < 1 000 entries
- ShardedCache: 6-bit LSH with multi-probe (primary + 6 one-bit-flip
  neighbors), lookup scans ~7×C/64 entries, suitable for C up to ~50 000

Benchmark results (N=10 000 base vectors, D=128, 50 clusters, 500 queries,
k=10, threshold=0.92, noise_std=0.02):
  LinearCache:  90.0% hit rate, 9.5× speedup, 0.744 mean recall — PASS
  ShardedCache: 85.6% hit rate, 6.7× speedup, 0.757 mean recall — PASS

Files added:
  crates/ruvector-semantic-cache/src/lib.rs      — QueryCache trait, NoCache, utilities
  crates/ruvector-semantic-cache/src/linear.rs   — LinearScanCache
  crates/ruvector-semantic-cache/src/sharded.rs  — ShardedCache (multi-probe LSH)
  crates/ruvector-semantic-cache/src/metrics.rs  — CacheStats (Cell-based, WASM-safe)
  crates/ruvector-semantic-cache/src/dataset.rs  — deterministic clustered dataset generator
  crates/ruvector-semantic-cache/src/bin/benchmark.rs — benchmark binary
  docs/adr/ADR-298-semantic-query-cache.md       — Architecture Decision Record
  docs/research/nightly/2026-08-09-semantic-query-cache/README.md — research doc
  docs/research/nightly/2026-08-09-semantic-query-cache/gist.md   — public gist

All 21 unit tests pass. Benchmark binary exits 0 with all acceptance criteria met.

Co-Authored-By: claude-flow <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_01DW2etWbojPcWNGMHQLcyb3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants