File size: 21,693 Bytes
c970469
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
from __future__ import annotations

import hashlib
import hmac
import json
from pathlib import Path
from typing import Any

import pytest

from scripts import ctx_ab_holdout as selector


ROOT = Path(__file__).resolve().parents[2]
PROTOCOL_PATH = ROOT / "benchmarks" / "ctx_ab" / "holdout-protocol-v1.json"


def _protocol(
    *,
    strategy: str | None = None,
    repositories: int = 10,
    private_canary: bool | None = None,
    candidates_per_repository: int = 1,
) -> dict[str, Any]:
    protocol = json.loads(PROTOCOL_PATH.read_text(encoding="utf-8"))
    rules = protocol["selection"]
    rules["analysis_repositories"] = repositories
    rules["analysis_scenarios"] = repositories
    rules["eligible_candidates_per_repository_required"] = candidates_per_repository
    effective_private_canary = (
        private_canary if private_canary is not None else strategy != "one-per-repository"
    )
    rules["eligible_repositories_required"] = repositories + int(effective_private_canary)
    if strategy is None:
        rules.pop("strategy", None)
    else:
        rules["strategy"] = strategy
    if private_canary is None:
        rules.pop("private_canary", None)
    else:
        rules["private_canary"] = private_canary
    return protocol


def _v2_protocol(*, generation: int) -> dict[str, Any]:
    protocol = _protocol(
        strategy="one-per-repository",
        private_canary=False,
        candidates_per_repository=generation,
    )
    protocol["schema_version"] = 2
    protocol["protocol_id"] = "production-graph-holdout-v2"
    protocol["protocol_generation"] = generation
    protocol["candidate_partition_seed"] = hashlib.sha256(
        selector.V2_CANDIDATE_PARTITION_PREFIX
        + str(protocol["universe"]["revision"]).encode("ascii")
    ).hexdigest()
    protocol["selection"]["candidate_slot"] = generation - 1
    return protocol


def _ledger(repositories: int, *, candidates_per_repository: int = 1) -> list[dict[str, object]]:
    rows: list[dict[str, object]] = []
    for repo_index in range(repositories):
        for candidate_index in range(candidates_per_repository):
            rows.append(
                {
                    "instance_id": f"repo-{repo_index}-candidate-{candidate_index}",
                    "repo": f"owner/repo-{repo_index}",
                    "production_paths": f"src/repo_{repo_index}/feature_{candidate_index}.py",
                    "test_path": f"tests/repo_{repo_index}/test_{candidate_index}.py",
                    "status": "eligible",
                }
            )
    return rows


def _exposure_ledger(*instance_ids: str) -> dict[str, object]:
    salt = "a" * 64
    hashes = sorted(
        hmac.new(
            bytes.fromhex(salt),
            instance_id.encode("utf-8"),
            hashlib.sha256,
        ).hexdigest()
        for instance_id in instance_ids
    )
    return {
        "schema_version": 1,
        "salt": salt,
        "instance_id_hmac_sha256": hashes,
    }


def _canonical_bytes(value: object) -> bytes:
    return json.dumps(value, sort_keys=True, separators=(",", ":")).encode()


def test_legacy_strategy_preserves_exact_v1_selection() -> None:
    protocol = json.loads(PROTOCOL_PATH.read_text(encoding="utf-8"))
    rows = [
        {
            "instance_id": f"owner__repo-{repo}-{suffix}",
            "repo": f"owner/repo-{repo}",
            "production_paths": f"src/{repo}/{suffix}.py",
            "test_path": f"tests/{repo}/test_{suffix}.py",
            "status": "eligible",
        }
        for repo, suffixes in zip("abcdefg", ("ab", "cd", "ef", "12", "34", "56", "78"))
        for suffix in suffixes
    ]

    assert selector.select_rows(rows, protocol) == {
        "protocol_id": "production-graph-holdout-v1",
        "analysis_instance_ids": [
            "owner__repo-g-8",
            "owner__repo-c-e",
            "owner__repo-b-c",
            "owner__repo-d-2",
            "owner__repo-e-4",
            "owner__repo-g-7",
        ],
        "analysis_repository_map": {
            "owner__repo-g-8": "https://github.com/owner/repo-g.git",
            "owner__repo-c-e": "https://github.com/owner/repo-c.git",
            "owner__repo-b-c": "https://github.com/owner/repo-b.git",
            "owner__repo-d-2": "https://github.com/owner/repo-d.git",
            "owner__repo-e-4": "https://github.com/owner/repo-e.git",
            "owner__repo-g-7": "https://github.com/owner/repo-g.git",
        },
        "canary_instance_id": "owner__repo-a-a",
        "canary_repository": "https://github.com/owner/repo-a.git",
    }


