sekkit
/

File size: 70,672 Bytes
00f7cea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
"""Single-file OvisOCR3 document parsing and cross-page table reconstruction.

Reading guide: public entry point -> document pipeline -> text/table helpers ->
model backend -> browser renderer. Import the text helpers without installing
vLLM or browser dependencies; those are loaded only when used.

Algorithm: parse pages independently, then visit adjacent table-bearing pages
from last to first. Write each merged table into the earlier page and render
that updated page before its next prediction.
 Keep non-table source untouched.

Support: the document pipeline accepts one merged table per adjacent pair and
binds the earlier page's last table to the later page's first table. Multiple
merges need explicit fragment bindings via apply_pair(); nested or incomplete
HTML tables are rejected on writeback. This is a conservative binding policy,
not a general table-fragment matching algorithm.

Page numbers are 1-based; table fragment indices are 0-based. Model predictions
use pair-local pages 1 and 2; saved Markdown and provenance use document pages.
Raw predictions, intermediate files and partial results are retained.
"""

from __future__ import annotations

import argparse
import base64
import copy
import html
import io
import json
import math
import re
import time
import traceback
import uuid
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Callable, Mapping, NamedTuple

NEGATIVE = "No cross-page table exists between these pages."
TABLE = re.compile(r"<table\b[^>]*>.*?</table\s*>", re.I | re.S)
IMAGE_REF = re.compile(
    r"(?<![\w/])(?:page_(\d+)/)?images/bbox_(\d+)_(\d+)_(\d+)_(\d+)\.jpg"
)
REF = re.compile(r"page_(\d+)/images/")
CATEGORIES = set(
    "abstract algorithm aside_text chart content display_formula doc_title figure_title footer footer_image footnote formula_number header header_image image inline_formula number paragraph_title reference_content seal table text vertical_text vision_footnote".split()
)

FragmentId = tuple[int, int]
Backend = Callable[[list[str], str, Path, int], tuple[str, str]]


class GenerationResult(NamedTuple):
    """Tuple-compatible raw model output; cleaning belongs to the page adapter."""

    text: str
    finish_reason: str


class RenderResult(NamedTuple):
    """Tuple-compatible render result; aliases map rendered crops to originals."""

    image_path: str
    rewritten_source: str
    alias_to_original: dict[str, str]
    diagnostics: dict


@dataclass
class PageState:
    """One document page and the original fragments behind its current tables."""

    number: int
    image_path: str
    original_markdown: str
    markdown: str
    finish_reason: str
    parse_diagnostics: dict | None = None
    table_sources: list[set[FragmentId]] = field(init=False)

    def __post_init__(self):
        self.table_sources = [
            {(self.number, index)}
            for index, _ in enumerate(TABLE.finditer(self.markdown))
        ]


@dataclass
class PairInput:
    """Two model inputs with explicit document/pair-local coordinate mapping."""

    page_numbers: tuple[int, int]
    image_paths: list[str]
    markdowns: list[str]
    alias_to_original: dict[str, str]
    rendered_pages: set[int]


@dataclass
class DocumentState:
    pages: list[PageState] = field(default_factory=list)
    chunks: list[list[int]] = field(default_factory=list)
    steps: list[dict] = field(default_factory=list)
    merged_tables: list[dict] = field(default_factory=list)
    parse_warnings: list[dict] = field(default_factory=list)


# Public entry point and document pipeline.


def reconstruct_document(
    image_paths,
    output,
    *,
    model_path=".",
    stage1_prompt="document_structured",
    mathjax=None,
    backend: Backend | None = None,
    renderer=None,
):
    """Reconstruct ordered page images into a document, keeping all evidence.

    Use a fresh output directory. Stage 1 accepts document_markdown[.txt] or
    document_structured[.txt]; prompts are loaded from model_path/prompts.
    Returns the saved result.json dictionary. Truncated generations, recovered
    JSON or invalid structured fields return PARTIAL with issue-specific warnings;
    other processing failures save partial output and raise RuntimeError.

    For research, inject backend(paths, prompt, folder, stage) -> (raw, reason)
    and/or renderer.render(source, owner, paths, folder, name) -> RenderResult.
    Injected renderers remain caller-owned. Defaults load vLLM and Chromium lazily.
    Engine settings, prompt bytes and renderer resize policy are unchanged.
    """
    stage1_prompt = stage1_prompt.removesuffix(".txt")
    if stage1_prompt not in {"document_markdown", "document_structured"}:
        raise ValueError("Choose document_markdown or document_structured")
    images = _document_images(image_paths)
    if Path(output).exists():
        raise FileExistsError("Use a fresh output directory")

    prompt_dir = Path(model_path) / "prompts"
    page_prompt = (prompt_dir / f"{stage1_prompt}.txt").read_text().removesuffix("\n")
    table_prompt = (prompt_dir / "crosspage_table.txt").read_text().removesuffix("\n")
    if backend is None:
        backend = VllmBackend(model_path)
    owns_renderer = renderer is None
    if owns_renderer:
        renderer = _BrowserRenderer(
            mathjax or Path(__file__).resolve().parent / "assets/mathjax.js"
        )
    try:
        result = _run_document(
            images,
            output,
            backend,
            renderer,
            page_prompt=page_prompt,
            table_prompt=table_prompt,
            stage1_prompt=stage1_prompt,
        )
    finally:
        if owns_renderer:
            renderer.close()

    if result["error"] and not result["error"].get("recoverable", False):
        raise RuntimeError(f"Document incomplete; inspect {output}/failure.json")
    return result


def _run_document(
    image_paths, output, backend, renderer, *, page_prompt, table_prompt, stage1_prompt
):
    """Parse pages, reconstruct adjacent pairs backwards, then persist results.

    Recovery is confined to Stage 1 output parsing. A failed window stops further
    reconstruction while preserving earlier pages, merges and failure details.
    No benchmark labels are used. The output directory must not already exist.
    """
    images = _document_images(image_paths)
    output = Path(output)
    output.mkdir(parents=True, exist_ok=False)
    save(
        output / "inputs.json",
        {
            "images": images,
            "stage1_prompt": stage1_prompt,
            "page_prompt": page_prompt,
            "table_prompt": table_prompt,
        },
    )
    state = DocumentState()
    error = None
    context = {"stage": 1, "pages": []}
    try:
        for number, image in enumerate(images, 1):
            context = {"stage": 1, "pages": [number]}
            page = _parse_page(
                number, image, output, backend, page_prompt, stage1_prompt
            )
            state.pages.append(page)
            warning = _page_warning(page)
            if warning:
                state.parse_warnings.append(warning)

        state.chunks = find_chunks([page.markdown for page in state.pages])
        save(
            output / "chunks.json",
            {
                "chunks": state.chunks,
                "basis": "Stage 1 predicted HTML tables only",
            },
        )
        for left, right in _adjacent_pairs(state.chunks):
            context = {"stage": 2, "pages": [left, right]}
            folder = output / "stage2" / f"pair_{left:04d}_{right:04d}"
            folder.mkdir(parents=True)
            pair = _prepare_pair(
                state.pages[left - 1 : right], images, folder, renderer
            )
            tables = _predict_pair(pair, folder, backend, table_prompt)
            _apply_merge(state, pair, tables)
            state.steps.append(
                {
                    "pages": list(pair.page_numbers),
                    "merged": bool(tables),
                    "rendered_pages": sorted(pair.rendered_pages),
                }
            )
            save(
                folder / "result.json",
                {
                    "tables": tables,
                    "pages": [
                        state.pages[number - 1].markdown for number in pair.page_numbers
                    ],
                    "groups": copy.deepcopy(state.merged_tables),
                },
            )
    except Exception as exc:
        error = {
            "type": type(exc).__name__,
            "message": str(exc),
            "recoverable": False,
            **context,
        }
        save(output / "failure.json", {**error, "traceback": traceback.format_exc()})
    return _finalize_document(state, images, output, error)


