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
// Implements REQ-0063 (content-drift staleness report): for each
// requirement's latest test record, compare against the current HEAD AND
// the set of files containing the requirement's REQ-NNNN marker. Reports
// Fresh / Drifted (no linked files changed) / STALE (linked files
// changed since the record).
use anyhow::Result;
use serde_json::json;
use std::path::PathBuf;
use crate::cli::StaleArgs;
use crate::commands::test_cmd::{self, Staleness};
use crate::storage::load_resolved;
pub fn run(args: StaleArgs, file: &Option<PathBuf>) -> Result<()> {
let (_, project) = load_resolved(file)?;
let mut rows = Vec::new();
let mut counts = (0usize, 0usize, 0usize, 0usize, 0usize); // fresh, drifted, stale, no_records, unknown
// REQ-0135: the staleness logic is id-agnostic, so run it over both
// ordinary requirements and safety requirements. This is what makes a
// SIL 3/4 SR's automated evidence go STALE when its linked code moves,
// rather than standing as a claim forever.
let mut process = |id: &str, tests: &[crate::model::TestRecord]| {
let latest = match tests.last() {
None => {
counts.3 += 1;
if !args.only_stale {
rows.push((
id.to_string(),
"no-records".to_string(),
"—".to_string(),
Vec::<String>::new(),
));
}
return;
}
Some(t) => t,
};
// REQ-0112: prefer content-hash comparison when the record
// carries one. Falls back to SHA-based detection for older
// records without a hash.
let s = match latest.content_hash.as_deref() {
Some(stored_hash) => test_cmd::staleness_by_content(
stored_hash,
latest.linked_files.as_ref(),
id,
&args.path,
),
None => test_cmd::staleness(&latest.commit, id, &args.path),
};
let label = match &s {
Staleness::Fresh => {
counts.0 += 1;
"fresh"
}
Staleness::Drifted { .. } => {
counts.1 += 1;
"drifted"
}
Staleness::Stale { .. } => {
counts.2 += 1;
"STALE"
}
Staleness::Unknown => {
counts.4 += 1;
"unknown"
}
};
if args.only_stale && !matches!(s, Staleness::Stale { .. }) {
return;
}
let changed: Vec<String> = match &s {
Staleness::Stale { changed, .. } => changed.clone(),
_ => Vec::new(),
};
rows.push((
id.to_string(),
label.to_string(),
test_cmd::short(&latest.commit),
changed,
));
};
for r in project.requirements.values() {
process(&r.id, &r.tests);
}
for sr in project.safety_requirements.values() {
process(&sr.id, &sr.tests);
}
if args.json {
println!(
"{}",
serde_json::to_string_pretty(&json!({
"summary": {
"fresh": counts.0,
"drifted": counts.1,
"stale": counts.2,
"no_records": counts.3,
"unknown": counts.4,
},
"rows": rows.iter().map(|(id, state, commit, changed)| json!({
"id": id, "state": state, "record_commit": commit, "changed_files": changed,
})).collect::<Vec<_>>(),
}))?
);
return Ok(());
}
println!("Staleness report (root: {})", args.path.display());
println!(" fresh : {}", counts.0);
println!(
" drifted : {} (HEAD moved but linked files unchanged)",
counts.1
);
println!(
" STALE : {} (linked files changed since record)",
counts.2
);
println!(" no records : {}", counts.3);
println!(" unknown : {} (no git context)", counts.4);
if rows.is_empty() {
if args.only_stale {
println!("\nNothing stale.");
}
return Ok(());
}
println!();
for (id, state, commit, changed) in &rows {
println!(" {:<10} {:<10} record={}", id, state, commit);
for c in changed {
println!(" changed: {}", c);
}
}
if counts.2 > 0 {
std::process::exit(1);
}
Ok(())
}