File size: 28,455 Bytes
ee5b44c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import bz2
from dataclasses import fields
import gzip
import hashlib
import json
import multiprocessing
import os
from pathlib import Path
import time

import numpy as np
import pandas as pd
import pytest

from simulator.config import Config
import simulator.empirical_maps as empirical_maps_module
import simulator.map_cache as map_cache_module
from simulator.empirical_maps import (
    PiecewiseRateMap,
    load_empirical_maps,
    load_empirical_rate_table,
)
from simulator.map_cache import (
    ARRAY_FIELDS,
    CACHE_ENV_VAR,
    CACHE_SCHEMA,
    build_cache_fingerprint,
    load_or_build_rate_maps,
    map_cache_directory,
)


def _write_interval_map(path: Path) -> None:
    path.write_text(
        "chr2 0 100 1e-8\n"
        "chr1 0 50 2e-8\n"
        "chr1 50 120 3e-8\n"
        "chr2 100 200 4e-8\n",
        encoding="utf-8",
    )


def _raw_sha256(array: np.ndarray) -> str:
    return hashlib.sha256(np.asarray(array).tobytes(order="C")).hexdigest()


def _assert_maps_exact(
    expected: dict[str, PiecewiseRateMap],
    actual: dict[str, PiecewiseRateMap],
) -> None:
    assert list(actual) == list(expected)
    scalar_fields = [
        field.name
        for field in fields(PiecewiseRateMap)
        if field.name not in ARRAY_FIELDS
    ]
    for chrom in expected:
        expected_map = expected[chrom]
        actual_map = actual[chrom]
        for field in scalar_fields:
            expected_value = getattr(expected_map, field)
            actual_value = getattr(actual_map, field)
            assert type(actual_value) is type(expected_value)
            assert actual_value == expected_value
        for field in ARRAY_FIELDS:
            expected_array = np.asarray(getattr(expected_map, field))
            actual_array = np.asarray(getattr(actual_map, field))
            assert actual_array.dtype == expected_array.dtype
            assert actual_array.shape == expected_array.shape
            assert actual_array.nbytes == expected_array.nbytes
            assert _raw_sha256(actual_array) == _raw_sha256(expected_array)
            assert actual_array.tobytes(order="C") == expected_array.tobytes(order="C")


def _cache_entries(cache_dir: Path) -> list[Path]:
    return sorted(
        path
        for path in cache_dir.iterdir()
        if path.is_dir() and not path.name.startswith(".")
    )


def _concurrent_cache_worker(
    source_text: str,
    cache_dir_text: str,
    counter_text: str,
    gate,
    result_queue,
) -> None:
    os.environ[CACHE_ENV_VAR] = cache_dir_text
    source = Path(source_text)
    counter = Path(counter_text)

    def builder(snapshot: Path) -> dict[str, PiecewiseRateMap]:
        descriptor = os.open(counter, os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0o600)
        try:
            os.write(descriptor, f"{os.getpid()}\n".encode("ascii"))
        finally:
            os.close(descriptor)
        time.sleep(0.25)
        return load_empirical_rate_table(
            str(snapshot),
            baseline_rate=1e-8,
            map_kind="recombination",
            rate_unit="per_bp",
            scale_to_baseline=False,
        )

    gate.wait(timeout=10)
    maps = load_or_build_rate_maps(
        source_path=source,
        parse_parameters={
            "parser": "concurrency-test",
            "baseline_rate": 1e-8,
            "map_kind": "recombination",
            "rate_unit": "per_bp",
            "scale_to_baseline": False,
        },
        related_code_paths={"test_parser": Path(__file__)},
        builder=builder,
        map_class=PiecewiseRateMap,
    )
    first = next(iter(maps.values()))
    result_queue.put(
        (
            type(first.position).__name__,
            first.position.dtype.str,
            first.position.shape,
            _raw_sha256(first.position),
        )
    )


def test_fingerprint_covers_source_parameters_schema_code_and_versions(
    tmp_path: Path,
) -> None:
    source = tmp_path / "map.bed"
    parser_code = tmp_path / "parser.py"
    _write_interval_map(source)
    parser_code.write_text("PARSER_VERSION = 1\n", encoding="utf-8")
    parameters = {
        "baseline_rate": 1e-8,
        "map_kind": "recombination",
        "rate_unit": "per_bp",
        "scale_to_baseline": False,
    }

    fingerprint, payload = build_cache_fingerprint(
        source,
        parameters,
        {"parser": parser_code},
    )

    assert len(fingerprint) == 64
    assert payload["schema"] == CACHE_SCHEMA
    assert payload["source"] == {
        "name": source.name,
        "size": source.stat().st_size,
        "sha256": hashlib.sha256(source.read_bytes()).hexdigest(),
    }
    assert payload["parse_parameters"] == parameters
    implementation = payload["implementation"]
    assert implementation["numpy_version"] == np.__version__
    assert implementation["pandas_version"] == pd.__version__
    assert implementation["python_version"]
    assert implementation["platform"]
    assert implementation["machine"]
    assert implementation["byteorder"] in {"little", "big"}
    assert implementation["code_sha256"]["parser"] == hashlib.sha256(
        parser_code.read_bytes()
    ).hexdigest()
    assert "cache_schema_implementation" in implementation["code_sha256"]

    changed_parameters = {**parameters, "scale_to_baseline": True}
    changed_parameter_fingerprint, _ = build_cache_fingerprint(
        source,
        changed_parameters,
        {"parser": parser_code},
    )
    assert changed_parameter_fingerprint != fingerprint

    parser_code.write_text("PARSER_VERSION = 2\n", encoding="utf-8")
    changed_code_fingerprint, _ = build_cache_fingerprint(
        source,
        parameters,
        {"parser": parser_code},
    )
    assert changed_code_fingerprint != fingerprint

    source.write_text(source.read_text(encoding="utf-8") + "chr3 0 10 5e-8\n", encoding="utf-8")
    changed_source_fingerprint, _ = build_cache_fingerprint(
        source,
        parameters,
        {"parser": parser_code},
    )
    assert changed_source_fingerprint not in {fingerprint, changed_code_fingerprint}


