astrid 0.8.0

Command-line interface for Astrid secure agent runtime
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
//! Distro manifest types and parsing.
//!
//! Parses `Distro.toml` into strongly-typed [`DistroManifest`] with validation
//! for schema version, semver, identifier formats, and variable references.

use std::collections::HashMap;
use std::path::Path;

use anyhow::Context;
use serde::{Deserialize, Serialize};

/// Current supported schema version.
pub(crate) const SCHEMA_VERSION: u32 = 1;

/// A parsed distro manifest.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct DistroManifest {
    /// Schema version for forward compatibility.
    pub(crate) schema_version: u32,
    /// Distro metadata.
    pub(crate) distro: DistroMeta,
    /// Shared variables for capsule env configuration.
    #[serde(default)]
    pub(crate) variables: HashMap<String, VariableDef>,
    /// Capsule entries in the distro.
    #[serde(default, rename = "capsule")]
    pub(crate) capsules: Vec<DistroCapsule>,
    /// Invite policy — when `Some`, the deployment ships with the
    /// `astrid-gateway` HTTP surface configured to accept new
    /// principals via invite redemption. `None` (the default) keeps
    /// the distro single-tenant: no public registration UI.
    ///
    /// The kernel never reads this directly — `astrid init` /
    /// `astrid distro apply` surfaces it to the operator and the
    /// gateway reads it through `/api/distribution`.
    #[serde(default)]
    pub(crate) invites: Option<InviteConfig>,
    /// Optional visual branding for the dashboard. The kernel and
    /// admin API ignore this entirely; only the gateway returns it
    /// through `/api/distribution`.
    #[serde(default)]
    pub(crate) branding: Option<BrandingConfig>,
}

/// Invite policy from `[invites]`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub(crate) struct InviteConfig {
    /// Group(s) allowed to issue invite tokens. An empty `issuers`
    /// list disables registration (single-tenant deployment). All
    /// names must be defined groups (built-in or custom).
    #[serde(default)]
    pub(crate) issuers: Vec<String>,
    /// Default group new redeemers join. Required when `issuers` is
    /// non-empty.
    #[serde(default)]
    pub(crate) default_group: Option<String>,
    /// Default token lifetime (e.g. `"24h"`, `"7d"`, `"30s"`).
    /// `None` falls back to the gateway's compiled-in default
    /// (24 hours).
    #[serde(default)]
    pub(crate) default_expires: Option<String>,
    /// Total-principal cap for the deployment. `"unlimited"` (the
    /// default) skips the check; integer strings cap the count.
    #[serde(default)]
    pub(crate) max_principals: Option<String>,
}

/// Visual branding from `[branding]`. Operator-controlled hints for
/// the dashboard.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub(crate) struct BrandingConfig {
    /// Icon — either a data URL (`data:image/svg+xml,...`) or a path
    /// relative to the distro root. Capped at 64 `KiB` on parse to
    /// keep malformed `Distro.toml` from ballooning memory.
    #[serde(default)]
    pub(crate) icon: Option<String>,
    /// Primary brand colour as a CSS hex string (`#RRGGBB`). The
    /// parser validates the shape; the dashboard interprets it.
    #[serde(default)]
    pub(crate) primary_color: Option<String>,
    /// Optional accent colour. Same shape constraints as
    /// [`Self::primary_color`].
    #[serde(default)]
    pub(crate) accent_color: Option<String>,
}

/// Distro identity and metadata (os-release style).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct DistroMeta {
    /// Machine-readable identifier (e.g. `astralis`).
    pub(crate) id: String,
    /// Display name (e.g. `Astralis`).
    pub(crate) name: String,
    /// Full human-readable string (e.g. `Astralis 0.1.0 (Genesis)`).
    #[serde(default)]
    pub(crate) pretty_name: Option<String>,
    /// Semantic version.
    pub(crate) version: String,
    /// Release codename (e.g. `genesis`).
    #[serde(default)]
    pub(crate) codename: Option<String>,
    /// Release date (YYYY-MM-DD).
    #[serde(default)]
    pub(crate) release_date: Option<String>,
    /// Short description.
    #[serde(default)]
    pub(crate) description: Option<String>,
    /// Original authors.
    #[serde(default)]
    pub(crate) authors: Vec<String>,
    /// Current maintainers.
    #[serde(default)]
    pub(crate) maintainers: Vec<String>,
    /// Homepage URL.
    #[serde(default)]
    pub(crate) homepage: Option<String>,
    /// Support URL.
    #[serde(default)]
    pub(crate) support: Option<String>,
    /// Bug tracker URL.
    #[serde(default)]
    pub(crate) bug_tracker: Option<String>,
    /// Source repository URL.
    #[serde(default)]
    pub(crate) repository: Option<String>,
    /// SPDX license identifier.
    #[serde(default)]
    pub(crate) license: Option<String>,
    /// Minimum Astrid runtime version required.
    #[serde(default)]
    pub(crate) astrid_version: Option<String>,
    /// Namespaced interface requirements.
    ///
    /// Outer key = namespace, inner key = interface name, value = semver requirement.
    /// Example: `[distro.requires.astrid] llm = "^1.0"`
    #[serde(default)]
    pub(crate) requires: HashMap<String, HashMap<String, String>>,
}

