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
//! Partitions (spec 3.2 §7.8): areas of a user's life or an organisation's teams kept
//! apart, with work across them only through the coordinator and under approval.
//!
//! A [`Partition`] is a policy class plus a memory scope plus a driver set, run by its own
//! `HarnessAgent` built with the partition's mode (the `kernel` feature of `adk-harness`):
//!
//! | Part | Mechanism |
//! |---|---|
//! | Policy class | [`Partition::class`] and [`Partition::tenant`] on the `AgentPrincipal` of every agent in it, through the runner's identity source ([`Partition::identity_source`]), so `PolicyRule::for_class` rules apply |
//! | Memory | The partition's [`memory_scope`](Partition::memory_scope) is the project of its long-term facts (`FactScope.project`) and of project-scoped memory ([`Partition::scope_memory`]) |
//! | Drivers | [`Partition::drivers`]: the driver ids mounted for the partition |
//! | Crossings | [`PartitionPolicy`]: a request that reaches another partition is denied unless the coordinator makes it, then `RequireApproval`, non-waivable, audited as `kernel.partition.crossing` (K-I15) |
//!
//! The approval's unit is the crossing fingerprint within one run (§13 Q3): an approved
//! read lets the coordinator's identical reads through for the rest of that run and nothing
//! beyond it; writes stay one-shot. [`Partitions`] keeps the partition table and the
//! crossing ledger in a [`PartitionStore`].

use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt;
use std::str::FromStr;
use std::sync::{Arc, Mutex, OnceLock};

use adk_core::audit_log::{AuditActor, AuditRecordDraft, AuditRecordKind, AuditTrail};
use adk_core::{
    AdkError, AgentPrincipal, Clock, ErrorCategory, ErrorComponent, IdentitySource, Memory,
    MemoryEntry, SystemClock,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::audit;
use crate::error::{KernelError, MEMORY_CROSSING_CODE, MEMORY_DELETE_CODE, NOT_FOUND_CODE, Result};
use crate::sched::{QuietHours, Quotas};

mod crossing;

pub use crossing::{
    CROSSING_PARTITION_ARG, Crossing, CrossingAccess, CrossingFilter, CrossingOutcome,
    CrossingRecord, CrossingResource, CrossingRoute, CrossingRun, CrossingTool, PartitionPolicy,
    RecurringCrossing,
};

/// Names a partition: `work`, `home`, a team (spec 3.2 §7.8).
///
/// # Example
///
/// ```rust
/// use adk_kernel::PartitionId;
///
/// let work = PartitionId::new("work");
/// assert_eq!(work.as_str(), "work");
/// assert_eq!(PartitionId::default().as_str(), PartitionId::DEFAULT);
/// assert_eq!(serde_json::to_value(&work).unwrap(), "work");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PartitionId(String);

impl PartitionId {
    /// The partition a process runs in when none is named.
    pub const DEFAULT: &'static str = "default";

    /// A partition id.
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// The id as text.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Default for PartitionId {
    fn default() -> Self {
        Self::new(Self::DEFAULT)
    }
}

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

impl AsRef<str> for PartitionId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<&str> for PartitionId {
    fn from(id: &str) -> Self {
        Self::new(id)
    }
}

impl From<String> for PartitionId {
    fn from(id: String) -> Self {
        Self(id)
    }
}

/// A partition's default mode (spec 3.2 §7.8): what its agents may do without asking.
///
/// The `kernel` feature of `adk-harness` maps it one to one onto `HarnessMode` (§10 HK1),
/// and the values serialize as `HarnessMode`'s do (`read_only`, `plan`, `ask`, `auto`).
/// Agentrix's Observe / Suggest / Automate is ReadOnly / Ask / Auto (§4).
///
/// # Example
///
/// ```rust
/// use adk_kernel::PartitionMode;
///
/// let mode: PartitionMode = "read_only".parse().unwrap();
/// assert_eq!(mode, PartitionMode::ReadOnly);
/// assert_eq!(mode.to_string(), "read_only");
/// assert_eq!(serde_json::to_value(PartitionMode::Auto).unwrap(), "auto");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PartitionMode {
    /// Reads and todos only.
    ReadOnly,
    /// Reads and planning; nothing that changes the workspace.
    Plan,
    /// Changes need approval unless a standing rule allows them.
    Ask,
    /// Changes run without approval, within the harness's own limits.
    Auto,
}

impl PartitionMode {
    /// The mode's name: `read_only`, `plan`, `ask` or `auto`.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::ReadOnly => "read_only",
            Self::Plan => "plan",
            Self::Ask => "ask",
            Self::Auto => "auto",
        }
    }
}

impl fmt::Display for PartitionMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // `pad`, so a width or alignment in the format string applies.
        f.pad(self.as_str())
    }
}

impl FromStr for PartitionMode {
    type Err = KernelError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        match text.trim().to_ascii_lowercase().replace('-', "_").as_str() {
            "read_only" | "readonly" => Ok(Self::ReadOnly),
            "plan" => Ok(Self::Plan),
            "ask" => Ok(Self::Ask),
            "auto" => Ok(Self::Auto),
            other => Err(KernelError::Config {
                reason: format!(
                    "unknown partition mode `{other}`; use read_only, plan, ask or auto"
                ),
            }),
        }
    }
}

/// Whether a partition takes part in crossings (spec 3.2 §7.8).
///
/// A crossing needs both partitions to allow it: a partition set to [`Deny`](Self::Deny)
/// is isolated both ways, even from the coordinator. Agentrix's Work and Home worlds are two
/// partitions with [`CoordinatorWithApproval`](Self::CoordinatorWithApproval) (§4).
///
/// # Example
///
/// ```rust
/// use adk_kernel::CrossPartition;
///
/// assert_eq!(CrossPartition::default(), CrossPartition::Deny);
/// assert_eq!(
///     serde_json::to_value(CrossPartition::CoordinatorWithApproval).unwrap(),
///     "coordinator_with_approval"
/// );
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CrossPartition {
    /// No request crosses into or out of the partition.
    #[default]
    Deny,
    /// The coordinator may cross, each crossing under a non-waivable approval (K-I15).
    CoordinatorWithApproval,
}

impl CrossPartition {
    /// The value's name: `deny` or `coordinator_with_approval`.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Deny => "deny",
            Self::CoordinatorWithApproval => "coordinator_with_approval",
        }
    }
}

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

impl FromStr for CrossPartition {
    type Err = KernelError;

    fn from_str(text: &str) -> Result<Self> {
        match text.trim().to_ascii_lowercase().replace('-', "_").as_str() {
            "deny" => Ok(Self::Deny),
            "coordinator_with_approval" | "coordinator" => Ok(Self::CoordinatorWithApproval),
            other => Err(KernelError::Config {
                reason: format!(
                    "unknown crossing setting `{other}`; use deny or coordinator_with_approval"
                ),
            }),
        }
    }
}

fn ask() -> PartitionMode {
    PartitionMode::Ask
}

fn unlimited(quotas: &Quotas) -> bool {
    *quotas == Quotas::default()
}

/// A partition (spec 3.2 §7.8): a policy class plus a memory scope plus a driver set, run by
/// its own `HarnessAgent` built with [`default_mode`](Self::default_mode).
///
/// Its [`quotas`](Self::quotas) and [`quiet_hours`](Self::quiet_hours) apply beside the
/// kernel's (spec 3.2 §7.9): each quota is the lower of the two, and every quiet-hours window
/// holds.
///
/// # Example
///
/// ```rust
/// use adk_kernel::{CrossPartition, Partition, PartitionMode, QuietHours, Quotas};
///
/// let work = Partition::new("work", "work")
///     .with_mode(PartitionMode::Auto)
///     .with_driver("calendar")
///     .with_quotas(Quotas::new().with_max_running(2))
///     .with_quiet_hours("19:00-08:00".parse::<QuietHours>().unwrap())
///     .with_cross_partition(CrossPartition::CoordinatorWithApproval);
/// assert_eq!(work.memory_scope, "work");
/// assert_eq!(work.drivers, ["calendar"]);
/// let wire = serde_json::to_value(&work).unwrap();
/// assert_eq!(wire["defaultMode"], "auto");
/// assert_eq!(wire["quotas"]["maxRunning"], 2);
/// assert_eq!(wire["quietHours"]["start"], "19:00");
/// assert_eq!(wire["crossPartition"], "coordinator_with_approval");
/// assert_eq!(serde_json::from_value::<Partition>(wire).unwrap(), work);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Partition {
    /// The partition's id: `work`, `home`, a team.
    pub id: PartitionId,
    /// `AgentPrincipal.class` of every agent in the partition.
    pub class: String,
    /// `AgentPrincipal.tenant` of every agent in the partition, on multi-tenant hosts.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tenant: Option<String>,
    /// The mode of the partition's `HarnessAgent`; the harness maps it onto `HarnessMode`.
    /// Defaults to `ask`.
    #[serde(default = "ask")]
    pub default_mode: PartitionMode,
    /// The project of the partition's long-term facts (spec 3.1 §7.15.2) and of its
    /// project-scoped memory. Defaults to the id.
    pub memory_scope: String,
    /// The ids of the drivers mounted for the partition (spec 3.2 §7.7). A driver reaches
    /// the partition's agents only when it is mounted, its manifest lists the partition
    /// and its id is here.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub drivers: Vec<String>,
    /// The partition's own quotas (spec 3.2 §7.9): each applies beside the kernel's, the
    /// lower of the two. Defaults to none.
    #[serde(default, skip_serializing_if = "unlimited")]
    pub quotas: Quotas,
    /// The partition's own quiet hours, which hold beside the kernel's: no proactive or
    /// maintenance process of the partition starts inside them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quiet_hours: Option<QuietHours>,
    /// Whether the partition takes part in crossings. Defaults to [`CrossPartition::Deny`].
    #[serde(default)]
    pub cross_partition: CrossPartition,
    /// Partitions the coordinator in this partition may no longer cross into: the prompt's
    /// "deny and block crossings to *X* from this partition".
    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
    pub blocked_crossings: BTreeSet<PartitionId>,
}

impl Partition {
    /// The principal attribute naming the partition of an agent
    /// ([`principal`](Self::principal)).
    pub const PRINCIPAL_ATTRIBUTE: &'static str = "adk.partition";

    /// A partition `id` of class `class` in `ask` mode, its memory scope its id, with no
    /// drivers and no crossings.
    pub fn new(id: impl Into<PartitionId>, class: impl Into<String>) -> Self {
        let id = id.into();
        Self {
            memory_scope: id.as_str().to_string(),
            id,
            class: class.into(),
            tenant: None,
            default_mode: PartitionMode::Ask,
            drivers: Vec::new(),
            quotas: Quotas::default(),
            quiet_hours: None,
            cross_partition: CrossPartition::Deny,
            blocked_crossings: BTreeSet::new(),
        }
    }

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

    /// Sets the mode.
    #[must_use]
    pub fn with_mode(mut self, mode: PartitionMode) -> Self {
        self.default_mode = mode;
        self
    }

    /// Sets the memory scope.
    #[must_use]
    pub fn with_memory_scope(mut self, scope: impl Into<String>) -> Self {
        self.memory_scope = scope.into();
        self
    }

    /// Adds a driver id.
    #[must_use]
    pub fn with_driver(mut self, driver: impl Into<String>) -> Self {
        self.drivers.push(driver.into());
        self
    }

    /// Sets the partition's own quotas.
    #[must_use]
    pub fn with_quotas(mut self, quotas: Quotas) -> Self {
        self.quotas = quotas;
        self
    }

    /// Sets the partition's own quiet hours.
    #[must_use]
    pub fn with_quiet_hours(mut self, quiet_hours: QuietHours) -> Self {
        self.quiet_hours = Some(quiet_hours);
        self
    }

    /// Sets whether the partition takes part in crossings.
    #[must_use]
    pub fn with_cross_partition(mut self, cross_partition: CrossPartition) -> Self {
        self.cross_partition = cross_partition;
        self
    }

    /// Blocks crossings from this partition into `to`.
    #[must_use]
    pub fn with_blocked_crossing(mut self, to: impl Into<PartitionId>) -> Self {
        self.blocked_crossings.insert(to.into());
        self
    }

    /// Checks the record: a non-empty id, class and memory scope, none with control
    /// characters.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Config`] naming the field.
    pub fn validate(&self) -> Result<()> {
        let fields = [
            ("id", self.id.as_str()),
            ("class", self.class.as_str()),
            ("memoryScope", self.memory_scope.as_str()),
        ];
        for (field, value) in fields {
            if value.trim().is_empty() || value.chars().any(char::is_control) {
                return Err(KernelError::Config {
                    reason: format!(
                        "partition `{}`: `{field}` must be non-empty text without control \
                         characters",
                        self.id
                    ),
                });
            }
        }
        Ok(())
    }

    /// The principal the agent `agent_name` of `app_name` acts as in this partition:
    /// `urn:adk:agent:{app}/{agent}` with the partition's class and tenant, and the
    /// partition's id as the [`PRINCIPAL_ATTRIBUTE`](Self::PRINCIPAL_ATTRIBUTE) attribute.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_kernel::Partition;
    ///
    /// let principal = Partition::new("work", "work-agents").with_tenant("acme").principal("ops", "harness");
    /// assert_eq!(principal.id.as_str(), "urn:adk:agent:ops/harness");
    /// assert_eq!(principal.class.as_deref(), Some("work-agents"));
    /// assert_eq!(principal.tenant.as_deref(), Some("acme"));
    /// assert_eq!(principal.attributes[Partition::PRINCIPAL_ATTRIBUTE], "work");
    /// ```
    pub fn principal(&self, app_name: &str, agent_name: &str) -> AgentPrincipal {
        let mut principal = AgentPrincipal::derived(app_name, agent_name)
            .with_class(self.class.clone())
            .with_attribute(Self::PRINCIPAL_ATTRIBUTE, self.id.as_str());
        if let Some(tenant) = &self.tenant {
            principal = principal.with_tenant(tenant.clone());
        }
        principal
    }

    /// The runner's identity source for the partition's harness (spec 3.1 §7.14): every
    /// agent of the runner whose root is `root_agent` acts as its
    /// [`principal`](Self::principal) in this partition, so its class and tenant reach every
    /// `ToolPolicyRequest`, `ToolConfirmationRequest`, `SpendKey` and audit record.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::IdentitySource;
    /// use adk_kernel::Partition;
    ///
    /// # futures::executor::block_on(async {
    /// let source = Partition::new("home", "home").identity_source("adk-rust", "harness");
    /// assert_eq!(source.current().await.unwrap().class.as_deref(), Some("home"));
    /// let member = source.principal_for("reviewer").await.unwrap().unwrap();
    /// assert_eq!(member.id.as_str(), "urn:adk:agent:adk-rust/reviewer");
    /// assert_eq!(member.class.as_deref(), Some("home"));
    /// # });
    /// ```
    pub fn identity_source(&self, app_name: &str, root_agent: &str) -> Arc<dyn IdentitySource> {
        Arc::new(PartitionIdentity {
            partition: self.clone(),
            app_name: app_name.to_string(),
            root_agent: root_agent.to_string(),
        })
    }

    /// `memory` confined to the partition's [`memory_scope`](Self::memory_scope): searches
    /// and writes go through the memory's project scoping, and a search or write naming
    /// another project is refused (K-I15).
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Config`] when `memory` does not keep projects apart
    /// (`Memory::supports_project_scoping` is `false`): a memory without project scoping
    /// would show every partition's entries to every other.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let memory = work.scope_memory(Arc::new(adk_memory::SqliteMemoryService::new(url).await?))?;
    /// let runner = Runner::builder().memory_service(memory) /* … */;
    /// ```
    pub fn scope_memory(&self, memory: Arc<dyn Memory>) -> Result<Arc<dyn Memory>> {
        if !memory.supports_project_scoping() {
            return Err(KernelError::Config {
                reason: format!(
                    "partition `{}` needs a memory that keeps projects apart: this memory \
                     does not implement project scoping, so it would show every partition's \
                     entries to every other; use a MemoryService with project support",
                    self.id
                ),
            });
        }
        Ok(Arc::new(PartitionMemory {
            inner: memory,
            partition: self.id.clone(),
            project: self.memory_scope.clone(),
        }))
    }
}

/// The identity of a partition's agents ([`Partition::identity_source`]).
#[derive(Debug)]
struct PartitionIdentity {
    partition: Partition,
    app_name: String,
    root_agent: String,
}

#[async_trait]
impl IdentitySource for PartitionIdentity {
    async fn current(&self) -> adk_core::Result<AgentPrincipal> {
        Ok(self.partition.principal(&self.app_name, &self.root_agent))
    }

    async fn principal_for(&self, agent_name: &str) -> adk_core::Result<Option<AgentPrincipal>> {
        Ok(Some(self.partition.principal(&self.app_name, agent_name)))
    }
}

/// A memory confined to one partition's project ([`Partition::scope_memory`]).
struct PartitionMemory {
    inner: Arc<dyn Memory>,
    partition: PartitionId,
    project: String,
}

impl PartitionMemory {
    fn refuse(&self, project: &str) -> AdkError {
        AdkError::new(
            ErrorComponent::Memory,
            ErrorCategory::Forbidden,
            MEMORY_CROSSING_CODE,
            format!(
                "partition `{}` reads and writes only its own memory scope `{}`, not `{project}`; \
                 the coordinator reaches another partition through a crossing (spec 3.2 K-I15)",
                self.partition, self.project
            ),
        )
    }
}

#[async_trait]
impl Memory for PartitionMemory {
    async fn search(&self, query: &str) -> adk_core::Result<Vec<MemoryEntry>> {
        self.inner.search_in_project(query, &self.project).await
    }

    async fn health_check(&self) -> adk_core::Result<()> {
        self.inner.health_check().await
    }

    async fn add(&self, entry: MemoryEntry) -> adk_core::Result<()> {
        self.inner.add_to_project(entry, &self.project).await
    }

    async fn delete(&self, _query: &str) -> adk_core::Result<u64> {
        // The inner `delete` matches entries in every project.
        Err(AdkError::new(
            ErrorComponent::Memory,
            ErrorCategory::Unsupported,
            MEMORY_DELETE_CODE,
            format!(
                "partition `{}` cannot delete by query: the memory's delete reaches every \
                 project; delete the partition's project through its MemoryService",
                self.partition
            ),
        ))
    }

    fn supports_project_scoping(&self) -> bool {
        true
    }

    async fn search_in_project(
        &self,
        query: &str,
        project_id: &str,
    ) -> adk_core::Result<Vec<MemoryEntry>> {
        if project_id != self.project {
            return Err(self.refuse(project_id));
        }
        self.inner.search_in_project(query, project_id).await
    }

    async fn add_to_project(&self, entry: MemoryEntry, project_id: &str) -> adk_core::Result<()> {
        if project_id != self.project {
            return Err(self.refuse(project_id));
        }
        self.inner.add_to_project(entry, project_id).await
    }
}

