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
//! The agent table's own checks (spec 3.3 §5.7, §10 K7, S-I12): what the kernel knows
//! about a definition without its host — the risk floor, its area's limits, a suggester's
//! ceiling, and whether a change lets an agent do more.

use std::collections::BTreeSet;

use adk_core::ApprovalRisk;
use serde_json::Value;

use crate::agents::factory::{AgentAssessment, AgentCeiling};
use crate::agents::record::{AgentDefinition, AgentOrigin, FileAccess, ShellAccess, WebAccess};
use crate::drivers::EffectClass;
use crate::partition::{Partition, PartitionMode};

/// The packs that let an agent spend money (spec 3.3 §5.7): payments and computer use.
pub const MONEY_PACKS: [&str; 3] = ["payments", "computer-use", "computer_use"];

/// How much a mode lets an agent do without asking: 0 for `read_only` to 3 for `auto`.
pub(crate) fn looseness(mode: PartitionMode) -> u8 {
    match mode {
        PartitionMode::ReadOnly => 0,
        PartitionMode::Plan => 1,
        PartitionMode::Ask => 2,
        PartitionMode::Auto => 3,
    }
}

/// The plain words of a mode, as the surfaces show it.
pub(crate) fn mode_words(mode: PartitionMode) -> &'static str {
    match mode {
        PartitionMode::ReadOnly => "Just look",
        PartitionMode::Plan => "Suggest",
        PartitionMode::Ask => "Ask first",
        PartitionMode::Auto => "Go ahead",
    }
}

/// The risk of a driver whose tools are of `classes` (spec 3.3 §5.7): low for reads,
/// medium for writing locally and scheduling with others, high for sending and deleting and
/// for tools it does not describe, high and money for spending or publishing (K-I9).
fn class_risk(class: EffectClass) -> (ApprovalRisk, bool) {
    match class {
        EffectClass::Read => (ApprovalRisk::Low, false),
        EffectClass::WriteLocal | EffectClass::ScheduleWithOthers => (ApprovalRisk::Medium, false),
        EffectClass::PublishPublic | EffectClass::Financial => (ApprovalRisk::High, true),
        _ => (ApprovalRisk::High, false),
    }
}

fn class_words(class: EffectClass) -> &'static str {
    match class {
        EffectClass::Read => "read",
        EffectClass::WriteLocal => "change your data",
        EffectClass::ScheduleWithOthers => "schedule with others",
        EffectClass::SendExternal => "send to others",
        EffectClass::PublishPublic => "publish publicly",
        EffectClass::Financial => "move money",
        EffectClass::Delete => "delete",
        _ => "do things it does not describe",
    }
}

