adk-kernel 3.0.0

Agent kernel for ADK-Rust: a resident supervisor with a process model, wake sources, an event bus and a control plane over durable runs
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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! Queue classes (spec 3.2 §7.9).
//!
//! Priority is a queue (§6 D-5): the kernel runs one `DurableWorker` per [`QueueClass`],
//! each with its own fallback poll interval (§13 Q2). A worker sleeps until the earliest
//! `not_before` deadline in its queue, and `enqueue` and `wake` notify it, so the poll only
//! catches runs another process enqueued (§10 S3).
//!
//! Phase 2 adds attention and resources (§7.9): [`Quotas`] per partition (max running, max
//! queued, a daily spend cap), [`QuietHours`] through the run's `not_before`, cost-aware
//! admission against the partition's daily spend, and preemption of the youngest
//! background run when an interactive one finds its partition at quota.

use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use std::time::Duration;

use adk_core::{UsageUnit, UsageVector};

use chrono::{DateTime, FixedOffset, Local, NaiveTime, TimeZone, Utc};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::error::KernelError;

/// A priority class: the run-store queue a process waits in and the worker that claims it
/// (spec 3.2 §7.9).
///
/// The three classes of the default configuration are [`Interactive`](Self::Interactive)
/// (a human turn), [`Background`](Self::Background) (goals, triggered and scheduled work)
/// and [`Maintenance`](Self::Maintenance) (the kernel's own work, `kernel.wake` runs).
/// [`Named`](Self::Named) is any other queue. A class serializes as its queue name.
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_kernel::QueueClass;
///
/// let class: QueueClass = "background".parse().unwrap();
/// assert_eq!(class, QueueClass::Background);
/// assert_eq!(class.poll_interval(), Duration::from_secs(2));
/// assert_eq!(QueueClass::Interactive.poll_interval(), Duration::from_millis(500));
/// assert_eq!(QueueClass::from_name("payments"), QueueClass::Named("payments".into()));
/// assert_eq!(serde_json::to_value(QueueClass::Maintenance).unwrap(), "maintenance");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum QueueClass {
    /// Human turns; claimed first-come with a 500 ms fallback poll.
    Interactive,
    /// Goals, triggered and scheduled processes; 2 s fallback poll.
    Background,
    /// The kernel's own work; 5 s fallback poll.
    Maintenance,
    /// Any other queue; 2 s fallback poll.
    Named(String),
}

impl QueueClass {
    /// The run-store queue name of [`Interactive`](Self::Interactive).
    pub const INTERACTIVE: &'static str = "interactive";
    /// The run-store queue name of [`Background`](Self::Background).
    pub const BACKGROUND: &'static str = "background";
    /// The run-store queue name of [`Maintenance`](Self::Maintenance).
    pub const MAINTENANCE: &'static str = "maintenance";

    /// The class of the run-store queue `name`; the three default names map to their
    /// classes and anything else is [`Named`](Self::Named).
    pub fn from_name(name: &str) -> Self {
        match name {
            Self::INTERACTIVE => Self::Interactive,
            Self::BACKGROUND => Self::Background,
            Self::MAINTENANCE => Self::Maintenance,
            other => Self::Named(other.to_string()),
        }
    }

    /// The run-store queue name the class's worker claims from.
    pub fn as_str(&self) -> &str {
        match self {
            Self::Interactive => Self::INTERACTIVE,
            Self::Background => Self::BACKGROUND,
            Self::Maintenance => Self::MAINTENANCE,
            Self::Named(name) => name,
        }
    }

    /// The worker's fallback poll interval (spec 3.2 §7.9, §13 Q2): 500 ms for
    /// `interactive`, 2 s for `background` and named queues, 5 s for `maintenance`.
    pub fn poll_interval(&self) -> Duration {
        match self {
            Self::Interactive => Duration::from_millis(500),
            Self::Background | Self::Named(_) => Duration::from_secs(2),
            Self::Maintenance => Duration::from_secs(5),
        }
    }

    /// The default queue classes: interactive, background and maintenance (spec 3.2 §7.2).
    pub fn defaults() -> Vec<Self> {
        vec![Self::Interactive, Self::Background, Self::Maintenance]
    }
}

impl fmt::Display for QueueClass {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for QueueClass {
    type Err = KernelError;

