lorum 0.1.2-alpha.1

Unified MCP configuration manager for AI coding tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! Command handlers for the lorum CLI.
//!
//! Each public function corresponds to a CLI subcommand and returns a
//! `Result<(), LorumError>` for uniform error reporting.

use std::path::PathBuf;

use crate::config;
use crate::error::LorumError;

pub mod backup_cmds;
pub mod doctor;
pub use doctor::{
    ConsistencyReport, DoctorResult, print_consistency_reports, print_doctor_results, run_doctor,
    run_doctor_consistency,
};
pub mod hook;
#[cfg(test)]
mod hook_tests;
pub mod init;
#[cfg(test)]
mod init_tests;
pub mod mcp;
#[cfg(test)]
mod mcp_tests;
pub mod rule;
#[cfg(test)]
mod rule_tests;
pub mod skill;
#[cfg(test)]
mod skill_tests;
#[cfg(test)]
mod tests;

/// Resolve a config path: returns `config_path` or the global default.
fn resolve_path(config_path: Option<&str>) -> Result<PathBuf, LorumError> {
    match config_path {
        Some(p) => Ok(PathBuf::from(p)),
        None => config::global_config_path(),
    }
}

/// Load config, treating "file not found" as empty default; propagates parse errors.
fn load_config_or_default(path: &std::path::Path) -> Result<config::LorumConfig, LorumError> {
    match config::load_config(path) {
        Ok(cfg) => Ok(cfg),
        Err(LorumError::ConfigNotFound { .. }) => Ok(config::LorumConfig::default()),
        Err(e) => Err(e),
    }
}

/// Run the `init` subcommand: creates a default config file.
pub fn run_init(config_path: Option<&str>, local: bool, yes: bool) -> Result<(), LorumError> {
    if local {
        init::run_interactive_init(true, yes)
    } else if let Some(path) = config_path {
        // Direct path mode (used by tests): bypass interactive init
        let path = std::path::PathBuf::from(path);
        if path.exists() {
            return Err(LorumError::Other {
                message: format!("config already exists: {}", path.display()),
            });
        }
        let cfg = config::LorumConfig::default();
        config::save_config(&path, &cfg)?;
        println!("created config at: {}", path.display());
        Ok(())
    } else {
        init::run_interactive_init(false, yes)
    }
}

/// Detect which AI coding tools are installed by checking for their config dirs.
fn detect_installed_tools() -> Vec<String> {
    let mut tools = Vec::new();
    if let Some(home) = dirs::home_dir() {
        if home.join(".claude").exists() {
            tools.push("claude-code".into());
        }
        if home.join(".codex").exists() {
            tools.push("codex".into());
        }
        if home.join(".proma").exists() {
            tools.push("proma".into());
        }
        if home.join(".kimi").exists() {
            tools.push("kimi".into());
        }
        if home.join(".codeium").exists() {
            tools.push("windsurf".into());
        }
        if home.join(".config").join("opencode").exists() {
            tools.push("opencode".into());
        }
        if home.join(".continue").exists() {
            tools.push("continue".into());
        }
    }
    if std::env::current_dir()
        .map(|d| d.join(".trae").exists())
        .unwrap_or(false)
    {
        tools.push("trae".into());
    }
    if std::env::current_dir()
        .map(|d| d.join(".cursor").exists())
        .unwrap_or(false)
    {
        tools.push("cursor".into());
    }
    if command_exists("windsurf") {
        tools.push("windsurf".into());
    }
    if command_exists("opencode") {
        tools.push("opencode".into());
    }
    if command_exists("continue") {
        tools.push("continue".into());
    }
    tools.sort();
    tools.dedup();
    tools
}
/// Summary of importing from a single tool across all supported dimensions.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct ImportSummary {
    tool: String,
    mcp_servers: usize,
    hook_handlers: usize,
    rules_sections: usize,
    errors: Vec<String>,
}

/// Run the `import` subcommand: reads configuration from tools and merges.
///
/// Supports importing across three dimensions: MCP servers, hooks, and rules.
/// When `from` is `"all"`, every registered tool is queried. If `dry_run` is
/// true, no files are written — only a preview is printed.
///
/// # Errors
///
/// - [`LorumError::AdapterNotFound`] if `from` names a tool with no adapter
///   in any dimension and `from` is not `"all"`.
/// - [`LorumError::ConfigWrite`] if saving the merged configuration fails.
pub fn run_import(from: &str, dry_run: bool, config_path: Option<&str>) -> Result<(), LorumError> {
    let path = resolve_path(config_path)?;
    let mut lorum_config = load_config_or_default(&path)?;

    let tool_names: Vec<String> = if from == "all" {
        crate::adapters::all_adapter_tool_names()
    } else {
        let known = crate::adapters::all_adapter_tool_names();
        if !known.iter().any(|n| n == from) {
            return Err(LorumError::AdapterNotFound { name: from.into() });
        }
        vec![from.to_string()]
    };

    let mut summaries: Vec<ImportSummary> = Vec::new();

    for tool_name in &tool_names {
        let mut summary = ImportSummary {
            tool: tool_name.clone(),
            mcp_servers: 0,
            hook_handlers: 0,
            rules_sections: 0,
            errors: Vec::new(),
        };

        import_mcp(tool_name, dry_run, &mut summary, &mut lorum_config);
        import_hooks(tool_name, dry_run, &mut summary, &mut lorum_config);
        import_rules(tool_name, dry_run, &mut summary)?;

        if summary.mcp_servers > 0
            || summary.hook_handlers > 0
            || summary.rules_sections > 0
            || !summary.errors.is_empty()
        {
            summaries.push(summary);
        }
    }

    if dry_run {
        print_import_preview(&summaries);
    } else {
        config::save_config(&path, &lorum_config)?;
        print_import_results(&summaries);
    }

    Ok(())
}

/// Import MCP servers from a single tool into the lorum config.
fn import_mcp(
    tool_name: &str,
    dry_run: bool,
    summary: &mut ImportSummary,
    lorum_config: &mut config::LorumConfig,
) {
    if let Some(adapter) = crate::adapters::find_adapter(tool_name) {
        match adapter.read_mcp() {
            Ok(mcp) => {
                summary.mcp_servers = mcp.servers.len();
                if !dry_run {
                    for (name, server) in &mcp.servers {
                        lorum_config
                            .mcp
                            .servers
                            .insert(name.clone(), server.clone());
                    }
                }
            }
            Err(e) => summary.errors.push(format!("mcp: {e}")),
        }
    }
}

/// Import hooks from a single tool into the lorum config.
fn import_hooks(
    tool_name: &str,
    dry_run: bool,
    summary: &mut ImportSummary,
    lorum_config: &mut config::LorumConfig,
) {
    if let Some(adapter) = crate::adapters::find_hooks_adapter(tool_name) {
        match adapter.read_hooks() {
            Ok(hooks) => {
                summary.hook_handlers = hooks.events.values().map(|v| v.len()).sum();
                if !dry_run {
                    for (event, handlers) in &hooks.events {
                        lorum_config
                            .hooks
                            .events
                            .insert(event.clone(), handlers.clone());
                    }
                }
            }
            Err(e) => summary.errors.push(format!("hooks: {e}")),
        }
    }
}

/// Import rules from a single tool into the project's RULES.md.
fn import_rules(
    tool_name: &str,
    dry_run: bool,
    summary: &mut ImportSummary,
) -> Result<(), LorumError> {
    if let Some(adapter) = crate::adapters::find_rules_adapter(tool_name) {
        let project_root = crate::rules::find_project_root(
            &std::env::current_dir().map_err(|e| LorumError::Io { source: e })?,
        )
        .unwrap_or_else(|| {
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))
        });
        match adapter.read_rules(&project_root) {
            Ok(Some(content)) => {
                let imported = crate::rules::parse_rules(&content);
                summary.rules_sections = imported.sections.len();
                if !dry_run {
                    let mut existing = match crate::rules::load_rules(&project_root) {
                        Ok(rules) => rules,
                        Err(LorumError::ConfigNotFound { .. }) => crate::rules::RulesFile {
                            preamble: crate::rules::DEFAULT_PREAMBLE.to_string(),
                            sections: Vec::new(),
                        },
                        Err(e) => return Err(e),
                    };
                    let existing_names: std::collections::HashSet<String> =
                        existing.sections.iter().map(|s| s.name.clone()).collect();
                    for section in imported.sections {
                        if !existing_names.contains(&section.name) {
                            existing.sections.push(section);
                        }
                    }
                    crate::rules::save_rules(&project_root, &existing)?;
                }
            }
            Ok(None) => {}
            Err(e) => summary.errors.push(format!("rules: {e}")),
        }
    }
    Ok(())
}

fn print_import_preview(summaries: &[ImportSummary]) {
    println!(
        "{:<15} {:>6} {:>6} {:>6} {:<30}",
        "TOOL", "MCP", "HOOKS", "RULES", "ERRORS"
    );
    for s in summaries {
        let errors = if s.errors.is_empty() {
            "".to_string()
        } else {
            s.errors.join(", ")
        };
        let errors_truncated = if errors.len() > 30 {
            format!("{}...", &errors[..27])
        } else {
            errors
        };
        println!(
            "{:<15} {:>6} {:>6} {:>6} {:<30}",
            s.tool, s.mcp_servers, s.hook_handlers, s.rules_sections, errors_truncated
        );
    }
}

fn print_import_results(summaries: &[ImportSummary]) {
    let total_mcp: usize = summaries.iter().map(|s| s.mcp_servers).sum();
    let total_hooks: usize = summaries.iter().map(|s| s.hook_handlers).sum();
    let total_rules: usize = summaries.iter().map(|s| s.rules_sections).sum();
    for s in summaries {
        println!(
            "imported {} mcp servers, {} hook handlers, {} rules sections from {}",
            s.mcp_servers, s.hook_handlers, s.rules_sections, s.tool
        );
    }
    println!(
        "imported {total_mcp} servers, {total_hooks} hook handlers, {total_rules} rules sections total"
    );
}

/// Run the `sync` subcommand: synchronises (or dry-runs) MCP configuration.
pub fn run_sync(
    dry_run: bool,
    tools: &[String],
    expand_env: bool,
    config_path: Option<&str>,
) -> Result<(), LorumError> {
    let config =
        config::resolve_effective_config_from_cwd(config_path.map(PathBuf::from).as_deref())?;
    let mcp = crate::env_interpolate::interpolate_mcp_config(&config.mcp, expand_env);

    if dry_run {
        let results = if tools.is_empty() {
            crate::sync::dry_run_all(&mcp)
        } else {
            crate::sync::dry_run_tools(&mcp, tools)
        };
        print_dry_run_results(&results);
    } else {
        let results = if tools.is_empty() {
            crate::sync::sync_all(&mcp)
        } else {
            crate::sync::sync_tools(&mcp, tools)
        };
        let failed = print_sync_results(&results);
        if failed > 0 {
            eprintln!("{failed} tool(s) failed to sync");
        }
    }
    Ok(())
}

fn print_dry_run_results(results: &[crate::sync::DryRunResult]) {
    for r in results {
        let status = if r.success { "OK" } else { "FAIL" };
        if let Some(diff) = &r.diff {
            let summary = format!(
                "+{}/-{}/~{}/={}",
                diff.added.len(),
                diff.removed.len(),
                diff.modified.len(),
                diff.unchanged.len()
            );
            println!("{:<15} {:<6} {summary}", r.tool, status);
        } else {
            println!("{:<15} {:<6}", r.tool, status);
        }
        if let Some(err) = &r.error {
            println!("  error: {err}");
        }
    }
}

fn print_sync_results(results: &[crate::sync::SyncResult]) -> usize {
    for r in results {
        let status = if r.success { "OK" } else { "FAIL" };
        println!("{:<15} {:<6} {} servers", r.tool, status, r.servers_synced);
        if let Some(err) = &r.error {
            println!("  error: {err}");
        }
    }
    results.iter().filter(|r| !r.success).count()
}

/// A single issue found during the self-check of the unified configuration.
#[derive(Debug, Clone)]
pub struct SelfCheckIssue {
    /// Category of the issue (e.g. "mcp", "hooks", "skills").
    pub category: String,
    /// Human-readable description.
    pub message: String,
}

