File size: 7,568 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
from __future__ import annotations

from hypothesis import given, settings, strategies as st

from ctx.engine.protocol import EngineEvent, HostAction, ScopeRef
from ctx.engine.reducer import MAX_ACTIVE_CAPABILITIES, reduce


NOW = "2026-08-01T12:00:00Z"
SCOPE = ScopeRef(
    tenant_id="tenant-property",
    workspace_id="workspace-property",
    repository_id="repository-property",
    session_id="session-property",
    exposure_id="exposure-property",
    host_context_id="host-property",
)


def _event(
    kind: str,
    revision: int,
    event_id: str,
    payload: dict[str, object],
) -> EngineEvent:
    return EngineEvent(
        event_id=event_id,
        kind=kind,
        scope=SCOPE,
        expected_revision=revision,
        occurred_at=NOW,
        payload=payload,
        correlation_id="plan-property",
        catalog_snapshot_digest="catalog-property",
        host_descriptor_digest="host-property-v1",
        engine_version="engine-property-v1",
        planner_version="planner-property-v1",
        policy_version="policy-property-v1",
        semantic_model_digest="semantic-model-property-v1",
        semantic_index_digest="semantic-index-property-v1",
        work_signature="work-signature-property-v1",
        random_seed=19,
    )


def _receipt(state, action: HostAction, event_id: str, *, applied: bool):
    payload: dict[str, object] = {
        "action_id": action.action_id,
        "action_kind": action.kind,
        "action_content_digest": action.content_digest,
        "action_precondition_revision": action.precondition_revision,
    }
    if applied:
        payload["verification"] = {"host_state": action.verification["expected_state"]}
        kind = "ActionApplied"
    else:
        payload["error"] = {"code": "generated-host-failure"}
        kind = "ActionFailed"
    return reduce(state, _event(kind, state.revision, event_id, payload))


@settings(max_examples=30, deadline=None)
@given(st.integers(min_value=0, max_value=12))
def test_requests_alone_never_activate_or_exceed_the_global_belt(size: int) -> None:
    state, _ = reduce(
        None,
        _event(
            "SessionStarted",
            0,
            "event-start",
            {"host_level": "activating"},
        ),
    )
    desired = [
        {
            "capability_id": f"skill:capability-{index}",
            "source_digest": f"digest-{index}",
            "lease_id": f"lease-{index}",
        }
        for index in range(size)
    ]

    state, transition = reduce(
        state,
        _event(
            "ReassessmentRequested",
            state.revision,
            "event-desired",
            {
                "owner_id": "owner-property",
                "desired_capabilities": desired,
            },
        ),
    )

    assert state.active_capability_ids == frozenset()
    assert (
        len([action for action in transition.actions if action.kind == "ActivateCapability"])
        <= MAX_ACTIVE_CAPABILITIES
    )


@settings(max_examples=8, deadline=None)
@given(
    child_shares_capability=st.booleans(),
    replacement_fails=st.booleans(),
    first_terminal_cleanup_fails=st.booleans(),
)
def test_generated_owner_receipt_replacement_and_terminal_interleavings(
    child_shares_capability: bool,
    replacement_fails: bool,
    first_terminal_cleanup_fails: bool,
) -> None:
    state, _ = reduce(
        None,
        _event(
            "SessionStarted",
            0,
            "sequence-start",
            {"host_level": "activating"},
        ),
    )
    original = [
        {
            "capability_id": f"skill:sequence-{index}",
            "source_digest": f"digest-{index}",
            "lease_id": f"lease-parent-{index}",
        }
        for index in range(5)
    ]
    state, transition = reduce(
        state,
        _event(
            "ReassessmentRequested",
            state.revision,
            "sequence-original",
            {"owner_id": "parent", "desired_capabilities": original},
        ),
    )
    counter = 0
    for action in transition.actions:
        state, _ = _receipt(
            state,
            action,
            f"sequence-initial-receipt-{counter}",
            applied=True,
        )
        counter += 1
        assert len(state.active_capability_ids) <= MAX_ACTIVE_CAPABILITIES

    if child_shares_capability:
        state, _ = reduce(
            state,
            _event(
                "ReassessmentRequested",
                state.revision,
                "sequence-child",
                {
                    "owner_id": "child",
                    "desired_capabilities": [
                        {
                            **original[1],
                            "lease_id": "lease-child-shared",
                        }
                    ],
                },
            ),
        )

    replacement = [
        *original[1:],
        {
            "capability_id": "skill:sequence-new",
            "source_digest": "digest-new",
            "lease_id": "lease-new",
        },
    ]
    state, transition = reduce(
        state,
        _event(
            "ReassessmentRequested",
            state.revision,
            "sequence-replacement",
            {"owner_id": "parent", "desired_capabilities": replacement},
        ),
    )
    removal = next(action for action in transition.actions if action.kind == "DeactivateCapability")
    state, transition = _receipt(
        state,
        removal,
        "sequence-old-inactive",
        applied=True,
    )
    addition = next(action for action in transition.actions if action.kind == "ActivateCapability")
    state, transition = _receipt(
        state,
        addition,
        "sequence-new-result",
        applied=not replacement_fails,
    )
    if replacement_fails:
        rollback = next(
            action for action in transition.actions if action.kind == "ActivateCapability"
        )
        state, _ = _receipt(
            state,
            rollback,
            "sequence-rollback-active",
            applied=True,
        )
    assert len(state.active_capability_ids) <= MAX_ACTIVE_CAPABILITIES

    state, transition = reduce(
        state,
        _event("SessionEnded", state.revision, "sequence-ended", {}),
    )
    initial_cleanup = next(
        action for action in transition.actions if action.kind == "DeactivateCapability"
    )
    if first_terminal_cleanup_fails:
        state, _ = _receipt(
            state,
            initial_cleanup,
            "sequence-cleanup-failed",
            applied=False,
        )
        state, transition = reduce(
            state,
            _event(
                "ReassessmentRequested",
                state.revision,
                "sequence-cleanup-retry",
                {"retry_failed_deactivations": [initial_cleanup.entity_id]},
            ),
        )
        cleanup: HostAction | None = next(
            action for action in transition.actions if action.kind == "DeactivateCapability"
        )
    else:
        cleanup = initial_cleanup

    while cleanup is not None:
        state, transition = _receipt(
            state,
            cleanup,
            f"sequence-cleanup-applied-{counter}",
            applied=True,
        )
        counter += 1
        assert len(state.active_capability_ids) <= MAX_ACTIVE_CAPABILITIES
        cleanup = next(
            (action for action in transition.actions if action.kind == "DeactivateCapability"),
            None,
        )

    assert state.active_capability_ids == frozenset()