aidaemon-core 0.28.0

The aidaemon agent kernel: a small, hook-driven agent loop with tools, providers, receipts and resumable state
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
use serde_json::{Map, Value};

use super::ConformanceReport;
use crate::tool::Tool;

/// Stable names of the checks run by [`check_tool`], each documenting the
/// rule it enforces.
pub mod tool_checks {
    /// `spec().name` matches `^[a-zA-Z0-9_-]{1,64}$`, the character set and
    /// length every major provider accepts for function names.
    pub const NAME_FORMAT: &str = "tool.name_format";
    /// `spec().description` is not empty or whitespace: the model chooses
    /// tools by their description.
    pub const DESCRIPTION_NON_EMPTY: &str = "tool.description_non_empty";
    /// `spec().parameters` is a JSON object schema with `"type": "object"`.
    pub const PARAMETERS_OBJECT_SCHEMA: &str = "tool.parameters_object_schema";
    /// `spec().parameters` has `"additionalProperties": false`, so strict
    /// providers accept it and invented arguments are rejected.
    pub const PARAMETERS_CLOSED: &str = "tool.parameters_closed";
    /// `properties` (if present) is an object, `required` (if present) is an
    /// array of strings, and every `required` key is declared in
    /// `properties`.
    pub const REQUIRED_KEYS_DECLARED: &str = "tool.required_keys_declared";
    /// `validate` accepts a minimal valid call: the author-supplied sample
    /// ([`super::ToolKitOptions::valid_arguments`]) or one generated from
    /// the schema by [`super::sample_arguments`].
    pub const VALIDATE_ACCEPTS_MINIMAL_CALL: &str = "tool.validate_accepts_minimal_call";
    /// `validate` rejects the minimal valid call with any one `required`
    /// key removed, so malformed proposals never reach policies or I/O.
    pub const VALIDATE_REJECTS_MISSING_REQUIRED: &str = "tool.validate_rejects_missing_required";
    /// `validate` rejects arguments that are not a JSON object (the kernel
    /// contract for object schemas).
    pub const VALIDATE_REJECTS_NON_OBJECT: &str = "tool.validate_rejects_non_object";
    /// A tool annotated `read_only` is not also `destructive`; the two are
    /// contradictory and break scheduling and approval policies.
    pub const READ_ONLY_NOT_DESTRUCTIVE: &str = "tool.read_only_not_destructive";
    /// `annotations_for` on the sample call only relaxes the tool-wide
    /// `annotations()`: it never reports a call as read-only, non-destructive
    /// or non-egress when the worst case is not. Authority checks trust the
    /// per-call value, so a tightening one would hide a stricter worst case.
    pub const ANNOTATIONS_FOR_ONLY_RELAXES: &str = "tool.annotations_for_only_relaxes";
    /// `spec()` and `annotations()` return identical values on repeated
    /// calls, so the cacheable request prefix stays stable.
    pub const SPEC_STABLE: &str = "tool.spec_stable";
}

/// Options for [`check_tool_with`].
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct ToolKitOptions {
    /// A known-valid argument object. Use it when `validate` checks more
    /// than the schema can express (a URL that must parse, a path that must
    /// exist) and the generated sample is rejected for that reason. It
    /// should contain only the required keys, so the
    /// missing-required check can remove them one by one.
    pub valid_arguments: Option<Value>,
}

impl ToolKitOptions {
    pub fn valid_arguments(mut self, arguments: Value) -> Self {
        self.valid_arguments = Some(arguments);
        self
    }
}

/// Run every tool check (see [`tool_checks`]) with a schema-generated
/// sample call. Never calls [`Tool::call`], so it is safe for tools with
/// side effects.
pub fn check_tool(tool: &dyn Tool) -> ConformanceReport {
    check_tool_with(tool, ToolKitOptions::default())
}

