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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
//! The process model (spec 3.2 ยง7.3).
//!
//! A process is a durable run (ยง6 D-2, K-I1): its id is the run id, its state is the run's
//! [`RunState`], and `Waiting` is sleep. The kernel keeps what the run
//! store does not in a side table, [`ProcessMeta`] in a [`ProcessStore`],
//! and serves a live [`ProcessView`] of each process without `adk-eval` (ยง7.10, ยง13 Q4).
//!
//! | OS notion | Kernel mapping |
//! |---|---|
//! | Process states | `RunState::{Queued, Running, Waiting, Completed, Failed, Cancelled}`; `Waiting` is sleep |
//! | Process tree | [`ProcessMeta::parent`]: the run that caused the process |
//! | Time slice | The lease; a process yields at every model-call boundary |
//! | `kill -TERM` / `kill -KILL` | `request_cancel` at the next checkpoint / lease revocation |
//! | Zombie | A `Running` run whose lease expired; the next claim resumes it |
//!
//! [`ProcessTable`] is the view over `DurableRunStore` plus the side table, with
//! `ps`, `kill`, `nice`, `suspend` and `resume`.

use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
use std::time::Duration;

use adk_core::{
    AdkError, DurableRunStore, Enqueued, ErrorCategory, ErrorComponent, NewRun, RunFilter,
    RunRecord, RunState, SpendLedger,
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::audit;
use crate::bus::KernelEvent;
use crate::error::{KernelError, Result};
use crate::partition::PartitionId;
use crate::sched::QueueClass;
use crate::stores::{ProcessFilter, ProcessStore, SpendScope};
use crate::supervise::{ASLEEP_KEY_PREFIX, SPAWN_KEY_PREFIX, Supervision, transcript_counts};
use crate::wake::WakeSet;

/// A process as `ps` shows it: the run, the kernel's side table and the live view
/// (spec 3.2 ยง7.3).
///
/// # Example
///
/// ```rust
/// use adk_core::{NewRun, RunRecord};
/// use adk_kernel::{Process, ProcessKind, ProcessMeta, QueueClass};
///
/// let run = RunRecord::queued("run-1", NewRun::new("background", "adk-rust", "alice", "adk-rust", serde_json::json!({})), chrono::Utc::now());
/// let meta = ProcessMeta::new("run-1", QueueClass::Background, ProcessKind::Goal);
/// let process = Process::new(run, meta);
/// assert_eq!(process.meta.pid, process.run.run_id);
/// assert_eq!(process.view.model_calls, 0);
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Process {
    /// The durable run (`adk-core/src/run_store.rs`).
    pub run: RunRecord,
    /// The kernel-owned side table, keyed by run id.
    pub meta: ProcessMeta,
    /// Live aggregates, served by the kernel without `adk-eval` (spec 3.2 ยง7.10).
    pub view: ProcessView,
}

impl Process {
    /// A process with an empty view.
    pub fn new(run: RunRecord, meta: ProcessMeta) -> Self {
        Self { run, meta, view: ProcessView::default() }
    }

    /// Sets the live view.
    #[must_use]
    pub fn with_view(mut self, view: ProcessView) -> Self {
        self.view = view;
        self
    }
}

/// A process's live aggregates (spec 3.2 ยง7.3, ยง7.10, ยง13 Q4): what `ps` and `top` show,
/// computed by sum and count over the kernel's records. Spend is in micro-USD, as
/// `SpendLedger` counts it; the kernel attributes each charge to the run that made it and
/// keeps daily totals per process ([`ProcessStore::spend`]).
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_kernel::ProcessView;
///
/// let mut view = ProcessView::default();
/// view.age = Duration::from_secs(90);
/// view.model_calls = 3;
/// let json = serde_json::to_value(&view).unwrap();
/// assert_eq!(json["ageMs"], 90_000);
/// assert_eq!(json["modelCalls"], 3);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ProcessView {
    /// Since the last state change: a `Waiting` process's time asleep, a parked run's wait.
    #[serde(rename = "ageMs", with = "crate::serde_util::millis")]
    pub age: Duration,
    /// Spend today (UTC), from the kernel's daily totals.
    pub spend_today: u64,
    /// Spend over the last seven UTC days, today included, from the kernel's daily totals.
    #[serde(rename = "spend7d")]
    pub spend_7d: u64,
    /// A `Running` process's spend per minute.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub burn_rate: Option<f64>,
    /// A `Running` process's spend by the end of the UTC day at its burn rate.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub projection: Option<u64>,
    /// Model calls the process made.
    pub model_calls: u64,
    /// Tool calls the process made.
    pub tool_calls: u64,
    /// The loop guard's last verdict (spec 3.1 ยง7.13).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub loop_guard: Option<String>,
    /// A goal process's progress (spec 3.1 ยง7.11, spec 3.2 ยง10 HK3).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub goal: Option<GoalProgress>,
}

/// Where a goal process stands (spec 3.2 ยง7.3, ยง10 HK3): iteration of max, spend, duration,
/// and the last check's exit code and output tail.
///
/// The kernel cannot read harness types (ยง6 D-1), so the harness reports it through a
/// [`ProgressSource`].
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_kernel::GoalProgress;
///
/// let progress = GoalProgress::new(3, 10)
///     .with_spend(12_500)
///     .with_duration(Duration::from_secs(240))
///     .with_last_check(1, "test result: FAILED. 1 passed; 2 failed");
/// assert_eq!((progress.iteration, progress.max_iterations), (3, 10));
/// assert_eq!(progress.last_check_exit, Some(1));
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct GoalProgress {
    /// The current iteration.
    pub iteration: u32,
    /// The most iterations the goal may take.
    pub max_iterations: u32,
    /// Spend so far, in micro-USD, from the `SpendLedger`.
    pub spend: u64,
    /// How long the goal has run.
    #[serde(rename = "durationMs", with = "crate::serde_util::millis")]
    pub duration: Duration,
    /// The last check's exit code.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_check_exit: Option<i32>,
    /// The tail of the last check's output.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_check_tail: Option<String>,
}

