//! The independent reviewer agent at work: a separate principal judges a batch //! of ops against a spec and signs the reviews — some pass, some are sent back. //! //! Run: `cargo run -p joule-code-reviewer --example review` use ed25519_dalek::SigningKey; use joule_code_cargo::{Anchor, CodingOp, Edit, SignedOp}; use joule_code_review::{approval, ReviewVerdict}; use joule_code_schema::{ContentId, Tier}; use joule_code_reviewer::Reviewer; use joule_code_spec::{Clause, ClauseKind, SignedSpec, Spec, ALG_ED25519 as SALG}; fn key(s: u8) -> SigningKey { SigningKey::from_bytes(&[s; 32]) } fn op(author: &SigningKey, body: &str) -> SignedOp { CodingOp { alg: 0x01, prior: ContentId::of_bytes(body.as_bytes()), edits: vec![Edit { path: "src/store.rs".into(), anchor: Anchor::ByteRange { start: 0, end: 0 }, replacement: body.as_bytes().to_vec(), }], author: author.verifying_key().to_bytes(), intent: "implement".into(), tier: Tier::L3, } .sign(author) .unwrap() } fn main() { let author = key(1); // the authoring principal let reviewer = Reviewer::new(key(2)); // a DISTINCT, independent principal let spec: SignedSpec = Spec { alg: SALG, intent: "a persistent todo list".into(), clauses: vec![ Clause { id: "C1".into(), text: "todos persist across reloads".into(), kind: ClauseKind::Requirement }, Clause { id: "NG1".into(), text: "no multiplayer".into(), kind: ClauseKind::NonGoal }, ], parent: None, author: author.verifying_key().to_bytes(), } .sign(&author) .unwrap(); let cases: &[(&str, &str, &[&str])] = &[ ("a finished op implementing C1", "fn save() { write_to_disk(); }", &["C1"]), ("an op shipping an unfinished marker", "fn save() { todo!() }", &["C1"]), ("an op implementing a non-goal", "fn multiplayer() {}", &["NG1"]), ]; for (label, body, clauses) in cases { let o = op(&author, body); let claimed: Vec = clauses.iter().map(|c| c.to_string()).collect(); let reviews = reviewer.review(&o, &spec, &claimed).unwrap(); let verdict = if approval(&o, &reviews).is_approved() { "✓ APPROVED" } else { "⛔ SENT BACK" }; println!("{label}\n {verdict} (reviewer principal {}…)", &hex8(reviewer.principal())); for r in &reviews { let mark = if r.review.verdict == ReviewVerdict::Approve { "✓" } else { "✗" }; print!(" {mark} {:?}", r.review.stage); if !r.review.findings.is_empty() { print!(" — {}", r.review.findings.join("; ")); } println!(); } println!(); } // independence is enforced at the source, not just the audit: let own = op(&key(2), "fn f() {}"); // authored by the reviewer's own principal match reviewer.review(&own, &spec, &["C1".to_string()]) { Err(e) => println!("self-review attempt → refused: {e}"), Ok(_) => println!("self-review (should not happen)"), } } fn hex8(k: [u8; 32]) -> String { k[..4].iter().map(|b| format!("{b:02x}")).collect() }