Commit ac261fc
[autorevert] dedupe shared recovery-detection pipeline across the two metrics queries (#8180)
## Summary
`autorevert_significant_reverts` and `autorevert_weekly_metrics` both
implement the same red/green-streak recovery-detection +
causal-attribution pipeline — ~140 identical lines (`commits` → … →
`recovery_with_attribution`). They've drifted once already: the causal
red-streak filter from #8176 had to be applied to each file separately,
and fixing only the summary query left the weekly chart stale until both
were patched.
This PR makes that shared pipeline a single, drift-proof definition.
## Approach
torchci has no SQL include/templating mechanism (each `query.sql` is
sent to ClickHouse verbatim), and ClickHouse view DDL is deployed
manually / out-of-band. A shared **parameterized view** would be the DRY
end-state, but it can't be created or verified from the read-only query
path, and a merged query referencing a not-yet-deployed view would 500
the HUD metrics page until the DDL is applied by hand. So this keeps
both queries self-contained and runnable, but:
1. Factors the **entire** shared pipeline — `commits` through
`recovery_with_attribution`, plus a new `causally_attributed_recoveries`
CTE that **centralizes the #8176 causal filter** (`reverted_commit_sha =
'' OR has(red_shas, reverted_commit_sha)`) — into a block delimited by
`-- @autorevert-shared-recovery-pipeline:begin` / `:end` markers. The
block is **byte-identical** in both files. Only each query's final
aggregation (per-revert-commit vs per-week) differs, below the `:end`
marker.
2. Adds a **jest unit test**
(`torchci/test/autorevertSharedPipeline.test.ts`) that fails CI if the
two marked blocks aren't byte-identical, if a marker is
missing/duplicated, or if the two `params.json` declare different
`params`. The extractor itself is unit-tested (missing / duplicated /
reversed markers) so the guard can't silently no-op. A future fix to the
recovery pipeline therefore *cannot* land in one query but not the
other.
Moving the causal filter into the shared
`causally_attributed_recoveries` CTE is the key win: previously each
query re-stated it in its own tail (`reverts_only` /
`unique_recoveries`), which is exactly what drifted in #8176. Now it
lives once, inside the byte-identical block.
> **Note:** an earlier revision of this PR enforced the byte-identity
with a `lintrunner` adapter (`AUTOREVERT_SHARED_CTE`); per review it's
now a torchci jest test instead, so the guard runs in the normal test
suite rather than extending the linter stack.
## Validation
Confirmed against ClickHouse that both refactored queries return results
**identical** to the pre-refactor versions:
| query | window | result |
|---|---|---|
| `autorevert_significant_reverts` | 2026-06-08 .. 06-15 | identical (3
rows; PR #186928 / `889f6eb` correctly stays excluded) |
| `autorevert_weekly_metrics` | 2026-06-08 .. 06-15 | identical
(`human_revert_recoveries = 0`) |
| `autorevert_weekly_metrics` | 2026-05-01 .. 06-15 | identical across
all 7 weeks (incl. human-revert and non-revert paths) |
The transformation is algebraically equivalence-preserving:
`significant`'s `reverts_only` predicate `is_revert = 1 AND <causal>`
becomes `<causal>` (applied upstream) then `is_revert = 1`; `weekly`'s
`unique_recoveries` reads the same pre-filtered set it did before. The
extra (superset) columns now carried through `weekly`'s shared CTEs are
unused by its final aggregation and don't change row cardinality.
`prettier --check`, `tsc`, and `eslint` run clean on the new test
locally (the jest run itself needs Node ≥18; the test reads the two real
query files and asserts byte-identity + equal params, validated
independently here).
## A parameterized view as a possible future migration
If the team prefers the fully DRY form, this shared block is a clean
candidate for a parameterized ClickHouse view that both queries `SELECT`
from — but that needs a coordinated manual DDL deploy (the view must
exist before the deployed app references it) plus a one-time
confirmation that the read user can `SELECT` from a parameterized view
taking an `Array(String)` argument. I'm happy to follow up with that as
a separate, deploy-coordinated PR. This change is the deployment-free
first step that closes the drift hole now.
---------
Co-authored-by: Ivan <izaitsevfb@meta.com>1 parent a0685f6 commit ac261fc
3 files changed
Lines changed: 219 additions & 37 deletions
Lines changed: 32 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
5 | 13 | | |
6 | 14 | | |
7 | 15 | | |
| |||
264 | 272 | | |
265 | 273 | | |
266 | 274 | | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
275 | 285 | | |
276 | 286 | | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
282 | 301 | | |
283 | 302 | | |
284 | 303 | | |
| |||
Lines changed: 79 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
5 | 13 | | |
6 | 14 | | |
7 | 15 | | |
| |||
42 | 50 | | |
43 | 51 | | |
44 | 52 | | |
| 53 | + | |
45 | 54 | | |
46 | 55 | | |
47 | 56 | | |
| |||
64 | 73 | | |
65 | 74 | | |
66 | 75 | | |
| 76 | + | |
67 | 77 | | |
68 | 78 | | |
69 | 79 | | |
| |||
79 | 89 | | |
80 | 90 | | |
81 | 91 | | |
| 92 | + | |
82 | 93 | | |
83 | 94 | | |
84 | 95 | | |
| |||
96 | 107 | | |
97 | 108 | | |
98 | 109 | | |
| 110 | + | |
99 | 111 | | |
100 | 112 | | |
101 | 113 | | |
| |||
104 | 116 | | |
105 | 117 | | |
106 | 118 | | |
| 119 | + | |
107 | 120 | | |
108 | 121 | | |
109 | | - | |
| 122 | + | |
110 | 123 | | |
111 | 124 | | |
112 | 125 | | |
113 | 126 | | |
114 | 127 | | |
115 | 128 | | |
| 129 | + | |
116 | 130 | | |
117 | 131 | | |
118 | 132 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
123 | 139 | | |
124 | 140 | | |
125 | 141 | | |
| 142 | + | |
126 | 143 | | |
127 | 144 | | |
128 | 145 | | |
| |||
138 | 155 | | |
139 | 156 | | |
140 | 157 | | |
141 | | - | |
142 | | - | |
143 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
144 | 163 | | |
145 | 164 | | |
146 | 165 | | |
| |||
151 | 170 | | |
152 | 171 | | |
153 | 172 | | |
| 173 | + | |
154 | 174 | | |
155 | 175 | | |
156 | 176 | | |
| |||
161 | 181 | | |
162 | 182 | | |
163 | 183 | | |
164 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
165 | 187 | | |
166 | 188 | | |
167 | 189 | | |
| |||
173 | 195 | | |
174 | 196 | | |
175 | 197 | | |
176 | | - | |
| 198 | + | |
177 | 199 | | |
178 | 200 | | |
179 | 201 | | |
| |||
185 | 207 | | |
186 | 208 | | |
187 | 209 | | |
| 210 | + | |
188 | 211 | | |
189 | 212 | | |
190 | 213 | | |
191 | 214 | | |
192 | | - | |
| 215 | + | |
193 | 216 | | |
194 | 217 | | |
195 | 218 | | |
| 219 | + | |
196 | 220 | | |
197 | 221 | | |
198 | 222 | | |
199 | 223 | | |
200 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
201 | 235 | | |
202 | 236 | | |
203 | 237 | | |
204 | 238 | | |
205 | 239 | | |
| 240 | + | |
206 | 241 | | |
207 | 242 | | |
208 | 243 | | |
| |||
211 | 246 | | |
212 | 247 | | |
213 | 248 | | |
214 | | - | |
| 249 | + | |
215 | 250 | | |
216 | 251 | | |
217 | 252 | | |
| |||
222 | 257 | | |
223 | 258 | | |
224 | 259 | | |
| 260 | + | |
| 261 | + | |
225 | 262 | | |
| 263 | + | |
| 264 | + | |
226 | 265 | | |
227 | 266 | | |
228 | 267 | | |
229 | | - | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
230 | 271 | | |
231 | 272 | | |
232 | 273 | | |
233 | 274 | | |
234 | | - | |
235 | | - | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
236 | 299 | | |
237 | 300 | | |
238 | 301 | | |
239 | 302 | | |
240 | 303 | | |
241 | 304 | | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
| 305 | + | |
251 | 306 | | |
252 | 307 | | |
253 | 308 | | |
| |||
0 commit comments