a4-cli 0.38.0

CLI tool for generating TypeScript SDKs from Arete stream specifications
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Component, Path, PathBuf};

use anyhow::{bail, Context, Result};
use semver::VersionReq;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

pub const MANIFEST_VERSION: u32 = 1;

#[derive(Debug, Clone)]
pub struct ProjectManifest {
    pub path: PathBuf,
    pub root: PathBuf,
    pub document: ManifestV1,
    pub manifest_hash: String,
}

impl ProjectManifest {
    pub fn load(path: impl AsRef<Path>) -> Result<Self> {
        let supplied = path.as_ref();
        let bytes = fs::read(supplied)
            .with_context(|| format!("Failed to read project manifest {}", supplied.display()))?;
        let source = std::str::from_utf8(&bytes)
            .with_context(|| format!("Project manifest {} is not UTF-8", supplied.display()))?;
        let value: toml::Value = toml::from_str(source)
            .with_context(|| format!("Failed to parse project manifest {}", supplied.display()))?;

        let version = value
            .get("manifest_version")
            .and_then(toml::Value::as_integer);
        if version.is_none() && value.get("stacks").is_some() {
            bail!(
                "{} uses the removed [[stacks]] configuration. Declare authored artifacts under [authoring] and installable SDKs under [dependencies]; .stack.json inputs are not inspected or converted",
                supplied.display()
            );
        }
        match version {
            Some(version) if version == i64::from(MANIFEST_VERSION) => {}
            Some(version) => bail!(
                "Unsupported manifest_version {version} in {}; this CLI supports manifest_version = {MANIFEST_VERSION}",
                supplied.display()
            ),
            None => bail!(
                "{} is missing required manifest_version = {MANIFEST_VERSION}",
                supplied.display()
            ),
        }

        let document: ManifestV1 = toml::from_str(source).with_context(|| {
            format!(
                "Failed to decode strict manifest_version = {MANIFEST_VERSION} document {}",
                supplied.display()
            )
        })?;
        document.validate()?;

        let path = fs::canonicalize(supplied)
            .with_context(|| format!("Failed to resolve manifest path {}", supplied.display()))?;
        let root = path
            .parent()
            .ok_or_else(|| anyhow::anyhow!("Project manifest has no parent directory"))?
            .to_path_buf();
        let manifest_hash = document.resolution_hash()?;
        Ok(Self {
            path,
            root,
            document,
            manifest_hash,
        })
    }

    pub fn dependency(&self, kind: DependencyKind, alias: &str) -> Option<&DependencyV1> {
        match kind {
            DependencyKind::Stack => self.document.dependencies.stacks.get(alias),
            DependencyKind::Program => self.document.dependencies.programs.get(alias),
        }
    }

