req-cli 0.3.1

Managed requirements CLI for LLM agents and humans
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
// Implements REQ-0049 (record test runs with git HEAD SHA + outcome + notes),
// REQ-0055 (req test run — drive cargo test, parse output, attach one
// pass/fail record per REQ), and REQ-0056 (verify-by-evidence policy:
// Verified status is backed by an automated test OR a written justification,
// recorded as a composition or inspection EvidenceKind on the same TestRecord
// shape with --promote auto-flipping status when a fresh passing record of
// any kind exists against current HEAD).
use anyhow::{anyhow, Context, Result};
use chrono::Utc;
use once_cell::sync::Lazy;
use regex::Regex;
use serde_json::json;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::process::Command;

use crate::cli::{TestCmd, TestRecordArgs, TestResultArg, TestRunArgs, VerifyArgs, VerifyKindArg};
use crate::model::{EvidenceKind, Status, TestOutcome, TestRecord};
use crate::storage::{self, load_for_mutation};

pub fn run(cmd: TestCmd, file: &Option<PathBuf>) -> Result<()> {
    match cmd {
        TestCmd::Record(args) => record(args, file),
        TestCmd::Run(args) => run_suite(args, file),
    }
}

pub fn verify(mut args: VerifyArgs, file: &Option<PathBuf>) -> Result<()> {
    let (path, mut project, _lock) = load_for_mutation(file)?;
    args.id = super::resolve_id(&project, &args.id)?;
    let kind = match args.by {
        VerifyKindArg::Composition => EvidenceKind::Composition,
        VerifyKindArg::Inspection => EvidenceKind::Inspection,
    };
    let commit = current_head_sha_opt().unwrap_or_else(|| "(no git)".into());
    let cites_prefix = if args.cites.is_empty() {
        String::new()
    } else {
        format!("cites: {}", args.cites.join(", "))
    };
    let record = TestRecord {
        at: Utc::now(),
        actor: super::current_actor(),
        commit: commit.clone(),
        outcome: TestOutcome::Pass,
        notes: format!("{}{}", cites_prefix, args.notes),
        kind,
    };
    let r = project.requirements.get_mut(&args.id).unwrap();
    r.tests.push(record.clone());
    r.history.push(super::history(
        format!(
            "{} evidence recorded against commit {}",
            kind.as_str(),
            short(&commit)
        ),
        Some(args.notes.clone()),
    ));
    r.updated = Utc::now();
    let mut promoted = false;
    if args.promote {
        // Promotion bypassed the lifecycle entirely before: Draft was
        // promoted straight to Verified. Now Implemented is the only
        // status that auto-promotes; everything else requires --force
        // so the user has to acknowledge the skip.
        let eligible = matches!(r.status, Status::Implemented);
        if eligible || args.force {
            if !matches!(r.status, Status::Verified | Status::Obsolete) {
                r.status = Status::Verified;
                r.history.push(super::history(
                    format!(
                        "status promoted to verified ({} evidence on HEAD)",
                        kind.as_str()
                    ),
                    None,
                ));
                promoted = true;
            }
        } else if !matches!(r.status, Status::Verified | Status::Obsolete) {
            return Err(anyhow!(
                "{} is at status '{}'; --promote only auto-promotes from \
                 'implemented'. Move it to implemented first (`req update \
                 {} --status implemented --reason ...`), or pass --force \
                 to skip the precondition.",
                args.id,
                r.status.as_str(),
                args.id
            ));
        }
    }
    project.updated = Utc::now();
    storage::save(&path, &project)?;

    if args.json {
        println!(
            "{}",
            serde_json::to_string_pretty(&serde_json::json!({
                "id": args.id, "kind": kind.as_str(),
                "commit": commit, "promoted": promoted,
                "requirement": project.requirements[&args.id],
            }))?
        );
    } else {
        println!(
            "Recorded {} evidence on {} against commit {}.{}",
            kind.as_str(),
            args.id,
            short(&commit),
            if promoted {
                " Promoted to Verified."
            } else {
                ""
            }
        );
    }
    Ok(())
}

