feat: M3.8.1 phase 3 — CacheAligner + CCR Store (18 tests)
CacheAligner (180 LOC, 8 tests): - Detects dynamic patterns: timestamps, UUIDs, session IDs, temp paths, SHAs - Uses once_cell Lazy statics + Regex for pattern matching - Separates stable prefix (cache-able) from dynamic tail (varies) - Reports drift metrics (0.0-1.0 ratio of dynamic content) - Preserves identical prefixes across calls for KV cache hits CcrStore (170 LOC, 10 tests): - LRU cache with IndexMap (preserves insertion order) - SHA256 hashing for content identification - TTL-based expiry (default 1hr, configurable) - Thread-safe (Mutex-wrapped) - Supports large content (tested 100KB+) ContextOptimizer integration (2 tests): - Wired CCR store into optimizer - Stores originals when compression occurs + CCR enabled - Returns hash for retrieval hints 50 optimizer tests total: Phase 1 (17) + Phase 2 (15) + Phase 3 (18) = 50 passing
This commit is contained in:
@@ -8,6 +8,8 @@ pub mod router;
|
||||
pub mod log;
|
||||
pub mod json;
|
||||
pub mod diff;
|
||||
pub mod cache_align;
|
||||
pub mod ccr;
|
||||
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -16,6 +18,8 @@ pub use router::ContentRouter;
|
||||
pub use log::LogCompressor;
|
||||
pub use json::JsonCrusher;
|
||||
pub use diff::DiffCompressor;
|
||||
pub use cache_align::{CacheAligner, AlignedContent};
|
||||
pub use ccr::CcrStore;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OptimizedChunk {
|
||||
@@ -84,6 +88,7 @@ pub struct ContextOptimizer {
|
||||
log_compressor: LogCompressor,
|
||||
json_crusher: JsonCrusher,
|
||||
diff_compressor: DiffCompressor,
|
||||
ccr_store: CcrStore,
|
||||
config: ContextOptimizerConfig,
|
||||
}
|
||||
|
||||
@@ -99,16 +104,23 @@ impl ContextOptimizer {
|
||||
let log_compressor = LogCompressor::new();
|
||||
let json_crusher = JsonCrusher::new();
|
||||
let diff_compressor = DiffCompressor::new();
|
||||
let ccr_store = CcrStore::new();
|
||||
|
||||
Ok(Self {
|
||||
router,
|
||||
log_compressor,
|
||||
json_crusher,
|
||||
diff_compressor,
|
||||
ccr_store,
|
||||
config,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get reference to CCR store for retrieval
|
||||
pub fn ccr_store(&self) -> &CcrStore {
|
||||
&self.ccr_store
|
||||
}
|
||||
|
||||
/// Optimize a chunk of content
|
||||
pub fn optimize(&self, content: &str) -> Result<OptimizedChunk> {
|
||||
if !self.config.enabled {
|
||||
@@ -143,12 +155,19 @@ impl ContextOptimizer {
|
||||
let original_tokens = estimate_tokens(content);
|
||||
let compressed_tokens = estimate_tokens(&compressed);
|
||||
|
||||
// Store original in CCR if compression happened and CCR is enabled
|
||||
let ccr_hash = if self.config.ccr_enabled && compressed != content {
|
||||
self.ccr_store.store(content).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(OptimizedChunk {
|
||||
compressed,
|
||||
original_tokens,
|
||||
compressed_tokens,
|
||||
content_type,
|
||||
ccr_hash: None, // TODO: Implement CCR
|
||||
ccr_hash,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -185,4 +204,26 @@ mod tests {
|
||||
let chunk = optimizer.optimize("test content").unwrap();
|
||||
assert_eq!(chunk.compressed, "test content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optimizer_with_ccr() {
|
||||
let optimizer = ContextOptimizer::new().unwrap();
|
||||
let log_content = "ERROR: failed\nWARN: ignored";
|
||||
let chunk = optimizer.optimize(log_content).unwrap();
|
||||
|
||||
// If compression happened and CCR enabled, should have hash
|
||||
if chunk.compressed != log_content {
|
||||
assert!(chunk.ccr_hash.is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optimizer_cache_align() {
|
||||
let content = "System prompt\nAt 2026-08-28T09:15:00Z query was:\nContext";
|
||||
let aligned = CacheAligner::align(content);
|
||||
|
||||
assert!(aligned.stable_prefix.contains("System"));
|
||||
assert!(aligned.stable_prefix.contains("Context"));
|
||||
assert!(aligned.dynamic_tail.contains("2026-08-28"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user