zccache 1.11.15

Local-first compiler cache for C/C++/Rust/Emscripten
Documentation
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
//! Cross-cutting behavior across save/load that touches more than a single
//! snapshot field: cache-hit recovery, context-key consistency, unicode
//! paths, double-roundtrip idempotency, overlapping contexts, all
//! `ContextState` variants, bit-flip detection, re-register after load,
//! GC behavior on save, concurrent save/load, large-graph stress.

use std::time::Duration;

use crate::core::NormalizedPath;
use crate::hash::ContentHash;
use tempfile::TempDir;

use super::super::super::context::CompileContext;
use super::super::super::graph::{CacheVerdict, ContextState, DepGraph};
use super::super::super::scanner::{IncludeDirective, IncludeKind, ScanResult};
use super::super::super::search_paths::IncludeSearchPaths;
use super::super::super::snapshot::{load_from_file, save_to_file, strings_to_paths, HEADER_SIZE};
use super::{always_fresh, dummy_hash, make_ctx, test_path};

#[test]
fn gc_trims_old_entries() {
    let graph = DepGraph::new();
    graph.register(make_ctx("/old.cpp"));
    assert_eq!(graph.stats().context_count, 1);

    // trim with zero duration removes all entries.
    let removed = graph.trim(Duration::ZERO);
    assert_eq!(removed, 1);
    assert_eq!(graph.stats().context_count, 0);
}

/// After save+load, a check() on the loaded graph must still return
/// Hit for previously-warm contexts. This is the most important
/// behavioral invariant.
#[test]
fn loaded_graph_serves_cache_hits() {
    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    let ctx = CompileContext {
        source_file: NormalizedPath::from("/src/main.cpp"),
        include_search: IncludeSearchPaths {
            user: vec![NormalizedPath::from("/include")],
            system: vec![NormalizedPath::from("/usr/include")],
            ..Default::default()
        },
        defines: vec!["NDEBUG".into()],
        flags: vec!["-O2".into(), "-std=c++17".into()],
        force_includes: vec![NormalizedPath::from("/pch.h")],
        unknown_flags: Vec::new(),
    };
    let key = graph.register(ctx);

    let hashes: std::collections::HashMap<NormalizedPath, ContentHash> = [
        (
            NormalizedPath::from("/src/main.cpp"),
            crate::hash::hash_bytes(b"src"),
        ),
        (
            NormalizedPath::from("/include/a.h"),
            crate::hash::hash_bytes(b"a"),
        ),
        (
            NormalizedPath::from("/pch.h"),
            crate::hash::hash_bytes(b"pch"),
        ),
    ]
    .into_iter()
    .collect();

    graph.update(
        &key,
        ScanResult {
            resolved: vec![NormalizedPath::from("/include/a.h")],
            unresolved: Vec::new(),
            has_computed: false,
        },
        |p| hashes.get(&NormalizedPath::new(p)).copied(),
    );

    // Verify original graph serves hits.
    let verdict = graph.check(&key, always_fresh, |p| {
        hashes.get(&NormalizedPath::new(p)).copied()
    });
    assert!(
        matches!(verdict, CacheVerdict::Hit { .. }),
        "original graph should hit, got {verdict:?}"
    );

    // Save, load, check again.
    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();

    let verdict = loaded.check(&key, always_fresh, |p| {
        hashes.get(&NormalizedPath::new(p)).copied()
    });
    assert!(
        matches!(verdict, CacheVerdict::Hit { .. }),
        "loaded graph should still serve hit, got {verdict:?}"
    );
}

