mirror of
https://github.com/storytold/autocraft.git
synced 2026-11-28 00:59:43 +01:00
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.
This commit is contained in:
1 parent
643b750765
commit
ded4e07234
3 files changed
+63
-16
No files matched your search
+30
-15
@@ -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 };
|
||||
|
||||
+11
-1
@@ -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<Lane, "steps">[] = [
|
||||
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 } }));
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
Reference in new issue
Block a user