hf2q 0.1.5

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
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
//! Mode-neutral fail-closed health lease for the one model worker owned by an
//! [`Engine`](super::engine::Engine). A Metal call may never return, so
//! receiver closure is not a sufficient readiness signal: the worker still
//! owns the receiver while blocked inside Objective-C. The supervisor lives
//! outside that worker and makes transaction expiry one-way and observable.

use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};
use std::sync::{Arc, Mutex, Weak};
use std::time::{Duration, Instant};

use anyhow::Result;
use tokio::sync::{mpsc, watch};

use super::sse::GenerationEvent;

const HEALTHY: u8 = 0;
const POISONED: u8 = 1;
const MONITOR_INTERVAL: Duration = Duration::from_millis(10);

#[derive(Clone)]
pub(super) struct EngineSupervisor {
    inner: Arc<EngineSupervisorInner>,
}

struct EngineSupervisorInner {
    health: AtomicU8,
    next_epoch: AtomicU64,
    armed: Mutex<Option<ArmedTransaction>>,
    reason: Mutex<Option<String>>,
    health_tx: watch::Sender<bool>,
}

#[derive(Clone, Debug)]
struct ArmedTransaction {
    epoch: u64,
    kind: &'static str,
    deadline: Instant,
}

pub(super) struct WorkerTransactionLease {
    supervisor: EngineSupervisor,
    epoch: u64,
    finished: bool,
}

impl EngineSupervisor {
    pub(super) fn new() -> Self {
        Self::new_with_monitor_start(Self::spawn_monitor)
    }

    fn new_with_monitor_start(
        start: impl FnOnce(Weak<EngineSupervisorInner>) -> std::io::Result<()>,
    ) -> Self {
        let (health_tx, _health_rx) = watch::channel(true);
        let inner = Arc::new(EngineSupervisorInner {
            health: AtomicU8::new(HEALTHY),
            next_epoch: AtomicU64::new(1),
            armed: Mutex::new(None),
            reason: Mutex::new(None),
            health_tx,
        });
        if let Err(error) = start(Arc::downgrade(&inner)) {
            tracing::error!(error = %error, "failed to start engine supervisor monitor");
            inner.poison_transaction(ArmedTransaction {
                epoch: 0,
                kind: "supervisor-monitor-spawn",
                deadline: Instant::now(),
            });
        }
        Self { inner }
    }

    fn spawn_monitor(inner: Weak<EngineSupervisorInner>) -> std::io::Result<()> {
        std::thread::Builder::new()
            .name("hf2q-engine-supervisor".into())
            .spawn(move || loop {
                let Some(inner) = inner.upgrade() else {
                    return;
                };
                inner.expire_if_due(Instant::now());
                drop(inner);
                std::thread::sleep(MONITOR_INTERVAL);
            })
            .map(|_| ())
    }

    pub(super) fn is_healthy(&self) -> bool {
        self.inner.expire_if_due(Instant::now());
        self.inner.health.load(Ordering::Acquire) == HEALTHY
    }

    pub(super) fn unhealthy_message(&self) -> String {
        self.inner
            .reason
            .lock()
            .ok()
            .and_then(|reason| reason.clone())
            .unwrap_or_else(|| super::engine::ENGINE_UNHEALTHY_MESSAGE.to_string())
    }

    pub(super) async fn wait_unhealthy(&self) {
        if !self.is_healthy() {
            return;
        }
        let mut health = self.inner.health_tx.subscribe();
        while *health.borrow_and_update() {
            if health.changed().await.is_err() {
                return;
            }
        }
    }

    /// Forward one worker event stream while the engine stays healthy. A
    /// poisoned transaction wins a simultaneous race, emits exactly one
    /// terminal Error event, then drops the worker receiver so late Metal
    /// returns cannot emit a second terminal or keep a cancelled slot alive.
    pub(super) fn guard_generation_events(
        &self,
        mut source: mpsc::Receiver<GenerationEvent>,
    ) -> mpsc::Receiver<GenerationEvent> {
        let supervisor = self.clone();
        let (guarded_tx, guarded_rx) = mpsc::channel(64);
        tokio::spawn(async move {
            loop {
                tokio::select! {
                    biased;
                    _ = supervisor.wait_unhealthy() => {
                        let _ = guarded_tx
                            .send(GenerationEvent::Error(supervisor.unhealthy_message()))
                            .await;
                        return;
                    }
                    _ = guarded_tx.closed() => {
                        // Propagate the HTTP/SSE consumer's disconnect to the
                        // raw worker channel.  Keeping `source` alive here
                        // would hide cancellation from `events.is_closed()`
                        // and let a bounded prefill/decode continue after the
                        // client had already gone away.
                        return;
                    }
                    event = source.recv() => {
                        let Some(event) = event else {
                            return;
                        };
                        let terminal = matches!(
                            event,
                            GenerationEvent::Done { .. } | GenerationEvent::Error(_)
                        );
                        match guarded_tx.try_send(event) {
                            Ok(()) if terminal => return,
                            Ok(()) => {}
                            Err(mpsc::error::TrySendError::Closed(_)) => return,
                            Err(mpsc::error::TrySendError::Full(_)) => {
                                // The HTTP consumer is not keeping up. Never
                                // let its bounded response queue backpressure
                                // the sole model worker: dropping `source`
                                // wakes any raw blocking send with an error,
                                // which the worker treats as request-local
                                // cancellation while other slots continue.
                                return;
                            }
                        }
                    }
                }
            }
        });
        guarded_rx
    }

    pub(super) fn arm(
        &self,
        kind: &'static str,
        timeout: Duration,
    ) -> Result<WorkerTransactionLease> {
        anyhow::ensure!(self.is_healthy(), "{}", self.unhealthy_message());
        anyhow::ensure!(!timeout.is_zero(), "worker transaction timeout is zero");
        let epoch = self.inner.next_epoch.fetch_add(1, Ordering::Relaxed);
        let mut armed = self
            .inner
            .armed
            .lock()
            .map_err(|_| anyhow::anyhow!("engine supervisor transaction lock poisoned"))?;
        anyhow::ensure!(
            self.inner.health.load(Ordering::Acquire) == HEALTHY,
            "{}",
            self.unhealthy_message()
        );
        anyhow::ensure!(
            armed.is_none(),
            "engine supervisor already has an armed worker transaction"
        );
        *armed = Some(ArmedTransaction {
            epoch,
            kind,
            deadline: Instant::now() + timeout,
        });
        Ok(WorkerTransactionLease {
            supervisor: self.clone(),
            epoch,
            finished: false,
        })
    }

    pub(super) fn run<T>(
        &self,
        kind: &'static str,
        timeout: Duration,
        call: impl FnOnce() -> Result<T>,
    ) -> Result<T> {
        let lease = self.arm(kind, timeout)?;
        let result = call();
        lease.finish()?;
        result
    }

    #[cfg(test)]
    pub(super) fn force_expire_for_test(&self) {
        let mut armed = self.inner.armed.lock().expect("supervisor lock");
        if let Some(transaction) = armed.as_mut() {
            transaction.deadline = Instant::now();
        }
        drop(armed);
        self.inner.expire_if_due(Instant::now());
    }

    #[cfg(test)]
    pub(super) fn backdate_without_monitor_for_test(&self) {
        let mut armed = self.inner.armed.lock().expect("supervisor lock");
        if let Some(transaction) = armed.as_mut() {
            transaction.deadline = Instant::now();
        }
    }

    pub(super) fn poison_now(&self, kind: &'static str) {
        let now = Instant::now();
        self.inner.poison_transaction(ArmedTransaction {
            epoch: self.inner.next_epoch.fetch_add(1, Ordering::Relaxed),
            kind,
            deadline: now,
        });
    }
}

impl WorkerTransactionLease {
    pub(super) fn finish(mut self) -> Result<()> {
        self.complete();
        anyhow::ensure!(
            self.supervisor.is_healthy(),
            "{}",
            self.supervisor.unhealthy_message()
        );
        Ok(())
    }

    fn complete(&mut self) {
        if self.finished {
            return;
        }
        self.supervisor
            .inner
            .complete_epoch(self.epoch, Instant::now());
        self.finished = true;
    }
}

impl Drop for WorkerTransactionLease {
    fn drop(&mut self) {
        self.complete();
    }
}

impl EngineSupervisorInner {
    fn complete_epoch(&self, epoch: u64, now: Instant) {
        let Ok(mut armed) = self.armed.lock() else {
            self.poison_transaction(ArmedTransaction {
                epoch,
                kind: "supervisor-lock",
                deadline: now,
            });
            return;
        };
        let Some(transaction) = armed
            .as_ref()
            .filter(|transaction| transaction.epoch == epoch)
            .cloned()
        else {
            return;
        };
        if now >= transaction.deadline {
            // Linearize readiness failure before clearing the lease. Otherwise
            // a concurrent readiness check could observe HEALTHY + no armed
            // transaction in the gap between disarm and poison.
            self.poison_transaction(transaction);
        }
        *armed = None;
    }

    fn expire_if_due(&self, now: Instant) {
        if self.health.load(Ordering::Acquire) != HEALTHY {
            return;
        }
        let expired = self
            .armed
            .lock()
            .ok()
            .and_then(|armed| armed.clone())
            .filter(|transaction| now >= transaction.deadline);
        let Some(expired) = expired else {
            return;
        };
        self.poison_transaction(expired);
    }

    fn poison_transaction(&self, expired: ArmedTransaction) {
        if self
            .health
            .compare_exchange(HEALTHY, POISONED, Ordering::AcqRel, Ordering::Acquire)
            .is_ok()
        {
            let message = format!(
                "{}: worker transaction {} (epoch {}) exceeded its continuously-awake deadline; process restart required",
                super::engine::ENGINE_UNHEALTHY_SENTINEL,
                expired.kind,
                expired.epoch
            );
            if let Ok(mut reason) = self.reason.lock() {
                *reason = Some(message.clone());
            }
            crate::serve::operator_ui::engine_unhealthy(message.clone());
            tracing::error!(
                transaction = expired.kind,
                epoch = expired.epoch,
                "engine worker transaction deadline expired"
            );
            self.health_tx.send_replace(false);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn expired_transaction_poison_is_one_way_and_not_receiver_closure() {
        let supervisor = EngineSupervisor::new();
        let lease = supervisor
            .arm("synthetic-gpu", Duration::from_secs(60))
            .unwrap();
        supervisor.force_expire_for_test();
        supervisor.wait_unhealthy().await;
        assert!(!supervisor.is_healthy());
        assert!(supervisor.unhealthy_message().contains("synthetic-gpu"));
        assert!(lease.finish().is_err(), "late success must not heal poison");
        assert!(supervisor
            .arm("later-gpu", Duration::from_secs(60))
            .is_err());
    }

    #[test]
    fn timely_completion_disarms_without_poisoning() {
        let supervisor = EngineSupervisor::new();
        supervisor
            .arm("synthetic-gpu", Duration::from_secs(60))
            .unwrap()
            .finish()
            .unwrap();
        assert!(supervisor.is_healthy());
        supervisor
            .arm("second-gpu", Duration::from_secs(60))
            .unwrap()
            .finish()
            .unwrap();
    }

    #[test]
    fn finish_observes_its_own_deadline_before_monitor_poll() {
        let supervisor = EngineSupervisor::new();
        let lease = supervisor
            .arm("deadline-race", Duration::from_secs(60))
            .unwrap();
        supervisor.backdate_without_monitor_for_test();
        assert!(lease.finish().is_err());
        assert!(!supervisor.is_healthy());
        assert!(supervisor.unhealthy_message().contains("deadline-race"));
    }

    #[tokio::test]
    async fn poisoned_stream_emits_one_error_and_ignores_late_done() {
        let supervisor = EngineSupervisor::new();
        let lease = supervisor
            .arm("synthetic-stream-gpu", Duration::from_secs(60))
            .unwrap();
        let (source_tx, source_rx) = mpsc::channel(4);
        let mut guarded = supervisor.guard_generation_events(source_rx);
        source_tx
            .send(GenerationEvent::Delta {
                kind: super::super::sse::DeltaKind::Content,
                text: "partial".into(),
            })
            .await
            .unwrap();
        assert!(matches!(
            guarded.recv().await,
            Some(GenerationEvent::Delta { .. })
        ));

        supervisor.force_expire_for_test();
        assert!(matches!(
            guarded.recv().await,
            Some(GenerationEvent::Error(message)) if message.contains("synthetic-stream-gpu")
        ));
        assert!(guarded.recv().await.is_none());
        assert!(lease.finish().is_err());
        assert!(source_tx
            .send(GenerationEvent::Done {
                finish_reason: "stop".into(),
                prompt_tokens: 1,
                completion_tokens: 1,
                stats: Default::default(),
            })
            .await
            .is_err());
    }

    #[tokio::test]
    async fn poison_wins_over_an_already_buffered_done_event() {
        let supervisor = EngineSupervisor::new();
        let lease = supervisor
            .arm("buffered-done-race", Duration::from_secs(60))
            .unwrap();
        let (source_tx, source_rx) = mpsc::channel(1);
        source_tx
            .send(GenerationEvent::Done {
                finish_reason: "stop".into(),
                prompt_tokens: 1,
                completion_tokens: 1,
                stats: Default::default(),
            })
            .await
            .unwrap();
        supervisor.backdate_without_monitor_for_test();
        assert!(lease.finish().is_err());

        let mut guarded = supervisor.guard_generation_events(source_rx);
        assert!(matches!(
            guarded.recv().await,
            Some(GenerationEvent::Error(message)) if message.contains("buffered-done-race")
        ));
        assert!(guarded.recv().await.is_none());
    }

    #[tokio::test]
    async fn guarded_receiver_drop_closes_the_worker_source() {
        let supervisor = EngineSupervisor::new();
        let (source_tx, source_rx) = mpsc::channel(1);
        let guarded = supervisor.guard_generation_events(source_rx);

        drop(guarded);
        tokio::time::timeout(Duration::from_secs(1), source_tx.closed())
            .await
            .expect("guard task must drop the raw worker receiver promptly");
        assert!(source_tx
            .send(GenerationEvent::Delta {
                kind: super::super::sse::DeltaKind::Content,
                text: "late".into(),
            })
            .await
            .is_err());
    }

    #[tokio::test]
    async fn guarded_backpressure_closes_the_worker_source() {
        let supervisor = EngineSupervisor::new();
        let (source_tx, source_rx) = mpsc::channel(128);
        let _guarded = supervisor.guard_generation_events(source_rx);

        for index in 0..65 {
            source_tx
                .send(GenerationEvent::Delta {
                    kind: super::super::sse::DeltaKind::Content,
                    text: index.to_string(),
                })
                .await
                .expect("raw bridge accepts events until guarded capacity is exceeded");
        }
        tokio::time::timeout(Duration::from_secs(1), source_tx.closed())
            .await
            .expect("guard task must drop the raw receiver on slow-client backpressure");
        assert!(supervisor.is_healthy(), "slow clients are request-local");
    }

    #[tokio::test]
    async fn monitor_spawn_failure_poison_is_immediate_and_observable() {
        let supervisor = EngineSupervisor::new_with_monitor_start(|_| {
            Err(std::io::Error::other("injected monitor spawn failure"))
        });
        assert!(!supervisor.is_healthy());
        supervisor.wait_unhealthy().await;
        assert!(supervisor
            .unhealthy_message()
            .contains("supervisor-monitor-spawn"));
        assert!(supervisor
            .arm("unobservable-gpu", Duration::from_secs(60))
            .is_err());
    }
}