/// The stored context key must match the key recomputed from the
/// loaded CompileContext. If lossy PathBuf→String→NormalizedPath conversion
/// corrupts paths, the key will diverge and lookups will silently fail.
#[test]
fn context_key_consistent_after_roundtrip() {
    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    let ctx = CompileContext {
        source_file: NormalizedPath::from("/src/main.cpp"),
        include_search: IncludeSearchPaths {
            iquote: vec![NormalizedPath::from("/iquote/dir")],
            user: vec![NormalizedPath::from("/user/dir")],
            system: vec![NormalizedPath::from("/system/dir")],
            after: vec![NormalizedPath::from("/after/dir")],
        },
        defines: vec!["FOO=1".into(), "BAR=2".into()],
        flags: vec!["-Wall".into()],
        force_includes: vec![NormalizedPath::from("/fi/pch.h")],
        unknown_flags: vec!["--custom".into()],
    };
    let original_key = ctx.context_key();
    graph.register(ctx);

    let hash = crate::hash::hash_bytes(b"x");
    let hashes: std::collections::HashMap<NormalizedPath, ContentHash> = [
        (NormalizedPath::from("/src/main.cpp"), hash),
        (NormalizedPath::from("/fi/pch.h"), hash),
    ]
    .into_iter()
    .collect();
    graph.update(
        &original_key,
        ScanResult {
            resolved: Vec::new(),
            unresolved: Vec::new(),
            has_computed: false,
        },
        |p| hashes.get(&NormalizedPath::new(p)).copied(),
    );

    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();

    // The loaded graph should find the entry by the original key.
    assert_eq!(
        loaded.get_state(&original_key),
        Some(ContextState::Warm),
        "loaded graph must find entry by original context key"
    );

    // Extract the loaded CompileContext and recompute its key.
    let snap = loaded.to_snapshot();
    assert_eq!(snap.contexts.len(), 1);
    let loaded_ctx = CompileContext {
        source_file: NormalizedPath::from(&snap.contexts[0].source_file),
        include_search: IncludeSearchPaths {
            iquote: strings_to_paths(snap.contexts[0].iquote.clone()),
            user: strings_to_paths(snap.contexts[0].user.clone()),
            system: strings_to_paths(snap.contexts[0].system.clone()),
            after: strings_to_paths(snap.contexts[0].after.clone()),
        },
        defines: snap.contexts[0].defines.clone(),
        flags: snap.contexts[0].flags.clone(),
        force_includes: strings_to_paths(snap.contexts[0].force_includes.clone()),
        unknown_flags: snap.contexts[0].unknown_flags.clone(),
    };
    let recomputed_key = loaded_ctx.context_key();
    assert_eq!(
        *original_key.hash().as_bytes(),
        *recomputed_key.hash().as_bytes(),
        "context key recomputed from loaded context must match stored key"
    );
}

/// Unicode paths must roundtrip correctly — they are common on macOS
/// (NFC normalization) and Windows (wide chars).
#[test]
fn unicode_paths_roundtrip() {
    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    let unicode_source = "/src/日本語/main.cpp";
    let unicode_header = "/inc/données/header.h";
    let unicode_define = "NÄME=Ünïcödé";
    let emoji_path = "/inc/🎉/emoji.h";

    let ctx = CompileContext {
        source_file: NormalizedPath::from(unicode_source),
        include_search: IncludeSearchPaths {
            user: vec![
                NormalizedPath::from(unicode_header),
                NormalizedPath::from(emoji_path),
            ],
            ..Default::default()
        },
        defines: vec![unicode_define.into()],
        flags: Vec::new(),
        force_includes: Vec::new(),
        unknown_flags: Vec::new(),
    };
    let key = graph.register(ctx);
    let hash = crate::hash::hash_bytes(b"x");
    let hashes: std::collections::HashMap<NormalizedPath, ContentHash> =
        [(NormalizedPath::from(unicode_source), hash)]
            .into_iter()
            .collect();
    graph.update(
        &key,
        ScanResult {
            resolved: Vec::new(),
            unresolved: Vec::new(),
            has_computed: false,
        },
        |p| hashes.get(&NormalizedPath::new(p)).copied(),
    );

    // Also store file includes with unicode paths.
    graph.store_file_includes(
        NormalizedPath::from(unicode_source),
        vec![IncludeDirective {
            kind: IncludeKind::Quoted,
            path: unicode_header.into(),
            line: 1,
        }],
    );

    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();

    assert_eq!(loaded.get_state(&key), Some(ContextState::Warm));
    let includes = loaded
        .get_file_includes(&NormalizedPath::from(unicode_source))
        .unwrap();
    assert_eq!(includes[0].path, unicode_header);

    // Verify the context's include search paths survived.
    let snap = loaded.to_snapshot();
    assert_eq!(
        snap.contexts[0].source_file,
        NormalizedPath::from(unicode_source).display().to_string()
    );
    assert!(snap.contexts[0]
        .user
        .contains(&NormalizedPath::from(unicode_header).display().to_string()));
    assert!(snap.contexts[0]
        .user
        .contains(&NormalizedPath::from(emoji_path).display().to_string()));
    assert!(snap.contexts[0]
        .defines
        .contains(&unicode_define.to_string()));
}

