llman 0.0.67

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
//! One-shot, idempotent conversion: legacy Partitioned pair (`spec.toon` +
//! optional `*.feature`) → single-track `<capability>.feature` (r136).
//!
//! - `requirements[]` become `@req:<id> @human` rule scenarios with the
//!   statement preserved verbatim as the scenario description (lossless).
//! - `feature: false` note rows are dropped (they were pointer noise).
//! - Existing `.feature` acceptance scenarios (`@executable`) are carried over.
//! - `spec.toon` is deleted after a successful write; re-running is a no-op.

use crate::fs_utils::atomic_write_with_mode;
use crate::sdd::shared::constants::{LLMANSPEC_DIR_NAME, SPEC_FILE};
use crate::sdd::spec::backend::FEATURE_BACKEND;
use crate::sdd::spec::ir::MainSpecDoc;
use anyhow::{Context, Result, anyhow};
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct MigrateArgs {
    pub dry_run: bool,
    #[allow(dead_code)]
    pub force: bool,
    /// Skip the confirmation prompt and apply (for agents/scripts).
    pub yes: bool,
    /// Treat the terminal as non-interactive even when stdin is a TTY.
    pub no_interactive: bool,
}

/// One capability's migration plan.
struct Plan {
    dir: PathBuf,
    capability: String,
    toon: Option<MainSpecDoc>,
}

pub fn run(args: MigrateArgs) -> Result<()> {
    run_at(Path::new("."), args)
}

pub fn run_at(root: &Path, args: MigrateArgs) -> Result<()> {
    let specs_root = root.join(LLMANSPEC_DIR_NAME).join("specs");
    if !specs_root.is_dir() {
        println!("No specs directory found; nothing to migrate.");
        return Ok(());
    }

    // Phase 1: scan legacy dirs.
    let mut plans: Vec<Plan> = Vec::new();
    for dir in collect_capability_dirs(&specs_root)? {
        let capability = dir
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or_default()
            .to_string();
        if !dir.join(SPEC_FILE).exists() {
            continue; // already single-track → idempotent no-op
        }
        let raw = fs::read_to_string(dir.join(SPEC_FILE))
            .with_context(|| format!("read {}", dir.join(SPEC_FILE).display()))?;
        let doc: MainSpecDoc =
            parse_legacy_toon(&raw).with_context(|| format!("parse legacy `{capability}`"))?;
        plans.push(Plan {
            dir,
            capability,
            toon: Some(doc),
        });
    }

    if plans.is_empty() {
        println!("Nothing to migrate; all capabilities are already single-track.");
        return Ok(());
    }
    println!("Legacy capabilities to migrate: {}", plans.len());

    if args.dry_run {
        for p in &plans {
            let acceptance_note = if p.dir.join(format!("{}.feature", p.capability)).exists() {
                "keep existing"
            } else {
                "none"
            };
            println!(
                "  {}{}.feature (rules {}, acceptance {})",
                p.dir.display(),
                p.capability,
                p.toon.as_ref().map(|d| d.requirements.len()).unwrap_or(0),
                acceptance_note,
            );
        }
        println!("\n(dry-run: no files written)");
        return Ok(());
    }

    if !args.yes {
        let interactive = crate::sdd::shared::interactive::is_interactive(args.no_interactive);
        if !interactive {
            return Err(anyhow!(
                "non-interactive terminal: re-run with --yes to apply, or --dry-run to preview"
            ));
        }
        let confirmed = inquire::Confirm::new(&format!(
            "Migrate {} capability(s) to the single-track format?",
            plans.len()
        ))
        .with_default(false)
        .prompt()
        .map_err(|e| anyhow!("confirmation prompt failed: {e}"))?;
        if !confirmed {
            println!("Aborted.");
            return Ok(());
        }
    }

    // Phase 2: apply.
    let mut migrated = 0usize;
    let mut failures = Vec::new();
    for p in &plans {
        match migrate_capability(p) {
            Ok(summary) => {
                println!("{}: {summary}", p.capability);
                migrated += 1;
            }
            Err(e) => failures.push(format!("{}: {e:#}", p.capability)),
        }
    }

    if !failures.is_empty() {
        eprintln!("Failures ({}):", failures.len());
        for f in &failures {
            eprintln!("  - {f}");
        }
        return Err(anyhow!(
            "migration completed with {} failure(s)",
            failures.len()
        ));
    }

    // Coverage summary over the migrated tree.
    println!("\nMigration complete: {migrated} capability(s) converted to single-track features.");
    Ok(())
}

fn migrate_capability(plan: &Plan) -> Result<String> {
    let Some(toon_doc) = &plan.toon else {
        return Ok("skipped".to_string());
    };
    let feature_path = plan.dir.join(format!("{}.feature", plan.capability));

    // Rules come from legacy toon requirements (statement verbatim).
    let merged = MainSpecDoc {
        kind: "llman.sdd.spec".to_string(),
        name: toon_doc.name.trim().to_string(),
        purpose: toon_doc.purpose.clone(),
        valid_scope: toon_doc.valid_scope.clone(),
        requirements: toon_doc.requirements.clone(),
        scenarios: Vec::new(),
    };

    // Acceptance scenarios are copied VERBATIM from every legacy feature:
    // the IR loses step boundaries within a type, and rstest-bdd binds by
    // exact step text, so byte-faithful blocks are the only safe carrier.
    let legacy_features = crate::sdd::spec::validation::discover_features(&plan.dir);
    let merged_files = legacy_features.len();
    let mut seen_names: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut acceptance_blocks: Vec<String> = Vec::new();
    let mut dropped = 0usize;
    for legacy in &legacy_features {
        let raw = fs::read_to_string(legacy)?;
        for (name, tags, block) in extract_scenario_blocks(&raw) {
            let executable = tags.iter().any(|t| {
                t.trim()
                    .trim_start_matches('@')
                    .eq_ignore_ascii_case("executable")
            });
            // Bare `@req:` (empty id) marks obsolete-contract scenarios — drop.
            let bare_req = tags.iter().any(|t| {
                t.trim()
                    .trim_start_matches('@')
                    .eq_ignore_ascii_case("req:")
            });
            let has_req = tags.iter().any(|t| {
                let t = t.trim().trim_start_matches('@');
                t.starts_with("req:") && t.len() > "req:".len()
            });
            if !executable || bare_req || !has_req {
                dropped = dropped.saturating_add(1);
                continue;
            }
            if seen_names.insert(name.clone()) {
                acceptance_blocks.push(block);
            } else {
                eprintln!(
                    "Warning: duplicate scenario `{name}` across merged features of `{}`; kept first",
                    plan.capability
                );
            }
        }
    }

    // Header + rules via the canonical renderer, then verbatim acceptance.
    let mut payload = FEATURE_BACKEND.dump_main_spec(&merged)?;
    if !payload.ends_with('\n') {
        payload.push('\n');
    }
    for block in &acceptance_blocks {
        payload.push_str(block);
        payload.push('\n');
    }
    atomic_write_with_mode(&feature_path, payload.as_bytes(), None)?;

    // Remove every legacy source file EXCEPT the freshly written target.
    // Compare canonically: glob paths may differ textually (`./` prefix).
    let target_canon = feature_path
        .canonicalize()
        .unwrap_or_else(|_| feature_path.clone());
    for legacy in &legacy_features {
        if legacy
            .canonicalize()
            .map(|c| c == target_canon)
            .unwrap_or(false)
        {
            continue;
        }
        fs::remove_file(legacy).with_context(|| format!("remove merged {}", legacy.display()))?;
    }
    fs::remove_file(plan.dir.join(SPEC_FILE))
        .with_context(|| format!("remove legacy {}", plan.dir.join(SPEC_FILE).display()))?;

    // r136: the report MUST carry the three-tier initial values.
    let tiers = FEATURE_BACKEND
        .parse_content(&payload, &format!("migrated `{}`", plan.capability))
        .map(|p| crate::sdd::spec::backend::feature_backend::compute_rule_morphology(&p));
    match tiers {
        Ok(t) => Ok(format!(
            "wrote {} (merged {merged_files} feature file(s); rules {} enforced {} manual {} pending {}; acceptance {}; dropped {dropped}); removed legacy spec.toon",
            feature_path.display(),
            t.rule_count,
            t.rule_enforced_count,
            t.rule_manual_count,
            t.rule_pending_count,
            t.acceptance_count,
        )),
        Err(e) => Ok(format!(
            "wrote {} (merged {merged_files} feature file(s); rules {}, acceptance {}, dropped {dropped}); removed legacy spec.toon; tier report unavailable: {e}",
            feature_path.display(),
            merged.requirements.len(),
            acceptance_blocks.len(),
        )),
    }
}

/// Extract top-level scenario blocks (`tags + 场景/Scenario + body`) from a
/// legacy feature file, verbatim. Returns `(scenario name, tags, block)` with
/// the block starting at its first tag line.
fn extract_scenario_blocks(content: &str) -> Vec<(String, Vec<String>, String)> {
    use std::fmt::Write as _;
    let mut out: Vec<(String, Vec<String>, String)> = Vec::new();
    let mut current_tags: Vec<String> = Vec::new();
    let mut current: Option<(String, Vec<String>, String)> = None;

    for line in content.lines() {
        let trimmed = line.trim();
        let is_tag = trimmed.starts_with('@');
        let is_scenario = trimmed.starts_with("场景:") || trimmed.starts_with("Scenario:");
        if is_tag {
            if let Some(done) = current.take() {
                out.push(done);
            }
            // Gherkin allows several space-separated tags on one line
            // (`@req:r1 @executable`) — split them so downstream checks see
            // each tag individually.
            current_tags.extend(trimmed.split_whitespace().map(str::to_string));
            continue;
        }
        if is_scenario {
            if let Some(done) = current.take() {
                out.push(done);
            }
            let name = trimmed
                .split_once(':')
                .map(|(_, rest)| rest.trim().to_string())
                .unwrap_or_default();
            let mut block = String::new();
            for t in &current_tags {
                let _ = writeln!(block, "  {t}");
            }
            let _ = writeln!(block, "  {trimmed}");
            current = Some((name, current_tags.clone(), block));
            current_tags.clear();
            continue;
        }
        if let Some((_, _, block)) = current.as_mut() {
            block.push_str(line);
            block.push('\n');
        } else if !current_tags.is_empty() && !trimmed.is_empty() {
            // Tags followed by a non-scenario line: orphaned tag run — reset.
            current_tags.clear();
        }
    }
    if let Some(done) = current.take() {
        out.push(done);
    }
    out
}
fn collect_capability_dirs(specs_root: &Path) -> Result<Vec<PathBuf>> {
    let mut out = Vec::new();
    let entries =
        fs::read_dir(specs_root).with_context(|| format!("read {}", specs_root.display()))?;
    for entry in entries.flatten() {
        if entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
            out.push(entry.path());
        }
    }
    out.sort();
    Ok(out)
}

/// Minimal reader for the LEGACY strict-TOON spec shape (migration-only).
/// Handles exactly the keys the old writer emitted; anything else fails loudly.
fn parse_legacy_toon(content: &str) -> Result<MainSpecDoc> {
    fn split_row(line: &str) -> Vec<String> {
        let mut out = Vec::new();
        let mut cur = String::new();
        let mut in_quotes = false;
        let mut chars = line.chars().peekable();
        while let Some(c) = chars.next() {
            match c {
                '"' if in_quotes && chars.peek() == Some(&'"') => {
                    cur.push('"');
                    chars.next();
                }
                '"' => in_quotes = !in_quotes,
                ',' if !in_quotes => out.push(std::mem::take(&mut cur)),
                _ => cur.push(c),
            }
        }
        out.push(cur);
        out
    }

    fn unquote(v: impl AsRef<str>) -> String {
        let t = v.as_ref().trim();
        if t.starts_with('"') && t.ends_with('"') && t.len() >= 2 {
            t[1..t.len() - 1].to_string()
        } else {
            t.to_string()
        }
    }

    fn push_requirement_row(
        cells: &[String],
        requirements: &mut Vec<crate::sdd::spec::ir::RequirementEntry>,
    ) {
        if cells.len() >= 3 {
            requirements.push(crate::sdd::spec::ir::RequirementEntry {
                req_id: unquote(cells[0].clone()).trim().to_string(),
                title: unquote(cells[1].clone()).trim().to_string(),
                statement: unquote(cells[2..].join(",")),
            });
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn push_scenario_row(
        cells: &[String],
        scenarios: &mut Vec<crate::sdd::spec::ir::ScenarioEntry>,
    ) {
        if cells.len() >= 6 {
            scenarios.push(crate::sdd::spec::ir::ScenarioEntry {
                req_id: unquote(cells[0].clone()).trim().to_string(),
                id: unquote(cells[1].clone()).trim().to_string(),
                given: unquote(cells[2].clone()),
                when_: unquote(cells[3].clone()),
                then_: unquote(cells[4].clone()),
                feature: unquote(cells[5].clone()).trim() != "false",
            });
        }
    }

    let mut kind = String::new();
    let mut name = String::new();
    let mut purpose = String::new();
    let mut valid_scope: Vec<String> = Vec::new();
    let mut requirements: Vec<crate::sdd::spec::ir::RequirementEntry> = Vec::new();
    let mut scenarios: Vec<crate::sdd::spec::ir::ScenarioEntry> = Vec::new();
    let mut section: Option<&'static str> = None;

    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        let indented = line.starts_with(char::is_whitespace);
        if !indented && let Some((key, rest)) = trimmed.split_once(':') {
            match key.trim() {
                "kind" => kind = unquote(rest),
                "name" => name = unquote(rest),
                "purpose" => purpose = unquote(rest),
                k if k.starts_with("valid_scope") => {
                    valid_scope = rest
                        .split(',')
                        .map(|v| unquote(v).trim().to_string())
                        .filter(|v| !v.is_empty())
                        .collect();
                    section = None;
                    continue;
                }
                k if k.starts_with("requirements") => {
                    section = Some("requirements");
                    continue;
                }
                k if k.starts_with("scenarios") => {
                    section = Some("scenarios");
                    continue;
                }
                _ => {}
            }
        }
        match section {
            Some("requirements") => push_requirement_row(&split_row(trimmed), &mut requirements),
            Some("scenarios") => push_scenario_row(&split_row(trimmed), &mut scenarios),
            _ => {}
        }
    }

    if kind.trim() != "llman.sdd.spec" {
        anyhow::bail!("legacy spec kind must be `llman.sdd.spec`, got `{kind}`");
    }
    Ok(MainSpecDoc {
        kind,
        name,
        purpose,
        valid_scope,
        requirements,
        scenarios,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    const LEGACY_TOON: &str = concat!(
        "kind: llman.sdd.spec\n",
        "name: \"demo\"\n",
        "purpose: \"demo purpose\"\n",
        "valid_scope[1]: \"src/\"\n",
        "requirements[2]{req_id,title,statement}:\n",
        "  r1,First,\"System MUST do X.\"\n",
        "  r2,Second,\"System MUST do Y.\"\n",
        "scenarios[1]{req_id,id,given,when,then,feature}:\n",
        "  r1,note,\"\",\"a trigger\",\"an outcome\",false\n",
    );

    #[test]
    fn toon2features_is_lossless_idempotent_and_cleans_toon() {
        let tmp = tempfile::TempDir::new().unwrap();
        let root = tmp.path();
        let dir = root.join(LLMANSPEC_DIR_NAME).join("specs").join("demo");
        fs::create_dir_all(&dir).unwrap();
        fs::write(dir.join(SPEC_FILE), LEGACY_TOON).unwrap();

        let args = MigrateArgs {
            dry_run: false,
            force: false,
            yes: true,
            no_interactive: true,
        };
        run_at(root, args.clone()).unwrap();

        assert!(!dir.join(SPEC_FILE).exists(), "legacy toon must be removed");
        let feature = fs::read_to_string(dir.join("demo.feature")).unwrap();
        assert!(feature.contains("# capability: demo"));
        assert!(feature.contains("@req:r1 @human"));
        assert!(feature.contains("System MUST do X."));
        // Note row (feature:false) is dropped.
        assert!(!feature.contains("note"));

        // Idempotent: second run is a no-op.
        run_at(root, args).unwrap();

        // Migrated output parses and validates under the new gates.
        let doc = FEATURE_BACKEND
            .parse_main_spec(&feature, "migrated")
            .unwrap();
        assert_eq!(doc.requirements.len(), 2);
        assert!(
            doc.requirements
                .iter()
                .all(|r| r.statement.contains("MUST"))
        );
    }
    #[test]
    fn extract_blocks_drops_orphans_and_bare_req_keeps_executable() {
        let raw = concat!(
            "# language: zh-CN\n",
            "功能: t\n",
            "\n",
            "  @req:r1 @executable\n",
            "  场景: linked\n",
            "    假如 a\n",
            "\n",
            "  @executable\n",
            "  场景: orphan\n",
            "    假如 b\n",
            "\n",
            "  @req: @executable\n",
            "  场景: obsolete-bare-req\n",
            "    假如 c\n",
            "\n",
            "  @req:r9 @human\n",
            "  场景: locked-rule\n",
            "    System MUST z.\n",
        );
        let blocks = extract_scenario_blocks(raw);
        // Extraction is faithful; filtering happens in migrate_capability.
        let names: Vec<&str> = blocks.iter().map(|(n, _, _)| n.as_str()).collect();
        assert_eq!(
            names,
            vec!["linked", "orphan", "obsolete-bare-req", "locked-rule"],
            "{names:?}"
        );
        let executable: Vec<bool> = blocks
            .iter()
            .map(|(_, tags, _)| {
                tags.iter().any(|t| {
                    t.trim()
                        .trim_start_matches('@')
                        .eq_ignore_ascii_case("executable")
                })
            })
            .collect();
        assert_eq!(executable, vec![true, true, true, false]);
    }

    #[test]
    fn extract_blocks_resets_tags_on_non_scenario_line() {
        let raw = concat!(
            "# capability: t\n",
            "功能: t\n",
            "  @orphan-run\n",
            "  free description text\n",
            "  @executable\n",
            "  场景: clean\n",
            "    假如 x\n",
        );
        let blocks = extract_scenario_blocks(raw);
        assert_eq!(blocks.len(), 1);
        assert_eq!(blocks[0].1.len(), 1, "only the executable tag remains");
    }

    #[test]
    fn extract_blocks_exposes_all_for_caller_dedupe() {
        let raw = concat!(
            "功能: t\n",
            "  @req:r1 @executable\n",
            "  场景: same-name\n",
            "    假如 a\n",
            "\n",
            "  @req:r2 @executable\n",
            "  场景: same-name\n",
            "    假如 b\n",
        );
        let blocks = extract_scenario_blocks(raw);
        assert_eq!(blocks.len(), 2, "extract returns all; caller dedupes");
        assert_eq!(blocks[0].0, "same-name");
        assert_eq!(blocks[1].0, "same-name");
    }
}