def _parse_page(number, image, output, backend, page_prompt, stage1_prompt):
    """Retain raw output, apply the existing cleanup, then adapt the page format."""
    folder = output / "stage1" / f"page_{number:04d}"
    folder.mkdir(parents=True)
    generation = GenerationResult(*backend([image], page_prompt, folder, 1))
    raw, reason = generation
    (folder / "raw.txt").write_text(raw)
    cleaned = _clean_truncated_repeats(raw.strip())
    save(
        folder / "generation.json",
        {
            "finish_reason": reason,
            "complete": reason == "stop",
            "tail_changed": cleaned != raw.strip(),
            "raw_chars": len(raw.strip()),
            "cleaned_chars": len(cleaned),
        },
    )
    (folder / "cleaned_raw.txt").write_text(cleaned)
    if reason not in ("stop", "length"):
        raise ValueError(f"Stage 1 page {number}: finish_reason={reason}")

    diagnostics = None
    if stage1_prompt == "document_structured":
        projected, blocks, diagnostics = structured_to_markdown(cleaned)
        save(folder / "blocks.json", blocks)
        save(folder / "adapter.json", diagnostics)
        (folder / "projected.md").write_text(projected)
        markdown = page_markdown(projected, number) if projected.strip() else ""
    else:
        markdown = page_markdown(cleaned, number)
    (folder / "page.md").write_text(markdown)
    return PageState(number, image, markdown, markdown, reason, diagnostics)


def _page_warning(page):
    """Separate generation truncation, JSON recovery and structured schema issues."""
    codes = []
    if page.finish_reason == "length":
        codes.append("generation_truncated")
    diagnostics = page.parse_diagnostics
    if diagnostics is not None:
        if not diagnostics["json_complete"]:
            codes.append("json_incomplete")
        if diagnostics["schema_issues"]:
            codes.append("schema_invalid")
        if (
            diagnostics["invalid_content_objects"]
            or not diagnostics["emitted_contents"]
        ):
            codes.append("content_invalid")
    if not codes:
        return None
    warning = {"page": page.number, "finish_reason": page.finish_reason, "codes": codes}
    if diagnostics is not None:
        warning["diagnostics"] = diagnostics
    return warning


def _adjacent_pairs(chunks):
    """Yield (2, 3), then (1, 2) for a three-page table-bearing run."""
    for chunk in chunks:
        for left in range(chunk[-1] - 1, chunk[0] - 1, -1):
            yield left, left + 1


def _prepare_pair(pages, images, folder, renderer):
    page_numbers = tuple(page.number for page in pages)
    document_to_local = {number: local for local, number in enumerate(page_numbers, 1)}
    paths, markdowns, aliases, rendered_pages = [], [], {}, set()
    for page in pages:
        path, markdown = page.image_path, page.markdown
        if markdown != page.original_markdown:
            rendered = RenderResult(
                *renderer.render(
                    markdown, page.number, images, folder, f"page_{page.number:04d}"
                )
            )
            path, markdown = rendered.image_path, rendered.rewritten_source
            aliases.update(rendered.alias_to_original)
            rendered_pages.add(page.number)
        paths.append(path)
        markdowns.append(remap(markdown, document_to_local))
    return PairInput(page_numbers, paths, markdowns, aliases, rendered_pages)


def _predict_pair(pair, folder, backend, table_prompt):
    page_context = "\n\n".join(
        f'<INPUT_PAGE index="{index}">\n{markdown}\n</INPUT_PAGE>'
        for index, markdown in enumerate(pair.markdowns, 1)
    )
    prompt = table_prompt.removesuffix("\n") + "\n\nInput Markdown:\n\n" + page_context
    if all(TABLE.search(markdown) for markdown in pair.markdowns):
        generation = GenerationResult(*backend(pair.image_paths, prompt, folder, 2))
    else:
        generation = GenerationResult(
            json.dumps([{"source_pages": [], "html": NEGATIVE}]), "stop"
        )
        save(
            folder / "gate_skip.json",
            {"reason": "README table gate", "model_called": False},
        )
    (folder / "raw.txt").write_text(generation.text)
    save(folder / "generation.json", {"finish_reason": generation.finish_reason})
    if generation.finish_reason != "stop":
        raise ValueError(
            f"Stage 2 pair {list(pair.page_numbers)}: finish_reason={generation.finish_reason}"
        )
    tables = parse_tables(generation.text)
    if len(tables) > 1:
        raise ValueError(
            "Multiple merges need explicit fragment bindings; output retained."
        )
    return [
        dict(table, html=_restore_image_refs(table["html"], pair)) for table in tables
    ]


def _restore_image_refs(table_html, pair):
    """Restore document page IDs, then resolve crops from re-rendered pages exactly."""
    local_to_document = {
        local: number for local, number in enumerate(pair.page_numbers, 1)
    }
    table_html = remap(table_html, local_to_document)

    def restore(match):
        reference = match[0]
        if match[1] is None:
            raise ValueError("Output crop lacks a page number.")
        if int(match[1]) in pair.rendered_pages:
            if reference not in pair.alias_to_original:
                raise ValueError("Unmappable crop from a rendered page: " + reference)
            return pair.alias_to_original[reference]
        return reference

    return IMAGE_REF.sub(restore, table_html)


def _apply_merge(state, pair, tables):
    """Validate writeback before updating page contents or fragment provenance."""
    earlier, later = (state.pages[number - 1] for number in pair.page_numbers)
    before = (earlier.markdown, later.markdown)
    after = apply_pair(before, tables)
    if any(TABLE.sub("", old) != TABLE.sub("", new) for old, new in zip(before, after)):
        raise ValueError("Writeback changed non-table content.")
    if tables:
        sources = earlier.table_sources[-1] | later.table_sources[0]
        state.merged_tables = [
            group
            for group in state.merged_tables
            if not sources.intersection(map(tuple, group["fragment_ids"]))
        ]
        state.merged_tables.append(
            {
                "source_pages": sorted({number for number, _ in sources}),
                "fragment_ids": sorted(sources),
                "html": tables[0]["html"],
            }
        )
        earlier.table_sources[-1] = sources
        del later.table_sources[0]
    earlier.markdown, later.markdown = after


def _save_document_crops(markdown, images, output):
    """Materialize page-qualified references from the original input images."""
    from PIL import Image

    errors = []
    references = {}
    for match in IMAGE_REF.finditer(markdown):
        references.setdefault(match[1], {})[match[0]] = match.groups()[1:]
    for page, crops in references.items():
        try:
            if page is None or not 1 <= int(page) <= len(images):
                raise ValueError("Image reference needs a valid source page number")
            with Image.open(images[int(page) - 1]) as source:
                for reference, coordinates in crops.items():
                    try:
                        left, top, right, bottom = map(int, coordinates)
                        width, height = source.size
                        box = (
                            max(0, min(width, round(left * width / 1000))),
                            max(0, min(height, round(top * height / 1000))),
                            max(0, min(width, round(right * width / 1000))),
                            max(0, min(height, round(bottom * height / 1000))),
                        )
                        if box[2] <= box[0] or box[3] <= box[1]:
                            raise ValueError("Image crop has zero or negative pixel area")
                        target = output / reference
                        target.parent.mkdir(parents=True, exist_ok=True)
                        source.crop(box).convert("RGB").save(target)
                    except Exception as exc:
                        errors.append({"reference": reference, "message": str(exc)})
        except Exception as exc:
            errors.extend(
                {"reference": reference, "message": str(exc)} for reference in crops
            )
    return errors


def _finalize_document(state, images, output, error):
    document = "\n\n".join(page.markdown for page in state.pages) + "\n"
    crop_errors = _save_document_crops(document, images, output)
    for page in state.pages:
        target = output / "pages" / f"page_{page.number:04d}.md"
        target.parent.mkdir(exist_ok=True)
        # Per-page Markdown is one directory below the shared crop assets.
        target.write_text(IMAGE_REF.sub(lambda m: "../" + m[0], page.markdown))
    (output / "document.md").write_text(document)
    if crop_errors and error is None:
        error = {"type": "ImageCropExportFailed", "recoverable": False}
        save(output / "failure.json", {**error, "image_export_errors": crop_errors})
    if error is None and state.parse_warnings:
        error = {
            "type": "Stage1OutputIncomplete",
            "recoverable": True,
            "pages": [warning["page"] for warning in state.parse_warnings],
        }
    result = {
        "status": "COMPLETE" if error is None else "PARTIAL",
        "error": error,
        "input_pages": len(images),
        "parsed_pages": len(state.pages),
        "chunks": state.chunks,
        "steps": state.steps,
        "merged_tables": state.merged_tables,
        "stage1_parse_warnings": state.parse_warnings,
        "image_export_errors": crop_errors,
    }
    save(output / "result.json", result)
    return result


# Reusable text adapters and table operations (standard library only).


def _document_images(image_paths):
    images = [str(Path(path).resolve()) for path in image_paths]
    if not images or not all(Path(path).is_file() for path in images):
        raise ValueError("Provide existing page images in reading order.")
    return images


def save(path, value):
    """Write a JSON artifact, creating its parent directory when needed."""
    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n")


def _is_valid_bbox(value):
    if not isinstance(value, list) or len(value) != 4:
        return False
    if not all(type(coordinate) is int for coordinate in value):
        return False
    left, top, right, bottom = value
    return 0 <= left <= right < 1000 and 0 <= top <= bottom < 1000


def _recover_json_prefix(text):
    """Return only complete prefix objects; never guess or repair JSON syntax."""
    blocks, unparsed_tail_chars = [], 0
    if not text.startswith("["):
        return blocks, unparsed_tail_chars
    position = 1
    decoder = json.JSONDecoder()
    while position < len(text):
        while position < len(text) and text[position].isspace():
            position += 1
        if position == len(text) or text[position] == "]":
            break
        try:
            block, end = decoder.raw_decode(text, position)
        except json.JSONDecodeError:
            unparsed_tail_chars = len(text) - position
            break
        blocks.append(block)
        position = end
        while position < len(text) and text[position].isspace():
            position += 1
        if position < len(text) and text[position] == ",":
            position += 1
        elif position < len(text) and text[position] != "]":
            unparsed_tail_chars = len(text) - position
            break
    return blocks, unparsed_tail_chars


def structured_to_markdown(text):
    """Project content in emitted order, retaining diagnostics for incomplete JSON.

    Preserve every category and keep only complete prefix objects on JSON failure.
    Invalid bbox/category fields are reported without discarding valid content.
    """
    stripped = text.strip()
    if stripped.startswith("```") and stripped.endswith("```"):
        stripped = re.sub(r"^```(?:json)?\s*\n|\n```$", "", stripped, flags=re.I)
    diagnostics = {
        "json_complete": False,
        "schema_issues": [],
        "invalid_content_objects": [],
        "unparsed_tail_chars": 0,
    }
    try:
        blocks = json.loads(stripped)
        if not isinstance(blocks, list):
            raise ValueError("Expected one JSON array")
        diagnostics["json_complete"] = True
    except ValueError as exc:
        diagnostics["parse_error"] = str(exc)
        blocks, diagnostics["unparsed_tail_chars"] = _recover_json_prefix(stripped)

    contents = []
    for index, block in enumerate(blocks):
        if not isinstance(block, dict) or not isinstance(block.get("content"), str):
            diagnostics["invalid_content_objects"].append(index)
            continue
        contents.append(block["content"])
        if not _is_valid_bbox(block.get("bbox")):
            diagnostics["schema_issues"].append({"index": index, "field": "bbox"})
        if (
            not isinstance(block.get("category"), str)
            or block["category"] not in CATEGORIES
        ):
            diagnostics["schema_issues"].append({"index": index, "field": "category"})
    diagnostics.update(
        parsed_objects=len(blocks),
        emitted_contents=len(contents),
        policy="join content in emitted array order; preserve every category; retain complete prefix objects only",
    )
    diagnostics["valid"] = (
        diagnostics["json_complete"]
        and not diagnostics["schema_issues"]
        and not diagnostics["invalid_content_objects"]
        and bool(contents)
    )
    return "\n\n".join(contents), blocks, diagnostics


def _clean_truncated_repeats(
    text: str,
    min_text_len: int = 8000,
    max_period: int = 200,
    min_period: int = 1,
    min_repeat_chars: int = 100,
    min_repeat_times: int = 5,
) -> str:
    n = len(text)
    if n < min_text_len:
        return text

    max_period = min(max_period, n - 1)
    for unit_len in range(min_period, max_period + 1):
        if text[n - 1] != text[n - 1 - unit_len]:
            continue

        match_len = 1
        idx = n - 2
        while idx >= unit_len and text[idx] == text[idx - unit_len]:
            match_len += 1
            idx -= 1

        total_len = match_len + unit_len
        repeat_times = total_len // unit_len
        tail_len = total_len % unit_len

        if repeat_times >= min_repeat_times and total_len >= min_repeat_chars:
            return text[: n - total_len + unit_len] + text[n - tail_len :]

    return text


def parse_tables(raw: str) -> list[dict]:
    """Return all positive objects or [] for the exact negative sentinel."""
    items = json.loads(raw)
    if not isinstance(items, list) or not items:
        raise ValueError("Expected a nonempty JSON array.")
    for item in items:
        if not isinstance(item, dict) or set(item) != {"source_pages", "html"}:
            raise ValueError("Each object needs exactly source_pages and html.")
        if not isinstance(item["source_pages"], list) or not isinstance(
            item["html"], str
        ):
            raise ValueError("Invalid field types.")
        if not item["source_pages"]:
            if len(items) == 1 and item["html"] == NEGATIVE:
                return []
            raise ValueError("Invalid or mixed negative sentinel.")
        if item["source_pages"] != [1, 2] or any(
            type(p) is not int for p in item["source_pages"]
        ):
            raise ValueError("A positive two-page result must use [1, 2].")
        if not TABLE.fullmatch(item["html"].strip()):
            raise ValueError("Expected complete HTML table output.")
    return items


def apply_pair(
    markdowns: tuple[str, str],
    tables: list[dict],
    bindings: list[tuple[int, int]] | None = None,
) -> tuple[str, str]:
    """Replace page 1's last table and remove page 2's first table by default.

    Multiple merged tables require explicit zero-based fragment bindings.
    Nested or incomplete HTML tables are rejected; non-table source is retained.
    """
    if len(markdowns) != 2:
        raise ValueError("Need exactly two pages.")
    spans = [list(TABLE.finditer(md)) for md in markdowns]
    if bindings is None:
        if not tables:
            return tuple(markdowns)
        if len(tables) != 1:
            raise ValueError("Multiple merged tables require explicit source bindings.")
        bindings = [(len(spans[0]) - 1, 0)]
    if len(tables) != len(bindings):
        raise ValueError("Need one explicit binding per merged table.")
    for md, matches in zip(markdowns, spans):
        if len(re.findall(r"<table\b", md, re.I)) != len(matches) or len(
            re.findall(r"</table\s*>", md, re.I)
        ) != len(matches):
            raise ValueError(
                "Nested or incomplete tables require a layout-aware editor."
            )
    edits = [{}, {}]
    for item, binding in zip(tables, bindings):
        parse_tables(json.dumps([item]))
        if len(re.findall(r"<table\b", item["html"], re.I)) != 1:
            raise ValueError(
                "Nested or multiple tables cannot be written into one fragment."
            )
        if item["source_pages"] != [1, 2] or len(binding) != 2:
            raise ValueError("Only positive two-page tables can be written back.")
        for page, index in enumerate(binding):
            if type(index) is not int or not 0 <= index < len(spans[page]):
                raise ValueError("Source table index out of range.")
            if index in edits[page]:
                raise ValueError("Two results claim the same source fragment.")
            edits[page][index] = item["html"] if page == 0 else ""
    output = []
    for page, md in enumerate(markdowns):
        for index in sorted(edits[page], reverse=True):
            span = spans[page][index]
            md = md[: span.start()] + edits[page][index] + md[span.end() :]
        output.append(md)
    return tuple(output)


def find_chunks(pages):
    """Maximal runs of >=2 pages with predicted HTML tables; 1-based IDs."""
    chunks, run = [], []
    for page, markdown in enumerate(pages, 1):
        if TABLE.search(markdown):
            run.append(page)
        else:
            if len(run) >= 2:
                chunks.append(run)
            run = []
    if len(run) >= 2:
        chunks.append(run)
    return chunks


def remap(markdown: str, mapping: Mapping[int, int]) -> str:
    def replace(match):
        page = int(match[1])
        if page not in mapping:
            raise ValueError(f"Image page {page} is outside the current pair.")
        return f"page_{mapping[page]}/images/"

    return REF.sub(replace, markdown)


def page_markdown(raw, page):
    """Accept Markdown, optionally fenced; qualify page-local crop references."""
    markdown = re.sub(
        r"^```(?:markdown|html)?\s*\n|\n```\s*$", "", raw.strip(), flags=re.I
    )
    if not markdown:
        raise ValueError("Empty Stage 1 Markdown.")
    # Each Stage 1 call contains one image, so crop IDs refer to this page.
    return IMAGE_REF.sub(
        lambda m: f"page_{page}/images/bbox_" + "_".join(m.groups()[1:]) + ".jpg",
        markdown,
    )


# Model inference and lazy browser lifecycle.


class VllmBackend:
    """Keep engine, sampling and request settings aligned with README infer().

    Benchmark-specific scheduling overrides do not belong in this backend.
    """

    def __init__(self, model_path):
        from vllm import LLM, SamplingParams

        self.model = LLM(
            model=str(Path(model_path).resolve()),
            tensor_parallel_size=1,
            gpu_memory_utilization=0.8,
            gdn_prefill_backend="triton",
            limit_mm_per_prompt={"image": 2},
        )
        self.tokenizer = self.model.get_tokenizer()
        self.sampling = SamplingParams(max_tokens=16384, temperature=0.0)

    def __call__(self, paths, task_prompt, folder, stage):
        from PIL import Image

        images = []
        for path in paths:
            with Image.open(path) as image:
                images.append(image.convert("RGB"))
        content = [{"type": "image"} for _ in images]
        content.append({"type": "text", "text": task_prompt})
        prompt = self.tokenizer.apply_chat_template(
            [{"role": "user", "content": content}],
            tokenize=False,
            add_generation_prompt=True,
            enable_thinking=False,
        )
        request = {
            "prompt": prompt,
            "multi_modal_data": {"image": images},
            "mm_processor_kwargs": {
                "images_kwargs": {
                    "min_pixels": 448 * 448,
                    "max_pixels": 2880 * 2880,
                }
            },
        }
        save(
            folder / "input.json",
            {
                "stage": stage,
                "images": list(paths),
                "prompt": prompt,
                "mm_processor_kwargs": request["mm_processor_kwargs"],
            },
        )
        output = self.model.generate([request], self.sampling)[0].outputs[0]
        return GenerationResult(output.text, output.finish_reason)


class _BrowserRenderer:
    def __init__(self, mathjax):
        self.mathjax = mathjax
        self.playwright = self.browser = self.renderer = None

    def render(self, *args):
        if self.renderer is None:
            from playwright.sync_api import sync_playwright

            self.playwright = sync_playwright().start()
            self.browser = self.playwright.chromium.launch(headless=True)
            self.renderer = Renderer(self.browser, self.mathjax)
        return self.renderer.render(*args)

    def close(self):
        if self.browser:
            self.browser.close()
        if self.playwright:
            self.playwright.stop()


# Content-preserving Markdown/HTML rendering.


CROP = re.compile(r"page_(\d+)/images/bbox_(\d+)_(\d+)_(\d+)_(\d+)\.jpg")
_CROP_ASSET = re.compile(
    r"(?:page_(\d+)/)?images/(?:bbox_)?(\d+)_(\d+)_(\d+)_(\d+)\.(?:jpg|jpeg|png)", re.I
)
_TAG = re.compile(
    r"<!--.*?-->|</?[A-Za-z][\w:-]*(?:[^>\'\"]|\"[^\"]*\"|\'[^\']*\')*>", re.S
)
_SRC = re.compile(r"""(?<![\w:-])src\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))""", re.I)
_ALLOWED = set(
    "p div span br hr h1 h2 h3 h4 h5 h6 table thead tbody tfoot tr td th caption colgroup col ul ol li dl dt dd blockquote pre code b strong em i u s del sup sub a img figure figcaption section article main center font".split()
)
_DROP = set("script style iframe object embed link meta base template".split())
_CSS = """
html,body{margin:0;padding:0;background:#fff;color:#111}
#capture{padding:28px;box-sizing:border-box;font-family:Arial,'Noto Sans CJK SC',sans-serif;font-size:18px;line-height:1.4}
table{border-collapse:collapse;width:100%;margin:12px 0}td,th{border:1px solid #666;padding:5px;vertical-align:top;overflow-wrap:anywhere}
img{max-width:100%;height:auto}h1{font-size:28px}h2{font-size:24px}h3{font-size:21px}p{margin:10px 0}
pre{white-space:pre-wrap;overflow-wrap:anywhere;font:inherit}code{font-family:monospace;white-space:pre-wrap;overflow-wrap:anywhere}
.math-display{display:block;text-align:center;margin:10px 0}.math-inline{display:inline-block;vertical-align:middle}.math-fallback{white-space:pre-wrap;overflow-wrap:anywhere}
mjx-container{max-width:none}svg{overflow:visible}th{font-weight:600}
"""


@dataclass(frozen=True)
class RenderLimits:
    """Configurable engineering limits, not validated OCR accuracy thresholds."""

    max_side: int = 32768
    max_render_pixels: int = 64_000_000
    max_source_chars: int = 4_000_000
    min_effective_font_px: float = 12.0
    min_effective_cell_height_px: float = 8.0
    timeout_ms: int = 30000

    def __post_init__(self):
        for name, value in asdict(self).items():
            if not math.isfinite(value) or value <= 0:
                raise ValueError(name + " must be finite and positive")


class RenderError(ValueError):
    """Concise caller-facing error; details remain in the render.json sidecar."""

    def __init__(self, code, message):
        self.code = code
        super().__init__(message)


def _escaped(text, pos):
    start = pos
    while start and text[start - 1] == "\\":
        start -= 1
    return (pos - start) % 2 == 1


def _dollar_is_math(body, mode):
    if mode == "all":
        return True
    if mode == "off" or not body or body != body.strip():
        return False
    if re.search(r"\\[A-Za-z]+", body):
        return True
    # Pure numbers and prose are ambiguous; preserve them, rather than invent math.
    if re.fullmatch(r"[A-Za-z]", body):
        return True
    if re.search(r"[^\x00-\x7f]", body):
        return bool(
            re.fullmatch(r"[\w\s\d.,_^=+*/<>{}()−-]+", body)
            and re.search(r"[_^=+*/<>−-]", body)
        )
    if re.fullmatch(r"\d+(?:\.\d+)?\\[,;!: ](?:[A-Za-z]+)", body):
        return True
    words = re.findall(r"[A-Za-z]+", body)
    if any(
        len(w) > 1
        and w not in {"sin", "cos", "tan", "log", "ln", "exp", "max", "min", "lim"}
        for w in words
    ):
        return False
    return bool(re.search(r"[_^=+*/<>{}]", body) or re.search(r"\w\s*-\s*\w", body))