/// What the kernel itself knows of `definition`'s risk (spec 3.3 §5.7's risk table), the
/// floor under any factory's assessment: its mode, files, shell, web and money packs; each
/// of its drivers by the effect classes of its tools (`drivers`: id and classes; a driver
/// with no class listed counts as undescribed); and a suggestion from work the person did
/// not start at a screen, which is high (decision 20). Abilities name only the drivers; the
/// factory words the rest.
///
/// # Example
///
/// ```rust
/// use adk_core::ApprovalRisk;
/// use adk_kernel::agents::{AgentDefinition, AgentOrigin, AgentUses, kernel_assessment};
/// use adk_kernel::drivers::EffectClass;
/// use adk_kernel::PartitionMode;
///
/// let calendar = AgentDefinition::new("Planner", "work")
///     .with_mode(PartitionMode::Plan)
///     .with_uses(AgentUses::default().with_driver("calendar"));
/// let low = kernel_assessment(&calendar, &AgentOrigin::ByHand, &[("calendar".into(), vec![EffectClass::Read])]);
/// assert_eq!(low.risk, ApprovalRisk::Low);
/// let money = kernel_assessment(&calendar, &AgentOrigin::ByHand, &[("calendar".into(), vec![EffectClass::Financial])]);
/// assert_eq!((money.risk, money.money), (ApprovalRisk::High, true));
/// ```
pub fn kernel_assessment(
    definition: &AgentDefinition,
    origin: &AgentOrigin,
    drivers: &[(String, Vec<EffectClass>)],
) -> AgentAssessment {
    let uses = &definition.uses;
    let mut risk = match definition.mode {
        PartitionMode::ReadOnly | PartitionMode::Plan => ApprovalRisk::Low,
        PartitionMode::Ask => ApprovalRisk::Medium,
        PartitionMode::Auto => ApprovalRisk::High,
    };
    if uses.files == FileAccess::ReadWrite || uses.web == WebAccess::Any {
        risk = risk.max(ApprovalRisk::Medium);
    }
    risk = risk.max(match uses.shell {
        ShellAccess::None => ApprovalRisk::Low,
        ShellAccess::Sandboxed => ApprovalRisk::Medium,
        ShellAccess::Host => ApprovalRisk::High,
    });
    if matches!(origin, AgentOrigin::Suggested { unattended: true, .. }) {
        risk = ApprovalRisk::High;
    }
    let mut assessment = AgentAssessment::new(risk);
    if uses.packs.iter().any(|pack| MONEY_PACKS.contains(&pack.as_str())) {
        assessment = assessment.with_money();
    }
    for driver in &uses.drivers {
        let classes: BTreeSet<EffectClass> = drivers
            .iter()
            .find(|(id, _)| id == driver)
            .map(|(_, classes)| classes.iter().copied().collect())
            .filter(|classes: &BTreeSet<EffectClass>| !classes.is_empty())
            .unwrap_or_else(|| BTreeSet::from([EffectClass::Other]));
        for class in &classes {
            let (class_risk, money) = class_risk(*class);
            assessment.risk = assessment.risk.max(class_risk);
            if money {
                assessment = assessment.with_money();
            }
        }
        let words: Vec<&str> = classes.iter().map(|class| class_words(*class)).collect();
        assessment
            .abilities
            .push(format!("It can use the {driver} driver, which can {}.", join_words(&words)));
    }
    assessment
}

fn join_words(words: &[&str]) -> String {
    match words {
        [] => String::new(),
        [one] => (*one).to_string(),
        [rest @ .., last] => format!("{} and {last}", rest.join(", ")),
    }
}

/// Why `definition` exceeds its area (spec 3.3 §5.7): a mode looser than the area's, a
/// daily limit above the area's, or a driver not mounted for it. Empty when it fits.
///
/// # Example
///
/// ```rust
/// use adk_kernel::agents::{AgentDefinition, AgentUses, area_limits};
/// use adk_kernel::{Partition, PartitionMode};
///
/// let home = Partition::new("home", "home").with_mode(PartitionMode::Ask);
/// let ok = AgentDefinition::new("Finance", "home").with_mode(PartitionMode::Plan);
/// assert!(area_limits(&ok, &home).is_empty());
/// let loose = ok.clone().with_mode(PartitionMode::Auto).with_uses(AgentUses::default().with_driver("mail"));
/// assert_eq!(area_limits(&loose, &home).len(), 2);
/// ```
pub fn area_limits(definition: &AgentDefinition, area: &Partition) -> Vec<String> {
    let mut over = Vec::new();
    if looseness(definition.mode) > looseness(area.default_mode) {
        over.push(format!(
            "it would {} where its area asks for {} at most",
            mode_words(definition.mode).to_lowercase(),
            mode_words(area.default_mode).to_lowercase()
        ));
    }
    if let Some(cap) = area.quotas.daily_spend_cap {
        match definition.daily_limit_micro_usd {
            Some(limit) if limit <= cap => {}
            Some(limit) => over.push(format!(
                "its daily limit of {} is above its area's {}",
                dollars(limit),
                dollars(cap)
            )),
            None => {}
        }
    }
    // Mounting a driver is its own approval (K-I13), never part of adding an agent.
    for driver in definition.uses.drivers.iter().filter(|driver| !area.drivers.contains(driver)) {
        over.push(format!("the {driver} driver is not mounted for its area; mount it there first"));
    }
    over
}

/// Micro-USD as the person reads money: `$2.50`.
fn dollars(micro_usd: u64) -> String {
    let cents = micro_usd.saturating_add(5_000) / 10_000;
    format!("${}.{:02}", cents / 100, cents % 100)
}

