adk-website 3.0.0

A prebuilt AWP 2.0 website for ADK-Rust: site kinds, a team of eight agents, agent-led onboarding, themes, capability packs, playbooks, jobs, alarms and the weekly review
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
//! Serving sites: the kernel's control plane and the agent edge (spec 3.5 §7.17, §7.22,
//! A-I20, A-I22): what `adk-website start`, `adk-website agency` and the container run.
//!
//! | Listener | Serves |
//! |---|---|
//! | Kernel (loopback by default) | The control plane, with device pairing: the setup code is claimed at `POST /api/kernel/pairing/claim` |
//! | Edge | The agent edge's routes, the stream feed and the content: the signed release on the site's `DirectoryHost`, or the holding page until the launch; in agency mode, each site's by its host (A-I22) |
//!
//! Each site is a [`RunningSite`] over its project ([`ProjectPaths`]); the kernel's stores are
//! durable in `data/kernel.db` ([`SiteStores`]). The setup code is issued while no device
//! is paired, so a restarted site does not offer a second one.
//!
//! The kernel runs each site's processes (spec 3.5 §7.20): the Guardian's probe when the site
//! has a domain claim ([`ProbeOptions`](super::ProbeOptions)), the weekly digest when the owner
//! has a channel, the hosting job's upkeep check when the site has an advisory or certificate
//! source, and — with a model — the back office ([`super::back_office`]): the Steward leading
//! the team, and each agent's jobs on their signals and schedules, under the owner's
//! standing rules in `data/kernel.db`. Agent sessions are kept in `data/kernel/sessions.db`.
//! Each site's jobs are bound through a `CharterKit`, which also runs the KPI tracker as the
//! kernel's `kernel.kpi` process, hourly. A job whose process is not scheduled is a warning in
//! the kernel's inventory, logged at start and at `GET /api/kernel/inventory`, with the
//! reason: no channel, no probe options or domain claim, no advisory or certificate source,
//! or no model.
//!
//! Every store of the kernel is in `data/kernel.db` beside the runs ([`SiteStores`]) — the
//! process table, schedules, spend ledger, the kill switch and the control plane's settings
//! among them — and the kernel refuses to start with one in memory
//! (`KernelBuilder::require_durable`). It records into the site's audit trail,
//! `data/audit.jsonl`; an agency's kernel records into the agency's own `data/audit.jsonl`.

use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use adk_core::audit_log::AuditTrail;
use adk_core::{AdkError, Result};
use adk_kernel::control::ControlPlaneConfig;
use adk_kernel::pairing::{IssuedCode, Pairing};
use adk_kernel::{Kernel, KernelConfig};
use serde::Deserialize;
use tokio::task::JoinHandle;

use super::agency::{Agency, AgencyRouter};
use super::kernel::SiteStores;
use super::project::ProjectPaths;
use super::site::{RunningSite, ServedWorkflows, SiteOptions};
use crate::onboarding::SetupGate;
use crate::team::SiteTeam;
use crate::workflows::SiteWorkflows;

fn config(error: impl std::fmt::Display) -> AdkError {
    AdkError::config(error.to_string())
}

/// How to serve a site.
#[derive(Debug, Clone)]
pub struct ServeOptions {
    /// The site project.
    pub dir: PathBuf,
    /// Where the edge listens.
    pub edge: SocketAddr,
    /// Where the kernel's control plane listens; loopback unless a token or SSO guards it.
    pub kernel: SocketAddr,
    /// The owner, the edge's URL, the channels, drivers and schedules.
    pub site: SiteOptions,
}

impl ServeOptions {
    /// The site at `dir` for `owner`, the edge on `127.0.0.1:8080` and the kernel on
    /// `127.0.0.1:7340`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_website::deploy::ServeOptions;
    ///
    /// let options = ServeOptions::new("bakery", "amina");
    /// assert_eq!(options.edge.port(), 8080);
    /// assert_eq!(options.site.owner, "amina");
    /// ```
    pub fn new(dir: impl Into<PathBuf>, owner: impl Into<String>) -> Self {
        Self {
            dir: dir.into(),
            edge: SocketAddr::from(([127, 0, 0, 1], 8080)),
            kernel: SocketAddr::from(([127, 0, 0, 1], 7340)),
            site: SiteOptions { owner: owner.into(), ..SiteOptions::default() },
        }
    }
}

/// Served sites; dropping it stops the edge's listener and the sites' background work.
pub struct Served {
    /// Where the edge listens.
    pub edge_addr: SocketAddr,
    /// Where the kernel's control plane listens.
    pub kernel_addr: SocketAddr,
    /// The setup code, also in the logs, while no device is paired (A-I20).
    pub setup_code: Option<IssuedCode>,
    /// The sites served.
    pub sites: Vec<RunningSite>,
    /// The kernel's durable stores.
    pub stores: SiteStores,
    kernel: Kernel,
    edge_task: JoinHandle<()>,
    chat: Option<adk_kernel::channels::ChannelGateway>,
}

impl std::fmt::Debug for Served {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Served")
            .field("edge_addr", &self.edge_addr)
            .field("kernel_addr", &self.kernel_addr)
            .field("sites", &self.sites)
            .finish_non_exhaustive()
    }
}

impl Served {
    /// The kernel serving the sites.
    pub fn kernel(&self) -> &Kernel {
        &self.kernel
    }

    /// The site's chat gateway, when [`SiteOptions::chat`](super::SiteOptions::chat) is set
    /// (spec 3.5 §10 X12): every chat platform's messages pass through it.
    pub fn chat(&self) -> Option<&adk_kernel::channels::ChannelGateway> {
        self.chat.as_ref()
    }

    /// Stops the edge, the sites' background work and the kernel with its control plane.
    ///
    /// # Errors
    ///
    /// The kernel's error on shutdown.
    pub async fn shutdown(mut self) -> Result<()> {
        self.edge_task.abort();
        if let Some(chat) = &self.chat {
            chat.stop();
        }
        drop(std::mem::take(&mut self.sites));
        self.kernel.stop().await.map_err(config)
    }
}

impl Drop for Served {
    fn drop(&mut self) {
        self.edge_task.abort();
    }
}