/// [`check_tool`] with options, for tools whose `validate` needs a
/// hand-written valid call.
pub fn check_tool_with(tool: &dyn Tool, options: ToolKitOptions) -> ConformanceReport {
    use tool_checks::*;

    let spec = tool.spec();
    let mut report = ConformanceReport::new(if spec.name.is_empty() {
        "<unnamed tool>".to_string()
    } else {
        spec.name.clone()
    });
    let params = &spec.parameters;

    report.record(NAME_FORMAT, check_name(&spec.name));
    report.record(
        DESCRIPTION_NON_EMPTY,
        if spec.description.trim().is_empty() {
            Err("description is empty".into())
        } else {
            Ok(())
        },
    );
    let is_object_schema =
        params.is_object() && params.get("type").and_then(Value::as_str) == Some("object");
    report.record(
        PARAMETERS_OBJECT_SCHEMA,
        if is_object_schema {
            Ok(())
        } else {
            Err(format!(
                "parameters must be a JSON object with \"type\": \"object\", got {}",
                abbreviate(params)
            ))
        },
    );
    report.record(
        PARAMETERS_CLOSED,
        match params.get("additionalProperties") {
            Some(Value::Bool(false)) => Ok(()),
            Some(other) => Err(format!(
                "\"additionalProperties\" must be false, got {other}"
            )),
            None => Err("\"additionalProperties\": false is missing".into()),
        },
    );
    let required = required_keys(params);
    report.record(REQUIRED_KEYS_DECLARED, check_required_declared(params));

    let (sample, supplied) = match options.valid_arguments {
        Some(args) => (args, true),
        None => (sample_arguments(params), false),
    };
    let origin = if supplied {
        "supplied valid_arguments"
    } else {
        "schema-generated sample"
    };
    let accepted = tool.validate(&sample);
    report.record(
        VALIDATE_ACCEPTS_MINIMAL_CALL,
        accepted.clone().map_err(|e| {
            let hint = if supplied {
                ""
            } else {
                " (if validate needs more than the schema expresses, pass \
                 ToolKitOptions::valid_arguments)"
            };
            format!("validate rejected the {origin} {sample}: {e}{hint}")
        }),
    );

    let keys = required.as_ref().cloned().unwrap_or_default();
    if keys.is_empty() {
        report.pass(VALIDATE_REJECTS_MISSING_REQUIRED);
        report.note(format!(
            "{VALIDATE_REJECTS_MISSING_REQUIRED}: schema has no required keys; check is vacuous"
        ));
    } else if let Some(object) = sample.as_object() {
        let accepted_with: Vec<String> = keys
            .iter()
            .filter(|key| {
                let mut partial = object.clone();
                partial.remove(key.as_str());
                tool.validate(&Value::Object(partial)).is_ok()
            })
            .cloned()
            .collect();
        report.record(
            VALIDATE_REJECTS_MISSING_REQUIRED,
            if accepted_with.is_empty() {
                Ok(())
            } else {
                Err(format!(
                    "validate accepted the {origin} with required key(s) removed: {}",
                    accepted_with.join(", ")
                ))
            },
        );
    } else {
        report.fail(
            VALIDATE_REJECTS_MISSING_REQUIRED,
            format!("the {origin} is not a JSON object: {sample}"),
        );
    }

    if is_object_schema {
        let rejected_all = [
            Value::String("not an object".into()),
            Value::Array(Vec::new()),
            Value::Null,
        ]
        .into_iter()
        .filter(|bad| tool.validate(bad).is_ok())
        .map(|bad| bad.to_string())
        .collect::<Vec<_>>();
        report.record(
            VALIDATE_REJECTS_NON_OBJECT,
            if rejected_all.is_empty() {
                Ok(())
            } else {
                Err(format!(
                    "validate accepted non-object arguments: {}",
                    rejected_all.join(", ")
                ))
            },
        );
    } else {
        report.fail(
            VALIDATE_REJECTS_NON_OBJECT,
            "parameters are not an object schema, so non-object arguments cannot be rejected"
                .to_string(),
        );
    }

    let annotations = tool.annotations();
    report.record(
        READ_ONLY_NOT_DESTRUCTIVE,
        if annotations.read_only && annotations.destructive {
            Err("annotated both read_only and destructive".into())
        } else {
            Ok(())
        },
    );

    let per_call = tool.annotations_for(&sample);
    let mut tightened = Vec::new();
    if annotations.read_only && !per_call.read_only {
        tightened.push("read_only");
    }
    if !annotations.destructive && per_call.destructive {
        tightened.push("destructive");
    }
    if !annotations.egress && per_call.egress {
        tightened.push("egress");
    }
    report.record(
        ANNOTATIONS_FOR_ONLY_RELAXES,
        if tightened.is_empty() {
            Ok(())
        } else {
            Err(format!(
                "annotations_for({sample}) is stricter than annotations() on: {}; \
                 annotations() must be the worst case",
                tightened.join(", ")
            ))
        },
    );

    let again = tool.spec();
    report.record(
        SPEC_STABLE,
        if again != spec {
            Err("spec() changed between two calls".into())
        } else if tool.annotations() != annotations {
            Err("annotations() changed between two calls".into())
        } else {
            Ok(())
        },
    );
    report
}

fn check_name(name: &str) -> Result<(), String> {
    let len = name.chars().count();
    if len == 0 || len > 64 {
        return Err(format!("name must be 1-64 characters, got {len}"));
    }
    match name
        .chars()
        .find(|c| !(c.is_ascii_alphanumeric() || *c == '_' || *c == '-'))
    {
        Some(bad) => Err(format!(
            "name {name:?} contains {bad:?}; allowed: ASCII letters, digits, '_' and '-'"
        )),
        None => Ok(()),
    }
}

