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
//! Quiet time, the daily spending limit and the mode while the kernel runs (spec 3.3 §10
//! K3, §8.1), per area: a partition, or `default` on a kernel without partitions.
//!
//! | Route (under `/api`) | Caller | Does |
//! |---|---|---|
//! | `GET /kernel/settings` | operator, or a device with `read` | Every area's settings in force, the primary agent's area marked `primary` ([`SettingsResponse`]) |
//! | `GET /kernel/settings/{partition}` | operator, or a device with `read` | One area's ([`AreaResponse`]) |
//! | `POST /kernel/settings/{partition}/quiet-time` | the person at the kernel or the home | Sets or turns off quiet time ([`QuietTimeRequest`]) |
//! | `POST /kernel/settings/{partition}/spending-limit` | the person at the kernel or the home | Sets or removes the daily spending limit ([`SpendingLimitRequest`]) |
//! | `POST /kernel/settings/{partition}/mode` | the person at the kernel or the home | Sets how much the area's agents act alone ([`PartitionModeRequest`](super::api::PartitionModeRequest)) |
//!
//! A change is recorded as `kernel.control` (`settings.quiet_time`, `settings.spending_limit`,
//! `settings.mode`, with `from` and `to`) before it is stored in the
//! [`ControlStore`](super::ControlStore), and it replaces, for the area, the value the kernel
//! and the area's partition record started with. Admission reads it at every claim, so it
//! takes effect from the agents' next step: no proactive process of the area starts inside
//! the new quiet time, and no background process starts past the new limit. A quiet window
//! a workspace sets (`[kernel] quiet_hours` of its `.adk/harness.toml`) still holds beside
//! the person's and is listed as `alsoQuiet`.
//!
//! A change holds while the area still starts with the value it replaced
//! ([`AreaStart`](super::AreaStart)): a different start value — a later `adk-rust up --mode`,
//! `adk-rust partitions set-mode` followed by a restart, or quiet hours and a cap set at
//! start — is newer and takes over, and the change is dropped at the area's next change.
//!
//! | Mode change | When it applies |
//! |---|---|
//! | Stricter than the area's mode | At every agent's next step; it also holds the overnight tidy-up and the kernel's questions back |
//! | Looser | Refused while an agent of the area is working, so no turn changes its rules midway (spec 3.1 I14); a session whose mode was set on its own, or tightened by the harness, keeps its mode |
//! | To a mode the kernel refuses | Refused, saying why: `adk-rust up` refuses Go ahead beyond loopback without `--sandbox` |
//!
//! The changing routes need the `operator` scope and refuse a paired device's token: they
//! are for the person at the kernel's own terminal or socket, or signed in to the home, never
//! a phone's token alone or a chat channel, which reaches no kernel route.

use std::collections::BTreeSet;
use std::sync::Arc;

use axum::Json;
use axum::Router;
use axum::extract::{Path as UrlPath, State};
use axum::routing::{get, post};
use serde::{Deserialize, Serialize};
use serde_json::json;

use super::api::PartitionModeRequest;
use super::paths;
use super::routes::{Caller, ControlBackend, record};
use super::store::{AreaSettings, AreaStart, Change, ControlStore};
use crate::error::{KernelError, NOT_FOUND_CODE, Result};
use crate::kernel::Kernel;
use crate::partition::{Partition, PartitionId, PartitionMode};
use crate::sched::QuietHours;

type Backend = State<Arc<dyn ControlBackend>>;

