Skip to main content

mermaid_cli/cli/
commands.rs

1use anyhow::{Context, Result, anyhow, bail};
2use std::path::Path;
3use std::sync::Arc;
4
5use crate::{
6    app::{Config, get_config_dir, init_config, load_config_or_warn},
7    domain::{
8        ChatRequest, Cmd, CompactionRecord, CompactionResult, CompactionTrigger, Msg, SlashCmd,
9        State, build_replacement_messages, estimate_context_usage_for_request, prepare_compaction,
10        update,
11    },
12    models::{BackendConfig, ChatMessage, Model, PROVIDER_REGISTRY, lookup_provider},
13    ollama::is_installed as is_ollama_installed,
14    runtime::{NewProviderProbe, RuntimeClient, RuntimeStore, TaskRecord},
15    session::ConversationManager,
16    utils::{resolve_api_key, resolve_api_key_with_fallback},
17};
18
19use super::{Commands, GitHost, OutputFormat, PairCommand, PluginCommand, PrCommand, QaCommand};
20
21/// Handle CLI subcommands
22/// Returns Ok(true) if the command was handled and we should exit
23/// Returns Ok(false) if we should continue to the main application
24pub async fn handle_command(
25    command: &Commands,
26    config: &Config,
27    cwd: &Path,
28    cli_model: Option<&str>,
29) -> Result<bool> {
30    match command {
31        Commands::Init => {
32            println!("Initializing Mermaid configuration...");
33            init_config()?;
34            println!("Configuration initialized successfully!");
35            Ok(true)
36        },
37        Commands::List => {
38            list_models(config).await?;
39            Ok(true)
40        },
41        Commands::Models => {
42            show_models(config).await?;
43            Ok(true)
44        },
45        Commands::ModelInfo { model } => {
46            show_model_info(model, config).await?;
47            Ok(true)
48        },
49        Commands::Version => {
50            show_version();
51            Ok(true)
52        },
53        Commands::Update { check, force } => {
54            run_update(*check, *force).await?;
55            Ok(true)
56        },
57        Commands::Status => {
58            show_status(config).await?;
59            Ok(true)
60        },
61        Commands::Doctor { format } => {
62            show_doctor(config, cwd, cli_model, *format).await?;
63            Ok(true)
64        },
65        Commands::SelfTest {
66            format,
67            keep_workspace,
68        } => {
69            run_self_test(config, *format, *keep_workspace)?;
70            Ok(true)
71        },
72        Commands::Tasks { limit } => {
73            show_tasks(*limit)?;
74            Ok(true)
75        },
76        Commands::Task { id } => {
77            show_task(id)?;
78            Ok(true)
79        },
80        Commands::Processes { limit } => {
81            show_processes(*limit)?;
82            Ok(true)
83        },
84        Commands::Logs { id } => {
85            show_logs(id)?;
86            Ok(true)
87        },
88        Commands::Stop { id } => {
89            stop_process(id)?;
90            Ok(true)
91        },
92        Commands::Restart { id } => {
93            restart_process(id)?;
94            Ok(true)
95        },
96        Commands::Open { target } => {
97            open_target(target)?;
98            Ok(true)
99        },
100        Commands::Ports => {
101            show_ports()?;
102            Ok(true)
103        },
104        Commands::Approvals => {
105            show_approvals()?;
106            Ok(true)
107        },
108        Commands::Approve { id } => {
109            approve(id)?;
110            Ok(true)
111        },
112        Commands::Deny { id } => {
113            deny(id)?;
114            Ok(true)
115        },
116        Commands::ToolRuns { limit } => {
117            show_tool_runs(*limit)?;
118            Ok(true)
119        },
120        Commands::Checkpoints { limit } => {
121            show_checkpoints(*limit)?;
122            Ok(true)
123        },
124        Commands::Restore { id, force } => {
125            restore_checkpoint(id, *force)?;
126            Ok(true)
127        },
128        Commands::Plugin { command } => {
129            handle_plugin(command)?;
130            Ok(true)
131        },
132        Commands::Daemon { command } => {
133            super::daemon::handle_daemon_command(command)?;
134            Ok(true)
135        },
136        Commands::Pair { command } => {
137            handle_pair(command)?;
138            Ok(true)
139        },
140        Commands::Qa { command } => {
141            handle_qa(command, config, cwd)?;
142            Ok(true)
143        },
144        Commands::Add { name, yes } => {
145            crate::mcp::add_server(name, *yes).await?;
146            Ok(true)
147        },
148        Commands::Remove { name } => {
149            crate::mcp::remove_server(name).await?;
150            Ok(true)
151        },
152        Commands::Pr { command } => {
153            handle_pr(command)?;
154            Ok(true)
155        },
156        Commands::Mcp => {
157            show_mcp_servers();
158            Ok(true)
159        },
160        Commands::CloudSetup => {
161            // Interactive stdin prompt — runs before the TUI enters
162            // raw mode so rpassword works. The in-TUI slash command
163            // `/cloud-setup` just points users here.
164            let _ = crate::ollama::setup_cloud_interactive();
165            Ok(true)
166        },
167        Commands::Chat => Ok(false),       // Continue to chat interface
168        Commands::Run { .. } => Ok(false), // Handled by main.rs
169    }
170}
171
172fn handle_qa(command: &QaCommand, config: &Config, cwd: &Path) -> Result<()> {
173    match command {
174        QaCommand::CompactSmoke { turns, format } => {
175            let report = match run_qa_compact_smoke(config, cwd, *turns) {
176                Ok(report) => report,
177                Err(err) => QaCompactSmokeReport::failed(cwd, *turns, err.to_string()),
178            };
179            print_qa_compact_report(&report, *format)?;
180            anyhow::ensure!(report.ok, "qa compact smoke failed");
181            Ok(())
182        },
183    }
184}
185
186#[derive(Debug, serde::Serialize)]
187struct DoctorReport {
188    ok: bool,
189    cwd: String,
190    active_model: Option<String>,
191    model_error: Option<String>,
192    model_capabilities: Option<DoctorModelCapabilities>,
193    safety_mode: String,
194    checkpoint_on_mutation: bool,
195    prompt_customized: bool,
196    ollama: DoctorCheck,
197    remote_providers: Vec<String>,
198    project_instructions: DoctorCheck,
199    tools: Vec<String>,
200    runtime: DoctorRuntime,
201    next_steps: Vec<String>,
202}
203
204#[derive(Debug, serde::Serialize)]
205struct DoctorModelCapabilities {
206    provider: String,
207    name: String,
208    supports_tools: bool,
209    supports_vision: bool,
210    reasoning: String,
211    max_context_tokens: Option<usize>,
212}
213
214#[derive(Debug, serde::Serialize)]
215struct DoctorCheck {
216    status: &'static str,
217    message: String,
218}
219
220#[derive(Debug, serde::Serialize)]
221struct DoctorRuntime {
222    daemon: DoctorCheck,
223    local_store: DoctorCheck,
224}
225
226async fn show_doctor(
227    config: &Config,
228    cwd: &Path,
229    cli_model: Option<&str>,
230    format: OutputFormat,
231) -> Result<()> {
232    let active_model_result = crate::app::resolve_model_id(cli_model, config).await;
233    let (active_model, model_error, model_capabilities) = match active_model_result {
234        Ok(model) => {
235            let snapshot = crate::domain::ProviderCapabilitySnapshot::from_model_id(&model);
236            (
237                Some(model),
238                None,
239                Some(DoctorModelCapabilities {
240                    provider: snapshot.provider,
241                    name: snapshot.model,
242                    supports_tools: snapshot.supports_tools,
243                    supports_vision: snapshot.supports_vision,
244                    reasoning: snapshot.reasoning,
245                    max_context_tokens: snapshot.max_context_tokens,
246                }),
247            )
248        },
249        Err(err) => (None, Some(err.to_string()), None),
250    };
251
252    let ollama_models = if is_ollama_installed() {
253        list_ollama_models(config).await
254    } else {
255        Vec::new()
256    };
257    let ollama = if !is_ollama_installed() {
258        DoctorCheck {
259            status: "warning",
260            message: "Ollama is not installed; remote providers can still work if configured."
261                .to_string(),
262        }
263    } else if ollama_models.is_empty() {
264        DoctorCheck {
265            status: "warning",
266            message: "Ollama is installed but no local/cloud models were listed.".to_string(),
267        }
268    } else {
269        DoctorCheck {
270            status: "ok",
271            message: format!("Ollama reachable with {} models.", ollama_models.len()),
272        }
273    };
274
275    let remote_providers = configured_remote_providers(config);
276    let instruction_paths = crate::app::instructions::find_instruction_files(cwd);
277    let project_instructions = if instruction_paths.is_empty() {
278        DoctorCheck {
279            status: "info",
280            message: "No AGENTS.md or MERMAID.md found.".to_string(),
281        }
282    } else if let Some(loaded) = crate::app::instructions::load_from_paths(&instruction_paths) {
283        DoctorCheck {
284            status: "ok",
285            message: format!(
286                "{} bytes loaded from {} source(s){}.",
287                loaded.byte_len,
288                loaded.sources.len(),
289                if loaded.truncated { " (truncated)" } else { "" }
290            ),
291        }
292    } else {
293        DoctorCheck {
294            status: "warning",
295            message: "Instruction files were found but could not be loaded.".to_string(),
296        }
297    };
298
299    let daemon = match RuntimeClient::daemon().health() {
300        Ok(read) => DoctorCheck {
301            status: "ok",
302            message: format!("daemon attached; database {}", read.value.database),
303        },
304        Err(err) => DoctorCheck {
305            status: "info",
306            message: format!("daemon not attached; CLI will use local runtime store ({err})"),
307        },
308    };
309    let local_store = match RuntimeClient::local().health() {
310        Ok(read) => DoctorCheck {
311            status: "ok",
312            message: format!("local runtime store ready at {}", read.value.database),
313        },
314        Err(err) => DoctorCheck {
315            status: "warning",
316            message: format!("local runtime store unavailable: {err}"),
317        },
318    };
319
320    let mut tools = vec![
321        "read/edit/write files".to_string(),
322        "run shell commands".to_string(),
323        "create checkpoints before risky mutations".to_string(),
324    ];
325    if std::env::var("OLLAMA_API_KEY").is_ok() {
326        tools.push("web search/fetch tools gated by OLLAMA_API_KEY".to_string());
327    }
328    if !config.mcp_servers.is_empty() {
329        tools.push(format!(
330            "{} configured MCP server(s)",
331            config.mcp_servers.len()
332        ));
333    }
334
335    let mut next_steps = Vec::new();
336    if active_model.is_none() {
337        next_steps.push(
338            "Pick a model with `mermaid --model <provider/model>` or run `mermaid list`."
339                .to_string(),
340        );
341    }
342    if remote_providers.is_empty() && ollama_models.is_empty() {
343        next_steps.push(
344            "Install or start Ollama, pull a model, or set a remote provider API key.".to_string(),
345        );
346    }
347    if instruction_paths.is_empty() {
348        next_steps.push("Optional: add MERMAID.md or AGENTS.md with project-specific run commands and conventions.".to_string());
349    }
350    if next_steps.is_empty() {
351        next_steps.push(
352            "Start Mermaid with `mermaid` or run one prompt with `mermaid run \"...\"`."
353                .to_string(),
354        );
355    }
356
357    let ok = active_model.is_some()
358        && local_store.status != "warning"
359        && (ollama.status == "ok" || !remote_providers.is_empty());
360    let report = DoctorReport {
361        ok,
362        cwd: cwd.display().to_string(),
363        active_model,
364        model_error,
365        model_capabilities,
366        safety_mode: safety_mode_name(config.safety.mode).to_string(),
367        checkpoint_on_mutation: config.safety.checkpoint_on_mutation,
368        prompt_customized: config.prompt.is_customized(),
369        ollama,
370        remote_providers,
371        project_instructions,
372        tools,
373        runtime: DoctorRuntime {
374            daemon,
375            local_store,
376        },
377        next_steps,
378    };
379    print_doctor_report(&report, format)
380}
381
382fn print_doctor_report(report: &DoctorReport, format: OutputFormat) -> Result<()> {
383    match format {
384        OutputFormat::Json => println!("{}", serde_json::to_string_pretty(report)?),
385        OutputFormat::Markdown => {
386            println!("# Mermaid Doctor\n");
387            print_doctor_text(report);
388        },
389        OutputFormat::Text => print_doctor_text(report),
390    }
391    Ok(())
392}
393
394fn print_doctor_text(report: &DoctorReport) {
395    println!(
396        "Mermaid Doctor: {}",
397        if report.ok {
398            "ready"
399        } else {
400            "needs attention"
401        }
402    );
403    println!("Project: {}", report.cwd);
404    match (&report.active_model, &report.model_error) {
405        (Some(model), _) => println!("  [OK] Active model: {model}"),
406        (None, Some(error)) => println!("  [WARNING] Active model: {error}"),
407        _ => println!("  [WARNING] Active model: unresolved"),
408    }
409    if let Some(caps) = &report.model_capabilities {
410        println!(
411            "       provider={} tools={} vision={} reasoning={} context={}",
412            caps.provider,
413            caps.supports_tools,
414            caps.supports_vision,
415            caps.reasoning,
416            caps.max_context_tokens
417                .map(|n| n.to_string())
418                .unwrap_or_else(|| "unknown".to_string())
419        );
420    }
421    println!(
422        "  [{}] Ollama: {}",
423        label(report.ollama.status),
424        report.ollama.message
425    );
426    println!(
427        "  [INFO] Remote providers: {}",
428        if report.remote_providers.is_empty() {
429            "none configured".to_string()
430        } else {
431            report.remote_providers.join(", ")
432        }
433    );
434    println!(
435        "  [{}] Project instructions: {}",
436        label(report.project_instructions.status),
437        report.project_instructions.message
438    );
439    println!(
440        "  [INFO] Safety: mode={}, checkpoint_on_mutation={}",
441        report.safety_mode, report.checkpoint_on_mutation
442    );
443    println!(
444        "  [INFO] Prompt customization: {}",
445        if report.prompt_customized {
446            "active"
447        } else {
448            "default"
449        }
450    );
451    println!(
452        "  [{}] Runtime daemon: {}",
453        label(report.runtime.daemon.status),
454        report.runtime.daemon.message
455    );
456    println!(
457        "  [{}] Runtime store: {}",
458        label(report.runtime.local_store.status),
459        report.runtime.local_store.message
460    );
461    println!("  [OK] Tool surface:");
462    for tool in &report.tools {
463        println!("       - {tool}");
464    }
465    println!("\nNext steps:");
466    for step in &report.next_steps {
467        println!("  - {step}");
468    }
469}
470
471#[derive(Debug, serde::Serialize)]
472struct SelfTestReport {
473    ok: bool,
474    workspace: String,
475    checks: Vec<String>,
476    compact_smoke: QaCompactSmokeReport,
477    runtime_store: DoctorCheck,
478    kept_workspace: bool,
479}
480
481fn run_self_test(config: &Config, format: OutputFormat, keep_workspace: bool) -> Result<()> {
482    let workspace = std::env::temp_dir().join(format!("mermaid-self-test-{}", fresh_qa_id()));
483    std::fs::create_dir_all(&workspace)
484        .with_context(|| format!("failed to create {}", workspace.display()))?;
485
486    let compact_smoke = match run_qa_compact_smoke(config, &workspace, 6) {
487        Ok(report) => report,
488        Err(err) => QaCompactSmokeReport::failed(&workspace, 6, err.to_string()),
489    };
490    let runtime_store = match RuntimeClient::local().health() {
491        Ok(read) => DoctorCheck {
492            status: "ok",
493            message: format!("local runtime store ready at {}", read.value.database),
494        },
495        Err(err) => DoctorCheck {
496            status: "warning",
497            message: err.to_string(),
498        },
499    };
500
501    let checks = vec![
502        "compact smoke exercises reducer compaction path".to_string(),
503        "compact smoke persists conversation and archive artifacts".to_string(),
504        "local runtime store opens without daemon".to_string(),
505    ];
506    let ok = compact_smoke.ok && runtime_store.status == "ok";
507    let report = SelfTestReport {
508        ok,
509        workspace: workspace.display().to_string(),
510        checks,
511        compact_smoke,
512        runtime_store,
513        kept_workspace: keep_workspace,
514    };
515
516    print_self_test_report(&report, format)?;
517    if !keep_workspace {
518        let _ = std::fs::remove_dir_all(&workspace);
519    }
520    anyhow::ensure!(report.ok, "mermaid self-test failed");
521    Ok(())
522}
523
524fn print_self_test_report(report: &SelfTestReport, format: OutputFormat) -> Result<()> {
525    match format {
526        OutputFormat::Json => println!("{}", serde_json::to_string_pretty(report)?),
527        OutputFormat::Markdown => {
528            println!("# Mermaid Self-Test\n");
529            print_self_test_text(report);
530        },
531        OutputFormat::Text => print_self_test_text(report),
532    }
533    Ok(())
534}
535
536fn print_self_test_text(report: &SelfTestReport) {
537    println!(
538        "Mermaid self-test: {}",
539        if report.ok { "ok" } else { "failed" }
540    );
541    println!("workspace: {}", report.workspace);
542    println!(
543        "compact smoke: {}",
544        if report.compact_smoke.ok {
545            "ok"
546        } else {
547            "failed"
548        }
549    );
550    println!("runtime store: {}", report.runtime_store.message);
551    println!("checks:");
552    for check in &report.checks {
553        println!("  - {check}");
554    }
555    if !report.ok
556        && let Some(failure) = &report.compact_smoke.failure
557    {
558        println!("failure: {failure}");
559    }
560}
561
562fn label(status: &str) -> &'static str {
563    match status {
564        "ok" => "OK",
565        "warning" => "WARNING",
566        "error" => "ERROR",
567        _ => "INFO",
568    }
569}
570
571fn safety_mode_name(mode: crate::runtime::SafetyMode) -> &'static str {
572    mode.as_str()
573}
574
575fn configured_remote_providers(config: &Config) -> Vec<String> {
576    let mut names = Vec::new();
577    for profile in PROVIDER_REGISTRY {
578        let env = config
579            .providers
580            .get(profile.name)
581            .and_then(|c| c.api_key_env.as_deref())
582            .unwrap_or(profile.api_key_env);
583        if resolve_api_key(env, None).is_some() {
584            names.push(profile.name.to_string());
585        }
586    }
587    for (name, provider) in &config.providers {
588        if names.iter().any(|existing| existing == name) {
589            continue;
590        }
591        if let Some(env) = provider.api_key_env.as_deref()
592            && resolve_api_key(env, None).is_some()
593        {
594            names.push(name.clone());
595        }
596    }
597    names.sort();
598    names
599}
600
601#[derive(Debug, serde::Serialize)]
602struct QaCompactSmokeReport {
603    ok: bool,
604    turns: usize,
605    archived_messages: usize,
606    preserved_messages: usize,
607    replacement_messages: usize,
608    conversation_path: Option<String>,
609    archive_path: Option<String>,
610    checks: Vec<String>,
611    failure: Option<String>,
612}
613
614impl QaCompactSmokeReport {
615    fn failed(cwd: &Path, turns: usize, failure: String) -> Self {
616        Self {
617            ok: false,
618            turns,
619            archived_messages: 0,
620            preserved_messages: 0,
621            replacement_messages: 0,
622            conversation_path: Some(
623                cwd.join(".mermaid")
624                    .join("conversations")
625                    .display()
626                    .to_string(),
627            ),
628            archive_path: None,
629            checks: Vec::new(),
630            failure: Some(failure),
631        }
632    }
633}
634
635fn run_qa_compact_smoke(
636    config: &Config,
637    cwd: &Path,
638    requested_turns: usize,
639) -> Result<QaCompactSmokeReport> {
640    let turns = requested_turns.max(3);
641    let mut state = State::new(config.clone(), cwd.to_path_buf(), qa_model_id(config));
642    for message in synthetic_compaction_messages(turns) {
643        state.session.append(message);
644    }
645
646    let (state_after_slash, compact_cmds) = update(
647        state,
648        Msg::Slash(SlashCmd::Compact(Some("qa compact smoke".to_string()))),
649    );
650    let turn = state_after_slash
651        .turn
652        .id()
653        .context("manual compaction did not enter a compaction turn")?;
654    let request = compact_cmds
655        .iter()
656        .find_map(|cmd| match cmd {
657            Cmd::CompactConversation { request, .. } => Some(request.clone()),
658            _ => None,
659        })
660        .context("manual compaction did not emit a CompactConversation command")?;
661
662    let before_snapshot = estimate_context_usage_for_request(&request.chat, Some(100_000));
663    let prepared = prepare_compaction(&request, Some(100_000))
664        .map_err(|reason| anyhow::anyhow!("prepare_compaction skipped: {reason}"))?;
665    anyhow::ensure!(
666        !prepared.archived_messages.is_empty(),
667        "compaction archived no messages"
668    );
669    anyhow::ensure!(
670        !prepared.preserved_messages.is_empty(),
671        "compaction preserved no messages"
672    );
673
674    let summary = deterministic_compaction_summary(&prepared, turns);
675    let mut record = CompactionRecord {
676        id: format!("qa_compact_{}", fresh_qa_id()),
677        trigger: CompactionTrigger::Manual,
678        created_at: chrono::Local::now(),
679        before_tokens: before_snapshot.used_tokens,
680        after_tokens: 0,
681        archived_message_count: prepared.archived_messages.len(),
682        preserved_message_count: prepared.preserved_messages.len(),
683        summary_tokens: summary.len().div_ceil(4),
684        duration_secs: 0.0,
685        verified: true,
686        verification_error: None,
687        focus: Some("qa compact smoke".to_string()),
688        archive_path: None,
689    };
690    let mut replacement = build_replacement_messages(&summary, &prepared, &record);
691    let mut after_chat: ChatRequest = request.chat.clone();
692    after_chat.messages = replacement.clone();
693    let mut after_snapshot = estimate_context_usage_for_request(&after_chat, Some(100_000));
694    record.after_tokens = after_snapshot.used_tokens;
695    replacement = build_replacement_messages(&summary, &prepared, &record);
696    after_chat.messages = replacement.clone();
697    after_snapshot = estimate_context_usage_for_request(&after_chat, Some(100_000));
698
699    let result = CompactionResult {
700        record,
701        replacement_messages: replacement,
702        archived_messages: prepared.archived_messages,
703        before_snapshot,
704        after_snapshot,
705        usage: None,
706    };
707    let (final_state, save_cmds) =
708        update(state_after_slash, Msg::CompactionFinished { turn, result });
709
710    let manager = ConversationManager::new(cwd)?;
711    let mut conversation_path = None;
712    let mut archive_path = None;
713    for cmd in save_cmds {
714        match cmd {
715            Cmd::SaveConversation(conversation) => {
716                manager.save_conversation(&conversation)?;
717                conversation_path = Some(
718                    manager
719                        .conversations_dir()
720                        .join(format!("{}.json", conversation.id))
721                        .display()
722                        .to_string(),
723                );
724            },
725            Cmd::SaveCompactionArchive {
726                archive,
727                conversation,
728                ..
729            } => {
730                // Archive first, then the stripped conversation (same order
731                // as the live effect path), with `?` so a failed archive
732                // aborts before the conversation is overwritten.
733                archive_path = Some(
734                    manager
735                        .save_compaction_archive(&archive)?
736                        .display()
737                        .to_string(),
738                );
739                manager.save_conversation(&conversation)?;
740                conversation_path = Some(
741                    manager
742                        .conversations_dir()
743                        .join(format!("{}.json", conversation.id))
744                        .display()
745                        .to_string(),
746                );
747            },
748            _ => {},
749        }
750    }
751
752    let conversation_path = conversation_path.context("compaction did not save conversation")?;
753    let archive_path = archive_path.context("compaction did not save archive")?;
754    let messages = final_state.session.messages();
755    let compactions = &final_state.session.conversation.compactions;
756
757    let mut checks = Vec::new();
758    anyhow::ensure!(
759        !compactions.is_empty(),
760        "conversation did not record compaction metadata"
761    );
762    checks.push("conversation records compaction metadata".to_string());
763    anyhow::ensure!(
764        messages
765            .first()
766            .is_some_and(|msg| msg.kind == crate::models::ChatMessageKind::ContextCheckpoint),
767        "replacement does not start with a context checkpoint"
768    );
769    checks.push("replacement starts with context checkpoint".to_string());
770    anyhow::ensure!(
771        std::path::Path::new(&conversation_path).exists(),
772        "conversation file missing after save"
773    );
774    checks.push("conversation file saved".to_string());
775    anyhow::ensure!(
776        std::path::Path::new(&archive_path).exists(),
777        "compaction archive file missing after save"
778    );
779    checks.push("archive file saved".to_string());
780    anyhow::ensure!(
781        compactions[0].archived_message_count > 0 && compactions[0].preserved_message_count > 0,
782        "compaction did not archive and preserve messages"
783    );
784    checks.push("archived and preserved message counts are non-zero".to_string());
785
786    Ok(QaCompactSmokeReport {
787        ok: true,
788        turns,
789        archived_messages: compactions[0].archived_message_count,
790        preserved_messages: compactions[0].preserved_message_count,
791        replacement_messages: messages.len(),
792        conversation_path: Some(conversation_path),
793        archive_path: Some(archive_path),
794        checks,
795        failure: None,
796    })
797}
798
799fn print_qa_compact_report(report: &QaCompactSmokeReport, format: OutputFormat) -> Result<()> {
800    match format {
801        OutputFormat::Json => {
802            println!("{}", serde_json::to_string_pretty(report)?);
803        },
804        OutputFormat::Text => {
805            println!(
806                "qa compact smoke: {}",
807                if report.ok { "ok" } else { "failed" }
808            );
809            println!("turns: {}", report.turns);
810            println!("archived messages: {}", report.archived_messages);
811            println!("preserved messages: {}", report.preserved_messages);
812            println!("replacement messages: {}", report.replacement_messages);
813            if let Some(path) = &report.conversation_path {
814                println!("conversation: {path}");
815            }
816            if let Some(path) = &report.archive_path {
817                println!("archive: {path}");
818            }
819            if let Some(failure) = &report.failure {
820                println!("failure: {failure}");
821            }
822        },
823        OutputFormat::Markdown => {
824            println!(
825                "# QA Compact Smoke\n\n- Status: {}\n- Turns: {}\n- Archived messages: {}\n- Preserved messages: {}\n- Replacement messages: {}",
826                if report.ok { "ok" } else { "failed" },
827                report.turns,
828                report.archived_messages,
829                report.preserved_messages,
830                report.replacement_messages
831            );
832            if let Some(path) = &report.conversation_path {
833                println!("- Conversation: `{path}`");
834            }
835            if let Some(path) = &report.archive_path {
836                println!("- Archive: `{path}`");
837            }
838            if let Some(failure) = &report.failure {
839                println!("\nFailure: `{failure}`");
840            }
841        },
842    }
843    Ok(())
844}
845
846fn qa_model_id(config: &Config) -> String {
847    if let Some(model) = config
848        .last_used_model
849        .as_ref()
850        .filter(|value| !value.is_empty())
851    {
852        return model.clone();
853    }
854    if !config.default_model.name.is_empty() {
855        if config.default_model.provider.is_empty() {
856            return config.default_model.name.clone();
857        }
858        return format!(
859            "{}/{}",
860            config.default_model.provider, config.default_model.name
861        );
862    }
863    "qa/deterministic".to_string()
864}
865
866fn synthetic_compaction_messages(turns: usize) -> Vec<ChatMessage> {
867    let mut messages = Vec::with_capacity(turns.saturating_mul(2));
868    for idx in 1..=turns {
869        messages.push(ChatMessage::user(format!(
870            "User turn {idx}: investigate Mermaid compaction behavior in src/domain/compaction.rs and keep exact file paths in the summary."
871        )));
872        messages.push(ChatMessage::assistant(format!(
873            "Assistant turn {idx}: inspected src/domain/compaction.rs, tests/reducer_flows.rs, and scripts/qa_mermaid.py; noted command `cargo test --all-targets` result placeholder {idx}."
874        )));
875    }
876    messages
877}
878
879fn deterministic_compaction_summary(
880    prepared: &crate::domain::PreparedCompaction,
881    turns: usize,
882) -> String {
883    format!(
884        "## Goal\n- Verify Mermaid can compact a multi-turn conversation through the reducer path.\n\n## User Preferences And Constraints\n- Headless QA must not require a human to open the TUI.\n\n## Project State\n- Synthetic QA conversation seeded with {turns} user/assistant turns.\n\n## Completed Work\n- Prepared compaction archived {} messages and preserved {} messages.\n\n## Current Work\n- Running deterministic compact smoke from the hidden QA command.\n\n## Key Decisions\n- Use deterministic summary text so fast QA does not call a real model.\n\n## Critical Files And Symbols\n- src/domain/compaction.rs: compaction preparation and replacement shape.\n- src/domain/reducer.rs: manual compaction completion handling.\n- scripts/qa_mermaid.py: headless QA harness.\n\n## Commands Tests And Results\n- mermaid qa compact-smoke --format json: running inside this smoke.\n\n## Open Questions Or Risks\n- Full TUI automation remains intentionally deferred.\n\n## Next Steps\n- Keep using the real-model QA tier for end-to-end dogfood checks.",
885        prepared.archived_messages.len(),
886        prepared.preserved_messages.len()
887    )
888}
889
890fn fresh_qa_id() -> u128 {
891    std::time::SystemTime::now()
892        .duration_since(std::time::UNIX_EPOCH)
893        .map(|duration| duration.as_nanos())
894        .unwrap_or_default()
895}
896
897fn show_tasks(limit: usize) -> Result<()> {
898    let read = RuntimeClient::auto().list_tasks(limit)?;
899    let mut tasks = read.value;
900    tasks.truncate(limit);
901    println!("Mermaid runtime tasks");
902    println!("Source: {}", read.source.as_str());
903    println!();
904    if tasks.is_empty() {
905        println!("No tasks recorded yet.");
906        return Ok(());
907    }
908    for task in tasks {
909        println!(
910            "{}  [{}] {}  {}  {}",
911            task.id, task.status, task.priority, task.updated_at, task.title
912        );
913        println!("    project: {}", task.project_path);
914        println!("    model: {}", task.model_id);
915    }
916    Ok(())
917}
918
919fn show_task(id: &str) -> Result<()> {
920    let detail = RuntimeClient::auto().task_detail(id)?.value;
921    print_task_detail(&detail.task);
922    let events = detail.events;
923    if !events.is_empty() {
924        println!();
925        println!("Timeline:");
926        for event in events {
927            println!("  {}  {}  {}", event.created_at, event.kind, event.message);
928        }
929    }
930    Ok(())
931}
932
933fn print_task_detail(task: &TaskRecord) {
934    println!("Task: {}", task.id);
935    println!("Title: {}", task.title);
936    println!("Status: {}", task.status);
937    println!("Priority: {}", task.priority);
938    println!("Project: {}", task.project_path);
939    println!("Model: {}", task.model_id);
940    if let Some(conversation_id) = &task.conversation_id {
941        println!("Conversation: {}", conversation_id);
942    }
943    println!("Created: {}", task.created_at);
944    println!("Updated: {}", task.updated_at);
945    if let Some(report) = &task.final_report {
946        println!();
947        println!("Final report:");
948        println!("{}", report);
949    }
950}
951
952fn show_processes(limit: usize) -> Result<()> {
953    let read = RuntimeClient::auto().list_processes(limit)?;
954    let mut processes = read.value;
955    processes.truncate(limit);
956    println!("Mermaid runtime processes");
957    println!("Source: {}", read.source.as_str());
958    println!();
959    if processes.is_empty() {
960        println!("No processes recorded yet.");
961        return Ok(());
962    }
963    for process in processes {
964        println!(
965            "{}  pid={}  status={}  {}",
966            process.id,
967            process.pid,
968            process.status.as_str(),
969            process.command
970        );
971        if let Some(task_id) = process.task_id {
972            println!("    task: {}", task_id);
973        }
974        if let Some(cwd) = process.cwd {
975            println!("    cwd: {}", cwd);
976        }
977        if let Some(log_path) = process.log_path {
978            println!("    log: {}", log_path);
979        }
980        if let Some(url) = process.detected_url {
981            println!("    url: {}", url);
982        }
983    }
984    Ok(())
985}
986
987async fn show_models(config: &Config) -> Result<()> {
988    list_models(config).await?;
989    probe_configured_provider_models(config).await?;
990    let store = RuntimeStore::open_default()?;
991    let probes = store.provider_probes().list(None, None)?;
992    if !probes.is_empty() {
993        println!("\nCached capability probes:");
994        for probe in probes {
995            println!(
996                "  - {}/{} {}={} ({})",
997                probe.provider,
998                probe.model_id,
999                probe.capability_key,
1000                probe.capability_value,
1001                probe.confidence
1002            );
1003        }
1004    }
1005    Ok(())
1006}
1007
1008async fn show_model_info(model: &str, config: &Config) -> Result<()> {
1009    let snapshot = crate::domain::ProviderCapabilitySnapshot::from_model_id(model);
1010    let store = RuntimeStore::open_default()?;
1011    let provider = snapshot.provider.clone();
1012
1013    // The static snapshot has no context window for Ollama. Probe `/api/show` for
1014    // the model's real one so this reports a real number, not "unknown".
1015    let mut context_tokens = snapshot.max_context_tokens;
1016    let mut context_confidence = "static";
1017    if provider == "ollama" {
1018        let backend = std::sync::Arc::new(crate::providers::factory::ollama_backend_config(config));
1019        if let Ok(adapter) =
1020            crate::models::adapters::ollama::OllamaAdapter::new(&snapshot.model, backend).await
1021            && let Some(info) = adapter.show_model_info().await
1022            && let Some(ctx) = info.context_length
1023        {
1024            context_tokens = Some(ctx);
1025            context_confidence = "probed";
1026        }
1027    }
1028
1029    for (key, value) in [
1030        ("supports_tools", snapshot.supports_tools.to_string()),
1031        ("supports_vision", snapshot.supports_vision.to_string()),
1032        ("reasoning", snapshot.reasoning.clone()),
1033    ] {
1034        let _ = store.provider_probes().upsert(NewProviderProbe {
1035            provider: provider.clone(),
1036            model_id: snapshot.model.clone(),
1037            capability_key: key.to_string(),
1038            capability_value: value,
1039            confidence: "static".to_string(),
1040            error: None,
1041        });
1042    }
1043    // Context window separately — probed (Ollama) or static.
1044    let _ = store.provider_probes().upsert(NewProviderProbe {
1045        provider: provider.clone(),
1046        model_id: snapshot.model.clone(),
1047        capability_key: "max_context_tokens".to_string(),
1048        capability_value: context_tokens
1049            .map(|n| n.to_string())
1050            .unwrap_or_else(|| "unknown".to_string()),
1051        confidence: context_confidence.to_string(),
1052        error: None,
1053    });
1054    println!("Model: {}", model);
1055    println!("Provider: {}", snapshot.provider);
1056    println!("Name: {}", snapshot.model);
1057    println!("Supports tools: {}", snapshot.supports_tools);
1058    println!("Supports vision: {}", snapshot.supports_vision);
1059    println!("Reasoning: {}", snapshot.reasoning);
1060    println!(
1061        "Context: {}",
1062        context_tokens
1063            .map(|n| n.to_string())
1064            .unwrap_or_else(|| "unknown".to_string())
1065    );
1066    if let Some(profile) = lookup_provider(&snapshot.provider) {
1067        for (key, value) in [
1068            (
1069                "max_output_tokens_param",
1070                format!("{:?}", profile.max_tokens_param),
1071            ),
1072            (
1073                "parallel_tool_calls",
1074                (!profile
1075                    .disable_parallel_tool_calls_for
1076                    .iter()
1077                    .any(|disabled| *disabled == snapshot.model))
1078                .to_string(),
1079            ),
1080            (
1081                "reasoning_parameter_shape",
1082                format!("{:?}", profile.reasoning_strategy),
1083            ),
1084            (
1085                "streaming_usage_available",
1086                "provider_dependent".to_string(),
1087            ),
1088            ("token_usage_field_shape", "openai_compatible".to_string()),
1089        ] {
1090            let _ = store.provider_probes().upsert(NewProviderProbe {
1091                provider: provider.clone(),
1092                model_id: snapshot.model.clone(),
1093                capability_key: key.to_string(),
1094                capability_value: value,
1095                confidence: "static".to_string(),
1096                error: None,
1097            });
1098        }
1099        println!("Token budget field: {:?}", profile.max_tokens_param);
1100        println!(
1101            "Single-tool-call models: {}",
1102            if profile.disable_parallel_tool_calls_for.is_empty() {
1103                "(none)".to_string()
1104            } else {
1105                profile.disable_parallel_tool_calls_for.join(", ")
1106            }
1107        );
1108    }
1109    Ok(())
1110}
1111
1112async fn probe_configured_provider_models(config: &Config) -> Result<()> {
1113    let client = reqwest::Client::builder()
1114        .timeout(std::time::Duration::from_secs(5))
1115        .build()?;
1116    for profile in PROVIDER_REGISTRY {
1117        let user_cfg = config.providers.get(profile.name);
1118        let env = user_cfg
1119            .and_then(|c| c.api_key_env.as_deref())
1120            .unwrap_or(profile.api_key_env);
1121        let Some(api_key) = resolve_api_key(env, None) else {
1122            continue;
1123        };
1124        let base_url = user_cfg
1125            .and_then(|c| c.base_url.clone())
1126            .unwrap_or_else(|| profile.base_url.to_string());
1127        let url = format!("{}/models", base_url.trim_end_matches('/'));
1128        let mut request = client.get(&url).bearer_auth(api_key);
1129        for (name, value) in profile.extra_headers {
1130            request = request.header(*name, *value);
1131        }
1132        if let Some(user_cfg) = user_cfg {
1133            for (name, value) in &user_cfg.extra_headers {
1134                request = request.header(name, value);
1135            }
1136        }
1137
1138        let result = request.send().await;
1139        match result {
1140            Ok(response) if response.status().is_success() => {
1141                let status = response.status();
1142                let body: serde_json::Value = response.json().await.unwrap_or_default();
1143                let ids = body
1144                    .get("data")
1145                    .and_then(|v| v.as_array())
1146                    .map(|items| {
1147                        items
1148                            .iter()
1149                            .filter_map(|item| item.get("id").and_then(|id| id.as_str()))
1150                            .map(str::to_string)
1151                            .collect::<Vec<_>>()
1152                    })
1153                    .unwrap_or_default();
1154                record_provider_probe(
1155                    profile.name,
1156                    "*",
1157                    "models_availability",
1158                    &format!("available:{}:{}", status.as_u16(), ids.len()),
1159                    "probed",
1160                    None,
1161                );
1162                for model_id in ids.into_iter().take(200) {
1163                    record_provider_probe(
1164                        profile.name,
1165                        &model_id,
1166                        "model_listed",
1167                        "true",
1168                        "listed",
1169                        None,
1170                    );
1171                }
1172            },
1173            Ok(response) => {
1174                record_provider_probe(
1175                    profile.name,
1176                    "*",
1177                    "models_availability",
1178                    "failed",
1179                    "failed",
1180                    Some(format!("HTTP {}", response.status().as_u16())),
1181                );
1182            },
1183            Err(error) => {
1184                record_provider_probe(
1185                    profile.name,
1186                    "*",
1187                    "models_availability",
1188                    "failed",
1189                    "failed",
1190                    Some(error.to_string()),
1191                );
1192            },
1193        }
1194    }
1195    Ok(())
1196}
1197
1198fn record_provider_probe(
1199    provider: &str,
1200    model_id: &str,
1201    key: &str,
1202    value: &str,
1203    confidence: &str,
1204    error: Option<String>,
1205) {
1206    if let Ok(store) = RuntimeStore::open_default() {
1207        let _ = store.provider_probes().upsert(NewProviderProbe {
1208            provider: provider.to_string(),
1209            model_id: model_id.to_string(),
1210            capability_key: key.to_string(),
1211            capability_value: value.to_string(),
1212            confidence: confidence.to_string(),
1213            error,
1214        });
1215    }
1216}
1217
1218fn show_approvals() -> Result<()> {
1219    let approvals = RuntimeClient::auto().list_approvals()?.value;
1220    if approvals.is_empty() {
1221        println!("No pending approvals.");
1222        return Ok(());
1223    }
1224    for approval in approvals {
1225        println!(
1226            "{} [{} -> {}] {}",
1227            approval.id,
1228            approval.risk_classification,
1229            approval.policy_decision,
1230            approval.proposed_action
1231        );
1232        if let Some(args) = approval.args_summary {
1233            println!("    args: {}", args);
1234        }
1235        if let Some(checkpoint_id) = approval.checkpoint_id {
1236            println!("    checkpoint: {}", checkpoint_id);
1237        }
1238        if approval.pending_action_json.is_some() {
1239            println!("    pending action: recorded");
1240        }
1241    }
1242    Ok(())
1243}
1244
1245fn approve(id: &str) -> Result<()> {
1246    let result = RuntimeClient::auto().approve(id)?;
1247    println!("Approved {}", id);
1248    if result.replayed {
1249        println!("{}", result.summary);
1250    }
1251    Ok(())
1252}
1253
1254fn deny(id: &str) -> Result<()> {
1255    let _ = RuntimeClient::auto().deny(id)?;
1256    println!("Denied {}", id);
1257    Ok(())
1258}
1259
1260fn show_tool_runs(limit: usize) -> Result<()> {
1261    let mut runs = RuntimeClient::auto().list_tool_runs(limit)?.value;
1262    runs.truncate(limit);
1263    if runs.is_empty() {
1264        println!("No tool runs recorded yet.");
1265        return Ok(());
1266    }
1267    for run in runs {
1268        println!(
1269            "{} [{}] {} started {}",
1270            run.id, run.status, run.tool_name, run.started_at
1271        );
1272        if let Some(turn_id) = run.turn_id {
1273            println!("    turn: {}", turn_id);
1274        }
1275        if let Some(call_id) = run.call_id {
1276            println!("    call: {}", call_id);
1277        }
1278        if let Some(finished_at) = run.finished_at {
1279            println!("    finished: {}", finished_at);
1280        }
1281    }
1282    Ok(())
1283}
1284
1285fn show_checkpoints(limit: usize) -> Result<()> {
1286    let mut checkpoints = RuntimeClient::auto().list_checkpoints(limit)?.value;
1287    checkpoints.truncate(limit);
1288    if checkpoints.is_empty() {
1289        println!("No checkpoints recorded yet.");
1290        return Ok(());
1291    }
1292    for checkpoint in checkpoints {
1293        println!(
1294            "{}  {}  {}",
1295            checkpoint.id, checkpoint.created_at, checkpoint.project_path
1296        );
1297        println!("    snapshot: {}", checkpoint.snapshot_path);
1298        println!("    files: {}", checkpoint.changed_files_json);
1299        if let Some(approval_id) = checkpoint.approval_id {
1300            println!("    approval: {}", approval_id);
1301        }
1302    }
1303    Ok(())
1304}
1305
1306fn restore_checkpoint(id: &str, force: bool) -> Result<()> {
1307    // Restoring overwrites the working tree from the checkpoint. Confirm first
1308    // (default NO); `--force` is the scripted-use bypass, and a non-interactive
1309    // session without it refuses rather than clobbering the tree unprompted (#113).
1310    if !crate::utils::confirm_or_refuse(
1311        &format!("Restore checkpoint {id}? This overwrites the current working tree."),
1312        force,
1313    )? {
1314        println!("Restore cancelled.");
1315        return Ok(());
1316    }
1317    let manifest = RuntimeClient::auto().restore_checkpoint(id)?.checkpoint;
1318    println!("Restored {} ({} files)", manifest.id, manifest.files.len());
1319    if let Some(repo) = manifest.shadow_git_repo {
1320        println!("Shadow repo: {}", repo);
1321    }
1322    if let Some(commit) = manifest.shadow_git_commit {
1323        println!("Shadow commit: {}", commit);
1324    }
1325    if let Some(action) = manifest.pending_action {
1326        println!("Pending action: {}", serde_json::to_string_pretty(&action)?);
1327    }
1328    Ok(())
1329}
1330
1331fn handle_plugin(command: &PluginCommand) -> Result<()> {
1332    match command {
1333        PluginCommand::Install { path } => {
1334            let preview = crate::runtime::plugin_capability_preview(path)?;
1335            print_plugin_capability_preview(&preview);
1336            let record = crate::runtime::install_plugin_from_path(path)?;
1337            println!(
1338                "Installed plugin {} ({}) — DISABLED.",
1339                record.name, record.id
1340            );
1341            println!(
1342                "Run `mermaid plugin enable {}` to activate it (this runs the plugin's hook code).",
1343                record.id
1344            );
1345        },
1346        PluginCommand::List => {
1347            let plugins = RuntimeClient::auto().list_plugins()?.value;
1348            if plugins.is_empty() {
1349                println!("No plugins installed.");
1350            } else {
1351                for plugin in plugins {
1352                    println!(
1353                        "{} [{}] {} ({})",
1354                        plugin.id,
1355                        if plugin.enabled {
1356                            "enabled"
1357                        } else {
1358                            "disabled"
1359                        },
1360                        plugin.name,
1361                        plugin.source
1362                    );
1363                }
1364            }
1365        },
1366        PluginCommand::Enable { id } => {
1367            // Surface what the plugin declares before activating its native code.
1368            let client = RuntimeClient::auto();
1369            if let Some(plugin) = client
1370                .list_plugins()?
1371                .value
1372                .into_iter()
1373                .find(|p| p.id == *id || p.name == *id)
1374                && let Ok(preview) =
1375                    crate::runtime::plugin_capability_preview(Path::new(&plugin.source))
1376            {
1377                print_plugin_capability_preview(&preview);
1378            }
1379            client.set_plugin_enabled(id, true)?;
1380            println!("Enabled plugin {} — its hooks will now run.", id);
1381        },
1382        PluginCommand::Disable { id } => {
1383            RuntimeClient::auto().set_plugin_enabled(id, false)?;
1384            println!("Disabled plugin {}", id);
1385        },
1386        PluginCommand::Audit { path } => {
1387            let manifest_path = if path.is_dir() {
1388                path.join("plugin.toml")
1389            } else {
1390                path.clone()
1391            };
1392            let raw = std::fs::read_to_string(&manifest_path)?;
1393            let manifest: crate::runtime::PluginManifest = toml::from_str(&raw)?;
1394            let root = manifest_path.parent().unwrap_or_else(|| Path::new("."));
1395            crate::runtime::validate_plugin_manifest(&manifest, root)?;
1396            let preview = crate::runtime::plugin_capability_preview(path)?;
1397            println!("Plugin manifest is valid: {}", manifest.name);
1398            print_plugin_capability_preview(&preview);
1399        },
1400    }
1401    Ok(())
1402}
1403
1404fn print_plugin_capability_preview(preview: &crate::runtime::PluginCapabilityPreview) {
1405    println!(
1406        "Capabilities declared by plugin {} (advisory, not sandbox-enforced):",
1407        preview.name
1408    );
1409    if preview.declared_capabilities.is_empty() && preview.capabilities_toml.is_none() {
1410        println!("  capabilities: (none declared)");
1411    } else {
1412        if !preview.declared_capabilities.is_empty() {
1413            println!("  declared: {}", preview.declared_capabilities.join(", "));
1414        }
1415        if let Some(value) = &preview.capabilities_toml {
1416            println!(
1417                "  capabilities.toml: {}",
1418                serde_json::to_string(value).unwrap_or_else(|_| "<unprintable>".to_string())
1419            );
1420        }
1421    }
1422    if !preview.hooks.is_empty() {
1423        println!("  hooks: {}", preview.hooks.join(", "));
1424    }
1425    if !preview.mcp.is_empty() {
1426        println!("  mcp: {}", preview.mcp.join(", "));
1427    }
1428    if !preview.bin.is_empty() {
1429        println!("  bin: {}", preview.bin.join(", "));
1430    }
1431}
1432
1433fn handle_pair(command: &PairCommand) -> Result<()> {
1434    let store = RuntimeStore::open_default()?;
1435    match command {
1436        PairCommand::Create { label, ttl_days } => {
1437            let ttl = ttl_days.unwrap_or(crate::runtime::DEFAULT_PAIRING_TTL_DAYS);
1438            let expires_at = crate::runtime::pairing_expiry_from_now(ttl);
1439            let (token, hash) = crate::runtime::generate_pairing_token()?;
1440            let record =
1441                store
1442                    .pairing_tokens()
1443                    .create(&hash, label.as_deref(), expires_at.as_deref())?;
1444            println!("Pairing token id: {}", record.id);
1445            println!("Pairing token: {}", token);
1446            println!(
1447                "Expires: {}",
1448                record.expires_at.as_deref().unwrap_or("never")
1449            );
1450            println!(
1451                "Use with daemon JSON by setting {}.",
1452                crate::runtime::daemon::DAEMON_TOKEN_ENV
1453            );
1454            println!("Store this now; Mermaid will not print it again.");
1455        },
1456        PairCommand::List => {
1457            let tokens = store.pairing_tokens().list()?;
1458            if tokens.is_empty() {
1459                println!("No pairing tokens.");
1460            } else {
1461                // Never print token_hash — only the non-secret metadata.
1462                for t in tokens {
1463                    println!(
1464                        "{} [{}] label={} created={} expires={} last_used={}",
1465                        t.id,
1466                        if t.enabled { "active" } else { "revoked" },
1467                        t.label.as_deref().unwrap_or("-"),
1468                        t.created_at,
1469                        t.expires_at.as_deref().unwrap_or("never"),
1470                        t.last_used_at.as_deref().unwrap_or("never"),
1471                    );
1472                }
1473            }
1474        },
1475        PairCommand::Revoke { id } => {
1476            if store.pairing_tokens().revoke(id)? {
1477                println!("Revoked pairing token {id}");
1478            } else {
1479                println!("No active pairing token with id {id}");
1480            }
1481        },
1482    }
1483    Ok(())
1484}
1485
1486fn show_logs(id: &str) -> Result<()> {
1487    let content = RuntimeClient::auto().process_log(id, None)?.content;
1488    print!("{}", content);
1489    Ok(())
1490}
1491
1492fn stop_process(id: &str) -> Result<()> {
1493    let process = RuntimeClient::auto().stop_process(id)?.item;
1494    println!("Stopped process {} (pid {})", id, process.pid);
1495    Ok(())
1496}
1497
1498fn restart_process(id: &str) -> Result<()> {
1499    let process = RuntimeClient::auto().restart_process(id)?.item;
1500    println!("Restarted process {} (pid {})", id, process.pid);
1501    Ok(())
1502}
1503
1504fn open_target(target: &str) -> Result<()> {
1505    if RuntimeClient::auto().open_process(target).is_err() {
1506        crate::utils::open_file(target);
1507    }
1508    Ok(())
1509}
1510
1511fn show_ports() -> Result<()> {
1512    print!("{}", RuntimeClient::auto().ports()?.ports);
1513    Ok(())
1514}
1515
1516/// List available models across all backends (honors user config).
1517pub async fn list_models(config: &Config) -> Result<()> {
1518    let ollama_models = list_ollama_models(config).await;
1519    if ollama_models.is_empty() {
1520        println!("No Ollama models installed locally.");
1521    } else {
1522        println!("Ollama models (local/cloud):");
1523        for name in &ollama_models {
1524            println!("  - ollama/{}", name);
1525        }
1526    }
1527
1528    println!("\nConfigured remote providers:");
1529    let mut any = false;
1530    for profile in PROVIDER_REGISTRY {
1531        let env = config
1532            .providers
1533            .get(profile.name)
1534            .and_then(|c| c.api_key_env.as_deref())
1535            .unwrap_or(profile.api_key_env);
1536        if resolve_api_key(env, None).is_some() {
1537            any = true;
1538            println!("  - {} (via ${})", profile.name, env);
1539        }
1540    }
1541    if !any {
1542        println!("  (none — set a provider API key env var to enable)");
1543    }
1544    println!("\nSwitch models in-session with /model <name>.");
1545    Ok(())
1546}
1547
1548/// Ask the local Ollama daemon for its list of models. Empty on
1549/// failure — the status widget separately shows whether Ollama is
1550/// reachable.
1551async fn list_ollama_models(config: &Config) -> Vec<String> {
1552    use crate::models::adapters::ollama::OllamaAdapter;
1553    let backend = BackendConfig {
1554        ollama_url: format!("{}:{}", config.ollama.host, config.ollama.port),
1555        timeout_secs: 5,
1556        max_idle_per_host: 2,
1557    };
1558    match OllamaAdapter::new("__list__", Arc::new(backend)).await {
1559        Ok(adapter) => adapter.list_models().await.unwrap_or_default(),
1560        Err(_) => Vec::new(),
1561    }
1562}
1563
1564/// Show version information
1565pub fn show_version() {
1566    println!("Mermaid v{}", env!("CARGO_PKG_VERSION"));
1567    println!("   An open-source, model-agnostic AI pair programmer");
1568}
1569
1570const RELEASE_LATEST_API: &str =
1571    "https://api.github.com/repos/noahsabaj/mermaid-cli/releases/latest";
1572const INSTALL_SH_URL: &str = "https://noahsabaj.github.io/mermaid-cli/install.sh";
1573const INSTALL_PS1_URL: &str = "https://noahsabaj.github.io/mermaid-cli/install.ps1";
1574
1575/// `mermaid update` — check GitHub Releases for a newer version and, unless
1576/// `--check`, re-run the platform install script to replace this binary in
1577/// place. The install script is the single source of truth for the
1578/// download + checksum + replace (incl. the running-exe rename on Windows), so
1579/// there's no archive-handling logic (or extra dependency) here.
1580async fn run_update(check: bool, force: bool) -> Result<()> {
1581    let current = env!("CARGO_PKG_VERSION");
1582    println!("Installed: v{current}");
1583
1584    let client = reqwest::Client::builder()
1585        .timeout(std::time::Duration::from_secs(15))
1586        .build()?;
1587    let resp = client
1588        .get(RELEASE_LATEST_API)
1589        .header("User-Agent", "mermaid-cli")
1590        .header("Accept", "application/vnd.github+json")
1591        .send()
1592        .await
1593        .map_err(|e| anyhow!("could not reach GitHub Releases: {e}"))?;
1594    if !resp.status().is_success() {
1595        bail!("GitHub Releases API returned HTTP {}", resp.status());
1596    }
1597    let release: serde_json::Value = resp.json().await?;
1598    let tag = release
1599        .get("tag_name")
1600        .and_then(|v| v.as_str())
1601        .ok_or_else(|| anyhow!("release response had no tag_name"))?;
1602    println!("Latest:    {tag}");
1603
1604    let up_to_date = version_at_least(current, tag.trim_start_matches('v'));
1605    if check {
1606        if up_to_date {
1607            println!("You're on the latest version.");
1608        } else {
1609            println!("Update available: v{current} -> {tag}. Run `mermaid update` to install it.");
1610        }
1611        return Ok(());
1612    }
1613    if up_to_date && !force {
1614        println!("Already up to date.");
1615        return Ok(());
1616    }
1617
1618    // Replace the binary in the directory it's running from, in place.
1619    let exe =
1620        std::env::current_exe().map_err(|e| anyhow!("could not locate current executable: {e}"))?;
1621    let install_dir = exe
1622        .parent()
1623        .ok_or_else(|| anyhow!("current executable has no parent directory"))?;
1624
1625    // Confirm before fetching + running the install script — it executes
1626    // downloaded shell/PowerShell and replaces the running binary. `--force` is
1627    // the scripted-use bypass; a non-interactive session without it refuses
1628    // rather than running fetched code unprompted (#110).
1629    let script_url = if cfg!(target_os = "windows") {
1630        INSTALL_PS1_URL
1631    } else {
1632        INSTALL_SH_URL
1633    };
1634    if !crate::utils::confirm_or_refuse(
1635        &format!(
1636            "About to download and run {script_url} to replace {}.",
1637            install_dir.display()
1638        ),
1639        force,
1640    )? {
1641        println!("Update cancelled.");
1642        return Ok(());
1643    }
1644
1645    println!("Updating {} …", install_dir.display());
1646    run_install_script(&client, install_dir).await?;
1647    println!("Updated. New version takes effect on the next run.");
1648    Ok(())
1649}
1650
1651/// Fetch the platform install script from the Pages site and run it, pointed at
1652/// `install_dir` so it updates in place without touching PATH.
1653async fn run_install_script(client: &reqwest::Client, install_dir: &Path) -> Result<()> {
1654    let windows = cfg!(target_os = "windows");
1655    let url = if windows {
1656        INSTALL_PS1_URL
1657    } else {
1658        INSTALL_SH_URL
1659    };
1660    let script = client
1661        .get(url)
1662        .header("User-Agent", "mermaid-cli")
1663        .send()
1664        .await
1665        .map_err(|e| anyhow!("could not fetch install script: {e}"))?
1666        .error_for_status()?
1667        .text()
1668        .await?;
1669
1670    let ext = if windows { "ps1" } else { "sh" };
1671    let script_path =
1672        std::env::temp_dir().join(format!("mermaid-update-{}.{ext}", std::process::id()));
1673    std::fs::write(&script_path, script)
1674        .map_err(|e| anyhow!("could not stage install script: {e}"))?;
1675
1676    let mut cmd = if windows {
1677        let mut c = tokio::process::Command::new("powershell");
1678        c.args(["-NoProfile", "-ExecutionPolicy", "Bypass", "-File"]);
1679        c.arg(&script_path);
1680        c
1681    } else {
1682        let mut c = tokio::process::Command::new("sh");
1683        c.arg(&script_path);
1684        c
1685    };
1686    cmd.env("MERMAID_INSTALL_DIR", install_dir)
1687        .env("MERMAID_NO_MODIFY_PATH", "1");
1688
1689    let status = cmd
1690        .status()
1691        .await
1692        .map_err(|e| anyhow!("could not run install script: {e}"))?;
1693    let _ = std::fs::remove_file(&script_path);
1694    if !status.success() {
1695        bail!("install script exited with {:?}", status.code());
1696    }
1697    Ok(())
1698}
1699
1700/// Parse a `[v]MAJOR.MINOR.PATCH[-pre][+build]` string into a comparable tuple.
1701fn parse_semver(s: &str) -> Option<(u64, u64, u64)> {
1702    let core = s.trim().trim_start_matches('v');
1703    let core = core.split(['-', '+']).next().unwrap_or(core);
1704    let mut parts = core.split('.');
1705    let major = parts.next()?.parse().ok()?;
1706    let minor = parts.next().unwrap_or("0").parse().ok()?;
1707    let patch = parts.next().unwrap_or("0").parse().ok()?;
1708    Some((major, minor, patch))
1709}
1710
1711/// True iff `current` is at least `latest` (no update needed). Unparseable
1712/// versions fall back to string equality, so we never falsely report
1713/// up-to-date on garbage — at worst we re-run the (idempotent) installer.
1714fn version_at_least(current: &str, latest: &str) -> bool {
1715    match (parse_semver(current), parse_semver(latest)) {
1716        (Some(c), Some(l)) => c >= l,
1717        _ => current == latest,
1718    }
1719}
1720
1721/// Show configured MCP servers
1722fn show_mcp_servers() {
1723    let config = load_config_or_warn();
1724
1725    if config.mcp_servers.is_empty() {
1726        println!("No MCP servers configured.\n");
1727        println!("Add one with: mermaid add <name>");
1728        println!("Examples:");
1729        println!("  mermaid add context7     # Library documentation");
1730        println!("  mermaid add playwright   # Browser automation");
1731        println!("  mermaid add memory       # Persistent knowledge graph");
1732        return;
1733    }
1734
1735    println!("Configured MCP servers:\n");
1736    for (name, server_cfg) in &config.mcp_servers {
1737        let package = server_cfg
1738            .args
1739            .iter()
1740            .find(|a| !a.starts_with('-'))
1741            .unwrap_or(&server_cfg.command);
1742        let env_keys: Vec<&String> = server_cfg.env.keys().collect();
1743        let env_display = if env_keys.is_empty() {
1744            String::new()
1745        } else {
1746            format!(
1747                " (env: {})",
1748                env_keys
1749                    .iter()
1750                    .map(|k| k.as_str())
1751                    .collect::<Vec<_>>()
1752                    .join(", ")
1753            )
1754        };
1755        println!("  {} — {}{}", name, package, env_display);
1756    }
1757    println!("\nManage with: mermaid add <name> / mermaid remove <name>");
1758}
1759
1760/// Show status of all dependencies
1761async fn show_status(config: &Config) -> Result<()> {
1762    println!("Mermaid Status:");
1763    println!();
1764
1765    // Check remote providers by API-key env presence (matches the
1766    // routing ProviderFactory uses when the user picks a model).
1767    let mut available: Vec<&'static str> = Vec::new();
1768    for profile in PROVIDER_REGISTRY {
1769        let env = config
1770            .providers
1771            .get(profile.name)
1772            .and_then(|c| c.api_key_env.as_deref())
1773            .unwrap_or(profile.api_key_env);
1774        if resolve_api_key(env, None).is_some() {
1775            available.push(profile.name);
1776        }
1777    }
1778    if available.is_empty() {
1779        println!("  [WARNING] Remote providers: none (no API keys in env)");
1780    } else {
1781        println!("  [OK] Remote providers: {}", available.join(", "));
1782    }
1783
1784    // Check Ollama (via HTTP, so remote deployments are honored).
1785    if is_ollama_installed() {
1786        let models = list_ollama_models(config).await;
1787        if models.is_empty() {
1788            println!("  [WARNING] Ollama: Installed (no models)");
1789        } else {
1790            println!("  [OK] Ollama: Running ({} models installed)", models.len());
1791            for model in models.iter().take(3) {
1792                println!("      - {}", model);
1793            }
1794            if models.len() > 3 {
1795                println!("      ... and {} more", models.len() - 3);
1796            }
1797        }
1798    } else {
1799        println!("  [ERROR] Ollama: Not installed");
1800    }
1801
1802    // Check configuration (uses platform-specific path via ProjectDirs)
1803    if let Ok(config_dir) = get_config_dir() {
1804        let config_path = config_dir.join("config.toml");
1805        if config_path.exists() {
1806            println!("  [OK] Configuration: {}", config_path.display());
1807        } else {
1808            println!("  [WARNING] Configuration: Not found (using defaults)");
1809        }
1810    }
1811
1812    // MCP Servers
1813    if config.mcp_servers.is_empty() {
1814        println!("  [INFO] MCP Servers: None configured (use 'mermaid add <name>')");
1815    } else {
1816        println!(
1817            "  [OK] MCP Servers: {} configured",
1818            config.mcp_servers.len()
1819        );
1820        for (name, server_cfg) in &config.mcp_servers {
1821            println!(
1822                "      - {} ({})",
1823                name,
1824                server_cfg.args.get(1).unwrap_or(&server_cfg.command)
1825            );
1826        }
1827    }
1828
1829    // Project instructions (Step 5h). Walks UP from cwd to git root or
1830    // $HOME to find the nearest supported instruction files.
1831    {
1832        let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
1833        let paths = crate::app::instructions::find_instruction_files(&cwd);
1834        if paths.is_empty() {
1835            println!("  [INFO] Project instructions: not found (AGENTS.md, MERMAID.md)");
1836        } else {
1837            match crate::app::instructions::load_from_paths(&paths) {
1838                Some(loaded) => {
1839                    let files = loaded
1840                        .sources
1841                        .iter()
1842                        .map(|source| {
1843                            source
1844                                .path
1845                                .file_name()
1846                                .and_then(|name| name.to_str())
1847                                .unwrap_or("instructions")
1848                        })
1849                        .collect::<Vec<_>>()
1850                        .join(", ");
1851                    println!(
1852                        "  [OK] Project instructions: {} at {} ({} bytes{})",
1853                        files,
1854                        loaded.path.display(),
1855                        loaded.byte_len,
1856                        if loaded.truncated { ", truncated" } else { "" }
1857                    );
1858                },
1859                None => {
1860                    println!(
1861                        "  [WARNING] Project instructions: found but unreadable ({})",
1862                        paths
1863                            .iter()
1864                            .map(|path| path.display().to_string())
1865                            .collect::<Vec<_>>()
1866                            .join(", ")
1867                    );
1868                },
1869            }
1870        }
1871    }
1872
1873    // OpenAI-compatible providers — list anything from the built-in
1874    // registry whose API key resolves, plus any user-defined custom
1875    // providers. No network probe (would slow `mermaid status`).
1876    show_provider_status(config);
1877
1878    // Environment variables (for API providers)
1879    println!("\n  Environment:");
1880    if std::env::var("OLLAMA_API_KEY").is_ok() {
1881        println!("    - OLLAMA_API_KEY: Set (for Ollama Cloud)");
1882    }
1883
1884    println!();
1885    Ok(())
1886}
1887
1888/// Print the remote-providers status block. Includes Anthropic (bespoke
1889/// Messages API) and any OpenAI-compatible provider whose API key resolves.
1890/// Custom providers from `[providers.<name>]` are listed if `base_url`
1891/// and `api_key_env` are both set and the env var resolves.
1892fn show_provider_status(config: &Config) {
1893    let mut configured: Vec<(String, String)> = Vec::new(); // (name, base_url)
1894
1895    // Anthropic — checked first because it's not in the OpenAI-compat
1896    // registry but is a top-tier provider users care about.
1897    let anth_cfg = config.providers.get("anthropic");
1898    if resolve_api_key(
1899        "ANTHROPIC_API_KEY",
1900        anth_cfg.and_then(|c| c.api_key_env.as_deref()),
1901    )
1902    .is_some()
1903    {
1904        let url = anth_cfg
1905            .and_then(|c| c.base_url.clone())
1906            .unwrap_or_else(|| "https://api.anthropic.com/v1".to_string());
1907        configured.push(("anthropic".to_string(), url));
1908    }
1909
1910    // Gemini — also bespoke (not in OpenAI-compat registry).
1911    let gem_cfg = config.providers.get("gemini");
1912    if resolve_api_key_with_fallback(
1913        "GOOGLE_API_KEY",
1914        "GEMINI_API_KEY",
1915        gem_cfg.and_then(|c| c.api_key_env.as_deref()),
1916    )
1917    .is_some()
1918    {
1919        let url = gem_cfg
1920            .and_then(|c| c.base_url.clone())
1921            .unwrap_or_else(|| "https://generativelanguage.googleapis.com/v1beta".to_string());
1922        configured.push(("gemini".to_string(), url));
1923    }
1924
1925    for profile in PROVIDER_REGISTRY {
1926        let user_cfg = config.providers.get(profile.name);
1927        let api_key_present = resolve_api_key(
1928            profile.api_key_env,
1929            user_cfg.and_then(|c| c.api_key_env.as_deref()),
1930        )
1931        .is_some();
1932        if api_key_present {
1933            let url = user_cfg
1934                .and_then(|c| c.base_url.clone())
1935                .unwrap_or_else(|| profile.base_url.to_string());
1936            configured.push((profile.name.to_string(), url));
1937        }
1938    }
1939
1940    // Custom providers — anything in config.providers not in registry
1941    // and not "anthropic" / "gemini" (already handled above).
1942    for (name, cfg) in &config.providers {
1943        if name == "anthropic" || name == "gemini" || lookup_provider(name).is_some() {
1944            continue;
1945        }
1946        if let (Some(url), Some(env)) = (&cfg.base_url, cfg.api_key_env.as_deref())
1947            && resolve_api_key(env, None).is_some()
1948        {
1949            configured.push((name.clone(), url.clone()));
1950        }
1951    }
1952
1953    if configured.is_empty() {
1954        println!(
1955            "  [INFO] Remote providers: None configured (set $ANTHROPIC_API_KEY, \
1956             $GOOGLE_API_KEY, $OPENAI_API_KEY, $GROQ_API_KEY, $OPENROUTER_API_KEY, etc., or \
1957             add [providers.<name>] to config.toml)"
1958        );
1959    } else {
1960        println!("  [OK] Remote providers: {} configured", configured.len());
1961        for (name, url) in configured {
1962            println!("      - {} ({})", name, url);
1963        }
1964    }
1965}
1966
1967/// Dispatch `mermaid pr` subcommands.
1968fn handle_pr(command: &PrCommand) -> Result<()> {
1969    match command {
1970        PrCommand::Create {
1971            title,
1972            body,
1973            summary,
1974            base,
1975            draft,
1976            web,
1977            provider,
1978        } => create_pr(CreatePrArgs {
1979            title: title.as_deref(),
1980            body: body.as_deref(),
1981            summary: summary.as_deref(),
1982            base: base.as_deref(),
1983            draft: *draft,
1984            web: *web,
1985            provider: *provider,
1986        }),
1987    }
1988}
1989
1990struct CreatePrArgs<'a> {
1991    title: Option<&'a str>,
1992    body: Option<&'a str>,
1993    summary: Option<&'a Path>,
1994    base: Option<&'a str>,
1995    draft: bool,
1996    web: bool,
1997    provider: Option<GitHost>,
1998}
1999
2000/// Create a PR/MR by driving the host's official CLI (`gh`/`glab`), reusing
2001/// its authentication. We wrap the platform CLI rather than reimplementing
2002/// per-provider REST clients (issue #2): it reuses existing `gh auth` /
2003/// `glab auth`, handles each host's quirks, and keeps the surface tiny.
2004fn create_pr(args: CreatePrArgs) -> Result<()> {
2005    // Body precedence: --summary <file> wins over inline --body.
2006    let body = match args.summary {
2007        Some(path) => Some(
2008            std::fs::read_to_string(path)
2009                .with_context(|| format!("failed to read summary file {}", path.display()))?,
2010        ),
2011        None => args.body.map(str::to_string),
2012    };
2013
2014    let host = match args.provider {
2015        Some(host) => host,
2016        None => detect_git_host()?,
2017    };
2018
2019    let (cli, install_hint) = match host {
2020        GitHost::Github => (
2021            "gh",
2022            "Install the GitHub CLI (https://cli.github.com) and run `gh auth login`.",
2023        ),
2024        GitHost::Gitlab => (
2025            "glab",
2026            "Install the GitLab CLI (https://gitlab.com/gitlab-org/cli) and run `glab auth login`.",
2027        ),
2028    };
2029    if which::which(cli).is_err() {
2030        anyhow::bail!("`{cli}` was not found on your PATH. {install_hint}");
2031    }
2032
2033    let argv = build_pr_argv(
2034        host,
2035        args.title,
2036        body.as_deref(),
2037        args.base,
2038        args.draft,
2039        args.web,
2040    );
2041
2042    println!("Creating pull/merge request via `{cli}`…");
2043    let status = std::process::Command::new(cli)
2044        .args(&argv)
2045        .status()
2046        .with_context(|| format!("failed to run `{cli}`"))?;
2047    anyhow::ensure!(status.success(), "`{cli}` exited unsuccessfully ({status})");
2048    Ok(())
2049}
2050
2051/// Auto-detect the host: prefer the `origin` remote URL, else fall back to
2052/// whichever provider CLI is installed.
2053fn detect_git_host() -> Result<GitHost> {
2054    if let Some(host) = git_origin_host() {
2055        return Ok(host);
2056    }
2057    if which::which("gh").is_ok() {
2058        return Ok(GitHost::Github);
2059    }
2060    if which::which("glab").is_ok() {
2061        return Ok(GitHost::Gitlab);
2062    }
2063    anyhow::bail!(
2064        "could not detect a Git host from the `origin` remote. Pass `--provider github|gitlab` and install the matching CLI (`gh`/`glab`)."
2065    )
2066}
2067
2068fn git_origin_host() -> Option<GitHost> {
2069    let output = std::process::Command::new("git")
2070        .args(["config", "--get", "remote.origin.url"])
2071        .output()
2072        .ok()?;
2073    if !output.status.success() {
2074        return None;
2075    }
2076    host_from_remote_url(String::from_utf8_lossy(&output.stdout).trim())
2077}
2078
2079fn host_from_remote_url(url: &str) -> Option<GitHost> {
2080    let lower = url.to_ascii_lowercase();
2081    if lower.contains("github.com") {
2082        Some(GitHost::Github)
2083    } else if lower.contains("gitlab") {
2084        Some(GitHost::Gitlab)
2085    } else {
2086        None
2087    }
2088}
2089
2090/// Build the argv passed to the host CLI. Pure (no I/O), so it's unit-tested.
2091fn build_pr_argv(
2092    host: GitHost,
2093    title: Option<&str>,
2094    body: Option<&str>,
2095    base: Option<&str>,
2096    draft: bool,
2097    web: bool,
2098) -> Vec<String> {
2099    let s = |v: &str| v.to_string();
2100    let has_content = title.is_some() || body.is_some();
2101    let mut argv = Vec::new();
2102    match host {
2103        GitHost::Github => {
2104            argv.push(s("pr"));
2105            argv.push(s("create"));
2106            if web {
2107                argv.push(s("--web"));
2108            }
2109            if draft {
2110                argv.push(s("--draft"));
2111            }
2112            if let Some(title) = title {
2113                argv.push(s("--title"));
2114                argv.push(s(title));
2115            }
2116            if let Some(body) = body {
2117                argv.push(s("--body"));
2118                argv.push(s(body));
2119            }
2120            // No explicit content (and not the web form) → let gh fill the
2121            // title/body from the branch's commits rather than blocking on an
2122            // interactive prompt.
2123            if !has_content && !web {
2124                argv.push(s("--fill"));
2125            }
2126            if let Some(base) = base {
2127                argv.push(s("--base"));
2128                argv.push(s(base));
2129            }
2130        },
2131        GitHost::Gitlab => {
2132            argv.push(s("mr"));
2133            argv.push(s("create"));
2134            if web {
2135                argv.push(s("--web"));
2136            }
2137            if draft {
2138                argv.push(s("--draft"));
2139            }
2140            if let Some(title) = title {
2141                argv.push(s("--title"));
2142                argv.push(s(title));
2143            }
2144            if let Some(body) = body {
2145                argv.push(s("--description"));
2146                argv.push(s(body));
2147            }
2148            if !has_content && !web {
2149                argv.push(s("--fill"));
2150            }
2151            if let Some(base) = base {
2152                argv.push(s("--target-branch"));
2153                argv.push(s(base));
2154            }
2155        },
2156    }
2157    argv
2158}
2159
2160#[cfg(test)]
2161mod tests {
2162    use super::*;
2163
2164    #[test]
2165    fn version_compare_handles_update_logic() {
2166        // Up to date / newer than latest ⇒ no update.
2167        assert!(version_at_least("0.10.2", "0.10.2"));
2168        assert!(version_at_least("0.11.0", "0.10.2"));
2169        assert!(version_at_least("1.0.0", "0.99.99"));
2170        // Older ⇒ update available.
2171        assert!(!version_at_least("0.10.1", "0.10.2"));
2172        assert!(!version_at_least("0.9.0", "0.10.0"));
2173        assert!(!version_at_least("0.10.2", "0.11.0"));
2174        // Pre-release/build suffixes and `v` prefixes are tolerated.
2175        assert!(version_at_least("0.10.2", "v0.10.2"));
2176        assert_eq!(parse_semver("v0.11.0-rc1+build"), Some((0, 11, 0)));
2177        assert_eq!(parse_semver("0.10"), Some((0, 10, 0)));
2178        // Garbage never falsely reports up-to-date unless identical.
2179        assert!(!version_at_least("0.10.2", "not-a-version"));
2180    }
2181
2182    #[test]
2183    fn host_from_remote_url_detects_provider() {
2184        assert_eq!(
2185            host_from_remote_url("https://github.com/foo/bar.git"),
2186            Some(GitHost::Github)
2187        );
2188        assert_eq!(
2189            host_from_remote_url("git@github.com:foo/bar.git"),
2190            Some(GitHost::Github)
2191        );
2192        assert_eq!(
2193            host_from_remote_url("https://gitlab.com/foo/bar.git"),
2194            Some(GitHost::Gitlab)
2195        );
2196        assert_eq!(
2197            host_from_remote_url("git@gitlab.example.com:foo/bar.git"),
2198            Some(GitHost::Gitlab)
2199        );
2200        assert_eq!(host_from_remote_url("https://bitbucket.org/foo/bar"), None);
2201    }
2202
2203    #[test]
2204    fn build_pr_argv_github_with_content() {
2205        let argv = build_pr_argv(
2206            GitHost::Github,
2207            Some("T"),
2208            Some("B"),
2209            Some("main"),
2210            true,
2211            false,
2212        );
2213        assert_eq!(
2214            argv,
2215            vec![
2216                "pr", "create", "--draft", "--title", "T", "--body", "B", "--base", "main"
2217            ]
2218        );
2219    }
2220
2221    #[test]
2222    fn build_pr_argv_github_fills_without_content() {
2223        let argv = build_pr_argv(GitHost::Github, None, None, None, false, false);
2224        assert!(argv.contains(&"--fill".to_string()));
2225        assert!(!argv.contains(&"--title".to_string()));
2226    }
2227
2228    #[test]
2229    fn build_pr_argv_web_skips_fill() {
2230        let argv = build_pr_argv(GitHost::Github, None, None, None, false, true);
2231        assert!(argv.contains(&"--web".to_string()));
2232        assert!(!argv.contains(&"--fill".to_string()));
2233    }
2234
2235    #[test]
2236    fn build_pr_argv_gitlab_uses_mr_and_target_branch() {
2237        let argv = build_pr_argv(GitHost::Gitlab, Some("T"), None, Some("main"), false, false);
2238        assert_eq!(&argv[0..2], &["mr", "create"]);
2239        assert!(argv.windows(2).any(|w| w == ["--target-branch", "main"]));
2240        assert!(argv.contains(&"--title".to_string()));
2241    }
2242
2243    #[test]
2244    fn qa_compact_smoke_persists_conversation_and_archive() {
2245        let dir = unique_temp_dir("mermaid-qa-compact-smoke");
2246        std::fs::create_dir_all(&dir).unwrap();
2247
2248        let report = run_qa_compact_smoke(&Config::default(), &dir, 6).unwrap();
2249
2250        assert!(report.ok);
2251        assert!(report.archived_messages > 0);
2252        assert!(report.preserved_messages > 0);
2253        assert!(report.replacement_messages >= 3);
2254        assert!(
2255            std::path::Path::new(report.conversation_path.as_ref().unwrap()).exists(),
2256            "conversation path should exist"
2257        );
2258        assert!(
2259            std::path::Path::new(report.archive_path.as_ref().unwrap()).exists(),
2260            "archive path should exist"
2261        );
2262
2263        let _ = std::fs::remove_dir_all(dir);
2264    }
2265
2266    #[test]
2267    fn qa_model_id_falls_back_to_deterministic() {
2268        assert_eq!(qa_model_id(&Config::default()), "qa/deterministic");
2269    }
2270
2271    fn unique_temp_dir(name: &str) -> std::path::PathBuf {
2272        let nanos = std::time::SystemTime::now()
2273            .duration_since(std::time::UNIX_EPOCH)
2274            .map(|duration| duration.as_nanos())
2275            .unwrap_or_default();
2276        std::env::temp_dir().join(format!("{name}-{nanos}"))
2277    }
2278}