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

import copy
import hashlib
import json
import os
import pickle
import threading
from concurrent.futures import ThreadPoolExecutor
from dataclasses import fields, replace
from datetime import UTC, datetime
from pathlib import Path

import pytest

import ctx.runtime.activated_skill_exposure as exposure_module
from ctx.core.install_policy_store import persist_install_policy
from ctx.engine.content import AuthorizedMaterial, MaterialDescriptor, PreparedCapabilityContent
from ctx.engine.engine import (
    CtxEngine,
    CtxEngineError,
    _PromptContextMaterialPermit,
    _PromptContextMaterialRoutePermit,
)
from ctx.engine.installation import InstallConsentPolicy
from ctx.engine.planning_v3 import CapabilityPlanSelectionV3, LoadPlanningAuthority
from ctx.engine.protocol import (
    PROMPT_CONTEXT_ACTION_PAYLOAD_SCHEMA_V1,
    PROMPT_CONTEXT_RECEIPT_SCHEMA_V1,
    HostAction,
    ScopeRef,
)
from ctx.engine.reducer import INSTALLATION_REDUCER_VERSION
from ctx.engine.replay import DefaultReplayInputFactory
from ctx.engine.state import CapabilityStateV3
from ctx.engine.store import SQLiteEngineStore, StreamId
from ctx.runtime.activated_skill_exposure import (
    ActivatedSkillExposureError,
    ActivatedSkillExposurePreparation,
    prepare_activated_skill_exposure,
)
from ctx.runtime.release_material import RELEASE_INSTALL_SKILL_ID
from ctx.runtime.production_catalog import open_release_pinned_query_catalog
from ctx.runtime.release_skill_dispatcher import (
    ReleaseSkillInstallRequest,
    _scope,
    dispatch_release_skill_install,
)
from ctx.runtime.release_skill_lifecycle import activate_installed_release_skill


NOW = "2026-08-02T12:30:00Z"
TRUSTED_NOW = datetime(2026, 8, 2, 12, 30, tzinfo=UTC)


def _digest(value: object) -> str:
    encoded = (
        value.encode("utf-8")
        if isinstance(value, str)
        else json.dumps(
            value,
            ensure_ascii=True,
            allow_nan=False,
            separators=(",", ":"),
            sort_keys=True,
        ).encode("utf-8")
    )
    return hashlib.sha256(encoded).hexdigest()


def _request(tmp_path: Path) -> ReleaseSkillInstallRequest:
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    state_root = tmp_path / "state"
    state_root.mkdir(mode=0o700)
    skill_root = tmp_path / "skills"
    skill_root.mkdir(mode=0o700)
    request = ReleaseSkillInstallRequest(
        host_context_id="host-neutral-test",
        host_identity_digest=_digest("host-neutral-test"),
        task="Repair nested Python context-manager state restoration",
        language="Python",
        session_id="release-install-session",
        workspace=workspace,
        journal_path=state_root / "engine.sqlite3",
        benefit_audit_path=state_root / "benefit.sqlite3",
        policy_store_root=state_root / "install-policy",
        skill_store_root=skill_root,
        occurred_at="2026-08-02T12:00:00Z",
    )
    persist_install_policy(
        InstallConsentPolicy(skill_mode="preapproved-auto"),
        request.policy_store_root,
    )
    installed = dispatch_release_skill_install(
        request,
        trusted_utc_now=lambda: TRUSTED_NOW,
    )
    assert installed.status == "installed"
    return request


def _activated_preparation(
    tmp_path: Path,
) -> tuple[ReleaseSkillInstallRequest, ActivatedSkillExposurePreparation]:
    request = _request(tmp_path)
    activation = activate_installed_release_skill(
        request,
        trusted_utc_now=lambda: TRUSTED_NOW,
    )
    return request, prepare_activated_skill_exposure(
        request=request,
        activation_evidence=activation,
    )