/// Save→load→save→load must produce an identical graph. Tests for
/// any drift introduced by a single roundtrip (e.g., path
/// normalization, field reordering, floating precision).
#[test]
fn double_roundtrip_idempotent() {
    let dir = TempDir::new().unwrap();
    let path1 = dir.path().join("pass1.bin");
    let path2 = dir.path().join("pass2.bin");
    let graph = DepGraph::new();

    // Build a non-trivial graph.
    for i in 0..5 {
        let ctx = CompileContext {
            source_file: NormalizedPath::from(format!("/src/file{i}.cpp")),
            include_search: IncludeSearchPaths {
                user: vec![NormalizedPath::from(format!("/inc{i}"))],
                system: vec![NormalizedPath::from("/sys")],
                ..Default::default()
            },
            defines: vec![format!("VAR{i}=1")],
            flags: vec!["-O2".into()],
            force_includes: Vec::new(),
            unknown_flags: Vec::new(),
        };
        let key = graph.register(ctx);
        graph.update(
            &key,
            ScanResult {
                resolved: vec![NormalizedPath::from(format!("/inc{i}/h.h"))],
                unresolved: vec![format!("missing{i}.h")],
                has_computed: i == 0, // one with computed includes
            },
            dummy_hash,
        );
        graph.store_file_includes(
            NormalizedPath::from(format!("/src/file{i}.cpp")),
            vec![IncludeDirective {
                kind: IncludeKind::Quoted,
                path: format!("h{i}.h"),
                line: i as u32 + 1,
            }],
        );
    }

    // First roundtrip.
    save_to_file(&graph, &path1).unwrap();
    let loaded1 = load_from_file(&path1).unwrap();

    // Second roundtrip.
    save_to_file(&loaded1, &path2).unwrap();
    let loaded2 = load_from_file(&path2).unwrap();

    // Compare snapshots field-by-field.
    let snap1 = loaded1.to_snapshot();
    let snap2 = loaded2.to_snapshot();
    assert_eq!(snap1.files.len(), snap2.files.len(), "file count mismatch");
    assert_eq!(
        snap1.contexts.len(),
        snap2.contexts.len(),
        "context count mismatch"
    );

    // Sort by path for deterministic comparison (DashMap order is random).
    let mut files1: Vec<_> = snap1.files.iter().map(|f| &f.path).collect();
    let mut files2: Vec<_> = snap2.files.iter().map(|f| &f.path).collect();
    files1.sort();
    files2.sort();
    assert_eq!(files1, files2, "file paths differ after double roundtrip");

    let mut keys1: Vec<_> = snap1.contexts.iter().map(|c| c.context_key).collect();
    let mut keys2: Vec<_> = snap2.contexts.iter().map(|c| c.context_key).collect();
    keys1.sort();
    keys2.sort();
    assert_eq!(keys1, keys2, "context keys differ after double roundtrip");
}