impl GoalProgress {
    /// Progress at `iteration` of `max_iterations`.
    pub fn new(iteration: u32, max_iterations: u32) -> Self {
        Self { iteration, max_iterations, ..Self::default() }
    }

    /// Sets the spend so far, in micro-USD.
    #[must_use]
    pub fn with_spend(mut self, micro_usd: u64) -> Self {
        self.spend = micro_usd;
        self
    }

    /// Sets how long the goal has run.
    #[must_use]
    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    /// Sets the last check's exit code and output tail.
    #[must_use]
    pub fn with_last_check(mut self, exit: i32, tail: impl Into<String>) -> Self {
        self.last_check_exit = Some(exit);
        self.last_check_tail = Some(tail.into());
        self
    }
}

/// The kernel-owned side table of one process, keyed by run id (spec 3.2 ยง7.3, ยง6 D-2).
///
/// `RunRecord` already carries the run's principal and whether its user was authenticated;
/// this holds only what the run store does not.
///
/// # Example
///
/// ```rust
/// use adk_kernel::{PartitionId, ProcessKind, ProcessMeta, QueueClass, Suspension};
///
/// let meta = ProcessMeta::new("run-9", QueueClass::Background, ProcessKind::Triggered { source: "file".into() })
///     .with_parent("run-1")
///     .with_suspension(Suspension::Freeze)
///     .with_label("repo", "adk-rust");
/// assert_eq!(meta.partition, PartitionId::default());
/// let json = serde_json::to_value(&meta).unwrap();
/// assert_eq!(json["suspendedBy"], "freeze");
/// assert_eq!(json["kind"]["triggered"]["source"], "file");
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ProcessMeta {
    /// The process id, equal to the run id.
    pub pid: String,
    /// The run that caused it: a proposal, a run-finished wake, a trigger.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,
    /// The partition it runs in.
    #[serde(default)]
    pub partition: PartitionId,
    /// Its priority class (spec 3.2 ยง7.9).
    pub queue: QueueClass,
    /// What kind of work it is.
    pub kind: ProcessKind,
    /// Its wake set, while `Waiting`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub wake: Option<WakeSet>,
    /// Why it is suspended, while parked by a suspension.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub suspended_by: Option<Suspension>,
    /// A suspension was requested and the run has not parked yet. Every replica's kernel
    /// mirrors pending requests and suspends the runs its own workers lease, so a process
    /// leased by another replica suspends too; the requester clears the flag once the run
    /// parks (spec 3.2 ยง7.3).
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub suspend_requested: bool,
    /// The agenda item it runs (spec 3.2 ยง7.5, phase 2).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agenda_item: Option<String>,
    /// Free-form labels.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub labels: BTreeMap<String, String>,
}

impl ProcessMeta {
    /// The side table of process `pid` in the default partition, with no parent, wake set,
    /// suspension, agenda item or labels.
    pub fn new(pid: impl Into<String>, queue: QueueClass, kind: ProcessKind) -> Self {
        Self {
            pid: pid.into(),
            parent: None,
            partition: PartitionId::default(),
            queue,
            kind,
            wake: None,
            suspended_by: None,
            suspend_requested: false,
            agenda_item: None,
            labels: BTreeMap::new(),
        }
    }

    /// Sets the run that caused the process.
    #[must_use]
    pub fn with_parent(mut self, parent: impl Into<String>) -> Self {
        self.parent = Some(parent.into());
        self
    }

    /// Sets the partition.
    #[must_use]
    pub fn with_partition(mut self, partition: impl Into<PartitionId>) -> Self {
        self.partition = partition.into();
        self
    }

    /// Sets the wake set.
    #[must_use]
    pub fn with_wake(mut self, wake: WakeSet) -> Self {
        self.wake = Some(wake);
        self
    }

    /// Sets why the process is suspended.
    #[must_use]
    pub fn with_suspension(mut self, suspension: Suspension) -> Self {
        self.suspended_by = Some(suspension);
        self
    }

    /// Sets the agenda item.
    #[must_use]
    pub fn with_agenda_item(mut self, item: impl Into<String>) -> Self {
        self.agenda_item = Some(item.into());
        self
    }

    /// Adds a label.
    #[must_use]
    pub fn with_label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.labels.insert(key.into(), value.into());
        self
    }
}

/// What kind of work a process is (spec 3.2 ยง7.3).
///
/// # Example
///
/// ```rust
/// use adk_kernel::ProcessKind;
///
/// assert_eq!(ProcessKind::Goal.to_string(), "goal");
/// assert_eq!(ProcessKind::Triggered { source: "cron".into() }.to_string(), "triggered:cron");
/// assert_eq!(serde_json::to_value(ProcessKind::Interactive).unwrap(), "interactive");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
#[non_exhaustive]
pub enum ProcessKind {
    /// A human turn.
    Interactive,
    /// A goal (spec 3.1 ยง7.11).
    Goal,
    /// A firing of a schedule.
    Scheduled,
    /// A run a wake source started.
    Triggered {
        /// The wake source's name.
        source: String,
    },
    /// A follow-up the agent proposed (spec 3.2 ยง7.5, phase 2).
    Proposed,
    /// The kernel's own work.
    Maintenance,
    /// A background sub-agent's run, started by the process its
    /// [`parent`](ProcessMeta::parent) names; its labels `subagent` and `subagent.name` carry
    /// the sub-agent's id and name.
    Subagent,
}

impl fmt::Display for ProcessKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Interactive => f.write_str("interactive"),
            Self::Goal => f.write_str("goal"),
            Self::Scheduled => f.write_str("scheduled"),
            Self::Triggered { source } => write!(f, "triggered:{source}"),
            Self::Proposed => f.write_str("proposed"),
            Self::Maintenance => f.write_str("maintenance"),
            Self::Subagent => f.write_str("subagent"),
        }
    }
}

/// Why a process is parked by a suspension rather than a wake set (spec 3.2 ยง7.3).
///
/// A suspended or freeze-parked process has no expiry of its own; `ps` shows its age
/// (ยง7.4).
///
/// # Example
///
/// ```rust
/// use adk_kernel::Suspension;
///
/// assert_eq!(serde_json::to_value(Suspension::Freeze).unwrap(), "freeze");
/// assert_eq!(Suspension::Preempted { by: "run-3".into() }.to_string(), "preempted by run-3");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
#[non_exhaustive]
pub enum Suspension {
    /// `adk-rust suspend` or [`ProcessTable::suspend`]; resumes on `resume`.
    Manual,
    /// An interactive run preempted it (spec 3.2 ยง7.9, phase 2); resumes when that run
    /// finishes.
    Preempted {
        /// The process that preempted it.
        by: String,
    },
    /// A freeze parked it (`ParkReason::Frozen`, ยง10 S3); resumes only on `unfreeze`
    /// (K-I21).
    Freeze,
    /// `Kernel::drain` parked it; resumes when the kernel starts again.
    Drain,
    /// A person paused the agent it runs (spec 3.3 ยง5.7); resumes only when the agent is
    /// resumed, never on `unfreeze` or a drain's end (K-I21).
    Paused {
        /// The paused agent's id.
        agent: String,
    },
}

impl fmt::Display for Suspension {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Manual => f.write_str("manual"),
            Self::Preempted { by } => write!(f, "preempted by {by}"),
            Self::Freeze => f.write_str("freeze"),
            Self::Drain => f.write_str("drain"),
            Self::Paused { agent } => write!(f, "{agent} paused"),
        }
    }
}

/// What [`ProcessTable::spawn`] records beside the run it enqueues (spec 3.2 ยง7.3).
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_kernel::{ProcessKind, ProcessSpec, Wake, WakeSet};
///
/// let spec = ProcessSpec::new(ProcessKind::Goal)
///     .with_partition("work")
///     .with_wake(WakeSet::any_of([Wake::FileChange {
///         root: ".".into(),
///         glob: "Cargo.lock".into(),
///         debounce: Duration::from_millis(500),
///     }]));
/// assert!(spec.wake.is_some());
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ProcessSpec {
    /// What kind of work it is.
    pub kind: ProcessKind,
    /// The run that caused it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent: Option<String>,
    /// The partition it runs in.
    #[serde(default)]
    pub partition: PartitionId,
    /// When set, the process starts asleep on this wake set: it parks `Waiting` before its
    /// first model call.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub wake: Option<WakeSet>,
    /// The agenda item it runs (phase 2).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agenda_item: Option<String>,
    /// Free-form labels.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub labels: BTreeMap<String, String>,
}

impl ProcessSpec {
    /// A spec of `kind` in the default partition that starts at once.
    pub fn new(kind: ProcessKind) -> Self {
        Self {
            kind,
            parent: None,
            partition: PartitionId::default(),
            wake: None,
            agenda_item: None,
            labels: BTreeMap::new(),
        }
    }

    /// Sets the run that caused the process.
    #[must_use]
    pub fn with_parent(mut self, parent: impl Into<String>) -> Self {
        self.parent = Some(parent.into());
        self
    }

    /// Sets the partition.
    #[must_use]
    pub fn with_partition(mut self, partition: impl Into<PartitionId>) -> Self {
        self.partition = partition.into();
        self
    }

    /// Starts the process asleep on `wake`.
    #[must_use]
    pub fn with_wake(mut self, wake: WakeSet) -> Self {
        self.wake = Some(wake);
        self
    }

    /// Sets the agenda item.
    #[must_use]
    pub fn with_agenda_item(mut self, item: impl Into<String>) -> Self {
        self.agenda_item = Some(item.into());
        self
    }

    /// Adds a label.
    #[must_use]
    pub fn with_label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.labels.insert(key.into(), value.into());
        self
    }
}

/// What a session's runs have used so far, as the hosted agent counts it (spec 3.2 ยง7.3,
/// ยง7.10): model calls, tool calls, cost in micro-USD, total tokens and the usage per
/// [`UsageUnit`](adk_core::UsageUnit) (`BudgetTracker::units`).
///
/// The counter is monotonic for the life of the agent: the kernel attributes the
/// difference between two readings to the process running in the session, so a process
/// sees only what it used.
///
/// # Example
///
/// ```rust
/// use adk_core::{UsageUnit, UsageVector};
/// use adk_kernel::RunUsage;
///
/// let usage = RunUsage::new(3, 2, 12_500)
///     .with_total_tokens(4_000)
///     .with_units(UsageVector::new().with(UsageUnit::AudioMillis, 90_000));
/// assert_eq!(usage.cost_micro_usd, 12_500);
/// assert_eq!(usage.units.get(UsageUnit::AudioMillis), 90_000);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RunUsage {
    /// Model calls.
    pub model_calls: u64,
    /// Tool calls.
    pub tool_calls: u64,
    /// Cost, in micro-USD.
    pub cost_micro_usd: u64,
    /// Input plus output tokens.
    #[serde(default)]
    pub total_tokens: u64,
    /// Usage per unit.
    #[serde(default)]
    pub units: adk_core::UsageVector,
}

impl RunUsage {
    /// Usage of `model_calls`, `tool_calls` and `cost_micro_usd`.
    pub fn new(model_calls: u64, tool_calls: u64, cost_micro_usd: u64) -> Self {
        Self { model_calls, tool_calls, cost_micro_usd, ..Self::default() }
    }

    /// Sets the total tokens.
    #[must_use]
    pub fn with_total_tokens(mut self, total_tokens: u64) -> Self {
        self.total_tokens = total_tokens;
        self
    }

    /// Sets the usage per unit.
    #[must_use]
    pub fn with_units(mut self, units: adk_core::UsageVector) -> Self {
        self.units = units;
        self
    }
}

/// Reports what only the hosted agent knows about a process: a goal's progress, the loop
/// guard's verdict and the session's usage (spec 3.2 ยง7.3, ยง10 HK3).
///
/// `adk-kernel` depends on no harness type (ยง6 D-1); the `kernel` feature of `adk-harness`
/// implements this over its goal store, loop guard and spend accounts and registers it with
/// [`KernelBuilder::progress_source`](crate::KernelBuilder::progress_source). Every method
/// has a default that reports nothing.
///
/// # Example
///
/// ```rust
/// use adk_core::RunRecord;
/// use adk_kernel::{GoalProgress, ProgressSource};
/// use async_trait::async_trait;
///
/// struct FixedProgress;
///
/// #[async_trait]
/// impl ProgressSource for FixedProgress {
///     async fn goal_progress(&self, _run: &RunRecord) -> adk_kernel::Result<Option<GoalProgress>> {
///         Ok(Some(GoalProgress::new(2, 10)))
///     }
/// }
/// ```
#[async_trait]
pub trait ProgressSource: Send + Sync {
    /// The progress of the goal `run` executes, or `None` when it runs no goal.
    ///
    /// # Errors
    ///
    /// Returns the source's store error; the view then omits the goal.
    async fn goal_progress(&self, _run: &RunRecord) -> Result<Option<GoalProgress>> {
        Ok(None)
    }

    /// The loop guard's last verdict for `run`, or `None` when it has none.
    ///
    /// # Errors
    ///
    /// Returns the source's store error; the view then omits the verdict.
    async fn loop_guard(&self, _run: &RunRecord) -> Result<Option<String>> {
        Ok(None)
    }

    /// The [`RunUsage`] counter of the session `run` continues, or `None` when the agent
    /// keeps none. The kernel reads it when a claim starts and on every scheduler tick, and
    /// attributes the cost between readings to the process ([`ProcessStore::add_spend`]).
    /// For a claim this returns `None` for, the kernel counts the usage the agent turn's
    /// events carry instead, so a plain `Runner` is accounted without a source.
    ///
    /// # Errors
    ///
    /// Returns the source's error; the kernel then attributes nothing for that reading.
    async fn usage(&self, _run: &RunRecord) -> Result<Option<RunUsage>> {
        Ok(None)
    }

    /// Called by [`Kernel::start`](crate::Kernel::start) with a handle on the kernel's
    /// processes, so a source that detects a condition that should park a process โ€” a goal
    /// that stopped making progress (spec 3.2 ยง7.3, ยง10 HK3) โ€” can suspend it at once.
    fn attach(&self, _processes: ProcessHandle) {}

    /// Called by [`Kernel::start`](crate::Kernel::start) with the kernel's ledger when it
    /// keeps one (spec 3.2 ยง7.10, feature `proc`), so the hosted runtime writes its own
    /// activity: the harness writes its tool events and audit records through it.
    #[cfg(feature = "proc")]
    fn attach_ledger(&self, _ledger: Arc<dyn crate::proc::LedgerSink>) {}
}

/// A handle on a running kernel's processes that does not keep the kernel alive, given to
/// a [`ProgressSource`] by [`ProgressSource::attach`] (spec 3.2 ยง7.3).
///
/// # Example
///
/// ```rust
/// use adk_kernel::{ProcessHandle, ProgressSource};
/// use std::sync::OnceLock;
///
/// #[derive(Default)]
/// struct Stalls(OnceLock<ProcessHandle>);
///
/// impl ProgressSource for Stalls {
///     fn attach(&self, processes: ProcessHandle) {
///         let _ = self.0.set(processes);
///     }
/// }
/// ```
#[derive(Clone)]
pub struct ProcessHandle {
    sup: std::sync::Weak<Supervision>,
}

impl fmt::Debug for ProcessHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ProcessHandle").field("alive", &(self.sup.strong_count() > 0)).finish()
    }
}

impl ProcessHandle {
    pub(crate) fn new(sup: &Arc<Supervision>) -> Self {
        Self { sup: Arc::downgrade(sup) }
    }

    /// Suspends `pid` as [`ProcessTable::suspend`] does: a running process parks at its
    /// next model-call boundary with [`Suspension::Manual`]. Returns `false` when the
    /// kernel is gone.
    ///
    /// # Errors
    ///
    /// Returns the errors of [`ProcessTable::suspend`].
    pub async fn suspend(&self, pid: &str) -> Result<bool> {
        let Some(sup) = self.sup.upgrade() else { return Ok(false) };
        ProcessTable::supervised(sup, None).suspend(pid).await?;
        Ok(true)
    }
}

/// The process table: a view over `DurableRunStore` plus the kernel's
/// [`ProcessStore`] (spec 3.2 ยง7.3).
///
/// [`Kernel::processes`](crate::Kernel::processes) returns the kernel's table, which also
/// lists runs of the kernel's queues the side table has no row for yet, reaches the
/// kernel's workers for `suspend`, and reads transcripts for the call counts; a table built
/// with [`new`](Self::new) reads and writes the stores only.
///
/// # Example
///
/// ```rust,no_run
/// use std::sync::Arc;
/// use adk_core::InMemoryRunStore;
/// use adk_kernel::{InMemoryProcessStore, ProcessFilter, ProcessTable};
///
/// # async fn example() -> adk_kernel::Result<()> {
/// let table = ProcessTable::new(Arc::new(InMemoryRunStore::new()), Arc::new(InMemoryProcessStore::new()));
/// for process in table.ps(ProcessFilter::default()).await? {
///     println!("{} {:?} {}", process.meta.pid, process.run.state, process.meta.kind);
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct ProcessTable {
    runs: Arc<dyn DurableRunStore>,
    processes: Arc<dyn ProcessStore>,
    spend: Option<Arc<dyn SpendLedger>>,
    progress: Option<Arc<dyn ProgressSource>>,
    sup: Option<Arc<Supervision>>,
}

impl fmt::Debug for ProcessTable {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ProcessTable")
            .field("spend", &self.spend.is_some())
            .field("progress", &self.progress.is_some())
            .field("supervised", &self.sup.is_some())
            .finish_non_exhaustive()
    }
}