/// A shared variable defined at the distro level.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct VariableDef {
    /// Whether this variable holds a secret (masked during input).
    #[serde(default)]
    pub(crate) secret: bool,
    /// Human-readable description shown during prompts.
    #[serde(default)]
    pub(crate) description: Option<String>,
    /// Default value.
    #[serde(default)]
    pub(crate) default: Option<String>,
}

/// A capsule entry in the distro manifest.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct DistroCapsule {
    /// Capsule package name (e.g. `astrid-capsule-session`).
    pub(crate) name: String,
    /// Source location (e.g. `@unicity-astrid/capsule-session`).
    pub(crate) source: String,
    /// Exact version to install (resolved to a git tag).
    pub(crate) version: String,
    /// Provider group for multi-select during init (e.g. `llm`).
    #[serde(default)]
    pub(crate) group: Option<String>,
    /// Deployment role (e.g. `uplink`).
    #[serde(default)]
    pub(crate) role: Option<String>,
    /// Environment variable mappings with `{{ var }}` template references.
    #[serde(default)]
    pub(crate) env: HashMap<String, String>,
}

/// Parse a `Distro.toml` string into a [`DistroManifest`].
pub(crate) fn parse_manifest(content: &str) -> anyhow::Result<DistroManifest> {
    let manifest: DistroManifest =
        toml::from_str(content).context("failed to parse Distro.toml")?;
    super::validate::validate_manifest(&manifest)?;
    Ok(manifest)
}

/// Load and parse a `Distro.toml` from disk.
pub(crate) fn load_manifest(path: &Path) -> anyhow::Result<DistroManifest> {
    let content = std::fs::read_to_string(path)
        .with_context(|| format!("failed to read {}", path.display()))?;
    parse_manifest(&content)
}

#[cfg(test)]
mod tests {
    use super::*;

    const MINIMAL: &str = r#"
schema-version = 1

[distro]
id = "test"
name = "Test"
version = "0.1.0"

[[capsule]]
name = "astrid-capsule-cli"
source = "@unicity-astrid/capsule-cli"
version = "0.1.0"
role = "uplink"
"#;

    #[test]
    fn parse_minimal_manifest() {
        let m = parse_manifest(MINIMAL).unwrap();
        assert_eq!(m.schema_version, 1);
        assert_eq!(m.distro.id, "test");
        assert_eq!(m.distro.name, "Test");
        assert_eq!(m.distro.version, "0.1.0");
        assert_eq!(m.capsules.len(), 1);
        assert_eq!(m.capsules[0].name, "astrid-capsule-cli");
        assert_eq!(m.capsules[0].role.as_deref(), Some("uplink"));
    }

