astrid-capsule 0.3.0

Core runtime management for User-Space Capsules in Astrid OS
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
//! Topological sort for capsule dependency ordering.
//!
//! Implements Kahn's algorithm to order capsules so that dependencies load
//! before their dependents. Edges are derived from capability-based
//! `requires`/`provides` declarations rather than package names.

use std::collections::VecDeque;
use std::fmt;
use std::path::PathBuf;

use crate::manifest::CapsuleManifest;

/// A manifest paired with its capsule directory path.
pub(crate) type ManifestEntry = (CapsuleManifest, PathBuf);

/// Error returned when the dependency graph contains a cycle.
#[derive(Debug, Clone)]
pub struct CycleError {
    /// Names of capsules involved in the cycle.
    pub cycle: Vec<String>,
}

impl fmt::Display for CycleError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "dependency cycle detected (involved nodes): {}",
            self.cycle.join(", ")
        )
    }
}

impl std::error::Error for CycleError {}

/// Match a capability requirement against a provided capability.
///
/// Both sides may contain `*` wildcards that match a single dot-separated
/// segment. The type prefix (before `:`) must match exactly; only the body
/// (after `:`) supports wildcard matching.
///
/// # Examples
///
/// - `topic:llm.stream.*` matches `topic:llm.stream.anthropic`
/// - `topic:foo` does NOT match `tool:foo` (type prefix mismatch)
/// - `topic:a.b` does NOT match `topic:a.b.c` (segment count mismatch)
pub fn capability_matches(requirement: &str, provided: &str) -> bool {
    let (req_type, req_body) = requirement.split_once(':').unwrap_or(("", requirement));
    let (prov_type, prov_body) = provided.split_once(':').unwrap_or(("", provided));
    if req_type != prov_type {
        return false;
    }
    // Zero-alloc: zip iterators directly instead of collecting into Vecs.
    // After zip exhausts the shorter side, check both are fully consumed
    // to enforce equal segment count.
    let mut req_segs = req_body.split('.');
    let mut prov_segs = prov_body.split('.');
    let all_matched = (&mut req_segs)
        .zip(&mut prov_segs)
        .all(|(r, p)| r == "*" || p == "*" || r == p);
    all_matched && req_segs.next().is_none() && prov_segs.next().is_none()
}

