File size: 27,134 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
from __future__ import annotations

from collections.abc import Callable, Mapping
from contextlib import contextmanager
import errno
import hashlib
import json
import os
from pathlib import Path
import platform
import shutil
import sys
import tempfile
import time
from typing import Any, TypeVar
import uuid
import warnings

import numpy as np
import pandas as pd


CACHE_SCHEMA = "dlcoalsim.empirical-map-cache.v1"
CACHE_ENV_VAR = "DLCOALSIM_CACHE_DIR"
MANIFEST_NAME = "manifest.json"
ARRAY_FIELDS = ("position", "rate")
SCALAR_FIELDS = (
    "chrom",
    "rate_unit",
    "rate_unit_before",
    "rate_unit_after",
    "mean_rate_before_scaling",
    "mean_rate_after_scaling",
    "scaling_method",
    "source_format",
)
_HASH_CHUNK_BYTES = 1024 * 1024
_MAX_MANIFEST_BYTES = 32 * 1024 * 1024
_MAX_INPUT_CHANGE_RETRIES = 3


RateMapT = TypeVar("RateMapT")


class MapCacheError(RuntimeError):
    """Base class for empirical-map cache failures."""


class MapCacheValidationError(MapCacheError):
    """Raised internally when an existing cache entry is incomplete or corrupt."""


class MapCacheStorageError(MapCacheError):
    """Raised internally when the optional cache storage cannot be used."""


class _CacheInputsChanged(MapCacheError):
    """Raised when a source or parser input changes while an entry is built."""


def map_cache_directory() -> Path:
    """Return the empirical-map cache root, honoring ``DLCOALSIM_CACHE_DIR``."""
    override = os.environ.get(CACHE_ENV_VAR)
    if override:
        return Path(override).expanduser().resolve(strict=False)

    if sys.platform == "darwin":
        base = Path.home() / "Library" / "Caches"
    elif os.name == "nt":
        local_app_data = os.environ.get("LOCALAPPDATA")
        base = Path(local_app_data).expanduser() if local_app_data else Path.home() / "AppData" / "Local"
    else:
        xdg_cache_home = os.environ.get("XDG_CACHE_HOME")
        base = Path(xdg_cache_home).expanduser() if xdg_cache_home else Path.home() / ".cache"
    return (base / "dlcoalsim" / "empirical-maps").resolve(strict=False)


def _canonical_json(value: object) -> str:
    return json.dumps(
        value,
        sort_keys=True,
        separators=(",", ":"),
        ensure_ascii=False,
        allow_nan=False,
    )


def _json_compatible(value: object) -> Any:
    """Cache-key values, sorted so that equal keys encode identically.

    ``dataset_state`` has a function of the same name and a different
    contract; see the note there.
    """

    if value is None or isinstance(value, (bool, int, str)):
        return value
    if isinstance(value, float):
        if not np.isfinite(value):
            raise ValueError("cache fingerprint values must be finite")
        return value
    if isinstance(value, np.generic):
        return _json_compatible(value.item())
    if isinstance(value, Path):
        return str(value)
    if isinstance(value, Mapping):
        return {
            str(key): _json_compatible(item)
            for key, item in sorted(value.items(), key=lambda pair: str(pair[0]))
        }
    if isinstance(value, (list, tuple)):
        return [_json_compatible(item) for item in value]
    raise TypeError(f"Unsupported cache fingerprint value: {type(value).__name__}")