/// One area's settings in force (spec 3.3 §10 K3), as the home's "What your agents can do"
/// shows them.
///
/// # Example
///
/// ```rust
/// use adk_kernel::control::api::AreaView;
///
/// let wire = serde_json::json!({
///     "partition": "default",
///     "primary": true,
///     "quietTime": [{ "start": "22:00", "end": "07:00" }],
///     "dailySpendingLimit": 5_000_000,
///     "spentToday": 1_840_000,
///     "mode": "ask"
/// });
/// let area: AreaView = serde_json::from_value(wire).unwrap();
/// assert_eq!(area.daily_spending_limit, Some(5_000_000));
/// assert!(area.primary, "the primary agent works in this area");
/// assert!(area.changed.is_none() && area.also_quiet.is_empty());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AreaView {
    /// The area.
    pub partition: PartitionId,
    /// Whether the primary agent works in this area: the partition the kernel's own target,
    /// [`KernelConfig::app_name`](crate::KernelConfig::app_name), is bound to, else `default`.
    /// The turns a terminal or the home sends run there, so they show this area's spend today
    /// and daily spending limit (spec 3.3 §10 K3, §13 Q7).
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub primary: bool,
    /// The person's quiet time: the window changed while the kernel runs, else those it
    /// started with. Empty is no quiet time.
    #[serde(default)]
    pub quiet_time: Vec<QuietHours>,
    /// Windows a workspace sets, which hold beside the person's.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub also_quiet: Vec<QuietHours>,
    /// The daily spending limit in micro-USD; `None` is no limit.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub daily_spending_limit: Option<u64>,
    /// Spent in the area today (UTC day), in micro-USD.
    #[serde(default)]
    pub spent_today: u64,
    /// How much the area's agents act alone; `None` when the kernel was not told the mode
    /// its agents started in.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mode: Option<PartitionMode>,
    /// What the person changed while the kernel ran, with when and by whom.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub changed: Option<AreaSettings>,
}

/// `GET /kernel/settings` answer (spec 3.3 §10 K3).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SettingsResponse {
    /// Every area, ordered by partition id.
    pub areas: Vec<AreaView>,
}

/// The answer of a settings read or change: the area after it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AreaResponse {
    /// The area.
    pub area: AreaView,
}

/// `POST /kernel/settings/{partition}/quiet-time` body (spec 3.3 §10 K3): the new window,
/// or `null` for no quiet time.
///
/// # Example
///
/// ```rust
/// use adk_kernel::control::api::QuietTimeRequest;
///
/// let body: QuietTimeRequest = serde_json::from_str(r#"{"window":{"start":"23:00","end":"07:00"}}"#).unwrap();
/// assert!(body.window.is_some());
/// let off: QuietTimeRequest = serde_json::from_str(r#"{"window":null}"#).unwrap();
/// assert!(off.window.is_none());
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QuietTimeRequest {
    /// The window; `None` turns quiet time off.
    pub window: Option<QuietHours>,
}

/// `POST /kernel/settings/{partition}/spending-limit` body (spec 3.3 §10 K3): the new limit
/// in micro-USD, or `null` for no limit.
///
/// # Example
///
/// ```rust
/// use adk_kernel::control::api::SpendingLimitRequest;
///
/// let body = SpendingLimitRequest { micro_usd: Some(5_000_000) };
/// assert_eq!(serde_json::to_value(&body).unwrap()["microUsd"], 5_000_000);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SpendingLimitRequest {
    /// The limit per UTC day; `None` removes it.
    pub micro_usd: Option<u64>,
}

/// What an area started with, before any change: the values a change replaces.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Start {
    /// The kernel's quiet hours and the partition record's own.
    pub(crate) quiet: Vec<QuietHours>,
    /// The workspace's window, which a change never replaces.
    pub(crate) beside: Vec<QuietHours>,
    /// The lower of the kernel's daily spend cap and the partition record's.
    pub(crate) cap: Option<u64>,
    /// The mode the area's agents started in, else the partition record's.
    pub(crate) mode: Option<PartitionMode>,
}

impl Start {
    /// The start values a change records it replaced.
    pub(crate) fn area(&self) -> AreaStart {
        AreaStart::new(self.quiet.clone(), self.cap, self.mode)
    }
}

/// How strict `mode` is: 0 for Go ahead, up to 3 for Just look.
fn strictness(mode: PartitionMode) -> u8 {
    match mode {
        PartitionMode::Auto => 0,
        PartitionMode::Ask => 1,
        PartitionMode::Plan => 2,
        PartitionMode::ReadOnly => 3,
    }
}

/// The stricter of `a` and `b`.
pub(crate) fn stricter(a: PartitionMode, b: PartitionMode) -> PartitionMode {
    if strictness(b) > strictness(a) { b } else { a }
}