    fn from_str(name: &str) -> Result<Self, Self::Err> {
        let name = name.trim();
        if name.is_empty() {
            return Err(KernelError::Config {
                reason: "a queue name must not be empty; use interactive, background, \
                         maintenance or a queue the kernel configures"
                    .to_string(),
            });
        }
        Ok(Self::from_name(name))
    }
}

impl Serialize for QueueClass {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for QueueClass {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let name = String::deserialize(deserializer)?;
        name.parse().map_err(serde::de::Error::custom)
    }
}

/// A daily local-time window in which proactive and maintenance processes never start
/// (spec 3.2 §7.5, §7.9, K-I11); interactive and approval-woken runs always may.
///
/// The window runs from `start` up to, not including, `end`. A `start` after `end` wraps
/// midnight (`22:00`–`07:00`); equal times make an empty window. Times are local: in the
/// IANA `time_zone` when set, following its daylight saving time; else at
/// `utc_offset_minutes` east of UTC when set; else in the host's time zone. A process that
/// would start inside the window gets `not_before` = [`release_at`](Self::release_at), so
/// its worker sleeps until the window ends. Serializes as
/// `{"start": "22:00", "end": "07:00", "utcOffsetMinutes": 180}` or
/// `{"start": "22:00", "end": "07:00", "timeZone": "Europe/London"}`.
///
/// # Example
///
/// ```rust
/// use chrono::{TimeZone, Utc};
/// use adk_kernel::QuietHours;
///
/// let quiet: QuietHours = "22:00-07:00".parse().unwrap();
/// let quiet = quiet.with_utc_offset_minutes(0);
/// let night = Utc.with_ymd_and_hms(2026, 10, 23, 23, 30, 0).unwrap();
/// assert!(quiet.contains(night));
/// assert_eq!(quiet.release_at(night), Utc.with_ymd_and_hms(2026, 10, 24, 7, 0, 0).unwrap());
/// let noon = Utc.with_ymd_and_hms(2026, 10, 23, 12, 0, 0).unwrap();
/// assert!(!quiet.contains(noon));
/// assert_eq!(quiet.release_at(noon), noon);
/// assert_eq!(serde_json::to_value(&quiet).unwrap()["start"], "22:00");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct QuietHours {
    /// Local time the window starts.
    #[serde(with = "hh_mm")]
    pub start: NaiveTime,
    /// Local time the window ends (exclusive).
    #[serde(with = "hh_mm")]
    pub end: NaiveTime,
    /// Minutes east of UTC the times are in; `None` is the host's time zone.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub utc_offset_minutes: Option<i32>,
    /// The IANA time zone the times are in, such as `Europe/London`, ahead of
    /// `utc_offset_minutes`: the window keeps its local times across daylight saving time,
    /// where a fixed offset moves them by an hour (spec 3.3 §10 K3).
    #[serde(default, skip_serializing_if = "Option::is_none", with = "zone")]
    pub time_zone: Option<chrono_tz::Tz>,
}

impl QuietHours {
    /// The window from `start` to `end`, in the host's time zone.
    pub fn new(start: NaiveTime, end: NaiveTime) -> Self {
        Self { start, end, utc_offset_minutes: None, time_zone: None }
    }

    /// Reads the times at `minutes` east of UTC instead of the host's time zone.
    #[must_use]
    pub fn with_utc_offset_minutes(mut self, minutes: i32) -> Self {
        self.utc_offset_minutes = Some(minutes);
        self
    }

    /// Reads the times in the IANA time zone `zone`, following its daylight saving time.
    ///
    /// # Example
    ///
    /// ```rust
    /// use chrono::{TimeZone, Utc};
    /// use adk_kernel::QuietHours;
    ///
    /// let quiet: QuietHours = "22:00-07:00".parse().unwrap();
    /// let quiet = quiet.with_time_zone(chrono_tz::Europe::London);
    /// // 22:30 in London is 21:30 UTC in summer and 22:30 UTC in winter.
    /// assert!(quiet.contains(Utc.with_ymd_and_hms(2026, 7, 1, 21, 30, 0).unwrap()));
    /// assert!(quiet.contains(Utc.with_ymd_and_hms(2026, 12, 1, 22, 30, 0).unwrap()));
    /// assert!(!quiet.contains(Utc.with_ymd_and_hms(2026, 12, 1, 21, 30, 0).unwrap()));
    /// ```
    #[must_use]
    pub fn with_time_zone(mut self, zone: chrono_tz::Tz) -> Self {
        self.time_zone = Some(zone);
        self
    }

    fn offset(&self) -> Option<FixedOffset> {
        self.utc_offset_minutes.and_then(|minutes| FixedOffset::east_opt(minutes * 60))
    }