def _sha256_file(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as handle:
        while chunk := handle.read(_HASH_CHUNK_BYTES):
            digest.update(chunk)
    return digest.hexdigest()


def _source_descriptor(path: Path) -> dict[str, object]:
    before = path.stat()
    sha256 = _sha256_file(path)
    after = path.stat()
    identity_before = (
        before.st_dev,
        before.st_ino,
        before.st_size,
        before.st_mtime_ns,
    )
    identity_after = (
        after.st_dev,
        after.st_ino,
        after.st_size,
        after.st_mtime_ns,
    )
    if identity_before != identity_after:
        raise _CacheInputsChanged(f"Empirical map changed while hashing: {path}")
    return {
        "name": path.name,
        "size": after.st_size,
        "sha256": sha256,
    }


def build_cache_fingerprint(
    source_path: str | Path,
    parse_parameters: Mapping[str, object],
    related_code_paths: Mapping[str, str | Path],
) -> tuple[str, dict[str, object]]:
    """Build a content-addressed fingerprint for one parsed empirical map."""
    source = Path(source_path).expanduser().resolve(strict=True)
    code_paths = {
        **{
            str(name): Path(path).expanduser().resolve(strict=True)
            for name, path in related_code_paths.items()
        },
        "cache_schema_implementation": Path(__file__).resolve(),
    }
    payload: dict[str, object] = {
        "schema": CACHE_SCHEMA,
        "source": _source_descriptor(source),
        "parse_parameters": _json_compatible(parse_parameters),
        "implementation": {
            "code_sha256": {
                name: _sha256_file(path)
                for name, path in sorted(code_paths.items())
            },
            "numpy_version": np.__version__,
            "pandas_version": pd.__version__,
            "python_version": platform.python_version(),
            "platform": sys.platform,
            "machine": platform.machine(),
            "byteorder": sys.byteorder,
        },
    }
    fingerprint = hashlib.sha256(_canonical_json(payload).encode("utf-8")).hexdigest()
    return fingerprint, payload


def _array_sha256(array: np.ndarray) -> str:
    contiguous = np.asarray(array)
    if not contiguous.flags.c_contiguous:
        contiguous = np.ascontiguousarray(contiguous)
    view = memoryview(contiguous).cast("B")
    digest = hashlib.sha256()
    for offset in range(0, len(view), _HASH_CHUNK_BYTES):
        digest.update(view[offset : offset + _HASH_CHUNK_BYTES])
    return digest.hexdigest()


def _fsync_directory(path: Path) -> None:
    flags = os.O_RDONLY
    if hasattr(os, "O_DIRECTORY"):
        flags |= os.O_DIRECTORY
    try:
        descriptor = os.open(path, flags)
    except OSError:
        return
    try:
        os.fsync(descriptor)
    finally:
        os.close(descriptor)


def _atomic_write_npy(path: Path, array: np.ndarray) -> None:
    temporary = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
    try:
        with temporary.open("xb") as handle:
            np.save(handle, array, allow_pickle=False)
            handle.flush()
            os.fsync(handle.fileno())
        os.replace(temporary, path)
    finally:
        if temporary.exists():
            temporary.unlink()


def _atomic_write_json(path: Path, value: object) -> None:
    temporary = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
    encoded = (_canonical_json(value) + "\n").encode("utf-8")
    try:
        with temporary.open("xb") as handle:
            handle.write(encoded)
            handle.flush()
            os.fsync(handle.fileno())
        os.replace(temporary, path)
    finally:
        if temporary.exists():
            temporary.unlink()


def _remove_path(path: Path) -> None:
    if path.is_symlink() or path.is_file():
        path.unlink(missing_ok=True)
    elif path.exists():
        shutil.rmtree(path)


@contextmanager
def _exclusive_cache_lock(path: Path):
    try:
        path.parent.mkdir(parents=True, exist_ok=True)
        handle = path.open("a+b")
    except OSError as exc:
        raise MapCacheStorageError(f"Cannot open empirical-map cache lock: {path}") from exc
    try:
        if os.name == "nt":
            import msvcrt

            handle.seek(0, os.SEEK_END)
            if handle.tell() == 0:
                handle.write(b"\0")
                handle.flush()
            while True:
                handle.seek(0)
                try:
                    msvcrt.locking(handle.fileno(), msvcrt.LK_NBLCK, 1)
                    break
                except OSError as exc:
                    if exc.errno not in {errno.EACCES, errno.EDEADLK}:
                        raise
                    time.sleep(0.05)
        else:
            import fcntl

            fcntl.flock(handle.fileno(), fcntl.LOCK_EX)
    except OSError as exc:
        try:
            handle.close()
        except OSError:
            pass
        raise MapCacheStorageError(f"Cannot lock empirical-map cache: {path}") from exc

    try:
        yield
    finally:
        try:
            if os.name == "nt":
                import msvcrt

                handle.seek(0)
                msvcrt.locking(handle.fileno(), msvcrt.LK_UNLCK, 1)
            else:
                import fcntl

                fcntl.flock(handle.fileno(), fcntl.LOCK_UN)
        except OSError:
            pass
        try:
            handle.close()
        except OSError:
            pass


def _manifest_content_sha256(manifest: Mapping[str, object]) -> str:
    content = {key: value for key, value in manifest.items() if key != "content_sha256"}
    try:
        encoded = _canonical_json(content).encode("utf-8")
    except (TypeError, ValueError, RecursionError) as exc:
        raise MapCacheValidationError("Cache manifest contains invalid JSON values") from exc
    return hashlib.sha256(encoded).hexdigest()


def _reject_nonstandard_json_constant(value: str):
    raise ValueError(f"Non-standard JSON constant {value!r} is not allowed")


def _serialize_maps(
    staging: Path,
    maps: Mapping[str, RateMapT],
    fingerprint: str,
    fingerprint_payload: Mapping[str, object],
) -> None:
    records: list[dict[str, object]] = []
    for index, (chrom, rate_map) in enumerate(maps.items()):
        scalars = {field: _json_compatible(getattr(rate_map, field)) for field in SCALAR_FIELDS}
        if scalars["chrom"] != chrom:
            raise ValueError(
                f"Empirical map dictionary key {chrom!r} does not match map chromosome "
                f"{scalars['chrom']!r}"
            )

        arrays: dict[str, dict[str, object]] = {}
        for field in ARRAY_FIELDS:
            array = np.asarray(getattr(rate_map, field))
            if array.dtype.hasobject:
                raise TypeError("empirical map cache does not support object arrays")
            array = np.ascontiguousarray(array)
            filename = f"map-{index:05d}-{field}.npy"
            _atomic_write_npy(staging / filename, array)
            arrays[field] = {
                "file": filename,
                "dtype": array.dtype.str,
                "shape": list(array.shape),
                "nbytes": array.nbytes,
                "sha256": _array_sha256(array),
            }
        records.append(
            {
                "key": chrom,
                "scalars": scalars,
                "arrays": arrays,
            }
        )

    manifest: dict[str, object] = {
        "schema": CACHE_SCHEMA,
        "fingerprint": fingerprint,
        "fingerprint_payload": fingerprint_payload,
        "maps": records,
    }
    manifest["content_sha256"] = _manifest_content_sha256(manifest)
    _atomic_write_json(staging / MANIFEST_NAME, manifest)
    _fsync_directory(staging)


def _load_manifest(path: Path) -> dict[str, object]:
    try:
        if path.stat().st_size > _MAX_MANIFEST_BYTES:
            raise MapCacheValidationError(f"Cache manifest is unexpectedly large: {path}")
        value = json.loads(
            path.read_text(encoding="utf-8"),
            parse_constant=_reject_nonstandard_json_constant,
        )
    except MapCacheValidationError:
        raise
    except (OSError, UnicodeError, ValueError, RecursionError) as exc:
        raise MapCacheValidationError(f"Cannot read cache manifest: {path}") from exc
    if not isinstance(value, dict):
        raise MapCacheValidationError(f"Cache manifest is not an object: {path}")
    return value


def _validated_array(
    entry: Path,
    descriptor: object,
    expected_filename: str,
) -> np.memmap:
    if not isinstance(descriptor, dict):
        raise MapCacheValidationError("Cache array descriptor is not an object")
    expected_keys = {"file", "dtype", "shape", "nbytes", "sha256"}
    if set(descriptor) != expected_keys:
        raise MapCacheValidationError("Cache array descriptor has an unexpected schema")

    filename = descriptor["file"]
    if (
        not isinstance(filename, str)
        or Path(filename).name != filename
        or filename != expected_filename
    ):
        raise MapCacheValidationError("Cache array filename is unsafe")
    array_path = entry / filename
    if array_path.is_symlink():
        raise MapCacheValidationError(f"Cache array must not be a symlink: {array_path}")
    try:
        array = np.load(array_path, mmap_mode="r", allow_pickle=False)
    except (OSError, ValueError, EOFError) as exc:
        raise MapCacheValidationError(f"Cannot load cache array: {array_path}") from exc
    if not isinstance(array, np.memmap):
        raise MapCacheValidationError(f"Cache array did not load as a memory map: {array_path}")

    shape = descriptor["shape"]
    if (
        not isinstance(descriptor["dtype"], str)
        or not isinstance(shape, list)
        or not all(
            isinstance(size, int) and not isinstance(size, bool) and size >= 0
            for size in shape
        )
        or not isinstance(descriptor["nbytes"], int)
        or isinstance(descriptor["nbytes"], bool)
        or not isinstance(descriptor["sha256"], str)
        or len(descriptor["sha256"]) != 64
        or any(character not in "0123456789abcdef" for character in descriptor["sha256"])
    ):
        raise MapCacheValidationError(f"Cache array metadata is invalid: {array_path}")
    if array.dtype.str != descriptor["dtype"]:
        raise MapCacheValidationError(f"Cache array dtype mismatch: {array_path}")
    if list(array.shape) != shape:
        raise MapCacheValidationError(f"Cache array shape mismatch: {array_path}")
    if array.nbytes != descriptor["nbytes"]:
        raise MapCacheValidationError(f"Cache array byte count mismatch: {array_path}")
    if _array_sha256(array) != descriptor["sha256"]:
        raise MapCacheValidationError(f"Cache array hash mismatch: {array_path}")
    if array.flags.writeable:
        raise MapCacheValidationError(f"Cache array unexpectedly loaded writeable: {array_path}")
    return array


def _validated_scalars(value: object) -> dict[str, object]:
    if not isinstance(value, dict) or set(value) != set(SCALAR_FIELDS):
        raise MapCacheValidationError("Cache scalar metadata has an unexpected schema")
    for field in (
        "chrom",
        "rate_unit",
        "rate_unit_before",
        "rate_unit_after",
        "scaling_method",
        "source_format",
    ):
        if not isinstance(value[field], str):
            raise MapCacheValidationError(f"Cache scalar metadata field {field!r} is invalid")
    for field in ("mean_rate_before_scaling", "mean_rate_after_scaling"):
        scalar = value[field]
        if not isinstance(scalar, float) or not np.isfinite(scalar):
            raise MapCacheValidationError(f"Cache scalar metadata field {field!r} is invalid")
    return dict(value)


def _load_entry(
    entry: Path,
    expected_fingerprint: str,
    expected_payload: Mapping[str, object],
    map_class: type[RateMapT],
) -> dict[str, RateMapT]:
    if entry.is_symlink() or not entry.is_dir():
        raise MapCacheValidationError(f"Cache entry is missing or unsafe: {entry}")
    manifest = _load_manifest(entry / MANIFEST_NAME)
    expected_manifest_keys = {
        "schema",
        "fingerprint",
        "fingerprint_payload",
        "maps",
        "content_sha256",
    }
    if set(manifest) != expected_manifest_keys:
        raise MapCacheValidationError("Cache manifest has an unexpected schema")
    if manifest["schema"] != CACHE_SCHEMA:
        raise MapCacheValidationError("Cache schema does not match")
    if manifest["fingerprint"] != expected_fingerprint:
        raise MapCacheValidationError("Cache fingerprint does not match")
    if manifest["fingerprint_payload"] != expected_payload:
        raise MapCacheValidationError("Cache fingerprint payload does not match")
    if manifest["content_sha256"] != _manifest_content_sha256(manifest):
        raise MapCacheValidationError("Cache manifest content hash does not match")

    records = manifest["maps"]
    if not isinstance(records, list) or not records:
        raise MapCacheValidationError("Cache manifest has no empirical maps")
    maps: dict[str, RateMapT] = {}
    expected_array_files: set[str] = set()
    for index, record in enumerate(records):
        if not isinstance(record, dict) or set(record) != {"key", "scalars", "arrays"}:
            raise MapCacheValidationError("Cached map record has an unexpected schema")
        key = record["key"]
        if not isinstance(key, str) or key in maps:
            raise MapCacheValidationError("Cached map key is invalid or duplicated")
        scalars = _validated_scalars(record["scalars"])
        if scalars["chrom"] != key:
            raise MapCacheValidationError("Cached map key and chromosome do not match")
        arrays = record["arrays"]
        if not isinstance(arrays, dict) or set(arrays) != set(ARRAY_FIELDS):
            raise MapCacheValidationError("Cached map arrays have an unexpected schema")
        loaded_arrays: dict[str, np.memmap] = {}
        for field in ARRAY_FIELDS:
            expected_filename = f"map-{index:05d}-{field}.npy"
            loaded_arrays[field] = _validated_array(
                entry,
                arrays[field],
                expected_filename,
            )
            expected_array_files.add(expected_filename)
        try:
            maps[key] = map_class(**scalars, **loaded_arrays)
        except (TypeError, ValueError) as exc:
            raise MapCacheValidationError(f"Cannot restore cached empirical map {key!r}") from exc
    actual_array_files = {path.name for path in entry.glob("*.npy") if path.is_file()}
    if actual_array_files != expected_array_files:
        raise MapCacheValidationError("Cache entry contains missing or unexpected array files")
    return maps


def _publish_entry(staging: Path, entry: Path) -> None:
    previous: Path | None = None
    if os.path.lexists(entry):
        previous = entry.with_name(f".{entry.name}.previous-{uuid.uuid4().hex}")
        os.replace(entry, previous)
    try:
        os.replace(staging, entry)
        _fsync_directory(entry.parent)
    except BaseException:
        if previous is not None and not os.path.lexists(entry):
            os.replace(previous, entry)
        raise
    else:
        if previous is not None:
            try:
                _remove_path(previous)
            except OSError:
                pass


def _cleanup_stale_artifacts(cache_root: Path, fingerprint: str) -> None:
    try:
        stale_paths = [
            *cache_root.glob(f".{fingerprint}.tmp-*"),
            *cache_root.glob(f".{fingerprint}.previous-*"),
            *cache_root.glob(f".{fingerprint}.source-*"),
        ]
        for path in stale_paths:
            _remove_path(path)
    except OSError as exc:
        raise MapCacheStorageError(
            f"Cannot clean stale empirical-map cache artifacts in {cache_root}"
        ) from exc


@contextmanager
def _verified_source_snapshot(
    source: Path,
    cache_root: Path,
    fingerprint: str,
    expected_source: Mapping[str, object],
):
    """Yield a private source snapshot whose bytes match the fingerprint."""

    try:
        descriptor, snapshot_text = tempfile.mkstemp(
            prefix=f".{fingerprint}.source-",
            dir=cache_root,
        )
    except OSError as exc:
        raise MapCacheStorageError(
            f"Cannot create empirical-map source snapshot in {cache_root}"
        ) from exc

    snapshot = Path(snapshot_text)
    digest = hashlib.sha256()
    size = 0
    try:
        try:
            with source.open("rb") as source_handle, os.fdopen(
                descriptor,
                "wb",
            ) as snapshot_handle:
                descriptor = -1
                while chunk := source_handle.read(_HASH_CHUNK_BYTES):
                    digest.update(chunk)
                    size += len(chunk)
                    snapshot_handle.write(chunk)
                snapshot_handle.flush()
                os.fsync(snapshot_handle.fileno())
        except OSError as exc:
            raise MapCacheStorageError(
                f"Cannot create verified empirical-map snapshot for {source}"
            ) from exc

        if (
            expected_source.get("size") != size
            or expected_source.get("sha256") != digest.hexdigest()
        ):
            raise _CacheInputsChanged(
                f"Empirical map changed before its verified snapshot was parsed: {source}"
            )
        yield snapshot
    finally:
        if descriptor >= 0:
            try:
                os.close(descriptor)
            except OSError:
                pass
        try:
            snapshot.unlink(missing_ok=True)
        except OSError:
            pass


def _validated_builder_output(
    builder: Callable[[Path], Mapping[str, RateMapT]],
    source: Path,
) -> dict[str, RateMapT]:
    maps = builder(source)
    if not isinstance(maps, Mapping) or not maps:
        raise ValueError("Empirical map parser returned no maps")
    return dict(maps)


def _build_and_publish(
    *,
    entry: Path,
    fingerprint: str,
    fingerprint_payload: Mapping[str, object],
    maps: Mapping[str, RateMapT],
) -> None:
    staging = Path(
        tempfile.mkdtemp(
            prefix=f".{fingerprint}.tmp-",
            dir=entry.parent,
        )
    )
    try:
        _serialize_maps(staging, maps, fingerprint, fingerprint_payload)
        _publish_entry(staging, entry)
    finally:
        if os.path.lexists(staging):
            _remove_path(staging)


def _warn_cache_fallback(source: Path, exc: BaseException) -> None:
    try:
        warnings.warn(
            f"Empirical-map cache unavailable for {source}; falling back to direct text "
            f"parsing ({type(exc).__name__}: {exc})",
            RuntimeWarning,
            stacklevel=3,
        )
    except RuntimeWarning:
        # A cache remains an optimization even when the caller treats warnings as errors.
        pass


def load_or_build_rate_maps(
    *,
    source_path: str | Path,
    parse_parameters: Mapping[str, object],
    related_code_paths: Mapping[str, str | Path],
    builder: Callable[[Path], Mapping[str, RateMapT]],
    map_class: type[RateMapT],
) -> dict[str, RateMapT]:
    """Load verified read-only map arrays, rebuilding an invalid cache atomically."""
    source = Path(source_path).expanduser().resolve(strict=True)
    try:
        cache_root = map_cache_directory()
    except (OSError, RuntimeError, ValueError) as exc:
        _warn_cache_fallback(source, exc)
        return _validated_builder_output(builder, source)

    for attempt in range(_MAX_INPUT_CHANGE_RETRIES):
        try:
            fingerprint, payload = build_cache_fingerprint(
                source,
                parse_parameters,
                related_code_paths,
            )
        except _CacheInputsChanged:
            if attempt + 1 == _MAX_INPUT_CHANGE_RETRIES:
                raise RuntimeError(
                    f"Empirical map inputs kept changing while building cache: {source}"
                ) from None
            continue

        try:
            cache_root.mkdir(parents=True, exist_ok=True)
        except OSError as exc:
            _warn_cache_fallback(source, exc)
            return _validated_builder_output(builder, source)

        entry = cache_root / fingerprint
        lock = cache_root / f"{fingerprint}.lock"
        try:
            with _exclusive_cache_lock(lock):
                _cleanup_stale_artifacts(cache_root, fingerprint)
                try:
                    cached_maps = _load_entry(entry, fingerprint, payload, map_class)
                except MapCacheValidationError:
                    with _verified_source_snapshot(
                        source,
                        cache_root,
                        fingerprint,
                        payload["source"],
                    ) as snapshot:
                        parsed_maps = _validated_builder_output(builder, snapshot)
                    current_fingerprint, current_payload = build_cache_fingerprint(
                        source,
                        parse_parameters,
                        related_code_paths,
                    )
                    if (
                        current_fingerprint != fingerprint
                        or current_payload != payload
                    ):
                        raise _CacheInputsChanged(
                            f"Empirical map inputs changed while building cache entry "
                            f"for {source}"
                        )
                    try:
                        _build_and_publish(
                            entry=entry,
                            fingerprint=fingerprint,
                            fingerprint_payload=payload,
                            maps=parsed_maps,
                        )
                        published_maps = _load_entry(
                            entry,
                            fingerprint,
                            payload,
                            map_class,
                        )
                    except (OSError, MapCacheValidationError) as exc:
                        _warn_cache_fallback(source, exc)
                        return parsed_maps
                    final_fingerprint, final_payload = build_cache_fingerprint(
                        source,
                        parse_parameters,
                        related_code_paths,
                    )
                    if final_fingerprint != fingerprint or final_payload != payload:
                        raise _CacheInputsChanged(
                            f"Empirical map inputs changed while publishing cache "
                            f"entry for {source}"
                        )
                    return published_maps

                current_fingerprint, current_payload = build_cache_fingerprint(
                    source,
                    parse_parameters,
                    related_code_paths,
                )
                if current_fingerprint != fingerprint or current_payload != payload:
                    raise _CacheInputsChanged(
                        f"Empirical map inputs changed while loading cache entry for {source}"
                    )
                return cached_maps
        except MapCacheStorageError as exc:
            _warn_cache_fallback(source, exc)
            return _validated_builder_output(builder, source)
        except _CacheInputsChanged:
            if attempt + 1 == _MAX_INPUT_CHANGE_RETRIES:
                raise RuntimeError(
                    f"Empirical map inputs kept changing while building cache: {source}"
                ) from None
            continue

    raise AssertionError("unreachable")