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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
from __future__ import annotations

import inspect
import json
import math
import shutil
from collections.abc import Iterator
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
from types import MappingProxyType
from typing import Any, cast

import polars as pl
import pytest

import microstructure.m8_l2_development as development
import microstructure.research.l2_multidate as l2_multidate_module
from microstructure.m8_l2_analysis_config import load_m8_l2_analysis_config
from microstructure.m8_l2_capture import M8L2SessionBundle
from microstructure.m8_l2_config import load_m8_l2_config
from microstructure.m8_l2_development import (
    M8L2DevelopmentError,
    ProducerSourceIdentity,
    lock_m8_l2_development,
    verify_m8_l2_development_lock,
)
from microstructure.m8_l2_inputs import (
    L2CampaignRuntimeIdentity,
    L2SessionFileAuthority,
)
from microstructure.research.l2_multidate import L2ObservedInterval

SECOND = 1_000_000_000
MILLISECOND = 1_000_000
COMMIT = "a" * 40
SOURCE_TREE = "b" * 64
CAMPAIGN_SHA = "c" * 64
RUNTIME_SHA = "d" * 64


@dataclass(frozen=True)
class _Loaded:
    book_observations: pl.DataFrame
    depth_deltas: pl.DataFrame
    intervals: tuple[L2ObservedInterval, ...]


@dataclass
class _FakeInput:
    root: Path
    session_id: str
    session_date: str
    role: str
    config_sha256: str
    config_source_sha256: str
    file_authority: L2SessionFileAuthority
    campaign_identity: L2CampaignRuntimeIdentity
    symbols: MappingProxyType[str, object]
    frames: dict[str, _Loaded]
    events: list[str]
    access_phase: str = "development"
    development_lock_sha256: str | None = None

    def load_symbol_frames(self, symbol: str) -> _Loaded:
        self.events.append(f"load:{self.session_date}:{symbol}")
        return self.frames[symbol]


@dataclass
class _Environment:
    capture: Any
    analysis: Any
    train_path: Path
    validation_path: Path
    train_input: _FakeInput
    validation_input: _FakeInput
    events: list[str]
    result: Any | None = None


def _source() -> ProducerSourceIdentity:
    return ProducerSourceIdentity(COMMIT, SOURCE_TREE, False)


def _frames(symbol: str, study_date: str) -> _Loaded:
    start = int(datetime.fromisoformat(f"{study_date}T14:00:00+00:00").timestamp()) * SECOND
    count = 280
    times = [start + index * 100 * MILLISECOND for index in range(count)]
    sequence = list(range(1, count + 1))
    mids = [
        100.0 + 0.025 * math.sin(index / 5.0) + 0.008 * math.sin(index / 17.0) for index in sequence
    ]
    bid_quantity = [3.0 + (index % 13) * 0.05 for index in sequence]
    ask_quantity = [2.5 + (index % 11) * 0.05 for index in sequence]
    continuity = f"capture:{symbol}:{study_date}"
    books = pl.DataFrame(
        {
            "venue": ["binance_spot"] * count,
            "symbol": [symbol] * count,
            "event_ts_ns": times,
            "available_ts_ns": times,
            "continuity_id": [continuity] * count,
            "sequence_end": sequence,
            "is_valid": [True] * count,
            "best_bid": [value - 0.01 for value in mids],
            "best_ask": [value + 0.01 for value in mids],
            "bid_quantity": bid_quantity,
            "ask_quantity": ask_quantity,
            "depth_bid_5": [value + 8.0 for value in bid_quantity],
            "depth_ask_5": [value + 7.0 for value in ask_quantity],
            "depth_bid_10": [value + 18.0 for value in bid_quantity],
            "depth_ask_10": [value + 17.0 for value in ask_quantity],
            "tick_size": [0.01] * count,
            "lot_size": [0.00001] * count,
        }
    )
    deltas = pl.DataFrame(
        {
            "venue": ["binance_spot"] * count,
            "symbol": [symbol] * count,
            "event_ts_ns": times,
            "available_ts_ns": times,
            "continuity_id": [continuity] * count,
            "first_update_id": sequence,
            "last_update_id": sequence,
            "bids": [
                [
                    {
                        "price_ticks": 10_000 + index,
                        "quantity_lots": 0 if index % 13 == 0 else 10,
                    }
                ]
                for index in sequence
            ],
            "asks": [[{"price_ticks": 10_002 + index, "quantity_lots": 10}] for index in sequence],
        }
    )
    return _Loaded(
        books,
        deltas,
        (L2ObservedInterval(continuity, start, start + 30 * SECOND),),
    )


def _bundle(root: Path, *, date: str, role: str, manifest_sha: str) -> M8L2SessionBundle:
    return M8L2SessionBundle(
        root=root,
        status="COMPLETE",
        session_id=hashlib_sha(date),
        session_date=date,
        role=role,
        manifest_path=root / "session_manifest.json",
        manifest_sha256=manifest_sha,
        checksum_path=root / "CHECKSUMS.sha256",
        marker_path=root / "_SUCCESS",
        reason_codes=(),
    )


def _terminal_bundle(
    root: Path,
    *,
    date: str,
    role: str,
    status: str,
    reasons: tuple[str, ...],
) -> M8L2SessionBundle:
    campaign = {
        "campaign_authority_sha256": CAMPAIGN_SHA,
        "runtime_commit": COMMIT,
        "runtime_source_tree_sha256": SOURCE_TREE,
        "runtime_fingerprint_sha256": RUNTIME_SHA,
        "runtime_dirty": False,
    }
    manifest = root / "session_manifest.json"
    manifest.write_bytes(_canonical({"authority": campaign}))
    checksum = root / "CHECKSUMS.sha256"
    checksum.write_text("session authority\n", encoding="ascii")
    return M8L2SessionBundle(
        root=root,
        status=cast(Any, status),
        session_id=hashlib_sha(date),
        session_date=date,
        role=role,
        manifest_path=manifest,
        manifest_sha256=hashlib_sha_bytes(manifest.read_bytes()),
        checksum_path=checksum,
        marker_path=root / ("_SUCCESS" if status == "COMPLETE" else "INSUFFICIENT_DATA"),
        reason_codes=reasons,
    )


def hashlib_sha(value: str) -> str:
    import hashlib

    return hashlib.sha256(value.encode()).hexdigest()