    fn local_time(&self, at: DateTime<Utc>) -> NaiveTime {
        if let Some(zone) = self.time_zone {
            return at.with_timezone(&zone).time();
        }
        match self.offset() {
            Some(offset) => at.with_timezone(&offset).time(),
            None => at.with_timezone(&Local).time(),
        }
    }

    fn inside(&self, time: NaiveTime) -> bool {
        match self.start.cmp(&self.end) {
            std::cmp::Ordering::Less => time >= self.start && time < self.end,
            std::cmp::Ordering::Greater => time >= self.start || time < self.end,
            std::cmp::Ordering::Equal => false,
        }
    }

    /// Whether `at` falls inside the window.
    pub fn contains(&self, at: DateTime<Utc>) -> bool {
        self.inside(self.local_time(at))
    }

    /// The earliest instant at or after `at` outside the window: `at` itself when it is
    /// outside, else the window's end.
    pub fn release_at(&self, at: DateTime<Utc>) -> DateTime<Utc> {
        if !self.contains(at) {
            return at;
        }
        let local_date = match (self.time_zone, self.offset()) {
            (Some(zone), _) => at.with_timezone(&zone).date_naive(),
            (None, Some(offset)) => at.with_timezone(&offset).date_naive(),
            (None, None) => at.with_timezone(&Local).date_naive(),
        };
        // The end is today when it is still ahead of `at`, else tomorrow.
        for days in 0..=2 {
            let Some(date) = local_date.checked_add_days(chrono::Days::new(days)) else {
                break;
            };
            let naive = date.and_time(self.end);
            let end = match (self.time_zone, self.offset()) {
                (Some(zone), _) => zone.from_local_datetime(&naive).earliest().map(|t| t.to_utc()),
                (None, Some(offset)) => {
                    offset.from_local_datetime(&naive).earliest().map(|t| t.to_utc())
                }
                (None, None) => Local.from_local_datetime(&naive).earliest().map(|t| t.to_utc()),
            };
            if let Some(end) = end
                && end > at
            {
                return end;
            }
        }
        at + chrono::Duration::days(1)
    }
}

impl FromStr for QuietHours {
    type Err = KernelError;

    /// Parses `HH:MM-HH:MM`, in the host's time zone.
    fn from_str(text: &str) -> Result<Self, Self::Err> {
        let invalid = || KernelError::Config {
            reason: format!(
                "invalid quiet hours `{text}`; write them as HH:MM-HH:MM, e.g. 22:00-07:00"
            ),
        };
        let (start, end) = text.trim().split_once('-').ok_or_else(invalid)?;
        let start = hh_mm::parse(start.trim()).ok_or_else(invalid)?;
        let end = hh_mm::parse(end.trim()).ok_or_else(invalid)?;
        Ok(Self::new(start, end))
    }
}

/// An IANA time zone as its name, such as `Europe/London`.
mod zone {
    use chrono_tz::Tz;
    use serde::{Deserialize, Deserializer, Serializer};

    pub(super) fn serialize<S: Serializer>(
        zone: &Option<Tz>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        match zone {
            Some(zone) => serializer.serialize_str(zone.name()),
            None => serializer.serialize_none(),
        }
    }

    pub(super) fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Option<Tz>, D::Error> {
        let Some(name) = Option::<String>::deserialize(deserializer)? else { return Ok(None) };
        name.parse::<Tz>().map(Some).map_err(|_| {
            serde::de::Error::custom(format!(
                "`{name}` is not an IANA time zone; name one such as `Europe/London`"
            ))
        })
    }
}

/// `NaiveTime` as `HH:MM` (seconds accepted on input).
mod hh_mm {
    use chrono::NaiveTime;
    use serde::{Deserialize, Deserializer, Serializer};

    pub(super) fn parse(text: &str) -> Option<NaiveTime> {
        NaiveTime::parse_from_str(text, "%H:%M")
            .or_else(|_| NaiveTime::parse_from_str(text, "%H:%M:%S"))
            .ok()
    }

    pub(super) fn serialize<S: Serializer>(
        time: &NaiveTime,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&time.format("%H:%M").to_string())
    }

    pub(super) fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<NaiveTime, D::Error> {
        let text = String::deserialize(deserializer)?;
        parse(&text)
            .ok_or_else(|| serde::de::Error::custom(format!("`{text}` is not an HH:MM time")))
    }
}

