greentic-operator 0.4.43

Greentic operator CLI for local dev and demo orchestration.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
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
use std::collections::BTreeSet;
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::Context;
use serde::{Deserialize, Serialize};
use serde_cbor::Value as CborValue;
use zip::result::ZipError;

#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum Domain {
    Messaging,
    Events,
    Secrets,
    OAuth,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DomainAction {
    Setup,
    Diagnostics,
    Verify,
}

#[derive(Clone, Debug)]
pub struct DomainConfig {
    pub providers_dir: &'static str,
    pub setup_flow: &'static str,
    pub diagnostics_flow: &'static str,
    pub verify_flows: &'static [&'static str],
}

#[derive(Clone, Debug, Serialize)]
pub struct ProviderPack {
    pub pack_id: String,
    pub file_name: String,
    pub path: PathBuf,
    pub entry_flows: Vec<String>,
}

#[derive(Clone, Debug, Serialize)]
pub struct PlannedRun {
    pub pack: ProviderPack,
    pub flow_id: String,
}

pub fn config(domain: Domain) -> DomainConfig {
    match domain {
        Domain::Messaging => DomainConfig {
            providers_dir: "providers/messaging",
            setup_flow: "setup_default",
            diagnostics_flow: "diagnostics",
            verify_flows: &["verify_webhooks"],
        },
        Domain::Events => DomainConfig {
            providers_dir: "providers/events",
            setup_flow: "setup_default",
            diagnostics_flow: "diagnostics",
            verify_flows: &["verify_subscriptions"],
        },
        Domain::Secrets => DomainConfig {
            providers_dir: "providers/secrets",
            setup_flow: "setup_default",
            diagnostics_flow: "diagnostics",
            verify_flows: &[],
        },
        Domain::OAuth => DomainConfig {
            providers_dir: "providers/oauth",
            setup_flow: "setup_default",
            diagnostics_flow: "diagnostics",
            verify_flows: &[],
        },
    }
}

pub fn validator_pack_path(root: &Path, domain: Domain) -> Option<PathBuf> {
    let name = match domain {
        Domain::Messaging => "validators-messaging.gtpack",
        Domain::Events => "validators-events.gtpack",
        Domain::Secrets => "validators-secrets.gtpack",
        Domain::OAuth => "validators-oauth.gtpack",
    };
    let path = root.join("validators").join(domain_name(domain)).join(name);
    if path.exists() { Some(path) } else { None }
}

pub fn ensure_cbor_packs(root: &Path) -> anyhow::Result<()> {
    let mut roots = Vec::new();
    let providers = root.join("providers");
    if providers.exists() {
        roots.push(providers);
    }
    let packs = root.join("packs");
    if packs.exists() {
        roots.push(packs);
    }
    for root in roots {
        for pack in collect_gtpacks(&root)? {
            let file = std::fs::File::open(&pack)?;
            let mut archive = zip::ZipArchive::new(file)?;
            let manifest = read_manifest_cbor(&mut archive, &pack).map_err(|err| {
                anyhow::anyhow!(
                    "failed to decode manifest.cbor in {}: {err}",
                    pack.display()
                )
            })?;
            if manifest.is_none() {
                return Err(missing_cbor_error(&pack));
            }
        }
    }
    Ok(())
}

pub fn manifest_cbor_issue_detail(path: &Path) -> anyhow::Result<Option<String>> {
    let file = std::fs::File::open(path)?;
    let mut archive = zip::ZipArchive::new(file)?;
    let mut manifest = match archive.by_name("manifest.cbor") {
        Ok(file) => file,
        Err(ZipError::FileNotFound) => {
            return Ok(Some("manifest.cbor missing from archive".to_string()));
        }
        Err(err) => return Err(err.into()),
    };
    let mut bytes = Vec::new();
    std::io::Read::read_to_end(&mut manifest, &mut bytes)?;
    let value = match serde_cbor::from_slice::<CborValue>(&bytes) {
        Ok(value) => value,
        Err(err) => return Ok(Some(err.to_string())),
    };
    if let Some(path) = find_manifest_string_type_mismatch(&value) {
        return Ok(Some(format!("invalid type at {path} (expected string)")));
    }
    Ok(None)
}

