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

Sentinel: shared:01m23vygyffx5hg2f5vm8r74sr:34d9b329ad is past its budget: trevato/forge: "each first page runs no more SQL statements warm than its budget" failed on 10 pulls in the last hour —… #15912

Merged opened this

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

Merged

mason/forge:mason/15907-sentinel-shared-01m23vygyffx5hg2f5vm8r74 → main 0 commits fork 1 thread · 1 unresolved

  • Merged. mason/15907-sentinel-shared-01m23vygyffx5hg2f5vm8r74 was merged into main as 802e4d8.

Review

Sign in to review this change.
6 files changed +105 −12
src/core/capacity.ts +21
@@ -501,6 +501,27 @@ export function storedCapacity(
501501 return row !== null && validCapacity(key, row.value) ? row.value : undefined;
502502 }
503503
504 /**
505 * What each of `keys` is now, from one read of the settings table: a pick that needs several keys
506 * costs one statement.
507 */
508 export function capacityValues<K extends CapacityKey>(db: Database, keys: readonly K[]): Record<K, string> {
509 const stored = new Map(
510 db
511 .query<{ key: string; value: string }, string[]>(
512 `SELECT key, value FROM forge_setting WHERE key IN (${keys.map(() => "?").join(",")})`,
513 )

Keys are interpolated into the SQL string. Bind them with placeholders instead and pass keys to .all().

514 .all(...keys)
515 .map((r) => [r.key, r.value]),
516 );
517 const out = {} as Record<K, string>;
518 for (const k of keys) {
519 const v = stored.get(k);
520 out[k] = v !== undefined && validCapacity(k, v) ? v : bootDefault(db, k);
521 }
522 return out;
523 }
524
504525 /** What `key` is now: the stored value, else the boot default. Read on every use. */
505526 export function capacityValue(db: Database, key: CapacityKey): string {
506527 return storedCapacity(db, key) ?? bootDefault(db, key);
src/core/pulls_ceiling.ts +10 −6
@@ -37,18 +37,22 @@ export function setOpenPullsCeiling(db: Database, repoId: string, value: number,
3737 ).run(key(repoId), String(value), byId);
3838 }
3939
40 export function openPullsNow(db: Database, repoId: string): number {
41 return db.query<{ c: number }, [string]>("SELECT COUNT(*) AS c FROM issue WHERE repo_id = ?1 AND kind = 'pull' AND state = 'open'").get(repoId)?.c ?? 0;
42 }
43
4440 /** The words for a count over a ceiling. */
4541 export const pullsPausedWords = (open: number, ceiling: number) => `paused: ${open} of ${ceiling} pulls open`;
4642
4743 /** The cause new builder runs are held, in words, or null when they may start. */
4844 export function pullsCeilingHold(db: Database, repoId: string): string | null {
49 const ceiling = openPullsCeiling(db, repoId);
45 // One statement for the setting and the count: the app page asks on every request (#15808).
46 const row = db
47 .query<{ value: string | null; open: number }, [string, string]>(
48 `SELECT (SELECT value FROM forge_setting WHERE key = ?1) AS value,
49 (SELECT COUNT(*) FROM issue WHERE repo_id = ?2 AND kind = 'pull' AND state = 'open') AS open`,
50 )
51 .get(key(repoId), repoId);
52 const n = row?.value == null ? NaN : Number(row.value);
53 const ceiling = Number.isInteger(n) && n >= 0 ? n : DEFAULT_PULLS_CEILING;
5054 if (ceiling === 0) return null;
51 const open = openPullsNow(db, repoId);
55 const open = row?.open ?? 0;
5256 return open >= ceiling ? pullsPausedWords(open, ceiling) : null;
5357 }
5458
src/crew/too_large.ts +3 −2
@@ -16,7 +16,7 @@
1616 import type { Database } from "bun:sqlite";
1717 import type { App } from "../app.ts";
1818 import { addComment, getIssueById, standsForPerson, type Issue } from "../core/issues.ts";
19 import { capacityValue } from "../core/capacity.ts";
19 import { capacityValues } from "../core/capacity.ts";
2020 import type { Repo } from "../core/repos.ts";
2121 import { getUserById, type User } from "../core/users.ts";
2222 import { speaksForCrew } from "./direct.ts";
@@ -32,7 +32,8 @@ export const TOO_LARGE_NOTE = "Too large for one run: ";
3232
3333 /** The two limits now, read on every pick. */
3434 export function limitsOf(db: Database): Limits {
35 return { boxes: Number(capacityValue(db, "issue.maxBoxes")), chars: Number(capacityValue(db, "issue.maxChars")) };
35 const v = capacityValues(db, ["issue.maxBoxes", "issue.maxChars"]);
36 return { boxes: Number(v["issue.maxBoxes"]), chars: Number(v["issue.maxChars"]) };
3637 }
3738
3839 /** A markdown checklist line, ticked or not: `- [ ] x`, `* [x] y`, `+ [X] z`. */
src/perf/page_budgets/app.ts +4 −1
@@ -1,3 +1,6 @@
11 import type { PageBudget } from "../page_budget.ts";
22
3 export const budget: PageBudget = { page: "an app page", pattern: "/:owner/:name", medianMs: 50, statements: 50 };
3 // 51 warm. The earlier 50 grew by one when the growth panel began reading the owner's
4 // open-pull ceiling and the open-pull count fresh on every request (#15808: the count moves with
5 // no setter to clear a kept panel); the two are now one statement.
6 export const budget: PageBudget = { page: "an app page", pattern: "/:owner/:name", medianMs: 50, statements: 51 };
src/perf/page_budgets/maintenance.ts +5 −3
@@ -5,6 +5,8 @@ import type { PageBudget } from "../page_budget.ts";
55 // over the duties would still show. One query reads every dismissal (#13967), one every quieted
66 // finding (#14557); one grouped read of the degraded findings (#14577); +3: the edge-kept duty
77 // (#14682); one grouped read of every duty's last run (#14674); +3: the deepsec-sweep duty
8 // (#15026); +3: the daily-proof duty (#15625); +3: the account-purge duty (#15620). 96 is the
9 // steady count measured warm on every run, not a load-sensitive figure.
10 export const budget: PageBudget = { page: "maintenance", pattern: "/maintenance", medianMs: 50, statements: 96 };
8 // (#15026); +3: the daily-proof duty (#15625); +3: the account-purge duty (#15620); +3: the
9 // revert-pulls duty (#15810, 9e794c78a: 26 duties, three reads each: last outcome, pause state,
10 // runs after the pause marker). 99 is the steady count measured warm
11 // on every run, not a load-sensitive figure.
12 export const budget: PageBudget = { page: "maintenance", pattern: "/maintenance", medianMs: 50, statements: 99 };
test/capacity-crew.test.ts +62
@@ -15,6 +15,7 @@ import {
1515 CAPACITY,
1616 capacityDefault,
1717 capacityValue,
18 capacityValues,
1819 crewForMemory,
1920 setCapacity,
2021 } from "../src/core/capacity.ts";
@@ -94,3 +95,64 @@ describe("the crew's default, and what overrides it", () => {
9495 expect(crew()).toEqual(["changed"]);
9596 });
9697 });
98
99 describe("capacityValues", () => {
100 let dir: string;
101 let app: App;
102 beforeEach(async () => {
103 dir = await mkdtemp(join(tmpdir(), "cap-values-"));
104 app = createApp({ dataDir: dir, port: 0 });
105 });
106 afterEach(async () => {
107 await rm(dir, { recursive: true, force: true });
108 });
109
110 test("reads several keys in one statement, agreeing with capacityValue", () => {
111 const keys = ["issue.maxBoxes", "issue.maxChars"] as const;
112 const got = capacityValues(app.db, keys);
113 for (const k of keys) expect(got[k]).toBe(capacityValue(app.db, k));
114 });
115
116 test("a stored value wins, an invalid stored value falls back to the default", () => {
117 app.db.run("INSERT OR REPLACE INTO forge_setting (key, value, updated_by, updated_at) VALUES ('issue.maxBoxes', 'nonsense', 'x', '2026-01-01')");
118 const got = capacityValues(app.db, ["issue.maxBoxes"]);
119 expect(got["issue.maxBoxes"]).toBe(capacityValue(app.db, "issue.maxBoxes"));
120 expect(Number.isFinite(Number(got["issue.maxBoxes"]))).toBe(true);
121 });
122 });
123
124 describe("capacityValues, stored and counted", () => {
125 let dir: string;
126 let app: App;
127 beforeEach(async () => {
128 dir = await mkdtemp(join(tmpdir(), "cap-values2-"));
129 app = createApp({ dataDir: dir, port: 0 });
130 });
131 afterEach(async () => {
132 await rm(dir, { recursive: true, force: true });
133 });
134
135 test("a valid stored value wins over the default", () => {
136 const before = capacityValue(app.db, "issue.maxBoxes");
137 const next = String(Number(before) + 1);
138 const set = setCapacity(app.db, "issue.maxBoxes", next, "tester");
139 expect(set.to).toBe(next);
140 expect(capacityValues(app.db, ["issue.maxBoxes"])["issue.maxBoxes"]).toBe(next);
141 });
142
143 test("two keys cost one statement", () => {
144 let n = 0;
145 const db = app.db as unknown as { query: (sql: string) => unknown };
146 const original = db.query.bind(app.db);
147 db.query = (sql: string) => {
148 n++;
149 return original(sql);
150 };
151 try {
152 capacityValues(app.db, ["issue.maxBoxes", "issue.maxChars"]);
153 } finally {
154 db.query = original;
155 }
156 expect(n).toBe(1);
157 });
158 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs