1use serde::{Deserialize, Serialize};
7
8use crate::{
9 api::{HealthResponse, Verdict},
10 types::{
11 Accuracy, DriveUsage, EndCause, JobProfile, JobRun, Lease, LeaseId, NoiseReport,
12 ResourceUsage, SessionKey, is_default,
13 },
14};
15
16#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19pub struct UiState {
20 pub generated_at_ms: i64,
23 pub service: HealthResponse,
25 pub first_session_at_ms: Option<i64>,
28 pub sessions: Vec<UiSession>,
30 pub active: Vec<ActiveJob>,
32 pub queue: Vec<QueuedJob>,
34 pub meters: Meters,
36 pub recent: Vec<RecentRun>,
38 pub notices: Vec<Notice>,
40 #[serde(default)]
43 pub ended: Vec<EndedLease>,
44}
45
46#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
48#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
49pub struct UiSession {
50 pub session: SessionKey,
52 pub cwd: Option<String>,
55 pub usage: ResourceUsage,
57}
58
59#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
61#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
62pub struct ActiveJob {
63 pub lease: Lease,
65 pub process: Option<JobProcess>,
67 #[serde(default)]
71 pub quiet_at_start: Option<bool>,
72}
73
74#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77pub struct JobProcess {
78 pub pid: u32,
80 pub name: String,
82 pub exe: Option<String>,
84 pub bound: bool,
88}
89
90#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
92#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
93pub struct QueuedJob {
94 pub lease: Lease,
96 pub position: u32,
98 pub verdict: Verdict,
100}
101
102#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
104#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
105pub struct Meters {
106 pub headroom_pct: f32,
108 pub cpu: CpuMeter,
110 pub ram: RamMeter,
112 pub gpus: Vec<GpuMeter>,
114 pub drives: Vec<DriveUsage>,
116}
117
118#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
120#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
121pub struct CpuMeter {
122 pub logical_cores: u32,
124 pub usage_pct: f32,
126 pub history_pct: Vec<f32>,
128 pub reserved_cores: f32,
130}
131
132#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
135pub struct RamMeter {
136 pub total_mb: u64,
138 pub used_mb: u64,
140 pub reserved_mb: u64,
142}
143
144#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
146#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
147pub struct GpuMeter {
148 pub name: String,
150 pub busiest_pct: f32,
152 pub vram_used_mb: u64,
155 pub vram_total_mb: Option<u64>,
158 pub reserved_pct: f32,
161 pub reserved_vram_mb: u64,
164 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
168 #[cfg_attr(any(feature = "schema", test), schemars(extend("default" = false)))]
169 pub unified_memory: bool,
170 #[serde(default, skip_serializing_if = "is_default")]
172 #[cfg_attr(any(feature = "schema", test), schemars(extend("default" = "exact")))]
173 pub process_load: Accuracy,
174}
175
176#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
178#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
179pub struct RecentRun {
180 pub lease_id: Option<LeaseId>,
182 pub session: Option<SessionKey>,
184 pub command: String,
186 pub cwd: String,
188 pub declared: JobProfile,
190 pub started_at_ms: i64,
192 pub ended_at_ms: i64,
194 pub end_cause: Option<EndCause>,
196 pub noise: Option<NoiseReport>,
198}
199
200impl From<JobRun> for RecentRun {
201 fn from(run: JobRun) -> Self {
202 Self {
203 lease_id: run.lease_id,
204 session: run.session,
205 command: run.command,
206 cwd: run.cwd,
207 declared: run.declared,
208 started_at_ms: run.started_at_ms,
209 ended_at_ms: run.ended_at_ms,
210 end_cause: run.end_cause,
211 noise: run.noise,
212 }
213 }
214}
215
216#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
218#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
219pub struct EndedLease {
220 pub lease_id: LeaseId,
222 pub session: SessionKey,
224 pub command: String,
226 pub cause: EndCause,
228 pub reason: String,
231 pub granted_at_ms: Option<i64>,
233 pub ended_at_ms: i64,
235}
236
237#[cfg_attr(any(feature = "schema", test), derive(schemars::JsonSchema))]
239#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
240pub struct Notice {
241 pub lease_id: LeaseId,
243 pub session: SessionKey,
245 pub cause: EndCause,
247 pub command: String,
249 #[serde(default)]
252 pub tool_use_id: Option<String>,
253 pub ended_at_ms: i64,
255 pub delivered_at_ms: Option<i64>,
257}
258
259#[cfg(test)]
260mod tests {
261 use super::*;
262 use crate::test_support::assert_json_round_trip;
263
264 #[test]
265 fn test_notice_json_round_trip_writes_cause_and_null_delivery() {
266 let notice = Notice {
267 lease_id: LeaseId(44),
268 session: SessionKey("3f2a9c1e-session".into()),
269 cause: EndCause::Removed,
270 command: "cargo test".into(),
271 tool_use_id: Some("toolu_01".into()),
272 ended_at_ms: 1_793_000_090_000,
273 delivered_at_ms: None,
274 };
275 assert_json_round_trip(¬ice);
276 let value = serde_json::to_value(¬ice).unwrap();
277 assert_eq!(value["lease_id"], 44);
278 assert_eq!(value["cause"], "removed");
279 assert_eq!(value["tool_use_id"], "toolu_01");
280 assert!(value["delivered_at_ms"].is_null());
281 }
282
283 #[test]
284 fn test_ended_lease_json_round_trip_writes_cause_and_null_grant() {
285 let ended = EndedLease {
286 lease_id: LeaseId(45),
287 session: SessionKey("3f2a9c1e-session".into()),
288 command: "npm test".into(),
289 cause: EndCause::Expired,
290 reason: "holder process exited".into(),
291 granted_at_ms: None,
292 ended_at_ms: 1_793_000_090_000,
293 };
294 assert_json_round_trip(&ended);
295 let value = serde_json::to_value(&ended).unwrap();
296 assert_eq!(value["cause"], "expired");
297 assert!(value["granted_at_ms"].is_null());
298 }
299
300 #[test]
301 fn test_ui_state_from_an_older_allotd_has_no_ended_leases() {
302 let mut value = serde_json::to_value(crate::contract::busy()).unwrap();
303 value.as_object_mut().unwrap().remove("ended");
304 let state: UiState = serde_json::from_value(value).unwrap();
305 assert!(state.ended.is_empty());
306 }
307
308 #[test]
309 fn test_gpu_meter_schema_defaults_match_the_rust_defaults() {
310 let schema = schemars::schema_for!(GpuMeter);
311 let properties = &schema.as_value()["properties"];
312 assert_eq!(
313 properties["process_load"]["default"],
314 serde_json::to_value(Accuracy::default()).unwrap()
315 );
316 assert_eq!(properties["unified_memory"]["default"], bool::default());
317 }
318
319 #[test]
320 fn test_gpu_meter_from_an_older_allotd_is_exact_and_not_unified() {
321 let old = r#"{"name":"RTX","busiest_pct":1.0,"vram_used_mb":0,"vram_total_mb":null,
322 "reserved_pct":0.0,"reserved_vram_mb":0}"#;
323 let meter: GpuMeter = serde_json::from_str(old).unwrap();
324 assert!(!meter.unified_memory);
325 assert_eq!(meter.process_load, Accuracy::Exact);
326 }
327
328 #[test]
329 fn test_notice_from_an_older_allotd_has_no_tool_use_id() {
330 let old = r#"{"lease_id":1,"session":"s","cause":"killed","command":"cargo build",
331 "ended_at_ms":5,"delivered_at_ms":null}"#;
332 let notice: Notice = serde_json::from_str(old).unwrap();
333 assert_eq!(notice.tool_use_id, None);
334 }
335}