/// The partition table and the crossing ledger (spec 3.2 §7.8, §13 Q8), on every backend
/// [`ProcessStore`](crate::ProcessStore) has: [`InMemoryPartitionStore`](crate::InMemoryPartitionStore),
/// `SqlitePartitionStore`, `PostgresPartitionStore` and `RedisPartitionStore`, with one
/// conformance suite (`partition_store_conformance_tests!`, feature `conformance`).
///
/// # Example
///
/// ```rust
/// use adk_kernel::{InMemoryPartitionStore, Partition, PartitionId, PartitionStore};
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> adk_kernel::Result<()> {
/// let store = InMemoryPartitionStore::new();
/// store.put(Partition::new("work", "work")).await?;
/// assert_eq!(store.get(&PartitionId::new("work")).await?.unwrap().class, "work");
/// assert_eq!(store.list().await?.len(), 1);
/// # Ok(())
/// # }
/// ```
#[async_trait]
pub trait PartitionStore: Send + Sync {
    /// Inserts `partition`, or replaces the record with the same id.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn put(&self, partition: Partition) -> Result<()>;

    /// The partition `id`, or `None`.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn get(&self, id: &PartitionId) -> Result<Option<Partition>>;

    /// Every partition, ordered by id in byte order.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn list(&self) -> Result<Vec<Partition>>;

    /// Deletes the partition `id`; `false` when there was none. Its crossing records stay.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn delete(&self, id: &PartitionId) -> Result<bool>;

    /// Appends one crossing record. Records are never changed or deleted.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn record_crossing(&self, record: CrossingRecord) -> Result<()>;

    /// The crossing records `filter` selects, ordered by time, then id, at most
    /// `filter.limit`.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn crossings(&self, filter: &CrossingFilter) -> Result<Vec<CrossingRecord>>;

    /// Records that the read with crossing-fingerprint digest `fingerprint` was approved in
    /// the durable run `run`, for the rest of that run (spec 3.2 §13 Q3). Granting twice
    /// keeps one grant.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn grant(&self, run: &str, fingerprint: &str) -> Result<()>;

    /// The fingerprint digests the durable run `run` holds grants for.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn grants(&self, run: &str) -> Result<Vec<String>>;

    /// Drops every grant of the durable run `run`, which finished; answers how many.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Store`] when the backend fails.
    async fn release_run(&self, run: &str) -> Result<u64>;
}

/// An unknown partition, as the control plane answers it (`404`).
pub(crate) fn unknown_partition(id: &PartitionId) -> KernelError {
    KernelError::Core(AdkError::new(
        ErrorComponent::Server,
        ErrorCategory::NotFound,
        NOT_FOUND_CODE,
        format!("no partition named `{id}`; list them with `adk-rust partitions`"),
    ))
}

/// How many (fingerprint, call, outcome) triples the policy remembers having recorded, so
/// the re-evaluations of one call (an edit, a prompt's re-check) record once.
const RECORDED_CAP: usize = 4096;

/// How many invocations' grants are kept in memory before they are all dropped.
const INVOCATION_GRANTS_CAP: usize = 4096;

/// One call's crossing as the ledger records it: the crossing, its fingerprint, and the
/// run, agent and tool that made it.
pub(crate) struct CrossingCall<'a> {
    pub(crate) crossing: &'a Crossing,
    pub(crate) fingerprint: &'a str,
    pub(crate) run: &'a str,
    pub(crate) agent: &'a str,
    pub(crate) tool: &'a str,
}

/// The partition table and its crossings (spec 3.2 §7.8): reads and changes partitions
/// with `kernel.partition.created` and `kernel.partition.changed` records, builds each
/// partition's [`PartitionPolicy`], and keeps the crossing ledger the policy and the crossing
/// tools write and the run's approvals are read from. Cloning shares it.
///
/// # Example
///
/// ```rust
/// use adk_core::audit_log::AuditActor;
/// use adk_kernel::{CrossPartition, Partition, Partitions};
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> adk_kernel::Result<()> {
/// let partitions = Partitions::in_memory();
/// let by = AuditActor::default();
/// partitions.put(Partition::new("home", "home").with_cross_partition(CrossPartition::CoordinatorWithApproval), by.clone()).await?;
/// partitions.put(Partition::new("work", "work").with_cross_partition(CrossPartition::CoordinatorWithApproval), by.clone()).await?;
/// let home = partitions.block("home", "work", by).await?;
/// assert!(home.blocked_crossings.contains(&"work".into()));
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct Partitions {
    inner: Arc<PartitionsInner>,
}

struct PartitionsInner {
    store: Arc<dyn PartitionStore>,
    audit: OnceLock<AuditTrail>,
    clock: Arc<dyn Clock>,
    recorded: Mutex<HashSet<(String, String, CrossingOutcome)>>,
    /// The approved reads of invocations outside a durable run, by invocation id; process
    /// memory, so they end with the process if the invocation never does.
    invocation_grants: Mutex<HashMap<String, HashSet<String>>>,
}

impl fmt::Debug for Partitions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Partitions")
            .field("audit", &self.inner.audit.get().is_some())
            .finish_non_exhaustive()
    }
}

impl Partitions {
    /// The partitions in `store`.
    pub fn new(store: Arc<dyn PartitionStore>) -> Self {
        Self::with_parts(store, Arc::new(SystemClock))
    }

    /// The partitions in a new [`InMemoryPartitionStore`](crate::InMemoryPartitionStore).
    pub fn in_memory() -> Self {
        Self::new(Arc::new(crate::stores::InMemoryPartitionStore::new()))
    }

