Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(config): Add `wt config show --doctor` option
Maximilian Roose286e84729e7
diff --git a/src/cli.rs b/src/cli.rsindex c364b3da4..0d615da31 100644--- a/src/cli.rs+++ b/src/cli.rs@@ -284,9 +284,24 @@ pub enum ConfigCommand {after_long_help = r#"Shows location and contents of user config (`~/.config/worktrunk/config.toml`)and project config (`.config/wt.toml`).-If a config file doesn't exist, shows defaults that would be used."#+If a config file doesn't exist, shows defaults that would be used.++## Doctor Mode++Use `--doctor` to test commit generation with a synthetic diff:++```console+wt config show --doctor+```++This verifies that the LLM command is configured correctly and can generate+commit messages."#)]- Show,+ Show {+ /// Test commit generation pipeline+ #[arg(long)]+ doctor: bool,+ },/// Manage caches (CI status, default branch)Cache {diff --git a/src/commands/config.rs b/src/commands/config.rsindex fb0260458..cb130765b 100644--- a/src/commands/config.rs+++ b/src/commands/config.rs@@ -3,15 +3,19 @@ use color_print::cformat;use etcetera::base_strategy::{BaseStrategy, choose_base_strategy};use std::fmt::Write as _;use std::path::PathBuf;+use worktrunk::config::WorktrunkConfig;use worktrunk::config::{find_unknown_project_keys, find_unknown_user_keys};use worktrunk::git::Repository;use worktrunk::path::format_path_for_display;use worktrunk::shell::Shell;-use worktrunk::styling::{HINT_EMOJI, INFO_EMOJI, WARNING_EMOJI, format_toml, format_with_gutter};+use worktrunk::styling::{+ ERROR_EMOJI, HINT_EMOJI, INFO_EMOJI, WARNING_EMOJI, format_toml, format_with_gutter,+};use super::configure_shell::{ConfigAction, scan_shell_configs};use super::list::ci_status::CachedCiStatus;use crate::help_pager::show_help_in_pager;+use crate::llm::test_commit_generation;use crate::output;/// Example configuration file content (displayed in help with values uncommented)@@ -81,26 +85,82 @@ pub fn handle_config_create() -> anyhow::Result<()> {}/// Handle the config show command-pub fn handle_config_show() -> anyhow::Result<()> {+pub fn handle_config_show(doctor: bool) -> anyhow::Result<()> {// Build the complete output as a string- let mut output = String::new();+ let mut show_output = String::new();// Render user config- render_user_config(&mut output)?;- output.push('\n');+ render_user_config(&mut show_output)?;+ show_output.push('\n');// Render project config if in a git repository- render_project_config(&mut output)?;- output.push('\n');+ render_project_config(&mut show_output)?;+ show_output.push('\n');// Render shell integration status- render_shell_status(&mut output)?;+ render_shell_status(&mut show_output)?;- // Display through pager- if let Err(e) = show_help_in_pager(&output) {+ // Display through pager (only if not in doctor mode, since doctor adds interactive output)+ if doctor {+ worktrunk::styling::eprintln!("{}", show_output);+ } else if let Err(e) = show_help_in_pager(&show_output) {log::debug!("Pager invocation failed: {}", e);// Fall back to direct output via eprintln (matches help behavior)- worktrunk::styling::eprintln!("{}", output);+ worktrunk::styling::eprintln!("{}", show_output);+ }++ // Run doctor checks if requested+ if doctor {+ run_doctor_checks()?;+ }++ Ok(())+}++/// Run diagnostic checks on configuration+fn run_doctor_checks() -> anyhow::Result<()> {+ output::info("Running diagnostic checks...")?;+ output::blank()?;++ // Test commit generation+ let config = WorktrunkConfig::load()?;+ let commit_config = &config.commit_generation;++ if !commit_config.is_configured() {+ output::warning("Commit generation is not configured")?;+ output::hint(cformat!(+ "Add <bright-black>[commit-generation]</> section to enable LLM commit messages"+ ))?;+ return Ok(());+ }++ let command_display = format!(+ "{}{}",+ commit_config.command.as_ref().unwrap(),+ if commit_config.args.is_empty() {+ String::new()+ } else {+ format!(" {}", commit_config.args.join(" "))+ }+ );++ output::progress(cformat!(+ "Testing commit generation with <bold>{command_display}</>"+ ))?;++ match test_commit_generation(commit_config) {+ Ok(message) => {+ output::success("Commit generation working")?;+ output::blank()?;+ output::info("Sample generated message:")?;+ output::gutter(format_with_gutter(&message, "", None))?;+ }+ Err(e) => {+ output::print(cformat!("{ERROR_EMOJI} <red>Commit generation failed</>"))?;+ output::gutter(format_with_gutter(&e.to_string(), "", None))?;+ output::blank()?;+ output::hint("Check that the command is installed and API keys are configured")?;+ }}Ok(())diff --git a/src/llm.rs b/src/llm.rsindex 4a280cf7e..c1b659b3e 100644--- a/src/llm.rs+++ b/src/llm.rs@@ -408,6 +408,61 @@ pub fn generate_squash_message(Ok(commit_message)}+/// Synthetic diff for testing commit generation+const SYNTHETIC_DIFF: &str = r#"diff --git a/src/main.rs b/src/main.rs+index abc1234..def5678 100644+--- a/src/main.rs++++ b/src/main.rs+@@ -10,6 +10,10 @@ fn main() {+ println!("Hello, world!");++++ // Add new feature++ let config = load_config();++ process_data(&config);+ }+"#;++/// Test commit generation with a synthetic diff.+///+/// Returns Ok(message) if the LLM command succeeds, or an error describing+/// what went wrong (command not found, API error, empty response, etc.)+pub fn test_commit_generation(+ commit_generation_config: &CommitGenerationConfig,+) -> anyhow::Result<String> {+ if !commit_generation_config.is_configured() {+ anyhow::bail!(+ "Commit generation is not configured. Add [commit-generation] to your config."+ );+ }++ let command = commit_generation_config.command.as_ref().unwrap();+ let args = &commit_generation_config.args;++ // Build prompt with synthetic data+ let recent_commits = vec![+ "feat: Add user authentication".to_string(),+ "fix: Handle edge case in parser".to_string(),+ "docs: Update README".to_string(),+ ];+ let context = TemplateContext {+ git_diff: SYNTHETIC_DIFF,+ branch: "feature/example",+ recent_commits: Some(&recent_commits),+ repo_name: "test-repo",+ commits: &[],+ target_branch: None,+ };+ let prompt = build_prompt(commit_generation_config, TemplateType::Commit, &context)?;++ execute_llm_command(command, args, &prompt).map_err(|e| {+ worktrunk::git::GitError::LlmCommandFailed {+ command: format_command_display(command, args),+ error: e.to_string(),+ }+ .into()+ })+}+#[cfg(test)]mod tests {use super::*;diff --git a/src/main.rs b/src/main.rsindex 5e7ea820d..18abfe2a5 100644--- a/src/main.rs+++ b/src/main.rs@@ -681,7 +681,7 @@ fn main() {}}ConfigCommand::Create => handle_config_create(),- ConfigCommand::Show => handle_config_show(),+ ConfigCommand::Show { doctor } => handle_config_show(doctor),ConfigCommand::Cache { action } => match action {CacheCommand::Show => handle_cache_show(),CacheCommand::Clear { cache_type } => handle_cache_clear(cache_type),diff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rsindex c3152ea01..a2fab59f9 100644--- a/tests/integration_tests/config_show.rs+++ b/tests/integration_tests/config_show.rs@@ -357,3 +357,72 @@ fn test_config_show_warns_unknown_user_keys() {"#);});}++/// Test `wt config show --doctor` when commit generation is not configured+#[test]+fn test_config_show_doctor_not_configured() {+ let repo = TestRepo::new();+ let temp_home = TempDir::new().unwrap();++ // Create isolated config directory+ let global_config_dir = temp_home.path().join(".config").join("worktrunk");+ fs::create_dir_all(&global_config_dir).unwrap();+ let config_path = global_config_dir.join("config.toml");+ fs::write(+ &config_path,+ "worktree-path = \"../{{ main_worktree }}.{{ branch }}\"",+ )+ .unwrap();++ let settings = setup_snapshot_settings_with_home(&repo, &temp_home);+ settings.bind(|| {+ let mut cmd = wt_command();+ repo.clean_cli_env(&mut cmd);+ // Override WORKTRUNK_CONFIG_PATH to point to our test config+ cmd.env("WORKTRUNK_CONFIG_PATH", &config_path);+ cmd.arg("config")+ .arg("show")+ .arg("--doctor")+ .current_dir(repo.root_path());+ set_temp_home_env(&mut cmd, temp_home.path());++ assert_cmd_snapshot!(cmd);+ });+}++/// Test `wt config show --doctor` when commit generation command doesn't exist+#[test]+fn test_config_show_doctor_command_not_found() {+ let repo = TestRepo::new();+ let temp_home = TempDir::new().unwrap();++ // Create isolated config directory+ let global_config_dir = temp_home.path().join(".config").join("worktrunk");+ fs::create_dir_all(&global_config_dir).unwrap();+ let config_path = global_config_dir.join("config.toml");+ fs::write(+ &config_path,+ r#"worktree-path = "../{{ main_worktree }}.{{ branch }}"++[commit-generation]+command = "nonexistent-llm-command-12345"+args = ["-m", "test-model"]+"#,+ )+ .unwrap();++ let settings = setup_snapshot_settings_with_home(&repo, &temp_home);+ settings.bind(|| {+ let mut cmd = wt_command();+ repo.clean_cli_env(&mut cmd);+ // Override WORKTRUNK_CONFIG_PATH to point to our test config+ cmd.env("WORKTRUNK_CONFIG_PATH", &config_path);+ cmd.arg("config")+ .arg("show")+ .arg("--doctor")+ .current_dir(repo.root_path());+ set_temp_home_env(&mut cmd, temp_home.path());++ assert_cmd_snapshot!(cmd);+ });+}diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_doctor_command_not_found.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_doctor_command_not_found.snapnew file mode 100644index 000000000..6f04b78ec--- /dev/null+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_doctor_command_not_found.snap@@ -0,0 +1,53 @@+---+source: tests/integration_tests/config_show.rs+info:+ program: wt+ args:+ - config+ - show+ - "--doctor"+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1735776000"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+⚪ User Config: [1m~/.config/worktrunk/config.toml[22m+[107m [0m worktree-path = [32m"../{{ main_worktree }}.{{ branch }}"[0m+[107m [0m+[107m [0m [1m[36m[commit-generation][0m+[107m [0m command = [32m"nonexistent-llm-command-12345"[0m+[107m [0m args = [[32m"-m"[0m, [32m"test-model"[0m]++⚪ Project Config: [1m[REPO]/.config/wt.toml[22m+💡 [2mNot found[22m++[2m⚪ Skipped bash; ~/.bashrc not found[22m+[2m⚪ Skipped zsh; ~/.zshrc not found[22m+[2m⚪ Skipped fish; ~/.config/fish/conf.d not found[22m++⚪ Running diagnostic checks...++🔄 [36mTesting commit generation with [1mnonexistent-llm-command-12345 -m test-model[22m[39m+❌ [31mCommit generation failed[39m+[107m [0m ❌ [31mCommit generation command failed[39m+[107m [0m [107m [0m Failed to spawn LLM command+[107m [0m+[107m [0m ⚪ Ran command:+[107m [0m [107m [0m nonexistent-llm-command-12345 -m test-model++💡 [2mCheck that the command is installed and API keys are configured[22mdiff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_doctor_not_configured.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_doctor_not_configured.snapnew file mode 100644index 000000000..495377b3f--- /dev/null+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_doctor_not_configured.snap@@ -0,0 +1,42 @@+---+source: tests/integration_tests/config_show.rs+info:+ program: wt+ args:+ - config+ - show+ - "--doctor"+ env:+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1735776000"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+⚪ User Config: [1m~/.config/worktrunk/config.toml[22m+[107m [0m worktree-path = [32m"../{{ main_worktree }}.{{ branch }}"[0m++⚪ Project Config: [1m[REPO]/.config/wt.toml[22m+💡 [2mNot found[22m++[2m⚪ Skipped bash; ~/.bashrc not found[22m+[2m⚪ Skipped zsh; ~/.zshrc not found[22m+[2m⚪ Skipped fish; ~/.config/fish/conf.d not found[22m++⚪ Running diagnostic checks...++🟡 [33mCommit generation is not configured[39m+💡 [2mAdd [90m[commit-generation][39m section to enable LLM commit messages[22mdiff --git a/tests/snapshots/integration__integration_tests__help__help_config_show.snap b/tests/snapshots/integration__integration_tests__help__help_config_show.snapindex 373d24270..1fc7f3a77 100644--- a/tests/snapshots/integration__integration_tests__help__help_config_show.snap+++ b/tests/snapshots/integration__integration_tests__help__help_config_show.snap@@ -22,6 +22,9 @@ wt config show - Show configuration files & locationsUsage: [1m[36mwt config show[0m [36m[OPTIONS][0m[1m[32mOptions:[0m+ [1m[36m--doctor[0m+ Test commit generation pipeline+[1m[36m-h[0m, [1m[36m--help[0mPrint help (see a summary with '-h')@@ -39,3 +42,12 @@ Shows location and contents of user config ([2m~/.config/worktrunk/config.tomland project config ([2m.config/wt.toml[0m).If a config file doesn't exist, shows defaults that would be used.++[32mDoctor Mode[0m++Use [2m--doctor[0m to test commit generation with a synthetic diff:++ [2mwt config show --doctor[0m++This verifies that the LLM command is configured correctly and can generate+commit messages.