    pub fn dependencies(&self) -> impl Iterator<Item = (DependencyKind, &String, &DependencyV1)> {
        self.document
            .dependencies
            .stacks
            .iter()
            .map(|(alias, dependency)| (DependencyKind::Stack, alias, dependency))
            .chain(
                self.document
                    .dependencies
                    .programs
                    .iter()
                    .map(|(alias, dependency)| (DependencyKind::Program, alias, dependency)),
            )
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ManifestV1 {
    pub manifest_version: u32,
    pub project: ProjectV1,
    #[serde(default)]
    pub install: InstallV1,
    #[serde(default)]
    pub sdk: SdkV1,
    #[serde(default)]
    pub dependencies: DependenciesV1,
    #[serde(default)]
    pub authoring: AuthoringV1,
}

impl ManifestV1 {
    pub fn new(project_name: String) -> Self {
        Self {
            manifest_version: MANIFEST_VERSION,
            project: ProjectV1 {
                name: project_name,
                private: false,
            },
            install: InstallV1::default(),
            sdk: SdkV1::default(),
            dependencies: DependenciesV1::default(),
            authoring: AuthoringV1::default(),
        }
    }

    pub fn validate(&self) -> Result<()> {
        if self.manifest_version != MANIFEST_VERSION {
            bail!("manifest_version must be {MANIFEST_VERSION}");
        }
        if self.project.name.trim().is_empty() {
            bail!("project.name cannot be empty");
        }
        validate_targets(&self.sdk.targets, "sdk.targets", true)?;

        for (kind, entries) in [
            (DependencyKind::Stack, &self.dependencies.stacks),
            (DependencyKind::Program, &self.dependencies.programs),
        ] {
            for (alias, dependency) in entries {
                validate_alias(alias, "dependency alias")?;
                dependency.validate(kind, alias, &self.sdk.targets, &self.authoring)?;
            }
        }

        for (name, stack) in &self.authoring.stacks {
            validate_alias(name, "authoring stack name")?;
            stack.validate(name)?;
            if stack
                .deployment_name
                .as_deref()
                .is_some_and(|value| value.trim().is_empty())
            {
                bail!("authoring stack '{name}' has an empty deployment_name");
            }
        }
        for (name, program) in &self.authoring.programs {
            validate_alias(name, "authoring program name")?;
            validate_relative_artifact_path(&program.program_spec, ArtifactPathKind::ProgramSpec)?;
        }
        Ok(())
    }

    pub fn resolution_hash(&self) -> Result<String> {
        let bytes = serde_json::to_vec(self)?;
        let digest = Sha256::digest(bytes);
        Ok(format!("arete-manifest-v1:{digest:x}"))
    }

    pub fn to_toml_pretty(&self) -> Result<String> {
        let mut value = toml::Value::try_from(self).context("Failed to encode project manifest")?;
        compact_manifest_value(&mut value)?;
        toml::to_string_pretty(&value).context("Failed to serialize project manifest")
    }
}

fn compact_manifest_value(value: &mut toml::Value) -> Result<()> {
    let root = value
        .as_table_mut()
        .ok_or_else(|| anyhow::anyhow!("Project manifest did not encode as a TOML table"))?;

    if let Some(project) = root.get_mut("project").and_then(toml::Value::as_table_mut) {
        if project.get("private").and_then(toml::Value::as_bool) == Some(false) {
            project.remove("private");
        }
    }

    let default_install = toml::Value::try_from(InstallV1::default())?;
    if root.get("install") == Some(&default_install) {
        root.remove("install");
    }

    let default_sdk = toml::Value::try_from(SdkV1::default())?;
    let default_sdk = default_sdk
        .as_table()
        .ok_or_else(|| anyhow::anyhow!("Default SDK configuration did not encode as a table"))?;
    let remove_sdk = if let Some(sdk) = root.get_mut("sdk").and_then(toml::Value::as_table_mut) {
        if sdk.get("targets") == default_sdk.get("targets") {
            sdk.remove("targets");
        }
        for language in ["typescript", "rust", "python"] {
            let remove_language = if let (Some(configuration), Some(default_configuration)) = (
                sdk.get_mut(language).and_then(toml::Value::as_table_mut),
                default_sdk.get(language).and_then(toml::Value::as_table),
            ) {
                for (key, default_value) in default_configuration {
                    if configuration.get(key) == Some(default_value) {
                        configuration.remove(key);
                    }
                }
                configuration.is_empty()
            } else {
                false
            };
            if remove_language {
                sdk.remove(language);
            }
        }
        sdk.is_empty()
    } else {
        false
    };
    if remove_sdk {
        root.remove("sdk");
    }

    let remove_dependencies = if let Some(dependencies) = root
        .get_mut("dependencies")
        .and_then(toml::Value::as_table_mut)
    {
        for kind in ["stacks", "programs"] {
            let remove_kind = if let Some(entries) = dependencies
                .get_mut(kind)
                .and_then(toml::Value::as_table_mut)
            {
                for dependency in entries
                    .iter_mut()
                    .filter_map(|(_, value)| value.as_table_mut())
                {
                    if dependency
                        .get("outputs")
                        .and_then(toml::Value::as_table)
                        .is_some_and(toml::map::Map::is_empty)
                    {
                        dependency.remove("outputs");
                    }
                }
                entries.is_empty()
            } else {
                false
            };
            if remove_kind {
                dependencies.remove(kind);
            }
        }
        dependencies.is_empty()
    } else {
        false
    };
    if remove_dependencies {
        root.remove("dependencies");
    }

    let remove_authoring = if let Some(authoring) = root
        .get_mut("authoring")
        .and_then(toml::Value::as_table_mut)
    {
        for kind in ["stacks", "programs"] {
            if authoring
                .get(kind)
                .and_then(toml::Value::as_table)
                .is_some_and(toml::map::Map::is_empty)
            {
                authoring.remove(kind);
            }
        }
        authoring.is_empty()
    } else {
        false
    };
    if remove_authoring {
        root.remove("authoring");
    }
    Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProjectV1 {
    pub name: String,
    #[serde(default)]
    pub private: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InstallV1 {
    #[serde(default)]
    pub allow_outside_project: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SdkV1 {
    #[serde(default = "default_targets")]
    pub targets: Vec<InstallTarget>,
    #[serde(default)]
    pub typescript: TypeScriptSdkV1,
    #[serde(default)]
    pub rust: RustSdkV1,
    #[serde(default)]
    pub python: PythonSdkV1,
}

impl Default for SdkV1 {
    fn default() -> Self {
        Self {
            targets: default_targets(),
            typescript: TypeScriptSdkV1::default(),
            rust: RustSdkV1::default(),
            python: PythonSdkV1::default(),
        }
    }
}

fn default_targets() -> Vec<InstallTarget> {
    vec![InstallTarget::TypeScript, InstallTarget::Rust]
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TypeScriptSdkV1 {
    #[serde(default = "default_typescript_output")]
    pub output_dir: String,
    #[serde(default = "default_typescript_package")]
    pub package: String,
}

impl Default for TypeScriptSdkV1 {
    fn default() -> Self {
        Self {
            output_dir: default_typescript_output(),
            package: default_typescript_package(),
        }
    }
}

fn default_typescript_output() -> String {
    "./generated/typescript".into()
}

fn default_typescript_package() -> String {
    "@usearete/react".into()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RustSdkV1 {
    #[serde(default = "default_rust_output")]
    pub output_dir: String,
    #[serde(default)]
    pub module_mode: bool,
    #[serde(default)]
    pub crate_prefix: String,
}

impl Default for RustSdkV1 {
    fn default() -> Self {
        Self {
            output_dir: default_rust_output(),
            module_mode: false,
            crate_prefix: String::new(),
        }
    }
}

fn default_rust_output() -> String {
    "./generated/rust".into()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PythonSdkV1 {
    #[serde(default = "default_python_output")]
    pub output_dir: String,
    #[serde(default)]
    pub module_mode: bool,
    #[serde(default)]
    pub package_prefix: String,
}

impl Default for PythonSdkV1 {
    fn default() -> Self {
        Self {
            output_dir: default_python_output(),
            module_mode: false,
            package_prefix: String::new(),
        }
    }
}

fn default_python_output() -> String {
    "./generated/python".into()
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DependenciesV1 {
    #[serde(default)]
    pub stacks: BTreeMap<String, DependencyV1>,
    #[serde(default)]
    pub programs: BTreeMap<String, DependencyV1>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DependencyV1 {
    pub source: DependencySourceV1,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub targets: Option<Vec<InstallTarget>>,
    #[serde(default)]
    pub outputs: DependencyOutputsV1,
    /// Where the generated SDK reads a registry stack, per LiveSpec alias,
    /// instead of the endpoints the stack's delivery provides: the user's own
    /// deployment, which `a4 up <alias>` records.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub endpoints: BTreeMap<String, StackEndpointsV1>,
}

/// One LiveSpec's stream endpoints in a user's own deployment.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct StackEndpointsV1 {
    pub websocket: String,
    pub query: String,
}

impl DependencyV1 {
    fn validate(
        &self,
        kind: DependencyKind,
        alias: &str,
        supported_targets: &[InstallTarget],
        authoring: &AuthoringV1,
    ) -> Result<()> {
        match &self.source {
            DependencySourceV1::Registry(RegistrySourceV1 { registry }) => {
                validate_registry_package(registry)?;
                let requirement = self.version.as_deref().ok_or_else(|| {
                    anyhow::anyhow!(
                        "registry dependency '{alias}' requires a semantic version requirement"
                    )
                })?;
                if matches!(requirement, "latest" | "stable" | "next") {
                    bail!("registry dependency '{alias}' cannot use mutable tag '{requirement}'");
                }
                VersionReq::parse(requirement).with_context(|| {
                    format!("dependency '{alias}' has invalid version requirement '{requirement}'")
                })?;
            }
            DependencySourceV1::Path(PathSourceV1 { path }) => {
                if self.version.is_some() {
                    bail!("path dependency '{alias}' cannot declare version");
                }
                validate_relative_artifact_path(
                    path,
                    match kind {
                        DependencyKind::Stack => ArtifactPathKind::StackManifest,
                        DependencyKind::Program => ArtifactPathKind::ProgramSpec,
                    },
                )?;
            }
            DependencySourceV1::Workspace(WorkspaceSourceV1 { workspace }) => {
                if self.version.is_some() {
                    bail!("workspace dependency '{alias}' cannot declare version");
                }
                validate_alias(workspace, "workspace source")?;
                let exists = match kind {
                    DependencyKind::Stack => authoring.stacks.contains_key(workspace),
                    DependencyKind::Program => authoring.programs.contains_key(workspace),
                };
                if !exists {
                    bail!(
                        "workspace dependency '{alias}' refers to missing same-kind authoring entry '{workspace}'"
                    );
                }
            }
        }

        if !self.endpoints.is_empty() {
            // A registry stack, or a composed stack of this project: both
            // generate from exact LiveSpecs a deployment of them serves.
            let composed_workspace = matches!(
                &self.source,
                DependencySourceV1::Workspace(WorkspaceSourceV1 { workspace })
                    if authoring.stacks.get(workspace).is_some_and(AuthoringStackV1::is_composed)
            );
            if kind != DependencyKind::Stack
                || !(matches!(self.source, DependencySourceV1::Registry(_)) || composed_workspace)
            {
                bail!(
                    "dependency '{alias}' declares endpoints; only a registry stack or a composed [authoring.stacks] entry can"
                );
            }
            for (live, endpoints) in &self.endpoints {
                validate_live_alias(live, alias)?;
                validate_endpoint(
                    &endpoints.websocket,
                    &["ws", "wss"],
                    &format!("dependency '{alias}' endpoints.{live}.websocket"),
                )?;
                validate_endpoint(
                    &endpoints.query,
                    &["http", "https"],
                    &format!("dependency '{alias}' endpoints.{live}.query"),
                )?;
            }
        }

        let targets = self.targets.as_deref().unwrap_or(supported_targets);
        validate_targets(targets, &format!("dependency '{alias}' targets"), true)?;
        for target in targets {
            if !supported_targets.contains(target) {
                bail!(
                    "dependency '{alias}' requests target '{}' absent from sdk.targets",
                    target.as_str()
                );
            }
        }
        Ok(())
    }

    pub fn selected_targets<'a>(&'a self, sdk: &'a SdkV1) -> &'a [InstallTarget] {
        self.targets.as_deref().unwrap_or(&sdk.targets)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DependencySourceV1 {
    Registry(RegistrySourceV1),
    Path(PathSourceV1),
    Workspace(WorkspaceSourceV1),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RegistrySourceV1 {
    pub registry: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PathSourceV1 {
    pub path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkspaceSourceV1 {
    pub workspace: String,
}

impl DependencySourceV1 {
    pub fn stable_description(&self) -> String {
        match self {
            Self::Registry(RegistrySourceV1 { registry }) => format!("registry:{registry}"),
            Self::Path(PathSourceV1 { path }) => format!("path:{}", normalize_relative(path)),
            Self::Workspace(WorkspaceSourceV1 { workspace }) => {
                format!("workspace:{workspace}")
            }
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DependencyOutputsV1 {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub typescript: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rust: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub python: Option<String>,
}

impl DependencyOutputsV1 {
    pub fn get(&self, target: InstallTarget) -> Option<&str> {
        match target {
            InstallTarget::TypeScript => self.typescript.as_deref(),
            InstallTarget::Rust => self.rust.as_deref(),
            InstallTarget::Python => self.python.as_deref(),
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthoringV1 {
    #[serde(default)]
    pub stacks: BTreeMap<String, AuthoringStackV1>,
    #[serde(default)]
    pub programs: BTreeMap<String, AuthoringProgramV1>,
}

/// One `[authoring.stacks.<name>]` entry: either a prebuilt StackManifest
/// (`manifest`, with `artifact_roots`) or a composition of parts (`live` and
/// `programs`), never both.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthoringStackV1 {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub manifest: Option<String>,
    #[serde(default)]
    pub artifact_roots: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deployment_name: Option<String>,
    /// Composed live views, by the live alias they take in the composed
    /// stack. Empty (and not serialized) for a prebuilt StackManifest, so its
    /// resolution hash is unchanged.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub live: BTreeMap<String, ComposedLiveV1>,
    /// Composed program SDKs, in declaration order.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub programs: Vec<ComposedProgramV1>,
}

impl AuthoringStackV1 {
    /// A prebuilt StackManifest file.
    pub fn prebuilt(manifest: String, artifact_roots: Vec<String>) -> Self {
        Self {
            manifest: Some(manifest),
            artifact_roots,
            deployment_name: None,
            live: BTreeMap::new(),
            programs: Vec::new(),
        }
    }

    /// Whether this entry composes parts instead of naming a StackManifest.
    pub fn is_composed(&self) -> bool {
        self.manifest.is_none()
    }

    /// The StackManifest path of a prebuilt entry.
    pub fn manifest_path(&self, name: &str) -> Result<&str> {
        self.manifest.as_deref().ok_or_else(|| {
            anyhow::anyhow!(
                "authoring stack '{name}' composes parts and has no StackManifest file; `a4 install` composes it"
            )
        })
    }

    fn validate(&self, name: &str) -> Result<()> {
        if let Some(manifest) = &self.manifest {
            if !self.live.is_empty() || !self.programs.is_empty() {
                bail!(
                    "authoring stack '{name}' sets both `manifest` and composed parts (`live`, `programs`); use one or the other"
                );
            }
            validate_relative_artifact_path(manifest, ArtifactPathKind::StackManifest)?;
            validate_artifact_roots(&self.artifact_roots, name)?;
            return Ok(());
        }
        if !self.artifact_roots.is_empty() {
            bail!(
                "authoring stack '{name}' sets artifact_roots without a `manifest`; a composed stack names its parts' files directly"
            );
        }
        if self.live.is_empty() {
            bail!(
                "authoring stack '{name}' needs a `manifest`, or composed `live` views (with optional `programs`)"
            );
        }
        let mut sources = BTreeSet::new();
        for (alias, live) in &self.live {
            validate_composed_live_alias(alias, name)?;
            live.validate(name, alias)?;
            if let Some(path) = &live.path {
                if !sources.insert(format!("path:{}", normalize_relative(path))) {
                    bail!(
                        "authoring stack '{name}' composes LiveSpec file '{path}' more than once"
                    );
                }
            }
        }
        let mut packages = BTreeSet::new();
        let mut program_paths = BTreeSet::new();
        for program in &self.programs {
            program.validate(name)?;
            if let Some(package) = &program.package {
                if !packages.insert(package.to_ascii_lowercase()) {
                    bail!(
                        "authoring stack '{name}' lists program package '{package}' more than once"
                    );
                }
            }
            if let Some(path) = &program.path {
                if !program_paths.insert(normalize_relative(path)) {
                    bail!(
                        "authoring stack '{name}' lists ProgramSpec file '{path}' more than once"
                    );
                }
            }
        }
        Ok(())
    }
}

/// One composed live alias: views from a published stack (`stack`, with an
/// optional `version`, `live_alias` and `views`), or from a local LiveSpec
/// file (`path`, with optional `views`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ComposedLiveV1 {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stack: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    /// The source stack's live alias, required when it has more than one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub live_alias: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    /// The selected views, in order. Absent selects every view the source
    /// stack selects (or, for a LiveSpec file, every view it defines).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub views: Option<Vec<String>>,
}

/// Where a composed live alias comes from.
pub enum ComposedLiveSource<'a> {
    Registry {
        stack: &'a str,
        requirement: &'a str,
        live_alias: Option<&'a str>,
    },
    Path(&'a str),
}

impl ComposedLiveV1 {
    pub fn source(&self) -> ComposedLiveSource<'_> {
        match (&self.stack, &self.path) {
            (Some(stack), _) => ComposedLiveSource::Registry {
                stack,
                requirement: self.version.as_deref().unwrap_or("*"),
                live_alias: self.live_alias.as_deref(),
            },
            (None, Some(path)) => ComposedLiveSource::Path(path),
            (None, None) => unreachable!("validated composed live source"),
        }
    }

    fn validate(&self, name: &str, alias: &str) -> Result<()> {
        let field = format!("authoring stack '{name}' live.{alias}");
        match (&self.stack, &self.path) {
            (Some(stack), None) => {
                validate_registry_package(stack)?;
                if let Some(version) = &self.version {
                    validate_part_requirement(version, &field)?;
                }
                if let Some(live_alias) = &self.live_alias {
                    if !is_live_alias(live_alias) {
                        bail!("{field} live_alias '{live_alias}' is not a LiveSpec alias (1-64 ASCII letters, digits, '-' or '_')");
                    }
                }
            }
            (None, Some(path)) => {
                if self.version.is_some() || self.live_alias.is_some() {
                    bail!("{field} reads a LiveSpec file, so it cannot set `version` or `live_alias`");
                }
                validate_relative_artifact_path(path, ArtifactPathKind::LiveSpec)?;
            }
            (Some(_), Some(_)) => bail!("{field} sets both `stack` and `path`; use one"),
            (None, None) => bail!(
                "{field} needs a source: `stack = \"<package>\"` or `path = \"<file>.live-spec.json\"`"
            ),
        }
        if let Some(views) = &self.views {
            if views.is_empty() {
                bail!("{field} views cannot be empty; omit `views` to select every view");
            }
            let mut unique = BTreeSet::new();
            for view in views {
                if !is_view_id(view) {
                    bail!("{field} view '{view}' is not a view ID ('<Entity>/<view>')");
                }
                if !unique.insert(view.as_str()) {
                    bail!("{field} selects view '{view}' more than once");
                }
            }
        }
        Ok(())
    }
}

/// One composed program SDK: a program package (`package`, with an optional
/// `version`) or a local ProgramSpec file (`path`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ComposedProgramV1 {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub package: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
}

/// Where a composed program comes from.
pub enum ComposedProgramSource<'a> {
    Registry {
        package: &'a str,
        requirement: &'a str,
    },
    Path(&'a str),
}

impl ComposedProgramV1 {
    pub fn source(&self) -> ComposedProgramSource<'_> {
        match (&self.package, &self.path) {
            (Some(package), _) => ComposedProgramSource::Registry {
                package,
                requirement: self.version.as_deref().unwrap_or("*"),
            },
            (None, Some(path)) => ComposedProgramSource::Path(path),
            (None, None) => unreachable!("validated composed program source"),
        }
    }

    fn validate(&self, name: &str) -> Result<()> {
        let field = format!("authoring stack '{name}' programs");
        match (&self.package, &self.path) {
            (Some(package), None) => {
                validate_registry_package(package)?;
                if let Some(version) = &self.version {
                    validate_part_requirement(version, &format!("{field} entry '{package}'"))?;
                }
            }
            (None, Some(path)) => {
                if self.version.is_some() {
                    bail!("{field} entry '{path}' reads a ProgramSpec file, so it cannot set `version`");
                }
                validate_relative_artifact_path(path, ArtifactPathKind::ProgramSpec)?;
            }
            (Some(_), Some(_)) => bail!("{field} entry sets both `package` and `path`; use one"),
            (None, None) => bail!(
                "{field} entry needs a source: `package = \"<package>\"` or `path = \"<file>.program-spec.json\"`"
            ),
        }
        Ok(())
    }
}

/// A composed part's version requirement: a semantic version range, never a
/// mutable tag.
fn validate_part_requirement(requirement: &str, field: &str) -> Result<()> {
    if matches!(requirement, "latest" | "stable" | "next") {
        bail!("{field} cannot use mutable tag '{requirement}'");
    }
    VersionReq::parse(requirement)
        .with_context(|| format!("{field} has invalid version requirement '{requirement}'"))?;
    Ok(())
}

/// A view ID's shape: `<Entity>/<view>`, both parts non-empty, no whitespace.
/// Whether the view exists is checked against its LiveSpec on install.
fn is_view_id(view: &str) -> bool {
    view.split_once('/')
        .is_some_and(|(entity, name)| !entity.is_empty() && !name.is_empty())
        && !view
            .chars()
            .any(|character| character.is_whitespace() || character.is_control())
}

/// A LiveSpec alias as a StackManifest spells it.
fn is_live_alias(live: &str) -> bool {
    !live.is_empty()
        && live.len() <= 64
        && live.bytes().any(|byte| byte.is_ascii_alphanumeric())
        && live
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
}

/// A composed stack's live alias: a StackManifest LiveSpec alias.
fn validate_composed_live_alias(alias: &str, name: &str) -> Result<()> {
    if !is_live_alias(alias) {
        bail!(
            "authoring stack '{name}' live alias '{alias}' is not a LiveSpec alias (1-64 ASCII letters, digits, '-' or '_')"
        );
    }
    Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthoringProgramV1 {
    pub program_spec: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InstallTarget {
    TypeScript,
    Rust,
    Python,
}

impl InstallTarget {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::TypeScript => "typescript",
            Self::Rust => "rust",
            Self::Python => "python",
        }
    }
}

impl std::fmt::Display for InstallTarget {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.as_str())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DependencyKind {
    Stack,
    Program,
}

impl DependencyKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Stack => "stack",
            Self::Program => "program",
        }
    }

    pub fn namespace(self) -> &'static str {
        match self {
            Self::Stack => "stacks",
            Self::Program => "programs",
        }
    }
}

impl std::fmt::Display for DependencyKind {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.as_str())
    }
}

#[derive(Clone, Copy)]
enum ArtifactPathKind {
    StackManifest,
    ProgramSpec,
    LiveSpec,
}

fn validate_relative_artifact_path(path: &str, kind: ArtifactPathKind) -> Result<()> {
    let parsed = Path::new(path);
    if path.trim().is_empty() || parsed.is_absolute() {
        bail!("artifact path '{path}' must be a non-empty manifest-relative path");
    }
    if parsed
        .components()
        .any(|component| matches!(component, Component::ParentDir))
    {
        bail!("artifact path '{path}' cannot contain parent traversal");
    }
    if path.ends_with(".stack.json") {
        bail!(
            "'{path}' uses the removed .stack.json format; use an exact StackManifest or ProgramSpec artifact"
        );
    }
    let expected = match kind {
        ArtifactPathKind::StackManifest => ".stack-manifest.json",
        ArtifactPathKind::ProgramSpec => ".program-spec.json",
        ArtifactPathKind::LiveSpec => ".live-spec.json",
    };
    if !path.ends_with(expected) {
        bail!("artifact path '{path}' must end with '{expected}'");
    }
    Ok(())
}

fn validate_artifact_roots(roots: &[String], authoring_name: &str) -> Result<()> {
    let mut unique = BTreeSet::new();
    for root in roots {
        let parsed = Path::new(root);
        if root.trim().is_empty()
            || parsed.is_absolute()
            || parsed
                .components()
                .any(|component| matches!(component, Component::ParentDir))
        {
            bail!(
                "authoring stack '{authoring_name}' artifact root '{root}' must be manifest-relative without parent traversal"
            );
        }
        if !unique.insert(normalize_relative(root)) {
            bail!("authoring stack '{authoring_name}' repeats artifact root '{root}'");
        }
    }
    Ok(())
}

fn validate_targets(targets: &[InstallTarget], field: &str, require_non_empty: bool) -> Result<()> {
    if require_non_empty && targets.is_empty() {
        bail!("{field} cannot be empty");
    }
    let mut unique = BTreeSet::new();
    for target in targets {
        if !unique.insert(*target) {
            bail!("{field} repeats target '{target}'");
        }
    }
    Ok(())
}

/// A LiveSpec alias as a StackManifest spells it (artifact rules, which allow
/// upper case), used as an `endpoints` key.
fn validate_live_alias(live: &str, dependency: &str) -> Result<()> {
    if !is_live_alias(live) {
        bail!(
            "dependency '{dependency}' endpoints key '{live}' is not a LiveSpec alias (1-64 ASCII letters, digits, '-' or '_')"
        );
    }
    Ok(())
}

/// An absolute endpoint URL with one of the allowed schemes and a host.
fn validate_endpoint(endpoint: &str, schemes: &[&str], field: &str) -> Result<()> {
    let url =
        url::Url::parse(endpoint).with_context(|| format!("{field} is not a URL: '{endpoint}'"))?;
    if !schemes.contains(&url.scheme()) || url.host_str().is_none_or(str::is_empty) {
        bail!(
            "{field} must be a {} URL with a host, not '{endpoint}'",
            schemes.join(" or ")
        );
    }
    Ok(())
}

fn validate_alias(alias: &str, kind: &str) -> Result<()> {
    let valid = !alias.is_empty()
        && alias.len() <= 64
        && alias
            .bytes()
            .next()
            .is_some_and(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit())
        && alias.bytes().all(|byte| {
            byte.is_ascii_lowercase() || byte.is_ascii_digit() || matches!(byte, b'-' | b'_')
        });
    if !valid {
        bail!(
            "{kind} '{alias}' must be 1-64 lowercase ASCII letters, digits, '-' or '_', starting with a letter or digit"
        );
    }
    Ok(())
}

fn validate_registry_package(package: &str) -> Result<()> {
    if package.is_empty()
        || package.len() > 128
        || !package
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'/'))
    {
        bail!("registry package '{package}' is not a portable package name");
    }
    Ok(())
}

fn normalize_relative(path: &str) -> String {
    Path::new(path)
        .components()
        .filter_map(|component| match component {
            Component::Normal(value) => Some(value.to_string_lossy().into_owned()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("/")
}

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

    fn parse(source: &str) -> Result<ManifestV1> {
        let manifest: ManifestV1 = toml::from_str(source)?;
        manifest.validate()?;
        Ok(manifest)
    }

    fn with_endpoints(dependency: &str, endpoints: &str) -> Result<ManifestV1> {
        parse(&format!(
            r#"
manifest_version = 1
[project]
name = "example"
[authoring.stacks.mine]
manifest = "./.arete/Mine.stack-manifest.json"
[dependencies.stacks.dep]
{dependency}
endpoints = {endpoints}
"#
        ))
    }

    #[test]
    fn a_registry_stack_records_its_deployment_endpoints_per_live_spec() {
        let manifest = with_endpoints(
            "source = { registry = \"ore\" }\nversion = \"^1.0.0\"",
            r#"{ live = { websocket = "wss://ore.stack.example", query = "https://ore.stack.example" }, Other_2 = { websocket = "ws://127.0.0.1:8878", query = "http://127.0.0.1:8878" } }"#,
        )
        .expect("endpoints validate");
        let endpoints = &manifest.dependencies.stacks["dep"].endpoints;
        assert_eq!(endpoints["live"].websocket, "wss://ore.stack.example");
        assert_eq!(endpoints["Other_2"].query, "http://127.0.0.1:8878");

        let written = manifest.to_toml_pretty().unwrap();
        let reparsed = parse(&written).unwrap();
        assert_eq!(&reparsed.dependencies.stacks["dep"].endpoints, endpoints);
    }

    #[test]
    fn a_manifest_without_endpoints_keeps_its_resolution_hash_shape() {
        let manifest = parse(
            "manifest_version = 1\n[project]\nname = \"example\"\n\
             [dependencies.stacks.ore]\nsource = { registry = \"ore\" }\nversion = \"^1.0.0\"\n",
        )
        .unwrap();
        let encoded = serde_json::to_string(&manifest).unwrap();
        assert!(!encoded.contains("endpoints"), "{encoded}");
    }

    #[test]
    fn endpoints_belong_to_registry_stacks_with_stream_urls() {
        let registry = "source = { registry = \"ore\" }\nversion = \"^1.0.0\"";
        let good = r#"{ live = { websocket = "wss://ore.stack.example", query = "https://ore.stack.example" } }"#;
        let cases = [
            (
                "source = { workspace = \"mine\" }",
                good,
                "only a registry stack",
            ),
            (
                registry,
                r#"{ live = { websocket = "https://ore.stack.example", query = "https://ore.stack.example" } }"#,
                "must be a ws or wss URL",
            ),
            (
                registry,
                r#"{ live = { websocket = "wss://ore.stack.example", query = "wss://ore.stack.example" } }"#,
                "must be a http or https URL",
            ),
            (
                registry,
                r#"{ live = { websocket = "not a url", query = "https://ore.stack.example" } }"#,
                "is not a URL",
            ),
            (
                registry,
                r#"{ "bad alias" = { websocket = "wss://ore.stack.example", query = "https://ore.stack.example" } }"#,
                "is not a LiveSpec alias",
            ),
        ];
        for (dependency, endpoints, expected) in cases {
            let error = format!("{:#}", with_endpoints(dependency, endpoints).unwrap_err());
            assert!(error.contains(expected), "{expected}: {error}");
        }
        let program = parse(&format!(
            "manifest_version = 1\n[project]\nname = \"example\"\n\
             [dependencies.programs.token]\nsource = {{ registry = \"spl-token\" }}\nversion = \"^1.0.0\"\n\
             endpoints = {good}\n"
        ))
        .unwrap_err();
        assert!(
            format!("{program:#}").contains("only a registry stack"),
            "{program:#}"
        );
        let unknown = with_endpoints(
            registry,
            r#"{ live = { websocket = "wss://ore.stack.example", query = "https://ore.stack.example", auth = "x" } }"#,
        )
        .unwrap_err();
        assert!(
            format!("{unknown:#}").contains("unknown field"),
            "{unknown:#}"
        );
    }

    fn composed(entry: &str) -> Result<ManifestV1> {
        parse(&format!(
            "manifest_version = 1\n[project]\nname = \"example\"\n\n[authoring.stacks.ore-plus-token]\n{entry}\n"
        ))
    }

    #[test]
    fn a_composed_stack_names_registry_parts_and_local_files() {
        let manifest = composed(
            r#"live.ore = { stack = "ore", version = "^1", views = ["OreRound/latest", "OreMiner/state"] }
live.multi = { stack = "multi", live_alias = "beta" }
live.local = { path = "./artifacts/local.live-spec.json" }
programs = [{ package = "spl-token", version = "^4" }, { package = "memo" }, { path = "./artifacts/vote.program-spec.json" }]
deployment_name = "ore-plus-token-prod"

[dependencies.stacks.ore-plus-token]
source = { workspace = "ore-plus-token" }
targets = ["typescript"]
endpoints = { ore = { websocket = "wss://mine.example", query = "https://mine.example" }, multi = { websocket = "wss://mine.example", query = "https://mine.example" }, local = { websocket = "wss://mine.example", query = "https://mine.example" } }"#,
        )
        .expect("a composed stack validates");
        let entry = &manifest.authoring.stacks["ore-plus-token"];
        assert!(entry.is_composed());
        assert_eq!(entry.live.len(), 3);
        assert!(matches!(
            entry.live["ore"].source(),
            ComposedLiveSource::Registry {
                stack: "ore",
                requirement: "^1",
                live_alias: None
            }
        ));
        assert!(matches!(
            entry.live["multi"].source(),
            ComposedLiveSource::Registry {
                requirement: "*",
                live_alias: Some("beta"),
                ..
            }
        ));
        assert!(matches!(
            entry.programs[1].source(),
            ComposedProgramSource::Registry {
                package: "memo",
                requirement: "*"
            }
        ));
        assert!(matches!(
            entry.programs[2].source(),
            ComposedProgramSource::Path("./artifacts/vote.program-spec.json")
        ));
        assert_eq!(
            entry.manifest_path("ore-plus-token").unwrap_err().to_string(),
            "authoring stack 'ore-plus-token' composes parts and has no StackManifest file; `a4 install` composes it"
        );
        // The composed parts round-trip through arete.toml.
        let reparsed = parse(&manifest.to_toml_pretty().unwrap()).unwrap();
        assert_eq!(
            reparsed.resolution_hash().unwrap(),
            manifest.resolution_hash().unwrap()
        );
    }

    #[test]
    fn a_prebuilt_stack_manifest_entry_keeps_its_resolution_hash_shape() {
        let manifest = parse(
            "manifest_version = 1\n[project]\nname = \"example\"\n\
             [authoring.stacks.local]\nmanifest = \"./.arete/Local.stack-manifest.json\"\nartifact_roots = [\"./.arete\"]\n",
        )
        .unwrap();
        let entry = &manifest.authoring.stacks["local"];
        assert!(!entry.is_composed());
        assert_eq!(
            serde_json::to_value(entry).unwrap(),
            serde_json::json!({
                "manifest": "./.arete/Local.stack-manifest.json",
                "artifact_roots": ["./.arete"],
            })
        );
    }

    #[test]
    fn composed_parts_are_validated() {
        let cases = [
            (
                "manifest = \"./Mine.stack-manifest.json\"\nlive.ore = { stack = \"ore\" }",
                "sets both `manifest` and composed parts",
            ),
            (
                "programs = [{ package = \"spl-token\" }]",
                "composed `live` views",
            ),
            (
                "artifact_roots = [\"./.arete\"]\nlive.ore = { stack = \"ore\" }",
                "artifact_roots without a `manifest`",
            ),
            (
                "live.ore = { stack = \"ore\", path = \"./ore.live-spec.json\" }",
                "sets both `stack` and `path`",
            ),
            ("live.ore = { views = [\"OreRound/latest\"] }", "needs a source"),
            (
                "live.ore = { path = \"./ore.live-spec.json\", version = \"^1\" }",
                "cannot set `version` or `live_alias`",
            ),
            (
                "live.ore = { path = \"./ore.program-spec.json\" }",
                "must end with '.live-spec.json'",
            ),
            (
                "live.ore = { path = \"../ore.live-spec.json\" }",
                "parent traversal",
            ),
            (
                "live.\"bad alias\" = { stack = \"ore\" }",
                "is not a LiveSpec alias",
            ),
            (
                "live.ore = { stack = \"ore\", live_alias = \"not an alias\" }",
                "live_alias 'not an alias' is not a LiveSpec alias",
            ),
            ("live.ore = { stack = \"ore\", views = [] }", "views cannot be empty"),
            (
                "live.ore = { stack = \"ore\", views = [\"OreRound/latest\", \"OreRound/latest\"] }",
                "selects view 'OreRound/latest' more than once",
            ),
            (
                "live.ore = { stack = \"ore\", views = [\"OreRound\"] }",
                "is not a view ID",
            ),
            (
                "live.ore = { stack = \"ore\", version = \"latest\" }",
                "mutable tag 'latest'",
            ),
            (
                "live.ore = { stack = \"ore\", version = \"not-a-range\" }",
                "invalid version requirement",
            ),
            (
                "live.ore = { stack = \"ore\" }\nprograms = [{ package = \"spl-token\" }, { package = \"SPL-token\" }]",
                "lists program package 'SPL-token' more than once",
            ),
            (
                "live.ore = { stack = \"ore\" }\nprograms = [{ package = \"spl-token\", path = \"./t.program-spec.json\" }]",
                "sets both `package` and `path`",
            ),
            (
                "live.ore = { stack = \"ore\" }\nprograms = [{ path = \"./t.program-spec.json\", version = \"^1\" }]",
                "cannot set `version`",
            ),
            (
                "live.a = { path = \"./x.live-spec.json\" }\nlive.b = { path = \"x.live-spec.json\" }",
                "composes LiveSpec file 'x.live-spec.json' more than once",
            ),
            (
                "live.ore = { stack = \"ore\", unknown = true }",
                "unknown field",
            ),
        ];
        for (entry, expected) in cases {
            let error = format!("{:#}", composed(entry).unwrap_err());
            assert!(error.contains(expected), "{entry}\n{expected}\n{error}");
        }
    }

    #[test]
    fn endpoints_belong_to_composed_but_not_prebuilt_workspace_stacks() {
        let endpoints = r#"endpoints = { ore = { websocket = "wss://mine.example", query = "https://mine.example" } }"#;
        let composed_endpoints = parse(&format!(
            "manifest_version = 1\n[project]\nname = \"example\"\n\
             [authoring.stacks.mix]\nlive.ore = {{ stack = \"ore\" }}\n\
             [dependencies.stacks.mix]\nsource = {{ workspace = \"mix\" }}\n{endpoints}\n"
        ));
        assert!(composed_endpoints.is_ok(), "{composed_endpoints:?}");
        let prebuilt = parse(&format!(
            "manifest_version = 1\n[project]\nname = \"example\"\n\
             [authoring.stacks.mine]\nmanifest = \"./Mine.stack-manifest.json\"\n\
             [dependencies.stacks.mine]\nsource = {{ workspace = \"mine\" }}\n{endpoints}\n"
        ))
        .unwrap_err();
        assert!(
            format!("{prebuilt:#}").contains("only a registry stack or a composed"),
            "{prebuilt:#}"
        );
    }

    #[test]
    fn strict_manifest_accepts_registry_path_and_workspace_sources() {
        let manifest = parse(
            r#"
manifest_version = 1
[project]
name = "example"
[sdk]
targets = ["typescript", "rust"]
[dependencies.stacks.ore]
source = { registry = "ore" }
version = "^1.4"
[dependencies.stacks.local]
source = { workspace = "local-stack" }
[dependencies.programs.token]
source = { path = "./artifacts/token.program-spec.json" }
targets = ["typescript"]
[authoring.stacks.local-stack]
manifest = "./.arete/Local.stack-manifest.json"
artifact_roots = ["./.arete"]
"#,
        )
        .expect("manifest should validate");
        assert_eq!(manifest.dependencies.stacks.len(), 2);
    }

    #[test]
    fn strict_manifest_rejects_unknown_fields_and_accepts_cross_kind_aliases() {
        let unknown = toml::from_str::<ManifestV1>(
            r#"
manifest_version = 1
[project]
name = "example"
typo = true
"#,
        );
        assert!(unknown.is_err());

        let same_alias = parse(
            r#"
manifest_version = 1
[project]
name = "example"
[dependencies.stacks.same]
source = { registry = "one" }
version = "1"
[dependencies.programs.same]
source = { registry = "two" }
version = "1"
"#,
        )
        .expect("stack and program aliases occupy separate namespaces");
        assert!(same_alias.dependencies.stacks.contains_key("same"));
        assert!(same_alias.dependencies.programs.contains_key("same"));
    }

    #[test]
    fn source_union_and_legacy_suffix_fail_closed() {
        let ambiguous = parse(
            r#"
manifest_version = 1
[project]
name = "example"
[dependencies.stacks.bad]
source = { registry = "ore", path = "./ore.stack-manifest.json" }
version = "1"
"#,
        );
        assert!(ambiguous.is_err());

        let legacy = parse(
            r#"
manifest_version = 1
[project]
name = "example"
[dependencies.stacks.bad]
source = { path = "./ore.stack.json" }
"#,
        );
        assert!(legacy
            .unwrap_err()
            .to_string()
            .contains("removed .stack.json"));
    }

    #[test]
    fn sdk_targets_must_not_be_empty() {
        let empty = parse(
            r#"
manifest_version = 1
[project]
name = "example"
[sdk]
targets = []
"#,
        );

        assert!(empty
            .unwrap_err()
            .to_string()
            .contains("sdk.targets cannot be empty"));
    }

    #[test]
    fn resolution_hash_ignores_toml_formatting() {
        let compact = parse("manifest_version=1\n[project]\nname='same'\n").unwrap();
        let formatted = parse(
            r#"
                manifest_version = 1

                # Comments are not resolution input.
                [project]
                name = "same"
            "#,
        )
        .unwrap();
        assert_eq!(
            compact.resolution_hash().unwrap(),
            formatted.resolution_hash().unwrap()
        );
    }

    #[test]
    fn generated_toml_omits_semantically_empty_defaults() {
        let manifest = ManifestV1::new("clean-project".into());
        let rendered = manifest.to_toml_pretty().unwrap();

        assert!(rendered.contains("manifest_version = 1"));
        assert!(rendered.contains("name = \"clean-project\""));
        assert!(!rendered.contains("private = false"));
        assert!(!rendered.contains("[install]"));
        assert!(!rendered.contains("[sdk"));
        assert!(!rendered.contains("[dependencies"));
        assert!(!rendered.contains("[authoring"));

        let reparsed = parse(&rendered).unwrap();
        assert_eq!(
            manifest.resolution_hash().unwrap(),
            reparsed.resolution_hash().unwrap()
        );
    }
}