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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
"""Frozen public aggregate-trade research-run producer.

This producer is deliberately narrower than the synthetic vertical slice.  It
loads one explicitly manifested, bounded public trade ingestion; proves trade-ID
and availability-clock continuity before deriving research epochs; evaluates
each instrument independently; and publishes predictive diagnostics only.  It
never invokes the execution simulator or calculates P&L.
"""

from __future__ import annotations

import hashlib
import json
import math
import os
import tempfile
from collections.abc import Mapping, Sequence
from dataclasses import asdict, dataclass
from datetime import UTC, datetime
from pathlib import Path
from typing import Any, cast

import numpy as np
import polars as pl

from microstructure.config import ProjectConfig
from microstructure.provenance import (
    provenance_header,
    read_json,
    sha256_file,
    write_json,
)
from microstructure.public_data import PublicTrades, read_public_trades
from microstructure.reporting import (
    load_run_bundle,
    render_executive_memo,
    render_model_comparison,
    render_technical_report,
    write_checksum_manifest,
)
from microstructure.research.analysis import (
    feature_stability_summary,
    ofi_future_return_association,
)
from microstructure.research.models import (
    block_bootstrap_metric,
    evaluate_model_ladder,
    paired_block_bootstrap_difference,
)
from microstructure.research.splits import WalkForwardPlan, expanding_walk_forward_splits
from microstructure.research.trade_only import (
    build_trade_only_research_frame,
    validate_trade_only_temporal_contract,
)

PUBLIC_PIPELINE_SCHEMA_VERSION = "1.0.0"
PUBLIC_EVIDENCE_TIER = "PUBLIC_SAMPLE_PARTIAL"
EXECUTION_EXCLUSION_REASON = (
    "Not run: aggregate-trade history has no contemporaneous quotes, depth, queue state, "
    "or local receipt clock, so execution, fees-to-alpha conversion, P&L, fills, and capacity "
    "would be unsupported claims."
)
HYPOTHESIS_BASELINE = "historical_prior"
HYPOTHESIS_BLOCK_POLICY = "fixed_contiguous_2x_label_horizon"
HYPOTHESIS_CAVEAT = (
    "Exploratory paired percentile interval on a bounded retrospective sample; it is a "
    "dependence diagnostic, not a p-value, confirmatory significance test, or basis for "
    "rejecting H0."
)
CROSS_INSTRUMENT_CONCLUSION = (
    "No pooled or cross-instrument conclusion is inferred: BTCUSDT and ETHUSDT are separate "
    "sample-specific diagnostics. They cannot support a persistent-alpha claim, regardless "
    "of their point-estimate directions."
)

__all__ = [
    "EXECUTION_EXCLUSION_REASON",
    "PUBLIC_EVIDENCE_TIER",
    "PUBLIC_PIPELINE_SCHEMA_VERSION",
    "PublicPipelineError",
    "produce_public_trade_run",
]


class PublicPipelineError(RuntimeError):
    """Raised when the frozen public run cannot be produced honestly."""


@dataclass(frozen=True, slots=True)
class _SymbolResult:
    symbol: str
    research: pl.DataFrame
    evaluation: pl.DataFrame
    plan: WalkForwardPlan
    predictions: pl.DataFrame
    comparison: pl.DataFrame
    selected_predictions: pl.DataFrame
    selected_model: str
    feature_columns: tuple[str, ...]
    temporal_audit: Mapping[str, Any]
    hypothesis_evaluation: Mapping[str, Any]


def _json_safe(value: Any) -> Any:
    if isinstance(value, np.generic):
        return _json_safe(value.item())
    if isinstance(value, float) and not math.isfinite(value):
        return None
    if isinstance(value, Path):
        return str(value)
    if isinstance(value, Mapping):
        return {str(key): _json_safe(item) for key, item in value.items()}
    if isinstance(value, (list, tuple)):
        return [_json_safe(item) for item in value]
    return value


def _write_json(path: Path, payload: Mapping[str, Any] | list[Any]) -> None:
    clean = _json_safe(payload)
    if not isinstance(clean, (dict, list)):
        raise TypeError("JSON artifact payload must be an object or list")
    write_json(path, clean)


def _atomic_write_text(path: Path, content: str) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    descriptor, temporary_name = tempfile.mkstemp(
        dir=path.parent,
        prefix=f".{path.name}.",
        suffix=".tmp",
        text=True,
    )
    try:
        with os.fdopen(descriptor, "w", encoding="utf-8", newline="\n") as handle:
            handle.write(content.rstrip() + "\n")
        os.replace(temporary_name, path)
    except BaseException:
        Path(temporary_name).unlink(missing_ok=True)
        raise


def _freeze_protocol(config: ProjectConfig, stage: Path) -> tuple[str, str]:
    source = config.project_root / "docs" / "PUBLIC_TRADE_PROTOCOL.md"
    if not source.is_file():
        raise PublicPipelineError(f"frozen public trade protocol is missing: {source}")
    before = sha256_file(source)
    try:
        content = source.read_bytes()
    except OSError as exc:
        raise PublicPipelineError(f"cannot read frozen public trade protocol: {source}") from exc
    after = sha256_file(source)
    if before != after or hashlib.sha256(content).hexdigest() != before:
        raise PublicPipelineError("public trade protocol changed while it was being frozen")

    relative = "protocol/PUBLIC_TRADE_PROTOCOL.md"
    destination = stage / relative
    destination.parent.mkdir(parents=True, exist_ok=True)
    descriptor, temporary_name = tempfile.mkstemp(
        dir=destination.parent,
        prefix=f".{destination.name}.",
        suffix=".tmp",
    )
    try:
        with os.fdopen(descriptor, "wb") as handle:
            handle.write(content)
            handle.flush()
            os.fsync(handle.fileno())
        os.replace(temporary_name, destination)
    except BaseException:
        Path(temporary_name).unlink(missing_ok=True)
        raise
    if sha256_file(destination) != before:
        raise PublicPipelineError("frozen protocol copy does not match its source SHA-256")
    return relative, before


def _utc_from_ns(timestamp_ns: int) -> str:
    seconds, nanoseconds = divmod(timestamp_ns, 1_000_000_000)
    instant = datetime.fromtimestamp(seconds, tz=UTC)
    return f"{instant:%Y-%m-%dT%H:%M:%S}.{nanoseconds:09d}Z"


def _stable_sha256(payload: Mapping[str, Any]) -> str:
    encoded = json.dumps(payload, sort_keys=True, separators=(",", ":"), allow_nan=False)
    return hashlib.sha256(encoded.encode("utf-8")).hexdigest()


def _mapping(value: object) -> Mapping[str, Any] | None:
    if not isinstance(value, Mapping) or not all(isinstance(key, str) for key in value):
        return None
    return cast(Mapping[str, Any], value)


def _project_path(path: Path, project_root: Path) -> str:
    resolved = path.resolve()
    try:
        return resolved.relative_to(project_root.resolve()).as_posix()
    except ValueError:
        return str(resolved)


def _verified_manifest_object(path: Path, expected_sha256: str) -> Mapping[str, Any]:
    before = sha256_file(path)
    if before != expected_sha256:
        raise PublicPipelineError(f"input manifest changed after verified loading: {path}")
    payload = _mapping(read_json(path))
    after = sha256_file(path)
    if after != expected_sha256 or after != before:
        raise PublicPipelineError(f"input manifest changed while capturing lineage: {path}")
    if payload is None:
        raise PublicPipelineError(f"verified input manifest is not a JSON object: {path}")
    return payload


