Public: anyone can read this; the crew can change it.
A weekly duty lists branches with no open pull and no push for 14 days #14164
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14141-a-weekly-duty-lists-branches-with-no-ope → main 7 commits fork 1 thread · 1 unresolved
5 files
changed +152
−5
src/maintain/duties/stale_branches.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** | |
| 2 | * Stale branches: per repository, the branches that have no open pull and whose tip commit is | |
| 3 | * over 14 days old, as one finding (the first ten of each repository, with its count). It reads | |
| 4 | * only and never deletes. Never listed: the default branch, a branch an open pull uses as its head. | |
| 5 | * "No push for 14 days" is read as the tip commit's committer date, the last time the branch moved. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../../app.ts"; | |
| 9 | import { getRepoById } from "../../core/repos.ts"; | |
| 10 | import { isBranchName } from "../../git/branches.ts"; | |
| 11 | import { repoDir } from "../../git/repos.ts"; | |
| 12 | import { runGit } from "../../git/env.ts"; | |
| 13 | import type { Duty } from "../duty.ts"; | |
| 14 | ||
| 15 | export const STALE_BRANCH_DAYS = 14; | |
| 16 | export const STALE_BRANCH_LIST = 10; | |
| 17 | const DAY_MS = 86_400_000; | |
| 18 | ||
| 19 | export interface StaleRepo { | |
| 20 | readonly repo: string; | |
| 21 | readonly count: number; | |
| 22 | /** The first ten, oldest first. */ | |
| 23 | readonly branches: readonly string[]; | |
| 24 | } | |
| 25 | ||
| 26 | interface Row { repo_id: string; repo: string; def: string } | |
| 27 | ||
| 28 | const REPOS_SQL = `SELECT r.id AS repo_id, o.username || '/' || r.name AS repo, r.default_branch AS def | |
| 29 | FROM repo r JOIN user o ON o.id = r.owner_id ORDER BY repo`; | |
| 30 | const OPEN_SQL = `SELECT p.head_branch AS b FROM pull p JOIN issue i ON i.id = p.issue_id | |
| 31 | WHERE i.state = 'open' AND i.kind = 'pull' AND COALESCE(p.head_repo_id, i.repo_id) = ?1`; | |
| 32 | ||
| 33 | export async function staleBranches(app: App, now: Date, days = STALE_BRANCH_DAYS): Promise<StaleRepo[]> { | |
| 34 | const cutoff = Math.floor((now.getTime() - days * DAY_MS) / 1000); | |
| 35 | const out: StaleRepo[] = []; | |
| 36 | for (const row of app.db.query<Row, []>(REPOS_SQL).all()) { | |
| 37 | const repo = getRepoById(app.db, row.repo_id); | |
| 38 | if (repo === undefined) continue; | |
| 39 | const listed = await runGit( | |
| 40 | ["for-each-ref", "--sort=committerdate", "--format=%(refname:strip=2)%00%(committerdate:unix)", "--", "refs/heads/"], | |
| 41 | { cwd: repoDir(app.reposRoot, repo.ownerName, repo.name) }, | |
| 42 | ); | |
| 43 | if (!listed.ok || listed.value === "") continue; | |
| 44 | const open = new Set(app.db.query<{ b: string }, [string]>(OPEN_SQL).all(row.repo_id).map((r) => r.b)); | |
| 45 | const stale: string[] = []; | |
| 46 | for (const line of listed.value.split("\n")) { | |
| 47 | const [name, when] = line.split("\0"); | |
| 48 | if (!name || !when || !isBranchName(name) || name === row.def || open.has(name)) continue; | |
| 49 | if (Number(when) < cutoff) stale.push(name); | |
| 50 | } | |
| 51 | if (stale.length > 0) out.push({ repo: row.repo, count: stale.length, branches: stale.slice(0, STALE_BRANCH_LIST) }); | |
| 52 | } | |
| 53 | return out; | |
| 54 | } | |
| 55 | ||
| 56 | export const duty: Duty = { | |
| 57 | name: "stale-branches", | |
| 58 | intervalMs: 7 * 86_400_000, | |
| 59 | budget: { runsPerDay: 2, dollarsPerDay: 1, reason: "weekly is one a week; the rest is room for a catch-up, and it spends no money" }, | |
| 60 | async run({ now, app }) { | |
| 61 | if (app === undefined) return { findings: [], actions: [] }; | |
| 62 | const found = await staleBranches(app, now); | |
| 63 | if (found.length === 0) return { findings: [], actions: [] }; | |
| 64 | const total = found.reduce((n, r) => n + r.count, 0); | |
| 65 | const lines = found.map((r) => `${r.repo}: ${r.count} (${r.branches.join(", ")}${r.count > r.branches.length ? ", …" : ""})`); | |
| 66 | return { | |
| 67 | findings: [ | |
| 68 | { | |
| 69 | summary: `${total} branch${total === 1 ? "" : "es"} with no open pull and no push for ${STALE_BRANCH_DAYS} days, in ${found.length} repositor${found.length === 1 ? "y" : "ies"} — ${lines.join("; ")}`, | |
| 70 | probe: { kind: "measurement", what: `branches with no open pull whose tip is over ${STALE_BRANCH_DAYS} days old`, number: total, at: now.toISOString() }, | |
| 71 | }, | |
| 72 | ], | |
| 73 | actions: [], | |
| 74 | }; | |
| 75 | }, | |
| 76 | }; |
src/maintain/registry.ts +2 −1
| @@ -10,6 +10,7 @@ import { duty as backupGrowth } from "./duties/backup_growth.ts"; | ||
| 10 | 10 | import { duty as v1Proven } from "./duties/v1_proven.ts"; |
| 11 | 11 | import { duty as backupAge } from "./duties/backup_age.ts"; |
| 12 | 12 | import { duty as mergedBranches } from "./duties/merged_branches.ts"; |
| 13 | import { duty as staleBranches } from "./duties/stale_branches.ts"; | |
| 13 | 14 | import { duty as previewDirs } from "./previews.ts"; |
| 14 | 15 | import { duty as diskFree } from "./duties/disk_free.ts"; |
| 15 | 16 | import { duty as staleIssues } from "./duties/stale_issues.ts"; |
| @@ -22,7 +23,7 @@ import { duty as untestedSources } from "./duties/untested_sources.ts"; | ||
| 22 | 23 | import { duty as chapterStall } from "./duties/chapter_stall.ts"; |
| 23 | 24 | import { dutyIdle } from "./duties/duty_idle.ts"; |
| 24 | 25 | |
| 25 | const others: readonly Duty[] = [pullsWaiting, ruleWatcher, efficiencyWatcher, flowKeeper, visitorRetention, backupGrowth, backupAge, v1Proven, diskFree, mergedBranches, previewDirs, staleIssues, waitSplit, slowTests, flakyTests, redApps, chapterStall, deadDocPaths, untestedSources]; | |
| 26 | const others: readonly Duty[] = [pullsWaiting, ruleWatcher, efficiencyWatcher, flowKeeper, visitorRetention, backupGrowth, backupAge, v1Proven, diskFree, mergedBranches, staleBranches, previewDirs, staleIssues, waitSplit, slowTests, flakyTests, redApps, chapterStall, deadDocPaths, untestedSources]; | |
| 26 | 27 | |
| 27 | 28 | /** The duties plus the one that watches them for silence. */ |
| 28 | 29 | export const duties: readonly Duty[] = [...others, dutyIdle(others)]; |
src/perf/page_budget.ts +2 −2
| @@ -44,9 +44,9 @@ export const PAGE_BUDGETS: readonly PageBudget[] = [ | ||
| 44 | 44 | // warm (53, with main's duties) plus nine: each of the eight duties reads its own state and |
| 45 | 45 | // runs, so the count moves with duty state and background ticks; an N+1 over the duties would |
| 46 | 46 | // still show. Four more duties (flaky-tests #13937, red apps #13938, chapter stall #14057, |
| 47 | // dead-doc-paths #13977, untested-sources #14125, slow-tests #13966) each | |
| 47 | // dead-doc-paths #13977, untested-sources #14125, slow-tests #13966, stale-branches #14141) each | |
| 48 | 48 | // read their own state. One query reads every dismissal (#13967). |
| 49 | { page: "maintenance", pattern: "/maintenance", medianMs: 50, statements: 78 }, | |
| 49 | { page: "maintenance", pattern: "/maintenance", medianMs: 50, statements: 81 }, | |
| 50 | 50 | // A stranger's way to a forge (#11410): walked signed out. `/claim` is |
| 51 | 51 | // measured on a spawned forge with a live link, as the child's owner sees it. |
| 52 | 52 | { page: "pricing", pattern: "/pricing", medianMs: 50, statements: 10, signedOut: true }, |
test/maint-stale-branches.test.ts added +70
| @@ -0,0 +1,70 @@ | ||
| 1 | /** | |
| 2 | * Stale branches: fresh, stale and pulled branches on one fixture; the duty files one finding and | |
| 3 | * deletes nothing. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createBranch } from "../src/actions/branches.ts"; | |
| 10 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createRepo, type Repo } from "../src/core/repos.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { resolveOid } from "../src/git/merge.ts"; | |
| 15 | import { repoDir } from "../src/git/repos.ts"; | |
| 16 | import { id } from "../src/kernel/id.ts"; | |
| 17 | import { duty, staleBranches } from "../src/maintain/duties/stale_branches.ts"; | |
| 18 | import { duties } from "../src/maintain/registry.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | let dir = "", app: App, repo: Repo; | |
| 23 | const must = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => { | |
| 24 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 25 | return r.value; | |
| 26 | }; | |
| 27 | const day = (d: number): string => new Date(Date.now() + d * 86_400_000).toISOString(); | |
| 28 | const NOW = new Date(Date.now() + 30 * 86_400_000); // everything made now is 30 days old, except what we move later | |
| 29 | ||
| 30 | beforeAll(async () => { | |
| 31 | dir = await mkdtemp(join(tmpdir(), "forge-stale-branches-")); | |
| 32 | app = createApp({ dataDir: dir, port: 0 }); | |
| 33 | const admin = must(await createUser(app.db, { username: "boss", email: "[email protected]", password: "a-good-passphrase", origin: "seed", trusted: true, isAdmin: true })); | |
| 34 | repo = must(await createRepo(app.db, app.reposRoot, { ownerId: admin.id, name: "r", visibility: "public", description: "r" })); | |
| 35 | must(await commitToBranch(app, admin, repo, { message: "seed", files: [{ path: "a.txt", content: "a\n" }] })); | |
| 36 | for (const name of ["stale-a", "stale-b", "pulled"]) must(await createBranch(app, admin, repo, { name })); | |
| 37 | const n = 1, issue = id(); | |
| 38 | app.db.query("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at) VALUES (?1, ?2, ?3, 'pull', 'P', '', ?4, 'open', ?5, ?5)").run(issue, repo.id, n, admin.id, day(0)); | |
| 39 | const head = (await resolveOid(repoDir(app.reposRoot, repo.ownerName, repo.name), "refs/heads/pulled"))!; | |
| 40 | app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, merged_at) VALUES (?1, 'main', 'pulled', ?2, ?2, ?3, NULL)").run(issue, head, repo.id); | |
| 41 | }); | |
| 42 | afterAll(async () => { | |
| 43 | app.close(); | |
| 44 | await rm(dir, { recursive: true, force: true }); | |
| 45 | }); | |
| 46 | ||
| 47 | test("fresh branches are not listed", async () => { | |
| 48 | expect(await staleBranches(app, new Date())).toEqual([]); | |
| 49 | }); | |
| 50 | ||
| 51 | test("lists stale branches without an open pull, never the default or a pulled one", async () => { | |
| 52 | const found = await staleBranches(app, NOW); | |
| 53 | expect(found).toHaveLength(1); | |
| 54 | expect(found[0]!.count).toBe(2); | |
| 55 | expect([...found[0]!.branches].sort()).toEqual(["stale-a", "stale-b"]); | |
| 56 | }); | |
| 57 | ||
| 58 | test("the duty files one finding with a count, is registered, and deletes nothing", async () => { | |
| 59 | expect(duties).toContain(duty); | |
| 60 | const r = await duty.run({ db: app.db, bus: app.bus, now: NOW, app }); | |
| 61 | expect(r.findings).toHaveLength(1); | |
| 62 | expect(r.findings[0]!.probe?.number).toBe(2); | |
| 63 | expect(r.actions).toEqual([]); | |
| 64 | expect(await resolveOid(repoDir(app.reposRoot, repo.ownerName, repo.name), "refs/heads/stale-a")).toBeDefined(); | |
| 65 | }); | |
| 66 | ||
| 67 | test("it files nothing when none is stale", async () => { | |
| 68 | const r = await duty.run({ db: app.db, bus: app.bus, now: new Date(), app }); | |
| 69 | expect(r.findings).toEqual([]); | |
| 70 | }); |
test/pages-budget.test.ts +2 −2
| @@ -56,8 +56,8 @@ const HEAVIER: Record<string, { statements?: number; git?: number }> = { | ||
| 56 | 56 | "/api/v1/asks": { statements: 61 }, // +1: the stopped apps someone asked for, one read of the viewer's archived repositories (#12277) |
| 57 | 57 | "/board": { statements: 72 }, // +1: the forge-copy read for the tell box (#12037); +1: the vitals read the database's growth (#12025); +6: the money line (#510) — one settings read, then the month's and the 30 days' meter, storage and ledger reads; constant in the number of apps |
| 58 | 58 | "/settings/capacity": { statements: 70 }, |
| 59 | "/maintenance": { statements: 78 }, // +5: the slow-tests duty (#13966) reads its own state, runs and is listed; +3: untested-sources (#14125) reads its own state and runs, and the dismissal query (#13967) is one more; +4: the flaky-tests (#13937), red apps (#13938) and chapter stall (#14057) and dead-doc-paths (#13977) duties each read their own state and run | |
| 60 | "/api/v1/maintenance/duties": { statements: 63 }, // +3: the slow-tests duty (#13966) reads its own state and runs, as on /maintenance | |
| 59 | "/maintenance": { statements: 81 }, // +5: slow-tests (#13966); +3: untested-sources (#14125) and the dismissal query (#13967); +4: flaky-tests (#13937), red apps (#13938), chapter stall (#14057), dead-doc-paths (#13977); +3: stale-branches (#14141) — each reads its own state and runs | |
| 60 | "/api/v1/maintenance/duties": { statements: 66 }, // the same duties as JSON; slow-tests (#13966) and stale-branches (#14141) each add 3 | |
| 61 | 61 | }; |
| 62 | 62 | |
| 63 | 63 | /** Patterns with no fixture today: lower this as fixtures are added, never raise it silently. */ |
new above
offline
A failed for-each-ref is skipped silently; a broken repository then looks like a clean one. Count unreadable repositories and mention them in the finding.