File size: 55,971 Bytes
2f382c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
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
#!/usr/bin/env python3
"""Run prompt-consensus accessibility masks on a small reviewed pilot set.

Supported backends:
  sam3          Meta SAM 3 text-prompt concept segmentation (preferred)
  grounded_sam  GroundingDINO boxes refined by SAM ViT-H (local control)

The tool saves per-prompt masks, a high-recall union, a majority-vote core, an
uncertainty mask, both raw/high-recall and filtered obstacle masks, overlays,
and machine-readable quality scores.  Automatic output is a review proposal,
never ground truth.
"""

from __future__ import annotations

import argparse
import html
import json
import math
import os
import re
import sys
import traceback
from collections import Counter
from dataclasses import dataclass
from pathlib import Path
from typing import Any

import cv2
import numpy as np
from PIL import Image, ImageOps


def read_jsonl(path: Path) -> list[dict[str, Any]]:
    rows: list[dict[str, Any]] = []
    with path.open("r", encoding="utf-8") as handle:
        for line in handle:
            if line.strip():
                rows.append(json.loads(line))
    return rows


def write_jsonl(path: Path, rows: list[dict[str, Any]]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    tmp_path = path.with_suffix(path.suffix + ".tmp")
    with tmp_path.open("w", encoding="utf-8") as handle:
        for row in rows:
            handle.write(json.dumps(row, ensure_ascii=False, sort_keys=True) + "\n")
    tmp_path.replace(path)


def json_dump(path: Path, value: Any) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    tmp_path = path.with_suffix(path.suffix + ".tmp")
    with tmp_path.open("w", encoding="utf-8") as handle:
        json.dump(value, handle, ensure_ascii=False, indent=2, sort_keys=True)
    tmp_path.replace(path)


STANDARD_SAMPLE_OUTPUTS = (
    "target_visible_candidate.png",
    "target_visible_core.png",
    "target_uncertain.png",
    "obstacle_unfiltered_evidence.png",
    "obstacle_all_detected.png",
    "obstacle.png",
    "obstacle_red.png",
    "overlay.png",
    "quality.json",
)
OBSTACLE_EVIDENCE_POLICY_VERSION = 1


def load_resume_quality(sample_dir: Path, sample_id: str, backend_name: str) -> dict[str, Any] | None:
    quality_path = sample_dir / "quality.json"
    if not quality_path.is_file():
        return None
    if any(not (sample_dir / filename).is_file() for filename in STANDARD_SAMPLE_OUTPUTS):
        return None
    with quality_path.open("r", encoding="utf-8") as handle:
        quality = json.load(handle)
    if quality.get("sample_id") != sample_id:
        return None
    if quality.get("backend") != backend_name:
        return None
    # Old output roots did not distinguish raw prompt evidence from the
    # high-recall-but-sanity-filtered evidence used by later hidden completion.
    # Force an explicit rerun rather than silently reusing that weaker audit
    # trail after this schema change.
    if quality.get("obstacle_evidence_policy_version") != OBSTACLE_EVIDENCE_POLICY_VERSION:
        return None
    if quality.get("review_status") == "error" or quality.get("error"):
        return None
    return quality


def safe_name(text: str) -> str:
    return re.sub(r"[^a-z0-9]+", "_", text.lower()).strip("_")


def prompt_list(value: Any) -> list[str]:
    """Normalize a prompt list while accepting the legacy JSON shape."""
    if isinstance(value, str):
        raw = [value]
    elif isinstance(value, (list, tuple)):
        raw = value
    else:
        raw = []
    return list(dict.fromkeys(str(prompt).strip() for prompt in raw if str(prompt).strip()))


def obstacle_prompt_resolution(
    config: dict[str, Any], category: str, backend_name: str | None = None
) -> dict[str, Any]:
    """Resolve backend-aware obstacle prompts without breaking legacy configs.

    When a backend-specific list is present it replaces the corresponding
    legacy global/category list.  The other level still falls back separately,
    so a config may specialize only global prompts or only one category.  With
    ``backend_name=None`` the output is exactly the historical legacy union.
    """
    global_prompts = prompt_list(config.get("obstacle_prompts"))
    category_map = config.get("category_obstacle_prompts")
    category_prompts = prompt_list(
        category_map.get(category) if isinstance(category_map, dict) else None
    )
    global_source = "legacy.obstacle_prompts"
    category_source = "legacy.category_obstacle_prompts"
    if backend_name:
        by_backend = config.get("obstacle_prompts_by_backend")
        if isinstance(by_backend, dict) and backend_name in by_backend:
            global_prompts = prompt_list(by_backend.get(backend_name))
            global_source = f"obstacle_prompts_by_backend.{backend_name}"
        category_by_backend = config.get("category_obstacle_prompts_by_backend")
        backend_categories = (
            category_by_backend.get(backend_name)
            if isinstance(category_by_backend, dict)
            else None
        )
        if isinstance(backend_categories, dict) and category in backend_categories:
            category_prompts = prompt_list(backend_categories.get(category))
            category_source = (
                f"category_obstacle_prompts_by_backend.{backend_name}.{category}"
            )
    prompts = list(dict.fromkeys([*global_prompts, *category_prompts]))
    return {
        "backend": backend_name,
        "category": category,
        "global_source": global_source,
        "category_source": category_source,
        "prompts": prompts,
    }


def obstacle_prompts_for_category(
    config: dict[str, Any], category: str, backend_name: str | None = None
) -> list[str]:
    """Return backend-aware obstacle prompts, with legacy fallback support."""
    return list(obstacle_prompt_resolution(config, category, backend_name)["prompts"])


def as_mapping(value: Any) -> dict[str, Any]:
    return dict(value) if isinstance(value, dict) else {}


def merge_mappings(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]:
    """Small recursive merge for backend-specific evidence policy overrides."""
    result = dict(base)
    for key, value in override.items():
        if isinstance(value, dict) and isinstance(result.get(key), dict):
            result[key] = merge_mappings(dict(result[key]), value)
        else:
            result[key] = value
    return result


def merge_prompt_config(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]:
    """Merge a small, output-run-specific prompt overlay safely.

    Nested objects are merged, while prompt lists are appended in stable
    de-duplicated order.  This lets a finite remediation run add a narrowly
    justified prompt without copying or silently editing the frozen base
    configuration used by a control run.  Scalar policy values remain an
    explicit override, so their provenance can be locked by the Slurm wrapper.
    """
    result = dict(base)
    for key, value in override.items():
        current = result.get(key)
        if isinstance(current, dict) and isinstance(value, dict):
            result[key] = merge_prompt_config(dict(current), value)
        elif isinstance(current, list) and isinstance(value, list):
            result[key] = list(dict.fromkeys([*current, *value]))
        else:
            result[key] = value
    return result


def float_or_default(value: Any, default: float) -> float:
    try:
        return float(value)
    except (TypeError, ValueError):
        return default


def normalized_float_mapping(value: Any) -> dict[str, float]:
    mapping = as_mapping(value)
    return {
        str(key).strip().lower(): float_or_default(item, 0.0)
        for key, item in mapping.items()
        if str(key).strip()
    }


def normalized_override_mapping(value: Any) -> dict[str, dict[str, Any]]:
    mapping = as_mapping(value)
    return {
        str(key).strip().lower(): as_mapping(item)
        for key, item in mapping.items()
        if str(key).strip() and isinstance(item, dict)
    }


def resolve_obstacle_evidence_policy(
    config: dict[str, Any], backend_name: str
) -> dict[str, Any]:
    """Compile a serializable trusted-obstacle-evidence policy.

    New configs may use ``obstacle_evidence_policy`` and optional
    ``obstacle_evidence_policy_by_backend.<backend>``.  The aliases
    ``obstacle_all_detected_policy`` / ``..._by_backend`` are accepted for
    early experiment configs.  In a legacy config, the existing
    ``obstacle_maximum_area_ratio`` map becomes the per-prompt area cap,
    confidence filtering remains disabled, and a conservative route-like
    component guard protects downstream hidden completion from broad scene
    surfaces.
    """
    base = as_mapping(
        config.get("obstacle_evidence_policy", config.get("obstacle_all_detected_policy", {}))
    )
    by_backend = as_mapping(
        config.get(
            "obstacle_evidence_policy_by_backend",
            config.get("obstacle_all_detected_policy_by_backend", {}),
        )
    )
    backend_override = as_mapping(by_backend.get(backend_name))
    raw = merge_mappings(base, backend_override)
    legacy_maximums = normalized_float_mapping(config.get("obstacle_maximum_area_ratio"))
    maximums = dict(legacy_maximums)
    maximums.update(
        normalized_float_mapping(
            raw.get("maximum_area_ratio", raw.get("maximum_area_ratio_by_prompt", {}))
        )
    )
    prompt_overrides = normalized_override_mapping(
        raw.get("prompt_overrides", raw.get("prompts", {}))
    )
    confidence_by_prompt = normalized_float_mapping(
        raw.get("minimum_confidence_by_prompt", raw.get("minimum_prompt_confidence_by_prompt", {}))
    )
    route_like_raw = as_mapping(
        raw.get("route_like", raw.get("route_like_component_policy", {}))
    )
    # The default is intentionally conservative: broad horizontal/ground-like
    # components are rejected from trusted evidence, but remain visible in
    # obstacle_unfiltered_evidence.png for human recovery.
    route_like = {
        "enabled": bool(route_like_raw.get("enabled", True)),
        "minimum_bbox_width_ratio": float_or_default(
            route_like_raw.get("minimum_bbox_width_ratio"), 0.80
        ),
        "minimum_bbox_height_ratio": float_or_default(
            route_like_raw.get("minimum_bbox_height_ratio"), 0.12
        ),
        "minimum_component_area_ratio": float_or_default(
            route_like_raw.get("minimum_component_area_ratio"), 0.03
        ),
    }
    default_maximum = float_or_default(
        raw.get("default_maximum_area_ratio", maximums.get("default", 0.15)),
        0.15,
    )
    return {
        "version": OBSTACLE_EVIDENCE_POLICY_VERSION,
        "backend": backend_name,
        "source": {
            "global": "obstacle_evidence_policy" if base else "legacy_defaults",
            "backend_override_present": bool(backend_override),
            "legacy_obstacle_maximum_area_ratio_used": bool(legacy_maximums),
        },
        "minimum_component_area_pixels": max(
            1, int(float_or_default(raw.get("minimum_component_area_pixels"), 16.0))
        ),
        "minimum_component_area_ratio": max(
            0.0, float_or_default(raw.get("minimum_component_area_ratio"), 0.00002)
        ),
        "minimum_prompt_confidence": min(
            1.0,
            max(
                0.0,
                float_or_default(
                    raw.get(
                        "minimum_prompt_confidence",
                        raw.get("minimum_confidence", 0.0),
                    ),
                    0.0,
                ),
            ),
        ),
        "minimum_confidence_by_prompt": confidence_by_prompt,
        "default_maximum_area_ratio": min(1.0, max(0.0, default_maximum)),
        "maximum_area_ratio_by_prompt": maximums,
        "full_frame_area_ratio": min(
            1.0, max(0.0, float_or_default(raw.get("full_frame_area_ratio"), 0.90))
        ),
        "prompt_overrides": prompt_overrides,
        "route_like": route_like,
    }


def evidence_minimum_component_area(policy: dict[str, Any], image_area: int) -> int:
    return max(
        int(policy["minimum_component_area_pixels"]),
        int(round(image_area * float(policy["minimum_component_area_ratio"]))),
    )


def evidence_prompt_limits(prompt: str, policy: dict[str, Any]) -> tuple[float, float]:
    key = prompt.strip().lower()
    override = as_mapping(policy["prompt_overrides"].get(key))
    confidence = float_or_default(
        override.get(
            "minimum_confidence",
            policy["minimum_confidence_by_prompt"].get(key, policy["minimum_prompt_confidence"]),
        ),
        float(policy["minimum_prompt_confidence"]),
    )
    maximum_area = float_or_default(
        override.get(
            "maximum_area_ratio",
            policy["maximum_area_ratio_by_prompt"].get(
                key, policy["default_maximum_area_ratio"]
            ),
        ),
        float(policy["default_maximum_area_ratio"]),
    )
    return min(1.0, max(0.0, confidence)), min(1.0, max(0.0, maximum_area))


def save_mask(path: Path, mask: np.ndarray) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    Image.fromarray(mask.astype(np.uint8) * 255, mode="L").save(path)


def save_red_mask(path: Path, mask: np.ndarray) -> None:
    """Save an RGB review visualization while preserving obstacle.png as binary."""
    path.parent.mkdir(parents=True, exist_ok=True)
    color = np.zeros((*mask.shape, 3), dtype=np.uint8)
    color[mask] = (230, 45, 45)
    Image.fromarray(color, mode="RGB").save(path)


def remove_small_components(mask: np.ndarray, minimum_area: int) -> np.ndarray:
    count, labels, stats, _ = cv2.connectedComponentsWithStats(mask.astype(np.uint8), 8)
    output = np.zeros_like(mask, dtype=bool)
    for index in range(1, count):
        if int(stats[index, cv2.CC_STAT_AREA]) >= minimum_area:
            output |= labels == index
    return output


def retain_core_connected_union(union: np.ndarray, core: np.ndarray, minimum_area: int) -> np.ndarray:
    union = remove_small_components(union, minimum_area)
    count, labels, stats, _ = cv2.connectedComponentsWithStats(union.astype(np.uint8), 8)
    output = np.zeros_like(union, dtype=bool)
    for index in range(1, count):
        component = labels == index
        if int(stats[index, cv2.CC_STAT_AREA]) >= minimum_area and np.any(component & core):
            output |= component
    return output


def retain_nearby_obstacles(
    obstacle: np.ndarray,
    target: np.ndarray,
    minimum_area: int,
    maximum_area_ratio: float = 0.15,
) -> np.ndarray:
    """Keep compact obstacle components spatially adjacent to the target surface."""
    distance = max(9, int(round(min(target.shape) * 0.035)) | 1)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (distance, distance))
    nearby = cv2.dilate(target.astype(np.uint8), kernel).astype(bool)
    count, labels, stats, _ = cv2.connectedComponentsWithStats(obstacle.astype(np.uint8), 8)
    output = np.zeros_like(obstacle, dtype=bool)
    image_area = obstacle.size
    for index in range(1, count):
        area = int(stats[index, cv2.CC_STAT_AREA])
        component = labels == index
        if (
            area >= minimum_area
            and area / image_area <= maximum_area_ratio
            and np.any(component & nearby)
        ):
            output |= component
    return output


def retain_compact_obstacles(
    obstacle: np.ndarray,
    minimum_area: int,
    maximum_area_ratio: float = 0.12,
) -> np.ndarray:
    """Keep compact scene obstacles without requiring target adjacency."""
    count, labels, stats, _ = cv2.connectedComponentsWithStats(obstacle.astype(np.uint8), 8)
    output = np.zeros_like(obstacle, dtype=bool)
    image_area = obstacle.size
    for index in range(1, count):
        area = int(stats[index, cv2.CC_STAT_AREA])
        if minimum_area <= area <= image_area * maximum_area_ratio:
            output |= labels == index
    return output


def route_like_component_count(mask: np.ndarray, route_policy: dict[str, Any]) -> int:
    """Count broad ground/route-like components that must not seed hidden masks."""
    if not bool(route_policy.get("enabled", False)) or not np.any(mask):
        return 0
    count, _, stats, _ = cv2.connectedComponentsWithStats(mask.astype(np.uint8), 8)
    height, width = mask.shape
    route_like = 0
    for index in range(1, count):
        area = int(stats[index, cv2.CC_STAT_AREA])
        width_ratio = float(stats[index, cv2.CC_STAT_WIDTH] / max(width, 1))
        height_ratio = float(stats[index, cv2.CC_STAT_HEIGHT] / max(height, 1))
        area_ratio = float(area / max(mask.size, 1))
        if (
            width_ratio >= float(route_policy["minimum_bbox_width_ratio"])
            and height_ratio >= float(route_policy["minimum_bbox_height_ratio"])
            and area_ratio >= float(route_policy["minimum_component_area_ratio"])
        ):
            route_like += 1
    return route_like


def trusted_obstacle_prompt_evidence(
    *,
    prompt: str,
    group: str,
    prediction: PromptPrediction,
    minimum_component_area: int,
    policy: dict[str, Any],
) -> tuple[np.ndarray, np.ndarray, dict[str, Any]]:
    """Return all cleaned evidence and the subset safe to seed hidden completion.

    The first mask is intentionally high recall: it only removes microscopic
    components.  The second is accepted only if its prompt confidence and
    image-area sanity checks pass and it is not route-like.  Rejecting at the
    prompt level makes the audit rationale easy to understand and guarantees
    that a broad false positive cannot leak into ``obstacle_all_detected``.
    """
    cleaned = remove_small_components(prediction.mask, minimum_component_area)
    minimum_confidence, maximum_area_ratio = evidence_prompt_limits(prompt, policy)
    area_ratio = float(cleaned.mean())
    reasons: list[str] = []
    if not np.any(cleaned):
        reasons.append("empty_after_small_component_removal")
    if float(prediction.confidence) < minimum_confidence:
        reasons.append("confidence_below_minimum")
    if area_ratio > maximum_area_ratio:
        reasons.append("area_ratio_exceeds_prompt_maximum")
    if area_ratio > float(policy["full_frame_area_ratio"]):
        reasons.append("area_ratio_exceeds_full_frame_guard")
    route_like_count = route_like_component_count(cleaned, policy["route_like"])
    if route_like_count:
        reasons.append("route_like_component_detected")
    accepted = not reasons
    trusted = cleaned if accepted else np.zeros_like(cleaned)
    decision = {
        "prompt": prompt,
        "group": group,
        "confidence": round(float(prediction.confidence), 6),
        "instance_count": int(prediction.instance_count),
        "minimum_confidence": round(minimum_confidence, 6),
        "maximum_area_ratio": round(maximum_area_ratio, 6),
        "cleaned_pixels": int(cleaned.sum()),
        "cleaned_area_ratio": round(area_ratio, 6),
        "route_like_component_count": route_like_count,
        "decision": "accepted" if accepted else "rejected",
        "reasons": reasons,
        "trusted_pixels": int(trusted.sum()),
    }
    return cleaned, trusted, decision


def area_plausibility(area_ratio: float, minimum: float, maximum: float) -> float:
    if area_ratio <= 0 or area_ratio < minimum / 2 or area_ratio > min(1.0, maximum * 1.2):
        return 0.0
    if minimum <= area_ratio <= maximum:
        return 1.0
    if area_ratio < minimum:
        return max(0.0, area_ratio / minimum)
    return max(0.0, 1.0 - (area_ratio - maximum) / max(1.0 - maximum, 1e-6))


@dataclass
class PromptPrediction:
    mask: np.ndarray
    confidence: float
    instance_count: int
    boxes: list[list[float]]


class Sam3Backend:
    name = "sam3"

    def __init__(self, args: argparse.Namespace):
        repo = Path(args.sam3_repo).resolve()
        checkpoint = Path(args.sam3_checkpoint).resolve()
        if not repo.is_dir():
            raise FileNotFoundError(f"SAM 3 repository not found: {repo}")
        if not checkpoint.is_file():
            raise FileNotFoundError(
                f"SAM 3 checkpoint not found: {checkpoint}. Accept the model terms and download sam3.pt first."
            )
        sys.path.insert(0, str(repo))
        import torch
        from sam3.model.sam3_image_processor import Sam3Processor
        from sam3.model_builder import build_sam3_image_model

        self.torch = torch
        self.device = args.device
        self.autocast_device = str(args.device).split(":", 1)[0]
        self.autocast_dtype = torch.bfloat16
        model = build_sam3_image_model(
            device=args.device,
            checkpoint_path=str(checkpoint),
            load_from_HF=False,
            compile=args.compile,
        )
        self.processor = Sam3Processor(
            model,
            device=args.device,
            confidence_threshold=args.confidence_threshold,
        )
        self.state: dict[str, Any] | None = None

    def begin_image(self, image: Image.Image) -> None:
        with self.torch.autocast(
            device_type=self.autocast_device,
            dtype=self.autocast_dtype,
            enabled=self.autocast_device == "cuda",
        ):
            self.state = self.processor.set_image(image)

    def predict(self, prompt: str) -> PromptPrediction:
        if self.state is None:
            raise RuntimeError("begin_image must be called before predict")
        with self.torch.autocast(
            device_type=self.autocast_device,
            dtype=self.autocast_dtype,
            enabled=self.autocast_device == "cuda",
        ):
            output = self.processor.set_text_prompt(prompt, self.state)
        masks_tensor = output["masks"]
        scores_tensor = output["scores"]
        boxes_tensor = output["boxes"]
        if masks_tensor.numel() == 0:
            shape = (int(output["original_height"]), int(output["original_width"]))
            return PromptPrediction(np.zeros(shape, dtype=bool), 0.0, 0, [])
        masks = masks_tensor.detach().cpu().numpy().astype(bool)
        while masks.ndim > 3 and masks.shape[1] == 1:
            masks = masks[:, 0]
        union = np.any(masks, axis=0)
        scores = scores_tensor.detach().float().cpu().numpy()
        boxes = boxes_tensor.detach().float().cpu().numpy().tolist()
        return PromptPrediction(union, float(scores.max()), int(len(scores)), boxes)


class GroundedSamBackend:
    name = "grounded_sam"

    def __init__(self, args: argparse.Namespace):
        repo = Path(args.grounded_sam_repo).resolve()
        config = Path(args.grounding_dino_config).resolve()
        dino_checkpoint = Path(args.grounding_dino_checkpoint).resolve()
        sam_checkpoint = Path(args.sam_checkpoint).resolve()
        for path in (repo, config, dino_checkpoint, sam_checkpoint):
            if not path.exists():
                raise FileNotFoundError(f"Required Grounded-SAM path not found: {path}")
        sys.path[:0] = [str(repo), str(repo / "GroundingDINO"), str(repo / "segment_anything")]
        import torch
        import GroundingDINO.groundingdino.datasets.transforms as T
        from GroundingDINO.groundingdino.models import build_model
        from GroundingDINO.groundingdino.util.slconfig import SLConfig
        from GroundingDINO.groundingdino.util.utils import clean_state_dict
        from segment_anything import SamPredictor, sam_model_registry

        model_args = SLConfig.fromfile(str(config))
        model_args.device = args.device
        if args.bert_path:
            model_args.bert_base_uncased_path = str(Path(args.bert_path).resolve())
        self.torch = torch
        self.transforms = T.Compose(
            [
                T.RandomResize([800], max_size=1333),
                T.ToTensor(),
                T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
            ]
        )
        self.device = args.device
        self.box_threshold = args.box_threshold
        self.model = build_model(model_args)
        checkpoint = torch.load(str(dino_checkpoint), map_location="cpu", weights_only=False)
        self.model.load_state_dict(clean_state_dict(checkpoint["model"]), strict=False)
        self.model.to(args.device).eval()
        sam = sam_model_registry[args.sam_model_type](checkpoint=str(sam_checkpoint)).to(args.device)
        self.predictor = SamPredictor(sam)
        self.image_pil: Image.Image | None = None
        self.image_tensor = None
        self.image_rgb: np.ndarray | None = None

    def begin_image(self, image: Image.Image) -> None:
        self.image_pil = image
        self.image_rgb = np.asarray(image)
        self.image_tensor, _ = self.transforms(image, None)
        self.predictor.set_image(self.image_rgb)

    def predict(self, prompt: str) -> PromptPrediction:
        if self.image_pil is None or self.image_tensor is None or self.image_rgb is None:
            raise RuntimeError("begin_image must be called before predict")
        caption = prompt.lower().strip()
        if not caption.endswith("."):
            caption += "."
        with self.torch.inference_mode():
            outputs = self.model(self.image_tensor[None].to(self.device), captions=[caption])
        logits = outputs["pred_logits"].detach().cpu().sigmoid()[0]
        boxes = outputs["pred_boxes"].detach().cpu()[0]
        confidence = logits.max(dim=1).values
        keep = confidence > self.box_threshold
        boxes = boxes[keep]
        confidence = confidence[keep]
        height, width = self.image_rgb.shape[:2]
        if boxes.shape[0] == 0:
            return PromptPrediction(np.zeros((height, width), dtype=bool), 0.0, 0, [])
        scale = self.torch.tensor([width, height, width, height], dtype=boxes.dtype)
        boxes = boxes * scale
        boxes[:, :2] -= boxes[:, 2:] / 2
        boxes[:, 2:] += boxes[:, :2]
        transformed = self.predictor.transform.apply_boxes_torch(boxes, (height, width)).to(self.device)
        with self.torch.inference_mode():
            masks, sam_scores, _ = self.predictor.predict_torch(
                point_coords=None,
                point_labels=None,
                boxes=transformed,
                multimask_output=False,
            )
        masks_np = masks[:, 0].detach().cpu().numpy().astype(bool)
        combined_confidence = float(
            math.sqrt(float(confidence.max()) * float(sam_scores.detach().cpu().max()))
        )
        return PromptPrediction(
            np.any(masks_np, axis=0),
            combined_confidence,
            int(len(masks_np)),
            boxes.tolist(),
        )


def build_backend(args: argparse.Namespace):
    if args.backend == "sam3":
        return Sam3Backend(args)
    if args.backend == "grounded_sam":
        return GroundedSamBackend(args)
    raise ValueError(f"Unsupported backend: {args.backend}")