    #[test]
    fn parse_full_manifest() {
        let toml = r#"
schema-version = 1

[distro]
id = "astralis"
name = "Astralis"
pretty-name = "Astralis 0.1.0 (Genesis)"
version = "0.1.0"
codename = "genesis"
release-date = "2026-03-21"
description = "The complete Astrid AI assistant experience"
authors = ["Astrid Core Team"]
maintainers = ["Joshua J. Bouw <josh@unicity-labs.com>"]
homepage = "https://github.com/unicity-astrid/astralis"
support = "https://github.com/unicity-astrid/astrid/discussions"
bug-tracker = "https://github.com/unicity-astrid/astralis/issues"
repository = "https://github.com/unicity-astrid/astralis"
license = "MIT OR Apache-2.0"
astrid-version = ">=0.5.0"

[distro.requires.astrid]
llm = "^1.0"
session = "^1.0"

[variables]
api_key = { secret = true, description = "API key" }
base_url = { description = "Base URL", default = "https://api.openai.com" }

[[capsule]]
name = "astrid-capsule-cli"
source = "@unicity-astrid/capsule-cli"
version = "0.1.0"
role = "uplink"

[[capsule]]
name = "astrid-capsule-openai-compat"
source = "@unicity-astrid/capsule-openai-compat"
version = "0.1.0"
group = "llm"

[capsule.env]
api_key = "{{ api_key }}"
base_url = "{{ base_url }}"
"#;
        let m = parse_manifest(toml).unwrap();
        assert_eq!(m.distro.codename.as_deref(), Some("genesis"));
        assert_eq!(m.distro.maintainers.len(), 1);
        assert_eq!(m.variables.len(), 2);
        assert!(m.variables["api_key"].secret);
        assert_eq!(
            m.variables["base_url"].default.as_deref(),
            Some("https://api.openai.com")
        );
        assert_eq!(m.capsules.len(), 2);
        assert_eq!(m.capsules[1].group.as_deref(), Some("llm"));
        assert_eq!(m.capsules[1].env["api_key"], "{{ api_key }}");
        let requires = &m.distro.requires;
        assert_eq!(requires["astrid"]["llm"], "^1.0");
    }

    #[test]
    fn parse_rejects_wrong_schema_version() {
        let toml = r#"
schema-version = 99

[distro]
id = "test"
name = "Test"
version = "0.1.0"

[[capsule]]
name = "cli"
source = "@org/cli"
version = "0.1.0"
role = "uplink"
"#;
        let err = parse_manifest(toml).unwrap_err();
        assert!(err.to_string().contains("schema-version"), "got: {err}");
    }

    #[test]
    fn parse_rejects_invalid_distro_id() {
        let toml = r#"
schema-version = 1

[distro]
id = "INVALID"
name = "Test"
version = "0.1.0"

[[capsule]]
name = "cli"
source = "@org/cli"
version = "0.1.0"
role = "uplink"
"#;
        let err = parse_manifest(toml).unwrap_err();
        assert!(err.to_string().contains("distro.id"), "got: {err}");
    }

    #[test]
    fn parse_rejects_no_capsules() {
        let toml = r#"
schema-version = 1

[distro]
id = "test"
name = "Test"
version = "0.1.0"
"#;
        let err = parse_manifest(toml).unwrap_err();
        assert!(
            err.to_string().contains("at least one capsule"),
            "got: {err}"
        );
    }

    #[test]
    fn parse_rejects_no_uplink() {
        let toml = r#"
schema-version = 1

[distro]
id = "test"
name = "Test"
version = "0.1.0"

[[capsule]]
name = "astrid-capsule-session"
source = "@org/session"
version = "0.1.0"
"#;
        let err = parse_manifest(toml).unwrap_err();
        assert!(err.to_string().contains("uplink"), "got: {err}");
    }

    #[test]
    fn parse_rejects_duplicate_capsule_names() {
        let toml = r#"
schema-version = 1

[distro]
id = "test"
name = "Test"
version = "0.1.0"

[[capsule]]
name = "astrid-capsule-cli"
source = "@org/cli"
version = "0.1.0"
role = "uplink"

[[capsule]]
name = "astrid-capsule-cli"
source = "@org/cli2"
version = "0.2.0"
role = "uplink"
"#;
        let err = parse_manifest(toml).unwrap_err();
        assert!(err.to_string().contains("duplicate"), "got: {err}");
    }

    #[test]
    fn parse_rejects_undefined_variable_ref() {
        let toml = r#"
schema-version = 1

[distro]
id = "test"
name = "Test"
version = "0.1.0"

[[capsule]]
name = "astrid-capsule-cli"
source = "@org/cli"
version = "0.1.0"
role = "uplink"

[[capsule]]
name = "astrid-capsule-llm"
source = "@org/llm"
version = "0.1.0"

[capsule.env]
key = "{{ undefined_var }}"
"#;
        let err = parse_manifest(toml).unwrap_err();
        assert!(err.to_string().contains("undefined_var"), "got: {err}");
    }

    #[test]
    fn parse_rejects_invalid_distro_version() {
        let toml = r#"
schema-version = 1

[distro]
id = "test"
name = "Test"
version = "not_semver"

[[capsule]]
name = "cli"
source = "@org/cli"
version = "0.1.0"
role = "uplink"
"#;
        let err = parse_manifest(toml).unwrap_err();
        assert!(err.to_string().contains("version"), "got: {err}");
    }
}