fn not_found(pid: &str) -> KernelError {
    KernelError::NotFound { pid: pid.to_string() }
}

impl ProcessTable {
    /// A table over `runs` and `processes`.
    pub fn new(runs: Arc<dyn DurableRunStore>, processes: Arc<dyn ProcessStore>) -> Self {
        Self { runs, processes, spend: None, progress: None, sup: None }
    }

    /// The kernel's table: the stores, progress and workers of `sup`.
    pub(crate) fn supervised(sup: Arc<Supervision>, spend: Option<Arc<dyn SpendLedger>>) -> Self {
        Self {
            runs: Arc::clone(&sup.runs),
            processes: Arc::clone(&sup.processes),
            spend,
            progress: sup.progress.clone(),
            sup: Some(sup),
        }
    }

    /// The ledger the processes' spend is budgeted against. The view reads the kernel's
    /// own daily totals ([`ProcessStore::spend`]) rather than this ledger, which has no
    /// per-run dimension.
    #[must_use]
    pub fn with_spend_ledger(mut self, ledger: Arc<dyn SpendLedger>) -> Self {
        self.spend = Some(ledger);
        self
    }

    /// Reads goal progress, loop-guard verdicts and usage from `source` for each view.
    #[must_use]
    pub fn with_progress_source(mut self, source: Arc<dyn ProgressSource>) -> Self {
        self.progress = Some(source);
        self
    }

    /// The run store the table reads.
    pub fn run_store(&self) -> &Arc<dyn DurableRunStore> {
        &self.runs
    }

    /// The side table the table reads.
    pub fn process_store(&self) -> &Arc<dyn ProcessStore> {
        &self.processes
    }

    /// The kernel's clock for a kernel's table, the system clock otherwise.
    fn now(&self) -> DateTime<Utc> {
        self.sup.as_ref().map_or_else(Utc::now, |sup| sup.clock.now())
    }

    /// Lists the processes `filter` selects, each with its live [`ProcessView`]; `Waiting`
    /// processes show their age (spec 3.2 ยง7.3, ยง8.1 `adk-rust ps`). Processes are ordered
    /// by creation. A side-table row whose run the run store pruned is deleted. The
    /// kernel's `kernel.wake` runs, which route wake events, are not listed.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] or [`KernelError::Core`] when a store fails.
    pub async fn ps(&self, filter: ProcessFilter) -> Result<Vec<Process>> {
        let mut meta_filter = filter.clone();
        meta_filter.states = Vec::new();
        meta_filter.limit = None;
        let mut rows: BTreeMap<String, (RunRecord, ProcessMeta)> = BTreeMap::new();
        for meta in self.processes.list(&meta_filter).await? {
            match self.runs.get(&meta.pid).await? {
                Some(run) => {
                    rows.insert(meta.pid.clone(), (run, meta));
                }
                None => {
                    self.processes.delete(&meta.pid).await?;
                }
            }
        }
        if let Some(sup) = &self.sup {
            for queue in &sup.queues {
                let mut runs = RunFilter::default().queue(queue.as_str());
                if !filter.states.is_empty() {
                    runs = runs.states(filter.states.iter().copied());
                }
                for run in self.runs.list(runs).await? {
                    if rows.contains_key(&run.run_id) {
                        continue;
                    }
                    let meta = sup.inferred_meta(&run);
                    if filter.matches(&meta) {
                        rows.insert(run.run_id.clone(), (run, meta));
                    }
                }
            }
        }
        let mut rows: Vec<(RunRecord, ProcessMeta)> = rows
            .into_values()
            .filter(|(run, _)| run.target != crate::wake::KERNEL_WAKE_TARGET)
            .filter(|(run, _)| filter.states.is_empty() || filter.states.contains(&run.state))
            .filter(|(run, _)| filter.target.as_ref().is_none_or(|target| *target == run.target))
            .collect();
        rows.sort_by(|(a, _), (b, _)| {
            a.created_at.cmp(&b.created_at).then(a.run_id.cmp(&b.run_id))
        });
        if let Some(limit) = filter.limit {
            rows.truncate(limit);
        }
        let now = self.now();
        let mut processes = Vec::with_capacity(rows.len());
        for (run, meta) in rows {
            let view = self.view(&run, &meta, now).await;
            processes.push(Process { run, meta, view });
        }
        Ok(processes)
    }

    /// The process `pid` with its live view, or `None` when no run has that id.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] or [`KernelError::Core`] when a store fails.
    pub async fn get(&self, pid: &str) -> Result<Option<Process>> {
        let Some(run) = self.runs.get(pid).await? else {
            return Ok(None);
        };
        let meta = self.meta(&run).await?;
        let view = self.view(&run, &meta, self.now()).await;
        Ok(Some(Process { run, meta, view }))
    }

