Skip to content

Commit 8d4021c

Browse files
voidstackloopclaude
andcommitted
Detect flaky checks across a form's full recorded history
Feature 2 of 4 requested large improvements. history::diff only ever compares two adjacent runs, so it can't distinguish "this check regressed and stayed broken" (worth investigating the one real change) from "this check has been flip-flopping for weeks" (worth investigating why it's unstable before trusting any single run's verdict at all) — both just look like "it changed" to diff. Added history::flakiness(runs): for each check present in the most recent run, counts how many times its status differed from the run right before it across the *entire* recorded history for that form. 2+ transitions is flagged flaky; 0 or 1 is a stable check or a single genuine regression/fix, not flakiness. Wired into report::build via a new FormReport.flaky field and a flakiness_of(check) lookup method, surfaced as a "⚠ Flaky" badge in --html and a "[FLAKY]" note in plain text (both `formwatch report` and `formwatch test`/`monitor`'s own per-run printing, which computes it from the same history::load_runs call they already make). Verified the >=2 threshold is real by temporarily changing it to >=1 and watching the "single sustained regression is not flakiness" test fail exactly as predicted, then restoring it. Separately verified report::build's wiring (not just flakiness()'s own logic, already covered directly in history.rs) with a dedicated test building three runs — one check flip-flops, one has a single sustained change — and confirming report::build only flags the former; verified that test too by loosening flakiness_of's filter and watching it catch the resulting false positive. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 88d9a0a commit 8d4021c

6 files changed

Lines changed: 238 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ project doesn't have a release yet, so everything below is grouped under
3232
already-changed page. A Pass carries no screenshot: it needs no
3333
evidence, and capturing one for every check on every run would just
3434
bloat history for no benefit.
35+
- Flakiness detection: `formwatch report` (plain-text and `--html`) now
36+
flags a check whose status has flip-flopped across its *entire*
37+
recorded history (2+ transitions), not just the single immediately-
38+
previous run `diff` compares against. A check that changed once and
39+
stayed changed is a genuine regression or fix, not flakiness — only
40+
the unstable, back-and-forth case gets the "Flaky" badge.
3541

3642
### Fixed
3743

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ the moment it finished, embedded directly in `--html`/`--json` output (a
9494
PASS carries none — it needs no evidence). The plain-text/colored output
9595
just notes that one was captured, since a terminal can't render it.
9696

97+
`formwatch report` also looks across a form's *entire* recorded history
98+
(not just the single previous run) and flags any check whose status has
99+
flip-flopped rather than settling — a `[FLAKY]` note in plain text, a
100+
"⚠ Flaky" badge in `--html`. A check that changed once and stayed changed
101+
is a genuine regression or fix, not flakiness; only a check that's
102+
genuinely unstable across several runs gets flagged.
103+
97104
## Custom checks
98105

99106
Anything specific to your forms that the built-in checks don't cover — a

‎src/history.rs‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,64 @@ pub fn diff(prev: &RunResult, curr: &RunResult) -> Vec<CheckChange> {
156156
changes
157157
}
158158

159+
/// One check's behavior across a form's *entire* recorded history, not
160+
/// just the single most recent run — whether it's been flip-flopping
161+
/// between statuses rather than settling into one sustained state. A
162+
/// flaky check calls for a different response (investigate *why* it's
163+
/// inconsistent) than a genuine regression or fix (investigate the one
164+
/// real change); [`diff`] alone can't tell the two apart since it only
165+
/// ever compares two adjacent runs.
166+
#[derive(Debug, Clone, PartialEq, Eq)]
167+
pub struct Flakiness {
168+
/// Which check this is — matched by name across every run, the same
169+
/// way [`diff`] matches between two.
170+
pub name: String,
171+
/// How many times this check's status differed from the run right
172+
/// before it, across every run recorded for the form. 0 means it's
173+
/// never changed; 1 means a single sustained change (a genuine
174+
/// regression or fix); 2 or more means it's genuinely flapping back
175+
/// and forth, not settling into a state.
176+
pub transitions: usize,
177+
/// The status as of the most recent run.
178+
pub current: Status,
179+
}
180+
181+
impl Flakiness {
182+
/// True once a check has changed status more than once across its
183+
/// recorded history — flapping, not a single sustained change.
184+
pub fn is_flaky(&self) -> bool {
185+
self.transitions >= 2
186+
}
187+
}
188+
189+
/// Computes every check's transition count across `runs` (oldest first,
190+
/// as [`load_runs`] returns them) for one form. Only checks present in
191+
/// the most recent run are reported — a check that's since been removed
192+
/// (a custom check deleted, a built-in one renamed) has nothing current
193+
/// to report flakiness *of*.
194+
pub fn flakiness(runs: &[RunResult]) -> Vec<Flakiness> {
195+
let Some(latest) = runs.last() else {
196+
return vec![];
197+
};
198+
latest
199+
.checks
200+
.iter()
201+
.map(|c| {
202+
let history: Vec<Status> = runs
203+
.iter()
204+
.filter_map(|r| r.checks.iter().find(|x| x.name == c.name))
205+
.map(|x| x.status)
206+
.collect();
207+
let transitions = history.windows(2).filter(|w| w[0] != w[1]).count();
208+
Flakiness {
209+
name: c.name.clone(),
210+
transitions,
211+
current: c.status,
212+
}
213+
})
214+
.collect()
215+
}
216+
159217
#[cfg(test)]
160218
mod tests {
161219
use super::*;
@@ -207,6 +265,62 @@ mod tests {
207265
assert!(diff(&prev, &curr).is_empty());
208266
}
209267

268+
#[test]
269+
fn flakiness_is_zero_for_a_check_that_never_changed() {
270+
let runs = vec![
271+
run(vec![check("Accessibility", Status::Pass)]),
272+
run(vec![check("Accessibility", Status::Pass)]),
273+
run(vec![check("Accessibility", Status::Pass)]),
274+
];
275+
let report = flakiness(&runs);
276+
assert_eq!(report.len(), 1);
277+
assert_eq!(report[0].transitions, 0);
278+
assert!(!report[0].is_flaky());
279+
}
280+
281+
#[test]
282+
fn flakiness_does_not_flag_a_single_sustained_regression() {
283+
// diff() alone would call this "a regression" — which is exactly
284+
// right here: it changed once and stayed changed. Flakiness is
285+
// specifically about the *unstable* case diff can't see because
286+
// it only ever compares two adjacent runs.
287+
let runs = vec![
288+
run(vec![check("Accessibility", Status::Pass)]),
289+
run(vec![check("Accessibility", Status::Fail)]),
290+
run(vec![check("Accessibility", Status::Fail)]),
291+
];
292+
let report = flakiness(&runs);
293+
assert_eq!(report[0].transitions, 1);
294+
assert!(
295+
!report[0].is_flaky(),
296+
"a single sustained change is a real regression, not flakiness"
297+
);
298+
}
299+
300+
#[test]
301+
fn flakiness_flags_a_check_that_flips_back_and_forth() {
302+
let runs = vec![
303+
run(vec![check("Accessibility", Status::Pass)]),
304+
run(vec![check("Accessibility", Status::Fail)]),
305+
run(vec![check("Accessibility", Status::Pass)]),
306+
run(vec![check("Accessibility", Status::Fail)]),
307+
];
308+
let report = flakiness(&runs);
309+
assert_eq!(report[0].transitions, 3);
310+
assert!(report[0].is_flaky());
311+
}
312+
313+
#[test]
314+
fn flakiness_only_reports_checks_present_in_the_latest_run() {
315+
let runs = vec![
316+
run(vec![check("Removed check", Status::Fail)]),
317+
run(vec![check("Accessibility", Status::Pass)]),
318+
];
319+
let report = flakiness(&runs);
320+
assert_eq!(report.len(), 1);
321+
assert_eq!(report[0].name, "Accessibility");
322+
}
323+
210324
#[test]
211325
fn urls_differing_only_by_hyphen_vs_underscore_get_distinct_slugs() {
212326
// Regression test: collapsing every non-alphanumeric character to

‎src/main.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,30 +312,40 @@ fn print_run(history_dir: &Path, run: &history::RunResult, json: bool) -> Result
312312
}
313313

314314
println!("\n{}", format!("== {} ({}) ==", run.name, run.url).bold());
315+
let prior = history::load_runs(history_dir, &run.url)?;
316+
let flaky = history::flakiness(&prior);
315317
for check in &run.checks {
316-
print_check(check);
318+
print_check(check, is_flaky(&flaky, check));
317319
}
318320

319-
let prior = history::load_runs(history_dir, &run.url)?;
320321
if let Some(prev) = prior.iter().rev().find(|r| r.timestamp < run.timestamp) {
321322
print_changes(&history::diff(prev, run));
322323
}
323324
Ok(run_has_failure(run))
324325
}
325326

326-
fn print_check(check: &checks::CheckResult) {
327+
fn is_flaky(flaky: &[history::Flakiness], check: &checks::CheckResult) -> bool {
328+
flaky.iter().any(|f| f.name == check.name && f.is_flaky())
329+
}
330+
331+
fn print_check(check: &checks::CheckResult, flaky: bool) {
327332
let label = match check.status {
328333
checks::Status::Pass => check.status.label().green().to_string(),
329334
checks::Status::Warn => check.status.label().yellow().to_string(),
330335
checks::Status::Fail => check.status.label().red().to_string(),
331336
};
337+
let flaky_note = if flaky {
338+
" [FLAKY]".yellow().to_string()
339+
} else {
340+
String::new()
341+
};
332342
let screenshot_note = if check.screenshot.is_some() {
333343
" (screenshot captured — see --html/--json report)"
334344
} else {
335345
""
336346
};
337347
println!(
338-
" [{label}] {}: {}{screenshot_note}",
348+
" [{label}] {}{flaky_note}: {}{screenshot_note}",
339349
check.name, check.detail
340350
);
341351
}
@@ -369,7 +379,7 @@ fn print_report(history_dir: &Path) -> Result<()> {
369379
format!("== {} ({}) ==", form.run.name, form.run.url).bold()
370380
);
371381
for check in &form.run.checks {
372-
print_check(check);
382+
print_check(check, form.flakiness_of(check).is_some());
373383
}
374384
print_changes(&form.changes);
375385
}

‎src/report.rs‎

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::history::{self, CheckChange, RunResult};
1+
use crate::checks::CheckResult;
2+
use crate::history::{self, CheckChange, Flakiness, RunResult};
23
use anyhow::Result;
34
use askama::Template;
45
use chrono::DateTime;
@@ -12,11 +13,27 @@ pub struct FormReport {
1213
pub run: RunResult,
1314
/// Per-check status changes since this form's previous run, if any.
1415
pub changes: Vec<CheckChange>,
16+
/// Every current check's transition count across this form's
17+
/// *entire* recorded history, not just the last two runs — see
18+
/// [`history::flakiness`].
19+
pub flaky: Vec<Flakiness>,
1520
/// `run.timestamp`, formatted for display (e.g. `"2026-09-11 14:30
1621
/// UTC"`) rather than a raw Unix timestamp.
1722
pub when: String,
1823
}
1924