/// The quiet windows in force in an area: the changed window in place of `kernel` and the
/// partition record's, while the area still starts with those, then the workspace's
/// `beside` (spec 3.3 §10 K3).
pub(crate) fn quiet_windows(
    kernel: Option<QuietHours>,
    beside: Option<QuietHours>,
    record: Option<&Partition>,
    changed: Option<&AreaSettings>,
) -> Vec<QuietHours> {
    let start: Vec<QuietHours> =
        kernel.into_iter().chain(record.and_then(|record| record.quiet_hours)).collect();
    let own = match changed.and_then(|settings| settings.quiet_time_over(&start)) {
        Some(change) => change.value.into_iter().collect(),
        None => start,
    };
    own.into_iter().chain(beside).collect()
}

/// The quiet windows that were in force in an area at `at`, as its changes record them
/// (spec 3.3 §5.1): the person's quiet time set at or before `at`, else the start windows,
/// then the workspace's.
#[cfg(feature = "proc")]
pub(crate) fn quiet_windows_at(
    start: &Start,
    changed: Option<&AreaSettings>,
    at: chrono::DateTime<chrono::Utc>,
) -> Vec<QuietHours> {
    let current = changed.and_then(|settings| settings.quiet_time.as_ref());
    let stale = changed.is_some_and(|settings| {
        settings.quiet_time.is_some() && settings.quiet_time_over(&start.quiet).is_none()
    });
    let own = match changed.and_then(|settings| settings.quiet_time_at(at)) {
        // A change the area's start has replaced since held until a restart the record does
        // not date: the start windows judge what came after it.
        Some(change) if stale && Some(change) == current => start.quiet.clone(),
        Some(change) => change.value.into_iter().collect(),
        None => start.quiet.clone(),
    };
    own.into_iter().chain(start.beside.iter().copied()).collect()
}

/// The daily spend cap in force in an area: the changed limit in place of `start`, the
/// lower of the kernel's and the partition record's, while the area still starts with it
/// (spec 3.3 §10 K3).
pub(crate) fn spend_cap(start: Option<u64>, changed: Option<&AreaSettings>) -> Option<u64> {
    match changed.and_then(|settings| settings.daily_spending_limit_over(start)) {
        Some(change) => change.value,
        None => start,
    }
}

/// The mode the person changed for an area, while its agents still start in the mode it
/// replaced and the kernel does not refuse it (spec 3.3 §10 K3).
pub(crate) fn changed_mode(
    start: Option<PartitionMode>,
    changed: Option<&AreaSettings>,
    control: &super::ControlPlaneConfig,
) -> Option<PartitionMode> {
    changed
        .and_then(|settings| settings.mode_over(start))
        .map(|change| change.value)
        .filter(|mode| control.refused_mode(*mode).is_none())
}

/// The changes of `settings` the area still starts with the values of, as the person sees
/// them: `None` when none is left.
fn in_force(settings: AreaSettings, start: &AreaStart) -> Option<AreaSettings> {
    let mut shown = AreaSettings::new(settings.partition.clone());
    shown.quiet_time = settings.quiet_time_over(&start.quiet_time).cloned();
    shown.daily_spending_limit =
        settings.daily_spending_limit_over(start.daily_spending_limit).cloned();
    shown.mode = settings.mode_over(start.mode).cloned();
    let any =
        shown.quiet_time.is_some() || shown.daily_spending_limit.is_some() || shown.mode.is_some();
    any.then_some(shown)
}

/// The routes, relative to `/api`, over the backend in their state.
pub(super) fn routes() -> Router<Arc<dyn ControlBackend>> {
    Router::new()
        .route(paths::SETTINGS, get(list))
        .route(paths::AREA_SETTINGS, get(one))
        .route(paths::QUIET_TIME, post(set_quiet_time))
        .route(paths::SPENDING_LIMIT, post(set_spending_limit))
        .route(paths::AREA_MODE, post(set_mode))
}

