spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / tests / test_canary_contract_golden.rs
| 1 | //! Golden tests for the frozen contracts of the evidence-driven RSI cut |
| 2 | //! (docs/plans/2026-10-12-evidence-driven-rsi-cut.md, "Narrow contract freeze"). |
| 3 | //! |
| 4 | //! Three families are pinned: (1) the canonical canary observation encoding and digests of the |
| 5 | //! new version 2 records, (2) the existing signed evaluation receipt bytes, digest and |
| 6 | //! verification, (3) the existing ledger record encoding, hash and validation. A change to any |
| 7 | //! pinned value is a format change and needs a new version, never an edit of the pin. |
| 8 | //! |
| 9 | //! 2026-10-26: family (2) gained the aien-protocols#59 framed digest contract (plan digest v2, |
| 10 | //! receipt protocol_version 3). The protocol_version 1 receipt pins it replaced are retained, |
| 11 | //! unedited and superseded, in evidence/protocol-receipt-v1-superseded-2026-10-26/. |
| 12 | |
| 13 | use p256::ecdsa::SigningKey; |
| 14 | use sha2::{Digest, Sha256}; |
| 15 | use spark_rsi::canary::observation::ObservationInput; |
| 16 | use spark_rsi::canary::{ |
| 17 | observation_id, CanaryAdapterManifest, CanaryObservationV2, CanaryRequestSpec, |
| 18 | CanaryWorkloadManifest, CandidateIdentity, LogHeader, Nondeterminism, ObservationLogWriter, |
| 19 | Provenance, SealedObservationLog, VerifiedStatus, CANARY_PROTOCOL_VERSION, |
| 20 | }; |
| 21 | use spark_rsi::evaluator::layers::LayerResult; |
| 22 | use spark_rsi::evaluator::{EvaluationMetricsSummary, EvaluationReceipt, ReceiptBindingV2}; |
| 23 | use spark_rsi::ledger::{compute_merkle_root, BlockType, LedgerBlock}; |
| 24 | use spark_rsi::supervisor::ipc::{SupervisorMessage, WorkerMessage}; |
| 25 | |
| 26 | fn sha(bytes: &[u8]) -> String { |
| 27 | hex::encode(Sha256::digest(bytes)) |
| 28 | } |
| 29 | |
| 30 | // ------------------------------------------------------------------ pinned values |
| 31 | |
| 32 | const EXPECTED_WORKLOAD_CANONICAL: &str = "{\"protocol_version\":1,\"requests\":[{\"deadline_ms\":2000,\"expected_output\":\"pong\",\"input\":\"ping\",\"nondeterminism\":\"exact\",\"request_id\":\"req-001\"},{\"deadline_ms\":2000,\"expected_output\":\"5\",\"input\":\"add 2 3\",\"nondeterminism\":\"exact\",\"request_id\":\"req-002\"}],\"workload_id\":\"golden-v1\",\"workload_version\":1}"; |
| 33 | const EXPECTED_WORKLOAD_DIGEST: &str = |
| 34 | "9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870"; |
| 35 | const EXPECTED_REQUEST_001_DIGEST: &str = |
| 36 | "f4b3d03bdbef5edb951f4a5a314989098226748eb720e81c382fbebd86e18017"; |
| 37 | const EXPECTED_ADAPTER_DIGEST: &str = |
| 38 | "4bcfa2c308cc84ea007827def1dbd5d8b5d038a3c676030374ff07621a5320e6"; |
| 39 | const EXPECTED_IDENTITY_BINDING_DIGEST: &str = |
| 40 | "dabd4116b1c49c42716973eb65110e439601396086adc2d35c9cb169e429360a"; |
| 41 | const EXPECTED_OBSERVATION_ID: &str = |
| 42 | "4b269fce18d83d73147676b7358c998a548a99ceebed02816d37a059926b8181"; |
| 43 | const EXPECTED_OBSERVATION_CANONICAL: &str = "{\"candidate_executable_digest\":\"86b30dc0591deb3800c70226d5ed326e20f01f9f8288036acdc7b5bd4c86af75\",\"elapsed_us\":1234,\"generation_id\":\"gen-golden-0001\",\"observation_id\":\"4b269fce18d83d73147676b7358c998a548a99ceebed02816d37a059926b8181\",\"observed_at_ms\":1700000000000,\"provenance\":\"measured\",\"record_version\":2,\"request_digest\":\"f4b3d03bdbef5edb951f4a5a314989098226748eb720e81c382fbebd86e18017\",\"request_id\":\"req-001\",\"result_digest\":\"9795c5ff8937f23526ccb207a5684c1fc94a7854e19c021b39d944e51f5baef2\",\"status\":\"verified_success\",\"workload_digest\":\"9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870\"}"; |
| 44 | const EXPECTED_OBSERVATION_DIGEST: &str = |
| 45 | "550f4dd46e1f3c197b6d8d973061bd978c722b332d5a416681216e8bf5e845c7"; |
| 46 | const EXPECTED_LOG_BYTES: &str = "{\"kind\":\"header\",\"record\":{\"candidate_executable_digest\":\"86b30dc0591deb3800c70226d5ed326e20f01f9f8288036acdc7b5bd4c86af75\",\"generation_id\":\"gen-golden-0001\",\"log_version\":1,\"opened_at_ms\":1700000000000,\"workload_digest\":\"9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870\"}}\n{\"kind\":\"observation\",\"record\":{\"observation\":{\"candidate_executable_digest\":\"86b30dc0591deb3800c70226d5ed326e20f01f9f8288036acdc7b5bd4c86af75\",\"elapsed_us\":1234,\"generation_id\":\"gen-golden-0001\",\"observation_id\":\"4b269fce18d83d73147676b7358c998a548a99ceebed02816d37a059926b8181\",\"observed_at_ms\":1700000000000,\"provenance\":\"measured\",\"record_version\":2,\"request_digest\":\"f4b3d03bdbef5edb951f4a5a314989098226748eb720e81c382fbebd86e18017\",\"request_id\":\"req-001\",\"result_digest\":\"9795c5ff8937f23526ccb207a5684c1fc94a7854e19c021b39d944e51f5baef2\",\"status\":\"verified_success\",\"workload_digest\":\"9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870\"},\"observation_digest\":\"550f4dd46e1f3c197b6d8d973061bd978c722b332d5a416681216e8bf5e845c7\"}}\n{\"kind\":\"rejection\",\"record\":{\"observed_at_ms\":1700000000600,\"reason\":\"unsolicited result for req-009\"}}\n{\"kind\":\"observation\",\"record\":{\"observation\":{\"candidate_executable_digest\":\"86b30dc0591deb3800c70226d5ed326e20f01f9f8288036acdc7b5bd4c86af75\",\"elapsed_us\":2000000,\"generation_id\":\"gen-golden-0001\",\"observation_id\":\"24e16074b967433cf803587aeb02d52c62817a8666e62ab2dcfe4e51bb5d0fe2\",\"observed_at_ms\":1700000002000,\"provenance\":\"measured\",\"record_version\":2,\"request_digest\":\"dfb1b215f345dc50033819a285844ec5ac12ccb07d5c64023c06a343b7cb3dbe\",\"request_id\":\"req-002\",\"result_digest\":null,\"status\":\"timeout\",\"workload_digest\":\"9431eef86afb1c4cd80a8ae023fbf391519d94ab67f6cd9820ae096bb0710870\"},\"observation_digest\":\"2830ea7d18215ec481d598814d0771e27426ebd1211e73237c8dd0e92e9e74a1\"}}\n{\"kind\":\"seal\",\"record\":{\"log_digest\":\"fb5479def502a60f902285ca26fecd75a6130957f7a4f01ba0759f743bc53ee8\",\"observation_count\":2,\"qualifying_count\":1,\"sealed_at_ms\":1700000003000}}\n"; |
| 47 | const EXPECTED_LOG_DIGEST: &str = |
| 48 | "fb5479def502a60f902285ca26fecd75a6130957f7a4f01ba0759f743bc53ee8"; |
| 49 | const EXPECTED_RECEIPT_V2_DIGEST: &str = |
| 50 | "764766d8efa6457fee8d32b17e2a6e5b42661a51afd4824f16830dac1c1b65e2"; |
| 51 | const EXPECTED_RECEIPT_V2_SIGNATURE: &str = "tpm2-p256:99d2faabc3456a1e66c67b01b14f4e027884ad7bfdd8f84dad110b0bcf9f23887d8f640a1fa69b547943766da1d833c2782ffa603591981099fd9341dcbbd797"; |
| 52 | const EXPECTED_RECEIPT_V1_DIGEST: &str = |
| 53 | "2f89a865a7cd6ea329b9df0f31ced5629e4cba6e629e91834737b8caa22cd817"; |
| 54 | const EXPECTED_PROTOCOL_KEY_FINGERPRINT: &str = |
| 55 | "8cbc48b78519c7a8a48f92d6e9e60f06645e872e6d148b660a5aef6afc990905"; |
| 56 | // The protocol receipt pins below are the strict framed digest contract of aien-protocols#59 |
| 57 | // (`AIEN_EVALUATION_PLAN_V2`, `AIEN_EVALUATION_RECEIPT_V3`, receipt protocol_version 3). They |
| 58 | // supersede the protocol_version 1 pins frozen on 2026-10-12, which are retained byte for byte in |
| 59 | // evidence/protocol-receipt-v1-superseded-2026-10-26/ and are no longer accepted by the protocol. |
| 60 | const EXPECTED_PROTOCOL_PLAN_V2_DIGEST: &str = |
| 61 | "a995483e6e843885dddf918c5124e74dd4872f7cc95e1078a685eea4abe2ddf0"; |
| 62 | const EXPECTED_PROTOCOL_RECEIPT_V3_DIGEST: &str = |
| 63 | "799211286a2842de68b08b806a6b2287beef4703edc5f8f0341efbcf91cd4658"; |
| 64 | const EXPECTED_PROTOCOL_RECEIPT_V3_SIGNATURE: &str = "03ccc524ad237d1936b8ad3efc00e8258ef3dbbe6421384c99c9448b1112bc27423e2e5dabfba27e8e9e73e1b12db07d332fbd160fc4d272c10ede52fe42dd72"; |
| 65 | const EXPECTED_LEDGER_PAYLOAD_DIGEST: &str = |
| 66 | "7b8e7b835fe25b68a1acf315430c15febccecb74f2ec198c4adf83ded8ba6c5d"; |
| 67 | const EXPECTED_LEDGER_BLOCK_HASH: &str = |
| 68 | "250ea0215642b8e1c221456e4a86d2249ffe3ac9b7d1999c7cca39e0c734a492"; |
| 69 | const EXPECTED_MERKLE_ROOT: &str = |
| 70 | "507f253911e35467cca570f695cd66b8e32baef97704eb4061b9b638070336f2"; |
| 71 | |
| 72 | const ADAPTER_TEXT: &str = "manifest_version = 1\nprotocol_version = 1\nexecutable = \"release/rsi-canary-fixture\"\nargs = [\"--adapter\"]\nworkload_id = \"golden-v1\"\nresponse_format = \"utf8-exact\"\n"; |
| 73 | |
| 74 | // ------------------------------------------------------------------ fixtures |
| 75 | |
| 76 | fn workload() -> CanaryWorkloadManifest { |
| 77 | CanaryWorkloadManifest { |
| 78 | workload_version: 1, |
| 79 | workload_id: "golden-v1".into(), |
| 80 | protocol_version: 1, |
| 81 | requests: vec![ |
| 82 | CanaryRequestSpec { |
| 83 | request_id: "req-001".into(), |
| 84 | input: "ping".into(), |
| 85 | expected_output: "pong".into(), |
| 86 | deadline_ms: 2000, |
| 87 | nondeterminism: Nondeterminism::Exact, |
| 88 | }, |
| 89 | CanaryRequestSpec { |
| 90 | request_id: "req-002".into(), |
| 91 | input: "add 2 3".into(), |
| 92 | expected_output: "5".into(), |
| 93 | deadline_ms: 2000, |
| 94 | nondeterminism: Nondeterminism::Exact, |
| 95 | }, |
| 96 | ], |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | fn identity() -> CandidateIdentity { |
| 101 | CandidateIdentity::new( |
| 102 | &sha(b"golden patch"), |
| 103 | &sha(b"golden tree"), |
| 104 | &sha(b"golden executable bytes"), |
| 105 | "gen-golden-0001", |
| 106 | ) |
| 107 | } |
| 108 | |
| 109 | fn success_observation() -> CanaryObservationV2 { |
| 110 | let w = workload(); |
| 111 | let id = identity(); |
| 112 | CanaryObservationV2::new(ObservationInput { |
| 113 | generation_id: &id.generation_id, |
| 114 | request_id: "req-001", |
| 115 | request_digest: &w.requests[0].request_digest(&w.workload_id), |
| 116 | workload_digest: &w.digest(), |
| 117 | candidate_executable_digest: &id.executable_digest, |
| 118 | result_digest: Some(&sha(b"pong")), |
| 119 | elapsed_us: 1234, |
| 120 | observed_at_ms: 1_700_000_000_000, |
| 121 | status: VerifiedStatus::VerifiedSuccess, |
| 122 | provenance: Provenance::Measured, |
| 123 | }) |
| 124 | } |
| 125 | |
| 126 | fn timeout_observation() -> CanaryObservationV2 { |
| 127 | let w = workload(); |
| 128 | let id = identity(); |
| 129 | CanaryObservationV2::new(ObservationInput { |
| 130 | generation_id: &id.generation_id, |
| 131 | request_id: "req-002", |
| 132 | request_digest: &w.requests[1].request_digest(&w.workload_id), |
| 133 | workload_digest: &w.digest(), |
| 134 | candidate_executable_digest: &id.executable_digest, |
| 135 | result_digest: None, |
| 136 | elapsed_us: 2_000_000, |
| 137 | observed_at_ms: 1_700_000_002_000, |
| 138 | status: VerifiedStatus::Timeout, |
| 139 | provenance: Provenance::Measured, |
| 140 | }) |
| 141 | } |
| 142 | |
| 143 | fn golden_log() -> (Vec<u8>, SealedObservationLog) { |
| 144 | let tmp = tempfile::tempdir().unwrap(); |
| 145 | let path = tmp.path().join("gen-golden-0001.jsonl"); |
| 146 | let w = workload(); |
| 147 | let id = identity(); |
| 148 | let mut writer = ObservationLogWriter::create( |
| 149 | &path, |
| 150 | LogHeader::new( |
| 151 | &id.generation_id, |
| 152 | &w.digest(), |
| 153 | &id.executable_digest, |
| 154 | 1_700_000_000_000, |
| 155 | ), |
| 156 | ) |
| 157 | .unwrap(); |
| 158 | writer.append_observation(&success_observation()).unwrap(); |
| 159 | writer |
| 160 | .append_rejection("unsolicited result for req-009", 1_700_000_000_600) |
| 161 | .unwrap(); |
| 162 | writer.append_observation(&timeout_observation()).unwrap(); |
| 163 | let sealed = writer.seal(1_700_000_003_000).unwrap(); |
| 164 | (std::fs::read(&path).unwrap(), sealed) |
| 165 | } |
| 166 | |
| 167 | fn layer(name: &str, passed: bool, score: f64) -> LayerResult { |
| 168 | LayerResult { |
| 169 | layer_name: name.to_string(), |
| 170 | is_hard_invariant: true, |
| 171 | passed, |
| 172 | score, |
| 173 | summary: format!("{} summary", name), |
| 174 | violations: Vec::new(), |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | fn base_receipt() -> EvaluationReceipt { |
| 179 | EvaluationReceipt { |
| 180 | cycle_id: "cycle-golden".to_string(), |
| 181 | candidate_id: "cand-golden".to_string(), |
| 182 | parent_id: "parent-golden".to_string(), |
| 183 | evaluated_at: "2026-10-12T00:00:00Z".to_string(), |
| 184 | evaluator_version: "1.0.0".to_string(), |
| 185 | passed_all_hard_invariants: true, |
| 186 | passed_statistical_gates: true, |
| 187 | admitted: true, |
| 188 | layer_results: vec![ |
| 189 | layer("correctness", true, 1.0), |
| 190 | layer("security", true, 0.5), |
| 191 | ], |
| 192 | metrics_summary: Some(EvaluationMetricsSummary { |
| 193 | latency_delta_pct: -1.0, |
| 194 | p_value: 0.01, |
| 195 | p95_ci_upper_degradation_pct: 0.5, |
| 196 | p99_ci_upper_degradation_pct: 0.7, |
| 197 | rss_growth_pct: 0.1, |
| 198 | candidate_resident_mb: 12, |
| 199 | }), |
| 200 | receipt_digest: String::new(), |
| 201 | signature: None, |
| 202 | format_version: 1, |
| 203 | binding: None, |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | fn v2_binding() -> ReceiptBindingV2 { |
| 208 | ReceiptBindingV2 { |
| 209 | subject_path: "README.md".to_string(), |
| 210 | subject_sha256: sha(b"golden readme"), |
| 211 | holdout_set_sha256: sha(b"golden holdouts"), |
| 212 | holdouts_total: 4, |
| 213 | holdouts_passed: 4, |
| 214 | policy_sha256: sha(b"golden policy"), |
| 215 | evaluator_binary_sha256: sha(b"golden judge binary"), |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | fn judge_key() -> SigningKey { |
| 220 | SigningKey::from_slice(&[7u8; 32]).unwrap() |
| 221 | } |
| 222 | |
| 223 | // ------------------------------------------------------------------ (1) canary records |
| 224 | |
| 225 | #[test] |
| 226 | fn workload_canonical_bytes_and_digests_are_frozen() { |
| 227 | let w = workload(); |
| 228 | assert_eq!( |
| 229 | String::from_utf8(w.canonical_bytes()).unwrap(), |
| 230 | EXPECTED_WORKLOAD_CANONICAL |
| 231 | ); |
| 232 | assert_eq!(w.digest(), EXPECTED_WORKLOAD_DIGEST); |
| 233 | assert_eq!( |
| 234 | w.requests[0].request_digest(&w.workload_id), |
| 235 | EXPECTED_REQUEST_001_DIGEST |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn adapter_manifest_digest_is_frozen() { |
| 241 | let m = CanaryAdapterManifest::parse(ADAPTER_TEXT).unwrap(); |
| 242 | assert_eq!(m.workload_id, "golden-v1"); |
| 243 | assert_eq!( |
| 244 | CanaryAdapterManifest::digest_of_text(ADAPTER_TEXT), |
| 245 | EXPECTED_ADAPTER_DIGEST |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn candidate_identity_binding_digest_is_frozen() { |
| 251 | assert_eq!( |
| 252 | identity().binding_digest(), |
| 253 | EXPECTED_IDENTITY_BINDING_DIGEST |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | fn observation_encoding_id_and_digest_are_frozen() { |
| 259 | let o = success_observation(); |
| 260 | assert_eq!( |
| 261 | o.observation_id, |
| 262 | observation_id("gen-golden-0001", "req-001", &o.request_digest) |
| 263 | ); |
| 264 | assert_eq!(o.observation_id, EXPECTED_OBSERVATION_ID); |
| 265 | assert_eq!( |
| 266 | String::from_utf8(o.canonical_bytes()).unwrap(), |
| 267 | EXPECTED_OBSERVATION_CANONICAL |
| 268 | ); |
| 269 | assert_eq!(o.digest_hex(), EXPECTED_OBSERVATION_DIGEST); |
| 270 | o.validate().unwrap(); |
| 271 | // The record round-trips through JSON unchanged. |
| 272 | let back: CanaryObservationV2 = serde_json::from_slice(&o.canonical_bytes()).unwrap(); |
| 273 | assert_eq!(back, o); |
| 274 | } |
| 275 | |
| 276 | #[test] |
| 277 | fn sealed_log_bytes_and_digest_are_frozen() { |
| 278 | let (bytes, sealed) = golden_log(); |
| 279 | assert_eq!( |
| 280 | String::from_utf8(bytes.clone()).unwrap(), |
| 281 | EXPECTED_LOG_BYTES |
| 282 | ); |
| 283 | assert_eq!(sealed.log_digest(), EXPECTED_LOG_DIGEST); |
| 284 | assert_eq!(sealed.seal.observation_count, 2); |
| 285 | assert_eq!(sealed.seal.qualifying_count, 1); |
| 286 | let verified = SealedObservationLog::verify_bytes(&bytes).unwrap(); |
| 287 | assert_eq!(verified.observations, sealed.observations); |
| 288 | assert_eq!(verified.seal, sealed.seal); |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn execute_protocol_wire_format_is_frozen() { |
| 293 | let req = SupervisorMessage::ExecuteRequest { |
| 294 | protocol_version: CANARY_PROTOCOL_VERSION, |
| 295 | generation_id: "gen-golden-0001".into(), |
| 296 | request_id: "req-001".into(), |
| 297 | request_digest: "aa".repeat(32), |
| 298 | workload_digest: "bb".repeat(32), |
| 299 | input: "ping".into(), |
| 300 | deadline_ms: 2000, |
| 301 | }; |
| 302 | assert_eq!( |
| 303 | serde_json::to_string(&req).unwrap(), |
| 304 | format!( |
| 305 | "{{\"ExecuteRequest\":{{\"protocol_version\":1,\"generation_id\":\"gen-golden-0001\",\"request_id\":\"req-001\",\"request_digest\":\"{}\",\"workload_digest\":\"{}\",\"input\":\"ping\",\"deadline_ms\":2000}}}}", |
| 306 | "aa".repeat(32), |
| 307 | "bb".repeat(32) |
| 308 | ) |
| 309 | ); |
| 310 | let res = WorkerMessage::ExecuteResult { |
| 311 | protocol_version: CANARY_PROTOCOL_VERSION, |
| 312 | generation_id: "gen-golden-0001".into(), |
| 313 | request_id: "req-001".into(), |
| 314 | output: "pong".into(), |
| 315 | worker_latency_us: Some(900), |
| 316 | }; |
| 317 | assert_eq!( |
| 318 | serde_json::to_string(&res).unwrap(), |
| 319 | "{\"ExecuteResult\":{\"protocol_version\":1,\"generation_id\":\"gen-golden-0001\",\"request_id\":\"req-001\",\"output\":\"pong\",\"worker_latency_us\":900}}" |
| 320 | ); |
| 321 | let ready = WorkerMessage::Ready { |
| 322 | generation_id: "gen-golden-0001".into(), |
| 323 | pid: 4242, |
| 324 | auth_token: String::new(), |
| 325 | protocol_version: CANARY_PROTOCOL_VERSION, |
| 326 | }; |
| 327 | assert_eq!( |
| 328 | serde_json::to_string(&ready).unwrap(), |
| 329 | "{\"Ready\":{\"generation_id\":\"gen-golden-0001\",\"pid\":4242,\"auth_token\":\"\",\"protocol_version\":1}}" |
| 330 | ); |
| 331 | // A Ready from an older worker has no protocol version and reads as 0: never ready. |
| 332 | let legacy: WorkerMessage = serde_json::from_str( |
| 333 | "{\"Ready\":{\"generation_id\":\"gen-old\",\"pid\":1,\"auth_token\":\"\"}}", |
| 334 | ) |
| 335 | .unwrap(); |
| 336 | match legacy { |
| 337 | WorkerMessage::Ready { |
| 338 | protocol_version, .. |
| 339 | } => assert_eq!(protocol_version, 0), |
| 340 | other => panic!("expected Ready, got {:?}", other), |
| 341 | } |
| 342 | // A result without the diagnostic latency still parses. |
| 343 | let bare: WorkerMessage = serde_json::from_str( |
| 344 | "{\"ExecuteResult\":{\"protocol_version\":1,\"generation_id\":\"g\",\"request_id\":\"r\",\"output\":\"o\"}}", |
| 345 | ) |
| 346 | .unwrap(); |
| 347 | match bare { |
| 348 | WorkerMessage::ExecuteResult { |
| 349 | worker_latency_us, .. |
| 350 | } => assert_eq!(worker_latency_us, None), |
| 351 | other => panic!("expected ExecuteResult, got {:?}", other), |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // ------------------------------------------------------------------ (2) evaluation receipts |
| 356 | |
| 357 | #[test] |
| 358 | fn evaluation_receipt_v2_digest_signature_and_verification_are_frozen() { |
| 359 | let judge = judge_key(); |
| 360 | let mut r = base_receipt(); |
| 361 | r.bind_and_sign_v2(v2_binding(), &judge).unwrap(); |
| 362 | assert_eq!(r.format_version, 2); |
| 363 | assert_eq!(r.receipt_digest, EXPECTED_RECEIPT_V2_DIGEST); |
| 364 | assert_eq!(r.signature.as_deref(), Some(EXPECTED_RECEIPT_V2_SIGNATURE)); |
| 365 | assert!(r.verify_digest()); |
| 366 | assert!(r.verify_signature(judge.verifying_key())); |
| 367 | r.verify_for_promotion( |
| 368 | judge.verifying_key(), |
| 369 | &sha(b"golden readme"), |
| 370 | &sha(b"golden policy"), |
| 371 | &sha(b"golden holdouts"), |
| 372 | ) |
| 373 | .unwrap(); |
| 374 | // Any change to a bound field breaks the digest and the signature. |
| 375 | let mut tampered = r.clone(); |
| 376 | tampered.binding.as_mut().unwrap().holdouts_passed = 3; |
| 377 | assert!(!tampered.verify_digest()); |
| 378 | assert!(!tampered.verify_signature(judge.verifying_key())); |
| 379 | let other = SigningKey::from_slice(&[9u8; 32]).unwrap(); |
| 380 | assert!(!r.verify_signature(other.verifying_key())); |
| 381 | } |
| 382 | |
| 383 | #[test] |
| 384 | fn evaluation_receipt_v1_digest_is_frozen() { |
| 385 | let r = base_receipt(); |
| 386 | let digest = EvaluationReceipt::compute_digest( |
| 387 | &r.cycle_id, |
| 388 | &r.candidate_id, |
| 389 | &r.parent_id, |
| 390 | r.admitted, |
| 391 | &r.layer_results, |
| 392 | ); |
| 393 | assert_eq!(digest, EXPECTED_RECEIPT_V1_DIGEST); |
| 394 | } |
| 395 | |
| 396 | // ------------------------------------------------------- protocol plan and receipt (framed v3) |
| 397 | |
| 398 | fn protocol_signer() -> aien_evaluation_protocol::SoftwareP256Signer { |
| 399 | aien_evaluation_protocol::SoftwareP256Signer::new( |
| 400 | SigningKey::from_slice(&[0x42u8; 32]).unwrap(), |
| 401 | ) |
| 402 | } |
| 403 | |
| 404 | /// A fixed plan in the shape `CanarySafetyEnvelope::build_plan` produces, with the probe |
| 405 | /// evaluator's descriptor and a second descriptor that shares its id, so the pin covers the |
| 406 | /// contract's total evaluator ordering. |
| 407 | fn protocol_plan() -> aien_evaluation_protocol::EvaluationPlan { |
| 408 | use aien_evaluation_protocol::{EvaluationPlan, EvaluatorDescriptor}; |
| 409 | use aien_protocol_types::{ArtifactRef, Digest32, EvaluationId}; |
| 410 | let mut plan = EvaluationPlan { |
| 411 | evaluation_id: EvaluationId(uuid::Uuid::from_u128( |
| 412 | 0x0123_4567_89ab_cdef_0123_4567_89ab_cdef, |
| 413 | )), |
| 414 | subject: ArtifactRef { |
| 415 | artifact_id: uuid::Uuid::from_u128(0x2222_2222_2222_4222_8222_2222_2222_2222), |
| 416 | digest: Digest32([0x22; 32]), |
| 417 | media_type: "application/rust-patch".to_string(), |
| 418 | byte_size: 4096, |
| 419 | }, |
| 420 | profile: "rsi-production".to_string(), |
| 421 | evaluators: vec![ |
| 422 | EvaluatorDescriptor { |
| 423 | evaluator_id: "aien-sovereign-probe".to_string(), |
| 424 | version: "0.1.0".to_string(), |
| 425 | required: true, |
| 426 | }, |
| 427 | EvaluatorDescriptor { |
| 428 | evaluator_id: "aien-sovereign-probe".to_string(), |
| 429 | version: "0.1.0".to_string(), |
| 430 | required: false, |
| 431 | }, |
| 432 | ], |
| 433 | baseline: None, |
| 434 | sandbox_profile: "rsi-canary-isolated".to_string(), |
| 435 | policy_digest: Digest32([0x77; 32]), |
| 436 | evaluator_manifest_digest: Digest32([0x88; 32]), |
| 437 | plan_digest: Digest32::ZERO, |
| 438 | }; |
| 439 | plan.plan_digest = plan.compute_plan_digest(); |
| 440 | plan |
| 441 | } |
| 442 | |
| 443 | /// The 2026-10-12 receipt fixture with only `protocol_version` changed (1 there, `version` here). |
| 444 | fn protocol_receipt(version: u32, key_id: String) -> aien_evaluation_protocol::EvaluationReceipt { |
| 445 | use aien_evaluation_protocol::{ |
| 446 | EvaluationReceipt as ProtocolReceipt, Verdict, VerifierIdentity, |
| 447 | }; |
| 448 | use aien_protocol_types::{Digest32, EvaluationId, Timestamp}; |
| 449 | ProtocolReceipt { |
| 450 | protocol_version: version, |
| 451 | evaluation_id: EvaluationId(uuid::Uuid::from_u128( |
| 452 | 0x0123_4567_89ab_cdef_0123_4567_89ab_cdef, |
| 453 | )), |
| 454 | request_digest: Digest32([0x11; 32]), |
| 455 | subject_digest: Digest32([0x22; 32]), |
| 456 | plan_digest: Digest32([0x33; 32]), |
| 457 | evidence_root: Digest32([0x44; 32]), |
| 458 | outcomes_root: Digest32([0x55; 32]), |
| 459 | verdict: Verdict::Pass, |
| 460 | verifier: VerifierIdentity { |
| 461 | principal_id: "rsi-golden".to_string(), |
| 462 | key_id, |
| 463 | trust_epoch: 1, |
| 464 | trusted_build_digest: Digest32([0x66; 32]), |
| 465 | policy_bundle_digest: Digest32([0x77; 32]), |
| 466 | }, |
| 467 | started_at: Timestamp(1_700_000_000_000), |
| 468 | completed_at: Timestamp(1_700_000_000_500), |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | #[test] |
| 473 | fn protocol_evaluation_plan_v2_digest_is_frozen() { |
| 474 | let plan = protocol_plan(); |
| 475 | assert_eq!( |
| 476 | hex::encode(plan.plan_digest.0), |
| 477 | EXPECTED_PROTOCOL_PLAN_V2_DIGEST |
| 478 | ); |
| 479 | assert!(plan.verify_plan_digest()); |
| 480 | let mut reordered = plan.clone(); |
| 481 | reordered.evaluators.reverse(); |
| 482 | assert_eq!(reordered.compute_plan_digest(), plan.plan_digest); |
| 483 | // Moving one byte from version into evaluator_id left the unframed concatenation, and so |
| 484 | // the old digest, unchanged. The framed digest must differ. |
| 485 | let mut shifted = plan.clone(); |
| 486 | for e in &mut shifted.evaluators { |
| 487 | e.evaluator_id.push('0'); |
| 488 | e.version = ".1.0".to_string(); |
| 489 | } |
| 490 | assert_ne!(shifted.compute_plan_digest(), plan.plan_digest); |
| 491 | assert!(!shifted.verify_plan_digest()); |
| 492 | } |
| 493 | |
| 494 | #[test] |
| 495 | fn protocol_signed_evaluation_receipt_v3_digest_and_signature_are_frozen() { |
| 496 | use aien_evaluation_protocol::{ |
| 497 | CryptographicTier, EvaluationError, Verdict, VerifierSigner, |
| 498 | FRAMED_RECEIPT_PROTOCOL_VERSION, |
| 499 | }; |
| 500 | |
| 501 | assert_eq!(FRAMED_RECEIPT_PROTOCOL_VERSION, 3); |
| 502 | let signer = protocol_signer(); |
| 503 | assert_eq!(signer.key_fingerprint(), EXPECTED_PROTOCOL_KEY_FINGERPRINT); |
| 504 | let receipt = protocol_receipt(3, signer.key_fingerprint()); |
| 505 | assert_eq!( |
| 506 | hex::encode(receipt.compute_receipt_digest().0), |
| 507 | EXPECTED_PROTOCOL_RECEIPT_V3_DIGEST |
| 508 | ); |
| 509 | let signed = receipt.sign_with_signer(&signer).unwrap(); |
| 510 | assert_eq!( |
| 511 | hex::encode(&signed.signature), |
| 512 | EXPECTED_PROTOCOL_RECEIPT_V3_SIGNATURE |
| 513 | ); |
| 514 | assert!(signed |
| 515 | .verify_with(&signer.verifying_key(), CryptographicTier::Tier2SoftwareKey) |
| 516 | .unwrap()); |
| 517 | let wrong = SigningKey::from_slice(&[0x99u8; 32]).unwrap(); |
| 518 | assert!(!matches!( |
| 519 | signed.verify_with(wrong.verifying_key(), CryptographicTier::Tier2SoftwareKey), |
| 520 | Ok(true) |
| 521 | )); |
| 522 | let mut tampered = signed.clone(); |
| 523 | tampered.receipt.verdict = Verdict::Fail; |
| 524 | assert!(!matches!( |
| 525 | tampered.verify_with(&signer.verifying_key(), CryptographicTier::Tier2SoftwareKey), |
| 526 | Ok(true) |
| 527 | )); |
| 528 | assert!(!matches!( |
| 529 | signed.verify_with(&signer.verifying_key(), CryptographicTier::Tier4HardwareTpm), |
| 530 | Ok(true) |
| 531 | )); |
| 532 | |
| 533 | // Strict cutover: a receipt below version 3 is neither signed nor verified, so no unframed |
| 534 | // digest is accepted through a fallback. |
| 535 | for old in [1u32, 2] { |
| 536 | assert!(matches!( |
| 537 | protocol_receipt(old, signer.key_fingerprint()).sign_with_signer(&signer), |
| 538 | Err(EvaluationError::UnsupportedProtocolVersion(v)) if v == old |
| 539 | )); |
| 540 | let mut downgraded = signed.clone(); |
| 541 | downgraded.receipt.protocol_version = old; |
| 542 | assert!(matches!( |
| 543 | downgraded.verify_with(&signer.verifying_key(), CryptographicTier::Tier2SoftwareKey), |
| 544 | Err(EvaluationError::UnsupportedProtocolVersion(v)) if v == old |
| 545 | )); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // ------------------------------------------------------------------ (3) ledger records |
| 550 | |
| 551 | #[test] |
| 552 | fn ledger_block_encoding_hash_and_validation_are_frozen() { |
| 553 | let payload = "{\"candidate\":\"cand-golden\",\"verdict\":\"admitted\"}".to_string(); |
| 554 | let block = LedgerBlock::new( |
| 555 | 1, |
| 556 | BlockType::Evaluation, |
| 557 | "2026-10-12T00:00:00+00:00".to_string(), |
| 558 | LedgerBlock::GENESIS_PREV_HASH.to_string(), |
| 559 | payload, |
| 560 | vec![sha(b"golden blob")], |
| 561 | ); |
| 562 | assert_eq!(block.payload_digest, EXPECTED_LEDGER_PAYLOAD_DIGEST); |
| 563 | assert_eq!(block.block_hash, EXPECTED_LEDGER_BLOCK_HASH); |
| 564 | block.verify_integrity().unwrap(); |
| 565 | let mut tampered = block.clone(); |
| 566 | tampered.payload_json = "{\"candidate\":\"cand-golden\",\"verdict\":\"refused\"}".into(); |
| 567 | assert!(tampered.verify_integrity().is_err()); |
| 568 | let mut relinked = block.clone(); |
| 569 | relinked.prev_block_hash = sha(b"another parent"); |
| 570 | assert!(relinked.verify_integrity().is_err()); |
| 571 | assert_eq!( |
| 572 | compute_merkle_root(&[block.block_hash.clone(), sha(b"leaf-2"), sha(b"leaf-3")]), |
| 573 | EXPECTED_MERKLE_ROOT |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | // ------------------------------------------------------------------ pin generator |
| 578 | |
| 579 | /// Prints every pinned value. Run with `--ignored --nocapture` when a format is deliberately |
| 580 | /// versioned; never to make a failing pin pass. |
| 581 | #[test] |
| 582 | #[ignore] |
| 583 | fn print_pins() { |
| 584 | let w = workload(); |
| 585 | let o = success_observation(); |
| 586 | let (bytes, sealed) = golden_log(); |
| 587 | let judge = judge_key(); |
| 588 | let mut r2 = base_receipt(); |
| 589 | r2.bind_and_sign_v2(v2_binding(), &judge).unwrap(); |
| 590 | let r1 = base_receipt(); |
| 591 | let block = LedgerBlock::new( |
| 592 | 1, |
| 593 | BlockType::Evaluation, |
| 594 | "2026-10-12T00:00:00+00:00".to_string(), |
| 595 | LedgerBlock::GENESIS_PREV_HASH.to_string(), |
| 596 | "{\"candidate\":\"cand-golden\",\"verdict\":\"admitted\"}".to_string(), |
| 597 | vec![sha(b"golden blob")], |
| 598 | ); |
| 599 | println!( |
| 600 | "PIN workload_canonical={}", |
| 601 | String::from_utf8(w.canonical_bytes()).unwrap() |
| 602 | ); |
| 603 | println!("PIN workload_digest={}", w.digest()); |
| 604 | println!( |
| 605 | "PIN request_001_digest={}", |
| 606 | w.requests[0].request_digest(&w.workload_id) |
| 607 | ); |
| 608 | println!( |
| 609 | "PIN adapter_digest={}", |
| 610 | CanaryAdapterManifest::digest_of_text(ADAPTER_TEXT) |
| 611 | ); |
| 612 | println!( |
| 613 | "PIN identity_binding_digest={}", |
| 614 | identity().binding_digest() |
| 615 | ); |
| 616 | println!("PIN observation_id={}", o.observation_id); |
| 617 | println!( |
| 618 | "PIN observation_canonical={}", |
| 619 | String::from_utf8(o.canonical_bytes()).unwrap() |
| 620 | ); |
| 621 | println!("PIN observation_digest={}", o.digest_hex()); |
| 622 | println!( |
| 623 | "PIN log_bytes={}", |
| 624 | serde_json::to_string(&String::from_utf8(bytes).unwrap()).unwrap() |
| 625 | ); |
| 626 | println!("PIN log_digest={}", sealed.log_digest()); |
| 627 | println!("PIN receipt_v2_digest={}", r2.receipt_digest); |
| 628 | println!("PIN receipt_v2_signature={}", r2.signature.unwrap()); |
| 629 | println!( |
| 630 | "PIN receipt_v1_digest={}", |
| 631 | EvaluationReceipt::compute_digest( |
| 632 | &r1.cycle_id, |
| 633 | &r1.candidate_id, |
| 634 | &r1.parent_id, |
| 635 | r1.admitted, |
| 636 | &r1.layer_results |
| 637 | ) |
| 638 | ); |
| 639 | { |
| 640 | use aien_evaluation_protocol::VerifierSigner; |
| 641 | let signer = protocol_signer(); |
| 642 | let receipt = protocol_receipt(3, signer.key_fingerprint()); |
| 643 | let signed = receipt.sign_with_signer(&signer).unwrap(); |
| 644 | println!("PIN protocol_key_fingerprint={}", signer.key_fingerprint()); |
| 645 | println!( |
| 646 | "PIN protocol_plan_v2_digest={}", |
| 647 | hex::encode(protocol_plan().plan_digest.0) |
| 648 | ); |
| 649 | println!( |
| 650 | "PIN protocol_receipt_v3_digest={}", |
| 651 | hex::encode(receipt.compute_receipt_digest().0) |
| 652 | ); |
| 653 | println!( |
| 654 | "PIN protocol_receipt_v3_signature={}", |
| 655 | hex::encode(&signed.signature) |
| 656 | ); |
| 657 | } |
| 658 | println!("PIN ledger_payload_digest={}", block.payload_digest); |
| 659 | println!("PIN ledger_block_hash={}", block.block_hash); |
| 660 | println!( |
| 661 | "PIN merkle_root={}", |
| 662 | compute_merkle_root(&[block.block_hash.clone(), sha(b"leaf-2"), sha(b"leaf-3")]) |
| 663 | ); |
| 664 | } |