1const IGNORED_LOCKFILES: &[&str] = &[
8 "Cargo.lock",
9 "uv.lock",
10 "package-lock.json",
11 "pnpm-lock.yaml",
12 "yarn.lock",
13 "poetry.lock",
14];
15
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Checks {
18 pub flags: Vec<String>,
19}
20
21pub fn unmentioned_files(task: &str, changed: &[String]) -> Vec<String> {
26 changed
27 .iter()
28 .filter(|path| {
29 if path.is_empty() {
30 return false;
31 }
32 let file_name = std::path::Path::new(path)
33 .file_name()
34 .and_then(|s| s.to_str())
35 .unwrap_or(path.as_str());
36
37 if IGNORED_LOCKFILES.contains(&file_name) || IGNORED_LOCKFILES.contains(&path.as_str())
38 {
39 return false;
40 }
41
42 let mentioned =
43 task.contains(path.as_str()) || (!file_name.is_empty() && task.contains(file_name));
44 !mentioned
45 })
46 .cloned()
47 .collect()
48}
49
50pub fn unexpected_subjects(task: &str, subjects: &[String]) -> Vec<String> {
55 if !specifies_commit(task) {
56 return Vec::new();
57 }
58 subjects
59 .iter()
60 .filter(|s| !task.contains(s.as_str()))
61 .cloned()
62 .collect()
63}
64
65fn specifies_commit(task: &str) -> bool {
66 for line in task.lines() {
67 let trimmed = line.trim();
68 if trimmed.starts_with('#') && contains_word_commit(trimmed) {
69 return true;
70 }
71 let stripped = trimmed.trim_start_matches(|c: char| {
72 c.is_whitespace()
73 || c == '*'
74 || c == '-'
75 || c == '_'
76 || c == '>'
77 || c == '`'
78 || c == '#'
79 });
80 if stripped.to_lowercase().starts_with("commit")
81 || stripped.to_lowercase().contains("commit message")
82 {
83 return true;
84 }
85 }
86 false
87}
88
89fn contains_word_commit(s: &str) -> bool {
90 s.to_lowercase()
91 .split(|c: char| !c.is_alphanumeric())
92 .any(|w| w == "commit" || w == "commits")
93}
94
95fn sanitize_flag(flag: String) -> String {
96 let single_line = flag.lines().next().unwrap_or("").to_string();
97 if single_line.chars().count() > 200 {
98 single_line.chars().take(200).collect()
99 } else {
100 single_line
101 }
102}
103
104pub fn task_lists_files(task: &str) -> bool {
106 task.lines().any(|line| {
107 let stripped = line
108 .trim_start_matches(|c: char| c.is_whitespace() || matches!(c, '#' | '*' | '-' | '_'));
109 let Some(prefix) = stripped.as_bytes().get(..5) else {
110 return false;
111 };
112 if !prefix.eq_ignore_ascii_case(b"files") {
113 return false;
114 }
115 matches!(
116 stripped[5..].chars().next(),
117 None | Some(':' | '*' | '_' | '.' | ' ')
118 )
119 })
120}
121
122fn is_unnamed_files_flag(flag: &str) -> bool {
123 flag.starts_with("files not named in the task")
124}
125
126pub fn should_run_model_review(
128 flags: &[String],
129 task_text: Option<&str>,
130 risky_matches: &[String],
131 fallback_candidates: usize,
132) -> bool {
133 if fallback_candidates > 0 || !risky_matches.is_empty() {
134 return true;
135 }
136 let lists_files = task_text.is_some_and(task_lists_files);
137 flags
138 .iter()
139 .any(|f| lists_files || !is_unnamed_files_flag(f))
140}
141
142pub fn build(
144 task: Option<&str>,
145 changed: &[String],
146 subjects: &[String],
147 host_cmds: usize,
148 outside_reads: usize,
149 outcome: Option<&str>,
150) -> Checks {
151 let mut flags = Vec::new();
152
153 if task.is_none() {
154 flags.push("task file not found: scope not checked".to_string());
155 }
156
157 if let Some(outc) = outcome {
158 if outc != "verified" {
159 flags.push(format!("outcome {outc}: review in depth"));
160 }
161 }
162
163 if changed.is_empty() {
164 flags.push("no files changed".to_string());
165 }
166
167 if let Some(t) = task {
168 if task_lists_files(t) {
169 let unmentioned = unmentioned_files(t, changed);
170 if !unmentioned.is_empty() {
171 let msg = if unmentioned.len() <= 10 {
172 format!("files not named in the task: {}", unmentioned.join(", "))
173 } else {
174 let first_10 = &unmentioned[..10];
175 let more = unmentioned.len() - 10;
176 format!(
177 "files not named in the task: {}, … (+{more} more)",
178 first_10.join(", ")
179 )
180 };
181 flags.push(msg);
182 }
183 }
184
185 let unexpected = unexpected_subjects(t, subjects);
186 for subj in unexpected {
187 if subj == crate::fix_round::COMMIT_MESSAGE
189 || subj == crate::fix_round::CONCERNS_COMMIT_MESSAGE
190 {
191 continue;
192 }
193 flags.push(format!("commit message not in the task: {subj}"));
194 }
195 }
196
197 if host_cmds > 0 {
198 flags.push(format!("{host_cmds} host command(s): see == host commands"));
199 }
200
201 if outside_reads > 0 {
202 flags.push(format!(
203 "{outside_reads} read(s) outside the repository: see == reads outside the repository"
204 ));
205 }
206
207 let flags = flags.into_iter().map(sanitize_flag).collect();
208 Checks { flags }
209}
210
211#[cfg(test)]
212mod tests {
213 use super::*;
214
215 #[test]
216 fn test_unmentioned_files() {
217 let task = "Mentions `src/a.rs` by path and `b.rs` by name only";
218 let changed = vec![
219 "src/a.rs".to_string(),
220 "src/x/b.rs".to_string(),
221 "src/c.rs".to_string(),
222 "Cargo.lock".to_string(),
223 ];
224 assert_eq!(unmentioned_files(task, &changed), vec!["src/c.rs"]);
225 }
226
227 #[test]
228 fn test_unexpected_subjects() {
229 let task = "Commit message: `feat: add x`";
230 let subjects = vec!["feat: add x".to_string(), "fix: y".to_string()];
231 assert_eq!(unexpected_subjects(task, &subjects), vec!["fix: y"]);
232 }
233
234 #[test]
235 fn test_unexpected_subjects_no_commit() {
236 let task = "Just implement the feature and run the tests.";
237 let subjects = vec!["feat: add x".to_string(), "fix: y".to_string()];
238 assert!(unexpected_subjects(task, &subjects).is_empty());
239 }
240
241 #[test]
242 fn test_build_does_not_flag_the_fix_round_commit() {
243 let task = "Commit message: `feat: add x`";
244 let subjects = vec![
245 "feat: add x".to_string(),
246 crate::fix_round::COMMIT_MESSAGE.to_string(),
247 crate::fix_round::CONCERNS_COMMIT_MESSAGE.to_string(),
248 "fix: y".to_string(),
249 ];
250 let checks = build(Some(task), &[], &subjects, 0, 0, Some("verified"));
251 let about_commits: Vec<&String> = checks
252 .flags
253 .iter()
254 .filter(|f| f.starts_with("commit message not in the task"))
255 .collect();
256 assert_eq!(
257 about_commits,
258 vec!["commit message not in the task: fix: y"]
259 );
260 }
261
262 #[test]
263 fn test_build_none_task() {
264 let checks = build(None, &[], &[], 0, 0, None);
265 assert_eq!(
266 checks.flags.first().map(|s| s.as_str()),
267 Some("task file not found: scope not checked")
268 );
269 }
270
271 #[test]
272 fn test_build_outcome_gate_failed() {
273 let checks = build(
274 Some("mentions a.rs"),
275 &["a.rs".to_string()],
276 &[],
277 0,
278 0,
279 Some("gate_failed"),
280 );
281 assert!(checks
282 .flags
283 .contains(&"outcome gate_failed: review in depth".to_string()));
284 }
285
286 #[test]
287 fn test_build_12_unmentioned_files() {
288 let changed: Vec<String> = (0..12).map(|i| format!("f{i}.rs")).collect();
289 let checks = build(Some("Files: other.rs"), &changed, &[], 0, 0, None);
290 assert_eq!(checks.flags.len(), 1);
291 let flag = &checks.flags[0];
292 for i in 0..10 {
293 assert!(flag.contains(&format!("f{i}.rs")));
294 }
295 assert!(!flag.contains("f10.rs"));
296 assert!(!flag.contains("f11.rs"));
297 assert!(flag.contains("(+2 more)"));
298 }
299
300 #[test]
301 fn test_build_clean() {
302 let task = "Files: `src/a.rs`\nCommit message: `feat: x`";
303 let changed = vec!["src/a.rs".to_string()];
304 let subjects = vec!["feat: x".to_string()];
305 let checks = build(Some(task), &changed, &subjects, 0, 0, Some("verified"));
306 assert!(checks.flags.is_empty());
307 }
308
309 #[test]
310 fn test_build_host_cmds() {
311 let checks = build(Some(""), &[], &[], 2, 0, None);
312 assert!(checks
313 .flags
314 .contains(&"2 host command(s): see == host commands".to_string()));
315 }
316
317 #[test]
318 fn test_ignored_lockfiles() {
319 let task = "no files mentioned";
320 let changed = vec![
321 "Cargo.lock".to_string(),
322 "uv.lock".to_string(),
323 "package-lock.json".to_string(),
324 "pnpm-lock.yaml".to_string(),
325 "yarn.lock".to_string(),
326 "poetry.lock".to_string(),
327 ];
328 assert!(unmentioned_files(task, &changed).is_empty());
329 }
330
331 #[test]
332 fn test_build_outside_reads() {
333 let checks = build(Some(""), &[], &[], 0, 3, None);
334 assert!(checks.flags.contains(
335 &"3 read(s) outside the repository: see == reads outside the repository".to_string()
336 ));
337 }
338
339 #[test]
340 fn test_task_lists_files_examples() {
341 for case in [
342 "## Files",
343 "**Files.** a.rs",
344 "- **Files**: a.rs",
345 "Files to change:",
346 "files:",
347 " __Files__\n- src/a.rs",
348 ] {
349 assert!(task_lists_files(case), "expected true for {case:?}");
350 }
351
352 for case in [
353 "Filesystem notes",
354 "The files are…",
355 "Update src/a.rs to handle empty input.",
356 "",
357 ] {
358 assert!(!task_lists_files(case), "expected false for {case:?}");
359 }
360 }
361
362 #[test]
363 fn test_no_files_section_skips_unmentioned_files_check() {
364 let task = "Update `src/a.rs` to handle empty input.";
365 let changed = vec!["src/a.rs".to_string(), "src/b.rs".to_string()];
366 let checks = build(Some(task), &changed, &[], 0, 0, Some("verified"));
367 assert!(
368 checks
369 .flags
370 .iter()
371 .all(|f| !f.starts_with("files not named in the task")),
372 "{:?}",
373 checks.flags
374 );
375 assert!(checks.flags.is_empty(), "{:?}", checks.flags);
376 assert!(!should_run_model_review(&checks.flags, Some(task), &[], 0));
377 }
378
379 #[test]
380 fn test_files_section_flags_only_unmentioned_files() {
381 let task = "## Files\n- `src/a.rs`\n\nUpdate `src/a.rs` to handle empty input.";
382 let changed = vec!["src/a.rs".to_string(), "src/b.rs".to_string()];
383 let checks = build(Some(task), &changed, &[], 0, 0, Some("verified"));
384 assert_eq!(
385 checks.flags,
386 vec!["files not named in the task: src/b.rs".to_string()]
387 );
388 assert!(should_run_model_review(&checks.flags, Some(task), &[], 0));
389 }
390
391 #[test]
392 fn test_no_files_section_still_reviews_fallback_candidates() {
393 let task = "Update `src/a.rs` to handle empty input.";
394 let changed = vec!["src/a.rs".to_string(), "src/b.rs".to_string()];
395 let checks = build(Some(task), &changed, &[], 0, 0, Some("verified"));
396 assert!(checks.flags.is_empty());
397 assert!(should_run_model_review(&checks.flags, Some(task), &[], 1));
398 }
399}