/// Multiple contexts referencing overlapping resolved includes.
/// All must survive independently.
#[test]
fn overlapping_contexts_roundtrip() {
    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    let shared_header = NormalizedPath::from("/inc/shared.h");

    // Two contexts that share the same header.
    let ctx_a = CompileContext {
        source_file: NormalizedPath::from("/src/a.cpp"),
        include_search: IncludeSearchPaths {
            user: vec![NormalizedPath::from("/inc")],
            ..Default::default()
        },
        defines: vec!["A=1".into()],
        flags: Vec::new(),
        force_includes: Vec::new(),
        unknown_flags: Vec::new(),
    };
    let ctx_b = CompileContext {
        source_file: NormalizedPath::from("/src/b.cpp"),
        include_search: IncludeSearchPaths {
            user: vec![NormalizedPath::from("/inc")],
            ..Default::default()
        },
        defines: vec!["B=1".into()],
        flags: Vec::new(),
        force_includes: Vec::new(),
        unknown_flags: Vec::new(),
    };

    let key_a = graph.register(ctx_a);
    let key_b = graph.register(ctx_b);

    let hashes: std::collections::HashMap<NormalizedPath, ContentHash> = [
        (
            NormalizedPath::from("/src/a.cpp"),
            crate::hash::hash_bytes(b"a"),
        ),
        (
            NormalizedPath::from("/src/b.cpp"),
            crate::hash::hash_bytes(b"b"),
        ),
        (shared_header.clone(), crate::hash::hash_bytes(b"shared")),
    ]
    .into_iter()
    .collect();

    graph.update(
        &key_a,
        ScanResult {
            resolved: vec![shared_header.clone()],
            unresolved: Vec::new(),
            has_computed: false,
        },
        |p| hashes.get(&NormalizedPath::new(p)).copied(),
    );
    graph.update(
        &key_b,
        ScanResult {
            resolved: vec![shared_header.clone()],
            unresolved: Vec::new(),
            has_computed: false,
        },
        |p| hashes.get(&NormalizedPath::new(p)).copied(),
    );

    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();

    assert_eq!(loaded.stats().context_count, 2);
    assert_eq!(loaded.get_state(&key_a), Some(ContextState::Warm));
    assert_eq!(loaded.get_state(&key_b), Some(ContextState::Warm));

    // Both should serve hits.
    let verdict_a = loaded.check(&key_a, always_fresh, |p| {
        hashes.get(&NormalizedPath::new(p)).copied()
    });
    let verdict_b = loaded.check(&key_b, always_fresh, |p| {
        hashes.get(&NormalizedPath::new(p)).copied()
    });
    assert!(matches!(verdict_a, CacheVerdict::Hit { .. }));
    assert!(matches!(verdict_b, CacheVerdict::Hit { .. }));

    // And they must have different artifact keys (different source files).
    match (verdict_a, verdict_b) {
        (CacheVerdict::Hit { artifact_key: ak_a }, CacheVerdict::Hit { artifact_key: ak_b }) => {
            assert_ne!(
                ak_a.hash().as_bytes(),
                ak_b.hash().as_bytes(),
                "different contexts should have different artifact keys"
            );
        }
        _ => unreachable!(),
    }
}

/// All three ContextState variants must survive roundtrip faithfully.
#[test]
fn all_states_roundtrip() {
    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    // Cold context: just register, never update.
    let cold_key = graph.register(make_ctx("/src/cold.cpp"));
    assert_eq!(graph.get_state(&cold_key), Some(ContextState::Cold));

    // Warm context: register + update.
    let warm_key = graph.register(make_ctx("/src/warm.cpp"));
    graph.update(
        &warm_key,
        ScanResult {
            resolved: Vec::new(),
            unresolved: Vec::new(),
            has_computed: false,
        },
        dummy_hash,
    );
    assert_eq!(graph.get_state(&warm_key), Some(ContextState::Warm));

    // Stale context: register + update + mark stale.
    let stale_key = graph.register(make_ctx("/src/stale.cpp"));
    graph.update(
        &stale_key,
        ScanResult {
            resolved: Vec::new(),
            unresolved: Vec::new(),
            has_computed: false,
        },
        dummy_hash,
    );
    graph.mark_stale(&stale_key);
    assert_eq!(graph.get_state(&stale_key), Some(ContextState::Stale));

    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();

    assert_eq!(
        loaded.get_state(&cold_key),
        Some(ContextState::Cold),
        "Cold state not preserved"
    );
    assert_eq!(
        loaded.get_state(&warm_key),
        Some(ContextState::Warm),
        "Warm state not preserved"
    );
    assert_eq!(
        loaded.get_state(&stale_key),
        Some(ContextState::Stale),
        "Stale state not preserved"
    );
}

