interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / adapters / aien / src / rsi_m5.rs
| 1 | //! Harness side of the VAC M5 slice: the RSI engine proposes a change to the fixture's README, the |
| 2 | //! separate judge builds parent and candidate, runs its holdouts and signs a version 2 receipt |
| 3 | //! (spark-rsi docs/RECEIPT-V2.md). Both run under their own operating-system accounts through the |
| 4 | //! operator's hook script (scripts/m5-propose-and-judge.sh); this module only reads what they |
| 5 | //! wrote. Nothing here grants, approves or decides anything: the gate before the write is |
| 6 | //! `interplane_provenance::evaluation::precheck`, and the daemon's normal approval still applies. |
| 7 | use crate::provenance_export::Evaluation; |
| 8 | use serde_json::Value; |
| 9 | use std::path::Path; |
| 10 | use std::process::Command; |
| 11 | |
| 12 | /// The one file the M5 task allows to change. |
| 13 | pub const TARGET: &str = "README.md"; |
| 14 | |
| 15 | /// The RSI engine's proposal and the judge's evidence for it. |
| 16 | #[derive(Debug, Clone)] |
| 17 | pub struct Judged { |
| 18 | pub proposal_id: String, |
| 19 | /// The whole new file the proposal writes. |
| 20 | pub content: String, |
| 21 | pub evaluation: Evaluation, |
| 22 | } |
| 23 | |
| 24 | /// Reads one `ImprovementProposal` (spark-rsi `propose` output) and refuses any proposal that is |
| 25 | /// not a whole-file change to `allowed`. A proposal aimed at a holdout, the policy or any other |
| 26 | /// file stops here, before anything reaches the judge's signature check or the daemon. |
| 27 | pub fn check_proposal(bytes: &[u8], allowed: &str) -> Result<(String, String), String> { |
| 28 | let v: Value = |
| 29 | serde_json::from_slice(bytes).map_err(|e| format!("proposal is not JSON: {e}"))?; |
| 30 | let field = |k: &str| { |
| 31 | v.get(k) |
| 32 | .and_then(Value::as_str) |
| 33 | .ok_or_else(|| format!("proposal has no {k}")) |
| 34 | }; |
| 35 | let target = field("target_file")?; |
| 36 | if target != allowed { |
| 37 | return Err(format!( |
| 38 | "RSI proposed a change to {target}; the task allows only {allowed}: refused" |
| 39 | )); |
| 40 | } |
| 41 | Ok(( |
| 42 | field("id")?.to_string(), |
| 43 | field("proposed_patch")?.to_string(), |
| 44 | )) |
| 45 | } |
| 46 | |
| 47 | /// The hook's switches for making counterfeit evidence in the gate's negative checks. A real run |
| 48 | /// never passes them on (the hook also ignores them unless `M5_GATE_NEGATIVE=1`). |
| 49 | pub const NEGATIVE_ONLY_ENV: [&str; 4] = [ |
| 50 | "M5_GATE_NEGATIVE", |
| 51 | "M5_SUBJECT_OVERRIDE", |
| 52 | "M5_JUDGE_KEY_OVERRIDE", |
| 53 | "M5_RECEIPT_V1", |
| 54 | ]; |
| 55 | |
| 56 | /// Runs `hook <ws> <out>` and reads `proposal.json`, `receipt.json` and `policy.json` from `out`, |
| 57 | /// which must not exist yet. The receipt and policy are kept as the exact bytes written, and the |
| 58 | /// policy must be byte for byte `pinned_policy`, the operator's own copy: the hook's copy is never |
| 59 | /// trusted on its own. The hook's output goes to `out/hook.log`. |
| 60 | pub fn propose_and_judge( |
| 61 | hook: &Path, |
| 62 | ws: &Path, |
| 63 | out: &Path, |
| 64 | pinned_policy: &[u8], |
| 65 | ) -> Result<Judged, String> { |
| 66 | run_hook(hook, ws, out, pinned_policy, None) |
| 67 | } |
| 68 | |
| 69 | /// The same hook and the same checks, for a proposal a real model turn produced: `proposal` is a |
| 70 | /// `proposal.json`-shaped file the harness wrote from the model's tool call (`id`, `target_file`, |
| 71 | /// `proposed_patch`). The hook skips `spark-rsi propose`; the judge still builds and evaluates the |
| 72 | /// exact bytes, and the receipt and policy are checked as for any other proposal. |
| 73 | pub fn judge_model_proposal( |
| 74 | hook: &Path, |
| 75 | ws: &Path, |
| 76 | out: &Path, |
| 77 | pinned_policy: &[u8], |
| 78 | proposal: &Path, |
| 79 | ) -> Result<Judged, String> { |
| 80 | run_hook(hook, ws, out, pinned_policy, Some(proposal)) |
| 81 | } |
| 82 | |
| 83 | fn run_hook( |
| 84 | hook: &Path, |
| 85 | ws: &Path, |
| 86 | out: &Path, |
| 87 | pinned_policy: &[u8], |
| 88 | proposal: Option<&Path>, |
| 89 | ) -> Result<Judged, String> { |
| 90 | if out.exists() { |
| 91 | return Err(format!( |
| 92 | "{} exists: a judged run is never reused", |
| 93 | out.display() |
| 94 | )); |
| 95 | } |
| 96 | std::fs::create_dir_all(out).map_err(|e| e.to_string())?; |
| 97 | let mut cmd = Command::new(hook); |
| 98 | cmd.arg(ws).arg(out); |
| 99 | for v in NEGATIVE_ONLY_ENV { |
| 100 | cmd.env_remove(v); |
| 101 | } |
| 102 | // Only the model-turn row may redirect the proposal; the RSI row never does. |
| 103 | cmd.env_remove("M5_PROPOSAL_FILE"); |
| 104 | if let Some(p) = proposal { |
| 105 | cmd.env("M5_PROPOSAL_FILE", p); |
| 106 | } |
| 107 | let run = cmd |
| 108 | .output() |
| 109 | .map_err(|e| format!("{}: {e}", hook.display()))?; |
| 110 | let log = [run.stdout.as_slice(), run.stderr.as_slice()].concat(); |
| 111 | std::fs::write(out.join("hook.log"), &log).map_err(|e| e.to_string())?; |
| 112 | if !run.status.success() { |
| 113 | return Err(format!( |
| 114 | "hook {} exited {}: {}", |
| 115 | hook.display(), |
| 116 | run.status, |
| 117 | String::from_utf8_lossy(&log).lines().last().unwrap_or("") |
| 118 | )); |
| 119 | } |
| 120 | let read = |n: &str| std::fs::read(out.join(n)).map_err(|e| format!("{n}: {e}")); |
| 121 | let (proposal_id, content) = check_proposal(&read("proposal.json")?, TARGET)?; |
| 122 | let policy = read("policy.json")?; |
| 123 | if policy != pinned_policy { |
| 124 | return Err("the judged policy is not the operator's pinned policy: refused".into()); |
| 125 | } |
| 126 | Ok(Judged { |
| 127 | proposal_id, |
| 128 | content, |
| 129 | evaluation: Evaluation { |
| 130 | receipt: read("receipt.json")?, |
| 131 | policy, |
| 132 | }, |
| 133 | }) |
| 134 | } |
| 135 | |
| 136 | #[cfg(test)] |
| 137 | mod tests { |
| 138 | use super::*; |
| 139 | use serde_json::json; |
| 140 | |
| 141 | fn proposal(target: &str) -> Vec<u8> { |
| 142 | serde_json::to_vec(&json!({"id": "p1", "title": "t", "description": "d", |
| 143 | "target_file": target, "proposed_patch": "new text\n", "kind": "Style", |
| 144 | "created_at": "2026-10-08T00:00:00Z", "sandbox_path": null})) |
| 145 | .unwrap() |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn readme_proposal_is_accepted() { |
| 150 | let (id, content) = check_proposal(&proposal("README.md"), TARGET).unwrap(); |
| 151 | assert_eq!((id.as_str(), content.as_str()), ("p1", "new text\n")); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn proposals_aimed_at_holdouts_policy_or_code_are_refused() { |
| 156 | for t in [ |
| 157 | "holdouts/HOLD-M5-001.json", |
| 158 | "policy.json", |
| 159 | "../m5/policy.json", |
| 160 | "src/main.rs", |
| 161 | "TASK.md", |
| 162 | ] { |
| 163 | let e = check_proposal(&proposal(t), TARGET).unwrap_err(); |
| 164 | assert!(e.contains("refused"), "{t}: {e}"); |
| 165 | } |
| 166 | assert!(check_proposal(b"{", TARGET).is_err()); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn a_used_output_directory_is_refused() { |
| 171 | let d = tempfile::tempdir().unwrap(); |
| 172 | let e = propose_and_judge(Path::new("/bin/true"), d.path(), d.path(), b"{}").unwrap_err(); |
| 173 | assert!(e.contains("never reused"), "{e}"); |
| 174 | } |
| 175 | } |