sui-spec 0.1.12

Declarative Lisp-authored specs for CppNix-parity behaviors. Rust types are the hard boundary; Lisp forms are the free-middle authoring surface. Both engines (tree-walker + VM) drive the same spec, so they cannot drift.
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
//! Substrate self-description — the typed catalog of every authored
//! sui-spec domain.
//!
//! Sui-spec has grown to 18 typed domains.  Operators + tooling
//! benefit from a typed inventory: "what does this substrate
//! cover?" becomes a typed query rather than a doc-string search.
//! Per the CSE pattern, the catalog is itself a typed Lisp spec —
//! reflection-as-spec.
//!
//! Adding a new domain to sui-spec means landing one
//! `(defsubstrate-domain ...)` form alongside the domain's own
//! module + spec.  Consumers (the future `sui spec list` CLI,
//! generated docs, drift detectors) iterate this catalog
//! mechanically.

use serde::{Deserialize, Serialize};
use tatara_lisp::DeriveTataraDomain;

use crate::SpecError;

/// One substrate domain — a typed border + Lisp spec inside
/// sui-spec.  Catalog entries name the domain itself, not the
/// types it owns.
#[derive(DeriveTataraDomain, Serialize, Deserialize, Debug, Clone)]
#[tatara(keyword = "defsubstrate-domain")]
pub struct SubstrateDomain {
    /// Module name (`"derivation"`, `"fetcher"`, ...).
    pub name: String,
    /// Lisp authoring keyword(s) the domain exposes.
    #[serde(rename = "authoringKeywords")]
    pub authoring_keywords: Vec<String>,
    /// Implementation maturity gate (M0..M5).
    #[serde(rename = "gate")]
    pub gate: MaturityGate,
    /// What this domain covers in one short phrase.
    pub purpose: String,
    /// Which cppnix subsystem the domain mirrors.
    #[serde(rename = "cppnixMirror")]
    pub cppnix_mirror: String,
    /// Other domains this one depends on (by name).  Forms the
    /// substrate dependency graph — `activation_script` depends
    /// on `module_system`; `fetcher` depends on `derivation` (FOD
    /// variant); etc.  Adding a new domain means declaring its
    /// dependencies here.
    #[serde(default, rename = "dependsOn")]
    pub depends_on: Vec<String>,
}

/// Implementation maturity level.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MaturityGate {
    /// Substrate primitive + interpreter — production-ready.
    Working,
    /// Typed border + canonical Lisp authored; interpreter is
    /// scoped to M2 (module system).
    M2TypedOnly,
    /// Typed border + canonical Lisp authored; interpreter is
    /// scoped to M3 (everything depending on the module system).
    M3TypedOnly,
    /// Typed border + canonical Lisp authored; interpreter is
    /// scoped to M4 (CA-derivations + dependent flow).
    M4TypedOnly,
    /// Informational only — no interpreter planned (e.g. format
    /// declarations, layout conventions).
    Informational,
}

pub const CANONICAL_CATALOG_LISP: &str = include_str!("../specs/catalog.lisp");

/// Compile the substrate catalog.
///
/// # Errors
///
/// Returns an error if the Lisp source fails to parse.
pub fn load_canonical() -> Result<Vec<SubstrateDomain>, SpecError> {
    crate::loader::load_all::<SubstrateDomain>(CANONICAL_CATALOG_LISP)
}

/// Find a domain entry by name.
///
/// # Errors
///
/// Returns an error if the catalog fails to parse or no entry matches.
pub fn lookup(name: &str) -> Result<SubstrateDomain, SpecError> {
    load_canonical()?
        .into_iter()
        .find(|d| d.name == name)
        .ok_or_else(|| SpecError::Load(format!(
            "no (defsubstrate-domain) with :name {name:?}",
        )))
}

/// Count of domains by maturity gate.
///
/// # Errors
///
/// Returns an error if the catalog fails to parse.
pub fn maturity_histogram() -> Result<std::collections::BTreeMap<&'static str, usize>, SpecError> {
    let cat = load_canonical()?;
    let mut h: std::collections::BTreeMap<&'static str, usize> =
        std::collections::BTreeMap::new();
    for d in &cat {
        let key = match d.gate {
            MaturityGate::Working => "Working",
            MaturityGate::M2TypedOnly => "M2TypedOnly",
            MaturityGate::M3TypedOnly => "M3TypedOnly",
            MaturityGate::M4TypedOnly => "M4TypedOnly",
            MaturityGate::Informational => "Informational",
        };
        *h.entry(key).or_default() += 1;
    }
    Ok(h)
}

