From 25d9ca6ae6b82867fd2b8977ea9c44cb226d2d43 Mon Sep 17 00:00:00 2001 From: Maxwell Weinzierl Date: Tue, 24 Nov 2026 08:07:38 -0800 Subject: [PATCH 1/2] AI-148 A cancelled CI run wakes the thread watching it A commit watch counted cancelled runs as quiet and folded them into the next message's count; when a commit's only run ended cancelled (usa-rs USA-3143: a test shard at its time limit, PR Gate failed), nothing was posted, the watch ended after 20 minutes and the thread slept 7 hours. Cancelled runs are now held and posted with their failed and cancelled jobs (at once when a job failed, else when the stage ends), unless the thread has since watched a newer commit in the same checkout. A watch whose runs were all skipped tells the thread once instead of ending without a word. --- apps/server/src/t4/T4RunWatch.test.ts | 101 +++++++++++++++++++++++++- apps/server/src/t4/T4RunWatch.ts | 74 +++++++++++++++++-- 2 files changed, 169 insertions(+), 6 deletions(-) diff --git a/apps/server/src/t4/T4RunWatch.test.ts b/apps/server/src/t4/T4RunWatch.test.ts index 789f28125f674..e24891563d417 100644 --- a/apps/server/src/t4/T4RunWatch.test.ts +++ b/apps/server/src/t4/T4RunWatch.test.ts @@ -195,6 +195,7 @@ describe("t4 watch_workflow_run", () => { yield* h.use((w) => w.watchCommit({ ...input, commit: "def5678" })); // A decision succeeds and another is cancelled while staging runs: no wake. + // (Cancelled with no failed job waits for the stage like a success.) h.runs.set("21", { ...h.runs.get("21"), status: "completed", conclusion: "success" }); h.runs.set("23", { commit: "def5678", @@ -216,7 +217,7 @@ describe("t4 watch_workflow_run", () => { expect(h.delivered).toHaveLength(1); expect(h.delivered[0]!.text).toContain("train decision #21: success"); expect(h.delivered[0]!.text).toContain("dashboard #22: failure"); - expect(h.delivered[0]!.text).toContain("(1 more skipped or cancelled.)"); + expect(h.delivered[0]!.text).toContain("train decision #23: cancelled"); expect(h.delivered[0]!.text).toContain("Still running: staging #20"); h.runs.set("20", { ...h.runs.get("20"), status: "completed", conclusion: "success" }); @@ -227,6 +228,104 @@ describe("t4 watch_workflow_run", () => { }), ); + it.effect("posts a cancelled run at once when a job in it failed (AI-148)", () => + Effect.gen(function* () { + const h = harness(); + h.runs.set("50", { + commit: "fff6666", + displayTitle: "Keep each state's certificate", + status: "in_progress", + jobs: [], + }); + yield* h.use((w) => + w.watchCommit({ ...input, threadId: "t4-owner", commit: "fff6666", note: "hand off" }), + ); + // usa-rs USA-3143: a shard cancelled at its time limit, the PR gate failed. + h.runs.set("50", { + ...h.runs.get("50"), + status: "completed", + conclusion: "cancelled", + jobs: [ + { name: "Tests (Shard 2/2)", conclusion: "success" }, + { name: "Tests (Shard 1/2)", conclusion: "cancelled" }, + { name: "PR Gate", conclusion: "failure" }, + ], + }); + yield* h.use((w) => w.sweep); + expect(h.delivered).toHaveLength(1); + expect(h.delivered[0]!.threadId).toBe("t4-owner"); + expect(h.delivered[0]!.text).toContain( + "Keep each state's certificate #50: cancelled (failed: PR Gate; cancelled: Tests (Shard 1/2))", + ); + expect(h.delivered[0]!.text).toContain("not green until it passes"); + expect(h.delivered[0]!.text).toContain("Your note: hand off"); + + yield* TestClock.adjust("21 minutes"); + yield* h.use((w) => w.sweep); + expect(h.delivered).toHaveLength(1); + expect(JSON.parse(h.meta.get("workflow-commit-watches")!)).toEqual([]); + }), + ); + + it.effect("posts a cancelled run with no failed job once nothing else runs", () => + Effect.gen(function* () { + const h = harness(); + h.runs.set("51", { commit: "fff7777", displayTitle: "PR checks", status: "in_progress" }); + yield* h.use((w) => w.watchCommit({ ...input, commit: "fff7777" })); + h.runs.set("51", { + ...h.runs.get("51"), + status: "completed", + conclusion: "cancelled", + jobs: [{ name: "Tests", conclusion: "cancelled" }], + }); + yield* h.use((w) => w.sweep); + expect(h.delivered).toHaveLength(1); + expect(h.delivered[0]!.text).toContain("PR checks #51: cancelled (cancelled: Tests)"); + }), + ); + + it.effect("stays quiet about a run a newer push of the same thread cancelled", () => + Effect.gen(function* () { + const h = harness(); + h.runs.set("60", { commit: "aaa0001", displayTitle: "PR checks", status: "in_progress" }); + yield* h.use((w) => w.watchCommit({ ...input, commit: "aaa0001" })); + yield* TestClock.adjust("2 minutes"); + h.runs.set("61", { commit: "aaa0002", displayTitle: "PR checks", status: "in_progress" }); + yield* h.use((w) => w.watchCommit({ ...input, commit: "aaa0002" })); + h.runs.set("60", { ...h.runs.get("60"), status: "completed", conclusion: "cancelled" }); + yield* h.use((w) => w.sweep); + expect(h.delivered).toEqual([]); + + h.runs.set("61", { ...h.runs.get("61"), status: "completed", conclusion: "success" }); + yield* h.use((w) => w.sweep); + expect(h.delivered).toHaveLength(1); + expect(h.delivered[0]!.text).toContain("for commit aaa0002000"); + expect(h.delivered[0]!.text).toContain("PR checks #61: success"); + }), + ); + + it.effect("tells the thread when every run for the commit was skipped", () => + Effect.gen(function* () { + const h = harness(); + h.runs.set("70", { commit: "abc0070", displayTitle: "checks", status: "in_progress" }); + yield* h.use((w) => w.watchCommit({ ...input, commit: "abc0070", note: "then hand off" })); + h.runs.set("70", { ...h.runs.get("70"), status: "completed", conclusion: "skipped" }); + yield* h.use((w) => w.sweep); + expect(h.delivered).toHaveLength(1); + expect(h.delivered[0]!.text).toContain( + "WORKFLOW RUNS finished for commit abc0070000: 1 skipped or cancelled, none passed or failed.", + ); + expect(h.delivered[0]!.text).toContain("Your note: then hand off"); + + // Said once; the watch then ends quietly. + yield* h.use((w) => w.sweep); + yield* TestClock.adjust("21 minutes"); + yield* h.use((w) => w.sweep); + expect(h.delivered).toHaveLength(1); + expect(JSON.parse(h.meta.get("workflow-commit-watches")!)).toEqual([]); + }), + ); + it.effect("does not watch a run twice when its commit is already watched", () => Effect.gen(function* () { const h = harness(); diff --git a/apps/server/src/t4/T4RunWatch.ts b/apps/server/src/t4/T4RunWatch.ts index 3f78b3f8d3757..08eb417ce1c35 100644 --- a/apps/server/src/t4/T4RunWatch.ts +++ b/apps/server/src/t4/T4RunWatch.ts @@ -65,8 +65,10 @@ const CommitWatch = Schema.Struct({ * "still running" (2026-10-30). */ held: Schema.optional(Schema.Array(Schema.String)), - /** Skipped or cancelled runs folded into the next message as a count. */ + /** Skipped runs (and cancelled ones a newer push replaced) folded into the next message as a count. */ quietRuns: Schema.optional(Schema.Number), + /** Whether this watch has posted to its thread yet. */ + posted: Schema.optional(Schema.Boolean), }); type CommitWatch = typeof CommitWatch.Type; const CommitWatches = Schema.fromJsonString(Schema.Array(CommitWatch)); @@ -112,6 +114,7 @@ export interface WorkflowRun { readonly status: string; readonly conclusion: string; readonly failedJobs: ReadonlyArray; + readonly cancelledJobs: ReadonlyArray; } const RunView = Schema.Struct({ @@ -135,7 +138,27 @@ const decodeRunView = Schema.decodeUnknownEffect(Schema.fromJsonString(RunView)) const FAILED = new Set(["failure", "timed_out", "startup_failure", "action_required"]); /** Runs that did nothing worth reading on their own (a train decision that declined). */ -const QUIET = new Set(["skipped", "cancelled", "neutral", "stale"]); +const QUIET = new Set(["skipped", "neutral", "stale"]); +/** + * A cancelled run is a result: a job hit its time limit or someone stopped it, + * and a PR's checks are not green. It counted as quiet until AI-148: usa-rs + * USA-3143's only run for its push ended cancelled (a test shard at its + * 25-minute limit), the watch posted nothing and ended, and the thread slept + * 7 hours (2026-11-24). Quiet only when the thread has since watched a newer + * commit in the same checkout - the cancel of a run a new push replaced. + */ +const CANCELLED_NOTE = + "A cancelled job usually hit its time limit or was stopped by hand (`ci logs` shows which). If these are a PR's checks, they are not green until it passes."; + +/** " (failed: a; cancelled: b)" for a run's jobs, or "". */ +const jobsDetail = (run: WorkflowRun | null) => { + if (run === null) return ""; + const parts = [ + run.failedJobs.length > 0 ? `failed: ${run.failedJobs.join(", ")}` : null, + run.cancelledJobs.length > 0 ? `cancelled: ${run.cancelledJobs.join(", ")}` : null, + ].filter((part) => part !== null); + return parts.length > 0 ? ` (${parts.join("; ")})` : ""; +}; /** The message a finished run posts into its thread. */ const finishedMessage = (watch: Watch, run: WorkflowRun) => @@ -144,6 +167,8 @@ const finishedMessage = (watch: Watch, run: WorkflowRun) => run.title ? `"${run.title}"` : null, run.url || null, run.failedJobs.length > 0 ? `Failed jobs: ${run.failedJobs.join(", ")}.` : null, + run.cancelledJobs.length > 0 ? `Cancelled jobs: ${run.cancelledJobs.join(", ")}.` : null, + run.conclusion === "cancelled" ? CANCELLED_NOTE : null, watch.note ? `Your note: ${watch.note}` : null, ] .filter((line) => line !== null) @@ -259,6 +284,9 @@ const make = Effect.gen(function* () { failedJobs: (run.jobs ?? []) .filter((job) => FAILED.has(job.conclusion ?? "")) .map((job) => job.name ?? "job"), + cancelledJobs: (run.jobs ?? []) + .filter((job) => job.conclusion === "cancelled") + .map((job) => job.name ?? "job"), } satisfies WorkflowRun; }); @@ -396,10 +424,20 @@ const make = Effect.gen(function* () { const held = [...(w.held ?? [])]; let quietRuns = w.quietRuns ?? 0; let failed = false; + let cancelled = false; + // A newer commit this thread watches in the same checkout replaced this one. + const replaced = list.some( + (other) => + other.threadId === w.threadId && + other.commit !== w.commit && + other.since > w.since && + other.repository === w.repository && + other.cwd === w.cwd, + ); if (finished.length > 0) lastActivity = now; for (const run of finished) { const conclusion = run.conclusion || "completed"; - if (QUIET.has(conclusion)) { + if (QUIET.has(conclusion) || (conclusion === "cancelled" && replaced)) { quietRuns += 1; continue; } @@ -409,22 +447,30 @@ const make = Effect.gen(function* () { : yield* view(String(run.databaseId), w.repository, w.cwd).pipe( Effect.orElseSucceed(() => null), ); - if (conclusion !== "success") failed = true; + // A cancelled run posts at once when a job in it failed (a PR gate); + // otherwise it waits like a success for the stage to end. + if (conclusion === "cancelled") { + cancelled = true; + if (detail !== null && detail.failedJobs.length > 0) failed = true; + } else if (conclusion !== "success") failed = true; held.push( - `- ${run.displayTitle || run.name || run.workflowName} #${run.databaseId}${run.attempt !== undefined && run.attempt > 1 ? ` (re-run, attempt ${run.attempt})` : ""}: ${conclusion}${detail && detail.failedJobs.length > 0 ? ` (failed: ${detail.failedJobs.join(", ")})` : ""} ${run.url ?? ""}`.trim(), + `- ${run.displayTitle || run.name || run.workflowName} #${run.databaseId}${run.attempt !== undefined && run.attempt > 1 ? ` (re-run, attempt ${run.attempt})` : ""}: ${conclusion}${jobsDetail(detail)} ${run.url ?? ""}`.trim(), ); } const reported = [...w.reported, ...finished.map(runKey)]; const expired = now - w.since > MAX_AGE_MS; + let posted = w.posted ?? false; // Post on a failure, or once nothing for the commit is running (a // stage of the release is over); hold everything in between. if (held.length > 0 && (failed || running.length === 0 || expired)) { + posted = true; yield* post( w.threadId, [ `WORKFLOW RUNS finished for commit ${w.commit.slice(0, 10)}:`, ...held, quietRuns > 0 ? `(${quietRuns} more skipped or cancelled.)` : null, + cancelled ? CANCELLED_NOTE : null, running.length > 0 ? `Still running: ${running.map((run) => `${run.displayTitle || run.name} #${run.databaseId}`).join("; ")} - you will be woken when they finish or one fails.` : expired @@ -438,6 +484,23 @@ const make = Effect.gen(function* () { held.length = 0; quietRuns = 0; } + // Every run skipped and nothing posted yet: the thread still waits on + // this watch, so say so rather than end without a word (AI-148). Not for + // a commit a newer push replaced - that watch wakes the thread. + if (!posted && !replaced && held.length === 0 && quietRuns > 0 && running.length === 0) { + posted = true; + yield* post( + w.threadId, + [ + `WORKFLOW RUNS finished for commit ${w.commit.slice(0, 10)}: ${quietRuns} skipped or cancelled, none passed or failed.`, + "Nothing ran to completion for this commit; `ci` shows the runs. A run it triggers later is still watched for 20 minutes.", + w.note ? `Your note: ${w.note}` : null, + ] + .filter((line) => line !== null) + .join("\n"), + ); + quietRuns = 0; + } const quiet = running.length === 0 && now - lastActivity > COMMIT_QUIET_MS; if (quiet && runs.length === 0) { yield* post( @@ -457,6 +520,7 @@ const make = Effect.gen(function* () { reported, held, quietRuns, + posted, lastActivity: running.length > 0 ? now : lastActivity, }); } From 1d5438d308c746e8514574cebc4d34c34c114985 Mon Sep 17 00:00:00 2001 From: Maxwell Weinzierl Date: Tue, 24 Nov 2026 08:12:29 -0800 Subject: [PATCH 2/2] AI-148 Shorten the cancelled-run comment for the lint rule --- apps/server/src/t4/T4RunWatch.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/apps/server/src/t4/T4RunWatch.ts b/apps/server/src/t4/T4RunWatch.ts index 08eb417ce1c35..af4e8bd9073d1 100644 --- a/apps/server/src/t4/T4RunWatch.ts +++ b/apps/server/src/t4/T4RunWatch.ts @@ -140,12 +140,9 @@ const FAILED = new Set(["failure", "timed_out", "startup_failure", "action_requi /** Runs that did nothing worth reading on their own (a train decision that declined). */ const QUIET = new Set(["skipped", "neutral", "stale"]); /** - * A cancelled run is a result: a job hit its time limit or someone stopped it, - * and a PR's checks are not green. It counted as quiet until AI-148: usa-rs - * USA-3143's only run for its push ended cancelled (a test shard at its - * 25-minute limit), the watch posted nothing and ended, and the thread slept - * 7 hours (2026-11-24). Quiet only when the thread has since watched a newer - * commit in the same checkout - the cancel of a run a new push replaced. + * A cancelled run is a result, not quiet (AI-148: usa-rs USA-3143's only run + * ended cancelled, nothing was posted, the thread slept 7 hours). Quiet only + * when the thread has since watched a newer commit in the same checkout. */ const CANCELLED_NOTE = "A cancelled job usually hit its time limit or was stopped by hand (`ci logs` shows which). If these are a PR's checks, they are not green until it passes.";