adk_kernel/lib.rs
1//! # adk-kernel
2//!
3//! The Agent Kernel of ADK-Rust, the always-on form of the harness (spec 3.2,
4//! `docs/design/spec-3.2-agent-kernel.md`): a resident supervisor with a process model,
5//! wake semantics, an event bus and a control plane, composed from the durable-run
6//! machinery `adk-core` and `adk-server` already hold.
7//!
8//! Three rules shape it (§1):
9//!
10//! - **Always resumable, not always running.** A process is a durable run; asleep is
11//! `Waiting` with a recorded [`WakeSet`] and no lease, so a sleeping agent makes no model
12//! call (K-I2).
13//! - **Every wake is a governed run.** A wake produces a `NewRun` or wakes a parked run;
14//! nothing calls a tool outside a run (K-I1), so policy, guardrails, confirmation, the
15//! action ledger and audit apply as for an interactive turn.
16//! - **The kernel owns state, surfaces attach.** CLI, web and phone are terminals on one
17//! running system over the [`control`] plane (K-I17).
18//!
19//! `adk-kernel` hosts any `Arc<Runner>` and depends on no harness type (§6 D-1); the
20//! `kernel` feature of `adk-harness` carries the glue (§10 HK1), and `adk-rust up` starts
21//! it.
22//!
23//! | Item | Spec |
24//! |---|---|
25//! | [`Kernel`], [`KernelBuilder`], [`KernelConfig`], [`KernelHealth`], [`KernelState`] | §7.2 |
26//! | [`Process`], [`ProcessMeta`], [`ProcessView`], [`GoalProgress`], [`ProcessKind`], [`Suspension`], [`ProcessTable`], [`ProcessSpec`], [`ProgressSource`] | §7.3 |
27//! | [`inventory`]: [`KernelInventory`](inventory::KernelInventory), [`InventoryWarning`](inventory::InventoryWarning), [`Kernel::inventory`], `GET /api/kernel/inventory`; `KernelBuilder::signals` and `Kernel::signal_sink` (feature `charter`) | §7.1, §7.4 |
28//! | [`stores::KernelStores`], [`stores::StoreUse`], [`stores::StoreOrigin`]: every stateful store on one backend, [`KernelBuilder::stores`], [`KernelBuilder::require_durable`], [`Kernel::store_uses`] | §7.1, §13 Q8 |
29//! | [`ProcessStore`], [`ProcessFilter`], [`SpendScope`], [`InMemoryProcessStore`], `SqliteProcessStore`, `PostgresProcessStore`, `RedisProcessStore`, `MongoProcessStore`, `FirestoreProcessStore`, `Neo4jProcessStore` | §7.3, §13 Q8 |
30//! | [`Wake`], [`WakeSet`], [`ExpiryAction`], [`WakeSource`], [`WakeEvent`], [`keys`], [`Trigger`], [`WakeOverlap`], [`WakeRouter`], [`WakeOutcome`], [`KernelWakeExecutor`], the wake source adapters | §7.4 |
31//! | [`vision`]: [`VisionWakeSource`], [`VisualWake`], [`VisualFrame`], [`VisualRegion`], [`vision_payload`]; with feature `vision`, the source as an `adk-vision` `WatchSink` | spec 3.7 §7.10 |
32//! | [`CronWatermarkStore`], [`SourceWatermark`], [`InMemoryCronWatermarkStore`], `SqliteCronWatermarkStore`, `PostgresCronWatermarkStore`, `RedisCronWatermarkStore`, `MongoCronWatermarkStore`, `FirestoreCronWatermarkStore`, `Neo4jCronWatermarkStore` | §7.4, §7.13, §13 Q8 |
33//! | [`KernelEvent`], [`EventKind`], [`EventFilter`], [`EventRecord`], [`EventBus`], [`EventSubscriber`], [`EventSink`], [`AuditSink`], [`SpendDelta`]; `WebhookSink` (feature `webhooks`) | §7.6 |
34//! | [`Agenda`], [`AgendaHandle`], [`ProposalOutcome`], [`AgendaItem`], [`AgendaState`], [`AgendaStateKind`], [`AgendaOrigin`], [`AgendaAnswer`], [`ObservationAudience`], [`PlanRef`], [`AgendaStore`], [`AgendaFilter`], [`InMemoryAgendaStore`], [`ProactivityPolicy`], [`ProactiveApproval`] | §7.5 |
35//! | [`agents`]: [`AgentRecord`](agents::AgentRecord), [`AgentStore`](agents::AgentStore) on every backend, [`AgentFactory`](agents::AgentFactory), [`AgentCaller`](agents::AgentCaller), [`AgentPreview`](agents::AgentPreview) | spec 3.3 §5.7, §10 K7 |
36//! | [`ConsolidationTick`], [`Consolidator`], [`admit_tick`], [`TickInputs`], [`TickAdmission`], [`TickMaterial`], [`TickRun`], [`TickOutcome`], [`TickProposal`], [`TickSpend`], [`CONSOLIDATION_TARGET`], [`CONSOLIDATION_LABEL`] | §7.5, §13 Q1, Q12 |
37//! | [`ReviewReport`], [`StandingRuleSource`], [`RuleAge`], [`StaleRule`], [`AgedProcess`] | §7.4, §9.1 |
38//! | [`QueueClass`], [`QuietHours`], [`Quotas`] | §7.9 |
39//! | [`drivers`]: `DriverManifest`, [`EffectClass`](drivers::EffectClass), `DriverStore`, `DriverSupervisor`, `DriverPolicy` (feature `drivers`) | §7.7 |
40//! | `proc` (feature `proc`): `Ledger`, `LedgerEntry`, `LedgerStore` on every backend, `LedgerSink`, `LedgerAuditTap`; `proc::baselines` (feature `proc-baselines`): `BaselinePass`, `BaselinePolicy`, `ObservationPhraser`, `Observation` | §7.10 |
41//! | [`PartitionId`], [`PartitionMode`], [`Partition`], [`CrossPartition`], [`PartitionStore`], [`InMemoryPartitionStore`], `SqlitePartitionStore`, `PostgresPartitionStore`, `RedisPartitionStore`, `MongoPartitionStore`, `FirestorePartitionStore`, `Neo4jPartitionStore`; [`Partitions`] | §7.8 |
42//! | [`Crossing`], [`CrossingResource`], [`CrossingAccess`], [`CrossingRun`], [`CrossingRecord`], [`CrossingOutcome`], [`CrossingFilter`], [`CrossingRoute`], [`PartitionPolicy`], [`CrossingTool`] | §7.8, §13 Q3, K-I15 |
43//! | [`control`]: [`ControlPlaneConfig`](control::ControlPlaneConfig), [`router`](control::router), [`ControlPlane`](control::ControlPlane), [`ControlClient`](control::ControlClient), [`api`](control::api) | §7.11 |
44//! | [`audit`] action names | §9.3 |
45//! | [`subagents`]: background sub-agents as processes of kind [`ProcessKind::Subagent`], [`KernelBuilder::subagents`], [`Kernel::subagents`], [`SubagentHandle`], [`KernelEvent::Subagent`] | `adk_core::subagent` |
46//! | `service` (feature `service`): `ServiceDefinition`, `install`, `uninstall`, `run_as_service` (Windows) | §7.12, §10 W3 |
47//! | `conformance` (feature `conformance`) | §11 |
48//! | `managed` (feature `managed`): `KernelDef`, `TenantKernel`, `KernelResolver`, `ManagedKernels` | §7.13, §10 M1 |
49//! | [`pairing`]: `Pairing`, `DeviceScope`, `DeviceStore` on every backend, `DeviceTokenExtractor` | spec 3.3 §10 K2 |
50//! | `channels` (feature `channels`): `ChannelSink`, `ChannelGateway`, `ChannelBindingStore` on every backend, `InMemoryChannel` | spec 3.3 §10 K1 |
51//!
52//! Agenda and proactivity (§7.5) and attention and resources (§7.9) are phase 2; drivers
53//! (§7.7) and partitions (§7.8) phase 3; channel wakes phase 5. Each adds to these types
54//! without changing them.
55//!
56//! # Features
57//!
58//! | Feature | Default | Pulls in |
59//! |---|---|---|
60//! | `sqlite` | on | `SqliteProcessStore`, on the pool of `harness.db` |
61//! | `postgres`, `redis` | off | `PostgresProcessStore`, `RedisProcessStore`, opened from the URL of `adk-rust serve --store` |
62//! | `mongodb`, `firestore`, `neo4j` | off | The process, agenda, driver, ledger, partition and cron watermark stores on MongoDB, Firestore or Neo4j (`MongoProcessStore`, `FirestoreProcessStore`, `Neo4jProcessStore` and the stores beside them), with the same backend of `adk-session`'s run, schedule and governance stores |
63//! | `webhooks` | on | `webhook_wake_hook` over `adk-server`'s `WebhookIngress`; `WebhookSink`, the outbound webhook sink over `adk-core`'s signed delivery |
64//! | `drivers` | on | [`drivers`]: manifests, the driver store, the supervisor over `adk-tool`'s MCP manager, the stdio launcher on `adk-sandbox` |
65//! | `service` | on | The `service` module: `adk-rust up` as a launchd agent, a systemd user unit or a Windows service (`windows-service` on Windows) |
66//! | `proc` | on | The content-free activity ledger (`proc`), its stores and `GET /api/kernel/proc/*`; the OTel GenAI histograms of the live aggregates |
67//! | `proc-baselines` | off | The daily baseline pass and observations over `adk-eval`'s ledger statistics (implies `proc`; the only `adk-eval` edge) |
68//! | `channels` | off | The chat channel contract (spec 3.3 §10 K1): the `channels` module, its binding store on every backend, and the adapter contract suite with `conformance` |
69//! | `managed` | off | The managed form: `KernelDef` in a managed definition, one kernel per tenant (`adk-managed`) |
70//! | `charter` | off | `charter` (spec 3.5 §7.10, §7.11): the charter store, the KPI tracker, autonomy records, signals and the site process kinds, on every backend of the process store |
71//! | `vision` | off | The bridge from `adk-vision` (spec 3.7 §7.10): `VisionWakeSource` as a `WatchSink` and `VisualWake` from a `WatchEvent`; the source itself needs no feature |
72//! | `conformance` | off | The `conformance` module |
73//!
74//! # Example
75//!
76//! ```rust,no_run
77//! use std::sync::Arc;
78//! use adk_core::InMemoryRunStore;
79//! use adk_kernel::{Kernel, KernelConfig, ProcessFilter};
80//!
81//! # async fn example(runner: Arc<adk_runner::Runner>) -> adk_kernel::Result<()> {
82//! let kernel = Kernel::builder()
83//! .config(KernelConfig::default())
84//! .run_store(Arc::new(InMemoryRunStore::new()))
85//! .agent("adk-rust", runner)
86//! .build()?;
87//! kernel.start().await?;
88//! for process in kernel.processes().ps(ProcessFilter::default()).await? {
89//! println!("{} {:?} {}", process.meta.pid, process.run.state, process.meta.kind);
90//! }
91//! kernel.shutdown_on_signal().await?;
92//! # Ok(())
93//! # }
94//! ```
95//!
96//! ## Stability
97//!
98//! Beta — new in 3.0.0, and its process model, wake sources and control plane are still taking
99//! shape in use. See [STABILITY.md](https://github.com/zavora-ai/adk-rust/blob/main/STABILITY.md).
100
101#![deny(missing_docs)]
102
103pub mod agenda;
104pub mod agents;
105pub mod audit;
106pub mod bus;
107#[cfg(feature = "channels")]
108pub mod channels;
109#[cfg(feature = "charter")]
110pub mod charter;
111#[cfg(feature = "conformance")]
112pub mod conformance;
113pub mod consolidation;
114pub mod control;
115pub mod drivers;
116pub mod error;
117pub mod inventory;
118pub mod kernel;
119#[cfg(feature = "managed")]
120pub mod managed;
121pub mod pairing;
122pub mod partition;
123#[cfg(feature = "proc")]
124pub mod proc;
125pub mod process;
126pub mod review;
127pub mod sched;
128mod serde_util;
129#[cfg(feature = "service")]
130pub mod service;
131pub mod stores;
132pub mod subagents;
133mod supervise;
134pub mod vision;
135pub mod wake;
136pub mod watermark;
137#[cfg(windows)]
138mod win;
139
140pub use agenda::{
141 AGENDA_TOOL_NAME, Agenda, AgendaAnswer, AgendaFilter, AgendaHandle, AgendaItem, AgendaOrigin,
142 AgendaState, AgendaStateKind, AgendaStore, ObservationAudience, PlanRef, ProactiveApproval,
143 ProactivityPolicy, ProposalOutcome,
144};
145#[cfg(feature = "webhooks")]
146pub use bus::WebhookSink;
147pub use bus::{
148 AuditSink, DEFAULT_EVENT_CAPACITY, EventBus, EventFilter, EventKind, EventRecord, EventSink,
149 EventSubscriber, KernelEvent, SpendDelta,
150};
151pub use consolidation::{
152 CONSOLIDATION_LABEL, CONSOLIDATION_TARGET, ConsolidationTick, Consolidator, TickAdmission,
153 TickInputs, TickMaterial, TickOutcome, TickProposal, TickRun, TickSpend, admit_tick,
154};
155pub use error::{KernelError, Result};
156pub use kernel::{
157 Kernel, KernelBuilder, KernelConfig, KernelHealth, KernelState, RunNow, RunNowTarget, Steering,
158 WorkerHealth,
159};
160pub use partition::{
161 CROSSING_PARTITION_ARG, CrossPartition, Crossing, CrossingAccess, CrossingFilter,
162 CrossingOutcome, CrossingRecord, CrossingResource, CrossingRoute, CrossingRun, CrossingTool,
163 Partition, PartitionId, PartitionMode, PartitionPolicy, PartitionStore, Partitions,
164 RecurringCrossing,
165};
166pub use process::{
167 GoalProgress, Process, ProcessHandle, ProcessKind, ProcessMeta, ProcessSpec, ProcessTable,
168 ProcessView, ProgressSource, RunUsage, Suspension,
169};
170pub use review::{AgedProcess, ReviewReport, RuleAge, StaleRule, StandingRuleSource};
171pub use sched::{QueueClass, QuietHours, Quotas};
172#[cfg(feature = "firestore")]
173pub use stores::FirestoreAgendaStore;
174#[cfg(feature = "firestore")]
175pub use stores::FirestoreCronWatermarkStore;
176#[cfg(feature = "firestore")]
177pub use stores::FirestorePartitionStore;
178#[cfg(feature = "firestore")]
179pub use stores::FirestoreProcessStore;
180#[cfg(feature = "mongodb")]
181pub use stores::MongoAgendaStore;
182#[cfg(feature = "mongodb")]
183pub use stores::MongoCronWatermarkStore;
184#[cfg(feature = "mongodb")]
185pub use stores::MongoPartitionStore;
186#[cfg(feature = "mongodb")]
187pub use stores::MongoProcessStore;
188#[cfg(feature = "neo4j")]
189pub use stores::Neo4jAgendaStore;
190#[cfg(feature = "neo4j")]
191pub use stores::Neo4jCronWatermarkStore;
192#[cfg(feature = "neo4j")]
193pub use stores::Neo4jPartitionStore;
194#[cfg(feature = "neo4j")]
195pub use stores::Neo4jProcessStore;
196#[cfg(feature = "postgres")]
197pub use stores::PostgresAgendaStore;
198#[cfg(feature = "postgres")]
199pub use stores::PostgresCronWatermarkStore;
200#[cfg(feature = "postgres")]
201pub use stores::PostgresPartitionStore;
202#[cfg(feature = "postgres")]
203pub use stores::PostgresProcessStore;
204#[cfg(feature = "redis")]
205pub use stores::RedisAgendaStore;
206#[cfg(feature = "redis")]
207pub use stores::RedisCronWatermarkStore;
208#[cfg(feature = "redis")]
209pub use stores::RedisPartitionStore;
210#[cfg(feature = "redis")]
211pub use stores::RedisProcessStore;
212#[cfg(feature = "sqlite")]
213pub use stores::SqliteAgendaStore;
214#[cfg(feature = "sqlite")]
215pub use stores::SqliteCronWatermarkStore;
216#[cfg(feature = "sqlite")]
217pub use stores::SqlitePartitionStore;
218#[cfg(feature = "sqlite")]
219pub use stores::SqliteProcessStore;
220pub use stores::{
221 InMemoryAgendaStore, InMemoryCronWatermarkStore, InMemoryPartitionStore, InMemoryProcessStore,
222 ProcessFilter, ProcessStore, SpendScope, UsageTotals,
223};
224pub use subagents::{SUBAGENT_LABEL, SUBAGENT_NAME_LABEL, SUBAGENT_NOTICE_SOURCE, SubagentHandle};
225pub use vision::{
226 VISION_PAYLOAD_KIND, VisionWakeSource, VisualFrame, VisualRegion, VisualWake, vision_payload,
227};
228#[cfg(feature = "webhooks")]
229pub use wake::webhook_wake_hook;
230pub use wake::{
231 ApprovalWakeSource, COALESCED_LABEL, CronWakeSource, ExpiryAction, FileWakeSource,
232 KERNEL_WAKE_TARGET, KernelWakeExecutor, RUN_NOW_SOURCE, RunFinishedWakeSource, SKIPPED_LABEL,
233 Trigger, WAKE_PAYLOAD_LIMIT, Wake, WakeEvent, WakeOutcome, WakeOverlap, WakeReason,
234 WakeReasonHooks, WakeRouter, WakeSet, WakeSource, kernel_wake_run, keys, queue_wake_ingress,
235};
236pub use watermark::{CronWatermarkStore, SourceWatermark};
237
238/// A short, stable tag of `path`: sixteen hex digits of the SHA-256 of its absolute form,
239/// lowercased on Windows, whose paths ignore case. It names a kernel home's pipe and service
240/// (spec 3.2 §10 W2, §7.12), and the directory of a socket too long for its home (spec 3.3
241/// G18), so a client and the kernel derive the same name from one home.
242pub(crate) fn path_tag(path: &std::path::Path) -> String {
243 use sha2::Digest as _;
244
245 let path = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
246 let text = path.to_string_lossy();
247 let text = if cfg!(windows) { text.to_lowercase() } else { text.into_owned() };
248 sha2::Sha256::digest(text.as_bytes())[..8].iter().map(|byte| format!("{byte:02x}")).collect()
249}