def _markdown_image(text, pos, parser, env):
    from markdown_it.common.utils import normalizeReference
    from markdown_it.helpers import parseLinkDestination, parseLinkLabel, parseLinkTitle
    from markdown_it.rules_inline.state_inline import StateInline

    state = StateInline(text, parser, env, [])
    end_label = parseLinkLabel(state, pos + 1, False)
    if end_label < 0:
        return None
    alt = text[pos + 2 : end_label]
    cursor = end_label + 1
    title = ""
    if cursor < len(text) and text[cursor] == "(":
        cursor += 1
        while cursor < len(text) and text[cursor].isspace():
            cursor += 1
        dest = parseLinkDestination(text, cursor, len(text))
        if not dest.ok:
            return None
        ref, cursor = dest.str, dest.pos
        spaces = cursor
        while cursor < len(text) and text[cursor].isspace():
            cursor += 1
        if cursor > spaces:
            parsed_title = parseLinkTitle(text, cursor, len(text))
            if parsed_title.ok:
                title, cursor = parsed_title.str, parsed_title.pos
                while cursor < len(text) and text[cursor].isspace():
                    cursor += 1
        if cursor >= len(text) or text[cursor] != ")":
            return None
        end = cursor + 1
    else:
        label, end = alt, cursor
        if cursor < len(text) and text[cursor] == "[":
            close = text.find("]", cursor + 1)
            if close < 0:
                return None
            label = text[cursor + 1 : close] or alt
            end = close + 1
        definition = env.get("references", {}).get(normalizeReference(label))
        if not definition:
            return None
        ref, title = definition["href"], definition.get("title", "")
    return end, ref, alt, title


def _image_markup(item, ref):
    if item["kind"] == "html":
        return html.escape(ref, quote=True)
    title = (
        (' "' + item["title"].replace("\\", "\\\\").replace('"', '\\"') + '"')
        if item["title"]
        else ""
    )
    return "![" + item["alt"] + "](<" + ref + ">" + title + ")"


def _edit_source(text, edits):
    out, previous = [], 0
    for start, end, value in sorted(edits):
        if start < previous:
            raise RenderError(
                "overlapping_source_spans",
                "Cannot safely map overlapping source elements.",
            )
        out.extend([text[previous:start], value])
        previous = end
    out.append(text[previous:])
    return "".join(out)


def prepare_source(source, parser, dollar_math="auto", html_document=False):
    """Protect math before CommonMark and identify each actual image occurrence.

    Code fences, indented code, inline code, HTML attributes and comments remain
    literal. Markdown image references are expanded per occurrence, not per URL.
    """
    from markdown_it.helpers import parseLinkDestination, parseLinkTitle

    prefix = "RDR" + uuid.uuid4().hex.upper()
    env = {}
    tokens = [] if html_document else parser.parse(source, env)
    offsets = [0]
    for line in source.splitlines(keepends=True):
        offsets.append(offsets[-1] + len(line))
    literal = {
        offsets[t.map[0]]: offsets[min(t.map[1], len(offsets) - 1)]
        for t in tokens
        if t.type in {"fence", "code_block"} and t.map
    }
    for definition in env.get("references", {}).values():
        if definition.get("map"):
            a, b = definition["map"]
            literal[offsets[a]] = offsets[min(b, len(offsets) - 1)]
    formulas, images, edits, notes = [], [], [], []
    i = 0
    while i < len(source):
        if i in literal:
            i = literal[i]
            continue
        if source[i] == "`" and not _escaped(source, i):
            run = re.match(r"`+", source[i:])[0]
            close = re.search(
                r"(?<!`)" + re.escape(run) + r"(?!`)", source[i + len(run) :]
            )
            if close:
                i += len(run) + close.end()
                continue
        if source.startswith("](", i):
            # Markdown link destinations are not mathematical prose.
            dest = parseLinkDestination(source, i + 2, len(source))
            if dest.ok:
                cursor = dest.pos
                while cursor < len(source) and source[cursor].isspace():
                    cursor += 1
                title = parseLinkTitle(source, cursor, len(source))
                if title.ok:
                    cursor = title.pos
                    while cursor < len(source) and source[cursor].isspace():
                        cursor += 1
                if cursor < len(source) and source[cursor] == ")":
                    i = cursor + 1
                    continue
        if source[i] == "<":
            tag = _TAG.match(source, i)
            if tag:
                raw = tag[0]
                kind = re.match(r"<([A-Za-z][\w:-]*)", raw)
                kind = kind[1].lower() if kind else ""
                if kind in _DROP | {"code"}:
                    close = re.search(
                        r"</" + re.escape(kind) + r"\s*>", source[tag.end() :], re.I
                    )
                    if close:
                        i = tag.end() + close.end()
                        continue
                if kind == "img":
                    src = _SRC.search(raw)
                    if src:
                        group = next(j for j in (1, 2, 3) if src[j] is not None)
                        start, end = i + src.start(group), i + src.end(group)
                        item = dict(
                            start=start,
                            end=end,
                            ref=html.unescape(src[group]),
                            kind="html",
                            token=prefix + "IMG" + str(len(images)),
                        )
                        images.append(item)
                        edits.append((start, end, item["token"]))
                i = tag.end()
                continue
        if source.startswith("![", i) and not _escaped(source, i):
            found = _markdown_image(source, i, parser, env)
            if found:
                end, ref, alt, title = found
                item = dict(
                    start=i,
                    end=end,
                    ref=ref,
                    alt=alt,
                    title=title,
                    kind="markdown",
                    token=prefix + "IMG" + str(len(images)),
                )
                images.append(item)
                edits.append((i, end, _image_markup(item, item["token"])))
                i = end
                continue
        opening = next(
            (s for s in ("\\(", "\\[", "$$", "$") if source.startswith(s, i)), None
        )
        if opening and not _escaped(source, i):
            closing = {"\\(": "\\)", "\\[": "\\]", "$$": "$$", "$": "$"}[opening]
            pos = i + len(opening)
            while True:
                end = source.find(closing, pos)
                if end < 0:
                    break
                if not _escaped(source, end):
                    break
                pos = end + len(closing)
            # Delimiters may not consume table cells or block boundaries.
            # An explicit unclosed formula remains visible in its own cell.
            boundary = re.search(
                r"</?(?:table|thead|tbody|tfoot|tr|td|th|div|p|h[1-6]|li|ul|ol|pre|blockquote)\b[^>]*>|<br\b[^>]*>",
                source[pos:],
                re.I,
            )
            if boundary and end >= 0 and pos + boundary.start() < end:
                notes.append({"code": "formula_block_boundary", "offset": i})
                end = -1
            if end >= 0:
                body = source[i + len(opening) : end]
                allowed = True
                if opening == "$":
                    allowed = (
                        "\n" not in body
                        and not source.startswith("$$", end)
                        and _dollar_is_math(body, dollar_math)
                    )
                    if not allowed:
                        notes.append(
                            {"code": "literal_or_ambiguous_dollar", "offset": i}
                        )
                elif "\n\n" in body and opening == "\\(":
                    allowed = False
                if allowed:
                    token = prefix + "MATH" + str(len(formulas)) + "X"
                    formulas.append(
                        dict(
                            token=token,
                            tex=html.unescape(body),
                            raw=source[i : end + len(closing)],
                            display=opening in {"\\[", "$$"},
                        )
                    )
                    edits.append((i, end + len(closing), token))
                    i = end + len(closing)
                    continue
            elif opening != "$":
                # Preserve malformed explicit notation visibly, even through Markdown.
                stop = source.find("\n\n", i)
                stop = len(source) if stop < 0 else stop
                tag_boundary = _TAG.search(source, i + len(opening))
                if tag_boundary:
                    stop = min(stop, tag_boundary.start())
                token = prefix + "MATH" + str(len(formulas)) + "X"
                formulas.append(
                    dict(
                        token=token,
                        tex="",
                        raw=source[i:stop],
                        display=False,
                        malformed=True,
                    )
                )
                edits.append((i, stop, token))
                i = stop
                continue
            # Consume an unrecognized delimiter as a unit (do not reinterpret $$ as $).
            i += len(opening)
            continue
        i += 1
    return _edit_source(source, edits), formulas, images, notes


