Public: anyone can read this; the crew can change it.

An issue the throughput keeper filed closes itself once its cause has cleared #16128

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16118-an-issue-the-throughput-keeper-filed-clo → main 0 commits fork

  • Merged. scout/16118-an-issue-the-throughput-keeper-filed-clo was merged into main as bfede87.

Review

Sign in to review this change.
3 files changed +128 −6
docs/features/keeper.md +2
@@ -15,6 +15,8 @@ Where it shows: one line on home and on the boards panel, `keeper` in the MCP `f
1515
1616 Red pulls are said, not filed (#16117). A red crew pull is already worked by its own builder and by the stale-reds sweep, so a filed "N open crew pulls have a red check" issue gave a builder nothing to push: by the time one read it, the listed pulls were green, unchecked or running (#16104 was parked twice for that). The `red_checks` line on the keeper panel names the five oldest red pulls instead (`redPulls` in `pile.ts`) and counts the rest; a reader who is not a site admin reads it with no repository named.
1717
18 A filed issue closes itself once its cause has cleared (#16118). Each tick reads the cause of every open issue the keeper filed (a `keeper_attempt` row with `action = 'file'`) again with `readKeeperFacts`; when it no longer holds (`stale_pile` is under 20, a ready issue exists again for `empty_pillar`, the keeper's own open issues not counted, or an old `red_checks` pile is under 5), the keeper closes it as dropped with a one-line note saying what cleared and when (`clearedBy` in `keeper.ts`). At most three close a tick, and none is reopened. An issue a person or another agent filed is never touched, and neither is one with a comment from a person. The week's file-once rule still stands, so a cause that returns is not filed again inside the week.
19
1820 An `empty_pillar` issue lists the ready issues and the hold on each (`landed`, `open_pull`, `stalled`...), so a person sees why nothing can start.
1921
2022 Two DORA numbers (`keeper_measures.ts`, #13110), over the last 7 days, each with the count behind it, on the flow panel (once the week has a reading), in the terminal's Flow dialog (which also says "No reading") and in `keeper.measures` of the MCP `flow` report and its API answer. **Change-fail rate**: landed pulls whose next finished main check was red, or, in the forge's own repository, whose next release was rolled back (a red production proof). **Revert rate**: landed pulls a revert pull landed for within seven days of their landing. Fewer than five landings in the week is "No reading", never a rate.
src/flow/keeper.ts +72 −5
@@ -5,17 +5,17 @@
55 * unsticking assigned issues with no run. After `EFFECT_MS` it checks whether
66 * landings rose; if not it undoes the act, remembers the cause as tried and
77 * asks the owner one question with one button. A cause it cannot act on is
8 * filed as one issue, once. It reads the database and spends no model tokens,
9 * so it still speaks when the subscription is out.
8 * filed as one issue, once, and closed again once its cause clears. It reads the database and
9 * spends no model tokens, so it still speaks when the subscription is out.
1010 */
1111
1212 import type { Database } from "bun:sqlite";
1313 import type { App } from "../app.ts";
14 import { createIssue } from "../core/issues.ts";
14 import { addComment, createIssue, setIssueState } from "../core/issues.ts";
1515 import { getRepo } from "../core/repos.ts";
1616 import { createLog } from "../kernel/log.ts";
1717 import { ownRepoPath } from "../kernel/own_repo.ts";
18 import { KEEPER_CAUSES, pickCause, readKeeperFacts, type KeeperCause, type KeeperMove } from "./keeper_cause.ts";
18 import { KEEPER_CAUSES, pickCause, readKeeperFacts, RED_PULLS, STALE_PULLS, type KeeperCause, type KeeperFacts, type KeeperMove } from "./keeper_cause.ts";
1919 import { measuresCached, type KeeperMeasures } from "./keeper_measures.ts";
2020 import { isLow, landedBetween, readRate, type Rate } from "./keeper_rate.ts";
2121 import { HOLDS, heldNow, heldWords } from "../crew/held.ts";
@@ -30,6 +30,8 @@ export const EFFECT_MS = 2 * HOUR_MS;
3030 export const TRIED_MS = 6 * HOUR_MS;
3131 /** A cause filed as an issue is not filed again this long. */
3232 export const FILE_ONCE_MS = 7 * 24 * HOUR_MS;
33 /** How many of its own filed issues one tick closes once their cause has cleared (#16118). */
34 export const CLOSE_PER_TICK = 3;
3335
3436 type Outcome = "pending" | "worked" | "undone" | "asked" | "answered" | "filed";
3537
@@ -149,6 +151,68 @@ export function fileOnce(app: App, found: { cause: KeeperCause; says: string },
149151 record(app.db, { at: now, cause: found.cause, action: "file", repoId: repo.id, says: `Filed #${made.value.number}`, outcome: "filed", checkAt: 0 });
150152 }
151153
154 interface FiledRow {
155 cause: string;
156 issue_id: string;
157 number: number;
158 repo_id: string;
159 author_id: string;
160 }
161
162 /**
163 * The keeper's own filed issues still open, oldest first: only those a `keeper_attempt` row with
164 * `action = 'file'` names, and none a person has commented on (a person's word keeps it open).
165 */
166 function openFiled(db: Database): FiledRow[] {
167 return db
168 .query<FiledRow, []>(
169 `SELECT a.cause, i.id AS issue_id, i.number, i.repo_id, i.author_id
170 FROM keeper_attempt a
171 JOIN issue i ON i.repo_id = a.repo_id AND i.number = CAST(substr(a.says, 8) AS INTEGER)
172 WHERE a.action = 'file' AND a.says LIKE 'Filed #%' AND i.state = 'open' AND i.kind = 'issue'
173 AND NOT EXISTS (SELECT 1 FROM comment c JOIN user u ON u.id = c.author_id
174 WHERE c.issue_id = i.id AND u.is_agent = 0 AND u.kind = 'user')
175 GROUP BY i.id ORDER BY MIN(a.id)`,
176 )
177 .all();
178 }
179
180 /**
181 * What cleared, when a filed cause no longer holds; undefined while it still holds. The keeper's
182 * own open issues are not counted as ready work, so "nothing is ready" is not cleared by itself.
183 */
184 export function clearedBy(cause: string, f: KeeperFacts, ownOpen: (repoId: string) => number = () => 0): string | undefined {
185 if (cause === "empty_pillar") {
186 const ready = f.repos.find((r) => r.startable + (r.inFlight ?? 0) > ownOpen(r.repoId));
187 return ready ? `${ready.slug} has a ready issue again` : f.repos.length === 0 ? "no autonomous repository is left to fill" : undefined;
188 }
189 if (cause === "stale_pile") return f.stale < STALE_PULLS ? `${f.stale} open crew pulls wait on a builder or a reviewer, under ${STALE_PULLS}` : undefined;
190 if (cause === "red_checks") return f.red < RED_PULLS ? `${f.red} open crew pulls have a failed check at their head, under ${RED_PULLS}` : undefined;
191 return undefined;
192 }
193
194 /**
195 * Closes, a few a tick, the keeper's own filed issues whose cause has cleared, each with a one-line
196 * note saying what cleared and when (#16118). A closed one is never reopened.
197 */
198 function closeCleared(app: App, now: Date, facts: () => KeeperFacts): void {
199 const open = openFiled(app.db);
200 if (open.length === 0) return;
201 const f = facts();
202 const ownOpen = (repoId: string): number => open.filter((o) => o.repo_id === repoId).length;
203 const at = `${now.toISOString().slice(0, 16).replace("T", " ")} UTC`;
204 let closed = 0;
205 for (const o of open) {
206 if (closed >= CLOSE_PER_TICK) break;
207 const why = clearedBy(o.cause, f, ownOpen);
208 if (why === undefined) continue;
209 const said = addComment(app.db, { issueId: o.issue_id, authorId: o.author_id, body: `The throughput keeper closed this: its cause cleared (${why}) at ${at}.` });
210 if (!said.ok || setIssueState(app.db, o.issue_id, "closed", "dropped") === undefined) continue;
211 app.bus.append({ kind: "issue.closed", repoId: o.repo_id, payload: { number: o.number, keeper: o.cause } });
212 closed++;
213 }
214 }
215
152216 function saveState(db: Database, cause: string, says: string, now: number): void {
153217 db.query(
154218 `INSERT INTO keeper_state (id, cause, says, at) VALUES (1, ?1, ?2, ?3)
@@ -166,12 +230,15 @@ export function tickKeeper(app: App, now = new Date(), onLift: () => void = () =
166230 const { db } = app;
167231 const t = now.getTime();
168232 settle(app, t);
233 let read: KeeperFacts | undefined;
234 const facts = (): KeeperFacts => (read ??= readKeeperFacts(app, now));
235 closeCleared(app, now, facts);
169236 const rate = readRate(db, now);
170237 if (!isLow(rate)) {
171238 saveState(db, "", "", t);
172239 return { cause: null, acted: null };
173240 }
174 const found = pickCause(readKeeperFacts(app, now), skipped(db, t));
241 const found = pickCause(facts(), skipped(db, t));
175242 if (!found) {
176243 saveState(db, "", "", t);
177244 return { cause: null, acted: null };
test/flow-keeper.test.ts +54 −1
@@ -20,7 +20,8 @@ import { flowLines, flowScreen } from "../src/tui/screens/flow.ts";
2020 import { scorecardScreen } from "../src/tui/screens/scorecard.ts";
2121 import { headlessTerminal } from "../src/tui/term/terminal.ts";
2222 import { settleApp } from "./helpers/tui.ts";
23 import { answerKeeper, EFFECT_MS, listAttempts, readKeeper, tickKeeper } from "../src/flow/keeper.ts";
23 import { answerKeeper, clearedBy, EFFECT_MS, listAttempts, readKeeper, tickKeeper } from "../src/flow/keeper.ts";
24 import { addComment } from "../src/core/issues.ts";
2425 import { pickCause, type KeeperFacts, type RepoFacts } from "../src/flow/keeper_cause.ts";
2526 import { flowFor } from "../src/system/telemetry.ts";
2627 import { serve, type Servers } from "../src/http/server.ts";
@@ -380,3 +381,55 @@ test("red_checks is said, not filed: no builder issue opens, and the panel names
380381 // Anyone but a site admin reads it with no repository named.
381382 expect(readKeeper(app.db, false).cause).toBe("Open crew pulls have a red check.");
382383 });
384
385 /**
386 * A `stale_pile` issue as the keeper files it: an open issue and the `keeper_attempt` row naming
387 * it.
388 */
389 const fileStalePile = (at: number): { id: string; number: number } => {
390 const made = openIssue(app, admin, fresh(), { title: "Throughput: 24 open crew pulls are waiting on a builder or a reviewer." });
391 if (!made.ok) throw new Error(String(made.error));
392 app.db
393 .query("INSERT INTO keeper_attempt (at, cause, action, repo_id, says, outcome, check_at) VALUES (?1, 'stale_pile', 'file', ?2, ?3, 'filed', 0)")
394 .run(at, repo.id, `Filed #${made.value.number}`);
395 return { id: made.value.id, number: made.value.number };
396 };
397 const stateOf = (id: string): string => app.db.query<{ state: string }, [string]>("SELECT state FROM issue WHERE id = ?1").get(id)!.state;
398
399 test("a filed issue closes itself with a note once its cause has cleared, and a person's issue is left alone (#16118)", () => {
400 const t = Date.UTC(2026, 10, 23, 9, 30);
401 const filedOne = fileStalePile(t - 3_600_000);
402 const persons = openIssue(app, admin, fresh(), { title: "Throughput: a person's own note" });
403 if (!persons.ok) throw new Error(String(persons.error));
404 // The pile is under its threshold now: the cause no longer holds.
405 tickKeeper(app, new Date(t), () => {});
406 expect(stateOf(filedOne.id)).toBe("closed");
407 const note = app.db.query<{ body: string }, [string]>("SELECT body FROM comment WHERE issue_id = ?1").all(filedOne.id).map((c) => c.body);
408 expect(note).toEqual([expect.stringContaining("The throughput keeper closed this: its cause cleared (")]);
409 expect(note[0]).toContain("under 20) at 2026-11-23 09:30 UTC.");
410 expect(stateOf(persons.value.id)).toBe("open");
411 // Never reopened, never noted twice.
412 tickKeeper(app, new Date(t + 60_000), () => {});
413 expect(stateOf(filedOne.id)).toBe("closed");
414 expect(app.db.query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM comment WHERE issue_id = ?1").get(filedOne.id)!.n).toBe(1);
415 });
416
417 test("a person's comment on a keeper-filed issue keeps it open after its cause clears (#16118)", () => {
418 const t = Date.UTC(2026, 10, 23, 10, 0);
419 const kept = fileStalePile(t - 3_600_000);
420 const sibling = fileStalePile(t - 3_600_000);
421 const said = addComment(app.db, { issueId: kept.id, authorId: admin.id, body: "Keep this one: I want the pile looked at." });
422 expect(said.ok).toBe(true);
423 tickKeeper(app, new Date(t), () => {});
424 expect(stateOf(sibling.id)).toBe("closed");
425 expect(stateOf(kept.id)).toBe("open");
426 });
427
428 test("clearedBy: nothing is ready is not cleared by the keeper's own open issue", () => {
429 const r: RepoFacts = { repoId: "r", slug: "a/b", startable: 1, inFlight: 0, running: 0, slots: 2, atCeiling: false, noHeadroom: false, resetsAt: null };
430 const f: KeeperFacts = { repos: [r], red: 0, stale: 25 };
431 expect(clearedBy("empty_pillar", f, () => 1)).toBeUndefined();
432 expect(clearedBy("empty_pillar", f, () => 0)).toBe("a/b has a ready issue again");
433 expect(clearedBy("stale_pile", f)).toBeUndefined();
434 expect(clearedBy("stale_pile", { ...f, stale: 4 })).toBe("4 open crew pulls wait on a builder or a reviewer, under 20");
435 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs