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        if provider_id.contains("webchat-gui") && config.verbose {
296            let preview = answers
297                .as_object()
298                .and_then(|m| m.get("nav_links"))
299                .map(|v| serde_json::to_string(v).unwrap_or_else(|_| "<unserializable>".into()))
300                .unwrap_or_else(|| "<absent>".into());
301            println!("  [nav_links] received answer for {provider_id}: {preview}");
302        }
303        match crate::tenant_config::sync_nav_links_to_tenant_config(
304            bundle_path,
305            &config.tenant,
306            provider_id,
307            answers,
308        ) {
309            Ok(true) => {
310                if config.verbose {
311                    println!("  [nav_links] updated tenant config for {provider_id}");
312                }
313            }
314            Ok(false) => {}
315            Err(e) => {
316                println!("  [nav_links] WARNING: failed to update tenant config: {e}");
317            }
318        }
319
320        // Register webhooks if the provider needs one (e.g. Telegram, Slack, Webex)
321        if let Some(result) = crate::webhook::register_webhook(
322            provider_id,
323            answers,
324            &config.tenant,
325            config.team.as_deref(),
326        ) {
327            let ok = result.get("ok").and_then(Value::as_bool).unwrap_or(false);
328            if ok {
329                println!("  [webhook] registered for {provider_id}");
330            } else {
331                let err = result
332                    .get("error")
333                    .and_then(Value::as_str)
334                    .unwrap_or("unknown");
335                println!("  [webhook] WARNING: registration failed for {provider_id}: {err}");
336            }
337        }
338
339        count += 1;
340    }
341
342    crate::platform_setup::persist_static_routes_artifact(bundle_path, &metadata.static_routes)?;
343    let _ = crate::deployment_targets::persist_explicit_deployment_targets(
344        bundle_path,
345        &metadata.deployment_targets,
346    );
347
348    // Print post-setup instructions for providers needing manual steps
349    let provider_configs: Vec<(String, Value)> = metadata
350        .setup_answers
351        .iter()
352        .map(|(id, val)| (id.clone(), val.clone()))
353        .collect();
354    let team = config.team.as_deref().unwrap_or("default");
355    crate::webhook::print_post_setup_instructions(&provider_configs, &config.tenant, team);
356
357    Ok(count)
358}
359
360fn compute_file_digest(path: &Path) -> anyhow::Result<String> {
361    let bytes = std::fs::read(path).with_context(|| format!("read {}", path.display()))?;
362    let digest = Sha256::digest(bytes);
363    let encoded = digest
364        .iter()
365        .map(|byte| format!("{byte:02x}"))
366        .collect::<String>();
367    Ok(format!("sha256:{encoded}"))
368}
369
370fn resolve_pack_ref(pack_ref: &str) -> anyhow::Result<PathBuf> {
371    let source = BundleSource::parse(pack_ref)?;
372    let resolved = source.resolve()?;
373
374    if resolved.extension().and_then(|ext| ext.to_str()) != Some("gtpack") {
375        anyhow::bail!(
376            "resolved pack ref is not a .gtpack file: {}",
377            resolved.display()
378        );
379    }
380
381    Ok(resolved)
382}
383
384/// Remove provider artifacts and config directories.
385pub fn execute_remove_provider_artifacts(
386    bundle_path: &Path,
387    providers_remove: &[String],
388) -> anyhow::Result<usize> {
389    let mut removed = 0usize;
390    let discovered = discovery::discover(bundle_path).ok();
391    for provider_id in providers_remove {
392        if let Some(discovered) = discovered.as_ref()
393            && let Some(provider) = discovered
394                .providers
395                .iter()
396                .find(|provider| provider.provider_id == *provider_id)
397        {
398            if provider.pack_path.exists() {
399                std::fs::remove_file(&provider.pack_path).with_context(|| {
400                    format!(
401                        "failed to remove provider pack {}",
402                        provider.pack_path.display()
403                    )
404                })?;
405            }
406            removed += 1;
407        } else {
408            let target_dir = get_pack_target_dir(bundle_path, provider_id);
409            let target_path = target_dir.join(format!("{provider_id}.gtpack"));
410            if target_path.exists() {
411                std::fs::remove_file(&target_path).with_context(|| {
412                    format!("failed to remove provider pack {}", target_path.display())
413                })?;
414                removed += 1;
415            }
416        }
417
418        let config_dir = bundle_path.join("state").join("config").join(provider_id);
419        if config_dir.exists() {
420            std::fs::remove_dir_all(&config_dir).with_context(|| {
421                format!(
422                    "failed to remove provider config dir {}",
423                    config_dir.display()
424                )
425            })?;
426        }
427    }
428    Ok(removed)
429}
430
431/// Search sibling bundles for provider packs referenced in setup_answers
432/// and install them into this bundle if missing.
433///
434/// "Missing" is determined by pack_id, not filename: a pack file with any
435/// filename that declares the matching pack_id in its manifest counts as
436/// already installed. Otherwise a custom-named pack (e.g. a tenant-specific
437/// build placed alongside the canonical name) gets clobbered every time
438/// setup runs.
439pub fn auto_install_provider_packs(bundle_path: &Path, metadata: &SetupPlanMetadata) {
440    let bundle_abs =
441        std::fs::canonicalize(bundle_path).unwrap_or_else(|_| bundle_path.to_path_buf());
442
443    let installed_ids: std::collections::HashSet<String> = discovery::discover(bundle_path)
444        .map(|d| {
445            d.providers
446                .into_iter()
447                .chain(d.app_packs)
448                .map(|p| p.provider_id)
449                .collect()
450        })
451        .unwrap_or_default();
452
453    for provider_id in metadata.setup_answers.keys() {
454        if installed_ids.contains(provider_id) {
455            continue;
456        }
457        let target_dir = get_pack_target_dir(bundle_path, provider_id);
458        let target_path = target_dir.join(format!("{provider_id}.gtpack"));
459        if target_path.exists() {
460            continue;
461        }
462
463        // Determine the provider domain from the ID
464        let domain = domain_from_provider_id(provider_id);
465
466        // Search for the pack in sibling bundles and build output
467        if let Some(source) = find_provider_pack_source(provider_id, domain, &bundle_abs) {
468            if let Err(err) = std::fs::create_dir_all(&target_dir) {
469                eprintln!(
470                    "  [provider] WARNING: failed to create {}: {err}",
471                    target_dir.display()
472                );
473                continue;
474            }
475            match std::fs::copy(&source, &target_path) {
476                Ok(_) => println!(
477                    "  [provider] installed {provider_id}.gtpack from {}",
478                    source.display()
479                ),
480                Err(err) => eprintln!(
481                    "  [provider] WARNING: failed to copy {}: {err}",
482                    source.display()
483                ),
484            }
485        } else {
486            eprintln!("  [provider] WARNING: {provider_id}.gtpack not found in sibling bundles");
487        }
488    }
489}
490
491/// Extract domain from a provider ID (e.g. "messaging-telegram" → "messaging").
492pub fn domain_from_provider_id(provider_id: &str) -> &str {
493    const DOMAIN_PREFIXES: &[&str] = &[
494        "messaging-",
495        "events-",
496        "oauth-",
497        "secrets-",
498        "mcp-",
499        "state-",
500        "telemetry-",
501    ];
502    for prefix in DOMAIN_PREFIXES {
503        if provider_id.starts_with(prefix) {
504            return prefix.trim_end_matches('-');
505        }
506    }
507    "messaging" // default
508}
509
510/// Search known locations for a provider pack file.
511///
512/// Search order:
513/// 1. Sibling bundle directories: `../<bundle>/providers/<domain>/<id>.gtpack`
514/// 2. Build output: `../greentic-messaging-providers/target/packs/<id>.gtpack`
515pub fn find_provider_pack_source(
516    provider_id: &str,
517    domain: &str,
518    bundle_abs: &Path,
519) -> Option<PathBuf> {
520    let parent = bundle_abs.parent()?;
521    let filename = format!("{provider_id}.gtpack");
522
523    // 1. Sibling bundles
524    if let Ok(entries) = std::fs::read_dir(parent) {
525        for entry in entries.flatten() {
526            let sibling = entry.path();
527            if sibling == *bundle_abs || !sibling.is_dir() {
528                continue;
529            }
530            let candidate = sibling.join("providers").join(domain).join(&filename);
531            if candidate.is_file() {
532                return Some(candidate);
533            }
534        }
535    }
536
537    // 2. Build output from greentic-messaging-providers
538    for ancestor in parent.ancestors().take(4) {
539        let candidate = ancestor
540            .join("greentic-messaging-providers")
541            .join("target")
542            .join("packs")
543            .join(&filename);
544        if candidate.is_file() {
545            return Some(candidate);
546        }
547    }
548
549    None
550}
551
552/// Execute the WriteGmapRules step.
553pub fn execute_write_gmap_rules(
554    bundle_path: &Path,
555    metadata: &SetupPlanMetadata,
556) -> anyhow::Result<()> {
557    for tenant_sel in &metadata.tenants {
558        let gmap_path =
559            bundle::gmap_path(bundle_path, &tenant_sel.tenant, tenant_sel.team.as_deref());
560
561        if let Some(parent) = gmap_path.parent() {
562            std::fs::create_dir_all(parent)?;
563        }
564
565        // Build gmap content from allow_paths
566        let mut content = String::new();
567        if tenant_sel.allow_paths.is_empty() {
568            content.push_str("_ = forbidden\n");
569        } else {
570            for path in &tenant_sel.allow_paths {
571                content.push_str(&format!("{} = allowed\n", path));
572            }
573            content.push_str("_ = forbidden\n");
574        }
575
576        std::fs::write(&gmap_path, content)
577            .with_context(|| format!("failed to write gmap: {}", gmap_path.display()))?;
578    }
579    Ok(())
580}
581
582/// Execute the CopyResolvedManifest step.
583pub fn execute_copy_resolved_manifests(
584    bundle_path: &Path,
585    metadata: &SetupPlanMetadata,
586) -> anyhow::Result<Vec<PathBuf>> {
587    let mut manifests = Vec::new();
588    let resolved_dir = bundle_path.join("resolved");
589    std::fs::create_dir_all(&resolved_dir)?;
590
591    for tenant_sel in &metadata.tenants {
592        let filename =
593            bundle::resolved_manifest_filename(&tenant_sel.tenant, tenant_sel.team.as_deref());
594        let manifest_path = resolved_dir.join(&filename);
595
596        // Create an empty manifest placeholder if it doesn't exist
597        if !manifest_path.exists() {
598            std::fs::write(&manifest_path, "# Resolved manifest placeholder\n")?;
599        }
600        manifests.push(manifest_path);
601    }
602
603    Ok(manifests)
604}
605
606/// Execute the ValidateBundle step.
607pub fn execute_validate_bundle(bundle_path: &Path) -> anyhow::Result<()> {
608    bundle::validate_bundle_exists(bundle_path)
609}
610
611/// Execute the BuildFlowIndex step.
612///
613/// Scans all flows in the bundle, builds a TF-IDF index and a routing-compatible
614/// index, and optionally generates intents.md documentation.
615/// Output is written to `bundle/state/indexes/`.
616///
617/// Requires the `fast2flow` feature AND the `fast2flow-bundle` crate wired as a
618/// dependency.  Until `fast2flow-bundle` is published or vendored, this is a
619/// no-op stub that logs a skip message.
620pub fn execute_build_flow_index(_bundle_path: &Path, _config: &SetupConfig) -> anyhow::Result<()> {
621    tracing::debug!("fast2flow indexing skipped (fast2flow-bundle not available)");
622    Ok(())
623}
624
625#[cfg(test)]
626mod tests {
627    use super::*;
628    use crate::platform_setup::StaticRoutesPolicy;
629    use std::collections::BTreeSet;
630
631    fn empty_metadata(pack_refs: Vec<String>) -> SetupPlanMetadata {
632        SetupPlanMetadata {
633            bundle_name: None,
634            pack_refs,
635            tenants: Vec::new(),
636            default_assignments: Vec::new(),
637            providers: Vec::new(),
638            update_ops: BTreeSet::new(),
639            remove_targets: BTreeSet::new(),
640            packs_remove: Vec::new(),
641            providers_remove: Vec::new(),
642            tenants_remove: Vec::new(),
643            access_changes: Vec::new(),
644            static_routes: StaticRoutesPolicy::default(),
645            deployment_targets: Vec::new(),
646            setup_answers: serde_json::Map::new(),
647            tunnel: None,
648        }
649    }
650
651    #[test]
652    fn resolve_packs_errors_when_any_pack_ref_fails() {
653        let metadata = empty_metadata(vec!["/definitely/missing/example.gtpack".to_string()]);
654        let err = execute_resolve_packs(Path::new("."), &metadata).unwrap_err();
655        let message = err.to_string();
656
657        assert!(message.contains("failed to resolve 1 pack ref"));
658        assert!(message.contains("/definitely/missing/example.gtpack"));
659    }
660
661    /// Regression: a custom-named pack whose manifest declares the matching
662    /// pack_id must satisfy `auto_install_provider_packs`. Filename-only
663    /// detection caused tenant-specific builds (e.g. `*-3aigent.gtpack`) to
664    /// be clobbered by the canonical name on every setup run.
665    #[test]
666    fn auto_install_skips_when_pack_id_matches_under_custom_filename() {
667        use std::io::Write;
668        use zip::write::{FileOptions, ZipWriter};
669
670        let temp = tempfile::tempdir().expect("tempdir");
671        let bundle = temp.path().join("bundle");
672        let messaging_dir = bundle.join("providers").join("messaging");
673        std::fs::create_dir_all(&messaging_dir).expect("create messaging dir");
674
675        let custom_pack = messaging_dir.join("messaging-webchat-gui-3aigent.gtpack");
676        let file = std::fs::File::create(&custom_pack).expect("create pack file");
677        let mut writer = ZipWriter::new(file);
678        let options: FileOptions<'_, ()> =
679            FileOptions::default().compression_method(zip::CompressionMethod::Stored);
680        writer
681            .start_file("pack.manifest.json", options)
682            .expect("start manifest");
683        writer
684            .write_all(
685                serde_json::json!({
686                    "pack_id": "messaging-webchat-gui",
687                    "display_name": "WebChat GUI",
688                })
689                .to_string()
690                .as_bytes(),
691            )
692            .expect("write manifest");
693        writer.finish().expect("finish zip");
694
695        let canonical_pack = messaging_dir.join("messaging-webchat-gui.gtpack");
696        assert!(!canonical_pack.exists(), "precondition: canonical absent");
697
698        let mut metadata = empty_metadata(vec![]);
699        metadata.setup_answers.insert(
700            "messaging-webchat-gui".to_string(),
701            serde_json::Value::Object(serde_json::Map::new()),
702        );
703
704        auto_install_provider_packs(&bundle, &metadata);
705
706        assert!(
707            custom_pack.exists(),
708            "custom-named pack must be left in place"
709        );
710        assert!(
711            !canonical_pack.exists(),
712            "must not auto-install canonical-named duplicate when pack_id already present"
713        );
714    }
715}