fn collect_gtpacks(root: &Path) -> anyhow::Result<Vec<PathBuf>> {
    let mut packs = Vec::new();
    let mut stack = vec![root.to_path_buf()];
    while let Some(dir) = stack.pop() {
        for entry in std::fs::read_dir(&dir)? {
            let entry = entry?;
            let path = entry.path();
            if entry.file_type()?.is_dir() {
                stack.push(path);
                continue;
            }
            if path.extension().and_then(|ext| ext.to_str()) == Some("gtpack") {
                packs.push(path);
            }
        }
    }
    Ok(packs)
}

fn append_packs_from_root<F>(
    packs: &mut Vec<ProviderPack>,
    seen: &mut BTreeSet<PathBuf>,
    root: &Path,
    read_manifest: F,
) -> anyhow::Result<()>
where
    F: Fn(&Path) -> anyhow::Result<PackManifest>,
{
    if !root.exists() {
        return Ok(());
    }
    for path in collect_gtpacks(root)? {
        append_pack(packs, seen, path, &read_manifest)?;
    }
    Ok(())
}

fn append_pack<F>(
    packs: &mut Vec<ProviderPack>,
    seen: &mut BTreeSet<PathBuf>,
    path: PathBuf,
    read_manifest: &F,
) -> anyhow::Result<()>
where
    F: Fn(&Path) -> anyhow::Result<PackManifest>,
{
    if !seen.insert(path.clone()) {
        return Ok(());
    }
    let file_name = path
        .file_name()
        .and_then(|value| value.to_str())
        .ok_or_else(|| anyhow::anyhow!("invalid pack file name: {}", path.display()))?
        .to_string();
    let manifest = read_manifest(&path)?;
    let meta = manifest
        .meta
        .ok_or_else(|| anyhow::anyhow!("pack manifest missing meta in {}", path.display()))?;
    packs.push(ProviderPack {
        pack_id: meta.pack_id,
        file_name,
        path,
        entry_flows: meta.entry_flows,
    });
    Ok(())
}

pub fn discover_provider_packs(root: &Path, domain: Domain) -> anyhow::Result<Vec<ProviderPack>> {
    let cfg = config(domain);
    let providers_dir = root.join(cfg.providers_dir);
    let packs_dir = root.join("packs");
    let mut packs = Vec::new();
    let mut seen = BTreeSet::new();
    append_packs_from_root(&mut packs, &mut seen, &providers_dir, read_pack_manifest)?;
    append_packs_from_root(&mut packs, &mut seen, &packs_dir, read_pack_manifest)?;
    packs.sort_by(|a, b| a.file_name.cmp(&b.file_name));
    Ok(packs)
}

pub fn discover_provider_packs_cbor_only(
    root: &Path,
    domain: Domain,
) -> anyhow::Result<Vec<ProviderPack>> {
    let cfg = config(domain);
    let providers_dir = root.join(cfg.providers_dir);
    let packs_dir = root.join("packs");
    let mut packs = Vec::new();
    let mut seen = BTreeSet::new();
    append_packs_from_root(
        &mut packs,
        &mut seen,
        &providers_dir,
        read_pack_manifest_cbor_only,
    )?;
    append_packs_from_root(
        &mut packs,
        &mut seen,
        &packs_dir,
        read_pack_manifest_cbor_only,
    )?;
    packs.sort_by(|a, b| a.file_name.cmp(&b.file_name));
    Ok(packs)
}

pub fn plan_runs(
    domain: Domain,
    action: DomainAction,
    packs: &[ProviderPack],
    provider_filter: Option<&str>,
    allow_missing_setup: bool,
) -> anyhow::Result<Vec<PlannedRun>> {
    let cfg = config(domain);
    let flows: Vec<&str> = match action {
        DomainAction::Setup => vec![cfg.setup_flow],
        DomainAction::Diagnostics => vec![cfg.diagnostics_flow],
        DomainAction::Verify => cfg.verify_flows.to_vec(),
    };

    let mut plan = Vec::new();
    for pack in packs {
        if let Some(filter) = provider_filter {
            let file_stem = pack
                .file_name
                .strip_suffix(".gtpack")
                .unwrap_or(&pack.file_name);
            let matches = pack.pack_id == filter
                || pack.file_name == filter
                || file_stem == filter
                || pack.pack_id.contains(filter)
                || pack.file_name.contains(filter)
                || file_stem.contains(filter);
            if !matches {
                continue;
            }
        }

        for flow in &flows {
            let has_flow = pack.entry_flows.iter().any(|entry| entry == flow);
            if !has_flow {
                if action == DomainAction::Setup && !allow_missing_setup {
                    return Err(anyhow::anyhow!(
                        "Missing required flow '{}' in provider pack {}",
                        flow,
                        pack.file_name
                    ));
                }
                eprintln!(
                    "Warning: provider pack {} missing flow {}; skipping.",
                    pack.file_name, flow
                );
                continue;
            }
            plan.push(PlannedRun {
                pack: pack.clone(),
                flow_id: (*flow).to_string(),
            });
        }
    }
    Ok(plan)
}

