1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
use std::collections::HashMap;
use std::path::Path;
use sha2::{Digest, Sha256};
use crate::delta_encoder::DeltaEncoder;
use crate::error::Result;
use crate::pipeline::{CompressionPipeline, SessionContext};
use crate::preset::Preset;
use crate::session_store::SessionStore;
use crate::types::CompressedContent;
/// Outcome of a cache lookup in [`CacheManager`].
pub enum CacheResult {
/// Previously seen content — returns a short inline reference (~13 tokens).
Dedup {
/// Inline token of the form `§ref:<hash_prefix>§`.
inline_ref: String,
/// Approximate token cost of the reference (always 13).
token_cost: u32,
},
/// Near-duplicate of cached content — returns a compact delta.
Delta {
/// The delta text (header + changed lines).
delta_text: String,
/// Approximate token cost of the delta.
token_cost: u32,
/// Similarity to the cached version.
similarity: f64,
},
/// Content not seen before — full compression result.
Fresh { output: CompressedContent },
}
/// Tracks when a dedup ref was last sent, so we can detect staleness.
#[derive(Debug, Clone)]
struct RefEntry {
/// The turn number when this ref was last sent to the LLM.
last_sent_turn: u64,
}
/// SHA-256 content-hash deduplication cache backed by [`SessionStore`],
/// with delta encoding for near-duplicate content and compaction awareness.
///
/// The turn-counter heuristic: each call to `get_or_compress` increments a
/// monotonic turn counter. When a dedup ref was last sent more than
/// `max_ref_age_turns` turns ago, the ref is considered stale (the original
/// content may have been compacted out of the LLM's context window) and the
/// full compressed content is re-sent instead.
pub struct CacheManager {
store: SessionStore,
max_size_bytes: u64,
delta_encoder: DeltaEncoder,
/// Monotonic turn counter — incremented on each get_or_compress call.
turn_counter: std::cell::Cell<u64>,
/// Maps content hash → turn when the dedup ref was last sent.
ref_tracker: std::cell::RefCell<HashMap<String, RefEntry>>,
/// Maximum age (in turns) before a dedup ref is considered stale.
/// After this many turns, the original content may have been compacted
/// out of the LLM's context window, so we re-send the full content.
max_ref_age_turns: u64,
}
impl CacheManager {
pub fn new(store: SessionStore, max_size_bytes: u64) -> Self {
Self {
store,
max_size_bytes,
delta_encoder: DeltaEncoder::new(),
turn_counter: std::cell::Cell::new(0),
ref_tracker: std::cell::RefCell::new(HashMap::new()),
max_ref_age_turns: 20,
}
}
/// Create a CacheManager with a custom ref staleness threshold.
pub fn with_ref_age(store: SessionStore, max_size_bytes: u64, max_ref_age_turns: u64) -> Self {
Self {
store,
max_size_bytes,
delta_encoder: DeltaEncoder::new(),
turn_counter: std::cell::Cell::new(0),
ref_tracker: std::cell::RefCell::new(HashMap::new()),
max_ref_age_turns,
}
}
/// Compute the SHA-256 hex digest of `bytes`.
fn sha256_hex(bytes: &[u8]) -> String {
let mut hasher = Sha256::new();
hasher.update(bytes);
format!("{:x}", hasher.finalize())
}
/// Advance the turn counter. Call this once per LLM interaction turn.
pub fn advance_turn(&self) {
self.turn_counter.set(self.turn_counter.get() + 1);
}
/// Get the current turn number.
pub fn current_turn(&self) -> u64 {
self.turn_counter.get()
}
/// Notify the cache that a context compaction has occurred.
///
/// This marks ALL existing dedup refs as stale, forcing the next read
/// of any cached content to re-send the full compressed version instead
/// of a dangling `§ref:...§` token.
///
/// Call this when:
/// - The harness signals a compaction event (PreCompact hook)
/// - A session is resumed after being idle
/// - The turn counter exceeds a threshold
pub fn notify_compaction(&self) {
self.ref_tracker.borrow_mut().clear();
}
/// Check if a dedup ref for the given hash is still fresh (likely still
/// in the LLM's context window).
fn is_ref_fresh(&self, hash: &str) -> bool {
let tracker = self.ref_tracker.borrow();
if let Some(entry) = tracker.get(hash) {
let age = self.turn_counter.get().saturating_sub(entry.last_sent_turn);
age < self.max_ref_age_turns
} else {
false
}
}
/// Record that a dedup ref was sent for the given hash at the current turn.
fn record_ref_sent(&self, hash: &str) {
self.ref_tracker.borrow_mut().insert(
hash.to_string(),
RefEntry {
last_sent_turn: self.turn_counter.get(),
},
);
}
/// Look up `content` in the cache with compaction awareness.
///
/// - On exact dedup with fresh ref: return `CacheResult::Dedup` (~13 tokens).
/// - On exact dedup with stale ref: re-compress and return `CacheResult::Fresh`
/// (the original content may have been compacted out of the LLM's context).
/// - On near-duplicate: return `CacheResult::Delta` with a compact diff.
/// - On cache miss: compress via `pipeline`, persist, return `CacheResult::Fresh`.
pub fn get_or_compress(
&self,
_path: &Path,
content: &[u8],
pipeline: &CompressionPipeline,
) -> Result<CacheResult> {
let hash = Self::sha256_hex(content);
// Exact match — check if the ref is still fresh
if self.store.get_cache_entry(&hash)?.is_some() {
if self.is_ref_fresh(&hash) {
// Ref is fresh — the LLM likely still has the original in context
let hash_prefix = &hash[..16];
let inline_ref = format!("§ref:{hash_prefix}§");
// Update the sent timestamp
self.record_ref_sent(&hash);
return Ok(CacheResult::Dedup {
inline_ref,
token_cost: 13,
});
} else {
// Ref is stale — re-send the full compressed content.
// The original may have been compacted out of the LLM's context.
let text = String::from_utf8_lossy(content).into_owned();
let ctx = SessionContext {
session_id: "cache".to_string(),
};
let preset = Preset::default();
let compressed = pipeline.compress(&text, &ctx, &preset)?;
// Record that we re-sent this content
self.record_ref_sent(&hash);
return Ok(CacheResult::Fresh { output: compressed });
}
}
// Near-duplicate check: compare against recent cache entries
let text = String::from_utf8_lossy(content).into_owned();
if let Some(delta_result) = self.try_delta_encode(&text)? {
// Store the new content in cache for future exact matches
let ctx = SessionContext {
session_id: "cache".to_string(),
};
let preset = Preset::default();
let compressed = pipeline.compress(&text, &ctx, &preset)?;
self.store.save_cache_entry(&hash, &compressed)?;
self.record_ref_sent(&hash);
let token_cost = (delta_result.delta_text.len() / 4) as u32;
return Ok(CacheResult::Delta {
delta_text: delta_result.delta_text,
token_cost: token_cost.max(5),
similarity: delta_result.similarity,
});
}
let ctx = SessionContext {
session_id: "cache".to_string(),
};
let preset = Preset::default();
let compressed = pipeline.compress(&text, &ctx, &preset)?;
self.store.save_cache_entry(&hash, &compressed)?;
// Record that this content was sent at the current turn
self.record_ref_sent(&hash);
Ok(CacheResult::Fresh { output: compressed })
}
/// Try to delta-encode content against recent cache entries.
/// Returns Some(DeltaResult) if a near-duplicate was found.
fn try_delta_encode(
&self,
new_content: &str,
) -> Result<Option<crate::delta_encoder::DeltaResult>> {
let entries = self.store.list_cache_entries_lru()?;
// Check the most recent entries (up to 10) for near-duplicates
let check_count = entries.len().min(10);
for (hash, _) in entries.iter().rev().take(check_count) {
if let Some(cached) = self.store.get_cache_entry(hash)? {
let hash_prefix = &hash[..hash.len().min(16)];
if let Ok(Some(delta)) =
self.delta_encoder
.encode(&cached.data, new_content, hash_prefix)
{
// Only use delta if it's actually smaller than the full content
if delta.delta_text.len() < new_content.len() {
return Ok(Some(delta));
}
}
}
}
Ok(None)
}
/// Check if `content` is already in the persistent cache (dedup lookup only).
///
/// Returns `Some(inline_ref)` if cached AND the ref is still fresh,
/// `None` if the content is not cached or the ref is stale.
pub fn check_dedup(&self, content: &[u8]) -> Result<Option<String>> {
let hash = Self::sha256_hex(content);
if self.store.get_cache_entry(&hash)?.is_some() {
if self.is_ref_fresh(&hash) {
let hash_prefix = &hash[..16];
self.record_ref_sent(&hash);
Ok(Some(format!("§ref:{hash_prefix}§")))
} else {
// Cache hit but ref is stale — don't return a dangling ref
Ok(None)
}
} else {
Ok(None)
}
}
/// Store a compressed result in the persistent cache, keyed by the
/// SHA-256 hash of the original content.
///
/// Also records the ref as sent at the current turn for compaction tracking.
pub fn store_compressed(
&self,
original_content: &[u8],
compressed: &CompressedContent,
) -> Result<()> {
let hash = Self::sha256_hex(original_content);
self.store.save_cache_entry(&hash, compressed)?;
self.record_ref_sent(&hash);
Ok(())
}
/// Invalidate the cache entry for `path` if its current content is known.
///
/// Reads the file at `path`, computes its hash, and removes the matching
/// entry from the store. If the file does not exist the call is a no-op.
pub fn invalidate(&self, path: &Path) -> Result<()> {
if !path.exists() {
return Ok(());
}
let bytes = std::fs::read(path)?;
let hash = Self::sha256_hex(&bytes);
self.store.delete_cache_entry(&hash)?;
Ok(())
}
/// Evict least-recently-used entries until total cache size is at or below
/// `max_size_bytes`.
///
/// Returns the number of bytes freed.
pub fn evict_lru(&self) -> Result<u64> {
let entries = self.store.list_cache_entries_lru()?;
// Compute current total size.
let total: u64 = entries.iter().map(|(_, sz)| sz).sum();
if total <= self.max_size_bytes {
return Ok(0);
}
let mut freed: u64 = 0;
let mut remaining = total;
for (hash, size) in &entries {
if remaining <= self.max_size_bytes {
break;
}
self.store.delete_cache_entry(hash)?;
freed += size;
remaining -= size;
}
Ok(freed)
}
}
// ── Tests ─────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use crate::preset::{
BudgetConfig, CollapseArraysConfig, CompressionConfig, CondenseConfig,
CustomTransformsConfig, ModelConfig, PresetMeta, StripNullsConfig, TerseModeConfig,
ToolSelectionConfig, TruncateStringsConfig,
};
use crate::session_store::SessionStore;
fn in_memory_store() -> (SessionStore, tempfile::TempDir) {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test.db");
let store = SessionStore::open_or_create(&path).unwrap();
(store, dir)
}
fn test_preset() -> Preset {
Preset {
preset: PresetMeta {
name: "test".into(),
version: "1.0".into(),
description: String::new(),
},
compression: CompressionConfig {
stages: vec![],
keep_fields: None,
strip_fields: None,
condense: Some(CondenseConfig {
enabled: true,
max_repeated_lines: 3,
}),
git_diff_fold: None,
strip_nulls: Some(StripNullsConfig { enabled: true }),
flatten: None,
truncate_strings: Some(TruncateStringsConfig {
enabled: true,
max_length: 500,
}),
collapse_arrays: Some(CollapseArraysConfig {
enabled: true,
max_items: 5,
summary_template: "... and {remaining} more items".into(),
}),
custom_transforms: Some(CustomTransformsConfig { enabled: true }),
},
tool_selection: ToolSelectionConfig {
max_tools: 5,
similarity_threshold: 0.7,
default_tools: vec![],
},
budget: BudgetConfig {
warning_threshold: 0.70,
ceiling_threshold: 0.85,
default_window_size: 200_000,
agents: Default::default(),
},
terse_mode: TerseModeConfig {
enabled: false,
level: crate::preset::TerseLevel::Moderate,
},
model: ModelConfig {
family: "anthropic".into(),
primary: "claude-sonnet-4-20250514".into(),
local: String::new(),
complexity_threshold: 0.4,
pricing: None,
},
}
}
fn make_pipeline() -> CompressionPipeline {
CompressionPipeline::new(&test_preset())
}
#[test]
fn first_read_is_miss() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
let content = b"hello world";
let result = cm
.get_or_compress(Path::new("file.txt"), content, &pipeline)
.unwrap();
assert!(matches!(result, CacheResult::Fresh { .. }));
}
#[test]
fn second_read_is_hit() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
let content = b"hello world";
let path = Path::new("file.txt");
// First read — miss
cm.get_or_compress(path, content, &pipeline).unwrap();
// Second read — hit
let result = cm.get_or_compress(path, content, &pipeline).unwrap();
match result {
CacheResult::Dedup {
inline_ref,
token_cost,
} => {
assert!(inline_ref.starts_with("§ref:"));
assert!(inline_ref.ends_with('§'));
assert_eq!(token_cost, 13);
}
CacheResult::Fresh { .. } | CacheResult::Delta { .. } => panic!("expected cache hit"),
}
}
#[test]
fn different_content_is_miss() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
let path = Path::new("file.txt");
cm.get_or_compress(path, b"content v1", &pipeline).unwrap();
let result = cm
.get_or_compress(path, b"content v2", &pipeline)
.unwrap();
assert!(matches!(result, CacheResult::Fresh { .. } | CacheResult::Delta { .. }));
}
#[test]
fn evict_lru_frees_bytes_when_over_limit() {
let (store, _dir) = in_memory_store();
// Very small limit so eviction triggers immediately.
let cm = CacheManager::new(store, 1);
let pipeline = make_pipeline();
let path = Path::new("f.txt");
// Populate cache with a few entries.
cm.get_or_compress(path, b"entry one", &pipeline).unwrap();
cm.get_or_compress(path, b"entry two", &pipeline).unwrap();
cm.get_or_compress(path, b"entry three", &pipeline).unwrap();
let freed = cm.evict_lru().unwrap();
assert!(freed > 0, "expected bytes to be freed");
}
#[test]
fn evict_lru_no_op_when_under_limit() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
cm.get_or_compress(Path::new("f.txt"), b"data", &pipeline)
.unwrap();
let freed = cm.evict_lru().unwrap();
assert_eq!(freed, 0);
}
#[test]
fn invalidate_removes_entry() {
let dir = tempfile::tempdir().unwrap();
let file_path = dir.path().join("test.txt");
std::fs::write(&file_path, b"some content").unwrap();
let store_path = dir.path().join("store.db");
let store = SessionStore::open_or_create(&store_path).unwrap();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
// Populate cache.
let content = std::fs::read(&file_path).unwrap();
cm.get_or_compress(&file_path, &content, &pipeline).unwrap();
// Verify it's a hit.
let hit = cm
.get_or_compress(&file_path, &content, &pipeline)
.unwrap();
assert!(matches!(hit, CacheResult::Dedup { .. }));
cm.invalidate(&file_path).unwrap();
let miss = cm
.get_or_compress(&file_path, &content, &pipeline)
.unwrap();
assert!(matches!(miss, CacheResult::Fresh { .. }));
}
#[test]
fn invalidate_nonexistent_path_is_noop() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
// Should not error.
cm.invalidate(Path::new("/nonexistent/path/file.txt"))
.unwrap();
}
// ── Compaction awareness tests ────────────────────────────────────────
#[test]
fn stale_ref_returns_fresh_instead_of_dedup() {
let (store, _dir) = in_memory_store();
// Set max_ref_age to 3 turns so refs go stale quickly
let cm = CacheManager::with_ref_age(store, u64::MAX, 3);
let pipeline = make_pipeline();
let content = b"hello world";
let path = Path::new("file.txt");
// First read — miss, records ref at turn 0
cm.get_or_compress(path, content, &pipeline).unwrap();
// Second read at turn 0 — ref is fresh (age 0 < 3)
let result = cm.get_or_compress(path, content, &pipeline).unwrap();
assert!(matches!(result, CacheResult::Dedup { .. }), "ref should be fresh at turn 0");
// Advance past the staleness threshold
for _ in 0..4 {
cm.advance_turn();
}
// Third read at turn 4 — ref is stale (age 4 >= 3), should re-send full content
let result = cm.get_or_compress(path, content, &pipeline).unwrap();
assert!(matches!(result, CacheResult::Fresh { .. }),
"stale ref should return Fresh, not Dedup");
}
#[test]
fn notify_compaction_invalidates_all_refs() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
let path = Path::new("file.txt");
// Populate cache
cm.get_or_compress(path, b"content A", &pipeline).unwrap();
cm.get_or_compress(path, b"content B", &pipeline).unwrap();
// Both should be dedup hits
assert!(matches!(
cm.get_or_compress(path, b"content A", &pipeline).unwrap(),
CacheResult::Dedup { .. }
));
assert!(matches!(
cm.get_or_compress(path, b"content B", &pipeline).unwrap(),
CacheResult::Dedup { .. }
));
// Simulate compaction
cm.notify_compaction();
// After compaction, refs are stale — should return Fresh
assert!(matches!(
cm.get_or_compress(path, b"content A", &pipeline).unwrap(),
CacheResult::Fresh { .. }
));
assert!(matches!(
cm.get_or_compress(path, b"content B", &pipeline).unwrap(),
CacheResult::Fresh { .. }
));
}
#[test]
fn ref_refreshed_after_resend() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::with_ref_age(store, u64::MAX, 3);
let pipeline = make_pipeline();
let content = b"hello world";
let path = Path::new("file.txt");
// First read — miss
cm.get_or_compress(path, content, &pipeline).unwrap();
// Advance past staleness
for _ in 0..4 {
cm.advance_turn();
}
// Read at turn 4 — stale, returns Fresh (re-sends content)
let result = cm.get_or_compress(path, content, &pipeline).unwrap();
assert!(matches!(result, CacheResult::Fresh { .. }));
// Immediately read again — ref was refreshed by the re-send, should be Dedup
let result = cm.get_or_compress(path, content, &pipeline).unwrap();
assert!(matches!(result, CacheResult::Dedup { .. }),
"ref should be fresh after re-send");
}
#[test]
fn check_dedup_returns_none_for_stale_ref() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::with_ref_age(store, u64::MAX, 2);
let pipeline = make_pipeline();
let content = b"test content";
let path = Path::new("file.txt");
// Populate cache
cm.get_or_compress(path, content, &pipeline).unwrap();
// check_dedup should return Some (ref is fresh)
assert!(cm.check_dedup(content).unwrap().is_some());
// Advance past staleness
for _ in 0..3 {
cm.advance_turn();
}
// check_dedup should return None (ref is stale)
assert!(cm.check_dedup(content).unwrap().is_none(),
"stale ref should not be returned by check_dedup");
}
#[test]
fn advance_turn_increments_counter() {
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
assert_eq!(cm.current_turn(), 0);
cm.advance_turn();
assert_eq!(cm.current_turn(), 1);
cm.advance_turn();
assert_eq!(cm.current_turn(), 2);
}
// ── Property-based tests ──────────────────────────────────────────────────
use proptest::prelude::*;
// ── Property 8: Cache deduplication ──────────────────────────────────────
// **Validates: Requirements 8.1, 8.2, 18.1, 18.2**
//
// For any file content, reading the file twice through the CacheManager
// (with no content change between reads) SHALL return a cache hit on the
// second read with a reference token of approximately 13 tokens.
proptest! {
/// **Validates: Requirements 8.1, 8.2, 18.1, 18.2**
///
/// For any file content, the second read through CacheManager SHALL be
/// a cache hit with tokens == 13.
#[test]
fn prop_cache_deduplication(
content in proptest::collection::vec(any::<u8>(), 1..=1000usize),
) {
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
let path = Path::new("file.txt");
// First read — must be a miss.
let first = cm.get_or_compress(path, &content, &pipeline).unwrap();
prop_assert!(
matches!(first, CacheResult::Fresh { .. }),
"first read should be a cache miss"
);
let second = cm.get_or_compress(path, &content, &pipeline).unwrap();
match second {
CacheResult::Dedup { inline_ref, token_cost } => {
prop_assert_eq!(
token_cost, 13,
"cache hit should report ~13 reference tokens"
);
prop_assert!(
inline_ref.starts_with("§ref:"),
"reference token should start with §ref:"
);
prop_assert!(
inline_ref.ends_with('§'),
"reference token should end with §"
);
}
CacheResult::Fresh { .. } | CacheResult::Delta { .. } => {
prop_assert!(false, "second read should be a cache hit, not a miss");
}
}
}
}
// ── Property 9: Cache invalidation on content change ─────────────────────
// **Validates: Requirements 8.3, 18.3**
//
// For any cached file, if the file content changes (producing a different
// SHA-256 hash), the CacheManager SHALL treat the next read as a cache miss
// and re-compress the updated content.
proptest! {
/// **Validates: Requirements 8.3, 18.3**
///
/// For any two distinct byte sequences, the first read of each is a
/// cache miss — content change always triggers re-compression.
#[test]
fn prop_cache_invalidation_on_content_change(
content_a in proptest::collection::vec(any::<u8>(), 1..=500usize),
content_b in proptest::collection::vec(any::<u8>(), 1..=500usize),
) {
// Only meaningful when the two contents differ (different hashes).
prop_assume!(content_a != content_b);
let (store, _dir) = in_memory_store();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
let path = Path::new("file.txt");
// Cache content_a.
let r1 = cm.get_or_compress(path, &content_a, &pipeline).unwrap();
prop_assert!(
matches!(r1, CacheResult::Fresh { .. }),
"first read of content_a should be a miss"
);
let r2 = cm.get_or_compress(path, &content_a, &pipeline).unwrap();
prop_assert!(
matches!(r2, CacheResult::Dedup { .. }),
"second read of content_a should be a hit"
);
let r3 = cm.get_or_compress(path, &content_b, &pipeline).unwrap();
prop_assert!(
matches!(r3, CacheResult::Fresh { .. } | CacheResult::Delta { .. }),
"read with changed content should be a cache miss or delta"
);
}
}
// ── Property 10: Cache LRU eviction ──────────────────────────────────────
// **Validates: Requirements 8.5**
//
// For any cache state where total size exceeds the configured maximum, the
// CacheManager SHALL evict entries in LRU order until total size is at or
// below the limit.
proptest! {
/// **Validates: Requirements 8.5**
///
/// After evict_lru, the total remaining cache size SHALL be at or below
/// max_size_bytes.
#[test]
fn prop_cache_lru_eviction(
// Generate 2-8 distinct content entries.
entries in proptest::collection::vec(
proptest::collection::vec(any::<u8>(), 10..=200usize),
2..=8usize,
),
) {
// Deduplicate entries so each has a unique hash.
let mut unique_entries: Vec<Vec<u8>> = Vec::new();
for e in &entries {
if !unique_entries.contains(e) {
unique_entries.push(e.clone());
}
}
prop_assume!(unique_entries.len() >= 2);
let (store, _dir) = in_memory_store();
// Use a very small limit (1 byte) to guarantee eviction is needed.
let cm = CacheManager::new(store, 1);
let pipeline = make_pipeline();
let path = Path::new("f.txt");
// Populate the cache.
for entry in &unique_entries {
cm.get_or_compress(path, entry, &pipeline).unwrap();
}
// Evict LRU entries.
let freed = cm.evict_lru().unwrap();
// Bytes freed must be > 0 since total > 1 byte.
prop_assert!(freed > 0, "evict_lru should free bytes when over limit");
// After eviction, total remaining size must be <= max_size_bytes (1).
// We verify by checking that evict_lru now returns 0 (nothing left to evict).
let freed_again = cm.evict_lru().unwrap();
prop_assert_eq!(
freed_again, 0,
"second evict_lru call should free 0 bytes (already at or below limit)"
);
}
}
// ── Property 34: Cache persistence across sessions ────────────────────────
// **Validates: Requirements 18.4**
//
// For any set of cache entries saved to the SessionStore, reloading the
// store (opening the same database file) SHALL produce the same cache
// entries, and a subsequent read with the same content hash SHALL return a
// cache hit.
proptest! {
/// **Validates: Requirements 18.4**
///
/// Cache entries written in one CacheManager instance SHALL survive a
/// store close/reopen. After a process restart, the ref tracker is
/// empty so the first read returns Fresh (re-sends content since we
/// can't know if the LLM still has it in context). The second read
/// in the same session returns Dedup.
#[test]
fn prop_cache_persistence_across_sessions(
content in proptest::collection::vec(any::<u8>(), 1..=500usize),
) {
use crate::session_store::SessionStore;
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("cache.db");
let path = Path::new("file.txt");
// Session 1: populate the cache.
{
let store = SessionStore::open_or_create(&db_path).unwrap();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
let r = cm.get_or_compress(path, &content, &pipeline).unwrap();
prop_assert!(
matches!(r, CacheResult::Fresh { .. }),
"first read should be a miss"
);
}
// Store is dropped here — connection closed, ref tracker lost.
// Session 2: reopen the same database file.
{
let store = SessionStore::open_or_create(&db_path).unwrap();
let cm = CacheManager::new(store, u64::MAX);
let pipeline = make_pipeline();
// First read in new session: cache entry exists in SQLite but
// ref tracker is empty (process restarted). Returns Fresh to
// re-send content since we can't know if the LLM still has it.
let r1 = cm.get_or_compress(path, &content, &pipeline).unwrap();
prop_assert!(
matches!(r1, CacheResult::Fresh { .. }),
"first read after restart should re-send (ref tracker empty)"
);
// Second read in same session: ref was recorded by the first
// read, so now it's a Dedup hit.
let r2 = cm.get_or_compress(path, &content, &pipeline).unwrap();
match r2 {
CacheResult::Dedup { token_cost, .. } => {
prop_assert_eq!(
token_cost, 13,
"second read in same session should be dedup hit"
);
}
CacheResult::Fresh { .. } | CacheResult::Delta { .. } => {
prop_assert!(
false,
"second read should be a dedup hit after re-send"
);
}
}
}
}
}
}