workmux 0.1.167

An opinionated workflow tool that orchestrates git worktrees and tmux
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
use anyhow::{Context, Result, anyhow};
use minijinja::{AutoEscape, Environment};
use serde_json::{Map as JsonMap, Number as JsonNumber, Value as JsonValue};
use std::collections::{BTreeMap, HashSet};
use std::path::Path;

/// Reserved template variable names that cannot be used in foreach
const RESERVED_TEMPLATE_KEYS: &[&str] = &["base_name", "agent", "num", "index", "foreach_vars"];

#[derive(Debug, Clone)]
pub struct WorktreeSpec {
    pub branch_name: String,
    pub agent: Option<String>,
    pub template_context: JsonValue,
}

pub type TemplateEnv = Environment<'static>;

/// Create and configure the template environment with filters and escape settings.
pub fn create_template_env() -> TemplateEnv {
    let mut env = Environment::new();
    env.set_auto_escape_callback(|_| AutoEscape::None);
    env.set_keep_trailing_newline(true);
    env.add_filter("slugify", slugify_filter);
    env
}

/// Render a prompt body string with the given template context.
pub fn render_prompt_body(body: &str, env: &TemplateEnv, context: &JsonValue) -> Result<String> {
    env.render_str(body, context)
        .context("Failed to render prompt template")
}

/// Validate that all variables used in the template exist in the provided context.
/// Returns an error listing missing variables and available ones if validation fails.
pub fn validate_template_variables(
    env: &TemplateEnv,
    template_str: &str,
    context: &JsonValue,
) -> Result<()> {
    let tmpl = env
        .template_from_str(template_str)
        .context("Failed to parse template syntax")?;

    let required_vars = tmpl.undeclared_variables(true);

    let available_vars: HashSet<&str> = match context {
        JsonValue::Object(map) => map.keys().map(|k| k.as_str()).collect(),
        _ => HashSet::new(),
    };

    let missing_vars: Vec<&str> = required_vars
        .iter()
        .map(|s| s.as_str())
        .filter(|req| !available_vars.contains(req))
        .collect();

    if !missing_vars.is_empty() {
        let mut missing_sorted = missing_vars.clone();
        missing_sorted.sort();

        let mut available_sorted: Vec<&str> = available_vars.into_iter().collect();
        available_sorted.sort();

        return Err(anyhow!(
            "Template uses undefined variables: {}\nAvailable variables: {}",
            missing_sorted.join(", "),
            available_sorted.join(", ")
        ));
    }

    Ok(())
}

pub fn generate_worktree_specs(
    base_name: &str,
    agents: &[String],
    count: Option<u32>,
    foreach_rows: Option<&[BTreeMap<String, String>]>,
    env: &TemplateEnv,
    branch_template: &str,
) -> Result<Vec<WorktreeSpec>> {
    let is_multi_mode = foreach_rows.is_some() || count.is_some() || agents.len() > 1;

    if !is_multi_mode {
        let agent = agents.first().cloned();
        let num: Option<u32> = None;
        let foreach_vars = BTreeMap::<String, String>::new();
        let context = build_template_context(base_name, &agent, &num, None, &foreach_vars);

        // Intentional: in single-agent/instance mode the CLI keeps the provided
        // branch name verbatim so users can opt into templating only when they
        // request multiple worktrees.
        return Ok(vec![WorktreeSpec {
            branch_name: base_name.to_string(),
            agent,
            template_context: context,
        }]);
    }

    if let Some(rows) = foreach_rows {
        return rows
            .iter()
            .enumerate()
            .map(|(idx, vars)| {
                let index = Some((idx + 1) as u32); // 1-indexed to match num
                build_spec(
                    env,
                    branch_template,
                    base_name,
                    None,
                    None,
                    index,
                    vars.clone(),
                )
            })
            .collect();
    }

    if let Some(times) = count {
        let iterations = times as usize;
        let default_agent = agents.first().cloned();
        let mut specs = Vec::with_capacity(iterations);
        for idx in 0..iterations {
            let num = Some((idx + 1) as u32);
            let index = num; // index equals num for --count
            specs.push(build_spec(
                env,
                branch_template,
                base_name,
                default_agent.clone(),
                num,
                index,
                BTreeMap::new(),
            )?);
        }
        return Ok(specs);
    }

    if agents.is_empty() {
        return Ok(vec![build_spec(
            env,
            branch_template,
            base_name,
            None,
            None,
            None,
            BTreeMap::new(),
        )?]);
    }

    let mut specs = Vec::with_capacity(agents.len());
    for (idx, agent_name) in agents.iter().enumerate() {
        let index = Some((idx + 1) as u32);
        specs.push(build_spec(
            env,
            branch_template,
            base_name,
            Some(agent_name.clone()),
            None,
            index,
            BTreeMap::new(),
        )?);
    }
    Ok(specs)
}