def prompt_consensus(
    predictions: list[PromptPrediction], minimum_area: int
) -> tuple[np.ndarray, np.ndarray, np.ndarray, dict[str, Any]]:
    if not predictions:
        raise ValueError("At least one prompt prediction is required")
    masks = np.stack([prediction.mask for prediction in predictions])
    votes = masks.sum(axis=0)
    union = votes > 0
    majority = max(2, math.ceil(len(predictions) / 2)) if len(predictions) > 1 else 1
    core = votes >= majority
    fallback = False
    if not np.any(core) and np.any(union):
        fallback = True
        best_index = int(np.argmax([prediction.confidence for prediction in predictions]))
        core = predictions[best_index].mask.copy()
    candidate = retain_core_connected_union(union, core, minimum_area)
    core = remove_small_components(core & candidate, minimum_area)
    uncertain = candidate & ~core
    union_area = int(candidate.sum())
    agreement = float(core.sum() / union_area) if union_area else 0.0
    confidences = [prediction.confidence for prediction in predictions]
    metadata = {
        "majority_vote": majority,
        "fallback_to_best_prompt": fallback,
        "agreement": round(agreement, 6),
        "mean_prompt_confidence": round(float(np.mean(confidences)), 6),
        "maximum_prompt_confidence": round(float(np.max(confidences)), 6),
        "detected_prompt_count": sum(value > 0 for value in confidences),
    }
    return candidate, core, uncertain, metadata


def overlay_masks(
    image: np.ndarray, target: np.ndarray, obstacle: np.ndarray, uncertain: np.ndarray
) -> np.ndarray:
    overlay = image.astype(np.float32).copy()
    colors = [
        (target, np.asarray([30, 210, 70], dtype=np.float32), 0.38),
        (uncertain, np.asarray([250, 210, 30], dtype=np.float32), 0.55),
        # Draw obstacles last so target/uncertainty colors never hide red occluders.
        (obstacle, np.asarray([230, 45, 45], dtype=np.float32), 0.58),
    ]
    for mask, color, alpha in colors:
        overlay[mask] = overlay[mask] * (1.0 - alpha) + color * alpha
    return np.clip(overlay, 0, 255).astype(np.uint8)


def score_category(
    mask: np.ndarray,
    consensus: dict[str, Any],
    thresholds: dict[str, float],
    category: str,
) -> tuple[float, dict[str, float]]:
    area_ratio = float(mask.mean())
    plausibility = area_plausibility(
        area_ratio,
        thresholds["minimum_target_area_ratio"],
        thresholds["maximum_target_area_ratio"],
    )
    bottom_contact = float(mask[int(mask.shape[0] * 0.8) :, :].mean() > 0.005)
    confidence = float(consensus["mean_prompt_confidence"])
    agreement = float(consensus["agreement"])
    evidence = 0.55 * confidence + 0.35 * agreement + 0.10 * bottom_contact
    score = evidence * (0.20 + 0.80 * plausibility)
    if category == "walkway":
        score *= 0.94  # prevent the generic class from dominating specific structures
    signals = {
        "area_ratio": round(area_ratio, 6),
        "area_plausibility": round(plausibility, 6),
        "bottom_contact": bottom_contact,
        "score": round(score, 6),
    }
    return score, signals


def predict_prompt_set(
    backend,
    prompts: list[str],
    output_dir: Path,
    minimum_component_area: int,
    save_prompt_masks: bool = True,
) -> tuple[np.ndarray, np.ndarray, np.ndarray, dict[str, Any]]:
    predictions = []
    prompt_metadata = []
    for prompt in prompts:
        prediction = backend.predict(prompt)
        predictions.append(prediction)
        if save_prompt_masks:
            save_mask(output_dir / f"{safe_name(prompt)}.png", prediction.mask)
        prompt_metadata.append(
            {
                "prompt": prompt,
                "confidence": round(prediction.confidence, 6),
                "instance_count": prediction.instance_count,
                "boxes_xyxy": prediction.boxes,
            }
        )
    candidate, core, uncertain, consensus = prompt_consensus(
        predictions, minimum_component_area
    )
    consensus["prompts"] = prompt_metadata
    return candidate, core, uncertain, consensus


def process_sample(
    row: dict[str, Any],
    backend,
    config: dict[str, Any],
    output_root: Path,
    args: argparse.Namespace,
) -> dict[str, Any]:
    sample_id = row["sample_id"]
    sample_dir = output_root / "samples" / sample_id
    quality_path = sample_dir / "quality.json"
    if args.resume:
        quality = load_resume_quality(sample_dir, sample_id, backend.name)
        if quality is not None:
            return quality

    # Phone photos commonly store the camera pixels in landscape orientation
    # and rely on EXIF to display them upright.  Normalize that orientation
    # before segmentation so every downstream mask uses the displayed frame.
    image = ImageOps.exif_transpose(Image.open(row["image_path"])).convert("RGB")
    rgb = np.asarray(image)
    image_area = rgb.shape[0] * rgb.shape[1]
    minimum_component_area = max(args.minimum_component_area, int(image_area * 0.0002))
    backend.begin_image(image)

    requested_categories = args.categories or list(config["categories"])
    fixed_category = row.get("category")
    if fixed_category:
        requested_categories = [fixed_category]
        if args.verify_manifest_category and not row.get("category_reviewed", False):
            requested_categories.extend(
                config.get("category_confusions", {}).get(fixed_category, [])
            )
            requested_categories = list(dict.fromkeys(requested_categories))

    category_results: dict[str, dict[str, Any]] = {}
    masks: dict[str, tuple[np.ndarray, np.ndarray, np.ndarray]] = {}
    thresholds = config["quality_thresholds"]
    save_full_artifacts = args.artifact_level == "full"
    for category in requested_categories:
        if category not in config["categories"]:
            raise ValueError(f"Unknown category in pilot manifest: {category}")
        prompt_dir = sample_dir / "prompts" / category
        candidate, core, uncertain, consensus = predict_prompt_set(
            backend,
            config["categories"][category]["prompts"],
            prompt_dir,
            minimum_component_area,
            save_prompt_masks=save_full_artifacts,
        )
        # --- per-category area cap (prevents whole-image over-segmentation) ---
        cat_max_area = config["categories"][category].get(
            "max_area_ratio",
            config.get("tactile_paving_max_area_ratio", None),
        )
        candidate_area_ratio = float(candidate.mean()) if candidate.any() else 0.0
        if cat_max_area is not None and candidate_area_ratio > float(cat_max_area):
            consensus["area_cap_rejected"] = True
            consensus["area_cap_ratio"] = round(candidate_area_ratio, 6)
            consensus["area_cap_limit"] = float(cat_max_area)
            candidate = np.zeros_like(candidate)
            core = np.zeros_like(core)
            uncertain = np.zeros_like(uncertain)
        else:
            consensus["area_cap_rejected"] = False
        # --- end area cap ---
        score, signals = score_category(candidate, consensus, thresholds, category)
        if save_full_artifacts:
            save_mask(sample_dir / "categories" / f"{category}_candidate.png", candidate)
            save_mask(sample_dir / "categories" / f"{category}_core.png", core)
            save_mask(sample_dir / "categories" / f"{category}_uncertain.png", uncertain)
        masks[category] = (candidate, core, uncertain)
        category_results[category] = {"consensus": consensus, **signals}

    best_scored_category = max(
        category_results, key=lambda key: category_results[key]["score"]
    )
    # A mask score is useful for flagging semantic disagreement, but it is not
    # reliable enough to silently overwrite a manifest category. Human review
    # overrides remain authoritative.
    selected_category = fixed_category or best_scored_category
    category_disagreement = bool(
        fixed_category and best_scored_category != fixed_category
    )
    target, target_core, target_uncertain = masks[selected_category]

    prompt_resolution = obstacle_prompt_resolution(
        config, selected_category, backend.name
    )
    obstacle_predictions: list[tuple[str, PromptPrediction]] = []
    obstacle_metadata = []
    for prompt in prompt_resolution["prompts"]:
        prediction = backend.predict(prompt)
        obstacle_predictions.append((prompt, prediction))
        if save_full_artifacts:
            save_mask(
                sample_dir / "prompts" / "obstacles" / f"{safe_name(prompt)}.png",
                prediction.mask,
            )
        obstacle_metadata.append(
            {
                "prompt": prompt,
                "group": "dynamic_or_compact",
                "confidence": round(prediction.confidence, 6),
                "instance_count": prediction.instance_count,
                "boxes_xyxy": prediction.boxes,
            }
        )
    maximum_ratios = config.get("obstacle_maximum_area_ratio", {})
    filtered_dynamic_obstacles = []
    for prompt, prediction in obstacle_predictions:
        cleaned = remove_small_components(prediction.mask, minimum_component_area)
        maximum_area_ratio = float(
            maximum_ratios.get(prompt, maximum_ratios.get("default", 0.15))
        )
        filtered_dynamic_obstacles.append(
            retain_nearby_obstacles(
                cleaned,
                target,
                minimum_component_area,
                maximum_area_ratio=maximum_area_ratio,
            )
        )
    dynamic_obstacle = (
        np.any(np.stack(filtered_dynamic_obstacles), axis=0)
        if filtered_dynamic_obstacles
        else np.zeros_like(target)
    )

    barrier_predictions: list[tuple[str, PromptPrediction]] = []
    barrier_backends = config.get("barrier_obstacle_backends", ["sam3"])
    barrier_prompts = (
        config.get("barrier_obstacle_prompts", [])
        if backend.name in barrier_backends
        else []
    )
    for prompt in barrier_prompts:
        prediction = backend.predict(prompt)
        barrier_predictions.append((prompt, prediction))
        if save_full_artifacts:
            save_mask(
                sample_dir / "prompts" / "obstacles" / f"{safe_name(prompt)}.png",
                prediction.mask,
            )
        obstacle_metadata.append(
            {
                "prompt": prompt,
                "group": "barrier_consensus",
                "confidence": round(prediction.confidence, 6),
                "instance_count": prediction.instance_count,
                "boxes_xyxy": prediction.boxes,
            }
        )
    if barrier_predictions:
        barrier_votes = np.stack(
            [prediction.mask for _, prediction in barrier_predictions]
        ).sum(axis=0)
        required_votes = max(2, math.ceil(len(barrier_predictions) / 2))
        barrier_obstacle = barrier_votes >= required_votes
        barrier_obstacle = retain_compact_obstacles(
            barrier_obstacle, minimum_component_area
        )
    else:
        barrier_obstacle = np.zeros_like(target)

    # Keep two explicit evidence layers.  The unfiltered union is an audit
    # artifact: every prompt after only microscopic-component removal.  The
    # trusted union applies per-prompt confidence, prompt-area, full-frame,
    # and broad-route sanity checks before it can seed a later hidden proposal.
    # ``obstacle.png`` below intentionally keeps its original nearby policy.
    evidence_policy = resolve_obstacle_evidence_policy(config, backend.name)
    evidence_minimum_area = evidence_minimum_component_area(evidence_policy, image_area)
    unfiltered_evidence_masks: list[np.ndarray] = []
    trusted_evidence_masks: list[np.ndarray] = []
    obstacle_evidence_decisions: list[dict[str, Any]] = []
    for group, predictions in (
        ("dynamic_or_compact", obstacle_predictions),
        ("barrier_consensus", barrier_predictions),
    ):
        for prompt, prediction in predictions:
            unfiltered, trusted, decision = trusted_obstacle_prompt_evidence(
                prompt=prompt,
                group=group,
                prediction=prediction,
                minimum_component_area=evidence_minimum_area,
                policy=evidence_policy,
            )
            unfiltered_evidence_masks.append(unfiltered)
            trusted_evidence_masks.append(trusted)
            obstacle_evidence_decisions.append(decision)
    obstacle_unfiltered_evidence = (
        np.any(np.stack(unfiltered_evidence_masks), axis=0)
        if unfiltered_evidence_masks
        else np.zeros_like(target)
    )
    obstacle_all_detected = (
        np.any(np.stack(trusted_evidence_masks), axis=0)
        if trusted_evidence_masks
        else np.zeros_like(target)
    )
    obstacle = dynamic_obstacle | barrier_obstacle
    target = target & ~obstacle
    target_core = target_core & target
    target_uncertain = target_uncertain & target

    kernel_size = max(5, int(round(min(rgb.shape[:2]) * 0.012)) | 1)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
    target_dilated = cv2.dilate(target.astype(np.uint8), kernel).astype(bool)
    obstacle_dilated = cv2.dilate(obstacle.astype(np.uint8), kernel).astype(bool)
    adjacency = target_dilated & obstacle_dilated
    adjacency_ratio = float(adjacency.sum() / max(target.sum(), 1))

    save_mask(sample_dir / "target_visible_candidate.png", target)
    save_mask(sample_dir / "target_visible_core.png", target_core)
    save_mask(sample_dir / "target_uncertain.png", target_uncertain)
    save_mask(sample_dir / "obstacle_unfiltered_evidence.png", obstacle_unfiltered_evidence)
    save_mask(sample_dir / "obstacle_all_detected.png", obstacle_all_detected)
    save_mask(sample_dir / "obstacle.png", obstacle)
    save_red_mask(sample_dir / "obstacle_red.png", obstacle)
    Image.fromarray(overlay_masks(rgb, target_core, obstacle, target_uncertain)).save(
        sample_dir / "overlay.png", quality=92
    )

    selected = category_results[selected_category]
    score = float(selected["score"])
    target_area_ratio = float(target.mean())
    uncertainty_ratio = float(target_uncertain.sum() / max(target.sum(), 1))
    obstacle_area_ratio = float(obstacle.mean())
    area_valid = (
        thresholds["minimum_target_area_ratio"]
        <= target_area_ratio
        <= thresholds["maximum_target_area_ratio"]
    )
    if not area_valid:
        review_status = "reject_or_reprompt"
    elif (
        score >= thresholds["auto_accept_score"]
        and not selected["consensus"]["fallback_to_best_prompt"]
        and uncertainty_ratio <= 0.35
        and obstacle_area_ratio <= 0.20
    ):
        review_status = "candidate_accept_after_visual_review"
    elif score >= thresholds["manual_review_score"]:
        review_status = "manual_review"
    else:
        review_status = "reject_or_reprompt"
    if (
        category_disagreement
        and not row.get("category_reviewed", False)
        and review_status == "candidate_accept_after_visual_review"
    ):
        review_status = "manual_review"
    quality = {
        "sample_id": sample_id,
        "image_path": row["image_path"],
        "backend": backend.name,
        "selected_category": selected_category,
        "category_verification": {
            "manifest_category": fixed_category,
            "best_scored_category": best_scored_category,
            "category_disagreement": category_disagreement,
            "category_reviewed": bool(row.get("category_reviewed", False)),
        },
        "quality_score": round(score, 6),
        "review_status": review_status,
        "target_area_ratio": round(target_area_ratio, 6),
        "target_core_ratio": round(float(target_core.mean()), 6),
        "uncertainty_ratio_within_target": round(
            uncertainty_ratio, 6
        ),
        "obstacle_area_ratio": round(obstacle_area_ratio, 6),
        "obstacle_unfiltered_evidence_pixels": int(obstacle_unfiltered_evidence.sum()),
        "obstacle_unfiltered_evidence_area_ratio": round(
            float(obstacle_unfiltered_evidence.mean()), 6
        ),
        "obstacle_all_detected_pixels": int(obstacle_all_detected.sum()),
        "obstacle_all_detected_area_ratio": round(float(obstacle_all_detected.mean()), 6),
        "obstacle_all_detected_policy": "sanitized_high_recall_prompt_union_v2",
        "obstacle_evidence_policy_version": OBSTACLE_EVIDENCE_POLICY_VERSION,
        "obstacle_evidence_minimum_component_area_pixels": evidence_minimum_area,
        "obstacle_evidence_artifact_semantics": {
            "obstacle_unfiltered_evidence.png": (
                "union of every obstacle/barrier prompt after only small-component removal; "
                "audit evidence only, never direct hidden-completion support"
            ),
            "obstacle_all_detected.png": (
                "high-recall prompt union after confidence, per-prompt area, full-frame, "
                "and route-like sanity filters; candidate evidence for later hidden completion"
            ),
            "obstacle.png": (
                "unchanged nearby/compact obstacle proposal used for visible-mask cleanup; "
                "not the high-recall evidence layer"
            ),
        },
        "obstacle_prompt_resolution": prompt_resolution,
        "obstacle_evidence_policy": evidence_policy,
        "obstacle_evidence_prompt_decisions": obstacle_evidence_decisions,
        "obstacle_evidence_decision_counts": dict(
            sorted(Counter(item["decision"] for item in obstacle_evidence_decisions).items())
        ),
        "barrier_obstacle_area_ratio": round(float(barrier_obstacle.mean()), 6),
        "target_obstacle_adjacency_ratio": round(adjacency_ratio, 6),
        "category_results": category_results,
        "obstacle_prompts": obstacle_metadata,
        "automatic_mask_is_ground_truth": False,
        "artifact_level": args.artifact_level,
    }
    json_dump(quality_path, quality)
    return quality


def build_html_report(output_dir: Path, results: list[dict[str, Any]]) -> None:
    rows = []
    for result in sorted(results, key=lambda item: item.get("quality_score", -1), reverse=True):
        sample_id = result["sample_id"]
        overlay = f"samples/{sample_id}/overlay.png"
        error = result.get("error")
        details = html.escape(error) if error else (
            f'{html.escape(result["selected_category"])} | '
            f'score={result["quality_score"]:.3f} | '
            f'{html.escape(result["review_status"])}'
        )
        rows.append(
            f'<article><img src="{html.escape(overlay)}" loading="lazy">'
            f'<div><strong>{html.escape(sample_id)}</strong><br>{details}</div></article>'
        )
    document = """<!doctype html><meta charset="utf-8"><title>Accessibility mask pilot</title>
<style>body{font-family:sans-serif;margin:20px}main{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:16px}article{border:1px solid #bbb;padding:8px}img{width:100%;height:auto}div{margin-top:6px;font-size:14px}</style>
<h1>Accessibility mask pilot</h1><p>Green: consensus core; yellow: uncertain target; red: obstacle. All masks require visual review.</p><main>"""
    document += "\n".join(rows) + "</main>"
    (output_dir / "report.html").write_text(document, encoding="utf-8")


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--pilot-manifest", default="output/accessibility_clip_screen/pilot_manifest.jsonl")
    parser.add_argument("--image", default=None, help="Single-image mode input image; bypasses --pilot-manifest.")
    parser.add_argument("--single-sample-id", default=None, help="Stable output ID used with --image.")
    parser.add_argument(
        "--single-category",
        choices=["curb_cut", "ramp", "stairs", "tactile_paving", "walkway"],
        default=None,
        help="Known category for --image. Omit to score all configured categories.",
    )
    parser.add_argument("--prompt-config", default="configs/accessibility_mask_prompts.json")
    parser.add_argument(
        "--prompt-config-override",
        default=None,
        help=(
            "Optional JSON overlay merged into --prompt-config for this output-only run. "
            "Nested prompt lists are appended and de-duplicated; the base config is never edited."
        ),
    )
    parser.add_argument("--output-dir", default="output/accessibility_mask_proposals/sam3")
    parser.add_argument("--backend", choices=["sam3", "grounded_sam"], default="sam3")
    parser.add_argument("--device", default="cuda")
    parser.add_argument("--categories", nargs="*", default=None)
    parser.add_argument("--limit", type=int, default=0)
    parser.add_argument("--minimum-component-area", type=int, default=128)
    parser.add_argument("--confidence-threshold", type=float, default=0.38)
    parser.add_argument("--compile", action="store_true")
    parser.add_argument("--resume", action="store_true")
    parser.add_argument(
        "--artifact-level",
        choices=["full", "standard"],
        default="full",
        help="standard omits regenerable per-prompt/category PNGs for large batch runs.",
    )
    parser.add_argument(
        "--verify-manifest-category",
        action="store_true",
        help="Compare configured confusable categories unless category_reviewed=true.",
    )
    parser.add_argument(
        "--allow-test-split",
        action="store_true",
        help=(
            "Explicitly allow frozen test RGB rows for a pre-registered front-end "
            "inference run. Default behavior remains validation-only."
        ),
    )
    parser.add_argument("--sam3-repo", default="repos/sam3")
    parser.add_argument("--sam3-checkpoint", default="weights/sam3/sam3.pt")
    parser.add_argument(
        "--grounded-sam-repo",
        default="repos/grounded-sam-compat",
    )
    parser.add_argument(
        "--grounding-dino-config",
        default="repos/grounded-sam-compat/GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py",
    )
    parser.add_argument(
        "--grounding-dino-checkpoint",
        default="weights/grounded-sam/groundingdino_swint_ogc.pth",
    )
    parser.add_argument(
        "--sam-checkpoint",
        default="weights/grounded-sam/sam_vit_h_4b8939.pth",
    )
    parser.add_argument("--sam-model-type", choices=["vit_h", "vit_l", "vit_b"], default="vit_h")
    parser.add_argument("--bert-path", default=None)
    parser.add_argument("--box-threshold", type=float, default=0.28)
    return parser