/// A partition's quotas (spec 3.2 §7.9): enforced at admission, they bound each partition
/// within a queue so none starves another (§13 Q2). `None` leaves a quota unlimited.
///
/// | Quota | Enforced |
/// |---|---|
/// | `max_running` | A claimed process whose partition already runs this many waits for a slot; an interactive one preempts the youngest background run |
/// | `max_queued` | [`ProcessTable::spawn`](crate::ProcessTable::spawn) and agenda scheduling refuse a process past it |
/// | `daily_spend_cap` | A background process is admitted only while the partition's spend today plus its budget's cost cap stays within it; else it sleeps until `BudgetReplenished` |
/// | `daily_caps` | Per [`UsageUnit`]: a background process is admitted only while the partition's usage of the unit today is below the cap; else it sleeps until `BudgetReplenished`, the next UTC day |
///
/// The `kernel.wake` runs the kernel routes wakes with are never counted or held.
/// The proactive limits — open proposals and the daily proactive budget — are the
/// partition's [`ProactivityPolicy`](crate::ProactivityPolicy).
///
/// # Example
///
/// ```rust
/// use adk_kernel::Quotas;
///
/// use adk_core::UsageUnit;
///
/// let quotas = Quotas::new()
///     .with_max_running(3)
///     .with_max_queued(20)
///     .with_daily_spend_cap(5_000_000)
///     .with_daily_cap(UsageUnit::Images, 200);
/// assert_eq!(quotas.max_running, Some(3));
/// let json = serde_json::to_value(&quotas).unwrap();
/// assert_eq!(json["dailySpendCap"], 5_000_000);
/// assert_eq!(json["dailyCaps"]["images"], 200);
/// assert_eq!(Quotas::default().max_running, None);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Quotas {
    /// Most processes of the partition running at once, over every queue.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_running: Option<usize>,
    /// Most processes of the partition queued at once.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_queued: Option<usize>,
    /// Most micro-USD the partition spends per UTC day.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub daily_spend_cap: Option<u64>,
    /// Most of each unit the partition uses per UTC day.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub daily_caps: BTreeMap<UsageUnit, u64>,
}

impl Quotas {
    /// No quota.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the most running processes.
    #[must_use]
    pub fn with_max_running(mut self, max: usize) -> Self {
        self.max_running = Some(max);
        self
    }

    /// Sets the most queued processes.
    #[must_use]
    pub fn with_max_queued(mut self, max: usize) -> Self {
        self.max_queued = Some(max);
        self
    }

    /// Sets the daily spend cap, in micro-USD.
    #[must_use]
    pub fn with_daily_spend_cap(mut self, micro_usd: u64) -> Self {
        self.daily_spend_cap = Some(micro_usd);
        self
    }

    /// Caps the partition's daily usage of `unit`.
    #[must_use]
    pub fn with_daily_cap(mut self, unit: UsageUnit, max: u64) -> Self {
        self.daily_caps.insert(unit, max);
        self
    }

    /// Each limit the lower of `self`'s and `other`'s; a limit only one sets applies.
    pub(crate) fn stricter(&self, other: &Self) -> Self {
        fn lower<T: Ord + Copy>(a: Option<T>, b: Option<T>) -> Option<T> {
            match (a, b) {
                (Some(a), Some(b)) => Some(a.min(b)),
                (a, b) => a.or(b),
            }
        }
        let mut daily_caps = self.daily_caps.clone();
        for (unit, max) in &other.daily_caps {
            let cap = daily_caps.entry(*unit).or_insert(*max);
            *cap = (*cap).min(*max);
        }
        Self {
            max_running: lower(self.max_running, other.max_running),
            max_queued: lower(self.max_queued, other.max_queued),
            daily_spend_cap: lower(self.daily_spend_cap, other.daily_spend_cap),
            daily_caps,
        }
    }

    /// Whether a daily limit of these quotas applies at admission.
    pub(crate) fn caps_daily_usage(&self) -> bool {
        self.daily_spend_cap.is_some() || !self.daily_caps.is_empty()
    }
}

/// How admission treats a claimed process (spec 3.2 §7.9).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum ProcessClass {
    /// A human turn: never held by quiet hours or budgets; preempts background work at
    /// quota.
    Interactive,
    /// Goals, triggered and scheduled work: quotas and the partition's daily spend cap.
    Background,
    /// Agenda follow-ups and the kernel's maintenance processes: quotas, the spend cap, the
    /// daily proactive budget and quiet hours.
    Proactive,
}

