Skip to main content

gent_protocol/
babysitter.rs

1//! Host attribution for ordinary conversation recovery.
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4
5pub const CHAT_FAILURE_MAX_AGE: i64 = 10 * 60;
6pub const CHAT_CHAIN_MAX_AGE: i64 = 30 * 60;
7/// Maximum number of client IDs on one chat claim, including the original.
8pub const MAX_CHAT_INPUTS: usize = 64;
9
10#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
11#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
12pub enum ChatOrigin {
13    Operator {
14        turn: Uuid,
15        started_at: i64,
16        client_id: Option<String>,
17        #[serde(default, skip_serializing_if = "Vec::is_empty")]
18        additional_client_ids: Vec<String>,
19    },
20    /// Ordered, source-paired admissions on a native user-started root.
21    ForegroundInputs(ForegroundInputs),
22    ThreadHandoff {
23        item: Uuid,
24        started_at: i64,
25    },
26    Continuation {
27        attempt: Uuid,
28    },
29    // Keep old single-input continuations wire compatible. Older daemons must
30    // reject this proof in full instead of dropping its additional input IDs.
31    SteeredContinuation {
32        attempt: Uuid,
33        root: Uuid,
34        parent: Uuid,
35        parent_failed_at: i64,
36        started_at: i64,
37        additional_client_ids: Vec<String>,
38    },
39}
40
41#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
42#[serde(deny_unknown_fields)]
43pub struct ForegroundInputs {
44    pub thread: Uuid,
45    pub turn: Uuid,
46    pub started_at: i64,
47    pub inputs: Vec<ForegroundInput>,
48}
49
50#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
51#[serde(deny_unknown_fields)]
52pub struct ForegroundInput {
53    pub client: Uuid,
54    pub sha256: String,
55    pub received_at_ms: i64,
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use serde_json::json;
62
63    #[test]
64    fn legacy_operator_origins_keep_the_single_input_wire_format() {
65        let turn = Uuid::new_v4();
66        let mut wire = json!({"kind":"operator","turn":turn,"started_at":42});
67        for client_id in [None, Some("first")] {
68            if let Some(id) = client_id {
69                wire["client_id"] = json!(id);
70            }
71            let origin: ChatOrigin = serde_json::from_value(wire.clone()).unwrap();
72            assert_eq!(
73                origin,
74                ChatOrigin::Operator {
75                    turn,
76                    started_at: 42,
77                    client_id: client_id.map(String::from),
78                    additional_client_ids: vec![],
79                }
80            );
81            let mut expected = wire.clone();
82            expected["client_id"] = json!(client_id);
83            assert_eq!(serde_json::to_value(origin).unwrap(), expected);
84        }
85    }
86
87    #[test]
88    fn multi_input_operator_origin_preserves_all_client_ids_in_order() {
89        let wire = json!({"kind":"operator","turn":Uuid::new_v4(),"started_at":42,
90                          "client_id":"first","additional_client_ids":["second","third"]});
91        let origin: ChatOrigin = serde_json::from_value(wire.clone()).unwrap();
92        assert_eq!(serde_json::to_value(origin).unwrap(), wire);
93    }
94}