fn record(mut args: TestRecordArgs, file: &Option<PathBuf>) -> Result<()> {
    let (path, mut project, _lock) = load_for_mutation(file)?;
    args.id = super::resolve_id(&project, &args.id)?;
    let commit = current_head_sha()
        .context("not in a git working tree — cannot record a test run without a commit SHA")?;
    let outcome = match args.result {
        TestResultArg::Pass => TestOutcome::Pass,
        TestResultArg::Fail => TestOutcome::Fail,
    };
    let record = TestRecord {
        at: Utc::now(),
        actor: super::current_actor(),
        commit,
        outcome,
        notes: args.notes,
        kind: EvidenceKind::Automated,
    };
    let r = project.requirements.get_mut(&args.id).unwrap();
    r.tests.push(record.clone());
    r.history.push(super::history(
        format!(
            "test {} recorded against commit {}",
            outcome.as_str(),
            short(&record.commit)
        ),
        Some(record.notes.clone()).filter(|s| !s.is_empty()),
    ));
    r.updated = Utc::now();
    project.updated = Utc::now();
    storage::save(&path, &project)?;

    if args.json {
        println!(
            "{}",
            serde_json::to_string_pretty(&project.requirements[&args.id])?
        );
    } else {
        println!(
            "Recorded {} test for {} against {}.",
            outcome.as_str(),
            args.id,
            short(&record.commit)
        );
    }
    Ok(())
}

fn current_head_sha() -> Result<String> {
    let out = Command::new("git").args(["rev-parse", "HEAD"]).output()?;
    if !out.status.success() {
        return Err(anyhow!(
            "git rev-parse HEAD failed: {}",
            String::from_utf8_lossy(&out.stderr)
        ));
    }
    Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
}

pub fn current_head_sha_opt() -> Option<String> {
    current_head_sha().ok()
}

pub fn short(sha: &str) -> String {
    sha.chars().take(9).collect()
}

// ---------- staleness ----------

/// Three-state staleness signal for a TestRecord, computed by intersecting
/// "files referencing this requirement in code" with "files changed in git
/// between the record commit and HEAD". `Fresh` means the record commit is
/// HEAD. `Drifted` means HEAD moved but no linked file changed. `Stale`
/// means at least one linked file changed since the record commit.
pub enum Staleness {
    Fresh,
    /// HEAD moved but none of the requirement's linked files changed.
    /// The number is how many linked files exist.
    Drifted {
        linked: usize,
    },
    /// At least one linked file changed since the record commit.
    /// `linked` is kept on the variant so `req stale --json` callers can
    /// see how many files the requirement is linked to alongside the
    /// changed-files list. Read by the JSON renderer in commands/stale.rs.
    #[allow(dead_code)]
    Stale {
        changed: Vec<String>,
        linked: usize,
    },
    /// No git context — neither fresh nor stale can be computed.
    Unknown,
}

impl Staleness {
    pub fn tag(&self) -> String {
        match self {
            Staleness::Fresh => "[matches HEAD]".to_string(),
            Staleness::Drifted { linked: 0 } => "[drifted — no linked files]".to_string(),
            Staleness::Drifted { linked } => {
                format!("[drifted — no changes to {} linked file(s)]", linked)
            }
            Staleness::Stale { changed, .. } => {
                format!("[STALE — changed: {}]", changed.join(", "))
            }
            Staleness::Unknown => "[HEAD unknown]".to_string(),
        }
    }
}

/// Files under `root` that contain `REQ-NNNN` for the given id.
pub fn files_referencing(req_id: &str, root: &std::path::Path) -> Vec<std::path::PathBuf> {
    use once_cell::sync::Lazy;
    use regex::Regex;
    static REQ_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"REQ-\d{4}").unwrap());
    const DEFAULTS: &[&str] = &[
        "rs", "py", "js", "ts", "tsx", "go", "java", "md", "toml", "c", "cpp", "h",
    ];
    const SKIP: &[&str] = &[
        ".git",
        "target",
        "node_modules",
        "dist",
        "build",
        ".venv",
        ".idea",
        ".vscode",
    ];

    let mut hits = Vec::new();
    fn walk(
        root: &std::path::Path,
        exts: &[&str],
        skip: &[&str],
        req_id: &str,
        req_re: &regex::Regex,
        hits: &mut Vec<std::path::PathBuf>,
    ) {
        let entries = match std::fs::read_dir(root) {
            Ok(e) => e,
            Err(_) => return,
        };
        for entry in entries.flatten() {
            let path = entry.path();
            let name = entry.file_name();
            let name_s = name.to_string_lossy();
            if path.is_dir() {
                if skip.iter().any(|s| *s == name_s.as_ref()) {
                    continue;
                }
                walk(&path, exts, skip, req_id, req_re, hits);
            } else if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
                if !exts.contains(&ext) {
                    continue;
                }
                if let Ok(text) = std::fs::read_to_string(&path) {
                    if req_re.find_iter(&text).any(|m| m.as_str() == req_id) {
                        hits.push(path);
                    }
                }
            }
        }
    }
    walk(root, DEFAULTS, SKIP, req_id, &REQ_RE, &mut hits);
    hits
}