def sanitize(soup):
    from bs4 import Comment

    removed = 0
    for node in list(soup.find_all(string=lambda t: isinstance(t, Comment))):
        node.extract()
    for tag in list(soup.find_all(True)):
        if tag.name is None:
            continue
        if tag.name in {"svg", "canvas", "math", "video", "audio"}:
            raise RenderError(
                "unsupported_embedded_media",
                "Embedded media needs an explicit image asset; source retained.",
            )
        if tag.name in _DROP:
            tag.decompose()
            removed += 1
            continue
        if tag.name not in _ALLOWED:
            tag.unwrap()
            removed += 1
            continue
        attrs = {}
        if tag.name in {"td", "th", "col", "colgroup"}:
            for key in ("rowspan", "colspan", "span"):
                if key in tag.attrs:
                    value = str(tag[key])
                    if not value.isdecimal() or not (
                        0 if key == "rowspan" else 1
                    ) <= int(value) <= (65534 if key == "rowspan" else 1000):
                        raise RenderError(
                            "invalid_table_span",
                            "Invalid table cell span; source retained.",
                        )
                    attrs[key] = value
        if tag.name == "img":
            attrs.update({k: tag[k] for k in ("src", "alt", "title") if k in tag.attrs})
        if tag.name == "ol" and str(tag.get("start", "")).lstrip("-").isdigit():
            attrs["start"] = tag["start"]
        # Retain safe alignment, but no CSS that can hide, move, clip or fetch.
        declarations = dict(
            (k.strip().lower(), v.strip().lower())
            for k, v in re.findall(r"([\w-]+)\s*:\s*([^;]+)", str(tag.get("style", "")))
        )
        align = declarations.get("text-align", str(tag.get("align", "")).lower())
        valign = declarations.get("vertical-align", str(tag.get("valign", "")).lower())
        style = []
        if align in {"left", "right", "center", "justify"}:
            style.append("text-align:" + align)
        if valign in {"top", "middle", "bottom", "baseline"}:
            style.append("vertical-align:" + valign)
        if style:
            attrs["style"] = ";".join(style)
        if dict(tag.attrs) != attrs:
            removed += 1
        tag.attrs = attrs
    return removed


def _png_data(image):
    from PIL import Image

    image = image.convert("RGBA")
    background = Image.new("RGBA", image.size, "white")
    background.alpha_composite(image)
    buf = io.BytesIO()
    background.convert("RGB").save(buf, format="PNG")
    return "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode("ascii")


def resolve_alias(ref, aliases, *, tolerance=2, min_iou=0.98, events=None):
    """Exact first; a small coordinate jitter must match one unique occurrence.

    This is a configurable rounding tolerance, not semantic figure localization.
    An ambiguous or newly predicted region is never assigned to the nearest image.
    """
    if ref in aliases:
        return aliases[ref]
    target = CROP.fullmatch(ref)
    if not target:
        raise RenderError("unmapped_crop", "Cannot map an image region to its source.")
    page, *box = map(int, target.groups())
    candidates = []
    for key in aliases:
        m = CROP.fullmatch(key)
        if not m or int(m[1]) != page:
            continue
        other = list(map(int, m.groups()[1:]))
        if max(abs(a - b) for a, b in zip(box, other)) > tolerance:
            continue
        intersection = max(0, min(box[2], other[2]) - max(box[0], other[0])) * max(
            0, min(box[3], other[3]) - max(box[1], other[1])
        )
        area = max(0, box[2] - box[0]) * max(0, box[3] - box[1])
        area2 = max(0, other[2] - other[0]) * max(0, other[3] - other[1])
        union = area + area2 - intersection
        if union and intersection / union >= min_iou:
            candidates.append(key)
    if len(candidates) != 1:
        raise RenderError(
            "ambiguous_crop" if candidates else "unmapped_crop",
            "Cannot uniquely map an image region to its source.",
        )
    if events is not None:
        events.append(
            {
                "code": "coordinate_rounding_match",
                "predicted": ref,
                "matched": candidates[0],
            }
        )
    return aliases[candidates[0]]