/// Why a suggested `definition` exceeds its suggester (spec 3.3 S-I12): what is over the
/// suggester's `ceiling` — its mode, uses and daily spending limit — and what a suggestion
/// never carries — a shell on this computer, payments or computer use. Empty when it fits.
///
/// # Example
///
/// ```rust
/// use adk_kernel::agents::{AgentCeiling, AgentDefinition, AgentUses, ShellAccess, over_ceiling};
/// use adk_kernel::PartitionMode;
///
/// let ceiling = AgentCeiling::new(PartitionMode::Auto, AgentUses::default().with_shell(ShellAccess::Host));
/// let host = AgentDefinition::new("Ops", "default").with_uses(AgentUses::default().with_shell(ShellAccess::Host));
/// assert_eq!(over_ceiling(&host, &ceiling), ["a shell on this computer"]);
///
/// let capped = AgentCeiling::new(PartitionMode::Auto, AgentUses::default()).with_daily_limit(2_000_000);
/// let unlimited = AgentDefinition::new("Ops", "default");
/// assert_eq!(over_ceiling(&unlimited, &capped), ["spending with no daily limit, where it may spend $2.00"]);
/// ```
pub fn over_ceiling(definition: &AgentDefinition, ceiling: &AgentCeiling) -> Vec<String> {
    let mut over = Vec::new();
    if looseness(definition.mode) > looseness(ceiling.mode) {
        over.push(format!(
            "{} where it may only {}",
            mode_words(definition.mode).to_lowercase(),
            mode_words(ceiling.mode).to_lowercase()
        ));
    }
    // Spending is at most the suggester's, so a suggestion under a limit carries one too
    // (spec 3.3 S-I12).
    if let Some(cap) = ceiling.daily_limit_micro_usd {
        match definition.daily_limit_micro_usd {
            Some(limit) if limit <= cap => {}
            Some(limit) => over.push(format!(
                "a daily spending limit of {}, above its {}",
                dollars(limit),
                dollars(cap)
            )),
            None => over
                .push(format!("spending with no daily limit, where it may spend {}", dollars(cap))),
        }
    }
    over.extend(definition.uses.over(&ceiling.uses));
    let host = "a shell on this computer".to_string();
    if definition.uses.shell == ShellAccess::Host && !over.contains(&host) {
        over.push(host);
    }
    for pack in definition.uses.packs.iter().filter(|pack| MONEY_PACKS.contains(&pack.as_str())) {
        let words = format!("the {pack} pack");
        if !over.contains(&words) {
            over.push(words);
        }
    }
    over
}

/// The definition fields `now` changes from `was`, by their wire names, sorted.
///
/// # Example
///
/// ```rust
/// use adk_kernel::agents::{AgentDefinition, changed_fields};
/// use adk_kernel::PartitionMode;
///
/// let was = AgentDefinition::new("Finance", "default");
/// let now = was.clone().with_mode(PartitionMode::Plan);
/// let mut renamed = now.clone();
/// renamed.display_name = "Bills".into();
/// assert_eq!(changed_fields(&was, &renamed), ["displayName", "mode"]);
/// ```
pub fn changed_fields(was: &AgentDefinition, now: &AgentDefinition) -> Vec<String> {
    let (Ok(Value::Object(old)), Ok(Value::Object(new))) = (
        serde_json::to_value(was.clone().normalized()),
        serde_json::to_value(now.clone().normalized()),
    ) else {
        return Vec::new();
    };
    old.keys()
        .chain(new.keys())
        .filter(|key| old.get(*key) != new.get(*key))
        .cloned()
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect()
}