/// Starts the kernel over `stores` with `sites`' workflows and triggers and device pairing on
/// its control plane, recording into `audit`, then the edge's listener serving `app`.
// `serve` and `serve_agency` differ in every argument; a struct for one private call adds
// nothing.
#[allow(clippy::too_many_arguments)]
async fn start(
    name: &str,
    home: PathBuf,
    stores: SiteStores,
    audit: AuditTrail,
    mut sites: Vec<RunningSite>,
    options: &ServeOptions,
    app: axum::Router,
    doorbell: Option<Arc<adk_kernel::channels::ChannelDoorbell>>,
) -> Result<Served> {
    let pairing = Pairing::new(stores.kernel.devices.clone());
    // `adk-rust pair accept` pairs a chat account that asked with `/pair` (spec 3.5 §10 X12).
    let pairing = match &options.site.channel_bindings {
        Some(bindings) => pairing.with_channel_bindings(bindings.clone()),
        None => pairing,
    };
    let paired = !pairing.devices().await.map_err(config)?.is_empty();
    let setup_code = if paired {
        None
    } else {
        Some(SetupGate::new(pairing.clone(), options.site.owner.clone()).issue().await?)
    };
    let control = ControlPlaneConfig::default()
        .without_socket()
        .with_http(options.kernel)
        .with_pairing(pairing);
    let mut site_workflows = SiteWorkflows::new();
    let mut triggers = Vec::new();
    let sessions: Option<Arc<dyn adk_session::SessionService>> = match &options.site.model {
        Some(_) => {
            std::fs::create_dir_all(&home).map_err(config)?;
            let url = format!("sqlite://{}?mode=rwc", home.join("sessions.db").display());
            let sessions = adk_session::SqliteSessionService::new(&url).await?;
            sessions.migrate().await?;
            Some(Arc::new(sessions))
        }
        None => None,
    };
    let mut kernel_config = KernelConfig::default()
        .with_app_name(name)
        .with_home(home.clone())
        .with_control(control)
        .with_governance(stores.governance.clone());
    // The site's proactive runs wait out the owner's quiet hours (spec 3.5 §7.21, §5 S10); an
    // agency's sites may keep different hours, and the kernel holds one set.
    if let [site] = sites.as_slice()
        && let Some(quiet) = crate::alarms::quiet_hours(&site.reporting)
    {
        kernel_config = kernel_config.with_quiet_hours(quiet);
    }
    // Every stateful store is durable in `data/kernel.db`: the process table, which the wake
    // router finds sleepers through, so a restart keeps every wake set, admission deferral
    // and daily total; the kill switch, mirrored; the control plane's settings. A store
    // that would be kept in memory refuses the start.
    let mut builder = Kernel::builder()
        .config(kernel_config)
        .stores(&stores.kernel)
        .require_durable()
        .audit(audit)
        .approval_service(stores.approvals.clone())
        .partitions(stores.partitions.clone());
    for site in &sites {
        use adk_kernel::charter::{CharterKit, SiteProcessKind};
        triggers.extend(site.triggers().iter().cloned());
        let team = SiteTeam::new(site.site.clone(), &site.template);
        let partition = site.partitions.back_office().id.clone();
        // The runs of a site's processes are its own app's, so its agents act as
        // `urn:adk:agent:{site}/{agent}`, an agency's sites included.
        let app = site.site.name.as_str();
        let mut runs: Vec<SiteProcessKind> = Vec::new();
        // The kinds this site does not run, each with its reason, for the startup log.
        let mut skipped: Vec<(SiteProcessKind, &str)> = Vec::new();
        if let Some(target) = site.digest_target(&options.site) {
            site_workflows = site_workflows.digest(site.site.id, target);
            runs.push(SiteProcessKind::Digest);
        } else {
            skipped.push((SiteProcessKind::Digest, "the owner has no channel"));
        }
        // The Guardian's probe every five minutes, when the site has a domain claim (spec
        // 3.5 §7.20, A-I9).
        if let Some(probe) = &site.guardian {
            site_workflows = site_workflows.probe(site.site.id, probe.clone());
            runs.push(SiteProcessKind::Probe);
        } else {
            skipped.push((SiteProcessKind::Probe, "no probe options or no domain claim"));
        }
        // A push to the content repository is a run of the site's own (spec 3.5 §7.8.2).
        if let Some(entry) = &site.repository {
            site_workflows = site_workflows.repository(site.site.id, entry.clone());
        }
        // The weekly playbook learning, decided by the owner (spec 3.5 §7.15).
        site_workflows = site_workflows.learning(site.site.id, site.learning.clone());
        if let Some(check) = site.upkeep(&stores, &options.site) {
            site_workflows = site_workflows.watch(site.site.id, check);
            runs.push(SiteProcessKind::Watch);
        } else {
            skipped.push((SiteProcessKind::Watch, "no advisory or certificate source"));
        }
        if let (Some(model), Some(sessions)) = (&options.site.model, &sessions) {
            let team = site.back_office(&stores, model.clone()).await?;
            let hosted;
            (builder, hosted) =
                super::back_office::host(builder, &team, &partition, app, sessions.clone())?;
            tracing::info!(site = app, agents = ?hosted, "the back office is hosted");
            runs.extend([
                SiteProcessKind::Content,
                SiteProcessKind::Reviews,
                SiteProcessKind::Outreach,
                SiteProcessKind::Support,
            ]);
        } else {
            skipped.extend(
                [
                    SiteProcessKind::Content,
                    SiteProcessKind::Reviews,
                    SiteProcessKind::Outreach,
                    SiteProcessKind::Support,
                ]
                .map(|kind| (kind, "the site has no model")),
            );
        }
        // The charter kit binds each job's process to its target, runs the KPI tracker as
        // the kernel's `kernel.kpi` process and files the edge's signals in the back office;
        // a kind the site does not run is named in the kernel's inventory with the reason.
        let mut kit = CharterKit::new(
            site.site.id,
            app,
            partition.clone(),
            stores.kernel.charter.signals.clone(),
        )
        .with_user(format!("site:{}", site.site.name))
        .tracker(site.tracker().clone());
        for process in crate::jobs::processes(&team, &site.template.jobs()) {
            kit = match skipped.iter().find(|(kind, _)| *kind == process.kind) {
                Some((_, reason)) => kit.skip(process.kind, *reason),
                None if runs.contains(&process.kind) => {
                    kit.job(process.kind, process.target, process.input)
                }
                None => kit,
            };
        }
        builder = kit.install(builder);
    }
    // The kernel fires each standing trigger's cron expression through one watermarked
    // source of its own, its cursor in the project's store.
    for trigger in triggers {
        builder = builder.trigger(trigger);
    }
    let workflows =
        ServedWorkflows::new(site_workflows, sites.iter().map(|site| site.experiments.clone()));
    let kernel = builder.workflow(Arc::new(workflows)).build().map_err(config)?;
    // The edge's signals wake the back office's processes (spec 3.5 §7.10, §7.20), filed in
    // each site's trail.
    for site in &sites {
        let partition = site.partitions.back_office().id.clone();
        let sink = kernel.signal_sink(partition).map_err(config)?;
        site.signals.attach(sink.with_audit(site.audit.clone()));
    }
    // A guardrail breached before the restart pauses its jobs again before the kernel takes
    // their work (A-I15).
    for site in &sites {
        let paused = site.tracker().restore_pauses().await.map_err(config)?;
        if !paused.is_empty() {
            tracing::info!(site = %site.site.name, jobs = ?paused, "jobs paused by a breached guardrail");
        }
    }
    kernel.start().await.map_err(config)?;
    let kernel_addr = kernel
        .control_addr()
        .await
        .ok_or_else(|| config("the kernel's control plane is not listening"))?;
    let idle = options.site.idle.unwrap_or(Duration::from_secs(60));
    for site in &mut sites {
        site.start(idle);
    }
    // The site's chat: its gateway on the kernel, and the adapters' routes on the edge.
    let (mut app, mut chat) = (app, None);
    if let (Some(adapters), [site]) = (&options.site.chat, sites.as_slice()) {
        let bindings: Arc<dyn adk_kernel::channels::ChannelBindingStore> =
            match &options.site.channel_bindings {
                Some(bindings) => bindings.clone(),
                None => stores.kernel.channel_bindings.clone(),
            };
        let (gateway, routes) =
            super::chat::start(adapters, &kernel, site, bindings, doorbell, sessions.clone())
                .await?;
        app = app.merge(routes);
        chat = Some(gateway);
    }
    let listener = tokio::net::TcpListener::bind(options.edge).await.map_err(config)?;
    let edge_addr = listener.local_addr().map_err(config)?;
    // The peer address keys anonymous callers' rate limits and conversations (spec 3.5 §7.3).
    let service = app.into_make_service_with_connect_info::<SocketAddr>();
    let edge_task = tokio::spawn(async move {
        if let Err(error) = axum::serve(listener, service).await {
            tracing::error!(error = %error, "the edge stopped");
        }
    });
    tracing::info!(edge = %edge_addr, kernel = %kernel_addr, sites = sites.len(), "serving");
    Ok(Served { edge_addr, kernel_addr, setup_code, sites, stores, kernel, edge_task, chat })
}

