tkt 0.3.0

Track tasks as markdown files in your git repo
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
//! Pure audit rules — each function takes a corpus (and optionally an injectable
//! dependency) and returns `Vec<Finding>`. No I/O, no git, no filesystem.

use std::path::Path;

use crate::core::{self, Status, Ticket};
use crate::findings::Finding;

/// Check resolution quality on done tickets:
/// - Missing `## Resolution` section entirely
/// - Resolution is empty or still "TBD"
pub fn check_resolution_quality(corpus: &[Ticket]) -> Vec<Finding> {
    let mut findings = Vec::new();

    for t in corpus.iter().filter(|t| t.status == Status::Done) {
        let fname = filename(t);

        if !t.body.contains("## Resolution") {
            findings.push(Finding {
                file: fname,
                rule: "missing-resolution".into(),
                message: "done ticket has no Resolution section".into(),
                severity: "warning".into(),
            });
            continue;
        }

        let has_content = t
            .body
            .split_once("## Resolution")
            .map(|(_, after)| {
                let text = after.lines().skip(1).collect::<Vec<_>>().join("\n");
                let trimmed = text.trim();
                !trimmed.is_empty() && trimmed != "TBD"
            })
            .unwrap_or(false);

        if !has_content {
            findings.push(Finding {
                file: fname,
                rule: "tbd-resolution".into(),
                message: "resolution is empty or still TBD".into(),
                severity: "warning".into(),
            });
        }
    }

    findings
}

/// Check AC completeness on done tickets:
/// - All AC boxes unchecked (none checked) on a done ticket
pub fn check_ac_completeness(corpus: &[Ticket]) -> Vec<Finding> {
    let mut findings = Vec::new();

    for t in corpus.iter().filter(|t| t.status == Status::Done) {
        let stats = t.file.ac_stats();
        if stats.unchecked > 0 && stats.checked == 0 {
            findings.push(Finding {
                file: filename(t),
                rule: "all-acs-unchecked-on-done".into(),
                message: format!("{} unchecked box(es), none checked", stats.unchecked),
                severity: "warning".into(),
            });
        }
    }

    findings
}

/// Check for stale WIP tickets — in_progress for more than 7 days.
///
/// `last_commit_ts` is an injectable function that returns the unix timestamp
/// of the last commit touching a given path, or `None` if unavailable.
pub fn check_stale_wip(
    corpus: &[Ticket],
    now: u64,
    last_commit_ts: impl Fn(&Path) -> Option<u64>,
) -> Vec<Finding> {
    let mut findings = Vec::new();
    let threshold = 7 * 24 * 60 * 60; // 7 days

    for t in corpus.iter().filter(|t| t.status == Status::InProgress) {
        if let Some(ts) = last_commit_ts(&t.path) {
            if now > ts && (now - ts) > threshold {
                let days = (now - ts) / (24 * 60 * 60);
                findings.push(Finding {
                    file: filename(t),
                    rule: "stale-wip".into(),
                    message: format!("in_progress for {} days (last commit)", days),
                    severity: "info".into(),
                });
            }
        }
    }

    findings
}

/// Check frontier health — high-priority tickets still open on the frontier.
pub fn check_frontier_health(corpus: &[Ticket]) -> Vec<Finding> {
    let mut findings = Vec::new();

    let frontier_ids: std::collections::HashSet<&str> = core::frontier(corpus)
        .iter()
        .map(|t| t.id.as_str())
        .collect();

    for t in corpus {
        if t.status == Status::Open && t.is_high_priority() && frontier_ids.contains(t.id.as_str())
        {
            findings.push(Finding {
                file: filename(t),
                rule: "high-priority-open".into(),
                message: "high-priority ticket still open".into(),
                severity: "info".into(),
            });
        }
    }

    findings
}

/// Flag done tickets that have validation_criteria but were closed without evidence.
pub fn check_validation_evidence(corpus: &[Ticket]) -> Vec<Finding> {
    let mut findings = Vec::new();

    for t in corpus.iter().filter(|t| t.status == Status::Done) {
        if t.validation_criteria.is_empty() {
            continue;
        }
        // Check if the body contains a Verification section with evidence
        let has_verification = t.body.contains("### Verification") && t.body.contains("");

        if !has_verification {
            findings.push(Finding {
                file: filename(t),
                rule: "low-evidence-closure".into(),
                message: format!(
                    "{} validation criteria defined but no evidence recorded",
                    t.validation_criteria.len()
                ),
                severity: "warning".into(),
            });
        }
    }

    findings
}

fn filename(t: &Ticket) -> String {
    t.path
        .file_name()
        .unwrap_or_default()
        .to_string_lossy()
        .to_string()
}

