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4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
#!/usr/bin/env python
"""Ingest one or more .osz archives into the compact v1 store.

End-to-end pipeline (PER CHUNK):
  1. Invoke the Rust ``osu_indexer`` binary on a chunk of archives;
     the indexer copies the raw archive to ``archives/sha256/``, hashes each
     member without writing extracted blob files, parses every .osu / .osb,
     resolves references, computes rosu-pp difficulty attributes, and emits one
     NDJSON line per row tagged with ``_table``.
  2. Group rows by ``_table``.
  3. Atomically write compact all-revisions Parquet files with
     ``archive_revisions`` deferred.
  4. Atomically commit ``archive_revisions`` as the chunk commit marker.
      Only after this row exists for an archive is the rest of its data
      guaranteed to be on disk.

Crash-safety guarantees:
  - Archive copies are tmp+rename atomic in the indexer.
  - Every parquet write is tmp+rename atomic via parquet_writer._atomic_write_parquet.
  - The chunk commit marker is ``archive_revisions``: if a chunk crashes
    before commit, ``--skip-already-ingested`` (default on) re-runs that
    chunk on the next invocation. Re-running the same chunk_batch_id is
    idempotent at every step.
  - Across chunks, only the in-progress chunk's work is at risk on Ctrl+C.
  - On startup we sweep orphan ``*.tmp.<pid>`` files older than
    ``--gc-tmp-files-age-min`` (default 60 minutes) so leaks don't grow
    unbounded.
"""

from __future__ import annotations

import argparse
import contextlib
import datetime as _dt
import hashlib
import json
import os
import re
import secrets
import subprocess
import sys
import tempfile
import time
from pathlib import Path

import pyarrow.dataset as ds
from tqdm.auto import tqdm

from parquet_writer import (
    commit_archive_revisions,
    group_rows_by_table,
    load_schemas,
    write_all_revisions_tables,
)


def _log(msg: str) -> None:
    """Print to stderr with ``flush=True``.

    Once stderr is not a TTY, Python switches it to block buffering, and
    progress lines for long-running steps stop appearing in real time.
    Forcing a flush per line keeps the log live without changing the host
    terminal's buffering policy.
    """
    print(msg, file=sys.stderr, flush=True)


def _default_batch_id() -> str:
    stamp = _dt.datetime.now(_dt.timezone.utc).strftime("%Y%m%dT%H%M%SZ")
    return f"{stamp}-{secrets.token_hex(3)}"


# Windows SetThreadExecutionState flags (winbase.h).
# Setting ES_CONTINUOUS | ES_SYSTEM_REQUIRED tells the OS to keep
# resetting the system idle timer until cleared — i.e. don't sleep.
# Display sleep / screen lock are NOT inhibited; only system sleep.
_ES_CONTINUOUS = 0x80000000
_ES_SYSTEM_REQUIRED = 0x00000001


def _process_alive(pid: int) -> bool:
    """Best-effort check whether process ``pid`` is still running.

    Used by :func:`repo_lock` to detect stale lock files left behind by a
    crashed previous run. False positives (treating a dead process as alive)
    only block the user with a clear "remove the lock" instruction; false
    negatives (treating a live process as dead) would let two writers race,
    which is the actual hazard — so the implementation errs on the side of
    "alive" whenever it cannot definitively prove the process has exited.
    """
    if pid <= 0:
        return False
    if sys.platform.startswith("win"):
        try:
            import ctypes  # noqa: PLC0415

            # use_last_error=True lets us read the Win32 error from
            # OpenProcess so we can distinguish "no such PID" (truly dead)
            # from "exists but unqueryable" (assume alive).
            kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
            # PROCESS_QUERY_LIMITED_INFORMATION (0x1000) is granted even
            # across user / integrity-level boundaries, so we don't false-
            # negative on processes we don't own.
            handle = kernel32.OpenProcess(0x1000, False, pid)
            if not handle:
                err = ctypes.get_last_error()
                # ERROR_INVALID_PARAMETER (87) is what the kernel returns
                # when there is no process with the given PID — that is the
                # only signal that lets us safely report "dead". Anything
                # else (ACCESS_DENIED = 5, etc.) means the process probably
                # exists but we can't query it; play it safe and assume
                # alive, because a false negative would let two writers race.
                return err != 87
            exit_code = ctypes.c_ulong(0)
            ok = kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code))
            kernel32.CloseHandle(handle)
            if not ok:
                return True  # uncertain — assume alive
            # STILL_ACTIVE = 259. Anything else = the process has exited.
            return exit_code.value == 259
        except (ImportError, OSError, AttributeError):
            return True  # uncertain — assume alive
    # POSIX: signal 0 just probes for liveness without delivering a signal.
    try:
        os.kill(pid, 0)
        return True
    except ProcessLookupError:
        return False
    except PermissionError:
        # Process exists but we don't own it — alive.
        return True
    except OSError:
        return True  # uncertain — assume alive


