uira-commit-hook-cli 0.1.1

Standalone CLI for git hooks and AI-assisted dev workflows
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
mod agent_workflow;
mod comments;
mod config;
mod diagnostics;
mod hooks;
mod linter;
mod runtime;
mod typos;

use agent_workflow::detectors::{typos::TyposDetector, Scope};
use agent_workflow::{AgentWorkflow, TaskOptions, WorkflowConfig, WorkflowResult, WorkflowTask};
use clap::{Parser, Subcommand};
use colored::Colorize;
use config::Config;
use hooks::HookExecutor;
use linter::Linter;
use runtime::block_on;
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::process;
use uira_agent::{init_subscriber, TelemetryConfig};
use uira_orchestration::features::builtin_skills::{create_builtin_skills, get_builtin_skill};
use uira_orchestration::features::uira_state::has_uira_state;
use uira_orchestration::{create_uira_session, get_agent_definitions, AgentConfig, SessionOptions};

#[derive(Parser)]
#[command(name = "uira-commit-hook-cli")]
#[command(version, about = "⚡ Lightning-fast Rust-native git hooks manager & AI harness", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize uira configuration
    Init {
        #[arg(short, long, default_value = "uira.yml")]
        config: String,
    },
    /// Install git hooks to .git/hooks/
    Install,
    /// Run a specific git hook
    Run { hook: String },
    /// Lint JS/TS files with native oxc
    Lint {
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        files: Vec<String>,
    },
    /// Check for typos
    Typos {
        #[arg(long, help = "Use AI to decide and apply fixes (stages by default, use --no-add to skip)")]
        ai: bool,
        #[arg(long, help = "Check only staged/cached files")]
        cached: bool,
        #[arg(long, help = "Commit after fixing with AI-generated message (e.g., 'fix: correct 3 typos in main.rs')", requires = "ai", conflicts_with = "no_add")]
        commit: bool,
        #[arg(long, help = "Fix only, do not stage modified files")]
        no_add: bool,
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        files: Vec<String>,
    },
    /// Format code (Rust via rustfmt/cargo fmt, JS/TS via oxfmt)
    Format {
        #[arg(long, help = "Check formatting without applying changes")]
        check: bool,
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        files: Vec<String>,
    },
    /// Manage agents
    Agent {
        #[command(subcommand)]
        action: AgentCommands,
    },
    /// Manage SDK sessions
    Session {
        #[command(subcommand)]
        action: SessionCommands,
    },
    /// Manage skills
    Skill {
        #[command(subcommand)]
        action: SkillCommands,
    },
    /// Score-based verification goals
    Goals {
        #[command(subcommand)]
        action: GoalsCommands,
    },
    /// Run diagnostics (lsp_diagnostics) on files
    Diagnostics {
        #[arg(long, help = "Use AI to decide and apply fixes (stages by default, use --no-add to skip)")]
        ai: bool,
        #[arg(long, help = "Check only staged/cached files")]
        cached: bool,
        #[arg(long, help = "Commit after fixing with AI-generated message (e.g., 'fix: resolve 5 type errors in auth.rs')", requires = "ai", conflicts_with = "no_add")]
        commit: bool,
        #[arg(long, help = "Fix only, do not stage modified files")]
        no_add: bool,
        #[arg(
            long,
            value_parser = clap::builder::PossibleValuesParser::new(["error", "warning", "all"]),
            help = "Severity filter: error, warning, all (default from config or 'error')"
        )]
        severity: Option<String>,
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        files: Vec<String>,
    },
    /// Check and manage comments with AI assistance
    Comments {
        #[arg(long, help = "Use AI to decide and apply fixes (stages by default, use --no-add to skip)")]
        ai: bool,
        #[arg(long, help = "Check only staged/cached files")]
        cached: bool,
        #[arg(long, help = "Commit after fixing with AI-generated message (e.g., 'fix: remove 2 stale comments')", requires = "ai", conflicts_with = "no_add")]
        commit: bool,
        #[arg(long, help = "Fix only, do not stage modified files")]
        no_add: bool,
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        files: Vec<String>,
    },
}

#[derive(Subcommand)]
enum AgentCommands {
    /// List all available agents
    List,
    /// Show details of a specific agent
    Info { name: String },
    /// Delegate a task to an agent
    Delegate {
        #[arg(short, long)]
        agent: String,
        #[arg(short, long)]
        prompt: String,
        #[arg(short, long)]
        model: Option<String>,
    },
}

#[derive(Subcommand)]
enum SessionCommands {
    /// Start a new session
    Start {
        #[arg(short, long)]
        config: Option<String>,
    },
    /// Show session status
    Status,
}

#[derive(Subcommand)]
enum SkillCommands {
    /// List available skills
    List,
    /// Show skill template
    Show { name: String },
}

#[derive(Subcommand)]
enum GoalsCommands {
    /// Check all goals or a specific goal
    Check {
        /// Optional goal name to check (checks all if not specified)
        name: Option<String>,
    },
    /// Watch goals continuously until all pass
    Watch {
        /// Check interval in seconds
        #[arg(short, long, default_value = "30")]
        interval: u64,
        /// Maximum iterations before giving up
        #[arg(short, long, default_value = "100")]
        max_iterations: u32,
    },
    /// List configured goals
    List,
}

fn main() {
    let telemetry_config = TelemetryConfig::default();
    init_subscriber(&telemetry_config);

    let cli = Cli::parse();

    let result = match cli.command {
        Commands::Init { config } => init_command(&config),
        Commands::Install => install_command(),
        Commands::Run { hook } => run_command(&hook),
        Commands::Lint { files } => lint_command(&files),
        Commands::Typos {
            ai,
            cached,
            commit,
            no_add,
            files,
        } => typos_command(ai, cached, commit, no_add, &files),
        Commands::Format { check, files } => format_command(check, &files),
        Commands::Agent { action } => agent_command(action),
        Commands::Session { action } => session_command(action),
        Commands::Skill { action } => skill_command(action),
        Commands::Goals { action } => goals_command(action),
        Commands::Diagnostics {
            ai,
            cached,
            commit,
            no_add,
            severity,
            files,
        } => diagnostics_command(ai, cached, commit, no_add, severity.as_deref(), &files),
        Commands::Comments {
            ai,
            cached,
            commit,
            no_add,
            files,
        } => comments_command(ai, cached, commit, no_add, &files),
    };

    if let Err(e) = result {
        eprintln!("Error: {}", e);
        process::exit(1);
    }
}

fn init_command(config_path: &str) -> anyhow::Result<()> {
    println!("⚡ Initializing uira...\n");

    let mut created_files = Vec::new();

    if !Path::new(config_path).exists() {
        let config = Config::default_config();
        let yaml = config.to_yaml()?;
        fs::write(config_path, yaml)?;
        created_files.push(config_path.to_string());
    }

    if created_files.is_empty() {
        println!("ℹ️  Config already exists");
    } else {
        println!("✅ Created:");
        for file in &created_files {
            println!("{}", file);
        }
    }

    println!("\n📦 Next steps:");
    println!("   1. Run: uira-commit-hook-cli install");
    println!("   2. Commit normally - hooks will run automatically");

    Ok(())
}

fn install_command() -> anyhow::Result<()> {
    println!("📦 Installing git hooks...\n");

    let git_dir = find_git_dir()?;
    let hooks_dir = git_dir.join("hooks");

    if !hooks_dir.exists() {
        fs::create_dir_all(&hooks_dir)?;
    }

    let config_path = "uira.yml";
    if !Path::new(config_path).exists() {
        anyhow::bail!(
            "Config file not found: {}. Run 'uira-commit-hook-cli init' first.",
            config_path
        );
    }

    let config = Config::from_file(config_path)?;
    let mut installed_hooks = Vec::new();

    for hook_name in config.hooks.keys() {
        let hook_path = hooks_dir.join(hook_name);
        let hook_script = generate_hook_script(hook_name);

        if hook_path.exists() {
            let existing = fs::read_to_string(&hook_path)?;
            if !existing.contains("# uira-commit-hook-cli managed hook") {
                let backup_path = hooks_dir.join(format!("{}.backup", hook_name));
                fs::rename(&hook_path, &backup_path)?;
                println!(
                    "   ⚠️  Backed up existing {} to {}.backup",
                    hook_name, hook_name
                );
            }
        }

        fs::write(&hook_path, hook_script)?;

        #[cfg(unix)]
        {
            let mut perms = fs::metadata(&hook_path)?.permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&hook_path, perms)?;
        }

        installed_hooks.push(hook_name.clone());
    }

    if installed_hooks.is_empty() {
        println!("ℹ️  No hooks defined in config");
    } else {
        println!("✅ Installed hooks:");
        for hook in &installed_hooks {
            println!("{}", hook);
        }
    }

    println!("\n🎉 Done! Git hooks are now active.");
    Ok(())
}

fn find_git_dir() -> anyhow::Result<std::path::PathBuf> {
    let current = std::env::current_dir()?;
    let mut path = current.as_path();

    loop {
        let git_dir = path.join(".git");
        if git_dir.is_dir() {
            return Ok(git_dir);
        }

        match path.parent() {
            Some(parent) => path = parent,
            None => anyhow::bail!("Not a git repository (or any parent up to mount point)"),
        }
    }
}

fn generate_hook_script(hook_name: &str) -> String {
    format!(
        r#"#!/bin/sh
# uira-commit-hook-cli managed hook - do not edit
# This hook was generated by uira-commit-hook-cli. To modify, edit uira.yml

REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
cd "$REPO_ROOT" || exit 1

if [ -x "./target/release/uira-commit-hook-cli" ]; then
    exec ./target/release/uira-commit-hook-cli run {}
fi

if [ -x "./target/debug/uira-commit-hook-cli" ]; then
    exec ./target/debug/uira-commit-hook-cli run {}
fi

exec cargo run -q -p uira-commit-hook-cli -- run {}
"#,
        hook_name, hook_name, hook_name
    )
}

fn run_command(hook_name: &str) -> anyhow::Result<()> {
    let config_path = "uira.yml";

    if !Path::new(config_path).exists() {
        anyhow::bail!(
            "Config file not found: {}. Run 'uira-commit-hook-cli init' first.",
            config_path
        );
    }

    let config = Config::from_file(config_path)?;

    let hook_config = config
        .hooks
        .get(hook_name)
        .ok_or_else(|| anyhow::anyhow!("Hook '{}' not found in config", hook_name))?;

    let executor = HookExecutor::new(hook_name.to_string());
    executor.execute(hook_config)?;

    println!("\n✅ Hook '{}' completed successfully", hook_name);
    Ok(())
}

fn lint_command(files: &[String]) -> anyhow::Result<()> {
    let linter = Linter::default();

    let files_to_lint = if files.is_empty() {
        collect_files_from_cwd()?
    } else {
        files.to_vec()
    };

    let success = linter.run(&files_to_lint)?;

    if !success {
        process::exit(1);
    }

    Ok(())
}

fn typos_command(ai: bool, cached: bool, commit: bool, no_add: bool, files: &[String]) -> anyhow::Result<()> {
    if ai {
        println!("🔍 Starting AI-assisted typos workflow...\n");

        let config = WorkflowConfig {
            auto_stage: !no_add,
            auto_commit: commit,
            cached_only: cached,
            files: files.to_vec(),
            ..Default::default()
        };

        block_on(async {
            let working_dir = std::env::current_dir()?;
            let detector = TyposDetector::new(&working_dir);
            let scope = if !files.is_empty() {
                Scope::from_files(working_dir.clone(), files.to_vec())
            } else if cached {
                Scope::from_staged(&working_dir)?
            } else {
                Scope::from_repo(&working_dir)?
            };

            let mut workflow = AgentWorkflow::new(
                WorkflowTask::Typos,
                config,
                Some(Box::new(detector)),
                Some(scope),
            )
            .await?;
            match workflow.run().await? {
                WorkflowResult::Complete {
                    iterations,
                    files_modified,
                    summary,
                } => {
                    println!("\n✅ Typos workflow complete!");
                    println!("   Iterations: {}", iterations);
                    println!("   Files modified: {}", files_modified.len());
                    if let Some(s) = summary {
                        println!("   Summary: {}", s);
                    }
                    Ok(())
                }
                WorkflowResult::MaxIterationsReached {
                    iterations,
                    files_modified,
                } => {
                    println!("\n⚠️  Max iterations ({}) reached", iterations);
                    println!("   Files modified: {}", files_modified.len());
                    std::process::exit(1);
                }
                WorkflowResult::VerificationFailed {
                    remaining_issues,
                    details,
                    ..
                } => {
                    println!(
                        "\n❌ Verification failed: {} issues remain",
                        remaining_issues
                    );
                    println!("   Details: {}", details);
                    std::process::exit(1);
                }
                WorkflowResult::Cancelled => {
                    println!("\n⚠️  Workflow cancelled");
                    std::process::exit(1);
                }
                WorkflowResult::Failed { error } => {
                    eprintln!("\n❌ Workflow failed: {}", error);
                    std::process::exit(1);
                }
            }
        })
    } else {
        println!("🔍 Checking for typos...\n");
        let mut cmd = std::process::Command::new("typos");

        if !files.is_empty() {
            cmd.args(files);
        } else if cached {
            let output = std::process::Command::new("git")
                .args(["diff", "--cached", "--name-only", "--diff-filter=ACM"])
                .output()
                .map_err(|_| anyhow::anyhow!("Failed to get staged files"))?;
            let staged_files: Vec<String> = String::from_utf8_lossy(&output.stdout)
                .lines()
                .filter(|l| !l.is_empty())
                .map(String::from)
                .collect();
            if staged_files.is_empty() {
                println!("✓ No staged files to check");
                return Ok(());
            }
            cmd.args(&staged_files);
        } else {
            cmd.arg(".");
        }

        let status = cmd.status().map_err(|_| {
            anyhow::anyhow!("Failed to run typos. Is it installed? Run: cargo install typos-cli")
        })?;
        if !status.success() {
            process::exit(1);
        }
        println!("✓ No typos found");
        Ok(())
    }
}

fn format_command(check: bool, files: &[String]) -> anyhow::Result<()> {
    if check {
        println!("🔍 Checking formatting...\n");
    } else {
        println!("🎨 Formatting code...\n");
    }

    let mut ran_any = false;

    let (rust_files, non_rust): (Vec<&String>, Vec<&String>) =
        files.iter().partition(|p| p.ends_with(".rs"));

    if files.is_empty() || !rust_files.is_empty() {
        let mut cmd = if rust_files.is_empty() {
            let mut cmd = std::process::Command::new("cargo");
            cmd.arg("fmt");
            cmd
        } else {
            let mut cmd = std::process::Command::new("rustfmt");
            cmd.args(rust_files);
            cmd
        };

        if check {
            cmd.arg("--check");
        }

        let status = cmd.status().map_err(|_| {
            anyhow::anyhow!("Failed to run rustfmt. Install: rustup component add rustfmt")
        })?;

        ran_any = true;

        if !status.success() {
            if check {
                eprintln!("\n❌ Rust files are not formatted correctly");
                eprintln!("   Run 'uira-commit-hook-cli format' to fix formatting");
            }
            process::exit(1);
        }
    }

    // Filter non_rust to only include JS/TS files or directories for oxfmt
    let js_ts_extensions = [".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs", ".mts", ".cts"];
    let js_ts_files: Vec<&String> = non_rust
        .into_iter()
        .filter(|p| {
            // Allow directories (oxfmt can handle them)
            std::path::Path::new(p).is_dir() || js_ts_extensions.iter().any(|ext| p.ends_with(ext))
        })
        .collect();

    if files.is_empty() || !js_ts_files.is_empty() {
        let mut cmd = std::process::Command::new("oxfmt");
        if check {
            cmd.arg("--check");
        }

        if files.is_empty() {
            cmd.arg(".");
        } else {
            cmd.args(js_ts_files);
        }

        let status = cmd.status().map_err(|_| {
            anyhow::anyhow!("Failed to run oxfmt. Install: npm add -D oxfmt (or pnpm/yarn/bun)")
        })?;

        ran_any = true;

        if !status.success() {
            if check {
                eprintln!("\n❌ JS/TS files are not formatted correctly");
                eprintln!("   Run 'uira-commit-hook-cli format' to fix formatting");
            }
            process::exit(1);
        }
    }

    if !ran_any {
        println!("ℹ️  No files to format");
        return Ok(());
    }

    if check {
        println!("✓ All files are properly formatted");
    } else {
        println!("✓ Formatting complete");
    }

    Ok(())
}

fn collect_files_from_cwd() -> anyhow::Result<Vec<String>> {
    let mut files = Vec::new();
    collect_files_recursive(Path::new("."), &mut files)?;
    Ok(files)
}

fn collect_files_recursive(dir: &Path, files: &mut Vec<String>) -> anyhow::Result<()> {
    if !dir.is_dir() {
        return Ok(());
    }

    let dir_name = dir.file_name().and_then(|n| n.to_str()).unwrap_or("");
    if dir_name == "node_modules" || dir_name == ".git" || dir_name == "dist" || dir_name == "build"
    {
        return Ok(());
    }

    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            collect_files_recursive(&path, files)?;
        } else if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
            if matches!(
                ext,
                "js" | "jsx" | "ts" | "tsx" | "mjs" | "cjs" | "mts" | "cts"
            ) {
                files.push(path.to_string_lossy().to_string());
            }
        }
    }

    Ok(())
}