// --- Deep audit rules (--deep) ---
// Only PURELY MECHANICAL checks belong here. Judgment calls (evidence quality,
// resolution substance, force justification adequacy) belong to the companion
// skill (references/audit-quality.md) where agents can reason about context.

/// Check evidence count vs criteria count on done tickets.
/// Purely mechanical: if N criteria defined, there should be N evidence items.
pub fn check_evidence_count(corpus: &[Ticket]) -> Vec<Finding> {
    let mut findings = Vec::new();

    for t in corpus.iter().filter(|t| t.status == Status::Done) {
        if t.validation_criteria.is_empty() {
            continue;
        }

        // Look for evidence in the Verification section
        let evidence_lines: Vec<&str> = t
            .body
            .lines()
            .skip_while(|l| !l.contains("### Verification"))
            .skip(1)
            .take_while(|l| !l.starts_with("## ") && !l.starts_with("### "))
            .filter(|l| l.starts_with("- ") || l.starts_with(""))
            .collect();

        if !evidence_lines.is_empty() && evidence_lines.len() < t.validation_criteria.len() {
            findings.push(Finding {
                file: filename(t),
                rule: "evidence-count-mismatch".into(),
                message: format!(
                    "{} validation criteria but only {} evidence items",
                    t.validation_criteria.len(),
                    evidence_lines.len()
                ),
                severity: "warning".into(),
            });
        }
    }

    findings
}

/// Check for template-only tickets (closed with no real content added).
/// Purely mechanical: exact string match for placeholder text.
/// Skips tickets with migration/superseded resolutions (legitimate force-close pattern).
pub fn check_template_only(corpus: &[Ticket]) -> Vec<Finding> {
    let mut findings = Vec::new();

    for t in corpus.iter().filter(|t| t.status == Status::Done) {
        // Skip tickets with migration resolutions — template body is expected
        let resolution_lower = t
            .body
            .split_once("## Resolution")
            .map(|(_, after)| after.to_lowercase())
            .unwrap_or_default();
        if resolution_lower.contains("migrated to")
            || resolution_lower.contains("superseded by")
            || resolution_lower.contains("moved to")
        {
            continue;
        }

        let has_tbd_body =
            t.body.contains("## What to build\n\nTBD") || t.body.contains("## What to build\n\n\n");
        let has_tbd_ac = t.body.contains("- [ ] TBD");

        if has_tbd_body || has_tbd_ac {
            findings.push(Finding {
                file: filename(t),
                rule: "template-only-closure".into(),
                message: "closed with template placeholders still present".into(),
                severity: "warning".into(),
            });
        }
    }

    findings
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;

    // --- Resolution quality ---

    #[test]
    fn resolution_quality_missing_section() {
        let corpus = vec![done_ticket("01", "auth", "# Auth\n\nSome body text.\n")];
        let findings = check_resolution_quality(&corpus);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule, "missing-resolution");
    }

    #[test]
    fn resolution_quality_tbd() {
        let corpus = vec![done_ticket(
            "01",
            "auth",
            "# Auth\n\n## Resolution (2026-01-01)\n\nTBD\n",
        )];
        let findings = check_resolution_quality(&corpus);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule, "tbd-resolution");
    }

    #[test]
    fn resolution_quality_empty() {
        let corpus = vec![done_ticket(
            "01",
            "auth",
            "# Auth\n\n## Resolution (2026-01-01)\n\n",
        )];
        let findings = check_resolution_quality(&corpus);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule, "tbd-resolution");
    }

    #[test]
    fn resolution_quality_good() {
        let corpus = vec![done_ticket(
            "01",
            "auth",
            "# Auth\n\n## Resolution (2026-01-01)\n\nShipped JWT tokens.\n",
        )];
        let findings = check_resolution_quality(&corpus);
        assert!(findings.is_empty());
    }

    #[test]
    fn resolution_quality_skips_non_done() {
        let corpus = vec![open_ticket(
            "01",
            "auth",
            "# Auth\n\nNo resolution section.\n",
        )];
        let findings = check_resolution_quality(&corpus);
        assert!(findings.is_empty());
    }

    // --- AC completeness ---

    #[test]
    fn ac_completeness_all_unchecked() {
        let corpus = vec![done_ticket(
            "01",
            "auth",
            "# Auth\n\n## Acceptance criteria\n\n- [ ] First\n- [ ] Second\n\n## Resolution (2026-01-01)\n\nDone\n",
        )];
        let findings = check_ac_completeness(&corpus);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule, "all-acs-unchecked-on-done");
        assert!(findings[0].message.contains("2 unchecked"));
    }

    #[test]
    fn ac_completeness_partially_checked_ok() {
        let corpus = vec![done_ticket(
            "01",
            "auth",
            "# Auth\n\n## Acceptance criteria\n\n- [x] First\n- [ ] Second\n\n## Resolution (2026-01-01)\n\nDone\n",
        )];
        let findings = check_ac_completeness(&corpus);
        assert!(findings.is_empty());
    }

    #[test]
    fn ac_completeness_no_ac_section_ok() {
        let corpus = vec![done_ticket(
            "01",
            "auth",
            "# Auth\n\n## Resolution (2026-01-01)\n\nDone\n",
        )];
        let findings = check_ac_completeness(&corpus);
        assert!(findings.is_empty());
    }

    // --- Stale WIP ---

    #[test]
    fn stale_wip_detected() {
        let corpus = vec![wip_ticket("01", "auth")];
        let now = 1_000_000;
        let eight_days_ago = now - 8 * 24 * 60 * 60;
        let findings = check_stale_wip(&corpus, now, |_| Some(eight_days_ago));
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule, "stale-wip");
        assert!(findings[0].message.contains("8 days"));
    }

    #[test]
    fn stale_wip_recent_ok() {
        let corpus = vec![wip_ticket("01", "auth")];
        let now = 1_000_000;
        let two_days_ago = now - 2 * 24 * 60 * 60;
        let findings = check_stale_wip(&corpus, now, |_| Some(two_days_ago));
        assert!(findings.is_empty());
    }

    #[test]
    fn stale_wip_no_timestamp_ok() {
        let corpus = vec![wip_ticket("01", "auth")];
        let findings = check_stale_wip(&corpus, 1_000_000, |_| None);
        assert!(findings.is_empty());
    }

    #[test]
    fn stale_wip_skips_non_wip() {
        let corpus = vec![open_ticket("01", "auth", "# Auth\n")];
        let old_ts = 0;
        let findings = check_stale_wip(&corpus, 1_000_000, |_| Some(old_ts));
        assert!(findings.is_empty());
    }

    // --- Frontier health ---

    #[test]
    fn frontier_health_high_priority_flagged() {
        let corpus = vec![open_ticket_with_priority("01", "auth", "high")];
        let findings = check_frontier_health(&corpus);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].rule, "high-priority-open");
    }

    #[test]
    fn frontier_health_medium_priority_ok() {
        let corpus = vec![open_ticket_with_priority("01", "auth", "medium")];
        let findings = check_frontier_health(&corpus);
        assert!(findings.is_empty());
    }

    #[test]
    fn frontier_health_blocked_high_not_flagged() {
        // High priority but blocked — not on frontier
        let corpus = vec![
            open_ticket("99", "blocker", "# Blocker\n"),
            blocked_high_ticket("01", "auth", "99"),
        ];
        let findings = check_frontier_health(&corpus);
        assert!(findings.is_empty());
    }

    // --- Test helpers ---

    fn done_ticket(id: &str, slug: &str, body: &str) -> Ticket {
        let content = format!(
            "---\nid: \"{}\"\ntitle: \"{}\"\nstatus: done\nblocked_by: []\n---\n{}",
            id, slug, body
        );
        Ticket::parse_str(&content, &PathBuf::from(format!("{}-{}.md", id, slug))).unwrap()
    }

    fn open_ticket(id: &str, slug: &str, body: &str) -> Ticket {
        let content = format!(
            "---\nid: \"{}\"\ntitle: \"{}\"\nstatus: open\nblocked_by: []\n---\n{}",
            id, slug, body
        );
        Ticket::parse_str(&content, &PathBuf::from(format!("{}-{}.md", id, slug))).unwrap()
    }

    fn wip_ticket(id: &str, slug: &str) -> Ticket {
        let content = format!(
            "---\nid: \"{}\"\ntitle: \"{}\"\nstatus: in_progress\nblocked_by: []\n---\n\n# {}\n",
            id, slug, slug
        );
        Ticket::parse_str(&content, &PathBuf::from(format!("{}-{}.md", id, slug))).unwrap()
    }

    fn open_ticket_with_priority(id: &str, slug: &str, priority: &str) -> Ticket {
        let content = format!(
            "---\nid: \"{}\"\ntitle: \"{}\"\nstatus: open\nblocked_by: []\npriority: {}\n---\n\n# {}\n",
            id, slug, priority, slug
        );
        Ticket::parse_str(&content, &PathBuf::from(format!("{}-{}.md", id, slug))).unwrap()
    }

    fn blocked_high_ticket(id: &str, slug: &str, blocked_by: &str) -> Ticket {
        let content = format!(
            "---\nid: \"{}\"\ntitle: \"{}\"\nstatus: open\nblocked_by: [\"{}\"]\npriority: high\n---\n\n# {}\n",
            id, slug, blocked_by, slug
        );
        Ticket::parse_str(&content, &PathBuf::from(format!("{}-{}.md", id, slug))).unwrap()
    }
}