class Renderer:
    def __init__(
        self,
        browser,
        mathjax,
        *,
        limits=None,
        dollar_math="auto",
        assets=None,
        asset_root=None,
        resize=None,
        resize_name=None,
        max_input_pixels=None,
    ):
        from markdown_it import MarkdownIt

        if dollar_math not in {"auto", "all", "off"}:
            raise ValueError("dollar_math must be auto, all or off")
        self.mathjax = Path(mathjax).resolve()
        self.limits = limits or RenderLimits()
        self.dollar_math = dollar_math
        self.assets = dict(assets or {})
        self.asset_root = Path(asset_root).resolve() if asset_root else None
        self.resize = resize
        self.resize_name = resize_name or (
            "custom"
            if resize
            else "area_budget_estimate" if max_input_pixels else "unknown"
        )
        if max_input_pixels is not None and max_input_pixels <= 0:
            raise ValueError("max_input_pixels must be positive")
        self.max_input_pixels = max_input_pixels
        self.page = browser.new_page(
            viewport={"width": 1200, "height": 900}, device_scale_factor=1
        )
        self.page.set_default_timeout(self.limits.timeout_ms)
        # No external assets, CSS or network dependencies in model-supplied HTML.
        self.page.route("**/*", lambda route: route.abort())
        self.md = MarkdownIt("commonmark", {"html": True}).enable(
            ["table", "strikethrough"]
        )

    def _asset(self, ref, owner, paths):
        from PIL import Image

        crop = _CROP_ASSET.fullmatch(ref)
        if crop:
            page, left, top, right, bottom = crop.groups()
            page = int(page) if page else owner
            left, top, right, bottom = map(int, (left, top, right, bottom))
            if (
                not 1 <= page <= len(paths)
                or not 0 <= left < right <= 1000
                or not 0 <= top < bottom <= 1000
            ):
                raise RenderError(
                    "invalid_crop",
                    "An image crop has invalid coordinates; source retained.",
                )
            with Image.open(paths[page - 1]) as im:
                box = (
                    round(left * im.width / 1000),
                    round(top * im.height / 1000),
                    round(right * im.width / 1000),
                    round(bottom * im.height / 1000),
                )
                if box[0] >= box[2] or box[1] >= box[3]:
                    raise RenderError(
                        "empty_crop", "An image crop is smaller than one pixel."
                    )
                if (box[2] - box[0]) * (
                    box[3] - box[1]
                ) > self.limits.max_render_pixels:
                    raise RenderError(
                        "asset_limit",
                        "An image asset exceeds the rendering pixel budget.",
                    )
                return _png_data(im.crop(box))
        if ref.startswith("data:"):
            header, sep, payload = ref.partition(",")
            if not sep or not re.fullmatch(
                r"data:image/(?:png|jpeg|webp|gif);base64", header, re.I
            ):
                raise RenderError(
                    "unsupported_asset",
                    "An image format is unsupported; source retained.",
                )
            with Image.open(io.BytesIO(base64.b64decode(payload, validate=True))) as im:
                if im.width * im.height > self.limits.max_render_pixels:
                    raise RenderError(
                        "asset_limit",
                        "An image asset exceeds the rendering pixel budget.",
                    )
                return _png_data(im)
        path = self.assets.get(ref)
        if path is None and self.asset_root:
            from urllib.parse import unquote, urlsplit

            if not urlsplit(ref).scheme and not ref.startswith("//"):
                candidate = (self.asset_root / unquote(ref)).resolve()
                if candidate.is_relative_to(self.asset_root):
                    path = candidate
        if path is None:
            raise RenderError(
                "unresolved_asset",
                "An image needs a local asset mapping; source retained.",
            )
        with Image.open(path) as im:
            if im.width * im.height > self.limits.max_render_pixels:
                raise RenderError(
                    "asset_limit", "An image asset exceeds the rendering pixel budget."
                )
            return _png_data(im)

    def render(self, md, owner, paths, directory, name):
        directory = Path(directory)
        directory.mkdir(parents=True, exist_ok=True)
        if Path(name).name != name or not name:
            raise ValueError("name must be a filename stem")
        started = time.monotonic()
        diag = {
            "version": 2,
            "status": "ERROR",
            "math": 0,
            "errors": 0,
            "broken": 0,
            "images": [],
            "notes": [],
            "limits": asdict(self.limits),
            "resize_policy": self.resize_name,
        }
        (directory / (name + ".source.md")).write_text(md)
        try:
            result = self._render(md, owner, paths, directory, name, diag)
            diag["status"] = "DEGRADED" if diag.get("formula_fallbacks") else "COMPLETE"
            return result
        except Exception as exc:
            diag["error"] = {
                "code": getattr(exc, "code", "render_failed"),
                "type": type(exc).__name__,
                "message": str(exc)[:1000],
            }
            if isinstance(exc, RenderError):
                raise
            raise RenderError(
                "render_failed",
                "Page rendering failed; source and diagnostics were retained.",
            ) from exc
        finally:
            diag["seconds"] = time.monotonic() - started
            save(directory / (name + ".render.json"), diag)

    def _render(self, md, owner, paths, directory, name, diag):
        from bs4 import BeautifulSoup, Comment, NavigableString
        from PIL import Image

        if not 1 <= owner <= len(paths):
            raise RenderError("invalid_owner", "Page index is outside the document.")
        if len(md) > self.limits.max_source_chars:
            raise RenderError(
                "source_limit", "Page source exceeds the configured rendering limit."
            )
        probe = (
            BeautifulSoup(md, "html.parser") if md.lstrip().startswith("<") else None
        )
        html_document = bool(
            probe is not None
            and probe.find(True)
            and all(
                not isinstance(x, NavigableString)
                or isinstance(x, Comment)
                or not str(x).strip()
                for x in probe.contents
            )
        )
        prepared, formulas, occurrences, notes = prepare_source(
            md, self.md, self.dollar_math, html_document
        )
        diag["input_format"] = "html" if html_document else "markdown"
        diag["notes"].extend(notes)
        soup = BeautifulSoup(
            prepared if html_document else self.md.render(prepared), "html.parser"
        )
        diag["sanitized_elements"] = sanitize(soup)
        by_token = {x["token"]: x for x in occurrences}
        seen = set()
        for image in soup.find_all("img"):
            key = image.get("src", "")
            if key not in by_token or key in seen:
                raise RenderError(
                    "image_mapping", "Cannot safely map image occurrences."
                )
            seen.add(key)
            item = by_token[key]
            image["src"] = self._asset(item["ref"], owner, paths)
            image["data-image-id"] = key
        if len(seen) != len(occurrences):
            raise RenderError(
                "image_mapping", "An image occurrence was lost during parsing."
            )
        by_math = {x["token"]: x for x in formulas}
        if by_math:
            pattern = re.compile(
                "|".join(map(re.escape, sorted(by_math, key=len, reverse=True)))
            )
            found = []
            for node in list(soup.find_all(string=pattern)):
                if isinstance(node, Comment):
                    continue
                pieces = pattern.split(str(node))
                matches = list(pattern.finditer(str(node)))
                for k, part in enumerate(pieces):
                    if part:
                        node.insert_before(NavigableString(part))
                    if k < len(matches):
                        item = by_math[matches[k][0]]
                        found.append(item["token"])
                        tag = soup.new_tag("span")
                        tag["data-math-id"] = item["token"]
                        tag["class"] = (
                            "math-display" if item["display"] else "math-inline"
                        )
                        tag.string = item["raw"]
                        node.insert_before(tag)
                node.extract()
            if sorted(found) != sorted(by_math):
                raise RenderError(
                    "formula_mapping", "A formula was lost during parsing."
                )
        tables = soup.find_all("table")
        expected_cells = len(soup.find_all(["td", "th"]))
        columns = max(
            (
                sum(
                    int(c.get("colspan", 1))
                    for c in row.find_all(["td", "th"], recursive=False)
                )
                for row in soup.find_all("tr")
            ),
            default=1,
        )
        with Image.open(paths[owner - 1]) as original:
            width = max(original.width, columns * 120 + 56)
        document = (
            '<!doctype html><html><head><meta charset="utf-8"><style>'
            + _CSS
            + '</style></head><body><div id="capture" style="width:'
            + str(width)
            + 'px">'
            + str(soup)
            + "</div></body></html>"
        )
        # Persist complete render input before any resource/readability rejection.
        (directory / (name + ".html")).write_text(document)
        if width > self.limits.max_side:
            raise RenderError(
                "size_limit",
                "Rendered page exceeds the configured size limit; complete source retained.",
            )
        page = self.page
        page.set_viewport_size({"width": width, "height": 900})
        page.set_content(document, wait_until="domcontentloaded")
        diag["expected_formulas"] = len(formulas)
        formula_results = []
        if formulas:
            if not self.mathjax.is_file():
                raise RenderError("mathjax_missing", "Local MathJax bundle is missing.")
            page.evaluate(
                "window.MathJax={startup:{typeset:false},svg:{fontCache:'none'},tex:{maxBuffer:100000,packages:['base','ams','newcommand']}}"
            )
            page.add_script_tag(path=str(self.mathjax))
            page.wait_for_function(
                "window.MathJax && MathJax.startup && MathJax.startup.promise"
            )
            formula_results = page.evaluate(
                """async ({items,timeout})=>{
              const work=async()=>{
                await MathJax.startup.promise;
                if(typeof MathJax.tex2svgPromise!=='function') throw Error('MathJax SVG API unavailable');
                const results=[];
                for(const item of items){
                  const target=document.querySelector('[data-math-id="'+item.token+'"]');
                  let error=null;
                  try{
                    if(item.malformed) throw Error('Unclosed formula delimiter');
                    if(!item.tex.trim()) throw Error('Empty formula');
                    const rendered=await MathJax.tex2svgPromise(item.tex,{display:item.display});
                    if(rendered.querySelector('[data-mml-node="merror"],mjx-merror,[data-mjx-error]')) throw Error('Invalid TeX');
                    // This screenshot renderer needs only SVG. Standalone tex2svg
                    // may emit assistive MathML without the CSS that hides it.
                    rendered.querySelectorAll('mjx-assistive-mml').forEach(node=>node.remove());
                    target.replaceChildren(rendered);
                  }catch(e){error=String(e.message||e);target.textContent=item.raw;target.classList.add('math-fallback');}
                  results.push({id:item.token,rendered:!error,error});
                }
                return results;
              };
              let timer;try{return await Promise.race([work(),new Promise((_,reject)=>{timer=setTimeout(()=>reject(Error('Formula rendering timeout')),timeout)})]);}finally{clearTimeout(timer);}
            }""",
                {"items": formulas, "timeout": self.limits.timeout_ms},
            )
        diag["formulas"] = formula_results
        diag["formula_fallbacks"] = sum(not x["rendered"] for x in formula_results)
        page.evaluate(
            """async timeout=>{
          let timer;try{await Promise.race([(async()=>{await document.fonts.ready;await Promise.all([...document.images].map(i=>i.decode()));await new Promise(r=>requestAnimationFrame(()=>requestAnimationFrame(r)));})(),new Promise((_,reject)=>{timer=setTimeout(()=>reject(Error('Image/font readiness timeout')),timeout)})]);}finally{clearTimeout(timer);}
        }""",
            self.limits.timeout_ms,
        )
        # Allow intrinsically wide math/tables to grow once, within the resource budget.
        needed = page.evaluate("document.documentElement.scrollWidth")
        if needed > width:
            width = needed + 28
            if width > self.limits.max_side:
                raise RenderError(
                    "size_limit",
                    "Rendered content is too wide; complete source retained.",
                )
            page.set_viewport_size({"width": width, "height": 900})
            page.locator("#capture").evaluate('(e,w)=>e.style.width=w+"px"', width)
        layout = page.evaluate("""()=>{
          const root=document.getElementById('capture');const area=root.getBoundingClientRect();
          const boxes=[...root.querySelectorAll('table,td,th,img,[data-math-id]')];
          const bad=boxes.filter(e=>{const b=e.getBoundingClientRect(),s=getComputedStyle(e);return !b.width||!b.height||s.display==='none'||s.visibility==='hidden'||Number(s.opacity)===0||b.left<0||b.right>area.right+.5;}).map(e=>({tag:e.tagName,id:e.dataset.imageId||e.dataset.mathId||null}));
          const fonts=[];const walker=document.createTreeWalker(root,NodeFilter.SHOW_TEXT);let n;
          while(n=walker.nextNode()){if(n.textContent.trim()&&!n.parentElement.closest('mjx-container')){const s=getComputedStyle(n.parentElement);fonts.push(parseFloat(s.fontSize));}}
          const cellHeights=[...root.querySelectorAll('td,th')].map(e=>e.getBoundingClientRect().height);
          return {math:root.querySelectorAll('mjx-container').length,broken:[...root.querySelectorAll('img')].filter(i=>!i.complete||!i.naturalWidth).length,
            tables:root.querySelectorAll('table').length,cells:cellHeights.length,invisible_or_clipped:bad,
            min_font_px:fonts.length?fonts.reduce((a,b)=>Math.min(a,b),Infinity):18,min_cell_height_px:cellHeights.length?cellHeights.reduce((a,b)=>Math.min(a,b),Infinity):null,
            width:document.documentElement.scrollWidth,height:document.documentElement.scrollHeight,
            images:[...root.querySelectorAll('img')].map(i=>{const b=i.getBoundingClientRect();return {id:i.dataset.imageId,x:b.x,y:b.y,w:b.width,h:b.height}})};
        }""")
        diag.update(layout)
        diag["errors"] = diag["formula_fallbacks"]
        if layout["tables"] != len(tables) or layout["cells"] != expected_cells:
            raise RenderError(
                "table_structure",
                "Browser parsing changed the table structure; source retained.",
            )
        if layout["invisible_or_clipped"] or layout["broken"]:
            raise RenderError(
                "invisible_content",
                "Some page content is not visible; source retained.",
            )
        if layout["math"] + diag["formula_fallbacks"] != len(formulas):
            raise RenderError(
                "formula_coverage", "Formula rendering coverage is incomplete."
            )
        w, h = layout["width"], layout["height"]
        # Snapshot has no live external scripts or assets; it opens offline as rendered.
        (directory / (name + ".html")).write_text(
            page.evaluate(
                "() => { const root=document.documentElement.cloneNode(true); root.querySelectorAll('script').forEach(e=>e.remove()); return '<!doctype html>'+root.outerHTML; }"
            )
        )
        if max(w, h) > self.limits.max_side or w * h > self.limits.max_render_pixels:
            raise RenderError(
                "size_limit",
                "Rendered page exceeds the configured size limit; complete HTML retained.",
            )
        target = (w, h)
        if not self.resize and not self.max_input_pixels:
            diag["notes"].append({"code": "model_resize_unspecified"})
        if self.resize:
            try:
                target = tuple(self.resize(w, h))
            except Exception as exc:
                raise RenderError(
                    "model_resize_limit",
                    "Page exceeds the model image-size limits; complete HTML retained.",
                ) from exc
        elif self.max_input_pixels:
            scale = min(1, math.sqrt(self.max_input_pixels / (w * h)))
            target = (max(1, math.floor(w * scale)), max(1, math.floor(h * scale)))
        if len(target) != 2 or min(target) <= 0:
            raise RenderError(
                "resize_policy", "Invalid target dimensions from resize policy."
            )
        scale = min(target[0] / w, target[1] / h)
        font = layout["min_font_px"] * scale
        cell = (
            layout["min_cell_height_px"] * scale
            if layout["min_cell_height_px"]
            else None
        )
        diag["readability"] = {
            "input_size": [w, h],
            "model_size": list(target),
            "scale": scale,
            "effective_font_px": font,
            "effective_cell_height_px": cell,
            "model_resize_known": bool(self.resize),
            "estimated_from_pixel_budget": bool(
                not self.resize and self.max_input_pixels
            ),
            "thresholds_are_heuristic": True,
        }
        if font < self.limits.min_effective_font_px or (
            cell is not None and cell < self.limits.min_effective_cell_height_px
        ):
            raise RenderError(
                "readability_limit",
                "Page text would be too small after resizing; complete HTML retained.",
            )
        aliases, edits = {}, []
        for image in layout["images"]:
            item = by_token[image["id"]]
            image["ref"] = item["ref"]
            box = [
                round(image["x"] / w * 1000),
                round(image["y"] / h * 1000),
                round((image["x"] + image["w"]) / w * 1000),
                round((image["y"] + image["h"]) / h * 1000),
            ]
            if not 0 <= box[0] < box[2] <= 1000 or not 0 <= box[1] < box[3] <= 1000:
                raise RenderError(
                    "crop_quantization",
                    "An image is too small for normalized coordinate mapping.",
                )
            alias = f"page_{owner}/images/bbox_" + "_".join(map(str, box)) + ".jpg"
            if alias in aliases:
                raise RenderError(
                    "crop_alias_collision",
                    "Image positions cannot be uniquely represented.",
                )
            aliases[alias] = item["ref"]
            edits.append((item["start"], item["end"], _image_markup(item, alias)))
        rewritten = _edit_source(md, edits)
        path = directory / (name + ".png")
        page.screenshot(path=str(path), full_page=True, timeout=self.limits.timeout_ms)
        with Image.open(path) as rendered:
            if rendered.size != (w, h):
                raise RenderError(
                    "screenshot_dimensions",
                    "Screenshot dimensions differ from image coordinate mapping.",
                )
        (directory / (name + ".rewritten.md")).write_text(rewritten)
        save(directory / (name + ".aliases.json"), aliases)
        return RenderResult(str(path), rewritten, aliases, diag)


# Command-line interface.


def main():
    parser = argparse.ArgumentParser(
        description="OvisOCR3 document-level cross-page reconstruction"
    )
    parser.add_argument("images", nargs="+", help="Page images in reading order")
    parser.add_argument("--model", default=".")
    parser.add_argument("--output", required=True)
    parser.add_argument(
        "--stage1-prompt",
        default="document_structured.txt",
        choices=[
            "document_markdown",
            "document_markdown.txt",
            "document_structured",
            "document_structured.txt",
        ],
    )
    parser.add_argument(
        "--mathjax",
        default=None,
        help="Local MathJax bundle; defaults to assets/mathjax.js beside this script",
    )
    args = parser.parse_args()
    result = reconstruct_document(
        args.images,
        args.output,
        model_path=args.model,
        stage1_prompt=args.stage1_prompt,
        mathjax=args.mathjax,
    )
    print(json.dumps(result, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()