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
8 changes: 5 additions & 3 deletions .github/workflows/ci-watch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ name: CI watch
# After every run of CI and Desktop: a job that failed and then passed on the same commit opens or
# updates a `flaky-test` issue naming each test that failed. After a run on main or in the merge
# queue, a job or a run whose median over its last runs there is over its budget in
# .github/ci-budgets.json opens or keeps current a `ci-budget` issue and fails this job; after a run
# on a pull request, so does starting more jobs than its workflow files allow (scripts/ci-watch.ts,
# scripts/ci-budget.ts). It reads runs, jobs and logs through the API, and a pull request's own
# .github/ci-budgets.json opens or keeps current a `ci-budget` issue and a warning, and stays green
# so main is not marked red for Staging; after a run on a pull request, starting more jobs than its
# workflow files allow opens one and fails this job (scripts/ci-watch.ts, scripts/ci-budget.ts). A
# branch cut before the budget's limits is not counted. It reads runs, jobs and logs through the
# API, and a pull request's own
# .github folder as data to count its jobs; nothing here runs a pull request's code, and runs from
# forks are left alone.
on:
Expand Down
50 changes: 50 additions & 0 deletions scripts/ci-budget.fixtures/budgets-before-limits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"CI": {
"run": 15,
"jobs": {
"changes": 2,
"format, lint, types": 3,
"API spec": 3,
"native types": 3,
"app (real browser)": 8,
"app (real browser, Chromium) (shard N of N)": 15,
"app (real browser, WebKit) (shard N of N)": 15,
"app (real browser, desktop setup window)": 12,
"tests (shard N of N)": 7,
"tests": 2,
"tests (changed files)": 12,
"startup (macos-latest)": 4,
"startup (windows-latest)": 4,
"typescript langgraph engine": 3,
"python harness (agent-crewai)": 5,
"python harness (agent-claude-sdk)": 5,
"python harness (agent-openai-agents)": 5,
"python harness (agent-langgraph-agui)": 5,
"python harness (agent-llamaindex)": 5,
"python harness (agent-agno)": 5,
"python harness (agent-strands)": 5,
"python harness (agent-pydantic-ai)": 5,
"python harness (agent-adk)": 5,
"python harness (agent-antigravity)": 5,
"python harness (agent-langroid)": 5,
"python harness (agent-ag2)": 5,
"python harness (agent-microsoft)": 5,
"compose config": 2,
"build": 3,
"component list": 1,
"component dockerfile (*)": 10,
"release note": 1,
"verify": 1
}
},
"Desktop": {
"run": 15,
"jobs": {
"changes": 2,
"core": 4,
"live (images)": 8,
"live (engines)": 8,
"app / app (*)": 15
}
}
}
40 changes: 37 additions & 3 deletions scripts/ci-watch-requests.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { afterAll, describe, expect, test } from "bun:test";
import {
chmodSync,
cpSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
Expand Down Expand Up @@ -51,6 +53,7 @@ type Issue = {
function watch(
watched: Recorded,
world: { issues?: Issue[]; fail?: boolean; earlier?: Recorded[] } = {},
args: string[] = [],
) {
const name = `${watched.run.id}-${Math.random().toString(36).slice(2)}`;
const worldFile = join(dir, `${name}.world.json`);
Expand All @@ -66,7 +69,7 @@ function watch(
);
writeFileSync(log, "");
writeFileSync(event, JSON.stringify({ workflow_run: watched.run }));
const result = Bun.spawnSync(["bun", "scripts/ci-watch.ts"], {
const result = Bun.spawnSync(["bun", "scripts/ci-watch.ts", ...args], {
cwd: ROOT,
env: {
...process.env,
Expand Down Expand Up @@ -148,9 +151,12 @@ describe("a run in the merge queue", () => {
const body = update?.find((a) => a.startsWith("body=")) ?? "";
// The record now holds this run's time first.
expect(body).toContain(`"run":${newest.run.id}`);
// Over budget, its issues already open: CI watch is red, and says so without a request more.
// Over budget, its issues already open: CI watch says so without a request more, as a warning
// and the issue, and stays green. A red CI watch marks main not green, and Staging deploys only
// a green commit of main (backend's scripts/staging-green.ts reads this repository too).
expect(out).toContain("still over, #2790");
expect(exit).toBe(1);
expect(out).toContain("::warning title=CI over budget::");
expect(exit).toBe(0);
});

test("with no record yet, starts one, and closes nothing it has not measured", () => {
Expand Down Expand Up @@ -194,6 +200,34 @@ test("a pull request's run reads its own jobs and nothing more", () => {
expect(exit).toBe(0);
});

test("a pull request from a branch older than the budget's limits is not counted, and does not crash", () => {
// In openbot-backend, run 47517953953 crashed CI watch: the pull request's own
// .github/ci-budgets.json, from a branch cut before the limits (#2988 here), had none ("undefined
// is not an object (evaluating 'file.limits[key]')"). This is this repository's file as it was
// then, with this repository's workflows beside it.
const [run] = pullRequests as [Recorded];
const tree = mkdtempSync(join(dir, "old-branch-"));
mkdirSync(join(tree, ".github"), { recursive: true });
cpSync(
join(ROOT, ".github", "workflows"),
join(tree, ".github", "workflows"),
{
recursive: true,
},
);
writeFileSync(
join(tree, ".github", "ci-budgets.json"),
readFileSync(
join(import.meta.dir, "ci-budget.fixtures", "budgets-before-limits.json"),
"utf8",
),
);
const { exit, out, err } = watch(run, {}, ["--tree", tree]);
expect(err).not.toContain("TypeError");
expect(exit).toBe(0);
expect(out).toContain("predates them");
});

test("when GitHub refuses (a rate limit), CI watch warns and stops green", () => {
const { exit, out, requests } = watch(newest, { fail: true });
expect(exit).toBe(0);
Expand Down
53 changes: 40 additions & 13 deletions scripts/ci-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
* that median by nothing, a job that has crept moves it over. The last runs' times live in the
* workflow's `CI budget record` issue, which each run adds its own to, so a run reads only its own
* jobs (the Actions token has 1,000 API requests an hour for the whole repository). A job or a run
* over budget opens a `ci-budget` issue and fails this job, until it is back under, when its issue
* is closed. Should GitHub's API fail or refuse (a rate limit), this warns and stops, green. After a run on a pull request, the jobs it started are held to the most
* its workflow files say a pull request can start, which a matrix known only at run time could
* outgrow. scripts/ci-budgets.test.ts holds every job to having a budget.
* over budget opens a `ci-budget` issue and a warning, until it is back under, when its issue is
* closed. It does not fail this job: that would mark main not green, and Staging deploys only green
* main. Should GitHub's API fail or refuse (a rate limit), this warns and stops, green.
*
* After a run on a pull request, the jobs it started are held to the most its workflow files say a
* pull request can start, which a matrix known only at run time could outgrow; a branch cut before
* the budget had its `limits` is not counted. scripts/ci-budgets.test.ts holds every job to having
* a budget, and fails a pull request over the limits.
*
* Runs from forks are left alone: their logs are not ours to turn into issue titles.
*
Expand All @@ -32,6 +36,7 @@
import { appendFileSync, existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import {
type BudgetFile,
type Budgets,
budgetFor,
fold,
Expand Down Expand Up @@ -346,7 +351,7 @@ const FLAKY_BODY =
const FLAKY_JOB_BODY =
"A job that failed and then passed on the same commit, with no change to the code, and whose log names no failing test: a runner, a download or a service the job waits on. Each comment below is one time it happened, with the errors from its log.";
const BUDGET_BODY =
"A CI job, or a whole run, is over its budget in .github/ci-budgets.json: its median over its last runs on main and in the merge queue, where timings are steady, is longer than the budget. One slow run does not do that; a job that has grown slower does. CI watch fails on every such run until it is back under, and then closes this. Make it fast again, or raise the budget in a pull request that says why.";
"A CI job, or a whole run, is over its budget in .github/ci-budgets.json: its median over its last runs on main and in the merge queue, where timings are steady, is longer than the budget. One slow run does not do that; a job that has grown slower does. CI watch warns on every such run until it is back under, and then closes this. Make it fast again, or raise the budget in a pull request that says why.";
const COUNT_BODY =
"A push to a pull request started more jobs than its workflow files allow (.github/ci-budgets.json, `pull request jobs` and `matrix legs`): a matrix known only once the run starts gave it more legs than its cap. Each comment below is one run that did. Start fewer, or raise the cap in a pull request that says why.";

Expand Down Expand Up @@ -381,6 +386,16 @@ export function recordBody(
return `${RECORD_BODY}\n\n| Job | Median | Budget | Last runs, newest first |\n|---|---|---|---|\n${rows.join("\n")}\n\n${writeLedger(ledger)}`;
}

/**
* A run's own budget file, when it has the repository's `limits`: a branch cut before they existed
* (or with no budget file at all) has none, and its job count cannot be checked against them.
*/
export function budgetsWithLimits(root: string): BudgetFile | undefined {
if (!existsSync(join(root, ".github/ci-budgets.json"))) return undefined;
const file = readBudgets(root);
return file.limits && typeof file.limits === "object" ? file : undefined;
}

/** At most this many stale issues closed in one run, two requests each. */
const CLOSES_PER_RUN = 5;

Expand Down Expand Up @@ -551,7 +566,12 @@ async function main() {
say(
`${filed} (median ${m.median.toFixed(1)} minutes over ${m.samples.length} runs, budget ${m.budget})`,
);
failed = true;
// Reported, not failed: a red CI watch on main marks main not green, and Staging (and any
// other pick of green main) would skip every commit until the job is fast again, though
// every real check passed. The issue tracks it; a pull request's own budget check fails.
console.log(
`::warning title=CI over budget::${quiet(`${run.name} / ${m.job}: median ${m.median.toFixed(1)} minutes, budget ${m.budget}`, 300)}`,
);
}
// Every other open issue of this workflow's: its job is back under budget, or, once the
// record has seen enough runs, never runs where times are held (a pull request's own job).
Expand Down Expand Up @@ -595,14 +615,21 @@ async function main() {
treeArgument > 0 && existsSync(process.argv[treeArgument + 1] ?? "")
? (process.argv[treeArgument + 1] as string)
: ROOT;
const count = pullRequestJobs(
join(tree, ".github/workflows"),
repository,
readBudgets(tree),
);
const most = total(count, run.name);
const treeBudgets = budgetsWithLimits(tree);
const count = treeBudgets
? pullRequestJobs(
join(tree, ".github/workflows"),
repository,
treeBudgets,
)
: undefined;
const most = count ? total(count, run.name) : 0;
const ran = started(jobs);
if (most > 0 && ran > most) {
if (!count) {
say(
"This pull request's branch has no budget limits in its .github/ci-budgets.json (it predates them), so its job count is not checked.",
);
} else if (most > 0 && ran > most) {
say(
upsertIssue(
repository,
Expand Down
Loading