/// What admission reads about a claimed process and its partition (spec 3.2 §7.9).
#[derive(Debug, Clone)]
pub(crate) struct AdmissionInput<'a> {
    pub(crate) class: ProcessClass,
    /// The run resumes after a decided approval; quiet hours never hold it.
    pub(crate) approval_woken: bool,
    pub(crate) now: DateTime<Utc>,
    pub(crate) quiet_hours: &'a [QuietHours],
    pub(crate) quotas: &'a Quotas,
    /// Admitted processes of the partition running now, this one excluded.
    pub(crate) running: usize,
    /// Whether one of them runs on the `background` queue and can be preempted.
    pub(crate) preemptible: bool,
    /// The partition's spend today, in micro-USD.
    pub(crate) partition_spent: u64,
    /// The partition's usage today per unit.
    pub(crate) partition_units: UsageVector,
    /// The partition's proactive spend today, in micro-USD.
    pub(crate) proactive_spent: u64,
    /// The partition's proactive model calls and total tokens today.
    pub(crate) proactive_calls_and_tokens: (u64, u64),
    /// The cost cap of the daily proactive budget.
    pub(crate) daily_budget: Option<u64>,
    /// The model-call and token caps of the daily proactive budget.
    pub(crate) daily_calls_and_tokens: (Option<u64>, Option<u64>),
    /// The cost cap of the process's own budget; 0 without one.
    pub(crate) need: u64,
}

/// Admission's answer for a claimed process (spec 3.2 §7.9).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Admission {
    /// It may run.
    Admit,
    /// Inside quiet hours: it sleeps until they end.
    QuietHours { until: DateTime<Utc> },
    /// The day's budget does not cover it: it sleeps until `BudgetReplenished`.
    Budget,
    /// The partition runs `max_running` processes: it waits for a slot, or, for an
    /// interactive one when `preempt` is set, takes the slot of the youngest background
    /// process, which parks at its next checkpoint.
    Quota { preempt: bool },
}

impl Admission {
    /// The label value a deferred process carries, `None` when admitted.
    pub(crate) fn reason(self) -> Option<&'static str> {
        match self {
            Self::Admit => None,
            Self::QuietHours { .. } => Some("quiet_hours"),
            Self::Budget => Some("budget"),
            Self::Quota { .. } => Some("quota"),
        }
    }
}

/// Whether `spent` plus `need` stays within `cap`; a process with no budget of its own
/// needs only that something is left.
fn covers(cap: Option<u64>, spent: u64, need: u64) -> bool {
    cap.is_none_or(|cap| spent.saturating_add(need.max(1)) <= cap)
}

/// Decides whether a claimed process starts now (spec 3.2 §7.9, K-I11): quiet hours first,
/// then the daily budgets, then the partition's running quota.
pub(crate) fn admit(input: &AdmissionInput<'_>) -> Admission {
    if input.class == ProcessClass::Proactive && !input.approval_woken {
        let mut until = input.now;
        // A window may end inside another; settle on an instant outside every one.
        for _ in 0..=input.quiet_hours.len() {
            let next = input
                .quiet_hours
                .iter()
                .map(|quiet| quiet.release_at(until))
                .max()
                .unwrap_or(until);
            if next == until {
                break;
            }
            until = next;
        }
        if until > input.now {
            return Admission::QuietHours { until };
        }
    }
    if input.class != ProcessClass::Interactive {
        let within_cap = covers(input.quotas.daily_spend_cap, input.partition_spent, input.need)
            && input
                .quotas
                .daily_caps
                .iter()
                .all(|(unit, max)| input.partition_units.get(*unit) < *max);
        let (calls, tokens) = input.proactive_calls_and_tokens;
        let (max_calls, max_tokens) = input.daily_calls_and_tokens;
        let within_budget = input.class != ProcessClass::Proactive
            || (covers(input.daily_budget, input.proactive_spent, input.need)
                && max_calls.is_none_or(|max| calls < max)
                && max_tokens.is_none_or(|max| tokens < max));
        if !within_cap || !within_budget {
            return Admission::Budget;
        }
    }
    if input.quotas.max_running.is_some_and(|max| input.running >= max) {
        return Admission::Quota {
            preempt: input.class == ProcessClass::Interactive && input.preemptible,
        };
    }
    Admission::Admit
}

/// The process an interactive one preempts (spec 3.2 §7.9): the youngest running process on
/// the `background` queue, by creation time and then id.
pub(crate) fn youngest_background<'a>(
    candidates: impl IntoIterator<Item = (&'a str, DateTime<Utc>)>,
) -> Option<&'a str> {
    candidates
        .into_iter()
        .max_by(|a, b| a.1.cmp(&b.1).then_with(|| a.0.cmp(b.0)))
        .map(|(pid, _)| pid)
}