/// Topologically sort the catalog so leaves (no dependencies)
/// come first.  This is the order an M2/M3/M4 implementer should
/// land interpreters in — implementing `derivation` requires
/// `hash` and `store_layout` to be working first.
///
/// # Errors
///
/// Returns `SpecError::Interp` with phase `dependency-cycle` if
/// the substrate graph isn't a DAG.
pub fn topological_order() -> Result<Vec<SubstrateDomain>, SpecError> {
    let cat = load_canonical()?;
    let by_name: std::collections::HashMap<String, SubstrateDomain> =
        cat.iter().cloned().map(|d| (d.name.clone(), d)).collect();
    let names: Vec<&str> = cat.iter().map(|d| d.name.as_str()).collect();

    // Kahn's algorithm — start with zero-in-degree nodes, peel.
    let mut indeg: std::collections::HashMap<&str, usize> =
        names.iter().map(|n| (*n, 0)).collect();
    for d in &cat {
        for dep in &d.depends_on {
            // d depends on dep → there's an edge dep → d, so d's
            // in-degree increases.
            *indeg.entry(d.name.as_str()).or_default() += 1;
            // touch the dep so it's in the map even if no other
            // domain depends on it
            indeg.entry(dep.as_str()).or_default();
        }
    }

    // Take zero-in-degree nodes, sorted by name for determinism.
    let mut frontier: Vec<&str> = indeg
        .iter()
        .filter(|(_, deg)| **deg == 0)
        .map(|(name, _)| *name)
        .collect();
    frontier.sort();

    let mut out: Vec<SubstrateDomain> = Vec::new();
    while let Some(name) = frontier.pop() {
        let domain = by_name.get(name).cloned().ok_or_else(|| {
            SpecError::Load(format!("internal: topological_order saw unknown `{name}`"))
        })?;
        out.push(domain);
        // For each child that depends on `name`, decrement its
        // in-degree; if it hits zero, add to frontier.
        let children: Vec<&str> = cat
            .iter()
            .filter(|d| d.depends_on.iter().any(|x| x == name))
            .map(|d| d.name.as_str())
            .collect();
        for child in children {
            let entry = indeg.entry(child).or_default();
            *entry = entry.saturating_sub(1);
            if *entry == 0 {
                frontier.push(child);
                frontier.sort();
            }
        }
    }

    if out.len() != cat.len() {
        return Err(SpecError::Interp {
            phase: "dependency-cycle".into(),
            message: format!(
                "topological_order produced {} of {} entries — cycle present",
                out.len(),
                cat.len(),
            ),
        });
    }
    Ok(out)
}

