Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(ansi): Prevent nested color leaks in output
Maximilian Roos35c9bf007edf
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 7db375838..c2db6dd5a 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -386,7 +386,7 @@ fn handle_squash(let squash_progress = match stats_parts.is_empty() {true => format!("π {CYAN}Squashing {commit_count} commits into 1...{CYAN:#}"),false => format!(- "π {CYAN}Squashing {commit_count} commits into 1 ({})...{CYAN:#}",+ "π {CYAN}Squashing {commit_count} commits into 1{CYAN:#} ({})...",stats_parts.join(", ")),};diff --git a/tests/common/mod.rs b/tests/common/mod.rsindex d24083c56..110c632b6 100644--- a/tests/common/mod.rs+++ b/tests/common/mod.rs@@ -579,3 +579,89 @@ pub fn resolve_git_dir(worktree_path: &Path) -> PathBuf {git_path}}++/// Validates ANSI escape sequences for the specific nested reset pattern that causes color leaks+///+/// Checks for the pattern: color code wrapping content that contains its own color codes with resets.+/// This causes the outer color to leak when the inner reset is encountered.+///+/// Example of the leak pattern:+/// ```text+/// \x1b[36mOuter text (\x1b[32minner\x1b[0m more)\x1b[0m+/// ^^^^ This reset kills the cyan!+/// "more)" appears without cyan+/// ```+///+/// # Example+/// ```+/// // Good - no nesting, proper closure+/// let output = "\x1b[36mtext\x1b[0m (stats)";+/// assert!(validate_ansi_codes(output).is_empty());+///+/// // Bad - nested reset breaks outer style+/// let output = "\x1b[36mtext (\x1b[32mnested\x1b[0m more)\x1b[0m";+/// let warnings = validate_ansi_codes(output);+/// assert!(!warnings.is_empty());+/// ```+pub fn validate_ansi_codes(text: &str) -> Vec<String> {+ let mut warnings = Vec::new();++ // Look for the specific pattern: color + content + color + content + reset + non-whitespace + reset+ // This indicates an outer style wrapping content with inner styles+ // We look for actual text (not just whitespace) between resets+ let nested_pattern = regex::Regex::new(+ r"(\x1b\[[0-9;]+m)([^\x1b]+)(\x1b\[[0-9;]+m)([^\x1b]*?)(\x1b\[0m)(\s*[^\s\x1b]+)(\x1b\[0m)",+ )+ .unwrap();++ for cap in nested_pattern.captures_iter(text) {+ let content_after_reset = cap[6].trim();++ // Only warn if there's actual content after the inner reset+ // (not just punctuation or whitespace)+ if !content_after_reset.is_empty()+ && content_after_reset.chars().any(|c| c.is_alphanumeric())+ {+ warnings.push(format!(+ "Nested color reset detected: content '{}' appears after inner reset but before outer reset - it will lose the outer color",+ content_after_reset+ ));+ }+ }++ warnings+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn test_validate_ansi_codes_no_leak() {+ // Good - no nesting+ let output = "\x1b[36mtext\x1b[0m (stats)";+ assert!(validate_ansi_codes(output).is_empty());++ // Good - nested but closes properly+ let output = "\x1b[36mtext\x1b[0m (\x1b[32mnested\x1b[0m)";+ assert!(validate_ansi_codes(output).is_empty());+ }++ #[test]+ fn test_validate_ansi_codes_detects_leak() {+ // Bad - nested reset breaks outer style+ let output = "\x1b[36mtext (\x1b[32mnested\x1b[0m more)\x1b[0m";+ let warnings = validate_ansi_codes(output);+ assert!(!warnings.is_empty());+ assert!(warnings[0].contains("more"));+ }++ #[test]+ fn test_validate_ansi_codes_ignores_punctuation() {+ // Punctuation after reset is acceptable (not a leak we care about)+ let output = "\x1b[36mtext (\x1b[32mnested\x1b[0m)\x1b[0m";+ let warnings = validate_ansi_codes(output);+ // Should not warn about ")" since it's just punctuation+ assert!(warnings.is_empty() || !warnings[0].contains("loses"));+ }+}diff --git a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snapindex afd24d0ee..05efeae14 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap@@ -13,17 +13,18 @@ info:GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"WORKTRUNK_COMMIT_GENERATION__ARGS: "fix: update file 1 content"WORKTRUNK_COMMIT_GENERATION__COMMAND: echo- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpY82I6F/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpGNwnK0/test-config.toml---success: trueexit_code: 0----- stdout ------π [36mSquashing 2 commits into 1 (2 files, [32m+2[0m)...[0m+π [36mSquashing 2 commits into 1[0m (2 files, [32m+2[0m)...π [36mGenerating squash commit message...[0mπ‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0m[40m [0m [1mSquash commits from main[0mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_deterministic.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_deterministic.snapindex 1e46c78c7..cb6faf3dd 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_squash_deterministic.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_deterministic.snap@@ -13,15 +13,16 @@ info:GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpK2R45F/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpn7Fz7h/test-config.toml---success: trueexit_code: 0----- stdout ------π [36mSquashing 3 commits into 1 (3 files, [32m+3[0m)...[0m+π [36mSquashing 3 commits into 1[0m (3 files, [32m+3[0m)...π [36mGenerating squash commit message...[0mπ‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0m[40m [0m [1mSquash commits from main[0mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snapindex a0db2a1ca..465c13f78 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_llm_fallback.snap@@ -13,16 +13,17 @@ info:GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"WORKTRUNK_COMMIT_GENERATION__COMMAND: nonexistent-llm-command- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpTWKzoF/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpqGPEwf/test-config.toml---success: trueexit_code: 0----- stdout ------π [36mSquashing 2 commits into 1 (2 files, [32m+2[0m)...[0m+π [36mSquashing 2 commits into 1[0m (2 files, [32m+2[0m)...π [36mGenerating squash commit message...[0mπ‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0m[40m [0m [1mSquash commits from main[0mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snapindex cbb36fa45..28f560ae3 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_llm.snap@@ -13,18 +13,19 @@ info:GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"GIT_CONFIG_GLOBAL: /dev/nullGIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"WORKTRUNK_COMMIT_GENERATION__ARGS__0: "-c"WORKTRUNK_COMMIT_GENERATION__ARGS__1: "cat >/dev/null && echo 'feat: implement user authentication system'"WORKTRUNK_COMMIT_GENERATION__COMMAND: sh- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpBfVgOr/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp8GMkOm/test-config.toml---success: trueexit_code: 0----- stdout ------π [36mSquashing 2 commits into 1 (1 file, [32m+1[0m)...[0m+π [36mSquashing 2 commits into 1[0m (1 file, [32m+1[0m)...π [36mGenerating squash commit message...[0mπ‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0m[40m [0m [1mSquash commits from main[0m