def _environment(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> _Environment:
    capture = load_m8_l2_config("configs/m8_l2_capture_study.toml")
    analysis = load_m8_l2_analysis_config("configs/m8_l2_analysis.toml")
    sessions = tmp_path / "sessions"
    train_path = sessions / f"2026-08-10-train-{hashlib_sha('train')[:20]}"
    validation_path = sessions / f"2026-08-11-validation-{hashlib_sha('validation')[:20]}"
    train_path.mkdir(parents=True)
    validation_path.mkdir()
    train_manifest = "1" * 64
    validation_manifest = "2" * 64
    campaign = L2CampaignRuntimeIdentity(CAMPAIGN_SHA, COMMIT, SOURCE_TREE, RUNTIME_SHA, False)
    events: list[str] = []
    symbols = MappingProxyType({symbol: object() for symbol in capture.study.symbols})
    train_input = _FakeInput(
        root=train_path.absolute(),
        session_id=hashlib_sha("2026-08-10"),
        session_date="2026-08-10",
        role="train",
        config_sha256=capture.hash,
        config_source_sha256=capture.source_sha256,
        file_authority=L2SessionFileAuthority(train_manifest, "3" * 64),
        campaign_identity=campaign,
        symbols=symbols,
        frames={symbol: _frames(symbol, "2026-08-10") for symbol in capture.study.symbols},
        events=events,
    )
    validation_input = _FakeInput(
        root=validation_path.absolute(),
        session_id=hashlib_sha("2026-08-11"),
        session_date="2026-08-11",
        role="validation",
        config_sha256=capture.hash,
        config_source_sha256=capture.source_sha256,
        file_authority=L2SessionFileAuthority(validation_manifest, "4" * 64),
        campaign_identity=campaign,
        symbols=symbols,
        frames={symbol: _frames(symbol, "2026-08-11") for symbol in capture.study.symbols},
        events=events,
    )
    bundles = {
        train_path.absolute(): _bundle(
            train_path.absolute(), date="2026-08-10", role="train", manifest_sha=train_manifest
        ),
        validation_path.absolute(): _bundle(
            validation_path.absolute(),
            date="2026-08-11",
            role="validation",
            manifest_sha=validation_manifest,
        ),
    }

    def fake_capture_verify(path: str | Path, *, expected_config: Any) -> M8L2SessionBundle:
        assert expected_config == capture
        return bundles[Path(path).absolute()]

    def fake_input_verify(
        path: str | Path,
        *,
        expected_config: Any,
        expected_date: str,
        expected_role: str,
        expected_file_authority: Any = None,
        expected_campaign: Any = None,
    ) -> _FakeInput:
        assert expected_config == capture
        events.append(f"authority:{expected_date}")
        value = train_input if expected_date == "2026-08-10" else validation_input
        assert Path(path).absolute() == value.root
        assert expected_role == value.role
        if expected_file_authority is not None:
            assert expected_file_authority == value.file_authority
        if expected_campaign is not None:
            assert expected_campaign == campaign
        return value

    monkeypatch.setattr(development, "verify_m8_l2_session_bundle", fake_capture_verify)
    monkeypatch.setattr(development, "_load_input_verifier", lambda: fake_input_verify)
    monkeypatch.setattr(development, "_producer_source_identity", lambda _root: _source())
    monkeypatch.setattr(
        development,
        "current_m8_l2_runtime_fingerprint_sha256",
        lambda: RUNTIME_SHA,
    )
    monkeypatch.setattr(development, "_utc_now", lambda: datetime(2026, 8, 11, 16, 0, tzinfo=UTC))
    return _Environment(
        capture,
        analysis,
        train_path,
        validation_path,
        train_input,
        validation_input,
        events,
    )


def test_foreign_import_origin_fails_before_bundle_or_economic_load(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path, monkeypatch)
    foreign = tmp_path / "foreign" / "research" / "l2_multidate.py"
    foreign.parent.mkdir(parents=True)
    foreign.write_text("# foreign checkout\n", encoding="utf-8")
    bundle_calls = 0

    def forbidden_bundle(*args: object, **kwargs: object) -> M8L2SessionBundle:
        nonlocal bundle_calls
        bundle_calls += 1
        raise AssertionError((args, kwargs))

    monkeypatch.setattr(l2_multidate_module, "__file__", str(foreign))
    monkeypatch.setattr(development, "verify_m8_l2_session_bundle", forbidden_bundle)
    target = tmp_path / "development-lock"
    with pytest.raises(M8L2DevelopmentError, match="foreign or mixed import origin"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )

    assert bundle_calls == 0
    assert environment.events == []
    assert not target.exists()


def test_import_origin_drift_before_marker_is_not_terminal(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path, monkeypatch)
    foreign = tmp_path / "foreign" / "research" / "l2_multidate.py"
    foreign.parent.mkdir(parents=True)
    foreign.write_text("# drifted checkout\n", encoding="utf-8")
    original_publish_inventory = development._publish_inventory

    def publish_then_drift(root: Path) -> None:
        original_publish_inventory(root)
        monkeypatch.setattr(l2_multidate_module, "__file__", str(foreign))

    monkeypatch.setattr(development, "_publish_inventory", publish_then_drift)
    target = tmp_path / "development-lock"
    with pytest.raises(M8L2DevelopmentError, match="foreign or mixed import origin"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )

    assert environment.events
    assert not target.exists()


def test_development_memory_budget_boundary_and_selection_estimate() -> None:
    frame = pl.DataFrame(
        {
            "feature_a": [1.0, 2.0, 3.0],
            "feature_b": [4.0, 5.0, 6.0],
            "future_mid_up": [0, 1, 0],
        }
    )
    observed = development._selection_workspace_upper_bytes(frame, frame, feature_count=2)
    assert observed > 2 * (frame.estimated_size("b") * 2)
    development._require_memory_budget(observed, observed, "selection boundary")
    with pytest.raises(M8L2DevelopmentError, match="memory budget"):
        development._require_memory_budget(observed + 1, observed, "selection overflow")
    assert development._causal_build_upper_bytes(60_000, 4) == (
        60_000 * 4 * development._CAUSAL_ENDPOINT_ROW_UPPER_BYTES
    )


def test_development_raw_overflow_is_system_failure_without_terminal(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path, monkeypatch)
    monkeypatch.setattr(development, "_MAX_DEVELOPMENT_RAW_BYTES", 1)
    target = tmp_path / "development-lock"
    with pytest.raises(M8L2DevelopmentError, match=r"raw materialization.*memory budget"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )
    assert not target.exists()
    assert not (target / "_LOCKED").exists()


def test_insufficient_development_publishes_verified_not_created_without_economic_load(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path, monkeypatch)
    train = _terminal_bundle(
        environment.train_path,
        date="2026-08-10",
        role="train",
        status="INSUFFICIENT_DATA",
        reasons=("GATE_COVERAGE",),
    )
    validation = _terminal_bundle(
        environment.validation_path,
        date="2026-08-11",
        role="validation",
        status="COMPLETE",
        reasons=(),
    )
    bundles = {train.root: train, validation.root: validation}
    monkeypatch.setattr(
        development,
        "verify_m8_l2_session_bundle",
        lambda path, *, expected_config: bundles[Path(path).absolute()],
    )

    def forbidden_loader() -> object:
        raise AssertionError("NOT_CREATED publication opened an economic input loader")

    monkeypatch.setattr(development, "_load_input_verifier", forbidden_loader)
    target = tmp_path / "development-not-created"
    result = lock_m8_l2_development(
        environment.capture,
        environment.analysis,
        train.root,
        validation.root,
        target,
    )

    assert result.status == "NOT_CREATED"
    assert result.children == ()
    assert result.reason_codes == ("DEVELOPMENT_SESSION_INSUFFICIENT::train::GATE_COVERAGE",)
    assert result.marker_path.read_bytes() == b"not-created\n"
    assert not list(result.root.rglob("*.parquet"))
    payload = json.loads(result.aggregate_path.read_text(encoding="ascii"))
    assert payload["status"] == "NOT_CREATED"
    assert payload["children"] == []
    assert payload["heldout_access"]["economic_rows_opened"] is False

    restored = verify_m8_l2_development_lock(
        environment.capture,
        environment.analysis,
        train.root,
        validation.root,
        result.root,
        expected_lock_sha256=result.aggregate_sha256,
    )
    assert restored == result
    assert environment.events == []


def test_not_created_rejects_caller_authority_swap_before_producer_entry(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path, monkeypatch)
    train = _terminal_bundle(
        environment.train_path,
        date="2026-08-10",
        role="train",
        status="INSUFFICIENT_DATA",
        reasons=("GATE_COVERAGE",),
    )
    validation = _terminal_bundle(
        environment.validation_path,
        date="2026-08-11",
        role="validation",
        status="COMPLETE",
        reasons=(),
    )
    authorities = {
        train.session_date: L2SessionFileAuthority(
            train.manifest_sha256,
            hashlib_sha_bytes(train.checksum_path.read_bytes()),
        ),
        validation.session_date: L2SessionFileAuthority(
            validation.manifest_sha256,
            hashlib_sha_bytes(validation.checksum_path.read_bytes()),
        ),
    }
    train.checksum_path.write_bytes(b"self-consistent replacement authority\n")
    bundles = {train.root: train, validation.root: validation}
    monkeypatch.setattr(
        development,
        "verify_m8_l2_session_bundle",
        lambda path, *, expected_config: bundles[Path(path).absolute()],
    )
    monkeypatch.setattr(
        development,
        "_load_input_verifier",
        lambda: pytest.fail("NOT_CREATED swap check opened an economic loader"),
    )
    target = tmp_path / "development-not-created"

    with pytest.raises(M8L2DevelopmentError, match="caller-held train session"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            train.root,
            validation.root,
            target,
            expected_session_file_authorities=authorities,
        )

    assert not target.exists()
    assert environment.events == []


def test_not_created_rechecks_caller_authority_at_marker_boundary(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path, monkeypatch)
    train = _terminal_bundle(
        environment.train_path,
        date="2026-08-10",
        role="train",
        status="INSUFFICIENT_DATA",
        reasons=("GATE_COVERAGE",),
    )
    validation = _terminal_bundle(
        environment.validation_path,
        date="2026-08-11",
        role="validation",
        status="COMPLETE",
        reasons=(),
    )
    bundles = {train.root: train, validation.root: validation}
    monkeypatch.setattr(
        development,
        "verify_m8_l2_session_bundle",
        lambda path, *, expected_config: bundles[Path(path).absolute()],
    )
    authorities = {
        bundle.session_date: L2SessionFileAuthority(
            bundle.manifest_sha256,
            hashlib_sha_bytes(bundle.checksum_path.read_bytes()),
        )
        for bundle in bundles.values()
    }
    original_publish_inventory = development._publish_inventory

    def publish_then_tamper(root: Path, **kwargs: object) -> None:
        original_publish_inventory(root, **kwargs)
        train.checksum_path.write_bytes(b"tampered before terminal marker\n")

    monkeypatch.setattr(development, "_publish_inventory", publish_then_tamper)
    monkeypatch.setattr(
        development,
        "_load_input_verifier",
        lambda: pytest.fail("NOT_CREATED marker check opened an economic loader"),
    )
    target = tmp_path / "development-not-created"

    with pytest.raises(M8L2DevelopmentError, match="caller-held train session"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            train.root,
            validation.root,
            target,
            expected_session_file_authorities=authorities,
        )

    assert not target.exists()
    assert environment.events == []


@pytest.fixture(scope="module")
def locked_environment(tmp_path_factory: pytest.TempPathFactory) -> Iterator[_Environment]:
    monkeypatch = pytest.MonkeyPatch()
    environment = _environment(tmp_path_factory.mktemp("l2-development"), monkeypatch)
    original_select = cast(Any, development.__dict__["select_multidate_model"])
    fit_observations: list[bool] = []
    destination = environment.train_path.parent.parent / "development-lock"

    def observed_select(*args: Any, **kwargs: Any) -> Any:
        fit_observations.append((destination / "_LOCKED").exists())
        return original_select(*args, **kwargs)

    monkeypatch.setattr(development, "select_multidate_model", observed_select)
    environment.result = lock_m8_l2_development(
        environment.capture,
        environment.analysis,
        environment.train_path,
        environment.validation_path,
        destination,
    )
    assert fit_observations == [False] * 8
    try:
        yield environment
    finally:
        monkeypatch.undo()


def _canonical(payload: dict[str, Any]) -> bytes:
    return (
        json.dumps(payload, sort_keys=True, separators=(",", ":"), ensure_ascii=True) + "\n"
    ).encode("ascii")


def _rebuild_publication(root: Path) -> None:
    import hashlib

    aggregate_raw = (root / "development_lock.json").read_bytes()
    aggregate_sha = hashlib.sha256(aggregate_raw).hexdigest()
    (root / "development_lock.sha256").write_text(
        f"{aggregate_sha}  development_lock.json\n", encoding="ascii"
    )
    payload_files = sorted(
        path
        for path in root.rglob("*")
        if path.is_file() and path.name not in {"_LOCKED", "CHECKSUMS.sha256", "inventory.json"}
    )
    entries = [
        {
            "path": path.relative_to(root).as_posix(),
            "sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
            "bytes": path.stat().st_size,
        }
        for path in payload_files
    ]
    inventory = {
        "schema_version": "m8-l2-development-inventory-v1",
        "artifact_kind": "m8_l2_development_lock_inventory",
        "files": entries,
    }
    (root / "inventory.json").write_bytes(_canonical(inventory))
    checksum_files = sorted(
        path
        for path in root.rglob("*")
        if path.is_file() and path.name not in {"_LOCKED", "CHECKSUMS.sha256"}
    )
    (root / "CHECKSUMS.sha256").write_text(
        "".join(
            f"{hashlib.sha256(path.read_bytes()).hexdigest()}  "
            f"{path.relative_to(root).as_posix()}\n"
            for path in checksum_files
        ),
        encoding="ascii",
    )


def test_development_lock_is_complete_outcome_blind_and_roundtrips(
    locked_environment: _Environment,
) -> None:
    result = locked_environment.result
    assert result is not None
    assert result.marker_path.read_bytes() == b"locked\n"
    assert len(result.children) == 8
    assert locked_environment.events[:2] == ["authority:2026-08-10", "authority:2026-08-11"]
    assert all(event.startswith("authority:") for event in locked_environment.events[:2])
    aggregate = json.loads(result.aggregate_path.read_text(encoding="ascii"))
    assert aggregate["heldout_access"] == {
        "paths_received": False,
        "file_hashes_received": False,
        "row_counts_received": False,
        "economic_rows_opened": False,
        "model_fit_or_update_after_lock": False,
    }
    assert aggregate["heldout_declarations"] == [
        {"date": "2026-08-12", "role": "primary_test"},
        {"date": "2026-08-13", "role": "replication_test"},
    ]
    restored = verify_m8_l2_development_lock(
        locked_environment.capture,
        locked_environment.analysis,
        locked_environment.train_path,
        locked_environment.validation_path,
        result.root,
        expected_lock_sha256=result.aggregate_sha256,
    )
    assert restored.aggregate_sha256 == result.aggregate_sha256
    assert [(item.symbol, item.endpoint) for item in restored.children] == [
        (symbol, endpoint.name)
        for symbol in locked_environment.capture.study.symbols
        for endpoint in locked_environment.analysis.endpoints
    ]


def test_execution_reference_is_train_only_and_matches_frozen_formula(
    locked_environment: _Environment,
) -> None:
    result = locked_environment.result
    assert result is not None
    aggregate = json.loads(result.aggregate_path.read_text(encoding="ascii"))
    for claim in aggregate["execution_references"]:
        payload = json.loads((result.root / claim["path"]).read_text(encoding="ascii"))
        assert payload["fit_date"] == "2026-08-10"
        assert payload["reference_price_statistic"] == "train_median_mid_price"
        assert payload["reference_depth_statistic"] == "train_q05_min_bid_ask_l1_depth"
        unrounded = min(
            100.0 / payload["reference_mid_price"],
            0.10 * payload["reference_l1_depth_q05"],
        )
        assert payload["unrounded_reference_quantity"] == pytest.approx(unrounded)
        assert payload["reference_quantity_lots"] == math.floor(
            unrounded / payload["lot_size"] + 1e-12
        )
        assert payload["reference_quantity"] == pytest.approx(
            payload["reference_quantity_lots"] * payload["lot_size"]
        )


def test_public_api_has_no_heldout_or_test_path_and_refuses_overwrite(
    locked_environment: _Environment,
) -> None:
    parameters = inspect.signature(lock_m8_l2_development).parameters
    assert "test_bundle_path" not in parameters
    assert "primary_bundle_path" not in parameters
    assert "replication_bundle_path" not in parameters
    result = locked_environment.result
    assert result is not None
    loads_before = sum(event.startswith("load:") for event in locked_environment.events)
    with pytest.raises(M8L2DevelopmentError, match="overwrite is forbidden"):
        lock_m8_l2_development(
            locked_environment.capture,
            locked_environment.analysis,
            locked_environment.train_path,
            locked_environment.validation_path,
            result.root,
        )
    assert sum(event.startswith("load:") for event in locked_environment.events) == loads_before


def test_rechecksummed_semantic_tamper_is_rejected(
    locked_environment: _Environment, tmp_path: Path
) -> None:
    result = locked_environment.result
    assert result is not None
    target = tmp_path / "tampered"
    shutil.copytree(result.root, target)
    aggregate_path = target / "development_lock.json"
    aggregate = json.loads(aggregate_path.read_text(encoding="ascii"))
    claim = aggregate["children"][0]
    child_path = target / claim["path"]
    child = json.loads(child_path.read_text(encoding="ascii"))
    child["selected_model"] = "tree_depth_999"
    child_path.write_bytes(_canonical(child))
    claim["sha256"] = hashlib_sha_bytes(child_path.read_bytes())
    aggregate_path.write_bytes(_canonical(aggregate))
    _rebuild_publication(target)
    with pytest.raises(M8L2DevelopmentError, match=r"contract changed|authorities disagree"):
        verify_m8_l2_development_lock(
            locked_environment.capture,
            locked_environment.analysis,
            locked_environment.train_path,
            locked_environment.validation_path,
            target,
        )


def hashlib_sha_bytes(raw: bytes) -> str:
    import hashlib

    return hashlib.sha256(raw).hexdigest()


def test_missing_terminal_marker_is_rejected(
    locked_environment: _Environment, tmp_path: Path
) -> None:
    result = locked_environment.result
    assert result is not None
    target = tmp_path / "no-marker"
    shutil.copytree(result.root, target)
    (target / "_LOCKED").unlink()
    with pytest.raises(M8L2DevelopmentError, match="terminal marker"):
        verify_m8_l2_development_lock(
            locked_environment.capture,
            locked_environment.analysis,
            locked_environment.train_path,
            locked_environment.validation_path,
            target,
        )


def test_late_lock_and_dirty_source_fail_before_economic_rows_open(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path / "late", monkeypatch)
    monkeypatch.setattr(development, "_utc_now", lambda: datetime(2026, 8, 12, 14, 0, tzinfo=UTC))
    target = tmp_path / "late-lock"
    with pytest.raises(M8L2DevelopmentError, match="deadline"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )
    assert not any(event.startswith("load:") for event in environment.events)
    assert not target.exists()

    environment.events.clear()
    monkeypatch.setattr(
        development,
        "_producer_source_identity",
        lambda _root: ProducerSourceIdentity(COMMIT, SOURCE_TREE, True),
    )
    monkeypatch.setattr(development, "_utc_now", lambda: datetime(2026, 8, 11, 16, 0, tzinfo=UTC))
    with pytest.raises(M8L2DevelopmentError, match="clean Git"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            tmp_path / "dirty-lock",
        )
    assert environment.events == []


def test_lock_crossing_deadline_during_fit_is_not_published(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path / "crossing-deadline", monkeypatch)
    observed_times = iter(
        (
            datetime(2026, 8, 11, 16, 0, tzinfo=UTC),
            datetime(2026, 8, 12, 14, 0, tzinfo=UTC),
        )
    )
    monkeypatch.setattr(development, "_utc_now", lambda: next(observed_times))
    target = tmp_path / "crossing-deadline-lock"

    with pytest.raises(M8L2DevelopmentError, match="deadline"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )

    assert any(event.startswith("load:") for event in environment.events)
    assert not target.exists()


def test_lock_crossing_deadline_during_publication_is_not_terminal(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path / "crossing-publication", monkeypatch)
    observed_times = iter(
        (
            datetime(2026, 8, 11, 16, 0, tzinfo=UTC),
            datetime(2026, 8, 12, 13, 59, 59, tzinfo=UTC),
            datetime(2026, 8, 12, 14, 0, tzinfo=UTC),
        )
    )
    monkeypatch.setattr(development, "_utc_now", lambda: next(observed_times))
    target = tmp_path / "crossing-publication-lock"

    with pytest.raises(M8L2DevelopmentError, match="deadline"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )

    assert any(event.startswith("load:") for event in environment.events)
    assert not target.exists()


def test_lock_crossing_deadline_during_final_authority_recheck_is_not_terminal(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path / "crossing-final-authority", monkeypatch)
    observed_times = iter(
        (
            datetime(2026, 8, 11, 16, 0, tzinfo=UTC),
            datetime(2026, 8, 12, 13, 59, 58, tzinfo=UTC),
            datetime(2026, 8, 12, 13, 59, 59, tzinfo=UTC),
            datetime(2026, 8, 12, 14, 0, tzinfo=UTC),
        )
    )
    monkeypatch.setattr(development, "_utc_now", lambda: next(observed_times))
    target = tmp_path / "crossing-final-authority-lock"

    with pytest.raises(M8L2DevelopmentError, match="deadline"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )

    assert any(event.startswith("load:") for event in environment.events)
    assert not target.exists()


def test_source_drift_during_fitting_is_not_published(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path / "source-drift", monkeypatch)
    identities = iter(
        (
            _source(),
            ProducerSourceIdentity("3" * 40, "4" * 64, False),
        )
    )
    monkeypatch.setattr(
        development,
        "_producer_source_identity",
        lambda _root: next(identities),
    )
    target = tmp_path / "source-drift-lock"

    with pytest.raises(M8L2DevelopmentError, match="changed before lock publication"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )

    assert any(event.startswith("load:") for event in environment.events)
    assert not target.exists()


def test_runtime_drift_fails_before_rows_and_before_terminal_publication(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    early = _environment(tmp_path / "runtime-early", monkeypatch)
    monkeypatch.setattr(
        development,
        "current_m8_l2_runtime_fingerprint_sha256",
        lambda: "e" * 64,
    )
    with pytest.raises(M8L2DevelopmentError, match="runtime differs"):
        lock_m8_l2_development(
            early.capture,
            early.analysis,
            early.train_path,
            early.validation_path,
            tmp_path / "runtime-early-lock",
        )
    assert not any(event.startswith("load:") for event in early.events)

    late = _environment(tmp_path / "runtime-late", monkeypatch)
    observed = iter((RUNTIME_SHA, "f" * 64))
    monkeypatch.setattr(
        development,
        "current_m8_l2_runtime_fingerprint_sha256",
        lambda: next(observed),
    )
    target = tmp_path / "runtime-late-lock"
    with pytest.raises(M8L2DevelopmentError, match="runtime changed"):
        lock_m8_l2_development(
            late.capture,
            late.analysis,
            late.train_path,
            late.validation_path,
            target,
        )
    assert any(event.startswith("load:") for event in late.events)
    assert not target.exists()


def test_system_fault_removes_unlocked_reservation(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path / "fault", monkeypatch)
    original_write_json = development._write_json

    def fail_regime(path: Path, payload: Any) -> str:
        if path.name == "regime_thresholds.json":
            raise OSError("injected durable-write failure")
        return original_write_json(path, payload)

    monkeypatch.setattr(development, "_write_json", fail_regime)
    target = tmp_path / "fault-lock"
    with pytest.raises(OSError, match="injected"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            target,
        )
    assert not target.exists()
    assert all(not event.startswith("load:2026-08-12") for event in environment.events)


def test_wrong_development_coordinates_and_campaign_fail_closed(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    environment = _environment(tmp_path / "wrong", monkeypatch)
    with pytest.raises(M8L2DevelopmentError, match="COMPLETE 2026-08-10 train"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.validation_path,
            environment.train_path,
            tmp_path / "reversed",
        )
    assert environment.events == []

    environment.validation_input.campaign_identity = L2CampaignRuntimeIdentity(
        "e" * 64, COMMIT, SOURCE_TREE, RUNTIME_SHA, False
    )
    with pytest.raises(M8L2DevelopmentError, match="campaign"):
        lock_m8_l2_development(
            environment.capture,
            environment.analysis,
            environment.train_path,
            environment.validation_path,
            tmp_path / "campaign-mismatch",
        )
    assert not any(event.startswith("load:") for event in environment.events)