fn required_keys(params: &Value) -> Option<Vec<String>> {
    match params.get("required") {
        None => Some(Vec::new()),
        Some(Value::Array(items)) => items
            .iter()
            .map(|v| v.as_str().map(str::to_string))
            .collect(),
        Some(_) => None,
    }
}

fn check_required_declared(params: &Value) -> Result<(), String> {
    let properties = match params.get("properties") {
        None => None,
        Some(Value::Object(map)) => Some(map),
        Some(other) => {
            return Err(format!(
                "\"properties\" must be an object, got {}",
                abbreviate(other)
            ));
        }
    };
    let Some(required) = required_keys(params) else {
        return Err("\"required\" must be an array of strings".into());
    };
    let missing: Vec<&str> = required
        .iter()
        .filter(|k| !properties.is_some_and(|p| p.contains_key(k.as_str())))
        .map(String::as_str)
        .collect();
    if missing.is_empty() {
        Ok(())
    } else {
        Err(format!(
            "required key(s) not declared in properties: {}",
            missing.join(", ")
        ))
    }
}

fn abbreviate(value: &Value) -> String {
    let text = value.to_string();
    if text.chars().count() > 80 {
        format!("{}...", text.chars().take(80).collect::<String>())
    } else {
        text
    }
}

/// Build a minimal argument value from a JSON Schema: only `required`
/// properties, each filled from `const`, `default`, the first `enum` value or
/// the first of `examples` when present, otherwise from its type (non-empty
/// distinct strings honouring `format`/`minLength`, numbers honouring
/// `minimum`, `false`, arrays of `minItems` samples, nested objects).
///
/// Exposed so authors can see what [`check_tool`] sends to `validate`.
pub fn sample_arguments(schema: &Value) -> Value {
    sample_value(schema, "value", 0)
}

fn sample_value(schema: &Value, key: &str, depth: usize) -> Value {
    if depth > 16 {
        return Value::Null;
    }
    let Some(obj) = schema.as_object() else {
        return Value::String(format!("sample-{key}"));
    };
    for field in ["const", "default"] {
        if let Some(v) = obj.get(field) {
            return v.clone();
        }
    }
    for field in ["enum", "examples"] {
        if let Some(first) = obj
            .get(field)
            .and_then(Value::as_array)
            .and_then(|a| a.first())
        {
            return first.clone();
        }
    }
    for field in ["anyOf", "oneOf", "allOf"] {
        if let Some(first) = obj.get(field).and_then(Value::as_array).and_then(|a| {
            a.iter()
                .find(|s| s.get("type").and_then(Value::as_str) != Some("null"))
                .or_else(|| a.first())
        }) {
            return sample_value(first, key, depth + 1);
        }
    }
    let ty = match obj.get("type") {
        Some(Value::String(t)) => t.as_str(),
        Some(Value::Array(types)) => types
            .iter()
            .filter_map(Value::as_str)
            .find(|t| *t != "null")
            .unwrap_or("null"),
        _ if obj.contains_key("properties") => "object",
        _ if obj.contains_key("items") => "array",
        _ => "string",
    };
    match ty {
        "object" => {
            let props = obj.get("properties").and_then(Value::as_object);
            let mut out = Map::new();
            for key in obj
                .get("required")
                .and_then(Value::as_array)
                .into_iter()
                .flatten()
                .filter_map(Value::as_str)
            {
                let prop = props.and_then(|p| p.get(key)).unwrap_or(&Value::Null);
                out.insert(key.to_string(), sample_value(prop, key, depth + 1));
            }
            Value::Object(out)
        }
        "array" => {
            let n = obj.get("minItems").and_then(Value::as_u64).unwrap_or(0) as usize;
            let item = obj.get("items").unwrap_or(&Value::Null);
            Value::Array((0..n).map(|_| sample_value(item, key, depth + 1)).collect())
        }
        "integer" | "number" => sample_number(obj, ty == "integer"),
        "boolean" => Value::Bool(false),
        "null" => Value::Null,
        _ => sample_string(obj, key),
    }
}

fn sample_number(obj: &Map<String, Value>, integer: bool) -> Value {
    let min = obj.get("minimum").and_then(Value::as_f64);
    let exclusive_min = obj.get("exclusiveMinimum").and_then(Value::as_f64);
    let max = obj
        .get("maximum")
        .and_then(Value::as_f64)
        .or_else(|| obj.get("exclusiveMaximum").and_then(Value::as_f64));
    let mut n = match (min, exclusive_min) {
        (Some(m), _) => m,
        (None, Some(m)) => m + 1.0,
        (None, None) => 1.0,
    };
    if let Some(max) = max
        && n > max
    {
        n = max;
    }
    if integer {
        Value::from(n.ceil() as i64)
    } else {
        serde_json::Number::from_f64(n)
            .map(Value::Number)
            .unwrap_or_else(|| Value::from(1))
    }
}

