open-standards/joule-code/joule-code-rs/crates/joule-code-reviewer/examples/gen-fixture.rs
Transaction Science 7f1e78577e tooling: meter-cmd (energy bracket) + gen-fixture (review fixtures)
Two small examples used to run and measure a real model-backed review:

- sandbox-meter meter_cmd: a `time`-like ENERGY bracket — measures the
  machine's IOReport energy over an external command's execution, plus an
  idle baseline of equal length so the workload's marginal draw is honest
  (machine-wide counters, the caveat printed not hidden). `cargo run
  --release --example meter_cmd -- <program> [args…]`.

- joule-code-reviewer gen-fixture: writes a `.joule/` review fixture (a
  signed spec + ops, one clean and one with a defect) so joule-review can be
  driven against a real model. `cargo run --example gen-fixture -- <dir>`.

Clippy clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-12 07:04:06 -04:00

55 lines
2.4 KiB
Rust

//! Generate a `.joule/` review fixture (signed spec + ops) in a workspace dir,
//! for driving `joule-review` against a real model.
//!
//! Run: `cargo run --example gen-fixture -- <workspace-dir>`
use ed25519_dalek::SigningKey;
use joule_code_cargo::{Anchor, CodingOp, Edit, SignedOp};
use joule_code_schema::{ContentId, Tier};
use joule_code_spec::{Clause, ClauseKind, Spec, ALG_ED25519 as SALG};
fn op(author: &SigningKey, intent: &str, path: &str, body: &str) -> SignedOp {
CodingOp {
alg: 0x01,
prior: ContentId::of_bytes(body.as_bytes()),
edits: vec![Edit { path: path.into(), anchor: Anchor::ByteRange { start: 0, end: 0 }, replacement: body.as_bytes().to_vec() }],
author: author.verifying_key().to_bytes(),
intent: intent.into(),
tier: Tier::L3,
}
.sign(author)
.unwrap()
}
fn main() {
let dir = std::path::PathBuf::from(std::env::args().nth(1).expect("usage: gen-fixture <dir>"));
let joule = dir.join(".joule");
std::fs::create_dir_all(&joule).unwrap();
let author = SigningKey::from_bytes(&[11u8; 32]); // distinct from the reviewer's identity
let spec = Spec {
alg: SALG,
intent: "a small key/value store with safe parsing".into(),
clauses: vec![
Clause { id: "C1".into(), text: "get(key) returns the stored value or None".into(), kind: ClauseKind::Requirement },
Clause { id: "C2".into(), text: "parsing untrusted input must not execute it".into(), kind: ClauseKind::Constraint },
],
parent: None,
author: author.verifying_key().to_bytes(),
}
.sign(&author)
.unwrap();
std::fs::write(joule.join("spec.adopted.json"), serde_json::to_string_pretty(&spec).unwrap()).unwrap();
let ops = vec![
// a clean op that implements C1
op(&author, "C1: implement get", "src/store.rs",
"pub fn get(&self, key: &str) -> Option<&str> { self.map.get(key).map(String::as_str) }"),
// an op claiming C2 but parsing untrusted input by eval — the model should catch it
op(&author, "C2: parse config", "src/parse.rs",
"pub fn parse(input: &str) -> Value { eval(input) /* run the config */ }"),
];
std::fs::write(joule.join("ops.json"), serde_json::to_string_pretty(&ops).unwrap()).unwrap();
println!("wrote {} (spec.adopted.json + {} ops)", joule.display(), ops.len());
}