#[derive(Debug, Deserialize)]
struct PackManifest {
    #[serde(default)]
    meta: Option<PackMeta>,
    #[serde(default)]
    pack_id: Option<String>,
    #[serde(default)]
    flows: Vec<PackFlow>,
}

#[derive(Debug, Deserialize)]
pub(crate) struct PackMeta {
    pub pack_id: String,
    #[serde(default)]
    pub entry_flows: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct PackFlow {
    id: String,
    #[serde(default)]
    entrypoints: Vec<String>,
}

fn read_pack_manifest(path: &Path) -> anyhow::Result<PackManifest> {
    let file = std::fs::File::open(path)?;
    match zip::ZipArchive::new(file) {
        Ok(mut archive) => {
            let manifest = read_pack_manifest_data(&mut archive, path)
                .with_context(|| format!("failed to read pack manifest from {}", path.display()))?;
            let meta = build_pack_meta(&manifest, path);
            Ok(PackManifest {
                meta: Some(meta),
                pack_id: None,
                flows: Vec::new(),
            })
        }
        Err(_) => read_pack_manifest_from_tar(path),
    }
}

pub(crate) fn read_pack_meta(path: &Path) -> anyhow::Result<PackMeta> {
    let manifest = if path.is_dir() {
        read_pack_manifest_from_dir(path)
    } else {
        read_pack_manifest(path)
    }?;
    manifest
        .meta
        .ok_or_else(|| anyhow::anyhow!("pack manifest missing meta in {}", path.display()))
}

fn read_pack_manifest_cbor_only(path: &Path) -> anyhow::Result<PackManifest> {
    let file = std::fs::File::open(path)?;
    match zip::ZipArchive::new(file) {
        Ok(mut archive) => {
            let manifest = match read_manifest_cbor(&mut archive, path).map_err(|err| {
                anyhow::anyhow!(
                    "failed to decode manifest.cbor in {}: {err}",
                    path.display()
                )
            })? {
                Some(manifest) => manifest,
                None => return Err(missing_cbor_error(path)),
            };
            let meta = build_pack_meta(&manifest, path);
            Ok(PackManifest {
                meta: Some(meta),
                pack_id: None,
                flows: Vec::new(),
            })
        }
        Err(_) => read_pack_manifest_from_tar(path),
    }
}

fn read_pack_manifest_data(
    archive: &mut zip::ZipArchive<std::fs::File>,
    path: &Path,
) -> anyhow::Result<PackManifest> {
    match read_manifest_cbor(archive, path) {
        Ok(Some(manifest)) => return Ok(manifest),
        Ok(None) => {}
        Err(err) => {
            return Err(anyhow::anyhow!(
                "failed to decode manifest.cbor in {}: {err}",
                path.display()
            ));
        }
    }
    match read_manifest_json(archive, "pack.manifest.json") {
        Ok(Some(manifest)) => return Ok(manifest),
        Ok(None) => {}
        Err(err) => {
            return Err(anyhow::anyhow!(
                "failed to decode pack.manifest.json in {}: {err}",
                path.display()
            ));
        }
    }
    Err(anyhow::anyhow!(
        "pack manifest not found in archive {} (expected manifest.cbor or pack.manifest.json)",
        path.display()
    ))
}

fn read_manifest_cbor(
    archive: &mut zip::ZipArchive<std::fs::File>,
    _path: &Path,
) -> anyhow::Result<Option<PackManifest>> {
    let mut file = match archive.by_name("manifest.cbor") {
        Ok(file) => file,
        Err(ZipError::FileNotFound) => return Ok(None),
        Err(err) => return Err(err.into()),
    };
    let mut bytes = Vec::new();
    std::io::Read::read_to_end(&mut file, &mut bytes)?;
    let manifest = parse_manifest_cbor_bytes(&bytes)?;
    Ok(Some(manifest))
}

fn read_pack_manifest_from_dir(path: &Path) -> anyhow::Result<PackManifest> {
    let manifest_path = path.join("manifest.cbor");
    if !manifest_path.exists() {
        return Err(anyhow::anyhow!(
            "pack manifest missing manifest.cbor in {}",
            path.display()
        ));
    }
    let bytes = fs::read(&manifest_path)?;
    parse_manifest_cbor_bytes(&bytes)
}

fn read_pack_manifest_from_tar(path: &Path) -> anyhow::Result<PackManifest> {
    let bytes = read_tar_entry_bytes(path, "manifest.cbor")?;
    let manifest = parse_manifest_cbor_bytes(&bytes)?;
    let meta = build_pack_meta(&manifest, path);
    Ok(PackManifest {
        meta: Some(meta),
        pack_id: None,
        flows: Vec::new(),
    })
}

fn read_tar_entry_bytes(path: &Path, entry_name: &str) -> anyhow::Result<Vec<u8>> {
    let file = std::fs::File::open(path)?;
    let mut archive = tar::Archive::new(file);
    for entry in archive.entries()? {
        let mut entry = entry?;
        if entry.path()?.as_ref() == Path::new(entry_name) {
            let mut bytes = Vec::new();
            std::io::Read::read_to_end(&mut entry, &mut bytes)?;
            return Ok(bytes);
        }
    }
    Err(anyhow::anyhow!(
        "pack manifest not found in archive {} (expected {entry_name})",
        path.display()
    ))
}

fn parse_manifest_cbor_bytes(bytes: &[u8]) -> anyhow::Result<PackManifest> {
    let value: CborValue = serde_cbor::from_slice(bytes)?;
    match decode_manifest_lenient(&value) {
        Ok(manifest) => Ok(manifest),
        Err(decode_err) => {
            if let Some(err_path) = find_manifest_string_type_mismatch(&value) {
                return Err(anyhow::anyhow!(
                    "invalid type at {} (expected string)",
                    err_path
                ));
            }
            Err(anyhow::anyhow!(
                "manifest.cbor uses symbol table encoding but could not be decoded: {decode_err}"
            ))
        }
    }
}

fn build_pack_meta(manifest: &PackManifest, path: &Path) -> PackMeta {
    let pack_id = manifest
        .meta
        .as_ref()
        .map(|meta| meta.pack_id.clone())
        .or_else(|| manifest.pack_id.clone())
        .unwrap_or_else(|| {
            let fallback = path
                .file_stem()
                .and_then(|value| value.to_str())
                .unwrap_or("pack")
                .to_string();
            eprintln!(
                "Warning: pack manifest missing pack id; using filename '{}' for {}",
                fallback,
                path.display()
            );
            fallback
        });
    let mut entry_flows = manifest
        .meta
        .as_ref()
        .map(|meta| meta.entry_flows.clone())
        .unwrap_or_default();
    if entry_flows.is_empty() {
        for flow in &manifest.flows {
            entry_flows.push(flow.id.clone());
            entry_flows.extend(flow.entrypoints.iter().cloned());
        }
    }
    if entry_flows.is_empty() {
        entry_flows.push(pack_id.clone());
    }
    PackMeta {
        pack_id,
        entry_flows,
    }
}

fn read_manifest_json(
    archive: &mut zip::ZipArchive<std::fs::File>,
    name: &str,
) -> anyhow::Result<Option<PackManifest>> {
    let mut file = match archive.by_name(name) {
        Ok(file) => file,
        Err(ZipError::FileNotFound) => return Ok(None),
        Err(err) => return Err(err.into()),
    };
    let mut contents = String::new();
    std::io::Read::read_to_string(&mut file, &mut contents)?;
    let manifest: PackManifest = serde_json::from_str(&contents)?;
    Ok(Some(manifest))
}

fn find_manifest_string_type_mismatch(value: &CborValue) -> Option<String> {
    let CborValue::Map(map) = value else {
        return None;
    };
    let symbols = symbols_map(map);

    if let Some(pack_id) = map_get(map, "pack_id")
        && !value_is_string_or_symbol(pack_id, symbols, "pack_ids")
    {
        return Some("pack_id".to_string());
    }

    if let Some(meta) = map_get(map, "meta") {
        let CborValue::Map(meta_map) = meta else {
            return Some("meta".to_string());
        };
        if let Some(pack_id) = map_get(meta_map, "pack_id")
            && !value_is_string_or_symbol(pack_id, symbols, "pack_ids")
        {
            return Some("meta.pack_id".to_string());
        }
        if let Some(entry_flows) = map_get(meta_map, "entry_flows") {
            let CborValue::Array(values) = entry_flows else {
                return Some("meta.entry_flows".to_string());
            };
            for (idx, value) in values.iter().enumerate() {
                if !value_is_string_or_symbol(value, symbols, "flow_ids") {
                    return Some(format!("meta.entry_flows[{idx}]"));
                }
            }
        }
    }

    if let Some(flows) = map_get(map, "flows") {
        let CborValue::Array(values) = flows else {
            return Some("flows".to_string());
        };
        for (idx, value) in values.iter().enumerate() {
            let CborValue::Map(flow) = value else {
                return Some(format!("flows[{idx}]"));
            };
            if let Some(id) = map_get(flow, "id")
                && !value_is_string_or_symbol(id, symbols, "flow_ids")
            {
                return Some(format!("flows[{idx}].id"));
            }
            if let Some(entrypoints) = map_get(flow, "entrypoints") {
                let CborValue::Array(values) = entrypoints else {
                    return Some(format!("flows[{idx}].entrypoints"));
                };
                for (jdx, value) in values.iter().enumerate() {
                    if !value_is_string_or_symbol(value, symbols, "entrypoints") {
                        return Some(format!("flows[{idx}].entrypoints[{jdx}]"));
                    }
                }
            }
        }
    }

    None
}

fn map_get<'a>(
    map: &'a std::collections::BTreeMap<CborValue, CborValue>,
    key: &str,
) -> Option<&'a CborValue> {
    map.iter().find_map(|(k, v)| match k {
        CborValue::Text(text) if text == key => Some(v),
        _ => None,
    })
}