fn build_spec(
    env: &TemplateEnv,
    branch_template: &str,
    base_name: &str,
    agent: Option<String>,
    num: Option<u32>,
    index: Option<u32>,
    foreach_vars: BTreeMap<String, String>,
) -> Result<WorktreeSpec> {
    // Extract agent from foreach_vars if present (treat "agent" as a special reserved key)
    let effective_agent = agent.or_else(|| foreach_vars.get("agent").cloned());

    let context = build_template_context(base_name, &effective_agent, &num, index, &foreach_vars);

    // Validate branch template before rendering
    validate_template_variables(env, branch_template, &context)
        .context("Invalid branch name template")?;

    let branch_name = env
        .render_str(branch_template, &context)
        .context("Failed to render branch template")?;
    Ok(WorktreeSpec {
        branch_name,
        agent: effective_agent,
        template_context: context,
    })
}

/// Extract the display name from an agent path or name.
/// For paths like "/usr/local/bin/claude", returns "claude".
/// For simple names like "claude", returns as-is.
fn agent_display_name(agent: &str) -> String {
    Path::new(agent)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(agent)
        .to_string()
}

fn build_template_context(
    base_name: &str,
    agent: &Option<String>,
    num: &Option<u32>,
    index: Option<u32>,
    foreach_vars: &BTreeMap<String, String>,
) -> JsonValue {
    let mut context = JsonMap::new();
    context.insert(
        "base_name".to_string(),
        JsonValue::String(base_name.to_string()),
    );

    // Use just the filename (without path) for template context so branch names
    // are clean (e.g., "feature-claude" not "feature-usr-local-bin-claude")
    let agent_value = agent
        .as_ref()
        .map(|value| JsonValue::String(agent_display_name(value)))
        .unwrap_or(JsonValue::Null);
    context.insert("agent".to_string(), agent_value);

    let num_value = num
        .as_ref()
        .map(|value| JsonValue::Number(JsonNumber::from(*value)))
        .unwrap_or(JsonValue::Null);
    context.insert("num".to_string(), num_value);

    let index_value = index
        .map(|value| JsonValue::Number(JsonNumber::from(value)))
        .unwrap_or(JsonValue::Null);
    context.insert("index".to_string(), index_value);

    let mut foreach_json = JsonMap::new();
    for (key, value) in foreach_vars {
        // Filter out ALL reserved keys to avoid collisions in templates
        // Reserved keys: base_name, agent, num, foreach_vars
        if !RESERVED_TEMPLATE_KEYS.contains(&key.as_str()) {
            foreach_json.insert(key.clone(), JsonValue::String(value.clone()));
            context.insert(key.clone(), JsonValue::String(value.clone()));
        }
    }
    context.insert("foreach_vars".to_string(), JsonValue::Object(foreach_json));

    JsonValue::Object(context)
}

pub fn parse_foreach_matrix(input: &str) -> Result<Vec<BTreeMap<String, String>>> {
    let mut columns: Vec<(String, Vec<String>)> = Vec::new();
    let mut seen = HashSet::new();

    for raw in input.split(';') {
        let trimmed = raw.trim();
        if trimmed.is_empty() {
            continue;
        }

        let (key, values_str) = trimmed.split_once(':').ok_or_else(|| {
            anyhow!(
                "Invalid --foreach segment '{}'. Use the format name:value1,value2",
                trimmed
            )
        })?;

        let key = key.trim();
        if key.is_empty() {
            return Err(anyhow!(
                "Invalid --foreach segment '{}': variable name cannot be empty",
                trimmed
            ));
        }
        if !seen.insert(key.to_string()) {
            return Err(anyhow!(
                "Duplicate variable '{}' found in --foreach option",
                key
            ));
        }

        let values: Vec<String> = values_str
            .split(',')
            .map(|v| v.trim())
            .filter(|v| !v.is_empty())
            .map(|v| v.to_string())
            .collect();

        if values.is_empty() {
            return Err(anyhow!(
                "Variable '{}' must have at least one value in --foreach",
                key
            ));
        }

        columns.push((key.to_string(), values));
    }

    if columns.is_empty() {
        return Err(anyhow!(
            "--foreach must include at least one variable with values"
        ));
    }

    let expected_len = columns[0].1.len();
    if columns
        .iter()
        .any(|(_, values)| values.len() != expected_len)
    {
        return Err(anyhow!(
            "All --foreach variables must have the same number of values"
        ));
    }

    let mut rows = Vec::with_capacity(expected_len);
    for idx in 0..expected_len {
        let mut map = BTreeMap::new();
        for (key, values) in &columns {
            map.insert(key.clone(), values[idx].clone());
        }
        rows.push(map);
    }

    Ok(rows)
}

fn slugify_filter(input: String) -> String {
    input
        .to_lowercase()
        .chars()
        .map(|c| match c {
            'a'..='z' | '0'..='9' => c,
            _ => '-',
        })
        .collect::<String>()
        .split('-')
        .filter(|segment| !segment.is_empty())
        .collect::<Vec<_>>()
        .join("-")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prompt::Prompt;
    use std::fs;
    use std::path::PathBuf;

    fn create_test_env() -> TemplateEnv {
        create_template_env()
    }

    #[test]
    fn parse_foreach_matrix_parses_rows() {
        let rows = parse_foreach_matrix("env:dev,prod;region:us,eu").unwrap();
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].get("env").unwrap(), "dev");
        assert_eq!(rows[0].get("region").unwrap(), "us");
        assert_eq!(rows[1].get("env").unwrap(), "prod");
        assert_eq!(rows[1].get("region").unwrap(), "eu");
    }

    #[test]
    fn parse_foreach_matrix_requires_matching_lengths() {
        assert!(parse_foreach_matrix("env:dev,prod;region:us").is_err());
    }

    #[test]
    fn generate_specs_with_agents() {
        let env = create_test_env();
        let agents = vec!["claude".to_string(), "gemini".to_string()];
        let specs = generate_worktree_specs(
            "feature",
            &agents,
            None,
            None,
            &env,
            "{{ base_name }}{% if agent %}-{{ agent }}{% endif %}",
        )
        .expect("specs");
        let summary: Vec<(String, Option<String>)> = specs
            .into_iter()
            .map(|spec| (spec.branch_name, spec.agent))
            .collect();
        assert_eq!(
            summary,
            vec![
                ("feature-claude".to_string(), Some("claude".to_string())),
                ("feature-gemini".to_string(), Some("gemini".to_string()))
            ]
        );
    }

    #[test]
    fn generate_specs_with_count_assigns_numbers() {
        let env = create_test_env();
        let specs = generate_worktree_specs(
            "feature",
            &[],
            Some(2),
            None,
            &env,
            "{{ base_name }}{% if num %}-{{ num }}{% endif %}",
        )
        .expect("specs");
        let names: Vec<String> = specs.into_iter().map(|s| s.branch_name).collect();
        assert_eq!(
            names,
            vec!["feature-1".to_string(), "feature-2".to_string()]
        );
    }

    #[test]
    fn single_agent_override_preserves_branch_name() {
        let env = create_test_env();
        let specs = generate_worktree_specs(
            "feature",
            &[String::from("gemini")],
            None,
            None,
            &env,
            "{{ base_name }}{% if agent %}-{{ agent }}{% endif %}",
        )
        .expect("specs");
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].branch_name, "feature");
        assert_eq!(specs[0].agent.as_deref(), Some("gemini"));
    }

    #[test]
    fn foreach_context_exposes_variables() {
        let env = create_test_env();
        let rows = parse_foreach_matrix("platform:ios,android;lang:swift,kotlin").expect("parse");
        let specs =
            generate_worktree_specs("feature", &[], None, Some(&rows), &env, "{{ base_name }}")
                .expect("specs");
        let rendered = env
            .render_str("{{ platform }}-{{ lang }}", &specs[0].template_context)
            .expect("prompt render");
        assert_eq!(rendered, "ios-swift");
    }

    #[test]
    fn foreach_provides_index_variable() {
        let env = create_test_env();
        let rows = parse_foreach_matrix("region:us,eu").expect("parse");
        let specs = generate_worktree_specs(
            "deploy",
            &[],
            None,
            Some(&rows),
            &env,
            "{{ base_name }}-{{ index }}-{{ region }}",
        )
        .expect("specs");

        assert_eq!(specs.len(), 2);
        assert_eq!(specs[0].branch_name, "deploy-1-us");
        assert_eq!(specs[1].branch_name, "deploy-2-eu");

        // Verify index is available in the template context for prompt rendering
        assert_eq!(specs[0].template_context["index"], 1);
        assert_eq!(specs[1].template_context["index"], 2);
    }

    #[test]
    fn render_prompt_template_inline_renders_variables() {
        let env = create_test_env();
        let mut context_map = JsonMap::new();
        context_map.insert(
            "branch".to_string(),
            JsonValue::String("feature-123".to_string()),
        );
        let context = JsonValue::Object(context_map);

        let prompt = Prompt::Inline("Working on {{ branch }}".to_string());
        let result = render_prompt_template(&prompt, &env, &context).expect("render success");

        match result {
            Prompt::Inline(text) => assert_eq!(text, "Working on feature-123"),
            _ => panic!("Expected Inline prompt"),
        }
    }

    #[test]
    fn render_prompt_template_from_file_reads_and_renders() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let env = create_test_env();
        let mut context_map = JsonMap::new();
        context_map.insert(
            "name".to_string(),
            JsonValue::String("test-branch".to_string()),
        );
        let context = JsonValue::Object(context_map);

        let mut temp_file = NamedTempFile::new().expect("create temp file");
        writeln!(temp_file, "Branch: {{{{ name }}}}").expect("write to temp file");
        let temp_path = temp_file.path().to_path_buf();

        let prompt = Prompt::FromFile(temp_path);
        let result = render_prompt_template(&prompt, &env, &context).expect("render success");

        match result {
            Prompt::Inline(text) => assert_eq!(text, "Branch: test-branch\n"),
            _ => panic!("Expected Inline prompt"),
        }
    }

    #[test]
    fn render_prompt_template_from_nonexistent_file_fails() {
        let env = create_test_env();
        let context = JsonValue::Null;

        let prompt = Prompt::FromFile(PathBuf::from("/nonexistent/path/to/file.txt"));
        let result = render_prompt_template(&prompt, &env, &context);

        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Failed to read prompt file")
        );
    }

    #[test]
    fn branch_template_renders_with_foreach_vars() {
        let env = create_test_env();
        let mut foreach_vars = BTreeMap::new();
        foreach_vars.insert("platform".to_string(), "ios".to_string());
        foreach_vars.insert("lang".to_string(), "swift".to_string());

        let context = build_template_context("feature", &None, &None, None, &foreach_vars);
        // MiniJinja doesn't support unpacking in for loops, so we iterate over keys
        let template = "{{ base_name }}{% for key in foreach_vars %}-{{ foreach_vars[key] | slugify }}{% endfor %}";
        let result = env.render_str(template, &context).expect("render");

        // The foreach_vars iteration should include both platform and lang values
        // BTreeMap is sorted, so lang comes before platform alphabetically
        assert_eq!(result, "feature-swift-ios");
    }

    #[test]
    fn foreach_with_agent_key_populates_spec_agent() {
        use crate::prompt::foreach_from_frontmatter;

        let env = create_test_env();
        let mut map = BTreeMap::new();
        map.insert(
            "agent".to_string(),
            vec!["claude".to_string(), "gemini".to_string()],
        );

        let rows = foreach_from_frontmatter(&map).expect("conversion success");
        let specs = generate_worktree_specs(
            "feature",
            &[],
            None,
            Some(&rows),
            &env,
            "{{ base_name }}{% if agent %}-{{ agent | slugify }}{% endif %}{% for key in foreach_vars %}-{{ foreach_vars[key] | slugify }}{% endfor %}",
        )
        .expect("specs");

        assert_eq!(specs.len(), 2);

        // First spec should have agent=claude and branch name should NOT include agent twice
        assert_eq!(specs[0].branch_name, "feature-claude");
        assert_eq!(specs[0].agent.as_deref(), Some("claude"));

        // Second spec should have agent=gemini
        assert_eq!(specs[1].branch_name, "feature-gemini");
        assert_eq!(specs[1].agent.as_deref(), Some("gemini"));
    }

    #[test]
    fn foreach_with_agent_and_other_vars_filters_agent_from_iteration() {
        use crate::prompt::foreach_from_frontmatter;

        let env = create_test_env();
        let mut map = BTreeMap::new();
        map.insert(
            "agent".to_string(),
            vec!["claude".to_string(), "gemini".to_string()],
        );
        map.insert(
            "platform".to_string(),
            vec!["ios".to_string(), "android".to_string()],
        );

        let rows = foreach_from_frontmatter(&map).expect("conversion success");
        let specs = generate_worktree_specs(
            "feature",
            &[],
            None,
            Some(&rows),
            &env,
            "{{ base_name }}{% if agent %}-{{ agent | slugify }}{% endif %}{% for key in foreach_vars %}-{{ foreach_vars[key] | slugify }}{% endfor %}",
        )
        .expect("specs");

        assert_eq!(specs.len(), 2);

        // Branch names should be: base-agent-platform (NOT base-agent-agent-platform or base-agent-platform-agent)
        // BTreeMap is sorted, so "agent" comes before "platform" alphabetically, but agent is filtered from foreach_vars
        assert_eq!(specs[0].branch_name, "feature-claude-ios");
        assert_eq!(specs[0].agent.as_deref(), Some("claude"));

        assert_eq!(specs[1].branch_name, "feature-gemini-android");
        assert_eq!(specs[1].agent.as_deref(), Some("gemini"));
    }

    #[test]
    fn foreach_filters_all_reserved_keys() {
        use crate::prompt::foreach_from_frontmatter;

        let env = create_test_env();
        let mut map = BTreeMap::new();
        // Try to use reserved keys in foreach
        map.insert(
            "base_name".to_string(),
            vec!["bad1".to_string(), "bad2".to_string()],
        );
        map.insert(
            "num".to_string(),
            vec!["bad3".to_string(), "bad4".to_string()],
        );
        map.insert(
            "foreach_vars".to_string(),
            vec!["bad5".to_string(), "bad6".to_string()],
        );
        map.insert(
            "agent".to_string(),
            vec!["bad7".to_string(), "bad8".to_string()],
        );
        map.insert(
            "platform".to_string(),
            vec!["ios".to_string(), "android".to_string()],
        );

        let rows = foreach_from_frontmatter(&map).expect("conversion success");

        // Verify that reserved keys are NOT in the rows at the top level (only in the BTreeMap for lookup)
        // But the row itself should still contain them for extraction
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].get("platform").unwrap(), "ios");
        assert_eq!(rows[1].get("platform").unwrap(), "android");

        let specs = generate_worktree_specs(
            "base",
            &[],
            None,
            Some(&rows),
            &env,
            "{{ base_name }}{% if agent %}-{{ agent | slugify }}{% endif %}{% for key in foreach_vars %}-{{ foreach_vars[key] | slugify }}{% endfor %}",
        )
        .expect("specs");

        // Branch name should only include platform, not the reserved keys
        // Reserved keys should be filtered from foreach_vars iteration
        assert_eq!(specs[0].branch_name, "base-bad7-ios");
        assert_eq!(specs[1].branch_name, "base-bad8-android");

        // base_name should be "base" (from function param), not "bad1" or "bad2"
        let context0 = &specs[0].template_context;
        assert_eq!(context0["base_name"].as_str().unwrap(), "base");

        // agent should be from foreach (bad7/bad8), not overwritten by reserved key collision
        assert_eq!(context0["agent"].as_str().unwrap(), "bad7");
    }

    #[test]
    fn validate_template_variables_detects_missing_vars() {
        let env = create_test_env();
        let mut map = JsonMap::new();
        map.insert("exists".to_string(), JsonValue::String("foo".to_string()));
        let context = JsonValue::Object(map);

        let valid = validate_template_variables(&env, "Hello {{ exists }}", &context);
        assert!(valid.is_ok());

        let invalid = validate_template_variables(&env, "Hello {{ missing }}", &context);
        assert!(invalid.is_err());
        let err_msg = invalid.unwrap_err().to_string();
        assert!(
            err_msg.contains("undefined variables: missing"),
            "Error message should list 'missing': {}",
            err_msg
        );
        assert!(
            err_msg.contains("Available variables: exists"),
            "Error message should list 'exists' as available: {}",
            err_msg
        );
    }

    #[test]
    fn validate_template_variables_handles_filters() {
        let env = create_test_env();
        let mut map = JsonMap::new();
        map.insert("text".to_string(), JsonValue::String("foo".to_string()));
        let context = JsonValue::Object(map);

        // Filters like | slugify shouldn't trigger false positives
        let valid = validate_template_variables(&env, "{{ text | slugify }}", &context);
        assert!(valid.is_ok());
    }

    #[test]
    fn validate_template_variables_handles_conditionals() {
        let env = create_test_env();
        let mut map = JsonMap::new();
        map.insert("agent".to_string(), JsonValue::Null);
        map.insert(
            "base_name".to_string(),
            JsonValue::String("feature".to_string()),
        );
        let context = JsonValue::Object(map);

        // Conditionals should validate variables inside them
        let valid = validate_template_variables(
            &env,
            "{{ base_name }}{% if agent %}-{{ agent }}{% endif %}",
            &context,
        );
        assert!(valid.is_ok());

        // Missing variable in conditional should error
        let invalid = validate_template_variables(
            &env,
            "{{ base_name }}{% if typo %}-{{ typo }}{% endif %}",
            &context,
        );
        assert!(invalid.is_err());
        assert!(invalid.unwrap_err().to_string().contains("typo"));
    }

    #[test]
    fn validate_template_variables_handles_loops() {
        let env = create_test_env();
        let mut map = JsonMap::new();
        let mut foreach_vars = JsonMap::new();
        foreach_vars.insert("platform".to_string(), JsonValue::String("ios".to_string()));
        map.insert("foreach_vars".to_string(), JsonValue::Object(foreach_vars));
        let context = JsonValue::Object(map);

        // Loop variable access should work
        let valid = validate_template_variables(
            &env,
            "{% for key in foreach_vars %}-{{ foreach_vars[key] }}{% endfor %}",
            &context,
        );
        assert!(valid.is_ok());
    }

    #[test]
    fn generate_specs_validates_branch_template() {
        let env = create_test_env();
        // This should fail because {{ bad_var }} doesn't exist in the standard context
        let result = generate_worktree_specs(
            "base",
            &["claude".to_string(), "gemini".to_string()],
            None,
            None,
            &env,
            "{{ base_name }}-{{ bad_var }}",
        );

        assert!(result.is_err());
        let err = result.unwrap_err();
        // Check the error chain with Debug format which shows the full chain
        let err_chain = format!("{:?}", err);
        assert!(
            err_chain.contains("Invalid branch name template"),
            "Error should mention 'Invalid branch name template': {}",
            err_chain
        );
        assert!(
            err_chain.contains("bad_var"),
            "Error chain should mention bad_var: {}",
            err_chain
        );
    }

    #[test]
    fn validate_template_variables_multiple_missing() {
        let env = create_test_env();
        let mut map = JsonMap::new();
        map.insert("exists".to_string(), JsonValue::String("foo".to_string()));
        let context = JsonValue::Object(map);

        let invalid =
            validate_template_variables(&env, "{{ missing1 }} and {{ missing2 }}", &context);
        assert!(invalid.is_err());
        let err_msg = invalid.unwrap_err().to_string();
        assert!(err_msg.contains("missing1"));
        assert!(err_msg.contains("missing2"));
    }

    // Helper function for tests
    fn render_prompt_template(
        prompt: &Prompt,
        env: &TemplateEnv,
        context: &JsonValue,
    ) -> Result<Prompt> {
        let template_str = match prompt {
            Prompt::Inline(text) => text.clone(),
            Prompt::FromFile(path) => fs::read_to_string(path)
                .with_context(|| format!("Failed to read prompt file '{}'", path.display()))?,
        };

        let rendered = env
            .render_str(&template_str, context)
            .context("Failed to render prompt template")?;
        Ok(Prompt::Inline(rendered))
    }
}