Skip to main content

greentic_setup/engine/
executors.rs

1//! Step executor implementations for the setup engine.
2//!
3//! Each executor handles a specific `SetupStepKind`.
4
5use std::path::{Path, PathBuf};
6
7use anyhow::Context;
8use serde_json::Value;
9use sha2::{Digest, Sha256};
10
11use crate::plan::{ResolvedPackInfo, SetupPlanMetadata};
12use crate::{bundle, bundle_source::BundleSource, discovery};
13
14use super::plan_builders::compute_simple_hash;
15use super::types::SetupConfig;
16
17/// Execute the CreateBundle step.
18pub fn execute_create_bundle(
19    bundle_path: &Path,
20    metadata: &SetupPlanMetadata,
21) -> anyhow::Result<()> {
22    bundle::create_demo_bundle_structure(bundle_path, metadata.bundle_name.as_deref())
23        .context("failed to create bundle structure")
24}
25
26/// Execute the ResolvePacks step.
27pub fn execute_resolve_packs(
28    _bundle_path: &Path,
29    metadata: &SetupPlanMetadata,
30) -> anyhow::Result<Vec<ResolvedPackInfo>> {
31    let mut resolved = Vec::new();
32    let mut failures = Vec::new();
33
34    for pack_ref in &metadata.pack_refs {
35        match resolve_pack_ref(pack_ref) {
36            Ok(resolved_path) => {
37                let canonical = resolved_path
38                    .canonicalize()
39                    .unwrap_or(resolved_path.clone());
40                let pack_meta = discovery::read_pack_meta(&canonical)?;
41                resolved.push(ResolvedPackInfo {
42                    source_ref: pack_ref.clone(),
43                    mapped_ref: canonical.display().to_string(),
44                    resolved_digest: compute_file_digest(&canonical)
45                        .unwrap_or_else(|_| format!("sha256:{}", compute_simple_hash(pack_ref))),
46                    pack_id: pack_meta.map(|meta| meta.pack_id).unwrap_or_else(|| {
47                        canonical
48                            .file_stem()
49                            .and_then(|s| s.to_str())
50                            .unwrap_or("unknown")
51                            .to_string()
52                    }),
53                    entry_flows: Vec::new(),
54                    cached_path: canonical.clone(),
55                    output_path: canonical,
56                });
57            }
58            Err(err) => {
59                failures.push(format!("{pack_ref}: {err}"));
60            }
61        }
62    }
63
64    if !failures.is_empty() {
65        anyhow::bail!(
66            "failed to resolve {} pack ref(s):\n{}",
67            failures.len(),
68            failures.join("\n")
69        );
70    }
71
72    Ok(resolved)
73}
74
75/// Execute the AddPacksToBundle step.
76pub fn execute_add_packs_to_bundle(
77    bundle_path: &Path,
78    resolved_packs: &[ResolvedPackInfo],
79) -> anyhow::Result<()> {
80    let mut metadata_entries = Vec::new();
81
82    for pack in resolved_packs {
83        // Determine target directory based on pack ID domain prefix
84        let target_dir = get_pack_target_dir(bundle_path, &pack.pack_id);
85        std::fs::create_dir_all(&target_dir)?;
86
87        let target_path = target_dir.join(format!("{}.gtpack", pack.pack_id));
88        if pack.cached_path.exists() && !target_path.exists() {
89            std::fs::copy(&pack.cached_path, &target_path).with_context(|| {
90                format!(
91                    "failed to copy pack {} to {}",
92                    pack.cached_path.display(),
93                    target_path.display()
94                )
95            })?;
96        }
97
98        let reference = target_path
99            .strip_prefix(bundle_path)
100            .unwrap_or(&target_path)
101            .to_string_lossy()
102            .replace('\\', "/");
103        let kind = if reference.starts_with("providers/") {
104            bundle::BundleReferenceKind::ExtensionProvider
105        } else {
106            bundle::BundleReferenceKind::AppPack
107        };
108        metadata_entries.push(bundle::BundleReference {
109            kind,
110            reference,
111            digest: Some(pack.resolved_digest.clone()),
112        });
113    }
114
115    bundle::register_bundle_references(bundle_path, &metadata_entries, None)?;
116    Ok(())
117}
118
119/// Determine the target directory for a pack based on its ID.
120///
121/// Packs with domain prefixes (e.g., `messaging-telegram`, `events-webhook`)
122/// go to `providers/<domain>/`. Other packs go to `packs/`.
123pub fn get_pack_target_dir(bundle_path: &Path, pack_id: &str) -> PathBuf {
124    const DOMAIN_PREFIXES: &[&str] = &[
125        "messaging-",
126        "events-",
127        "oauth-",
128        "secrets-",
129        "mcp-",
130        "state-",
131    ];
132
133    for prefix in DOMAIN_PREFIXES {
134        if pack_id.starts_with(prefix) {
135            let domain = prefix.trim_end_matches('-');
136            return bundle_path.join("providers").join(domain);
137        }
138    }
139
140    // Default to packs/ for non-provider packs
141    bundle_path.join("packs")
142}
143
144/// Execute the ApplyPackSetup step.
145pub fn execute_apply_pack_setup(
146    bundle_path: &Path,
147    metadata: &SetupPlanMetadata,
148    config: &SetupConfig,
149) -> anyhow::Result<usize> {
150    let mut count = 0;
151
152    if !metadata.providers_remove.is_empty() {
153        count += execute_remove_provider_artifacts(bundle_path, &metadata.providers_remove)?;
154    }
155
156    // Auto-install provider packs that are referenced in setup_answers
157    // but not yet present in the bundle.
158    auto_install_provider_packs(bundle_path, metadata);
159
160    // Discover packs so we can find pack_path for secret alias seeding
161    let discovered = if bundle_path.exists() {
162        discovery::discover(bundle_path).ok()
163    } else {
164        None
165    };
166
167    // Persist setup answers to local config files and dev secrets store
168    for (provider_id, answers) in &metadata.setup_answers {
169        // Write answers to provider config directory
170        let config_dir = bundle_path.join("state").join("config").join(provider_id);
171        std::fs::create_dir_all(&config_dir)?;
172
173        let config_path = config_dir.join("setup-answers.json");
174        let content =
175            serde_json::to_string_pretty(answers).context("failed to serialize setup answers")?;
176        std::fs::write(&config_path, content).with_context(|| {
177            format!(
178                "failed to write setup answers to: {}",
179                config_path.display()
180            )
181        })?;
182
183        // Persist all answer values to the dev secrets store so that
184        // WASM components can read them via the secrets API at runtime.
185        let pack_path = discovered.as_ref().and_then(|d| {
186            d.find_setup_target(provider_id)
187                .map(|p| p.pack_path.as_path())
188        });
189        let env = crate::resolve_env(Some(&config.env));
190        if config.verbose {
191            let team_display = config.team.as_deref().unwrap_or("(none)");
192            println!(
193                "  [secrets] scope: env={env}, tenant={}, team={team_display}, provider={provider_id}",
194                config.tenant
195            );
196            let example_uri = crate::canonical_secret_uri(
197                &env,
198                &config.tenant,
199                config.team.as_deref(),
200                provider_id,
201                "_example_key",
202            );
203            println!("  [secrets] URI pattern: {example_uri}");
204            if let Some(config_map) = answers.as_object() {
205                let keys: Vec<&String> = config_map.keys().collect();
206                println!("  [secrets] answer keys: {keys:?}");
207            }
208        }
209        let rt = tokio::runtime::Runtime::new()
210            .context("failed to create tokio runtime for secrets persistence")?;
211        let persisted = rt.block_on(crate::qa::persist::persist_all_config_as_secrets(
212            bundle_path,
213            &env,
214            &config.tenant,
215            config.team.as_deref(),
216            provider_id,
217            answers,
218            pack_path,
219        ))?;
220        if config.verbose {
221            if persisted.is_empty() {
222                println!(
223                    "  [secrets] WARNING: 0 key(s) persisted for {provider_id} (all values empty?)"
224                );
225            } else {
226                println!(
227                    "  [secrets] persisted {} key(s) for {provider_id}: {:?}",
228                    persisted.len(),
229                    persisted
230                );
231            }
232        }
233
234        // Materialize a provider config envelope so runtime/provider ingest
235        // paths can read setup-applied config, not just raw setup answers.
236        if let Some(pack_path) = pack_path {
237            crate::config_envelope::write_provider_config_envelope(
238                &bundle_path.join(".providers"),
239                provider_id,
240                "setup-input",
241                answers,
242                pack_path,
243                false,
244            )
245            .with_context(|| {
246                format!(
247                    "failed to write provider config envelope for {} using {}",
248                    provider_id,
249                    pack_path.display()
250                )
251            })?;
252        } else if config.verbose {
253            println!(
254                "  [config] WARNING: no resolved pack path for {provider_id}; skipped config envelope write"
255            );
256        }
257
258        // Sync OAuth answers to tenant config JSON for webchat-gui providers
259        match crate::tenant_config::sync_oauth_to_tenant_config(
260            bundle_path,
261            &config.tenant,
262            provider_id,
263            answers,
264        ) {
265            Ok(true) => {
266                if config.verbose {
267                    println!("  [oauth] updated tenant config for {provider_id}");
268                }
269            }
270            Ok(false) => {}
271            Err(e) => {
272                println!("  [oauth] WARNING: failed to update tenant config: {e}");
273            }
274        }
275
276        // Sync `skin` answer to tenant config JSON for webchat-gui providers
277        match crate::tenant_config::sync_skin_to_tenant_config(
278            bundle_path,
279            &config.tenant,
280            provider_id,
281            answers,
282        ) {
283            Ok(true) => {
284                if config.verbose {
285                    println!("  [skin] updated tenant config for {provider_id}");
286                }
287            }
288            Ok(false) => {}
289            Err(e) => {
290                println!("  [skin] WARNING: failed to update tenant config: {e}");
291            }
292        }
293
294        // Sync `nav_links_json` answer to tenant config JSON for webchat-gui providers
295        match crate::tenant_config::sync_nav_links_to_tenant_config(
296            bundle_path,
297            &config.tenant,
298            provider_id,
299            answers,
300        ) {
301            Ok(true) => {
302                if config.verbose {
303                    println!("  [nav_links] updated tenant config for {provider_id}");
304                }
305            }
306            Ok(false) => {}
307            Err(e) => {
308                println!("  [nav_links] WARNING: failed to update tenant config: {e}");
309            }
310        }
311
312        // Register webhooks if the provider needs one (e.g. Telegram, Slack, Webex)
313        if let Some(result) = crate::webhook::register_webhook(
314            provider_id,
315            answers,
316            &config.tenant,
317            config.team.as_deref(),
318        ) {
319            let ok = result.get("ok").and_then(Value::as_bool).unwrap_or(false);
320            if ok {
321                println!("  [webhook] registered for {provider_id}");
322            } else {
323                let err = result
324                    .get("error")
325                    .and_then(Value::as_str)
326                    .unwrap_or("unknown");
327                println!("  [webhook] WARNING: registration failed for {provider_id}: {err}");
328            }
329        }
330
331        count += 1;
332    }
333
334    crate::platform_setup::persist_static_routes_artifact(bundle_path, &metadata.static_routes)?;
335    let _ = crate::deployment_targets::persist_explicit_deployment_targets(
336        bundle_path,
337        &metadata.deployment_targets,
338    );
339
340    // Print post-setup instructions for providers needing manual steps
341    let provider_configs: Vec<(String, Value)> = metadata
342        .setup_answers
343        .iter()
344        .map(|(id, val)| (id.clone(), val.clone()))
345        .collect();
346    let team = config.team.as_deref().unwrap_or("default");
347    crate::webhook::print_post_setup_instructions(&provider_configs, &config.tenant, team);
348
349    Ok(count)
350}
351
352fn compute_file_digest(path: &Path) -> anyhow::Result<String> {
353    let bytes = std::fs::read(path).with_context(|| format!("read {}", path.display()))?;
354    let digest = Sha256::digest(bytes);
355    let encoded = digest
356        .iter()
357        .map(|byte| format!("{byte:02x}"))
358        .collect::<String>();
359    Ok(format!("sha256:{encoded}"))
360}
361
362fn resolve_pack_ref(pack_ref: &str) -> anyhow::Result<PathBuf> {
363    let source = BundleSource::parse(pack_ref)?;
364    let resolved = source.resolve()?;
365
366    if resolved.extension().and_then(|ext| ext.to_str()) != Some("gtpack") {
367        anyhow::bail!(
368            "resolved pack ref is not a .gtpack file: {}",
369            resolved.display()
370        );
371    }
372
373    Ok(resolved)
374}
375
376/// Remove provider artifacts and config directories.
377pub fn execute_remove_provider_artifacts(
378    bundle_path: &Path,
379    providers_remove: &[String],
380) -> anyhow::Result<usize> {
381    let mut removed = 0usize;
382    let discovered = discovery::discover(bundle_path).ok();
383    for provider_id in providers_remove {
384        if let Some(discovered) = discovered.as_ref()
385            && let Some(provider) = discovered
386                .providers
387                .iter()
388                .find(|provider| provider.provider_id == *provider_id)
389        {
390            if provider.pack_path.exists() {
391                std::fs::remove_file(&provider.pack_path).with_context(|| {
392                    format!(
393                        "failed to remove provider pack {}",
394                        provider.pack_path.display()
395                    )
396                })?;
397            }
398            removed += 1;
399        } else {
400            let target_dir = get_pack_target_dir(bundle_path, provider_id);
401            let target_path = target_dir.join(format!("{provider_id}.gtpack"));
402            if target_path.exists() {
403                std::fs::remove_file(&target_path).with_context(|| {
404                    format!("failed to remove provider pack {}", target_path.display())
405                })?;
406                removed += 1;
407            }
408        }
409
410        let config_dir = bundle_path.join("state").join("config").join(provider_id);
411        if config_dir.exists() {
412            std::fs::remove_dir_all(&config_dir).with_context(|| {
413                format!(
414                    "failed to remove provider config dir {}",
415                    config_dir.display()
416                )
417            })?;
418        }
419    }
420    Ok(removed)
421}
422
423/// Search sibling bundles for provider packs referenced in setup_answers
424/// and install them into this bundle if missing.
425///
426/// "Missing" is determined by pack_id, not filename: a pack file with any
427/// filename that declares the matching pack_id in its manifest counts as
428/// already installed. Otherwise a custom-named pack (e.g. a tenant-specific
429/// build placed alongside the canonical name) gets clobbered every time
430/// setup runs.
431pub fn auto_install_provider_packs(bundle_path: &Path, metadata: &SetupPlanMetadata) {
432    let bundle_abs =
433        std::fs::canonicalize(bundle_path).unwrap_or_else(|_| bundle_path.to_path_buf());
434
435    let installed_ids: std::collections::HashSet<String> = discovery::discover(bundle_path)
436        .map(|d| {
437            d.providers
438                .into_iter()
439                .chain(d.app_packs)
440                .map(|p| p.provider_id)
441                .collect()
442        })
443        .unwrap_or_default();
444
445    for provider_id in metadata.setup_answers.keys() {
446        if installed_ids.contains(provider_id) {
447            continue;
448        }
449        let target_dir = get_pack_target_dir(bundle_path, provider_id);
450        let target_path = target_dir.join(format!("{provider_id}.gtpack"));
451        if target_path.exists() {
452            continue;
453        }
454
455        // Determine the provider domain from the ID
456        let domain = domain_from_provider_id(provider_id);
457
458        // Search for the pack in sibling bundles and build output
459        if let Some(source) = find_provider_pack_source(provider_id, domain, &bundle_abs) {
460            if let Err(err) = std::fs::create_dir_all(&target_dir) {
461                eprintln!(
462                    "  [provider] WARNING: failed to create {}: {err}",
463                    target_dir.display()
464                );
465                continue;
466            }
467            match std::fs::copy(&source, &target_path) {
468                Ok(_) => println!(
469                    "  [provider] installed {provider_id}.gtpack from {}",
470                    source.display()
471                ),
472                Err(err) => eprintln!(
473                    "  [provider] WARNING: failed to copy {}: {err}",
474                    source.display()
475                ),
476            }
477        } else {
478            eprintln!("  [provider] WARNING: {provider_id}.gtpack not found in sibling bundles");
479        }
480    }
481}
482
483/// Extract domain from a provider ID (e.g. "messaging-telegram" → "messaging").
484pub fn domain_from_provider_id(provider_id: &str) -> &str {
485    const DOMAIN_PREFIXES: &[&str] = &[
486        "messaging-",
487        "events-",
488        "oauth-",
489        "secrets-",
490        "mcp-",
491        "state-",
492        "telemetry-",
493    ];
494    for prefix in DOMAIN_PREFIXES {
495        if provider_id.starts_with(prefix) {
496            return prefix.trim_end_matches('-');
497        }
498    }
499    "messaging" // default
500}
501
502/// Search known locations for a provider pack file.
503///
504/// Search order:
505/// 1. Sibling bundle directories: `../<bundle>/providers/<domain>/<id>.gtpack`
506/// 2. Build output: `../greentic-messaging-providers/target/packs/<id>.gtpack`
507pub fn find_provider_pack_source(
508    provider_id: &str,
509    domain: &str,
510    bundle_abs: &Path,
511) -> Option<PathBuf> {
512    let parent = bundle_abs.parent()?;
513    let filename = format!("{provider_id}.gtpack");
514
515    // 1. Sibling bundles
516    if let Ok(entries) = std::fs::read_dir(parent) {
517        for entry in entries.flatten() {
518            let sibling = entry.path();
519            if sibling == *bundle_abs || !sibling.is_dir() {
520                continue;
521            }
522            let candidate = sibling.join("providers").join(domain).join(&filename);
523            if candidate.is_file() {
524                return Some(candidate);
525            }
526        }
527    }
528
529    // 2. Build output from greentic-messaging-providers
530    for ancestor in parent.ancestors().take(4) {
531        let candidate = ancestor
532            .join("greentic-messaging-providers")
533            .join("target")
534            .join("packs")
535            .join(&filename);
536        if candidate.is_file() {
537            return Some(candidate);
538        }
539    }
540
541    None
542}
543
544/// Execute the WriteGmapRules step.
545pub fn execute_write_gmap_rules(
546    bundle_path: &Path,
547    metadata: &SetupPlanMetadata,
548) -> anyhow::Result<()> {
549    for tenant_sel in &metadata.tenants {
550        let gmap_path =
551            bundle::gmap_path(bundle_path, &tenant_sel.tenant, tenant_sel.team.as_deref());
552
553        if let Some(parent) = gmap_path.parent() {
554            std::fs::create_dir_all(parent)?;
555        }
556
557        // Build gmap content from allow_paths
558        let mut content = String::new();
559        if tenant_sel.allow_paths.is_empty() {
560            content.push_str("_ = forbidden\n");
561        } else {
562            for path in &tenant_sel.allow_paths {
563                content.push_str(&format!("{} = allowed\n", path));
564            }
565            content.push_str("_ = forbidden\n");
566        }
567
568        std::fs::write(&gmap_path, content)
569            .with_context(|| format!("failed to write gmap: {}", gmap_path.display()))?;
570    }
571    Ok(())
572}
573
574/// Execute the CopyResolvedManifest step.
575pub fn execute_copy_resolved_manifests(
576    bundle_path: &Path,
577    metadata: &SetupPlanMetadata,
578) -> anyhow::Result<Vec<PathBuf>> {
579    let mut manifests = Vec::new();
580    let resolved_dir = bundle_path.join("resolved");
581    std::fs::create_dir_all(&resolved_dir)?;
582
583    for tenant_sel in &metadata.tenants {
584        let filename =
585            bundle::resolved_manifest_filename(&tenant_sel.tenant, tenant_sel.team.as_deref());
586        let manifest_path = resolved_dir.join(&filename);
587
588        // Create an empty manifest placeholder if it doesn't exist
589        if !manifest_path.exists() {
590            std::fs::write(&manifest_path, "# Resolved manifest placeholder\n")?;
591        }
592        manifests.push(manifest_path);
593    }
594
595    Ok(manifests)
596}
597
598/// Execute the ValidateBundle step.
599pub fn execute_validate_bundle(bundle_path: &Path) -> anyhow::Result<()> {
600    bundle::validate_bundle_exists(bundle_path)
601}
602
603/// Execute the BuildFlowIndex step.
604///
605/// Scans all flows in the bundle, builds a TF-IDF index and a routing-compatible
606/// index, and optionally generates intents.md documentation.
607/// Output is written to `bundle/state/indexes/`.
608///
609/// Requires the `fast2flow` feature AND the `fast2flow-bundle` crate wired as a
610/// dependency.  Until `fast2flow-bundle` is published or vendored, this is a
611/// no-op stub that logs a skip message.
612pub fn execute_build_flow_index(_bundle_path: &Path, _config: &SetupConfig) -> anyhow::Result<()> {
613    tracing::debug!("fast2flow indexing skipped (fast2flow-bundle not available)");
614    Ok(())
615}
616
617#[cfg(test)]
618mod tests {
619    use super::*;
620    use crate::platform_setup::StaticRoutesPolicy;
621    use std::collections::BTreeSet;
622
623    fn empty_metadata(pack_refs: Vec<String>) -> SetupPlanMetadata {
624        SetupPlanMetadata {
625            bundle_name: None,
626            pack_refs,
627            tenants: Vec::new(),
628            default_assignments: Vec::new(),
629            providers: Vec::new(),
630            update_ops: BTreeSet::new(),
631            remove_targets: BTreeSet::new(),
632            packs_remove: Vec::new(),
633            providers_remove: Vec::new(),
634            tenants_remove: Vec::new(),
635            access_changes: Vec::new(),
636            static_routes: StaticRoutesPolicy::default(),
637            deployment_targets: Vec::new(),
638            setup_answers: serde_json::Map::new(),
639            tunnel: None,
640        }
641    }
642
643    #[test]
644    fn resolve_packs_errors_when_any_pack_ref_fails() {
645        let metadata = empty_metadata(vec!["/definitely/missing/example.gtpack".to_string()]);
646        let err = execute_resolve_packs(Path::new("."), &metadata).unwrap_err();
647        let message = err.to_string();
648
649        assert!(message.contains("failed to resolve 1 pack ref"));
650        assert!(message.contains("/definitely/missing/example.gtpack"));
651    }
652
653    /// Regression: a custom-named pack whose manifest declares the matching
654    /// pack_id must satisfy `auto_install_provider_packs`. Filename-only
655    /// detection caused tenant-specific builds (e.g. `*-3aigent.gtpack`) to
656    /// be clobbered by the canonical name on every setup run.
657    #[test]
658    fn auto_install_skips_when_pack_id_matches_under_custom_filename() {
659        use std::io::Write;
660        use zip::write::{FileOptions, ZipWriter};
661
662        let temp = tempfile::tempdir().expect("tempdir");
663        let bundle = temp.path().join("bundle");
664        let messaging_dir = bundle.join("providers").join("messaging");
665        std::fs::create_dir_all(&messaging_dir).expect("create messaging dir");
666
667        let custom_pack = messaging_dir.join("messaging-webchat-gui-3aigent.gtpack");
668        let file = std::fs::File::create(&custom_pack).expect("create pack file");
669        let mut writer = ZipWriter::new(file);
670        let options: FileOptions<'_, ()> =
671            FileOptions::default().compression_method(zip::CompressionMethod::Stored);
672        writer
673            .start_file("pack.manifest.json", options)
674            .expect("start manifest");
675        writer
676            .write_all(
677                serde_json::json!({
678                    "pack_id": "messaging-webchat-gui",
679                    "display_name": "WebChat GUI",
680                })
681                .to_string()
682                .as_bytes(),
683            )
684            .expect("write manifest");
685        writer.finish().expect("finish zip");
686
687        let canonical_pack = messaging_dir.join("messaging-webchat-gui.gtpack");
688        assert!(!canonical_pack.exists(), "precondition: canonical absent");
689
690        let mut metadata = empty_metadata(vec![]);
691        metadata.setup_answers.insert(
692            "messaging-webchat-gui".to_string(),
693            serde_json::Value::Object(serde_json::Map::new()),
694        );
695
696        auto_install_provider_packs(&bundle, &metadata);
697
698        assert!(
699            custom_pack.exists(),
700            "custom-named pack must be left in place"
701        );
702        assert!(
703            !canonical_pack.exists(),
704            "must not auto-install canonical-named duplicate when pack_id already present"
705        );
706    }
707}