/// Serves the site at `options.dir` (spec 3.5 §7.17, §7.22): issues the setup code into the
/// logs while no device is paired, starts the kernel over its durable stores with device
/// pairing on its control plane, and serves the edge with the release behind it.
///
/// # Errors
///
/// A project that does not load, a store that does not open, or a listener that does not
/// bind.
pub async fn serve(mut options: ServeOptions) -> Result<Served> {
    let paths = ProjectPaths::new(&options.dir);
    std::fs::create_dir_all(paths.data()).map_err(config)?;
    // The site's chat gateway hears each decision the approval service takes (spec 3.5 §10
    // X12).
    let doorbell =
        options.site.chat.is_some().then(|| Arc::new(adk_kernel::channels::ChannelDoorbell::new()));
    let notifiers = doorbell.iter().map(|bell| bell.clone() as Arc<dyn adk_core::ApprovalNotifier>);
    let stores =
        SiteStores::open_with(&paths.database("kernel"), &options.site.owner, notifiers.collect())
            .await?;
    if options.site.chat.is_some() && options.site.channel_bindings.is_none() {
        options.site.channel_bindings = Some(stores.kernel.channel_bindings.clone());
    }
    let site = RunningSite::open(paths.clone(), &stores, &options.site).await?;
    site.partitions.install(&stores.partitions, adk_core::audit_log::AuditActor::default()).await?;
    let app = site.router.clone();
    let name = site.site.name.clone();
    let audit = site.audit.clone();
    start(&name, paths.data().join("kernel"), stores, audit, vec![site], &options, app, doorbell)
        .await
}

