metactl 0.1.7

metactl v2 reference kernel and JSON-RPC service
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
use std::collections::{BTreeMap, BTreeSet};

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

use crate::types::{Ref, RefKind, TrustTier, VisibilityScope, API_VERSION};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum LibrarySourceRole {
    Baseline,
    Overlay,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum LibrarySourceType {
    LocalPath,
    Git,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LibrarySourceLocation {
    #[serde(rename = "type")]
    pub source_type: LibrarySourceType,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(default, rename = "ref", skip_serializing_if = "Option::is_none")]
    pub ref_: Option<String>,
    pub digest: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactOverridePolicy {
    None,
    AllowOverlay,
    AllowBaselinePrecedence,
}

impl Default for ArtifactOverridePolicy {
    fn default() -> Self {
        Self::None
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LibraryArtifactManifest {
    pub artifact_ref: Ref,
    pub digest: String,
    pub source_path: String,
    #[serde(default)]
    pub locked: bool,
    #[serde(default)]
    pub override_policy: ArtifactOverridePolicy,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub override_reason: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LibrarySourceManifest {
    pub kind: String,
    pub id: String,
    pub version: String,
    pub title: String,
    pub source_role: LibrarySourceRole,
    pub read_only: bool,
    pub writable: bool,
    pub pinned: bool,
    pub visibility_scope: VisibilityScope,
    pub trust_tier: TrustTier,
    pub source: LibrarySourceLocation,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub artifacts: Vec<LibraryArtifactManifest>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum BaselinePrecedenceMode {
    Explicit,
    FailOnConflict,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CommittedProjectionConfig {
    pub enabled: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tracked_paths: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LibraryProfileManifest {
    pub kind: String,
    pub id: String,
    pub version: String,
    pub title: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub baseline_refs: Vec<String>,
    pub overlay_ref: String,
    pub baseline_precedence: BaselinePrecedenceMode,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub default_targets: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub committed_projection: Option<CommittedProjectionConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LibraryStackManifest {
    pub api_version: String,
    pub kind: String,
    pub id: String,
    pub version: String,
    pub title: String,
    pub active_profile_ref: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub sources: Vec<LibrarySourceManifest>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub profiles: Vec<LibraryProfileManifest>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ResolvedOverrideStatus {
    None,
    OverrodeBaseline,
    BaselinePrecedence,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ResolvedLibraryArtifact {
    pub artifact_ref: Ref,
    pub source_id: String,
    pub source_role: LibrarySourceRole,
    pub source_digest: String,
    pub artifact_digest: String,
    pub locked: bool,
    pub override_status: ResolvedOverrideStatus,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub generated_paths: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LibraryStackLock {
    pub api_version: String,
    pub kind: String,
    pub stack_ref: Ref,
    pub profile_ref: Ref,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub resolved_artifacts: Vec<ResolvedLibraryArtifact>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub conflicts: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone)]
struct ResolutionCandidate<'a> {
    source: &'a LibrarySourceManifest,
    artifact: &'a LibraryArtifactManifest,
    override_status: ResolvedOverrideStatus,
}

pub fn resolve_library_stack(stack: &LibraryStackManifest) -> Result<LibraryStackLock> {
    if stack.api_version != API_VERSION {
        return Err(anyhow!(
            "METACTL_STACK_API_VERSION: expected {API_VERSION}, got {}",
            stack.api_version
        ));
    }
    let sources = sources_by_id(stack)?;
    let profile = active_profile(stack)?;
    let overlay = sources.get(profile.overlay_ref.as_str()).ok_or_else(|| {
        anyhow!(
            "METACTL_STACK_OVERLAY_NOT_FOUND: overlay {}",
            profile.overlay_ref
        )
    })?;
    if overlay.source_role != LibrarySourceRole::Overlay || overlay.read_only || !overlay.writable {
        return Err(anyhow!(
            "METACTL_STACK_INVALID_OVERLAY: {} must be the single writable overlay",
            overlay.id
        ));
    }

    let mut ordered_sources = Vec::new();
    for baseline_ref in &profile.baseline_refs {
        let baseline = sources
            .get(baseline_ref.as_str())
            .ok_or_else(|| anyhow!("METACTL_STACK_BASELINE_NOT_FOUND: baseline {baseline_ref}"))?;
        if baseline.source_role != LibrarySourceRole::Baseline
            || !baseline.read_only
            || baseline.writable
        {
            return Err(anyhow!(
                "METACTL_STACK_INVALID_BASELINE: {} must be read-only",
                baseline.id
            ));
        }
        if !baseline.pinned {
            return Err(anyhow!(
                "METACTL_STACK_UNPINNED_BASELINE: baseline {} must be pinned",
                baseline.id
            ));
        }
        ordered_sources.push(*baseline);
    }
    ordered_sources.push(*overlay);

    let mut resolved: BTreeMap<String, ResolutionCandidate<'_>> = BTreeMap::new();
    for source in ordered_sources {
        for artifact in &source.artifacts {
            let key = artifact_identity_key(&artifact.artifact_ref);
            match resolved.get_mut(&key) {
                None => {
                    resolved.insert(
                        key,
                        ResolutionCandidate {
                            source,
                            artifact,
                            override_status: ResolvedOverrideStatus::None,
                        },
                    );
                }
                Some(existing) => {
                    resolve_collision(profile, source, artifact, existing, &key)?;
                }
            }
        }
    }

    Ok(LibraryStackLock {
        api_version: API_VERSION.to_string(),
        kind: "library_stack_lock".to_string(),
        stack_ref: Ref {
            kind: RefKind::Artifact,
            id: stack.id.clone(),
            version: Some(stack.version.clone()),
        },
        profile_ref: Ref {
            kind: RefKind::Artifact,
            id: profile.id.clone(),
            version: Some(profile.version.clone()),
        },
        resolved_artifacts: resolved
            .values()
            .map(|candidate| ResolvedLibraryArtifact {
                artifact_ref: candidate.artifact.artifact_ref.clone(),
                source_id: candidate.source.id.clone(),
                source_role: candidate.source.source_role.clone(),
                source_digest: candidate.source.source.digest.clone(),
                artifact_digest: candidate.artifact.digest.clone(),
                locked: candidate.artifact.locked,
                override_status: candidate.override_status.clone(),
                generated_paths: Vec::new(),
            })
            .collect(),
        conflicts: Vec::new(),
        warnings: Vec::new(),
    })
}

fn artifact_identity_key(ref_: &Ref) -> String {
    format!("{:?}:{}", ref_.kind, ref_.id)
}

fn sources_by_id(stack: &LibraryStackManifest) -> Result<BTreeMap<&str, &LibrarySourceManifest>> {
    let mut sources = BTreeMap::new();
    for source in &stack.sources {
        if sources.insert(source.id.as_str(), source).is_some() {
            return Err(anyhow!(
                "METACTL_STACK_DUPLICATE_SOURCE: source {}",
                source.id
            ));
        }
    }
    Ok(sources)
}

fn active_profile(stack: &LibraryStackManifest) -> Result<&LibraryProfileManifest> {
    let matches = stack
        .profiles
        .iter()
        .filter(|profile| profile.id == stack.active_profile_ref)
        .collect::<Vec<_>>();
    match matches.as_slice() {
        [profile] => Ok(*profile),
        [] => Err(anyhow!(
            "METACTL_STACK_PROFILE_NOT_FOUND: active profile {}",
            stack.active_profile_ref
        )),
        _ => Err(anyhow!(
            "METACTL_STACK_DUPLICATE_PROFILE: active profile {}",
            stack.active_profile_ref
        )),
    }
}

fn resolve_collision<'a>(
    profile: &LibraryProfileManifest,
    source: &'a LibrarySourceManifest,
    artifact: &'a LibraryArtifactManifest,
    existing: &mut ResolutionCandidate<'a>,
    key: &str,
) -> Result<()> {
    if source.source_role == LibrarySourceRole::Overlay {
        if existing.artifact.locked {
            return Err(anyhow!(
                "METACTL_STACK_LOCKED_OVERRIDE: {key} from {} cannot be overridden by {}",
                existing.source.id,
                source.id
            ));
        }
        if existing.artifact.override_policy == ArtifactOverridePolicy::AllowOverlay {
            *existing = ResolutionCandidate {
                source,
                artifact,
                override_status: ResolvedOverrideStatus::OverrodeBaseline,
            };
            return Ok(());
        }
        return Err(anyhow!(
            "METACTL_STACK_ACCIDENTAL_COLLISION: {key} from {} conflicts with {}",
            existing.source.id,
            source.id
        ));
    }

    if existing.source.source_role == LibrarySourceRole::Baseline
        && source.source_role == LibrarySourceRole::Baseline
    {
        if profile.baseline_precedence == BaselinePrecedenceMode::Explicit
            && existing.artifact.override_policy == ArtifactOverridePolicy::AllowBaselinePrecedence
        {
            existing.override_status = ResolvedOverrideStatus::BaselinePrecedence;
            return Ok(());
        }
        return Err(anyhow!(
            "METACTL_STACK_BASELINE_CONFLICT: {key} from {} conflicts with {}",
            existing.source.id,
            source.id
        ));
    }

    Err(anyhow!("METACTL_STACK_ACCIDENTAL_COLLISION: {key}"))
}

pub fn active_stack_source_ids(stack: &LibraryStackManifest) -> Result<Vec<String>> {
    let sources = sources_by_id(stack)?;
    let profile = active_profile(stack)?;
    let mut seen = BTreeSet::new();
    let mut ids = Vec::new();
    for id in profile
        .baseline_refs
        .iter()
        .chain(std::iter::once(&profile.overlay_ref))
    {
        if !sources.contains_key(id.as_str()) {
            return Err(anyhow!("METACTL_STACK_SOURCE_NOT_FOUND: source {id}"));
        }
        if seen.insert(id.clone()) {
            ids.push(id.clone());
        }
    }
    Ok(ids)
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;
    use std::path::PathBuf;

    use super::{
        resolve_library_stack, LibraryStackLock, LibraryStackManifest, ResolvedLibraryArtifact,
    };

    fn fixture_root() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/library_stack")
    }

    fn load_stack(case: &str) -> LibraryStackManifest {
        let path = fixture_root().join(case).join("stack.json");
        serde_json::from_slice(&std::fs::read(path).expect("stack bytes")).expect("stack")
    }

    fn load_lock(case: &str) -> LibraryStackLock {
        let path = fixture_root().join(case).join("lock.json");
        serde_json::from_slice(&std::fs::read(path).expect("lock bytes")).expect("lock")
    }

    fn artifact_map(lock: &LibraryStackLock) -> BTreeMap<String, ResolvedLibraryArtifact> {
        lock.resolved_artifacts
            .iter()
            .cloned()
            .map(|item| {
                (
                    format!("{:?}:{}", item.artifact_ref.kind, item.artifact_ref.id),
                    item,
                )
            })
            .collect()
    }

    #[test]
    fn resolves_positive_stack_fixtures_to_expected_locks() {
        for case in [
            "user-only",
            "one-baseline",
            "multi-baseline",
            "allowed-override",
        ] {
            let stack = load_stack(case);
            let expected = load_lock(case);
            let actual = resolve_library_stack(&stack).expect(case);
            assert_eq!(actual.api_version, expected.api_version, "{case}");
            assert_eq!(actual.kind, expected.kind, "{case}");
            assert_eq!(actual.stack_ref, expected.stack_ref, "{case}");
            assert_eq!(actual.profile_ref, expected.profile_ref, "{case}");
            assert_eq!(artifact_map(&actual), artifact_map(&expected), "{case}");
        }
    }

    #[test]
    fn rejects_locked_baseline_override() {
        let stack = load_stack("locked-conflict");
        let err = resolve_library_stack(&stack).expect_err("locked override should fail");
        assert!(err.to_string().contains("METACTL_STACK_LOCKED_OVERRIDE"));
    }

    #[test]
    fn rejects_accidental_collision() {
        let stack = load_stack("accidental-collision");
        let err = resolve_library_stack(&stack).expect_err("collision should fail");
        assert!(err
            .to_string()
            .contains("METACTL_STACK_ACCIDENTAL_COLLISION"));
    }
}