/// What `now` lets an agent do that `was` did not (spec 3.3 §5.7): a looser mode, more
/// uses, a higher or no daily limit. Empty when the change only makes it do less or the
/// same; its name, colour, line, instructions and model never count.
///
/// # Example
///
/// ```rust
/// use adk_kernel::agents::{AgentDefinition, AgentUses, FileAccess, widened};
/// use adk_kernel::PartitionMode;
///
/// let was = AgentDefinition::new("Finance", "default").with_mode(PartitionMode::Ask);
/// assert!(widened(&was, &was.clone().with_mode(PartitionMode::Plan)).is_empty());
/// let more = was.clone().with_uses(AgentUses::default().with_files(FileAccess::Read));
/// assert_eq!(widened(&was, &more), ["reading files"]);
/// ```
pub fn widened(was: &AgentDefinition, now: &AgentDefinition) -> Vec<String> {
    let mut more = Vec::new();
    if looseness(now.mode) > looseness(was.mode) {
        more.push(format!(
            "{} instead of {}",
            mode_words(now.mode).to_lowercase(),
            mode_words(was.mode).to_lowercase()
        ));
    }
    more.extend(now.uses.over(&was.uses));
    match (was.daily_limit_micro_usd, now.daily_limit_micro_usd) {
        (Some(_), None) => more.push("no daily limit of its own".to_string()),
        (Some(old), Some(new)) if new > old => {
            more.push(format!("a daily limit of {new} micro-USD instead of {old}"));
        }
        _ => {}
    }
    more
}

#[cfg(test)]
mod tests {
    use adk_core::AgentId;

    use super::*;
    use crate::agents::record::AgentUses;

    fn definition(mode: PartitionMode, uses: AgentUses) -> AgentDefinition {
        AgentDefinition::new("Finance", "default").with_mode(mode).with_uses(uses)
    }

    fn risk(mode: PartitionMode, uses: AgentUses) -> (ApprovalRisk, bool) {
        let assessment = kernel_assessment(&definition(mode, uses), &AgentOrigin::ByHand, &[]);
        (assessment.risk, assessment.money)
    }

    /// Wave 3 finding 25: what an area refuses reads as plain words, in dollars, with no
    /// internal identifier.
    #[test]
    fn area_refusals_are_plain_words_in_dollars() {
        let mut area = Partition::new("home", "home").with_mode(PartitionMode::Ask);
        area.quotas.daily_spend_cap = Some(2_000_000);
        let definition = definition(PartitionMode::Ask, AgentUses::default().with_driver("mail"))
            .with_daily_limit(5_000_000);
        let over = area_limits(&definition, &area);
        assert_eq!(
            over,
            [
                "its daily limit of $5.00 is above its area's $2.00",
                "the mail driver is not mounted for its area; mount it there first"
            ]
        );
        assert_eq!(dollars(2_499_999), "$2.50");
        assert_eq!(dollars(999_999), "$1.00");
    }

    #[test]
    fn the_risk_table_rows() {
        use ApprovalRisk::{High, Low, Medium};
        let none = AgentUses::default;
        // Low: Just look or Suggest; no shell; no web or listed sites.
        assert_eq!(risk(PartitionMode::ReadOnly, none()), (Low, false));
        assert_eq!(
            risk(
                PartitionMode::Plan,
                none().with_files(FileAccess::Read).with_web(WebAccess::listed(["a.io"]))
            ),
            (Low, false)
        );
        // Medium: Ask first; changing files; the sandboxed shell; any site.
        assert_eq!(risk(PartitionMode::Ask, none()), (Medium, false));
        assert_eq!(
            risk(PartitionMode::Plan, none().with_files(FileAccess::ReadWrite)),
            (Medium, false)
        );
        assert_eq!(
            risk(PartitionMode::Plan, none().with_shell(ShellAccess::Sandboxed)),
            (Medium, false)
        );
        assert_eq!(risk(PartitionMode::Plan, none().with_web(WebAccess::Any)), (Medium, false));
        // High: Go ahead; a shell on this computer.
        assert_eq!(risk(PartitionMode::Auto, none()), (High, false));
        assert_eq!(
            risk(PartitionMode::ReadOnly, none().with_shell(ShellAccess::Host)),
            (High, false)
        );
        // High, spends money: payments or computer use.
        assert_eq!(risk(PartitionMode::ReadOnly, none().with_pack("payments")), (High, true));
        assert_eq!(risk(PartitionMode::ReadOnly, none().with_pack("computer-use")), (High, true));
    }

