From ded4e072344aa98a67c33450ebf97b2dfde9b779 Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Sat, 21 Nov 2026 08:31:09 +0000 Subject: [PATCH] Planner: labels-apply waits while the repo's GitHub write budget is used up A labels job whose writes the pacer refuses labels nothing and was queued again on every input change. The labels lane reads the same budget the writer checks (pipelineWriteWait, the read-only half of tryBegin) and leaves out a repo whose label writes would wait past the writer's bound; the planner wakes when the window frees and the stage says until when. --- src/github/write.ts | 45 ++++++++++++++++++++----------- src/runner/autopilot.ts | 12 ++++++++- test/planner-write-budget.test.ts | 22 +++++++++++++++ 3 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 test/planner-write-budget.test.ts diff --git a/src/github/write.ts b/src/github/write.ts index 5038fd0b..1ba709e0 100644 --- a/src/github/write.ts +++ b/src/github/write.ts @@ -160,6 +160,34 @@ function paceOwner(identity: string, target: string, owner?: string): string { * window's rows via the audit_at index: fine at this volume) and inserts when under budget. Returns the row id, or * how long to wait before the window frees. */ +/** How long a write of `action` by `identity` on `owner`'s key waits for the budget now (0: it may start). Reads only. */ +function keyWait(db: DatabaseSync, action: string, identity: string, owner: string, now: number): number { + const rows = db.prepare( + `SELECT at, json_extract(details, '$.state') AS state, json_extract(details, '$.error') AS error FROM audit + WHERE at >= ? AND json_extract(details, '$.identity') = ? AND json_extract(details, '$.paceOwner') = ? ORDER BY at`, + ).all(new Date(now - 3_600_000).toISOString(), identity, owner) as { at: string; state: string | null; error: string | null }[]; + const times = rows.map((r) => Date.parse(r.at)); + const inMinute = times.filter((t) => t > now - 60_000); + const share = BULK_ACTIONS.has(action) ? BULK_SHARE : 1; + const perMinute = Math.floor(PACE_PER_MINUTE * share); + const perHour = Math.floor(PACE_PER_HOUR * share); + let wait = 0; + if (inMinute.length >= perMinute) wait = inMinute[inMinute.length - perMinute]! + 60_000 - now; + if (times.length >= perHour) wait = Math.max(wait, times[times.length - perHour]! + 3_600_000 - now); + const lastSecondary = rows.filter((r) => r.state === "failed" && SECONDARY_LIMIT.test(r.error ?? "")).map((r) => Date.parse(r.at)).at(-1); + if (lastSecondary !== undefined) wait = Math.max(wait, lastSecondary + SECONDARY_PAUSE_MS - now); + return wait > 0 ? Math.max(wait, 1) : 0; +} + +/** + * How long the pipeline's write of `action` on `repo` (made as the app, or the box account while writes use it) waits for + * the budget now; 0 when it may start. The planner reads it so it does not queue a job whose writes would be refused as paced. + */ +export function pipelineWriteWait(db: DatabaseSync, action: string, repo: string, now: number = Date.now()): number { + const identity = githubIdentity(db) === "box" ? "legacy" : "app"; + return keyWait(db, action, identity, paceOwner(identity, repo), now); +} + function tryBegin(w: Writer, action: string, target: string, details: Details, now: number, ownerArg?: string): { id: number } | { wait: number } { const identity = String(details.identity ?? ""); const owner = paceOwner(identity, target, ownerArg); @@ -169,21 +197,8 @@ function tryBegin(w: Writer, action: string, target: string, details: Details, n if (identity.startsWith("user:")) return { id: insert(started) }; w.db.exec("BEGIN IMMEDIATE"); try { - const rows = w.db.prepare( - `SELECT at, json_extract(details, '$.state') AS state, json_extract(details, '$.error') AS error FROM audit - WHERE at >= ? AND json_extract(details, '$.identity') = ? AND json_extract(details, '$.paceOwner') = ? ORDER BY at`, - ).all(new Date(now - 3_600_000).toISOString(), identity, owner) as { at: string; state: string | null; error: string | null }[]; - const times = rows.map((r) => Date.parse(r.at)); - const inMinute = times.filter((t) => t > now - 60_000); - const share = BULK_ACTIONS.has(action) ? BULK_SHARE : 1; - const perMinute = Math.floor(PACE_PER_MINUTE * share); - const perHour = Math.floor(PACE_PER_HOUR * share); - let wait = 0; - if (inMinute.length >= perMinute) wait = inMinute[inMinute.length - perMinute]! + 60_000 - now; - if (times.length >= perHour) wait = Math.max(wait, times[times.length - perHour]! + 3_600_000 - now); - const lastSecondary = rows.filter((r) => r.state === "failed" && SECONDARY_LIMIT.test(r.error ?? "")).map((r) => Date.parse(r.at)).at(-1); - if (lastSecondary !== undefined) wait = Math.max(wait, lastSecondary + SECONDARY_PAUSE_MS - now); - if (wait > 0) { w.db.exec("COMMIT"); return { wait: Math.max(wait, 1) }; } + const wait = keyWait(w.db, action, identity, owner, now); + if (wait > 0) { w.db.exec("COMMIT"); return { wait }; } const id = insert({ ...started, paceOwner: owner }); w.db.exec("COMMIT"); return { id }; diff --git a/src/runner/autopilot.ts b/src/runner/autopilot.ts index 98070685..a13cd58a 100644 --- a/src/runner/autopilot.ts +++ b/src/runner/autopilot.ts @@ -23,6 +23,7 @@ import { writePlanCache } from "../core/plan-cache.ts"; import { refreshLiveness } from "../liveness.ts"; import { embedderId, modelName } from "../embed/model.ts"; import { jevUsable } from "../agents/jev.ts"; +import { PACE_MAX_WAIT_MS, pipelineWriteWait } from "../github/write.ts"; import { embedDue } from "../embed/store.ts"; import { commentApprovalOf } from "../act/approval.ts"; import { selectFixups } from "../fixup/select.ts"; @@ -426,7 +427,16 @@ const LANE_CODE: Omit[] = [ detect: (db, ctx) => { if (!ctx.run) return []; const repos = new Set(allowed(plannedItems(db, "label", { plans: plansOf(db, ctx) })).map((p) => p.facts.repo)); - return ctx.tracked.filter((repo) => repos.has(repo)).map((repo) => ({ step: "labels-apply", params: { repo } })); + // A repo whose GitHub write budget is used up would label nothing (the writes are refused as paced): it waits for the window to move. + const now = Date.now(); + return ctx.tracked.filter((repo) => { + if (!repos.has(repo)) return false; + const wait = pipelineWriteWait(db, "labels-apply", repo, now); + if (wait <= PACE_MAX_WAIT_MS) return true; + ctx.wake?.push(now + wait); + if (ctx.notes) ctx.notes["labels-apply"] ??= `The GitHub write budget for ${repo} is used up until ${hhmm(now + wait)}`; + return false; + }).map((repo) => ({ step: "labels-apply", params: { repo } })); }, }, { diff --git a/test/planner-write-budget.test.ts b/test/planner-write-budget.test.ts new file mode 100644 index 00000000..e21ea0d5 --- /dev/null +++ b/test/planner-write-budget.test.ts @@ -0,0 +1,22 @@ +// The labels stage is not queued for a repo whose GitHub write budget is used up: its writes would be refused as paced and the +// job would label nothing. The stage says until when, and the planner wakes then. +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { setApproval } from "../src/labels/approval.ts"; +import { detectAutopilot, LANES } from "../src/runner/autopilot.ts"; +import { world } from "./world-db.ts"; + +test("labels-apply waits while the repo's write budget is used up", async () => { + const w = world().repo().issue(1, { label: "bugfix" }); + setApproval(w.db, "op", "app", true); + const lanes = LANES.filter((l) => l.name === "labels"); + assert.deepEqual((await detectAutopilot(w.db, { lanes })).proposals.map((p) => p.step), ["labels-apply"]); + + const add = w.db.prepare("INSERT INTO audit (at, login, action, target, details) VALUES (?, 'pipeline', 'comment', 'app#2', ?)"); + const now = Date.now(); + for (let i = 0; i < 60; i++) add.run(new Date(now - 30_000 + i * 100).toISOString(), JSON.stringify({ identity: "legacy", paceOwner: "", state: "done" })); + const held = await detectAutopilot(w.db, { lanes }); + assert.deepEqual(held.proposals, []); + assert.match(held.status?.["labels-apply"] ?? "", /write budget for app is used up until/); + assert.ok((held.wakeAt ?? 0) > now, "the planner wakes when the window moves"); +});