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

A whole repository has been stuck for 12 days: 38 open pulls, 37 red, the engine idle behind them, and a re-check does nothing visible #12620

Merged opened this in answer to

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

Merged

mason/forge:mason/12611-a-whole-repository-has-been-stuck-for-12 → main 11 commits fork 1 thread · 1 unresolved

  • Merged. mason/12611-a-whole-repository-has-been-stuck-for-12 was merged into main as e182afa.

Review

Sign in to review this change.
20 files changed +972 −24
docs/MCP.md +1 −1
@@ -115,7 +115,7 @@ bytes in `tools/list`. After changing a tool run `bun scripts/mcp-table.ts`;
115115 | `flow` | `bugs`, `cpu`, `drift`, `growth` | Pipeline (admin); `q`: `/fleet` rows, `action` plans an op, `apply` does it; `kind:bugs`; `repo`: why stuck | 530 |
116116 | `follow_mode` | — | How a fork follows its parent: take updates, tell me, or don't follow | 281 |
117117 | `following` | — | Where a fork stands against what it follows: in step, behind, ahead, diverged | 243 |
118 | `forge` | — | How is my forge? What needs you, recommended; `apply` takes them | 199 |
118 | `forge` | — | How is my forge? What needs you, recommended; `apply` takes them | 291 |
119119 | `forge_proposals` | — | What this forge proposed to its parent; `reachable` false if it is silent | 245 |
120120 | `forge_view` | — | The forge as one picture: counts, apps, runs with stations, health | 282 |
121121 | `forges` | `refused` | Forges sent from here (admin; a stage says when to resend), or one forge's plan to read or grant | 629 |
docs/features/README.md +1
@@ -163,6 +163,7 @@ Read with the guide page [Ship and undo](../guide-ship-and-undo.md).
163163 - [Pin screenshots](pin-screenshots.md)
164164 - [Pulls untouched for 14 days](stale-pulls.md)
165165 - [Ranked checks](ranked-checks.md)
166 - [A repository stuck behind its pulls](stuck-pile.md)
166167 - [Release heartbeat and the main-ahead alarm](release-heartbeat.md)
167168 - [Release keeper](release-keeper.md)
168169 - [Release what the Later gate holds](release-later.md)
docs/features/stuck-pile.md added +8
@@ -0,0 +1,8 @@
1 # A repository stuck behind its pulls (#12611)
2
3 A repository is **stuck** when it has at least three open pulls and every one is red (its latest check on its head failed) or untouched for three days. Its ready issues cannot start while those pulls hold them (`open_pull`), and nothing says so.
4
5 - **Needs stack.** `flow/stuck_pile.ts` finds it; `actions/needs.ts` raises it once, by name, for whoever writes to the repository: `/asks`-style stack, the terminal's "How is my forge?" (`r` re-checks all, `h` hands all to builders, `c` closes all after asking for a reason; each shows its plan, a dry run, and acts only on a yes) and the MCP `forge` tool.
6 - **Home.** One lane of its own above the lanes (`pileRegion` in `web/pages/home_flow.ts`): one sentence, then a row per pull with days old, days untouched, red, and why no builder is on it (a person holds or is assigned it, a person opened it, it is approved, its check has not finished or never ran).
7 - **Choices** (`crew/stuck_pile.ts`, a person's word only, a writer's access): re-check all on today's main (at most 50 a call, the check queue's own order), hand all to the builder that opened each, or close all with a reason (their issues go back to ready, unassigned). Web: `POST /:owner/:name/stuck` with `choice` (`recheck`, `builders`, `close`) and `reason`, then back to the page the form was on (`next`: the home or the repository's pulls). MCP: `forge {apply: "stuck:<repo id>", choice, reason?, dryRun?}`, planned first unless `dryRun: false`.
8 - **A re-check says where it stands.** `POST …/pulls/:n/checks` (page and API) answers `Check queued at the current head: place 3 of 12.`, and the pull's checks line shows the place while it waits (`checks/place.ts`, in the order checks were asked; a rebase's check or a pull a red main holds may be passed over). When nothing can be queued the refusal names why: no head, owner suspended, a guest's fork, a head awaiting a writer's approval, or no declared check.
scripts/unused-exports-allow.json +1
@@ -61,6 +61,7 @@
6161 "src/core/webhooks.ts deliveryBusy",
6262 "src/crew/agent_runner.ts useRunner",
6363 "src/crew/ceiling_stagger.ts forgetStagger",
64 "src/flow/stuck_pile.ts forgetStuckRepos",
6465 "src/crew/conversation_confirm.ts CONFIRM_TTL_MS",
6566 "src/crew/first_look.ts useLookModel",
6667 "src/crew/hot_files.ts rememberPullFiles",
src/actions/needs.ts +34 −1
@@ -15,6 +15,7 @@ import { newestAgentRun } from "../core/runs.ts";
1515 import { STALE_DAYS } from "../web/studio/review_set.ts";
1616 import { askStackFor, type askLinesFor } from "../web/studio/asks.ts";
1717 import { weekOld } from "../flow/oldest_approved.ts";
18 import { pileLine, stuckReposCached } from "../flow/stuck_pile.ts";
1819 import { drillNow } from "./backups_now.ts";
1920 import { builderOf, giveBack, handBack } from "./give_back.ts";
2021 import { getIssueById } from "../core/issues.ts";
@@ -223,6 +224,38 @@ function approvedNeeds(ctx: Home): Need[] {
223224 );
224225 }
225226
227 /** The three choices on a stuck repository (`crew/stuck_pile.ts`), in the order offered. */
228 export const STUCK_CHOICES = ["Re-check all on today's main", "Hand all to builders", "Close all with a reason"] as const;
229
230 /**
231 * A repository stuck behind its own pulls (#12611), raised once by name for whoever may write
232 * to it. Every choice acts on many pulls and none is safe to take unasked, so each is the
233 * owner's: `forge {apply: "stuck:<id>", choice}` takes one.
234 */
235 function stuckNeeds(ctx: Home, user: User): Need[] {
236 if (user.kind !== "user") return [];
237 const out: Need[] = [];
238 for (const s of stuckReposCached(ctx.app.db)) {
239 const repo = getRepoById(ctx.app.db, s.repoId);
240 if (repo === undefined || !canWrite(ctx.app.db, repo, user)) continue;
241 out.push({
242 id: `stuck:${s.repoId}`,
243 sentence: `${s.ref} is stuck: ${s.open} open pulls, ${s.red} red, ${s.untouched} untouched. ${pileLine()}`,
244 why: "work finished enough to have a pull, parked behind a red nobody is looking at",
245 waits: "nothing is built or landed there until the pile moves",
246 choices: [...STUCK_CHOICES],
247 recommended: needsDecision(
248 `${s.ref}: re-check all, hand all to builders, or close all?`,
249 "each choice moves many pulls; re-checking is the gentlest, closing sets their issues ready again",
250 STUCK_CHOICES,
251 ),
252 link: `/${s.owner}/${s.name}/pulls`,
253 items: [],
254 });
255 }
256 return out;
257 }
258
226259 /** The stack: the forge's own needs first (an admin's), then the asks, most consequential first. */
227260 export function needsStack(ctx: Home, user: User, chips?: Chips): Need[] {
228261 const db = ctx.app.db;
@@ -278,7 +311,7 @@ export function needsStack(ctx: Home, user: User, chips?: Chips): Need[] {
278311 items: [a.repo.id],
279312 }),
280313 );
281 return [...forge, ...(user.isAdmin ? approvedNeeds(ctx) : []), ...rows, ...asleep];
314 return [...forge, ...stuckNeeds(ctx, user), ...(user.isAdmin ? approvedNeeds(ctx) : []), ...rows, ...asleep];
282315 }
283316
284317 /** One or two sentences: serving or not, the crew, what is unusual. */
src/checks/again.ts +27 −8
@@ -9,10 +9,14 @@ import { pullDir, type Pull } from "../core/pulls.ts";
99 import type { Run } from "../core/runs.ts";
1010 import type { User } from "../core/users.ts";
1111 import { resolveOid } from "../git/merge.ts";
12 import { accountActive } from "../core/accounts.ts";
13 import { isContributionFork } from "../core/contribute.ts";
14 import { issueMayRun } from "../core/pull_trust.ts";
15 import { placeWords, queuePlace, type Place } from "./place.ts";
1216 import { checksFor } from "./service.ts";
1317
1418 export type Asked =
15 | { ok: true; run: Run }
19 | { ok: true; run: Run; place: Place | undefined }
1620 | { ok: false; error: "no_checks" | "not_open" | "no_check_declared"; message: string };
1721
1822 export async function askCheck(
@@ -35,11 +39,26 @@ export async function askCheck(
3539 head === ""
3640 ? undefined
3741 : await checks.request(repo, `pull-${pull.number}`, head, "asked", pull.id, user.id);
38 if (run === undefined)
39 return {
40 ok: false,
41 error: "no_check_declared",
42 message: "this head declares no check, or waits for a writer's approval",
43 };
44 return { ok: true, run };
42 if (run === undefined) return { ok: false, error: "no_check_declared", message: whyNotQueued(app, repo, pull, head) };
43 return { ok: true, run, place: queuePlace(app.db, run) };
4544 }
45
46 /** Why a check could not be queued, as far as the forge can tell from what it holds. */
47 export function whyNotQueued(app: App, repo: Repo, pull: Pull, head: string): string {
48 if (head === "") return "this pull has no head commit to check";
49 if (!accountActive(app.db, repo.ownerId)) return "the repository's owner is suspended or removed, so no code of theirs runs";
50 if (isContributionFork(app.db, repo.id)) return "a guest's fork runs nothing; its code runs only as a pull a person approved";
51 if (!issueMayRun(app.db, repo, pull.id, head)) return "this head waits for a writer's approval before any check runs";
52 return "this head declares no check, or the forge is stopping";
53 }
54
55 /**
56 * What a person is told after a check was queued: where it stands in the line;
57 * undefined when there is nothing beyond the plain ask.
58 */
59 export const askedWords = (asked: Extract<Asked, { ok: true }>): string | undefined =>
60 asked.run.state === "queued" && asked.place !== undefined
61 ? `Check queued at the current head: ${placeWords(asked.place)}.`
62 : asked.run.state === "running"
63 ? "Check running at the current head."
64 : undefined;
src/checks/place.ts added +28
@@ -0,0 +1,28 @@
1 /**
2 * Where a queued check stands in the line (#12611): `place` of `of`, in the
3 * order the checks were asked. The forge's own pick can pass one over (a
4 * rebase's check goes first, a red main holds a pull's), so it is the place in
5 * line, not a promise of when. One read of the queued rows; a check that is not
6 * queued has none.
7 */
8
9 import type { Database } from "bun:sqlite";
10 import type { Run } from "../core/runs.ts";
11
12 export interface Place {
13 readonly place: number;
14 readonly of: number;
15 }
16
17 export function queuePlace(db: Database, run: Pick<Run, "id" | "state" | "kind">): Place | undefined {
18 if (run.kind !== "check" || run.state !== "queued") return undefined;
19 const row = db
20 .query<{ before: number; total: number }, [string]>(
21 `SELECT COALESCE(SUM(id <= ?1), 0) AS before, COUNT(*) AS total FROM run WHERE kind = 'check' AND state = 'queued'`,
22 )
23 .get(run.id);
24 return row === null || row.before === 0 ? undefined : { place: row.before, of: row.total };
25 }
26
27 /** `place 3 of 12`, or empty when the check is not waiting in line. */
28 export const placeWords = (p: Place | undefined): string => (p === undefined ? "" : `place ${p.place} of ${p.of}`);
src/contract/openapi/routes/postApiV1ReposOwnerNamePullsNumberChecks.ts +2 −2
@@ -3,11 +3,11 @@ import { REPO, type Route } from "../shared.ts";
33 export const route: Route = {
44 method: "post",
55 path: `${REPO}/pulls/{number}/checks`,
6 summary: "Ask for a check again at the pull's head as it is now (rw scope, a writer's); answers 202 with { run }",
6 summary: "Ask for a check again at the pull's head as it is now (rw scope, a writer's); answers 202 with { run, place, says }",
77 auth: true,
88 role: "member",
99 status: 202,
10 reply: { run: "object" },
10 reply: { run: "object", place: "object", says: "string" },
1111 refusals: {
1212 forbidden: "only a writer can ask for a check (403)",
1313 not_open: "only an open pull is checked (409)",
src/crew/stuck_pile.ts added +115
@@ -0,0 +1,115 @@
1 /**
2 * The three things a person may do about a repository stuck behind its pulls
3 * (#12611, `flow/stuck_pile.ts`): ask every pull's check again on today's
4 * main, hand every pull to the crew member that built it, or close every pull
5 * with a reason, which sets the issues they closed back to ready. Each takes
6 * a person's word (never an agent's), a writer's access, and answers what it
7 * did to each pull and why it left one alone. `dryRun` answers the plan only.
8 */
9
10 import type { App } from "../app.ts";
11 import { addComment, assignIssue, setIssueState } from "../actions/issues.ts";
12 import { builderOf, handBack } from "../actions/give_back.ts";
13 import { askCheck } from "../checks/again.ts";
14 import { RECHECK_MAX } from "../checks/recheck.ts";
15 import { getIssue, getIssueById } from "../core/issues.ts";
16 import { closesFromBody, getPull, type Pull } from "../core/pulls.ts";
17 import { canWrite, getRepoById } from "../core/repos.ts";
18 import type { User } from "../core/users.ts";
19 import { pileRows, readStuckRepos, whyNoBuilder } from "../flow/stuck_pile.ts";
20
21 export const PILE_CHOICES = ["recheck", "builders", "close"] as const;
22 export type PileChoice = (typeof PILE_CHOICES)[number];
23
24 /** The longest reason taken. */
25 export const REASON_MAX = 300;
26
27 export interface PileDone {
28 readonly choice: PileChoice;
29 readonly dryRun: boolean;
30 /** Pull numbers acted on (or that would be). */
31 readonly acted: number[];
32 /** Pull numbers left, each with why. */
33 readonly left: { pull: number; why: string }[];
34 }
35
36 export const isPileChoice = (v: unknown): v is PileChoice => PILE_CHOICES.includes(v as PileChoice);
37
38 /** The default reason a close carries. */
39 export const closeReason = (by: string): string =>
40 `Closed from the stuck pile by @${by}: every open pull here was red or untouched, and none was landing. Its issue is ready again.`;
41
42 export async function takePileChoice(
43 app: App,
44 user: User,
45 repoId: string,
46 choice: PileChoice,
47 opts: { dryRun?: boolean; reason?: string } = {},
48 ): Promise<PileDone | { error: string }> {
49 const dryRun = opts.dryRun === true;
50 const repo = getRepoById(app.db, repoId);
51 if (repo === undefined || user.kind !== "user" || !canWrite(app.db, repo, user)) return { error: "not found" };
52 if (!readStuckRepos(app.db).some((s) => s.repoId === repo.id)) return { error: "that repository is not stuck any more" };
53 const reason = (opts.reason ?? "").trim().slice(0, REASON_MAX) || closeReason(user.username);
54 const acted: number[] = [];
55 const left: { pull: number; why: string }[] = [];
56 const open = pileRows(app.db).filter((r) => r.repo_id === repo.id);
57 for (const r of open.sort((a, b) => a.number - b.number)) {
58 const issue = getIssueById(app.db, r.id);
59 const pull: Pull | undefined = getPull(app.db, repo.id, r.number);
60 if (issue === undefined || pull === undefined || issue.state !== "open") continue;
61 if (choice === "recheck") {
62 if (acted.length >= RECHECK_MAX) {
63 left.push({ pull: r.number, why: `past ${RECHECK_MAX} in one call; ask again` });
64 continue;
65 }
66 if (dryRun) acted.push(r.number);
67 else {
68 const asked = await askCheck(app, user, repo, pull);
69 if (asked.ok) acted.push(r.number);
70 else left.push({ pull: r.number, why: asked.message });
71 }
72 } else if (choice === "builders") {
73 const builder = builderOf(app.db, repo, issue);
74 const why = whyNoBuilder(r);
75 if (builder === undefined) left.push({ pull: r.number, why: "no crew member opened it" });
76 else if (why !== "") left.push({ pull: r.number, why });
77 else if (dryRun) acted.push(r.number);
78 else {
79 const given = handBack(app, user, repo, issue, builder, `Given back to the crew by @${user.username}: the repository's pulls were stuck.`);
80 if (given.ok) acted.push(r.number);
81 else left.push({ pull: r.number, why: given.error.message });
82 }
83 } else if (dryRun) acted.push(r.number);
84 else {
85 addComment(app, user, repo, issue, reason);
86 if (!setIssueState(app, user, repo, issue, "closed", { reason: "parked", stale: true }).ok) {
87 left.push({ pull: r.number, why: "it could not be closed" });
88 continue;
89 }
90 // The issues it was for go back to ready.
91 for (const n of closesFromBody(pull.body)) {
92 const about = getIssue(app.db, repo.id, n);
93 if (about?.state === "open" && about.assigneeId !== null) assignIssue(app, user, repo, about, null);
94 }
95 acted.push(r.number);
96 }
97 }
98 return { choice, dryRun, acted, left };
99 }
100
101 /** What a person is told after a choice: what was done to how many pulls, and what was left. */
102 export function pileWords(done: PileDone): string {
103 const verb = { recheck: "Check asked for", builders: "Handed to the crew:", close: "Closed:" }[done.choice];
104 const left = done.left.length === 0 ? "" : ` ${done.left.length} left alone (${done.left[0]!.why}${done.left.length > 1 ? ", and others" : ""}).`;
105 return `${verb} ${done.acted.length} ${done.acted.length === 1 ? "pull" : "pulls"}.${left}`;
106 }
107
108 /** What a dry run says it would do, one line, then the first pulls it would leave and why. */
109 export function pilePlan(done: PileDone): string[] {
110 const verb = { recheck: "Ask the check again on", builders: "Hand to the crew", close: "Close" }[done.choice];
111 const lines = [`${verb} ${done.acted.length} ${done.acted.length === 1 ? "pull" : "pulls"}.`];
112 for (const l of done.left.slice(0, 3)) lines.push(`Leave #${l.pull}: ${l.why}.`);
113 if (done.left.length > 3) lines.push(`${done.left.length - 3} more left alone.`);
114 return lines;
115 }
src/flow/stuck_pile.ts added +157
@@ -0,0 +1,157 @@
1 /**
2 * A repository stuck behind its own pulls (#12611): every open pull is red or
3 * has not been touched for `UNTOUCHED_DAYS`, and there are at least `PILE_MIN`
4 * of them. Nothing in it moves by itself: the ready issues are held by these
5 * pulls (`open_pull`), the reds wait for a builder or a person, and no one is
6 * told. One read names each such repository, its pulls oldest first with
7 * their ages, and why each sits. The needs stack raises it once
8 * (`actions/needs.ts`), the home shows it as a pile of its own
9 * (`pileRegion` in `web/pages/home_flow.ts`), and `crew/stuck_pile.ts` takes the three choices.
outdated

Names web/pages/home_pile.ts, which does not exist; the lane is pileRegion in web/pages/home_flow.ts. Fix the pointer.

10 */
11
12 import type { Database } from "bun:sqlite";
13
14 /** The fewest open pulls that make a pile. */
15 export const PILE_MIN = 3;
16 /** A pull no one has touched for this long counts as sitting. */
17 export const UNTOUCHED_DAYS = 3;
18 /** The most pulls a pile names; the rest are counted. */
19 export const PILE_LISTED = 50;
20
21 const DAY_MS = 86_400_000;
22
23 export interface PilePull {
24 readonly id: string;
25 readonly number: number;
26 readonly title: string;
27 readonly red: boolean;
28 /** Days since anything touched it: an edit, a comment, or a run on it. */
29 readonly idleDays: number;
30 /** Days since it was opened. */
31 readonly ageDays: number;
32 /** Why no builder is on it, in words; empty when one may be sent. */
33 readonly why: string;
34 }
35
36 export interface StuckRepo {
37 readonly repoId: string;
38 readonly owner: string;
39 readonly name: string;
40 /** `owner/name`. */
41 readonly ref: string;
42 readonly open: number;
43 readonly red: number;
44 /** Open pulls untouched past `UNTOUCHED_DAYS`. */
45 readonly untouched: number;
46 readonly oldestDays: number;
47 /** Oldest first, at most `PILE_LISTED`. */
48 readonly pulls: readonly PilePull[];
49 }
50
51 export interface PileRow {
52 id: string;
53 number: number;
54 title: string;
55 repo_id: string;
56 owner: string;
57 name: string;
58 created_at: string;
59 touched: string;
60 last_check: string | null;
61 held: number;
62 person: number;
63 agent: number;
64 approved: number;
65 }
66
67 /** Walks `issue_open_pull_age`'s open pulls, then the run index of each. */
68 export const PILE_SQL = `SELECT i.id AS id, i.number AS number, i.title AS title, i.repo_id AS repo_id,
69 o.username AS owner, r.name AS name, i.created_at AS created_at,
70 MAX(i.updated_at, COALESCE((SELECT MAX(COALESCE(t.finished_at, t.created_at)) FROM run t
71 WHERE t.issue_id = i.id AND t.kind IN ('agent','check','deploy')), '')) AS touched,
72 (SELECT c.state FROM run c WHERE c.kind = 'check' AND c.issue_id = i.id AND c.commit_oid = p.head_oid
73 ORDER BY c.id DESC LIMIT 1) AS last_check,
74 EXISTS (SELECT 1 FROM pull_hold h WHERE h.issue_id = i.id AND h.released_at IS NULL) AS held,
75 EXISTS (SELECT 1 FROM user u WHERE u.id = i.assignee_id AND u.is_agent = 0) AS person,
76 COALESCE((SELECT a.is_agent FROM user a WHERE a.id = i.author_id), 0) AS agent,
77 EXISTS (SELECT 1 FROM review v WHERE v.issue_id = i.id AND v.state = 'approved') AS approved
78 FROM issue i JOIN pull p ON p.issue_id = i.id
79 JOIN repo r ON r.id = i.repo_id JOIN user o ON o.id = r.owner_id
80 WHERE i.state = 'open' AND i.kind = 'pull' AND p.merged_at IS NULL`;
81
82 /**
83 * Why a builder is not sent to this pull, from what is on the row; empty when one may be.
84 * A red check is no reason: the builder is sent to fix it.
85 */
86 export function whyNoBuilder(r: Pick<PileRow, "held" | "person" | "agent" | "approved" | "last_check">): string {
87 if (r.held === 1) return "a person holds it";
88 if (r.person === 1) return "a person is assigned to it";
89 if (r.agent !== 1) return "a person opened it, not a crew member";
90 if (r.approved === 1) return "it is approved and waits for a person to land it";
91 if (r.last_check === "queued" || r.last_check === "running") return "its check has not finished";
92 if (r.last_check === null) return "it has no check on its current head";
93 return "";
94 }
95
96 /** Every open pull on the forge, one row each. */
97 export const pileRows = (db: Database): PileRow[] => db.query<PileRow, []>(PILE_SQL).all();
98
99 const days = (now: number, iso: string): number => Math.max(0, Math.floor((now - Date.parse(iso)) / DAY_MS));
100
101 /** Every repository whose open pulls are all red or untouched, the longest-stuck first. */
102 export function readStuckRepos(db: Database, now = new Date()): StuckRepo[] {
103 const at = now.getTime();
104 const byRepo = new Map<string, PileRow[]>();
105 for (const row of pileRows(db)) {
106 const rows = byRepo.get(row.repo_id) ?? [];
107 rows.push(row);
108 byRepo.set(row.repo_id, rows);
109 }
110 const out: StuckRepo[] = [];
111 for (const [repoId, rows] of byRepo) {
112 if (rows.length < PILE_MIN) continue;
113 const pulls: PilePull[] = rows.map((r) => ({
114 id: r.id,
115 number: r.number,
116 title: r.title,
117 red: r.last_check === "failed",
118 idleDays: days(at, r.touched),
119 ageDays: days(at, r.created_at),
120 why: whyNoBuilder(r),
121 }));
122 // Every pull sits: red, or untouched past the window. One moving pull and it is ordinary work.
123 if (!pulls.every((p) => p.red || p.idleDays >= UNTOUCHED_DAYS)) continue;
124 pulls.sort((a, b) => b.idleDays - a.idleDays || a.number - b.number);
125 out.push({
126 repoId,
127 owner: rows[0]!.owner,
128 name: rows[0]!.name,
129 ref: `${rows[0]!.owner}/${rows[0]!.name}`,
130 open: pulls.length,
131 red: pulls.filter((p) => p.red).length,
132 untouched: pulls.filter((p) => p.idleDays >= UNTOUCHED_DAYS).length,
133 oldestDays: Math.max(...pulls.map((p) => p.ageDays)),
134 pulls: pulls.slice(0, PILE_LISTED),
135 });
136 }
137 return out.sort((a, b) => b.oldestDays - a.oldestDays || a.ref.localeCompare(b.ref));
138 }
139
140 /** The one sentence under the heading: what the pile costs. The counts are the lane's own. */
141 export const pileLine = (): string => "Every open pull is red or untouched, so its ready issues cannot start.";
142
143 /** How long one read serves every request. */
144 export const PILE_TTL_MS = 30_000;
145 const kept = new WeakMap<Database, { at: number; value: StuckRepo[] }>();
146
147 /** `readStuckRepos` for a page: one read serves every request in 30 seconds. */
148 export function stuckReposCached(db: Database, now = new Date()): StuckRepo[] {
149 const hit = kept.get(db);
150 if (hit !== undefined && now.getTime() >= hit.at && now.getTime() - hit.at < PILE_TTL_MS) return hit.value;
151 const value = readStuckRepos(db, now);
152 kept.set(db, { at: now.getTime(), value });
153 return value;
154 }
155
156 /** Drops the kept read, so the next one sees what changed (a test that changes a pull). */
157 export const forgetStuckRepos = (db: Database): void => void kept.delete(db);
src/http/api.ts +3 −2
@@ -8,6 +8,7 @@
88 * back to continue); errors are `{ error, message }`.
99 */
1010
11 import { CHECK_ASKED } from "../actions/pull_ask_words.ts";
1112 import { liveOf } from "../perf/live.ts";
1213 import { reviewWords } from "../actions/review_words.ts";
1314 import { growth, MAX_DAYS as MAX_GROWTH_DAYS } from "../core/growth.ts";
@@ -35,7 +36,7 @@ import { recordDrill } from "../core/domain_drills.ts";
3536 import { drillVerdict } from "../kernel/domain_drill.ts";
3637 import { DOMAINS } from "../kernel/domains.ts";
3738 import { readBounded } from "./limits.ts";
38 import { askCheck } from "../checks/again.ts";
39 import { askCheck, askedWords } from "../checks/again.ts";
3940 import { nextUp, queueJson } from "../core/next_up.ts";
4041 import { notStartable } from "../crew/not_startable.ts";
4142 import { mayView, mayViewId, sealedScope } from "../core/sealed.ts";
@@ -4359,7 +4360,7 @@ export function apiRoutes(router: Router<Handler>): void {
43594360 return apiError("forbidden", 403, "only a writer can ask for a check");
43604361 const asked = await askCheck(ctx.app, ctx.auth.user, found.repo, found.pull);
43614362 if (!asked.ok) return apiError(asked.error, 409, asked.message);
4362 return json({ run: asked.run }, 202);
4363 return json({ run: asked.run, place: asked.place ?? null, says: askedWords(asked) ?? CHECK_ASKED }, 202);
43634364 },
43644365 );
43654366
src/http/mcp/tools/forge.ts +12 −1
@@ -1,4 +1,5 @@
11 import { crewNow } from "../../../actions/crew_now.ts";
2 import { takePileChoice, isPileChoice, PILE_CHOICES } from "../../../crew/stuck_pile.ts";
23 import { applyRecommended, forgeSays, needsStack } from "../../../actions/needs.ts";
34 import { good, bad, type Tool } from "../shared.ts";
45
@@ -9,16 +10,26 @@ export const tool: Tool = {
910 type: "object",
1011 properties: {
1112 apply: { type: "string" },
13 choice: { type: "string", enum: [...PILE_CHOICES] },
14 reason: { type: "string" },
1215 dryRun: { type: "boolean" },
1316 },
1417 },
1518 doc:
16 "`{ apply?, dryRun? }`: `{ says, needs, recommended }`: one or two sentences, then what needs you (the home page's own stack, most consequential first: `sentence`, `why`, `choices`, `recommended` with `reason`, `undo`, `safe`, and the `ask` to put to the owner when not), and the call that applies them. `apply: \"recommended\"` (or one need's `id`) takes the recommended choice of each safe need (a close the crew advised, a restore drill, a stopped run or assigned issue handed to the crew) and answers `done` with what each did; a ship is never one (a merge has no undo), nor a need with no safe default: those say `Needs your decision` with their real `options`, and are listed under `left` by name; planned first (`dryRun` is true unless `false`); each receipt names its undo (`asks_act` `undo`)",
19 "`{ apply?, dryRun? }`: `{ says, needs, recommended }`: one or two sentences, then what needs you (the home page's own stack, most consequential first: `sentence`, `why`, `choices`, `recommended` with `reason`, `undo`, `safe`, and the `ask` to put to the owner when not), and the call that applies them. A stuck repository (every open pull red or untouched) is a need named `stuck:<id>`: `{ apply: \"stuck:<id>\", choice: \"recheck\" | \"builders\" | \"close\", reason? }` takes one choice over all its pulls (planned first, `dryRun` true unless `false`), answering `acted` and `left` with why. `apply: \"recommended\"` (or one need's `id`) takes the recommended choice of each safe need (a close the crew advised, a restore drill, a stopped run or assigned issue handed to the crew) and answers `done` with what each did; a ship is never one (a merge has no undo), nor a need with no safe default: those say `Needs your decision` with their real `options`, and are listed under `left` by name; planned first (`dryRun` is true unless `false`); each receipt names its undo (`asks_act` `undo`)",
1720 writes: true,
1821 dryByDefault: true,
1922 readsWithout: "apply",
2023 async run(ctx, user, args) {
2124 const apply = args["apply"];
25 const choice = args["choice"];
26 if (choice !== undefined) {
27 if (typeof apply !== "string" || !apply.startsWith("stuck:") || !isPileChoice(choice))
28 return bad("invalid", "choice is recheck, builders or close, and apply is the stuck need's id");
29 const reason = typeof args["reason"] === "string" ? args["reason"] : undefined;
30 const done = await takePileChoice(ctx.app, user, apply.slice(6), choice, { dryRun: args["dryRun"] !== false, ...(reason === undefined ? {} : { reason }) });
31 return "error" in done ? bad("not_found", done.error) : good(done);
32 }
2233 if (apply !== undefined) {
2334 if (typeof apply !== "string" || apply === "") return bad("invalid", "apply is \"recommended\" or a need's id");
2435 return good(await applyRecommended(ctx, user, args["dryRun"] !== false, apply === "recommended" ? undefined : apply));
src/http/server.ts +2
@@ -420,6 +420,7 @@ import {
420420 pullSliceSubmit,
421421 pullLandPastSubmit,
422422 pullChecksSubmit,
423 stuckSubmit,
423424 pullReviewStateSubmit,
424425 pullApproveHereSubmit,
425426 pullReviewSubmit,
@@ -1353,6 +1354,7 @@ export function buildRouter(): Router<Handler> {
13531354 // A like from a card without a script (#7751): the feed's `liked`, then back to the feed.
13541355 router.add("POST", "/feed/like", likeSubmit);
13551356 router.add("POST", "/:owner/:name/pulls/:number/checks", pullChecksSubmit);
1357 router.add("POST", "/:owner/:name/stuck", stuckSubmit);
13561358 router.add("POST", "/:owner/:name/pulls/:number/ship", shipSubmit);
13571359 router.add("POST", "/:owner/:name/pulls/:number/update", updateSubmit);
13581360 router.add("POST", "/:owner/:name/pulls/:number/say", saySubmit);
src/tui/screens/needs.ts +48 −4
@@ -12,12 +12,13 @@
1212 import { crewNow } from "../../actions/crew_now.ts";
1313 import { applyRecommended, forgeSays, needsStack, type Need } from "../../actions/needs.ts";
1414 import type { Ctx, Screen } from "../app/app.ts";
15 import { pilePlan, pileWords, takePileChoice, type PileChoice } from "../../crew/stuck_pile.ts";
1516 import { binding } from "../app/keys.ts";
1617 import type { Session } from "../session.ts";
1718 import { style } from "../term/style.ts";
1819 import { truncate } from "../term/width.ts";
1920 import { fill, len, split } from "../ui/layout.ts";
20 import { ListState, drawList, live, runAsync, type ListRow } from "./common.ts";
21 import { ListState, confirmScreen, drawList, live, promptScreen, runAsync, type ListRow } from "./common.ts";
2122 import type { Command } from "../app/palette.ts";
2223
2324 /** The stack this session's user may see; none when nobody is signed in. */
@@ -47,9 +48,33 @@ export function needsScreen(f: Session, ctx: Ctx): Screen {
4748 running = runningFor(f);
4849 ctx.invalidate();
4950 };
50 return {
51 /** The plan of a choice (a dry run), as a confirm; yes takes it. */
52 const planning = (at: Need, choice: PileChoice, reason = ""): void =>
53 runAsync(ctx, async () => {
54 const repoId = at.id.slice(6);
55 const opts = reason === "" ? {} : { reason };
56 const plan = await takePileChoice(f.app, f.user!, repoId, choice, { ...opts, dryRun: true });
57 if ("error" in plan) return ctx.toast(plan.error, "danger");
58 ctx.push(
59 confirmScreen(ctx, screen, {
60 title: "Take this choice?",
61 body: [at.sentence, ...pilePlan(plan)],
62 actions: [{ label: "Do it" }, { label: "Cancel", variant: "ghost" }],
63 width: 72,
64 onChoose: (i) => {
65 if (i !== 0) return;
66 runAsync(ctx, async () => {
67 const done = await takePileChoice(f.app, f.user!, repoId, choice, opts);
68 ctx.toast("error" in done ? done.error : pileWords(done), "error" in done ? "danger" : undefined);
69 reload();
70 });
71 },
72 }),
73 );
74 });
75 const screen: Screen = {
5176 title: "How is my forge?",
52 keys: [binding(["j", "k"], "move cursor"), binding(["a"], "take the recommended choice")],
77 keys: [binding(["j", "k"], "move cursor"), binding(["a"], "take the recommended choice"), binding(["r"], "stuck repository: re-check all"), binding(["h"], "stuck repository: hand all to builders"), binding(["c"], "stuck repository: close all")],
5378 async load(signal) {
5479 live(f, signal, {}, reload);
5580 },
@@ -68,8 +93,26 @@ export function needsScreen(f: Session, ctx: Ctx): Screen {
6893 },
6994 key(e) {
7095 if (state.key(e, stack.length)) return true;
71 if (e.ctrl || e.alt || e.key !== "a") return false;
96 if (e.ctrl || e.alt) return false;
7297 const at = stack[state.index];
98 // A stuck repository's choices over all its pulls: each is planned first and asked.
99 const pick = { r: "recheck", h: "builders", c: "close" }[e.key] as PileChoice | undefined;
100 if (pick !== undefined && at?.id.startsWith("stuck:") && f.user !== undefined) {
101 if (pick !== "close") planning(at, pick);
102 else
103 ctx.push(
104 promptScreen(ctx, screen, {
105 title: "Close all pulls",
106 label: "Reason, said on each closed pull (empty for the usual one)",
107 onSubmit: (reason) => {
108 planning(at, "close", reason);
109 return undefined;
110 },
111 }),
112 );
113 return true;
114 }
115 if (e.key !== "a") return false;
73116 const why = whyNot(f, at);
74117 if (why !== "" || at === undefined || f.user === undefined) {
75118 ctx.toast(why, "danger");
@@ -85,6 +128,7 @@ export function needsScreen(f: Session, ctx: Ctx): Screen {
85128 return true;
86129 },
87130 };
131 return screen;
88132 }
89133
90134 /** Offered to anyone signed in: the stack is their own. */
src/web/pages/home_flow.ts +43 −1
@@ -58,7 +58,11 @@ import { readableNotes } from "../../actions/desk.ts";
5858 import { deskStrip } from "../desk_strip.ts";
5959 import { heldLine } from "../studio/held.ts";
6060 import { interruptedOutLine } from "../studio/interrupted_out.ts";
61 import { listReposWritableTo } from "../../core/repos.ts";
61 import { canWrite, getRepoById, listReposWritableTo } from "../../core/repos.ts";
62 import type { Database } from "bun:sqlite";
63 import type { User } from "../../core/users.ts";
64 import { pileLine, stuckReposCached } from "../../flow/stuck_pile.ts";
65 import { btn, field } from "../ui.ts";
6266 import { chart, figuresGrid, figuresSection } from "./home_figures.ts";
6367 import { tellBox } from "../studio/living.ts";
6468 import { requestsCard } from "../requests_card.ts";
@@ -210,6 +214,43 @@ export function laneRegion(
210214 }
211215
212216
217 /**
218 * A repository stuck behind its own pulls (#12611): one lane of its own, not counted in the
219 * lanes above. A heading and a count, one sentence of what it costs, a row per pull, and the three
220 * choices over all of them. Only for repositories this person writes to.
221 */
222 export function pileRegion(db: Database, user: User, now: number): Html | null {
223 const piles = stuckReposCached(db, new Date(now)).filter((s) => {
224 const repo = getRepoById(db, s.repoId);
225 return repo !== undefined && canWrite(db, repo, user);
226 });
227 if (piles.length === 0) return null;
228 const sections = piles.map((s) => {
229 const base = `/${s.owner}/${s.name}`;
230 const rows = s.pulls.map((p) => {
231 const facts = [`${String(p.ageDays)} d old`, `untouched ${String(p.idleDays)} d`, ...(p.red ? ["red"] : []), ...(p.why === "" ? [] : [p.why])];
232 return html`<li><a href="${base}/pulls/${String(p.number)}">#${String(p.number)} ${p.title}</a> <span class="muted small">${facts.join(", ")}</span></li>`;
233 });
234 return html`<section class="lane" data-lane="pile" data-tone="stuck" data-count="${String(s.open)}" aria-label="${s.ref} is stuck">
235 <header class="lane-head">
236 <h2 class="lane-name"><a class="lane-name-link" href="${base}/pulls">${s.ref} is stuck</a></h2>
237 <span class="lane-count num" aria-hidden="true">${String(s.open)}</span>
238 </header>
239 <p class="lane-health">${pileLine()}</p>
240 <form class="row-fields" method="post" action="${base}/stuck">
241 <input type="hidden" name="next" value="/" />
242 ${field({ name: "reason", label: "Reason, said on each closed pull", maxlength: 300, id: `pile-reason-${s.name}` })}
243 ${btn("Re-check all on today's main", { size: "sm", name: "choice", value: "recheck" })}
244 ${btn("Hand all to builders", { size: "sm", name: "choice", value: "builders" })}
245 ${btn("Close all", { size: "sm", name: "choice", value: "close" })}
246 </form>
247 <ul class="list setting-list">${rows}</ul>
248 ${s.open > s.pulls.length ? html`<p class="muted small">${String(s.open - s.pulls.length)} more not listed</p>` : null}
249 </section>`;
250 });
251 return html`<div id="hf-pile">${sections}</div>`;
252 }
253
213254 /** What the lanes' reading needs of the forge. */
214255 const pictureOptions = (app: App): PictureOptions => ({ dataDir: app.config.dataDir, ceilingUsd: forgeCeiling(app.config) });
215256 const optionsOf = (ctx: Ctx, user: { readonly id: string } | undefined = ctx.auth.user): PictureOptions => ({ ...pictureOptions(ctx.app), ...(user === undefined ? {} : { viewer: user.id }) });
@@ -564,6 +605,7 @@ export async function homeFlowPage(ctx: Ctx, known?: { readonly spawned: Awaited
564605 ${deskStrip(ctx.app.db, readableNotes(ctx.app, ctx.auth.user))}
565606 ${stopped}
566607 ${after}
608 ${pileRegion(ctx.app.db, user, now)}
567609 ${main}
568610 ${wide ? figuresSection(picture, now) : null}
569611 ${forges}
src/web/pages/pull.ts +33 −3
@@ -11,7 +11,9 @@
1111 import { returnTo, withReturn } from "../return_to.ts";
1212 import { pullRoster } from "../studio/pull_roster.ts";
1313 import { originCheckLead, originCheckOf } from "../../origins/pull-check.ts";
14 import { askCheck } from "../../checks/again.ts";
14 import { askCheck, askedWords } from "../../checks/again.ts";
15 import { isPileChoice, pileWords, takePileChoice } from "../../crew/stuck_pile.ts";
16 import { placeWords, queuePlace } from "../../checks/place.ts";
1517 import { addCommentThrough } from "../../actions/issues.ts";
1618 import { COMMENT_EMPTY, COMMENT_NOT_POSTED } from "../../actions/comment_words.ts";
1719 import type { AccessError } from "../../actions/access.ts";
@@ -69,6 +71,7 @@ import { wallOf } from "../../contract/policy.ts";
6971 import type { Ctx } from "../../http/context.ts";
7072 import {
7173 crumbsFor,
74 loadRepo,
7275 notFoundPage,
7376 repoBase,
7477 repoHeader,
@@ -316,6 +319,8 @@ export function checksLine(
316319 progress = "",
317320 /** Where this line answers for itself (`data-live-src`); empty for none. */
318321 liveSrc = "",
322 /** `placeWords` of a queued check; empty when it is not waiting in line. */
323 place = "",
319324 ): Html {
320325 if (!isApp && !required && check.state === "none" && !waiting && again === "")
321326 return html``;
@@ -351,7 +356,7 @@ export function checksLine(
351356 : check.state === "queued"
352357 ? html`<span class="now"
353358 >${icon("loader")}Check
354 ${run ? queuedPhrase(run.createdAt) : "queued"}</span
359 ${run ? queuedPhrase(run.createdAt) : "queued"}${place === "" ? "" : `, ${place}`}</span
355360 >${link}`
356361 : check.state === "running"
357362 ? html`<span class="now">${icon("loader")}${words ?? "Check running"}</span
@@ -498,6 +503,7 @@ function pullChecksLine(
498503 stale,
499504 progress,
500505 `${thread}/checks-line`,
506 placeWords(check.run === null ? undefined : queuePlace(ctx.app.db, check.run)),
501507 );
502508 const found = !isApp ? null : pullPreviewFindings(ctx.app.db, repo.id, previewEnv(pull.number));
503509 const red = redAloneLine(redMark(repo.id, pull.id, status.headOid));
@@ -1558,11 +1564,35 @@ export async function pullChecksSubmit(ctx: Ctx): Promise<Response> {
15581564 ctx,
15591565 dest,
15601566 asked.ok
1561 ? { message: CHECK_ASKED }
1567 ? { message: askedWords(asked) ?? CHECK_ASKED }
15621568 : { tone: "danger", message: `No check was asked for: ${asked.message}.` },
15631569 );
15641570 }
15651571
1572 // POST /:owner/:name/stuck — one choice over every pull of a stuck repository (#12611):
1573 // `choice` is recheck, builders or close, `reason` is said on a close.
1574 export async function stuckSubmit(ctx: Ctx): Promise<Response> {
1575 const repo = loadRepo(ctx);
1576 if (!repo) return notFoundPage(ctx);
1577 if (!ctx.auth.user || !canWrite(ctx.app.db, repo, ctx.auth.user))
1578 return forbiddenPage(ctx, "Only someone who can write to this repository can do this.");
1579 const form = await readForm(ctx, "rw");
1580 if (form instanceof Response) return form;
1581 const choice = field(form, "choice");
1582 // Back to where the form was: the home, or this repository's pulls; nothing else is honoured.
1583 const dest = field(form, "next") === `${repoBase(repo)}/pulls` ? `${repoBase(repo)}/pulls` : "/";
1584 if (!isPileChoice(choice)) return flashTo(ctx, dest, { tone: "danger", message: "Choose re-check, hand to builders or close." });
1585 const reason = field(form, "reason");
1586 const done = await takePileChoice(ctx.app, ctx.auth.user, repo.id, choice, reason === "" ? {} : { reason });
1587 return flashTo(
1588 ctx,
1589 dest,
1590 "error" in done
1591 ? { tone: "danger", message: `Nothing was done: ${done.error}.` }
1592 : { message: pileWords(done) },
1593 );
1594 }
1595
15661596 // POST /:owner/:name/pulls/:number/merge
15671597 export async function pullMergeSubmit(ctx: Ctx): Promise<Response> {
15681598 const found = pullContext(ctx);
test/ask-check-says.test.ts added +66
@@ -0,0 +1,66 @@
1 /**
2 * Asking for a check again over the API says where it stands (#12611): `place` and the
3 * words to show.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { openPull } from "../src/actions/pulls.ts";
10 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
11 import { createApp, type App } from "../src/app.ts";
12 import { startChecks, type ChecksHandle } from "../src/checks/service.ts";
13 import { createSession } from "../src/core/sessions.ts";
14 import { createUser } from "../src/core/users.ts";
15 import { forgeHttp, type ForgeHttp } from "../src/http/server.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17
18 quietLogs();
19
20 let dataDir = "";
21 let app: App;
22 let checks: ChecksHandle;
23 let http: ForgeHttp;
24 let cookie = "";
25 let number = 0;
26
27 beforeAll(async () => {
28 dataDir = await mkdtemp(join(tmpdir(), "forge-ask-says-"));
29 app = createApp({ dataDir, port: 0, host: "forge.test" });
30 checks = startChecks(app, { forgeUrl: "http://127.0.0.1:9", concurrency: 1 });
31 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
32 if (!made.ok) throw new Error(made.error);
33 const alice = made.value;
34 const repo = await createRepo(app, alice, { name: "lib", visibility: "public" });
35 if (!repo.ok) throw new Error(String(repo.error));
36 const declared = await commitToBranch(app, alice, repo.value, { message: "Declare a check", files: [{ path: "forge.json", content: `{"check": {"run": "true", "install": ""}}\n` }] });
37 if (!declared.ok) throw new Error(JSON.stringify(declared.error));
38 const topic = await commitToBranch(app, alice, repo.value, { branch: "topic", base: repo.value.defaultBranch, message: "change", files: [{ path: "t.md", content: "t\n" }] });
39 if (!topic.ok) throw new Error(JSON.stringify(topic.error));
40 const opened = await openPull(app, alice, repo.value, { title: "A change", baseBranch: repo.value.defaultBranch, headBranch: "topic" });
41 if (!opened.ok) throw new Error(String(opened.error));
42 number = opened.value.number;
43 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
44 http = forgeHttp(app);
45 }, 120_000);
46
47 afterAll(async () => {
48 await checks.stop();
49 http.close();
50 app.close();
51 await rm(dataDir, { recursive: true, force: true });
52 });
53
54 test("POST …/checks answers 202 with the run, the place in line (or null) and the words to say", async () => {
55 const res = await http.handle(new Request(`http://forge.test/api/v1/repos/alice/lib/pulls/${number}/checks`, {
56 method: "POST",
57 headers: { cookie, origin: "http://forge.test", "content-type": "application/json" },
58 body: "{}",
59 }));
60 expect(res.status).toBe(202);
61 const body = (await res.json()) as { run: { id: string }; place: { place: number; of: number } | null; says: string };
62 expect(body.run.id).toBeTruthy();
63 expect(body).toHaveProperty("place");
64 if (body.place !== null) expect(body.says).toBe(`Check queued at the current head: place ${body.place.place} of ${body.place.of}.`);
65 else expect(body.says.length).toBeGreaterThan(0);
66 });
test/openapi-oa-pull-checks.test.ts +1 −1
@@ -13,6 +13,6 @@ test("GET and POST …/pulls/{number}/checks are described", () => {
1313 const ok = (m: string, code: string) =>
1414 paths[P]?.[m]?.responses?.[code]?.content?.["application/json"]?.schema?.properties;
1515 expect(Object.keys(ok("get", "200"))).toEqual(["head", "state", "required", "run", "runs"]);
16 expect(Object.keys(ok("post", "202"))).toEqual(["run"]);
16 expect(Object.keys(ok("post", "202"))).toEqual(["run", "place", "says"]);
1717 expect(JSON.stringify(paths[P]?.post)).toContain("no_check_declared");
1818 });
test/stuck-pile-doors.test.ts added +152
@@ -0,0 +1,152 @@
1 /**
2 * The doors of the stuck pile (#12611): the form on the home, the MCP `forge` tool and the
3 * terminal's needs screen each take one choice over all of a stuck repository's pulls, and each
4 * refuses who may not and plans before it acts.
5 */
6 import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test";
7 import { snappyFixture, type SnappyFixture } from "../bench/snappy.ts";
8 import { needsStack } from "../src/actions/needs.ts";
9 import { forgetStuckRepos } from "../src/flow/stuck_pile.ts";
10 import { tool as forgeTool } from "../src/http/mcp/tools/forge.ts";
11 import { needsScreen } from "../src/tui/screens/needs.ts";
12 import type { Ctx as TuiCtx, Screen } from "../src/tui/app/app.ts";
13 import type { Ctx as HttpCtx } from "../src/http/context.ts";
14 import type { KeyEvent } from "../src/tui/term/input.ts";
15 import type { Session } from "../src/tui/session.ts";
16
17 const DAY = 86_400_000;
18 let f: SnappyFixture;
19 let repoId = "";
20 let path = "";
21 let n = 100000;
22
23 beforeAll(async () => {
24 f = await snappyFixture({ repos: 1, issuesPerRepo: 1 });
25 repoId = f.repos[0]!.id;
26 path = `/${f.owner.username}/${f.repos[0]!.name}`;
27 }, 120_000);
28 afterAll(() => f.close());
29 beforeEach(() => {
30 forgetStuckRepos(f.app.db);
31 f.app.db.run("DELETE FROM issue WHERE kind = 'pull'");
32 for (let i = 0; i < 3; i++) {
33 n++;
34 const at = new Date(Date.now() - 10 * DAY).toISOString();
35 f.app.db.run("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at) VALUES (?1, ?2, ?3, 'pull', ?4, '', ?5, 'open', ?6, ?6)", [`sp${n}`, repoId, n, `change ${n}`, f.owner.id, at]);
36 f.app.db.run("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', ?2, '', 'h')", [`sp${n}`, `b${n}`]);
37 }
38 });
39
40 const open = (): number => (f.app.db.query("SELECT COUNT(*) AS n FROM issue WHERE kind = 'pull' AND state = 'open'").get() as { n: number }).n;
41 const post = (headers: Record<string, string>, body: Record<string, string>): Promise<Response> =>
42 fetch(`${f.base}${path}/stuck`, {
43 method: "POST",
44 redirect: "manual",
45 headers: { "content-type": "application/x-www-form-urlencoded", ...headers },
46 body: new URLSearchParams(body),
47 });
48
49 test("POST /stuck: a guest, a non-writer and a cross-origin post change nothing", async () => {
50 const guest = await post({ origin: f.base }, { choice: "close" });
51 expect([303, 401, 403]).toContain(guest.status);
52 const peer = await post({ origin: f.base, cookie: f.peerCookie }, { choice: "close" });
53 expect([403, 404]).toContain(peer.status);
54 const cross = await post({ origin: "https://evil.example", cookie: f.ownerCookie }, { choice: "close" });
55 expect(cross.status).toBeGreaterThanOrEqual(400);
56 expect(open()).toBe(3);
57 });
58
59 test("POST /stuck: a choice that is not one of the three says so and changes nothing", async () => {
60 const res = await post({ origin: f.base, cookie: f.ownerCookie }, { choice: "delete-everything" });
61 expect(res.status).toBe(303);
62 expect(open()).toBe(3);
63 });
64
65 test("POST /stuck: a writer closes them with a reason, and is sent back to the page the form was on", async () => {
66 const there = await post({ origin: f.base, cookie: f.ownerCookie }, { choice: "close", reason: "stale", next: `${path}/pulls` });
67 expect(there.status).toBe(303);
68 expect(there.headers.get("location")).toBe(`${path}/pulls`);
69 expect(open()).toBe(0);
70 });
71
72 test("POST /stuck: the home is the default way back, and an address elsewhere is not followed", async () => {
73 const res = await post({ origin: f.base, cookie: f.ownerCookie }, { choice: "recheck", next: "https://evil.example/" });
74 expect(res.status).toBe(303);
75 expect(res.headers.get("location")).toBe("/");
76 });
77
78 test("MCP forge: a choice is a plan unless dryRun is false; a bad choice and a non-stuck id are refused", async () => {
79 const httpCtx = (): HttpCtx => ({
80 app: f.app,
81 request: new Request("http://localhost/mcp"),
82 url: new URL("http://localhost/mcp"),
83 params: {},
84 auth: { user: f.owner, scope: undefined, via: "session" },
85 theme: undefined,
86 });
87 const run = (args: Record<string, unknown>) => forgeTool.run(httpCtx(), f.owner, args) as Promise<{ ok: boolean; value?: unknown }>;
88 const plan = await run({ apply: `stuck:${repoId}`, choice: "close" });
89 expect(plan).toMatchObject({ ok: true, value: { choice: "close", dryRun: true } });
90 expect(open()).toBe(3);
91 expect((await run({ apply: `stuck:${repoId}`, choice: "obliterate" })).ok).toBe(false);
92 expect((await run({ apply: "recommended", choice: "close" })).ok).toBe(false);
93 expect((await run({ apply: "stuck:nope", choice: "close", dryRun: false })).ok).toBe(false);
94 expect(open()).toBe(3);
95 const done = await run({ apply: `stuck:${repoId}`, choice: "close", dryRun: false, reason: "parked" });
96 expect(done).toMatchObject({ ok: true, value: { dryRun: false } });
97 expect(open()).toBe(0);
98 });
99
100 /** The terminal: screens pushed are kept, a key is a key. */
101 function terminal() {
102 const toasts: string[] = [];
103 const stack: Screen[] = [];
104 const tracked: Promise<unknown>[] = [];
105 const ctx = {
106 toast: (t: string) => toasts.push(t),
107 invalidate() {},
108 push: (s: Screen) => stack.push(s),
109 pop: () => stack.pop(),
110 setCursor() {},
111 track: (p: Promise<unknown>) => (tracked.push(p), p),
112 } as unknown as TuiCtx;
113 const screen = needsScreen({ user: f.owner, db: f.app.db, app: f.app } as unknown as Session, ctx);
114 const press = async (k: string, to: Screen = screen) => {
115 const event: KeyEvent = { type: "key", key: k, ctrl: false, alt: false, shift: false };
116 to.key(event);
117 await Promise.all(tracked);
118 await Promise.resolve();
119 };
120 const at = needsStack({ app: f.app }, f.owner).findIndex((x) => x.id === `stuck:${repoId}`);
121 return { screen, press, stack, toasts, at };
122 }
123
124 test("the terminal: r and h plan first and act only on a yes; Esc leaves everything as it was", async () => {
125 const t = terminal();
126 for (let i = 0; i < t.at; i++) await t.press("j");
127 await t.press("h");
128 expect(t.stack).toHaveLength(1);
129 expect(t.toasts).toEqual([]);
130 await t.press("esc", t.stack[0]!);
131 expect(t.toasts).toEqual([]);
132 expect(f.app.db.query("SELECT COUNT(*) AS n FROM issue WHERE kind = 'pull' AND assignee_id IS NOT NULL").get()).toEqual({ n: 0 });
133 await t.press("r");
134 expect(t.stack).toHaveLength(1);
135 await t.press("enter", t.stack[0]!);
136 expect(t.toasts.join(" ")).toContain("left alone");
137 });
138
139 test("the terminal: c asks for a reason, plans, and closes every pull on a yes", async () => {
140 const t = terminal();
141 for (let i = 0; i < t.at; i++) await t.press("j");
142 await t.press("c");
143 const prompt = t.stack.pop()!;
144 for (const k of "stale") await t.press(k, prompt);
145 await t.press("enter", prompt);
146 await new Promise((r) => setTimeout(r, 20)); // real-timer: the prompt's async submit has no clock to step
147 expect(t.stack).toHaveLength(1);
148 expect(open()).toBe(3);
149 await t.press("enter", t.stack[0]!);
150 expect(open()).toBe(0);
151 expect(t.toasts.at(-1)).toContain("Closed: 3 pulls");
152 });
test/stuck-pile.test.ts added +238
@@ -0,0 +1,238 @@
1 /**
2 * A repository stuck behind its own pulls (#12611): found, raised once in the needs stack for
3 * whoever writes there, shown as a lane of its own on the home, and moved by its three choices;
4 * and a re-check says where it stands in the line.
5 */
6 import type { CheckSummary } from "../src/core/runs.ts";
7 import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test";
8 import { mkdtemp, rm } from "node:fs/promises";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { createApp, type App } from "../src/app.ts";
12 import { createAgent, addToCrew } from "../src/actions/crew.ts";
13 import { needsStack } from "../src/actions/needs.ts";
14 import { getRepoById } from "../src/core/repos.ts";
15 import { whyNotQueued } from "../src/checks/again.ts";
16 import { getPull } from "../src/core/pulls.ts";
17 import { placeWords, queuePlace } from "../src/checks/place.ts";
18 import { getRun } from "../src/core/runs.ts";
19 import { createUser, getUserByName, type User } from "../src/core/users.ts";
20 import { pilePlan, takePileChoice } from "../src/crew/stuck_pile.ts";
21 import { forgetStuckRepos, PILE_MIN, readStuckRepos } from "../src/flow/stuck_pile.ts";
22 import { pileRegion } from "../src/web/pages/home_flow.ts";
23
24 const DAY = 86_400_000;
25 const now = new Date("2026-11-14T12:00:00Z");
26 const ago = (days: number): string => new Date(now.getTime() - days * DAY).toISOString();
27 let dir: string;
28 let app: App;
29 let alice: User;
30 let bob: User;
31 let n = 0;
32
33 beforeAll(async () => {
34 dir = await mkdtemp(join(tmpdir(), "stuck-pile-"));
35 app = createApp({ dataDir: dir, port: 0 });
36 const mk = async (username: string, first: boolean): Promise<User> => {
37 const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first });
38 if (!u.ok) throw new Error(String(u.error));
39 return u.value;
40 };
41 alice = await mk("alice", true);
42 bob = await mk("bob", false);
43 app.db.run("INSERT INTO repo (id, owner_id, name, visibility) VALUES ('r1', ?1, 'scratch', 'public')", [alice.id]);
44 });
45 afterAll(async () => {
46 app.close?.();
47 await rm(dir, { recursive: true, force: true });
48 });
49 beforeEach(() => {
50 forgetStuckRepos(app.db);
51 app.db.run("DELETE FROM run");
52 app.db.run("DELETE FROM issue");
53 });
54
55 /** An open pull whose check on its head is `check`, last touched `idle` days ago. */
56 function pull(o: { check?: "failed" | "ok" | "queued"; idle: number; body?: string }): number {
57 n++;
58 const id = `p${n}`;
59 app.db.run("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at, updated_at) VALUES (?1, 'r1', ?2, 'pull', ?3, ?4, ?5, 'open', ?6, ?6)", [id, n, `change ${n}`, o.body ?? "", alice.id, ago(o.idle)]);
60 app.db.run("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', ?2, '', 'h')", [id, `b${n}`]);
61 if (o.check !== undefined)
62 app.db.run("INSERT INTO run (id, repo_id, kind, issue_id, commit_oid, state, created_at, finished_at) VALUES (?1, 'r1', 'check', ?2, 'h', ?3, ?4, ?4)", [`c${n}`, id, o.check, ago(o.idle)]);
63 return n;
64 }
65
66 test("every open pull red or untouched makes a pile, named with its ages", () => {
67 for (let i = 0; i < PILE_MIN; i++) pull({ check: "failed", idle: 12 + i });
68 const [s] = readStuckRepos(app.db, now);
69 expect(s?.ref).toBe("alice/scratch");
70 expect(s?.open).toBe(PILE_MIN);
71 expect(s?.red).toBe(PILE_MIN);
72 expect(s?.oldestDays).toBe(14);
73 expect(s?.pulls.map((p) => p.idleDays)).toEqual([14, 13, 12]);
74 });
75
76 test("one moving pull, or too few pulls, is ordinary work", () => {
77 pull({ check: "failed", idle: 12 });
78 pull({ check: "failed", idle: 12 });
79 expect(readStuckRepos(app.db, now)).toEqual([]);
80 pull({ check: "ok", idle: 0 });
81 expect(readStuckRepos(app.db, now)).toEqual([]);
82 });
83
84 test("a green pull untouched for days sits as much as a red one", () => {
85 pull({ check: "ok", idle: 9 });
86 pull({ check: "failed", idle: 1 });
87 pull({ check: "failed", idle: 1 });
88 expect(readStuckRepos(app.db, now)[0]?.untouched).toBe(1);
89 });
90
91 test("the needs stack raises it once, by name, for a writer and not for anyone else", () => {
92 for (let i = 0; i < 4; i++) pull({ check: "failed", idle: 10 });
93 const rows = needsStack({ app }, alice).filter((r) => r.id.startsWith("stuck:"));
94 expect(rows).toHaveLength(1);
95 expect(rows[0]!.sentence).toContain("alice/scratch is stuck: 4 open pulls");
96 expect(rows[0]!.choices).toEqual(["Re-check all on today's main", "Hand all to builders", "Close all with a reason"]);
97 expect(rows[0]!.recommended.safe).toBe(false);
98 expect(needsStack({ app }, bob).filter((r) => r.id.startsWith("stuck:"))).toEqual([]);
99 });
100
101 test("the home shows the pile as one lane of its own with an age on each pull", () => {
102 for (let i = 0; i < 3; i++) pull({ check: "failed", idle: 10 });
103 const html = String(pileRegion(app.db, alice, now.getTime()));
104 expect(html).toContain('data-lane="pile"');
105 expect(html).toContain("10 d");
106 expect(html).toContain('action="/alice/scratch/stuck"');
107 expect(pileRegion(app.db, bob, now.getTime())).toBeNull();
108 });
109
110 test("closing all closes every pull with the reason and sets its issue ready again", async () => {
111 app.db.run("INSERT INTO issue (id, repo_id, number, kind, title, author_id, state, assignee_id) VALUES ('is1', 'r1', 900, 'issue', 'the ask', ?1, 'open', ?1)", [alice.id]);
112 for (let i = 0; i < 3; i++) pull({ check: "failed", idle: 10, body: i === 0 ? "Closes #900" : "" });
113 const plan = await takePileChoice(app, alice, "r1", "close", { dryRun: true });
114 expect(plan).toMatchObject({ dryRun: true });
115 expect((plan as { acted: number[] }).acted).toHaveLength(3);
116 expect(app.db.query("SELECT COUNT(*) AS n FROM issue WHERE kind = 'pull' AND state = 'open'").get()).toEqual({ n: 3 });
117 const done = await takePileChoice(app, alice, "r1", "close", { reason: "stale since the 2nd" });
118 expect((done as { acted: number[] }).acted).toHaveLength(3);
119 expect(app.db.query("SELECT COUNT(*) AS n FROM issue WHERE kind = 'pull' AND state = 'open'").get()).toEqual({ n: 0 });
120 expect(app.db.query("SELECT assignee_id FROM issue WHERE id = 'is1'").get()).toEqual({ assignee_id: null });
121 expect(app.db.query("SELECT COUNT(*) AS n FROM comment WHERE body = 'stale since the 2nd'").get()).toEqual({ n: 3 });
122 expect(readStuckRepos(app.db, now)).toEqual([]);
123 });
124
125 test("a person who cannot write, and a repository no longer stuck, get nothing done", async () => {
126 for (let i = 0; i < 3; i++) pull({ check: "failed", idle: 10 });
127 expect(await takePileChoice(app, bob, "r1", "close")).toEqual({ error: "not found" });
128 expect(await takePileChoice(app, alice, "r1", "close", { dryRun: true })).toMatchObject({ dryRun: true });
129 pull({ check: "ok", idle: 0 });
130 expect(await takePileChoice(app, alice, "r1", "close")).toEqual({ error: "that repository is not stuck any more" });
131 expect(app.db.query("SELECT COUNT(*) AS n FROM issue WHERE state = 'open'").get()).toEqual({ n: 4 });
132 });
133
134 test("handing to builders leaves a pull no crew member opened, and says why", async () => {
135 for (let i = 0; i < 3; i++) pull({ check: "failed", idle: 10 });
136 const done = (await takePileChoice(app, alice, "r1", "builders")) as { acted: number[]; left: { why: string }[] };
137 expect(done.acted).toEqual([]);
138 expect(done.left).toHaveLength(3);
139 expect(done.left[0]!.why).toBe("no crew member opened it");
140 });
141
142 test("a queued check says its place in line; one not waiting has none", () => {
143 for (const [id, state] of [["a1", "queued"], ["a2", "running"], ["a3", "queued"], ["a4", "queued"]] as const)
144 app.db.run("INSERT INTO run (id, repo_id, kind, state) VALUES (?1, 'r1', 'check', ?2)", [id, state]);
145 const run = (id: string) => ({ id, kind: "check" as const, state: id === "a2" ? ("running" as const) : ("queued" as const) });
146 expect(placeWords(queuePlace(app.db, run("a3")))).toBe("place 2 of 3");
147 expect(queuePlace(app.db, run("a2"))).toBeUndefined();
148 });
149
150 test("the words after a re-check say the place", async () => {
151 const { askedWords } = await import("../src/checks/again.ts");
152 const run = (state: "queued" | "running") => {
153 app.db.run("DELETE FROM run");
154 app.db.run("INSERT INTO run (id, repo_id, kind, state) VALUES ('w1', 'r1', 'check', ?1)", [state]);
155 return getRun(app.db, "w1")!;
156 };
157 expect(askedWords({ ok: true, run: run("queued"), place: { place: 3, of: 12 } })).toBe("Check queued at the current head: place 3 of 12.");
158 expect(askedWords({ ok: true, run: run("running"), place: undefined })).toBe("Check running at the current head.");
159 });
160
161 /** A pull `bot`, a crew member, opened. */
162 async function crewPull(check: "failed" | "ok" | "queued" | undefined): Promise<number> {
163 if (getUserByName(app.db, "bot") === undefined) {
164 await createAgent(app, alice, { username: "bot" });
165 addToCrew(app, alice, getRepoById(app.db, "r1")!, "bot");
166 }
167 const number = pull({ ...(check === undefined ? {} : { check }), idle: 10 });
168 app.db.run("UPDATE issue SET author_id = ?1 WHERE repo_id = 'r1' AND number = ?2", [getUserByName(app.db, "bot")!.id, number]);
169 return number;
170 }
171
172 test("builders: a pull a crew member opened is handed back, planned first; one with no check on its head is left, with why", async () => {
173 const a = await crewPull("failed");
174 const b = await crewPull("failed");
175 const c = await crewPull(undefined);
176 const plan = (await takePileChoice(app, alice, "r1", "builders", { dryRun: true })) as { acted: number[]; left: { pull: number; why: string }[] };
177 expect(plan.acted).toEqual([a, b]);
178 expect(plan.left).toEqual([{ pull: c, why: "it has no check on its current head" }]);
179 expect(app.db.query("SELECT COUNT(*) AS n FROM issue WHERE kind = 'pull' AND assignee_id IS NOT NULL").get()).toEqual({ n: 0 });
180 const done = (await takePileChoice(app, alice, "r1", "builders")) as { acted: number[] };
181 expect(done.acted).toEqual([a, b]);
182 const bot = getUserByName(app.db, "bot")!;
183 expect(app.db.query("SELECT COUNT(*) AS n FROM issue WHERE kind = 'pull' AND assignee_id = ?1").get(bot.id)).toEqual({ n: 2 });
184 });
185
186 test("a person holding or assigned to a pull keeps it from the builders", async () => {
187 const a = await crewPull("failed");
188 const b = await crewPull("failed");
189 await crewPull("failed");
190 app.db.run("UPDATE issue SET assignee_id = ?1 WHERE repo_id = 'r1' AND number = ?2", [alice.id, a]);
191 const plan = (await takePileChoice(app, alice, "r1", "builders", { dryRun: true })) as { acted: number[]; left: { pull: number; why: string }[] };
192 expect(plan.left).toEqual([{ pull: a, why: "a person is assigned to it" }]);
193 expect(plan.acted).not.toContain(a);
194 expect(plan.acted).toContain(b);
195 });
196
197 test("recheck: the plan names every pull; a forge that runs no checks leaves each with why", async () => {
198 const a = await crewPull("failed");
199 const b = await crewPull("failed");
200 await crewPull("failed");
201 const plan = (await takePileChoice(app, alice, "r1", "recheck", { dryRun: true })) as { acted: number[] };
202 expect(plan.acted).toHaveLength(3);
203 const done = (await takePileChoice(app, alice, "r1", "recheck")) as { acted: number[]; left: { pull: number; why: string }[] };
204 expect(done.acted).toEqual([]);
205 expect(done.left.map((l) => l.pull)).toEqual([a, b, a + 2]);
206 expect(done.left[0]!.why).toBe("this forge runs no checks");
207 });
208
209 test("a plan reads as one line and the first pulls it leaves", () => {
210 const lines = pilePlan({ choice: "builders", dryRun: true, acted: [1, 2], left: [1, 2, 3, 4, 5].map((pull) => ({ pull, why: "x" })) });
211 expect(lines).toEqual(["Hand to the crew 2 pulls.", "Leave #1: x.", "Leave #2: x.", "Leave #3: x.", "2 more left alone."]);
212 });
213
214 test("why a check could not be queued, each refusal in its own words", async () => {
215 const repo = getRepoById(app.db, "r1")!;
216 const n = pull({ check: "failed", idle: 10 });
217 const open = getPull(app.db, "r1", n)!;
218 expect(whyNotQueued(app, repo, open, "")).toBe("this pull has no head commit to check");
219 expect(whyNotQueued(app, repo, open, "h")).toBe("this head declares no check, or the forge is stopping");
220 // A pull proposed from a child run waits for a writer's approval.
221 app.db.run("UPDATE pull SET head_branch = 'child/x' WHERE issue_id = ?1", [open.id]);
222 expect(whyNotQueued(app, repo, getPull(app.db, "r1", n)!, "h")).toBe("this head waits for a writer's approval before any check runs");
223 // A guest's fork runs nothing.
224 app.db.run("INSERT INTO repo (id, owner_id, name, visibility) VALUES ('r2', ?1, 'fork', 'public')", [bob.id]);
225 app.db.run("INSERT INTO contribution_fork (repo_id, made_at) VALUES ('r2', ?1)", [ago(1)]);
226 expect(whyNotQueued(app, getRepoById(app.db, "r2")!, open, "h")).toBe("a guest's fork runs nothing; its code runs only as a pull a person approved");
227 app.db.run("UPDATE user SET suspended_at = ?1 WHERE id = ?2", [ago(0), alice.id]);
228 expect(whyNotQueued(app, repo, open, "h")).toBe("the repository's owner is suspended or removed, so no code of theirs runs");
229 app.db.run("UPDATE user SET suspended_at = NULL WHERE id = ?1", [alice.id]);
230 });
231
232 test("the checks line says the place of a queued check, and nothing for one that is not waiting", async () => {
233 const { checksLine } = await import("../src/web/pages/pull.ts");
234 const check: CheckSummary = { state: "queued", run: null };
235 const line = (place: string) => String(checksLine("/a/b", check, false, true, false, "", "", 0, "", false, "", "", place));
236 expect(line("place 2 of 5")).toContain(", place 2 of 5");
237 expect(line("")).not.toContain("place");
238 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs