Skip to main content

talos_sync_types/
facts.rs

1//! Shared facts projected from existing Talos domain objects.
2//!
3//! This module owns synchronization envelopes only. Canonical message, result,
4//! configuration and error shapes are generated from their existing owners.
5
6use serde::{Deserialize, Serialize};
7
8use crate::{generated as wire, SyncResourceKey};
9
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase", deny_unknown_fields)]
12pub struct SessionExecutionBinding {
13    pub computer_id: String,
14    pub agent_id: String,
15    pub workspace_id: String,
16    pub workspace_user_id: String,
17}
18
19#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
20#[serde(
21    tag = "type",
22    rename_all = "camelCase",
23    rename_all_fields = "camelCase",
24    deny_unknown_fields
25)]
26pub enum SyncEventKey {
27    Agent {
28        identity: wire::AgentTurnRef,
29        sequence: u64,
30    },
31    Notice {
32        peer_id: String,
33        source_event_id: String,
34    },
35}
36
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38#[serde(rename_all = "camelCase", deny_unknown_fields)]
39pub struct SyncTurnMetadata {
40    pub identity: wire::AgentTurnRef,
41    pub coordinator_id: String,
42    pub previous_turn_id: Option<String>,
43}
44
45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
46#[serde(rename_all = "camelCase", deny_unknown_fields)]
47pub struct SyncSessionSnapshot {
48    pub session: wire::SessionInfo,
49    pub conf: wire::SessionConf,
50    pub environment: Option<wire::TalosSessionEnvironment>,
51    pub channel_id: Option<String>,
52    pub thread_id: Option<String>,
53    pub binding: SessionExecutionBinding,
54    pub tree_identity: wire::SessionTreeIdentity,
55    pub memory_processed_through: Option<SyncEventKey>,
56}
57
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
59#[serde(rename_all = "camelCase", deny_unknown_fields)]
60pub struct SyncFileSnapshot {
61    pub file: wire::FileInfo,
62    pub storage_peer_id: String,
63}
64
65#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
66#[serde(
67    tag = "kind",
68    content = "data",
69    rename_all = "camelCase",
70    deny_unknown_fields
71)]
72pub enum SyncResourceSnapshot {
73    Computer(wire::NodeInfo),
74    Project(wire::ProjectInfo),
75    Session(Box<SyncSessionSnapshot>),
76    File(SyncFileSnapshot),
77}
78
79impl SyncResourceSnapshot {
80    pub fn resource(&self) -> SyncResourceKey {
81        match self {
82            Self::Computer(node) => SyncResourceKey::Computer {
83                computer_id: node.node_id.clone(),
84            },
85            Self::Project(project) => SyncResourceKey::Project {
86                project_id: project.project_id.clone(),
87            },
88            Self::Session(session) => SyncResourceKey::Session {
89                session_id: session.session.id.clone(),
90            },
91            Self::File(file) => SyncResourceKey::File {
92                file_id: file.file.file_id.clone(),
93            },
94        }
95    }
96
97    pub fn computer_id(&self) -> Option<&str> {
98        match self {
99            Self::Computer(node) => Some(&node.node_id),
100            Self::Project(project) => Some(&project.node_id),
101            Self::Session(session) => Some(&session.binding.computer_id),
102            Self::File(_) => None,
103        }
104    }
105}
106
107#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
108#[serde(rename_all = "camelCase", deny_unknown_fields)]
109pub struct SyncTimelineEntry {
110    pub key: SyncEventKey,
111    pub event: wire::TimelineEventPayload,
112    pub persisted_at: i64,
113}
114
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116#[serde(rename_all = "camelCase", deny_unknown_fields)]
117pub struct SyncActiveTurnMetadata {
118    pub identity: wire::AgentTurnRef,
119    pub state: wire::ActiveTurnState,
120    pub updated_at: i64,
121    pub last_event: SyncEventKey,
122}
123
124#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
125#[serde(rename_all = "camelCase", deny_unknown_fields)]
126pub struct SyncAcceptedCommand {
127    pub command: wire::IdempotentCommand,
128    pub idempotency_key: String,
129    pub request_id: String,
130    pub request_hash: String,
131    pub identity: wire::AgentTurnRef,
132    pub message_id: String,
133    pub status: wire::TurnCommandStatus,
134    pub committed_event: SyncEventKey,
135    pub expires_at: Option<i64>,
136}
137
138#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
139#[serde(
140    tag = "type",
141    content = "data",
142    rename_all = "camelCase",
143    rename_all_fields = "camelCase",
144    deny_unknown_fields
145)]
146pub enum SyncChange {
147    Metadata {
148        before: Option<Box<SyncResourceSnapshot>>,
149        after: Box<SyncResourceSnapshot>,
150    },
151    Turn(SyncTurnMetadata),
152    Event(SyncTimelineEntry),
153    ActiveTurnMetadata(SyncActiveTurnMetadata),
154    AcceptedCommand(SyncAcceptedCommand),
155    Reset {
156        history_generation: u64,
157    },
158    Delete,
159}