fn symbols_map(
    map: &std::collections::BTreeMap<CborValue, CborValue>,
) -> Option<&std::collections::BTreeMap<CborValue, CborValue>> {
    let symbols = map_get(map, "symbols")?;
    match symbols {
        CborValue::Map(map) => Some(map),
        _ => None,
    }
}

fn value_is_string_or_symbol(
    value: &CborValue,
    symbols: Option<&std::collections::BTreeMap<CborValue, CborValue>>,
    symbol_key: &str,
) -> bool {
    if matches!(value, CborValue::Text(_)) {
        return true;
    }
    let CborValue::Integer(idx) = value else {
        return false;
    };
    let symbols = match symbols {
        Some(symbols) => symbols,
        None => return true,
    };
    let Some(CborValue::Array(values)) = map_get(symbols, symbol_key)
        .or_else(|| map_get(symbols, symbol_key.strip_suffix('s').unwrap_or(symbol_key)))
    else {
        return true;
    };
    let idx = match usize::try_from(*idx) {
        Ok(idx) => idx,
        Err(_) => return true,
    };
    matches!(values.get(idx), Some(CborValue::Text(_)))
}

fn decode_manifest_lenient(value: &CborValue) -> anyhow::Result<PackManifest> {
    let CborValue::Map(map) = value else {
        return Err(anyhow::anyhow!("manifest is not a map"));
    };
    let symbols = symbols_map(map);

    let (meta_pack_id, meta_entry_flows) = if let Some(meta) = map_get(map, "meta") {
        let CborValue::Map(meta_map) = meta else {
            return Err(anyhow::anyhow!("meta is not a map"));
        };
        let pack_id = resolve_string_symbol(map_get(meta_map, "pack_id"), symbols, "pack_ids")?;
        let entry_flows = resolve_string_array(
            map_get(meta_map, "entry_flows"),
            symbols,
            "flow_ids",
            Some("entrypoints"),
        )?;
        (pack_id, entry_flows)
    } else {
        (None, Vec::new())
    };

    let pack_id = resolve_string_symbol(map_get(map, "pack_id"), symbols, "pack_ids")?
        .or(meta_pack_id)
        .ok_or_else(|| anyhow::anyhow!("pack_id missing"))?;

    let mut flows = Vec::new();
    if let Some(flows_value) = map_get(map, "flows") {
        let CborValue::Array(values) = flows_value else {
            return Err(anyhow::anyhow!("flows is not an array"));
        };
        for (idx, value) in values.iter().enumerate() {
            let CborValue::Map(flow) = value else {
                return Err(anyhow::anyhow!("flows[{idx}] is not a map"));
            };
            let id = resolve_string_symbol(map_get(flow, "id"), symbols, "flow_ids")?
                .ok_or_else(|| anyhow::anyhow!("flows[{idx}].id missing"))?;
            let entrypoints =
                resolve_string_array(map_get(flow, "entrypoints"), symbols, "entrypoints", None)?;
            flows.push(PackFlow { id, entrypoints });
        }
    }

    Ok(PackManifest {
        meta: Some(PackMeta {
            pack_id,
            entry_flows: meta_entry_flows,
        }),
        pack_id: None,
        flows,
    })
}

