track-core 0.1.0

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

use dialoguer::{theme::ColorfulTheme, Input};

use crate::config::{
    ApiConfigFile, ConfigService, LlamaCppConfigFile, RemoteAgentConfigFile,
    RemoteAgentReviewFollowUpConfigFile, TrackConfigFile, DEFAULT_LLAMACPP_MODEL_HF_FILE,
    DEFAULT_LLAMACPP_MODEL_HF_REPO, DEFAULT_REMOTE_AGENT_PORT, DEFAULT_REMOTE_AGENT_WORKSPACE_ROOT,
    DEFAULT_REMOTE_PROJECTS_REGISTRY_PATH,
};
use crate::errors::{ErrorCode, TrackError};
use crate::paths::{
    collapse_home_path, collapse_path_value, get_managed_remote_agent_key_path,
    get_managed_remote_agent_known_hosts_path, resolve_path_from_invocation_dir,
};
use crate::terminal_ui::{
    format_note, format_prompt_label, format_summary, SummaryTone, ValueTone,
};
use crate::types::RemoteAgentPreferredTool;

pub const NONE_SENTINEL: &str = "none";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigureReason {
    FirstRun,
    Manual,
}

pub trait Prompter {
    fn ask(&mut self, prompt: &str) -> Result<String, TrackError>;
    fn println(&mut self, line: &str);
}

pub struct TerminalPrompter;

impl Prompter for TerminalPrompter {
    fn ask(&mut self, prompt: &str) -> Result<String, TrackError> {
        Input::<String>::with_theme(&ColorfulTheme::default())
            .with_prompt(prompt)
            .allow_empty(true)
            .interact_text()
            .map_err(|error| {
                TrackError::new(
                    ErrorCode::InteractiveRequired,
                    format!("Could not read interactive input: {error}"),
                )
            })
    }

    fn println(&mut self, line: &str) {
        println!("{line}");
    }
}

pub fn parse_project_roots_input(input: &str) -> Vec<String> {
    input
        .split([',', '\n'])
        .map(|value| value.trim().to_owned())
        .filter(|value| !value.is_empty())
        .collect::<Vec<_>>()
}

pub fn parse_project_aliases_input(input: &str) -> Result<BTreeMap<String, String>, TrackError> {
    if input.trim().is_empty() {
        return Ok(BTreeMap::new());
    }

    let mut aliases = BTreeMap::new();

    for entry in input
        .split([',', '\n'])
        .map(|value| value.trim())
        .filter(|value| !value.is_empty())
    {
        let Some((alias, canonical_name)) = entry.split_once('=') else {
            return Err(TrackError::new(
                ErrorCode::InvalidConfigInput,
                "Project aliases must use alias=canonical-name format.",
            ));
        };

        let alias = alias.trim();
        let canonical_name = canonical_name.trim();
        if alias.is_empty() || canonical_name.is_empty() {
            return Err(TrackError::new(
                ErrorCode::InvalidConfigInput,
                "Project aliases must use alias=canonical-name format.",
            ));
        }

        aliases.insert(alias.to_owned(), canonical_name.to_owned());
    }

    Ok(aliases)
}

fn format_project_aliases_input(aliases: &BTreeMap<String, String>) -> String {
    aliases
        .iter()
        .map(|(alias, canonical_name)| format!("{alias}={canonical_name}"))
        .collect::<Vec<_>>()
        .join(", ")
}

fn format_project_roots_display(roots: &[String]) -> String {
    roots
        .iter()
        .map(|value| collapse_path_value(value))
        .collect::<Vec<_>>()
        .join(", ")
}

fn create_default_config_file() -> TrackConfigFile {
    TrackConfigFile {
        project_roots: Vec::new(),
        project_aliases: BTreeMap::new(),
        api: ApiConfigFile::default(),
        llama_cpp: LlamaCppConfigFile::default(),
        remote_agent: None,
    }
}

fn ensure_interactive_terminal(config_path: &std::path::Path) -> Result<(), TrackError> {
    if io::stdin().is_terminal() && io::stdout().is_terminal() {
        return Ok(());
    }

    Err(TrackError::new(
        ErrorCode::InteractiveRequired,
        format!(
            "Config setup requires an interactive terminal. Create {} manually or rerun `track` in a terminal.",
            collapse_home_path(config_path)
        ),
    ))
}