def test_one_per_repository_selects_deterministic_ten_repo_analysis() -> None:
    protocol = _protocol(strategy="one-per-repository", private_canary=False)
    ledger = _ledger(10, candidates_per_repository=2)

    first = selector.select_rows(ledger, protocol)
    second = selector.select_rows(list(reversed(ledger)), protocol)
    ranked = sorted(
        {selector.canonical_repo_url(str(row["repo"])) for row in ledger},
        key=lambda repo: (selector._digest(str(protocol["selection_seed"]), repo), repo),
    )
    expected_ids = [
        min(
            (row for row in ledger if selector.canonical_repo_url(str(row["repo"])) == repository),
            key=lambda row: (
                selector._digest(
                    str(protocol["selection_seed"]),
                    str(row["instance_id"]),
                ),
                str(row["instance_id"]),
            ),
        )["instance_id"]
        for repository in ranked
    ]

    assert first == second
    assert len(first["analysis_instance_ids"]) == 10
    assert first["analysis_instance_ids"] == expected_ids
    assert list(first["analysis_repository_map"].values()) == ranked[:10]
    assert len(set(first["analysis_repository_map"].values())) == 10
    assert first["canary_instance_id"] is None
    assert first["canary_repository"] is None
    assert selector._validated_selection(first, protocol)[0] == first["analysis_instance_ids"]


def test_one_per_repository_supports_private_canary() -> None:
    protocol = _protocol(strategy="one-per-repository", private_canary=True)
    selection = selector.select_rows(_ledger(11, candidates_per_repository=2), protocol)
    assert selection["canary_instance_id"] is not None
    assert selection["canary_repository"] is not None
    assert len(selector._validated_selection(selection, protocol)[0]) == 11


def test_one_per_repository_rejects_insufficient_repositories() -> None:
    protocol = _protocol(strategy="one-per-repository", private_canary=False)
    with pytest.raises(ValueError, match="ten repositories"):
        selector.select_rows(_ledger(9, candidates_per_repository=2), protocol)


def test_one_per_repository_rejects_insufficient_candidates() -> None:
    protocol = _protocol(
        strategy="one-per-repository",
        private_canary=False,
        candidates_per_repository=2,
    )
    with pytest.raises(ValueError, match="ten repositories"):
        selector.select_rows(_ledger(10, candidates_per_repository=1), protocol)


def test_v2_generations_select_disjoint_candidate_slots() -> None:
    ledger = _ledger(10, candidates_per_repository=2)
    first = selector.select_rows(ledger, _v2_protocol(generation=1))
    second = selector.select_rows(ledger, _v2_protocol(generation=2))

    assert set(first["analysis_instance_ids"]).isdisjoint(second["analysis_instance_ids"])
    assert set(first["analysis_repository_map"].values()) == set(
        second["analysis_repository_map"].values()
    )


def test_v2_excludes_previously_exposed_candidate_before_ranking() -> None:
    protocol = _v2_protocol(generation=1)
    ledger = _ledger(10, candidates_per_repository=2)
    baseline = selector.select_rows(ledger, protocol)
    exposed_id = str(baseline["analysis_instance_ids"][0])

    filtered = selector.reject_historical_exposures(
        ledger,
        _exposure_ledger(exposed_id),
    )
    selection = selector.select_rows(filtered, protocol)

    assert exposed_id not in selection["analysis_instance_ids"]
    assert next(row for row in filtered if row["instance_id"] == exposed_id) == {
        **next(row for row in ledger if row["instance_id"] == exposed_id),
        "status": "rejected",
        "rejection_code": "historical-exposure",
    }
    selector.require_exposure_disjoint_selection(
        selection,
        _exposure_ledger(exposed_id),
    )


def test_v2_generation_fails_closed_without_fresh_candidate_slot() -> None:
    protocol = _v2_protocol(generation=2)

    with pytest.raises(ValueError, match="ten repositories with at least two eligible rows"):
        selector.select_rows(_ledger(10, candidates_per_repository=1), protocol)


