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

import hashlib
import json
import traceback
from collections.abc import Iterator, Sequence
from dataclasses import dataclass, field, replace
from pathlib import Path
from typing import cast, overload

import pytest

from ctx.engine.benefit import NetBenefitPolicy
from ctx.engine.planner import CapabilityCandidate, PlannerValidationError, WorkObservation
from ctx.runtime.benefit_closure import (
    BenefitClosureError,
    EligibleCatalogClaim,
    QueryCapabilityEligibility,
    QueryHostPolicyAuthority,
    ReviewedBenefitAuthorities,
    ReviewedBenefitProfiles,
    catalog_candidate_entry_claim_digest,
    eligible_catalog_claim_digest,
    load_reviewed_benefit_profiles,
    load_reviewed_benefit_profiles_bytes,
    prepare_query_benefit_closure,
)
from ctx.runtime.planning_v3 import normalize_candidate_pool


def _digest(value: str) -> str:
    return hashlib.sha256(value.encode("utf-8")).hexdigest()


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


CALIBRATION_DIGEST = _digest("reviewed-calibration")
CATALOG_SNAPSHOT_DIGEST = _digest("eligible-catalog-snapshot")
ELIGIBILITY_DIGEST = _digest("eligible-catalog-index")
HOST_POLICY_DIGEST = _digest("query-host-policy")
POLICY = NetBenefitPolicy(calibration_digest=CALIBRATION_DIGEST, minimum_relevance_ppm=1)


def _profile_record(
    *,
    capability_id: str = "skill:ctx-python-testing",
    actionability: str = "manual",
    entry_claim_digest: str | None = None,
    minimum_matching_signals: int = 2,
    minimum_non_language_matching_signals: int = 1,
    allowed_signals: list[str] | None = None,
) -> dict[str, object]:
    kind, name = capability_id.split(":", 1)
    return {
        "actionability": actionability,
        "capability_id": capability_id,
        "catalog_entry_claim_digest": entry_claim_digest
        or catalog_candidate_entry_claim_digest(
            _candidate(capability_id=capability_id, actionability=actionability)
        ),
        "complements": [],
        "conflicts": [],
        "costs": {
            "approval_prompts": 0,
            "child_agent_units": 0,
            "context_tokens": 180,
            "credential_burden_units": 0,
            "permission_burden_units": 0,
            "process_units": 0,
            "runtime_millis": 0,
            "tool_schema_tokens": 0,
        },
        "coverage_keys": [f"coverage-{name}"],
        "expected_task_benefit_ppm": 500_000,
        "kind": kind,
        "match_policy": {
            "allowed_equivalence_keys": [],
            "allowed_reason_codes": ["graph-match", "signal-match"],
            "allowed_signals": allowed_signals or ["python", "testing"],
            "minimum_matching_signals": minimum_matching_signals,
            "minimum_non_language_matching_signals": minimum_non_language_matching_signals,
            "required_any_signals": ["testing"],
        },
        "maximum_relevance_ppm": 700_000,
        "name": name,
        "profile_id": f"profile-{name}",
        "review_basis_digest": _digest(f"review-basis:{capability_id}"),
        "security_approved": True,
        "source_trusted": True,
        "trust_ppm": 900_000,
    }


def _manifest(
    *profiles: dict[str, object],
    authority_id: str = "ctx-release",
    authority_kind: str = "ctx-release",
    catalog_layer_kind: str = "ctx",
    sequence: int = 1,
) -> dict[str, object]:
    ordered = sorted(
        profiles or (_profile_record(),),
        key=lambda value: (
            str(value["capability_id"]),
            str(value["actionability"]),
            str(value["profile_id"]),
        ),
    )
    return {
        "authority": {
            "authority_digest": _digest(f"benefit-authority:{authority_id}:{sequence}"),
            "authority_id": authority_id,
            "authority_kind": authority_kind,
            "catalog_layer_kind": catalog_layer_kind,
            "sequence": sequence,
        },
        "bindings": {
            "calibration_digest": CALIBRATION_DIGEST,
            "candidate_projection_version": "ctx.catalog-entry.graph-v4",
            "catalog_artifact_sha256": _digest(f"eligible-catalog-artifact:{authority_id}"),
            "catalog_namespace_digest": _digest(f"catalog-namespace:{authority_id}"),
            "catalog_provenance_digest": _digest(f"catalog-provenance:{authority_id}"),
            "catalog_retrieval_snapshot_digest": _digest(f"catalog-retrieval:{authority_id}"),
            "installation_snapshot_digest": None,
            "material_snapshot_digest": None,
            "policy_digest": POLICY.policy_digest,
        },
        "profiles": ordered,
        "schema": "ctx.reviewed-benefit-profiles-v2",
    }


