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

Open-pull ceiling: a pull handed to a person after changes were requested does not count #16717

Merged opened this

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

Merged

flow/ceiling-handed → main 0 commits 2 threads · 2 unresolved

  • Merged. flow/ceiling-handed was merged into main as 58f6866.

Review

Sign in to review this change.
3 files changed +184 −4
src/core/pulls_ceiling.ts +34 −2
@@ -28,6 +28,38 @@ export const PULLS_HELD_NOTE = "New builds wait for reviews.";
2828 const WAITS_FOR_AUTHOR = `(NOT COALESCE((SELECT is_agent FROM user WHERE user.id = issue.author_id), 0)
2929 AND COALESCE((SELECT head_repo_id FROM pull WHERE pull.issue_id = issue.id), issue.repo_id) = issue.repo_id)`;
3030
31 /**
32 * What the hand-over card starts with: the thread's own record of it, and what `handover` ignores as
33 * a person's word. Here, not in `crew/guard.ts` (which re-exports it), so this module reads it at
34 * load without importing the crew, whose imports reach back to it.
35 */
36 export const HANDOVER_NOTE = "Handed to a person by the crew:";
37
38 /**
39 * An open pull the crew handed to a person after changes were requested (`crew/requested.ts`,
40 * `crew/guard.ts`): the newest verdict asks for changes on the head, a hand-over note came after
41 * it, and no person has spoken since. The note counts only as the crew writes it: by the verdict's
42 * own reviewer, an agent that is not the pull's author, so no one else's comment with the prefix
43 * can take a pull off the ceiling. No builder run starts on it and the reviewer has had its
44 * say, so like WAITS_FOR_AUTHOR it holds nothing back a review or a builder could shrink. A push
45 * (a new head), a new verdict or a person's word makes it count again. Over `issue`.
46 */
47 const HANDED_TO_A_PERSON = `EXISTS (SELECT 1 FROM review v JOIN pull hp ON hp.issue_id = v.issue_id
outdated

Medium: comment n can be written by anyone (the builder, any commenter). Anyone who writes the prefix can take a pull off the ceiling. Require n.author_id to be an agent (u.is_agent = 1) other than issue.author_id, and add a test where a builder-authored or non-agent note with the prefix leaves the pull counted.

48 WHERE v.issue_id = issue.id AND v.state = 'changes_requested' AND v.commit_oid = hp.head_oid
49 AND v.id = (SELECT id FROM review WHERE issue_id = issue.id AND state != 'commented' ORDER BY created_at DESC, id DESC LIMIT 1)
50 AND EXISTS (SELECT 1 FROM comment n JOIN user na ON na.id = n.author_id
51 WHERE n.issue_id = issue.id AND n.created_at >= v.created_at
52 AND substr(n.body, 1, ${String(HANDOVER_NOTE.length)}) = '${HANDOVER_NOTE}'
53 AND n.author_id = v.author_id AND na.is_agent = 1 AND n.author_id != issue.author_id AND n.origin_author IS NULL
54 AND NOT EXISTS (SELECT 1 FROM comment w JOIN user u ON u.id = w.author_id
55 WHERE w.issue_id = issue.id AND w.created_at > n.created_at AND u.is_agent = 0 AND w.origin_author IS NULL)))`;
56
57 /**
58 * What counts toward the ceiling: an open pull a review or a builder can shrink. The one definition
59 * every reader uses.
60 */
61 const COUNTS = `issue.kind = 'pull' AND issue.state = 'open' AND NOT ${WAITS_FOR_AUTHOR} AND NOT ${HANDED_TO_A_PERSON}`;
62
3163 const key = (repoId: string) => `crew.openPullsCeiling.${repoId}`;
3264
3365 export function openPullsCeiling(db: Database, repoId: string): number {
@@ -63,7 +95,7 @@ export function pullsCeilingHold(db: Database, repoId: string): string | null {
6395 const row = db
6496 .query<{ value: string | null; open: number }, [string, string]>(
6597 `SELECT (SELECT value FROM forge_setting WHERE key = ?1) AS value,
66 (SELECT COUNT(*) FROM issue WHERE repo_id = ?2 AND kind = 'pull' AND state = 'open' AND NOT ${WAITS_FOR_AUTHOR}) AS open`,
98 (SELECT COUNT(*) FROM issue WHERE repo_id = ?2 AND ${COUNTS}) AS open`,
6799 )
68100 .get(key(repoId), repoId);
69101 const ceiling = ceilingOf(row?.value);
@@ -75,7 +107,7 @@ export function pullsCeilingHold(db: Database, repoId: string): string | null {
75107 /** Every repository held now, as [repo id, cause]: only repositories with open pulls are walked. */
76108 export function pullsCeilingHeld(db: Database): { repoId: string; cause: string }[] {
77109 const rows = db
78 .query<{ repo_id: string; c: number }, []>(`SELECT repo_id, COUNT(*) AS c FROM issue WHERE kind = 'pull' AND state = 'open' AND NOT ${WAITS_FOR_AUTHOR} GROUP BY repo_id`)
110 .query<{ repo_id: string; c: number }, []>(`SELECT repo_id, COUNT(*) AS c FROM issue WHERE ${COUNTS} GROUP BY repo_id`)
79111 .all();
80112 const held: { repoId: string; cause: string }[] = [];
81113 for (const r of rows) {
src/crew/guard.ts +2 −2
@@ -18,6 +18,7 @@
1818 */
1919
2020 import type { Database } from "bun:sqlite";
21 import { HANDOVER_NOTE } from "../core/pulls_ceiling.ts";

Minor: move this import below ../core/decisions.ts/../core/issues.ts so the ../core/ imports stay in order.

2122 import type { Agent } from "../core/agents.ts";
2223 import { recordDecision } from "../core/decisions.ts";
2324 import type { Issue } from "../core/issues.ts";
@@ -250,8 +251,7 @@ export function guardCounts(
250251 /** Crew `changes_requested` verdicts on one pull, with no approval or person's word between, before it is a person's. */
251252 export const HANDOVER_ROUNDS = 3;
252253
253 /** What the hand-over card starts with: the thread's own record of it, and what `handover` ignores as a person's word. */
254 export const HANDOVER_NOTE = "Handed to a person by the crew:";
254 export { HANDOVER_NOTE };
255255
256256 /**
257257 * Why a pass handed this pull to a person, in one line: the newest hand-over
test/ceiling-handed.test.ts added +148
@@ -0,0 +1,148 @@
1 /**
2 * A pull the crew handed to a person after changes were requested: no builder run starts on it and
3 * the reviewer has had its say, so it does not count toward the open-pull ceiling and holds no new
4 * build back. A push or a person's word makes it count again.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { addToCrew, createAgent } from "../src/actions/crew.ts";
12 import { addComment, openIssue } from "../src/actions/issues.ts";
13 import { setPullsCeiling } from "../src/actions/planner_switch.ts";
14 import { openPull } from "../src/actions/pulls.ts";
15 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
16 import { getAgentByName, type Agent } from "../src/core/agents.ts";
17 import { getIssueById, type Issue } from "../src/core/issues.ts";
18 import { getPull } from "../src/core/pulls.ts";
19 import { pullsCeilingHold } from "../src/core/pulls_ceiling.ts";
20 import { getRepoById, type Repo } from "../src/core/repos.ts";
21 import { submitReview } from "../src/core/reviews.ts";
22 import { createUser, type User } from "../src/core/users.ts";
23 import { HANDOVER_NOTE } from "../src/crew/guard.ts";
24 import { queueAgentRun } from "../src/crew/dispatch.ts";
25 import { quietLogs } from "./helpers/globals.ts";
26 import { seatAs } from "./helpers/seat.ts";
27
28 quietLogs();
29
30 let dataDir: string;
31 let app: App;
32 let alice: User;
33 let repo: Repo;
34 let builder: Agent;
35 let reviewer: Agent;
36 let issue: Issue;
37 let handed: Issue;
38
39 const fresh = () => getRepoById(app.db, repo.id)!;
40 const at = (iso: string, commentId: string) => app.db.query("UPDATE comment SET created_at = ?1 WHERE id = ?2").run(iso, commentId);
41
42 beforeAll(async () => {
43 dataDir = await mkdtemp(join(tmpdir(), "forge-ceiling-handed-"));
44 app = createApp({ dataDir, port: 0 });
45 const a = await createUser(app.db, {
46 origin: "seed",
47 trusted: true,
48 username: "alice",
49 email: "[email protected]",
50 password: "a-good-passphrase",
51 adminIfFirst: true,
52 });
53 if (!a.ok) throw new Error(a.error);
54 alice = a.value;
55 const made = await createRepo(app, alice, { name: "notes", visibility: "public" });
56 if (!made.ok) throw new Error(String(made.error));
57 repo = made.value;
58 const first = await commitToBranch(app, alice, repo, {
59 message: "Start",
60 files: [{ path: "package.json", content: `{"name":"notes"}\n` }],
61 });
62 if (!first.ok) throw new Error(JSON.stringify(first.error));
63 for (const name of ["b1", "r1"]) {
64 const m = await createAgent(app, alice, { username: name });
65 if (!m.ok) throw new Error(String(m.error));
66 expect(addToCrew(app, alice, repo, name).ok).toBe(true);
67 }
68 builder = getAgentByName(app.db, "b1")!;
69 reviewer = getAgentByName(app.db, "r1")!;
70 expect(seatAs(app.db, repo.id, reviewer.user.id, "reviewer")).toBe(true);
71 const opened = openIssue(app, alice, fresh(), { title: "Rename a note" });
72 if (!opened.ok) throw new Error(String(opened.error));
73 issue = opened.value;
74 const pushed = await commitToBranch(app, alice, fresh(), {
75 branch: "crew/fix",
76 message: "Fix",
77 files: [{ path: "f.md", content: "x\n" }],
78 });
79 if (!pushed.ok) throw new Error(JSON.stringify(pushed.error));
80 const pull = await openPull(app, alice, fresh(), { title: "Builder fix", baseBranch: "main", headBranch: "crew/fix" });
81 if (!pull.ok) throw new Error(JSON.stringify(pull.error));
82 // The builder's pull: the kind that counts.
83 app.db.query("UPDATE issue SET author_id = ?1 WHERE id = ?2").run(builder.user.id, pull.value.id);
84 handed = getIssueById(app.db, pull.value.id)!;
85 setPullsCeiling(app, alice, fresh(), "1");
86 });
87
88 afterAll(async () => {
89 await rm(dataDir, { recursive: true, force: true });
90 });
91
92 describe("a pull handed to a person after changes were requested", () => {
93 test("before the hand-over it counts", () => {
94 const head = getPull(app.db, repo.id, handed.number)!.headOid;
95 const verdict = submitReview(app.db, {
96 issueId: handed.id,
97 authorId: reviewer.user.id,
98 state: "changes_requested",
99 body: "Split it.",
100 commitOid: head,
101 createdAt: "2026-11-01T00:00:00.000Z",
102 });
103 expect(verdict.ok).toBe(true);
104 expect(pullsCeilingHold(app.db, repo.id)).toBe("paused: 1 of 1 pulls open");
105 });
106
107 test("the prefix from anyone but the verdict's reviewer leaves it counted: the builder, a person, an outside author", () => {
108 const by = (author: User, origin: string | null) => {
109 const note = addComment(app, author, fresh(), handed, `${HANDOVER_NOTE} take me off the ceiling.`);
110 if (!note.ok) throw new Error(String(note.error));
111 at("2026-11-01T00:30:00.000Z", note.value.id);
112 if (origin !== null) app.db.query("UPDATE comment SET origin_author = ?1 WHERE id = ?2").run(origin, note.value.id);
113 expect(pullsCeilingHold(app.db, repo.id)).toBe("paused: 1 of 1 pulls open");
114 app.db.query("DELETE FROM comment WHERE id = ?1").run(note.value.id);
115 };
116 by(builder.user, null);
117 by(alice, null);
118 by(reviewer.user, "someone-elsewhere");
119 });
120
121 test("handed over, it holds no new build", () => {
122 const note = addComment(
123 app,
124 reviewer.user,
125 fresh(),
126 handed,
127 `${HANDOVER_NOTE} two follow-up runs on the review's changes ended without a push.`,
128 );
129 if (!note.ok) throw new Error(String(note.error));
130 at("2026-11-01T01:00:00.000Z", note.value.id);
131 expect(pullsCeilingHold(app.db, repo.id)).toBeNull();
132 expect(queueAgentRun(app, fresh(), issue, builder, "assigned")).toBeDefined();
133 });
134
135 test("a person's word after the hand-over makes it count again", () => {
136 const word = addComment(app, alice, fresh(), handed, "Try splitting the rename from the move.");
137 if (!word.ok) throw new Error(String(word.error));
138 at("2026-11-01T02:00:00.000Z", word.value.id);
139 expect(pullsCeilingHold(app.db, repo.id)).toBe("paused: 1 of 1 pulls open");
140 app.db.query("DELETE FROM comment WHERE id = ?1").run(word.value.id);
141 expect(pullsCeilingHold(app.db, repo.id)).toBeNull();
142 });
143
144 test("a push makes it count again: the verdict is on an older head", () => {
145 app.db.query("UPDATE pull SET head_oid = ?1 WHERE issue_id = ?2").run("0".repeat(40), handed.id);
146 expect(pullsCeilingHold(app.db, repo.id)).toBe("paused: 1 of 1 pulls open");
147 });
148 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs