llman 0.0.68

A tool for managing LLM application rules(prompts) ...
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
use crate::config_schema::ConfigSchemaKind;
use crate::config_schema::{LLMANSPEC_SCHEMA_URL, prepend_schema_header, validate_yaml_value};
use crate::fs_utils::atomic_write_with_mode;
use crate::sdd::shared::constants::LLMANSPEC_CONFIG_FILE;
use anyhow::{Result, anyhow};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};

const EXPECTED_SCHEMA: &str = "spec-driven";

pub(crate) const OPTIONAL_SKILL_NAMES: &[&str] = &[
    "llman-sdd-continue",
    "llman-sdd-ff",
    "llman-sdd-validate",
    "llman-sdd-arch-review",
    "llman-sdd-wayfinder",
    "llman-sdd-research",
];

const DEFAULT_CONFIG_EN: &str = r#"schema: spec-driven
locale: en

# Optional extra skills (disabled by default, uncomment to enable).
# On `llman sdd init --update`, managed candidates are:
#   default workflow skills + entries listed here (extra_skills extend).
# Then `.agents/skills/llman-sdd-*` is scanned: anything not in that candidate
# set is removed first, then candidates are written/updated. Only the
# `llman-sdd-` prefix is touched (custom skills without that prefix are kept).
# Deprecated shipped skills (e.g. removed from defaults) are cleaned this way.
# extra_skills:
#   - llman-sdd-continue
#   - llman-sdd-ff
#   - llman-sdd-validate
#   - llman-sdd-arch-review
#   - llman-sdd-wayfinder
#   - llman-sdd-research

# BDD integration (optional, uncomment to enable)
# bdd:
#   framework: pytest-bdd
#   feature_dir: tests/features/
#   # default_language: en
#   # Filtered runners: include {feature_*} so validate --all/--specs runs per capability.
#   # run_command: "pytest {feature_dir} -k {feature_name} -v"
#   # Project-wide runners (no placeholders): validate --all/--specs runs the suite once (batch-once).
#   # run_command: "cargo test --features bdd"
#   # verify_prompt: |
#   #   Map test failures to requirement IDs.
"#;

const DEFAULT_CONFIG_ZH_HANS: &str = r#"schema: spec-driven
locale: zh-Hans

# 可选额外技能(默认禁用,取消注释以启用)。
# 运行 `llman sdd init --update` 时,管理候选集为:
#   默认 workflow 技能 + 本列表(extra_skills 扩展)。
# 然后扫描 `.agents/skills/llman-sdd-*`:不在候选集中的先删除,再写入/更新候选。
# 仅处理 `llman-sdd-` 前缀(无此前缀的自定义技能不会被删)。
# 已废弃的内置技能(从默认集移除后)会因此被正确清理。
# extra_skills:
#   - llman-sdd-continue
#   - llman-sdd-ff
#   - llman-sdd-validate
#   - llman-sdd-arch-review
#   - llman-sdd-wayfinder
#   - llman-sdd-research

# BDD 集成(可选,取消注释以启用)
# bdd:
#   framework: pytest-bdd
#   feature_dir: tests/features/
#   # default_language: zh-CN
#   # 过滤型 runner:写 {feature_*},validate --all/--specs 按 capability 分别执行。
#   # run_command: "pytest {feature_dir} -k {feature_name} -v"
#   # 项目级 runner(无占位符):validate --all/--specs 整批只跑一次(batch-once)。
#   # run_command: "cargo test --features bdd"
#   # verify_prompt: |
#   #   将测试失败映射到对应的 requirement ID。
"#;

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default)]
#[schemars(description = "Archive behaviour configuration.")]
pub struct ArchiveConfig {
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "When true, unchecked tasks without a defer link are errors (not just warnings). Default: false (transition period)."
    )]
    pub strict_defer: Option<bool>,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "Minimum task completion ratio (0.0–1.0) required for archiving. Default: none (disabled)."
    )]
    pub min_completion_ratio: Option<f64>,
}

impl ArchiveConfig {
    pub fn strict_defer(&self) -> bool {
        self.strict_defer.unwrap_or(false)
    }

    pub fn min_completion_ratio(&self) -> Option<f64> {
        self.min_completion_ratio
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default)]
#[schemars(
    description = "BDD integration settings. When defined, enables feature-as-spec mode (directory-based .feature validation) and BDD-aware verify prompts."
)]
pub struct BddConfig {
    /// BDD framework identifier (optional, only used to derive a default run_command
    /// when run_command is not set: pytest-bdd, rstest-bdd, cucumber-js, behave).
    #[serde(default)]
    #[schemars(
        description = "BDD framework identifier (optional). Only used to derive a default run_command when run_command is unset."
    )]
    pub framework: String,

    /// Root directory for .feature files, relative to project root
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(description = "Root directory for .feature files, relative to project root.")]
    pub feature_dir: Option<String>,

    /// Gherkin parsing language (default: "en")
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(description = "Gherkin parsing language code (e.g. 'en', 'zh-CN'). Default: 'en'.")]
    pub default_language: Option<String>,

    /// Custom test run command with placeholders: {feature_dir}, {feature_name}, {feature_path}
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "Custom test run command. Placeholders: {feature_dir}, {feature_name}, {feature_path}. Without placeholders, validate --all/--specs runs the command at most once per batch (batch-once)."
    )]
    pub run_command: Option<String>,

    /// Extra prompt injected during verify phase
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(description = "Extra prompt text injected during verify phase.")]
    pub verify_prompt: Option<String>,

    /// Declared harness-binding sources (r2): how llman decides which
    /// `.feature` scenarios are actually bound/executed by the consumer.
    /// When absent, the legacy count-all harness criterion stays in effect.
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "Declared harness-binding sources (r2). kind=tags: a scenario is bound when its tags contain all listed tags; kind=scenario-attrs: extract path/name literal pairs from #[scenario(...)] attribute blocks in glob-matched Rust files. Multiple sources union with dedup."
    )]
    pub bindings: Option<Vec<BindingSource>>,
}

/// One harness-binding source declaration (`bdd.bindings[]`, r2).
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
#[serde(tag = "kind")]
pub enum BindingSource {
    /// Tag predicate source: a `.feature` scenario is bound when its tags
    /// contain ALL listed tags (`@` prefix optional).
    #[serde(rename = "tags")]
    Tags {
        /// Required tag names; MUST NOT be empty.
        #[serde(default)]
        #[schemars(
            length(min = 1),
            description = "Required tag names (leading '@' optional). MUST NOT be empty."
        )]
        tags: Vec<String>,
    },
    /// Per-scenario attribute source: extract `path = "..."` / `name = "..."`
    /// literal pairs from `#[scenario( ... )]` attribute blocks inside files
    /// matching the given globs (repo-root relative). Feature paths outside
    /// `llmanspec/specs/**` are ignored.
    #[serde(rename = "scenario-attrs")]
    ScenarioAttrs {
        /// Glob patterns of Rust sources to scan; MUST NOT be empty.
        #[serde(default)]
        #[schemars(
            length(min = 1),
            description = "Glob patterns (repo-root relative) of Rust source files to scan for #[scenario(path=..., name=...)] attributes. MUST NOT be empty."
        )]
        files: Vec<String>,
    },
}

impl BddConfig {
    pub fn effective_run_command(&self) -> String {
        if let Some(cmd) = &self.run_command {
            return cmd.clone();
        }
        match self.framework.as_str() {
            "pytest-bdd" => "pytest {feature_dir} -k {feature_name} -v".into(),
            "rstest-bdd" => "cargo test --features bdd".into(),
            "cucumber-js" => "npx cucumber-js {feature_path}".into(),
            "behave" => "behave {feature_path}".into(),
            _ => "echo 'No run_command configured. Set bdd.run_command in config.yaml'".into(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[schemars(description = "SDD project configuration for llmanspec.")]
pub struct SddConfig {
    #[schemars(description = "Schema identifier. Must be \"spec-driven\".")]
    pub schema: String,

    #[serde(default = "default_locale")]
    #[schemars(description = "Locale used for SDD templates and skills.")]
    pub locale: String,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "Additional optional SDD skills to enable (extend candidates). \
        On init --update, candidates = default workflow skills + this list; \
        then `.agents/skills/llman-sdd-*` not in candidates are removed before rewrite. \
        Valid values: llman-sdd-continue, llman-sdd-ff, \
        llman-sdd-validate, llman-sdd-arch-review, \
        llman-sdd-wayfinder, llman-sdd-research."
    )]
    pub extra_skills: Option<Vec<String>>,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(description = "Archive behaviour settings (defer tracking, completion gates).")]
    pub archive: Option<ArchiveConfig>,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "BDD integration settings. When defined, enables feature-as-spec mode (directory-based .feature validation) and BDD-aware verify prompts."
    )]
    pub bdd: Option<BddConfig>,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(description = "Unified Git-native flow tuning (branch prefix, worktree).")]
    pub sdd: Option<FlowConfig>,
}

/// Unified Git-native flow tuning (r111/r116).
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, Default)]
#[schemars(description = "Unified Git-native flow settings.")]
pub struct FlowConfig {
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "Prefix for feature branches created by `change start`. Default: \"sdd/\"."
    )]
    pub branch_prefix: Option<String>,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "Root directory for worktrees created by `change start --worktree`. \
        Default: <repo>/.git/sdd/worktrees/."
    )]
    pub worktree_root: Option<PathBuf>,

    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(
        description = "Worktree directory naming: \"id\" (default, = change-id) or \
        \"hash\" (deterministic base32(sha256(change_id))[:8], letters only)."
    )]
    pub worktree_naming: Option<String>,
}

impl Default for SddConfig {
    fn default() -> Self {
        Self {
            schema: EXPECTED_SCHEMA.to_string(),
            locale: default_locale(),
            extra_skills: None,
            archive: None,
            bdd: None,
            sdd: None,
        }
    }
}

impl SddConfig {
    pub fn archive_config(&self) -> ArchiveConfig {
        self.archive.clone().unwrap_or_default()
    }
}

fn default_locale() -> String {
    "en".to_string()
}

pub fn config_path(llmanspec_dir: &Path) -> PathBuf {
    llmanspec_dir.join(LLMANSPEC_CONFIG_FILE)
}

pub fn config_with_locale(locale: Option<&str>) -> SddConfig {
    let mut config = SddConfig::default();
    if let Some(locale) = locale {
        config.locale = normalize_locale(locale);
    }
    config
}

pub fn load_config(llmanspec_dir: &Path) -> Result<Option<SddConfig>> {
    let path = config_path(llmanspec_dir);
    if !path.exists() {
        return Ok(None);
    }
    let content = fs::read_to_string(&path)
        .map_err(|err| anyhow!(t!("sdd.config.read_failed", error = err)))?;
    let yaml_value: serde_yaml::Value = serde_yaml::from_str(&content)
        .map_err(|err| anyhow!(t!("sdd.config.parse_failed", error = err)))?;

    // Reject old-format configs
    reject_old_format(&yaml_value, &path)?;

    if let Err(error) = validate_yaml_value(ConfigSchemaKind::Llmanspec, &yaml_value) {
        return Err(anyhow!(t!(
            "sdd.config.schema_invalid",
            path = path.display(),
            error = error
        )));
    }
    let mut config: SddConfig = serde_yaml::from_value(yaml_value)
        .map_err(|err| anyhow!(t!("sdd.config.parse_failed", error = err)))?;

    if config.schema.trim() != EXPECTED_SCHEMA {
        return Err(anyhow!(
            "Unsupported schema '{}'. Expected '{}'.",
            config.schema.trim(),
            EXPECTED_SCHEMA
        ));
    }

    if let Some(ref extra) = config.extra_skills {
        let valid: HashSet<&str> = OPTIONAL_SKILL_NAMES.iter().copied().collect();
        for name in extra {
            if !valid.contains(name.as_str()) {
                return Err(anyhow!(
                    "Unknown extra_skills entry '{}'. Valid options: {}",
                    name,
                    OPTIONAL_SKILL_NAMES.join(", ")
                ));
            }
        }
    }

    config.locale = normalize_locale(&config.locale);
    config.schema = EXPECTED_SCHEMA.to_string();
    Ok(Some(config))
}

fn reject_old_format(value: &serde_yaml::Value, path: &Path) -> Result<()> {
    let Some(mapping) = value.as_mapping() else {
        return Ok(());
    };
    let has_old_keys = mapping
        .keys()
        .any(|k| k.as_str() == Some("spec_style") || k.as_str() == Some("version"));
    if has_old_keys {
        return Err(anyhow!(
            "Old config format detected in {}. Please run `llman sdd init` to reinitialize.",
            path.display()
        ));
    }
    Ok(())
}

pub fn load_required_config(llmanspec_dir: &Path) -> Result<SddConfig> {
    load_config(llmanspec_dir)?.ok_or_else(|| {
        let path = config_path(llmanspec_dir);
        anyhow!(t!("sdd.config.missing", path = path.display()))
    })
}

pub fn write_default_config(llmanspec_dir: &Path, locale: &str) -> Result<()> {
    let path = config_path(llmanspec_dir);
    let raw = match normalize_locale(locale).as_str() {
        "zh-Hans" => DEFAULT_CONFIG_ZH_HANS,
        _ => DEFAULT_CONFIG_EN,
    };
    let content = prepend_schema_header(raw, LLMANSPEC_SCHEMA_URL);
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    atomic_write_with_mode(&path, content.as_bytes(), None)?;
    Ok(())
}

pub fn load_or_create_config(llmanspec_dir: &Path) -> Result<SddConfig> {
    match load_config(llmanspec_dir)? {
        Some(config) => Ok(config),
        None => {
            let config = SddConfig::default();
            write_default_config(llmanspec_dir, &config.locale)?;
            Ok(config)
        }
    }
}

pub fn write_config(llmanspec_dir: &Path, config: &SddConfig) -> Result<()> {
    let path = config_path(llmanspec_dir);
    let content = serde_yaml::to_string(config)
        .map_err(|err| anyhow!(t!("sdd.config.serialize_failed", error = err)))?;
    let content = prepend_schema_header(&content, LLMANSPEC_SCHEMA_URL);
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    atomic_write_with_mode(&path, content.as_bytes(), None)
        .map_err(|err| anyhow!(t!("sdd.config.write_failed", error = err)))?;
    Ok(())
}

pub fn normalize_locale(value: &str) -> String {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return default_locale();
    }
    let lower = trimmed.to_lowercase();
    if lower == "zh" || lower.starts_with("zh-hans") || lower == "zh-cn" {
        return "zh-Hans".to_string();
    }
    if lower.starts_with("en") {
        return "en".to_string();
    }
    trimmed.to_string()
}

pub fn locale_fallbacks(locale: &str) -> Vec<String> {
    let normalized = normalize_locale(locale);
    let mut seen = HashSet::new();
    let mut locales = Vec::new();

    push_unique(&mut locales, &mut seen, normalized.clone());
    if let Some((lang, _)) = normalized.split_once('-') {
        push_unique(&mut locales, &mut seen, lang.to_string());
    }
    push_unique(&mut locales, &mut seen, "en".to_string());

    locales
}

fn push_unique(locales: &mut Vec<String>, seen: &mut HashSet<String>, value: String) {
    if seen.insert(value.clone()) {
        locales.push(value);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn normalize_locale_defaults_to_en() {
        assert_eq!(normalize_locale(""), "en");
        assert_eq!(normalize_locale("en-US"), "en");
        assert_eq!(normalize_locale("zh"), "zh-Hans");
        assert_eq!(normalize_locale("zh-cn"), "zh-Hans");
    }

    #[test]
    fn locale_fallbacks_include_en() {
        let fallbacks = locale_fallbacks("zh-Hans");
        assert_eq!(fallbacks, vec!["zh-Hans", "zh", "en"]);
        let fallbacks = locale_fallbacks("en");
        assert_eq!(fallbacks, vec!["en"]);
    }

    #[test]
    fn load_config_normalizes_locale() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        let path = config_path(llmanspec_dir);
        let content = "schema: spec-driven\nlocale: zh-cn\n";
        fs::write(&path, content).expect("write config");
        let config = load_config(llmanspec_dir).expect("load").expect("config");
        assert_eq!(config.locale, "zh-Hans");
    }

    #[test]
    fn load_config_rejects_old_format() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        let path = config_path(llmanspec_dir);
        let content = "version: 1\nspec_style: ison\nlocale: en\n";
        fs::write(&path, content).expect("write config");
        let result = load_config(llmanspec_dir);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Old config format detected"), "got: {err}");
    }

    #[test]
    fn load_config_rejects_wrong_schema() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        let path = config_path(llmanspec_dir);
        let content = "schema: other\nlocale: en\n";
        fs::write(&path, content).expect("write config");
        let result = load_config(llmanspec_dir);
        assert!(result.is_err());
    }

    #[test]
    fn bindings_deserialize_tags_and_scenario_attrs() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        let path = config_path(llmanspec_dir);
        let content = "\
schema: spec-driven
bdd:
  run_command: \"cargo test --features bdd\"
  bindings:
    - kind: tags
      tags: [executable]
    - kind: scenario-attrs
      files: [\"tests/bdd/bindings_*.rs\"]
";
        fs::write(&path, content).expect("write config");
        let config = load_config(llmanspec_dir).expect("load").expect("config");
        let bdd = config.bdd.expect("bdd");
        let bindings = bdd.bindings.expect("bindings");
        assert_eq!(bindings.len(), 2);
        assert_eq!(
            bindings[0],
            BindingSource::Tags {
                tags: vec!["executable".to_string()]
            }
        );
        assert_eq!(
            bindings[1],
            BindingSource::ScenarioAttrs {
                files: vec!["tests/bdd/bindings_*.rs".to_string()]
            }
        );
    }

    #[test]
    fn bindings_absent_is_none() {
        // Pin the serde(default) behavior: bdd present, bindings omitted.
        let config: SddConfig = serde_yaml::from_str(
            "\
schema: spec-driven
bdd:
  run_command: \"cargo test --features bdd\"
",
        )
        .expect("parse");
        let bdd = config.bdd.expect("bdd block");
        assert!(bdd.bindings.is_none(), "omitted bindings must stay None");
    }

    #[test]
    fn bindings_reject_unknown_kind() {
        let result = serde_yaml::from_str::<SddConfig>(
            "\
schema: spec-driven
bdd:
  bindings:
    - kind: magic
",
        );
        assert!(result.is_err(), "unknown kind must fail deserialization");
    }

    #[test]
    fn bindings_reject_empty_tags_via_schema() {
        // Empty `tags` violates the schemars length(min=1) constraint that the
        // runtime YAML schema validation enforces (config-schemas r73).
        let yaml: serde_yaml::Value = serde_yaml::from_str(
            "\
schema: spec-driven
bdd:
  bindings:
    - kind: tags
      tags: []
",
        )
        .expect("yaml");
        let err = crate::config_schema::validate_yaml_value(
            crate::config_schema::ConfigSchemaKind::Llmanspec,
            &yaml,
        )
        .expect_err("empty tags must fail schema validation");
        assert!(err.contains("tags"), "got: {err}");
    }

    #[test]
    fn bindings_reject_empty_files_via_schema() {
        let yaml: serde_yaml::Value = serde_yaml::from_str(
            "\
schema: spec-driven
bdd:
  bindings:
    - kind: scenario-attrs
      files: []
",
        )
        .expect("yaml");
        let err = crate::config_schema::validate_yaml_value(
            crate::config_schema::ConfigSchemaKind::Llmanspec,
            &yaml,
        )
        .expect_err("empty files must fail schema validation");
        assert!(err.contains("files"), "got: {err}");
    }

    #[test]
    fn default_config_has_spec_driven() {
        let config = SddConfig::default();
        assert_eq!(config.schema, "spec-driven");
        assert_eq!(config.locale, "en");
        assert!(config.extra_skills.is_none());
    }

    #[test]
    fn write_default_config_writes_en_template() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        write_default_config(llmanspec_dir, "en").expect("write default config");
        let path = config_path(llmanspec_dir);
        assert!(path.exists());
        let content = fs::read_to_string(&path).expect("read config");
        assert!(
            content.contains("yaml-language-server"),
            "should have schema header"
        );
        assert!(content.contains("schema: spec-driven"));
        assert!(content.contains("locale: en"));
        assert!(
            content.contains("# extra_skills:"),
            "en template should have extra_skills comment"
        );
        assert!(
            content.contains("llman-sdd-*"),
            "en template should document managed prefix cleanup"
        );
    }

    #[test]
    fn write_default_config_writes_zh_hans_template() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        write_default_config(llmanspec_dir, "zh-Hans").expect("write default config");
        let path = config_path(llmanspec_dir);
        assert!(path.exists());
        let content = fs::read_to_string(&path).expect("read config");
        assert!(content.contains("yaml-language-server"));
        assert!(content.contains("schema: spec-driven"));
        assert!(content.contains("locale: zh-Hans"));
        assert!(
            content.contains("# extra_skills:"),
            "zh-Hans template should have extra_skills comment"
        );
        assert!(
            content.contains("llman-sdd-*"),
            "zh-Hans template should document managed prefix cleanup"
        );
    }

    #[test]
    fn write_default_config_round_trip_parses_correctly() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        write_default_config(llmanspec_dir, "en").expect("write default config");
        let config = load_config(llmanspec_dir)
            .expect("load should succeed")
            .expect("config should exist");
        assert_eq!(config.schema, "spec-driven");
        assert_eq!(config.locale, "en");
        // Comments are stripped by serde
        assert!(config.extra_skills.is_none());
    }

    #[test]
    fn write_config_still_outputs_compact_yaml() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        let config = SddConfig::default();
        write_config(llmanspec_dir, &config).expect("write config");
        let path = config_path(llmanspec_dir);
        let content = fs::read_to_string(&path).expect("read config");
        // write_config uses serde — no comments, just compact YAML
        assert!(
            !content.contains("# Project context"),
            "serde output should not have comments"
        );
        assert!(content.contains("schema: spec-driven"));
    }

    #[test]
    fn load_config_rejects_unknown_extra_skills() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        let path = config_path(llmanspec_dir);
        let content = "schema: spec-driven\nlocale: en\nextra_skills:\n  - nonexistent-skill\n";
        fs::write(&path, content).expect("write config");
        let result = load_config(llmanspec_dir);
        assert!(result.is_err(), "expected error for unknown extra_skills");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("Unknown extra_skills entry"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn load_config_accepts_valid_extra_skills() {
        let dir = tempdir().expect("tempdir");
        let llmanspec_dir = dir.path();
        let path = config_path(llmanspec_dir);
        let content = "schema: spec-driven\nlocale: en\nextra_skills:\n  - llman-sdd-continue\n";
        fs::write(&path, content).expect("write config");
        let config = load_config(llmanspec_dir).expect("load").expect("config");
        assert_eq!(
            config.extra_skills,
            Some(vec!["llman-sdd-continue".to_string(),])
        );
    }
}