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    ThreadHandoff {
21        item: Uuid,
22        started_at: i64,
23    },
24    Continuation {
25        attempt: Uuid,
26    },
27    // Keep old single-input continuations wire compatible. Older daemons must
28    // reject this proof in full instead of dropping its additional input IDs.
29    SteeredContinuation {
30        attempt: Uuid,
31        root: Uuid,
32        parent: Uuid,
33        parent_failed_at: i64,
34        started_at: i64,
35        additional_client_ids: Vec<String>,
36    },
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use serde_json::json;
43
44    #[test]
45    fn legacy_operator_origins_keep_the_single_input_wire_format() {
46        let turn = Uuid::new_v4();
47        let mut wire = json!({"kind":"operator","turn":turn,"started_at":42});
48        for client_id in [None, Some("first")] {
49            if let Some(id) = client_id {
50                wire["client_id"] = json!(id);
51            }
52            let origin: ChatOrigin = serde_json::from_value(wire.clone()).unwrap();
53            assert_eq!(
54                origin,
55                ChatOrigin::Operator {
56                    turn,
57                    started_at: 42,
58                    client_id: client_id.map(String::from),
59                    additional_client_ids: vec![],
60                }
61            );
62            let mut expected = wire.clone();
63            expected["client_id"] = json!(client_id);
64            assert_eq!(serde_json::to_value(origin).unwrap(), expected);
65        }
66    }
67
68    #[test]
69    fn multi_input_operator_origin_preserves_all_client_ids_in_order() {
70        let wire = json!({"kind":"operator","turn":Uuid::new_v4(),"started_at":42,
71                          "client_id":"first","additional_client_ids":["second","third"]});
72        let origin: ChatOrigin = serde_json::from_value(wire.clone()).unwrap();
73        assert_eq!(serde_json::to_value(origin).unwrap(), wire);
74    }
75}