/// A bit-flip in the rkyv payload should be caught by validation.
#[test]
fn bit_flip_in_payload_detected() {
    use super::super::super::snapshot::SnapshotError;

    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    let key = graph.register(make_ctx("/src/a.cpp"));
    graph.update(
        &key,
        ScanResult {
            resolved: vec![NormalizedPath::from("/inc/b.h")],
            unresolved: Vec::new(),
            has_computed: false,
        },
        dummy_hash,
    );

    save_to_file(&graph, &path).unwrap();

    // Read the file, flip a byte in the payload, write it back.
    let mut data = std::fs::read(&path).unwrap();
    assert!(data.len() > HEADER_SIZE + 10);
    // Flip a bit in the middle of the payload.
    let flip_idx = HEADER_SIZE + (data.len() - HEADER_SIZE) / 2;
    data[flip_idx] ^= 0xFF;
    std::fs::write(&path, &data).unwrap();

    match load_from_file(&path) {
        Err(SnapshotError::Corrupt(_)) => {} // Expected
        Ok(_) => {
            // rkyv might not catch every bit-flip if it lands on
            // a valid-looking field. This is acceptable — we just
            // want to verify the validation path exists.
        }
        Err(other) => panic!("unexpected error: {other}"),
    }
}

/// Stress test: many contexts + files to verify no panics, no data
/// loss, and reasonable performance.
#[test]
fn large_graph_roundtrip() {
    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    let n_contexts = 200;
    let n_headers_per_ctx = 10;
    let mut keys = Vec::new();

    for i in 0..n_contexts {
        let ctx = CompileContext {
            source_file: NormalizedPath::from(format!("/src/file{i}.cpp")),
            include_search: IncludeSearchPaths {
                user: vec![NormalizedPath::from(format!("/inc{i}"))],
                ..Default::default()
            },
            defines: (0..5).map(|d| format!("DEF{d}={i}")).collect(),
            flags: vec!["-O2".into(), format!("-std=c++{}", 14 + (i % 4) * 3)],
            force_includes: Vec::new(),
            unknown_flags: Vec::new(),
        };
        let key = graph.register(ctx);

        let resolved: Vec<NormalizedPath> = (0..n_headers_per_ctx)
            .map(|h| NormalizedPath::from(format!("/inc{i}/header{h}.h")))
            .collect();
        graph.update(
            &key,
            ScanResult {
                resolved,
                unresolved: Vec::new(),
                has_computed: false,
            },
            dummy_hash,
        );
        keys.push(key);
    }

    assert_eq!(graph.stats().context_count, n_contexts);

    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();

    assert_eq!(loaded.stats().context_count, n_contexts);

    // Spot-check a few contexts.
    for key in keys.iter().take(10) {
        assert_eq!(loaded.get_state(key), Some(ContextState::Warm));
        let verdict = loaded.check(key, always_fresh, dummy_hash);
        assert!(
            matches!(verdict, CacheVerdict::Hit { .. }),
            "context should hit after load"
        );
    }
}

