Skip to content

Commit 8e153ce

Browse files
Pigbibicodex
andcommitted
test: compose P0 control contracts synthetically
Co-Authored-By: Codex <noreply@openai.com>
1 parent 38a8d12 commit 8e153ce

1 file changed

Lines changed: 269 additions & 0 deletions

File tree

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
from __future__ import annotations
2+
3+
import importlib.util
4+
import json
5+
import math
6+
import sys
7+
import unittest
8+
from pathlib import Path
9+
10+
ROOT = Path(__file__).resolve().parents[1]
11+
SCRIPTS = ROOT / "scripts"
12+
AS_OF = "2026-08-05T12:00:00Z"
13+
14+
15+
def _load_module(name: str):
16+
spec = importlib.util.spec_from_file_location(name, SCRIPTS / f"{name}.py")
17+
module = importlib.util.module_from_spec(spec)
18+
assert spec.loader is not None
19+
sys.modules[spec.name] = module
20+
spec.loader.exec_module(module)
21+
return module
22+
23+
24+
deployment_bundle_contract = _load_module("deployment_bundle_contract")
25+
activation_contract = _load_module("activation_contract")
26+
reconciliation_record_contract = _load_module("reconciliation_record_contract")
27+
28+
29+
class ControlContractCompositionTest(unittest.TestCase):
30+
@staticmethod
31+
def _sha(character: str) -> str:
32+
return character * 64
33+
34+
@staticmethod
35+
def _revision(character: str) -> str:
36+
return character * 40
37+
38+
def _bundle(self, *, platform: str = "interactive-brokers") -> dict[str, object]:
39+
bundle: dict[str, object] = {
40+
"schema": "qsl.deployment_bundle.v1",
41+
"bundle_id": f"bundle.synthetic.{platform}.20260805",
42+
"created_at": "2026-08-05T08:00:00Z",
43+
"digest_algorithm": "sha256",
44+
"strategy": {
45+
"id": "synthetic-strategy",
46+
"source_id": "synthetic-strategy-source",
47+
"revision": self._revision("a"),
48+
"artifact_sha256": self._sha("b"),
49+
},
50+
"profile": {
51+
"id": "synthetic-profile",
52+
"revision": self._revision("c"),
53+
"artifact_sha256": self._sha("d"),
54+
},
55+
"config": {
56+
"id": "synthetic-config",
57+
"revision": self._revision("e"),
58+
"artifact_sha256": self._sha("f"),
59+
},
60+
"evidence": {
61+
"id": "synthetic-evidence",
62+
"revision": self._revision("1"),
63+
"artifact_sha256": self._sha("2"),
64+
},
65+
"target": {"id": f"synthetic-{platform}", "platform_id": platform},
66+
"dependencies": {
67+
"qpk": {"id": "quant-platform-kit", "revision": self._revision("3"), "artifact_sha256": self._sha("4")},
68+
"strategy": {"id": "synthetic-strategy-source", "revision": self._revision("a"), "artifact_sha256": self._sha("5")},
69+
"pipeline": {"id": "synthetic-pipeline", "revision": self._revision("6"), "artifact_sha256": self._sha("7")},
70+
"platform": {"id": platform, "revision": self._revision("8"), "artifact_sha256": self._sha("9")},
71+
},
72+
}
73+
bundle["bundle_sha256"] = deployment_bundle_contract.calculate_bundle_sha256(bundle)
74+
return bundle
75+
76+
def _activation(self, bundle: dict[str, object]) -> dict[str, object]:
77+
activation: dict[str, object] = {
78+
"schema": "qsl.activation.v1",
79+
"activation_id": "activation.synthetic.disabled.20260805",
80+
"created_at": "2026-08-05T09:00:00Z",
81+
"digest_algorithm": "sha256",
82+
"contract_only": True,
83+
"deployment_bundle": {
84+
"schema": bundle["schema"],
85+
"bundle_id": bundle["bundle_id"],
86+
"bundle_sha256": bundle["bundle_sha256"],
87+
},
88+
"stage": "DISABLED",
89+
"effective_at": "2026-08-05T10:00:00Z",
90+
"expires_at": "2026-08-05T18:00:00Z",
91+
"human_authority": {
92+
"stage": "DISABLED",
93+
"authority_id": "synthetic-human-authority.disabled.20260805",
94+
"authority_version": "v1",
95+
"authority_receipt_sha256": self._sha("c"),
96+
},
97+
"target": {
98+
"platform": bundle["target"]["platform_id"],
99+
"repository": "QuantStrategyLab/InteractiveBrokersPlatform",
100+
"revision": bundle["dependencies"]["platform"]["revision"],
101+
"environment": "synthetic-disabled",
102+
"account_alias": "synthetic-research",
103+
"account_digest_sha256": self._sha("d"),
104+
},
105+
}
106+
activation["activation_sha256"] = activation_contract.calculate_activation_sha256(activation)
107+
return activation
108+
109+
def _composition(self) -> tuple[dict[str, object], dict[str, object], dict[str, object]]:
110+
bundle = self._bundle()
111+
activation = self._activation(bundle)
112+
record = reconciliation_record_contract.build_missing_record(
113+
record_id="reconciliation.synthetic.disabled.20260805",
114+
produced_at="2026-08-05T11:00:00Z",
115+
expires_at="2026-08-05T17:00:00Z",
116+
expected_bundle=bundle,
117+
expected_activation=activation,
118+
as_of=AS_OF,
119+
)
120+
return bundle, activation, record
121+
122+
def _validate(self, bundle, activation, record):
123+
deployment_bundle_contract.validate_bundle(bundle)
124+
activation_contract.validate_activation(activation, expected_bundle=bundle, as_of=AS_OF)
125+
return reconciliation_record_contract.validate_reconciliation_record(
126+
record,
127+
expected_bundle=bundle,
128+
expected_activation=activation,
129+
as_of=AS_OF,
130+
)
131+
132+
def test_synthetic_disabled_composition_is_exact_and_deterministic(self):
133+
bundle, activation, record = self._composition()
134+
validated = self._validate(bundle, activation, record)
135+
self.assertEqual(activation["stage"], "DISABLED")
136+
self.assertTrue(activation["contract_only"])
137+
self.assertEqual(record["status"], "MISSING")
138+
self.assertTrue(record["contract_only"])
139+
self.assertEqual(
140+
record["assertions"],
141+
{
142+
"apply_performed": False,
143+
"config_sync_performed": False,
144+
"runtime_mutation_performed": False,
145+
"runtime_active_verified": False,
146+
"fills_verified": False,
147+
"capital_use_verified": False,
148+
},
149+
)
150+
self.assertEqual(validated["deployment_bundle"]["bundle_sha256"], bundle["bundle_sha256"])
151+
self.assertEqual(validated["activation"]["activation_sha256"], activation["activation_sha256"])
152+
self.assertEqual(validated["expected_identity"]["deployment_bundle_sha256"], bundle["bundle_sha256"])
153+
self.assertEqual(validated["expected_identity"]["activation_sha256"], activation["activation_sha256"])
154+
self.assertEqual(
155+
deployment_bundle_contract.calculate_bundle_sha256(bundle),
156+
deployment_bundle_contract.calculate_bundle_sha256(dict(reversed(bundle.items()))),
157+
)
158+
self.assertEqual(
159+
activation_contract.calculate_activation_sha256(activation),
160+
activation_contract.calculate_activation_sha256(dict(reversed(activation.items()))),
161+
)
162+
self.assertEqual(
163+
reconciliation_record_contract.calculate_reconciliation_sha256(record),
164+
reconciliation_record_contract.calculate_reconciliation_sha256(dict(reversed(record.items()))),
165+
)
166+
rebuilt = self._composition()
167+
self.assertEqual(json.dumps((bundle, activation, record), sort_keys=True), json.dumps(rebuilt, sort_keys=True))
168+
169+
def test_bundle_mutations_fail_closed_through_activation_and_reconciliation_chain(self):
170+
for mutate in (
171+
lambda value: value["strategy"].update({"id": "mutated-strategy"}),
172+
lambda value: value["dependencies"]["strategy"].update({"artifact_sha256": self._sha("0")}),
173+
lambda value: value["evidence"].update({"artifact_sha256": self._sha("0")}),
174+
):
175+
with self.subTest(mutate=mutate):
176+
bundle, activation, record = self._composition()
177+
mutate(bundle)
178+
with self.assertRaises((activation_contract.ActivationValidationError, reconciliation_record_contract.ReconciliationValidationError)):
179+
activation_contract.validate_activation(activation, expected_bundle=bundle, as_of=AS_OF)
180+
with self.assertRaises(reconciliation_record_contract.ReconciliationValidationError):
181+
reconciliation_record_contract.validate_reconciliation_record(
182+
record, expected_bundle=bundle, expected_activation=activation, as_of=AS_OF
183+
)
184+
185+
def test_activation_mutations_fail_closed_through_reconciliation_chain(self):
186+
mutations = (
187+
lambda value: value.update({"activation_id": "activation.synthetic.other.20260805"}),
188+
lambda value: value.update({"activation_sha256": self._sha("0")}),
189+
lambda value: value["target"].update({"environment": "synthetic-other"}),
190+
lambda value: value.update({"stage": "PAPER_DRY_RUN"}),
191+
lambda value: value.update({"effective_at": "2026-08-05T11:30:00Z"}),
192+
)
193+
for mutate in mutations:
194+
with self.subTest(mutate=mutate):
195+
bundle, activation, record = self._composition()
196+
mutate(activation)
197+
if activation["stage"] != activation["human_authority"]["stage"]:
198+
activation["human_authority"]["stage"] = activation["stage"]
199+
if activation["activation_sha256"] != self._sha("0"):
200+
activation["activation_sha256"] = activation_contract.calculate_activation_sha256(activation)
201+
with self.assertRaises(reconciliation_record_contract.ReconciliationValidationError):
202+
reconciliation_record_contract.validate_reconciliation_record(
203+
record, expected_bundle=bundle, expected_activation=activation, as_of=AS_OF
204+
)
205+
206+
def test_non_missing_observer_and_comparison_material_is_rejected(self):
207+
for field, value in (
208+
("status", "MATCHED"),
209+
("status", "MISMATCHED"),
210+
("observer", {"id": "synthetic-observer"}),
211+
("observation", {"observed_at": "2026-08-05T11:00:00Z"}),
212+
("comparison", {"complete": False}),
213+
):
214+
with self.subTest(field=field):
215+
bundle, activation, record = self._composition()
216+
record[field] = value
217+
record["reconciliation_sha256"] = reconciliation_record_contract.calculate_reconciliation_sha256(record)
218+
with self.assertRaises(reconciliation_record_contract.ReconciliationValidationError):
219+
self._validate(bundle, activation, record)
220+
221+
def test_unsafe_material_and_invalid_synthetic_inputs_fail_closed(self):
222+
for key, value in (
223+
("unknown", {"nested": True}),
224+
("credential", "not-a-credential"),
225+
("raw_payload", {"synthetic": True}),
226+
("account", {"number": "00000000"}),
227+
("order", {"id": "synthetic-order"}),
228+
("fill", {"id": "synthetic-fill"}),
229+
("capital", {"amount": 1}),
230+
):
231+
with self.subTest(key=key):
232+
bundle, activation, record = self._composition()
233+
record["expected_identity"][key] = value
234+
record["reconciliation_sha256"] = reconciliation_record_contract.calculate_reconciliation_sha256(record)
235+
with self.assertRaises(reconciliation_record_contract.ReconciliationValidationError):
236+
self._validate(bundle, activation, record)
237+
238+
bundle, activation, record = self._composition()
239+
bundle["created_at"] = "2026-02-30T08:00:00Z"
240+
bundle["bundle_sha256"] = deployment_bundle_contract.calculate_bundle_sha256(bundle)
241+
with self.assertRaises(deployment_bundle_contract.BundleValidationError):
242+
deployment_bundle_contract.validate_bundle(bundle)
243+
244+
bundle, activation, record = self._composition()
245+
record["produced_at"] = "2026-02-30T11:00:00Z"
246+
record["reconciliation_sha256"] = reconciliation_record_contract.calculate_reconciliation_sha256(record)
247+
with self.assertRaises(reconciliation_record_contract.ReconciliationValidationError):
248+
self._validate(bundle, activation, record)
249+
250+
bundle, activation, record = self._composition()
251+
record["assertions"]["runtime_active_verified"] = math.nan
252+
with self.assertRaises(reconciliation_record_contract.ReconciliationValidationError):
253+
self._validate(bundle, activation, record)
254+
255+
bundle, activation, record = self._composition()
256+
other_bundle = self._bundle(platform="binance-platform")
257+
with self.assertRaises(reconciliation_record_contract.ReconciliationValidationError):
258+
reconciliation_record_contract.validate_reconciliation_record(
259+
record, expected_bundle=other_bundle, expected_activation=activation, as_of=AS_OF
260+
)
261+
262+
def test_serialized_synthetic_composition_has_no_credentials_or_business_payload(self):
263+
composition = json.dumps(self._composition(), sort_keys=True)
264+
for forbidden in ("credential", "secret", "token", "password", "raw_payload", "orders", "capital_value"):
265+
self.assertNotIn(forbidden, composition.lower())
266+
267+
268+
if __name__ == "__main__":
269+
unittest.main()

0 commit comments

Comments
 (0)