def _authority(
    *records: dict[str, object],
    authority_id: str = "ctx-release",
    authority_kind: str = "ctx-release",
    catalog_layer_kind: str = "ctx",
    sequence: int = 1,
) -> ReviewedBenefitProfiles:
    body = _canonical_bytes(
        _manifest(
            *records,
            authority_id=authority_id,
            authority_kind=authority_kind,
            catalog_layer_kind=catalog_layer_kind,
            sequence=sequence,
        )
    )
    return load_reviewed_benefit_profiles_bytes(body, hashlib.sha256(body).hexdigest())


def _authorities(*values: ReviewedBenefitProfiles) -> ReviewedBenefitAuthorities:
    return ReviewedBenefitAuthorities.create(values or (_authority(),))


def _candidate(
    *,
    capability_id: str = "skill:ctx-python-testing",
    matching_signals: tuple[str, ...] = ("python", "testing"),
    actionability: str = "manual",
    reason_codes: tuple[str, ...] = ("graph-match", "signal-match"),
) -> CapabilityCandidate:
    kind, name = capability_id.split(":", 1)
    return CapabilityCandidate(
        capability_id=capability_id,
        kind=kind,
        name=name,
        source_digest=_digest(f"source:{capability_id}:{actionability}"),
        normalized_score_ppm=700_000,
        matching_signals=matching_signals,
        reason_codes=reason_codes,
        actionability=actionability,
        install_descriptor_digest=(
            _digest(f"install-descriptor:{capability_id}") if actionability == "install" else None
        ),
        install_plan_digest=(
            _digest(f"install-plan:{capability_id}") if actionability == "install" else None
        ),
    )


def _observation(
    *,
    signals: tuple[str, ...] = ("testing",),
    languages: tuple[str, ...] = ("python",),
    baseline: tuple[str, ...] = (),
    rejected: tuple[str, ...] = (),
) -> WorkObservation:
    return WorkObservation(
        signals=signals,
        languages=languages,
        baseline_capability_ids=baseline,
        rejected_capability_ids=rejected,
        requested_limit=5,
    )


@dataclass
class _EligibleSource:
    candidates: object
    claims: dict[str, EligibleCatalogClaim]
    catalog_snapshot_digest: str
    catalog_retrieval_snapshot_digest: str
    catalog_namespace_digest: str
    catalog_provenance_digest: str
    catalog_artifact_sha256: str
    candidate_projection_version: str
    material_snapshot_digest: str | None
    installation_snapshot_digest: str | None
    profile_snapshot_digest: str
    authority_snapshot_digest: str
    eligibility_snapshot_digest: str
    calibration_digest: str
    policy_digest: str
    close_calls: int = 0
    cleanup_failure: bool = False
    retrieve_calls: int = 0
    claim_calls: int = 0
    candidate_rounds: tuple[object, ...] = ()
    claim_rounds: dict[str, tuple[EligibleCatalogClaim, ...]] = field(default_factory=dict)
    drift_binding_on_retrieve: bool = False
    drift_binding_on_claim: bool = False
    explode_at: str | None = None
    leak_cleanup_class: bool = False

    def retrieve(self, _observation: WorkObservation) -> Sequence[CapabilityCandidate]:
        if self.explode_at == "retrieve":
            raise RuntimeError("/Users/alice/private-repo sk-live-secret")
        self.retrieve_calls += 1
        value = (
            self.candidate_rounds[min(self.retrieve_calls - 1, len(self.candidate_rounds) - 1)]
            if self.candidate_rounds
            else self.candidates
        )
        if self.drift_binding_on_retrieve:
            self.catalog_provenance_digest = _digest("drifted-during-retrieve")
        return cast(Sequence[CapabilityCandidate], value)

    def entry_claim(self, presentation: CapabilityCandidate) -> EligibleCatalogClaim:
        if self.explode_at == "claim":
            raise RuntimeError("/Users/alice/private-repo sk-live-secret")
        self.claim_calls += 1
        rounds = self.claim_rounds.get(presentation.capability_id, ())
        value = (
            rounds[min(self.claim_calls - 1, len(rounds) - 1)]
            if rounds
            else self.claims[presentation.capability_id]
        )
        if self.drift_binding_on_claim:
            self.catalog_provenance_digest = _digest("drifted-during-claim")
        return value

    def close(self) -> None:
        self.close_calls += 1
        if self.leak_cleanup_class:
            raise Alice_private_repo_sk_live_secret
        if self.explode_at == "close":
            raise RuntimeError("/Users/alice/private-repo sk-live-secret")
        if self.cleanup_failure:
            raise RuntimeError("cleanup sentinel")