#[cfg(test)]
mod admission_tests {
    use chrono::TimeZone;
    use proptest::prelude::*;

    use super::*;

    fn at(hour: u32, minute: u32) -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 10, 23, hour, minute, 0).unwrap()
    }

    fn input<'a>(
        class: ProcessClass,
        quiet: &'a [QuietHours],
        quotas: &'a Quotas,
    ) -> AdmissionInput<'a> {
        AdmissionInput {
            class,
            approval_woken: false,
            now: at(12, 0),
            quiet_hours: quiet,
            quotas,
            running: 0,
            preemptible: false,
            partition_spent: 0,
            partition_units: UsageVector::new(),
            proactive_spent: 0,
            proactive_calls_and_tokens: (0, 0),
            daily_budget: None,
            daily_calls_and_tokens: (None, None),
            need: 0,
        }
    }

    #[test]
    fn daily_unit_caps_hold_background_and_proactive_work() {
        let quotas = Quotas::new().with_daily_cap(UsageUnit::Images, 3);
        for class in [ProcessClass::Background, ProcessClass::Proactive] {
            let mut room = input(class, &[], &quotas);
            room.partition_units = UsageVector::new().with(UsageUnit::Images, 2);
            assert_eq!(admit(&room), Admission::Admit, "{class:?}");
            room.partition_units = UsageVector::new().with(UsageUnit::Images, 3);
            assert_eq!(admit(&room), Admission::Budget, "{class:?} at the cap");
        }
        let mut human = input(ProcessClass::Interactive, &[], &quotas);
        human.partition_units = UsageVector::new().with(UsageUnit::Images, 30);
        assert_eq!(admit(&human), Admission::Admit, "a human turn is never held");
        // A cap on one unit ignores the others.
        let mut other = input(ProcessClass::Background, &[], &quotas);
        other.partition_units = UsageVector::new().with(UsageUnit::AudioMillis, 1 << 40);
        assert_eq!(admit(&other), Admission::Admit);
    }

    #[test]
    fn the_daily_proactive_budget_caps_model_calls_and_tokens() {
        let quotas = Quotas::new();
        let mut proactive = input(ProcessClass::Proactive, &[], &quotas);
        proactive.daily_calls_and_tokens = (Some(10), Some(50_000));
        proactive.proactive_calls_and_tokens = (9, 49_999);
        assert_eq!(admit(&proactive), Admission::Admit);
        proactive.proactive_calls_and_tokens = (10, 0);
        assert_eq!(admit(&proactive), Admission::Budget, "model calls reached");
        proactive.proactive_calls_and_tokens = (0, 50_000);
        assert_eq!(admit(&proactive), Admission::Budget, "tokens reached");
        let mut background = input(ProcessClass::Background, &[], &quotas);
        background.daily_calls_and_tokens = (Some(10), Some(50_000));
        background.proactive_calls_and_tokens = (10, 50_000);
        assert_eq!(admit(&background), Admission::Admit, "the proactive budget is not the cap");
    }

    #[test]
    fn stricter_quotas_take_the_lower_cap_per_unit() {
        let kernel =
            Quotas::new().with_daily_cap(UsageUnit::Images, 10).with_daily_cap(UsageUnit::Pages, 5);
        let partition = Quotas::new()
            .with_daily_cap(UsageUnit::Images, 3)
            .with_daily_cap(UsageUnit::AudioMillis, 60_000);
        assert_eq!(
            kernel.stricter(&partition).daily_caps,
            BTreeMap::from([
                (UsageUnit::Images, 3),
                (UsageUnit::AudioMillis, 60_000),
                (UsageUnit::Pages, 5),
            ])
        );
        let json = serde_json::json!({ "dailyCaps": { "images": 3 } });
        assert_eq!(
            serde_json::from_value::<Quotas>(json).unwrap(),
            Quotas::new().with_daily_cap(UsageUnit::Images, 3)
        );
    }

    fn night() -> QuietHours {
        "22:00-07:00".parse::<QuietHours>().unwrap().with_utc_offset_minutes(0)
    }

    #[test]
    fn the_admission_table() {
        let quiet = [night()];
        let quotas = Quotas::new().with_max_running(3).with_daily_spend_cap(1_000);
        let base = |class| input(class, &quiet, &quotas);

        // Free partition, daytime: everything runs.
        for class in [ProcessClass::Interactive, ProcessClass::Background, ProcessClass::Proactive]
        {
            assert_eq!(admit(&base(class)), Admission::Admit, "{class:?}");
        }
        // Quiet hours hold proactive work only, and never an approval-woken run.
        let mut late = base(ProcessClass::Proactive);
        late.now = at(23, 0);
        assert_eq!(
            admit(&late),
            Admission::QuietHours { until: Utc.with_ymd_and_hms(2026, 10, 24, 7, 0, 0).unwrap() }
        );
        late.approval_woken = true;
        assert_eq!(admit(&late), Admission::Admit);
        for class in [ProcessClass::Interactive, ProcessClass::Background] {
            let mut late = base(class);
            late.now = at(23, 0);
            assert_eq!(admit(&late), Admission::Admit, "{class:?}");
        }
        // Partition at quota: background and proactive wait; interactive preempts when it can.
        for class in [ProcessClass::Background, ProcessClass::Proactive] {
            let mut full = base(class);
            full.running = 3;
            full.preemptible = true;
            assert_eq!(admit(&full), Admission::Quota { preempt: false }, "{class:?}");
        }
        let mut full = base(ProcessClass::Interactive);
        full.running = 3;
        assert_eq!(admit(&full), Admission::Quota { preempt: false }, "nothing to preempt");
        full.preemptible = true;
        assert_eq!(admit(&full), Admission::Quota { preempt: true });
        // Spend cap: a background run whose budget the rest of the day does not cover waits.
        let mut spent = base(ProcessClass::Background);
        spent.partition_spent = 900;
        spent.need = 100;
        assert_eq!(admit(&spent), Admission::Admit, "exactly covered");
        spent.need = 101;
        assert_eq!(admit(&spent), Admission::Budget);
        spent.need = 0;
        spent.partition_spent = 1_000;
        assert_eq!(admit(&spent), Admission::Budget, "budget exhausted");
        let mut human = base(ProcessClass::Interactive);
        human.partition_spent = 5_000;
        assert_eq!(admit(&human), Admission::Admit, "a human turn is never held by spend");
        // Daily proactive budget: proactive only.
        let mut proactive = base(ProcessClass::Proactive);
        proactive.daily_budget = Some(500);
        proactive.proactive_spent = 450;
        proactive.need = 60;
        assert_eq!(admit(&proactive), Admission::Budget);
        let mut background = base(ProcessClass::Background);
        background.daily_budget = Some(500);
        background.proactive_spent = 450;
        background.need = 60;
        assert_eq!(admit(&background), Admission::Admit, "the proactive budget is not the cap");
    }

    #[test]
    fn overlapping_windows_release_outside_both() {
        let quiet = [
            "22:00-02:00".parse::<QuietHours>().unwrap().with_utc_offset_minutes(0),
            "01:00-06:30".parse::<QuietHours>().unwrap().with_utc_offset_minutes(0),
        ];
        let quotas = Quotas::new();
        let mut late = input(ProcessClass::Proactive, &quiet, &quotas);
        late.now = at(23, 0);
        assert_eq!(
            admit(&late),
            Admission::QuietHours { until: Utc.with_ymd_and_hms(2026, 10, 24, 6, 30, 0).unwrap() }
        );
    }

    #[test]
    fn preemption_takes_the_youngest_background_run() {
        let runs =
            [("run-a", at(9, 0)), ("run-c", at(11, 0)), ("run-b", at(11, 0)), ("run-d", at(8, 0))];
        assert_eq!(youngest_background(runs), Some("run-c"));
        assert_eq!(youngest_background(std::iter::empty()), None);
    }

    #[test]
    fn quiet_hours_boundaries() {
        let quiet = night();
        assert!(quiet.contains(at(22, 0)), "the start is inside");
        assert!(
            !quiet.contains(Utc.with_ymd_and_hms(2026, 10, 24, 7, 0, 0).unwrap()),
            "the end is outside"
        );
        assert!(!quiet.contains(at(21, 59)));
        let empty = "09:00-09:00".parse::<QuietHours>().unwrap().with_utc_offset_minutes(0);
        assert!(!empty.contains(at(9, 0)), "equal times make no window");
        let east = "22:00-07:00".parse::<QuietHours>().unwrap().with_utc_offset_minutes(180);
        assert!(east.contains(at(20, 0)), "23:00 at UTC+3");
        assert_eq!(
            east.release_at(at(20, 0)),
            Utc.with_ymd_and_hms(2026, 10, 24, 4, 0, 0).unwrap()
        );
        assert!("22-07".parse::<QuietHours>().is_err());
    }

    fn class() -> impl Strategy<Value = ProcessClass> {
        prop_oneof![
            Just(ProcessClass::Interactive),
            Just(ProcessClass::Background),
            Just(ProcessClass::Proactive)
        ]
    }

    #[derive(Debug, Clone)]
    enum Step {
        Claim { class: ProcessClass, need: u64 },
        Finish(usize),
        NextDay,
    }

    fn step() -> impl Strategy<Value = Step> {
        prop_oneof![
            4 => (class(), 0u64..400).prop_map(|(class, need)| Step::Claim { class, need }),
            2 => (0usize..8).prop_map(Step::Finish),
            1 => Just(Step::NextDay),
        ]
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(256))]

        /// Admission never lets a partition run more than `max_running` processes, nor
        /// spend past its cap or its daily proactive budget, whatever arrives in what order;
        /// each admitted process spends at most its budget's cost cap.
        #[test]
        fn admission_never_exceeds_quota_or_budget(
            max_running in 1usize..5,
            cap in 0u64..2_000,
            budget in 0u64..1_500,
            steps in proptest::collection::vec(step(), 1..80),
        ) {
            let quotas = Quotas::new().with_max_running(max_running).with_daily_spend_cap(cap);
            let mut running: Vec<ProcessClass> = Vec::new();
            let (mut background_spent, mut proactive_spent) = (0u64, 0u64);
            for step in steps {
                match step {
                    Step::Claim { class, need } => {
                        let decision = admit(&AdmissionInput {
                            class,
                            approval_woken: false,
                            now: at(12, 0),
                            quiet_hours: &[],
                            quotas: &quotas,
                            running: running.len(),
                            preemptible: running.contains(&ProcessClass::Background),
                            partition_spent: background_spent + proactive_spent,
                            partition_units: UsageVector::new(),
                            proactive_spent,
                            proactive_calls_and_tokens: (0, 0),
                            daily_budget: Some(budget),
                            daily_calls_and_tokens: (None, None),
                            need,
                        });
                        match decision {
                            Admission::Admit => {
                                running.push(class);
                                match class {
                                    ProcessClass::Background => background_spent += need,
                                    ProcessClass::Proactive => proactive_spent += need,
                                    ProcessClass::Interactive => {}
                                }
                            }
                            Admission::Quota { preempt: true } => {
                                // The youngest background run stops counting; the human
                                // turn takes its slot.
                                let victim = running.iter().rposition(|c| *c == ProcessClass::Background);
                                prop_assert!(victim.is_some());
                                running.remove(victim.unwrap());
                                running.push(class);
                            }
                            Admission::Quota { preempt: false } | Admission::Budget => {}
                            Admission::QuietHours { .. } => prop_assert!(false, "no quiet hours set"),
                        }
                    }
                    Step::Finish(index) => {
                        if !running.is_empty() {
                            running.remove(index % running.len());
                        }
                    }
                    Step::NextDay => {
                        background_spent = 0;
                        proactive_spent = 0;
                    }
                }
                prop_assert!(running.len() <= max_running, "{} running of {max_running}", running.len());
                prop_assert!(background_spent + proactive_spent <= cap);
                prop_assert!(proactive_spent <= budget);
            }
        }

        /// No proactive process is admitted inside quiet hours, and its release time lies
        /// outside them; interactive and approval-woken runs are never held by them.
        #[test]
        fn no_proactive_process_starts_inside_quiet_hours(
            start in 0u32..24 * 60,
            end in 0u32..24 * 60,
            offset in -12i32 * 60..14 * 60,
            minute in 0i64..3 * 24 * 60,
            class in class(),
            approval_woken in any::<bool>(),
        ) {
            let time = |m: u32| NaiveTime::from_hms_opt(m / 60, m % 60, 0).unwrap();
            let quiet = [QuietHours::new(time(start), time(end)).with_utc_offset_minutes(offset)];
            let quotas = Quotas::new();
            let now = at(0, 0) + chrono::Duration::minutes(minute);
            let mut probe = input(class, &quiet, &quotas);
            probe.now = now;
            probe.approval_woken = approval_woken;
            let decision = admit(&probe);
            let held = class == ProcessClass::Proactive && !approval_woken && quiet[0].contains(now);
            match decision {
                Admission::Admit => prop_assert!(!held),
                Admission::QuietHours { until } => {
                    prop_assert!(held);
                    prop_assert!(until > now);
                    prop_assert!(!quiet[0].contains(until));
                    prop_assert!(until - now <= chrono::Duration::days(1));
                }
                other => prop_assert!(false, "unexpected {other:?}"),
            }
        }
    }
}