/// The kernel behind `backend`, or a `Config` error naming why there is none.
fn kernel_of(backend: &dyn ControlBackend) -> Result<&Kernel> {
    backend.kernel().ok_or_else(|| KernelError::Config {
        reason: "the settings routes read a kernel's admission settings; serve them from a \
                 Kernel"
            .into(),
    })
}

/// Refuses a change from anyone but the person at the kernel or signed in to the home: the
/// `operator` scope, and no paired device's token (spec 3.3 §10 K3, S-I7).
pub(super) fn require_owner(caller: &Caller, action: &str) -> Result<()> {
    caller.require_operator(action)?;
    if caller.context.metadata.contains_key(crate::pairing::DEVICE_ID_METADATA) {
        return Err(crate::pairing::forbidden(format!(
            "a paired device may not {action}: change it in the home or on the kernel's own \
             terminal"
        )));
    }
    Ok(())
}

/// The areas of `kernel`: `default` when the partition table is empty or names it, every
/// partition in the table, and every area with a change.
async fn areas(kernel: &Kernel, store: &dyn ControlStore) -> Result<Vec<PartitionId>> {
    let partitions = kernel.partitions().list().await?;
    let mut ids: BTreeSet<PartitionId> = partitions.into_iter().map(|p| p.id).collect();
    if ids.is_empty() {
        ids.insert(PartitionId::default());
    }
    ids.extend(store.list_area_settings().await?.into_iter().map(|s| s.partition));
    Ok(ids.into_iter().collect())
}

/// `id` when it names an area of `kernel`, else a `404` naming the areas there are.
async fn known_area(kernel: &Kernel, store: &dyn ControlStore, id: String) -> Result<PartitionId> {
    let id = PartitionId::new(id);
    let known = areas(kernel, store).await?;
    if known.contains(&id) || id.as_str() == PartitionId::DEFAULT {
        return Ok(id);
    }
    let names = known.iter().map(PartitionId::as_str).collect::<Vec<_>>().join(", ");
    Err(KernelError::Core(adk_core::AdkError::new(
        adk_core::ErrorComponent::Server,
        adk_core::ErrorCategory::NotFound,
        NOT_FOUND_CODE,
        format!("no area `{id}` on this kernel; its areas are {names}"),
    )))
}

/// What `partition` started with on `kernel`.
pub(crate) async fn start_of(kernel: &Kernel, partition: &PartitionId) -> Result<Start> {
    let config = kernel.config();
    let record = kernel.partitions().get(partition).await?;
    let quiet = config
        .quiet_hours
        .into_iter()
        .chain(record.as_ref().and_then(|record| record.quiet_hours))
        .collect();
    let cap = match &record {
        Some(record) => config.quotas.stricter(&record.quotas).daily_spend_cap,
        None => config.quotas.daily_spend_cap,
    };
    // The mode the area's harness started in governs it until the next start, whatever the
    // partition record says now.
    let mode = config
        .control
        .start_modes
        .get(partition)
        .copied()
        .or_else(|| record.as_ref().map(|record| record.default_mode));
    Ok(Start { quiet, beside: config.proactivity.quiet_hours.into_iter().collect(), cap, mode })
}

/// The settings in force in `partition` on `kernel`.
pub(crate) async fn view(
    kernel: &Kernel,
    store: &dyn ControlStore,
    partition: &PartitionId,
) -> Result<AreaView> {
    let start = start_of(kernel, partition).await?;
    let changed = store.area_settings(partition).await?;
    let quiet_time =
        match changed.as_ref().and_then(|settings| settings.quiet_time_over(&start.quiet)) {
            Some(change) => change.value.into_iter().collect(),
            None => start.quiet.clone(),
        };
    let daily_spending_limit = spend_cap(start.cap, changed.as_ref());
    let mode = changed_mode(start.mode, changed.as_ref(), &kernel.config().control);
    let changed = changed.and_then(|settings| in_force(settings, &start.area()));
    let sup = kernel.supervision();
    let today = sup.clock.now().date_naive();
    let scope = crate::stores::SpendScope::Partition(partition.clone());
    let primary = kernel.partition_of_target(&kernel.config().app_name);
    Ok(AreaView {
        partition: partition.clone(),
        primary: primary.unwrap_or(&PartitionId::default()) == partition,
        quiet_time,
        also_quiet: start.beside,
        daily_spending_limit,
        spent_today: sup.processes.spend(&scope, today, today).await?,
        mode: mode.or(start.mode),
        changed,
    })
}