/// Compute the transitive dependency closure of one domain (the
/// set of all domains reachable via `depends_on` edges, including
/// the domain itself).
///
/// # Errors
///
/// Returns `SpecError::Load` if the catalog fails to parse or
/// `name` is missing.  Returns `SpecError::Interp` with phase
/// `dependency-cycle` if the graph contains a cycle.
pub fn transitive_dependencies(name: &str) -> Result<std::collections::BTreeSet<String>, SpecError> {
    let cat = load_canonical()?;
    let by_name: std::collections::HashMap<&str, &SubstrateDomain> =
        cat.iter().map(|d| (d.name.as_str(), d)).collect();
    if !by_name.contains_key(name) {
        return Err(SpecError::Load(format!("domain `{name}` not in catalog")));
    }
    let mut seen: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    let mut stack: Vec<String> = vec![name.to_string()];
    let mut in_path: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    while let Some(current) = stack.pop() {
        if !seen.insert(current.clone()) {
            continue;
        }
        if !in_path.insert(current.clone()) {
            return Err(SpecError::Interp {
                phase: "dependency-cycle".into(),
                message: format!("cycle detected involving `{current}`"),
            });
        }
        let Some(domain) = by_name.get(current.as_str()) else {
            return Err(SpecError::Load(format!(
                "domain `{current}` referenced but not in catalog",
            )));
        };
        for dep in &domain.depends_on {
            stack.push(dep.clone());
        }
    }
    Ok(seen)
}

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

    #[test]
    fn catalog_parses() {
        let cat = load_canonical().expect("catalog must compile");
        assert!(
            cat.len() >= 15,
            "catalog must enumerate at least 15 domains, got {}",
            cat.len(),
        );
    }

    #[test]
    fn every_authored_domain_is_in_catalog() {
        let cat = load_canonical().unwrap();
        let names: HashSet<&str> = cat.iter().map(|d| d.name.as_str()).collect();
        // The 18 domains today.  If a new domain lands without a
        // catalog entry, this test fires.
        for required in [
            "derivation",
            "realisation",
            "module_system",
            "activation_script",
            "flake",
            "lock_file",
            "registry",
            "fetcher",
            "substituter",
            "sandbox",
            "store_layout",
            "gc",
            "hash",
            "nar",
            "narinfo",
            "eval_cache",
            "profile",
            "trust_model",
            "worker_protocol",
        ] {
            assert!(
                names.contains(required),
                "catalog missing domain `{required}` — sui-spec/src/{required}.rs \
                 exists but its catalog entry doesn't",
            );
        }
    }

    #[test]
    fn maturity_histogram_sums_to_catalog_size() {
        let cat = load_canonical().unwrap();
        let h = maturity_histogram().unwrap();
        let total: usize = h.values().sum();
        assert_eq!(total, cat.len());
    }

    #[test]
    fn working_domains_include_the_known_three() {
        let cat = load_canonical().unwrap();
        let working: HashSet<&str> = cat
            .iter()
            .filter(|d| d.gate == MaturityGate::Working)
            .map(|d| d.name.as_str())
            .collect();
        // These three have full implementations on the substrate.
        for required in ["derivation", "flake"] {
            assert!(
                working.contains(required),
                "catalog: {required} should be Working — substrate has the impl today",
            );
        }
    }

    #[test]
    fn lookup_finds_known_domain() {
        let d = lookup("derivation").expect("derivation must be in catalog");
        assert_eq!(d.name, "derivation");
        assert!(d.purpose.len() > 10);
    }

    #[test]
    fn lookup_errors_on_missing() {
        let err = lookup("nonexistent-domain").expect_err("must error on unknown");
        match err {
            SpecError::Load(msg) => assert!(msg.contains("nonexistent-domain")),
            _ => panic!("expected SpecError::Load"),
        }
    }

    #[test]
    fn transitive_dependencies_of_activation_includes_module_system() {
        let deps = transitive_dependencies("activation_script").unwrap();
        // activation_script depends on module_system (M2 gate) +
        // derivation; derivation depends on hash + store_layout.
        // Closure must contain all five.
        for required in [
            "activation_script", // self
            "module_system",
            "derivation",
            "hash",
            "store_layout",
        ] {
            assert!(
                deps.contains(required),
                "transitive deps missing `{required}`: {deps:?}",
            );
        }
    }

    #[test]
    fn transitive_dependencies_of_hash_is_just_itself() {
        let deps = transitive_dependencies("hash").unwrap();
        assert_eq!(deps.len(), 1);
        assert!(deps.contains("hash"));
    }

    #[test]
    fn every_declared_dependency_exists_in_catalog() {
        let cat = load_canonical().unwrap();
        let names: std::collections::HashSet<&str> =
            cat.iter().map(|d| d.name.as_str()).collect();
        for d in &cat {
            for dep in &d.depends_on {
                assert!(
                    names.contains(dep.as_str()),
                    "domain `{}` declares dependency on `{dep}`, \
                     which is not in the catalog",
                    d.name,
                );
            }
        }
    }

    #[test]
    fn substrate_graph_has_no_cycles() {
        let cat = load_canonical().unwrap();
        for d in &cat {
            let _ = transitive_dependencies(&d.name)
                .unwrap_or_else(|e| panic!("cycle starting from `{}`: {e:?}", d.name));
        }
    }

    #[test]
    fn topological_order_covers_catalog() {
        let topo = topological_order().unwrap();
        let cat = load_canonical().unwrap();
        assert_eq!(topo.len(), cat.len(), "topo skipped some domains");
        let names: std::collections::HashSet<&str> =
            topo.iter().map(|d| d.name.as_str()).collect();
        for c in &cat {
            assert!(names.contains(c.name.as_str()), "missing `{}` in topo", c.name);
        }
    }

    #[test]
    fn topological_order_respects_dependencies() {
        let topo = topological_order().unwrap();
        let pos: std::collections::HashMap<&str, usize> = topo
            .iter()
            .enumerate()
            .map(|(i, d)| (d.name.as_str(), i))
            .collect();
        for d in &topo {
            for dep in &d.depends_on {
                assert!(
                    pos[dep.as_str()] < pos[d.name.as_str()],
                    "topo violates: `{}` (pos {}) depends on `{dep}` (pos {})",
                    d.name,
                    pos[d.name.as_str()],
                    pos[dep.as_str()],
                );
            }
        }
    }
}