    #[test]
    fn drivers_count_by_their_effect_classes() {
        let uses = AgentUses::default().with_driver("cal");
        let read_only = definition(PartitionMode::ReadOnly, uses.clone());
        let assess = |classes: Vec<EffectClass>| {
            let a = kernel_assessment(&read_only, &AgentOrigin::ByHand, &[("cal".into(), classes)]);
            (a.risk, a.money)
        };
        assert_eq!(assess(vec![EffectClass::Read]), (ApprovalRisk::Low, false));
        assert_eq!(
            assess(vec![EffectClass::Read, EffectClass::ScheduleWithOthers]),
            (ApprovalRisk::Medium, false)
        );
        assert_eq!(assess(vec![EffectClass::WriteLocal]), (ApprovalRisk::Medium, false));
        assert_eq!(assess(vec![EffectClass::SendExternal]), (ApprovalRisk::High, false));
        assert_eq!(assess(vec![EffectClass::Delete]), (ApprovalRisk::High, false));
        assert_eq!(assess(vec![EffectClass::PublishPublic]), (ApprovalRisk::High, true));
        assert_eq!(assess(vec![EffectClass::Financial]), (ApprovalRisk::High, true));
        // A driver the kernel knows nothing of counts as undescribed.
        assert_eq!(assess(vec![]), (ApprovalRisk::High, false));
        let words = kernel_assessment(
            &read_only,
            &AgentOrigin::ByHand,
            &[("cal".into(), vec![EffectClass::Read, EffectClass::ScheduleWithOthers])],
        );
        assert_eq!(
            words.abilities,
            ["It can use the cal driver, which can read and schedule with others."]
        );
    }

    #[test]
    fn an_unattended_suggestion_is_high_risk() {
        let origin = |unattended| AgentOrigin::Suggested {
            agent: AgentId::new("urn:adk:agent:adk-rust/adk-rust").unwrap(),
            session: "s".into(),
            run: None,
            unattended,
        };
        let plain = definition(PartitionMode::ReadOnly, AgentUses::default());
        assert_eq!(kernel_assessment(&plain, &origin(false), &[]).risk, ApprovalRisk::Low);
        assert_eq!(kernel_assessment(&plain, &origin(true), &[]).risk, ApprovalRisk::High);
    }

    #[test]
    fn a_suggestion_never_carries_a_host_shell_or_money() {
        let generous = AgentCeiling::new(
            PartitionMode::Auto,
            AgentUses::default().with_shell(ShellAccess::Host).with_pack("payments"),
        );
        let wants = definition(
            PartitionMode::Ask,
            AgentUses::default().with_shell(ShellAccess::Host).with_pack("payments"),
        );
        assert_eq!(
            over_ceiling(&wants, &generous),
            ["a shell on this computer", "the payments pack"]
        );
        let tight = AgentCeiling::new(PartitionMode::Plan, AgentUses::default());
        let looser = definition(PartitionMode::Ask, AgentUses::default());
        assert_eq!(over_ceiling(&looser, &tight), ["ask first where it may only suggest"]);
    }

    #[test]
    fn area_limits_cover_mode_limit_and_drivers() {
        let home = Partition::new("home", "home")
            .with_mode(PartitionMode::Ask)
            .with_driver("calendar")
            .with_quotas(crate::sched::Quotas::new().with_daily_spend_cap(1_000));
        let fits = definition(PartitionMode::Ask, AgentUses::default().with_driver("calendar"))
            .with_daily_limit(500);
        assert!(area_limits(&fits, &home).is_empty());
        let over = definition(PartitionMode::Auto, AgentUses::default().with_driver("mail"))
            .with_daily_limit(5_000);
        assert_eq!(area_limits(&over, &home).len(), 3);
    }

    #[test]
    fn widening_ignores_words_and_model() {
        let was = definition(PartitionMode::Ask, AgentUses::default()).with_daily_limit(100);
        let mut words = was.clone().with_instructions("New words").with_description("New line");
        words.display_name = "Bills".into();
        assert!(widened(&was, &words).is_empty());
        let mut unlimited = was.clone();
        unlimited.daily_limit_micro_usd = None;
        assert_eq!(widened(&was, &unlimited), ["no daily limit of its own"]);
        assert_eq!(widened(&was, &was.clone().with_daily_limit(50)), Vec::<String>::new());
    }
}