@pytest.mark.parametrize(
    ("field", "value"),
    [
        ("protocol_generation", 3),
        ("candidate_partition_seed", "0" * 64),
    ],
)
def test_v2_rejects_candidate_partition_protocol_drift(field: str, value: object) -> None:
    protocol = _v2_protocol(generation=2)
    protocol[field] = value

    with pytest.raises(ValueError, match="candidate partition contract"):
        selector.select_rows(_ledger(10, candidates_per_repository=3), protocol)


def test_v2_rejects_candidate_slot_drift() -> None:
    protocol = _v2_protocol(generation=2)
    protocol["selection"]["candidate_slot"] = 0

    with pytest.raises(ValueError, match="candidate partition contract"):
        selector.select_rows(_ledger(10, candidates_per_repository=2), protocol)


@pytest.mark.parametrize(
    ("field", "value"),
    [
        ("analysis_scenarios", 9),
        ("eligible_repositories_required", 11),
        ("eligible_candidates_per_repository_required", 0),
    ],
)
def test_one_per_repository_rejects_invalid_cardinalities(field: str, value: int) -> None:
    protocol = _protocol(strategy="one-per-repository", private_canary=False)
    protocol["selection"][field] = value
    with pytest.raises(ValueError, match="one-per-repository"):
        selector.select_rows(_ledger(11), protocol)


def test_one_per_repository_requires_explicit_canary_mode() -> None:
    protocol = _protocol(strategy="one-per-repository", private_canary=False)
    protocol["selection"].pop("private_canary")
    with pytest.raises(ValueError, match="explicit private_canary"):
        selector.select_rows(_ledger(10), protocol)


def test_selector_rejects_duplicate_instance_ids() -> None:
    protocol = _protocol(strategy="one-per-repository", private_canary=False)
    ledger = _ledger(10, candidates_per_repository=2)
    ledger[1]["instance_id"] = ledger[0]["instance_id"]
    with pytest.raises(ValueError, match="duplicate instance IDs"):
        selector.select_rows(ledger, protocol)


def test_v2_selection_validation_requires_distinct_analysis_repositories() -> None:
    protocol = _protocol(strategy="one-per-repository", private_canary=False)
    selection = selector.select_rows(_ledger(10, candidates_per_repository=2), protocol)
    selector._validated_selection(selection, protocol)
    first_id = selection["analysis_instance_ids"][0]
    second_id = selection["analysis_instance_ids"][1]
    selection["analysis_repository_map"][second_id] = selection["analysis_repository_map"][first_id]
    with pytest.raises(ValueError, match="claim selection is invalid"):
        selector._validated_selection(selection, protocol)


@pytest.mark.parametrize(
    ("field", "value"),
    [
        ("canary_instance_id", "public-canary"),
        ("canary_repository", "https://github.com/public/sympy.git"),
    ],
)
def test_v2_selection_validation_requires_null_external_canary_fields(
    field: str,
    value: str,
) -> None:
    protocol = _protocol(strategy="one-per-repository", private_canary=False)
    selection = selector.select_rows(_ledger(10, candidates_per_repository=2), protocol)
    selection[field] = value
    with pytest.raises(ValueError, match="claim selection is invalid"):
        selector._validated_selection(selection, protocol)


def _v2_cli_arguments(
    tmp_path: Path,
    *,
    protocol_bytes: bytes,
    expected_protocol_sha256: str | None,
    exposure_ledger_bytes: bytes | None = None,
) -> tuple[list[str], Path, Path, Path]:
    protocol_path = tmp_path / "protocol.json"
    source_path = tmp_path / "source.jsonl"
    private = tmp_path / "private"
    private.mkdir(mode=0o700)
    ledger_path = private / "ledger.csv"
    selection_path = private / "selection.json"
    exposure_path = private / "exposure-ledger.json"
    protocol_path.write_bytes(protocol_bytes)
    source_path.write_text("{}\n", encoding="utf-8")
    if exposure_ledger_bytes is None:
        exposure_ledger_bytes = _canonical_bytes(_exposure_ledger("synthetic-nonmatching-history"))
    exposure_path.write_bytes(exposure_ledger_bytes)
    exposure_path.chmod(0o600)
    arguments = [
        "--protocol",
        str(protocol_path),
        "--selection-jsonl",
        str(source_path),
        "--ledger",
        str(ledger_path),
        "--selection",
        str(selection_path),
        "--exposure-ledger",
        str(exposure_path),
    ]
    if expected_protocol_sha256 is not None:
        arguments.extend(
            [
                "--expected-acquisition-protocol-sha256",
                expected_protocol_sha256,
            ]
        )
    return arguments, source_path, ledger_path, selection_path


