-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_composition.py
More file actions
339 lines (306 loc) · 12 KB
/
Copy pathtest_composition.py
File metadata and controls
339 lines (306 loc) · 12 KB
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
"""Verify session.check_and_record equals compose_decisions on identical inputs."""
from __future__ import annotations
from dataclasses import dataclass
from itertools import product
import pytest
import vibap.proxy as proxy_module
from vibap.passport import MissionPassport, issue_passport
from vibap.policy_backend import (
PolicyDecision,
clear_registry,
compose_decisions,
get_backend,
register_backend,
timed_evaluate,
)
@dataclass
class _FixedBackend:
name: str
returned: PolicyDecision
def evaluate(self, **kwargs) -> PolicyDecision:
return self.returned
def _pd(decision: str, *, backend: str, label: str = "", reasons: tuple[str, ...] = ()) -> PolicyDecision:
return PolicyDecision(
backend=backend,
label=label,
decision=decision,
reasons=reasons,
)
def _spec(pd: PolicyDecision) -> dict[str, str]:
return {
"backend": pd.backend,
"label": pd.label,
"policy_inline": "",
"policy_sha256": "x",
}
def _expected_decision(decisions: list[PolicyDecision]) -> proxy_module.Decision:
final, _ = compose_decisions(decisions)
return proxy_module.Decision.PERMIT if final == "Allow" else proxy_module.Decision.DENY
def _expected_event_backends(
native_decision: PolicyDecision,
extra_decisions: list[PolicyDecision],
) -> list[str]:
"""Post Phase-3 external-review-G-CRITICAL fix (2026-04-17): the proxy no longer
short-circuits on first Deny. Every registered backend is evaluated
on every call so the receipt audit trail is complete; deny-wins
semantics are handled internally by compose_decisions."""
if not extra_decisions:
return []
backends = ["native_claims"]
for pd in extra_decisions:
backends.append(pd.backend)
return backends
def _assert_matches_compose(
*,
decision: proxy_module.Decision,
reason: str,
event,
native_decision: PolicyDecision,
extra_decisions: list[PolicyDecision],
) -> None:
all_decisions = [native_decision, *extra_decisions]
final, first_denier = compose_decisions(all_decisions)
assert decision == _expected_decision(all_decisions)
assert [pd["backend"] for pd in event.policy_decisions] == _expected_event_backends(
native_decision,
extra_decisions,
)
if final == "Allow":
assert reason == "within scope"
return
if first_denier is None:
assert "fail-closed" in reason
return
if first_denier.backend == "native":
assert all(r in reason for r in first_denier.reasons)
return
assert (first_denier.label or first_denier.backend) in reason
assert all(r in reason for r in first_denier.reasons)
@pytest.fixture(autouse=True)
def _clean_registry():
clear_registry()
yield
clear_registry()
def _run_case(
proxy,
private_key,
*,
tool_name: str,
arguments: dict[str, str],
native_decision: PolicyDecision,
extra_decisions: list[PolicyDecision],
) -> tuple[proxy_module.Decision, str, object]:
register_backend(_FixedBackend(name="native", returned=native_decision))
for pd in extra_decisions:
register_backend(_FixedBackend(name=pd.backend, returned=pd))
mission = MissionPassport(
agent_id="composition-test",
mission="composition-test",
allowed_tools=[tool_name],
forbidden_tools=[],
resource_scope=[],
max_tool_calls=10,
max_duration_s=60,
additional_policies=[_spec(pd) for pd in extra_decisions],
)
session = proxy.start_session(issue_passport(mission, private_key, ttl_s=60))
return session.check_and_record(tool_name, arguments)
class TestCompositionEquivalence:
def test_proxy_output_equals_compose_decisions_on_allow_case(self, proxy, private_key):
native = _pd("Allow", backend="native", reasons=("within scope",))
decision, reason, event = _run_case(
proxy,
private_key,
tool_name="read_file",
arguments={"path": "x"},
native_decision=native,
extra_decisions=[],
)
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[],
)
def test_proxy_output_equals_compose_decisions_on_native_deny(self, proxy, private_key):
native = _pd("Deny", backend="native", reasons=("native veto",))
decision, reason, event = _run_case(
proxy,
private_key,
tool_name="read_file",
arguments={"path": "x"},
native_decision=native,
extra_decisions=[],
)
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[],
)
def test_proxy_output_equals_compose_decisions_on_cedar_deny(self, proxy, private_key):
native = _pd("Allow", backend="native", reasons=("within scope",))
cedar = _pd("Deny", backend="cedar", label="security_team", reasons=("cedar deny",))
decision, reason, event = _run_case(
proxy,
private_key,
tool_name="read_file",
arguments={"path": "x"},
native_decision=native,
extra_decisions=[cedar],
)
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[cedar],
)
def test_proxy_output_equals_compose_decisions_on_forbid_rules_deny(self, proxy, private_key):
native = _pd("Allow", backend="native", reasons=("within scope",))
forbid = _pd("Deny", backend="forbid_rules", label="compliance", reasons=("rule matched",))
decision, reason, event = _run_case(
proxy,
private_key,
tool_name="read_file",
arguments={"path": "x"},
native_decision=native,
extra_decisions=[forbid],
)
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[forbid],
)
def test_proxy_output_equals_compose_decisions_on_all_abstain_fails_closed(self, proxy, private_key):
native = _pd("Abstain", backend="native")
abstain = _pd("Abstain", backend="cedar", label="security_team")
decision, reason, event = _run_case(
proxy,
private_key,
tool_name="read_file",
arguments={"path": "x"},
native_decision=native,
extra_decisions=[abstain],
)
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[abstain],
)
def test_proxy_output_equals_compose_decisions_on_native_allow_cedar_deny(self, proxy, private_key):
native = _pd("Allow", backend="native", reasons=("within scope",))
cedar = _pd("Deny", backend="cedar", label="security_team", reasons=("explicit forbid",))
decision, reason, event = _run_case(
proxy,
private_key,
tool_name="read_file",
arguments={"path": "x"},
native_decision=native,
extra_decisions=[cedar],
)
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[cedar],
)
def test_proxy_output_equals_compose_decisions_on_deny_precedence_ordering(self, proxy, private_key):
native = _pd("Allow", backend="native", reasons=("within scope",))
allow = _pd("Allow", backend="cedar", label="security_team")
first_deny = _pd("Deny", backend="forbid_rules", label="compliance", reasons=("first deny",))
second_deny = _pd("Deny", backend="extra", label="later", reasons=("second deny",))
decision, reason, event = _run_case(
proxy,
private_key,
tool_name="read_file",
arguments={"path": "x"},
native_decision=native,
extra_decisions=[allow, first_deny, second_deny],
)
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[allow, first_deny, second_deny],
)
def test_proxy_output_equals_compose_decisions_on_budget_exhaustion(self, proxy, private_key):
mission = MissionPassport(
agent_id="budget-composition",
mission="budget-composition",
allowed_tools=["read_file"],
forbidden_tools=[],
resource_scope=[],
max_tool_calls=1,
max_duration_s=60,
)
session = proxy.start_session(issue_passport(mission, private_key, ttl_s=60))
first_decision, _, _ = session.check_and_record("read_file", {"path": "x"})
assert first_decision == proxy_module.Decision.PERMIT
tool_name = "read_file"
arguments = {"path": "x"}
target = proxy_module._policy_event_target(tool_name, arguments)
action_class = proxy_module._policy_action_class(tool_name)
resource_family = proxy_module._policy_resource_family(tool_name, arguments, target, action_class)
side_effect_class = proxy_module._policy_side_effect_class(tool_name, action_class, resource_family)
native = timed_evaluate(
get_backend("native"),
tool_name=tool_name,
arguments=arguments,
principal=str(session.passport_claims.get("sub", "unknown")),
target=target,
context={
"passport": dict(session.passport_claims),
"session": {
"tool_call_count": session.tool_call_count,
"tool_call_count_by_class": dict(session.tool_call_count_by_class),
"side_effect_counts": dict(session.tool_call_count_by_class),
"delegated_budget_reserved": session.delegated_budget_reserved,
"delegation_depth": len(session.passport_claims.get("delegation_chain", []) or []),
"elapsed_s": session.elapsed_s,
"cwd": session.passport_claims.get("cwd"),
},
"elapsed_s": session.elapsed_s,
"tool_call_count": session.tool_call_count,
"action_class": action_class,
"side_effect_class": side_effect_class,
},
policy_spec={},
)
decision, reason, event = session.check_and_record(tool_name, arguments)
assert native.decision == "Deny"
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[],
)
def test_proxy_output_equals_compose_decisions_over_z3_property_inputs(self, proxy, private_key):
for native_label, b1_label, b2_label in product(("Allow", "Deny", "Abstain"), repeat=3):
native = _pd(native_label, backend="native", reasons=(native_label.lower(),) if native_label != "Abstain" else ())
b1 = _pd(b1_label, backend="b1", label="L1", reasons=(b1_label.lower(),) if b1_label != "Abstain" else ())
b2 = _pd(b2_label, backend="b2", label="L2", reasons=(b2_label.lower(),) if b2_label != "Abstain" else ())
decision, reason, event = _run_case(
proxy,
private_key,
tool_name="read_file",
arguments={"path": "x"},
native_decision=native,
extra_decisions=[b1, b2],
)
_assert_matches_compose(
decision=decision,
reason=reason,
event=event,
native_decision=native,
extra_decisions=[b1, b2],
)