def _selection_and_action(
    request: ReleaseSkillInstallRequest,
    *,
    catalog_planning_authority: bool = False,
) -> tuple[CapabilityPlanSelectionV3, HostAction]:
    engine = CtxEngine(
        store=SQLiteEngineStore(request.journal_path),
        replay_factory=DefaultReplayInputFactory(reducer_version=INSTALLATION_REDUCER_VERSION),
    )
    snapshot = engine.snapshot(_scope(request))
    assert snapshot.state is not None
    capability = snapshot.state.capability(RELEASE_INSTALL_SKILL_ID)
    assert isinstance(capability, CapabilityStateV3)
    authorized = capability.current_authorized_material
    assert authorized is not None
    original = capability.selection.selection
    presentation = replace(
        original.presentation,
        actionability="load",
        install_descriptor_digest=None,
        install_plan_digest=None,
    )
    planning_material = authorized
    if catalog_planning_authority:
        planning_material = AuthorizedMaterial.from_catalog(
            catalog_identity_digest=original.catalog_identity.identity_digest,
            descriptor=MaterialDescriptor.create(
                capability_id=capability.capability_id,
                kind=capability.kind,
                actionability="load",
                content_sha256=capability.material_identity.content_sha256,
                content_bytes=capability.material_identity.content_bytes,
                estimated_tokens=max(
                    1,
                    (capability.material_identity.content_bytes + 3) // 4,
                ),
                provenance_digest=_digest("reviewed-load-material-snapshot"),
                material_identity_digest=capability.material_identity.identity_digest,
            ),
        )
    selection = CapabilityPlanSelectionV3(
        presentation=presentation,
        catalog_identity=original.catalog_identity,
        benefit=original.benefit,
        authority=LoadPlanningAuthority(material=planning_material),
    )
    row = {
        "authorized_material": planning_material.to_dict(),
        "capability_id": capability.capability_id,
        "capability_kind": capability.kind,
        "catalog_identity": capability.catalog_identity.to_dict(),
        "material_identity": capability.material_identity.to_dict(),
        "source_digest": presentation.source_digest,
    }
    plan_digest = _digest("logical-prompt-plan")
    presentation_action_id = "logical-prompt-presentation"
    presentation_action_content_digest = _digest("logical-prompt-presentation")
    source_digest = _digest(
        {
            "capabilities": [row],
            "execution_intent": "activate",
            "plan_digest": plan_digest,
            "presentation_action_content_digest": presentation_action_content_digest,
            "presentation_action_id": presentation_action_id,
            "schema": "ctx.prompt-context-bundle-v1",
        }
    )
    scope = ScopeRef(
        tenant_id="local",
        workspace_id=_digest(str(request.workspace.resolve())),
        repository_id=_digest(str(request.workspace.resolve())),
        session_id="later-logical-prompt",
        exposure_id="later-logical-prompt-exposure",
        host_context_id="host-neutral-test",
    )
    action = HostAction(
        action_id="later-logical-prompt-context",
        kind="PreparePromptContext",
        scope=scope,
        precondition_revision=2,
        payload={
            "capabilities": (row,),
            "execution_intent": "activate",
            "plan_digest": plan_digest,
            "presentation_action_content_digest": presentation_action_content_digest,
            "presentation_action_id": presentation_action_id,
            "schema": PROMPT_CONTEXT_ACTION_PAYLOAD_SCHEMA_V1,
        },
        source_digest=source_digest,
        plan_id=plan_digest,
        catalog_snapshot_id=capability.catalog_snapshot_id,
        lease_id="later-logical-prompt-lease",
        expires_at="2099-08-02T12:30:00Z",
        required_host_feature="prompt-context",
        verification={
            "receipt_required": True,
            "expected_state": "prompt-context-prepared",
            "receipt_schema": PROMPT_CONTEXT_RECEIPT_SCHEMA_V1,
        },
        rollback={
            "kind": "discard-prompt-context",
            "exposure_id": scope.exposure_id,
        },
    )
    return selection, action


def _query_engine(request: ReleaseSkillInstallRequest) -> CtxEngine:
    return CtxEngine(
        store=SQLiteEngineStore(request.journal_path),
        replay_factory=DefaultReplayInputFactory(reducer_version=INSTALLATION_REDUCER_VERSION),
    )