fn sample_string(obj: &Map<String, Value>, key: &str) -> Value {
    let mut s = match obj.get("format").and_then(Value::as_str) {
        Some("uri" | "url" | "iri" | "uri-reference") => "https://example.com/".to_string(),
        Some("email") => "alice.rivera@example.com".to_string(),
        Some("date-time") => "2026-01-01T00:00:00Z".to_string(),
        Some("date") => "2026-01-01".to_string(),
        Some("time") => "00:00:00Z".to_string(),
        Some("uuid") => "00000000-0000-4000-8000-000000000000".to_string(),
        Some("hostname") => "example.com".to_string(),
        _ => format!("sample-{key}"),
    };
    let min = obj.get("minLength").and_then(Value::as_u64).unwrap_or(0) as usize;
    while s.chars().count() < min {
        s.push('x');
    }
    if let Some(max) = obj.get("maxLength").and_then(Value::as_u64) {
        s = s.chars().take(max as usize).collect();
    }
    Value::String(s)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ToolSpec;
    use crate::testing::spec;
    use crate::tool::{FnTool, ToolAnnotations, ToolOutput};
    use serde_json::json;

    fn tool(spec: ToolSpec, annotations: ToolAnnotations) -> impl Tool {
        FnTool::new(spec, annotations, |_args: Value| async {
            Ok(ToolOutput::text("ok"))
        })
    }

    #[test]
    fn a_well_formed_tool_passes() {
        let report = check_tool(&tool(
            spec("echo", "Echo", &["text"]),
            ToolAnnotations::READ_ONLY,
        ));
        report.assert_all_passed();
        assert_eq!(report.passed.len(), 11);
    }

    #[test]
    fn every_problem_is_reported_at_once() {
        let bad = ToolSpec {
            name: "bad name!".into(),
            description: " ".into(),
            parameters: json!({"type": "object", "required": ["path"]}),
        };
        let report = check_tool(&tool(
            bad,
            ToolAnnotations {
                read_only: true,
                destructive: true,
                idempotent: true,
                egress: false,
            },
        ));
        for check in [
            tool_checks::NAME_FORMAT,
            tool_checks::DESCRIPTION_NON_EMPTY,
            tool_checks::PARAMETERS_CLOSED,
            tool_checks::REQUIRED_KEYS_DECLARED,
            tool_checks::READ_ONLY_NOT_DESTRUCTIVE,
        ] {
            assert!(report.failure(check).is_some(), "{check}: {report}");
        }
        assert!(report.passed_check(tool_checks::VALIDATE_REJECTS_MISSING_REQUIRED));
    }

    #[test]
    fn a_per_call_value_that_tightens_the_worst_case_is_reported() {
        struct Tightens;
        #[async_trait::async_trait]
        impl Tool for Tightens {
            fn spec(&self) -> ToolSpec {
                spec("tightens", "Tightens per call", &[])
            }
            fn annotations(&self) -> ToolAnnotations {
                ToolAnnotations::READ_ONLY
            }
            fn annotations_for(&self, _arguments: &Value) -> ToolAnnotations {
                ToolAnnotations::DESTRUCTIVE
            }
            async fn call(
                &self,
                _args: Value,
                _ctx: &crate::tool::ToolContext,
            ) -> crate::tool::ToolResult {
                Ok(ToolOutput::text("ok"))
            }
        }
        let report = check_tool(&Tightens);
        let failure = report
            .failure(tool_checks::ANNOTATIONS_FOR_ONLY_RELAXES)
            .expect("tightening is reported");
        assert!(failure.contains("read_only"), "{failure}");
        assert!(failure.contains("destructive"), "{failure}");
        assert!(failure.contains("egress"), "{failure}");
    }

    #[test]
    fn samples_follow_the_schema() {
        let schema = json!({
            "type": "object",
            "properties": {
                "url": {"type": "string", "format": "uri"},
                "mode": {"type": "string", "enum": ["fast", "slow"]},
                "limit": {"type": "integer", "minimum": 5},
                "flag": {"type": "boolean"},
                "tags": {"type": "array", "minItems": 1, "items": {"type": "string", "minLength": 12}},
                "nested": {"type": "object", "properties": {"a": {"type": "number"}}, "required": ["a"]},
                "optional": {"type": "string"}
            },
            "required": ["url", "mode", "limit", "flag", "tags", "nested"]
        });
        assert_eq!(
            sample_arguments(&schema),
            json!({
                "url": "https://example.com/",
                "mode": "fast",
                "limit": 5,
                "flag": false,
                "tags": ["sample-tagsx"],
                "nested": {"a": 1.0}
            })
        );
    }
}