stateknot_runtime/
cancellation_schema.rs1use serde_json::Value;
7use stateknot_core::{Digest, SchemaId, SchemaIdError, SchemaReference, Version};
8use thiserror::Error;
9
10use crate::{JsonSchemaRegistryBuilder, JsonSchemaRegistryError};
11
12pub const STANDARD_AGENT_CANCELLATION_EVENT_SCHEMA_ID: &str =
14 "https://stknot.com/schemas/runtime/agent-cancellation-event/1.0.0";
15
16const DOCUMENT: &str = include_str!("../schemas/agent-cancellation-event-1.0.0.json");
17
18pub fn standard_agent_cancellation_event_schema()
26-> Result<(SchemaReference, Value), StandardAgentCancellationSchemaError> {
27 let document = serde_json::from_str::<Value>(DOCUMENT)
28 .map_err(|_| StandardAgentCancellationSchemaError::EmbeddedDocument)?;
29 let canonical = serde_json_canonicalizer::to_vec(&document)
30 .map_err(|_| StandardAgentCancellationSchemaError::Canonicalization)?;
31 let id = STANDARD_AGENT_CANCELLATION_EVENT_SCHEMA_ID
32 .parse::<SchemaId>()
33 .map_err(StandardAgentCancellationSchemaError::identifier)?;
34 let reference = SchemaReference::new(id, Version::new(1, 0, 0), Digest::sha256(canonical));
35 Ok((reference, document))
36}
37
38pub fn register_standard_agent_cancellation_event_schema(
45 builder: &mut JsonSchemaRegistryBuilder,
46) -> Result<SchemaReference, StandardAgentCancellationSchemaRegistrationError> {
47 let (reference, document) = standard_agent_cancellation_event_schema()?;
48 builder.register(reference.clone(), document)?;
49 Ok(reference)
50}
51
52#[derive(Clone, Debug, Eq, Error, PartialEq)]
54#[non_exhaustive]
55pub enum StandardAgentCancellationSchemaError {
56 #[error("embedded agent cancellation event schema is invalid JSON")]
58 EmbeddedDocument,
59 #[error("embedded agent cancellation event schema cannot be canonicalized")]
61 Canonicalization,
62 #[error("embedded agent cancellation event schema identifier is invalid: {source}")]
64 Identifier {
65 #[source]
67 source: SchemaIdError,
68 },
69}
70
71impl StandardAgentCancellationSchemaError {
72 const fn identifier(source: SchemaIdError) -> Self {
73 Self::Identifier { source }
74 }
75}
76
77#[derive(Clone, Debug, Eq, Error, PartialEq)]
79#[non_exhaustive]
80pub enum StandardAgentCancellationSchemaRegistrationError {
81 #[error(transparent)]
83 Definition(#[from] StandardAgentCancellationSchemaError),
84 #[error(transparent)]
86 Registry(#[from] JsonSchemaRegistryError),
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 use crate::JsonSchemaRegistryLimits;
93 use serde_json::json;
94 use stateknot_core::BoundedJson;
95
96 #[test]
97 fn standard_schema_is_digest_pinned_and_registry_valid() {
98 let (reference, document) = standard_agent_cancellation_event_schema().unwrap();
99 assert_eq!(
100 reference.id().as_str(),
101 STANDARD_AGENT_CANCELLATION_EVENT_SCHEMA_ID
102 );
103 assert_eq!(reference.version(), Version::new(1, 0, 0));
104
105 let mut builder = JsonSchemaRegistryBuilder::new(JsonSchemaRegistryLimits::default());
106 builder.register(reference.clone(), document).unwrap();
107 let registry = builder.build().unwrap();
108 assert!(registry.contains(&reference));
109 }
110
111 #[test]
112 fn standard_schema_accepts_only_the_closed_confirmation_shape() {
113 let (reference, document) = standard_agent_cancellation_event_schema().unwrap();
114 let mut builder = JsonSchemaRegistryBuilder::new(JsonSchemaRegistryLimits::default());
115 builder.register(reference.clone(), document).unwrap();
116 let registry = builder.build().unwrap();
117 let valid = json!({
118 "operation": "agent_cancellation_confirmed",
119 "graph_digest": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
120 "checkpoint_id": "01912345-6789-7abc-8def-0123456789ab",
121 "superstep": "1",
122 "failure_id": "01912345-6789-7abc-8def-0123456789ac"
123 });
124 registry
125 .validate_bounded(
126 &reference,
127 &BoundedJson::try_from_value(valid.clone()).unwrap(),
128 )
129 .unwrap();
130
131 let mut leaked = valid;
132 leaked["usage"] = json!({"model_turns": 1});
133 assert!(
134 registry
135 .validate_bounded(&reference, &BoundedJson::try_from_value(leaked).unwrap())
136 .is_err()
137 );
138 }
139}