repograph 0.4.0

Register, group, and expose local git repos as structured context for AI agents
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
//! `repograph init` — interactive setup command.
//!
//! Two modes:
//!
//! - **Non-interactive** (`--no-prompt --agents <list>`): validates the list,
//!   writes `[agents] selected = [...]` to config, returns. No cliclack UI.
//!   Required for CI / automation / non-TTY contexts.
//! - **Interactive** (default in a TTY): routes to either the first-run flow
//!   (no `[agents]` section yet) or the settings-panel flow (already
//!   configured). All UI via cliclack; stdout is never touched.
//!
//! ## Manual validation script
//!
//! Manual TTY scenarios — first run, full first run with repo + workspace,
//! settings panel, reset, etc. — are documented in
//! `openspec/changes/init-command/design.md` under "Manual Validation Script"
//! and must be walked before archiving the change.

use std::collections::BTreeSet;
use std::path::{Path, PathBuf};

use clap::Parser;
use repograph_core::agent_artifact::{
    self, ArtifactResult, has_artifact_writer, install_artifacts, scope_is_meaningful,
};
use repograph_core::{
    AgentId, Agents, Config, Repo, RepographError, validate_git_repo, validate_workspace_name,
};

use crate::prompt::{
    PROJECT_ROOT_ENV, detect_agents, discover_project_roots, effective_projects_root, host_home,
    path_suggestions, prompt_scope, scan_git_repos, select_agents_interactively, stdout_is_tty,
};

#[derive(Debug, Parser)]
pub struct Args {
    /// Comma-separated agent IDs to configure. Required with `--no-prompt`;
    /// optional in interactive mode (preselects but lets the user adjust).
    /// Valid IDs: `claude-code`, `agents-md`, `cursor`, `aider`, `windsurf`,
    /// `copilot`.
    #[arg(long, value_name = "LIST")]
    pub agents: Option<String>,

    /// Skip the interactive flow entirely. Requires `--agents`. Useful for CI,
    /// dotfile bootstrapping, and non-TTY contexts.
    #[arg(long, requires = "agents")]
    pub no_prompt: bool,

    /// Where to install agent artifacts. Defaults to `user` when omitted;
    /// required under `--no-prompt` when any selected agent has a meaningful
    /// scope choice (i.e. its user and project paths differ — today that's
    /// `claude-code` and `windsurf`). Project-only agents silently use the
    /// project path regardless of this flag.
    #[arg(long, value_parser = parse_scope, value_name = "SCOPE")]
    pub scope: Option<agent_artifact::Scope>,

    /// Overwrite existing artifacts even outside the managed delimiter block.
    /// Without this flag, repograph rewrites only the delimited region of
    /// pre-existing files (preserving user content); with it, the file is
    /// replaced fresh.
    #[arg(long)]
    pub force: bool,
}

/// Parse the `--scope` CLI value. Mirrors `Scope`'s serde lowercase rendering
/// so the user-facing surface is the same on the wire and on the command line.
fn parse_scope(s: &str) -> Result<agent_artifact::Scope, String> {
    match s {
        "user" => Ok(agent_artifact::Scope::User),
        "project" => Ok(agent_artifact::Scope::Project),
        other => Err(format!(
            "invalid scope '{other}', expected `user` or `project`"
        )),
    }
}

/// Entry point dispatched from `main.rs`.
///
/// # Errors
///
/// Propagates [`RepographError`] for the documented failure modes. See the
/// `init-command` spec for the full table of scenarios.
#[tracing::instrument(skip(args, config_dir), fields(
    no_prompt = args.no_prompt,
    agents_flag = args.agents.as_deref().unwrap_or("<none>"),
    scope = ?args.scope,
    force = args.force,
    config_dir = %config_dir.display(),
))]
pub fn run(args: &Args, config_dir: &Path) -> Result<(), RepographError> {
    tracing::debug!("init: start");

    let mut config = Config::load(config_dir)?;

    if args.no_prompt {
        run_non_interactive(args, &mut config, config_dir)?;
        tracing::info!("init: completed (non-interactive)");
        return Ok(());
    }

    if !stdout_is_tty() {
        tracing::warn!("init: non-TTY without --no-prompt");
        return Err(RepographError::NeedsInit(
            "stdout is not a TTY; pass `--no-prompt --agents <list>` to run \
             `repograph init` non-interactively, or invoke it in an interactive shell"
                .to_string(),
        ));
    }

    if config.agents().is_some() {
        run_settings_panel(args, &mut config, config_dir)?;
    } else {
        run_first_run(args, &mut config, config_dir)?;
    }
    tracing::info!("init: completed (interactive)");
    Ok(())
}

/// Does the selection contain at least one agent for which `Scope::User` and
/// `Scope::Project` resolve to different paths? If yes, `--scope` must be
/// explicit under `--no-prompt`.
fn requires_scope(selected: &[AgentId]) -> bool {
    selected
        .iter()
        .any(|&a| has_artifact_writer(a) && scope_is_meaningful(a))
}

/// Comma-joined `as_str()` form of the agents in `selected` for which
/// `scope_is_meaningful` is true. Used to name the offending agents in the
/// `--no-prompt`/`--scope` validation error.
fn scope_bearing_names(selected: &[AgentId]) -> String {
    selected
        .iter()
        .filter(|&&a| has_artifact_writer(a) && scope_is_meaningful(a))
        .map(AgentId::as_str)
        .collect::<Vec<_>>()
        .join(", ")
}

/// Resolve the install scope for an interactive run. If the user passed
/// `--scope` on the command line, use that. Otherwise, if at least one
/// selected agent has a meaningful scope choice, prompt; if not, default to
/// `User` (which falls through to project for project-only agents anyway).
fn resolve_scope_interactive(
    args: &Args,
    selected: &[AgentId],
) -> Result<agent_artifact::Scope, RepographError> {
    if let Some(s) = args.scope {
        return Ok(s);
    }
    if !requires_scope(selected) {
        return Ok(agent_artifact::Scope::User);
    }
    let home = host_home().unwrap_or_else(|| PathBuf::from("~"));
    let cwd = std::env::current_dir().map_err(RepographError::Io)?;
    prompt_scope(&home, &cwd)
}

/// Install per-agent artifacts for `selected` and log per-result outcomes on
/// stderr. No-op if no agent in the selection has a writer.
///
/// Returns `Err` only for failures that prevent the install from running at
/// all (e.g. `current_dir()` failed, or `--scope user` was chosen but the
/// host has no home directory). Per-agent install failures are captured as
/// [`ArtifactResult::Failed`] and surfaced via `warn!`; they do NOT abort the
/// command's exit code.
fn run_install(
    selected: &[AgentId],
    scope: agent_artifact::Scope,
    force: bool,
) -> Result<(), RepographError> {
    if !selected.iter().any(|&a| has_artifact_writer(a)) {
        return Ok(());
    }
    let cwd = std::env::current_dir().map_err(RepographError::Io)?;
    let home = match host_home() {
        Some(h) => h,
        None if scope == agent_artifact::Scope::Project => cwd.clone(),
        None => {
            return Err(RepographError::UsageError(
                "could not determine home directory for `--scope user`; \
                 pass `--scope project` or set `HOME` in the environment"
                    .into(),
            ));
        }
    };
    let results = install_artifacts(selected, scope, &home, &cwd, force);
    log_install_results(&results);
    Ok(())
}

/// Emit one `tracing` line per install result on stderr per the logging
/// contract. `Failed` results use `warn!`; everything else `info!`.
fn log_install_results(results: &[ArtifactResult]) {
    for r in results {
        match r {
            ArtifactResult::Written {
                agent,
                capability,
                path,
            } => {
                tracing::info!(
                    agent = agent.as_str(),
                    capability = capability.skill_name(),
                    path = %path.display(),
                    "artifact written",
                );
            }
            ArtifactResult::Unchanged {
                agent,
                capability,
                path,
            } => {
                tracing::info!(
                    agent = agent.as_str(),
                    capability = capability.skill_name(),
                    path = %path.display(),
                    "artifact unchanged",
                );
            }
            ArtifactResult::Skipped { agent, reason } => {
                tracing::info!(agent = agent.as_str(), reason = *reason, "artifact skipped",);
            }
            ArtifactResult::Failed {
                agent,
                capability,
                error,
            } => {
                tracing::warn!(
                    agent = agent.as_str(),
                    capability = capability.skill_name(),
                    err = ?error,
                    "artifact failed",
                );
            }
        }
    }
}

fn run_non_interactive(
    args: &Args,
    config: &mut Config,
    config_dir: &Path,
) -> Result<(), RepographError> {
    // clap's `requires = "agents"` on `--no-prompt` guarantees `args.agents`
    // is `Some` at this point. The empty-string case is treated as "no
    // selection" (a valid configured-but-empty state).
    let list = args.agents.as_deref().unwrap_or("");
    let selected = parse_agent_list(list)?;

    if requires_scope(&selected) && args.scope.is_none() {
        return Err(RepographError::UsageError(format!(
            "--scope must be explicit under --no-prompt when selected agents include \
             {names}; pass `--scope user` or `--scope project`",
            names = scope_bearing_names(&selected),
        )));
    }

    config.set_agents(Some(Agents {
        selected: selected.clone(),
    }));
    config.save(config_dir)?;

    let scope = args.scope.unwrap_or(agent_artifact::Scope::User);
    run_install(&selected, scope, args.force)?;
    Ok(())
}

/// Parse the comma-separated `--agents` string into a `Vec<AgentId>`.
/// Empty input yields an empty vector (a valid configured-but-empty state).
/// Whitespace around entries is trimmed. Unknown IDs propagate as
/// `RepographError::InvalidName` (exit `2`).
fn parse_agent_list(list: &str) -> Result<Vec<AgentId>, RepographError> {
    if list.trim().is_empty() {
        return Ok(Vec::new());
    }
    let mut out = Vec::new();
    let mut seen = BTreeSet::new();
    for raw in list.split(',') {
        let token = raw.trim();
        if token.is_empty() {
            continue;
        }
        let id = AgentId::parse(token)?;
        if seen.insert(id) {
            out.push(id);
        }
    }
    Ok(out)
}

fn run_first_run(
    args: &Args,
    config: &mut Config,
    config_dir: &Path,
) -> Result<(), RepographError> {
    cliclack::intro("repograph init").map_err(RepographError::Io)?;
    cliclack::note(
        "Welcome",
        "Let's get repograph set up for your agent toolchain.",
    )
    .map_err(RepographError::Io)?;

    // Detection seed + optional --agents preselect override.
    let mut preselected = detect_agents(host_home().as_deref());
    if let Some(list) = args.agents.as_deref() {
        let parsed = parse_agent_list(list)?;
        preselected.extend(parsed);
    }

    let selected = select_agents_interactively(&preselected)?;
    // Resolve install scope before the existing project-root flow so the user
    // makes all the agent-related decisions up front.
    let scope = resolve_scope_interactive(args, &selected)?;

    config.set_agents(Some(Agents {
        selected: selected.clone(),
    }));
    config.save(config_dir)?;

    run_install(&selected, scope, args.force)?;

    // One-time project-root setup. Stored persistently so future repo
    // registrations and the future `context` command can use it.
    pick_projects_root_step(config, config_dir)?;

    // Optional repo registration + bulk workspace assignment.
    let registered = maybe_register_repos(config, config_dir)?;
    maybe_assign_repos_to_workspaces(config, config_dir, &registered)?;

    finish_outro(config, &selected)
}

/// First-run step asking "Where do you keep your projects?" and persisting
/// the answer to `[settings] projects_root`. Skip-able. No-ops when the
/// effective value is already known (env var or pre-existing config).
fn pick_projects_root_step(config: &mut Config, config_dir: &Path) -> Result<(), RepographError> {
    if effective_projects_root(config).is_some() {
        return Ok(());
    }
    ask_and_store_projects_root(config, config_dir)
}

/// Render the projects-root prompt unconditionally and persist the result.
/// Used by the first-run flow (gated by [`pick_projects_root_step`]) and by
/// the settings-panel "Change project root" action (which always wants to
/// ask, regardless of current state).
///
/// Detected candidates (existing directories under `$HOME` that contain at
/// least one git repo) are surfaced as primary options; the user can also
/// type a custom path or skip. The answer is written to
/// `[settings] projects_root`.
enum ProjectsRootChoice {
    Use(PathBuf),
    Custom,
    Skip,
}

fn ask_and_store_projects_root(
    config: &mut Config,
    config_dir: &Path,
) -> Result<(), RepographError> {
    let detected = discover_project_roots(host_home().as_deref());

    let mut select = cliclack::select::<usize>("Where do you keep your projects?");
    let mut choices: Vec<ProjectsRootChoice> = Vec::new();
    for root in &detected {
        let count = scan_git_repos(root).len();
        let label = format!("{}  ({count} repos)", root.display());
        choices.push(ProjectsRootChoice::Use(root.clone()));
        select = select.item(choices.len() - 1, label, "");
    }
    choices.push(ProjectsRootChoice::Custom);
    select = select.item(choices.len() - 1, "Enter a custom path...", "");
    choices.push(ProjectsRootChoice::Skip);
    select = select.item(
        choices.len() - 1,
        "Skip — I'll set this later",
        "change anytime from `repograph init`",
    );

    let idx = select.interact().map_err(RepographError::Io)?;
    let new_root: Option<PathBuf> = match &choices[idx] {
        ProjectsRootChoice::Use(p) => Some(p.clone()),
        ProjectsRootChoice::Custom => {
            let raw: String = cliclack::input("Project root path")
                .placeholder("/home/you/code  (Tab to autocomplete)")
                .autocomplete(path_suggestions)
                .interact()
                .map_err(RepographError::Io)?;
            let trimmed = raw.trim();
            if trimmed.is_empty() {
                None
            } else {
                let path = PathBuf::from(trimmed);
                if !path.is_dir() {
                    cliclack::log::warning(format!(
                        "{} does not currently exist — stored anyway, will be \
                         checked next time it's used",
                        path.display()
                    ))
                    .map_err(RepographError::Io)?;
                }
                Some(path)
            }
        }
        ProjectsRootChoice::Skip => None,
    };

    let mut settings = config.settings().cloned().unwrap_or_default();
    settings.projects_root = new_root;
    // Persist the section even when the chosen value is None, so the
    // section's presence records "the user has answered this question."
    config.set_settings(Some(settings));
    config.save(config_dir)?;
    Ok(())
}

fn run_settings_panel(
    args: &Args,
    config: &mut Config,
    config_dir: &Path,
) -> Result<(), RepographError> {
    cliclack::intro("repograph init").map_err(RepographError::Io)?;
    let current = config
        .agents()
        .map(|a| a.selected.clone())
        .unwrap_or_default();
    let root_label = effective_projects_root(config)
        .map_or_else(|| "(not set)".to_string(), |p| p.display().to_string());
    let summary = format!(
        "agents:        {}\nprojects root: {}\nrepos:         {}\nworkspaces:    {}",
        if current.is_empty() {
            "(none)".to_string()
        } else {
            current
                .iter()
                .map(AgentId::as_str)
                .collect::<Vec<_>>()
                .join(", ")
        },
        root_label,
        config.repos().len(),
        config.workspaces().len(),
    );
    cliclack::note("Current configuration", summary).map_err(RepographError::Io)?;

    loop {
        let action: SettingsAction = cliclack::select("What would you like to do?")
            .item(SettingsAction::UpdateAgents, "Update agent selection", "")
            .item(SettingsAction::ChangeProjectRoot, "Change project root", "")
            .item(SettingsAction::AddRepo, "Register another repo", "")
            .item(SettingsAction::ManageWorkspaces, "Manage workspaces", "")
            .item(SettingsAction::Reset, "Reset everything", "destructive")
            .item(SettingsAction::Cancel, "Cancel", "")
            .interact()
            .map_err(RepographError::Io)?;

        match action {
            SettingsAction::ChangeProjectRoot => {
                change_project_root(config, config_dir)?;
            }
            SettingsAction::UpdateAgents => {
                let current_set: BTreeSet<AgentId> = config
                    .agents()
                    .map(|a| a.selected.iter().copied().collect())
                    .unwrap_or_default();
                let selected = select_agents_interactively(&current_set)?;
                let scope = resolve_scope_interactive(args, &selected)?;
                config.set_agents(Some(Agents {
                    selected: selected.clone(),
                }));
                config.save(config_dir)?;
                run_install(&selected, scope, args.force)?;
                cliclack::log::success(format!(
                    "agents updated → {}",
                    if selected.is_empty() {
                        "(none)".to_string()
                    } else {
                        selected
                            .iter()
                            .map(AgentId::as_str)
                            .collect::<Vec<_>>()
                            .join(", ")
                    }
                ))
                .map_err(RepographError::Io)?;
            }
            SettingsAction::AddRepo => {
                let registered = register_repos_step(config, config_dir)?;
                maybe_assign_repos_to_workspaces(config, config_dir, &registered)?;
            }
            SettingsAction::ManageWorkspaces => {
                manage_workspaces(config, config_dir)?;
            }
            SettingsAction::Reset => {
                let confirm =
                    cliclack::confirm("Reset everything? This deletes all configuration.")
                        .initial_value(false)
                        .interact()
                        .map_err(RepographError::Io)?;
                if confirm {
                    *config = Config::default();
                    config.save(config_dir)?;
                    cliclack::outro("repograph reset — all configuration cleared")
                        .map_err(RepographError::Io)?;
                    return Ok(());
                }
            }
            SettingsAction::Cancel => {
                cliclack::outro("no changes").map_err(RepographError::Io)?;
                return Ok(());
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SettingsAction {
    UpdateAgents,
    ChangeProjectRoot,
    AddRepo,
    ManageWorkspaces,
    Reset,
    Cancel,
}

/// Settings-panel action: prompt the user for a new projects root and
/// persist it. Always renders the prompt (unlike the first-run step, which
/// short-circuits on an existing value). When `REPOGRAPH_PROJECT_ROOT` is
/// active, warns the user that the env var will override whatever they pick.
fn change_project_root(config: &mut Config, config_dir: &Path) -> Result<(), RepographError> {
    if std::env::var_os(PROJECT_ROOT_ENV)
        .as_ref()
        .is_some_and(|v| !v.is_empty())
    {
        cliclack::log::warning(format!(
            "{PROJECT_ROOT_ENV} is set in the environment; it overrides whatever \
             you pick here. Unset it to make the stored value effective."
        ))
        .map_err(RepographError::Io)?;
    }
    if let Some(path) = config.settings().and_then(|s| s.projects_root.as_ref()) {
        cliclack::log::info(format!("current stored value: {}", path.display()))
            .map_err(RepographError::Io)?;
    }
    ask_and_store_projects_root(config, config_dir)
}

/// First-run gate for the repo-registration step. Asks the outer
/// "Register repos now?" confirm so the user can decline the whole phase;
/// on `yes`, delegates to [`register_repos_step`].
fn maybe_register_repos(
    config: &mut Config,
    config_dir: &Path,
) -> Result<Vec<String>, RepographError> {
    let yes = cliclack::confirm("Register repos now?")
        .initial_value(false)
        .interact()
        .map_err(RepographError::Io)?;
    if !yes {
        return Ok(Vec::new());
    }
    register_repos_step(config, config_dir)
}

/// Two-phase repo registration:
///
/// 1. If a projects root is effective and has unregistered git-repo
///    children, render a `multiselect` of them (no preselection). Each pick
///    registers via [`bulk_register_path`], which uses the directory basename
///    silently on the first attempt and prompts once for an alternative name
///    on a conflict.
/// 2. Then offer a `"Register a repo at a custom path?"` confirm loop with a
///    free-form input (autocomplete) for paths outside the projects root.
///
/// Returns the names of every repo successfully registered in this step so
/// the caller can route them to the bulk-workspace assignment.
fn register_repos_step(
    config: &mut Config,
    config_dir: &Path,
) -> Result<Vec<String>, RepographError> {
    let mut registered: Vec<String> = Vec::new();
    let mut had_multiselect = false;

    if let Some(root) = effective_projects_root(config) {
        let already: BTreeSet<PathBuf> = config.repos().values().map(|r| r.path.clone()).collect();
        let candidates: Vec<PathBuf> = scan_git_repos(&root)
            .into_iter()
            .filter(|p| !already.contains(p))
            .collect();
        if !candidates.is_empty() {
            had_multiselect = true;
            let picked = pick_multiple_from_candidates(&root, &candidates)?;
            for path in picked {
                if let Some(name) = bulk_register_path(config, config_dir, &path)? {
                    registered.push(name);
                }
            }
        }
    }

    // Custom-path loop. When the multiselect didn't render (no candidates,
    // no root, all candidates already registered) we skip the first confirm
    // so the user doesn't have to say "yes" twice for a single repo.
    let mut skip_first_confirm = !had_multiselect;
    loop {
        if !skip_first_confirm {
            let prompt = if registered.is_empty() {
                "Register a repo at a custom path?"
            } else {
                "Register another repo at a custom path?"
            };
            let yes = cliclack::confirm(prompt)
                .initial_value(false)
                .interact()
                .map_err(RepographError::Io)?;
            if !yes {
                break;
            }
        }
        skip_first_confirm = false;

        let path = free_form_path_input()?;
        if let Some(name) = interactive_register_path(config, config_dir, &path)? {
            registered.push(name);
        }
    }

    Ok(registered)
}

fn pick_multiple_from_candidates(
    root: &Path,
    candidates: &[PathBuf],
) -> Result<Vec<PathBuf>, RepographError> {
    let mut multi = cliclack::multiselect::<PathBuf>(format!("Repositories in {}", root.display()))
        .required(false);
    for repo in candidates {
        let name = repo
            .file_name()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_default();
        multi = multi.item(repo.clone(), name, "");
    }
    multi.interact().map_err(RepographError::Io)
}

/// Bulk-register a scanned path with its basename as the name. First attempt
/// is silent (no prompt); on a `Conflict` we prompt once for an alternative
/// name. Persistent failures log a warning and skip the path so the rest of
/// the batch can proceed. Returns the registered name on success.
fn bulk_register_path(
    config: &mut Config,
    config_dir: &Path,
    path: &Path,
) -> Result<Option<String>, RepographError> {
    let default_name = path
        .file_name()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_default();
    if default_name.is_empty() {
        cliclack::log::warning(format!(
            "skipping {}: cannot derive a name from the path",
            path.display()
        ))
        .map_err(RepographError::Io)?;
        return Ok(None);
    }

    let first_attempt = config.add_repo(
        default_name.clone(),
        Repo {
            path: path.to_path_buf(),
            description: None,
            stack: vec![],
        },
    );
    match first_attempt {
        Ok(()) => {
            config.save(config_dir)?;
            cliclack::log::success(format!("registered '{default_name}' → {}", path.display()))
                .map_err(RepographError::Io)?;
            return Ok(Some(default_name));
        }
        Err(RepographError::Conflict { kind, name }) => {
            cliclack::log::warning(format!(
                "{} conflict on {}: '{name}' already registered",
                kind,
                path.display()
            ))
            .map_err(RepographError::Io)?;
        }
        Err(e) => {
            cliclack::log::warning(format!("skipping {}: {e}", path.display()))
                .map_err(RepographError::Io)?;
            return Ok(None);
        }
    }

    // One-shot alternative-name prompt.
    let alt_input: String = cliclack::input(format!(
        "Different name for {}? (leave empty to skip)",
        path.display()
    ))
    .default_input(&default_name)
    .interact()
    .map_err(RepographError::Io)?;
    let alt_name = alt_input.trim().to_string();
    if alt_name.is_empty() || alt_name == default_name {
        cliclack::log::warning(format!("skipped {}", path.display()))
            .map_err(RepographError::Io)?;
        return Ok(None);
    }

    match config.add_repo(
        alt_name.clone(),
        Repo {
            path: path.to_path_buf(),
            description: None,
            stack: vec![],
        },
    ) {
        Ok(()) => {
            config.save(config_dir)?;
            cliclack::log::success(format!("registered '{alt_name}' → {}", path.display()))
                .map_err(RepographError::Io)?;
            Ok(Some(alt_name))
        }
        Err(e) => {
            cliclack::log::error(format!("skipped {}: {e}", path.display()))
                .map_err(RepographError::Io)?;
            Ok(None)
        }
    }
}

/// Interactive registration for a user-typed free-form path. Always prompts
/// for a name (default = basename); loops on conflict / validation failure
/// so the user can correct it. The outer caller already validated the path
/// via [`free_form_path_input`] → `validate_git_repo`, so failures here are
/// almost always name conflicts.
fn interactive_register_path(
    config: &mut Config,
    config_dir: &Path,
    path: &Path,
) -> Result<Option<String>, RepographError> {
    let default_name = path
        .file_name()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_default();
    let mut current_default = default_name;
    loop {
        let name: String = cliclack::input("Name")
            .default_input(&current_default)
            .interact()
            .map_err(RepographError::Io)?;
        let name = name.trim().to_string();
        if name.is_empty() {
            cliclack::log::warning(format!("skipped {}", path.display()))
                .map_err(RepographError::Io)?;
            return Ok(None);
        }
        let repo = Repo {
            path: path.to_path_buf(),
            description: None,
            stack: vec![],
        };
        match config.add_repo(name.clone(), repo) {
            Ok(()) => {
                config.save(config_dir)?;
                cliclack::log::success(format!("registered '{name}' → {}", path.display()))
                    .map_err(RepographError::Io)?;
                return Ok(Some(name));
            }
            Err(e) => {
                cliclack::log::error(e.to_string()).map_err(RepographError::Io)?;
                current_default = name;
                // Loop and re-prompt the name.
            }
        }
    }
}

fn free_form_path_input() -> Result<PathBuf, RepographError> {
    loop {
        let raw: String = cliclack::input("Path to repository")
            .placeholder("/home/you/code/your-repo  (Tab to autocomplete)")
            .autocomplete(path_suggestions)
            .interact()
            .map_err(RepographError::Io)?;
        let path = Path::new(raw.trim());
        match validate_git_repo(path) {
            Ok(p) => return Ok(p),
            Err(e) => {
                cliclack::log::error(e.to_string()).map_err(RepographError::Io)?;
                // Loop and re-prompt.
            }
        }
    }
}

/// After repo registration, route each newly-registered repo into zero or
/// more workspaces of the user's choosing. The flow has two phases:
///
/// 1. **Workspace prep** — an optional create-new loop runs first, gated by
///    a `Create new workspaces first?` confirm when at least one workspace
///    already exists, or entered directly when none do (the outer confirm
///    already signalled intent). This seeds the target pool with anything
///    the user wants to assign into below.
/// 2. **Per-repo assignment** — for each repo in `repo_names`, render a
///    `multiselect` over the full workspace set (existing + just-created),
///    no preselection. The user picks the workspaces *that repo* should
///    join. Empty submissions are valid: that repo stays unassigned.
///
/// Each pick triggers a `Config::add_members(ws, &[repo])` call; the whole
/// step persists with a single `Config::save` at the end so partial failure
/// can't leave a half-written membership graph on disk.
///
/// No-op when `repo_names` is empty. Skipped entirely if the outer confirm
/// is declined. Final summary log enumerates the per-repo assignments.
fn maybe_assign_repos_to_workspaces(
    config: &mut Config,
    config_dir: &Path,
    repo_names: &[String],
) -> Result<(), RepographError> {
    if repo_names.is_empty() {
        return Ok(());
    }

    let n = repo_names.len();
    let outer_prompt = if n == 1 {
        format!("Add '{}' to workspaces?", repo_names[0])
    } else {
        format!("Add these {n} repos to workspaces?")
    };
    let yes = cliclack::confirm(outer_prompt)
        .initial_value(false)
        .interact()
        .map_err(RepographError::Io)?;
    if !yes {
        return Ok(());
    }

    // Phase 1: optional create-new loop, so phase 2 has targets to pick from.
    // If the registry already has workspaces, the user opts in via a confirm;
    // otherwise we enter the loop directly because there's no other path to
    // a target.
    let create_new = if config.workspaces().is_empty() {
        true
    } else {
        cliclack::confirm("Create new workspaces first?")
            .initial_value(false)
            .interact()
            .map_err(RepographError::Io)?
    };
    if create_new {
        loop {
            let ws_name = prompt_workspace_name(config)?;
            config.create_workspace(ws_name.clone(), None)?;
            let another = cliclack::confirm("Create another workspace?")
                .initial_value(false)
                .interact()
                .map_err(RepographError::Io)?;
            if !another {
                break;
            }
        }
    }

    // Phase 2: per-repo workspace assignment. The target pool is the full
    // registry of workspaces (existing + just-created). It cannot be empty
    // here: if it started empty, phase 1 forced create-new and the prompt
    // loop blocks until a valid workspace name lands.
    let all_ws: Vec<String> = config.workspaces().keys().cloned().collect();

    let mut assignments: Vec<(String, Vec<String>)> = Vec::new();
    for repo_name in repo_names {
        let mut multi = cliclack::multiselect::<String>(format!("Workspaces for '{repo_name}'"))
            .required(false);
        for ws in &all_ws {
            multi = multi.item(ws.clone(), ws.clone(), "");
        }
        let picked: Vec<String> = multi.interact().map_err(RepographError::Io)?;
        if picked.is_empty() {
            continue;
        }
        for ws in &picked {
            config.add_members(ws, std::slice::from_ref(repo_name))?;
        }
        assignments.push((repo_name.clone(), picked));
    }

    config.save(config_dir)?;

    if assignments.is_empty() {
        cliclack::log::info("no workspace assignments made").map_err(RepographError::Io)?;
    } else if assignments.len() == 1 && assignments[0].1.len() == 1 {
        let (repo, wss) = &assignments[0];
        cliclack::log::success(format!("added '{repo}' to '{}'", wss[0]))
            .map_err(RepographError::Io)?;
    } else {
        let lines: Vec<String> = assignments
            .iter()
            .map(|(repo, wss)| format!("  {repo}{}", wss.join(", ")))
            .collect();
        cliclack::log::success(format!("workspace assignments:\n{}", lines.join("\n")))
            .map_err(RepographError::Io)?;
    }
    Ok(())
}

fn prompt_workspace_name(config: &Config) -> Result<String, RepographError> {
    loop {
        let raw: String = cliclack::input("Workspace name (lowercase, kebab-case)")
            .interact()
            .map_err(RepographError::Io)?;
        let name = raw.trim().to_string();
        if let Err(e) = validate_workspace_name(&name) {
            cliclack::log::error(e.to_string()).map_err(RepographError::Io)?;
            continue;
        }
        if config.workspaces().contains_key(&name) {
            cliclack::log::error(format!("workspace '{name}' already exists"))
                .map_err(RepographError::Io)?;
            continue;
        }
        return Ok(name);
    }
}

fn manage_workspaces(config: &mut Config, config_dir: &Path) -> Result<(), RepographError> {
    let action = cliclack::select::<WsAction>("Workspaces")
        .item(WsAction::Create, "Create", "")
        .item(WsAction::AddMembers, "Add members", "")
        .item(WsAction::RemoveMembers, "Remove members", "")
        .item(WsAction::Delete, "Delete workspace", "")
        .item(WsAction::Back, "Back", "")
        .interact()
        .map_err(RepographError::Io)?;

    match action {
        WsAction::Create => {
            let name = prompt_workspace_name(config)?;
            config.create_workspace(name.clone(), None)?;
            config.save(config_dir)?;
            cliclack::log::success(format!("workspace '{name}' created"))
                .map_err(RepographError::Io)?;
            if !config.repos().is_empty() {
                let yes = cliclack::confirm(format!("Add repos to '{name}' now?"))
                    .initial_value(true)
                    .interact()
                    .map_err(RepographError::Io)?;
                if yes {
                    add_repos_to_workspace(config, config_dir, &name)?;
                }
            }
            Ok(())
        }
        WsAction::AddMembers => {
            let Some(ws_name) = pick_existing_workspace(config, "Add members to")? else {
                return Ok(());
            };
            add_repos_to_workspace(config, config_dir, &ws_name)
        }
        WsAction::RemoveMembers => {
            let Some(ws_name) = pick_existing_workspace(config, "Remove members from")? else {
                return Ok(());
            };
            let members = config
                .resolve_workspace(&ws_name)?
                .0
                .into_iter()
                .map(|(name, _)| name.clone())
                .collect::<Vec<_>>();
            if members.is_empty() {
                cliclack::log::warning(format!("workspace '{ws_name}' has no live members"))
                    .map_err(RepographError::Io)?;
                return Ok(());
            }
            let mut sel = cliclack::select::<String>("Repo to remove");
            for m in &members {
                sel = sel.item(m.clone(), m.clone(), "");
            }
            let repo = sel.interact().map_err(RepographError::Io)?;
            config.remove_members(&ws_name, std::slice::from_ref(&repo))?;
            config.save(config_dir)?;
            cliclack::log::success(format!("removed '{repo}' from '{ws_name}'"))
                .map_err(RepographError::Io)?;
            Ok(())
        }
        WsAction::Delete => {
            let Some(ws_name) = pick_existing_workspace(config, "Delete workspace")? else {
                return Ok(());
            };
            let confirm = cliclack::confirm(format!("Delete workspace '{ws_name}'?"))
                .initial_value(false)
                .interact()
                .map_err(RepographError::Io)?;
            if confirm {
                config.remove_workspace(&ws_name)?;
                config.save(config_dir)?;
                cliclack::log::success(format!("workspace '{ws_name}' deleted"))
                    .map_err(RepographError::Io)?;
            }
            Ok(())
        }
        WsAction::Back => Ok(()),
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WsAction {
    Create,
    AddMembers,
    RemoveMembers,
    Delete,
    Back,
}

fn pick_existing_workspace(
    config: &Config,
    prompt: &str,
) -> Result<Option<String>, RepographError> {
    if config.workspaces().is_empty() {
        cliclack::log::warning("no workspaces registered yet").map_err(RepographError::Io)?;
        return Ok(None);
    }
    let mut select = cliclack::select::<String>(prompt);
    for ws in config.workspaces().keys() {
        select = select.item(ws.clone(), ws.clone(), "");
    }
    Ok(Some(select.interact().map_err(RepographError::Io)?))
}

/// Bulk-add zero or more registered repos to a single existing workspace
/// via a `multiselect`. Repos already in the workspace are filtered out so
/// the picker only shows actionable candidates. Empty submissions are a
/// no-op (no save, no log). Singular wording when exactly one repo lands.
fn add_repos_to_workspace(
    config: &mut Config,
    config_dir: &Path,
    ws_name: &str,
) -> Result<(), RepographError> {
    let current: BTreeSet<String> = config
        .resolve_workspace(ws_name)?
        .0
        .into_iter()
        .map(|(name, _)| name.clone())
        .collect();
    let candidates: Vec<String> = config
        .repos()
        .keys()
        .filter(|n| !current.contains(n.as_str()))
        .cloned()
        .collect();
    if candidates.is_empty() {
        let reason = if config.repos().is_empty() {
            "no repos registered yet"
        } else {
            "all registered repos are already members"
        };
        cliclack::log::warning(format!("{reason} — '{ws_name}' unchanged"))
            .map_err(RepographError::Io)?;
        return Ok(());
    }
    let mut multi =
        cliclack::multiselect::<String>(format!("Repos to add to '{ws_name}'")).required(false);
    for n in &candidates {
        multi = multi.item(n.clone(), n.clone(), "");
    }
    let picked: Vec<String> = multi.interact().map_err(RepographError::Io)?;
    if picked.is_empty() {
        return Ok(());
    }
    config.add_members(ws_name, &picked)?;
    config.save(config_dir)?;
    let n = picked.len();
    let msg = if n == 1 {
        format!("added '{}' to '{ws_name}'", picked[0])
    } else {
        format!("added {n} repos to '{ws_name}'")
    };
    cliclack::log::success(msg).map_err(RepographError::Io)?;
    Ok(())
}

fn finish_outro(config: &Config, selected: &[AgentId]) -> Result<(), RepographError> {
    let agents_line = if selected.is_empty() {
        "(none)".to_string()
    } else {
        selected
            .iter()
            .map(AgentId::as_str)
            .collect::<Vec<_>>()
            .join(", ")
    };
    let summary = format!(
        "agents:     {agents_line}\nrepos:      {}\nworkspaces: {}\n\nNext:\n  repograph status\n  repograph context  (coming soon)",
        config.repos().len(),
        config.workspaces().len(),
    );
    cliclack::outro_note("Setup complete", summary).map_err(RepographError::Io)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;

    #[test]
    fn parse_empty_list_returns_empty_vec() {
        assert!(parse_agent_list("").unwrap().is_empty());
        assert!(parse_agent_list("   ").unwrap().is_empty());
    }

    #[test]
    fn parse_single_id() {
        let v = parse_agent_list("claude-code").unwrap();
        assert_eq!(v, vec![AgentId::ClaudeCode]);
    }

    #[test]
    fn parse_multiple_preserves_order() {
        let v = parse_agent_list("cursor,claude-code,agents-md").unwrap();
        assert_eq!(
            v,
            vec![AgentId::Cursor, AgentId::ClaudeCode, AgentId::AgentsMd]
        );
    }

    #[test]
    fn parse_trims_whitespace() {
        let v = parse_agent_list("  claude-code , cursor  ").unwrap();
        assert_eq!(v, vec![AgentId::ClaudeCode, AgentId::Cursor]);
    }

    #[test]
    fn parse_dedupes_while_preserving_first_occurrence() {
        let v = parse_agent_list("cursor,cursor,claude-code").unwrap();
        assert_eq!(v, vec![AgentId::Cursor, AgentId::ClaudeCode]);
    }

    #[test]
    fn parse_unknown_id_errors() {
        let err = parse_agent_list("claude-code,bogus").unwrap_err();
        match err {
            RepographError::InvalidName { kind, name, .. } => {
                assert_eq!(kind, "agent");
                assert_eq!(name, "bogus");
            }
            other => panic!("expected InvalidName, got {other:?}"),
        }
        assert_eq!(
            RepographError::InvalidName {
                kind: "agent",
                name: "bogus".into(),
                reason: "x"
            }
            .exit_code(),
            2
        );
    }

    #[test]
    fn requires_scope_predicate_matches_matrix() {
        assert!(requires_scope(&[AgentId::ClaudeCode]));
        assert!(requires_scope(&[AgentId::Windsurf]));
        assert!(requires_scope(&[AgentId::ClaudeCode, AgentId::AgentsMd]));
        assert!(!requires_scope(&[AgentId::AgentsMd]));
        assert!(!requires_scope(&[AgentId::Cursor]));
        assert!(!requires_scope(&[AgentId::Aider]));
        assert!(!requires_scope(&[AgentId::Copilot]));
        assert!(!requires_scope(&[]));
    }

    #[test]
    fn scope_bearing_names_lists_only_meaningful_agents() {
        let names = scope_bearing_names(&[
            AgentId::AgentsMd,
            AgentId::ClaudeCode,
            AgentId::Cursor,
            AgentId::Windsurf,
        ]);
        assert!(names.contains("claude-code"));
        assert!(names.contains("windsurf"));
        assert!(!names.contains("agents-md"));
        assert!(!names.contains("cursor"));
    }

    #[test]
    fn parse_scope_accepts_user_and_project() {
        assert_eq!(parse_scope("user").unwrap(), agent_artifact::Scope::User);
        assert_eq!(
            parse_scope("project").unwrap(),
            agent_artifact::Scope::Project
        );
        assert!(parse_scope("bogus").is_err());
        assert!(parse_scope("USER").is_err()); // case-sensitive
        assert!(parse_scope("").is_err());
    }
}