Skip to main content

codex_mobile_contracts/
management.rs

1use serde::{Deserialize, Serialize};
2
3use crate::runtime::RuntimeStatus;
4
5pub const SUPPORTED_BRIDGE_PROTOCOL_VERSION: i32 = 4;
6
7#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
8#[serde(rename_all = "snake_case")]
9pub enum BridgeManagementOperation {
10    #[default]
11    Install,
12    Update,
13    Repair,
14    Rollback,
15    StartService,
16    StopService,
17    RestartService,
18    Uninstall,
19}
20
21impl BridgeManagementOperation {
22    pub fn as_str(self) -> &'static str {
23        match self {
24            Self::Install => "install",
25            Self::Update => "update",
26            Self::Repair => "repair",
27            Self::Rollback => "rollback",
28            Self::StartService => "start_service",
29            Self::StopService => "stop_service",
30            Self::RestartService => "restart_service",
31            Self::Uninstall => "uninstall",
32        }
33    }
34}
35
36#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
37#[serde(rename_all = "snake_case")]
38pub enum BridgeManagementStatus {
39    #[default]
40    Queued,
41    Running,
42    Reconnecting,
43    Succeeded,
44    Failed,
45}
46
47impl BridgeManagementStatus {
48    pub fn is_terminal(self) -> bool {
49        matches!(self, Self::Succeeded | Self::Failed)
50    }
51}
52
53#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
54#[serde(rename_all = "snake_case")]
55pub enum BridgeManagementPhase {
56    #[default]
57    ResolveTarget,
58    InstallRelease,
59    ActivateRelease,
60    ControlService,
61    CleanupManagedFiles,
62    RestartService,
63    VerifyHealth,
64    Done,
65}
66
67impl BridgeManagementPhase {
68    pub fn as_str(&self) -> &'static str {
69        match self {
70            Self::ResolveTarget => "resolve_target",
71            Self::InstallRelease => "install_release",
72            Self::ActivateRelease => "activate_release",
73            Self::ControlService => "control_service",
74            Self::CleanupManagedFiles => "cleanup_managed_files",
75            Self::RestartService => "restart_service",
76            Self::VerifyHealth => "verify_health",
77            Self::Done => "done",
78        }
79    }
80}
81
82#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
83#[serde(rename_all = "camelCase")]
84#[serde(default)]
85pub struct ManagedBridgeReleaseDescriptor {
86    pub crate_name: String,
87    pub version: String,
88    pub registry: String,
89}
90
91pub type BridgeReleaseDescriptor = ManagedBridgeReleaseDescriptor;
92
93#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
94#[serde(rename_all = "camelCase")]
95#[serde(default)]
96pub struct ManagedInstallRecord {
97    pub install_state: String,
98    pub current_artifact_id: Option<String>,
99    pub current_version: Option<String>,
100    pub current_build_hash: Option<String>,
101    pub current_sha256: Option<String>,
102    pub current_protocol_version: Option<i32>,
103    pub current_release_path: Option<String>,
104    pub previous_artifact_id: Option<String>,
105    pub previous_version: Option<String>,
106    pub previous_build_hash: Option<String>,
107    pub previous_sha256: Option<String>,
108    pub previous_protocol_version: Option<i32>,
109    pub previous_release_path: Option<String>,
110    pub last_operation: Option<String>,
111    pub last_operation_status: Option<String>,
112    pub last_operation_at_ms: i64,
113    pub installed_at_ms: i64,
114    pub updated_at_ms: i64,
115}
116
117pub type RemoteInstallRecord = ManagedInstallRecord;
118
119#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
120#[serde(rename_all = "camelCase")]
121#[serde(default)]
122pub struct ManagedServiceStatus {
123    pub installed: bool,
124    pub active: bool,
125    pub enabled: bool,
126    pub load_state: String,
127    pub active_state: String,
128    pub unit_file_state: String,
129    pub sub_state: String,
130    pub result: String,
131    pub exec_main_status: Option<i32>,
132    pub checked_at_ms: i64,
133}
134
135#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
136#[serde(rename_all = "camelCase")]
137#[serde(default)]
138pub struct ManagedBridgeHealthSnapshot {
139    pub ok: bool,
140    pub bridge_version: Option<String>,
141    pub build_hash: Option<String>,
142    pub protocol_version: Option<i32>,
143    pub runtime_count: i32,
144    pub primary_runtime_id: Option<String>,
145    pub runtime: Option<RuntimeStatus>,
146}
147
148#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
149#[serde(rename_all = "camelCase")]
150#[serde(default)]
151pub struct ManagedBridgeSnapshot {
152    pub available_release: Option<ManagedBridgeReleaseDescriptor>,
153    pub install_record: Option<ManagedInstallRecord>,
154    pub service_status: ManagedServiceStatus,
155    pub bridge_health: Option<ManagedBridgeHealthSnapshot>,
156    pub current_link_target: Option<String>,
157    pub current_binary_sha256: Option<String>,
158    pub unit_file_exists: bool,
159    pub env_file_exists: bool,
160    pub can_update: bool,
161    pub needs_repair: bool,
162    pub can_rollback: bool,
163    #[serde(default)]
164    pub warnings: Vec<String>,
165}
166
167#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
168#[serde(rename_all = "camelCase")]
169#[serde(default)]
170pub struct PlatformDescriptor {
171    pub os: String,
172    pub arch: String,
173    pub supported: bool,
174    pub reason: Option<String>,
175}
176
177#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
178#[serde(rename_all = "camelCase")]
179#[serde(default)]
180pub struct RemoteToolchainStatus {
181    pub cargo_available: bool,
182    pub rustc_available: bool,
183    pub compiler_available: bool,
184    pub systemd_user_available: bool,
185    pub cargo_path: Option<String>,
186    pub rustc_path: Option<String>,
187    pub compiler_path: Option<String>,
188    pub reason: Option<String>,
189}
190
191#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
192#[serde(rename_all = "camelCase")]
193#[serde(default)]
194pub struct RemoteRegistryStatus {
195    pub crate_name: String,
196    pub registry: String,
197    pub reachable: bool,
198    pub latest_version: Option<String>,
199    pub error: Option<String>,
200}
201
202#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
203#[serde(rename_all = "camelCase")]
204#[serde(default)]
205pub struct RemoteServiceStatus {
206    pub installed: bool,
207    pub active: bool,
208    pub enabled: bool,
209    pub lingering_enabled: bool,
210    pub load_state: String,
211    pub active_state: String,
212    pub unit_file_state: String,
213    pub sub_state: String,
214    pub result: String,
215    pub fragment_path: Option<String>,
216    pub exec_main_pid: Option<i64>,
217    pub exec_main_status: Option<i32>,
218    pub last_message: String,
219    pub checked_at_ms: i64,
220}
221
222#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
223#[serde(rename_all = "camelCase")]
224#[serde(default)]
225pub struct BridgeHealthSnapshot {
226    pub ok: bool,
227    pub bridge_version: Option<String>,
228    pub build_hash: Option<String>,
229    pub protocol_version: Option<i32>,
230    pub runtime_count: i32,
231    pub primary_runtime_id: Option<String>,
232    pub runtime: Option<RuntimeStatus>,
233}
234
235#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
236#[serde(rename_all = "camelCase")]
237#[serde(default)]
238pub struct DiagnosticLogEntry {
239    pub level: String,
240    pub source: String,
241    pub code: String,
242    pub message: String,
243    pub detail: Option<String>,
244    #[serde(default)]
245    pub detail_samples: Vec<String>,
246    pub first_at_ms: i64,
247    pub last_at_ms: i64,
248    pub repeat_count: i32,
249}
250
251#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
252#[serde(rename_all = "camelCase")]
253#[serde(default)]
254pub struct RemoteLogChunk {
255    pub text: String,
256    pub collected_at_ms: i64,
257    #[serde(default)]
258    pub entries: Vec<DiagnosticLogEntry>,
259    pub truncated: bool,
260}
261
262#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
263#[serde(rename_all = "camelCase")]
264#[serde(default)]
265pub struct RemoteInspectionReport {
266    pub detected_platform: PlatformDescriptor,
267    pub toolchain_status: RemoteToolchainStatus,
268    pub registry_status: RemoteRegistryStatus,
269    pub available_release: Option<BridgeReleaseDescriptor>,
270    pub install_record: Option<RemoteInstallRecord>,
271    pub service_status: RemoteServiceStatus,
272    pub bridge_health: Option<BridgeHealthSnapshot>,
273    pub current_link_target: Option<String>,
274    pub current_binary_sha256: Option<String>,
275    pub unit_file_exists: bool,
276    pub env_file_exists: bool,
277    pub can_update: bool,
278    pub needs_repair: bool,
279    pub can_rollback: bool,
280    #[serde(default)]
281    pub warnings: Vec<String>,
282    pub app_server_log: RemoteLogChunk,
283    pub bridge_service_log: RemoteLogChunk,
284    pub systemd_log: RemoteLogChunk,
285    pub recent_log: RemoteLogChunk,
286}
287
288#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
289#[serde(rename_all = "camelCase")]
290#[serde(default)]
291pub struct BridgeManagementTask {
292    pub task_id: String,
293    pub operation: BridgeManagementOperation,
294    pub status: BridgeManagementStatus,
295    pub phase: BridgeManagementPhase,
296    pub summary: String,
297    pub detail: Option<String>,
298    pub failure_code: Option<String>,
299    pub target_version: Option<String>,
300    pub current_version: Option<String>,
301    pub started_at_ms: i64,
302    pub updated_at_ms: i64,
303    pub snapshot: Option<ManagedBridgeSnapshot>,
304}
305
306#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
307#[serde(rename_all = "camelCase")]
308#[serde(default)]
309pub struct BridgeManagementTaskPayload {
310    pub task: BridgeManagementTask,
311}
312
313#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
314#[serde(rename_all = "camelCase")]
315#[serde(default)]
316pub struct BridgeManagementTaskResultPayload {
317    pub task: Option<BridgeManagementTask>,
318}