studio-worker 0.4.10

Pull-based image-generation worker for the minis.gg studio.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! The worker's pulse: what the header always says.  Pure data built from
//! the replica, so what the header says is tested without egui.

use chrono::{DateTime, Utc};

use crate::auto_register::RegistrationState;
use crate::daemon_api::ModelEntry;
use crate::daemon_link::LinkState;
use crate::runtime::{CurrentJob, SessionState};

use super::format::format_duration;
use super::theme::Tone;

/// What the header shows.
#[derive(Debug, Clone, PartialEq)]
pub struct Pulse {
    pub activity: Activity,
    pub daemon: Signal,
    pub studio: Signal,
    /// `None` while the daemon does not answer: nothing is known.
    pub gpu: Option<GpuMemory>,
    /// The daemon answers, so Pause / Resume can reach it.
    pub can_pause: bool,
    pub paused: bool,
}

/// What the worker is doing.
#[derive(Debug, Clone, PartialEq)]
pub enum Activity {
    /// The daemon does not answer.
    Offline,
    Idle,
    /// Not claiming studio jobs, nothing running.
    Paused,
    /// The one-job gate is taken but no job is listed yet.
    Busy,
    Running {
        kind: String,
        model: String,
        elapsed: String,
        /// Further jobs running besides the one shown.
        more: usize,
    },
}

impl Activity {
    /// The headline, e.g. `Running image · sdxl · 12s`.
    pub fn headline(&self) -> String {
        match self {
            Activity::Offline => "Not connected".into(),
            Activity::Idle => "Idle".into(),
            Activity::Paused => "Paused".into(),
            Activity::Busy => "Busy".into(),
            Activity::Running {
                kind,
                model,
                elapsed,
                ..
            } => format!("Running {kind} \u{00b7} {model} \u{00b7} {elapsed}"),
        }
    }

    /// The line under the headline.
    pub fn detail(&self) -> String {
        match self {
            Activity::Offline => "waiting for the worker daemon".into(),
            Activity::Idle => "waiting for work".into(),
            Activity::Paused => "not claiming studio jobs".into(),
            Activity::Busy => "a job is starting".into(),
            Activity::Running { more: 0, .. } => "one job running".into(),
            Activity::Running { more, .. } => format!("+{more} more running"),
        }
    }

    pub fn tone(&self) -> Tone {
        match self {
            Activity::Offline => Tone::Bad,
            Activity::Idle => Tone::Good,
            Activity::Paused => Tone::Neutral,
            Activity::Busy | Activity::Running { .. } => Tone::Busy,
        }
    }

    /// Running work glows.
    pub fn glows(&self) -> bool {
        matches!(self, Activity::Busy | Activity::Running { .. })
    }
}

/// A short labelled state with its tone and a longer hover text.
#[derive(Debug, Clone, PartialEq)]
pub struct Signal {
    pub label: String,
    pub tone: Tone,
    pub detail: String,
}

impl Signal {
    fn new(label: &str, tone: Tone, detail: impl Into<String>) -> Self {
        Self {
            label: label.to_string(),
            tone,
            detail: detail.into(),
        }
    }
}

/// Device memory held by the models the daemon keeps loaded.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GpuMemory {
    /// Sum of the catalogue estimates of loaded (or loading) models, GB.
    pub held_gb: f32,
    /// Device total, GB; 0 when unknown.
    pub total_gb: f32,
}

/// Held memory at or above this share of the device reads as a warning.
pub const GPU_TIGHT_FRACTION: f32 = 0.9;

impl GpuMemory {
    /// Memory the loaded models hold, from their catalogue estimates.
    pub fn from_models(models: &[ModelEntry], total_gb: f32) -> Self {
        let held_gb = models
            .iter()
            .filter(|m| holds_memory(&m.state))
            .map(|m| m.vram_gb_estimate.max(0.0))
            // A plain `sum` of nothing is -0.0, which prints as "-0".
            .fold(0.0, |a, b| a + b);
        Self { held_gb, total_gb }
    }

    /// Share of the device held, 0..=1; 0 when the total is unknown.
    pub fn fraction(&self) -> f32 {
        if self.total_gb > 0.0 {
            (self.held_gb / self.total_gb).clamp(0.0, 1.0)
        } else {
            0.0
        }
    }

    /// `≈ 6.5 / 24 GB`, or `≈ 6.5 GB` when the total is unknown.
    pub fn label(&self) -> String {
        if self.total_gb > 0.0 {
            format!(
                "\u{2248} {} / {} GB",
                format_gb(self.held_gb),
                format_gb(self.total_gb)
            )
        } else {
            format!("\u{2248} {} GB", format_gb(self.held_gb))
        }
    }

    pub fn tone(&self) -> Tone {
        if self.fraction() >= GPU_TIGHT_FRACTION {
            Tone::Busy
        } else {
            Tone::Neutral
        }
    }
}

/// Whether a model in `state` holds device memory.
pub fn holds_memory(state: &str) -> bool {
    matches!(state, "loaded" | "loading" | "unloading")
}

/// `6.5`, `24`, `0` — one decimal, none when whole.
pub fn format_gb(gb: f32) -> String {
    // Adding 0.0 turns a negative zero into a plain one.
    let rounded = (gb * 10.0).round() / 10.0 + 0.0;
    if rounded.fract() == 0.0 {
        format!("{rounded:.0}")
    } else {
        format!("{rounded:.1}")
    }
}

/// Everything the pulse is built from.
pub struct PulseInputs<'a> {
    pub link: &'a LinkState,
    pub registered: bool,
    pub registration: &'a RegistrationState,
    pub session: &'a SessionState,
    pub busy: bool,
    pub paused: bool,
    pub active: &'a [CurrentJob],
    pub models: &'a [ModelEntry],
    pub vram_total_gb: f32,
    pub now: DateTime<Utc>,
}

impl Pulse {
    pub fn build(i: PulseInputs<'_>) -> Self {
        let connected = i.link.is_connected();
        Self {
            activity: activity(&i, connected),
            daemon: daemon_signal(i.link),
            studio: studio_signal(connected, i.registered, i.registration, i.session),
            gpu: connected.then(|| GpuMemory::from_models(i.models, i.vram_total_gb)),
            can_pause: connected,
            paused: connected && i.paused,
        }
    }
}

fn activity(i: &PulseInputs<'_>, connected: bool) -> Activity {
    if !connected {
        return Activity::Offline;
    }
    // The oldest running job leads; the rest are counted.
    if let Some(first) = i.active.iter().min_by_key(|j| j.started_at) {
        return Activity::Running {
            kind: first.kind.as_str().to_string(),
            model: first.model.clone(),
            elapsed: format_duration(i.now.signed_duration_since(first.started_at)),
            more: i.active.len() - 1,
        };
    }
    if i.busy {
        Activity::Busy
    } else if i.paused {
        Activity::Paused
    } else {
        Activity::Idle
    }
}

fn daemon_signal(link: &LinkState) -> Signal {
    let (label, tone) = match link {
        LinkState::Connected { .. } => ("Daemon connected", Tone::Good),
        LinkState::Connecting => ("Connecting", Tone::Neutral),
        LinkState::Starting { .. } => ("Daemon starting", Tone::Busy),
        LinkState::Unreachable { .. } => ("Daemon unreachable", Tone::Bad),
    };
    Signal::new(label, tone, link.summary())
}

