Skip to main content

multicast_codemode/
checkpoint.rs

1use base64::Engine;
2use serde::{Deserialize, Serialize};
3
4/// Engine-owned continuation saved independently of the model-facing execution record.
5///
6/// A checkpoint contains trusted bytecode. An executor must verify `identity`
7/// before loading it and reject incompatible data instead of replaying source.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct ExecutionCheckpoint {
10    /// Executor build and complete program fingerprint.
11    pub identity: String,
12    /// Opaque engine bytes, encoded as base64 on JSON transports.
13    #[serde(with = "bytes")]
14    pub bytes: Vec<u8>,
15}
16
17mod bytes {
18    use super::*;
19
20    pub fn serialize<S: serde::Serializer>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
21        serializer.serialize_str(&base64::engine::general_purpose::STANDARD.encode(value))
22    }
23
24    pub fn deserialize<'de, D: serde::Deserializer<'de>>(
25        deserializer: D,
26    ) -> Result<Vec<u8>, D::Error> {
27        let value = String::deserialize(deserializer)?;
28        base64::engine::general_purpose::STANDARD
29            .decode(value)
30            .map_err(serde::de::Error::custom)
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn checkpoint_transport_uses_literal_base64_and_rejects_invalid_bytes() {
40        let value = ExecutionCheckpoint {
41            identity: "fixture".into(),
42            bytes: vec![0, 1, 255],
43        };
44        let wire = serde_json::json!({"identity":"fixture","bytes":"AAH/"});
45        assert_eq!(serde_json::to_value(&value).unwrap(), wire);
46        assert_eq!(
47            serde_json::from_value::<ExecutionCheckpoint>(wire).unwrap(),
48            value
49        );
50        assert!(
51            serde_json::from_str::<ExecutionCheckpoint>(r#"{"identity":"fixture","bytes":"!"}"#)
52                .is_err()
53        );
54    }
55}