    fn with_parts(store: Arc<dyn PartitionStore>, clock: Arc<dyn Clock>) -> Self {
        Self {
            inner: Arc::new(PartitionsInner {
                store,
                audit: OnceLock::new(),
                clock,
                recorded: Mutex::new(HashSet::new()),
                invocation_grants: Mutex::new(HashMap::new()),
            }),
        }
    }

    /// Writes the partition and crossing records to `trail` (spec 3.2 §9.3).
    #[must_use]
    pub fn with_audit(self, trail: AuditTrail) -> Self {
        let _ = self.inner.audit.set(trail);
        self
    }

    /// Reads crossing times from `clock` instead of the system clock.
    #[must_use]
    pub fn with_clock(self, clock: Arc<dyn Clock>) -> Self {
        let partitions = Self::with_parts(Arc::clone(&self.inner.store), clock);
        if let Some(trail) = self.inner.audit.get() {
            let _ = partitions.inner.audit.set(trail.clone());
        }
        partitions
    }

    /// Sets the audit trail when none is set yet; the kernel attaches its own at build.
    pub fn attach_audit(&self, trail: AuditTrail) {
        let _ = self.inner.audit.set(trail);
    }

    /// The store.
    pub fn store(&self) -> &Arc<dyn PartitionStore> {
        &self.inner.store
    }

    /// The partition `id`, or `None`.
    ///
    /// # Errors
    ///
    /// Returns the store's error.
    pub async fn get(&self, id: &PartitionId) -> Result<Option<Partition>> {
        self.inner.store.get(id).await
    }

    /// Every partition, ordered by id.
    ///
    /// # Errors
    ///
    /// Returns the store's error.
    pub async fn list(&self) -> Result<Vec<Partition>> {
        self.inner.store.list().await
    }

    /// Creates or changes `partition`, recording `kernel.partition.created` or
    /// `kernel.partition.changed` with the fields that changed.
    ///
    /// # Errors
    ///
    /// Returns [`KernelError::Config`] when the record is invalid, or the store's error.
    pub async fn put(&self, partition: Partition, by: AuditActor) -> Result<()> {
        partition.validate()?;
        let before = self.inner.store.get(&partition.id).await?;
        let to = serde_json::to_value(&partition)?;
        let (action, fields, from) = match &before {
            None => (audit::PARTITION_CREATED, Vec::new(), Value::Null),
            Some(before) => {
                let from = serde_json::to_value(before)?;
                let changed: Vec<String> = match (&from, &to) {
                    (Value::Object(old), Value::Object(new)) => old
                        .keys()
                        .chain(new.keys())
                        .filter(|key| old.get(*key) != new.get(*key))
                        .cloned()
                        .collect::<BTreeSet<_>>()
                        .into_iter()
                        .collect(),
                    _ => Vec::new(),
                };
                if changed.is_empty() {
                    return Ok(());
                }
                (audit::PARTITION_CHANGED, changed, from)
            }
        };
        self.inner.store.put(partition.clone()).await?;
        let fields = json!({
            "partition": partition.id,
            "fields": fields,
            "from": from,
            "to": to,
        });
        self.record_audit(action, by, fields).await;
        Ok(())
    }

    /// Sets the mode of partition `id`.
    ///
    /// # Errors
    ///
    /// Returns a `404` error when there is no such partition, or the store's error.
    pub async fn set_mode(
        &self,
        id: impl Into<PartitionId>,
        mode: PartitionMode,
        by: AuditActor,
    ) -> Result<Partition> {
        let id = id.into();
        let partition = self.inner.store.get(&id).await?.ok_or_else(|| unknown_partition(&id))?;
        let partition = partition.with_mode(mode);
        self.put(partition.clone(), by).await?;
        Ok(partition)
    }

    /// Blocks crossings from `from` into `to`: the prompt's "deny and block crossings to
    /// *to* from this partition". The block stays until the partition record is changed.
    ///
    /// # Errors
    ///
    /// Returns a `404` error when `from` is not a partition, or the store's error.
    pub async fn block(
        &self,
        from: impl Into<PartitionId>,
        to: impl Into<PartitionId>,
        by: AuditActor,
    ) -> Result<Partition> {
        let from = from.into();
        let partition =
            self.inner.store.get(&from).await?.ok_or_else(|| unknown_partition(&from))?;
        let partition = partition.with_blocked_crossing(to);
        self.put(partition.clone(), by).await?;
        Ok(partition)
    }

    /// The crossing records `filter` selects.
    ///
    /// # Errors
    ///
    /// Returns the store's error.
    pub async fn crossings(&self, filter: &CrossingFilter) -> Result<Vec<CrossingRecord>> {
        self.inner.store.crossings(filter).await
    }

    /// The policy of partition `home`'s harness ([`PartitionPolicy`]).
    pub fn policy(&self, home: impl Into<PartitionId>) -> PartitionPolicy {
        PartitionPolicy::new(self.clone(), home.into())
    }