fn studio_signal(
    connected: bool,
    registered: bool,
    registration: &RegistrationState,
    session: &SessionState,
) -> Signal {
    if !connected {
        return Signal::new(
            "Studio unknown",
            Tone::Neutral,
            "the daemon holds the studio session; it does not answer",
        );
    }
    if !registered {
        return match registration {
            RegistrationState::Pending { .. } => Signal::new(
                "Awaiting approval",
                Tone::Busy,
                "the studio operator has not approved this worker yet",
            ),
            RegistrationState::Rejected { reason } => {
                Signal::new("Registration rejected", Tone::Bad, reason.clone())
            }
            RegistrationState::Pristine | RegistrationState::Approved => Signal::new(
                "Registering",
                Tone::Neutral,
                "asking the studio for a registration slot",
            ),
        };
    }
    let detail = session.summary();
    match session {
        SessionState::Connected => Signal::new("Studio connected", Tone::Good, detail),
        SessionState::Connecting => Signal::new("Connecting to studio", Tone::Busy, detail),
        SessionState::Reconnecting { attempt } => Signal {
            label: format!("Reconnecting ({attempt})"),
            tone: Tone::Busy,
            detail,
        },
        SessionState::WaitingForApproval => Signal::new("Awaiting approval", Tone::Busy, detail),
        SessionState::AuthFailed { .. } => Signal::new("Studio auth failed", Tone::Bad, detail),
        SessionState::Fatal { .. } => Signal::new("Studio session ended", Tone::Bad, detail),
        SessionState::Stopped => Signal::new("Studio stopped", Tone::Neutral, detail),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::daemon_api::ModelSourceBrief;
    use crate::runtime::JobSource;
    use crate::types::{ModelEngine, TaskKind};

    fn connected() -> LinkState {
        LinkState::Connected {
            url: "http://127.0.0.1:4787".into(),
            version: "0.4.9".into(),
        }
    }

    fn job(id: &str, model: &str, secs_ago: i64, now: DateTime<Utc>) -> CurrentJob {
        CurrentJob {
            job_id: id.into(),
            kind: TaskKind::Image,
            model: model.into(),
            prompt: String::new(),
            started_at: now - chrono::Duration::seconds(secs_ago),
            source: JobSource::Local,
        }
    }

    fn model(state: &str, gb: f32) -> ModelEntry {
        ModelEntry {
            id: format!("m-{state}"),
            display_name: "M".into(),
            kind: TaskKind::Llm,
            vram_gb_estimate: gb,
            source: ModelSourceBrief {
                engine: ModelEngine::LlamaCpp,
            },
            enabled: true,
            exclusive_group: None,
            state: state.into(),
            resident: false,
            since: None,
            error: None,
            loadable: true,
        }
    }

    struct Given {
        link: LinkState,
        registered: bool,
        registration: RegistrationState,
        session: SessionState,
        busy: bool,
        paused: bool,
        active: Vec<CurrentJob>,
        models: Vec<ModelEntry>,
        now: DateTime<Utc>,
    }

    impl Default for Given {
        fn default() -> Self {
            Self {
                link: connected(),
                registered: true,
                registration: RegistrationState::Approved,
                session: SessionState::Connected,
                busy: false,
                paused: false,
                active: Vec::new(),
                models: Vec::new(),
                now: Utc::now(),
            }
        }
    }

    fn pulse(g: &Given) -> Pulse {
        Pulse::build(PulseInputs {
            link: &g.link,
            registered: g.registered,
            registration: &g.registration,
            session: &g.session,
            busy: g.busy,
            paused: g.paused,
            active: &g.active,
            models: &g.models,
            vram_total_gb: 24.0,
            now: g.now,
        })
    }

    #[test]
    fn an_idle_connected_worker_reads_calm() {
        let p = pulse(&Given::default());
        assert_eq!(p.activity, Activity::Idle);
        assert_eq!(p.activity.headline(), "Idle");
        assert_eq!(p.activity.tone(), Tone::Good);
        assert!(!p.activity.glows());
        assert_eq!(p.daemon.label, "Daemon connected");
        assert_eq!(p.studio.label, "Studio connected");
        assert!(p.can_pause && !p.paused);
    }

    #[test]
    fn the_oldest_running_job_leads_and_the_rest_are_counted() {
        let now = Utc::now();
        let g = Given {
            active: vec![job("b", "young", 3, now), job("a", "sdxl", 72, now)],
            now,
            ..Given::default()
        };
        let p = pulse(&g);
        assert_eq!(
            p.activity.headline(),
            "Running image \u{00b7} sdxl \u{00b7} 1m 12s"
        );
        assert_eq!(p.activity.detail(), "+1 more running");
        assert!(p.activity.glows());

        let g = Given {
            active: vec![job("a", "sdxl", 5, now)],
            now,
            ..Given::default()
        };
        assert_eq!(pulse(&g).activity.detail(), "one job running");
    }

    #[test]
    fn paused_busy_and_offline_each_say_so() {
        let p = pulse(&Given {
            paused: true,
            ..Given::default()
        });
        assert_eq!(p.activity, Activity::Paused);
        assert!(p.paused);
        assert_eq!(p.activity.detail(), "not claiming studio jobs");

        let p = pulse(&Given {
            busy: true,
            ..Given::default()
        });
        assert_eq!(p.activity, Activity::Busy);
        assert!(p.activity.glows());

        let p = pulse(&Given {
            link: LinkState::Unreachable {
                error: "e".into(),
                started_daemon: true,
            },
            paused: true,
            ..Given::default()
        });
        assert_eq!(p.activity, Activity::Offline);
        assert_eq!(p.activity.tone(), Tone::Bad);
        assert_eq!(p.daemon.label, "Daemon unreachable");
        assert_eq!(p.studio.label, "Studio unknown");
        assert_eq!(p.gpu, None, "no stale memory figure");
        assert!(!p.can_pause && !p.paused, "no stale pause state");
    }

    #[test]
    fn every_link_state_has_its_signal() {
        let cases = [
            (LinkState::Connecting, "Connecting", Tone::Neutral),
            (
                LinkState::Starting { error: "e".into() },
                "Daemon starting",
                Tone::Busy,
            ),
        ];
        for (link, label, tone) in cases {
            let s = pulse(&Given {
                link,
                ..Given::default()
            })
            .daemon;
            assert_eq!((s.label.as_str(), s.tone), (label, tone));
        }
    }

    #[test]
    fn the_studio_signal_follows_registration_then_the_session() {
        let unregistered = |registration| Given {
            registered: false,
            registration,
            ..Given::default()
        };
        let label = |g: &Given| pulse(g).studio.label;
        assert_eq!(
            label(&unregistered(RegistrationState::Pending {
                request_id: "r".into(),
                since: Utc::now()
            })),
            "Awaiting approval"
        );
        let rejected = pulse(&unregistered(RegistrationState::Rejected {
            reason: "unknown contributor".into(),
        }))
        .studio;
        assert_eq!(rejected.label, "Registration rejected");
        assert_eq!(rejected.tone, Tone::Bad);
        assert_eq!(rejected.detail, "unknown contributor");
        assert_eq!(
            label(&unregistered(RegistrationState::Pristine)),
            "Registering"
        );

        let with_session = |session| Given {
            session,
            ..Given::default()
        };
        for (session, expected, tone) in [
            (SessionState::Connecting, "Connecting to studio", Tone::Busy),
            (
                SessionState::Reconnecting { attempt: 4 },
                "Reconnecting (4)",
                Tone::Busy,
            ),
            (
                SessionState::WaitingForApproval,
                "Awaiting approval",
                Tone::Busy,
            ),
            (
                SessionState::AuthFailed { reason: "x".into() },
                "Studio auth failed",
                Tone::Bad,
            ),
            (
                SessionState::Fatal { reason: "x".into() },
                "Studio session ended",
                Tone::Bad,
            ),
            (SessionState::Stopped, "Studio stopped", Tone::Neutral),
        ] {
            let s = pulse(&with_session(session)).studio;
            assert_eq!((s.label.as_str(), s.tone), (expected, tone));
        }
    }

    #[test]
    fn gpu_memory_counts_the_models_that_hold_it() {
        let g = Given {
            models: vec![
                model("loaded", 6.0),
                model("loading", 0.5),
                model("unloading", 1.0),
                model("unloaded", 12.0),
                model("failed", 3.0),
            ],
            ..Given::default()
        };
        let gpu = pulse(&g).gpu.expect("known while connected");
        assert_eq!(gpu.held_gb, 7.5);
        assert_eq!(gpu.label(), "\u{2248} 7.5 / 24 GB");
        assert!((gpu.fraction() - 7.5 / 24.0).abs() < 1e-6);
        assert_eq!(gpu.tone(), Tone::Neutral);
    }

    #[test]
    fn gpu_memory_warns_when_tight_and_copes_without_a_total() {
        let tight = GpuMemory {
            held_gb: 22.0,
            total_gb: 24.0,
        };
        assert_eq!(tight.tone(), Tone::Busy);
        let unknown = GpuMemory {
            held_gb: 2.0,
            total_gb: 0.0,
        };
        assert_eq!(unknown.fraction(), 0.0);
        assert_eq!(unknown.label(), "\u{2248} 2 GB");
        let over = GpuMemory {
            held_gb: 30.0,
            total_gb: 24.0,
        };
        assert_eq!(over.fraction(), 1.0);
    }

    #[test]
    fn no_loaded_models_hold_plain_zero() {
        let gpu = GpuMemory::from_models(&[], 24.0);
        assert_eq!(gpu.label(), "\u{2248} 0 / 24 GB");
    }

    #[test]
    fn gigabytes_read_short() {
        assert_eq!(format_gb(24.0), "24");
        assert_eq!(format_gb(6.54), "6.5");
        assert_eq!(format_gb(0.0), "0");
        assert_eq!(format_gb(0.04), "0");
        assert_eq!(format_gb(-0.0), "0");
        assert_eq!(format_gb(-0.04), "0");
    }
}