pub mod categories;
pub mod limits;
pub mod ownership;
pub use categories::{CategoryKey, Entry, Family, FrozenShape, Inventory};
pub use limits::{DeclaredBytes, Limits, Projection};
pub use ownership::{Ownership, Transition};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("invalid frozen identity: {0}")]
Identity(String),
#[error("invalid or incomplete category: {0}")]
Category(String),
#[error("unknown required bound: {0}")]
Unknown(String),
#[error("checked memory arithmetic overflow")]
Overflow,
#[error("memory limit exceeded: {0}")]
Limit(&'static str),
#[error("invalid ownership event: {0}")]
Ownership(&'static str),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FrozenIdentity {
pub artifact_sha256: String,
pub workload_sha256: String,
pub transition_plan_sha256: String,
}
impl FrozenIdentity {
pub fn validate(&self) -> Result<(), Error> {
for hash in [
&self.artifact_sha256,
&self.workload_sha256,
&self.transition_plan_sha256,
] {
if hash.len() != 64 || !hash.bytes().all(|c| c.is_ascii_hexdigit()) {
return Err(Error::Identity("expected SHA-256 identity".into()));
}
}
Ok(())
}
}
#[derive(Debug, Serialize)]
pub struct Receipt {
schema: &'static str,
evidence_scope: &'static str,
runtime_qualified: bool,
identity: FrozenIdentity,
shape: FrozenShape,
inventory: Vec<(CategoryKey, Entry)>,
limits: Limits,
projection: Projection,
transition: Transition,
}
impl Receipt {
pub fn declared(
inventory: &Inventory,
expected: &FrozenShape,
limits: &Limits,
ownership: &Ownership,
) -> Result<Self, Error> {
inventory.require_shape(expected)?;
ownership.require_identity(&expected.identity)?;
limits.require_family(expected.family)?;
let projection = limits.project(inventory.total()?)?;
let transition = ownership.snapshot();
if !projection.bytes.covers(transition.peak) {
return Err(Error::Ownership("trace peak exceeds inventory projection"));
}
Ok(Self {
schema: "hf2q.memory-projection.v2",
evidence_scope: "caller_declared_model_free_projection_only",
runtime_qualified: false,
identity: expected.identity.clone(),
shape: expected.clone(),
inventory: inventory.entries(),
limits: limits.clone(),
projection,
transition,
})
}
}
#[cfg(test)]
mod tests;