Source file BuildTraceStore.ml
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
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
module TraceStoreObserver =
MlFront_Thunk.ThunkParsers.Results.MakeObserverWithErrorReporter
let unimplemented_read operation =
raise
(BuildExceptions.EngineShutdown
{
trace =
[
ErrorBacktraceItem'
{
error_code = "2a794854";
cant_do = "read from the trace store";
because = Printf.sprintf "`%s` unimplemented" operation;
recommendations = [ "Please file a bug report." ];
error_locations = [];
};
];
exitcode_posix = 4;
exitcode_windows = 4;
})
let unimplemented_write operation =
raise
(BuildExceptions.EngineShutdown
{
trace =
[
ErrorBacktraceItem'
{
error_code = "2a794854";
cant_do = "write to the trace store";
because = Printf.sprintf "`%s` unimplemented" operation;
recommendations = [ "Please file a bug report." ];
error_locations = [];
};
];
exitcode_posix = 4;
exitcode_windows = 4;
})
let package_version_to_proto ~package_id ~package_semver :
Traces.package_version option =
match
( (package_id : MlFront_Core.PackageId.t),
(package_semver : MlFront_Thunk.ThunkSemver64.t) )
with
| ( { library_id; namespace; full_name = _; fixlen_name = _ },
{ major; minor; patch; prerelease; build } ) ->
Some
{
package_id =
Some
{
vendor = Some (MlFront_Core.LibraryId.vendor library_id);
qualifier = Some (MlFront_Core.LibraryId.qualifier library_id);
unit_ = Some (MlFront_Core.LibraryId.unit library_id);
namespace;
};
package_semver =
Some
{
major = Some major;
minor = Some minor;
patch = Some patch;
prerelease;
build;
};
}
let module_version_to_proto ~module_id ~module_semver :
Traces.module_version option =
match
( (module_id : MlFront_Core.StandardModuleId.t),
(module_semver : MlFront_Thunk.ThunkSemver64.t) )
with
| ( { library_id; namespace_front; namespace_tail; definition = _ },
{ major; minor; patch; prerelease; build } ) ->
Some
{
module_id =
Some
{
vendor = Some (MlFront_Core.LibraryId.vendor library_id);
qualifier = Some (MlFront_Core.LibraryId.qualifier library_id);
unit_ = Some (MlFront_Core.LibraryId.unit library_id);
namespace_front;
namespace_tail = Some namespace_tail;
};
module_semver =
Some
{
major = Some major;
minor = Some minor;
patch = Some patch;
prerelease;
build;
};
}
let parse_library_id ~vendor ~qualifier ~unit_ =
match (vendor, qualifier, unit_) with
| Some vendor, Some qualifier, Some unit_ ->
MlFront_Core.LibraryId.parse (vendor ^ qualifier ^ "_" ^ unit_)
| _ -> None
let parse_semver ~major ~minor ~patch ~prerelease ~build =
match (major, minor, patch) with
| Some major, Some minor, Some patch ->
MlFront_Thunk.ThunkSemver64.from_parts major minor patch prerelease build
| _ -> None
let package_version_from_proto :
Traces.package_version ->
(MlFront_Core.PackageId.t * MlFront_Thunk.ThunkSemver64.t) option = function
| {
package_id = Some { vendor; qualifier; unit_; namespace };
package_semver = Some { major; minor; patch; prerelease; build };
} -> begin
match parse_library_id ~vendor ~qualifier ~unit_ with
| None -> None
| Some library_id -> (
let package_id =
MlFront_Core.PackageId.create ~library_id ~namespace
in
match parse_semver ~major ~minor ~patch ~prerelease ~build with
| Some package_semver -> Some (package_id, package_semver)
| None -> None)
end
| _ -> None
let module_version_from_proto :
Traces.module_version ->
(MlFront_Core.StandardModuleId.t * MlFront_Thunk.ThunkSemver64.t) option =
function
| {
module_id =
Some
{
vendor;
qualifier;
unit_;
namespace_front;
namespace_tail = Some namespace_tail;
};
module_semver = Some { major; minor; patch; prerelease; build };
} -> begin
match parse_library_id ~vendor ~qualifier ~unit_ with
| None -> None
| Some library_id -> (
let module_id =
MlFront_Core.StandardModuleId.create_explicit ~library_id
~namespace_front ~namespace_tail
in
match parse_semver ~major ~minor ~patch ~prerelease ~build with
| Some module_semver -> Some (module_id, module_semver)
| None -> None)
end
| _ -> None
let object_slot_to_proto slot = MlFront_Thunk.ThunkCommand.object_slot_list slot
let object_slot_from_proto slot =
MlFront_Thunk.ThunkCommand.InternalUse.parse_object_slot
(module TraceStoreObserver)
MlFront_Thunk.ThunkParsers.Results.State.none `DirectDecode None slot
|> Result.to_option
let position_to_proto : Fmlib_parse.Position.t -> Traces.position option =
let open Fmlib_parse in
let i32 x = Some (Int32.of_int x) in
fun pos ->
let line = Position.line pos in
let byte_bol = Position.byte_offset_bol pos in
let byte_col = Position.byte_column pos in
let correction = Position.column pos - byte_col in
Some
{
line = i32 line;
byte_bol = i32 byte_bol;
byte_col = i32 byte_col;
correction = i32 correction;
}
type fmlib_position = {
line : int;
byte_bol : int;
byte_col : int;
correction : int;
}
(** Hack for {!Obj.magic} until https://github.com/hbr/fmlib/issues/25 fixed *)
let () =
let real = Fmlib_parse.Position.start in
let sample = { line = 1; byte_bol = 2; byte_col = 3; correction = 4 } in
let real_sz = Obj.size (Obj.repr real) in
let sample_sz = Obj.size (Obj.repr sample) in
assert (real_sz = sample_sz);
let fake : fmlib_position = Obj.magic real in
assert (
fake.line = 0 && fake.byte_bol = 0 && fake.byte_col = 0
&& fake.correction = 0);
let fake2 : Fmlib_parse.Position.t = Obj.magic sample in
let open Fmlib_parse in
assert (
Position.line fake2 = 1
&& Position.byte_offset_bol fake2 = 2
&& Position.byte_column fake2 = 3
&& Position.column fake2 - Position.byte_column fake2 = 4)
let position_from_proto : Traces.position -> Fmlib_parse.Position.t option =
function
| {
line = Some line;
byte_bol = Some byte_bol;
byte_col = Some byte_col;
correction = Some correction;
} ->
let line = Int32.to_int line in
let byte_bol = Int32.to_int byte_bol in
let byte_col = Int32.to_int byte_col in
let correction = Int32.to_int correction in
let p : fmlib_position = { line; byte_bol; byte_col; correction } in
Some (Obj.magic p)
| _ -> None
let range_to_proto : Fmlib_parse.Position.range -> Traces.range option =
function
| start, end_ ->
match (position_to_proto start, position_to_proto end_) with
| Some start', Some end' -> Some { start = Some start'; end_ = Some end' }
| _ -> None
let range_from_proto : Traces.range -> Fmlib_parse.Position.range option =
function
| { start = Some start'; end_ = Some end' } -> begin
match (position_from_proto start', position_from_proto end') with
| Some start'', Some end'' -> Some (start'', end'')
| _ -> None
end
| _ -> None
let checksum_to_proto :
Fmlib_parse.Position.range * string -> Traces.checksum option =
fun (range, checksum) ->
match range_to_proto range with
| Some range' ->
Some { checksum_value = Some checksum; checksum_range = Some range' }
| None -> None
let checksum_from_proto :
Traces.checksum -> (Fmlib_parse.Position.range * string) option =
fun { checksum_value; checksum_range } ->
match (checksum_value, checksum_range) with
| Some checksum, Some range -> begin
match range_from_proto range with
| Some range' -> Some (range', checksum)
| None -> None
end
| _ -> None
let k_to_proto : BuildCore.Alacarte_3_2_apparatus.K.t -> Traces.key =
let open BuildCore.Alacarte_3_2_apparatus in
function
| { key_datum; debug_reference } -> (
let debug_reference_range, debug_reference_file_sha256 =
match debug_reference with
| None -> (None, None)
| Some reference ->
( range_to_proto reference.reference_range,
Some reference.reference_file_sha256 )
in
match key_datum with
| ChecksumKey
{
checksum_kind = ValuesFileKind;
checksum_sha256_hex;
checksum_sha256_base32 = _;
} ->
{
values_canonical_id = None;
module_version = None;
package_version = None;
keykind_slot = [];
keykind_assetpath = None;
debug_reference_range;
debug_reference_file_sha256;
keykind = Some Traces.Keykind_checksum_valuesfile;
checksum_sha256 = Some checksum_sha256_hex;
}
| PackageKey
{ package_kind = DistributionPackageKind; package_id; package_semver }
->
{
checksum_sha256 = None;
values_canonical_id = None;
module_version = None;
keykind_slot = [];
keykind_assetpath = None;
debug_reference_range;
debug_reference_file_sha256;
keykind = Some Traces.Keykind_package_dist;
package_version =
package_version_to_proto ~package_id ~package_semver;
}
| ModuleKey { module_kind; module_id; module_semver } -> begin
let module_version =
module_version_to_proto ~module_id ~module_semver
in
match module_kind with
| UserFormKind { slot } ->
{
checksum_sha256 = None;
values_canonical_id = None;
package_version = None;
debug_reference_range;
debug_reference_file_sha256;
keykind = Some Traces.Keykind_user_form;
module_version;
keykind_slot = object_slot_to_proto slot;
keykind_assetpath = None;
}
| UserBundleKind ->
{
checksum_sha256 = None;
values_canonical_id = None;
package_version = None;
debug_reference_range;
debug_reference_file_sha256;
keykind = Some Traces.Keykind_user_bundle;
module_version =
module_version_to_proto ~module_id ~module_semver;
keykind_slot = [];
keykind_assetpath = None;
}
| UserAssetKind { asset_path } ->
{
checksum_sha256 = None;
values_canonical_id = None;
package_version = None;
debug_reference_range;
debug_reference_file_sha256;
keykind = Some Traces.Keykind_user_asset;
module_version;
keykind_slot = [];
keykind_assetpath = Some asset_path;
}
end)
let k_from_proto ~buildlogtrace ~valuestore_get :
Traces.key ->
((unit -> string) * BuildCore.Alacarte_3_2_apparatus.K.t) option
BuildCore.Alacarte_xpromise_apparatus.Promise.t =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let apply_aliases = None in
let g_package_dist package_version post =
match package_version_from_proto package_version with
| None -> return None
| Some (package_id, package_semver) -> begin
let k =
K.create_for_distribution ~debug_reference:None ~package_id
~package_semver ()
in
let* k = post k in
return @@ Some ((fun () -> K.show k), k)
end
in
let g_user_form keykind_slot module_version post =
match
( object_slot_from_proto (String.concat "." keykind_slot),
module_version_from_proto module_version )
with
| Some slot, Some (module_id, module_semver) ->
let k =
K.create_for_form ~apply_aliases ~debug_reference:None ~module_id
~module_semver ~slot ()
in
let* k = post k in
return @@ Some ((fun () -> K.show k), k)
| _ -> return None
in
let g_user_bundle module_version post =
match module_version_from_proto module_version with
| Some (module_id, module_semver) ->
let k =
K.create_for_bundle ~apply_aliases ~debug_reference:None ~module_id
~module_semver ()
in
let* k = post k in
return @@ Some ((fun () -> K.show k), k)
| _ -> return None
in
let g_user_asset asset_path module_version post =
match module_version_from_proto module_version with
| Some (module_id, module_semver) ->
let k =
K.create_for_asset ~apply_aliases ~debug_reference:None ~module_id
~module_semver ~asset_path ()
in
let* k = post k in
return @@ Some ((fun () -> K.show k), k)
| _ -> return None
in
let with_debug_reference ~debug_reference_range ~debug_reference_file_sha256
key =
match (debug_reference_range, debug_reference_file_sha256) with
| Some range, Some sha256 -> (
let* source_file_opt =
valuestore_get key
(V.get_source_file_value_id ~source_file_sha256:sha256)
in
match (source_file_opt, range_from_proto range) with
| Some source_file, Some reference_range ->
if buildlogtrace then
Printf.eprintf "[buildlog] read source %s\n"
(Format.asprintf "%a"
(MlFront_Thunk.ThunkParsers.Results.pp_range
(Some (MlFront_Core.FilePath.to_string source_file)))
reference_range);
let r =
{
K.reference_range;
reference_file_sha256 = sha256;
reference_transient =
Some { reference_file = BuildCore.Io.disk_file source_file };
}
in
return (K.with_debug_reference key r)
| _ -> return key)
| _ -> return key
in
function
| { keykind = None; _ } -> return None
| {
keykind = Some Keykind_checksum_valuesfile;
checksum_sha256 = Some values_file_sha256;
values_canonical_id = _;
module_version = _;
package_version = _;
keykind_slot = _;
keykind_assetpath = _;
debug_reference_range;
debug_reference_file_sha256;
} -> begin
match
K.create_checksum_for_values_file ~debug_reference:None
~values_file_sha256 ()
with
| Ok k ->
let* k =
with_debug_reference ~debug_reference_range
~debug_reference_file_sha256 k
in
return (Some ((fun () -> K.show k), k))
| Error _ -> return None
end
| { keykind = Some Keykind_checksum_valuesfile; checksum_sha256 = None; _ } ->
return None
| { keykind = Some Keykind_unspecified; _ } ->
unimplemented_read "k_from_proto::Keykind_unspecified"
| {
keykind = Some Keykind_package_dist;
checksum_sha256 = _;
values_canonical_id = _;
module_version = _;
keykind_slot = _;
keykind_assetpath = _;
debug_reference_range;
debug_reference_file_sha256;
package_version = Some package_version;
} ->
g_package_dist package_version
(with_debug_reference ~debug_reference_range
~debug_reference_file_sha256)
| { keykind = Some Keykind_package_dist; package_version = None; _ } ->
return None
| {
keykind = Some Keykind_user_form;
checksum_sha256 = _;
values_canonical_id = _;
module_version = Some module_version;
package_version = _;
keykind_slot;
keykind_assetpath = _;
debug_reference_range;
debug_reference_file_sha256;
} ->
g_user_form keykind_slot module_version
(with_debug_reference ~debug_reference_range
~debug_reference_file_sha256)
| {
keykind = Some Keykind_user_bundle;
checksum_sha256 = _;
values_canonical_id = _;
module_version = Some module_version;
package_version = _;
keykind_slot = _;
keykind_assetpath = _;
debug_reference_range;
debug_reference_file_sha256;
} ->
g_user_bundle module_version
(with_debug_reference ~debug_reference_range
~debug_reference_file_sha256)
| {
keykind = Some Keykind_user_asset;
checksum_sha256 = _;
values_canonical_id = _;
module_version = Some module_version;
package_version = _;
keykind_slot = _;
keykind_assetpath;
debug_reference_range;
debug_reference_file_sha256;
} -> begin
match keykind_assetpath with
| None -> unimplemented_read "k_from_proto::Keykind_user_asset"
| Some p ->
g_user_asset p module_version
(with_debug_reference ~debug_reference_range
~debug_reference_file_sha256)
end
| {
keykind =
Some (Keykind_user_form | Keykind_user_bundle | Keykind_user_asset);
module_version = None;
_;
} ->
return None
let k_kvhash_to_proto (k, kvhash) : Traces.dependency =
{ depkey = Some (k_to_proto k); kvhash = Some kvhash }
let k_kvhash_from_proto ~buildlogtrace ~valuestore_get :
Traces.dependency ->
(BuildCore.Alacarte_3_2_apparatus.K.t * _) option
BuildCore.Alacarte_xpromise_apparatus.Promise.t =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
function
| { depkey = Some depkey; kvhash = Some kvhash } -> begin
let* depkey_result = k_from_proto ~buildlogtrace ~valuestore_get depkey in
match (depkey_result, kvhash) with
| Some (_show, k), _ -> return (Some (k, kvhash))
| _ -> return None
end
| _ -> return None
let v_to_proto : BuildCore.Alacarte_3_2_apparatus.V.t -> Traces.value option =
function
| BuildCore.Alacarte_3_2_apparatus.V.ValuesFile
{ value_id; value_sha256; value = Some { valuesfile_canonical_id } } ->
Some
{
valuetype = Some Traces.Valuetype_valuesfile;
value_id = Some value_id;
value_sha256 = Some value_sha256;
valuesfile_canonical_id = Some valuesfile_canonical_id;
values_canonical_id = None;
values_file_sha256 = None;
values_file_local = None;
form_values_file_sha256 = None;
form_id = None;
form_values_canonical_id = None;
object_id = None;
object_range = None;
bundle_values_file_sha256 = None;
bundle_values_file_local = None;
bundle_id = None;
bundle_range = None;
bundle_values_canonical_id = None;
asset_values_file_sha256 = None;
asset_id = None;
asset_path = None;
asset_range = None;
asset_values_canonical_id = None;
asset_mirrors = [];
asset_checksum_sha256 = None;
asset_checksum_sha1 = None;
dist_id = None;
dist_json = None;
}
| BuildCore.Alacarte_3_2_apparatus.V.ValuesFile _ ->
unimplemented_write "v_to_proto::ValuesFile"
| BuildCore.Alacarte_3_2_apparatus.V.Values
{
value_id;
value_sha256;
value =
Some
{
values_canonical_id;
values_file_sha256;
values_file_local;
values_origin = _;
values_transient = _;
};
} ->
Some
{
valuetype = Some Traces.Valuetype_values;
value_id = Some value_id;
value_sha256 = Some value_sha256;
values_canonical_id = Some values_canonical_id;
values_file_sha256 = Some values_file_sha256;
values_file_local =
Option.map
(fun (`Validated v) -> BuildCore.Io.file_origin v)
values_file_local;
valuesfile_canonical_id = None;
form_values_file_sha256 = None;
form_id = None;
form_values_canonical_id = None;
object_id = None;
object_range = None;
bundle_values_file_sha256 = None;
bundle_values_file_local = None;
bundle_id = None;
bundle_range = None;
bundle_values_canonical_id = None;
asset_values_file_sha256 = None;
asset_id = None;
asset_path = None;
asset_range = None;
asset_values_canonical_id = None;
asset_mirrors = [];
asset_checksum_sha256 = None;
asset_checksum_sha1 = None;
dist_id = None;
dist_json = None;
}
| BuildCore.Alacarte_3_2_apparatus.V.Values _ ->
unimplemented_write "v_to_proto::Values"
| BuildCore.Alacarte_3_2_apparatus.V.Distribution
{
distribution_id = package_id, package_semver;
distribution_json;
distribution = _;
} ->
Some
{
valuetype = Some Traces.Valuetype_distribution;
value_id = None;
value_sha256 = None;
dist_id = package_version_to_proto ~package_id ~package_semver;
dist_json = Some distribution_json;
valuesfile_canonical_id = None;
values_canonical_id = None;
values_file_sha256 = None;
values_file_local = None;
form_values_file_sha256 = None;
form_id = None;
form_values_canonical_id = None;
object_id = None;
object_range = None;
bundle_values_file_sha256 = None;
bundle_values_file_local = None;
bundle_id = None;
bundle_range = None;
bundle_values_canonical_id = None;
asset_values_file_sha256 = None;
asset_id = None;
asset_path = None;
asset_range = None;
asset_values_canonical_id = None;
asset_mirrors = [];
asset_checksum_sha256 = None;
asset_checksum_sha1 = None;
}
| BuildCore.Alacarte_3_2_apparatus.V.Form
{ form_values_canonical_id; form_values_file_sha256; form_id } ->
Some
{
valuetype = Some Traces.Valuetype_form;
value_id = None;
value_sha256 = None;
form_values_file_sha256 = Some form_values_file_sha256;
form_id =
module_version_to_proto ~module_id:form_id.id
~module_semver:form_id.version;
form_values_canonical_id = Some form_values_canonical_id;
valuesfile_canonical_id = None;
values_canonical_id = None;
values_file_sha256 = None;
values_file_local = None;
object_id = None;
object_range = None;
bundle_values_file_sha256 = None;
bundle_values_file_local = None;
bundle_id = None;
bundle_range = None;
bundle_values_canonical_id = None;
asset_values_file_sha256 = None;
asset_id = None;
asset_path = None;
asset_range = None;
asset_values_canonical_id = None;
asset_mirrors = [];
asset_checksum_sha256 = None;
asset_checksum_sha1 = None;
dist_id = None;
dist_json = None;
}
| BuildCore.Alacarte_3_2_apparatus.V.Object
{
value_id;
value_sha256;
value = Some { object_id; object_range; object_origin = _ };
} ->
Some
{
valuetype = Some Traces.Valuetype_object;
value_id = Some value_id;
value_sha256 = Some value_sha256;
object_id =
module_version_to_proto ~module_id:object_id.id
~module_semver:object_id.version;
object_range = range_to_proto object_range;
valuesfile_canonical_id = None;
values_canonical_id = None;
values_file_sha256 = None;
values_file_local = None;
form_values_file_sha256 = None;
form_id = None;
form_values_canonical_id = None;
bundle_values_file_sha256 = None;
bundle_values_file_local = None;
bundle_id = None;
bundle_range = None;
bundle_values_canonical_id = None;
asset_values_file_sha256 = None;
asset_id = None;
asset_path = None;
asset_range = None;
asset_values_canonical_id = None;
asset_mirrors = [];
asset_checksum_sha256 = None;
asset_checksum_sha1 = None;
dist_id = None;
dist_json = None;
}
| BuildCore.Alacarte_3_2_apparatus.V.Object _ ->
unimplemented_write "v_to_proto::Object"
| BuildCore.Alacarte_3_2_apparatus.V.Constant
{ value_id; value_sha256; value = Some { constant_transient = _ } } ->
Some
{
valuetype = Some Traces.Valuetype_constant;
value_id = Some value_id;
value_sha256 = Some value_sha256;
valuesfile_canonical_id = None;
values_canonical_id = None;
values_file_sha256 = None;
values_file_local = None;
form_values_file_sha256 = None;
form_id = None;
form_values_canonical_id = None;
object_id = None;
object_range = None;
bundle_values_file_sha256 = None;
bundle_values_file_local = None;
bundle_id = None;
bundle_range = None;
bundle_values_canonical_id = None;
asset_values_file_sha256 = None;
asset_id = None;
asset_path = None;
asset_range = None;
asset_values_canonical_id = None;
asset_mirrors = [];
asset_checksum_sha256 = None;
asset_checksum_sha1 = None;
dist_id = None;
dist_json = None;
}
| BuildCore.Alacarte_3_2_apparatus.V.Constant _ ->
unimplemented_write "v_to_proto::Constant"
| BuildCore.Alacarte_3_2_apparatus.V.Bundle
{
value_id;
value_sha256;
value =
Some
{
bundle_values_canonical_id;
bundle_values_file_sha256;
bundle_id;
bundle_range;
bundle_values_file_local;
};
} ->
Some
{
valuetype = Some Traces.Valuetype_bundle;
value_id = Some value_id;
value_sha256 = Some value_sha256;
bundle_values_canonical_id = Some bundle_values_canonical_id;
bundle_values_file_sha256 = Some bundle_values_file_sha256;
bundle_values_file_local =
Option.map
(fun (`Validated v) -> BuildCore.Io.file_origin v)
bundle_values_file_local;
bundle_id =
module_version_to_proto ~module_id:bundle_id.id
~module_semver:bundle_id.version;
bundle_range = range_to_proto bundle_range;
valuesfile_canonical_id = None;
values_canonical_id = None;
values_file_sha256 = None;
values_file_local = None;
form_values_file_sha256 = None;
form_id = None;
form_values_canonical_id = None;
object_id = None;
object_range = None;
asset_values_file_sha256 = None;
asset_id = None;
asset_path = None;
asset_range = None;
asset_values_canonical_id = None;
asset_mirrors = [];
asset_checksum_sha256 = None;
asset_checksum_sha1 = None;
dist_id = None;
dist_json = None;
}
| BuildCore.Alacarte_3_2_apparatus.V.Bundle _ ->
unimplemented_write "v_to_proto::Bundle"
| BuildCore.Alacarte_3_2_apparatus.V.Asset
{
value_id;
value_sha256;
value =
Some
{
asset_values_canonical_id;
asset_values_file_sha256;
asset_id;
asset_path;
asset_range;
asset_mirrors;
asset_checksum;
};
} ->
Some
{
valuetype = Some Traces.Valuetype_asset;
value_id = Some value_id;
value_sha256 = Some value_sha256;
asset_values_canonical_id = Some asset_values_canonical_id;
asset_values_file_sha256 = Some asset_values_file_sha256;
asset_id =
module_version_to_proto ~module_id:asset_id.id
~module_semver:asset_id.version;
asset_path = Some asset_path;
asset_range = range_to_proto asset_range;
asset_mirrors = fst asset_mirrors :: snd asset_mirrors;
asset_checksum_sha256 =
(match asset_checksum with
| `Sha256 sha256 -> checksum_to_proto sha256
| _ -> None);
asset_checksum_sha1 =
(match asset_checksum with
| `Sha1 sha1 -> checksum_to_proto sha1
| _ -> None);
valuesfile_canonical_id = None;
values_canonical_id = None;
values_file_sha256 = None;
values_file_local = None;
form_values_file_sha256 = None;
form_id = None;
form_values_canonical_id = None;
object_id = None;
object_range = None;
bundle_values_file_sha256 = None;
bundle_values_file_local = None;
bundle_id = None;
bundle_range = None;
bundle_values_canonical_id = None;
dist_id = None;
dist_json = None;
}
| BuildCore.Alacarte_3_2_apparatus.V.Asset _ ->
unimplemented_write "v_to_proto::Asset"
| BuildCore.Alacarte_3_2_apparatus.V.Input_not_found _ ->
unimplemented_write "v_to_proto::Input_not_found"
| BuildCore.Alacarte_3_2_apparatus.V.Failure_is_pending ->
unimplemented_write "v_to_proto::Failure_is_pending"
module CstIo =
MlFront_Thunk.ThunkCst.Io (BuildCore.Alacarte_xpromise_apparatus.Promise)
type exists_result = ValueNotNeeded | ValueExists | ValueMissing
let direct_valuesfile_from_proto ~value_existence_check ~key ~value_id
~value_sha256 ~valuesfile_canonical_id =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let* exists_result = value_existence_check key value_id in
match exists_result with
| ValueMissing -> return None
| ValueNotNeeded | ValueExists -> begin
let show = fun () -> "valuesfile" in
return
(Some
( show,
({
value_id;
value_sha256;
value = Some { valuesfile_canonical_id };
}
: V.valuesfile V.persistent) ))
end
let v_valuesfile_from_proto ~value_existence_check key : Traces.value -> _ =
let open BuildCore.Alacarte_3_2_apparatus in
function
| {
valuetype = Some Valuetype_valuesfile;
value_id = Some value_id;
value_sha256 = Some value_sha256;
valuesfile_canonical_id = Some valuesfile_canonical_id;
values_canonical_id = _;
values_file_sha256 = _;
values_file_local = _;
form_values_file_sha256 = _;
form_id = _;
form_values_canonical_id = _;
object_id = _;
object_range = _;
bundle_values_file_sha256 = _;
bundle_values_file_local = _;
bundle_id = _;
bundle_range = _;
bundle_values_canonical_id = _;
asset_values_file_sha256 = _;
asset_id = _;
asset_path = _;
asset_range = _;
asset_values_canonical_id = _;
asset_mirrors = _;
asset_checksum_sha256 = _;
asset_checksum_sha1 = _;
dist_id = _;
dist_json = _;
}
when String.starts_with ~prefix:BuildPaths.prefix_valuesjsonfile value_id
-> (
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let* show_and_v_opt =
direct_valuesfile_from_proto ~value_existence_check ~key ~value_id
~value_sha256 ~valuesfile_canonical_id
in
match show_and_v_opt with
| None -> return None
| Some (show, v) -> return (Some (show, V.ValuesFile v)))
| { value_id; value_sha256; _ } ->
unimplemented_read
(Printf.sprintf "v_from_proto::Valuetype_valuesfile::1::%b::%b"
(value_id <> None) (value_sha256 <> None))
let direct_values_from_proto ~value_id ~value_sha256 ~values_canonical_id
~values_file_sha256 ~values_file_local ~values_file values =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let* safe_maybe_local_values_file =
match values_file_local with
| None -> return None
| Some local_path ->
BuildInstance.ValueStore.safe_local_values_file
~values_file_local:local_path ~values_file_sha256
in
let value : V.values =
{
values_canonical_id;
values_file_sha256;
values_file_local = safe_maybe_local_values_file;
values_origin = None;
values_transient =
Some
{
values;
values_file =
Option.value
~default:(BuildCore.Io.disk_file values_file)
(Option.map
(fun (`Validated v) -> v)
safe_maybe_local_values_file);
};
}
in
let show = fun () -> "values " ^ value_id in
return (Some (show, V.Values { value_id; value_sha256; value = Some value }))
let v_values_from_proto ~parsetrace ~valuestore_get ~valuestore_put
~build_pubkey ~build_seckey key : Traces.value -> _ =
let open BuildCore.Alacarte_3_2_apparatus in
let open BuildInstance in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
function
| {
valuetype = Some Valuetype_values;
value_id = Some value_id;
value_sha256 = Some expected_value_sha256;
valuesfile_canonical_id = _;
values_canonical_id = Some values_canonical_id;
values_file_sha256 = Some values_file_sha256;
values_file_local;
form_values_file_sha256 = _;
form_id = _;
form_values_canonical_id = _;
object_id = _;
object_range = _;
bundle_values_file_sha256 = _;
bundle_values_file_local = _;
bundle_id = _;
bundle_range = _;
bundle_values_canonical_id = _;
asset_values_file_sha256 = _;
asset_id = _;
asset_path = _;
asset_range = _;
asset_values_canonical_id = _;
asset_mirrors = _;
asset_checksum_sha256 = _;
asset_checksum_sha1 = _;
dist_id = _;
dist_json = _;
}
when String.starts_with ~prefix:BuildPaths.prefix_values value_id -> (
let* values_opt =
ValueStore.get_generic_ast0 ~valuestore_get:(valuestore_get key)
~build_pubkey ~value_id
~unmarshal:(fun bs -> Marshal.from_bytes bs 0)
()
in
let valuesjsonfile_value_id =
V.get_valuesjsonfile_value_id ~values_file_sha256
in
match values_opt with
| Some (values, `Sha256 value_sha256)
when String.equal expected_value_sha256 value_sha256 -> (
let* valuesfile_opt = valuestore_get key valuesjsonfile_value_id in
match valuesfile_opt with
| None -> return None
| Some values_file ->
direct_values_from_proto ~value_id ~value_sha256
~values_canonical_id ~values_file_sha256 ~values_file_local
~values_file values)
| Some _ ->
return None
| None ->
begin
Assumptions
.parsed_values_are_reparsed_when_incompatible_with_runtime ();
let* valuesfile_fp_opt =
valuestore_get key valuesjsonfile_value_id
in
match valuesfile_fp_opt with
| Some valuesfile_fp ->
let valuesfile = BuildCore.Io.disk_file valuesfile_fp in
let* parsed =
CstIo.parse ~downgrade_errors_into_warnings:()
(module TraceStoreObserver)
valuesfile
in
if parsetrace then
Printf.eprintf "[parse] %s b/c incompatible ast\n"
valuesjsonfile_value_id;
begin
match parsed with
| Error _ -> return None
| Ok cst ->
let values_canonical_id' =
MlFront_Thunk.ThunkCst.canonical_id cst
in
if String.equal values_canonical_id values_canonical_id'
then
match
MlFront_Thunk.ThunkAst.parse
~downgrade_errors_into_warnings:()
~origin:(Some (BuildCore.Io.file_origin valuesfile))
~inferred_package_id_or_reason_whynone:
(Either.right
"trace store loads do not have implicit \
package ids")
(module TraceStoreObserver)
cst
with
| Error _ -> return None
| Ok values ->
let* _added, (value_sha256, _value_sz) =
BuildInstance.ValueStore.add_generic_ast0
~valuestore_put:(valuestore_put key)
~build_seckey ~value_id
~marshalled_ast_bytes:(fun () ->
Marshal.to_bytes values [])
()
in
direct_values_from_proto ~value_id ~value_sha256
~values_canonical_id ~values_file_sha256
~values_file_local ~values_file:valuesfile_fp
values
else return None
end
| None -> return None
end)
| { value_id; values_file_sha256; values_canonical_id; _ } ->
unimplemented_read
(Printf.sprintf "v_from_proto::Valuetype_values::1::%b::%b::%b"
(value_id <> None)
(values_file_sha256 <> None)
(values_canonical_id <> None))
let v_form_from_proto : Traces.value -> _ =
let open BuildCore.Alacarte_3_2_apparatus in
let return = BuildCore.Alacarte_xpromise_apparatus.Promise.return in
function
| {
valuetype = Some Valuetype_form;
value_id = _;
value_sha256 = _;
valuesfile_canonical_id = _;
values_canonical_id = _;
values_file_sha256 = _;
values_file_local = _;
form_id = Some form_id;
form_values_file_sha256 = Some form_values_file_sha256;
form_values_canonical_id = Some form_values_canonical_id;
object_id = _;
object_range = _;
bundle_values_file_sha256 = _;
bundle_values_file_local = _;
bundle_id = _;
bundle_range = _;
bundle_values_canonical_id = _;
asset_values_file_sha256 = _;
asset_id = _;
asset_path = _;
asset_range = _;
asset_values_canonical_id = _;
asset_mirrors = _;
asset_checksum_sha256 = _;
asset_checksum_sha1 = _;
dist_id = _;
dist_json = _;
} -> (
match module_version_from_proto form_id with
| None ->
return None
| Some (id, version) ->
let value : V.form =
{
form_values_canonical_id;
form_values_file_sha256;
form_id = { id; version };
}
in
let show = fun () -> "form" in
return (Some (show, V.Form value)))
| { value_id; form_values_file_sha256; form_id; form_values_canonical_id; _ }
->
unimplemented_read
(Printf.sprintf "v_from_proto::Valuetype_form::1::%b::%b::%b::%b"
(value_id <> None)
(form_values_file_sha256 <> None)
(form_id <> None)
(form_values_canonical_id <> None))
let v_bundle_from_proto ~value_existence_check key : Traces.value -> _ =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
function
| {
valuetype = Some Valuetype_bundle;
value_id = Some value_id;
value_sha256 = Some value_sha256;
valuesfile_canonical_id = _;
values_canonical_id = _;
values_file_sha256 = _;
values_file_local = _;
form_id = _;
form_values_file_sha256 = _;
form_values_canonical_id = _;
object_id = _;
object_range = _;
bundle_values_file_sha256 = Some bundle_values_file_sha256;
bundle_values_file_local = Some bundle_values_file_local;
bundle_id = Some id;
bundle_range = Some range;
bundle_values_canonical_id = Some bundle_values_canonical_id;
asset_values_file_sha256 = _;
asset_id = _;
asset_path = _;
asset_range = _;
asset_values_canonical_id = _;
asset_mirrors = _;
asset_checksum_sha256 = _;
asset_checksum_sha1 = _;
dist_id = _;
dist_json = _;
}
when String.starts_with ~prefix:BuildPaths.prefix_bundle value_id -> (
match (module_version_from_proto id, range_from_proto range) with
| Some (id, version), Some bundle_range -> begin
let* exists_result1 = value_existence_check key value_id in
let* exists_result2 =
value_existence_check key
(V.get_valuesjsonfile_value_id
~values_file_sha256:bundle_values_file_sha256)
in
match (exists_result1, exists_result2) with
| _, ValueMissing | ValueMissing, _ ->
Assumptions
.persisted_values_are_checked_for_existence_during_trace_store_load
();
return None
| (ValueNotNeeded | ValueExists), (ValueNotNeeded | ValueExists) ->
let* bundle_values_file_local =
Assumptions.no_trust_for_local_values_file ();
BuildInstance.ValueStore.safe_local_values_file
~values_file_local:bundle_values_file_local
~values_file_sha256:bundle_values_file_sha256
in
let value : V.bundle option =
Some
{
bundle_id = { id; version };
bundle_values_canonical_id;
bundle_values_file_sha256;
bundle_range;
bundle_values_file_local;
}
in
let show = fun () -> "bundle " ^ value_id in
return (Some (show, V.Bundle { value_id; value_sha256; value }))
end
| _ -> return None)
| _ -> unimplemented_read "v_from_proto::Valuetype_bundle"
let v_asset_from_proto ~value_existence_check key : Traces.value -> _ =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
function
| {
valuetype = Some Valuetype_asset;
value_id = Some value_id;
value_sha256 = Some value_sha256;
valuesfile_canonical_id = _;
values_canonical_id = _;
values_file_sha256 = _;
values_file_local = _;
form_values_file_sha256 = _;
form_id = _;
form_values_canonical_id = _;
object_id = _;
object_range = _;
bundle_values_file_sha256 = _;
bundle_values_file_local = _;
bundle_id = _;
bundle_range = _;
bundle_values_canonical_id = _;
asset_values_file_sha256 = Some asset_values_file_sha256;
asset_id = Some id;
asset_path = Some asset_path;
asset_range = Some range;
asset_values_canonical_id = Some asset_values_canonical_id;
asset_mirrors = first_mirror :: rest_mirrors;
asset_checksum_sha256;
asset_checksum_sha1;
dist_id = _;
dist_json = _;
}
when String.starts_with ~prefix:BuildPaths.prefix_asset value_id -> (
match (module_version_from_proto id, range_from_proto range) with
| Some (id, version), Some asset_range ->
let file_checksum =
match (asset_checksum_sha256, asset_checksum_sha1) with
| Some sha256, _ ->
checksum_from_proto sha256
|> Option.map (fun sha256 -> `Sha256 sha256)
| None, Some sha1 ->
checksum_from_proto sha1 |> Option.map (fun sha1 -> `Sha1 sha1)
| None, None -> None
in
begin
let* exists_result = value_existence_check key value_id in
match (exists_result, file_checksum) with
| ValueMissing, _ | _, None -> return None
| (ValueNotNeeded | ValueExists), Some file_checksum -> begin
let value : V.asset option =
Some
{
asset_values_canonical_id;
asset_values_file_sha256;
asset_id = { id; version };
asset_path;
asset_range;
asset_mirrors = (first_mirror, rest_mirrors);
asset_checksum = file_checksum;
}
in
let show = fun () -> "asset " ^ value_id in
return (Some (show, V.Asset { value_id; value_sha256; value }))
end
end
| _ -> return None)
| _ -> unimplemented_read "v_from_proto::Valuetype_asset"
let v_constant_from_proto ~valuestore_get key : Traces.value -> _ =
let open BuildCore.Alacarte_3_2_apparatus in
let open BuildInstance in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
function
| {
valuetype = Some Valuetype_constant;
value_id = Some value_id;
value_sha256 = Some value_sha256;
valuesfile_canonical_id = _;
values_canonical_id = _;
values_file_sha256 = _;
values_file_local = _;
form_values_file_sha256 = _;
form_id = _;
form_values_canonical_id = _;
object_id = _;
object_range = _;
bundle_values_file_sha256 = _;
bundle_values_file_local = _;
bundle_id = _;
bundle_range = _;
bundle_values_canonical_id = _;
asset_values_file_sha256 = _;
asset_id = _;
asset_path = _;
asset_range = _;
asset_values_canonical_id = _;
asset_mirrors = _;
asset_checksum_sha256 = _;
asset_checksum_sha1 = _;
dist_id = _;
dist_json = _;
}
when String.starts_with ~prefix:BuildPaths.prefix_constant value_id -> (
let* constant_opt =
ValueStore.get_constant0 ~valuestore_get:(valuestore_get key) ~value_id
()
in
match constant_opt with
| None ->
return None
| Some constant_value ->
let show = fun () -> "constant " ^ value_id in
return
(Some
( show,
V.Constant
{
value_id;
value_sha256;
value =
Some { constant_transient = Some { constant_value } };
} )))
| _ -> unimplemented_read "v_from_proto::Valuetype_constant"
let v_distribution_from_proto value =
match (value : Traces.value) with
| {
valuetype = _;
value_id = _;
value_sha256 = _;
valuesfile_canonical_id = _;
values_canonical_id = _;
values_file_sha256 = _;
values_file_local = _;
form_values_file_sha256 = _;
form_id = _;
form_values_canonical_id = _;
object_id = _;
object_range = _;
bundle_values_file_sha256 = _;
bundle_id = _;
bundle_range = _;
bundle_values_canonical_id = _;
bundle_values_file_local = _;
asset_values_file_sha256 = _;
asset_id = _;
asset_path = _;
asset_range = _;
asset_values_canonical_id = _;
asset_mirrors = _;
asset_checksum_sha256 = _;
asset_checksum_sha1 = _;
dist_id = Some dist_id;
dist_json = Some dist_json;
} ->
let open BuildCore.Alacarte_3_2_apparatus in
let return = BuildCore.Alacarte_xpromise_apparatus.Promise.return in
begin
match package_version_from_proto dist_id with
| None -> return None
| Some (package_id, package_semver) -> (
let show = fun () -> "distribution" in
let jsonfile =
BuildCore.Io.inmemory_file
~origin:
(MlFront_Core.FilePath.of_string_exn "/dev/null/tracestore")
dist_json
in
let dist_result =
BuildCore.Alacarte_xpromise_apparatus.Promise.run_promise
@@ SecDist.DistIo.parse (module TraceStoreObserver) jsonfile
in
match dist_result with
| Error _ -> return None
| Ok distribution ->
return
(Some
( show,
V.Distribution
{
distribution_id = (package_id, package_semver);
distribution_json = dist_json;
distribution;
} )))
end
| _ -> unimplemented_read "v_from_proto::Valuetype_distribution"
let v_object_from_proto ~value_existence_check key : Traces.value -> _ =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
function
| {
valuetype = Some Valuetype_object;
value_id = Some value_id;
value_sha256 = Some value_sha256;
valuesfile_canonical_id = _;
values_canonical_id = _;
values_file_sha256 = _;
values_file_local = _;
form_values_file_sha256 = _;
form_id = _;
form_values_canonical_id = _;
object_id = Some id;
object_range = Some range;
bundle_values_file_sha256 = _;
bundle_values_file_local = _;
bundle_id = _;
bundle_range = _;
bundle_values_canonical_id = _;
asset_values_file_sha256 = _;
asset_id = _;
asset_path = _;
asset_range = _;
asset_values_canonical_id = _;
asset_mirrors = _;
asset_checksum_sha256 = _;
asset_checksum_sha1 = _;
dist_id = _;
dist_json = _;
}
when String.starts_with ~prefix:BuildPaths.prefix_object value_id -> (
match (module_version_from_proto id, range_from_proto range) with
| Some (id, version), Some object_range -> begin
let* exists_result = value_existence_check key value_id in
match exists_result with
| ValueMissing -> return None
| ValueNotNeeded | ValueExists ->
let value : V.object_ option =
Some
{
object_id = { id; version };
object_range;
object_origin = None;
}
in
let show = fun () -> "object " ^ value_id in
return (Some (show, V.Object { value_id; value_sha256; value }))
end
| _ -> return None)
| { valuetype = Some Valuetype_object; value_id = Some value_id; _ } ->
unimplemented_read ("v_from_proto::Valuetype_object::" ^ value_id)
| _ -> unimplemented_read "v_from_proto::Valuetype_object"
let v_from_proto ~parsetrace ~value_existence_check ~valuestore_get
~valuestore_put ~build_pubkey ~build_seckey key value :
((unit -> string) * BuildCore.Alacarte_3_2_apparatus.V.t) option
BuildCore.Alacarte_xpromise_apparatus.Promise.t =
Assumptions.persisted_values_are_checked_for_existence_during_trace_store_load
();
let return = BuildCore.Alacarte_xpromise_apparatus.Promise.return in
match (value : Traces.value) with
| { valuetype = Some Valuetype_valuesfile; _ } ->
v_valuesfile_from_proto ~value_existence_check key value
| { valuetype = Some Valuetype_values; _ } ->
v_values_from_proto ~parsetrace ~valuestore_get ~valuestore_put
~build_pubkey ~build_seckey key value
| { valuetype = Some Valuetype_form; _ } -> v_form_from_proto value
| { valuetype = Some Valuetype_bundle; _ } ->
v_bundle_from_proto ~value_existence_check key value
| { valuetype = Some Valuetype_asset; _ } ->
v_asset_from_proto ~value_existence_check key value
| { valuetype = Some Valuetype_constant; _ } ->
v_constant_from_proto ~valuestore_get key value
| { valuetype = Some Valuetype_object; _ } ->
v_object_from_proto ~value_existence_check key value
| { valuetype = Some Valuetype_distribution; _ } ->
v_distribution_from_proto value
| { valuetype = Some Valuetype_unspecified; _ } ->
unimplemented_read "v_from_proto::Valuetype_unspecified"
| { valuetype = Some _; _ } -> .
| _ -> return None
let to_proto :
BuildCore.Alacarte_6_4_test.StateSuspending.trace ->
Traces.constructive_trace = function
| { key; dependencies; result } ->
{
key = Some (k_to_proto key);
dependencies = List.map k_kvhash_to_proto dependencies;
value = v_to_proto result;
}
let show_keykind : Traces.key_kind -> string = function
| Keykind_unspecified -> "unspecified"
| Keykind_user_form -> "user_form"
| Keykind_user_bundle -> "user_bundle"
| Keykind_user_asset -> "user_asset"
| Keykind_checksum_valuesfile -> "checksum_valuesfile"
| Keykind_package_dist -> "package_dist"
let show_dependency : Traces.dependency -> string = function
| {
depkey =
Some
{
keykind;
debug_reference_file_sha256 = _;
checksum_sha256 = _;
values_canonical_id;
module_version = _;
package_version = _;
keykind_slot = _;
keykind_assetpath = _;
debug_reference_range = _;
};
kvhash = Some _;
} ->
Printf.sprintf "keykind=%s, values_canonical_id=%s"
(Option.map show_keykind keykind |> Option.value ~default:"(no keykind)")
(Option.value ~default:"<none>" values_canonical_id)
| _ -> "(malformed dependency)"
let does_function_change_value ~valuestore_get ~build_pubkey
~values_canonical_id k f =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let* values_opt =
BuildInstance.ValueStore.get_generic_ast0 ~valuestore_get:(valuestore_get k)
~build_pubkey
~value_id:(V.get_values_value_id ~values_canonical_id)
~unmarshal:(fun bs -> Marshal.from_bytes bs 0)
()
in
begin
match values_opt with
| None ->
return true
| Some (values, `Sha256 _value_sha256) ->
match f values with
| `Unmodified -> return false
| `Modified _ -> return true
end
(** Check if a value has been transformed by a function.*)
let is_transformed_value ~valuestore_get ~build_pubkey ~transform_values k v =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
match transform_values with
| None -> return false
| Some f -> begin
match (v : V.t) with
| V.Values
{
value_id = _;
value_sha256 = _;
value =
Some
{
values_canonical_id = _;
values_file_sha256 = _;
values_file_local = _;
values_origin = _;
values_transient = Some { values; values_file = _ };
};
} -> (
match f values with
| `Unmodified -> return false
| `Modified _ -> return true)
| V.Bundle
{
value_id = _;
value_sha256 = _;
value =
Some
{
bundle_values_canonical_id;
bundle_values_file_sha256 = _;
bundle_id = _;
bundle_range = _;
bundle_values_file_local = _;
};
} ->
let* changed =
does_function_change_value ~valuestore_get ~build_pubkey
~values_canonical_id:bundle_values_canonical_id k f
in
if changed then return true else return false
| V.Asset
{
value_id = _;
value_sha256 = _;
value =
Some
{
asset_values_canonical_id;
asset_values_file_sha256 = _;
asset_id = _;
asset_path = _;
asset_range = _;
asset_mirrors = _;
asset_checksum = _;
};
} ->
let* changed =
does_function_change_value ~valuestore_get ~build_pubkey
~values_canonical_id:asset_values_canonical_id k f
in
if changed then return true else return false
| _ -> return false
end
type reader_state = {
buildlogtrace : bool;
parsetrace : bool;
value_existence_check :
BuildCore.Alacarte_3_2_apparatus.K.t ->
string ->
exists_result BuildCore.Alacarte_xpromise_apparatus.Promise.t;
valuestore_get :
BuildCore.Alacarte_3_2_apparatus.K.t ->
string ->
MlFront_Core.FilePath.t option
BuildCore.Alacarte_xpromise_apparatus.Promise.t;
valuestore_put :
BuildCore.Alacarte_3_2_apparatus.K.t ->
string ->
string ->
(bool * (string * int64)) BuildCore.Alacarte_xpromise_apparatus.Promise.t;
build_pubkey : [ `PublicKey of string ];
build_seckey : [ `SecretKey of string ];
transform_values :
(MlFront_Thunk.ThunkAst.t ->
[ `Modified of MlFront_Thunk.ThunkAst.t | `Unmodified ])
option;
keys_to_invalidate : (BuildCore.Alacarte_3_2_apparatus.K.t, unit) Hashtbl.t;
}
(** [keys_to_invalidate] is maintained so we can scan over all the traces and
remove any matching ValuesFile. Confer
{!Assumptions.cannot_assume_asset_and_assets_come_before_values_files_in_traces}.
*)
let from_proto
({
buildlogtrace;
parsetrace;
value_existence_check;
valuestore_get;
valuestore_put;
build_pubkey;
build_seckey;
transform_values;
keys_to_invalidate;
} :
reader_state) :
Traces.constructive_trace ->
BuildInstance.ValueStore.trace option
BuildCore.Alacarte_xpromise_apparatus.Promise.t =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
function
| { key = None; dependencies = _; value = _ } ->
if buildlogtrace then Printf.eprintf "[buildlog] no key\n";
return None
| { key = _; dependencies = _; value = None } ->
if buildlogtrace then Printf.eprintf "[buildlog] no value\n";
return None
| { key = Some key; dependencies; value = Some value } -> (
let* k_result = k_from_proto ~buildlogtrace ~valuestore_get key in
match k_result with
| None ->
if buildlogtrace then Printf.eprintf "[buildlog] key miss\n";
return None
| Some (k_show, k) -> begin
let maybe_deps_promises =
List.map
(k_kvhash_from_proto ~buildlogtrace ~valuestore_get)
dependencies
in
let* maybe_deps =
BuildCore.Alacarte_xpromise_apparatus.Promise.parallel
maybe_deps_promises
in
let deps' = List.filter_map Fun.id maybe_deps in
if List.length maybe_deps <> List.length deps' then begin
(if buildlogtrace then
let maybe_dep_rows = List.combine dependencies maybe_deps in
let missing_deps =
List.filter_map
(fun (dep, dep_result) ->
match dep_result with
| None -> Some (show_dependency dep)
| Some _ -> None)
maybe_dep_rows
in
Printf.eprintf
"[buildlog] miss %s. %d of %d deps found. missing: %s\n"
(k_show ()) (List.length deps') (List.length maybe_deps)
(String.concat ", " missing_deps));
return None
end
else begin
let* v_opt =
v_from_proto ~parsetrace ~value_existence_check ~valuestore_get
~valuestore_put ~build_pubkey ~build_seckey k value
in
match v_opt with
| None ->
if buildlogtrace then
Printf.eprintf "[buildlog] value miss %s\n" (k_show ());
return None
| Some (v_show, v) ->
let* is_transformed =
is_transformed_value ~valuestore_get ~build_pubkey
~transform_values k v
in
if is_transformed then (
Hashtbl.add keys_to_invalidate k ();
if buildlogtrace then
Printf.eprintf "[buildlog] skip changed %s\n" (k_show ());
return None)
else (
if buildlogtrace then
Printf.eprintf "[buildlog] read %s %s. %d deps\n"
(v_show ()) (k_show ()) (List.length deps');
return
(Some
({ key = k; dependencies = deps'; result = v }
: BuildCore.Alacarte_6_4_test.StateSuspending.trace)))
end
end)
let max_segment_size = 16777216
let stop_segment_size = 12582912
module StringSet = Set.Make (String)
module KeySet = Set.Make (BuildCore.Alacarte_3_2_apparatus.K)
let remove_pending_keys ~buildlogtrace ~keys_to_invalidate traces =
let open BuildCore.Alacarte_3_2_apparatus in
let ( let* ) = BuildCore.Alacarte_xpromise_apparatus.Promise.bind in
let return = BuildCore.Alacarte_xpromise_apparatus.Promise.return in
let graph =
BuildCore.Alacarte_6_4_test.StateSuspending
.create_ancestorgraph_from_trace_list traces
in
let dfs_iter_once =
CCGraph.Traverse.dfs ~tbl:(CCGraph.mk_map ~cmp:K.compare ()) ~graph
(fun f ->
Hashtbl.iter
(fun key_to_invalidate () -> f key_to_invalidate)
keys_to_invalidate)
in
let key_removals = ref KeySet.empty in
dfs_iter_once (fun transitive_key ->
key_removals := KeySet.add transitive_key !key_removals);
List.fold_left
(fun acc
({ key; dependencies = _deps; result = _ } as trace :
BuildInstance.ValueStore.trace) ->
let* acc = acc in
if KeySet.mem key !key_removals then (
if buildlogtrace then
Printf.eprintf "[buildlog] skip backtracked %s\n" (K.show key);
return acc)
else return (trace :: acc))
(return []) traces
let read_segment_exn ~on_fail (reader_state : reader_state) bytes pos len =
let safe_sub_segment_exn bytes pos len =
try Bytes.sub bytes pos len
with Invalid_argument _ -> raise (on_fail "premature end of segment")
in
let rec aux acc bytes pos len =
if pos >= len then List.rev acc
else begin
let tracesz_b = safe_sub_segment_exn bytes pos 4 in
let tracesz32 = Bytes.get_int32_be tracesz_b 0 in
if Int64.compare (Int64.of_int32 tracesz32) (Int64.of_int max_int) > 0
then raise (on_fail "trace segment size too large");
let tracesz = Int32.to_int tracesz32 in
let trace_b = safe_sub_segment_exn bytes (pos + 4) tracesz in
let nextpos = pos + 4 + tracesz in
let decoder = Pbrt.Decoder.of_bytes trace_b in
try
let ctrace = Traces.decode_pb_constructive_trace decoder in
let trace_promise = from_proto reader_state ctrace in
let trace_and_sz_promise =
BuildCore.Alacarte_xpromise_apparatus.Promise.map
(fun x -> (x, `TraceSize tracesz))
trace_promise
in
aux (trace_and_sz_promise :: acc) bytes nextpos len
with Pbrt.Decoder.Failure _msg -> aux acc bytes nextpos len
end
in
aux [] bytes pos len
let read_exactly : Unix.file_descr -> bytes -> int -> int -> int =
fun fd buf pos len ->
let rec aux off total remaining =
if remaining <= 0 then total
else
let got = Unix.read fd buf off remaining in
if got = 0 then total else aux (off + got) (total + got) (remaining - got)
in
aux pos 0 len
let read_segment_frame ~on_finish ~on_fail ~on_more reader_state tracefd =
let segmentsz_b = Bytes.create 4 in
let was_read = read_exactly tracefd segmentsz_b 0 4 in
if was_read = 0 then on_finish
else begin
if was_read <> 4 then raise (on_fail "failed to read trace size");
let segment_cksum_b = Bytes.create 32 in
let was_read = read_exactly tracefd segment_cksum_b 0 32 in
if was_read <> 32 then raise (on_fail "failed to read trace checksum");
let segmentsz32 = Bytes.get_int32_be segmentsz_b 0 in
if Int64.compare (Int64.of_int32 segmentsz32) (Int64.of_int max_int) > 0
then raise (on_fail "trace size too large");
let segmentsz = Int32.to_int segmentsz32 in
let trace_b = Bytes.create segmentsz in
let was_read = read_exactly tracefd trace_b 0 segmentsz in
if was_read <> segmentsz then
raise
(on_fail
(Printf.sprintf
"failed to read trace segment. %d bytes expected, %d bytes read"
segmentsz was_read));
let segment_cksum =
String.to_bytes
(Digestif.SHA256.to_raw_string (Digestif.SHA256.digest_bytes trace_b))
in
if not (Bytes.equal segment_cksum segment_cksum_b) then
raise (on_fail "failed to verify trace segment checksum");
let exception SegmentError of string in
let moretraces_promises =
try
read_segment_exn
~on_fail:(fun s -> SegmentError s)
reader_state trace_b 0 segmentsz
with SegmentError msg -> raise (on_fail msg)
in
on_more (moretraces_promises, `SegmentSize segmentsz)
end
let rec read_traces_into_list ~on_fail reader_state tracefd =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let rec aux_partial_traces_exn acc =
let* acc = acc in
match
read_segment_frame
~on_finish:(`Finished (return acc))
~on_fail:(on_fail acc)
~on_more:(fun ps -> `More ps)
reader_state tracefd
with
| `Finished res -> res
| `More (moretraces_promises, `SegmentSize _) ->
let* moretraces =
BuildCore.Alacarte_xpromise_apparatus.Promise.parallel
moretraces_promises
in
aux_partial_traces_exn (return (moretraces :: acc))
in
let* traces = aux_partial_traces_exn (return []) in
from_option_list_list traces
and from_option_list_list :
(BuildInstance.ValueStore.trace option * [ `TraceSize of int ]) list list ->
BuildInstance.ValueStore.trace option list
MlFront_Thunk.Promises.PromiseMinimal.t =
let return = BuildCore.Alacarte_xpromise_apparatus.Promise.return in
fun x -> return (List.map fst (List.flatten (List.rev x)))
let rec read_traces_gracefully ~preconfig ~transform_values tracefd =
let buildlogtrace = BuildConfig.preconfig_buildlogtrace preconfig in
let parsetrace = BuildConfig.preconfig_parsetrace preconfig in
let valuestore = BuildConfig.preconfig_valuestore preconfig in
let build_pubkey = BuildConfig.preconfig_build_pubkey preconfig in
let build_seckey = BuildConfig.preconfig_build_seckey preconfig in
let (_offset : int) = Unix.lseek tracefd 0 Unix.SEEK_SET in
let keys_to_invalidate = Hashtbl.create 16 in
let valuestore_get _key value_id =
BuildInstance.ValueStore.get_value_file ~valuestore ~value_id ()
in
let valuestore_put _key value_id contents =
BuildInstance.ValueStore.put_value_file ~valuestore ~value_id contents
in
let value_existence_check =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
match BuildConfig.preconfig_integrity preconfig with
| `Existence -> (
fun key value_id ->
Assumptions
.persisted_values_are_checked_for_existence_during_trace_store_load ();
let* value_fp_opt = valuestore_get key value_id in
match value_fp_opt with
| None -> return ValueMissing
| Some _value_fp -> return ValueExists)
| `Checksum | `None -> fun _key _value_id -> return ValueNotNeeded
in
let reader_state : reader_state =
{
buildlogtrace;
parsetrace;
value_existence_check;
valuestore_get;
valuestore_put;
build_pubkey;
build_seckey;
transform_values;
keys_to_invalidate;
}
in
let exception
PartialTraces of
(string
* BuildInstance.ValueStore.trace option list
MlFront_Thunk.Promises.PromiseMinimal.t)
in
match
read_traces_into_list
~on_fail:(fun acc s -> PartialTraces (s, from_option_list_list acc))
reader_state tracefd
with
| traces -> load_traces ~buildlogtrace ~keys_to_invalidate traces
| exception PartialTraces (msg, traces) ->
Printf.eprintf "[buildlog] [warning] failed to read all traces: %s\n" msg;
load_traces ~buildlogtrace ~keys_to_invalidate traces
and load_traces ~buildlogtrace ~keys_to_invalidate traces =
let traces' = BuildInstance.Launcher.run_isolated_promise traces in
let traces' = List.filter_map Fun.id traces' in
if Hashtbl.length keys_to_invalidate = 0 then traces'
else
BuildInstance.Launcher.run_isolated_promise
(remove_pending_keys ~buildlogtrace ~keys_to_invalidate traces')
let visit_traces_in_file ~build_pubkey ~build_seckey ~valuestore_get
~valuestore_put tracefd visit_trace =
let value_existence_check key value_id =
let ( let* ), return =
BuildCore.Alacarte_xpromise_apparatus.Promise.(bind, return)
in
let* value_fp_opt = valuestore_get key value_id in
match value_fp_opt with
| None -> return ValueMissing
| Some _value_fp -> return ValueExists
in
let reader_state : reader_state =
{
buildlogtrace = false;
parsetrace = false;
value_existence_check;
valuestore_get;
valuestore_put;
build_pubkey;
build_seckey;
transform_values = None;
keys_to_invalidate = Hashtbl.create 0;
}
in
let exception FailedRead of string in
let rec aux_partial_traces_exn () =
match
read_segment_frame ~on_finish:(`Finished ())
~on_fail:(fun s -> FailedRead s)
~on_more:(fun ps -> `More ps)
reader_state tracefd
with
| `Finished () -> ()
| `More (moretraces_promises, `SegmentSize _) ->
let moretraces =
BuildCore.Alacarte_xpromise_apparatus.Promise.run_promise
@@ BuildCore.Alacarte_xpromise_apparatus.Promise.parallel
moretraces_promises
in
List.iter
(fun (trace, `TraceSize sz) ->
match trace with
| None -> ()
| Some
({ key; dependencies; result } : BuildInstance.ValueStore.trace)
->
visit_trace sz key dependencies result)
moretraces;
aux_partial_traces_exn ()
in
aux_partial_traces_exn ()
let save ~config traces tracefd =
let buildlogtrace = BuildConfig.buildlogtrace config in
let dtraces_promise =
BuildInstance.ValueStore.dehydrate_traces
?buildlogtrace:(if buildlogtrace then Some () else None)
~valuestore:(BuildConfig.valuestore config)
~build_seckey:(BuildConfig.build_seckey config)
traces
in
let dtraces = BuildInstance.Launcher.run_isolated_promise dtraces_promise in
let exception CouldNotTrace of string in
let (_offset : int) = Unix.lseek tracefd 0 Unix.SEEK_SET in
let write _what buf pos len =
let ret = Unix.write tracefd buf pos len in
ret
in
try
let buf = Buffer.create max_segment_size in
let encoder = Pbrt.Encoder.create () in
let flush_buffer len =
let seg_b = Buffer.to_bytes buf in
let segsz = Bytes.length seg_b in
if segsz = 0 then len
else begin
if segsz > max_segment_size then
raise (CouldNotTrace "Trace segment too large");
let segsz_b = Bytes.create 4 in
Bytes.set_int32_be segsz_b 0 (Int32.of_int segsz);
let was_written = write "header" segsz_b 0 4 in
if was_written <> 4 then
raise (CouldNotTrace "Failed to write trace segment header");
let seg_cksum =
Digestif.SHA256.to_raw_string (Digestif.SHA256.digest_bytes seg_b)
in
let was_written = write "checksum" (String.to_bytes seg_cksum) 0 32 in
if was_written <> 32 then
raise (CouldNotTrace "Failed to write trace segment checksum");
let was_written = write "segment" seg_b 0 segsz in
if was_written <> segsz then
raise (CouldNotTrace "Failed to write trace segment data");
Buffer.clear buf;
Pbrt.Encoder.reset encoder;
let len' = len + 4 + 32 + segsz in
if len' < 0 then raise (CouldNotTrace "Trace file length overflow");
len'
end
in
let add_buffer len trace =
let trace' = to_proto trace in
Pbrt.Encoder.clear encoder;
Traces.encode_pb_constructive_trace trace' encoder;
let bs = Pbrt.Encoder.to_bytes encoder in
let bs_sz = Bytes.length bs in
let len' =
if len + Buffer.length buf > stop_segment_size then flush_buffer len
else len
in
if len' < 0 then raise (CouldNotTrace "Trace file length overflow");
Buffer.add_int32_be buf (Int32.of_int bs_sz);
Buffer.add_bytes buf bs;
len'
in
let rec aux len = function
| [] -> len
| trace :: rest ->
let len' = add_buffer len trace in
aux len' rest
in
let newlen = aux 0 dtraces in
let newlen = flush_buffer newlen in
if buildlogtrace then
Printf.eprintf "[buildlog] trace store - %d bytes\n" newlen;
Unix.ftruncate tracefd newlen;
Unix.close tracefd
with CouldNotTrace msg ->
prerr_endline ("[warning] " ^ msg);
Unix.close tracefd
let get_apply_aliases ~config =
let open BuildInstance.Syntax in
let* state = get in
Assumptions
.both_tracestore_and_lockfiles_resolve_build_metadata_for_user_module_keys ();
return
(BuildCore.Alacarte_6_4_test.StateSuspending.apply_aliases
~build_number:(BuildConfig.build_number config)
state)