impl Kernel {
    /// The settings in force in `partition` (spec 3.3 §10 K3), as
    /// `GET /kernel/settings/{partition}` answers them: quiet time, the daily spending limit
    /// with what was spent today, the mode, and whether the primary agent works there.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_kernel::{Kernel, PartitionId};
    ///
    /// # async fn example(kernel: &Kernel) -> adk_kernel::Result<()> {
    /// let area = kernel.area_settings(&PartitionId::default()).await?;
    /// let left = area.daily_spending_limit.map(|limit| limit.saturating_sub(area.spent_today));
    /// # let _ = left;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns a store error when the partition table, the control store or the spend totals
    /// cannot be read.
    pub async fn area_settings(&self, partition: &PartitionId) -> Result<AreaView> {
        let store = self.config().control.control_store.clone();
        view(self, store.as_ref(), partition).await
    }
}

async fn list(State(backend): Backend, caller: Caller) -> Result<Json<SettingsResponse>> {
    caller.require_operator("read the settings")?;
    let kernel = kernel_of(backend.as_ref())?;
    let store = kernel.config().control.control_store.clone();
    let mut views = Vec::new();
    for area in areas(kernel, store.as_ref()).await? {
        views.push(view(kernel, store.as_ref(), &area).await?);
    }
    Ok(Json(SettingsResponse { areas: views }))
}

async fn one(
    State(backend): Backend,
    caller: Caller,
    UrlPath(partition): UrlPath<String>,
) -> Result<Json<AreaResponse>> {
    caller.require_operator("read the settings")?;
    let kernel = kernel_of(backend.as_ref())?;
    let store = kernel.config().control.control_store.clone();
    let partition = known_area(kernel, store.as_ref(), partition).await?;
    Ok(Json(AreaResponse { area: view(kernel, store.as_ref(), &partition).await? }))
}

/// What a change of one setting does to an area's record: it sets the change, made by
/// `by` at `at`.
type Set = Box<dyn Fn(&mut AreaSettings, &str, chrono::DateTime<chrono::Utc>) + Send + Sync>;

/// A change of one setting.
struct Setting {
    /// The record's action, such as `settings.quiet_time`.
    action: &'static str,
    /// The admission label of the processes it releases, which meet it at their next step.
    readmit: Option<&'static str>,
    /// The setting's value in the area before.
    from: fn(&AreaView) -> serde_json::Value,
    /// Its value after.
    to: serde_json::Value,
    /// The mode it sets, which [`check_mode`] admits first.
    mode: Option<PartitionMode>,
    /// What it does to the area's record.
    set: Set,
}

/// Whether an agent of `partition` is working now: a process of the area, not one of the
/// kernel's own, is running.
async fn working(kernel: &Kernel, partition: &PartitionId) -> Result<bool> {
    let sup = kernel.supervision();
    let filter = adk_core::RunFilter::default()
        .app_name(kernel.config().app_name.clone())
        .states([adk_core::RunState::Running]);
    let running = sup.runs.list(filter).await?;
    for run in running.iter().filter(|run| !run.target.starts_with("kernel.")) {
        let area = sup.processes.get(&run.run_id).await?.map(|meta| meta.partition);
        if area.unwrap_or_default() == *partition {
            return Ok(true);
        }
    }
    Ok(false)
}

/// Refuses a mode the kernel refuses, and a looser mode than `before`, the area's, while an
/// agent of the area is working: a loosening applies from an agent's next turn, never midway
/// through one (spec 3.1 I14, spec 3.3 §10 K3).
async fn check_mode(
    kernel: &Kernel,
    partition: &PartitionId,
    before: Option<PartitionMode>,
    mode: PartitionMode,
) -> Result<()> {
    if let Some(reason) = kernel.config().control.refused_mode(mode) {
        return Err(KernelError::Config { reason: reason.to_string() });
    }
    let loosens = match before {
        Some(before) => strictness(mode) < strictness(before),
        None => mode != PartitionMode::ReadOnly,
    };
    if loosens && working(kernel, partition).await? {
        return Err(KernelError::Core(adk_core::AdkError::new(
            adk_core::ErrorComponent::Server,
            adk_core::ErrorCategory::Conflict,
            crate::error::INVALID_STATE_CODE,
            format!(
                "an agent in area `{partition}` is working: let its agents do more once it \
                 finishes, so no turn changes its rules midway"
            ),
        )));
    }
    Ok(())
}

/// Records the change with the setting's value before and after, stores it in the area's
/// record, after dropping the changes the area's start values have replaced, wakes the
/// processes admission held for it so they meet it at their next step, and answers with the
/// area after it.
async fn change(
    backend: &dyn ControlBackend,
    caller: &Caller,
    partition: String,
    setting: Setting,
) -> Result<Json<AreaResponse>> {
    require_owner(
        caller,
        &format!("change {}", setting.action.trim_start_matches("settings.").replace('_', " ")),
    )?;
    let kernel = kernel_of(backend)?;
    let store = kernel.config().control.control_store.clone();
    let partition = known_area(kernel, store.as_ref(), partition).await?;
    let before = view(kernel, store.as_ref(), &partition).await?;
    if let Some(mode) = setting.mode {
        check_mode(kernel, &partition, before.mode, mode).await?;
    }
    let extra = [("from", (setting.from)(&before)), ("to", setting.to.clone())];
    record(backend, caller, setting.action, Some(partition.as_str()), &extra).await?;
    let start = start_of(kernel, &partition).await?.area();
    let (by, at) = (caller.context.user_id.as_str(), kernel.config().clock.now());
    let set = &setting.set;
    store
        .update_area_settings(&partition, &|settings: &mut AreaSettings| {
            settings.settle(&start);
            set(settings, by, at);
        })
        .await?;
    if let Some(reason) = setting.readmit {
        let filter = crate::stores::ProcessFilter::default().partition(partition.clone());
        kernel.supervision().readmit(&filter, &[reason]).await;
    }
    Ok(Json(AreaResponse { area: view(kernel, store.as_ref(), &partition).await? }))
}

async fn set_quiet_time(
    State(backend): Backend,
    caller: Caller,
    UrlPath(partition): UrlPath<String>,
    Json(request): Json<QuietTimeRequest>,
) -> Result<Json<AreaResponse>> {
    let window = request.window;
    if let Some(window) = window
        && window.start == window.end
    {
        return Err(KernelError::Config {
            reason: "quiet time needs a start and an end that differ; send `null` to turn it off"
                .into(),
        });
    }
    let setting = Setting {
        action: "settings.quiet_time",
        readmit: Some("quiet_hours"),
        from: |before| json!(before.quiet_time),
        to: json!(window),
        mode: None,
        set: Box::new(move |settings, by, at| {
            settings.change_quiet_time(Change::new(window, by, at));
        }),
    };
    change(backend.as_ref(), &caller, partition, setting).await
}

async fn set_spending_limit(
    State(backend): Backend,
    caller: Caller,
    UrlPath(partition): UrlPath<String>,
    Json(request): Json<SpendingLimitRequest>,
) -> Result<Json<AreaResponse>> {
    let limit = request.micro_usd;
    let setting = Setting {
        action: "settings.spending_limit",
        readmit: Some("budget"),
        from: |before| json!(before.daily_spending_limit),
        to: json!(limit),
        mode: None,
        set: Box::new(move |settings, by, at| {
            settings.daily_spending_limit = Some(Change::new(limit, by, at));
        }),
    };
    change(backend.as_ref(), &caller, partition, setting).await
}

async fn set_mode(
    State(backend): Backend,
    caller: Caller,
    UrlPath(partition): UrlPath<String>,
    Json(request): Json<PartitionModeRequest>,
) -> Result<Json<AreaResponse>> {
    let mode = request.mode;
    let setting = Setting {
        action: "settings.mode",
        readmit: None,
        from: |before| json!(before.mode),
        to: json!(mode),
        mode: Some(mode),
        set: Box::new(move |settings, by, at| settings.mode = Some(Change::new(mode, by, at))),
    };
    change(backend.as_ref(), &caller, partition, setting).await
}