    /// Enqueues `run` and records its process (spec 3.2 ยง7.3, ยง7.4). The process's queue is
    /// the run's queue. With [`ProcessSpec::wake`] the process starts asleep: it parks
    /// `Waiting` on its wake set before its first model call and holds no lease (K-I2).
    ///
    /// The run's idempotency key is prefixed `kernel:asleep:` for a sleeper and
    /// `kernel:spawn:` otherwise, before the caller's key or a generated one: the prefix
    /// carries the sleep intent to the worker's claim, so a sleeper never depends on its
    /// side-table row being written first. A key already held in the queue returns the
    /// existing process.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Frozen`] while the kernel is frozen, [`KernelError::Config`]
    /// when `run`'s target is a partition agent of a partition other than `spec.partition`,
    /// or `run` continues a session another partition's agent ran (spec 3.2 ยง7.8, K-I15),
    /// [`KernelError::Quota`] when the partition already queues its `max_queued` processes
    /// (spec 3.2 ยง7.9), the run store's error when `run` is invalid or its concurrency key
    /// refuses it, or a store error. A spawn of a partition agent's target in the default
    /// partition runs in the target's partition.
    pub async fn spawn(&self, mut run: NewRun, mut spec: ProcessSpec) -> Result<Process> {
        self.refuse_while_frozen()?;
        if let Some(sup) = &self.sup {
            // A paused or removed agent takes no work; an added agent runs only in its area
            // (spec 3.3 ยง5.7, K-I15).
            if let Some(agent) = sup.targets.admit_spawn(&run.target).await? {
                // A replica that has not built it yet hands its claim back rather than
                // failing it (spec 3.3 ยง10 S4).
                sup.targets.expect(&agent.id);
                if spec.partition.as_str() == PartitionId::DEFAULT {
                    spec.partition = agent.area;
                } else if spec.partition != agent.area {
                    return Err(KernelError::Config {
                        reason: format!(
                            "{} works in area `{}`, so a process of `{}` cannot run it \
                             (spec 3.2 K-I15)",
                            agent.display_name, agent.area, spec.partition
                        ),
                    });
                }
            }
            // A partition agent's target runs only its own partition (spec 3.2 ยง7.8, K-I15).
            spec.partition = sup.partition_for(&run.target, spec.partition)?;
            sup.check_session_partition(&run, &spec.partition).await?;
            if let Some(max) = sup.quotas_of(&spec.partition).await.max_queued {
                let queued = sup.queued_in(&spec.partition).await;
                if queued >= max {
                    return Err(KernelError::Quota {
                        partition: spec.partition.to_string(),
                        reason: format!(
                            "{queued} processes queued, the most max_queued ({max}) allows"
                        ),
                    });
                }
            }
        }
        let asleep = spec.wake.is_some();
        // The key carries the sleep intent to the claim, so a sleeper parks before its first
        // model call however long its row below takes to write; the caller's key follows
        // the prefix, so a repeated spawn still finds its run.
        let prefix = if asleep { ASLEEP_KEY_PREFIX } else { SPAWN_KEY_PREFIX };
        let key = run.idempotency_key.take().unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
        run.idempotency_key = Some(format!("{prefix}{key}"));
        let record = match self.runs.enqueue(run).await? {
            Enqueued::Created(record) => record,
            Enqueued::Existing(record) => {
                let meta = self.meta(&record).await?;
                let view = self.view(&record, &meta, self.now()).await;
                return Ok(Process { run: record, meta, view });
            }
            Enqueued::Rejected { blocking_run_id } => {
                return Err(AdkError::new(
                    ErrorComponent::Server,
                    ErrorCategory::Conflict,
                    crate::error::SPAWN_REJECTED_CODE,
                    format!(
                        "run {blocking_run_id} holds the concurrency key and the run refuses to \
                         queue behind it; wait for it to finish or enqueue with OnConflict::Enqueue"
                    ),
                )
                .into());
            }
        };
        let mut labels = spec.labels;
        // A cron sleeper recurs: its successor sleeps on the same set (spec 3.2 ยง8.1).
        if let Some(wake) = &spec.wake
            && wake.any_of.iter().any(|wake| matches!(wake, crate::wake::Wake::Cron { .. }))
        {
            labels.insert(crate::supervise::RECUR_LABEL.to_string(), serde_json::to_string(wake)?);
        }
        let meta = ProcessMeta {
            pid: record.run_id.clone(),
            parent: spec.parent,
            partition: spec.partition,
            queue: QueueClass::from_name(&record.queue),
            kind: spec.kind,
            wake: spec.wake,
            suspended_by: None,
            suspend_requested: asleep,
            agenda_item: spec.agenda_item,
            labels,
        };
        self.processes.put(meta.clone()).await?;
        if let Some(sup) = &self.sup {
            if asleep {
                sup.roots_changed.notify_one();
            }
            sup.publish(KernelEvent::Process {
                pid: meta.pid.clone(),
                from: RunState::Queued,
                to: RunState::Queued,
                reason: Some(if asleep { "spawned asleep" } else { "spawned" }.to_string()),
            });
            sup.record(
                audit::PROCESS_SPAWNED,
                sup.actor_for(Some(&record)),
                Supervision::process_fields(&meta, None, None),
            )
            .await;
            sup.nudge();
        }
        let view = self.view(&record, &meta, self.now()).await;
        Ok(Process { run: record, meta, view })
    }

