1use serde::{Deserialize, Serialize};
7
8use crate::output::{Diagnostic, FunctionReport, ObligationReport};
9
10pub const VERSION: u32 = 1;
12
13#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
15pub struct Target {
16 pub package_id: String,
18 pub name: String,
20 pub kind: Vec<String>,
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "snake_case")]
27pub enum Phase {
28 Focus,
30 Full,
32}
33
34#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36#[serde(deny_unknown_fields)]
37pub struct InputEvent {
38 pub version: u32,
40 pub event_id: String,
42 pub run_id: String,
44 pub context_sha256: String,
46 pub input_revision: String,
48 pub target: Target,
50 pub phase: Phase,
52 pub function: Option<String>,
54 pub obligation: Option<String>,
56 pub observation: Observation,
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
62#[serde(deny_unknown_fields)]
63pub struct ObligationProgress {
64 pub description: String,
66 pub completed: bool,
68}
69
70#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
72#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
73pub enum Observation {
74 PhaseStarted,
76 FunctionStarted,
78 ObligationProgress(ObligationProgress),
80 ObligationObserved(ObligationReport),
82 FunctionObserved(FunctionReport),
84 Diagnostic(Diagnostic),
86 PhaseFinished,
88 CompilerSealed,
90 Unavailable(String),
92}
93
94impl InputEvent {
95 pub fn validate(&self) -> Result<(), String> {
100 if self.version != VERSION
101 || self.event_id.is_empty()
102 || self.run_id.is_empty()
103 || self.context_sha256.is_empty()
104 || self.input_revision.is_empty()
105 || self.target.package_id.is_empty()
106 || self.target.name.is_empty()
107 || self.target.kind.is_empty()
108 {
109 return Err("SAIL0902: incomplete proof event identity".into());
110 }
111 if self.obligation.is_some() && self.function.is_none() {
112 return Err("SAIL0902: obligation event has no function identity".into());
113 }
114 if matches!(self.observation, Observation::CompilerSealed) && self.phase != Phase::Full {
115 return Err("SAIL0902: compiler seal cannot finalize a focused phase".into());
116 }
117 match &self.observation {
118 Observation::FunctionStarted | Observation::FunctionObserved(_) => {
119 if self.function.is_none() {
120 return Err("SAIL0902: function event has no function identity".into());
121 }
122 }
123 Observation::ObligationProgress(progress) => {
124 if progress.description.is_empty() || progress.description.len() > 4096 {
125 return Err("SAIL0902: invalid bounded obligation progress description".into());
126 }
127 let id = self
128 .obligation
129 .as_deref()
130 .and_then(|id| id.parse::<sail_ledger::ObligationId>().ok());
131 if !id.as_ref().is_some_and(|id| {
132 Some(id.function.as_str()) == self.function.as_deref()
133 && sail_ledger::ObligationKind::is_query_kind(&id.kind.0)
134 }) {
135 return Err(
136 "SAIL0902: query progress requires an exact obligation identity".into(),
137 );
138 }
139 }
140 Observation::ObligationObserved(report)
141 if self.obligation.as_deref() != Some(&report.id) =>
142 {
143 return Err("SAIL0902: obligation identity disagrees with evidence".into());
144 }
145 _ => {}
146 }
147 if let Observation::FunctionObserved(report) = &self.observation {
148 if self.function.as_deref() != Some(&report.function) {
149 return Err("SAIL0902: function identity disagrees with evidence".into());
150 }
151 if report.establishment != crate::output::EstablishmentName::Provisional
152 || report.verdict != crate::output::VerdictName::Provisional
153 || report.evidence_tier.is_some()
154 {
155 return Err("SAIL0902: live function evidence must remain provisional".into());
156 }
157 }
158 Ok(())
159 }
160}
161
162#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
164pub struct Event {
165 pub version: u32,
167 pub stream_id: String,
169 pub sequence: u64,
171 pub input: InputEvent,
173}
174
175#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
177pub struct Cursor {
178 pub stream_id: String,
180 pub after: u64,
182}
183
184#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
186pub struct Page {
187 pub version: u32,
189 pub next: Cursor,
191 pub events: Vec<Event>,
193 pub terminal: bool,
195 pub gap: Option<String>,
197}