car-server-core 0.52.0

Transport-neutral library for the CAR daemon JSON-RPC dispatcher (used by car-server and tokhn-daemon)
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Per-WebSocket inference lifecycle control.
//!
//! The registry is session-scoped: an ID minted for one socket is
//! indistinguishable from a never-issued ID on every other socket. Active,
//! pending-control, orphan, and terminal records are bounded. A backend task is
//! owned until it has actually returned, so an unconfirmed response cannot
//! detach work or release its admission charge.

use car_proto::InferenceControlStatus;
use std::collections::{HashMap, VecDeque};
use std::future::Future;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::{watch, OwnedSemaphorePermit, Semaphore};

pub const DEFAULT_MAX_ACTIVE: usize = 64;
pub const DEFAULT_MAX_TOMBSTONES: usize = 256;
pub const DEFAULT_MAX_PENDING_CONTROLS: usize = 64;
pub const DEFAULT_MAX_ORPHANS: usize = 64;
pub const DEFAULT_TOMBSTONE_TTL: Duration = Duration::from_secs(300);
pub const DEFAULT_TERMINATION_ACK_TIMEOUT: Duration = Duration::from_secs(5);
pub const MAX_DEADLINE_TIMEOUT_MS: u64 = 600_000;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ControlCause {
    Cancel,
    Deadline,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegistryError {
    ActiveLimitReached,
    OrphanLimitReached,
    PendingControlLimitReached,
    DuplicateInferenceId,
    InferenceNotActive,
    DeadlineAlreadyScheduled,
}

#[derive(Debug, Clone, Copy)]
pub struct RegistryConfig {
    pub max_active: usize,
    pub max_tombstones: usize,
    pub max_pending_controls: usize,
    pub max_orphans: usize,
    pub tombstone_ttl: Duration,
    pub termination_ack_timeout: Duration,
}

impl Default for RegistryConfig {
    fn default() -> Self {
        Self {
            max_active: DEFAULT_MAX_ACTIVE,
            max_tombstones: DEFAULT_MAX_TOMBSTONES,
            max_pending_controls: DEFAULT_MAX_PENDING_CONTROLS,
            max_orphans: DEFAULT_MAX_ORPHANS,
            tombstone_ttl: DEFAULT_TOMBSTONE_TTL,
            termination_ack_timeout: DEFAULT_TERMINATION_ACK_TIMEOUT,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TerminalOutcome {
    Completed,
    Controlled(InferenceControlStatus),
}

struct ActiveEntry {
    run_id: Option<String>,
    request_id: Option<String>,
    phase: ActivePhase,
    terminal: watch::Sender<Option<TerminalOutcome>>,
    deadline_token: Option<u64>,
    backend_task: Option<tokio::task::JoinHandle<()>>,
    backend_terminated: bool,
}

#[derive(Clone)]
enum ActivePhase {
    Running,
    Terminating(u64),
}

struct Tombstone {
    at: Instant,
    outcome: TerminalOutcome,
    /// Retained until the backend wrapper observes the provider/runner future
    /// return. Keeping the JoinHandle makes the task abort-on-session-drop.
    backend_task: Option<tokio::task::JoinHandle<()>>,
}

#[derive(Default)]
struct RegistryState {
    active: HashMap<String, ActiveEntry>,
    tombstones: HashMap<String, Tombstone>,
    tombstone_order: VecDeque<String>,
    next_token: u64,
    orphan_count: usize,
}

enum ControlStart {
    New(u64),
    Wait(watch::Receiver<Option<TerminalOutcome>>),
    Return(InferenceControlStatus),
}

/// Owns a claimed `Running -> Terminating(token)` transition until it reaches
/// one terminal outcome. Cancellation and panic drop the control future, so a
/// trailing `finish` call is not sufficient: without this guard the entry
/// remains `Terminating` and every later control waits forever on its watch
/// channel. Drop publishes the cause-specific unconfirmed outcome under the
/// exact token; a stale guard can never overwrite another terminal.
struct ControlClaimGuard<'a> {
    registry: &'a InferenceRegistry,
    inference_id: &'a str,
    token: u64,
    abandoned_status: InferenceControlStatus,
    armed: bool,
}

impl<'a> ControlClaimGuard<'a> {
    fn new(
        registry: &'a InferenceRegistry,
        inference_id: &'a str,
        token: u64,
        cause: ControlCause,
    ) -> Self {
        let abandoned_status = match cause {
            ControlCause::Cancel => InferenceControlStatus::TerminationUnconfirmed,
            ControlCause::Deadline => InferenceControlStatus::DeadlineExceededUnconfirmed,
        };
        Self {
            registry,
            inference_id,
            token,
            abandoned_status,
            armed: true,
        }
    }

    fn finish(mut self, status: InferenceControlStatus) -> InferenceControlStatus {
        let won = self.registry.finish(
            self.inference_id,
            Some(self.token),
            TerminalOutcome::Controlled(status),
        );
        self.armed = false;
        if won {
            status
        } else {
            InferenceControlStatus::AlreadyTerminal
        }
    }
}

impl Drop for ControlClaimGuard<'_> {
    fn drop(&mut self) {
        if self.armed {
            let _ = self.registry.finish(
                self.inference_id,
                Some(self.token),
                TerminalOutcome::Controlled(self.abandoned_status),
            );
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeadlineReservationError {
    Terminal(InferenceControlStatus),
    Registry(RegistryError),
}

pub struct DeadlineReservation {
    registry: Arc<InferenceRegistry>,
    inference_id: String,
    token: u64,
    terminal: watch::Receiver<Option<TerminalOutcome>>,
    armed: bool,
}

impl DeadlineReservation {
    /// Wait relative to receipt of the deadline RPC. Completion or another
    /// control wakes the timer early. The acknowledgement closure is not
    /// constructed or polled unless this exact reservation wins the race.
    pub async fn wait_and_control<F, Fut>(
        mut self,
        timeout: Duration,
        acknowledgement: F,
    ) -> Result<InferenceControlStatus, RegistryError>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = bool>,
    {
        if self.terminal.borrow().is_none() {
            tokio::select! {
                _ = tokio::time::sleep(timeout) => {}
                _ = self.terminal.changed() => {
                    return Ok(InferenceControlStatus::AlreadyTerminal);
                }
            }
        } else {
            return Ok(InferenceControlStatus::AlreadyTerminal);
        }

        // A deadline reservation bounds one timer per active inference, but a
        // sleeping timer is not a termination operation. Admit it against the
        // shared control cap only once it actually expires, leaving capacity
        // for an urgent cancel while many long deadlines are armed.
        let _pending = self.registry.try_acquire_control()?;
        let start = self
            .registry
            .claim_reserved_deadline(&self.inference_id, self.token);
        self.armed = false;
        Ok(self
            .registry
            .drive_control_start(
                &self.inference_id,
                ControlCause::Deadline,
                start,
                acknowledgement,
            )
            .await)
    }
}

impl Drop for DeadlineReservation {
    fn drop(&mut self) {
        if self.armed {
            self.registry
                .release_deadline(&self.inference_id, self.token);
        }
    }
}

pub struct InferenceRegistry {
    config: RegistryConfig,
    pending_controls: Arc<Semaphore>,
    state: Mutex<RegistryState>,
}

impl Default for InferenceRegistry {
    fn default() -> Self {
        Self::with_config(RegistryConfig::default())
    }
}

impl InferenceRegistry {
    pub fn with_config(config: RegistryConfig) -> Self {
        assert!(
            config.max_active > 0,
            "active registry bound must be positive"
        );
        assert!(
            config.max_tombstones > 0,
            "tombstone bound must be positive"
        );
        assert!(
            config.max_pending_controls > 0,
            "pending-control bound must be positive"
        );
        assert!(config.max_orphans > 0, "orphan bound must be positive");
        assert!(
            config.max_orphans >= config.max_active,
            "orphan bound must cover every already-active inference"
        );
        assert!(
            config.max_orphans <= config.max_tombstones,
            "orphan bound must fit inside tombstone bound"
        );
        Self {
            pending_controls: Arc::new(Semaphore::new(config.max_pending_controls)),
            config,
            state: Mutex::new(RegistryState::default()),
        }
    }

    pub fn try_acquire_control(self: &Arc<Self>) -> Result<OwnedSemaphorePermit, RegistryError> {
        self.pending_controls
            .clone()
            .try_acquire_owned()
            .map_err(|_| RegistryError::PendingControlLimitReached)
    }

    pub fn begin(
        &self,
    ) -> Result<(String, watch::Receiver<Option<TerminalOutcome>>), RegistryError> {
        self.begin_for_run(None, None)
    }

    pub fn begin_for_run(
        &self,
        run_id: Option<String>,
        request_id: Option<String>,
    ) -> Result<(String, watch::Receiver<Option<TerminalOutcome>>), RegistryError> {
        for _ in 0..8 {
            let id = format!("inf_{}", uuid::Uuid::new_v4().simple());
            match self.begin_with_id_for_run(&id, run_id.clone(), request_id.clone()) {
                Ok(receiver) => return Ok((id, receiver)),
                Err(RegistryError::DuplicateInferenceId) => continue,
                Err(error) => return Err(error),
            }
        }
        Err(RegistryError::DuplicateInferenceId)
    }

    pub fn begin_with_id(
        &self,
        inference_id: &str,
    ) -> Result<watch::Receiver<Option<TerminalOutcome>>, RegistryError> {
        self.begin_with_id_for_run(inference_id, None, None)
    }

    fn begin_with_id_for_run(
        &self,
        inference_id: &str,
        run_id: Option<String>,
        request_id: Option<String>,
    ) -> Result<watch::Receiver<Option<TerminalOutcome>>, RegistryError> {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        if state.active.contains_key(inference_id) || state.tombstones.contains_key(inference_id) {
            return Err(RegistryError::DuplicateInferenceId);
        }
        if state.active.len() >= self.config.max_active {
            return Err(RegistryError::ActiveLimitReached);
        }
        // Every admitted active inference may terminalize before its backend
        // wrapper returns, converting its owned JoinHandle into an orphaned,
        // non-evictable tombstone. Treat the active entry itself as that
        // worst-case capacity reservation. `finish` removes one active before
        // adding at most one orphan, so this sum cannot grow after admission.
        if state.active.len().saturating_add(state.orphan_count) >= self.config.max_orphans {
            return Err(RegistryError::OrphanLimitReached);
        }
        let (terminal, receiver) = watch::channel(None);
        state.active.insert(
            inference_id.to_string(),
            ActiveEntry {
                run_id,
                request_id,
                phase: ActivePhase::Running,
                terminal,
                deadline_token: None,
                backend_task: None,
                backend_terminated: false,
            },
        );
        Ok(receiver)
    }

    /// Active inference identities attributed to one run, sorted for stable
    /// receipt selection.
    pub fn active_for_run(&self, run_id: &str) -> Vec<(String, Option<String>)> {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        let mut active: Vec<_> = state
            .active
            .iter()
            .filter(|(_, entry)| entry.run_id.as_deref() == Some(run_id))
            .map(|(inference_id, entry)| (inference_id.clone(), entry.request_id.clone()))
            .collect();
        active.sort();
        active
    }

    pub fn attach_backend(
        &self,
        inference_id: &str,
        task: tokio::task::JoinHandle<()>,
    ) -> Result<(), RegistryError> {
        let mut state = self.state.lock().expect("inference registry poisoned");
        let Some(entry) = state.active.get_mut(inference_id) else {
            task.abort();
            return Err(RegistryError::InferenceNotActive);
        };
        if entry.backend_terminated {
            drop(task);
        } else {
            entry.backend_task = Some(task);
        }
        Ok(())
    }

    /// Called only after the actual backend future has returned.
    pub fn backend_terminated(&self, inference_id: &str) {
        let mut state = self.state.lock().expect("inference registry poisoned");
        if let Some(entry) = state.active.get_mut(inference_id) {
            entry.backend_terminated = true;
            entry.backend_task.take();
            return;
        }
        if let Some(tombstone) = state.tombstones.get_mut(inference_id) {
            if tombstone.backend_task.take().is_some() {
                state.orphan_count = state.orphan_count.saturating_sub(1);
            }
        }
        self.prune_locked(&mut state, Instant::now());
    }

    pub fn is_active(&self, inference_id: &str) -> bool {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        state.active.contains_key(inference_id)
    }

    pub fn complete(&self, inference_id: &str) -> bool {
        self.finish(inference_id, None, TerminalOutcome::Completed)
    }

    pub fn terminal_outcome(&self, inference_id: &str) -> Option<TerminalOutcome> {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        state.tombstones.get(inference_id).map(|t| t.outcome)
    }

    pub fn reserve_deadline(
        self: &Arc<Self>,
        inference_id: &str,
    ) -> Result<DeadlineReservation, DeadlineReservationError> {
        let (token, terminal) = {
            let mut state = self.state.lock().expect("inference registry poisoned");
            self.prune_locked(&mut state, Instant::now());
            if state.tombstones.contains_key(inference_id) {
                return Err(DeadlineReservationError::Terminal(
                    InferenceControlStatus::AlreadyTerminal,
                ));
            }
            let Some(entry) = state.active.get(inference_id) else {
                return Err(DeadlineReservationError::Terminal(
                    InferenceControlStatus::Unknown,
                ));
            };
            if entry.deadline_token.is_some() {
                return Err(DeadlineReservationError::Registry(
                    RegistryError::DeadlineAlreadyScheduled,
                ));
            }
            state.next_token = state.next_token.wrapping_add(1);
            let token = state.next_token;
            let entry = state
                .active
                .get_mut(inference_id)
                .expect("entry inspected above");
            entry.deadline_token = Some(token);
            (token, entry.terminal.subscribe())
        };
        Ok(DeadlineReservation {
            registry: self.clone(),
            inference_id: inference_id.to_string(),
            token,
            terminal,
            armed: true,
        })
    }

    pub async fn schedule_deadline<F, Fut>(
        self: &Arc<Self>,
        inference_id: &str,
        timeout: Duration,
        acknowledgement: F,
    ) -> Result<InferenceControlStatus, DeadlineReservationError>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = bool>,
    {
        self.reserve_deadline(inference_id)?
            .wait_and_control(timeout, acknowledgement)
            .await
            .map_err(DeadlineReservationError::Registry)
    }

    pub async fn control<F>(
        &self,
        inference_id: &str,
        cause: ControlCause,
        acknowledgement: F,
    ) -> InferenceControlStatus
    where
        F: Future<Output = bool>,
    {
        let start = self.begin_control(inference_id);
        self.drive_control_start(inference_id, cause, start, || acknowledgement)
            .await
    }

    pub fn counts(&self) -> (usize, usize) {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        (state.active.len(), state.tombstones.len())
    }

    pub fn orphan_count(&self) -> usize {
        self.state
            .lock()
            .expect("inference registry poisoned")
            .orphan_count
    }

    /// Disconnect teardown: every retained active/orphan handle is aborted.
    pub fn abort_all(&self) {
        let mut state = self.state.lock().expect("inference registry poisoned");
        for (_, mut entry) in state.active.drain() {
            if let Some(task) = entry.backend_task.take() {
                task.abort();
            }
        }
        for (_, mut tombstone) in state.tombstones.drain() {
            if let Some(task) = tombstone.backend_task.take() {
                task.abort();
            }
        }
        state.tombstone_order.clear();
        state.orphan_count = 0;
    }

    /// Roll back a start whose `infer.started` frame could not be delivered.
    pub fn abandon(&self, inference_id: &str) {
        let mut state = self.state.lock().expect("inference registry poisoned");
        if let Some(mut entry) = state.active.remove(inference_id) {
            if let Some(task) = entry.backend_task.take() {
                task.abort();
            }
        }
    }

    /// Abort a backend after `infer.started` may already have entered the
    /// socket sink, retaining a terminal tombstone so a later flush can never
    /// expose an ID that control RPCs report as unknown.
    pub fn abort_after_started_admission(&self, inference_id: &str) {
        self.abort_owned_backend(inference_id);
    }

    /// Abort this handler's owned backend while preserving an existing
    /// terminal outcome. If no control has terminalized the entry yet, publish
    /// a completed tombstone first so any frame already admitted to the socket
    /// can never expose an unknown ID. If control already moved the JoinHandle
    /// into a tombstone, detach and abort it there and release its orphan charge.
    pub fn abort_owned_backend(&self, inference_id: &str) {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        if let Some(mut entry) = state.active.remove(inference_id) {
            let outcome = TerminalOutcome::Completed;
            let _ = entry.terminal.send(Some(outcome));
            if let Some(task) = entry.backend_task.take() {
                task.abort();
            }
            self.insert_tombstone_locked(&mut state, inference_id.to_string(), outcome, None);
            return;
        }
        let Some(tombstone) = state.tombstones.get_mut(inference_id) else {
            return;
        };
        if let Some(task) = tombstone.backend_task.take() {
            task.abort();
            state.orphan_count = state.orphan_count.saturating_sub(1);
        }
    }

    fn begin_control(&self, inference_id: &str) -> ControlStart {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        if state.tombstones.contains_key(inference_id) {
            return ControlStart::Return(InferenceControlStatus::AlreadyTerminal);
        }
        let Some(phase) = state
            .active
            .get(inference_id)
            .map(|entry| entry.phase.clone())
        else {
            return ControlStart::Return(InferenceControlStatus::Unknown);
        };
        match phase {
            ActivePhase::Running => {
                state.next_token = state.next_token.wrapping_add(1);
                let token = state.next_token;
                state
                    .active
                    .get_mut(inference_id)
                    .expect("entry inspected above")
                    .phase = ActivePhase::Terminating(token);
                ControlStart::New(token)
            }
            ActivePhase::Terminating(_) => ControlStart::Wait(
                state
                    .active
                    .get(inference_id)
                    .expect("entry inspected above")
                    .terminal
                    .subscribe(),
            ),
        }
    }

    fn claim_reserved_deadline(&self, inference_id: &str, deadline_token: u64) -> ControlStart {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        if state.tombstones.contains_key(inference_id) {
            return ControlStart::Return(InferenceControlStatus::AlreadyTerminal);
        }
        let Some(entry) = state.active.get_mut(inference_id) else {
            return ControlStart::Return(InferenceControlStatus::Unknown);
        };
        if entry.deadline_token != Some(deadline_token) {
            return ControlStart::Return(InferenceControlStatus::AlreadyTerminal);
        }
        entry.deadline_token = None;
        match entry.phase.clone() {
            ActivePhase::Running => {
                state.next_token = state.next_token.wrapping_add(1);
                let token = state.next_token;
                state
                    .active
                    .get_mut(inference_id)
                    .expect("entry inspected above")
                    .phase = ActivePhase::Terminating(token);
                ControlStart::New(token)
            }
            ActivePhase::Terminating(_) => ControlStart::Wait(entry.terminal.subscribe()),
        }
    }

    async fn drive_control_start<F, Fut>(
        &self,
        inference_id: &str,
        cause: ControlCause,
        start: ControlStart,
        acknowledgement: F,
    ) -> InferenceControlStatus
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = bool>,
    {
        match start {
            ControlStart::Return(status) => status,
            ControlStart::Wait(mut receiver) => {
                if receiver.borrow().is_none() {
                    let _ = receiver.changed().await;
                }
                InferenceControlStatus::AlreadyTerminal
            }
            ControlStart::New(token) => {
                let claim = ControlClaimGuard::new(self, inference_id, token, cause);
                let confirmed =
                    tokio::time::timeout(self.config.termination_ack_timeout, acknowledgement())
                        .await
                        .unwrap_or(false);
                let status = match (cause, confirmed) {
                    (ControlCause::Cancel, true) => InferenceControlStatus::CancelledConfirmed,
                    (ControlCause::Cancel, false) => InferenceControlStatus::TerminationUnconfirmed,
                    (ControlCause::Deadline, true) => {
                        InferenceControlStatus::DeadlineExceededConfirmed
                    }
                    (ControlCause::Deadline, false) => {
                        InferenceControlStatus::DeadlineExceededUnconfirmed
                    }
                };
                claim.finish(status)
            }
        }
    }

    fn release_deadline(&self, inference_id: &str, token: u64) {
        let mut state = self.state.lock().expect("inference registry poisoned");
        if let Some(entry) = state.active.get_mut(inference_id) {
            if entry.deadline_token == Some(token) {
                entry.deadline_token = None;
            }
        }
    }

    fn finish(
        &self,
        inference_id: &str,
        expected_token: Option<u64>,
        outcome: TerminalOutcome,
    ) -> bool {
        let mut state = self.state.lock().expect("inference registry poisoned");
        self.prune_locked(&mut state, Instant::now());
        let Some(entry) = state.active.get(inference_id) else {
            return false;
        };
        if let Some(expected) = expected_token {
            if !matches!(entry.phase, ActivePhase::Terminating(actual) if actual == expected) {
                return false;
            }
        } else if !matches!(entry.phase, ActivePhase::Running) {
            return false;
        }
        let mut entry = state
            .active
            .remove(inference_id)
            .expect("entry inspected above");
        let _ = entry.terminal.send(Some(outcome));
        let backend_task = if entry.backend_terminated {
            None
        } else {
            entry.backend_task.take()
        };
        if backend_task.is_some() {
            state.orphan_count += 1;
        }
        self.insert_tombstone_locked(&mut state, inference_id.to_string(), outcome, backend_task);
        true
    }

    fn insert_tombstone_locked(
        &self,
        state: &mut RegistryState,
        inference_id: String,
        outcome: TerminalOutcome,
        backend_task: Option<tokio::task::JoinHandle<()>>,
    ) {
        state.tombstones.insert(
            inference_id.clone(),
            Tombstone {
                at: Instant::now(),
                outcome,
                backend_task,
            },
        );
        state.tombstone_order.push_back(inference_id);
        self.enforce_tombstone_bound_locked(state);
    }

    fn enforce_tombstone_bound_locked(&self, state: &mut RegistryState) {
        while state.tombstones.len() > self.config.max_tombstones {
            let Some(index) = state.tombstone_order.iter().position(|id| {
                state
                    .tombstones
                    .get(id)
                    .is_none_or(|entry| entry.backend_task.is_none())
            }) else {
                break;
            };
            let id = state
                .tombstone_order
                .remove(index)
                .expect("index inspected above");
            state.tombstones.remove(&id);
        }
    }

    fn prune_locked(&self, state: &mut RegistryState, now: Instant) {
        let mut retained = VecDeque::with_capacity(state.tombstone_order.len());
        while let Some(id) = state.tombstone_order.pop_front() {
            let remove = state.tombstones.get(&id).is_none_or(|entry| {
                entry.backend_task.is_none()
                    && now.duration_since(entry.at) >= self.config.tombstone_ttl
            });
            if remove {
                state.tombstones.remove(&id);
            } else {
                retained.push_back(id);
            }
        }
        state.tombstone_order = retained;
        self.enforce_tombstone_bound_locked(state);
    }
}