use crate::plan::validate_input_paths;
use crate::{
Artifact, CheckOutcome, ContractError, Invocation, Subject, artifact_shape, hex, identifier,
nonempty, require, sha256,
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ProcessStatus {
Completed,
Unavailable,
TimedOut,
Interrupted,
FailedToStart,
CaptureFailed,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ReportedResult {
pub adapter: String,
pub outcome: CheckOutcome,
pub reason: String,
pub artifact: Artifact,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExecutionReceipt {
pub version: u32,
pub attempt_id: String,
pub plan_sha256: String,
pub check_id: String,
pub subject: Subject,
pub invocation: Invocation,
pub executable: String,
pub executable_sha256: Option<String>,
pub tool_version: Option<String>,
pub started_at_ms: u64,
pub finished_at_ms: u64,
pub elapsed_ms: u64,
pub process: ProcessStatus,
pub exit_code: Option<i32>,
pub signal: Option<i32>,
pub capture_complete: bool,
pub stdout: Option<Artifact>,
pub stderr: Option<Artifact>,
pub inputs_after: Vec<Artifact>,
pub observed_scope: Option<Vec<String>>,
pub observed_tests: Vec<String>,
pub reported: Option<ReportedResult>,
pub diagnostic: Option<String>,
}
impl ExecutionReceipt {
pub fn parse(bytes: &[u8]) -> Result<Self, ContractError> {
let receipt: Self = crate::from_json(bytes)?;
receipt.validate()?;
Ok(receipt)
}
pub fn validate(&self) -> Result<(), ContractError> {
require(self.version == 1, "unsupported receipt version")?;
require(
identifier(&self.attempt_id) && identifier(&self.check_id),
"invalid receipt identifier",
)?;
require(hex(&self.plan_sha256, &[64]), "invalid plan digest")?;
self.subject.validate()?;
self.invocation.validate()?;
require(
nonempty(&self.executable),
"executable identity is required",
)?;
if let Some(hash) = &self.executable_sha256 {
require(hex(hash, &[64]), "invalid executable digest")?;
}
if let Some(version) = &self.tool_version {
require(nonempty(version), "empty tool version")?;
}
require(
self.finished_at_ms >= self.started_at_ms,
"finish precedes start",
)?;
require(
!(self.exit_code.is_some() && self.signal.is_some()),
"exit code and terminating signal are mutually exclusive",
)?;
if self.process == ProcessStatus::Completed {
require(
self.exit_code.is_some() && self.signal.is_none(),
"completed execution requires an exit code",
)?;
require(
self.capture_complete && self.stdout.is_some() && self.stderr.is_some(),
"completed execution requires complete stdout/stderr",
)?;
} else {
require(
self.diagnostic.as_deref().is_some_and(nonempty),
"incomplete execution requires a diagnostic",
)?;
}
for a in self.stdout.iter().chain(self.stderr.iter()) {
artifact_shape(a)?;
}
validate_input_paths(&self.inputs_after)?;
if let Some(paths) = &self.observed_scope {
let unique: BTreeSet<_> = paths.iter().collect();
require(
unique.len() == paths.len() && paths.iter().all(|p| crate::relative_path(p)),
"invalid or duplicate observed scope",
)?;
}
let tests: BTreeSet<_> = self.observed_tests.iter().collect();
require(
tests.len() == self.observed_tests.len() && tests.iter().all(|t| nonempty(t)),
"invalid or duplicate observed tests",
)?;
if let Some(reported) = &self.reported {
require(
identifier(&reported.adapter) && nonempty(&reported.reason),
"reported assessment requires adapter and reason",
)?;
artifact_shape(&reported.artifact)?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Actor {
pub agent_id: String,
pub attempt_id: String,
pub model: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum AuditVerdict {
Supported,
Contradicted,
Inconclusive,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditReceipt {
pub expectation_id: String,
pub plan_sha256: String,
pub subject: Subject,
pub producer: Actor,
pub reviewer: Actor,
pub probe_check: String,
pub verdict: AuditVerdict,
pub observed: String,
pub artifact: Artifact,
pub captured_at_ms: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ManualObservation {
pub expectation_id: String,
pub plan_sha256: String,
pub subject: Subject,
pub observer: String,
pub observed: String,
pub verdict: AuditVerdict,
pub artifact: Artifact,
pub captured_at_ms: u64,
}
#[derive(Debug, Clone, Default)]
pub struct VerifiedArtifacts {
entries: BTreeMap<(String, String), u64>,
}
impl VerifiedArtifacts {
pub fn insert(&mut self, artifact: &Artifact, bytes: &[u8]) -> Result<(), ContractError> {
artifact_shape(artifact)?;
require(
sha256(bytes) == artifact.sha256,
"artifact bytes do not match SHA-256",
)?;
self.entries.insert(
(artifact.path.clone(), artifact.sha256.clone()),
bytes.len() as u64,
);
Ok(())
}
pub fn contains(&self, artifact: &Artifact) -> bool {
self.entries
.contains_key(&(artifact.path.clone(), artifact.sha256.clone()))
}
pub fn byte_len(&self, artifact: &Artifact) -> Option<u64> {
self.entries
.get(&(artifact.path.clone(), artifact.sha256.clone()))
.copied()
}
}