def _v2_cli_protocol(source_path: Path, exposure_ledger_bytes: bytes) -> dict[str, Any]:
    protocol = _protocol(strategy="one-per-repository", private_canary=False)
    protocol["schema_version"] = 2
    protocol["protocol_id"] = "production-graph-holdout-v2"
    protocol["protocol_generation"] = 1
    protocol["candidate_partition_seed"] = hashlib.sha256(
        selector.V2_CANDIDATE_PARTITION_PREFIX
        + str(protocol["universe"]["revision"]).encode("ascii")
    ).hexdigest()
    protocol["selection"]["candidate_slot"] = 0
    protocol["stage"] = "acquisition-frozen"
    protocol["universe"]["expected_rows"] = 10
    protocol["universe"]["raw_parquet_sha256"] = "1" * 64
    protocol["universe"]["duckdb_cli_sha256"] = "2" * 64
    protocol["universe"]["selection_jsonl_sha256"] = hashlib.sha256(
        source_path.read_bytes()
    ).hexdigest()
    protocol["exposure_ledger_sha256"] = hashlib.sha256(exposure_ledger_bytes).hexdigest()
    return protocol


def test_v2_selector_cli_accepts_matching_acquisition_protocol_digest(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    placeholder_protocol = b"{}"
    arguments, source_path, ledger_path, selection_path = _v2_cli_arguments(
        tmp_path,
        protocol_bytes=placeholder_protocol,
        expected_protocol_sha256=hashlib.sha256(placeholder_protocol).hexdigest(),
    )
    exposure_bytes = (tmp_path / "private" / "exposure-ledger.json").read_bytes()
    protocol_bytes = json.dumps(
        _v2_cli_protocol(source_path, exposure_bytes),
        sort_keys=True,
        separators=(",", ":"),
    ).encode()
    protocol_path = tmp_path / "protocol.json"
    protocol_path.write_bytes(protocol_bytes)
    digest_index = arguments.index("--expected-acquisition-protocol-sha256") + 1
    arguments[digest_index] = hashlib.sha256(protocol_bytes).hexdigest()
    rows = [
        {
            **row,
            "base_commit": "",
            "production_changed_lines": 0,
            "rejection_code": "",
        }
        for row in _ledger(10)
    ]
    monkeypatch.setattr(selector, "_load_jsonl", lambda _: rows)
    monkeypatch.setattr(selector, "evaluate_row", lambda row, _: row)

    assert selector.main(arguments) == 0
    assert ledger_path.is_file()
    assert selection_path.is_file()


def test_v2_selector_cli_output_is_deterministic_with_authenticated_exposure_ledger(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    placeholder_protocol = b"{}"
    arguments, source_path, ledger_path, selection_path = _v2_cli_arguments(
        tmp_path,
        protocol_bytes=placeholder_protocol,
        expected_protocol_sha256=hashlib.sha256(placeholder_protocol).hexdigest(),
    )
    exposure_bytes = (tmp_path / "private" / "exposure-ledger.json").read_bytes()
    protocol_bytes = _canonical_bytes(_v2_cli_protocol(source_path, exposure_bytes))
    (tmp_path / "protocol.json").write_bytes(protocol_bytes)
    arguments[arguments.index("--expected-acquisition-protocol-sha256") + 1] = hashlib.sha256(
        protocol_bytes
    ).hexdigest()
    rows = [
        {
            **row,
            "base_commit": "",
            "production_changed_lines": 0,
            "rejection_code": "",
        }
        for row in _ledger(10)
    ]
    monkeypatch.setattr(selector, "_load_jsonl", lambda _: rows)
    monkeypatch.setattr(selector, "evaluate_row", lambda row, _: row)

    assert selector.main(arguments) == 0
    first = (ledger_path.read_bytes(), selection_path.read_bytes())
    assert selector.main(arguments) == 0

    assert (ledger_path.read_bytes(), selection_path.read_bytes()) == first


def test_v2_selector_cli_rejects_authenticated_empty_exposure_ledger(
    tmp_path: Path,
) -> None:
    exposure = _canonical_bytes(_exposure_ledger())
    placeholder_protocol = b"{}"
    arguments, source_path, ledger_path, selection_path = _v2_cli_arguments(
        tmp_path,
        protocol_bytes=placeholder_protocol,
        expected_protocol_sha256=hashlib.sha256(placeholder_protocol).hexdigest(),
        exposure_ledger_bytes=exposure,
    )
    protocol_bytes = _canonical_bytes(_v2_cli_protocol(source_path, exposure))
    (tmp_path / "protocol.json").write_bytes(protocol_bytes)
    arguments[arguments.index("--expected-acquisition-protocol-sha256") + 1] = hashlib.sha256(
        protocol_bytes
    ).hexdigest()
    ledger_path.write_text("preserve-ledger", encoding="utf-8")
    selection_path.write_text("preserve-selection", encoding="utf-8")

    with pytest.raises(SystemExit, match="exposure ledger.*must not be empty"):
        selector.main(arguments)

    assert ledger_path.read_text(encoding="utf-8") == "preserve-ledger"
    assert selection_path.read_text(encoding="utf-8") == "preserve-selection"


@pytest.mark.parametrize("failure", ["missing-path", "missing-digest", "tampered", "wrong"])
def test_v2_selector_cli_requires_exact_authenticated_exposure_ledger(
    tmp_path: Path,
    failure: str,
) -> None:
    exposure = _canonical_bytes(_exposure_ledger("synthetic-nonmatching-history"))
    placeholder_protocol = b"{}"
    arguments, source_path, ledger_path, selection_path = _v2_cli_arguments(
        tmp_path,
        protocol_bytes=placeholder_protocol,
        expected_protocol_sha256=hashlib.sha256(placeholder_protocol).hexdigest(),
        exposure_ledger_bytes=exposure,
    )
    protocol = _v2_cli_protocol(source_path, exposure)
    if failure == "missing-digest":
        protocol.pop("exposure_ledger_sha256")
    protocol_bytes = _canonical_bytes(protocol)
    (tmp_path / "protocol.json").write_bytes(protocol_bytes)
    arguments[arguments.index("--expected-acquisition-protocol-sha256") + 1] = hashlib.sha256(
        protocol_bytes
    ).hexdigest()
    exposure_path = tmp_path / "private" / "exposure-ledger.json"
    if failure == "missing-path":
        index = arguments.index("--exposure-ledger")
        del arguments[index : index + 2]
    elif failure == "tampered":
        exposure_path.write_bytes(exposure + b"\n")
    elif failure == "wrong":
        exposure_path.write_bytes(_canonical_bytes(_exposure_ledger("synthetic-prior-task")))
    ledger_path.write_text("preserve-ledger", encoding="utf-8")
    selection_path.write_text("preserve-selection", encoding="utf-8")

    with pytest.raises(SystemExit, match="exposure ledger"):
        selector.main(arguments)

    assert ledger_path.read_text(encoding="utf-8") == "preserve-ledger"
    assert selection_path.read_text(encoding="utf-8") == "preserve-selection"


@pytest.mark.parametrize("failure", ["missing", "drift"])
def test_v2_selector_cli_authenticates_protocol_before_private_io(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
    failure: str,
) -> None:
    placeholder_protocol = b"{}"
    arguments, source_path, ledger_path, selection_path = _v2_cli_arguments(
        tmp_path,
        protocol_bytes=placeholder_protocol,
        expected_protocol_sha256=None,
    )
    exposure_bytes = (tmp_path / "private" / "exposure-ledger.json").read_bytes()
    protocol_bytes = json.dumps(
        _v2_cli_protocol(source_path, exposure_bytes),
        sort_keys=True,
        separators=(",", ":"),
    ).encode()
    (tmp_path / "protocol.json").write_bytes(protocol_bytes)
    if failure == "drift":
        arguments.extend(
            [
                "--expected-acquisition-protocol-sha256",
                hashlib.sha256(protocol_bytes).hexdigest(),
            ]
        )
        (tmp_path / "protocol.json").write_bytes(protocol_bytes + b"\n")
    ledger_path.write_text("preserve-ledger", encoding="utf-8")
    selection_path.write_text("preserve-selection", encoding="utf-8")

    def fail_private_read(_: Path) -> list[dict[str, Any]]:
        raise AssertionError("private rows were read before protocol authentication")

    monkeypatch.setattr(selector, "_load_jsonl", fail_private_read)
    message = (
        "requires --expected-acquisition-protocol-sha256"
        if failure == "missing"
        else "does not match the expected SHA-256"
    )
    with pytest.raises(SystemExit, match=message):
        selector.main(arguments)

    assert ledger_path.read_text(encoding="utf-8") == "preserve-ledger"
    assert selection_path.read_text(encoding="utf-8") == "preserve-selection"