class Alice_private_repo_sk_live_secret(RuntimeError):
    pass


class _ExplodingAttributeSource:
    def __init__(self, source: _EligibleSource, attribute: str) -> None:
        self._source = source
        self._attribute = attribute

    def __getattr__(self, name: str) -> object:
        if name == self._attribute:
            raise RuntimeError("/Users/alice/private-repo sk-live-secret")
        return getattr(self._source, name)


def _source(
    profiles: ReviewedBenefitAuthorities,
    *candidates: CapabilityCandidate,
    claim_authorities: dict[str, ReviewedBenefitProfiles] | None = None,
) -> _EligibleSource:
    values = candidates or (_candidate(),)
    authority_by_id = claim_authorities or {
        candidate.capability_id: profiles.authorities[0] for candidate in values
    }
    claims = {
        candidate.capability_id: EligibleCatalogClaim.create(
            authority_by_id[candidate.capability_id],
            presentation=candidate,
        )
        for candidate in values
    }
    return _EligibleSource(
        candidates=values,
        claims=claims,
        catalog_snapshot_digest=CATALOG_SNAPSHOT_DIGEST,
        catalog_retrieval_snapshot_digest=profiles.catalog_retrieval_snapshot_digest,
        catalog_namespace_digest=profiles.catalog_namespace_digest,
        catalog_provenance_digest=profiles.catalog_provenance_digest,
        catalog_artifact_sha256=profiles.catalog_artifact_sha256,
        candidate_projection_version=profiles.candidate_projection_version,
        material_snapshot_digest=profiles.material_snapshot_digest,
        installation_snapshot_digest=profiles.installation_snapshot_digest,
        profile_snapshot_digest=profiles.profile_snapshot_digest,
        authority_snapshot_digest=profiles.authority_snapshot_digest,
        eligibility_snapshot_digest=ELIGIBILITY_DIGEST,
        calibration_digest=profiles.calibration_digest,
        policy_digest=profiles.policy_digest,
    )


@dataclass
class _HostPolicy:
    host_policy_snapshot_digest: str = HOST_POLICY_DIGEST
    available: bool = True
    permissions_allowed: bool = True
    credentials_available: bool = True
    return_none: bool = False
    drift: bool = False
    claim_digest_override: str | None = None
    explode: bool = False
    calls: int = 0

    def eligibility_for(
        self,
        presentation: CapabilityCandidate,
        claim: EligibleCatalogClaim,
    ) -> QueryCapabilityEligibility | None:
        if self.explode:
            raise RuntimeError("/Users/alice/private-repo sk-live-secret")
        self.calls += 1
        if self.return_none:
            return None
        result = QueryCapabilityEligibility(
            presentation_digest=_presentation_digest(presentation),
            catalog_entry_claim_digest=claim.catalog_entry_claim_digest,
            catalog_claim_digest=(
                self.claim_digest_override or eligible_catalog_claim_digest(claim)
            ),
            available=self.available,
            permissions_allowed=self.permissions_allowed,
            credentials_available=self.credentials_available,
        )
        if self.drift:
            self.host_policy_snapshot_digest = _digest("drifted-host-policy")
        return result


def _presentation_digest(candidate: CapabilityCandidate) -> str:
    from ctx.runtime.authenticated_benefit import capability_presentation_digest

    return capability_presentation_digest(candidate)