fn resolve_string_symbol(
    value: Option<&CborValue>,
    symbols: Option<&std::collections::BTreeMap<CborValue, CborValue>>,
    symbol_key: &str,
) -> anyhow::Result<Option<String>> {
    let Some(value) = value else {
        return Ok(None);
    };
    match value {
        CborValue::Text(text) => Ok(Some(text.clone())),
        CborValue::Integer(idx) => {
            let Some(symbols) = symbols else {
                return Ok(Some(idx.to_string()));
            };
            let Some(values) = symbol_array(symbols, symbol_key) else {
                return Ok(Some(idx.to_string()));
            };
            let idx = usize::try_from(*idx).unwrap_or(usize::MAX);
            match values.get(idx) {
                Some(CborValue::Text(text)) => Ok(Some(text.clone())),
                _ => Ok(Some(idx.to_string())),
            }
        }
        _ => Err(anyhow::anyhow!("expected string or symbol index")),
    }
}

fn symbol_array<'a>(
    symbols: &'a std::collections::BTreeMap<CborValue, CborValue>,
    key: &'a str,
) -> Option<&'a Vec<CborValue>> {
    if let Some(CborValue::Array(values)) = map_get(symbols, key) {
        return Some(values);
    }
    if let Some(stripped) = key.strip_suffix('s')
        && let Some(CborValue::Array(values)) = map_get(symbols, stripped)
    {
        return Some(values);
    }
    None
}

