1use std::collections::BTreeSet;
20use std::fmt;
21
22use serde::{Deserialize, Deserializer, Serialize, Serializer};
23
24use super::metrics_v2::{KeyId, MAX_COUNT, SignatureHex, TimeStamp, VaultId};
25use super::metrics_v2::{closed_string, schema_const};
26use crate::ValidationError;
27
28pub const RECEIPTS_V2_SCHEMA: &str = "leanctx.cloud.receipts/v2";
29pub const RECEIPTS_V2_ACK_SCHEMA: &str = "leanctx.cloud.receipts-ack/v2";
30pub const ENGINE_RECEIPT_V2_SCHEMA: &str = "leanctx.instance-receipt/v2";
31pub const RECEIPTS_DOMAIN: &str = "leanctx.cloud.receipts.v2\n";
33pub const RECEIPTS_ROUTE: &str = "/receipts";
35pub const RECEIPT_ENGINE_ID: &str = "lean-ctx-local";
37pub const RECEIPT_CAPABILITY_ID: &str = "capability://leanctx/context-optimization";
39pub const HMAC_PREFIX: &str = "hmac-sha256:";
41
42pub const MAX_RECEIPTS_V2: usize = 256;
43pub const MAX_RECEIPTS_BODY_BYTES: usize = 1 << 20;
44pub const MAX_MEASUREMENTS: usize = 32;
45pub const MAX_SOURCE_COUNT: u16 = 256;
46
47fn is_lower_hex(value: &str) -> bool {
48 value
49 .bytes()
50 .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
51}
52
53fn is_hmac(value: &str) -> bool {
54 value
55 .strip_prefix(HMAC_PREFIX)
56 .is_some_and(|hex| hex.len() == 64 && is_lower_hex(hex))
57}
58
59fn is_sha256(value: &str) -> bool {
60 value
61 .strip_prefix("sha256:")
62 .is_some_and(|hex| hex.len() == 64 && is_lower_hex(hex))
63}
64
65fn is_receipt_id(value: &str) -> bool {
66 value.strip_prefix("rcpt_").is_some_and(|rest| {
67 (1..=64).contains(&rest.len())
68 && rest
69 .bytes()
70 .all(|b| b.is_ascii_digit() || b.is_ascii_lowercase())
71 })
72}
73
74fn is_invocation_id(value: &str) -> bool {
75 (32..=64).contains(&value.len()) && is_lower_hex(value)
76}
77
78fn is_semver(value: &str) -> bool {
80 let (core, pre) = match value.split_once('-') {
81 Some((core, pre)) => (core, Some(pre)),
82 None => (value, None),
83 };
84 let parts: Vec<&str> = core.split('.').collect();
85 let digits =
86 |p: &str, max: usize| (1..=max).contains(&p.len()) && p.bytes().all(|b| b.is_ascii_digit());
87 parts.len() == 3
88 && digits(parts[0], 4)
89 && digits(parts[1], 4)
90 && digits(parts[2], 6)
91 && pre.is_none_or(|p| {
92 (1..=32).contains(&p.len()) && p.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'.')
93 })
94}
95
96fn is_measurement_name(value: &str) -> bool {
98 let shape = (1..=64).contains(&value.len())
99 && value
100 .bytes()
101 .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'_' | b'-'));
102 let mut run = 0usize;
103 let token_like = value.bytes().any(|b| {
104 run = if b.is_ascii_alphanumeric() {
105 run + 1
106 } else {
107 0
108 };
109 run >= 24
110 });
111 shape && !token_like
112}
113
114closed_string!(
115 ReceiptHmac,
117 is_hmac,
118 "receipt hmac"
119);
120closed_string!(
121 Sha256Ref,
123 is_sha256,
124 "sha256 digest"
125);
126closed_string!(
127 ReceiptIdV2,
129 is_receipt_id,
130 "receipt id"
131);
132closed_string!(
133 InvocationIdV2,
135 is_invocation_id,
136 "invocation id"
137);
138closed_string!(
139 ReceiptSemver,
141 is_semver,
142 "version"
143);
144closed_string!(
145 MeasurementName,
147 is_measurement_name,
148 "measurement name"
149);
150
151schema_const!(ReceiptsSchema, RECEIPTS_V2_SCHEMA);
152schema_const!(ReceiptsAckSchema, RECEIPTS_V2_ACK_SCHEMA);
153schema_const!(EngineReceiptSchema, ENGINE_RECEIPT_V2_SCHEMA);
154schema_const!(EngineIdConst, RECEIPT_ENGINE_ID);
155schema_const!(CapabilityIdConst, RECEIPT_CAPABILITY_ID);
156schema_const!(EngineKindConst, "instance");
157
158#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
160pub struct SchemaVersionOne;
161
162impl Serialize for SchemaVersionOne {
163 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
164 serializer.serialize_u8(1)
165 }
166}
167
168impl<'de> Deserialize<'de> for SchemaVersionOne {
169 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
170 (u64::deserialize(deserializer)? == 1)
171 .then_some(Self)
172 .ok_or_else(|| serde::de::Error::custom("schema_version must be 1"))
173 }
174}
175
176#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
177#[serde(rename_all = "snake_case")]
178pub enum MeasurementUnit {
179 Tokens,
180 Bytes,
181 Ms,
182 Count,
183 Lines,
184 RatioPpm,
185}
186
187impl MeasurementUnit {
188 pub fn of(unit: &str) -> Option<Self> {
190 Some(match unit {
191 "tokens" => Self::Tokens,
192 "bytes" => Self::Bytes,
193 "ms" => Self::Ms,
194 "count" => Self::Count,
195 "lines" => Self::Lines,
196 "ratio_ppm" => Self::RatioPpm,
197 _ => return None,
198 })
199 }
200}
201
202#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
203#[serde(rename_all = "snake_case")]
204pub enum Classification {
205 Measured,
206 Estimated,
207 Unavailable,
208}
209
210#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
211#[serde(rename_all = "snake_case")]
212pub enum AdmissionDecision {
213 Admitted,
214 Rejected,
215}
216
217#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
218#[serde(rename_all = "snake_case")]
219pub enum ObservationStatus {
220 Succeeded,
221 Degraded,
222 Rejected,
223 Failed,
224}
225
226#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
227#[serde(rename_all = "snake_case")]
228pub enum FailureCode {
229 PolicyRejected,
230 SourceUnavailable,
231 SourceIntegrityMismatch,
232 ResourceLimit,
233 UnsupportedOperation,
234 Internal,
235}
236
237#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
238#[serde(deny_unknown_fields)]
239pub struct ReceiptEngineV2 {
240 pub instance_id: EngineIdConst,
241 pub instance_version: ReceiptSemver,
242}
243
244#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
245#[serde(deny_unknown_fields)]
246pub struct ReceiptOperationV2 {
247 pub capability_id: CapabilityIdConst,
248 pub capability_version: ReceiptSemver,
249}
250
251#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
252#[serde(deny_unknown_fields)]
253pub struct PolicyAdmissionV2 {
254 pub decision: AdmissionDecision,
255 pub policy_digest: Option<Sha256Ref>,
257}
258
259#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
260#[serde(deny_unknown_fields)]
261pub struct ReceiptInvocationV2 {
262 pub schema_version: SchemaVersionOne,
263 pub invocation_id: InvocationIdV2,
264 pub instance: ReceiptEngineV2,
265 pub operation: ReceiptOperationV2,
266 pub input_hmac: ReceiptHmac,
267 pub source_count: u16,
269 #[serde(default, skip_serializing_if = "Option::is_none")]
273 pub session_ref: Option<ReceiptHmac>,
274 pub policy_admission: PolicyAdmissionV2,
275}
276
277#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
278#[serde(deny_unknown_fields)]
279pub struct MeasurementV2 {
280 pub name: MeasurementName,
281 pub unit: MeasurementUnit,
282 pub classification: Classification,
283 #[serde(default, skip_serializing_if = "Option::is_none")]
284 pub value: Option<u64>,
285}
286
287#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
288#[serde(deny_unknown_fields)]
289pub struct FailureV2 {
290 pub code: FailureCode,
291 pub retryable_by_host: bool,
292}
293
294#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
295#[serde(deny_unknown_fields)]
296pub struct ReceiptObservationV2 {
297 pub schema_version: SchemaVersionOne,
298 pub invocation_id: InvocationIdV2,
299 pub status: ObservationStatus,
300 pub output_hmac: Option<ReceiptHmac>,
301 pub source_count: u16,
303 pub measurements: Vec<MeasurementV2>,
304 pub failure: Option<FailureV2>,
305}
306
307#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
309#[serde(deny_unknown_fields)]
310pub struct EngineReceiptV2 {
311 pub schema: EngineReceiptSchema,
312 pub invocation: ReceiptInvocationV2,
313 pub observation: ReceiptObservationV2,
314}
315
316impl EngineReceiptV2 {
317 pub fn validate(&self) -> Result<(), ValidationError> {
318 let (i, o) = (&self.invocation, &self.observation);
319 if i.invocation_id != o.invocation_id {
320 return Err(ValidationError::new("receipt: invocation_id differs"));
321 }
322 if !(1..=MAX_SOURCE_COUNT).contains(&i.source_count) || o.source_count > MAX_SOURCE_COUNT {
323 return Err(ValidationError::new("receipt: source_count out of range"));
324 }
325 if o.measurements.len() > MAX_MEASUREMENTS
326 || o.measurements
327 .iter()
328 .any(|m| m.value.is_some_and(|v| v > MAX_COUNT))
329 {
330 return Err(ValidationError::new("receipt: measurements"));
331 }
332 Ok(())
333 }
334
335 pub fn digest(&self) -> Result<Sha256Ref, ValidationError> {
337 use sha2::Digest as _;
338 let value = serde_json::to_value(self)
339 .map_err(|e| ValidationError::new(format!("receipt: encode: {e}")))?;
340 let bytes = super::canonical_json(&value)?;
341 let hex: String = sha2::Sha256::digest(&bytes)
342 .iter()
343 .map(|b| format!("{b:02x}"))
344 .collect();
345 Sha256Ref::new(&format!("sha256:{hex}"))
346 .ok_or_else(|| ValidationError::new("receipt: digest"))
347 }
348}
349
350#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
351#[serde(deny_unknown_fields)]
352pub struct ReceiptItemV2 {
353 pub receipt_id: ReceiptIdV2,
354 pub kind: EngineKindConst,
355 pub vault_id: VaultId,
356 pub created_at: TimeStamp,
357 pub digest: Sha256Ref,
358 pub document: EngineReceiptV2,
359}
360
361impl ReceiptItemV2 {
362 pub fn new(
364 receipt_id: ReceiptIdV2,
365 vault_id: VaultId,
366 created_at: TimeStamp,
367 document: EngineReceiptV2,
368 ) -> Result<Self, ValidationError> {
369 document.validate()?;
370 Ok(Self {
371 receipt_id,
372 kind: EngineKindConst,
373 vault_id,
374 created_at,
375 digest: document.digest()?,
376 document,
377 })
378 }
379
380 pub fn validate(&self) -> Result<(), ValidationError> {
381 self.document.validate()?;
382 if self.document.digest()? != self.digest {
383 return Err(ValidationError::new(
384 "receipt: digest does not bind the document",
385 ));
386 }
387 Ok(())
388 }
389}
390
391#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
393#[serde(deny_unknown_fields)]
394pub struct ReceiptsBodyV2 {
395 pub schema: ReceiptsSchema,
396 pub receipts: Vec<ReceiptItemV2>,
397}
398
399impl ReceiptsBodyV2 {
400 pub fn validate(&self) -> Result<(), ValidationError> {
401 if self.receipts.is_empty() || self.receipts.len() > MAX_RECEIPTS_V2 {
402 return Err(ValidationError::new("receipts: 1-256 per batch"));
403 }
404 let mut ids = BTreeSet::new();
405 if !self
406 .receipts
407 .iter()
408 .all(|r| ids.insert(r.receipt_id.as_str()))
409 {
410 return Err(ValidationError::new("receipts: duplicate receipt_id"));
411 }
412 self.receipts.iter().try_for_each(ReceiptItemV2::validate)
413 }
414
415 pub fn signing_input(&self, key_id: &KeyId) -> Result<Vec<u8>, ValidationError> {
418 let mut value = serde_json::to_value(self)
419 .map_err(|e| ValidationError::new(format!("receipts: encode: {e}")))?;
420 value["key_id"] = serde_json::Value::String(key_id.as_str().to_string());
421 super::signing_input(RECEIPTS_DOMAIN, &value)
422 }
423
424 pub fn signed(self, key_id: KeyId, signature: SignatureHex) -> ReceiptsRequestV2 {
425 ReceiptsRequestV2 {
426 schema: self.schema,
427 receipts: self.receipts,
428 key_id,
429 signature,
430 }
431 }
432}
433
434#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
436#[serde(deny_unknown_fields)]
437pub struct ReceiptsRequestV2 {
438 pub schema: ReceiptsSchema,
439 pub receipts: Vec<ReceiptItemV2>,
440 pub key_id: KeyId,
441 pub signature: SignatureHex,
442}
443
444impl ReceiptsRequestV2 {
445 pub fn body(&self) -> ReceiptsBodyV2 {
446 ReceiptsBodyV2 {
447 schema: self.schema,
448 receipts: self.receipts.clone(),
449 }
450 }
451
452 pub fn validate(&self) -> Result<(), ValidationError> {
453 self.body().validate()
454 }
455}
456
457#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
458#[serde(deny_unknown_fields)]
459pub struct ReceiptsAckV2 {
460 pub schema: ReceiptsAckSchema,
461 pub accepted: u16,
462 pub duplicates: u16,
463}
464
465#[cfg(test)]
466mod tests {
467 use super::*;
468
469 #[test]
470 fn closed_strings_follow_the_contract_patterns() {
471 assert!(ReceiptHmac::new(&format!("hmac-sha256:{}", "a".repeat(64))).is_some());
472 for bad in [
473 format!("sha256:{}", "a".repeat(64)),
474 format!("hmac-sha256:{}", "A".repeat(64)),
475 "hmac-sha256:".to_string(),
476 ] {
477 assert!(ReceiptHmac::new(&bad).is_none(), "{bad}");
478 }
479 assert!(ReceiptIdV2::new("rcpt_01jb8z5").is_some());
480 assert!(ReceiptIdV2::new("engine-receipt-1").is_none());
481 assert!(InvocationIdV2::new(&"0".repeat(32)).is_some());
482 assert!(InvocationIdV2::new(&"0".repeat(31)).is_none());
483 assert!(InvocationIdV2::new("src/main.rs").is_none());
484 for good in ["3.12.0", "1.0.0", "3.12.0-rc.1"] {
485 assert!(ReceiptSemver::new(good).is_some(), "{good}");
486 }
487 for bad in ["3.12", "3.12.0-", "3.12.0-a b", "v3.12.0"] {
488 assert!(ReceiptSemver::new(bad).is_none(), "{bad}");
489 }
490 assert!(MeasurementName::new("tokens_baseline").is_some());
491 for bad in ["skprojabcdefghijklmnopqrstuvwxyz", "/etc/passwd", "A", ""] {
492 assert!(MeasurementName::new(bad).is_none(), "{bad}");
493 }
494 }
495}