/// Perform a self-check of the effective configuration and return structured issues.
///
/// Checks MCP servers (command availability, env references), hooks (event
/// names, handler fields), and the unified skills directory structure.
pub fn perform_self_check(config_path: Option<&str>) -> Result<Vec<SelfCheckIssue>, LorumError> {
    let config =
        config::resolve_effective_config_from_cwd(config_path.map(PathBuf::from).as_deref())?;

    let mut issues = Vec::new();
    check_mcp_servers_structured(&config, &mut issues);
    check_hooks_structured(&config, &mut issues);
    check_skills_structured(&mut issues);

    Ok(issues)
}

/// Run the `check` subcommand: validates the effective configuration.
///
/// Checks MCP servers (command availability, env references), hooks (event
/// names, handler fields), and the unified skills directory structure.
pub fn run_check(config_path: Option<&str>) -> Result<(), LorumError> {
    let config =
        config::resolve_effective_config_from_cwd(config_path.map(PathBuf::from).as_deref())?;

    let issues = perform_self_check(config_path)?;

    // ── Summary ─────────────────────────────────────────────────────
    if issues.is_empty() {
        let hook_events = config.hooks.events.len();
        let hook_handlers: usize = config.hooks.events.values().map(|v| v.len()).sum();
        println!(
            "config is valid ({} servers, {} hook events, {} handlers)",
            config.mcp.servers.len(),
            hook_events,
            hook_handlers,
        );
    } else {
        for issue in &issues {
            eprintln!("issue: {}", issue.message);
        }
        return Err(LorumError::Other {
            message: format!("{} issue(s) found", issues.len()),
        });
    }
    Ok(())
}

/// Check MCP servers for command availability and unset env references.
fn check_mcp_servers_structured(config: &config::LorumConfig, issues: &mut Vec<SelfCheckIssue>) {
    for (name, server) in &config.mcp.servers {
        if server.command.is_empty() {
            issues.push(SelfCheckIssue {
                category: "mcp".into(),
                message: format!("server '{name}' has empty command"),
            });
            continue;
        }
        if !command_exists(&server.command) {
            issues.push(SelfCheckIssue {
                category: "mcp".into(),
                message: format!(
                    "server '{name}' command '{}' not found on PATH",
                    server.command
                ),
            });
        }
        // Check for unset env references in command, args, and env values.
        let mut refs = find_unset_env_refs(&server.command);
        for arg in &server.args {
            refs.extend(find_unset_env_refs(arg));
        }
        for val in server.env.values() {
            refs.extend(find_unset_env_refs(val));
        }
        for var in refs {
            issues.push(SelfCheckIssue {
                category: "mcp".into(),
                message: format!(
                    "server '{name}' references unset environment variable '${{{var}}}'"
                ),
            });
        }
    }
}

/// Check hooks for valid event names and non-empty handler fields.
fn check_hooks_structured(config: &config::LorumConfig, issues: &mut Vec<SelfCheckIssue>) {
    for (event, handlers) in &config.hooks.events {
        if event.is_empty() {
            issues.push(SelfCheckIssue {
                category: "hooks".into(),
                message: "hooks: empty event name".into(),
            });
            continue;
        }
        if !is_valid_kebab_case(event) {
            issues.push(SelfCheckIssue {
                category: "hooks".into(),
                message: format!("hooks: event '{event}' is not valid kebab-case"),
            });
        }
        for (i, h) in handlers.iter().enumerate() {
            if h.matcher.is_empty() {
                issues.push(SelfCheckIssue {
                    category: "hooks".into(),
                    message: format!("hooks: event '{event}' handler {i} has empty matcher"),
                });
            }
            if h.command.is_empty() {
                issues.push(SelfCheckIssue {
                    category: "hooks".into(),
                    message: format!("hooks: event '{event}' handler {i} has empty command"),
                });
            }
        }
    }
}

/// Check the unified skills directory structure.
fn check_skills_structured(issues: &mut Vec<SelfCheckIssue>) {
    match crate::skills::global_skills_dir() {
        Ok(dir) if dir.exists() => match crate::skills::scan_skills_dir(&dir) {
            Ok(entries) => {
                for entry in &entries {
                    if entry.manifest.name.is_empty() {
                        issues.push(SelfCheckIssue {
                            category: "skills".into(),
                            message: format!(
                                "skill '{}' has empty manifest name",
                                entry.dir_path.display()
                            ),
                        });
                    }
                }
            }
            Err(e) => {
                issues.push(SelfCheckIssue {
                    category: "skills".into(),
                    message: format!("failed to scan skills directory '{}': {e}", dir.display()),
                });
            }
        },
        _ => {} // No global skills directory yet — that's fine.
    }
}