def _input_lineage(public: PublicTrades, config: ProjectConfig) -> dict[str, Any]:
    ingestion = _verified_manifest_object(
        public.ingestion_manifest_path, public.ingestion_manifest_sha256
    )
    dataset = _verified_manifest_object(
        public.dataset_manifest_path, public.dataset_manifest_sha256
    )

    manifest_hashes = set(public.input_manifest_sha256s)
    data_hashes = set(public.raw_artifact_sha256s)
    raw_entries = ingestion.get("raw_artifacts")
    if not isinstance(raw_entries, list):
        raise PublicPipelineError("verified ingestion manifest has no raw artifact array")
    for entry_value in raw_entries:
        entry = _mapping(entry_value)
        if entry is None:
            raise PublicPipelineError("verified raw artifact entry is not an object")
        raw_sha = entry.get("sha256")
        raw_manifest_sha = entry.get("manifest_sha256")
        if isinstance(raw_sha, str):
            data_hashes.add(raw_sha)
            raw_path = public.ingestion_manifest_path.parent.parent / str(entry.get("path"))
            if sha256_file(raw_path) != raw_sha:
                raise PublicPipelineError(f"raw input changed while capturing lineage: {raw_path}")
        if isinstance(raw_manifest_sha, str):
            manifest_hashes.add(raw_manifest_sha)
            raw_manifest_path = public.ingestion_manifest_path.parent.parent / str(
                entry.get("manifest_path")
            )
            if sha256_file(raw_manifest_path) != raw_manifest_sha:
                raise PublicPipelineError(
                    f"raw manifest changed while capturing lineage: {raw_manifest_path}"
                )

    part_entries = dataset.get("artifacts")
    if not isinstance(part_entries, list):
        raise PublicPipelineError("verified dataset manifest has no artifact array")
    for entry_value in part_entries:
        entry = _mapping(entry_value)
        if entry is None:
            raise PublicPipelineError("verified normalized artifact entry is not an object")
        data_sha = entry.get("data_sha256")
        part_manifest_sha = entry.get("manifest_sha256")
        if isinstance(data_sha, str):
            data_hashes.add(data_sha)
            data_path = public.dataset_manifest_path.parent.parent / str(entry.get("data_path"))
            if sha256_file(data_path) != data_sha:
                raise PublicPipelineError(
                    f"normalized part changed while capturing lineage: {data_path}"
                )
        if isinstance(part_manifest_sha, str):
            manifest_hashes.add(part_manifest_sha)
            part_manifest_path = public.dataset_manifest_path.parent.parent / str(
                entry.get("manifest_path")
            )
            if sha256_file(part_manifest_path) != part_manifest_sha:
                raise PublicPipelineError(
                    "normalized part manifest changed while capturing lineage: "
                    f"{part_manifest_path}"
                )

    return {
        "ingestion_manifest": {
            "absolute_path": str(public.ingestion_manifest_path.resolve()),
            "project_relative_or_absolute_path": _project_path(
                public.ingestion_manifest_path, config.project_root
            ),
            "sha256": public.ingestion_manifest_sha256,
        },
        "normalized_dataset_manifest": {
            "absolute_path": str(public.dataset_manifest_path.resolve()),
            "project_relative_or_absolute_path": _project_path(
                public.dataset_manifest_path, config.project_root
            ),
            "sha256": public.dataset_manifest_sha256,
        },
        "normalized_parts": [
            _project_path(path, config.project_root) for path in public.part_paths
        ],
        "raw_artifacts": [
            _project_path(path, config.project_root) for path in public.raw_artifact_paths
        ],
        "raw_manifests": [
            _project_path(path, config.project_root) for path in public.raw_manifest_paths
        ],
        "manifest_sha256": sorted(manifest_hashes),
        "data_sha256": sorted(data_hashes),
    }


def _derive_verified_continuity(
    public: PublicTrades, config: ProjectConfig
) -> tuple[pl.DataFrame, list[dict[str, Any]]]:
    if public.rows > public.row_bound:
        raise PublicPipelineError("materialized public rows exceed the verified configured bound")
    raw = public.polars_trades
    expected_symbols = set(config.data.symbols)
    if set(str(value) for value in raw.get_column("symbol").unique()) != expected_symbols:
        raise PublicPipelineError("materialized public symbols do not match the configuration")
    if raw.get_column("continuity_id").null_count() != raw.height:
        raise PublicPipelineError(
            "archive normalized rows must retain null source continuity before derivation"
        )

    derived_frames: list[pl.DataFrame] = []
    audits: list[dict[str, Any]] = []
    for symbol in config.data.symbols:
        source = raw.filter(pl.col("symbol") == symbol).sort("trade_id")
        if source.is_empty():
            raise PublicPipelineError(f"verified public input has no rows for {symbol}")
        ids = source.get_column("trade_id").to_numpy().astype(np.int64)
        available = source.get_column("available_ts_ns").to_numpy().astype(np.int64)
        event = source.get_column("event_ts_ns").to_numpy().astype(np.int64)
        if np.any(np.diff(ids) != 1):
            raise PublicPipelineError(f"aggregate trade IDs are not contiguous by one for {symbol}")
        if np.any(np.diff(available) < 0):
            raise PublicPipelineError(
                f"availability clock reverses in aggregate-trade-ID order for {symbol}"
            )
        if np.any(available < event):
            raise PublicPipelineError(f"availability precedes event time for {symbol}")
        bases = set(str(value) for value in source.get_column("availability_basis").unique())
        if bases != {"exchange_event_time_proxy"}:
            raise PublicPipelineError(
                f"historical public trades require the exchange-event-time proxy for {symbol}"
            )
        if source.get_column("received_ts_ns").null_count() != source.height:
            raise PublicPipelineError(
                f"historical public trades cannot claim local receipt time for {symbol}"
            )

        continuity_id = (
            f"public-aggtrade:{public.ingestion_manifest_sha256[:16]}:{symbol}:"
            f"{int(ids[0])}-{int(ids[-1])}"
        )
        derived_frames.append(source.with_columns(pl.lit(continuity_id).alias("continuity_id")))
        audits.append(
            {
                "symbol": symbol,
                "rows": source.height,
                "first_trade_id": int(ids[0]),
                "last_trade_id": int(ids[-1]),
                "trade_id_step": 1,
                "trade_ids_contiguous": True,
                "availability_clock_nondecreasing": True,
                "tied_availability_rows": int(np.count_nonzero(np.diff(available) == 0)),
                "availability_basis": "exchange_event_time_proxy",
                "local_receipt_time_available": False,
                "derived_continuity_id": continuity_id,
                "derivation_timing": "assigned only after ID and clock verification",
                "source_rows_mutated": False,
            }
        )
    return pl.concat(derived_frames).sort(["symbol", "trade_id"]), audits