def main() -> int:
    args = build_parser().parse_args()
    config_path = Path(args.prompt_config).resolve()
    config_override_path = (
        Path(args.prompt_config_override).resolve()
        if args.prompt_config_override
        else None
    )
    output_dir = Path(args.output_dir).resolve()
    output_dir.mkdir(parents=True, exist_ok=True)
    with config_path.open("r", encoding="utf-8") as handle:
        config = json.load(handle)
    if not isinstance(config, dict):
        raise ValueError(f"prompt config must be a JSON object: {config_path}")
    if config_override_path is not None:
        with config_override_path.open("r", encoding="utf-8") as handle:
            override = json.load(handle)
        if not isinstance(override, dict):
            raise ValueError(f"prompt config override must be a JSON object: {config_override_path}")
        config = merge_prompt_config(config, override)
    if args.image:
        image_path = Path(args.image).expanduser().resolve()
        if not image_path.is_file():
            raise FileNotFoundError(image_path)
        sample_id = args.single_sample_id or safe_name(image_path.stem) or "single_image"
        rows = [
            {
                "sample_id": sample_id,
                "image_path": str(image_path),
                "category": args.single_category,
                "category_reviewed": bool(args.single_category),
            }
        ]
    else:
        pilot_path = Path(args.pilot_manifest).resolve()
        rows = read_jsonl(pilot_path)
    if args.limit > 0:
        rows = rows[: args.limit]
    allowed_splits = {None, "validation"}
    if args.allow_test_split:
        allowed_splits.add("test")
    invalid_splits = sorted(
        {str(row.get("benchmark_split")) for row in rows if row.get("benchmark_split") not in allowed_splits}
    )
    if invalid_splits:
        raise ValueError(
            "Proposal manifest contains unsupported benchmark split(s): "
            f"{invalid_splits}. Pass --allow-test-split only for an explicitly "
            "registered test front-end inference run."
        )
    test_accessed = any(row.get("benchmark_split") == "test" for row in rows)
    backend = None
    initialization_error = None
    try:
        backend = build_backend(args)
        if backend.torch.cuda.is_available():
            backend.torch.cuda.reset_peak_memory_stats()
    except Exception as exc:
        traceback.print_exc()
        initialization_error = f"{type(exc).__name__}: {exc}"
    results = []
    if initialization_error is not None:
        for row in rows:
            results.append(
                {
                    "sample_id": row["sample_id"],
                    "image_path": row["image_path"],
                    "backend": args.backend,
                    "quality_score": -1.0,
                    "review_status": "error",
                    "error": f"model_initialization_failed: {initialization_error}",
                }
            )
        write_jsonl(output_dir / "results.jsonl", results)
    else:
        assert backend is not None
        for index, row in enumerate(rows, start=1):
            print(f"[{index}/{len(rows)}] {row['sample_id']}", flush=True)
            try:
                result = process_sample(row, backend, config, output_dir, args)
            except Exception as exc:
                traceback.print_exc()
                result = {
                    "sample_id": row["sample_id"],
                    "image_path": row["image_path"],
                    "backend": args.backend,
                    "quality_score": -1.0,
                    "review_status": "error",
                    "error": f"{type(exc).__name__}: {exc}",
                }
            results.append(result)
            write_jsonl(output_dir / "results.jsonl", results)
    build_html_report(output_dir, results)
    failed = sum(item.get("review_status") == "error" for item in results)
    peak_bytes = (
        int(backend.torch.cuda.max_memory_allocated())
        if backend is not None and backend.torch.cuda.is_available()
        else 0
    )
    summary = {
        "backend": args.backend,
        "sample_count": len(results),
        "completed": len(results) - failed,
        "failed": failed,
        "failure_rate": failed / len(results) if results else 1.0,
        "status_counts": dict(
            sorted(
                {
                    status: sum(item.get("review_status") == status for item in results)
                    for status in {item.get("review_status") for item in results}
                }.items()
            )
        ),
        "mean_quality_score": round(
            float(np.mean([item["quality_score"] for item in results if item["quality_score"] >= 0])),
            6,
        )
        if any(item["quality_score"] >= 0 for item in results)
        else None,
        "automatic_masks_are_ground_truth": False,
        "prompt_config": str(config_path),
        "prompt_config_override": str(config_override_path) if config_override_path else None,
        "model_initialization_error": initialization_error,
        "peak_gpu_memory_bytes": peak_bytes,
        "peak_gpu_memory_gib": peak_bytes / (1024**3),
        "environment": {
            "python": sys.version,
            "executable": sys.executable,
            "torch": getattr(getattr(backend, "torch", None), "__version__", None),
            "cuda_runtime": getattr(getattr(backend, "torch", None), "version", None).cuda
            if backend is not None
            else None,
            "gpu_name": backend.torch.cuda.get_device_name(0)
            if backend is not None and backend.torch.cuda.is_available()
            else None,
            "slurm_job_id": os.environ.get("SLURM_JOB_ID"),
        },
        "resolved_arguments": vars(args),
        "test_accessed": test_accessed,
        "test_access_explicitly_allowed": bool(args.allow_test_split),
    }
    json_dump(output_dir / "summary.json", summary)
    print(json.dumps(summary, ensure_ascii=False, indent=2))
    return 0 if all(item.get("review_status") != "error" for item in results) else 2


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