    /// Why `crossing` may not happen, or `Ok` when it may ask for approval: both
    /// partitions exist and take part in crossings, the caller is the coordinator, and the
    /// source has not blocked the target (spec 3.2 §7.8, K-I15).
    ///
    /// # Errors
    ///
    /// The refusal's reason, also when the partition table cannot be read: a crossing
    /// fails closed.
    pub async fn admit(
        &self,
        crossing: &Crossing,
        coordinator: bool,
    ) -> std::result::Result<(), String> {
        let read = |id: &PartitionId| {
            let id = id.clone();
            async move {
                match self.inner.store.get(&id).await {
                    Ok(Some(partition)) => Ok(partition),
                    Ok(None) => Err(format!("there is no partition named `{id}`")),
                    Err(error) => Err(format!(
                        "the partition table could not be read ({error}), so the crossing is \
                         refused"
                    )),
                }
            }
        };
        let from = read(&crossing.from).await?;
        let to = read(&crossing.to).await?;
        if !coordinator {
            return Err(format!(
                "{} belongs to partition `{}`; only the coordinator may cross into another \
                 partition, and only under approval",
                crossing.label(),
                crossing.to
            ));
        }
        for partition in [&from, &to] {
            if partition.cross_partition == CrossPartition::Deny {
                return Err(format!(
                    "partition `{}` takes part in no crossings (cross_partition: deny)",
                    partition.id
                ));
            }
        }
        if from.blocked_crossings.contains(&crossing.to) {
            return Err(format!(
                "crossings to `{}` from partition `{}` are blocked",
                crossing.to, crossing.from
            ));
        }
        Ok(())
    }

    /// Whether the read `fingerprint` (a [`Crossing::fingerprint`]) was approved earlier in
    /// `run` and the run still holds the grant, so the coordinator's identical read needs no
    /// approval for the rest of the run (spec 3.2 §13 Q3). A failed read of the store answers
    /// `false`: the crossing asks again.
    pub async fn granted(&self, run: &CrossingRun, fingerprint: &str) -> bool {
        let digest = crossing::digest(fingerprint);
        match run {
            CrossingRun::Invocation(id) => self
                .inner
                .invocation_grants
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .get(id)
                .is_some_and(|grants| grants.contains(&digest)),
            CrossingRun::Durable(id) => match self.inner.store.grants(id).await {
                Ok(grants) => grants.contains(&digest),
                Err(error) => {
                    tracing::warn!(error = %error, "could not read the run's crossing grants; the crossing asks again");
                    false
                }
            },
        }
    }

    /// The fingerprint digests `run` holds grants for.
    ///
    /// # Errors
    ///
    /// Returns the store's error for a durable run.
    pub async fn grants(&self, run: &CrossingRun) -> Result<Vec<String>> {
        match run {
            CrossingRun::Invocation(id) => {
                let grants = self.inner.invocation_grants.lock().unwrap_or_else(|e| e.into_inner());
                let mut digests: Vec<String> =
                    grants.get(id).map(|set| set.iter().cloned().collect()).unwrap_or_default();
                digests.sort();
                Ok(digests)
            }
            CrossingRun::Durable(id) => self.inner.store.grants(id).await,
        }
    }

    /// Drops every grant of `run`, which finished, so no later run can reuse one (spec 3.2
    /// §13 Q3): the kernel's worker calls it when a durable run ends, the partition's
    /// harness when an invocation outside a durable run ends. Answers how many it dropped.
    ///
    /// # Errors
    ///
    /// Returns the store's error for a durable run.
    pub async fn release_run(&self, run: &CrossingRun) -> Result<u64> {
        match run {
            CrossingRun::Invocation(id) => Ok(self
                .inner
                .invocation_grants
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .remove(id)
                .map_or(0, |grants| grants.len() as u64)),
            CrossingRun::Durable(id) => self.inner.store.release_run(id).await,
        }
    }

    /// Records that `crossing` ran in `run` after its approval, and, for a read, keeps the
    /// approval the coordinator's identical reads reuse for the rest of the run. A write is
    /// never kept: it is approved one call at a time. A crossing tool calls it after the
    /// governed path authorized the call.
    ///
    /// # Errors
    ///
    /// Returns the store's error; the tool then reports the crossing unrecorded.
    pub async fn executed(
        &self,
        crossing: &Crossing,
        fingerprint: &str,
        run: &CrossingRun,
        agent: &str,
        tool: &str,
    ) -> Result<()> {
        let read = crossing.access == CrossingAccess::Read;
        if read && self.granted(run, fingerprint).await {
            return Ok(());
        }
        let call = CrossingCall { crossing, fingerprint, run: run.as_str(), agent, tool };
        let record = self.crossing_record(&call, CrossingOutcome::Approved, None);
        self.inner.store.record_crossing(record.clone()).await?;
        self.audit_crossing(&record).await;
        if read {
            match run {
                CrossingRun::Invocation(id) => {
                    let mut grants =
                        self.inner.invocation_grants.lock().unwrap_or_else(|e| e.into_inner());
                    // Dropping everything only makes later reads ask again.
                    if grants.len() > INVOCATION_GRANTS_CAP {
                        grants.clear();
                    }
                    grants.entry(id.clone()).or_default().insert(record.fingerprint);
                }
                CrossingRun::Durable(id) => self.inner.store.grant(id, &record.fingerprint).await?,
            }
        }
        Ok(())
    }