def test_cached_load_is_mmap_and_strictly_matches_uncached_parser(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed"
    cache_dir = tmp_path / "cache"
    _write_interval_map(source)
    monkeypatch.setenv(CACHE_ENV_VAR, str(cache_dir))
    cfg = Config(
        recomb_map=str(source),
        mut_map="",
        baseline_rec=1e-8,
        recomb_map_unit="per_bp",
    )
    direct = load_empirical_rate_table(
        str(source),
        baseline_rate=cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )

    first = load_empirical_maps(cfg)
    _assert_maps_exact(direct, first.rec_maps)
    assert all(
        isinstance(getattr(rate_map, field), np.memmap)
        and not getattr(rate_map, field).flags.writeable
        for rate_map in first.rec_maps.values()
        for field in ARRAY_FIELDS
    )
    assert first.metadata["recombination"] == empirical_maps_module._summarize_maps(
        str(source),
        direct,
        cfg.recomb_map_unit,
    )
    assert first.metadata["recombination"]["source_size_bytes"] == source.stat().st_size
    assert first.metadata["recombination"]["source_sha256"] == hashlib.sha256(
        source.read_bytes()
    ).hexdigest()
    assert map_cache_directory() == cache_dir.resolve()

    entries = _cache_entries(cache_dir)
    assert len(entries) == 1
    manifest = json.loads((entries[0] / "manifest.json").read_text(encoding="utf-8"))
    assert manifest["schema"] == CACHE_SCHEMA
    assert manifest["fingerprint"] == entries[0].name
    assert len(manifest["maps"]) == len(direct)
    fingerprint_payload = manifest["fingerprint_payload"]
    assert fingerprint_payload["source"]["sha256"] == hashlib.sha256(source.read_bytes()).hexdigest()
    assert fingerprint_payload["parse_parameters"] == {
        "parser": "load_empirical_rate_table",
        "compression": "none",
        "baseline_rate": cfg.baseline_rec,
        "map_kind": "recombination",
        "rate_unit": cfg.recomb_map_unit,
        "scale_to_baseline": False,
    }
    assert set(fingerprint_payload["implementation"]["code_sha256"]) == {
        "cache_schema_implementation",
        "empirical_maps_parser",
        "rate_validation_config",
    }
    assert not list(cache_dir.glob(".*.tmp-*"))
    assert not list(cache_dir.glob(".*.source-*"))

    def cache_miss_is_a_failure(*_args, **_kwargs):
        raise AssertionError("a valid cache hit must not invoke the text parser")

    monkeypatch.setattr(
        empirical_maps_module,
        "load_empirical_rate_table",
        cache_miss_is_a_failure,
    )
    second = load_empirical_maps(cfg)
    _assert_maps_exact(direct, second.rec_maps)


def test_loaded_recombination_and_mutation_metadata_record_source_provenance(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    recombination = tmp_path / "recombination.bed"
    mutation = tmp_path / "mutation.bed"
    cache_dir = tmp_path / "cache"
    _write_interval_map(recombination)
    _write_interval_map(mutation)
    mutation.write_text(
        mutation.read_text(encoding="utf-8").replace("1e-8", "1.25e-8"),
        encoding="utf-8",
    )
    monkeypatch.setenv(CACHE_ENV_VAR, str(cache_dir))

    store = load_empirical_maps(
        Config(
            recomb_map=str(recombination),
            mut_map=str(mutation),
            recomb_map_unit="per_bp",
            mut_map_unit="per_bp",
        )
    )

    for kind, source in (
        ("recombination", recombination),
        ("mutation", mutation),
    ):
        metadata = store.metadata[kind]
        assert metadata["source_size_bytes"] == source.stat().st_size
        assert metadata["source_sha256"] == hashlib.sha256(source.read_bytes()).hexdigest()


def test_corrupt_array_or_scalar_metadata_is_safely_rebuilt(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed"
    cache_dir = tmp_path / "cache"
    _write_interval_map(source)
    monkeypatch.setenv(CACHE_ENV_VAR, str(cache_dir))
    cfg = Config(recomb_map=str(source), mut_map="", recomb_map_unit="per_bp")
    direct = load_empirical_rate_table(
        str(source),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )
    load_empirical_maps(cfg)
    entry = _cache_entries(cache_dir)[0]
    manifest_path = entry / "manifest.json"
    manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
    array_path = entry / manifest["maps"][0]["arrays"]["position"]["file"]
    array_path.write_bytes(b"not a valid npy")

    original_parser = empirical_maps_module.load_empirical_rate_table
    parser_calls = 0

    def counted_parser(*args, **kwargs):
        nonlocal parser_calls
        parser_calls += 1
        return original_parser(*args, **kwargs)

    monkeypatch.setattr(
        empirical_maps_module,
        "load_empirical_rate_table",
        counted_parser,
    )
    rebuilt_from_array_damage = load_empirical_maps(cfg)
    assert parser_calls == 1
    _assert_maps_exact(direct, rebuilt_from_array_damage.rec_maps)

    manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
    manifest["maps"][0]["scalars"]["source_format"] = "corrupt-but-valid-json"
    manifest_path.write_text(json.dumps(manifest), encoding="utf-8")

    rebuilt_from_metadata_damage = load_empirical_maps(cfg)
    assert parser_calls == 2
    _assert_maps_exact(direct, rebuilt_from_metadata_damage.rec_maps)
    repaired_manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
    assert repaired_manifest["maps"][0]["scalars"]["source_format"] == "bed_intervals"

    repaired_manifest["maps"][0]["scalars"]["mean_rate_before_scaling"] = float("nan")
    manifest_path.write_text(json.dumps(repaired_manifest), encoding="utf-8")

    rebuilt_from_nonfinite_json = load_empirical_maps(cfg)
    assert parser_calls == 3
    _assert_maps_exact(direct, rebuilt_from_nonfinite_json.rec_maps)
    repaired_manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
    assert np.isfinite(
        repaired_manifest["maps"][0]["scalars"]["mean_rate_before_scaling"]
    )
    assert not list(cache_dir.glob(".*.previous-*"))
    assert not list(cache_dir.glob(".*.tmp-*"))


def test_cache_hit_rechecks_source_before_returning(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed"
    cache_dir = tmp_path / "cache"
    _write_interval_map(source)
    monkeypatch.setenv(CACHE_ENV_VAR, str(cache_dir))
    cfg = Config(recomb_map=str(source), mut_map="")
    initial = load_empirical_maps(cfg)
    initial_rate_sha256 = _raw_sha256(initial.rec_maps["chr1"].rate)

    original_load_entry = map_cache_module._load_entry
    source_changed = False

    def load_entry_after_source_change(*args, **kwargs):
        nonlocal source_changed
        if not source_changed:
            source_changed = True
            source.write_text(
                source.read_text(encoding="utf-8").replace(
                    "chr1 50 120 3e-8",
                    "chr1 50 120 9e-8",
                ),
                encoding="utf-8",
            )
        return original_load_entry(*args, **kwargs)

    original_parser = empirical_maps_module.load_empirical_rate_table
    parser_calls = 0

    def counted_parser(*args, **kwargs):
        nonlocal parser_calls
        parser_calls += 1
        return original_parser(*args, **kwargs)

    monkeypatch.setattr(map_cache_module, "_load_entry", load_entry_after_source_change)
    monkeypatch.setattr(
        empirical_maps_module,
        "load_empirical_rate_table",
        counted_parser,
    )

    actual = load_empirical_maps(cfg)
    expected = original_parser(
        str(source),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )

    assert source_changed
    assert parser_calls == 1
    _assert_maps_exact(expected, actual.rec_maps)
    assert _raw_sha256(actual.rec_maps["chr1"].rate) != initial_rate_sha256
    assert len(_cache_entries(cache_dir)) == 2


def test_cache_build_rechecks_source_after_publishing(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed"
    cache_dir = tmp_path / "cache"
    _write_interval_map(source)
    monkeypatch.setenv(CACHE_ENV_VAR, str(cache_dir))
    cfg = Config(recomb_map=str(source), mut_map="")

    original_publish = map_cache_module._build_and_publish
    source_changed = False

    def publish_then_change_source(*args, **kwargs):
        nonlocal source_changed
        original_publish(*args, **kwargs)
        if not source_changed:
            source_changed = True
            source.write_text(
                source.read_text(encoding="utf-8").replace(
                    "chr1 50 120 3e-8",
                    "chr1 50 120 8e-8",
                ),
                encoding="utf-8",
            )

    original_parser = empirical_maps_module.load_empirical_rate_table
    parser_calls = 0

    def counted_parser(*args, **kwargs):
        nonlocal parser_calls
        parser_calls += 1
        return original_parser(*args, **kwargs)

    monkeypatch.setattr(
        map_cache_module,
        "_build_and_publish",
        publish_then_change_source,
    )
    monkeypatch.setattr(
        empirical_maps_module,
        "load_empirical_rate_table",
        counted_parser,
    )

    actual = load_empirical_maps(cfg)
    expected = original_parser(
        str(source),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )

    assert source_changed
    assert parser_calls == 2
    _assert_maps_exact(expected, actual.rec_maps)
    assert len(_cache_entries(cache_dir)) == 2


def test_unavailable_cache_falls_back_without_swallowing_parser_errors(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed"
    blocked_cache_path = tmp_path / "cache-is-a-file"
    _write_interval_map(source)
    blocked_cache_path.write_text("not a directory\n", encoding="utf-8")
    monkeypatch.setenv(CACHE_ENV_VAR, str(blocked_cache_path))
    cfg = Config(recomb_map=str(source), mut_map="")
    direct = load_empirical_rate_table(
        str(source),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )

    with pytest.warns(RuntimeWarning, match="falling back to direct text parsing"):
        fallback = load_empirical_maps(cfg)

    _assert_maps_exact(direct, fallback.rec_maps)
    assert all(
        not isinstance(getattr(rate_map, field), np.memmap)
        for rate_map in fallback.rec_maps.values()
        for field in ARRAY_FIELDS
    )
    assert blocked_cache_path.is_file()

    def cache_root_resolution_failure() -> Path:
        raise OSError("cache root resolution failure")

    monkeypatch.setattr(
        map_cache_module,
        "map_cache_directory",
        cache_root_resolution_failure,
    )
    with pytest.warns(RuntimeWarning, match="falling back to direct text parsing"):
        resolution_fallback = load_empirical_maps(cfg)
    _assert_maps_exact(direct, resolution_fallback.rec_maps)

    def scientific_parser_failure(*_args, **_kwargs):
        raise ValueError("scientific parser failure")

    monkeypatch.setattr(
        empirical_maps_module,
        "load_empirical_rate_table",
        scientific_parser_failure,
    )
    with pytest.warns(RuntimeWarning, match="falling back to direct text parsing"):
        with pytest.raises(ValueError, match="scientific parser failure"):
            load_empirical_maps(cfg)


def test_crash_residue_is_cleaned_and_missing_entry_is_rebuilt(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed"
    cache_dir = tmp_path / "cache"
    _write_interval_map(source)
    monkeypatch.setenv(CACHE_ENV_VAR, str(cache_dir))
    cfg = Config(recomb_map=str(source), mut_map="")
    direct = load_empirical_rate_table(
        str(source),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )
    load_empirical_maps(cfg)
    entry = _cache_entries(cache_dir)[0]
    fingerprint = entry.name
    previous = cache_dir / f".{fingerprint}.previous-crashed"
    staging = cache_dir / f".{fingerprint}.tmp-crashed"
    snapshot = cache_dir / f".{fingerprint}.source-crashed"
    entry.replace(previous)
    staging.mkdir()
    (staging / "partial.npy").write_bytes(b"partial")
    snapshot.write_bytes(b"partial source snapshot")

    original_parser = empirical_maps_module.load_empirical_rate_table
    parser_calls = 0

    def counted_parser(*args, **kwargs):
        nonlocal parser_calls
        parser_calls += 1
        return original_parser(*args, **kwargs)

    monkeypatch.setattr(
        empirical_maps_module,
        "load_empirical_rate_table",
        counted_parser,
    )
    rebuilt = load_empirical_maps(cfg)

    assert parser_calls == 1
    _assert_maps_exact(direct, rebuilt.rec_maps)
    assert (cache_dir / fingerprint).is_dir()
    assert not previous.exists()
    assert not staging.exists()
    assert not snapshot.exists()


def test_source_content_change_uses_a_new_cache_entry(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed"
    cache_dir = tmp_path / "cache"
    _write_interval_map(source)
    monkeypatch.setenv(CACHE_ENV_VAR, str(cache_dir))
    cfg = Config(recomb_map=str(source), mut_map="")

    before = load_empirical_maps(cfg)
    before_sha = _raw_sha256(before.rec_maps["chr1"].rate)
    source.write_text(
        source.read_text(encoding="utf-8").replace("chr1 50 120 3e-8", "chr1 50 120 7e-8"),
        encoding="utf-8",
    )
    direct_after = load_empirical_rate_table(
        str(source),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )
    after = load_empirical_maps(cfg)

    _assert_maps_exact(direct_after, after.rec_maps)
    assert _raw_sha256(after.rec_maps["chr1"].rate) != before_sha
    assert len(_cache_entries(cache_dir)) == 2


def test_cache_preserves_symlink_filename_compression_semantics(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    compressed_target = tmp_path / "compressed-map-data.bin"
    with gzip.open(compressed_target, "wt", encoding="utf-8") as handle:
        handle.write("chr1 0 100 1e-8\nchr1 100 200 2e-8\n")
    source_alias = tmp_path / "map.bed.gz"
    try:
        source_alias.symlink_to(compressed_target)
    except OSError as exc:
        pytest.skip(f"symbolic links are unavailable: {exc}")

    monkeypatch.setenv(CACHE_ENV_VAR, str(tmp_path / "cache"))
    cfg = Config(recomb_map=str(source_alias), mut_map="")
    direct = load_empirical_rate_table(
        str(source_alias),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )

    cached = load_empirical_maps(cfg)

    _assert_maps_exact(direct, cached.rec_maps)
    entry = _cache_entries(tmp_path / "cache")[0]
    manifest = json.loads((entry / "manifest.json").read_text(encoding="utf-8"))
    assert manifest["fingerprint_payload"]["parse_parameters"]["compression"] == "gzip"


def test_cache_preserves_plain_alias_to_misleading_gzip_filename(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    plain_target = tmp_path / "plain-text-map.gz"
    plain_target.write_text(
        "chr1 0 100 1e-8\nchr1 100 200 2e-8\n",
        encoding="utf-8",
    )
    source_alias = tmp_path / "map.bed"
    try:
        source_alias.symlink_to(plain_target)
    except OSError as exc:
        pytest.skip(f"symbolic links are unavailable: {exc}")

    monkeypatch.setenv(CACHE_ENV_VAR, str(tmp_path / "cache"))
    cfg = Config(recomb_map=str(source_alias), mut_map="")
    direct = load_empirical_rate_table(
        str(source_alias),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )

    cached = load_empirical_maps(cfg)

    _assert_maps_exact(direct, cached.rec_maps)
    entry = _cache_entries(tmp_path / "cache")[0]
    manifest = json.loads((entry / "manifest.json").read_text(encoding="utf-8"))
    assert manifest["fingerprint_payload"]["parse_parameters"]["compression"] == "none"


def test_cache_preserves_legacy_non_gzip_codec_inference(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed.bz2"
    with bz2.open(source, "wt", encoding="utf-8") as handle:
        handle.write("chr1 0 100 1e-8\nchr1 100 200 2e-8\n")

    monkeypatch.setenv(CACHE_ENV_VAR, str(tmp_path / "cache"))
    cfg = Config(recomb_map=str(source), mut_map="")
    direct = load_empirical_rate_table(
        str(source),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )

    cached = load_empirical_maps(cfg)

    _assert_maps_exact(direct, cached.rec_maps)
    entry = _cache_entries(tmp_path / "cache")[0]
    manifest = json.loads((entry / "manifest.json").read_text(encoding="utf-8"))
    assert manifest["fingerprint_payload"]["parse_parameters"]["compression"] == "bz2"


def test_symlink_retarget_during_build_cannot_poison_resolved_source_cache(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    target_a = tmp_path / "target-a.bed"
    target_b = tmp_path / "target-b.bed"
    source_alias = tmp_path / "map-alias.bed"
    target_a.write_text("chr1 0 100 1e-8\n", encoding="utf-8")
    target_b.write_text("chr1 0 100 9e-8\n", encoding="utf-8")
    try:
        source_alias.symlink_to(target_a)
    except OSError as exc:
        pytest.skip(f"symbolic links are unavailable: {exc}")

    monkeypatch.setenv(CACHE_ENV_VAR, str(tmp_path / "cache"))
    direct_a = load_empirical_rate_table(
        str(target_a),
        baseline_rate=1e-8,
        map_kind="recombination",
        rate_unit=Config().recomb_map_unit,
        scale_to_baseline=False,
    )
    direct_b = load_empirical_rate_table(
        str(target_b),
        baseline_rate=1e-8,
        map_kind="recombination",
        rate_unit=Config().recomb_map_unit,
        scale_to_baseline=False,
    )
    original_load_entry = map_cache_module._load_entry
    retargeted = False

    def load_entry_after_retarget(*args, **kwargs):
        nonlocal retargeted
        if not retargeted:
            retargeted = True
            source_alias.unlink()
            source_alias.symlink_to(target_b)
        return original_load_entry(*args, **kwargs)

    monkeypatch.setattr(
        map_cache_module,
        "_load_entry",
        load_entry_after_retarget,
    )
    through_alias = load_empirical_maps(
        Config(recomb_map=str(source_alias), mut_map="")
    )
    later_direct_target_hit = load_empirical_maps(
        Config(recomb_map=str(target_a), mut_map="")
    )

    assert retargeted
    _assert_maps_exact(direct_a, through_alias.rec_maps)
    _assert_maps_exact(direct_a, later_direct_target_hit.rec_maps)
    assert _raw_sha256(
        later_direct_target_hit.rec_maps["chr1"].rate
    ) != _raw_sha256(direct_b["chr1"].rate)


def test_builder_parses_verified_snapshot_during_aba_source_change(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    source = tmp_path / "map.bed"
    source_a = "chr1 0 100 1e-8\n"
    source_b = "chr1 0 100 9e-8\n"
    source.write_text(source_a, encoding="utf-8")
    monkeypatch.setenv(CACHE_ENV_VAR, str(tmp_path / "cache"))
    cfg = Config(recomb_map=str(source), mut_map="")
    direct_a = load_empirical_rate_table(
        str(source),
        cfg.baseline_rec,
        map_kind="recombination",
        rate_unit=cfg.recomb_map_unit,
        scale_to_baseline=False,
    )
    original_parser = empirical_maps_module.load_empirical_rate_table
    parser_calls = 0

    def parse_while_original_source_changes(path, *args, **kwargs):
        nonlocal parser_calls
        parser_calls += 1
        source.write_text(source_b, encoding="utf-8")
        try:
            return original_parser(path, *args, **kwargs)
        finally:
            source.write_text(source_a, encoding="utf-8")

    monkeypatch.setattr(
        empirical_maps_module,
        "load_empirical_rate_table",
        parse_while_original_source_changes,
    )
    first = load_empirical_maps(cfg)
    second = load_empirical_maps(cfg)

    assert parser_calls == 1
    _assert_maps_exact(direct_a, first.rec_maps)
    _assert_maps_exact(direct_a, second.rec_maps)


def test_concurrent_processes_build_one_atomic_entry(
    tmp_path: Path,
) -> None:
    source = tmp_path / "map.bed"
    cache_dir = tmp_path / "cache"
    counter = tmp_path / "builder-calls.txt"
    _write_interval_map(source)
    context = multiprocessing.get_context("spawn")
    gate = context.Event()
    result_queue = context.Queue()
    processes = [
        context.Process(
            target=_concurrent_cache_worker,
            args=(
                str(source),
                str(cache_dir),
                str(counter),
                gate,
                result_queue,
            ),
        )
        for _ in range(2)
    ]
    for process in processes:
        process.start()
    gate.set()
    for process in processes:
        process.join(timeout=20)
        if process.is_alive():
            process.terminate()
            process.join(timeout=5)

    assert [process.exitcode for process in processes] == [0, 0]
    results = [result_queue.get(timeout=2) for _ in processes]
    assert results[0] == results[1]
    assert results[0][0] == "memmap"
    assert len(counter.read_text(encoding="utf-8").splitlines()) == 1
    assert len(_cache_entries(cache_dir)) == 1
    assert not list(cache_dir.glob(".*.tmp-*"))
    assert not list(cache_dir.glob(".*.previous-*"))
    assert not list(cache_dir.glob(".*.source-*"))