fn resolve_string_array(
    value: Option<&CborValue>,
    symbols: Option<&std::collections::BTreeMap<CborValue, CborValue>>,
    symbol_key: &str,
    fallback_key: Option<&str>,
) -> anyhow::Result<Vec<String>> {
    let Some(value) = value else {
        return Ok(Vec::new());
    };
    let CborValue::Array(values) = value else {
        return Err(anyhow::anyhow!("expected array"));
    };
    let mut out = Vec::new();
    for (idx, value) in values.iter().enumerate() {
        match resolve_string_symbol(Some(value), symbols, symbol_key) {
            Ok(Some(value)) => out.push(value),
            Ok(None) => {}
            Err(err) => {
                if let Some(fallback_key) = fallback_key
                    && let Ok(Some(value)) =
                        resolve_string_symbol(Some(value), symbols, fallback_key)
                {
                    out.push(value);
                    continue;
                }
                return Err(anyhow::anyhow!("{err} at index {idx}"));
            }
        }
    }
    Ok(out)
}

fn missing_cbor_error(path: &Path) -> anyhow::Error {
    anyhow::anyhow!(
        "ERROR: demo packs must be CBOR-only (.gtpack must contain manifest.cbor). Rebuild the pack with greentic-pack build (do not use --dev). Missing in {}",
        path.display()
    )
}

pub(crate) fn domain_name(domain: Domain) -> &'static str {
    match domain {
        Domain::Messaging => "messaging",
        Domain::Events => "events",
        Domain::Secrets => "secrets",
        Domain::OAuth => "oauth",
    }
}