/// A new compile request for the same context after loading must
/// find the existing warm entry (not create a duplicate cold one).
#[test]
fn register_after_load_finds_existing() {
    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    let ctx = CompileContext {
        source_file: NormalizedPath::from("/src/main.cpp"),
        include_search: IncludeSearchPaths {
            user: vec![NormalizedPath::from("/inc")],
            ..Default::default()
        },
        defines: vec!["X=1".into()],
        flags: Vec::new(),
        force_includes: Vec::new(),
        unknown_flags: Vec::new(),
    };
    let original_key = graph.register(ctx.clone());
    graph.update(
        &original_key,
        ScanResult {
            resolved: vec![NormalizedPath::from("/inc/a.h")],
            unresolved: Vec::new(),
            has_computed: false,
        },
        dummy_hash,
    );

    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();

    // Simulate a new compile request with the identical context.
    let new_key = loaded.register(ctx);
    assert_eq!(
        original_key.hash().as_bytes(),
        new_key.hash().as_bytes(),
        "re-registering same context must produce same key"
    );
    // The existing warm entry must still be there (not overwritten).
    assert_eq!(
        loaded.get_state(&new_key),
        Some(ContextState::Warm),
        "re-register must not overwrite warm entry with cold"
    );
    assert_eq!(
        loaded.stats().context_count,
        1,
        "re-register must not create duplicate"
    );
}

/// GC during save must not discard recently-accessed warm contexts.
#[test]
fn gc_on_save_preserves_fresh_entries() {
    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = DepGraph::new();

    // Register and update 10 contexts.
    let mut keys = Vec::new();
    for i in 0..10 {
        let key = graph.register(make_ctx(&format!("/src/f{i}.cpp")));
        graph.update(
            &key,
            ScanResult {
                resolved: Vec::new(),
                unresolved: Vec::new(),
                has_computed: false,
            },
            dummy_hash,
        );
        keys.push(key);
    }

    // Save triggers GC (1-day TTL). All entries are fresh, so none should be trimmed.
    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();

    assert_eq!(
        loaded.stats().context_count,
        10,
        "GC should not trim fresh entries"
    );
    for key in &keys {
        assert_eq!(loaded.get_state(key), Some(ContextState::Warm));
    }
}

/// Concurrent save + load should not panic or corrupt (thread safety).
#[test]
fn concurrent_save_load() {
    use std::sync::Arc;

    let dir = TempDir::new().unwrap();
    let path = test_path(&dir);
    let graph = Arc::new(DepGraph::new());

    // Populate the graph.
    for i in 0..50 {
        let key = graph.register(make_ctx(&format!("/src/f{i}.cpp")));
        graph.update(
            &key,
            ScanResult {
                resolved: vec![NormalizedPath::from(format!("/inc/h{i}.h"))],
                unresolved: Vec::new(),
                has_computed: false,
            },
            dummy_hash,
        );
    }

    // Save once so the file exists.
    save_to_file(&graph, &path).unwrap();

    let mut handles = Vec::new();

    // Writer threads.
    for _ in 0..3 {
        let g = Arc::clone(&graph);
        let p = path.clone();
        handles.push(std::thread::spawn(move || {
            for _ in 0..5 {
                let _ = save_to_file(&g, &p);
            }
        }));
    }

    // Reader threads.
    for _ in 0..3 {
        let p = path.clone();
        handles.push(std::thread::spawn(move || {
            for _ in 0..5 {
                // May fail if file is being rewritten — that's OK.
                let _ = load_from_file(&p);
            }
        }));
    }

    // Mutator threads (add new entries while saving).
    for t in 0..2 {
        let g = Arc::clone(&graph);
        handles.push(std::thread::spawn(move || {
            for i in 0..20 {
                let key = g.register(make_ctx(&format!("/src/t{t}_new{i}.cpp")));
                g.update(
                    &key,
                    ScanResult {
                        resolved: Vec::new(),
                        unresolved: Vec::new(),
                        has_computed: false,
                    },
                    dummy_hash,
                );
            }
        }));
    }

    for h in handles {
        h.join().expect("thread panicked");
    }

    // Final save+load should be consistent.
    save_to_file(&graph, &path).unwrap();
    let loaded = load_from_file(&path).unwrap();
    assert!(loaded.stats().context_count >= 50);
}