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    /// A named host handoff whose every user input was a journaled notification.
23    DirectHandoff {
24        item: Uuid,
25        proof: ForegroundInputs,
26    },
27    ThreadHandoff {
28        item: Uuid,
29        started_at: i64,
30    },
31    Continuation {
32        attempt: Uuid,
33    },
34    // Keep old single-input continuations wire compatible. Older daemons must
35    // reject this proof in full instead of dropping its additional input IDs.
36    SteeredContinuation {
37        attempt: Uuid,
38        root: Uuid,
39        parent: Uuid,
40        parent_failed_at: i64,
41        started_at: i64,
42        additional_client_ids: Vec<String>,
43    },
44}
45
46#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
47#[serde(deny_unknown_fields)]
48pub struct ForegroundInputs {
49    pub thread: Uuid,
50    pub turn: Uuid,
51    pub started_at: i64,
52    pub inputs: Vec<ForegroundInput>,
53}
54
55#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
56#[serde(deny_unknown_fields)]
57pub struct ForegroundInput {
58    pub client: Uuid,
59    pub sha256: String,
60    pub received_at_ms: i64,
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use serde_json::json;
67
68    #[test]
69    fn legacy_operator_origins_keep_the_single_input_wire_format() {
70        let turn = Uuid::new_v4();
71        let mut wire = json!({"kind":"operator","turn":turn,"started_at":42});
72        for client_id in [None, Some("first")] {
73            if let Some(id) = client_id {
74                wire["client_id"] = json!(id);
75            }
76            let origin: ChatOrigin = serde_json::from_value(wire.clone()).unwrap();
77            assert_eq!(
78                origin,
79                ChatOrigin::Operator {
80                    turn,
81                    started_at: 42,
82                    client_id: client_id.map(String::from),
83                    additional_client_ids: vec![],
84                }
85            );
86            let mut expected = wire.clone();
87            expected["client_id"] = json!(client_id);
88            assert_eq!(serde_json::to_value(origin).unwrap(), expected);
89        }
90    }
91
92    #[test]
93    fn multi_input_operator_origin_preserves_all_client_ids_in_order() {
94        let wire = json!({"kind":"operator","turn":Uuid::new_v4(),"started_at":42,
95                          "client_id":"first","additional_client_ids":["second","third"]});
96        let origin: ChatOrigin = serde_json::from_value(wire.clone()).unwrap();
97        assert_eq!(serde_json::to_value(origin).unwrap(), wire);
98    }
99}