#[cfg(test)]
mod tests {
    use chrono::Utc;

    use super::*;

    fn window(text: &str) -> QuietHours {
        text.parse().unwrap()
    }

    #[test]
    fn a_changed_window_replaces_the_start_windows_and_keeps_the_workspace_one() {
        let kernel = Some(window("22:00-07:00"));
        let beside = Some(window("12:00-13:00"));
        let record = Partition::new("work", "work").with_quiet_hours(window("19:00-08:00"));
        assert_eq!(
            quiet_windows(kernel, beside, Some(&record), None),
            [window("22:00-07:00"), window("19:00-08:00"), window("12:00-13:00")]
        );
        let changed = AreaSettings::new("work").with_quiet_time(Change::new(
            Some(window("23:00-06:00")),
            "ana",
            Utc::now(),
        ));
        assert_eq!(
            quiet_windows(kernel, beside, Some(&record), Some(&changed)),
            [window("23:00-06:00"), window("12:00-13:00")]
        );
        let off = AreaSettings::new("work").with_quiet_time(Change::new(None, "ana", Utc::now()));
        assert_eq!(
            quiet_windows(kernel, beside, Some(&record), Some(&off)),
            [window("12:00-13:00")]
        );
    }

    /// A window kept in a named time zone keeps its local times when the clocks change,
    /// where a fixed offset moves it by an hour (spec 3.3 §10 K3).
    #[test]
    fn a_window_in_a_time_zone_follows_daylight_saving_time() {
        use chrono::TimeZone;

        let body: QuietTimeRequest = serde_json::from_value(json!({
            "window": { "start": "22:00", "end": "07:00", "timeZone": "Europe/London" }
        }))
        .unwrap();
        let zoned = body.window.unwrap();
        assert_eq!(serde_json::to_value(zoned).unwrap()["timeZone"], "Europe/London");
        // Saved in summer from a browser at UTC+1.
        let fixed = window("22:00-07:00").with_utc_offset_minutes(60);
        let summer = Utc.with_ymd_and_hms(2026, 7, 1, 21, 30, 0).unwrap();
        let winter = Utc.with_ymd_and_hms(2026, 12, 1, 21, 30, 0).unwrap();
        assert!(zoned.contains(summer) && fixed.contains(summer), "22:30 in London");
        assert!(!zoned.contains(winter), "21:30 in London: not quiet yet");
        assert!(fixed.contains(winter), "a fixed offset has moved the window by an hour");
        assert_eq!(
            zoned.release_at(Utc.with_ymd_and_hms(2026, 12, 2, 6, 0, 0).unwrap()),
            Utc.with_ymd_and_hms(2026, 12, 2, 7, 0, 0).unwrap()
        );
        let error = serde_json::from_value::<QuietTimeRequest>(json!({
            "window": { "start": "22:00", "end": "07:00", "timeZone": "Mars/Olympus" }
        }))
        .unwrap_err();
        assert!(error.to_string().contains("IANA"), "{error}");
    }

    #[test]
    fn a_changed_limit_replaces_the_start_cap() {
        assert_eq!(spend_cap(Some(5), None), Some(5));
        let raised = AreaSettings::new("default").with_daily_spending_limit(Change::new(
            Some(9),
            "ana",
            Utc::now(),
        ));
        assert_eq!(spend_cap(Some(5), Some(&raised)), Some(9));
        let removed = AreaSettings::new("default").with_daily_spending_limit(Change::new(
            None,
            "ana",
            Utc::now(),
        ));
        assert_eq!(spend_cap(Some(5), Some(&removed)), None);
        let mode_only = AreaSettings::new("default").with_mode(Change::new(
            PartitionMode::Auto,
            "ana",
            Utc::now(),
        ));
        assert_eq!(spend_cap(Some(5), Some(&mode_only)), Some(5), "another change keeps the cap");
    }
}