    /// The crossings since `since` that recur at least `at_least` times, by source, target,
    /// resource and name, most frequent first (spec 3.2 §7.8): what the maintenance process
    /// reports, with partition membership as the remedy. A request counts once: held, denied,
    /// or let through by its run's approval; the approval of a held request is not counted
    /// again.
    ///
    /// # Errors
    ///
    /// Returns the store's error.
    pub async fn recurring_crossings(
        &self,
        since: chrono::DateTime<chrono::Utc>,
        at_least: usize,
    ) -> Result<Vec<RecurringCrossing>> {
        let filter = CrossingFilter::default().since(since).outcomes([
            CrossingOutcome::Held,
            CrossingOutcome::Denied,
            CrossingOutcome::RunGrant,
        ]);
        let mut counts: std::collections::BTreeMap<
            (PartitionId, PartitionId, CrossingResource, String),
            usize,
        > = std::collections::BTreeMap::new();
        for record in self.inner.store.crossings(&filter).await? {
            *counts.entry((record.from, record.to, record.resource, record.name)).or_default() += 1;
        }
        let mut recurring: Vec<RecurringCrossing> = counts
            .into_iter()
            .filter(|(_, count)| *count >= at_least)
            .map(|((from, to, resource, name), count)| {
                RecurringCrossing::new(from, to, resource, name, count)
            })
            .collect();
        recurring.sort_by_key(|crossing| std::cmp::Reverse(crossing.count));
        Ok(recurring)
    }

    /// Records a decision the policy made about `call`, once per call id and outcome.
    pub(crate) async fn decided(
        &self,
        call: &CrossingCall<'_>,
        call_id: Option<&str>,
        outcome: CrossingOutcome,
        reason: Option<String>,
    ) {
        if let Some(call_id) = call_id {
            let mut recorded = self.inner.recorded.lock().unwrap_or_else(|e| e.into_inner());
            if recorded.len() > RECORDED_CAP {
                recorded.clear();
            }
            // Keyed by the call, not the run: a prompt's re-check sees no durable run id.
            if !recorded.insert((crossing::digest(call.fingerprint), call_id.to_string(), outcome))
            {
                return;
            }
        }
        let record = self.crossing_record(call, outcome, reason);
        if let Err(error) = self.inner.store.record_crossing(record.clone()).await {
            tracing::warn!(error = %error, "could not write a crossing record");
        }
        self.audit_crossing(&record).await;
    }

    fn crossing_record(
        &self,
        call: &CrossingCall<'_>,
        outcome: CrossingOutcome,
        reason: Option<String>,
    ) -> CrossingRecord {
        let crossing = call.crossing;
        CrossingRecord {
            id: uuid::Uuid::new_v4().to_string(),
            at: self.inner.clock.now(),
            run: call.run.to_string(),
            agent: call.agent.to_string(),
            from: crossing.from.clone(),
            to: crossing.to.clone(),
            resource: crossing.resource,
            name: crossing.name.clone(),
            access: crossing.access,
            tool: call.tool.to_string(),
            fingerprint: crossing::digest(call.fingerprint),
            outcome,
            reason,
        }
    }

    /// Writes `kernel.partition.crossing` (spec 3.2 §9.3): ids, names and a digest, never
    /// the call's arguments.
    async fn audit_crossing(&self, record: &CrossingRecord) {
        let actor = match adk_core::AgentId::new(record.agent.clone()) {
            Ok(agent) => AuditActor { agent: Some(agent), ..AuditActor::default() },
            Err(_) => AuditActor::default(),
        };
        let fields = json!({
            "partition": record.from,
            "to": record.to,
            "resource": record.resource,
            "name": record.name,
            "access": record.access,
            "outcome": record.outcome,
            "run": record.run,
            "tool": record.tool,
            "fingerprintDigest": record.fingerprint,
            "reason": record.reason,
            "by": record.agent,
        });
        self.record_audit(audit::PARTITION_CROSSING, actor, fields).await;
    }

    async fn record_audit(&self, action: &str, actor: AuditActor, fields: Value) {
        let Some(trail) = self.inner.audit.get() else { return };
        let mut draft =
            AuditRecordDraft::new(AuditRecordKind::Other(audit::KERNEL_AUDIT_KIND.into()), action)
                .actor(actor);
        if let Value::Object(fields) = fields {
            for (key, value) in fields {
                draft = draft.ext(key, value);
            }
        }
        if let Err(error) = trail.record(draft).await {
            tracing::warn!(audit.action = action, error = %error, "could not write a partition audit record");
        }
    }
}