fn prompt_with_default(
    prompter: &mut dyn Prompter,
    label: &str,
    default_value: Option<&str>,
    allow_clear: bool,
) -> Result<String, TrackError> {
    let prompt = format_prompt_label(label, default_value.filter(|value| !value.is_empty()));

    let response = prompter.ask(&prompt)?.trim().to_owned();

    if allow_clear && response.eq_ignore_ascii_case(NONE_SENTINEL) {
        return Ok(String::new());
    }

    if response.is_empty() {
        return Ok(default_value.unwrap_or_default().to_owned());
    }

    Ok(response)
}

fn prompt_required_value(
    prompter: &mut dyn Prompter,
    label: &str,
    default_value: Option<&str>,
) -> Result<String, TrackError> {
    loop {
        let response = prompt_with_default(prompter, label, default_value, false)?;
        if !response.trim().is_empty() {
            return Ok(response.trim().to_owned());
        }

        prompter.println("Please enter a value.");
    }
}

fn prompt_project_roots(
    prompter: &mut dyn Prompter,
    defaults: &[String],
) -> Result<Vec<String>, TrackError> {
    loop {
        let response = prompt_with_default(
            prompter,
            "Project roots (comma-separated)",
            Some(&format_project_roots_display(defaults)),
            false,
        )?;

        let project_roots = parse_project_roots_input(&response);
        if !project_roots.is_empty() {
            return Ok(project_roots);
        }

        prompter.println("Please enter at least one project root.");
    }
}

fn prompt_project_aliases(
    prompter: &mut dyn Prompter,
    defaults: &BTreeMap<String, String>,
) -> Result<BTreeMap<String, String>, TrackError> {
    loop {
        let response = prompt_with_default(
            prompter,
            "Project aliases (alias=canonical-name, comma-separated)",
            Some(&format_project_aliases_input(defaults)),
            true,
        )?;

        match parse_project_aliases_input(&response) {
            Ok(aliases) => return Ok(aliases),
            Err(error) => prompter.println(error.message()),
        }
    }
}

fn prompt_api_port(prompter: &mut dyn Prompter, default_port: u16) -> Result<u16, TrackError> {
    loop {
        let response = prompt_with_default(
            prompter,
            "Local API port",
            Some(&default_port.to_string()),
            false,
        )?;

        match response.parse::<u16>() {
            Ok(port) if port > 0 => return Ok(port),
            _ => prompter.println("Please enter a valid TCP port."),
        }
    }
}

fn prompt_remote_agent_host(
    prompter: &mut dyn Prompter,
    default_host: Option<&str>,
) -> Result<Option<String>, TrackError> {
    let response = prompt_with_default(prompter, "Remote agent host", default_host, true)?;
    let trimmed = response.trim();
    if trimmed.is_empty() {
        Ok(None)
    } else {
        Ok(Some(trimmed.to_owned()))
    }
}

fn prompt_remote_agent_port(
    prompter: &mut dyn Prompter,
    default_port: u16,
) -> Result<u16, TrackError> {
    loop {
        let response = prompt_with_default(
            prompter,
            "Remote SSH port",
            Some(&default_port.to_string()),
            false,
        )?;

        match response.parse::<u16>() {
            Ok(port) if port > 0 => return Ok(port),
            _ => prompter.println("Please enter a valid SSH port."),
        }
    }
}

fn prompt_yes_no(
    prompter: &mut dyn Prompter,
    label: &str,
    default_value: bool,
) -> Result<bool, TrackError> {
    loop {
        let default_display = if default_value { "yes" } else { "no" };
        let response = prompt_with_default(prompter, label, Some(default_display), false)?;

        match response.trim().to_ascii_lowercase().as_str() {
            "y" | "yes" | "true" => return Ok(true),
            "n" | "no" | "false" => return Ok(false),
            _ => prompter.println("Please answer yes or no."),
        }
    }
}

fn managed_remote_agent_key_exists() -> Result<bool, TrackError> {
    Ok(get_managed_remote_agent_key_path()?.exists())
}