/// Sort capsule manifests in dependency order using Kahn's algorithm.
///
/// Capsules with no requirements come first. Dependencies are resolved by
/// matching each capsule's `requires` against other capsules' effective
/// `provides` using [`capability_matches`] for wildcard support.
///
/// Uses any-satisfies semantics: a requirement is met when ANY single
/// capsule provides a matching capability.
///
/// Unsatisfied requirements are logged as warnings and treated as met -
/// the capsule still loads, it just won't have that capability guaranteed.
///
/// # Errors
///
/// Returns [`CycleError`] paired with the original unsorted vector if the
/// dependency graph contains a cycle. This avoids cloning the input as a
/// fallback buffer.
pub fn toposort_manifests(
    manifests: Vec<ManifestEntry>,
) -> Result<Vec<ManifestEntry>, (CycleError, Vec<ManifestEntry>)> {
    if manifests.len() <= 1 {
        return Ok(manifests);
    }

    let len = manifests.len();
    // adjacency[i] = list of indices that depend on i (i.e., i must load before them)
    let mut dependents: Vec<Vec<usize>> = vec![Vec::new(); len];
    let mut in_degree: Vec<usize> = vec![0; len];

    // Build capability-based adjacency list in a scoped block so the
    // borrows on `manifests` are released before the error path needs
    // to return ownership.
    {
        let all_provides: Vec<&[String]> = manifests
            .iter()
            .map(|(m, _)| m.effective_provides())
            .collect();

        for (idx, (manifest, _)) in manifests.iter().enumerate() {
            for req in &manifest.dependencies.requires {
                let mut satisfied = false;
                for (prov_idx, provides) in all_provides.iter().enumerate() {
                    if prov_idx == idx {
                        continue;
                    }
                    if provides.iter().any(|p| capability_matches(req, p)) {
                        // prov_idx must load before idx
                        dependents[prov_idx].push(idx);
                        in_degree[idx] += 1;
                        satisfied = true;
                        // Continue: all providers get an ordering edge.
                    }
                }
                if !satisfied {
                    tracing::warn!(
                        capsule = %manifest.package.name,
                        requirement = %req,
                        "Required capability not provided by any loaded capsule"
                    );
                }
            }
        }
    }

    // BFS from zero-in-degree nodes
    let mut queue: VecDeque<usize> = in_degree
        .iter()
        .enumerate()
        .filter(|(_, d)| **d == 0)
        .map(|(i, _)| i)
        .collect();

    let mut order: Vec<usize> = Vec::with_capacity(len);

    while let Some(idx) = queue.pop_front() {
        order.push(idx);
        for &dependent in &dependents[idx] {
            in_degree[dependent] -= 1;
            if in_degree[dependent] == 0 {
                queue.push_back(dependent);
            }
        }
    }

    if order.len() != len {
        // Extract cycle members (nodes with remaining in-degree > 0)
        let cycle: Vec<String> = in_degree
            .iter()
            .enumerate()
            .filter(|(_, d)| **d > 0)
            .map(|(i, _)| manifests[i].0.package.name.clone())
            .collect();
        return Err((CycleError { cycle }, manifests));
    }

    // Reorder manifests according to topological order.
    // Convert to Option so we can take() by index without cloning.
    let mut slots: Vec<Option<ManifestEntry>> = manifests.into_iter().map(Some).collect();
    let sorted = order
        .into_iter()
        .map(|i| slots[i].take().expect("each index visited exactly once"))
        .collect();

    Ok(sorted)
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use crate::manifest::{CapabilitiesDef, DependenciesDef, ToolDef};

    /// Create a manifest with explicit provides and requires capabilities.
    fn manifest_with_caps(name: &str, provides: &[&str], requires: &[&str]) -> ManifestEntry {
        let m = CapsuleManifest {
            package: crate::manifest::PackageDef {
                name: name.to_string(),
                version: "0.1.0".to_string(),
                description: None,
                authors: Vec::new(),
                repository: None,
                homepage: None,
                documentation: None,
                license: None,
                license_file: None,
                readme: None,
                keywords: Vec::new(),
                categories: Vec::new(),
                astrid_version: None,
                publish: None,
                include: None,
                exclude: None,
                metadata: None,
            },
            components: Vec::new(),
            dependencies: DependenciesDef {
                provides: provides.iter().map(|s| (*s).to_string()).collect(),
                requires: requires.iter().map(|s| (*s).to_string()).collect(),
            },
            capabilities: Default::default(),
            env: HashMap::new(),
            context_files: Vec::new(),
            commands: Vec::new(),
            mcp_servers: Vec::new(),
            skills: Vec::new(),
            uplinks: Vec::new(),
            llm_providers: Vec::new(),
            interceptors: Vec::new(),
            cron_jobs: Vec::new(),
            tools: Vec::new(),
            topics: Vec::new(),
            effective_provides_cache: std::sync::OnceLock::new(),
        };
        (m, PathBuf::from(format!("/capsules/{name}")))
    }

    /// Create a manifest with no explicit provides/requires (auto-derive test).
    fn manifest_bare(name: &str) -> ManifestEntry {
        manifest_with_caps(name, &[], &[])
    }

    fn names(manifests: &[ManifestEntry]) -> Vec<&str> {
        manifests
            .iter()
            .map(|(m, _)| m.package.name.as_str())
            .collect()
    }

    // -- capability_matches tests --

    #[test]
    fn capability_matches_exact() {
        assert!(capability_matches("topic:foo.bar", "topic:foo.bar"));
    }

    #[test]
    fn capability_matches_wildcard_in_requirement() {
        assert!(capability_matches(
            "topic:llm.stream.*",
            "topic:llm.stream.anthropic"
        ));
    }

    #[test]
    fn capability_matches_wildcard_in_provider() {
        assert!(capability_matches(
            "topic:llm.request.generate.anthropic",
            "topic:llm.request.generate.*"
        ));
    }

    #[test]
    fn capability_matches_type_mismatch() {
        assert!(!capability_matches("topic:foo", "tool:foo"));
    }

    #[test]
    fn capability_matches_segment_count_mismatch() {
        assert!(!capability_matches("topic:a.b", "topic:a.b.c"));
    }

    #[test]
    fn capability_matches_no_prefix() {
        assert!(capability_matches("foo", "foo"));
        assert!(!capability_matches("foo", "bar"));
    }

    #[test]
    fn capability_matches_both_wildcards() {
        assert!(capability_matches("topic:*.stream", "topic:llm.*"));
    }

    #[test]
    fn capability_matches_middle_wildcard() {
        assert!(capability_matches(
            "topic:llm.*.anthropic",
            "topic:llm.stream.anthropic"
        ));
        assert!(!capability_matches(
            "topic:llm.*.anthropic",
            "topic:llm.stream.openai"
        ));
    }

    #[test]
    fn capability_matches_empty_strings() {
        // capability_matches("", "") returns true as an implementation artifact:
        // split_once(':') on "" gives None, so both types are ""; split('.')
        // on "" gives [""], so both bodies match. This is only safe because
        // manifest validation (load_manifest) rejects empty strings before any
        // call to toposort_manifests. Tests that bypass manifest validation
        // must not pass empty strings here.
        assert!(capability_matches("", ""));
    }

    #[test]
    fn capability_matches_empty_body_after_prefix() {
        // "topic:" has an empty body, which splits to [""]. This matches
        // another "topic:" with empty body. Again, manifest validation
        // prevents this from occurring in practice.
        assert!(capability_matches("topic:", "topic:"));
        assert!(!capability_matches("topic:", "tool:"));
    }

    // -- toposort tests --

    #[test]
    fn empty_graph() {
        let result = toposort_manifests(vec![]).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn single_node() {
        let input = vec![manifest_bare("a")];
        let result = toposort_manifests(input).unwrap();
        assert_eq!(names(&result), vec!["a"]);
    }

    #[test]
    fn capability_edge() {
        // B requires topic:foo, A provides topic:foo => A before B
        let input = vec![
            manifest_with_caps("b", &[], &["topic:foo"]),
            manifest_with_caps("a", &["topic:foo"], &[]),
        ];
        let result = toposort_manifests(input).unwrap();
        let n = names(&result);
        assert!(
            n.iter().position(|&x| x == "a").unwrap() < n.iter().position(|&x| x == "b").unwrap()
        );
    }

    #[test]
    fn wildcard_requires() {
        // B requires topic:llm.stream.*, A provides topic:llm.stream.anthropic => A before B
        let input = vec![
            manifest_with_caps("b", &[], &["topic:llm.stream.*"]),
            manifest_with_caps("a", &["topic:llm.stream.anthropic"], &[]),
        ];
        let result = toposort_manifests(input).unwrap();
        let n = names(&result);
        assert!(
            n.iter().position(|&x| x == "a").unwrap() < n.iter().position(|&x| x == "b").unwrap()
        );
    }

    #[test]
    fn all_providers_ordered_before_consumer() {
        // C requires topic:llm.stream.*, both A and B provide it.
        // All providers get ordering edges, so both A and B load before C.
        let input = vec![
            manifest_with_caps("c", &[], &["topic:llm.stream.*"]),
            manifest_with_caps("a", &["topic:llm.stream.anthropic"], &[]),
            manifest_with_caps("b", &["topic:llm.stream.openai"], &[]),
        ];
        let result = toposort_manifests(input).unwrap();
        let n = names(&result);
        let c_pos = n.iter().position(|&x| x == "c").unwrap();
        let a_pos = n.iter().position(|&x| x == "a").unwrap();
        let b_pos = n.iter().position(|&x| x == "b").unwrap();
        assert!(a_pos < c_pos);
        assert!(b_pos < c_pos);
    }

    #[test]
    fn unsatisfied_requirement_still_succeeds() {
        // B requires topic:missing - no provider exists, B still loads
        let input = vec![
            manifest_bare("a"),
            manifest_with_caps("b", &[], &["topic:missing"]),
        ];
        let result = toposort_manifests(input).unwrap();
        assert_eq!(names(&result).len(), 2);
    }

    #[test]
    fn cycle_detected() {
        // A requires what B provides, B requires what A provides
        let input = vec![
            manifest_with_caps("a", &["topic:x"], &["topic:y"]),
            manifest_with_caps("b", &["topic:y"], &["topic:x"]),
        ];
        let (err, original) = toposort_manifests(input).unwrap_err();
        assert!(err.cycle.contains(&"a".to_string()));
        assert!(err.cycle.contains(&"b".to_string()));
        assert!(err.to_string().contains("dependency cycle detected"));
        assert_eq!(original.len(), 2);
    }

    #[test]
    fn three_node_cycle() {
        let input = vec![
            manifest_with_caps("a", &["topic:x"], &["topic:z"]),
            manifest_with_caps("b", &["topic:y"], &["topic:x"]),
            manifest_with_caps("c", &["topic:z"], &["topic:y"]),
        ];
        let (err, original) = toposort_manifests(input).unwrap_err();
        assert_eq!(err.cycle.len(), 3);
        assert_eq!(original.len(), 3);
    }

    #[test]
    fn no_dependencies_preserves_all() {
        let input = vec![manifest_bare("x"), manifest_bare("y"), manifest_bare("z")];
        let result = toposort_manifests(input).unwrap();
        assert_eq!(result.len(), 3);
    }

    #[test]
    fn diamond_dependency_via_capabilities() {
        // d requires topic:b + topic:c, b and c require topic:a
        let input = vec![
            manifest_with_caps("d", &["topic:d"], &["topic:b", "topic:c"]),
            manifest_with_caps("b", &["topic:b"], &["topic:a"]),
            manifest_with_caps("c", &["topic:c"], &["topic:a"]),
            manifest_with_caps("a", &["topic:a"], &[]),
        ];
        let result = toposort_manifests(input).unwrap();
        let n = names(&result);
        let pos = |name: &str| n.iter().position(|&x| x == name).unwrap();
        assert!(pos("a") < pos("b"));
        assert!(pos("a") < pos("c"));
        assert!(pos("b") < pos("d"));
        assert!(pos("c") < pos("d"));
    }

    // -- effective_provides tests --

    #[test]
    fn effective_provides_auto_derives_from_ipc_publish() {
        let (mut m, _) = manifest_bare("test");
        m.capabilities = CapabilitiesDef {
            ipc_publish: vec!["foo.bar".to_string(), "baz".to_string()],
            ..Default::default()
        };
        assert_eq!(m.effective_provides(), vec!["topic:foo.bar", "topic:baz"]);
    }

    #[test]
    fn effective_provides_explicit_overrides_auto_derive() {
        let (mut m, _) = manifest_bare("test");
        m.dependencies.provides = vec!["custom:cap".to_string()];
        m.capabilities = CapabilitiesDef {
            ipc_publish: vec!["should.be.ignored".to_string()],
            ..Default::default()
        };
        assert_eq!(m.effective_provides(), vec!["custom:cap"]);
    }

    #[test]
    fn effective_provides_includes_tools() {
        let (mut m, _) = manifest_bare("test");
        m.tools = vec![ToolDef {
            name: "run_shell".to_string(),
            description: "Run shell".to_string(),
            input_schema: serde_json::json!({}),
        }];
        assert_eq!(m.effective_provides(), vec!["tool:run_shell"]);
    }

    #[test]
    fn effective_provides_empty_when_nothing_declared() {
        let (m, _) = manifest_bare("test");
        assert!(m.effective_provides().is_empty());
    }

    // -- auto-derived provides create edges in toposort --

    #[test]
    fn toposort_uses_auto_derived_provides() {
        // A has ipc_publish = ["foo.bar"] (auto-derives topic:foo.bar)
        // B requires topic:foo.bar
        let (mut a, a_path) = manifest_bare("a");
        a.capabilities = CapabilitiesDef {
            ipc_publish: vec!["foo.bar".to_string()],
            ..Default::default()
        };
        let b = manifest_with_caps("b", &[], &["topic:foo.bar"]);

        let input = vec![b, (a, a_path)];
        let result = toposort_manifests(input).unwrap();
        let n = names(&result);
        assert!(
            n.iter().position(|&x| x == "a").unwrap() < n.iter().position(|&x| x == "b").unwrap()
        );
    }

    #[test]
    fn wildcard_body_in_requires_matches_all_providers_of_type() {
        // "topic:*" matches any single-segment topic body.
        // This is unusual but valid - creates ordering edges to all topic providers.
        let input = vec![
            manifest_with_caps("consumer", &[], &["topic:*"]),
            manifest_with_caps("provider-a", &["topic:foo"], &[]),
            manifest_with_caps("provider-b", &["topic:bar"], &[]),
        ];
        let result = toposort_manifests(input).unwrap();
        let n = names(&result);
        let pos = |name: &str| n.iter().position(|&x| x == name).unwrap();
        // Both providers must load before the consumer
        assert!(pos("provider-a") < pos("consumer"));
        assert!(pos("provider-b") < pos("consumer"));
    }

    // -- shipped capsule integration tests --

    #[test]
    fn react_requires_satisfied_by_identity_and_session() {
        // Verify that the react capsule's [dependencies].requires are
        // satisfiable by the identity capsule's auto-derived provides
        // and the session capsule's explicit provides. Session uses
        // explicit provides because its ipc_publish uses wildcards for
        // per-request scoped reply topics, which would break auto-derivation
        // (wildcard segment count differs from react's requires).
        let identity = {
            let (mut m, p) = manifest_bare("astrid-capsule-identity");
            m.capabilities = CapabilitiesDef {
                ipc_publish: vec!["identity.v1.response.ready".into()],
                ..Default::default()
            };
            (m, p)
        };
        let session = {
            let (mut m, p) = manifest_bare("astrid-capsule-session");
            m.dependencies.provides = vec![
                "topic:session.v1.response.get_messages".into(),
                "topic:session.v1.response.clear".into(),
            ];
            m.capabilities = CapabilitiesDef {
                ipc_publish: vec![
                    "session.v1.response.get_messages.*".into(),
                    "session.v1.response.clear.*".into(),
                ],
                ..Default::default()
            };
            (m, p)
        };
        let react = manifest_with_caps(
            "astrid-capsule-react",
            &[],
            &[
                "topic:identity.v1.response.ready",
                "topic:session.v1.response.get_messages",
            ],
        );

        let input = vec![react, identity, session];
        let result = toposort_manifests(input).unwrap();
        let n = names(&result);
        let pos = |name: &str| n.iter().position(|&x| x == name).unwrap();

        // React must load after both identity and session
        assert!(pos("astrid-capsule-identity") < pos("astrid-capsule-react"));
        assert!(pos("astrid-capsule-session") < pos("astrid-capsule-react"));
    }
}