repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by admin
smith-core/src/metadata/lifecycle.rs
Raw//! Typed inline lifecycle evidence (SMH-SPEC-PLUG0001, SMH-SPEC-SPEC0001).
use super::{
AccessError, EntryId, MutationState, StructuredValue, boolean, fields, scalar, string, unsigned,
};
use serde::Serialize;
/// Durable handler admission.
pub const INVOCATION_INTENT: &str = "lifecycle-invocation-intent";
/// Durable handler outcome, distinct from primitive mutation evidence.
pub const INVOCATION_COMPLETION: &str = "lifecycle-invocation-completion";
/// Durable primitive admission.
pub const EFFECT_INTENT: &str = "lifecycle-effect-intent";
/// Durable primitive outcome.
pub const EFFECT_COMPLETION: &str = "lifecycle-effect-completion";
/// Full inline evidence limit, never a truncation allowance.
pub const BYTES_MAX: usize = 16 * 1024;
/// Handler admission under a real durable user or submission entry.
#[derive(Serialize)]
pub struct InvocationIntent<'a> {
/// Owning user/submission identity.
pub root_entry_id: EntryId,
/// This intent entry identity.
pub invocation_id: EntryId,
/// Host boundary.
pub boundary: smith::lifecycle::Boundary,
/// Provider round.
pub round_index: u32,
/// Registered handler name.
pub handler: &'a str,
/// Actual loaded generation.
pub component: &'a crate::tools::ToolComponentProvenance,
}
/// Complete primitive input, admitted before transport.
#[derive(Serialize)]
pub struct EffectIntent<'a> {
/// Owning user/submission.
pub root_entry_id: EntryId,
/// Handler intent parent.
pub invocation_id: EntryId,
/// This intent entry identity.
pub effect_id: EntryId,
/// Zero-based handler-local ordinal.
pub ordinal: u16,
/// Selected host primitive.
pub operation: &'a str,
/// Full canonical redacted input.
pub input: &'a str,
}
/// Complete primitive output and mutation evidence.
#[derive(Serialize)]
pub struct EffectCompletion<'a> {
/// Owning user/submission.
pub root_entry_id: EntryId,
/// Handler intent parent.
pub invocation_id: EntryId,
/// Matched primitive intent.
pub effect_id: EntryId,
/// Matched ordinal.
pub ordinal: u16,
/// Primitive success.
pub ok: bool,
/// Actual primitive mutation evidence.
pub mutation_state: MutationState,
/// Full canonical redacted result.
pub output: &'a str,
}
/// Nonmutating handler outcome.
#[derive(Serialize)]
pub struct InvocationCompletion<'a> {
/// Owning user/submission.
pub root_entry_id: EntryId,
/// Matched handler intent.
pub invocation_id: EntryId,
/// Handler success, not primitive success.
pub ok: bool,
/// Full bounded redacted handler outcome.
pub output: &'a str,
}
/// Whether this metadata kind belongs to the lifecycle ledger.
pub fn reserved(kind: &str) -> bool {
matches!(
kind,
INVOCATION_INTENT | INVOCATION_COMPLETION | EFFECT_INTENT | EFFECT_COMPLETION
)
}
/// Read a required nonzero durable identity.
pub(crate) fn identity<'a>(
value: impl StructuredValue<'a>,
name: &str,
) -> Result<EntryId, AccessError> {
let id: EntryId = scalar(string(value.field(name)?.ok_or(AccessError::Malformed)?)?)?;
if id.as_u128() == 0 {
return Err(AccessError::Malformed);
}
Ok(id)
}
pub(super) fn validate<'a>(kind: &str, value: impl StructuredValue<'a>) -> Result<(), AccessError> {
let required = match kind {
INVOCATION_INTENT => 1 | 2 | 16 | 32 | 64 | 128,
EFFECT_INTENT => 1 | 2 | 4 | 8 | 256 | 512,
EFFECT_COMPLETION => 1 | 2 | 4 | 8 | 1024 | 2048 | 4096,
INVOCATION_COMPLETION => 1 | 2 | 1024 | 4096,
_ => return Err(AccessError::Malformed),
};
let mut seen = 0u16;
for field in fields(value)? {
let (name, field) = field?;
let bit = match name {
"root_entry_id" | "invocation_id" | "effect_id" => {
let id: EntryId = scalar(string(field)?)?;
if id.as_u128() == 0 {
return Err(AccessError::Malformed);
}
match name {
"root_entry_id" => 1,
"invocation_id" => 2,
_ => 4,
}
}
"ordinal" => {
if unsigned(field)? >= u64::from(super::NESTED_EFFECTS_MAX) {
return Err(AccessError::Malformed);
}
8
}
"boundary" => {
scalar::<smith::lifecycle::Boundary>(string(field)?)?;
16
}
"round_index" => {
u32::try_from(unsigned(field)?).map_err(|_| AccessError::Malformed)?;
32
}
"handler" | "operation" => {
let text = string(field)?;
if text.is_empty() || text.len() > 128 {
return Err(AccessError::Malformed);
}
if name == "handler" { 64 } else { 256 }
}
"component" => {
super::component_provenance(field)?;
128
}
"input" | "output" => {
if string(field)?.len() > BYTES_MAX {
return Err(AccessError::Malformed);
}
if name == "input" { 512 } else { 4096 }
}
"ok" => {
boolean(field)?;
1024
}
"mutation_state" => {
scalar::<MutationState>(string(field)?)?;
2048
}
_ => return Err(AccessError::Malformed),
};
if bit & required == 0 || seen & bit != 0 {
return Err(AccessError::Malformed);
}
seen |= bit;
}
if seen != required {
return Err(AccessError::Malformed);
}
Ok(())
}