def _prepare(
    profiles: ReviewedBenefitAuthorities,
    source: _EligibleSource,
    *,
    observation: WorkObservation | object | None = None,
    host_policy: _HostPolicy | None = None,
):  # type: ignore[no-untyped-def]
    return prepare_query_benefit_closure(
        source=source,
        observation=observation if observation is not None else _observation(),  # type: ignore[arg-type]
        profiles=profiles,
        policy=POLICY,
        host_policy=host_policy or _HostPolicy(),
    )


def test_reviewed_profiles_load_from_exact_file_and_frozen_bytes(tmp_path: Path) -> None:
    body = _canonical_bytes(_manifest())
    expected_sha256 = hashlib.sha256(body).hexdigest()
    path = tmp_path / "reviewed-benefit-profiles.json"
    path.write_bytes(body)
    path.chmod(0o444)

    from_path = load_reviewed_benefit_profiles(path, expected_sha256)
    from_bytes = load_reviewed_benefit_profiles_bytes(body, expected_sha256)

    assert from_path == from_bytes
    assert from_path.profile_snapshot_digest == expected_sha256
    assert from_path.policy_digest == POLICY.policy_digest
    assert len(from_path.profiles) == 1


@pytest.mark.parametrize(
    "mutation",
    ["unknown-root", "wrong-layer", "duplicate-profile", "unreviewed-signal"],
)
def test_reviewed_profiles_reject_non_closed_cross_authority_or_ambiguous_input(
    mutation: str,
) -> None:
    manifest = _manifest()
    if mutation == "unknown-root":
        manifest["description"] = "catalog prose is not profile authority"
    elif mutation == "wrong-layer":
        manifest["authority"]["catalog_layer_kind"] = "user"  # type: ignore[index]
    elif mutation == "duplicate-profile":
        manifest["profiles"] = [manifest["profiles"][0], manifest["profiles"][0]]  # type: ignore[index]
    else:
        manifest["profiles"][0]["match_policy"]["required_any_signals"] = ["secret"]  # type: ignore[index]
    body = _canonical_bytes(manifest)

    with pytest.raises(BenefitClosureError):
        load_reviewed_benefit_profiles_bytes(body, hashlib.sha256(body).hexdigest())


def test_query_closure_freezes_exact_presentations_authorities_policy_and_facts() -> None:
    profiles = _authorities()
    observation = _observation()
    raw_source = _source(profiles)

    closure = _prepare(profiles, raw_source, observation=observation)

    assert raw_source.close_calls == 1
    assert raw_source.retrieve_calls == 2
    assert closure.source.retrieve(observation) == (_candidate(),)
    facts = closure.benefit_facts.benefit_candidate(_candidate(), observation)
    assert facts is not None
    assert facts.expected_task_benefit_ppm == 500_000
    assert facts.relevance_ppm == 700_000
    assert facts.costs is not None and facts.costs.context_tokens == 180
    assert closure.authority_snapshot_digest == profiles.authority_snapshot_digest
    assert closure.policy is POLICY
    assert closure.policy_digest == POLICY.policy_digest
    assert closure.upstream_host_policy_snapshot_digest == HOST_POLICY_DIGEST
    assert (
        closure.host_policy_snapshot_digest
        == closure.host_policy_authority.host_policy_snapshot_digest
    )
    assert closure.closure_snapshot_digest == closure.source.catalog_snapshot_digest

    with pytest.raises(PlannerValidationError, match="observation mismatch"):
        closure.source.retrieve(WorkObservation(signals=("security",), languages=("python",)))
    closure.source.close()
    with pytest.raises(PlannerValidationError, match="closed"):
        closure.source.retrieve(observation)


def test_query_closure_value_recomputes_its_public_pins() -> None:
    profiles = _authorities()
    closure = _prepare(profiles, _source(profiles))

    with pytest.raises(TypeError, match="closure factory"):
        replace(closure, policy_digest=_digest("substituted-policy"))


def test_reviewed_values_recompute_profile_and_manifest_membership() -> None:
    authority = _authority()

    with pytest.raises(BenefitClosureError, match="profile_digest"):
        replace(authority.profiles[0], source_trusted=False)
    with pytest.raises(BenefitClosureError, match="profile_snapshot_digest"):
        replace(authority, sequence=2)


