Find the longest streak of consecutive failed runs per pipeline

HardSQLPro

Why it matters Streak detection drives alerting on flaky jobs, consecutive eval failures and user activity streaks.

A flaky training pipeline is worst when it fails many times in a row. The longest run of consecutive failures per pipeline is a gaps and islands problem.

Write query() returning a SQLite SQL string. Table runs(pipeline TEXT, run_no INTEGER, status TEXT) with status 'ok' or 'failed'. Runs are ordered by run_no within a pipeline. run_no values can skip numbers (deleted runs); a skipped number does NOT break a streak, only an 'ok' run does.

Return (pipeline, longest) for every pipeline, where longest is the length of its longest streak of consecutive 'failed' runs in run_no order, and 0 if it never failed. Order by pipeline ascending.

Signaturedef query()

Example

pipeline 'p' with run_no 1 failed, 2 failed, 3 ok, 4 failed gives [('p', 2)].

Understand the idea

Gaps and islands means finding runs of consecutive rows that share a property. The classic trick: number all rows in order, number rows again within each status, and subtract. Inside an island both numbers rise together, so the difference is constant; it changes when the island is broken.

Worked example: statuses F, F, ok, F. Overall row numbers 1, 2, 3, 4. Row numbers within status: F gets 1, 2, then 3 for the last F; ok gets 1. Differences for the F rows: 1 - 1 = 0, 2 - 2 = 0, 4 - 3 = 1. So the islands are {rows 1, 2} with grp 0 and {row 4} with grp 1. Longest = 2.

Rule: grp = ROW_NUMBER() OVER (PARTITION BY key ORDER BY t) - ROW_NUMBER() OVER (PARTITION BY key, status ORDER BY t).

Used for failure streaks in CI and training pipelines and for daily activity streaks.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: example_run1(query())
  • Sample 2: skipped run numbers_run2(query())