fn agent_command(action: AgentCommands) -> anyhow::Result<()> {
    match action {
        AgentCommands::List => {
            println!("{}", "⚡ Available Agents".bold());
            println!();

            let agents = get_agent_definitions(None);
            let mut sorted_agents: Vec<_> = agents.iter().collect();
            sorted_agents.sort_by_key(|(name, _)| name.as_str());

            // Group agents by base name
            let mut groups: std::collections::HashMap<String, Vec<(&String, &AgentConfig)>> =
                std::collections::HashMap::new();

            for (name, config) in &sorted_agents {
                let base_name = name.split('-').next().unwrap_or(name).to_string();
                groups.entry(base_name).or_default().push((name, config));
            }

            let mut group_names: Vec<_> = groups.keys().cloned().collect();
            group_names.sort();

            for group in group_names {
                let members = groups.get(&group).unwrap();
                println!("  {}", group.cyan().bold());
                for (name, config) in members {
                    let model = config
                        .model
                        .map(|m| format!(" ({})", m))
                        .unwrap_or_default();
                    println!("    {} {}{}", "".dimmed(), name, model.dimmed());
                }
                println!();
            }

            println!(
                "{} {} agents available",
                "Total:".bold(),
                sorted_agents.len()
            );
            Ok(())
        }
        AgentCommands::Info { name } => {
            let agents = get_agent_definitions(None);
            if let Some(agent) = agents.get(&name) {
                println!("{}", format!("⚡ Agent: {}", name).bold());
                println!();
                println!("{} {}", "Description:".cyan(), agent.description);
                println!(
                    "{} {}",
                    "Model:".cyan(),
                    agent
                        .model
                        .map(|m| m.to_string())
                        .unwrap_or_else(|| "default (sonnet)".to_string())
                );
                println!("{} {}", "Tools:".cyan(), agent.tools.join(", "));
                println!();
                println!("{}", "Prompt:".cyan());
                println!("{}", "-".repeat(60).dimmed());

                // Show first 500 chars of prompt or full if shorter
                let prompt_preview = if agent.prompt.len() > 500 {
                    format!(
                        "{}...\n\n[truncated, {} total chars]",
                        &agent.prompt[..500],
                        agent.prompt.len()
                    )
                } else {
                    agent.prompt.clone()
                };
                println!("{}", prompt_preview);
                Ok(())
            } else {
                anyhow::bail!(
                    "Agent '{}' not found. Use 'uira-commit-hook-cli agent list' to see available agents.",
                    name
                );
            }
        }
        AgentCommands::Delegate {
            agent,
            prompt,
            model,
        } => {
            let agents = get_agent_definitions(None);
            if !agents.contains_key(&agent) {
                anyhow::bail!(
                    "Agent '{}' not found. Use 'uira-commit-hook-cli agent list' to see available agents.",
                    agent
                );
            }

            println!("{}", format!("⚡ Delegating to agent: {}", agent).bold());
            println!("{} {}", "Prompt:".cyan(), prompt);
            if let Some(m) = &model {
                println!("{} {}", "Model override:".cyan(), m);
            }
            println!();

            // For now, just show what would be delegated
            // Full delegation requires SDK bridge implementation
            println!(
                "{}",
                "Note: Full delegation requires active SDK session.".yellow()
            );
            println!("To delegate, use the delegate_task MCP tool:");
            println!();
            println!(
                "  delegate_task(agent=\"{}\", prompt=\"{}\"{})",
                agent,
                prompt,
                model
                    .map(|m| format!(", model=\"{}\"", m))
                    .unwrap_or_default()
            );

            Ok(())
        }
    }
}

fn session_command(action: SessionCommands) -> anyhow::Result<()> {
    match action {
        SessionCommands::Start { config: _ } => {
            println!("{}", "⚡ Starting Uira Session".bold());
            println!();

            let options = SessionOptions {
                working_directory: Some(std::env::current_dir()?.to_string_lossy().to_string()),
                ..Default::default()
            };

            let session = create_uira_session(Some(options));

            println!("{} Created session with:", "".green());
            println!(
                "  {} {} agents",
                "".dimmed(),
                session.query_options.agents.len()
            );
            println!(
                "  {} {} tools",
                "".dimmed(),
                session.query_options.allowed_tools.len()
            );
            println!(
                "  {} {} MCP servers",
                "".dimmed(),
                session.query_options.mcp_servers.len()
            );
            println!(
                "  {} {} context files",
                "".dimmed(),
                session.state.context_files.len()
            );

            if !session.state.context_files.is_empty() {
                println!();
                println!("{}", "Context files:".cyan());
                for file in &session.state.context_files {
                    println!("  {} {}", "".dimmed(), file);
                }
            }

            println!();
            println!(
                "{} Session ready. Use with Claude Agent SDK or uira-commit-hook-cli agent delegate.",
                "".green()
            );

            Ok(())
        }
        SessionCommands::Status => {
            println!("{}", "⚡ Session Status".bold());
            println!();

            // Check for state directory (session state)
            let state_dir = Path::new(".uira/state");
            let has_session_state = state_dir.exists();

            // Check for plan state file
            let has_plan_state = has_uira_state(".");

            if has_session_state || has_plan_state {
                println!("{} Active state found:", "".green());
                if has_session_state {
                    println!("  {} .uira/state/", "".dimmed());
                }
                if has_plan_state {
                    println!("  {} .uira/boulder.json", "".dimmed());
                }
            } else {
                println!("{} No active session state found", "".dimmed());
            }

            // Check for config files
            let config_candidates = ["uira.yaml", "uira.yml", "uira.json", ".uira.yaml"];
            let found_config: Vec<_> = config_candidates
                .iter()
                .filter(|p| Path::new(p).exists())
                .collect();

            println!();
            if found_config.is_empty() {
                println!("{} No config file found", "".dimmed());
            } else {
                println!("{} Config files:", "".green());
                for cfg in found_config {
                    println!("  {} {}", "".dimmed(), cfg);
                }
            }

            Ok(())
        }
    }
}

fn skill_command(action: SkillCommands) -> anyhow::Result<()> {
    match action {
        SkillCommands::List => {
            println!("{}", "⚡ Available Skills".bold());
            println!();

            let skills = create_builtin_skills();

            if skills.is_empty() {
                println!("{} No skills found.", "".dimmed());
                println!();
                println!("Skills are loaded from SKILL.md files in the skills/ directory.");
                return Ok(());
            }

            for skill in &skills {
                println!("  {} {}", "".cyan(), skill.name.bold());
                if !skill.description.is_empty() {
                    println!("    {}", skill.description.dimmed());
                }
                if let Some(agent) = &skill.agent {
                    println!("    Agent: {}", agent);
                }
                if let Some(model) = &skill.model {
                    println!("    Model: {}", model);
                }
                println!();
            }

            println!("{} {} skills available", "Total:".bold(), skills.len());
            Ok(())
        }
        SkillCommands::Show { name } => {
            if let Some(skill) = get_builtin_skill(&name) {
                println!("{}", format!("⚡ Skill: {}", skill.name).bold());
                println!();

                if !skill.description.is_empty() {
                    println!("{} {}", "Description:".cyan(), skill.description);
                }
                if let Some(agent) = &skill.agent {
                    println!("{} {}", "Agent:".cyan(), agent);
                }
                if let Some(model) = &skill.model {
                    println!("{} {}", "Model:".cyan(), model);
                }
                if let Some(hint) = &skill.argument_hint {
                    println!("{} {}", "Argument hint:".cyan(), hint);
                }

                println!();
                println!("{}", "Template:".cyan());
                println!("{}", "-".repeat(60).dimmed());
                println!("{}", skill.template);
                Ok(())
            } else {
                anyhow::bail!(
                    "Skill '{}' not found. Use 'uira-commit-hook-cli skill list' to see available skills.",
                    name
                );
            }
        }
    }
}

fn goals_command(action: GoalsCommands) -> anyhow::Result<()> {
    let rt = tokio::runtime::Runtime::new()?;
    rt.block_on(async { goals_command_async(action).await })
}

fn diagnostics_command(
    ai: bool,
    cached: bool,
    commit: bool,
    no_add: bool,
    severity: Option<&str>,
    files: &[String],
) -> anyhow::Result<()> {
    use anyhow::Context;
    use colored::Colorize;
    use std::process::Command;
    use uira_oxc::{LintRule, Linter, Severity};

    println!("🔍 Running diagnostics...\n");

    let files_to_check = if cached {
        let output = Command::new("git")
            .args(["diff", "--cached", "--name-only", "--diff-filter=ACMR"])
            .output()
            .context("Failed to get staged files")?;
        String::from_utf8_lossy(&output.stdout)
            .lines()
            .map(String::from)
            .collect::<Vec<_>>()
    } else if files.is_empty() {
        collect_files_from_cwd()?
    } else {
        files.to_vec()
    };

    if files_to_check.is_empty() {
        println!("{} No files to check", "".green());
        return Ok(());
    }

    if ai {
        let workflow_config = WorkflowConfig {
            auto_stage: !no_add,
            auto_commit: commit,
            cached_only: cached,
            files: files_to_check.clone(),
            task_options: TaskOptions {
                severity: severity.map(String::from),
                ..Default::default()
            },
            ..Default::default()
        };

        return block_on(async {
            let mut workflow =
                AgentWorkflow::new(WorkflowTask::Diagnostics, workflow_config, None, None).await?;
            match workflow.run().await? {
                WorkflowResult::Complete {
                    iterations,
                    files_modified,
                    summary,
                } => {
                    println!("\n✅ Diagnostics workflow complete!");
                    println!("   Iterations: {}", iterations);
                    println!("   Files modified: {}", files_modified.len());
                    if let Some(s) = summary {
                        println!("   Summary: {}", s);
                    }
                    Ok(())
                }
                WorkflowResult::MaxIterationsReached { .. } => {
                    println!("\n⚠️  Max iterations reached");
                    std::process::exit(1);
                }
                WorkflowResult::VerificationFailed {
                    remaining_issues, ..
                } => {
                    println!("\n{} issues remain", remaining_issues);
                    std::process::exit(1);
                }
                WorkflowResult::Cancelled => {
                    println!("\n⚠️  Workflow cancelled");
                    std::process::exit(1);
                }
                WorkflowResult::Failed { error } => {
                    eprintln!("\n❌ Workflow failed: {}", error);
                    std::process::exit(1);
                }
            }
        });
    }

    let severity = severity.unwrap_or("error");

    let (js_ts_files, other_files): (Vec<_>, Vec<_>) = files_to_check.iter().partition(|f| {
        let ext = std::path::Path::new(f)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("");
        matches!(
            ext,
            "js" | "jsx" | "ts" | "tsx" | "mjs" | "cjs" | "mts" | "cts"
        )
    });

    let rust_files: Vec<_> = other_files
        .iter()
        .filter(|f| f.ends_with(".rs"))
        .cloned()
        .collect();

    let mut error_count = 0;
    let mut warning_count = 0;

    if !js_ts_files.is_empty() {
        let linter = Linter::new(LintRule::recommended());
        let js_files_owned: Vec<String> = js_ts_files.into_iter().cloned().collect();
        let diagnostics = linter.lint_files(&js_files_owned);

        let filtered: Vec<_> = diagnostics
            .iter()
            .filter(|d| match severity {
                "error" => matches!(d.severity, Severity::Error),
                "warning" => matches!(d.severity, Severity::Error | Severity::Warning),
                _ => true,
            })
            .collect();

        for d in &filtered {
            match d.severity {
                Severity::Error => error_count += 1,
                Severity::Warning => warning_count += 1,
                Severity::Info => {}
            }

            let severity_str = match d.severity {
                Severity::Error => "error".red().bold(),
                Severity::Warning => "warning".yellow().bold(),
                Severity::Info => "info".blue(),
            };

            println!(
                "{}:{}:{}: {} [{}]",
                d.file.dimmed(),
                d.line,
                d.column,
                severity_str,
                d.rule.cyan()
            );
            println!("  {}", d.message);
            if let Some(suggestion) = &d.suggestion {
                println!("  {}: {}", "suggestion".dimmed(), suggestion);
            }
            println!();
        }
    }

    if !rust_files.is_empty() {
        let output = Command::new("cargo")
            .arg("check")
            .arg("--message-format=short")
            .output()
            .context("Failed to run cargo check")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            for line in stderr.lines() {
                if line.contains("error")
                    && (severity == "error" || severity == "warning" || severity == "all")
                {
                    error_count += 1;
                    println!("{}", line.red());
                } else if line.contains("warning") && (severity == "warning" || severity == "all") {
                    warning_count += 1;
                    println!("{}", line.yellow());
                }
            }
        }
    }

    println!();
    if error_count > 0 {
        println!(
            "{} {} error(s), {} warning(s)",
            "".red().bold(),
            error_count,
            warning_count
        );
        process::exit(1);
    } else if warning_count > 0 {
        println!("{} {} warning(s)", "".yellow().bold(), warning_count);
    } else {
        println!("{} No diagnostics found", "".green().bold());
    }

    Ok(())
}

fn comments_command(ai: bool, cached: bool, commit: bool, no_add: bool, files: &[String]) -> anyhow::Result<()> {
    use anyhow::Context;
    use colored::Colorize;
    use std::process::Command;
    use uira_comment_checker::{CommentDetector, FilterChain};

    println!("💬 Checking comments...\n");

    let files_to_check = if cached {
        let output = Command::new("git")
            .args(["diff", "--cached", "--name-only", "--diff-filter=ACMR"])
            .output()
            .context("Failed to get staged files")?;
        String::from_utf8_lossy(&output.stdout)
            .lines()
            .map(String::from)
            .collect::<Vec<_>>()
    } else if files.is_empty() {
        collect_files_from_cwd()?
    } else {
        files.to_vec()
    };

    if files_to_check.is_empty() {
        println!("{} No files to check", "".green());
        return Ok(());
    }

    if ai {
        let workflow_config = WorkflowConfig {
            auto_stage: !no_add,
            auto_commit: commit,
            cached_only: cached,
            files: files_to_check.clone(),
            ..Default::default()
        };

        return block_on(async {
            let mut workflow =
                AgentWorkflow::new(WorkflowTask::Comments, workflow_config, None, None).await?;
            match workflow.run().await? {
                WorkflowResult::Complete {
                    iterations,
                    files_modified,
                    summary,
                } => {
                    println!("\n✅ Comments workflow complete!");
                    println!("   Iterations: {}", iterations);
                    println!("   Files modified: {}", files_modified.len());
                    if let Some(s) = summary {
                        println!("   Summary: {}", s);
                    }
                    Ok(())
                }
                WorkflowResult::MaxIterationsReached { .. } => {
                    println!("\n⚠️  Max iterations reached");
                    std::process::exit(1);
                }
                WorkflowResult::VerificationFailed {
                    remaining_issues, ..
                } => {
                    println!("\n{} issues remain", remaining_issues);
                    std::process::exit(1);
                }
                WorkflowResult::Cancelled => {
                    println!("\n⚠️  Workflow cancelled");
                    std::process::exit(1);
                }
                WorkflowResult::Failed { error } => {
                    eprintln!("\n❌ Workflow failed: {}", error);
                    std::process::exit(1);
                }
            }
        });
    }

    let detector = CommentDetector::new();
    let filter_chain = FilterChain::new();
    let mut comment_count = 0;

    for file in &files_to_check {
        let content = match std::fs::read_to_string(file) {
            Ok(c) => c,
            Err(_) => continue,
        };

        let comments = detector.detect(&content, file, false);

        for comment in comments {
            if filter_chain.should_skip(&comment) {
                continue;
            }
            comment_count += 1;

            println!(
                "{}:{}: {}",
                file.dimmed(),
                comment.line_number,
                comment.text.trim().yellow()
            );
        }
    }

    println!();
    if comment_count > 0 {
        println!(
            "{} Found {} comment(s). Use --ai to analyze with AI.",
            "!".yellow().bold(),
            comment_count
        );
    } else {
        println!("{} No actionable comments found", "".green().bold());
    }

    Ok(())
}

async fn goals_command_async(action: GoalsCommands) -> anyhow::Result<()> {
    let cwd = std::env::current_dir()?;
    let config_path = cwd.join("uira.yml");

    if !config_path.exists() {
        anyhow::bail!(
            "Config file not found: uira.yml. Run 'uira-commit-hook-cli init' first or create a config with goals."
        );
    }

    let config = uira_core::load_config(Some(&config_path))?;
    let goals = &config.goals.goals;

    if goals.is_empty() {
        println!("{}", "No goals configured in uira.yml".yellow());
        println!();
        println!("Add goals to your config:");
        println!(
            r#"
goals:
  goals:
    - name: pixel-match
      workspace: .uira/goals/pixel-match/
      command: bun run check.ts
      target: 99.9
    - name: test-coverage
      command: "bun run coverage --json | jq '.total'"
      target: 80
"#
        );
        return Ok(());
    }

    match action {
        GoalsCommands::List => {
            println!("{}", "⚡ Configured Goals".bold());
            println!();

            for goal in goals {
                let (status, status_color) = if goal.enabled {
                    ("", "green")
                } else {
                    ("", "dimmed")
                };
                let status_display = if status_color == "green" {
                    status.green()
                } else {
                    status.dimmed()
                };
                println!(
                    "  {} {} (target: {:.1})",
                    status_display,
                    goal.name.bold(),
                    goal.target
                );
                if let Some(desc) = &goal.description {
                    println!("    {}", desc.dimmed());
                }
                println!("    Command: {}", goal.command.dimmed());
                if let Some(ws) = &goal.workspace {
                    println!("    Workspace: {}", ws.dimmed());
                }
                println!();
            }

            println!("{} {} goals configured", "Total:".bold(), goals.len());
            Ok(())
        }
        GoalsCommands::Check { name } => {
            let runner = uira_orchestration::GoalRunner::new(&cwd);

            let goals_to_check: Vec<_> = if let Some(ref n) = name {
                goals.iter().filter(|g| g.name == *n).cloned().collect()
            } else {
                goals.clone()
            };

            if goals_to_check.is_empty() {
                if let Some(n) = name {
                    anyhow::bail!("Goal '{}' not found", n);
                }
            }

            println!("{}", "⚡ Checking Goals".bold());
            println!();

            let result = runner.check_all(&goals_to_check).await;

            for r in &result.results {
                let status = if r.passed { "".green() } else { "".red() };
                println!(
                    "  {} {} {:.1}/{:.1} ({}ms)",
                    status,
                    r.name.bold(),
                    r.score,
                    r.target,
                    r.duration_ms
                );
                if let Some(err) = &r.error {
                    println!("    Error: {}", err.red());
                }
            }

            println!();
            if result.all_passed {
                println!("{}", "✅ All goals passed!".green().bold());
            } else {
                let passed = result.results.iter().filter(|r| r.passed).count();
                println!(
                    "{}",
                    format!("{}/{} goals passed", passed, result.results.len())
                        .red()
                        .bold()
                );
                process::exit(1);
            }
            Ok(())
        }
        GoalsCommands::Watch {
            interval,
            max_iterations,
        } => {
            let runner = uira_orchestration::GoalRunner::new(&cwd);

            println!("{}", "⚡ Watching Goals".bold());
            println!(
                "Checking every {}s (max {} iterations)",
                interval, max_iterations
            );
            println!();

            let options = uira_orchestration::hooks::VerifyOptions {
                check_interval_secs: interval,
                max_iterations,
                max_duration: None,
                on_progress: Some(Box::new(|result| {
                    println!(
                        "[Iteration {}] {}/{} goals passed",
                        result.iteration,
                        result.results.iter().filter(|r| r.passed).count(),
                        result.results.len()
                    );
                    for r in &result.results {
                        let status = if r.passed { "" } else { "" };
                        println!("  {} {}: {:.1}/{:.1}", status, r.name, r.score, r.target);
                    }
                    println!();
                })),
            };

            let result = runner.verify_until_complete(goals, options).await;

            if result.all_passed {
                println!("{}", "✅ All goals passed!".green().bold());
            } else {
                println!(
                    "{}",
                    format!(
                        "❌ Goals did not pass after {} iterations",
                        result.iteration
                    )
                    .red()
                    .bold()
                );
                process::exit(1);
            }
            Ok(())
        }
    }
}