/// Check whether a command exists on PATH or as an absolute/relative path.
fn command_exists(cmd: &str) -> bool {
    if cmd.contains('/') || cmd.contains('\\') {
        return std::path::Path::new(cmd).is_file();
    }
    if let Ok(path_env) = std::env::var("PATH") {
        for dir in path_env.split(if cfg!(windows) { ';' } else { ':' }) {
            let full = std::path::Path::new(dir).join(cmd);
            if full.is_file() {
                return true;
            }
            #[cfg(windows)]
            if std::path::Path::new(dir)
                .join(format!("{cmd}.exe"))
                .is_file()
            {
                return true;
            }
        }
    }
    false
}

/// Find all `${VAR}` references in a string and return those that are unset.
fn find_unset_env_refs(value: &str) -> Vec<String> {
    let mut unset = Vec::new();
    let chars: Vec<char> = value.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        if chars[i] == '$' && i + 1 < chars.len() && chars[i + 1] == '{' {
            i += 2;
            let mut var_name = String::new();
            let mut found_close = false;
            while i < chars.len() {
                if chars[i] == '}' {
                    found_close = true;
                    i += 1;
                    break;
                }
                var_name.push(chars[i]);
                i += 1;
            }
            if found_close && std::env::var(&var_name).is_err() {
                unset.push(var_name);
            }
        } else {
            i += 1;
        }
    }
    unset
}

/// Validate that a string is valid kebab-case (lowercase letters, digits, hyphens).
pub(crate) fn is_valid_kebab_case(s: &str) -> bool {
    !s.is_empty()
        && s.len() <= 64
        && s.chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
        && !s.starts_with('-')
        && !s.ends_with('-')
        && !s.contains("--")
}

/// Run the `status` subcommand: shows installation status per tool.
///
/// Displays a panoramic view across all four dimensions (MCP, Rules, Hooks,
/// Skills) for every registered tool.
pub fn run_status(_config_path: Option<&str>) -> Result<(), LorumError> {
    let mut tool_names = std::collections::BTreeSet::new();

    for a in crate::adapters::all_adapters() {
        tool_names.insert(a.name().to_string());
    }
    for a in crate::adapters::all_rules_adapters() {
        tool_names.insert(a.name().to_string());
    }
    for a in crate::adapters::all_hooks_adapters() {
        tool_names.insert(a.name().to_string());
    }
    for a in crate::adapters::all_skills_adapters() {
        tool_names.insert(a.name().to_string());
    }

    let cwd = std::env::current_dir().ok();

    println!(
        "{:<15} {:>6} {:>8} {:>8} {:>8}",
        "TOOL", "MCP", "RULES", "HOOKS", "SKILLS"
    );

    for name in tool_names {
        let mcp = mcp_status(&name);
        let rules = rules_status(&name, cwd.as_deref());
        let hooks = hooks_status(&name);
        let skills = skills_status(&name);

        println!(
            "{:<15} {:>6} {:>8} {:>8} {:>8}",
            name,
            fmt_count(mcp),
            fmt_count(rules),
            fmt_count(hooks),
            fmt_count(skills),
        );
    }
    Ok(())
}

/// Format a dimension count for display: `None` → "-", `Some(0)` → "·", `Some(n)` → "n".
fn fmt_count(count: Option<usize>) -> String {
    match count {
        None => "-".to_string(),
        Some(0) => "·".to_string(),
        Some(n) => n.to_string(),
    }
}

/// Query MCP server count for a tool, returning `None` if the tool has no MCP adapter.
fn mcp_status(name: &str) -> Option<usize> {
    let adapter = crate::adapters::find_adapter(name)?;
    let paths = adapter.config_paths();
    if !paths.iter().any(|p| p.exists()) {
        return Some(0);
    }
    adapter.read_mcp().map(|m| m.servers.len()).ok()
}

/// Query rules section count for a tool, returning `None` if unsupported.
fn rules_status(name: &str, project_root: Option<&std::path::Path>) -> Option<usize> {
    let adapter = crate::adapters::find_rules_adapter(name)?;
    let root = project_root?;
    let content = adapter.read_rules(root).ok()?;
    Some(
        content
            .map(|c| crate::rules::parse_rules(&c).sections.len())
            .unwrap_or(0),
    )
}

/// Query hooks count for a tool, returning `None` if unsupported.
fn hooks_status(name: &str) -> Option<usize> {
    let adapter = crate::adapters::find_hooks_adapter(name)?;
    let paths = adapter.config_paths();
    if !paths.iter().any(|p| p.exists()) {
        return Some(0);
    }
    adapter
        .read_hooks()
        .map(|h| h.events.values().map(|v| v.len()).sum())
        .ok()
}

/// Query skills count for a tool, returning `None` if unsupported.
fn skills_status(name: &str) -> Option<usize> {
    let adapter = crate::adapters::find_skills_adapter(name)?;
    adapter.read_skills().map(|s| s.len()).ok()
}

/// Run the `config` subcommand: outputs resolved configuration.
pub fn run_config(
    resolve_env: bool,
    local: bool,
    global: bool,
    format: config::OutputFormat,
    config_path: Option<&str>,
) -> Result<(), LorumError> {
    let (config, source) = if let Some(p) = config_path {
        (
            config::load_config(std::path::Path::new(p))?,
            format!("file: {p}"),
        )
    } else if global {
        let path = config::global_config_path()?;
        (
            load_config_or_default(&path)?,
            format!("global: {}", path.display()),
        )
    } else if local {
        let cwd = std::env::current_dir()?;
        match config::find_project_config(&cwd) {
            Some(p) => {
                let proj = config::load_project_config(&p)?
                    .ok_or_else(|| LorumError::ConfigNotFound { path: p.clone() })?;
                (
                    config::LorumConfig {
                        mcp: proj.mcp,
                        hooks: proj.hooks,
                    },
                    format!("local: {}", p.display()),
                )
            }
            None => {
                return Err(LorumError::ConfigNotFound {
                    path: cwd.join(".lorum").join("config.yaml"),
                });
            }
        }
    } else {
        let path = config::global_config_path()?;
        let cfg = config::resolve_effective_config_from_cwd(None)?;
        let cwd = std::env::current_dir()?;
        let source = if config::find_project_config(&cwd).is_some() {
            format!("merged: global ({}) + local project", path.display())
        } else {
            format!("global: {}", path.display())
        };
        (cfg, source)
    };

    let output = if resolve_env {
        let mcp = crate::env_interpolate::interpolate_mcp_config(&config.mcp, true);
        let hooks = crate::env_interpolate::interpolate_hooks_config(&config.hooks, true);
        config::LorumConfig { mcp, hooks }
    } else {
        config
    };

    match format {
        config::OutputFormat::Yaml => {
            let yaml = serde_yaml::to_string(&output).map_err(|e| LorumError::Other {
                message: format!("failed to serialize config: {e}"),
            })?;
            println!("# Source: {source}");
            print!("{yaml}");
        }
        config::OutputFormat::Json => {
            let json = serde_json::to_string_pretty(&output).map_err(|e| LorumError::Other {
                message: format!("failed to serialize config: {e}"),
            })?;
            println!("{json}");
        }
    }
    Ok(())
}

/// Run the `backup list` subcommand.
pub fn run_backup_list(config_path: Option<&str>) -> Result<(), LorumError> {
    backup_cmds::run_backup_list(config_path)
}

/// Run the `backup create` subcommand.
pub fn run_backup_create(
    tools: &[String],
    all: bool,
    config_path: Option<&str>,
) -> Result<(), LorumError> {
    backup_cmds::run_backup_create(tools, all, config_path)
}

/// Run the `backup restore` subcommand.
pub fn run_backup_restore(
    tool: &str,
    backup: Option<&str>,
    config_path: Option<&str>,
) -> Result<(), LorumError> {
    backup_cmds::run_backup_restore(tool, backup, config_path)
}