From fde8571ef381cdf86ff97e41224d8fc7721987cd Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 20 Nov 2026 00:03:24 +0100 Subject: [PATCH] Batch trace: Windows panic paths and relative work/ paths, one base for a drop's batches with suspects first, unreadable results wait out the rerun A panic path may start with a drive letter; only an absolute path loses a runner checkout prefix. The batches a trace starts are built on one fetched head, and the suspects start before the rest, so a full house defers the rest, never the tests alone. After the rerun, results that cannot be read count as not fresh: the batch waits until the deadline. The fake default branch follows setMain, and the rebuild test checks the new base. Co-Authored-By: Claude Opus 5.5 --- src/merge/batch-blame.ts | 10 +++++--- src/merge/batch.ts | 26 +++++++++++++------- test/batch-trace.test.ts | 51 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/src/merge/batch-blame.ts b/src/merge/batch-blame.ts index 41b5f1a0..47a1283e 100644 --- a/src/merge/batch-blame.ts +++ b/src/merge/batch-blame.ts @@ -18,10 +18,14 @@ export const MAX_SUSPECTS = 3; const ANSI = /\u001b\[[0-9;?]*[A-Za-z]/g; const ARROW = /^\s*-->\s+(\S+?):(\d+)(?::\d+)?\s*$/; // `thread 'x' panicked at src/a.rs:3:5:` (Rust 1.73 on) and `panicked at 'msg', src/a.rs:3:5` (before). -const PANIC = /panicked at (?:'.*?', |".*?", )?([^\s:'"]+\.rs):(\d+)(?::\d+)?/; +// The path may start with a Windows drive (`D:\a\app\app\src\a.rs:3:5`); its colon is not the line's. +const PANIC = /panicked at (?:'.*?', |".*?", )?((?:[A-Za-z]:)?[^\s:'"]+\.rs):(\d+)(?::\d+)?/; const ERROR = /^\s*error(\[E\d+\])?:/; -/** A runner's checkout prefix (`/home/runner/work///`, `D:\a\\\`), cut so the path is the repo's. */ -const CHECKOUT = /^(?:.*?[/\\](?:work|a)[/\\][^/\\]+[/\\][^/\\]+[/\\])/; +/** + * A runner's checkout prefix (`/home/runner/work///`, `D:\a\\\`), cut so the path is the repo's. + * Only an absolute path has one: a relative path such as `crates/work/x/y/lib.rs` is already the repo's. + */ +const CHECKOUT = /^(?:\/|[A-Za-z]:[/\\])(?:.*?[/\\])?(?:work|a)[/\\][^/\\]+[/\\][^/\\]+[/\\]/; /** A path inside the repo, or null: relative, no `..`, no option-like or odd characters, not a dependency's source. */ export function repoPath(raw: string): string | null { diff --git a/src/merge/batch.ts b/src/merge/batch.ts index bd31ba38..660f5365 100644 --- a/src/merge/batch.ts +++ b/src/merge/batch.ts @@ -145,9 +145,12 @@ export async function runBatches(db: DatabaseSync, params: BatchParams, log: (li if (r.code !== 0) log(` ${b.branch}: could not close the batch PR: ${oneLine(r.stderr)}`); }; - /** Builds a batch of the candidates, in order, from the current head of the default branch; null when no PR could be merged. */ - async function startBatch(cands: Cand[], max: number, parentId: number | null): Promise { - const base = await git.fetchMain(ownerOf(repo), repo, branch); + /** + * Builds a batch of the candidates, in order, from the current head of the default branch (or `onBase`, a head the caller + * fetched for several batches that must share it); null when no PR could be merged. + */ + async function startBatch(cands: Cand[], max: number, parentId: number | null, onBase?: string): Promise { + const base = onBase ?? await git.fetchMain(ownerOf(repo), repo, branch); const who = await whoAmI(d.exec); const author = await commitIdentity(db, repo, async () => who); setPipelineLogin(db, who.login); @@ -278,12 +281,16 @@ export async function runBatches(db: DatabaseSync, params: BatchParams, log: (li for (const [n, w] of traced) audit(db, ACTOR, "batch-blame", refOf(n), { run: runId, batch: b.branch, reason: oneLine(w, 200) }); s.split.push(b.branch); log(` ${b.branch}: ${why}; traced to ${list}`); - const rest = members.filter((m) => !traced.has(m.number) && !mergeHeld(db, repo, m.number)); - if (rest.length && left() > 0 && activeBatches(db, repo).length < MAX_ACTIVE) await startBatch(asCands(rest), left(), b.id); + // Every batch started here is built on one head, so a suspect alone is never red from a change the others did not have. + const base = await git.fetchMain(ownerOf(repo), repo, branch); + // The suspects go first: their tests alone are what the trace asks for. A rest that finds no room returns to the queue + // and is batched again later; nothing of it is lost. for (const m of members.filter((x) => traced.has(x.number) && !mergeHeld(db, repo, x.number))) { if (left() <= 0 || activeBatches(db, repo).length >= MAX_ACTIVE) break; - await startBatch(asCands([m]), 1, b.id); + await startBatch(asCands([m]), 1, b.id, base); } + const rest = members.filter((m) => !traced.has(m.number) && !mergeHeld(db, repo, m.number)); + if (rest.length && left() > 0 && activeBatches(db, repo).length < MAX_ACTIVE) await startBatch(asCands(rest), left(), b.id, base); } /** The members a failure's log text points at, when that is a trace worth following (some, not all, and few). */ @@ -342,9 +349,10 @@ export async function runBatches(db: DatabaseSync, params: BatchParams, log: (li return; } const failure = await readBatchFailure(b); - if (b.ci_reruns && b.ci_rerun_at && failure) { - // Only results from after the rerun say it failed again; older ones are what the rerun replaces. - const fresh = failure.checks.every((c) => !!c.completedAt && Date.parse(c.completedAt) > Date.parse(b.ci_rerun_at!)); + if (b.ci_reruns && b.ci_rerun_at) { + // Only results from after the rerun say it failed again; older ones are what the rerun replaces. Results that cannot + // be read are not fresh either: a GitHub hiccup must not split or hold before the wait is over. + const fresh = !!failure && failure.checks.every((c) => !!c.completedAt && Date.parse(c.completedAt) > Date.parse(b.ci_rerun_at!)); if (!fresh && d.now().getTime() < Date.parse(b.ci_rerun_at) + RERUN_WAIT_MS) { s.waiting.push(b.branch); return; } } const traced = failure && await traceOf(b, members, failure.checks.flatMap((c) => c.steps.map((x) => x.tail)).join("\n")); diff --git a/test/batch-trace.test.ts b/test/batch-trace.test.ts index 4198c450..90e05959 100644 --- a/test/batch-trace.test.ts +++ b/test/batch-trace.test.ts @@ -41,6 +41,10 @@ describe("failure locations", () => { ]); for (const bad of ["-rf", "../x.rs", "a//b.rs", "a/./b.rs", "target/debug/x.rs", "x y.rs", "$(rm).rs"]) assert.equal(repoPath(bad), null, bad); assert.equal(repoPath("D:\\a\\app\\app\\src\\win.rs"), "src/win.rs"); + // Only an absolute path loses a checkout prefix; a relative one is the repo's as it is. + assert.equal(repoPath("crates/work/x/y/lib.rs"), "crates/work/x/y/lib.rs"); + assert.equal(repoPath("/home/runner/work/app/app/crates/work/x/y/lib.rs"), "crates/work/x/y/lib.rs"); + assert.deepEqual(failureLocations("thread 'w' panicked at D:\\a\\app\\app\\src\\win.rs:9:1:\n"), [{ file: "src/win.rs", line: 9, what: "panic" }]); }); }); @@ -73,6 +77,7 @@ function setup(prs: number[], o: { build?: boolean } = {}) { if (a[0] === "diff-tree") return ok("diff --git a/x b/x\n--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a\n+b\n"); if (a[0] === "rev-list") return ok(`${sha40(Number(/sha(\d+)/.exec(a[1]!)![1]))}\n`); if (a[0] === "diff" && a.includes("--name-only")) return ok(`${(fake.files[Number(/sha(\d+)$/.exec(a.at(-1)!)![1])] ?? []).join("\n")}\n`); + if (a[0] === "rev-parse" && a[1] === "refs/batch/main") return ok(`${mainHead}\n`); if (a[0] === "blame") { const n = fake.blame[`${a.at(-1)}:${a[a.indexOf("-L") + 1]!.split(",")[0]}`]; return n ? ok(`${sha40(n)} 4 4 1\nauthor x\n`) : ok(`${MAIN} 4 4 1\n`); @@ -94,7 +99,8 @@ function setup(prs: number[], o: { build?: boolean } = {}) { const membersOf = (id: number) => w.rows("SELECT number FROM merge_batch_prs WHERE batch_id = ? ORDER BY position", id).map((r) => r.number); const red = () => w.gh.live(900, { ci: "failure" }); const setMain = (sha: string) => { mainHead = sha; }; - return { w, fake, reruns, run, batches, membersOf, red, setMain }; + const baseOf = (id: number) => (w.rows("SELECT base_sha FROM merge_batches WHERE id = ?", id)[0] as { base_sha: string }).base_sha; + return { w, fake, reruns, run, batches, membersOf, red, setMain, baseOf, deps }; } const errorAt = (file: string, line: number) => `test\tRun tests\terror[E0308]: mismatched types\ntest\tRun tests\t --> ${file}:${line}:5\n`; @@ -112,7 +118,7 @@ describe("a red batch", () => { const rows = t.batches(); assert.equal(rows[0]!.state, "split"); assert.match(rows[0]!.summary!, /traced to #2 \(the error at src\/b\.rs:4 is in its commit c000000\)/); - assert.deepEqual(rows.slice(1).map((b) => [b.parent_id, t.membersOf(b.id)]), [[1, [1, 3]], [1, [2]]]); + assert.deepEqual(rows.slice(1).map((b) => [b.parent_id, t.membersOf(b.id)]), [[1, [2]], [1, [1, 3]]]); assert.equal(batchSizeOf(t.w.db, "app"), size); assert.deepEqual(t.reruns, []); assert.equal(t.w.rows("SELECT target FROM audit WHERE action = 'batch-blame'").map((r) => r.target).join(), "app#2"); @@ -182,6 +188,7 @@ describe("a red batch", () => { assert.equal(s.superseded.length, 1); assert.equal(t.batches()[0]!.state, "superseded"); assert.deepEqual(t.membersOf(t.batches()[1]!.id), [1, 2]); + assert.equal(t.baseOf(t.batches()[1]!.id), "d".repeat(40)); assert.deepEqual(t.w.rows("SELECT number FROM stages WHERE stage = 'hold'"), []); }); @@ -199,6 +206,44 @@ describe("a red batch", () => { await t.run(); const rows = t.batches(); assert.match(rows[0]!.summary!, /combined tree failed at test; traced to #3/); - assert.deepEqual(rows.slice(1).map((b) => t.membersOf(b.id)), [[1, 2], [3]]); + assert.deepEqual(rows.slice(1).map((b) => t.membersOf(b.id)), [[3], [1, 2]]); + }); + + test("after the rerun, results that cannot be read wait out the deadline instead of holding the PR", async () => { + const t = setup([1]); + await t.run(); + t.red(); + await t.run(); + assert.deepEqual(t.reruns, ["77"]); + // The check runs cannot be read now: no fresh result, so the batch waits. + const exec = t.deps.exec; + t.deps.exec = async (cmd, args, opts) => (cmd === "gh" && args[0] === "api" && /check-runs/.test(args[1] ?? "") ? { code: 1, stdout: "", stderr: "HTTP 502" } : exec(cmd, args, opts)); + assert.equal((await t.run()).waiting.length, 1); + assert.equal(t.batches()[0]!.state, "open"); + assert.deepEqual(t.w.rows("SELECT number FROM stages WHERE stage = 'hold'"), []); + // Past the deadline the batch goes on, as it would with stale results. + t.w.db.prepare("UPDATE merge_batches SET ci_rerun_at = '2026-01-01T00:00:00Z'").run(); + await t.run(); + assert.equal(t.batches()[0]!.state, "failed"); + assert.deepEqual(t.w.rows("SELECT number FROM stages WHERE stage = 'hold'").map((r) => r.number), [1]); + }); + + test("the batches a trace starts share one base, also when the default branch moves between them", async () => { + const t = setup([1, 2, 3]); + await t.run(); + t.fake.log = errorAt("src/b.rs", 4); + t.fake.blame["src/b.rs:4"] = 2; + t.red(); + // The default branch moves after each fetch: separate fetches would give each batch its own base. + let fetches = 0; + const exec = t.deps.exec; + t.deps.exec = async (cmd, args, opts) => { + if (cmd === "git" && args.includes("fetch")) t.setMain(`${"e".repeat(39)}${++fetches}`); + return exec(cmd, args, opts); + }; + await t.run(); + const started = t.batches().slice(1); + assert.equal(started.length, 2); + assert.equal(t.baseOf(started[0]!.id), t.baseOf(started[1]!.id)); }); });