Skip to main content

adk_kernel/
inventory.rs

1//! What a kernel runs, and what it would silently not do (spec 3.2 §7.1, §7.4; #1278):
2//! [`Kernel::inventory`](crate::Kernel::inventory), logged when the kernel starts and served at
3//! `GET /api/kernel/inventory`.
4
5use serde::{Deserialize, Serialize};
6
7use crate::partition::PartitionId;
8use crate::stores::StoreUse;
9use crate::wake::Wake;
10
11/// What a kernel runs: its targets, standing triggers, wake sources, the partitions it files
12/// signals in and its stores, with a warning for each thing it would silently not do.
13///
14/// | Warning | When |
15/// |---|---|
16/// | [`TRIGGER_UNSERVED`](InventoryWarning::TRIGGER_UNSERVED) | A trigger's run names a target no agent or workflow of the kernel serves |
17/// | [`SIGNAL_UNFILED`](InventoryWarning::SIGNAL_UNFILED) | A trigger wakes on a signal in a partition the kernel files no signals in (`KernelBuilder::signals`) |
18/// | [`STORE_IN_MEMORY`](InventoryWarning::STORE_IN_MEMORY) | A stateful store is held in memory: one per store the builder defaulted, one per bundle that keeps stores in memory |
19/// | [`KERNEL_IDLE`](InventoryWarning::KERNEL_IDLE) | The kernel has no agent, trigger, workflow, schedule store or agent factory |
20/// | [`APPROVALS_UNROUTED`](InventoryWarning::APPROVALS_UNROUTED) | The approval service takes no router from the kernel (`ApprovalService::route_unrouted`), so a call it holds with no route of its own reaches no inbox |
21/// | [`APPROVALS_UNHEARD`](InventoryWarning::APPROVALS_UNHEARD) | The approval service takes no notifier from the kernel (`ApprovalService::add_notifier`), so no `held` or `decided` event is published |
22/// | any other code | A warning the host or a kit added (`KernelBuilder::inventory_warning`), such as a job it does not schedule |
23///
24/// # Example
25///
26/// ```rust
27/// use std::sync::Arc;
28/// use adk_core::InMemoryRunStore;
29/// use adk_kernel::Kernel;
30/// use adk_kernel::inventory::InventoryWarning;
31///
32/// let kernel = Kernel::builder().run_store(Arc::new(InMemoryRunStore::new())).build()?;
33/// let inventory = kernel.inventory();
34/// assert!(inventory.warnings.iter().any(|warning| warning.code == InventoryWarning::KERNEL_IDLE));
35/// # Ok::<(), adk_kernel::KernelError>(())
36/// ```
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38#[serde(rename_all = "camelCase")]
39#[non_exhaustive]
40pub struct KernelInventory {
41    /// The app the kernel's runs belong to.
42    pub app_name: String,
43    /// The agent targets, sorted: those given to the builder and the agents a person added.
44    /// Workflow ids cannot be listed.
45    pub targets: Vec<String>,
46    /// The standing triggers, in the order they were added.
47    pub triggers: Vec<InventoryTrigger>,
48    /// The wake sources' names, the kernel's own included.
49    pub wake_sources: Vec<String>,
50    /// Whether the kernel's scheduler fires a schedule store.
51    pub schedules: bool,
52    /// The partitions the kernel files signals in (`KernelBuilder::signals`).
53    pub signal_partitions: Vec<PartitionId>,
54    /// Each stateful store: where it came from and whether it is held in memory.
55    pub stores: Vec<StoreUse>,
56    /// What the kernel would silently not do.
57    pub warnings: Vec<InventoryWarning>,
58}
59
60/// One standing trigger of a [`KernelInventory`].
61#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
62#[serde(rename_all = "camelCase")]
63#[non_exhaustive]
64pub struct InventoryTrigger {
65    /// The trigger's name.
66    pub name: String,
67    /// What fires it.
68    pub wake: Wake,
69    /// The target its run names.
70    pub target: String,
71    /// The queue its run waits on.
72    pub queue: String,
73    /// The partition its processes run in.
74    pub partition: PartitionId,
75    /// What a firing does while a process it started is unfinished.
76    #[serde(default)]
77    pub overlap: crate::wake::WakeOverlap,
78    /// Whether an agent or workflow of the kernel serves the target.
79    pub served: bool,
80}
81
82/// Something a kernel would silently not do, in a [`KernelInventory`].
83///
84/// # Example
85///
86/// ```rust
87/// use adk_kernel::inventory::InventoryWarning;
88///
89/// let warning = InventoryWarning::new("site.job_unscheduled", "the weekly digest is not scheduled: the owner has no channel")
90///     .with_subject("site.digest");
91/// assert_eq!(serde_json::to_value(&warning)?["subject"], "site.digest");
92/// # Ok::<(), serde_json::Error>(())
93/// ```
94#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
95#[serde(rename_all = "camelCase")]
96#[non_exhaustive]
97pub struct InventoryWarning {
98    /// A stable code, such as [`TRIGGER_UNSERVED`](Self::TRIGGER_UNSERVED).
99    pub code: String,
100    /// What the warning concerns: a trigger, a store or a job.
101    #[serde(default, skip_serializing_if = "Option::is_none")]
102    pub subject: Option<String>,
103    /// What happens, and how to fix it.
104    pub message: String,
105}
106
107impl InventoryWarning {
108    /// A trigger's run names a target no agent or workflow of the kernel serves.
109    pub const TRIGGER_UNSERVED: &'static str = "kernel.trigger.unserved";
110    /// A trigger wakes on a signal in a partition the kernel files no signals in.
111    pub const SIGNAL_UNFILED: &'static str = "kernel.trigger.signal_unfiled";
112    /// A stateful store is held in memory, so its state ends with the process.
113    pub const STORE_IN_MEMORY: &'static str = "kernel.store.in_memory";
114    /// The kernel has nothing to run.
115    pub const KERNEL_IDLE: &'static str = "kernel.idle";
116    /// Held calls the approval service routes nowhere reach no inbox.
117    pub const APPROVALS_UNROUTED: &'static str = "kernel.approvals.unrouted";
118    /// The approval service's requests are not published on the event bus.
119    pub const APPROVALS_UNHEARD: &'static str = "kernel.approvals.unheard";
120
121    /// A warning of `code` saying `message`.
122    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
123        Self { code: code.into(), subject: None, message: message.into() }
124    }
125
126    /// Names what the warning concerns.
127    #[must_use]
128    pub fn with_subject(mut self, subject: impl Into<String>) -> Self {
129        self.subject = Some(subject.into());
130        self
131    }
132}