Public: anyone can read this; the crew can change it.
A maintenance duty files one issue naming open issues untouched for 60 days with no pull and no blocker #13617
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13592-a-maintenance-duty-files-one-issue-namin → main 1 commit fork
4 files
changed +167
−1
docs/features/maintenance.md +9
| @@ -91,3 +91,12 @@ Test: `test/standby-restore.test.ts`. | ||
| 91 | 91 | - The page is not pushed to Cloudflare automatically; `standby.ts` is started by hand (the forge only checks it). |
| 92 | 92 | - The admin is told on the page, event and status, and by the notification address they saved (`/account`); no mail. |
| 93 | 93 | - Where the kernel gives no boot id, any restart ends maintenance. |
| 94 | ||
| 95 | ## Stale issues (#13592) | |
| 96 | ||
| 97 | The `stale-issues` duty runs once a day and files one issue, "Open issues untouched for 60 days with no | |
| 98 | pull and no blocker", naming up to 20 open issues of the forge's own repository whose last comment or | |
| 99 | edit is over 60 days old, that no open pull says it closes (`Closes #n`) and that wait for no open | |
| 100 | blocker (oldest first; the rest are counted). While that report is open, later nights file none; closing | |
| 101 | it lets the next night file a fresh list. The duty never comments on, labels or closes an issue it names. | |
| 102 | It shows as the `stale-issues` row on `/maintenance`. Test: `test/maintain-stale-issues.test.ts`. |
src/maintain/duties/stale_issues.ts added +90
| @@ -0,0 +1,90 @@ | ||
| 1 | /** | |
| 2 | * Stale issues (#13592): open issues untouched for 60 days that no open pull says it closes and | |
| 3 | * that wait for no open blocker, named in one issue, 20 at most. "Touched" is a comment or an edit | |
| 4 | * (both bump `issue.updated_at`). One open report stands for them all (`fileIssueOnce`, by title), | |
| 5 | * and the duty only names an issue: it never comments on, labels or closes one. | |
| 6 | * Reads the open issues of the forge's own repository by `issue_repo_kind_state_number`; ordering | |
| 7 | * is done after the read, so there is no SCAN and no TEMP B-TREE. | |
| 8 | */ | |
| 9 | ||
| 10 | import type { Database } from "bun:sqlite"; | |
| 11 | import { fileIssueOnce } from "../../checks/flake.ts"; | |
| 12 | import { getRepo } from "../../core/repos.ts"; | |
| 13 | import { closesFromBody } from "../../core/pulls.ts"; | |
| 14 | import { ownRepoPath } from "../../kernel/own_repo.ts"; | |
| 15 | import type { Duty } from "../duty.ts"; | |
| 16 | ||
| 17 | export const STALE_ISSUE_DAYS = 60; | |
| 18 | /** The most names one report carries; the rest are counted, not listed. */ | |
| 19 | export const STALE_ISSUE_CAP = 20; | |
| 20 | export const TITLE = "Open issues untouched for 60 days with no pull and no blocker"; | |
| 21 | const DAY_MS = 86_400_000; | |
| 22 | ||
| 23 | export interface StaleIssue { | |
| 24 | readonly number: number; | |
| 25 | readonly title: string; | |
| 26 | readonly ageDays: number; | |
| 27 | } | |
| 28 | ||
| 29 | interface Row { number: number; title: string; updated_at: string } | |
| 30 | ||
| 31 | /** | |
| 32 | * Open issues of `repoId` last touched before `now - days`, with no open pull whose description | |
| 33 | * closes them and no open blocker, oldest touch first (ties by number). The report itself is never | |
| 34 | * listed. | |
| 35 | */ | |
| 36 | export function staleIssues(db: Database, repoId: string, now: Date, days = STALE_ISSUE_DAYS): StaleIssue[] { | |
| 37 | const cutoff = new Date(now.getTime() - days * DAY_MS).toISOString(); | |
| 38 | const rows = db | |
| 39 | .query<Row, [string, string]>( | |
| 40 | `SELECT number, title, updated_at FROM issue | |
| 41 | WHERE repo_id = ?1 AND kind = 'issue' AND state = 'open' AND updated_at < ?2 | |
| 42 | AND NOT EXISTS (SELECT 1 FROM issue_dependency d JOIN issue b ON b.id = d.blocked_by_id | |
| 43 | WHERE d.issue_id = issue.id AND b.state = 'open')`, | |
| 44 | ) | |
| 45 | .all(repoId, cutoff) | |
| 46 | .filter((r) => r.title !== TITLE); | |
| 47 | if (rows.length === 0) return []; | |
| 48 | const claimed = new Set<number>(); | |
| 49 | for (const p of db.query<{ body: string | null }, [string]>("SELECT body FROM issue WHERE repo_id = ?1 AND kind = 'pull' AND state = 'open'").all(repoId)) | |
| 50 | for (const n of closesFromBody(p.body ?? "")) claimed.add(n); | |
| 51 | return rows | |
| 52 | .filter((r) => !claimed.has(r.number)) | |
| 53 | .sort((a, b) => a.updated_at.localeCompare(b.updated_at) || a.number - b.number) | |
| 54 | .map((r) => ({ number: r.number, title: r.title, ageDays: Math.floor((now.getTime() - Date.parse(r.updated_at)) / DAY_MS) })); | |
| 55 | } | |
| 56 | ||
| 57 | /** One line of the report: a number and a title kept to one line, never markup that renders. */ | |
| 58 | const lineOf = (i: StaleIssue): string => `- #${i.number} ${i.title.replace(/\s+/g, " ").replace(/[<>`]/g, "").slice(0, 120)} (${i.ageDays} days)`; | |
| 59 | ||
| 60 | export function bodyOf(found: readonly StaleIssue[]): string { | |
| 61 | const shown = found.slice(0, STALE_ISSUE_CAP); | |
| 62 | const more = found.length - shown.length; | |
| 63 | return ( | |
| 64 | `_Filed by the stale issues duty from the issue table, not written by a person._\n\n` + | |
| 65 | `${found.length} open issue${found.length === 1 ? " has" : "s have"} had no comment or edit for ${STALE_ISSUE_DAYS} days, ` + | |
| 66 | `no open pull that closes ${found.length === 1 ? "it" : "them"} and no open blocker. Still wanted? Comment on it, link a pull, or close it.\n\n` + | |
| 67 | `${shown.map(lineOf).join("\n")}${more > 0 ? `\n\n…and ${more} more, oldest first above.` : ""}\n\n` + | |
| 68 | `Nothing was changed on these issues. One report stays open at a time; close it and the next night files a fresh list.` | |
| 69 | ); | |
| 70 | } | |
| 71 | ||
| 72 | export const duty: Duty = { | |
| 73 | name: "stale-issues", | |
| 74 | intervalMs: 24 * 3_600_000, | |
| 75 | budget: { runsPerDay: 3, dollarsPerDay: 1, reason: "once a day; the rest is room for a catch-up, and it spends no money" }, | |
| 76 | run({ db, bus, now, app }) { | |
| 77 | if (app === undefined) return { findings: [], actions: [] }; | |
| 78 | const [owner = "", name = ""] = ownRepoPath().split("/"); | |
| 79 | const repo = getRepo(db, owner, name); | |
| 80 | if (repo === undefined) return { findings: [], actions: [] }; | |
| 81 | const found = staleIssues(db, repo.id, now); | |
| 82 | if (found.length === 0) return { findings: [], actions: [] }; | |
| 83 | const finding = { | |
| 84 | summary: `${found.length} open issue${found.length === 1 ? "" : "s"} untouched for ${STALE_ISSUE_DAYS} days with no pull and no blocker: ${found.slice(0, 5).map((i) => `#${i.number}`).join(", ")}${found.length > 5 ? ", …" : ""}`, | |
| 85 | probe: { kind: "measurement" as const, what: "open issues untouched for 60 days with no open pull closing them and no open blocker", number: found.length, at: now.toISOString() }, | |
| 86 | }; | |
| 87 | const filed = fileIssueOnce(db, bus, repo, { title: TITLE, body: bodyOf(found) }); | |
| 88 | return { findings: [finding], actions: filed.opened ? [`filed stale issues report #${filed.number} naming ${Math.min(found.length, STALE_ISSUE_CAP)}`] : [] }; | |
| 89 | }, | |
| 90 | }; |
src/maintain/registry.ts +2 −1
| @@ -11,9 +11,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 | 13 | import { duty as diskFree } from "./duties/disk_free.ts"; |
| 14 | import { duty as staleIssues } from "./duties/stale_issues.ts"; | |
| 14 | 15 | import { dutyIdle } from "./duties/duty_idle.ts"; |
| 15 | 16 | |
| 16 | const others: readonly Duty[] = [pullsWaiting, ruleWatcher, efficiencyWatcher, flowKeeper, visitorRetention, backupGrowth, backupAge, v1Proven, diskFree, mergedBranches]; | |
| 17 | const others: readonly Duty[] = [pullsWaiting, ruleWatcher, efficiencyWatcher, flowKeeper, visitorRetention, backupGrowth, backupAge, v1Proven, diskFree, mergedBranches, staleIssues]; | |
| 17 | 18 | |
| 18 | 19 | /** The duties plus the one that watches them for silence. */ |
| 19 | 20 | export const duties: readonly Duty[] = [...others, dutyIdle(others)]; |
test/maintain-stale-issues.test.ts added +66
| @@ -0,0 +1,66 @@ | ||
| 1 | /** Stale issues (#13592): an aged fixture; one report, 20 names, never twice, nothing touched. */ | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { rulesWorld } from "./helpers/rules-app.ts"; | |
| 4 | import { duty, STALE_ISSUE_CAP, TITLE } from "../src/maintain/duties/stale_issues.ts"; | |
| 5 | import { duties } from "../src/maintain/registry.ts"; | |
| 6 | import { id } from "../src/kernel/id.ts"; | |
| 7 | ||
| 8 | process.env.FORGE_SENTINEL_REPO = "alice/rules"; | |
| 9 | const w = rulesWorld(); | |
| 10 | const NOW = new Date("2026-11-15T12:00:00Z"); | |
| 11 | const ago = (d: number): string => new Date(NOW.getTime() - d * 86_400_000).toISOString(); | |
| 12 | let seq = 100; | |
| 13 | function add(kind: "issue" | "pull", days: number, body = "", state = "open"): { id: string; number: number } { | |
| 14 | const n = ++seq, i = id(); | |
| 15 | w.app.db.query("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?9)").run(i, w.repoId, n, kind, `${kind} ${n}`, body, w.ownerId, state, ago(days)); | |
| 16 | if (kind === "pull") w.app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', ?2, ?3, ?3)").run(i, `b/${n}`, "0".repeat(40)); | |
| 17 | return { id: i, number: n }; | |
| 18 | } | |
| 19 | const run = async () => await duty.run({ db: w.app.db, bus: w.app.bus, app: w.app, now: NOW }); | |
| 20 | const report = () => w.app.db.query<{ body: string; number: number }, [string]>("SELECT body, number FROM issue WHERE title = ?1 AND state = 'open'").all(TITLE); | |
| 21 | ||
| 22 | test("fresh and aged-with-a-pull-or-blocker issues are left out; the plain aged one is named once", async () => { | |
| 23 | add("issue", 59); | |
| 24 | const withPull = add("issue", 90); | |
| 25 | add("pull", 1, `Closes #${withPull.number}`); | |
| 26 | const blocker = add("issue", 1); | |
| 27 | const blocked = add("issue", 90); | |
| 28 | w.app.db.query("INSERT INTO issue_dependency (issue_id, blocked_by_id, actor_id) VALUES (?1, ?2, ?3)").run(blocked.id, blocker.id, w.ownerId); | |
| 29 | expect(await run()).toMatchObject({ actions: [] }); // nothing stale yet | |
| 30 | const stale = add("issue", 61); | |
| 31 | const out = await run(); | |
| 32 | expect(out.findings[0]!.probe!.number).toBe(1); | |
| 33 | expect(out.actions).toHaveLength(1); | |
| 34 | const [r] = report(); | |
| 35 | expect(r!.body).toContain(`#${stale.number} `); | |
| 36 | expect(r!.body).not.toContain(`#${withPull.number} `); | |
| 37 | expect(r!.body).not.toContain(`#${blocked.number} `); | |
| 38 | expect((await run()).actions).toEqual([]); // never twice while one is open | |
| 39 | expect(report()).toHaveLength(1); | |
| 40 | // a closed blocker no longer holds an issue back | |
| 41 | w.app.db.query("UPDATE issue SET state = 'closed' WHERE id = ?1").run(blocker.id); | |
| 42 | w.app.db.query("UPDATE issue SET state = 'closed' WHERE title = ?1").run(TITLE); | |
| 43 | await run(); | |
| 44 | expect(report()[0]!.body).toContain(`#${blocked.number} `); | |
| 45 | }); | |
| 46 | ||
| 47 | test("it names an issue and touches nothing: no comment, same state and time", async () => { | |
| 48 | const before = w.app.db.query("SELECT COUNT(*) AS n FROM comment").get(); | |
| 49 | const row = w.app.db.query("SELECT state, updated_at FROM issue WHERE number = 101").get(); | |
| 50 | await run(); | |
| 51 | expect(w.app.db.query("SELECT COUNT(*) AS n FROM comment").get()).toEqual(before); | |
| 52 | expect(w.app.db.query("SELECT state, updated_at FROM issue WHERE number = 101").get()).toEqual(row); | |
| 53 | }); | |
| 54 | ||
| 55 | test("at most 20 names, the rest counted; a closed report lets the next night file a new one", async () => { | |
| 56 | w.app.db.query("UPDATE issue SET state = 'closed' WHERE title = ?1").run(TITLE); | |
| 57 | for (let i = 0; i < 25; i++) add("issue", 70); | |
| 58 | expect((await run()).actions).toHaveLength(1); | |
| 59 | const [r] = report(); | |
| 60 | expect(r!.body.split("\n").filter((l) => l.startsWith("- #"))).toHaveLength(STALE_ISSUE_CAP); | |
| 61 | expect(r!.body).toMatch(/\.\.\.|…and \d+ more/); | |
| 62 | }); | |
| 63 | ||
| 64 | test("the duty is in the registry", () => { | |
| 65 | expect(duties.map((d) => d.name)).toContain("stale-issues"); | |
| 66 | }); |
new above
offline