interplane.Get Omega
Read in your language

spark-rsi-mv

Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger

Brought by drakestapleton

branch main Branches Tags History

spark-rsi-mv / src / promotion_gate.rs

105 lines · 4154 bytes Raw Blame History
1//! Promotion gate: no change is promoted unless its exact identity matches the identity covered
2//! by a valid version 2 receipt signed by the separate judge (docs/RECEIPT-V2.md).
3//!
4//! Everything here fails closed. A missing judge key, a judge key equal to the proposer's own
5//! key, a missing or invalid policy, a version 1 receipt, a bad signature, or any identity
6//! mismatch refuses the promotion.
7
8use crate::actor::judge::EvaluationPolicy;
9use crate::evaluator::EvaluationReceipt;
10use p256::ecdsa::VerifyingKey;
11use std::path::Path;
12
13/// The change about to be promoted: the one file it replaces, the digest of the bytes the change
14/// carries, and the digest of the bytes read back from the staged tree right before the swap.
15pub struct PromotionSubject<'a> {
16 /// The commit (or tree id) the change is applied on; the receipt must name it as its parent.
17 pub parent_id: &'a str,
18 pub path: &'a str,
19 pub content_sha256: &'a str,
20 pub disk_sha256: &'a str,
21}
22
23impl<'a> PromotionSubject<'a> {
24 /// A subject whose staged bytes were confirmed equal to the change's bytes.
25 pub fn same(parent_id: &'a str, path: &'a str, sha256: &'a str) -> Self {
26 Self {
27 parent_id,
28 path,
29 content_sha256: sha256,
30 disk_sha256: sha256,
31 }
32 }
33}
34
35/// Checks `receipt` before promoting `subject`.
36///
37/// `judge_public_key_hex` is the operator-pinned SEC1 hex key of the judge. `own_key` is the
38/// promoting process's own key: a receipt the promoter could have signed itself is refused.
39/// The policy file is read here, so the policy and holdout digests come from the operator's copy,
40/// never from the receipt.
41pub fn check_v2_promotion(
42 receipt: &EvaluationReceipt,
43 judge_public_key_hex: Option<&str>,
44 own_key: &VerifyingKey,
45 judge_policy_file: Option<&str>,
46 subject: &PromotionSubject,
47) -> Result<(), String> {
48 if subject.content_sha256 != subject.disk_sha256 {
49 return Err(format!(
50 "staged {} hashes to {}, not the change's {}; refusing to promote",
51 subject.path, subject.disk_sha256, subject.content_sha256
52 ));
53 }
54 let key_hex = judge_public_key_hex
55 .ok_or("no pinned judge public key (judge_public_key_hex); promotion fails closed")?;
56 let key_bytes =
57 hex::decode(key_hex.trim()).map_err(|e| format!("judge public key is not hex: {}", e))?;
58 let judge_key = VerifyingKey::from_sec1_bytes(&key_bytes)
59 .map_err(|e| format!("judge public key is invalid: {}", e))?;
60 if &judge_key == own_key {
61 return Err(
62 "judge public key equals the promoter's own key; the judge must be separate".into(),
63 );
64 }
65 let policy_file = judge_policy_file
66 .ok_or("no pinned evaluation policy (judge_policy_file); promotion fails closed")?;
67 let (policy, policy_sha256) = EvaluationPolicy::load(Path::new(policy_file))?;
68 receipt.verify_for_promotion(
69 &judge_key,
70 subject.content_sha256,
71 &policy_sha256,
72 &policy.holdout_set_sha256,
73 )?;
74 let b = receipt
75 .binding
76 .as_ref()
77 .ok_or("version 2 receipt without binding")?;
78 if receipt.parent_id != subject.parent_id {
79 return Err(format!(
80 "receipt judged the change against parent {}, but it applies on {}",
81 receipt.parent_id, subject.parent_id
82 ));
83 }
84 if b.subject_path != subject.path {
85 return Err(format!(
86 "receipt covers {}, but the change replaces {}",
87 b.subject_path, subject.path
88 ));
89 }
90 if !policy.allowed_targets.contains(&b.subject_path) {
91 return Err(format!(
92 "receipt subject {} is not an allowed target in the pinned policy",
93 b.subject_path
94 ));
95 }
96 if b.holdouts_total == 0
97 || (b.holdouts_passed as f64) < policy.min_holdout_pass_ratio * (b.holdouts_total as f64)
98 {
99 return Err(format!(
100 "holdouts passed {}/{} is below the policy threshold {}",
101 b.holdouts_passed, b.holdouts_total, policy.min_holdout_pass_ratio
102 ));
103 }
104 Ok(())
105}