def test_query_closure_rejects_forged_claim_outside_advertised_authorities() -> None:
    profiles = _authorities()
    closure = _prepare(profiles, _source(profiles))
    candidate = closure.source.retrieve(_observation())[0]
    user_authority = _authority(
        authority_id="local-user",
        authority_kind="user",
        catalog_layer_kind="user",
    )
    forged_claim = EligibleCatalogClaim.create(user_authority, presentation=candidate)
    forged_host_fact = replace(
        closure.host_eligibilities[0],
        catalog_entry_claim_digest=forged_claim.catalog_entry_claim_digest,
        catalog_claim_digest=eligible_catalog_claim_digest(forged_claim),
    )

    with pytest.raises(TypeError, match="closure factory"):
        replace(
            closure,
            catalog_claims=(forged_claim,),
            host_eligibilities=(forged_host_fact,),
        )


@pytest.mark.parametrize(
    "field_name",
    [
        "catalog_entry_claim_digest",
        "presentation_digest",
    ],
)
def test_query_closure_rechecks_claim_on_reconstruction(field_name: str) -> None:
    profiles = _authorities()
    closure = _prepare(profiles, _source(profiles))
    original_claim = closure.catalog_claims[0]
    forged_claim = replace(original_claim, **{field_name: _digest(f"other-{field_name}")})
    forged_host_fact = replace(
        closure.host_eligibilities[0],
        catalog_claim_digest=eligible_catalog_claim_digest(forged_claim),
    )

    with pytest.raises(TypeError, match="closure factory"):
        replace(
            closure,
            catalog_claims=(forged_claim,),
            host_eligibilities=(forged_host_fact,),
        )


def test_query_closure_supports_independent_ctx_and_user_authorities() -> None:
    ctx_authority = _authority()
    user_candidate = _candidate(capability_id="agent:user-reviewer")
    user_claim = catalog_candidate_entry_claim_digest(user_candidate)
    user_authority = _authority(
        _profile_record(
            capability_id=user_candidate.capability_id,
            entry_claim_digest=user_claim,
        ),
        authority_id="local-user",
        authority_kind="user",
        catalog_layer_kind="user",
    )
    profiles = _authorities(ctx_authority, user_authority)
    source = _source(
        profiles,
        _candidate(),
        user_candidate,
        claim_authorities={
            "skill:ctx-python-testing": ctx_authority,
            user_candidate.capability_id: user_authority,
        },
    )

    closure = _prepare(profiles, source)

    assert {item.capability_id for item in closure.source.retrieve(_observation())} == {
        "skill:ctx-python-testing",
        "agent:user-reviewer",
    }


def test_host_policy_cannot_reuse_same_entry_digest_across_authorities() -> None:
    candidate = _candidate()
    ctx_authority = _authority()
    user_authority = _authority(
        authority_id="local-user",
        authority_kind="user",
        catalog_layer_kind="user",
    )
    profiles = _authorities(ctx_authority, user_authority)
    source = _source(
        profiles,
        candidate,
        claim_authorities={candidate.capability_id: ctx_authority},
    )
    user_claim = EligibleCatalogClaim.create(user_authority, presentation=candidate)

    with pytest.raises(BenefitClosureError, match="cross-authority"):
        _prepare(
            profiles,
            source,
            host_policy=_HostPolicy(
                claim_digest_override=eligible_catalog_claim_digest(user_claim)
            ),
        )


def test_query_closure_abstains_on_unobserved_or_unreviewed_presentation_tokens() -> None:
    profiles = _authorities()
    unobserved = _candidate(matching_signals=("testing",))
    source = _source(profiles, unobserved)
    observation = _observation(signals=("security",), languages=("python",))

    closure = _prepare(profiles, source, observation=observation)
    assert closure.source.retrieve(observation) == ()

    private_reason = _candidate(reason_codes=("graph-match", "sk-live-secret-token"))
    source = _source(profiles, private_reason)
    closure = _prepare(profiles, source)
    assert closure.source.retrieve(_observation()) == ()


def test_query_closure_is_deterministic_and_weak_matches_abstain() -> None:
    profiles = _authorities()
    observation = _observation()
    first = _prepare(
        profiles,
        _source(profiles, _candidate(matching_signals=("python",))),
        observation=observation,
    )
    second = _prepare(
        profiles,
        _source(profiles, _candidate(matching_signals=("python",))),
        observation=observation,
    )

    assert first.source.retrieve(observation) == ()
    assert first.benefit_facts.presentation_digests == ()
    assert first.closure_snapshot_digest == second.closure_snapshot_digest


@pytest.mark.parametrize("excluded", ["baseline", "rejected"])
def test_query_closure_applies_shared_eligibility_before_requiring_profiles(excluded: str) -> None:
    profiles = _authorities()
    unprofiled = _candidate(capability_id="agent:unprofiled-reviewer")
    observation = _observation(
        baseline=(unprofiled.capability_id,) if excluded == "baseline" else (),
        rejected=(unprofiled.capability_id,) if excluded == "rejected" else (),
    )
    source = _source(profiles, unprofiled)

    closure = _prepare(profiles, source, observation=observation)

    assert closure.source.retrieve(observation) == ()
    assert source.claim_calls == 0
    assert source.close_calls == 1


def test_query_closure_rejects_unreviewed_or_authority_substituted_claim() -> None:
    profiles = _authorities()
    unreviewed = _candidate(capability_id="agent:unreviewed")
    source = _source(profiles, unreviewed)

    with pytest.raises(BenefitClosureError, match="without an exact reviewed profile"):
        _prepare(profiles, source)
    assert source.close_calls == 1

    user_authority = _authority(
        authority_id="local-user",
        authority_kind="user",
        catalog_layer_kind="user",
    )
    substituted = _source(profiles)
    substituted.claims["skill:ctx-python-testing"] = EligibleCatalogClaim.create(
        user_authority,
        presentation=_candidate(),
    )
    with pytest.raises(BenefitClosureError, match="without an exact reviewed profile"):
        _prepare(profiles, substituted)


def test_reviewed_install_claim_rejects_source_and_plan_substitution() -> None:
    reviewed = _candidate(actionability="install")
    authority = _authority(_profile_record(actionability="install"))
    profiles = _authorities(authority)
    substituted = replace(
        reviewed,
        source_digest=_digest("substituted-source"),
        install_descriptor_digest=_digest("substituted-descriptor"),
        install_plan_digest=_digest("substituted-plan"),
    )
    source = _source(profiles, substituted)

    with pytest.raises(BenefitClosureError, match="without an exact reviewed profile"):
        _prepare(profiles, source)

    assert source.close_calls == 1


def test_query_closure_requires_the_exact_reviewed_policy() -> None:
    manifest = _manifest()
    manifest["bindings"]["policy_digest"] = _digest("different-reviewed-policy")  # type: ignore[index]
    body = _canonical_bytes(manifest)
    authority = load_reviewed_benefit_profiles_bytes(body, hashlib.sha256(body).hexdigest())
    profiles = _authorities(authority)
    source = _source(profiles)

    with pytest.raises(BenefitClosureError, match="policy does not match"):
        _prepare(profiles, source)

    assert source.close_calls == 1


@pytest.mark.parametrize("during", ["retrieve", "claim"])
def test_query_closure_rejects_binding_drift_during_source_reads(during: str) -> None:
    profiles = _authorities()
    source = _source(profiles)
    if during == "retrieve":
        source.drift_binding_on_retrieve = True
    else:
        source.drift_binding_on_claim = True

    with pytest.raises(BenefitClosureError, match="binding drift"):
        _prepare(profiles, source)
    assert source.close_calls == 1


def test_query_closure_rejects_same_digest_candidate_or_claim_drift() -> None:
    profiles = _authorities()
    candidate = _candidate()
    changed = _candidate(matching_signals=("python",))
    source = _source(profiles, candidate)
    source.candidate_rounds = ((candidate,), (changed,))
    with pytest.raises(
        BenefitClosureError,
        match="claim does not match exact presentation|changed candidates or claims",
    ):
        _prepare(profiles, source)

    source = _source(profiles, candidate)
    first_claim = source.claims[candidate.capability_id]
    second_claim = replace(first_claim, authority_digest=_digest("changed-claim-authority"))
    source.claim_rounds[candidate.capability_id] = (first_claim, second_claim)
    with pytest.raises(BenefitClosureError, match="changed candidates or claims"):
        _prepare(profiles, source)