def _try_atomic_create_lock(lock_path: Path, payload: str) -> bool:
    """Atomically create the lock file with ``payload``.

    Returns ``True`` on success, ``False`` if the file already exists.
    ``O_CREAT | O_EXCL`` is the kernel's only race-free way to take an
    exclusive lock against a concurrent process — a separate ``exists()``
    check followed by ``write_text()`` has a TOCTOU window where two
    launches both see "no lock" and both write, producing two writers
    that each believe they hold it.
    """
    try:
        fd = os.open(lock_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
    except FileExistsError:
        return False
    with os.fdopen(fd, "w", encoding="utf-8") as f:
        f.write(payload)
    return True


@contextlib.contextmanager
def repo_lock(repo_root: Path, force: bool = False):
    """Hold an exclusive single-writer lock on ``repo_root`` for the with body.

    Stores ``<pid>\\n<utc-iso-timestamp>\\n`` in ``repo_root/.ingest.lock``.
    On entry, refuses to proceed if the lock already exists and points to a
    PID that's still alive — two concurrent ingests against the same repo
    would race on orphan-parquet cleanup and output file names, producing
    duplicate rows or lost work.

    Acquisition uses ``O_CREAT | O_EXCL`` so two simultaneous launches can't
    both pass the existence check before either writes. Stale locks (PID no
    longer alive) are cleaned up automatically with a one-line stderr notice.
    Pass ``force=True`` (CLI ``--force-lock``) to override even a live-PID
    lock — only safe if the user has manually confirmed the other process
    is gone.

    Released on exit even if the body raises. The lock file lives at the
    repo root rather than under ``data/`` so that ``rm -rf data/`` for a
    fresh restart doesn't accidentally remove an active lock.

    Yields ``had_stale_lock: bool`` so the body can decide whether paranoid
    crash-recovery sweeps (e.g. orphan tmp file GC) need to run. Lock file
    presence at startup means the previous run did not unwind cleanly:
    either it was hard-killed (OS crash, SIGKILL, power loss) before its
    ``finally`` could unlink the file, or the user is overriding a live
    lock with ``--force-lock``. A clean exit (including caught Ctrl+C and
    propagated exceptions) always unlinks the lock, so its absence on the
    next startup is a positive proof of clean shutdown.
    """
    repo_root.mkdir(parents=True, exist_ok=True)
    lock_path = repo_root / ".ingest.lock"
    payload = (
        f"{os.getpid()}\n"
        f"{_dt.datetime.now(_dt.timezone.utc).isoformat()}\n"
    )
    had_stale_lock = False

    if not _try_atomic_create_lock(lock_path, payload):
        # Lock already exists. Diagnose owner before deciding whether to clean.
        had_stale_lock = True
        existing_pid: int | None = None
        existing_started: str = "?"
        try:
            content = lock_path.read_text(encoding="utf-8").strip().splitlines()
            existing_pid = int(content[0])
            if len(content) > 1:
                existing_started = content[1]
        except (OSError, ValueError, IndexError):
            pass

        if existing_pid is not None and _process_alive(existing_pid):
            if not force:
                raise SystemExit(
                    f"error: ingest lock at {lock_path} is held by PID "
                    f"{existing_pid} (started {existing_started}). "
                    f"Refusing to launch a second writer against {repo_root} — "
                    f"two concurrent ingests would corrupt the dataset "
                    f"(orphan-parquet cleanup and output file names are not "
                    f"multi-writer safe). If you are certain that PID is gone, "
                    f"remove the lock file or pass --force-lock."
                )
            print(
                f"warning: --force-lock overriding existing lock "
                f"(PID {existing_pid}, started {existing_started}); this is "
                f"unsafe if the other process is still writing.",
                file=sys.stderr,
            )
        elif existing_pid is not None:
            print(
                f"info: removing stale ingest lock at {lock_path} "
                f"(PID {existing_pid} no longer alive; was started "
                f"{existing_started})",
                file=sys.stderr,
            )
        else:
            print(
                f"info: removing unparseable ingest lock at {lock_path}",
                file=sys.stderr,
            )

        try:
            lock_path.unlink()
        except OSError:
            pass
        # Re-attempt atomically. If a third process raced in between our
        # unlink and create, refuse rather than overwriting their lock.
        if not _try_atomic_create_lock(lock_path, payload):
            raise SystemExit(
                f"error: lock at {lock_path} was re-acquired by another "
                f"process between cleanup and retry; refusing to proceed."
            )

    try:
        yield had_stale_lock
    finally:
        try:
            lock_path.unlink()
        except OSError:
            pass


@contextlib.contextmanager
def keep_awake(enabled: bool = True):
    """Inhibit system sleep for the duration of the ``with`` block.

    On Windows: ``SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED)``
    on entry, ``SetThreadExecutionState(ES_CONTINUOUS)`` on exit (clears the
    flags so the system can sleep again on its normal idle policy).

    On non-Windows or when ``enabled=False``: no-op.

    The display is NOT kept awake — only the system. Phase 17 runs unattended,
    so we want to let the screen turn off but keep the box awake.
    """
    if not enabled or not sys.platform.startswith("win"):
        yield
        return

    try:
        import ctypes  # noqa: PLC0415 — keep import optional for non-win platforms
    except ImportError:
        print(
            "warning: ctypes unavailable; system may sleep during the run",
            file=sys.stderr,
        )
        yield
        return

    try:
        kernel32 = ctypes.windll.kernel32
    except (AttributeError, OSError) as e:
        print(
            f"warning: SetThreadExecutionState unavailable ({e}); "
            f"system may sleep during the run",
            file=sys.stderr,
        )
        yield
        return

    prev = kernel32.SetThreadExecutionState(_ES_CONTINUOUS | _ES_SYSTEM_REQUIRED)
    if prev == 0:
        # 0 means the call failed; the doc says GetLastError gives details,
        # but a single warning is enough — fall through and let the run
        # proceed even without the inhibitor.
        print(
            "warning: SetThreadExecutionState failed; system may sleep during the run",
            file=sys.stderr,
        )
        yield
        return

    print(
        "keep-awake: system sleep inhibited (ES_SYSTEM_REQUIRED). "
        "Display can still sleep on its normal timer.",
        file=sys.stderr,
    )
    try:
        yield
    finally:
        try:
            kernel32.SetThreadExecutionState(_ES_CONTINUOUS)
            print("keep-awake: released; normal sleep policy restored.", file=sys.stderr)
        except Exception:
            # We're already exiting; swallow rather than mask the real error.
            pass


def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
    p = argparse.ArgumentParser(
        prog="ingest_osz",
        description="Ingest .osz archives into the compact v1 store.",
    )
    p.add_argument(
        "archives",
        nargs="+",
        help=".osz files (or directories of .osz files) to ingest",
    )
    p.add_argument(
        "--repo-root",
        default=".",
        help="root of the osu-everything repo (default: cwd)",
    )
    p.add_argument(
        "--schema-version",
        default="v1",
        help="schema version directory under data/",
    )
    p.add_argument(
        "--schemas-dir",
        default=None,
        help="override schema-loading directory (default: <repo>/schemas/<schema-version>)",
    )
    p.add_argument(
        "--rosu-indexer",
        default="./target/release/osu_indexer",
        help="path to the compiled Rust indexer binary",
    )

    p.add_argument(
        "--archives-dir",
        default=None,
        help="absolute directory where archive .osz CAS copies are written "
        "(default: <repo-root>/archives)",
    )

    p.add_argument(
        "--indexer-workers",
        type=int,
        default=None,
        help="number of archives the Rust indexer processes concurrently per "
        "chunk (forwarded as --workers). Defaults to the indexer's own "
        "default of min(4, available_parallelism). Higher values keep both "
        "input and CAS drives busy continuously and parallelize rosu-pp "
        "across cores; on a single HDD spindle, going past 4 workers "
        "thrashes seeks.",
    )

    p.add_argument(
        "--write-all-revisions",
        dest="write_all_revisions",
        action="store_true",
        default=True,
    )
    p.add_argument(
        "--no-write-all-revisions",
        dest="write_all_revisions",
        action="store_false",
    )

    p.add_argument(
        "--physical-partitioning",
        choices=("schema", "none"),
        default="none",
        help="physical all_revisions partition layout. 'schema' preserves the "
        "schema partition dirs; 'none' writes one parquet per table per chunk "
        "while retaining partition columns in the file body.",
    )

    p.add_argument(
        "--ingest-batch-id",
        default=None,
        help="parent batch id; chunks get '-chunk-NNNN' suffix when >1 chunk "
        "(defaults to UTC ISO-8601 + random suffix)",
    )
    p.add_argument(
        "--limit",
        type=int,
        default=None,
        help="stop after N archives (useful for smoke tests)",
    )
    p.add_argument(
        "--dry-run",
        action="store_true",
        help="run the indexer but do not write Parquet",
    )

    p.add_argument(
        "--skip-difficulty",
        action="store_true",
        help="forward to osu_indexer: skip the rosu-pp difficulty pass",
    )
    p.add_argument(
        "--keep-ndjson",
        type=Path,
        default=None,
        help="path to keep the indexer's NDJSON output for inspection "
        "(default: write to a temp file and delete after; in chunked mode "
        "the per-chunk NDJSON gets a '.<chunk_idx>' suffix)",
    )

    # Resume / crash-safety knobs.
    p.add_argument(
        "--skip-already-ingested",
        dest="skip_already_ingested",
        action="store_true",
        default=True,
        help="hash each input archive at startup and skip those whose "
        "archive_sha256 is already in archive_revisions/ (default on)",
    )
    p.add_argument(
        "--no-skip-already-ingested",
        dest="skip_already_ingested",
        action="store_false",
    )
    p.add_argument(
        "--archive-sha-cache",
        dest="archive_sha_cache",
        action="store_true",
        default=True,
        help="cache (path, mtime, size, sha256) for input archives in "
        ".scratch/input_archive_shas.json so the pre-chunk hashing pass "
        "skips files that haven't changed since last run (default on)",
    )
    p.add_argument(
        "--no-archive-sha-cache",
        dest="archive_sha_cache",
        action="store_false",
        help="force-rehash every input archive at startup, ignoring the cache",
    )
    p.add_argument(
        "--chunk-size",
        type=int,
        default=1000,
        help="archives per indexer invocation; smaller = finer resume "
        "granularity, larger = fewer parquet files (default 1000)",
    )
    p.add_argument(
        "--gc-tmp-files",
        dest="gc_tmp_files",
        action="store_true",
        default=True,
        help="enable orphan *.tmp.<pid> file GC on startup (default on). "
        "By default the walk only runs when a stale .ingest.lock is "
        "detected at startup (= previous run was hard-killed); on clean "
        "exits and Ctrl+C the walk is skipped because atomic tmp+rename + "
        "the indexer's signal-aware drain leave no orphans. Pass "
        "--always-gc-tmp-files to walk unconditionally.",
    )
    p.add_argument(
        "--no-gc-tmp-files",
        dest="gc_tmp_files",
        action="store_false",
        help="never walk for orphan tmp files. Use only when you've "
        "manually confirmed the repo is clean — leftover tmp files will "
        "stay on disk forever as wasted space until cleaned by hand.",
    )
    p.add_argument(
        "--always-gc-tmp-files",
        dest="always_gc_tmp_files",
        action="store_true",
        default=False,
        help="walk archives/ and data/ for orphan *.tmp.<pid> files on every "
        "startup, regardless of whether the previous run shut down cleanly.",
    )
    p.add_argument(
        "--always-crash-recovery",
        dest="always_crash_recovery",
        action="store_true",
        default=False,
        help="run orphan-Parquet cleanup on every startup, regardless of "
        "whether the previous run shut down cleanly.",
    )
    p.add_argument(
        "--gc-tmp-files-age-min",
        type=int,
        default=60,
        help="minimum age in minutes for tmp files to be eligible for GC "
        "(default 60); set to 0 to delete all orphans",
    )
    p.add_argument(
        "--cleanup-orphan-parquets",
        dest="cleanup_orphan_parquets",
        action="store_true",
        default=True,
        help="on startup, delete all_revisions/<table>/.../part-<batch>.parquet "
        "files whose batch_id has no archive_revisions commit marker — "
        "fixes duplicate rows after a crashed chunk (default on)",
    )
    p.add_argument(
        "--no-cleanup-orphan-parquets",
        dest="cleanup_orphan_parquets",
        action="store_false",
    )

    p.add_argument(
        "--quiet-indexer",
        action="store_true",
        help="forward --quiet to the Rust indexer, suppressing the per-archive "
        "progress line. The chunk-level ETA/timing summary is still printed.",
    )

    p.add_argument(
        "--keep-awake",
        dest="keep_awake",
        action="store_true",
        default=True,
        help="on Windows, prevent system sleep for the duration of the run "
        "via SetThreadExecutionState (ES_SYSTEM_REQUIRED). Display sleep is "
        "not inhibited. Default on; no-op on non-Windows.",
    )
    p.add_argument(
        "--no-keep-awake",
        dest="keep_awake",
        action="store_false",
    )

    p.add_argument(
        "--force-lock",
        dest="force_lock",
        action="store_true",
        default=False,
        help="override an existing repo lock even if the holding PID is still "
        "alive. UNSAFE: two concurrent ingests against the same repo will "
        "corrupt the dataset. Only pass this if you have manually confirmed "
        "the previous process is gone.",
    )

    return p.parse_args(argv)


def _resolve_indexer_path(indexer: Path, repo_root: Path) -> Path | None:
    """Find the indexer binary by trying cwd- and repo-root-relative,
    with and without a Windows .exe suffix."""
    candidates: list[Path] = []
    if indexer.is_absolute():
        candidates.append(indexer)
    else:
        candidates.extend([Path.cwd() / indexer, repo_root / indexer])
    if sys.platform.startswith("win"):
        candidates += [c.with_suffix(c.suffix + ".exe") for c in list(candidates)]
    for c in candidates:
        if c.exists():
            return c.resolve()
    return None


def collect_archives(inputs: list[str]) -> list[Path]:
    out: list[Path] = []
    for arg in inputs:
        p = Path(arg)
        if p.is_dir():
            out.extend(sorted(p.rglob("*.osz")))
        elif p.is_file() and p.suffix.lower() == ".osz":
            out.append(p)
        else:
            print(f"warning: skipping non-.osz argument {arg!r}", file=sys.stderr)
    return out


def _format_duration(seconds: float) -> str:
    """Compact human-readable duration ('42s', '7.3m', '4.2h')."""
    if seconds < 60:
        return f"{seconds:.0f}s"
    if seconds < 3600:
        return f"{seconds / 60:.1f}m"
    return f"{seconds / 3600:.1f}h"


def _format_size(num_bytes: float) -> str:
    """Compact human-readable byte size ('312 KB', '47.2 MB', '3.1 GB')."""
    n = float(num_bytes)
    for unit in ("B", "KB", "MB", "GB", "TB"):
        if n < 1024 or unit == "TB":
            return f"{n:.1f} {unit}" if unit != "B" else f"{int(n)} B"
        n /= 1024
    return f"{n:.1f} TB"


def already_ingested_shas(archive_revisions_dir: Path) -> set[str]:
    """Read existing all_revisions/archive_revisions/ parquets and return the
    set of archive_sha256 values currently committed.

    Returns an empty set if the directory is empty or missing. Iterates
    batches under a tqdm bar so the user sees progress while a full-corpus
    archive_revisions/ (tens of thousands of rows across many parquets) is
    being read.
    """
    if not archive_revisions_dir.exists():
        return set()
    parquets = list(archive_revisions_dir.rglob("*.parquet"))
    if not parquets:
        return set()
    dataset = ds.dataset([str(p) for p in parquets], format="parquet")
    scanner = dataset.scanner(columns=["archive_sha256"])
    total = scanner.count_rows()
    out: set[str] = set()
    bar = tqdm(
        total=total,
        desc=f"reading committed archive_sha256s ({len(parquets)} parquet(s))",
        unit="row",
        unit_scale=True,
        file=sys.stderr,
        mininterval=1.0,
        dynamic_ncols=True,
    )
    try:
        for batch in scanner.to_batches():
            out.update(batch.column("archive_sha256").to_pylist())
            bar.update(batch.num_rows)
    finally:
        bar.close()
    return out


def sha256_of_file(path: Path) -> str:
    h = hashlib.sha256()
    with path.open("rb") as f:
        for chunk in iter(lambda: f.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()


# JSON sidecar mapping ``str(path.resolve()) -> {mtime_ns, size_bytes, sha256}``.
# A re-run with unchanged inputs is dominated by the cost of statting each file
# and reading the cache; on a 38k-archive HDD corpus this collapses ~55 minutes
# of SHA-256 to a few seconds. A stale cache loses correctness only if the user
# replaces a file's contents while preserving both mtime AND size — pass
# ``--no-archive-sha-cache`` to force re-hashing if that's a real concern.
_ARCHIVE_SHA_CACHE_BASENAME = "input_archive_shas.json"


def _archive_sha_cache_path(repo_root: Path) -> Path:
    return repo_root / ".scratch" / _ARCHIVE_SHA_CACHE_BASENAME


def _load_archive_sha_cache(cache_path: Path) -> dict[str, dict]:
    if not cache_path.exists():
        return {}
    try:
        data = json.loads(cache_path.read_text(encoding="utf-8"))
    except (OSError, json.JSONDecodeError) as e:
        _log(f"  warning: ignoring unreadable sha cache at {cache_path}: {e}")
        return {}
    if not isinstance(data, dict):
        return {}
    return data


def _save_archive_sha_cache(cache_path: Path, cache: dict[str, dict]) -> None:
    cache_path.parent.mkdir(parents=True, exist_ok=True)
    tmp = cache_path.with_suffix(cache_path.suffix + f".tmp.{os.getpid()}")
    try:
        tmp.write_text(json.dumps(cache), encoding="utf-8")
        os.replace(tmp, cache_path)
    except OSError as e:
        _log(f"  warning: could not save sha cache to {cache_path}: {e}")
        try:
            tmp.unlink()
        except OSError:
            pass


def filter_already_ingested(
    archives: list[Path],
    known_shas: set[str],
    cache_path: Path | None = None,
) -> tuple[list[Path], list[Path], list[tuple[Path, Path]]]:
    """Hash each archive and partition into (to_ingest, already_done, duplicates).

    The hashing pass also de-duplicates the *input* list by content SHA-256.
    If ``osu_archives/`` accidentally contains two files whose bytes are
    identical (e.g. ``1.osz`` plus ``copy_of_1.osz``), the duplicate would
    otherwise produce two ``archive_revisions`` rows with the same primary
    key in the same chunk. We keep the first occurrence (sorted glob order
    is already deterministic) and report the rest as ``duplicates``.

    Always hashes — including when ``known_shas`` is empty, so duplicate
    inputs in a fresh run are still caught. To opt out of the hashing cost
    on small/trusted inputs, pass ``--no-skip-already-ingested`` to
    ``ingest_osz.py``; that path skips this entire function.

    With ``cache_path`` set, an on-disk sidecar maps
    ``str(path.resolve()) -> {mtime_ns, size_bytes, sha256}``. Inputs whose
    (path, mtime_ns, size_bytes) match a cache entry skip the full hash; new
    or modified files are hashed once and added to the cache. Saved every
    5,000 hashes and again at function exit so a Ctrl+C mid-pass keeps the
    work done so far. The first full-corpus pass on HDD still takes ~50 min
    (~12 archives/sec); subsequent passes finish in seconds.
    """
    cache: dict[str, dict] = (
        _load_archive_sha_cache(cache_path) if cache_path is not None else {}
    )
    cache_dirty = False
    cache_hits = 0
    save_every = 5000
    hashes_since_save = 0

    def _persist() -> None:
        nonlocal cache_dirty, hashes_since_save
        if cache_path is None or not cache_dirty:
            return
        _save_archive_sha_cache(cache_path, cache)
        cache_dirty = False
        hashes_since_save = 0

    to_ingest: list[Path] = []
    already_done: list[Path] = []
    duplicates: list[tuple[Path, Path]] = []
    seen_in_input: dict[str, Path] = {}
    try:
        for p in tqdm(
            archives,
            total=len(archives),
            desc="hashing archives",
            unit="archive",
            file=sys.stderr,
            mininterval=1.0,
            dynamic_ncols=True,
        ):
            cache_key: str | None = None
            mtime_ns: int | None = None
            size_bytes: int | None = None
            try:
                stat = p.stat()
                size_bytes = stat.st_size
                mtime_ns = stat.st_mtime_ns
                cache_key = str(p.resolve())
            except OSError:
                # If we can't stat, fall through to a fresh hash; sha256_of_file
                # will surface the same OSError if the file is genuinely gone.
                pass
            entry = cache.get(cache_key) if cache_key is not None else None
            if (
                isinstance(entry, dict)
                and entry.get("mtime_ns") == mtime_ns
                and entry.get("size_bytes") == size_bytes
                and isinstance(entry.get("sha256"), str)
            ):
                sha = entry["sha256"]
                cache_hits += 1
            else:
                sha = sha256_of_file(p)
                if cache_key is not None:
                    cache[cache_key] = {
                        "mtime_ns": mtime_ns,
                        "size_bytes": size_bytes,
                        "sha256": sha,
                    }
                    cache_dirty = True
                    hashes_since_save += 1
                    if hashes_since_save >= save_every:
                        _persist()
            if sha in seen_in_input:
                duplicates.append((p, seen_in_input[sha]))
            elif sha in known_shas:
                already_done.append(p)
                seen_in_input[sha] = p
            else:
                to_ingest.append(p)
                seen_in_input[sha] = p
    finally:
        _persist()
    if cache_path is not None:
        _log(
            f"  sha cache: {cache_hits:,}/{len(archives):,} hit(s); "
            f"{len(archives) - cache_hits:,} fresh hash(es) "
            f"({cache_path})"
        )
    return to_ingest, already_done, duplicates


_TMP_PATTERN = re.compile(r"\.tmp\.\d+$")
_PART_BATCH_ID_PATTERN = re.compile(r"^part-(.+)\.parquet$")


def committed_batch_ids(archive_revisions_dir: Path) -> set[str]:
    """Return the set of ``batch_id``s with a committed ``archive_revisions``
    parquet file on disk.

    Each AR file is named ``part-<batch_id>.parquet``; its existence is the
    commit marker for that batch. Used by :func:`cleanup_orphan_parquets`
    to identify all_revisions parquets from crashed chunks.
    """
    if not archive_revisions_dir.exists():
        return set()
    out: set[str] = set()
    bar = tqdm(
        desc="scanning archive_revisions/",
        unit="file",
        file=sys.stderr,
        mininterval=1.0,
        dynamic_ncols=True,
    )
    try:
        for f in archive_revisions_dir.rglob("*.parquet"):
            bar.update(1)
            m = _PART_BATCH_ID_PATTERN.match(f.name)
            if m:
                out.add(m.group(1))
    finally:
        bar.close()
    return out


def committed_batch_ids_for_parent(
    archive_revisions_dir: Path,
    parent_batch_id: str,
) -> list[str]:
    """Return committed ingest_batch_id values matching a parent batch.

    This reads row data instead of relying on ``part-<batch>.parquet``
    filenames so the guard still works after metadata compaction rewrites old
    commit-marker files into ``compact-*.parquet``.
    """

    files = sorted(archive_revisions_dir.rglob("*.parquet")) if archive_revisions_dir.exists() else []
    if not files:
        return []
    prefix = f"{parent_batch_id}-chunk-"
    dataset = ds.dataset([str(p) for p in files], format="parquet")
    if "ingest_batch_id" not in dataset.schema.names:
        return []
    table = dataset.to_table(columns=["ingest_batch_id"])
    out = {
        str(batch_id)
        for batch_id in table["ingest_batch_id"].to_pylist()
        if batch_id
        and (str(batch_id) == parent_batch_id or str(batch_id).startswith(prefix))
    }
    return sorted(out)


def cleanup_orphan_parquets(
    all_revisions_root: Path,
    committed: set[str],
) -> dict[str, int]:
    """Delete ``part-<batch_id>.parquet`` files in non-archive_revisions
    tables whose ``batch_id`` is not in ``committed``.

    These are remnants of crashed chunks: their tables were written but
    the chunk's ``archive_revisions`` row never made it to disk, so
    skip-already-ingested will re-process the chunk and write fresh
    parquets. Without cleanup the resume produces duplicate rows.

    AR itself is never touched. Returns a per-table count of deletions.
    """
    out: dict[str, int] = {}
    if not all_revisions_root.exists():
        return out
    table_dirs = [
        d for d in all_revisions_root.iterdir()
        if d.is_dir() and d.name != "archive_revisions"
    ]
    for table_dir in tqdm(
        table_dirs,
        total=len(table_dirs),
        desc="scanning all_revisions tables",
        unit="table",
        file=sys.stderr,
        mininterval=1.0,
        dynamic_ncols=True,
    ):
        n = 0
        for parquet in table_dir.rglob("*.parquet"):
            m = _PART_BATCH_ID_PATTERN.match(parquet.name)
            if not m:
                continue
            if m.group(1) not in committed:
                try:
                    parquet.unlink()
                    n += 1
                except OSError:
                    pass
        if n:
            out[table_dir.name] = n
    return out


def cleanup_orphan_tmp_files(
    repo_root: Path,
    max_age_seconds: int,
    extra_roots: list[tuple[str, Path]] | None = None,
) -> dict[str, int]:
    """Delete orphan ``*.tmp.<pid>`` files under archives/ and data/.

    Files are eligible when their mtime is older than ``max_age_seconds``;
    very young tmp files might belong to a concurrent run, so we skip them.
    Returns ``{subdir: count_deleted}``.

    ``extra_roots`` is an optional list of ``(label, absolute_path)`` pairs
    to also walk, used when ``--archives-dir`` points outside ``repo_root``.

    Pass ``max_age_seconds=0`` to delete unconditionally (dangerous if any
    other process is mid-write — only safe when we're the sole writer).

    Each subdir's rglob is wrapped in an indeterminate-total tqdm spinner
    because we don't know the file count up-front and a full-corpus repo
    can hold millions of entries — without the bar a user on HDD just sees
    silence for 5-10 minutes.
    """
    standard_subs = ("archives", "data")
    out: dict[str, int] = {s: 0 for s in standard_subs}
    extras = list(extra_roots or [])
    for label, _ in extras:
        out.setdefault(label, 0)

    # Build the (label, path) walk list, deduping by resolved absolute path so
    # an extra root that aliases repo_root/archives does not get scanned twice.
    seen: set[Path] = set()
    targets: list[tuple[str, Path]] = []
    for label, path in (
        [(s, repo_root / s) for s in standard_subs] + extras
    ):
        try:
            key = Path(path).resolve()
        except OSError:
            key = Path(path)
        if key in seen:
            continue
        seen.add(key)
        targets.append((label, path))

    now = time.time()
    for sub, root in targets:
        if not root.exists():
            continue
        bar = tqdm(
            desc=f"scanning {sub}/",
            unit="file",
            file=sys.stderr,
            mininterval=1.0,
            dynamic_ncols=True,
        )
        try:
            for f in root.rglob("*"):
                bar.update(1)
                if not f.is_file():
                    continue
                if _TMP_PATTERN.search(f.name) is None:
                    continue
                try:
                    age = now - f.stat().st_mtime
                except OSError:
                    continue
                if age < max_age_seconds:
                    continue
                try:
                    f.unlink()
                    out[sub] += 1
                except OSError:
                    pass
        finally:
            bar.close()
    return out


# Per-chunk indexer wall-time bound. Phase 15 cold-cache wall is ~42 s for 100
# archives (~0.42 s/archive); chunk_size=1000 should finish well under 10 min.
# 1800 s is ~4× the projected typical chunk wall, leaving ample headroom for
# I/O contention but bounded so a single deadlocked rosu-pp call (or any other
# stuck syscall) cannot hang the orchestrator indefinitely with --keep-awake
# active. On TimeoutExpired the chunk fails cleanly: the deferred
# archive_revisions commit means no rows commit, and resume re-runs the chunk.
INDEXER_TIMEOUT_SECONDS = 1800


def run_indexer(
    indexer: Path,
    archives: list[Path],
    archives_dir: Path,
    blobs_dir: Path,
    out_file: Path,
    batch_id: str,
    schema_version: str,
    skip_difficulty: bool,
    quiet: bool = False,
    workers: int | None = None,
    timeout_seconds: int | None = INDEXER_TIMEOUT_SECONDS,
) -> None:
    """Invoke the Rust indexer; raise CalledProcessError on non-zero exit.

    The indexer streams a per-archive progress line to its own stderr, which
    we let pass through unbuffered so the user sees real-time progress
    during long chunks. Pass ``quiet=True`` to suppress (the indexer's
    summary line still prints).

    ``workers`` overrides the indexer's parallelism; ``None`` lets the
    indexer pick its own default (``min(4, available_parallelism)``).
    Larger values keep input + CAS drives busy in parallel and parallelize
    the per-beatmap rosu-pp pass across cores.

    A wall-time ``timeout_seconds`` (default ``INDEXER_TIMEOUT_SECONDS``)
    bounds the call so a deadlocked subprocess cannot hang the orchestrator
    forever; pass ``None`` to disable. On timeout, the call site sees
    ``subprocess.TimeoutExpired`` propagate up; the chunk's
    ``archive_revisions`` commit has not happened yet, so resume re-runs it.
    """
    cmd: list[str] = [
        str(indexer),
        "--archives-dir", str(archives_dir),
        "--blobs-dir", str(blobs_dir),
        "--out-file", str(out_file),
        "--ingest-batch-id", batch_id,
        "--schema-version", schema_version,
    ]
    if skip_difficulty:
        cmd.append("--skip-difficulty")
    cmd.append("--no-write-blobs")
    if quiet:
        cmd.append("--quiet")
    if workers is not None:
        cmd.extend(["--workers", str(workers)])
    cmd.extend(str(a) for a in archives)
    _log(
        f"  indexer: {len(archives)} archive(s); batch_id={batch_id}"
    )
    try:
        subprocess.run(cmd, check=True, timeout=timeout_seconds)
    except subprocess.TimeoutExpired as e:
        _log(
            f"  indexer TIMEOUT after {e.timeout}s on batch_id={batch_id} "
            f"({len(archives)} archive(s)); chunk will not commit, "
            f"resume the run to retry."
        )
        raise


def _process_chunk(
    chunk: list[Path],
    chunk_batch_id: str,
    args: argparse.Namespace,
    indexer_path: Path,
    schemas: dict,
    repo_root: Path,
    archives_dir: Path,
    blobs_dir: Path,
    all_revisions_root: Path,
    keep_ndjson: Path | None,
) -> dict:
    """Run the full per-chunk pipeline. Returns counts/paths/timings for reporting.

    Each step is timed separately (``*_seconds`` keys in the returned dict)
    so a long-running chunk's bottleneck is visible without re-running.
    """

    # NDJSON destination: --keep-ndjson if supplied, else a temp file.
    # The tempfile lives under repo_root/.scratch (typically the same drive
    # as the canonical store, e.g. E: on the dev box) rather than the OS
    # default ($TEMP / C:\) — at chunk_size=1000 each chunk's NDJSON is
    # 2-5 GB, and 38 chunks of create+delete on the system drive would
    # waste ~115 GB of write churn unrelated to the dataset.
    if keep_ndjson is not None:
        ndjson_path = keep_ndjson
        ndjson_path.parent.mkdir(parents=True, exist_ok=True)
        cleanup_ndjson = False
    else:
        scratch_dir = repo_root / ".scratch"
        scratch_dir.mkdir(parents=True, exist_ok=True)
        tmp = tempfile.NamedTemporaryFile(
            prefix=f"ingest-{chunk_batch_id}-",
            suffix=".ndjson",
            dir=str(scratch_dir),
            delete=False,
        )
        tmp.close()
        ndjson_path = Path(tmp.name)
        cleanup_ndjson = True

    chunk_start = time.perf_counter()
    summary: dict = {"chunk_batch_id": chunk_batch_id, "n_archives": len(chunk)}

    try:
        t0 = time.perf_counter()
        run_indexer(
            indexer=indexer_path,
            archives=chunk,
            archives_dir=archives_dir,
            blobs_dir=blobs_dir,
            out_file=ndjson_path,
            batch_id=chunk_batch_id,
            schema_version=args.schema_version,
            skip_difficulty=args.skip_difficulty,
            quiet=args.quiet_indexer,
            workers=args.indexer_workers,
        )
        summary["indexer_seconds"] = time.perf_counter() - t0

        try:
            summary["ndjson_bytes"] = ndjson_path.stat().st_size
        except OSError:
            summary["ndjson_bytes"] = 0

        t0 = time.perf_counter()
        rows_by_table = group_rows_by_table(ndjson_path)
        summary["group_seconds"] = time.perf_counter() - t0
        n_rows = sum(len(rs) for rs in rows_by_table.values())
        summary["n_rows"] = n_rows
        summary["archive_revisions_emitted"] = len(rows_by_table.get("archive_revisions", []))
        summary["set_revisions"] = len(rows_by_table.get("set_revisions", []))
        summary["beatmaps"] = len(rows_by_table.get("beatmaps", []))

        # P0 safety: an indexer that exited 0 but produced no archive rows is
        # never expected during a normal run. Refuse to commit an empty chunk
        # — the resume path (skip-already-ingested) treats archive_revisions
        # as proof the chunk is done, so a silently-empty commit would
        # permanently mask a broken chunk.
        if rows_by_table.get("archive_revisions") is None or not rows_by_table.get("archive_revisions"):
            raise RuntimeError(
                f"indexer exited 0 but produced no archive_revisions rows for "
                f"chunk {chunk_batch_id} ({len(chunk)} input archive(s)); "
                f"refusing to commit an empty chunk. NDJSON at {ndjson_path}"
            )

        if args.dry_run:
            print("  dry-run: skipping Parquet writes", file=sys.stderr)
            summary["wall_seconds"] = time.perf_counter() - chunk_start
            return summary

        # Step 3: write all_revisions tables EXCEPT archive_revisions.
        # (archive_revisions is the chunk commit marker — written last.)
        if args.write_all_revisions:
            t0 = time.perf_counter()
            written = write_all_revisions_tables(
                rows_by_table,
                schemas,
                all_revisions_root,
                chunk_batch_id,
                defer_tables=("archive_revisions",),
                physical_partitioning=args.physical_partitioning,
            )
            summary["parquet_seconds"] = time.perf_counter() - t0
            summary["written_files"] = sum(len(ps) for ps in written.values())

        # Step 4: COMMIT — atomically write archive_revisions.
        # After this returns, --skip-already-ingested treats this chunk's
        # archives as done.
        if args.write_all_revisions:
            t0 = time.perf_counter()
            ar_paths = commit_archive_revisions(
                rows_by_table,
                schemas,
                all_revisions_root,
                chunk_batch_id,
                physical_partitioning=args.physical_partitioning,
            )
            summary["ar_commit_seconds"] = time.perf_counter() - t0
            summary["archive_revisions_committed"] = len(ar_paths)

    finally:
        if cleanup_ndjson:
            try:
                ndjson_path.unlink()
            except OSError:
                pass

    summary["wall_seconds"] = time.perf_counter() - chunk_start
    return summary


def main(argv: list[str] | None = None) -> int:
    args = parse_args(argv)

    repo_root = Path(args.repo_root).resolve()
    if args.schemas_dir is not None:
        schemas_dir = Path(args.schemas_dir).resolve()
    else:
        schemas_dir = repo_root / "schemas" / args.schema_version
    if not schemas_dir.exists():
        print(
            f"error: {schemas_dir} not found - run from repo root or pass --repo-root",
            file=sys.stderr,
        )
        return 2

    indexer_path = _resolve_indexer_path(Path(args.rosu_indexer), repo_root)
    if indexer_path is None:
        print(
            f"error: indexer binary not found at {args.rosu_indexer} "
            f"(searched cwd and {repo_root}) - build it with "
            f"`cargo build --release -p osu_indexer`",
            file=sys.stderr,
        )
        return 2

    archives = collect_archives(args.archives)
    if args.limit:
        archives = archives[: args.limit]
    if not archives:
        print("error: no .osz archives to ingest", file=sys.stderr)
        return 2

    parent_batch_id = args.ingest_batch_id or _default_batch_id()
    archives_dir = (
        Path(args.archives_dir).resolve()
        if args.archives_dir
        else repo_root / "archives"
    )
    blobs_dir = repo_root / "blobs"
    all_revisions_root = repo_root / "data" / args.schema_version / "all_revisions"

    if archives_dir != repo_root / "archives":
        _log(f"archives dir: {archives_dir}")

    # Acquire the single-writer repo lock BEFORE inhibiting sleep — if the
    # lock fails we don't want to leave the system in a no-sleep state.
    # The lock context yields had_stale_lock=True when a prior lock file
    # existed at startup; that's our signal that the previous run was
    # hard-killed (its finally never ran) and orphan tmp file GC should
    # actually run instead of being short-circuited.
    try:
        with repo_lock(repo_root, force=args.force_lock) as had_stale_lock:
            with keep_awake(enabled=args.keep_awake):
                return _run_main(
                    args=args,
                    repo_root=repo_root,
                    archives=archives,
                    parent_batch_id=parent_batch_id,
                    archives_dir=archives_dir,
                    blobs_dir=blobs_dir,
                    all_revisions_root=all_revisions_root,
                    schemas_dir=schemas_dir,
                    indexer_path=indexer_path,
                    had_stale_lock=had_stale_lock,
                )
    except KeyboardInterrupt:
        # Propagated up after both context managers have unwound. The chunk
        # commit marker (archive_revisions) is the last write in a chunk's
        # pipeline, so any uncommitted chunk simply doesn't appear on disk —
        # rerunning with --skip-already-ingested resumes from the next chunk.
        _log(
            "\ningest_osz: ctrl+c received; in-flight chunk did not commit, "
            "resume the run to retry it."
        )
        return 130


def _run_main(
    args: argparse.Namespace,
    repo_root: Path,
    archives: list[Path],
    parent_batch_id: str,
    archives_dir: Path,
    blobs_dir: Path,
    all_revisions_root: Path,
    schemas_dir: Path,
    indexer_path: Path,
    had_stale_lock: bool,
) -> int:
    """Body of ``main`` extracted so the wake-lock context manager wraps every
    long-running step (GC, hashing pass, chunked ingest) without nesting
    half the function under an extra indent."""

    schemas = load_schemas(schemas_dir)
    _log(f"loaded {len(schemas)} table schema(s) from {schemas_dir}")

    # Crash-recovery cleanup runs first; each step is a no-op when the repo is
    # already clean.
    # The orphan-tmp-file walk is paranoid crash recovery: it costs ~150
    # file/s on a HDD spindle (multiple minutes for full-corpus repos).
    # It's only useful when the previous run might have left in-flight
    # tmp+rename writes incomplete. With:
    #   - atomic_copy_if_absent and _atomic_write_parquet using tmp+rename
    #   - the indexer's ctrlc::set_handler draining workers on Ctrl+C
    #   - repo_lock's finally unlinking .ingest.lock on every soft exit
    # a clean shutdown leaves zero orphans. So we only run the walk when
    # the lock file *survived* the previous run (= hard-killed) or the
    # user explicitly forces it via --always-gc-tmp-files.
    if args.gc_tmp_files and (args.always_gc_tmp_files or had_stale_lock):
        reason = (
            "forced by --always-gc-tmp-files"
            if args.always_gc_tmp_files
            else "previous run left a stale lock"
        )
        _log(
            f"scanning for orphan *.tmp.<pid> files older than "
            f"{args.gc_tmp_files_age_min} min ({reason})"
        )
        # Also walk an archive CAS root that lives outside repo_root.
        extra_roots = []
        if archives_dir != repo_root / "archives":
            extra_roots.append((f"archives ({archives_dir})", archives_dir))
        gc_summary = cleanup_orphan_tmp_files(
            repo_root,
            max_age_seconds=args.gc_tmp_files_age_min * 60,
            extra_roots=extra_roots,
        )
        gc_total = sum(gc_summary.values())
        _log(f"  removed {gc_total} orphan tmp file(s) ({gc_summary})")
    elif args.gc_tmp_files:
        _log(
            "skipping orphan tmp file scan "
            "(previous run shut down cleanly; pass --always-gc-tmp-files "
            "to force a paranoid walk)"
        )

    ar_dir = all_revisions_root / "archive_revisions"

    # Crash-recovery suite. The compact writer only needs orphan parquet
    # cleanup: any table parquet whose batch_id lacks an archive_revisions
    # commit marker belongs to an interrupted chunk and must be removed before
    # retry to avoid duplicate rows.
    should_run_recovery = had_stale_lock or args.always_crash_recovery
    if should_run_recovery:
        recovery_reason = (
            "forced by --always-crash-recovery"
            if args.always_crash_recovery
            else "previous run left a stale lock"
        )
        _log(f"running crash-recovery suite ({recovery_reason})")

        if args.cleanup_orphan_parquets:
            _log("scanning all_revisions/ for orphan parquet files")
            committed = committed_batch_ids(ar_dir)
            orphan_summary = cleanup_orphan_parquets(
                all_revisions_root, committed
            )
            orphan_total = sum(orphan_summary.values())
            _log(
                f"  removed {orphan_total} orphan parquet(s) "
                f"({len(orphan_summary)} table(s); "
                f"{len(committed)} committed batch_id(s))"
            )
    else:
        _log(
            "skipping crash-recovery suite "
            "(previous run shut down cleanly; pass --always-crash-recovery "
            "to force orphan-parquet cleanup)"
        )

    # Hash each input archive and partition into (to-ingest, already-done,
    # duplicate) so two copies of the same .osz can't produce duplicate
    # archive_revisions rows in one chunk and a resumed run skips committed
    # archives without re-running the indexer on them.
    if args.skip_already_ingested:
        known = already_ingested_shas(ar_dir)
        if known:
            _log(
                f"hashing {len(archives):,} input(s) against "
                f"{len(known):,} known archive_sha256(s)"
            )
        else:
            _log(
                f"hashing {len(archives):,} input(s) (dedup-only; "
                f"no committed archives yet)"
            )
        cache_path = (
            _archive_sha_cache_path(repo_root) if args.archive_sha_cache else None
        )
        archives, already_done, duplicates = filter_already_ingested(
            archives, known, cache_path=cache_path
        )
        if already_done:
            _log(f"  skipped {len(already_done):,} already-ingested archive(s)")
        if duplicates:
            _log(
                f"  warning: dropped {len(duplicates):,} duplicate input archive(s) "
                f"(same content SHA-256 as another input). First few:"
            )
            for dup, kept in duplicates[:5]:
                try:
                    dup_rel = dup.relative_to(repo_root)
                except ValueError:
                    dup_rel = dup
                try:
                    kept_rel = kept.relative_to(repo_root)
                except ValueError:
                    kept_rel = kept
                _log(f"    {dup_rel}  ==  {kept_rel}")
            if len(duplicates) > 5:
                _log(f"    ... and {len(duplicates) - 5} more")
        if not archives:
            _log("all input archives are already ingested; nothing to do")
            return 0

    if not args.dry_run:
        existing_batches = committed_batch_ids_for_parent(ar_dir, parent_batch_id)
        if existing_batches:
            preview = ", ".join(existing_batches[:5])
            if len(existing_batches) > 5:
                preview += f", ... ({len(existing_batches)} total)"
            raise RuntimeError(
                f"ingest_batch_id {parent_batch_id!r} already has committed "
                f"archive_revisions parquet(s): {preview}. Refusing to ingest "
                "new archives with a reused batch id because it would overwrite "
                "existing part-<batch>.parquet files. Choose a fresh BATCH_ID."
            )

    chunk_size = max(1, args.chunk_size)
    chunks = [archives[i : i + chunk_size] for i in range(0, len(archives), chunk_size)]
    multi_chunk = len(chunks) > 1

    _log(
        f"ingest_osz: {len(archives)} archive(s); repo={repo_root}; "
        f"parent_batch={parent_batch_id}; "
        f"{len(chunks)} chunk(s) of up to {chunk_size}; dry_run={args.dry_run}"
    )

    overall_start = time.perf_counter()
    cumulative_archives = 0
    total_archives = len(archives)
    chunk_summaries: list[dict] = []
    for chunk_idx, chunk in enumerate(chunks):
        if multi_chunk:
            chunk_batch_id = f"{parent_batch_id}-chunk-{chunk_idx:04d}"
        else:
            chunk_batch_id = parent_batch_id

        # Per-chunk NDJSON path (suffixed when multi-chunk so retained traces don't clobber).
        if args.keep_ndjson is not None:
            if multi_chunk:
                keep_ndjson = args.keep_ndjson.with_suffix(
                    f".chunk-{chunk_idx:04d}{args.keep_ndjson.suffix}"
                )
            else:
                keep_ndjson = args.keep_ndjson
        else:
            keep_ndjson = None

        _log(
            f"\n[{chunk_idx + 1}/{len(chunks)}] chunk_batch_id={chunk_batch_id} "
            f"({len(chunk)} archive(s))"
        )

        summary = _process_chunk(
            chunk=chunk,
            chunk_batch_id=chunk_batch_id,
            args=args,
            indexer_path=indexer_path,
            schemas=schemas,
            repo_root=repo_root,
            archives_dir=archives_dir,
            blobs_dir=blobs_dir,
            all_revisions_root=all_revisions_root,
            keep_ndjson=keep_ndjson,
        )
        chunk_summaries.append(summary)
        cumulative_archives += len(chunk)

        if not args.dry_run:
            elapsed = time.perf_counter() - overall_start
            rate = cumulative_archives / elapsed if elapsed > 0 else 0.0
            remaining = max(total_archives - cumulative_archives, 0)
            eta_seconds = remaining / rate if rate > 0 else 0.0
            wall = summary.get("wall_seconds", 0.0)
            ndjson_b = summary.get("ndjson_bytes", 0)
            pct = 100.0 * cumulative_archives / total_archives if total_archives else 100.0
            _log(
                f"  chunk {chunk_idx + 1}/{len(chunks)} done in "
                f"{_format_duration(wall)} | "
                f"{cumulative_archives:,}/{total_archives:,} ({pct:.1f}%) | "
                f"{rate:.2f} ar/s | ETA {_format_duration(eta_seconds)}"
            )
            _log(
                f"    timings: indexer {summary.get('indexer_seconds', 0):.1f}s | "
                f"group {summary.get('group_seconds', 0):.1f}s | "
                f"parquet {summary.get('parquet_seconds', 0):.1f}s | "
                f"ar-commit {summary.get('ar_commit_seconds', 0):.1f}s"
            )
            _log(
                f"    output: {summary.get('written_files', 0)} all_rev parquet(s), "
                f"AR={summary.get('archive_revisions_committed', 0)}, "
                f"NDJSON={_format_size(ndjson_b)} "
                f"({summary.get('n_rows', 0):,} rows)"
            )

    overall_elapsed = time.perf_counter() - overall_start
    if cumulative_archives:
        overall_rate = cumulative_archives / overall_elapsed if overall_elapsed > 0 else 0.0
        _log(
            f"\ningest_osz: done. {len(chunks)} chunk(s), "
            f"{cumulative_archives:,} archive(s) in "
            f"{_format_duration(overall_elapsed)} "
            f"({overall_rate:.2f} ar/s)."
        )
    else:
        _log(f"\ningest_osz: done. {len(chunks)} chunk(s) processed.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())