def _trade_feature_columns(config: ProjectConfig, frame: pl.DataFrame) -> tuple[str, ...]:
    columns: list[str] = ["log_trade_return_1"]
    for window in config.features.trade_windows:
        columns.extend(
            [
                f"signed_trade_volume_w{window}",
                f"trade_volume_w{window}",
                f"trade_imbalance_w{window}",
            ]
        )
    intensity = config.features.intensity_window
    columns.extend([f"trade_count_w{intensity}", f"trade_intensity_w{intensity}"])
    columns.append(f"realized_volatility_w{config.features.volatility_window}")
    selected = tuple(dict.fromkeys(columns))
    missing = sorted(set(selected).difference(frame.columns))
    if missing:
        raise PublicPipelineError(f"declared trade-only model features are missing: {missing}")
    return selected


def _serialize_plan(plan: WalkForwardPlan) -> dict[str, Any]:
    return {
        "contract": (
            "single-symbol decision-time buckets; expanding training; feature-ready and "
            "uncensored evaluation; labels ending at or beyond evaluation are purged; "
            "configured embargo applied"
        ),
        "index_basis": "zero-based row positions in this symbol's evaluation_frame.parquet",
        "decision_time_count": plan.decision_time_count,
        "folds": [
            {
                "fold_id": fold.fold_id,
                "train_indices": [int(value) for value in fold.train_indices.tolist()],
                "validation_indices": [int(value) for value in fold.validation_indices.tolist()],
                "train_start_ts_ns": fold.train_start_ts_ns,
                "train_end_ts_ns": fold.train_end_ts_ns,
                "validation_start_ts_ns": fold.validation_start_ts_ns,
                "validation_end_ts_ns": fold.validation_end_ts_ns,
                "purged_rows": fold.purged_rows,
                "embargoed_time_buckets": fold.embargoed_time_buckets,
            }
            for fold in plan.folds
        ],
        "final_train_indices": [int(value) for value in plan.final_train_indices.tolist()],
        "test_indices": [int(value) for value in plan.test_indices.tolist()],
        "test_start_ts_ns": plan.test_start_ts_ns,
        "test_end_ts_ns": plan.test_end_ts_ns,
        "test_used_for_selection": False,
    }


def _add_fixed_blocks(predictions: pl.DataFrame, *, block_width: int) -> pl.DataFrame:
    group = ["split", "fold_id"]
    return (
        predictions.with_columns(
            pl.col("decision_sequence").min().over(group).alias("_block_origin_trade_id")
        )
        .with_columns(
            ((pl.col("decision_sequence") - pl.col("_block_origin_trade_id")) // block_width)
            .cast(pl.Int64)
            .alias("_block_index")
        )
        .with_columns(
            pl.concat_str(
                [
                    "symbol",
                    "split",
                    pl.col("fold_id").cast(pl.String),
                    pl.col("_block_index").cast(pl.String),
                ],
                separator=":",
            ).alias("bootstrap_block"),
            (pl.col("_block_origin_trade_id") + pl.col("_block_index") * block_width).alias(
                "bootstrap_block_start_trade_id"
            ),
        )
        .with_columns(
            (pl.col("bootstrap_block_start_trade_id") + block_width - 1).alias(
                "bootstrap_block_end_trade_id"
            ),
            pl.lit(block_width, dtype=pl.Int64).alias("bootstrap_block_width_trades"),
            pl.lit("fixed_contiguous_2x_label_horizon").alias("bootstrap_block_policy"),
        )
        .drop("_block_origin_trade_id", "_block_index")
    )


def _bootstrap_comparison(
    comparison: pl.DataFrame,
    predictions: pl.DataFrame,
    *,
    symbol: str,
    selected_model: str,
    metric: str,
    n_bootstrap: int,
    seed: int,
    horizon: int,
) -> pl.DataFrame:
    rows: list[dict[str, Any]] = []
    block_width = 2 * horizon
    for index, source_row in enumerate(
        comparison.sort(["split", "fold_id", "requested_model"]).to_dicts()
    ):
        row = dict(source_row)
        requested_model = str(row.get("requested_model", row["model"]))
        row["selected_on_validation"] = requested_model == selected_model
        row["selected_on"] = "validation" if requested_model == selected_model else None
        row["selection_contract"] = "validation folds only; final test opened after selection"
        row["test_used_for_selection"] = False
        row["evidence_tier"] = PUBLIC_EVIDENCE_TIER
        row["execution_evaluated"] = False
        row[f"{metric}_ci_low"] = None
        row[f"{metric}_ci_high"] = None
        row["bootstrap_status"] = None
        row["bootstrap_blocks"] = None
        row["bootstrap_samples"] = None
        row["bootstrap_seed"] = None
        row["bootstrap_block_width_trades"] = None
        row["bootstrap_block_policy"] = None
        row["bootstrap_limitation"] = None
        if row["split"] == "test":
            evaluated = predictions.filter(
                (pl.col("split") == "test")
                & (pl.col("model") == str(row["model"]))
                & (pl.col("requested_model") == requested_model)
            )
            interval_seed = seed + index
            interval = block_bootstrap_metric(
                evaluated,
                metric=metric,
                block_column="bootstrap_block",
                n_bootstrap=n_bootstrap,
                seed=interval_seed,
            )
            row[f"{metric}_ci_low"] = interval.lower
            row[f"{metric}_ci_high"] = interval.upper
            row["bootstrap_status"] = interval.status
            row["bootstrap_blocks"] = interval.n_blocks
            row["bootstrap_samples"] = interval.n_bootstrap
            row["bootstrap_seed"] = interval_seed
            row["bootstrap_block_width_trades"] = block_width
            row["bootstrap_block_policy"] = "fixed_contiguous_2x_label_horizon"
            row["bootstrap_limitation"] = (
                "percentile dependence diagnostic on fixed trade blocks; not a p-value or "
                "confirmatory coverage guarantee"
            )
        rows.append(row)
    return pl.DataFrame(rows, infer_schema_length=None).with_columns(
        pl.lit(symbol).alias("symbol"),
        pl.lit(symbol).alias("instrument"),
        pl.lit(symbol).alias("instrument_scope"),
    )


def _metric_delta_direction(metric: str) -> tuple[str, int]:
    if metric in {"log_loss", "brier_score", "expected_calibration_error"}:
        return "negative_selected_minus_prior_is_favorable", -1
    if metric in {"accuracy", "balanced_accuracy", "roc_auc", "pr_auc"}:
        return "positive_selected_minus_prior_is_favorable", 1
    raise PublicPipelineError(f"unsupported paired hypothesis metric: {metric}")


def _paired_hypothesis_evaluation(
    predictions: pl.DataFrame,
    *,
    symbol: str,
    selected_model: str,
    metric: str,
    n_bootstrap: int,
    seed: int,
    horizon: int,
) -> dict[str, Any]:
    selected = predictions.filter(
        (pl.col("split") == "test") & (pl.col("requested_model") == selected_model)
    )
    baseline = predictions.filter(
        (pl.col("split") == "test") & (pl.col("requested_model") == HYPOTHESIS_BASELINE)
    )
    if selected.is_empty():
        raise PublicPipelineError(
            f"validation-selected test predictions are missing for paired test: {symbol}"
        )
    if baseline.is_empty():
        raise PublicPipelineError(f"historical-prior test predictions are missing for {symbol}")

    identity_columns = [
        "row_id",
        "y_true",
        "bootstrap_block",
        "bootstrap_block_start_trade_id",
        "bootstrap_block_end_trade_id",
        "bootstrap_block_width_trades",
        "bootstrap_block_policy",
    ]
    for name, frame in (("selected", selected), ("historical_prior", baseline)):
        missing = sorted(set(identity_columns).difference(frame.columns))
        if missing:
            raise PublicPipelineError(
                f"{name} predictions lack paired-bootstrap identity columns: {missing}"
            )
        if frame.get_column("row_id").n_unique() != frame.height:
            raise PublicPipelineError(f"{name} predictions contain duplicate row IDs for {symbol}")

    selected_identity = selected.select(identity_columns).sort("row_id")
    baseline_identity = baseline.select(identity_columns).sort("row_id")
    if not selected_identity.equals(baseline_identity):
        raise PublicPipelineError(
            f"selected and historical-prior predictions do not share identical rows/blocks for {symbol}"
        )

    block_width = 2 * horizon
    policies = selected.get_column("bootstrap_block_policy").unique().to_list()
    widths = selected.get_column("bootstrap_block_width_trades").unique().to_list()
    if policies != [HYPOTHESIS_BLOCK_POLICY] or widths != [block_width]:
        raise PublicPipelineError(
            f"paired hypothesis blocks do not implement the frozen 2x-horizon policy for {symbol}"
        )

    interval = paired_block_bootstrap_difference(
        selected,
        baseline,
        metric=metric,
        block_column="bootstrap_block",
        n_bootstrap=n_bootstrap,
        seed=seed,
    )
    favorable_direction, favorable_sign = _metric_delta_direction(metric)
    signed_point = favorable_sign * interval.point_estimate
    if not math.isfinite(signed_point):
        point_assessment = "unavailable"
        point_favorable: bool | None = None
    elif signed_point > 0:
        point_assessment = "favorable_point_only"
        point_favorable = True
    elif signed_point < 0:
        point_assessment = "unfavorable_point"
        point_favorable = False
    else:
        point_assessment = "point_tie"
        point_favorable = False

    if interval.lower is None or interval.upper is None:
        interval_relation = "unavailable"
    elif interval.lower <= 0.0 <= interval.upper:
        interval_relation = "includes_zero"
    elif favorable_sign * interval.lower > 0.0 and favorable_sign * interval.upper > 0.0:
        interval_relation = "entirely_favorable"
    else:
        interval_relation = "entirely_unfavorable"

    return {
        "symbol": symbol,
        "selected_model": selected_model,
        "baseline": HYPOTHESIS_BASELINE,
        "metric": metric,
        "delta_definition": "selected_model_minus_historical_prior",
        "point_delta": interval.point_estimate,
        "ci_level": 0.95,
        "ci_low": interval.lower,
        "ci_high": interval.upper,
        "n_obs": selected.height,
        "n_blocks": interval.n_blocks,
        "samples": interval.n_bootstrap,
        "seed": interval.seed,
        "status": interval.status,
        "block_column": "bootstrap_block",
        "block_policy": HYPOTHESIS_BLOCK_POLICY,
        "block_width_trades": block_width,
        "paired_row_ids_identical": True,
        "paired_blocks_identical": True,
        "favorable_direction": favorable_direction,
        "point_favorable": point_favorable,
        "point_assessment": point_assessment,
        "interval_relation_to_zero": interval_relation,
        "exploratory": True,
        "significance_claim_authorized": False,
        "h0_rejection_authorized": False,
        "caveat": HYPOTHESIS_CAVEAT,
        "cross_instrument_conclusion": CROSS_INSTRUMENT_CONCLUSION,
    }


def _attach_paired_hypothesis_to_comparison(
    comparison: pl.DataFrame,
    hypothesis: Mapping[str, Any],
) -> pl.DataFrame:
    selected_model = str(hypothesis["selected_model"])
    rows: list[dict[str, Any]] = []
    attached = 0
    for source in comparison.to_dicts():
        row = dict(source)
        selected_test = row["split"] == "test" and row.get("requested_model") == selected_model
        paired_fields = {
            "paired_baseline": None,
            "paired_metric": None,
            "paired_metric_delta": None,
            "paired_metric_delta_ci_low": None,
            "paired_metric_delta_ci_high": None,
            "paired_n_obs": None,
            "paired_bootstrap_blocks": None,
            "paired_bootstrap_samples": None,
            "paired_bootstrap_seed": None,
            "paired_bootstrap_status": None,
            "paired_bootstrap_block_policy": None,
            "paired_favorable_direction": None,
            "paired_point_favorable": None,
            "paired_exploratory": None,
            "paired_significance_claim_authorized": None,
        }
        if selected_test:
            attached += 1
            paired_fields.update(
                {
                    "paired_baseline": hypothesis["baseline"],
                    "paired_metric": hypothesis["metric"],
                    "paired_metric_delta": hypothesis["point_delta"],
                    "paired_metric_delta_ci_low": hypothesis["ci_low"],
                    "paired_metric_delta_ci_high": hypothesis["ci_high"],
                    "paired_n_obs": hypothesis["n_obs"],
                    "paired_bootstrap_blocks": hypothesis["n_blocks"],
                    "paired_bootstrap_samples": hypothesis["samples"],
                    "paired_bootstrap_seed": hypothesis["seed"],
                    "paired_bootstrap_status": hypothesis["status"],
                    "paired_bootstrap_block_policy": hypothesis["block_policy"],
                    "paired_favorable_direction": hypothesis["favorable_direction"],
                    "paired_point_favorable": hypothesis["point_favorable"],
                    "paired_exploratory": hypothesis["exploratory"],
                    "paired_significance_claim_authorized": hypothesis[
                        "significance_claim_authorized"
                    ],
                }
            )
        row.update(paired_fields)
        rows.append(row)
    if attached != 1:
        raise PublicPipelineError(
            "paired hypothesis metadata must attach to exactly one selected test comparison row"
        )
    return pl.DataFrame(rows, infer_schema_length=None)


def _rows_by_plan(
    evaluation: pl.DataFrame, indices: np.ndarray[Any, np.dtype[np.int64]]
) -> pl.DataFrame:
    indexed = evaluation.with_row_index("_research_row_id")
    return indexed.filter(pl.col("_research_row_id").is_in(indices)).drop("_research_row_id")


def _evaluate_symbol(
    trades: pl.DataFrame,
    *,
    config: ProjectConfig,
    symbol_index: int,
) -> _SymbolResult:
    symbol = str(trades.get_column("symbol")[0])
    research = build_trade_only_research_frame(trades, config.features).with_columns(
        pl.col("label_horizon_trades").alias("label_horizon_events")
    )
    temporal = validate_trade_only_temporal_contract(research)
    evaluation = research.filter(pl.col("feature_ready"))
    if evaluation.is_empty():
        raise PublicPipelineError(f"trade-only feature construction produced no rows for {symbol}")
    features = _trade_feature_columns(config, evaluation)
    plan = expanding_walk_forward_splits(evaluation, config.evaluation)
    ladder = evaluate_model_ladder(
        evaluation,
        plan,
        config.models,
        seed=config.run.seed + symbol_index * 100_000,
        calibration_bins=config.evaluation.calibration_bins,
        target="future_trade_up",
        features=features,
    )
    block_width = 2 * config.features.label_horizon_events
    predictions = _add_fixed_blocks(ladder.predictions, block_width=block_width)
    comparison = _bootstrap_comparison(
        ladder.comparison,
        predictions,
        symbol=symbol,
        selected_model=ladder.selected_model,
        metric=ladder.selection_metric,
        n_bootstrap=config.evaluation.bootstrap_samples,
        seed=config.run.seed + symbol_index * 100_000 + 10_000,
        horizon=config.features.label_horizon_events,
    )
    selected = predictions.filter(
        (pl.col("split") == "test") & (pl.col("requested_model") == ladder.selected_model)
    )
    if selected.is_empty() or selected.get_column("requested_model").n_unique() != 1:
        raise PublicPipelineError(f"validation-selected test predictions are missing for {symbol}")
    hypothesis = _paired_hypothesis_evaluation(
        predictions,
        symbol=symbol,
        selected_model=ladder.selected_model,
        metric=ladder.selection_metric,
        n_bootstrap=config.evaluation.bootstrap_samples,
        seed=config.run.seed + symbol_index * 100_000 + 20_000,
        horizon=config.features.label_horizon_events,
    )
    comparison = _attach_paired_hypothesis_to_comparison(comparison, hypothesis)
    return _SymbolResult(
        symbol=symbol,
        research=research,
        evaluation=evaluation,
        plan=plan,
        predictions=predictions,
        comparison=comparison,
        selected_predictions=selected,
        selected_model=ladder.selected_model,
        feature_columns=features,
        temporal_audit=asdict(temporal),
        hypothesis_evaluation=hypothesis,
    )


def _trade_summary(
    trades: pl.DataFrame, continuity_audits: Sequence[Mapping[str, Any]]
) -> pl.DataFrame:
    continuity = {
        str(row["symbol"]): str(row["derived_continuity_id"]) for row in continuity_audits
    }
    rows: list[dict[str, Any]] = []
    for frame in trades.partition_by("symbol", maintain_order=True):
        symbol = str(frame.get_column("symbol")[0])
        buy = frame.filter(pl.col("aggressor_side") == "buy")
        sell = frame.filter(pl.col("aggressor_side") == "sell")
        rows.append(
            {
                "symbol": symbol,
                "rows": frame.height,
                "first_trade_id": int(cast(int, frame.get_column("trade_id").min())),
                "last_trade_id": int(cast(int, frame.get_column("trade_id").max())),
                "observed_start_utc": _utc_from_ns(
                    int(cast(int, frame.get_column("event_ts_ns").min()))
                ),
                "observed_end_utc": _utc_from_ns(
                    int(cast(int, frame.get_column("event_ts_ns").max()))
                ),
                "total_quantity": float(cast(float, frame.get_column("quantity").sum())),
                "total_quote_quantity": float(
                    cast(float, frame.get_column("quote_quantity").sum())
                ),
                "buy_rows": buy.height,
                "sell_rows": sell.height,
                "buy_quantity": float(cast(float, buy.get_column("quantity").sum())),
                "sell_quantity": float(cast(float, sell.get_column("quantity").sum())),
                "derived_continuity_id": continuity[symbol],
                "availability_basis": "exchange_event_time_proxy",
                "analysis_kind": "public_trade_sample_summary_descriptive",
                "descriptive_only": True,
            }
        )
    return pl.DataFrame(rows).sort("symbol")


def _write_analyses(
    *,
    trades: pl.DataFrame,
    symbol_results: Sequence[_SymbolResult],
    continuity_audits: Sequence[Mapping[str, Any]],
    config: ProjectConfig,
    stage: Path,
    generated_at_utc: str,
) -> dict[str, str]:
    summary = _trade_summary(trades, continuity_audits)
    stability_frames: list[pl.DataFrame] = []
    flow_frames: list[pl.DataFrame] = []
    flow_feature = f"trade_imbalance_w{max(config.features.trade_windows)}"
    for result in symbol_results:
        train = _rows_by_plan(result.evaluation, result.plan.final_train_indices)
        test = _rows_by_plan(result.evaluation, result.plan.test_indices)
        stability_frames.append(
            feature_stability_summary(
                train,
                test,
                feature_columns=result.feature_columns,
                group_columns=("symbol",),
            ).with_columns(
                pl.lit("final_training_period").alias("reference_period"),
                pl.lit("untouched_final_test").alias("comparison_period"),
                pl.lit(PUBLIC_EVIDENCE_TIER).alias("evidence_tier"),
            )
        )
        labeled = result.evaluation.filter(~pl.col("right_censored"))
        flow_frames.append(
            ofi_future_return_association(
                labeled,
                horizon_return_columns={
                    config.features.label_horizon_events: "future_trade_return"
                },
                ofi_column=flow_feature,
                min_observations=3,
            ).with_columns(
                pl.lit("all_feature_ready_labeled_rows").alias("analysis_scope"),
                pl.lit(PUBLIC_EVIDENCE_TIER).alias("evidence_tier"),
            )
        )
    stability = pl.concat(stability_frames)
    flow_return = pl.concat(flow_frames)
    artifacts = {
        "trade_summary": "analysis/trade_summary.parquet",
        "feature_stability": "analysis/feature_stability.parquet",
        "flow_return_analysis": "analysis/flow_return_analysis.parquet",
    }
    for name, frame in (
        ("trade_summary", summary),
        ("feature_stability", stability),
        ("flow_return_analysis", flow_return),
    ):
        path = stage / artifacts[name]
        path.parent.mkdir(parents=True, exist_ok=True)
        frame.write_parquet(path)
    _write_json(
        stage / "analysis" / "manifest.json",
        {
            "generated_at_utc": generated_at_utc,
            "evidence_tier": PUBLIC_EVIDENCE_TIER,
            "descriptive_only": True,
            "economic_claim_authorized": False,
            "execution_claim_authorized": False,
            "artifacts": {
                "trade_summary": {"path": artifacts["trade_summary"], "rows": summary.height},
                "feature_stability": {
                    "path": artifacts["feature_stability"],
                    "rows": stability.height,
                    "reference": "final training period",
                    "comparison": "untouched final test",
                },
                "flow_return_analysis": {
                    "path": artifacts["flow_return_analysis"],
                    "rows": flow_return.height,
                    "scope": "all feature-ready labeled rows",
                },
            },
            "limitations": [
                "Retrospective bounded sample; no confirmatory significance claim.",
                "Exchange event time is an availability proxy, not local receipt evidence.",
                "Trade-only observations cannot support book or execution analysis.",
            ],
        },
    )
    return {**artifacts, "analysis_manifest": "analysis/manifest.json"}


def _quality_payload(
    public: PublicTrades,
    continuity_audits: Sequence[Mapping[str, Any]],
    *,
    generated_at_utc: str,
) -> dict[str, Any]:
    return {
        "generated_at_utc": generated_at_utc,
        "dataset": "manifested_public_aggregate_trades",
        "rows_checked": public.validation.rows_checked,
        "summary": {
            "errors": public.validation.error_count,
            "warnings": public.validation.warning_count,
        },
        "normalized_schema_validation": {
            "dataset": public.validation.dataset,
            "rows_checked": public.validation.rows_checked,
            "errors": public.validation.error_count,
            "warnings": public.validation.warning_count,
            "findings": [asdict(finding) for finding in public.validation.findings],
        },
        "aggregate_trade_continuity": list(continuity_audits),
        "row_bound": public.row_bound,
        "row_bound_respected": public.rows <= public.row_bound,
        "canonical_source_order": list(public.canonical_order),
        "mutation_policy": (
            "verified normalized observations were not repaired or overwritten; continuity "
            "exists only in persisted derived research frames"
        ),
    }


def _hypothesis_number(value: object) -> str:
    if value is None:
        return "N/A"
    if isinstance(value, (int, float)) and not isinstance(value, bool):
        number = float(value)
        return f"{number:.6f}" if math.isfinite(number) else "N/A"
    return str(value)


def _render_hypothesis_report_section(stage: Path) -> str:
    artifact = stage / "metrics" / "hypothesis_evaluation.json"
    payload = _mapping(read_json(artifact))
    if payload is None:
        raise PublicPipelineError("serialized hypothesis evaluation must be a JSON object")
    rows = payload.get("per_symbol")
    if not isinstance(rows, list) or not rows:
        raise PublicPipelineError("serialized hypothesis evaluation has no per-symbol rows")

    lines = [
        "## Paired H0/H1 diagnostic",
        "",
        (
            "The frozen comparison is validation-selected test predictions versus the "
            "historical-prior baseline on identical `row_id` and fixed 2x-horizon blocks. "
            "For Δ log-loss (selected minus historical prior), a negative value favors the "
            "selected model."
        ),
        "",
        (
            "| Symbol | Selected model | Baseline | Metric | Point Δ | Paired 95% interval | "
            "Observations | Blocks | Samples | Seed | Status |"
        ),
        "| --- | --- | --- | --- | ---: | --- | ---: | ---: | ---: | ---: | --- |",
    ]
    for value in rows:
        row = _mapping(value)
        if row is None:
            raise PublicPipelineError("serialized per-symbol hypothesis row is not an object")
        interval = (
            f"[{_hypothesis_number(row.get('ci_low'))}, {_hypothesis_number(row.get('ci_high'))}]"
        )
        cells = (
            row.get("symbol"),
            row.get("selected_model"),
            row.get("baseline"),
            row.get("metric"),
            _hypothesis_number(row.get("point_delta")),
            interval,
            row.get("n_obs"),
            row.get("n_blocks"),
            row.get("samples"),
            row.get("seed"),
            row.get("status"),
        )
        lines.append(
            "| "
            + " | ".join(str(cell).replace("|", "\\|").replace("\n", " ") for cell in cells)
            + " |"
        )
    lines.extend(
        [
            "",
            (
                "These paired percentile intervals are dependence diagnostics, not p-values "
                "or confirmatory significance intervals; H0 is not rejected by this "
                "exploratory design."
            ),
            "",
            (
                "No cross-instrument estimate was pooled. Mixed directions, if present, "
                "cannot support persistent alpha; even matching directions would remain "
                "bounded, sample-specific diagnostics."
            ),
        ]
    )
    return "\n".join(lines)


def _report_set(stage: Path) -> None:
    bundle = load_run_bundle(stage, require_complete=False, verify_integrity=False)
    hypothesis_section = _render_hypothesis_report_section(stage)
    _atomic_write_text(
        stage / "reports" / "technical_report.md",
        render_technical_report(bundle),
    )
    _atomic_write_text(
        stage / "reports" / "executive_memo.md",
        render_executive_memo(bundle),
    )
    _atomic_write_text(
        stage / "reports" / "model_comparison.md",
        render_model_comparison(bundle)
        + "\nExecution status: `NOT_RUN`. Reason: "
        + EXECUTION_EXCLUSION_REASON
        + "\n\n"
        + hypothesis_section
        + "\n",
    )


def produce_public_trade_run(
    config: ProjectConfig,
    stage: Path,
    *,
    ingestion_manifest_path: str | Path,
    ingestion_manifest_sha256: str,
) -> None:
    """Populate an empty atomic stage with one verified public trade-only run.

    The caller owns staging-directory creation, cleanup on failure, and final
    rename.  This function writes ``_SUCCESS`` only after every artifact, report,
    and checksum is durable in the supplied stage.
    """

    destination = stage.resolve()
    if destination.exists() and any(destination.iterdir()):
        raise PublicPipelineError("public run stage must be empty")
    destination.mkdir(parents=True, exist_ok=True)

    if config.data.mode != "binance_rest":
        raise PublicPipelineError("public trade producer requires data.mode='binance_rest'")
    explicit_manifest_path = Path(ingestion_manifest_path).resolve()
    public = read_public_trades(
        config,
        explicit_manifest_path,
        ingestion_manifest_sha256=ingestion_manifest_sha256,
    )
    if public.evidence_tier == "FULL_DATA" and not public.all_requested_ranges_complete:
        raise PublicPipelineError("reader evidence tier contradicts manifested completeness")
    derived_trades, continuity_audits = _derive_verified_continuity(public, config)
    if public.validation.has_errors:
        raise PublicPipelineError("verified public normalized input has quality errors")

    generated = provenance_header(
        project_root=config.project_root,
        config_hash=config.hash,
        evidence_tier=PUBLIC_EVIDENCE_TIER,
        input_manifests=[],
    )
    generated_at = cast(str, generated["generated_at_utc"])
    lineage = _input_lineage(public, config)
    manifest_hashes = cast(list[str], lineage["manifest_sha256"])
    data_hashes = cast(list[str], lineage["data_sha256"])
    protocol_path, protocol_sha256 = _freeze_protocol(config, destination)

    symbol_results: list[_SymbolResult] = []
    for symbol_index, symbol in enumerate(config.data.symbols):
        symbol_results.append(
            _evaluate_symbol(
                derived_trades.filter(pl.col("symbol") == symbol),
                config=config,
                symbol_index=symbol_index,
            )
        )

    research_root = destination / "research"
    model_root = destination / "models"
    combined_research: list[pl.DataFrame] = []
    combined_evaluation: list[pl.DataFrame] = []
    combined_predictions: list[pl.DataFrame] = []
    combined_comparison: list[pl.DataFrame] = []
    combined_selected: list[pl.DataFrame] = []
    symbol_manifest: dict[str, Any] = {}
    for result in symbol_results:
        slug = result.symbol.lower()
        symbol_research = research_root / slug
        symbol_models = model_root / slug
        symbol_research.mkdir(parents=True, exist_ok=True)
        symbol_models.mkdir(parents=True, exist_ok=True)
        result.research.write_parquet(symbol_research / "research_frame.parquet")
        result.evaluation.write_parquet(symbol_research / "evaluation_frame.parquet")
        _write_json(symbol_research / "folds.json", _serialize_plan(result.plan))
        result.predictions.write_parquet(symbol_models / "predictions.parquet")
        result.comparison.write_parquet(symbol_models / "comparison.parquet")
        result.selected_predictions.write_parquet(
            symbol_models / "selected_test_predictions.parquet"
        )
        combined_research.append(result.research)
        combined_evaluation.append(result.evaluation)
        combined_predictions.append(result.predictions)
        combined_comparison.append(result.comparison)
        combined_selected.append(result.selected_predictions)
        symbol_manifest[result.symbol] = {
            "research_frame": f"research/{slug}/research_frame.parquet",
            "evaluation_frame": f"research/{slug}/evaluation_frame.parquet",
            "folds": f"research/{slug}/folds.json",
            "predictions": f"models/{slug}/predictions.parquet",
            "comparison": f"models/{slug}/comparison.parquet",
            "selected_test_predictions": f"models/{slug}/selected_test_predictions.parquet",
            "selected_model": result.selected_model,
            "selection_metric": config.models.selection_metric,
            "selection_source": "validation folds only",
            "test_used_for_selection": False,
            "feature_columns": list(result.feature_columns),
            "temporal_audit": dict(result.temporal_audit),
            "paired_hypothesis_evaluation": dict(result.hypothesis_evaluation),
            "test_start_utc": _utc_from_ns(result.plan.test_start_ts_ns),
            "test_end_utc": _utc_from_ns(result.plan.test_end_ts_ns),
        }

    research = pl.concat(combined_research)
    evaluation = pl.concat(combined_evaluation)
    predictions = pl.concat(combined_predictions)
    comparison = pl.concat(combined_comparison)
    selected_predictions = pl.concat(combined_selected)
    research.write_parquet(research_root / "research_frame.parquet")
    evaluation.write_parquet(research_root / "evaluation_frame.parquet")
    predictions.write_parquet(model_root / "predictions.parquet")
    comparison.write_parquet(model_root / "comparison.parquet")
    selected_predictions.write_parquet(model_root / "selected_test_predictions.parquet")

    metrics_root = destination / "metrics"
    _write_json(metrics_root / "predictive_metrics.json", comparison.to_dicts())
    hypothesis_rows = [dict(result.hypothesis_evaluation) for result in symbol_results]
    hypothesis_payload = {
        "schema_version": PUBLIC_PIPELINE_SCHEMA_VERSION,
        "generated_at_utc": generated_at,
        "evidence_tier": PUBLIC_EVIDENCE_TIER,
        "hypotheses": {
            "H0": (
                "The validation-selected model does not improve held-out selection-metric "
                "performance over the historical-prior classifier."
            ),
            "H1_exploratory": (
                "Causal aggregate-trade features improve held-out selection-metric "
                "performance relative to the historical prior."
            ),
        },
        "comparison_contract": (
            "per-symbol validation-selected final-test predictions minus historical-prior "
            "predictions on identical row_id and fixed dependency block"
        ),
        "selection_metric": config.models.selection_metric,
        "delta_definition": "selected_model_minus_historical_prior",
        "bootstrap": {
            "method": "paired fixed-block percentile bootstrap",
            "ci_level": 0.95,
            "samples": config.evaluation.bootstrap_samples,
            "seed_policy": "run_seed + symbol_index*100000 + 20000",
            "block_policy": HYPOTHESIS_BLOCK_POLICY,
            "block_width_trades": 2 * config.features.label_horizon_events,
        },
        "per_symbol": hypothesis_rows,
        "cross_instrument_conclusion": {
            "status": "not_inferred",
            "pooling_performed": False,
            "persistent_alpha_claim_authorized": False,
            "text": CROSS_INSTRUMENT_CONCLUSION,
        },
        "exploratory": True,
        "significance_claim_authorized": False,
        "caveat": HYPOTHESIS_CAVEAT,
    }
    _write_json(metrics_root / "hypothesis_evaluation.json", hypothesis_payload)
    _write_json(metrics_root / "execution_metrics.json", [])
    _write_json(metrics_root / "execution_sensitivity.json", [])
    _write_json(
        metrics_root / "execution_exclusion.json",
        {
            "status": "NOT_RUN",
            "reason": EXECUTION_EXCLUSION_REASON,
            "execution_metrics_rows": 0,
            "execution_sensitivity_rows": 0,
            "pnl_calculated": False,
            "profitability_claim_authorized": False,
        },
    )

    analysis_artifacts = _write_analyses(
        trades=derived_trades,
        symbol_results=symbol_results,
        continuity_audits=continuity_audits,
        config=config,
        stage=destination,
        generated_at_utc=generated_at,
    )
    quality = _quality_payload(public, continuity_audits, generated_at_utc=generated_at)
    _write_json(destination / "quality" / "summary.json", quality)

    market_state = evaluation.select(
        "symbol",
        "decision_ts_ns",
        "decision_trade_id",
        "price",
        "quantity",
        "aggressor_side",
        f"trade_imbalance_w{max(config.features.trade_windows)}",
        f"realized_volatility_w{config.features.volatility_window}",
        pl.lit(PUBLIC_EVIDENCE_TIER).alias("evidence_tier"),
        pl.lit("trade_only_no_book_state").alias("market_state_scope"),
    ).sort(["decision_ts_ns", "symbol", "decision_trade_id"])
    dashboard_path = destination / "dashboard" / "market_state.parquet"
    dashboard_path.parent.mkdir(parents=True, exist_ok=True)
    market_state.write_parquet(dashboard_path)

    data_snapshot = {
        "schema_version": PUBLIC_PIPELINE_SCHEMA_VERSION,
        "mode": "binance_rest_trade_only",
        "source": config.data.source,
        "evidence_tier": PUBLIC_EVIDENCE_TIER,
        "reader_effective_evidence_tier": public.evidence_tier,
        "configured_requested_evidence_tier": config.run.evidence_tier,
        "producer_evidence_policy": (
            "always PUBLIC_SAMPLE_PARTIAL for this retrospective exploratory protocol"
        ),
        "requested_period_utc": {
            "start": config.data.start.isoformat().replace("+00:00", "Z"),
            "end": config.data.end.isoformat().replace("+00:00", "Z") if config.data.end else None,
        },
        "observed_period_utc": {
            "start": public.observed.start_utc,
            "end_inclusive": public.observed.end_inclusive_utc,
        },
        "rows": public.rows,
        "row_bound": public.row_bound,
        "all_requested_ranges_complete": public.all_requested_ranges_complete,
        "canonical_order": list(public.canonical_order),
        "manifest_authority": {
            "policy": "explicit path and caller-supplied SHA-256; no directory discovery",
            "absolute_path": str(public.ingestion_manifest_path.resolve()),
            "project_relative_or_absolute_path": _project_path(
                public.ingestion_manifest_path, config.project_root
            ),
            "sha256": public.ingestion_manifest_sha256,
        },
        "lineage": lineage,
        "symbols": [
            {
                "symbol": item.symbol,
                "rows": item.rows,
                "complete_range": item.complete_range,
                "tick_size": str(item.tick_size),
                "lot_size": str(item.lot_size),
                "observed_start_utc": item.observed.start_utc,
                "observed_end_inclusive_utc": item.observed.end_inclusive_utc,
            }
            for item in public.symbols
        ],
        "transformation": (
            "source normalized rows unchanged; one derived continuity epoch per symbol was "
            "assigned only after contiguous aggregate-ID and nonreversing-clock checks"
        ),
    }
    _write_json(destination / "data" / "manifest_snapshot.json", data_snapshot)

    resolved_config = config.public_dict()
    resolved_config["effective_evidence_tier"] = PUBLIC_EVIDENCE_TIER
    resolved_config["reader_effective_evidence_tier"] = public.evidence_tier
    resolved_config["evidence_policy"] = (
        "retrospective public trade protocol cannot be promoted above PUBLIC_SAMPLE_PARTIAL"
    )
    resolved_config["protocol"] = {
        "path": protocol_path,
        "sha256": protocol_sha256,
    }
    _write_json(destination / "resolved_config.json", resolved_config)

    git_metadata = cast(Mapping[str, Any], generated["git"])
    run_key_inputs = {
        "config_sha256": config.hash,
        "input_manifest_sha256": manifest_hashes,
        "input_data_sha256": data_hashes,
        "protocol_sha256": protocol_sha256,
        "git": {
            "commit": str(git_metadata.get("commit", "UNKNOWN")),
            "dirty": bool(git_metadata.get("dirty", False)),
            "source_tree_sha256": str(git_metadata.get("source_tree_sha256", "UNKNOWN")),
        },
        "seed": config.run.seed,
        "protocol": "public_aggregate_trade_exploratory_v1",
    }
    run_key = _stable_sha256(run_key_inputs)
    generated.update(
        {
            "evidence_tier": PUBLIC_EVIDENCE_TIER,
            "requested_evidence_tier": config.run.evidence_tier,
            "effective_evidence_tier": PUBLIC_EVIDENCE_TIER,
            "reader_effective_evidence_tier": public.evidence_tier,
            "input_manifest_sha256": manifest_hashes,
            "input_data_sha256": data_hashes,
            "ingestion_manifest_path": _project_path(
                public.ingestion_manifest_path, config.project_root
            ),
            "ingestion_manifest_absolute_path": str(public.ingestion_manifest_path.resolve()),
            "ingestion_manifest_sha256": public.ingestion_manifest_sha256,
            "ingestion_manifest_authority": (
                "explicit path and caller-supplied SHA-256; no directory discovery"
            ),
            "protocol_path": protocol_path,
            "protocol_sha256": protocol_sha256,
            "run_key": run_key,
            "run_key_inputs": run_key_inputs,
            "pipeline_schema_version": PUBLIC_PIPELINE_SCHEMA_VERSION,
            "seed": config.run.seed,
            "observed_start_utc": public.observed.start_utc,
            "observed_end_utc": public.observed.end_inclusive_utc,
            "data_availability_clock": "exchange_event_time_proxy",
            "local_receipt_time_available": False,
            "execution_simulated": False,
        }
    )
    _write_json(destination / "provenance.json", generated)

    artifacts = {
        "resolved_config": "resolved_config.json",
        "protocol": protocol_path,
        "data_manifest_snapshot": "data/manifest_snapshot.json",
        "quality_summary": "quality/summary.json",
        "research_frame": "research/research_frame.parquet",
        "evaluation_frame": "research/evaluation_frame.parquet",
        "predictions": "models/predictions.parquet",
        "selected_test_predictions": "models/selected_test_predictions.parquet",
        "model_comparison_data": "models/comparison.parquet",
        "predictive_metrics": "metrics/predictive_metrics.json",
        "hypothesis_evaluation": "metrics/hypothesis_evaluation.json",
        "execution_metrics": "metrics/execution_metrics.json",
        "execution_sensitivity": "metrics/execution_sensitivity.json",
        "execution_exclusion": "metrics/execution_exclusion.json",
        "market_state": "dashboard/market_state.parquet",
        "technical_report": "reports/technical_report.md",
        "executive_memo": "reports/executive_memo.md",
        "model_comparison": "reports/model_comparison.md",
        **analysis_artifacts,
    }
    run_manifest = {
        "schema_version": PUBLIC_PIPELINE_SCHEMA_VERSION,
        "run_id": config.run.name,
        "run_key": run_key,
        "status": "complete",
        "evidence_tier": PUBLIC_EVIDENCE_TIER,
        "data": {
            "mode": "binance_rest_trade_only",
            "source": config.data.source,
            "symbols": list(config.data.symbols),
            "rows": public.rows,
            "row_bound": public.row_bound,
            "all_requested_ranges_complete": public.all_requested_ranges_complete,
            "reader_effective_evidence_tier": public.evidence_tier,
            "configured_requested_evidence_tier": config.run.evidence_tier,
            "observed_start_utc": public.observed.start_utc,
            "observed_end_utc": public.observed.end_inclusive_utc,
            "observed_start_ts_ns": public.observed.start_ns,
            "observed_end_ts_ns": public.observed.end_inclusive_ns,
            "availability_basis": "exchange_event_time_proxy",
            "local_receipt_time_available": False,
            "symbol_coverage": [
                {
                    "symbol": item.symbol,
                    "rows": item.rows,
                    "complete_range": item.complete_range,
                    "observed_start_utc": item.observed.start_utc,
                    "observed_end_inclusive_utc": item.observed.end_inclusive_utc,
                }
                for item in public.symbols
            ],
        },
        "artifacts": artifacts,
        "research": {
            "question": (
                "whether recent observable aggregate-trade direction and size contain "
                "out-of-time information about future trade-price direction"
            ),
            "target": "future_trade_up",
            "label_horizon_trades": config.features.label_horizon_events,
            "evaluation_contract": "separate per-symbol expanding purged walk-forward",
            "selection_contract": "validation folds only; final test never used for selection",
            "bootstrap_contract": {
                "samples": config.evaluation.bootstrap_samples,
                "seeded": True,
                "block_width_trades": 2 * config.features.label_horizon_events,
                "block_policy": "fixed_contiguous_2x_label_horizon",
                "status": "dependence diagnostic, not confirmatory significance",
            },
            "hypothesis_evaluation": {
                "artifact": "metrics/hypothesis_evaluation.json",
                "hypotheses": ["H0", "H1_exploratory"],
                "baseline": HYPOTHESIS_BASELINE,
                "metric": config.models.selection_metric,
                "delta_definition": "selected_model_minus_historical_prior",
                "paired_on": ["row_id", "bootstrap_block"],
                "per_symbol_only": True,
                "cross_instrument_pooling": False,
                "persistent_alpha_claim_authorized": False,
                "exploratory": True,
                "significance_claim_authorized": False,
                "caveat": HYPOTHESIS_CAVEAT,
                "cross_instrument_conclusion": CROSS_INSTRUMENT_CONCLUSION,
            },
            "symbols": symbol_manifest,
            "descriptive_analysis": {
                "manifest": analysis_artifacts["analysis_manifest"],
                "descriptive_only": True,
                "economic_claim_authorized": False,
            },
        },
        "execution_assumptions": {
            "status": "NOT_RUN",
            "reason": EXECUTION_EXCLUSION_REASON,
            "pnl_calculated": False,
            "fills_calculated": False,
            "capacity_calculated": False,
            "profitability_claim_authorized": False,
        },
        "warnings": [
            "PUBLIC_SAMPLE_PARTIAL: results are bounded, retrospective, and sample-specific.",
            "Exchange event time is an availability proxy and not local receipt evidence.",
            EXECUTION_EXCLUSION_REASON,
            "Bootstrap intervals are dependence diagnostics and not significance claims.",
            CROSS_INSTRUMENT_CONCLUSION,
        ],
    }
    _write_json(destination / "run_manifest.json", run_manifest)
    _report_set(destination)
    if _input_lineage(public, config) != lineage:
        raise PublicPipelineError("external input lineage changed while producing the run")
    write_checksum_manifest(destination)
    descriptor = os.open(destination / "_SUCCESS", os.O_CREAT | os.O_EXCL | os.O_WRONLY)
    with os.fdopen(descriptor, "w", encoding="utf-8") as success:
        success.write("complete\n")
    load_run_bundle(destination)