Skip to main content

telltale_machine/
output_condition.rs

1//! Output-condition commit gating primitives.
2//!
3//! This module centralizes policy, metadata, and verification records for
4//! output-conditioned execution.
5
6use serde::{Deserialize, Serialize};
7
8/// Output-condition policy for deterministic commit gating.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub enum OutputConditionPolicy {
11    /// Skip output-condition gating entirely.
12    Disabled,
13    /// Accept all output-condition checks.
14    AllowAll,
15    /// Reject all output-condition checks.
16    DenyAll,
17    /// Accept only listed predicate references.
18    PredicateAllowList(Vec<String>),
19}
20
21/// Output-condition metadata checked by the ProtocolMachine kernel before committing outputs.
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23pub struct OutputConditionMeta {
24    /// Stable predicate id/hash.
25    pub predicate_ref: String,
26    /// Optional opaque witness reference.
27    pub witness_ref: Option<String>,
28    /// Opaque digest over pending output payload.
29    pub output_digest: String,
30}
31
32/// Recorded result of one output-condition verification.
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34pub struct OutputConditionCheck {
35    /// Metadata that was checked.
36    pub meta: OutputConditionMeta,
37    /// Deterministic verifier outcome.
38    pub passed: bool,
39}
40
41/// Optional output-condition metadata emitted by the host for the current step.
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43pub struct OutputConditionHint {
44    /// Stable predicate identifier (name/hash) for gating output commit.
45    pub predicate_ref: String,
46    /// Optional opaque witness reference.
47    pub witness_ref: Option<String>,
48}
49
50impl OutputConditionMeta {
51    /// Build metadata from a host hint and a deterministic output digest.
52    #[must_use]
53    pub fn from_hint(hint: OutputConditionHint, output_digest: String) -> Self {
54        Self {
55            predicate_ref: hint.predicate_ref,
56            witness_ref: hint.witness_ref,
57            output_digest,
58        }
59    }
60
61    /// Default metadata when the host does not provide a hint.
62    #[must_use]
63    pub fn default_observable(output_digest: String) -> Self {
64        Self {
65            predicate_ref: "protocol_machine.observable_output".to_string(),
66            witness_ref: None,
67            output_digest,
68        }
69    }
70}
71
72/// Deterministic output-condition verifier.
73#[must_use]
74pub fn verify_output_condition(policy: &OutputConditionPolicy, meta: &OutputConditionMeta) -> bool {
75    match policy {
76        OutputConditionPolicy::Disabled | OutputConditionPolicy::AllowAll => true,
77        OutputConditionPolicy::DenyAll => false,
78        OutputConditionPolicy::PredicateAllowList(allowed) => {
79            allowed.iter().any(|p| p == &meta.predicate_ref)
80        }
81    }
82}