def test_host_policy_is_query_scoped_exact_and_not_install_consent() -> None:
    profiles = _authorities()
    source = _source(profiles)
    denied = _HostPolicy(permissions_allowed=False, credentials_available=False)

    closure = _prepare(profiles, source, host_policy=denied)
    benefit = closure.benefit_facts.benefit_candidate(_candidate(), _observation())

    assert benefit is not None
    assert benefit.permissions_allowed is False
    assert benefit.credentials_available is False
    assert denied.calls == 2

    source = _source(profiles)
    unavailable = _prepare(
        profiles,
        source,
        host_policy=_HostPolicy(available=False),
    )
    assert unavailable.source.retrieve(_observation()) == ()

    source = _source(profiles)
    with pytest.raises(BenefitClosureError, match="no exact query eligibility"):
        _prepare(profiles, source, host_policy=_HostPolicy(return_none=True))

    source = _source(profiles)
    with pytest.raises(BenefitClosureError, match="drift"):
        _prepare(profiles, source, host_policy=_HostPolicy(drift=True))


def test_query_closure_rejects_reconstructed_allow_after_host_denial() -> None:
    profiles = _authorities()
    closure = _prepare(
        profiles,
        _source(profiles),
        host_policy=_HostPolicy(permissions_allowed=False, credentials_available=False),
    )
    denied = closure.host_eligibilities[0]
    forged_allow = replace(
        denied,
        permissions_allowed=True,
        credentials_available=True,
    )

    with pytest.raises(TypeError, match="closure factory"):
        replace(closure, host_eligibilities=(forged_allow,))


def test_query_closure_closes_owned_source_on_invalid_arguments() -> None:
    profiles = _authorities()
    source = _source(profiles)

    with pytest.raises(TypeError, match="observation"):
        _prepare(profiles, source, observation=object())

    assert source.close_calls == 1


def test_query_closure_preserves_primary_error_when_cleanup_also_fails() -> None:
    profiles = _authorities()
    source = _source(profiles, _candidate(capability_id="agent:unreviewed"))
    source.cleanup_failure = True

    with pytest.raises(BenefitClosureError, match="without an exact reviewed profile") as exc:
        _prepare(profiles, source)

    assert any("cleanup also failed" in note for note in (exc.value.__notes__ or []))


@pytest.mark.parametrize("boundary", ["retrieve", "claim", "host-policy", "close"])
def test_query_closure_redacts_injected_boundary_exception_messages(boundary: str) -> None:
    profiles = _authorities()
    source = _source(profiles)
    host_policy = _HostPolicy(explode=boundary == "host-policy")
    if boundary != "host-policy":
        source.explode_at = boundary

    with pytest.raises(BenefitClosureError) as exc:
        _prepare(profiles, source, host_policy=host_policy)

    assert "alice" not in str(exc.value)
    assert "sk-live-secret" not in str(exc.value)
    assert exc.value.__suppress_context__ is True


class _ExplodingSnapshotHostPolicy:
    @property
    def host_policy_snapshot_digest(self) -> str:
        raise BenefitClosureError("/Users/alice/private-repo sk-live-secret")

    def eligibility_for(
        self,
        _presentation: CapabilityCandidate,
        _claim: EligibleCatalogClaim,
    ) -> QueryCapabilityEligibility | None:
        raise AssertionError("snapshot access must fail before eligibility lookup")


class _ExplodingEligibilityAttributeHostPolicy:
    host_policy_snapshot_digest = HOST_POLICY_DIGEST

    def __getattribute__(self, name: str) -> object:
        if name == "eligibility_for":
            raise RuntimeError("/Users/alice/private-repo sk-live-secret")
        return super().__getattribute__(name)


def test_query_closure_redacts_host_policy_snapshot_property_errors() -> None:
    profiles = _authorities()
    source = _source(profiles)

    with pytest.raises(BenefitClosureError) as exc:
        prepare_query_benefit_closure(
            source=source,
            observation=_observation(),
            profiles=profiles,
            policy=POLICY,
            host_policy=_ExplodingSnapshotHostPolicy(),
        )

    assert "alice" not in str(exc.value)
    assert "sk-live-secret" not in str(exc.value)
    assert exc.value.__suppress_context__ is True


@pytest.mark.parametrize("attribute", ["retrieve", "entry_claim"])
def test_query_closure_redacts_source_callable_attribute_errors(attribute: str) -> None:
    profiles = _authorities()
    source = _source(profiles)

    with pytest.raises(BenefitClosureError) as exc:
        prepare_query_benefit_closure(
            source=_ExplodingAttributeSource(source, attribute),  # type: ignore[arg-type]
            observation=_observation(),
            profiles=profiles,
            policy=POLICY,
            host_policy=_HostPolicy(),
        )

    assert "alice" not in str(exc.value)
    assert "sk-live-secret" not in str(exc.value)
    assert exc.value.__suppress_context__ is True


def test_query_closure_redacts_host_policy_callable_attribute_errors() -> None:
    profiles = _authorities()

    with pytest.raises(BenefitClosureError) as exc:
        prepare_query_benefit_closure(
            source=_source(profiles),
            observation=_observation(),
            profiles=profiles,
            policy=POLICY,
            host_policy=cast(
                QueryHostPolicyAuthority,
                _ExplodingEligibilityAttributeHostPolicy(),
            ),
        )

    assert "alice" not in str(exc.value)
    assert "sk-live-secret" not in str(exc.value)
    assert exc.value.__suppress_context__ is True


def test_query_closure_cleanup_note_does_not_expose_injected_exception_class() -> None:
    profiles = _authorities()
    source = _source(profiles, _candidate(capability_id="agent:unreviewed"))
    source.leak_cleanup_class = True

    with pytest.raises(BenefitClosureError) as exc:
        _prepare(profiles, source)

    notes = exc.value.__notes__ or []
    assert notes == ["CTX eligible-source cleanup also failed"]
    assert "Alice" not in " ".join(notes)


class _LyingSequence(Sequence[CapabilityCandidate]):
    def __init__(self, candidate: CapabilityCandidate, actual: int) -> None:
        self.candidate = candidate
        self.actual = actual

    def __len__(self) -> int:
        return 1

    @overload
    def __getitem__(self, index: int) -> CapabilityCandidate: ...

    @overload
    def __getitem__(self, index: slice) -> Sequence[CapabilityCandidate]: ...

    def __getitem__(
        self, index: int | slice
    ) -> CapabilityCandidate | Sequence[CapabilityCandidate]:
        if isinstance(index, slice):
            return ((self.candidate,) * self.actual)[index]
        if index < 0 or index >= self.actual:
            raise IndexError(index)
        return self.candidate

    def __iter__(self) -> Iterator[CapabilityCandidate]:
        return iter((self.candidate,) * self.actual)


def test_shared_candidate_bound_uses_realized_iteration_not_reported_length() -> None:
    with pytest.raises(PlannerValidationError, match="candidate pool limit"):
        normalize_candidate_pool(_LyingSequence(_candidate(), 513), _observation())


class _LyingAuthoritySequence(Sequence[ReviewedBenefitProfiles]):
    def __init__(self, authority: ReviewedBenefitProfiles, actual: int) -> None:
        self.authority = authority
        self.actual = actual

    def __len__(self) -> int:
        return 1

    @overload
    def __getitem__(self, index: int) -> ReviewedBenefitProfiles: ...

    @overload
    def __getitem__(self, index: slice) -> Sequence[ReviewedBenefitProfiles]: ...

    def __getitem__(
        self, index: int | slice
    ) -> ReviewedBenefitProfiles | Sequence[ReviewedBenefitProfiles]:
        if isinstance(index, slice):
            return ((self.authority,) * self.actual)[index]
        if index < 0 or index >= self.actual:
            raise IndexError(index)
        return self.authority

    def __iter__(self) -> Iterator[ReviewedBenefitProfiles]:
        return iter((self.authority,) * self.actual)


def test_reviewed_authority_bound_uses_realized_iteration_not_reported_length() -> None:
    with pytest.raises(BenefitClosureError, match="bounded item limit"):
        ReviewedBenefitAuthorities.create(_LyingAuthoritySequence(_authority(), 65))


def test_profile_path_failures_redact_sensitive_locations_from_traceback() -> None:
    sensitive = "/private/clients/acme-secret/reviewed-benefit.json"

    with pytest.raises(BenefitClosureError) as exc:
        load_reviewed_benefit_profiles(Path(sensitive), "a" * 64)

    rendered = "".join(
        traceback.format_exception(type(exc.value), exc.value, exc.value.__traceback__)
    )
    assert sensitive not in rendered
    assert "acme-secret" not in rendered
    assert exc.value.__cause__ is None
    assert exc.value.__suppress_context__ is True