def _route_authority(
    request: ReleaseSkillInstallRequest,
    action: HostAction,
    selections: tuple[CapabilityPlanSelectionV3, ...],
    *,
    expected_catalog_snapshot_digest: str | None = None,
) -> _PromptContextMaterialRoutePermit:
    catalog_digest = expected_catalog_snapshot_digest or action.catalog_snapshot_id or ""
    authority = _query_engine(request)._issue_prompt_context_material_permit(  # noqa: SLF001
        action,
        selections,
        expected_catalog_snapshot_digest=catalog_digest,
    )
    routes = authority._consume_and_issue_routes(  # noqa: SLF001
        action=action,
        selections=selections,
        expected_catalog_snapshot_digest=catalog_digest,
        external_capability_ids=frozenset({RELEASE_INSTALL_SKILL_ID}),
    )
    return routes[RELEASE_INSTALL_SKILL_ID]


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_prepares_one_authority_bound_material_once(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    request, preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request)
    calls: list[tuple[str, tuple[str, ...], str]] = []

    def authorize(
        _engine: CtxEngine,
        prompt_action: HostAction,
        selections: tuple[CapabilityPlanSelectionV3, ...],
        *,
        expected_catalog_snapshot_digest: str,
    ) -> None:
        calls.append(
            (
                prompt_action.content_digest,
                tuple(item.presentation.capability_id for item in selections),
                expected_catalog_snapshot_digest,
            )
        )

    monkeypatch.setattr(CtxEngine, "authorize_prompt_context", authorize)
    route_authority = _route_authority(request, action, (selection,))
    lifecycle_store = SQLiteEngineStore(request.journal_path)
    lifecycle_stream = StreamId.from_scope(_scope(request))
    records_before = tuple(lifecycle_store.records(lifecycle_stream))

    content = preparation.material_permit.prepare_prompt_context_once(
        action=action,
        selection=selection,
        expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
        route_authority=route_authority,
    )

    assert isinstance(content, PreparedCapabilityContent)
    assert content.capability_id == RELEASE_INSTALL_SKILL_ID
    assert hashlib.sha256(content.content.encode("utf-8")).hexdigest() == content.content_sha256
    assert len(content.content.encode("utf-8")) == content.content_bytes
    assert tuple(lifecycle_store.records(lifecycle_stream)) == records_before
    assert calls == [
        (
            action.content_digest,
            (RELEASE_INSTALL_SKILL_ID,),
            action.catalog_snapshot_id,
        )
    ]
    with pytest.raises(ActivatedSkillExposureError, match="already consumed"):
        preparation.material_permit.prepare_prompt_context_once(
            action=action,
            selection=selection,
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            route_authority=route_authority,
        )


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_routes_reviewed_catalog_selection_to_exact_cas(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    request, preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(
        request,
        catalog_planning_authority=True,
    )
    assert isinstance(selection.authority, LoadPlanningAuthority)
    selected_material = selection.authority.material
    assert selected_material.origin == "catalog"

    monkeypatch.setattr(
        CtxEngine,
        "authorize_prompt_context",
        lambda _engine, _action, _selections, **_kwargs: None,
    )

    content = preparation.material_permit.prepare_prompt_context_once(
        action=action,
        selection=selection,
        expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
        route_authority=_route_authority(request, action, (selection,)),
    )

    assert content.capability_id == RELEASE_INSTALL_SKILL_ID
    assert content.content_sha256 == request.skill_store_root.joinpath(content.content_sha256).name


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_authorizes_before_reading_material(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    request, preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request)

    def forbidden_rederivation(**_kwargs: object) -> object:
        raise AssertionError("material was read before route authorization")

    monkeypatch.setattr(
        exposure_module,
        "_rederive_under_material_lock",
        forbidden_rederivation,
    )
    monkeypatch.setattr(
        CtxEngine,
        "authorize_prompt_context",
        lambda *_args, **_kwargs: None,
    )
    route_authority = _route_authority(
        request,
        action,
        (selection,),
        expected_catalog_snapshot_digest="a" * 64,
    )

    with pytest.raises(ActivatedSkillExposureError, match="material route rejected"):
        preparation.material_permit.prepare_prompt_context_once(
            action=action,
            selection=selection,
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            route_authority=route_authority,
        )


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_permit_is_atomic_under_concurrency(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    request, preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request)
    calls: list[str] = []
    entered = threading.Event()
    release = threading.Event()

    monkeypatch.setattr(
        CtxEngine,
        "authorize_prompt_context",
        lambda *_args, **_kwargs: None,
    )
    route_authority = _route_authority(request, action, (selection,))
    original_rederive = exposure_module._rederive_under_material_lock

    def blocked_rederive(**kwargs: object):  # type: ignore[no-untyped-def]
        calls.append(action.content_digest)
        entered.set()
        assert release.wait(timeout=10)
        return original_rederive(**kwargs)  # type: ignore[arg-type]

    monkeypatch.setattr(exposure_module, "_rederive_under_material_lock", blocked_rederive)

    def consume() -> PreparedCapabilityContent:
        return preparation.material_permit.prepare_prompt_context_once(
            action=action,
            selection=selection,
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            route_authority=route_authority,
        )

    with ThreadPoolExecutor(max_workers=2) as pool:
        first = pool.submit(consume)
        assert entered.wait(timeout=10)
        second = pool.submit(consume)
        with pytest.raises(ActivatedSkillExposureError, match="already consumed"):
            second.result(timeout=10)
        release.set()
        prepared = first.result(timeout=10)

    assert prepared.capability_id == RELEASE_INSTALL_SKILL_ID
    assert len(calls) == 1


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_is_digest_only_and_non_serializable(tmp_path: Path) -> None:
    request, preparation = _activated_preparation(tmp_path)

    assert {field.name for field in fields(preparation)} == {
        "activation_evidence_digest",
        "installed_lineage_digest",
        "material_identity_digest",
        "material_permit",
        "preparation_digest",
        "skill_cas_root_identity_digest",
    }
    rendered = repr(preparation)
    assert str(request.journal_path) not in rendered
    assert str(request.skill_store_root) not in rendered
    assert "SKILL.md" not in rendered
    assert not hasattr(preparation.material_permit, "emit")
    with pytest.raises(TypeError):
        copy.copy(preparation.material_permit)
    with pytest.raises(TypeError):
        pickle.dumps(preparation.material_permit)


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_rejects_arbitrary_callable_authority(
    tmp_path: Path,
) -> None:
    request, preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request)

    class FakeAuthority:
        def authorize_prompt_context(self, *_args: object, **_kwargs: object) -> None:
            return None

    with pytest.raises(TypeError, match="engine-issued material route"):
        preparation.material_permit.prepare_prompt_context_once(
            action=action,
            selection=selection,
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            route_authority=FakeAuthority(),  # type: ignore[arg-type]
        )


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_release_catalog_rejects_callable_authority_and_external_source(
    tmp_path: Path,
) -> None:
    request, _preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request, catalog_planning_authority=True)
    catalog = open_release_pinned_query_catalog()

    class FakeAuthority:
        def authorize_prompt_context(self, *_args: object, **_kwargs: object) -> None:
            return None

    with pytest.raises(TypeError, match="engine-issued material authority"):
        catalog.prepare_prompt_context(
            action,
            (selection,),
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            authority=FakeAuthority(),  # type: ignore[arg-type]
        )
    forged = object.__new__(_PromptContextMaterialPermit)
    with pytest.raises(TypeError, match="exact activated-skill permit"):
        catalog.prepare_prompt_context(
            action,
            (selection,),
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            authority=forged,
            external_material_source=lambda: None,
        )
    catalog.close()


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_engine_bundle_permit_cannot_be_replayed_through_reopened_catalog(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    request, _preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request)
    selections = (selection,)
    catalog_digest = action.catalog_snapshot_id or ""
    monkeypatch.setattr(
        CtxEngine,
        "authorize_prompt_context",
        lambda *_args, **_kwargs: None,
    )
    authority = _query_engine(request)._issue_prompt_context_material_permit(  # noqa: SLF001
        action,
        selections,
        expected_catalog_snapshot_digest=catalog_digest,
    )
    authority._consume_and_issue_routes(  # noqa: SLF001
        action=action,
        selections=selections,
        expected_catalog_snapshot_digest=catalog_digest,
        external_capability_ids=frozenset({RELEASE_INSTALL_SKILL_ID}),
    )
    reopened = open_release_pinned_query_catalog()
    reopened.close()

    with pytest.raises(CtxEngineError, match="already consumed"):
        authority._consume_and_issue_routes(  # noqa: SLF001
            action=action,
            selections=selections,
            expected_catalog_snapshot_digest=catalog_digest,
            external_capability_ids=frozenset({RELEASE_INSTALL_SKILL_ID}),
        )


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_does_not_recreate_missing_journal(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    request, preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request)
    monkeypatch.setattr(
        CtxEngine,
        "authorize_prompt_context",
        lambda *_args, **_kwargs: None,
    )
    route_authority = _route_authority(request, action, (selection,))
    request.journal_path.unlink()
    state_after_unlink = frozenset(request.journal_path.parent.iterdir())

    with pytest.raises(ActivatedSkillExposureError, match="could not be rederived"):
        preparation.material_permit.prepare_prompt_context_once(
            action=action,
            selection=selection,
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            route_authority=route_authority,
        )

    assert not request.journal_path.exists()
    assert frozenset(request.journal_path.parent.iterdir()) == state_after_unlink


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_sanitizes_unexpected_consumption_failure(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    request, preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request)
    secret = f"{request.journal_path}:token-secret"
    monkeypatch.setattr(
        CtxEngine,
        "authorize_prompt_context",
        lambda *_args, **_kwargs: None,
    )

    def fail(**_kwargs: object) -> object:
        raise RuntimeError(secret)

    monkeypatch.setattr(exposure_module, "_rederive_under_material_lock", fail)
    with pytest.raises(ActivatedSkillExposureError) as captured:
        preparation.material_permit.prepare_prompt_context_once(
            action=action,
            selection=selection,
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            route_authority=_route_authority(request, action, (selection,)),
        )

    assert secret not in str(captured.value)
    assert captured.value.__cause__ is None


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_reverifies_cas_at_permit_consumption(
    tmp_path: Path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    request, preparation = _activated_preparation(tmp_path)
    selection, action = _selection_and_action(request)
    monkeypatch.setattr(
        CtxEngine,
        "authorize_prompt_context",
        lambda *_args, **_kwargs: None,
    )
    route_authority = _route_authority(request, action, (selection,))
    assert isinstance(selection.authority, LoadPlanningAuthority)
    material = selection.authority.material
    assert material.installed_material_lineage is not None
    engine = CtxEngine(
        store=SQLiteEngineStore(request.journal_path),
        replay_factory=DefaultReplayInputFactory(reducer_version=INSTALLATION_REDUCER_VERSION),
    )
    snapshot = engine.snapshot(_scope(request))
    assert snapshot.state is not None
    capability = snapshot.state.capability(RELEASE_INSTALL_SKILL_ID)
    assert isinstance(capability, CapabilityStateV3)
    target = request.skill_store_root / capability.material_identity.content_sha256
    target.write_bytes(b"x" * capability.material_identity.content_bytes)

    with pytest.raises(ActivatedSkillExposureError, match="exact installed UTF-8 material"):
        preparation.material_permit.prepare_prompt_context_once(
            action=action,
            selection=selection,
            expected_catalog_snapshot_digest=action.catalog_snapshot_id or "",
            route_authority=route_authority,
        )


@pytest.mark.skipif(os.name == "nt", reason="installed skill CAS is POSIX-only")
def test_activated_skill_exposure_rejects_nonmatching_activation_evidence(
    tmp_path: Path,
) -> None:
    first = tmp_path / "first"
    first.mkdir()
    second = tmp_path / "second"
    second.mkdir()
    request = _request(first)
    other_request = _request(second)
    activate_installed_release_skill(
        request,
        trusted_utc_now=lambda: TRUSTED_NOW,
    )
    other_activation = activate_installed_release_skill(
        other_request,
        trusted_utc_now=lambda: TRUSTED_NOW,
    )

    with pytest.raises(ActivatedSkillExposureError, match="activation evidence"):
        prepare_activated_skill_exposure(
            request=request,
            activation_evidence=other_activation,
        )