/// Files changed in git between `record_commit` and HEAD.
fn git_changed_since(record_commit: &str) -> Option<std::collections::BTreeSet<String>> {
    let out = std::process::Command::new("git")
        .args(["diff", "--name-only", &format!("{}..HEAD", record_commit)])
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    Some(
        String::from_utf8_lossy(&out.stdout)
            .lines()
            .map(|l| l.trim().to_string())
            .filter(|l| !l.is_empty())
            .collect(),
    )
}

pub fn staleness(record_commit: &str, req_id: &str, source_root: &std::path::Path) -> Staleness {
    let head = match current_head_sha_opt() {
        Some(h) => h,
        None => return Staleness::Unknown,
    };
    if head == record_commit {
        return Staleness::Fresh;
    }
    let linked = files_referencing(req_id, source_root);
    let linked_strs: std::collections::BTreeSet<String> = linked
        .iter()
        .map(|p| p.to_string_lossy().replace('\\', "/"))
        .collect();
    let changed = match git_changed_since(record_commit) {
        Some(c) => c,
        None => return Staleness::Unknown,
    };
    let mut overlap: Vec<String> = linked_strs
        .iter()
        .filter(|f| {
            changed
                .iter()
                .any(|c| c.replace('\\', "/").ends_with(f.as_str()) || f.ends_with(c))
        })
        .cloned()
        .collect();
    overlap.sort();
    overlap.dedup();
    if overlap.is_empty() {
        Staleness::Drifted {
            linked: linked.len(),
        }
    } else {
        Staleness::Stale {
            changed: overlap,
            linked: linked.len(),
        }
    }
}

// ---------- req test run ----------

static TEST_LINE: Lazy<Regex> = Lazy::new(|| {
    // matches `test req_0006_some_name ... ok` or `... FAILED` or `... ignored`
    Regex::new(r"(?m)^test\s+(?:[\w:]+::)?(req_(\d{4})\w*)\s+\.\.\.\s+(ok|FAILED|ignored)").unwrap()
});

#[derive(Debug, Default)]
struct ReqResult {
    passed: Vec<String>,
    failed: Vec<String>,
    ignored: Vec<String>,
}

fn run_suite(args: TestRunArgs, file: &Option<PathBuf>) -> Result<()> {
    let (path, mut project, _lock) = load_for_mutation(file)?;

    // Either parse a pre-captured log file (--from-file) or run the test
    // command and parse its combined stdout+stderr. The file path bypasses
    // shell quoting entirely, which matters for tests on Windows where
    // splitting --cmd on whitespace drops cmd.exe's /C argument boundaries.
    let (combined, exec_success) = if let Some(p) = &args.from_file {
        let body = std::fs::read_to_string(p)
            .with_context(|| format!("read --from-file {}", p.display()))?;
        (body, true)
    } else {
        let parts: Vec<&str> = args.cmd.split_whitespace().collect();
        if parts.is_empty() {
            return Err(anyhow!("empty test command"));
        }
        let out = Command::new(parts[0])
            .args(&parts[1..])
            .output()
            .with_context(|| format!("invoke {}", args.cmd))?;
        let stdout = String::from_utf8_lossy(&out.stdout).into_owned();
        let stderr = String::from_utf8_lossy(&out.stderr).into_owned();
        (format!("{}\n{}", stdout, stderr), out.status.success())
    };

    let mut by_req: BTreeMap<String, ReqResult> = BTreeMap::new();
    for cap in TEST_LINE.captures_iter(&combined) {
        let test_name = cap[1].to_string();
        let id = format!("REQ-{}", &cap[2]);
        let verdict = &cap[3];
        let bucket = by_req.entry(id).or_default();
        match verdict {
            "ok" => bucket.passed.push(test_name),
            "FAILED" => bucket.failed.push(test_name),
            "ignored" => bucket.ignored.push(test_name),
            _ => {}
        }
    }

    if by_req.is_empty() {
        let msg = "no test names matched the `req_NNNN_*` convention";
        if args.json {
            println!(
                "{}",
                serde_json::to_string_pretty(
                    &json!({ "ok": exec_success, "matched": 0, "message": msg })
                )?
            );
        } else {
            eprintln!("{}", msg);
        }
        return Ok(());
    }

    let commit = current_head_sha_opt();
    let actor = super::current_actor();

    let mut records_to_apply: Vec<(String, TestRecord)> = Vec::new();
    let mut summary: Vec<serde_json::Value> = Vec::new();
    for (req_id, res) in &by_req {
        let exists = project.requirements.contains_key(req_id);
        let outcome = if !res.failed.is_empty() {
            TestOutcome::Fail
        } else {
            TestOutcome::Pass
        };
        let total = res.passed.len() + res.failed.len() + res.ignored.len();
        let notes = format!(
            "cargo test: {} pass / {} fail / {} ignored — {}",
            res.passed.len(),
            res.failed.len(),
            res.ignored.len(),
            if res.failed.is_empty() {
                res.passed
                    .iter()
                    .chain(res.ignored.iter())
                    .cloned()
                    .collect::<Vec<_>>()
                    .join(", ")
            } else {
                res.failed.join(", ")
            }
        );
        summary.push(json!({
            "req_id": req_id,
            "exists_in_project": exists,
            "outcome": outcome.as_str(),
            "tests": total,
            "passed": res.passed.len(),
            "failed": res.failed.len(),
            "ignored": res.ignored.len(),
            "test_names": {
                "passed": res.passed,
                "failed": res.failed,
                "ignored": res.ignored,
            },
        }));
        if !exists || args.dry_run {
            continue;
        }
        let record = TestRecord {
            at: Utc::now(),
            actor: actor.clone(),
            commit: commit.clone().unwrap_or_else(|| "(no git)".into()),
            outcome,
            notes,
            kind: EvidenceKind::Automated,
        };
        records_to_apply.push((req_id.clone(), record));
    }

    let mut promoted: Vec<String> = Vec::new();
    if !args.dry_run {
        for (req_id, record) in &records_to_apply {
            let r = project.requirements.get_mut(req_id).unwrap();
            r.tests.push(record.clone());
            r.history.push(super::history(
                format!(
                    "test {} recorded against commit {} via req test run",
                    record.outcome.as_str(),
                    short(&record.commit)
                ),
                None,
            ));
            r.updated = Utc::now();
        }
        // Auto-promote pass after writing records, so the latest record is
        // already on r.tests when we evaluate "is there fresh evidence?".
        if args.promote {
            let head = current_head_sha_opt();
            for (req_id, _) in &records_to_apply {
                let r = project.requirements.get_mut(req_id).unwrap();
                if matches!(r.status, Status::Verified | Status::Obsolete) {
                    continue;
                }
                let fresh = match &head {
                    Some(h) => r
                        .tests
                        .iter()
                        .any(|t| t.outcome == TestOutcome::Pass && &t.commit == h),
                    None => false,
                };
                if fresh {
                    r.status = Status::Verified;
                    r.history.push(super::history(
                        "status promoted to verified (req test run --promote, fresh passing record on HEAD)",
                        None,
                    ));
                    promoted.push(req_id.clone());
                }
            }
        }
        if !records_to_apply.is_empty() || !promoted.is_empty() {
            project.updated = Utc::now();
            storage::save(&path, &project)?;
        }
    }

    if args.json {
        println!(
            "{}",
            serde_json::to_string_pretty(&json!({
                "ok": exec_success,
                "dry_run": args.dry_run,
                "source": args.from_file.as_ref().map(|p| p.display().to_string())
                    .unwrap_or_else(|| args.cmd.clone()),
                "matched_requirements": summary.len(),
                "recorded": if args.dry_run { 0 } else { records_to_apply.len() },
                "results": summary,
            }))?
        );
    } else {
        let mode = if args.dry_run { " (dry-run)" } else { "" };
        println!("req test run{} — `{}`", mode, args.cmd);
        for entry in &summary {
            let id = entry["req_id"].as_str().unwrap();
            let exists = entry["exists_in_project"].as_bool().unwrap();
            let outcome = entry["outcome"].as_str().unwrap();
            let p = entry["passed"].as_u64().unwrap();
            let f = entry["failed"].as_u64().unwrap();
            let i = entry["ignored"].as_u64().unwrap();
            let tag = if !exists { " (unknown REQ)" } else { "" };
            println!(
                "  {} {:<4} {} pass / {} fail / {} ignored{}",
                id,
                outcome.to_uppercase(),
                p,
                f,
                i,
                tag
            );
        }
        if !args.dry_run {
            println!();
            println!("Recorded {} test record(s).", records_to_apply.len());
            if args.promote {
                println!("Promoted {} requirement(s) to Verified.", promoted.len());
            }
        }
    }

    if !exec_success {
        std::process::exit(1);
    }
    Ok(())
}