/// An agency's file: the sites it runs, each a project directory relative to the file
/// (spec 3.5 §7.22).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct AgencyFile {
    /// The sites.
    #[serde(rename = "site")]
    pub sites: Vec<AgencySite>,
}

/// One site of an agency.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct AgencySite {
    /// The domain it is served at.
    pub domain: String,
    /// Its project directory, relative to the agency file.
    pub dir: PathBuf,
}

/// Serves every site `file` lists from one harness (spec 3.5 §7.22, A-I22): one kernel whose
/// durable table holds each site's isolated partitions, and one edge listener that sends a
/// request only to the site its host names. `options.dir` is ignored.
///
/// # Errors
///
/// An unreadable file, a duplicate site, or [`serve`]'s errors.
pub async fn serve_agency(file: &Path, options: ServeOptions) -> Result<Served> {
    let text = tokio::fs::read_to_string(file).await.map_err(config)?;
    let listed: AgencyFile = toml::from_str(&text).map_err(config)?;
    let root = file.parent().unwrap_or(Path::new("."));
    let agency_paths = ProjectPaths::new(root);
    std::fs::create_dir_all(agency_paths.data()).map_err(config)?;
    let stores = SiteStores::open(&agency_paths.database("kernel"), &options.site.owner).await?;
    // The agency's kernel serves every site, so it records into the agency's own trail.
    let audit = AuditTrail::new(Arc::new(
        adk_core::audit_log::FileAuditRecorder::open(agency_paths.data().join("audit.jsonl"))?
            .with_log_id("agency"),
    ));
    let mut agency = Agency::new(stores.partitions.clone());
    let (mut edges, mut sites) = (std::collections::BTreeMap::new(), Vec::new());
    for listed in &listed.sites {
        let paths = ProjectPaths::new(root.join(&listed.dir));
        let site = RunningSite::open(paths, &stores, &options.site).await?;
        agency.add(site.site.clone()).await?;
        if site.domain != listed.domain {
            tracing::warn!(listed = %listed.domain, project = %site.domain, "the agency file and the project name different domains");
        }
        edges.insert(listed.domain.to_ascii_lowercase(), site.router.clone());
        sites.push(site);
    }
    let app = AgencyRouter::new(edges).router();
    if options.site.chat.is_some() {
        tracing::warn!(
            "an agency's sites run no chat gateway: serve each site on its own for chat"
        );
    }
    start("agency", agency_paths.data().join("kernel"), stores, audit, sites, &options, app, None)
        .await
}