25+
impl FormReport {
26+
/// This check's flakiness record, if it's actually flaky (2+
27+
/// transitions across recorded history) — `None` for a check that's
28+
/// never changed or changed exactly once, so the template only has
29+
/// to ask one question to decide whether to show a badge.
30+
pub fn flakiness_of(&self, check: &CheckResult) -> Option<&Flakiness> {
31+
self.flaky
32+
.iter()
33+
.find(|f| f.name == check.name && f.is_flaky())
34+
}
35+
}
36+
2037
/// The full report model — every form formwatch has ever recorded a run
2138
/// for, newest-first. Renders as HTML via [`askama::Template`] using
2239
/// `templates/report.html`; the plain-text `formwatch report` command
@@ -50,8 +67,14 @@ pub fn build(history_dir: &Path) -> Result<ReportTemplate> {
5067
.find(|r| r.timestamp < run.timestamp)
5168
.map(|prev| history::diff(prev, &run))
5269
.unwrap_or_default();
70+
let flaky = history::flakiness(&prior_runs);
5371
let when = human_time(run.timestamp);
54-
forms.push(FormReport { run, changes, when });
72+
forms.push(FormReport {
73+
run,
74+
changes,
75+
flaky,
76+
when,
77+
});
5578
}
5679
Ok(ReportTemplate {
5780
forms,
@@ -114,4 +137,69 @@ mod tests {
114137

115138
let _ = std::fs::remove_dir_all(&dir);
116139
}
140+
141+
#[test]
142+
fn build_flags_a_flaky_check_but_not_a_stable_one() {
143+
// report::build wires history::flakiness in via prior_runs — this
144+
// test is really about that wiring, not flakiness()'s own logic
145+
// (already covered directly in history.rs's tests): a check that
146+
// flip-flopped across three runs should be flagged, and one that
147+
// changed once (a genuine regression) should not.
148+
let dir = std::env::temp_dir().join("formwatch-test-report-build-flaky");
149+
let _ = std::fs::remove_dir_all(&dir);
150+
151+
let checks = |flappy, sustained| {
152+
vec![
153+
CheckResult {
154+
name: "Flappy".to_string(),
155+
status: flappy,
156+
detail: String::new(),
157+
screenshot: None,
158+
},
159+
CheckResult {
160+
name: "Sustained".to_string(),
161+
status: sustained,
162+
detail: String::new(),
163+
screenshot: None,
164+
},
165+
]
166+
};
167+
for (ts, flappy, sustained) in [
168+
(1, Status::Pass, Status::Pass),
169+
(2, Status::Fail, Status::Fail),
170+
(3, Status::Pass, Status::Fail),
171+
] {
172+
history::save_run(
173+
&dir,
174+
&RunResult {
175+
name: "x".into(),
176+
url: "https://city.gov/apply".into(),
177+
timestamp: ts,
178+
checks: checks(flappy, sustained),
179+
},
180+
)
181+
.expect("save run");
182+
}
183+
184+
let report = build(&dir).expect("build");
185+
let form = &report.forms[0];
186+
let flappy_check = form.run.checks.iter().find(|c| c.name == "Flappy").unwrap();
187+
let sustained_check = form
188+
.run
189+
.checks
190+
.iter()
191+
.find(|c| c.name == "Sustained")
192+
.unwrap();
193+
194+
assert!(
195+
form.flakiness_of(flappy_check).is_some(),
196+
"a check that flip-flopped across history should be flagged flaky"
197+
);
198+
assert!(
199+
form.flakiness_of(sustained_check).is_none(),
200+
"a check with a single sustained change is a real regression, not flakiness"
201+
);
202+
203+
let _ = std::fs::remove_dir_all(&dir);
204+
}
117205
}

‎templates/report.html‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
.changes { padding: 0.5rem 1rem; font-size: 0.85rem; background: #fffbea; border-top: 1px solid #eee; }
2020
.changes ul { margin: 0.3rem 0 0 1.1rem; padding: 0; }
2121
.screenshot { max-width: 100%; max-height: 20rem; margin-top: 0.5rem; border: 1px solid #ddd; border-radius: 4px; display: block; }
22+
.flaky-badge { display: inline-block; margin-left: 0.4rem; padding: 0.1rem 0.45rem; border-radius: 4px; font-size: 0.7rem; font-weight: 600; background: #eee; color: #555; }
2223
</style>
2324
</head>
2425
<body>
@@ -34,7 +35,11 @@ <h2>{{ form.run.name }}<small>{{ form.run.url }} &middot; last checked {{ form.w
3435
<tr>
3536
<td><span class="badge {{ check.status.css_class() }}">{{ check.status.label() }}</span></td>
3637
<td>
37-
<strong>{{ check.name }}</strong><br>{{ check.detail }}
38+
<strong>{{ check.name }}</strong>
39+
{% if let Some(f) = form.flakiness_of(check) %}
40+
<span class="flaky-badge" title="Changed status {{ f.transitions }} times across recorded history">&#9888; Flaky</span>
41+
{% endif %}
42+
<br>{{ check.detail }}
3843
{% if let Some(shot) = check.screenshot %}
3944
<img class="screenshot" src="{{ shot }}" alt="{{ check.name }} screenshot">
4045
{% endif %}

0 commit comments

Comments
 (0)