Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 100 additions & 1 deletion apps/server/src/t4/T4RunWatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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" });
Expand All @@ -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();
Expand Down
71 changes: 66 additions & 5 deletions apps/server/src/t4/T4RunWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -112,6 +114,7 @@ export interface WorkflowRun {
readonly status: string;
readonly conclusion: string;
readonly failedJobs: ReadonlyArray<string>;
readonly cancelledJobs: ReadonlyArray<string>;
}

const RunView = Schema.Struct({
Expand All @@ -135,7 +138,24 @@ 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, 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.";

/** " (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) =>
Expand All @@ -144,6 +164,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)
Expand Down Expand Up @@ -259,6 +281,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;
});

Expand Down Expand Up @@ -396,10 +421,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;
}
Expand All @@ -409,22 +444,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
Expand All @@ -438,6 +481,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(
Expand All @@ -457,6 +517,7 @@ const make = Effect.gen(function* () {
reported,
held,
quietRuns,
posted,
lastActivity: running.length > 0 ? now : lastActivity,
});
}
Expand Down
Loading