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

Clog note: measure time to detect #6769

Parked opened this
Closed without merging

scout/forge:scout/6757-clog-note-measure-time-to-detect → main 6 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
17 files changed +457 −29
README.md +2
@@ -44,6 +44,8 @@ A hand-over card on `/asks` (an agent's pull the reviewer stopped on after three
4444
4545 Home opens with "Since you were here · 2 days": the pulls that landed in public repositories since you last loaded `/` (1 hour to 7 days back, a day on a first visit); the terminal's home shows the same lines. See `docs/features/since-you-were-here.md`.
4646
47 An admin's home and the terminal's Flow dialog also say when the forge is clogged and how long the last clog took to notice and to clear: `docs/features/clog-watch.md`.
48
4749 A pull the merge queue holds as red alone (red on its own merge at its current head; cleared by a new head or a moved main) says so under its checks line: "red alone at <time>, waiting for a new head or a new main", on the pull page, its live checks line and the studio change step, and in the checks row of the pull's terminal screen (`redMark` in `src/checks/queue.ts`). Nothing is drawn when it is not held.
4850
4951 On a sealed issue, a writer gets the private repository to fix it in: `POST /api/v1/repos/:owner/:name/issues/:number/private`, the issue page's button, and `p` on the issue's terminal screen, which shows the repository and its clone URL (built from the forge's origin). All three refuse alike: not found for anyone who may not see sealed reports, `not_sealed`, and `unverified`.
docs/features/clog-watch.md added +8
@@ -0,0 +1,8 @@
1 # Clog watch (#6757)
2
3 Once a minute the forge reads its flow (open pulls that conflict, pulls opened and landed over 2 h, the oldest approved wait) and declares a clog on the thresholds in `src/flow/clog.ts`. A clog clears after two quiet readings.
4
5 - **Where to see it (admin only):** home shows one muted line while a clog stands (`Clogged for 30 h (noticed 30 h in): <cause>.`) and, for a day after, the last cleared note (cause, time to detect, time to clear). The terminal's Flow dialog shows the same lines. A guest and an agent see neither.
6 - **Time to detect** is declared-minus-started. The start is the oldest approved pull's wait when that is what clogs; for any other reason it is the reading itself, so the note leaves the line out.
7 - **State:** `clog_state` (the standing clog) and `clog_last` (the last cleared note). Events `clog.started` and `clog.cleared` go to the outbox for admins.
8 - **Code:** `flow/clog_tick.ts` (the tick), `flow/clog_read.ts` (the reader), `flow/clog_watch.ts` and `flow/clog_note.ts` (the pure steps).
src/core/event_audience.ts +2
@@ -154,6 +154,8 @@ export function classifiedAudience(kind: string): EventAudience | undefined {
154154 kind.startsWith("drill.") ||
155155 // A snapshot moving is the forge owner's too, and has no repository.
156156 kind.startsWith("backup.") ||
157 // A clog is the whole forge's flow, the owner's to hear (`flow/clog_tick.ts`).
158 kind.startsWith("clog.") ||
157159 // A report from outside waits for a person who decides; its arrival is
158160 // theirs to see, never the readers' (`actions/reports.ts`).
159161 kind.startsWith("report.") ||
src/core/migrations/20261027120000-clog-state.ts added +20
@@ -0,0 +1,20 @@
1 // The clog the minute tick is standing in (#6757): at most one row. `since` is
2 // when it was declared, `started_at` when it really began, both epoch ms;
3 // `reasons` is the JSON list on the reading that declared it, `quiet` the
4 // quiet readings in a row so far. `clog_last` keeps the last cleared note, so a
5 // page can show it without scanning the outbox.
6 export default `
7 CREATE TABLE IF NOT EXISTS clog_state (
8 id INTEGER PRIMARY KEY CHECK (id = 1),
9 since INTEGER NOT NULL,
10 started_at INTEGER NOT NULL,
11 reasons TEXT NOT NULL,
12 quiet INTEGER NOT NULL DEFAULT 0
13 );
14
15 CREATE TABLE IF NOT EXISTS clog_last (
16 id INTEGER PRIMARY KEY CHECK (id = 1),
17 cleared_at INTEGER NOT NULL,
18 note TEXT NOT NULL
19 );
20 `;
src/flow/clog_note.ts +4 −6
@@ -27,12 +27,10 @@ export function duration(ms: number): string {
2727
2828 export function clogNote(input: ClogNoteInput): string {
2929 const why = input.reasons.map((r) => r.trim()).filter(Boolean);
30 const lines = [
31 `Clog cleared. Cause: ${why.length ? why.join("; ") : "not recorded"}.`,
32 "",
33 `- Time to detect: ${duration(input.detectedAt - input.startedAt)}`,
34 `- Time to clear: ${duration(input.clearedAt - input.detectedAt)}`,
35 ];
30 const lines = [`Clog cleared. Cause: ${why.length ? why.join("; ") : "not recorded"}.`, ""];
31 // Detection that is the start itself measured nothing: the line is left out.
32 if (input.detectedAt !== input.startedAt) lines.push(`- Time to detect: ${duration(input.detectedAt - input.startedAt)}`);
33 lines.push(`- Time to clear: ${duration(input.clearedAt - input.detectedAt)}`);
3634 const usd = input.strandedUsd;
3735 if (typeof usd === "number" && Number.isFinite(usd) && usd > 0) {
3836 lines.push(`- Stranded at peak: $${usd.toLocaleString("en-US", { maximumFractionDigits: 2 })}`);
src/flow/clog_read.ts added +44
@@ -0,0 +1,44 @@
1 /**
2 * What the clog watch knows, for a person (#6757): the clog standing now and
3 * the note of the last one cleared, as plain lines. The reader is the owner's
4 * alone (`clog.*` is an admins' event); a page or screen that shows these
5 * lines gates on that before it calls.
6 */
7
8 import type { Database } from "bun:sqlite";
9 import { duration } from "./clog_note.ts";
10 import { loadState } from "./clog_tick.ts";
11
12 const DAY_MS = 86_400_000;
13
14 export interface ClogRead {
15 readonly standing: { readonly since: number; readonly startedAt: number; readonly reasons: readonly string[] } | null;
16 readonly last: { readonly clearedAt: number; readonly note: string } | null;
17 }
18
19 export function readClog(db: Database): ClogRead {
20 const state = loadState(db);
21 const last = db.query<{ cleared_at: number; note: string }, []>("SELECT cleared_at, note FROM clog_last WHERE id = 1").get();
22 return {
23 standing: state ? { since: state.since, startedAt: state.startedAt, reasons: state.firstReasons } : null,
24 last: last ? { clearedAt: last.cleared_at, note: last.note } : null,
25 };
26 }
27
28 /**
29 * "Clogged for 2.5 h (noticed 12 min in): <cause>." and the last cleared note
30 * as one line each. A cleared note older than `keepMs` is left out; none, no lines.
31 */
32 export function clogLines(read: ClogRead, now: number, keepMs = DAY_MS): string[] {
33 const out: string[] = [];
34 if (read.standing) {
35 const s = read.standing;
36 const cause = s.reasons.length ? s.reasons.join("; ") : "cause not recorded";
37 const lag = s.since > s.startedAt ? ` (noticed ${duration(s.since - s.startedAt)} in)` : "";
38 out.push(`Clogged for ${duration(now - s.startedAt)}${lag}: ${cause}.`);
39 }
40 if (read.last && now - read.last.clearedAt <= keepMs) {
41 out.push(read.last.note.trim().split("\n").map((l) => l.replace(/^- /, "")).filter(Boolean).join(" · "));
42 }
43 return out;
44 }
src/flow/clog_tick.ts added +89
@@ -0,0 +1,89 @@
1 /**

The tick writes events only the outbox sees. Add the admin-gated reader (page and tty) that makes the note reachable.

2 * The clog tick (#6757): once a minute, read the flow, step the clog watch
3 * and keep its state in `clog_state`. A clog's real start is the wait of the
4 * oldest approved pull when that is what clogs, else the reading itself; the
5 * cleared note, with "Time to detect", rides the event outbox as
6 * `clog.cleared` (and `clog.started` when it is declared).
7 */
8
9 import type { Database } from "bun:sqlite";
10 import type { EventBus } from "../core/events.ts";
11 import { CLOG_APPROVED_HOURS, clogOf } from "./clog.ts";
12 import { clogNote } from "./clog_note.ts";
13 import { type ClogState, clogStep } from "./clog_watch.ts";
14 import { conflictedPulls } from "./conflicted.ts";
15 import { readOldestApproved } from "./oldest_approved.ts";
16
17 const HOUR_MS = 3_600_000;
18
19 interface Row {
20 since: number;
21 started_at: number;
22 reasons: string;
23 quiet: number;
24 }
25
26 export function loadState(db: Database): ClogState | null {
27 const row = db.query<Row, []>("SELECT since, started_at, reasons, quiet FROM clog_state WHERE id = 1").get();
28 if (!row) return null;
29 let reasons: string[] = [];
30 try {
31 const parsed: unknown = JSON.parse(row.reasons);
32 if (Array.isArray(parsed)) reasons = parsed.filter((r): r is string => typeof r === "string");
33 } catch {
34 // A row we cannot read is a clog with no recorded cause.
35 }
36 return { since: row.since, startedAt: row.started_at, firstReasons: reasons, ...(row.quiet > 0 ? { quiet: row.quiet } : {}) };
37 }
38
39 function saveState(db: Database, state: ClogState | null): void {
40 if (!state) {
41 db.query("DELETE FROM clog_state WHERE id = 1").run();
42 return;
43 }
44 db.query(
45 `INSERT INTO clog_state (id, since, started_at, reasons, quiet) VALUES (1, ?1, ?2, ?3, ?4)
46 ON CONFLICT(id) DO UPDATE SET since = ?1, started_at = ?2, reasons = ?3, quiet = ?4`,
47 ).run(state.since, state.startedAt, JSON.stringify(state.firstReasons), state.quiet ?? 0);
48 }
49
50 /** Pulls opened and landed over the 2 h before `now`, forge-wide. */
51 function twoHours(db: Database, now: Date): { opened: number; landed: number } {
52 const from = new Date(now.getTime() - 2 * HOUR_MS).toISOString();
53 const to = now.toISOString();
54 const row = db
55 .query<{ opened: number; landed: number }, [string, string]>(
56 `SELECT
57 (SELECT COUNT(*) FROM pull p JOIN issue i ON i.id = p.issue_id
58 WHERE i.created_at >= ?1 AND i.created_at <= ?2) AS opened,
59 (SELECT COUNT(*) FROM pull p JOIN issue i ON i.id = p.issue_id JOIN repo r ON r.id = i.repo_id
60 WHERE p.base_branch = r.default_branch AND p.merged_at >= ?1 AND p.merged_at <= ?2) AS landed`,
61 )
62 .get(from, to);
63 return { opened: row?.opened ?? 0, landed: row?.landed ?? 0 };
64 }
65
66 /** One tick; returns the event it appended, if any. Never throws for a missing table row. */
67 export function tickClog(db: Database, bus: EventBus, now = new Date()): "started" | "cleared" | null {
68 const { opened, landed } = twoHours(db, now);
69 const oldest = readOldestApproved(db, now);
70 const hours = oldest?.hours ?? 0;
71 const reading = clogOf({ conflicted: conflictedPulls(db).length, opened, landed, oldestApprovedHours: hours });
72 const at = now.getTime();
73 // When the oldest approved pull is what clogs, the clog began when it began to wait.
74 const startedAt = hours > CLOG_APPROVED_HOURS ? at - hours * HOUR_MS : at;
75 const step = clogStep(loadState(db), reading.reasons, at, startedAt);
76 saveState(db, step.state);
77 if (step.event === "started") {
78 bus.append({ kind: "clog.started", payload: { reasons: [...reading.reasons] } });
79 } else if (step.event === "cleared") {
80 const prev = step.since ?? at;
81 const note = clogNote({ reasons: step.reasons ?? [], startedAt: step.startedAt ?? prev, detectedAt: prev, clearedAt: at });
82 db.query(
83 `INSERT INTO clog_last (id, cleared_at, note) VALUES (1, ?1, ?2)
84 ON CONFLICT(id) DO UPDATE SET cleared_at = ?1, note = ?2`,
85 ).run(at, note);
86 bus.append({ kind: "clog.cleared", payload: { note } });
87 }
88 return step.event;
89 }
src/flow/clog_watch.ts +13 −6
@@ -8,8 +8,10 @@
88 export const QUIET_TO_CLEAR = 2;
99
1010 export interface ClogState {
11 /** When the clog started, epoch ms. */
11 /** When the clog was declared, epoch ms. */
1212 readonly since: number;
13 /** When it really began (the oldest stuck item's wait, say); never after `since`. */
14 readonly startedAt: number;
1315 /** The reasons on the reading that started it. */
1416 readonly firstReasons: readonly string[];
1517 /** Quiet readings in a row so far; absent when zero. */
@@ -21,17 +23,22 @@ export type ClogEvent = "started" | "cleared";
2123 export interface ClogStep {
2224 readonly state: ClogState | null;
2325 readonly event: ClogEvent | null;
24 /** On "cleared": when the clog had started. */
26 /** On "cleared": when the clog was declared, and when it really began. */
2527 readonly since?: number;
28 readonly startedAt?: number;
29 /** On "cleared": the reasons the clog was declared on. */
30 readonly reasons?: readonly string[];
2631 }
2732
28 export function clogStep(prev: ClogState | null, reading: readonly string[], now: number): ClogStep {
33 /** `startedAt` is when the reading says the clog began (the reading's own time when it cannot tell); it counts only on the reading that declares it. */
34 export function clogStep(prev: ClogState | null, reading: readonly string[], now: number, startedAt: number): ClogStep {
2935 if (!prev) {
3036 if (reading.length === 0) return { state: null, event: null };
31 return { state: { since: now, firstReasons: [...reading] }, event: "started" };
37 const began = Number.isFinite(startedAt) ? Math.min(startedAt, now) : now;
38 return { state: { since: now, startedAt: began, firstReasons: [...reading] }, event: "started" };
3239 }
33 if (reading.length > 0) return { state: { since: prev.since, firstReasons: prev.firstReasons }, event: null };
40 if (reading.length > 0) return { state: { since: prev.since, startedAt: prev.startedAt, firstReasons: prev.firstReasons }, event: null };
3441 const quiet = (prev.quiet ?? 0) + 1;
35 if (quiet >= QUIET_TO_CLEAR) return { state: null, event: "cleared", since: prev.since };
42 if (quiet >= QUIET_TO_CLEAR) return { state: null, event: "cleared", since: prev.since, startedAt: prev.startedAt, reasons: prev.firstReasons };
3643 return { state: { ...prev, quiet }, event: null };
3744 }
src/main.ts +11
@@ -4,6 +4,7 @@
44 * environment-only — a handful of variables, all with sane defaults.
55 */
66
7 import { tickClog } from "./flow/clog_tick.ts";
78 import { BOOT_ACTOR, setSignup, signupFromEnv } from "./settings/signup.ts";
89 import { startMemorySampler } from "./boards/memory.ts";
910 import { startSampler } from "./boards/sampler.ts";
@@ -519,6 +520,15 @@ const tickAsks = async (): Promise<void> => {
519520 // Followed upstreams are read on a slow tick (`actions/upstream_follow.ts`):
520521 // the tip and how far behind land on the follow row; nothing is opened.
521522 const followTimer = setInterval(() => void checkDue(app), 60_000);
523 // The clog watch (#6757): the flow is read once a minute; a clog's state lives in
524 // `clog_state` and its cleared note, with the time to detect, rides the outbox.
525 const clogTimer = setInterval(() => {
526 try {
527 tickClog(app.db, app.bus);
528 } catch (error) {
529 app.log.error("clog tick failed", { error: String(error) });
530 }
531 }, 60_000);
522532 const staleTimer = setInterval(() => void tickAsks(), 120_000);
523533 void tickAsks();
524534
@@ -667,6 +677,7 @@ function drain(signal: NodeJS.Signals): Promise<unknown> {
667677 clearInterval(syncTimer);
668678 clearInterval(staleTimer);
669679 clearInterval(followTimer);
680 clearInterval(clogTimer);
670681 clearInterval(processTimer);
671682 clearInterval(leaseTimer);
672683 // The platform's SIGTERM: the listener stays open while routers learn
src/tui/screens/flow.ts +2
@@ -27,6 +27,7 @@ import { firedAlarms } from "../../flow/alarms.ts";
2727 import { readVitals, vitalsFor, vitalsLine } from "../../flow/vitals.ts";
2828 import { readLeadTime, type LeadTime } from "../../flow/lead_time.ts";
2929 import { readOpeners, type Opener } from "../../flow/openers.ts";
30 import { clogLines, readClog } from "../../flow/clog_read.ts";
3031 import { readQueues, redBatchWords } from "../../flow/queue.ts";
3132 import { reviewersBusy, reviewingSays } from "../../flow/reviewing.ts";
3233 import { requestedSays, requestedWaits } from "../../flow/requested.ts";
@@ -72,6 +73,7 @@ export function flowLines(
7273 ...wrap(verdict(stages, now.getTime()), inner),
7374 "",
7475 ...wrap(vitalsLine(vitalsFor(readVitals(db, now, agent !== undefined), agent === undefined)), inner),
76 ...(agent === undefined ? clogLines(readClog(db), now.getTime()).flatMap((l) => wrap(l, inner)) : []),
7577 ...firedAlarms(db, now, { publicOnly: agent !== undefined, ceilingUsd }).flatMap((a) => wrap(a.says, inner)),
7678 "",
7779 ...wrap(parkedSays(readParked(db, now, agent !== undefined)), inner),
src/web/studio/home.ts +2 −1
@@ -53,7 +53,7 @@ import { needsYouSection } from "./asks_page.ts";
5353 import { flowCards } from "./flow_card.ts";
5454 import { composer, feedJs, feedNote, feedRegion } from "./feed.ts";
5555 import { alarmLines, cachedAlarms } from "./alarm_lines.ts";
56 import { kpiRow, vitalsRow, waitingLine } from "./kpi_row.ts";
56 import { clogRow, kpiRow, vitalsRow, waitingLine } from "./kpi_row.ts";
5757 import { feedCandidateCache } from "../../feed/feed.ts";
5858 import { notePhase, notePhaseRest, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts";
5959 import { kpiCache, panelCache, waitingCache } from "./home_cache.ts";
@@ -289,6 +289,7 @@ export async function studioHome(
289289 ? timePhase(ctx.request, "kpis", () => kpiRow(ctx.app.db, kpiOpts), () => kpiCache(ctx.app.db))
290290 : liveLine(ctx)}
291291 ${figures ? timePhase(ctx.request, "vitals", () => vitalsRow(ctx.app.db)) : null}
292 ${figures ? clogRow(ctx.app.db) : null}
292293 ${figures ? alarmLines(cachedAlarms(ctx.app.db, new Date(), ceilingUsd)) : null}
293294 ${timePhase(ctx.request, "waiting", () => waitingLine(ctx.app.db, user), () => waitingCache(ctx.app.db))}
294295 ${figures
src/web/studio/kpi_row.ts +8
@@ -10,6 +10,7 @@
1010 import type { Database } from "bun:sqlite";
1111 import { backupAge, backupFull, backupOverdue, buildersValue, checkTime, reactNote, speedWords, spendCeilingWords, spendFull, spendValue, usersValue, visitorsNote, visitorsValue, waitingWords } from "../../flow/kpi_words.ts";
1212 import type { Kpis } from "../../flow/kpis.ts";
13 import { clogLines, readClog } from "../../flow/clog_read.ts";
1314 import { vitalsLine } from "../../flow/vitals.ts";
1415 import { cachedKpis, cachedVitals, cachedWaiting } from "./home_cache.ts";
1516 import { escapeHtml, html, raw, type Html } from "../html.ts";
@@ -196,3 +197,10 @@ export function vitalsRow(db: Database, now: Date = new Date()): Html {
196197 const rest = vitalsLine(v).split(" · ").slice(1).join(" · ");
197198 return html`<p class="small muted"><a href="/boards">${v.open} open pulls</a> · ${rest}</p>`;
198199 }
200
201 /** Home's clog lines for an admin (#6757): the clog standing now and the last cleared note; nothing when there is neither. */
202 export function clogRow(db: Database, now: Date = new Date()): Html {
203 const lines = clogLines(readClog(db), now.getTime());
204 if (lines.length === 0) return html``;
205 return html`${lines.map((l) => html`<p class="small muted break">${l}</p>`)}`;
206 }
test/event-audience-coverage.test.ts +7
@@ -177,6 +177,13 @@ describe("event audience coverage", () => {
177177 expect(eventAudience("brand.new_kind")).toBe("admins");
178178 });
179179
180 test("a clog's events carry its cause and are classified for admins, never guests or agents", () => {
181 for (const kind of ["clog.started", "clog.cleared"]) {
182 expect(classifiedAudience(kind)).toBe("admins");
183 expect(eventAudience(kind)).toBe("admins");
184 }
185 });
186
180187 test("the scan names a kind it cannot read literally", () => {
181188 const { unreadable } = emittedKinds({
182189 "src/x.ts": "app.bus.append({ kind: pick(a), payload: {} });",
test/flow-clog-door.test.ts added +139
@@ -0,0 +1,139 @@
1 /**
2 * The clog on a door (#6757): the tick's other reasons print no "Time to
3 * detect", a corrupt row and a restart are survived, and the standing clog and
4 * the last cleared note show to the admin on home and in the Flow dialog, never to a guest or an agent.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtempSync, rmSync } from "node:fs";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { createUser } from "../src/core/users.ts";
12 import { tickClog } from "../src/flow/clog_tick.ts";
13 import { clogLines, readClog } from "../src/flow/clog_read.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { flowLines } from "../src/tui/screens/flow.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17
18 quietLogs();
19
20 const T0 = new Date("2026-10-26T12:00:00.000Z");
21 const ago = (): string => new Date(T0.getTime() - 5 * 3_600_000).toISOString();
22 const after = (min: number): Date => new Date(T0.getTime() + min * 60_000);
23
24 let root: string;
25 let app: App;
26 let servers: Servers;
27 let uid: string;
28 let n = 0;
29 let adminCookie = "";
30 let guestCookie = "";
31
32 async function signIn(username: string): Promise<string> {
33 const res = await fetch(`${servers.url}/signin`, {
34 method: "POST",
35 headers: { origin: servers.url },
36 body: new URLSearchParams({ username, password: "a-good-passphrase" }),
37 redirect: "manual",
38 });
39 return (res.headers.get("set-cookie") ?? "").split(";")[0]!;
40 }
41
42 beforeAll(async () => {
43 root = mkdtempSync(join(tmpdir(), "clog-door-"));
44 app = createApp({ dataDir: root, port: 0 });
45 for (const [username, first] of [["alice", true], ["guest", false]] as const) {
46 const u = await createUser(app.db, {
47 origin: "seed",
48 trusted: true,
49 username,
50 email: `${username}@forge.local`,
51 password: "a-good-passphrase",
52 adminIfFirst: first,
53 });
54 if (!u.ok) throw new Error(u.error);
55 if (first) uid = u.value.id;
56 }
57 app.db.run("INSERT INTO repo (id, owner_id, name, visibility) VALUES ('pub', ?1, 'pub', 'public')", [uid]);
58 servers = await serve(app);
59 adminCookie = await signIn("alice");
60 guestCookie = await signIn("guest");
61 });
62 afterAll(() => {
63 servers.stop();
64 app.close();
65 rmSync(root, { recursive: true, force: true });
66 });
67
68 const reset = (): void => {
69 app.db.run("DELETE FROM clog_state");
70 app.db.run("DELETE FROM clog_last");
71 app.db.run("DELETE FROM issue");
72 };
73
74 function pull(createdAt: string, merged: string | null = null): string {
75 const id = `p${++n}`;
76 app.db.run("INSERT INTO issue (id, repo_id, number, kind, title, author_id, state, created_at) VALUES (?1, 'pub', ?2, 'pull', 't', ?3, 'open', ?4)", [id, n, uid, createdAt]);
77 app.db.run("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', ?2, '', 'h', ?3)", [id, `b${n}`, merged]);
78 return id;
79 }
80
81 const cleared = (): string => app.db.query<{ note: string }, []>("SELECT note FROM clog_last").get()?.note ?? "";
82
83 test("a landing-ratio clog starts at the reading and its note prints no Time to detect", () => {
84 reset();
85 for (let i = 0; i < 10; i++) pull(T0.toISOString());
86 expect(tickClog(app.db, app.bus, T0)).toBe("started");
87 const row = app.db.query<{ since: number; started_at: number }, []>("SELECT since, started_at FROM clog_state").get()!;
88 expect(row.started_at).toBe(row.since);
89 app.db.run("DELETE FROM issue");
90 tickClog(app.db, app.bus, after(1));
91 expect(tickClog(app.db, app.bus, after(2))).toBe("cleared");
92 expect(cleared()).toContain("Cause: 0 landed of 10 opened");
93 expect(cleared()).not.toContain("Time to detect");
94 });
95
96 test("a conflict clog starts at the reading too", () => {
97 reset();
98 for (let i = 0; i < 11; i++) {
99 const id = pull(ago());
100 app.db.run("INSERT INTO run (id, repo_id, kind, state, issue_id, reason, created_at) VALUES (?1, 'pub', 'agent', 'queued', ?2, 'conflict', ?3)", [`r${id}`, id, T0.toISOString()]);
101 }
102 expect(tickClog(app.db, app.bus, T0)).toBe("started");
103 const row = app.db.query<{ since: number; started_at: number }, []>("SELECT since, started_at FROM clog_state").get()!;
104 expect(row.started_at).toBe(row.since);
105 app.db.run("DELETE FROM run");
106 });
107
108 test("a corrupt reasons row is a clog with no recorded cause, and a standing row resumes after a restart", () => {
109 reset();
110 app.db.run("INSERT INTO clog_state (id, since, started_at, reasons, quiet) VALUES (1, ?1, ?2, '{not json', 1)", [T0.getTime(), T0.getTime() - 3_600_000]);
111 const read = readClog(app.db);
112 expect(read.standing?.reasons).toEqual([]);
113 expect(clogLines(read, T0.getTime())[0]).toContain("cause not recorded");
114 // The next quiet reading (a fresh tick, as after a restart) finishes the clear from the stored row.
115 expect(tickClog(app.db, app.bus, after(1))).toBe("cleared");
116 expect(cleared()).toContain("Cause: not recorded");
117 expect(cleared()).toContain("Time to detect: 60 min");
118 });
119
120 test("clogLines: the standing clog, then a cleared note while it is fresh", () => {
121 const read = { standing: { since: T0.getTime(), startedAt: T0.getTime() - 30 * 3_600_000, reasons: ["a", "b"] }, last: { clearedAt: T0.getTime(), note: "Clog cleared. Cause: x.\n\n- Time to detect: 5 min\n- Time to clear: 2 min\n" } };
122 const lines = clogLines(read, T0.getTime());
123 expect(lines[0]).toBe("Clogged for 30 h (noticed 30 h in): a; b.");
124 expect(lines[1]).toBe("Clog cleared. Cause: x. · Time to detect: 5 min · Time to clear: 2 min");
125 expect(clogLines(read, T0.getTime() + 2 * 86_400_000)).toHaveLength(1);
126 expect(clogLines({ standing: null, last: null }, T0.getTime())).toEqual([]);
127 });
128
129 test("the admin sees the clog on home and in the Flow dialog; a guest and an agent see neither", async () => {
130 reset();
131 app.db.run("INSERT INTO clog_state (id, since, started_at, reasons, quiet) VALUES (1, ?1, ?1, ?2, 0)", [Date.now(), JSON.stringify(["31 pulls conflict <b>"])]);
132 const home = async (cookie: string): Promise<string> => (await fetch(`${servers.url}/`, { headers: { cookie } })).text();
133 const mine = await home(adminCookie);
134 expect(mine).toContain("31 pulls conflict &lt;b&gt;");
135 expect(mine).not.toContain("<b>");
136 expect(await home(guestCookie)).not.toContain("pulls conflict");
137 expect(flowLines(app.db, new Date(), [], undefined, []).join("\n")).toContain("31 pulls conflict");
138 expect(flowLines(app.db, new Date(), [], { queues: [] }, []).join("\n")).not.toContain("31 pulls conflict");
139 });
test/flow-clog-note.test.ts +6
@@ -31,3 +31,9 @@ test("a negative span reads as zero", () => {
3131 test("no reasons still reads", () => {
3232 expect(clogNote({ ...base, reasons: [] })).toContain("Cause: not recorded.");
3333 });
34
35 test("detection at the start itself leaves the line out", () => {
36 const note = clogNote({ ...base, detectedAt: 0 });
37 expect(note).not.toContain("Time to detect");
38 expect(note).toContain("Time to clear: 70 min");
39 });
test/flow-clog-tick.test.ts added +68
@@ -0,0 +1,68 @@
1 /** The clog tick (#6757): a real start is stored, survives between ticks, and reaches the cleared note. */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createUser } from "../src/core/users.ts";
8 import { tickClog } from "../src/flow/clog_tick.ts";
9
10 const T0 = new Date("2026-10-26T12:00:00.000Z");
11 const after = (min: number): Date => new Date(T0.getTime() + min * 60_000);
12 const ago = (h: number): string => new Date(T0.getTime() - h * 3_600_000).toISOString();
13
14 let dir: string;
15 let app: App;
16
17 beforeAll(async () => {
18 dir = await mkdtemp(join(tmpdir(), "clog-tick-"));
19 app = createApp({ dataDir: dir, port: 0 });
20 const u = await createUser(app.db, {
21 origin: "seed",
22 trusted: true,
23 username: "alice",
24 email: "[email protected]",
25 password: "a-good-passphrase",
26 adminIfFirst: true,
27 });
28 if (!u.ok) throw new Error(u.error);
29 app.db.run("INSERT INTO repo (id, owner_id, name, visibility) VALUES ('pub', ?1, 'pub', 'public')", [u.value.id]);
30 app.db.run(
31 "INSERT INTO issue (id, repo_id, number, kind, title, author_id, state) VALUES ('p1', 'pub', 1, 'pull', 't', ?1, 'open')",
32 [u.value.id],
33 );
34 app.db.run("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES ('p1', 'main', 'b', '', 'h')");
35 app.db.run("INSERT INTO review (id, issue_id, author_id, state, created_at) VALUES ('r1', 'p1', ?1, 'approved', ?2)", [u.value.id, ago(30)]);
36 });
37 afterAll(async () => {
38 app.close?.();
39 await rm(dir, { recursive: true, force: true });
40 });
41
42 const events = (kind: string): { note?: string; reasons?: string[] }[] =>
43 app.db
44 .query<{ payload: string }, [string]>("SELECT payload FROM event WHERE kind = ?1 ORDER BY rowid")
45 .all(kind)
46 .map((r) => JSON.parse(r.payload));
47
48 test("an approved pull waiting 30 h declares a clog that began 30 h ago; the cleared note prints the time to detect", () => {
49 expect(tickClog(app.db, app.bus, T0)).toBe("started");
50 const row = app.db.query<{ since: number; started_at: number }, []>("SELECT since, started_at FROM clog_state").get()!;
51 expect(row.since).toBe(T0.getTime());
52 expect(row.started_at).toBe(T0.getTime() - 30 * 3_600_000);
53 expect(events("clog.started")[0]?.reasons?.[0]).toContain("oldest approved pull has waited 30 h");
54
55 // Still waiting: no second event, same stored start.
56 expect(tickClog(app.db, app.bus, after(1))).toBeNull();
57 expect(app.db.query<{ started_at: number }, []>("SELECT started_at FROM clog_state").get()!.started_at).toBe(row.started_at);
58
59 // It lands; two quiet minutes clear it.
60 app.db.run("UPDATE pull SET merged_at = ?1 WHERE issue_id = 'p1'", [after(1).toISOString()]);
61 expect(tickClog(app.db, app.bus, after(2))).toBeNull();
62 expect(tickClog(app.db, app.bus, after(3))).toBe("cleared");
63 expect(app.db.query("SELECT 1 FROM clog_state").get()).toBeNull();
64 const note = events("clog.cleared")[0]?.note ?? "";
65 expect(note).toContain("Cause: oldest approved pull has waited 30 h");
66 expect(note).toContain("Time to detect: 30 h");
67 expect(note).toContain("Time to clear: 3 min");
68 });
test/flow-clog-watch.test.ts +32 −16
@@ -1,40 +1,56 @@
11 import { expect, test } from "bun:test";
2 import { clogNote } from "../src/flow/clog_note.ts";
23 import { clogStep } from "../src/flow/clog_watch.ts";
34
45 test("quiet with no clog stays null", () => {
5 expect(clogStep(null, [], 1)).toEqual({ state: null, event: null });
6 expect(clogStep(null, [], 1, 1)).toEqual({ state: null, event: null });
67 });
78
89 test("a reason starts a clog once and it stands without repeats", () => {
9 const a = clogStep(null, ["red main"], 10);
10 const a = clogStep(null, ["red main"], 10, 10);
1011 expect(a.event).toBe("started");
11 expect(a.state).toEqual({ since: 10, firstReasons: ["red main"] });
12 const b = clogStep(a.state, ["red main", "stuck"], 20);
12 expect(a.state).toEqual({ since: 10, startedAt: 10, firstReasons: ["red main"] });
13 const b = clogStep(a.state, ["red main", "stuck"], 20, 20);
1314 expect(b.event).toBeNull();
14 expect(b.state).toEqual({ since: 10, firstReasons: ["red main"] });
15 expect(b.state).toEqual({ since: 10, startedAt: 10, firstReasons: ["red main"] });
1516 });
1617
1718 test("one quiet reading is still standing; a reason resets the count", () => {
18 const a = clogStep(null, ["x"], 10);
19 const b = clogStep(a.state, [], 20);
19 const a = clogStep(null, ["x"], 10, 10);
20 const b = clogStep(a.state, [], 20, 20);
2021 expect(b.event).toBeNull();
2122 expect(b.state?.since).toBe(10);
22 const c = clogStep(b.state, ["x"], 30);
23 const d = clogStep(c.state, [], 40);
23 const c = clogStep(b.state, ["x"], 30, 30);
24 const d = clogStep(c.state, [], 40, 40);
2425 expect(d.event).toBeNull();
2526 expect(d.state).not.toBeNull();
2627 });
2728
2829 test("two quiet readings in a row clear it with the start time", () => {
29 let s = clogStep(null, ["x"], 10).state;
30 s = clogStep(s, [], 20).state;
31 const c = clogStep(s, [], 30);
32 expect(c).toEqual({ state: null, event: "cleared", since: 10 });
30 let s = clogStep(null, ["x"], 10, 10).state;
31 s = clogStep(s, [], 20, 20).state;
32 const c = clogStep(s, [], 30, 30);
33 expect(c).toEqual({ state: null, event: "cleared", since: 10, startedAt: 10, reasons: ["x"] });
3334 });
3435
3536 test("a clog after a clear starts anew", () => {
36 const c = clogStep(clogStep(clogStep(null, ["x"], 10).state, [], 20).state, [], 30);
37 const r = clogStep(c.state, ["y"], 50);
37 const c = clogStep(clogStep(clogStep(null, ["x"], 10, 10).state, [], 20, 20).state, [], 30, 30);
38 const r = clogStep(c.state, ["y"], 50, 50);
3839 expect(r.event).toBe("started");
39 expect(r.state).toEqual({ since: 50, firstReasons: ["y"] });
40 expect(r.state).toEqual({ since: 50, startedAt: 50, firstReasons: ["y"] });
41 });
42
43 test("a real start rides the state to the clear and into the note", () => {
44 const a = clogStep(null, ["x"], 600_000, -300_000);
45 expect(a.state).toEqual({ since: 600_000, startedAt: -300_000, firstReasons: ["x"] });
46 const b = clogStep(clogStep(a.state, ["x"], 660_000, 660_000).state, [], 720_000, 720_000);
47 const c = clogStep(b.state, [], 780_000, 780_000);
48 expect(c.startedAt).toBe(-300_000);
49 const note = clogNote({ reasons: ["x"], startedAt: c.startedAt!, detectedAt: c.since!, clearedAt: 780_000 });
50 expect(note).toContain("Time to detect: 15 min");
51 });
52
53 test("a start after now, or not a number, falls back to now", () => {
54 expect(clogStep(null, ["x"], 10, 99).state?.startedAt).toBe(10);
55 expect(clogStep(null, ["x"], 10, NaN).state?.startedAt).toBe(10);
4056 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs