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
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15907-sentinel-shared-01m23vygyffx5hg2f5vm8r74 → main 0 commits fork 1 thread · 1 unresolved
6 files
changed +105
−12
src/core/capacity.ts +21
| @@ -501,6 +501,27 @@ export function storedCapacity( | ||
| 501 | 501 | return row !== null && validCapacity(key, row.value) ? row.value : undefined; |
| 502 | 502 | } |
| 503 | 503 | |
| 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 | ) | |
| 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 | ||
| 504 | 525 | /** What `key` is now: the stored value, else the boot default. Read on every use. */ |
| 505 | 526 | export function capacityValue(db: Database, key: CapacityKey): string { |
| 506 | 527 | 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, | ||
| 37 | 37 | ).run(key(repoId), String(value), byId); |
| 38 | 38 | } |
| 39 | 39 | |
| 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 | ||
| 44 | 40 | /** The words for a count over a ceiling. */ |
| 45 | 41 | export const pullsPausedWords = (open: number, ceiling: number) => `paused: ${open} of ${ceiling} pulls open`; |
| 46 | 42 | |
| 47 | 43 | /** The cause new builder runs are held, in words, or null when they may start. */ |
| 48 | 44 | 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; | |
| 50 | 54 | if (ceiling === 0) return null; |
| 51 | const open = openPullsNow(db, repoId); | |
| 55 | const open = row?.open ?? 0; | |
| 52 | 56 | return open >= ceiling ? pullsPausedWords(open, ceiling) : null; |
| 53 | 57 | } |
| 54 | 58 |
src/crew/too_large.ts +3 −2
| @@ -16,7 +16,7 @@ | ||
| 16 | 16 | import type { Database } from "bun:sqlite"; |
| 17 | 17 | import type { App } from "../app.ts"; |
| 18 | 18 | import { addComment, getIssueById, standsForPerson, type Issue } from "../core/issues.ts"; |
| 19 | import { capacityValue } from "../core/capacity.ts"; | |
| 19 | import { capacityValues } from "../core/capacity.ts"; | |
| 20 | 20 | import type { Repo } from "../core/repos.ts"; |
| 21 | 21 | import { getUserById, type User } from "../core/users.ts"; |
| 22 | 22 | import { speaksForCrew } from "./direct.ts"; |
| @@ -32,7 +32,8 @@ export const TOO_LARGE_NOTE = "Too large for one run: "; | ||
| 32 | 32 | |
| 33 | 33 | /** The two limits now, read on every pick. */ |
| 34 | 34 | 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"]) }; | |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | /** A markdown checklist line, ticked or not: `- [ ] x`, `* [x] y`, `+ [X] z`. */ |
src/perf/page_budgets/app.ts +4 −1
| @@ -1,3 +1,6 @@ | ||
| 1 | 1 | import type { PageBudget } from "../page_budget.ts"; |
| 2 | 2 | |
| 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"; | ||
| 5 | 5 | // over the duties would still show. One query reads every dismissal (#13967), one every quieted |
| 6 | 6 | // finding (#14557); one grouped read of the degraded findings (#14577); +3: the edge-kept duty |
| 7 | 7 | // (#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 { | ||
| 15 | 15 | CAPACITY, |
| 16 | 16 | capacityDefault, |
| 17 | 17 | capacityValue, |
| 18 | capacityValues, | |
| 18 | 19 | crewForMemory, |
| 19 | 20 | setCapacity, |
| 20 | 21 | } from "../src/core/capacity.ts"; |
| @@ -94,3 +95,64 @@ describe("the crew's default, and what overrides it", () => { | ||
| 94 | 95 | expect(crew()).toEqual(["changed"]); |
| 95 | 96 | }); |
| 96 | 97 | }); |
| 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 | }); |
new above
offline
Keys are interpolated into the SQL string. Bind them with placeholders instead and pass keys to .all().