fn install_managed_remote_agent_key(source_path: &str) -> Result<(), TrackError> {
    let source_path = resolve_path_from_invocation_dir(source_path)?;
    let managed_key_path = get_managed_remote_agent_key_path()?;
    let known_hosts_path = get_managed_remote_agent_known_hosts_path()?;

    let Some(parent_directory) = managed_key_path.parent() else {
        return Err(TrackError::new(
            ErrorCode::InvalidRemoteAgentConfig,
            "Could not determine the managed remote-agent directory.",
        ));
    };

    fs::create_dir_all(parent_directory).map_err(|error| {
        TrackError::new(
            ErrorCode::InvalidRemoteAgentConfig,
            format!(
                "Could not create the managed remote-agent directory at {}: {error}",
                collapse_home_path(parent_directory)
            ),
        )
    })?;

    fs::copy(&source_path, &managed_key_path).map_err(|error| {
        TrackError::new(
            ErrorCode::InvalidRemoteAgentConfig,
            format!(
                "Could not copy the SSH private key from {} to {}: {error}",
                collapse_home_path(&source_path),
                collapse_home_path(&managed_key_path)
            ),
        )
    })?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;

        fs::set_permissions(&managed_key_path, fs::Permissions::from_mode(0o600)).map_err(
            |error| {
                TrackError::new(
                    ErrorCode::InvalidRemoteAgentConfig,
                    format!(
                        "Could not set permissions on the managed SSH private key at {}: {error}",
                        collapse_home_path(&managed_key_path)
                    ),
                )
            },
        )?;
    }

    if !known_hosts_path.exists() {
        fs::write(&known_hosts_path, "").map_err(|error| {
            TrackError::new(
                ErrorCode::InvalidRemoteAgentConfig,
                format!(
                    "Could not create the managed known_hosts file at {}: {error}",
                    collapse_home_path(&known_hosts_path)
                ),
            )
        })?;
    }

    Ok(())
}

fn prompt_remote_agent_key_import(
    prompter: &mut dyn Prompter,
    has_existing_managed_key: bool,
) -> Result<(), TrackError> {
    loop {
        let label = if has_existing_managed_key {
            "SSH private key to import"
        } else {
            "SSH private key to import"
        };
        let response = prompt_with_default(prompter, label, None, false)?;
        let trimmed = response.trim();

        if trimmed.is_empty() && has_existing_managed_key {
            return Ok(());
        }

        if trimmed.is_empty() {
            prompter.println(
                "Please provide a private SSH key path or finish setup later by rerunning `track`.",
            );
            continue;
        }

        return install_managed_remote_agent_key(trimmed);
    }
}

fn format_config_saved_output(
    config: &TrackConfigFile,
    config_path: &std::path::Path,
    reason: ConfigureReason,
) -> String {
    let (remote_agent_display, remote_agent_tone) = match config.remote_agent.as_ref() {
        Some(remote_agent) => (
            format!(
                "{}@{}:{}",
                remote_agent.user, remote_agent.host, remote_agent.port
            ),
            ValueTone::Plain,
        ),
        None => ("disabled".to_owned(), ValueTone::Plain),
    };

    let summary = format_summary(
        match reason {
            ConfigureReason::FirstRun => "Config created",
            ConfigureReason::Manual => "Config updated",
        },
        SummaryTone::Success,
        &[
            ("File", collapse_home_path(config_path), ValueTone::Path),
            (
                "Project roots",
                format!("{} configured", config.project_roots.len()),
                ValueTone::Plain,
            ),
            (
                "Aliases",
                format!("{} configured", config.project_aliases.len()),
                ValueTone::Plain,
            ),
            ("API port", config.api.port.to_string(), ValueTone::Plain),
            ("Remote", remote_agent_display, remote_agent_tone),
        ],
    );

    let preserved_model_override_fields = preserved_model_override_fields(&config.llama_cpp);
    if preserved_model_override_fields.is_empty() {
        return summary;
    }

    format!(
        "{summary}\n\n{}",
        format_note(
            "Advanced",
            &format!(
                "The following fields are set in {} but are not managed by the wizard: {}. Edit the file directly if you need to change them.",
                collapse_home_path(config_path),
                preserved_model_override_fields.join(", "),
            ),
        )
    )
}

fn preserved_model_override_fields(config: &LlamaCppConfigFile) -> Vec<&'static str> {
    let mut fields = Vec::new();

    if config.model_path.is_some() {
        fields.push("llamaCpp.modelPath");
    }

    if let (Some(repo), Some(file)) = (
        config.model_hf_repo.as_deref(),
        config.model_hf_file.as_deref(),
    ) {
        let uses_builtin_default =
            repo == DEFAULT_LLAMACPP_MODEL_HF_REPO && file == DEFAULT_LLAMACPP_MODEL_HF_FILE;
        if !uses_builtin_default {
            fields.push("llamaCpp.modelHfRepo");
            fields.push("llamaCpp.modelHfFile");
        }
    }

    fields
}

pub fn run_configure_command(
    config_service: &ConfigService,
    reason: ConfigureReason,
) -> Result<String, TrackError> {
    ensure_interactive_terminal(config_service.resolved_path())?;
    let mut prompter = TerminalPrompter;
    run_configure_command_with_prompter(config_service, &mut prompter, reason)
}

pub fn run_configure_command_with_prompter(
    config_service: &ConfigService,
    prompter: &mut dyn Prompter,
    reason: ConfigureReason,
) -> Result<String, TrackError> {
    let existing_config = match config_service.load_config_file() {
        Ok(config) => Some(config),
        Err(error) if error.code == ErrorCode::ConfigNotFound => None,
        Err(error) => return Err(error),
    };
    let defaults = existing_config.unwrap_or_else(create_default_config_file);

    // The wizard stays linear on purpose: establish the local filesystem and
    // API settings first, then optionally layer in remote-agent details.
    let intro = match reason {
        ConfigureReason::FirstRun => format_summary(
            "Config setup",
            SummaryTone::Info,
            &[(
                "File",
                collapse_home_path(config_service.resolved_path()),
                ValueTone::Path,
            )],
        ),
        ConfigureReason::Manual => format_summary(
            "Config editor",
            SummaryTone::Info,
            &[(
                "File",
                collapse_home_path(config_service.resolved_path()),
                ValueTone::Path,
            )],
        ),
    };
    prompter.println(&intro);
    prompter.println(&format_note("Enter", "keep current values"));
    prompter.println(&format_note(NONE_SENTINEL, "clear optional values"));

    let api_port = prompt_api_port(prompter, defaults.api.port)?;
    let project_roots = prompt_project_roots(prompter, &defaults.project_roots)?;
    let project_aliases = prompt_project_aliases(prompter, &defaults.project_aliases)?;
    let remote_agent_host = prompt_remote_agent_host(
        prompter,
        defaults
            .remote_agent
            .as_ref()
            .map(|remote_agent| remote_agent.host.as_str()),
    )?;
    let remote_agent = if let Some(host) = remote_agent_host {
        let existing_remote_agent = defaults.remote_agent.as_ref();
        let remote_user = prompt_required_value(
            prompter,
            "Remote agent user",
            existing_remote_agent.map(|remote_agent| remote_agent.user.as_str()),
        )?;
        let remote_port = prompt_remote_agent_port(
            prompter,
            existing_remote_agent
                .map(|remote_agent| remote_agent.port)
                .unwrap_or(DEFAULT_REMOTE_AGENT_PORT),
        )?;
        let remote_workspace_root = prompt_required_value(
            prompter,
            "Remote workspace root",
            existing_remote_agent
                .map(|remote_agent| remote_agent.workspace_root.as_str())
                .or(Some(DEFAULT_REMOTE_AGENT_WORKSPACE_ROOT)),
        )?;
        let remote_projects_registry_path = prompt_required_value(
            prompter,
            "Remote projects registry path",
            existing_remote_agent
                .map(|remote_agent| remote_agent.projects_registry_path.as_str())
                .or(Some(DEFAULT_REMOTE_PROJECTS_REGISTRY_PATH)),
        )?;
        prompt_remote_agent_key_import(prompter, managed_remote_agent_key_exists()?)?;
        let existing_review_follow_up =
            existing_remote_agent.and_then(|remote_agent| remote_agent.review_follow_up.as_ref());
        let review_follow_up_enabled = prompt_yes_no(
            prompter,
            "Enable automatic GitHub review follow-ups",
            existing_review_follow_up
                .map(|review_follow_up| review_follow_up.enabled)
                .unwrap_or(false),
        )?;

        // We intentionally remember the last configured reviewer when the
        // feature is toggled off. That keeps the wizard fast to re-enable on a
        // later run instead of forcing users back through the web UI.
        // TODO: If users ask to clear the remembered GitHub user from the
        // wizard, add an explicit prompt for that instead of overloading the
        // yes/no flow here.
        let review_follow_up = if review_follow_up_enabled {
            let main_user = prompt_required_value(
                prompter,
                "GitHub user for automatic follow-ups",
                existing_review_follow_up
                    .and_then(|review_follow_up| review_follow_up.main_user.as_deref()),
            )?;

            Some(RemoteAgentReviewFollowUpConfigFile {
                enabled: true,
                main_user: Some(main_user),
                default_review_prompt: existing_review_follow_up
                    .and_then(|review_follow_up| review_follow_up.default_review_prompt.clone()),
            })
        } else {
            existing_review_follow_up
                .and_then(|review_follow_up| review_follow_up.main_user.clone())
                .map(|main_user| RemoteAgentReviewFollowUpConfigFile {
                    enabled: false,
                    main_user: Some(main_user),
                    default_review_prompt: existing_review_follow_up.and_then(|review_follow_up| {
                        review_follow_up.default_review_prompt.clone()
                    }),
                })
        };

        Some(RemoteAgentConfigFile {
            host,
            user: remote_user,
            port: remote_port,
            workspace_root: remote_workspace_root,
            projects_registry_path: remote_projects_registry_path,
            // The web UI owns the preferred runner choice today, so the wizard
            // preserves any existing selection instead of introducing a second
            // place where users have to keep the same setting in sync.
            preferred_tool: existing_remote_agent
                .map(|remote_agent| remote_agent.preferred_tool)
                .unwrap_or(RemoteAgentPreferredTool::Codex),
            // TODO: If people start preferring the terminal wizard for remote
            // dispatch setup, add a multiline editor flow here. For now the
            // shell prelude stays web-managed so the wizard preserves an
            // existing value instead of trying to squeeze multiline shell
            // setup into a single-line prompt.
            shell_prelude: existing_remote_agent
                .and_then(|remote_agent| remote_agent.shell_prelude.clone()),
            review_follow_up,
        })
    } else {
        // TODO: Consider removing the managed SSH key when remote dispatch is
        // disabled explicitly. We leave it in place for now so a temporary
        // config change does not silently destroy a secret the user imported on
        // purpose.
        None
    };

    let config = TrackConfigFile {
        project_roots,
        project_aliases,
        api: ApiConfigFile { port: api_port },
        llama_cpp: defaults.llama_cpp.clone(),
        remote_agent,
    };

    config_service.save_config_file(&config)?;
    Ok(format_config_saved_output(
        &config,
        config_service.resolved_path(),
        reason,
    ))
}

#[cfg(test)]
mod tests {
    use std::collections::{BTreeMap, VecDeque};

    use tempfile::TempDir;

    use super::{
        parse_project_aliases_input, parse_project_roots_input,
        run_configure_command_with_prompter, ConfigureReason, Prompter,
    };
    use crate::config::{
        ConfigService, LlamaCppConfigFile, RemoteAgentReviewFollowUpConfigFile, TrackConfigFile,
        DEFAULT_LLAMACPP_MODEL_HF_FILE, DEFAULT_LLAMACPP_MODEL_HF_REPO,
    };
    use crate::test_support::{set_env_var, track_data_env_lock};

    struct ScriptedPrompter {
        answers: VecDeque<String>,
        lines: Vec<String>,
    }

    impl ScriptedPrompter {
        fn new(answers: &[&str]) -> Self {
            Self {
                answers: answers.iter().map(|value| (*value).to_owned()).collect(),
                lines: Vec::new(),
            }
        }
    }

    impl Prompter for ScriptedPrompter {
        fn ask(&mut self, _prompt: &str) -> Result<String, crate::errors::TrackError> {
            Ok(self
                .answers
                .pop_front()
                .expect("scripted prompt should have enough answers"))
        }

        fn println(&mut self, line: &str) {
            self.lines.push(line.to_owned());
        }
    }

    fn temp_config_service() -> (TempDir, ConfigService) {
        let directory = TempDir::new().expect("tempdir should be created");
        let service = ConfigService::new(Some(directory.path().join("config.json")))
            .expect("config service should resolve");
        (directory, service)
    }

    #[test]
    fn parses_project_roots() {
        assert_eq!(
            parse_project_roots_input("~/work, ~/oss\n~/lab"),
            vec!["~/work", "~/oss", "~/lab"]
        );
    }

    #[test]
    fn parses_project_aliases() {
        let aliases = parse_project_aliases_input("proj-x=project-x, infra=platform")
            .expect("aliases should parse");

        assert_eq!(aliases.get("proj-x"), Some(&"project-x".to_owned()));
        assert_eq!(aliases.get("infra"), Some(&"platform".to_owned()));
    }

    #[test]
    fn writes_first_run_config() {
        let (_directory, service) = temp_config_service();
        let mut prompter =
            ScriptedPrompter::new(&["3210", "~/work, ~/oss", "proj-x=project-x", ""]);

        let output =
            run_configure_command_with_prompter(&service, &mut prompter, ConfigureReason::FirstRun)
                .expect("config wizard should succeed");

        assert!(output.contains("Config created"));
        let raw = std::fs::read_to_string(service.resolved_path()).expect("config should save");
        assert!(!raw.contains("\"llamaCpp\""));
        assert!(raw.contains("\"projectRoots\""));
        assert!(raw.contains("\"api\""));
    }

    #[test]
    fn mentions_preserved_manual_model_overrides() {
        let (_directory, service) = temp_config_service();
        service
            .save_config_file(&TrackConfigFile {
                project_roots: vec!["~/work".to_owned()],
                project_aliases: BTreeMap::new(),
                api: crate::config::ApiConfigFile::default(),
                llama_cpp: LlamaCppConfigFile {
                    model_path: Some("~/.models/custom.gguf".to_owned()),
                    model_hf_repo: None,
                    model_hf_file: None,
                },
                remote_agent: None,
            })
            .expect("seed config should save");

        let mut prompter = ScriptedPrompter::new(&["", "", "", ""]);
        let output =
            run_configure_command_with_prompter(&service, &mut prompter, ConfigureReason::Manual)
                .expect("config wizard should succeed");

        assert!(output.contains("llamaCpp.modelPath"));
    }

    #[test]
    fn does_not_call_out_builtin_hugging_face_defaults() {
        let (_directory, service) = temp_config_service();
        service
            .save_config_file(&TrackConfigFile {
                project_roots: vec!["~/work".to_owned()],
                project_aliases: BTreeMap::new(),
                api: crate::config::ApiConfigFile::default(),
                llama_cpp: LlamaCppConfigFile {
                    model_path: None,
                    model_hf_repo: Some(DEFAULT_LLAMACPP_MODEL_HF_REPO.to_owned()),
                    model_hf_file: Some(DEFAULT_LLAMACPP_MODEL_HF_FILE.to_owned()),
                },
                remote_agent: None,
            })
            .expect("seed config should save");

        let mut prompter = ScriptedPrompter::new(&["", "", "", ""]);
        let output =
            run_configure_command_with_prompter(&service, &mut prompter, ConfigureReason::Manual)
                .expect("config wizard should succeed");

        assert!(!output.contains("llamaCpp.modelHfRepo"));
        assert!(!output.contains("llamaCpp.modelHfFile"));
    }

    #[test]
    fn writes_remote_review_follow_up_from_wizard() {
        let (_directory, service) = temp_config_service();
        let _track_data_dir_guard = track_data_env_lock()
            .lock()
            .expect("track data dir lock should not be poisoned");
        let data_dir = service
            .resolved_path()
            .parent()
            .expect("config path should have a parent")
            .join("track-data")
            .join("issues");
        let _track_data_dir = set_env_var("TRACK_DATA_DIR", &data_dir);

        let ssh_key_source = data_dir
            .parent()
            .expect("data dir should have a parent")
            .join("id_ed25519.source");
        std::fs::create_dir_all(
            ssh_key_source
                .parent()
                .expect("SSH key source should have a parent"),
        )
        .expect("SSH key source parent should be created");
        std::fs::write(&ssh_key_source, "not-a-real-private-key")
            .expect("SSH key source should be written");

        let ssh_key_source = ssh_key_source.to_string_lossy().into_owned();
        let mut prompter = ScriptedPrompter::new(&[
            "3210",
            "~/work",
            "",
            "builder.example.com",
            "codex",
            "2222",
            "/srv/track",
            "/srv/track/projects.json",
            &ssh_key_source,
            "yes",
            "octocat",
        ]);

        run_configure_command_with_prompter(&service, &mut prompter, ConfigureReason::FirstRun)
            .expect("config wizard should succeed");

        let saved = service
            .load_config_file()
            .expect("saved config should load successfully");
        let remote_agent = saved
            .remote_agent
            .expect("remote agent config should be present");

        assert_eq!(
            remote_agent.review_follow_up,
            Some(RemoteAgentReviewFollowUpConfigFile {
                enabled: true,
                main_user: Some("octocat".to_owned()),
                default_review_prompt: None,
            })
        );
    }
}