use lsp_max::max_protocol::Receipt;
use std::fs;
fn write_with_receipt(path: &std::path::Path, content: &str) -> Receipt {
let hash = blake3::hash(content.as_bytes()).to_hex().to_string();
fs::write(path, content).expect("write artifact");
Receipt {
receipt_id: "rcpt-demo".to_string(),
hash,
prev_receipt_hash: None,
}
}
fn verify(path: &std::path::Path, receipt: &Receipt) -> bool {
let bytes = fs::read(path).expect("read artifact");
blake3::hash(&bytes).to_hex().to_string() == receipt.hash
}
fn main() {
let dir = tempfile::tempdir().expect("temp dir");
let artifact = dir.path().join("artifact.json");
let receipt = write_with_receipt(&artifact, r#"{"result":"value"}"#);
assert!(
verify(&artifact, &receipt),
"receipt produced from final bytes must verify against the file on disk"
);
fs::write(&artifact, r#"{"result":"TAMPERED"}"#).expect("overwrite");
assert!(
!verify(&artifact, &receipt),
"a receipt MUST fail to verify once its artifact is modified"
);
let trap_file = dir.path().join("trap.json");
let base = r#"{"data":1}"#;
let pre_hash = blake3::hash(base.as_bytes()).to_hex().to_string();
let injected = format!(r#"{{"data":1,"digest":"{pre_hash}"}}"#); fs::write(&trap_file, &injected).expect("write trap");
let stale_receipt = Receipt {
receipt_id: "rcpt-trap".to_string(),
hash: pre_hash,
prev_receipt_hash: None,
};
assert!(
!verify(&trap_file, &stale_receipt),
"circular-hash trap MUST be detectable: digest of pre-injection bytes != final file"
);
let genesis = receipt; assert!(
genesis.prev_receipt_hash.is_none(),
"genesis has no prev hash"
);
let linked = Receipt {
receipt_id: "rcpt-demo-2".to_string(),
hash: blake3::hash(b"second artifact").to_hex().to_string(),
prev_receipt_hash: Some(genesis.hash.clone()),
};
assert_eq!(
linked.prev_receipt_hash.as_deref(),
Some(genesis.hash.as_str()),
"linked receipt must reference the exact genesis hash (Merkle chain)"
);
let json = serde_json::to_string(&linked).expect("serialize");
let back: Receipt = serde_json::from_str(&json).expect("deserialize");
assert_eq!(
back.prev_receipt_hash, linked.prev_receipt_hash,
"chain link survives serde"
);
println!("WITNESS receipt_chain: 5 contract assertions held");
println!(" [1] receipt from final bytes verifies against the file");
println!(" [2] modifying the artifact makes the receipt fail to verify (tamper-evident)");
println!(" [3] the circular-hash trap is detectable (digest != final file)");
println!(" [4] genesis has no prev hash; the next receipt links the prior hash");
println!(" [5] serde roundtrip preserves the chain link");
println!();
println!("This example panics (non-zero exit) if content-addressing or chain");
println!("linkage regresses — the same blake3(final_bytes) pattern that");
println!("anti-llm-cheat-lsp/src/ocel.rs uses to write production receipts.");
}