    /// Kills `pid` through `DurableRunStore::request_cancel`: cooperative at the next
    /// checkpoint, never leaving a lease held (K-I5); a `Queued` or `Waiting` process,
    /// freeze-parked ones included, is `Cancelled` at once (K-I21).
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::NotFound`] for an unknown `pid`, [`KernelError::InvalidState`]
    /// for a finished one, or a store error.
    pub async fn kill(&self, pid: &str, reason: &str) -> Result<()> {
        let run = self.runs.get(pid).await?.ok_or_else(|| not_found(pid))?;
        if run.state.is_terminal() {
            return Err(KernelError::InvalidState {
                pid: pid.to_string(),
                state: run.state,
                action: "killed",
            });
        }
        self.runs.request_cancel(pid).await?;
        let meta = self.meta(&run).await?;
        if run.state != RunState::Running {
            // Cancelled at once, so it will not sleep again; a running one clears its wake
            // set when its attempt finishes.
            let mut cleared = meta.clone();
            if cleared.wake.take().is_some() {
                self.processes.put(cleared).await?;
                if let Some(sup) = &self.sup {
                    sup.roots_changed.notify_one();
                }
            }
        }
        if let Some(sup) = &self.sup {
            sup.heartbeat_now();
            if run.state != RunState::Running {
                sup.publish(KernelEvent::Process {
                    pid: pid.to_string(),
                    from: run.state,
                    to: RunState::Cancelled,
                    reason: Some(reason.to_string()),
                });
            }
            sup.record(
                audit::PROCESS_KILLED,
                sup.actor_for(Some(&run)),
                Supervision::process_fields(&meta, Some(reason), None),
            )
            .await;
        }
        Ok(())
    }

    /// Moves a `Queued` run or a sleeping process (`Waiting` with a wake set) to `queue`
    /// (spec 3.2 ยง6 D-5) and returns the moved process.
    ///
    /// The run store has no queue change, so `nice` cancels the run and enqueues it again on
    /// `queue`, in the same session, with the same wake set: **the pid changes**. The new
    /// process's [`ProcessMeta::parent`] is the old pid. A sleeper whose turn already reached
    /// the session continues it with an empty user turn, as a resumed run does, so its
    /// prompt is not sent twice.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::NotFound`] for an unknown `pid`; [`KernelError::InvalidState`]
    /// for a `Running` process (suspend it first), a suspended or approval-parked one, or a
    /// finished one; or a store error.
    pub async fn nice(&self, pid: &str, queue: QueueClass) -> Result<Process> {
        let run = self.runs.get(pid).await?.ok_or_else(|| not_found(pid))?;
        let meta = self.meta(&run).await?;
        let movable = match run.state {
            RunState::Queued => true,
            RunState::Waiting => meta.wake.is_some() && meta.suspended_by.is_none(),
            _ => false,
        };
        if !movable {
            return Err(KernelError::InvalidState {
                pid: pid.to_string(),
                state: run.state,
                action: "moved to another queue (only a queued or sleeping process moves; \
                         suspend a running one first)",
            });
        }
        if meta.queue == queue {
            let view = self.view(&run, &meta, self.now()).await;
            return Ok(Process { run, meta, view });
        }
        // Checked before the cancel, so a freeze never leaves the process cancelled and not
        // re-enqueued.
        self.refuse_while_frozen()?;
        self.runs.request_cancel(pid).await?;
        if meta.wake.is_some() {
            let mut cleared = meta.clone();
            cleared.wake = None;
            self.processes.put(cleared).await?;
        }
        let input = if run.checkpoint.is_some() {
            serde_json::Value::String(String::new())
        } else {
            run.input.clone()
        };
        let mut next = NewRun::new(
            queue.as_str(),
            run.app_name.clone(),
            run.user_id.clone(),
            run.target.clone(),
            input,
        )
        .with_max_attempts(run.max_attempts);
        next.session_id = Some(crate::supervise::run_session(&run));
        if let Some(key) = &run.concurrency_key {
            next = next.with_concurrency_key(key.clone(), run.on_conflict);
        }
        next.not_before = run.not_before;
        next.principal = run.principal.clone();
        next.authenticated = run.authenticated;
        let mut spec = ProcessSpec::new(meta.kind.clone()).with_parent(pid);
        spec.partition = meta.partition.clone();
        spec.wake = meta.wake.clone();
        spec.agenda_item = meta.agenda_item.clone();
        spec.labels = meta.labels.clone();
        let moved = self.spawn(next, spec).await?;
        if let Some(sup) = &self.sup {
            sup.record(
                audit::PROCESS_REQUEUED,
                sup.actor_for(Some(&run)),
                json!({
                    "pid": moved.meta.pid,
                    "parent": pid,
                    "partition": moved.meta.partition,
                    "queue": queue,
                    "kind": moved.meta.kind,
                    "from": meta.queue,
                }),
            )
            .await;
        }
        Ok(moved)
    }

    /// Suspends `pid` through the worker's suspend path (ยง10 S1): it stops at its next
    /// model-call boundary and parks `Waiting` with [`Suspension::Manual`]; a `Queued` or
    /// `Waiting` process is marked suspended where it is.
    ///
    /// A `Running` process gets [`ProcessMeta::suspend_requested`]: every replica's kernel
    /// mirrors pending requests every 250 ms and suspends the runs its own workers lease, so
    /// the request reaches the replica that holds the lease without a run-store change; the
    /// flag is cleared once the run parks.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::NotFound`] for an unknown `pid`, [`KernelError::InvalidState`]
    /// for a finished one, or a store error.
    pub async fn suspend(&self, pid: &str) -> Result<()> {
        self.suspend_marked(pid, |by| {
            by.get_or_insert(Suspension::Manual);
        })
        .await
    }

    /// Suspends `pid` as [`suspend`](Self::suspend) does, marked as `suspension` unless a
    /// person suspended it by hand: a paused agent's processes are marked
    /// [`Suspension::Paused`], so only its resume wakes them (spec 3.3 ยง5.7, K-I21).
    pub(crate) async fn suspend_as(&self, pid: &str, suspension: Suspension) -> Result<()> {
        self.suspend_marked(pid, |by| {
            if *by != Some(Suspension::Manual) {
                *by = Some(suspension);
            }
        })
        .await
    }

    async fn suspend_marked(
        &self,
        pid: &str,
        mark: impl FnOnce(&mut Option<Suspension>),
    ) -> Result<()> {
        let run = self.runs.get(pid).await?.ok_or_else(|| not_found(pid))?;
        if run.state.is_terminal() {
            return Err(KernelError::InvalidState {
                pid: pid.to_string(),
                state: run.state,
                action: "suspended",
            });
        }
        let mut meta = self.meta(&run).await?;
        mark(&mut meta.suspended_by);
        // A running or queued process parks at its next model-call boundary; a waiting
        // one is already parked.
        meta.suspend_requested = run.state != RunState::Waiting;
        self.processes.put(meta.clone()).await?;
        match (run.state, &self.sup) {
            (RunState::Running, Some(sup)) => {
                sup.suspend_local(pid);
            }
            (RunState::Waiting, Some(sup)) => {
                sup.record(
                    audit::PROCESS_SUSPENDED,
                    sup.actor_for(Some(&run)),
                    Supervision::process_fields(&meta, Some("suspended"), None),
                )
                .await;
            }
            _ => {}
        }
        Ok(())
    }

    /// Resumes a suspended or sleeping `pid` through `DurableRunStore::wake`. A process a
    /// freeze parked resumes only on `unfreeze` (K-I21).
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::NotFound`] for an unknown `pid`, [`KernelError::InvalidState`]
    /// when it is not `Waiting` or was parked by a freeze, or a store error.
    pub async fn resume(&self, pid: &str) -> Result<()> {
        let run = self.runs.get(pid).await?.ok_or_else(|| not_found(pid))?;
        let mut meta = self.meta(&run).await?;
        let queued_and_suspended = run.state == RunState::Queued && meta.suspend_requested;
        if run.state != RunState::Waiting && !queued_and_suspended {
            return Err(KernelError::InvalidState {
                pid: pid.to_string(),
                state: run.state,
                action: "resumed (it is not suspended or asleep)",
            });
        }
        if meta.suspended_by == Some(Suspension::Freeze) {
            return Err(KernelError::InvalidState {
                pid: pid.to_string(),
                state: run.state,
                action: "resumed before the freeze is lifted; run `adk-rust unfreeze`",
            });
        }
        meta.suspended_by = None;
        meta.suspend_requested = false;
        meta.wake = None;
        // Written before the wake, so the claim that follows reads no suspension to honour.
        self.processes.put(meta.clone()).await?;
        if run.state == RunState::Waiting {
            self.runs.wake(pid).await?;
        }
        if let Some(sup) = &self.sup {
            sup.record(
                audit::PROCESS_RESUMED,
                sup.actor_for(Some(&run)),
                Supervision::process_fields(&meta, Some("resumed"), None),
            )
            .await;
            sup.nudge();
        }
        Ok(())
    }

    /// [`KernelError::Frozen`] while the kernel's control refuses work.
    fn refuse_while_frozen(&self) -> Result<()> {
        match &self.sup {
            Some(sup) => sup.governance.check().map_err(|error| KernelError::Frozen {
                reason: sup.governance.reason().unwrap_or(error.message),
            }),
            None => Ok(()),
        }
    }

    /// The stored side table of `run`, or the inferred one when nothing recorded it.
    async fn meta(&self, run: &RunRecord) -> Result<ProcessMeta> {
        let inferred = || match &self.sup {
            Some(sup) => sup.inferred_meta(run),
            None => Supervision::infer_meta(run),
        };
        Ok(self.processes.get(&run.run_id).await?.unwrap_or_else(inferred))
    }

    /// The live view of one process (spec 3.2 ยง7.3, ยง7.10). A part a store cannot answer is
    /// left at its default rather than failing the listing.
    async fn view(&self, run: &RunRecord, meta: &ProcessMeta, now: DateTime<Utc>) -> ProcessView {
        let mut view = ProcessView::default();
        let local = self.sup.as_ref().and_then(|sup| {
            sup.local.lock().unwrap_or_else(|e| e.into_inner()).get(&run.run_id).cloned()
        });
        let since = match (run.state, &local) {
            (RunState::Running, Some(local)) => local.claimed_at,
            _ => run.updated_at,
        };
        view.age = (now - since).to_std().unwrap_or_default();
        let today = now.date_naive();
        let scope = SpendScope::Process(run.run_id.clone());
        match self.processes.spend(&scope, today, today).await {
            Ok(spend) => view.spend_today = spend,
            Err(error) => {
                tracing::debug!(process.pid = %run.run_id, error = %error, "could not read today's spend")
            }
        }
        match self.processes.spend(&scope, today - chrono::Days::new(6), today).await {
            Ok(spend) => view.spend_7d = spend,
            Err(error) => {
                tracing::debug!(process.pid = %run.run_id, error = %error, "could not read the week's spend")
            }
        }
        if run.state == RunState::Running
            && let Some(local) = &local
            && let (Some((first_at, first)), Some((last_at, last))) =
                (local.samples.front(), local.samples.back())
        {
            let minutes = (*last_at - *first_at).num_milliseconds() as f64 / 60_000.0;
            if minutes > 0.0 {
                let rate = (last.saturating_sub(*first)) as f64 / minutes;
                view.burn_rate = Some(rate);
                let end_of_day =
                    (today + chrono::Days::new(1)).and_time(chrono::NaiveTime::MIN).and_utc();
                let remaining = (end_of_day - now).num_milliseconds().max(0) as f64 / 60_000.0;
                view.projection = Some(view.spend_today + (rate * remaining) as u64);
            }
        }
        let sessions = self.sup.as_ref().and_then(|sup| sup.sessions.clone());
        if let Some(sessions) = sessions
            && let Some(counts) = transcript_counts(sessions.as_ref(), run).await
        {
            view.model_calls = counts.model_calls;
            view.tool_calls = counts.tool_calls;
        } else if let Some(local) = &local
            && let Some(last) = &local.last
        {
            let base = local.baseline.clone().unwrap_or_default();
            view.model_calls = last.model_calls.saturating_sub(base.model_calls);
            view.tool_calls = last.tool_calls.saturating_sub(base.tool_calls);
        } else if let Some(local) = &local {
            view.model_calls = local.turn.model_calls;
            view.tool_calls = local.turn.tool_calls;
        }
        // A partition's own source answers for its processes (spec 3.2 ยง7.8).
        let progress = self
            .sup
            .as_ref()
            .and_then(|sup| {
                sup.targets
                    .progress_of(&run.target)
                    .or_else(|| sup.partition_progress.get(&meta.partition).cloned())
            })
            .or_else(|| self.progress.clone());
        if let Some(progress) = progress {
            match progress.loop_guard(run).await {
                Ok(verdict) => view.loop_guard = verdict,
                Err(error) => {
                    tracing::debug!(process.pid = %run.run_id, error = %error, "could not read the loop guard")
                }
            }
            if meta.kind == ProcessKind::Goal || run.target == crate::supervise::GOAL_TARGET {
                match progress.goal_progress(run).await {
                    Ok(Some(mut goal)) => {
                        if goal.spend == 0 {
                            let from = run.created_at.date_naive();
                            goal.spend =
                                self.processes.spend(&scope, from, today).await.unwrap_or(0);
                        }
                        view.goal = Some(goal);
                    }
                    Ok(None) => {}
                    Err(error) => {
                        tracing::debug!(process.pid = %run.run_id, error = %error, "could not read the goal's progress")
                    }
                }
            }
        }
        view
    }
}