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

A hold the forge marked as red releases itself when that pull is green on the same head, or after 24 h #6859

Closed opened this
Closed without merging

scout/forge:scout/6847-a-hold-the-forge-marked-as-red-releases → main 4 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +226 −4
src/core/holds.ts +59 −4
@@ -92,6 +92,8 @@ export function holdsFor(
9292 /**
9393 * Hold a pull. A pull holds one hold: holding one that is already held puts
9494 * the new person and the new words on it, and keeps when it was first held.
95 * `redHead` marks a hold the forge placed because that head was red; without
96 * it the hold is a person's and the tick never lets it go (`crew/hold_sweep.ts`).
9597 */
9698 export function setHold(
9799 db: Database,
@@ -99,18 +101,19 @@ export function setHold(
99101 by: string,
100102 why: string,
101103 until?: "green",
104 redHead: string | null = null,
102105 ): Hold {
103106 const green = until === "green" ? 1 : 0;
104107 const changed = db
105108 .query(
106 `UPDATE pull_hold SET held_by = ?2, why = ?3, until_green = ?4
109 `UPDATE pull_hold SET held_by = ?2, why = ?3, until_green = ?4, red_head = ?5
107110 WHERE issue_id = ?1 AND released_at IS NULL`,
108111 )
109 .run(pullId, by, why, green).changes;
112 .run(pullId, by, why, green, redHead).changes;
110113 if (changed === 0)
111114 db.query(
112 "INSERT INTO pull_hold (id, issue_id, held_by, why, until_green) VALUES (?1, ?2, ?3, ?4, ?5)",
113 ).run(id(), pullId, by, why, green);
115 "INSERT INTO pull_hold (id, issue_id, held_by, why, until_green, red_head) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
116 ).run(id(), pullId, by, why, green, redHead);
114117 return getHold(db, pullId)!;
115118 }
116119
@@ -127,6 +130,58 @@ export function releaseHold(db: Database, pullId: string, by: string): boolean {
127130 );
128131 }
129132
133 /** A standing hold the forge placed for red: the head it was red on, and since when. */
134 export interface RedHold {
135 readonly pullId: string;
136 readonly heldBy: string;
137 readonly redHead: string;
138 readonly heldAt: string;
139 /** `releaseOnGreen` already lets this one go on green. */
140 readonly untilGreen: boolean;
141 }
142
143 /** Every standing marked hold. A person's hold is never among them. */
144 export function redHolds(db: Database): RedHold[] {
145 return db
146 .query<
147 {
148 issue_id: string;
149 held_by: string;
150 red_head: string;
151 held_at: string;
152 until_green: number;
153 },
154 []
155 >(
156 `SELECT issue_id, held_by, red_head, held_at, until_green FROM pull_hold
157 WHERE released_at IS NULL AND red_head IS NOT NULL`,
158 )
159 .all()
160 .map((r) => ({
161 pullId: r.issue_id,
162 heldBy: r.held_by,
163 redHead: r.red_head,
164 heldAt: r.held_at,
165 untilGreen: r.until_green === 1,
166 }));
167 }
168
169 /**
170 * Let a marked hold go, in the forge's name: no person released it. The
171 * WHERE keeps `red_head IS NOT NULL`, so a person who re-held the pull since
172 * the read is never released. False when nothing was let go.
173 */
174 export function releaseRedHold(db: Database, pullId: string, at: string): boolean {
175 return (
176 db
177 .query(
178 `UPDATE pull_hold SET released_at = ?2
179 WHERE issue_id = ?1 AND released_at IS NULL AND red_head IS NOT NULL`,
180 )
181 .run(pullId, at).changes > 0
182 );
183 }
184
130185 /**
131186 * The forge lets go of a hold set "until green". False when none stands or
132187 * the hold is a plain one: a green check never lifts those.
src/core/migration-pins/20261027110000-hold-red-head.pin added +1
@@ -0,0 +1,1 @@
1 77b7d02257e9dd589fb5ad0827d159607f1913caf27e476111dc9450187b1f15
src/core/migrations/20261027110000-hold-red-head.ts added +5
@@ -0,0 +1,5 @@
1 // A hold the forge placed because a pull was red keeps the head it was red
2 // on (#6847), so the tick can let it go. Null: a person's hold. Additive.
3 export default `
4 ALTER TABLE pull_hold ADD COLUMN red_head TEXT;
5 `;
src/crew/autonomy.ts +7
@@ -50,6 +50,7 @@ import {
5050 stopStands,
5151 } from "../core/decisions.ts";
5252 import { getHold } from "../core/holds.ts";
53 import { sweepRedHolds } from "./hold_sweep.ts";
5354 import { certain } from "./decide.ts";
5455 import { decideAndRecord } from "./ledger.ts";
5556 import { sentinelTick } from "./sentinel.ts";
@@ -1362,6 +1363,12 @@ export async function autonomyTick(
13621363 } catch (error) {
13631364 log.error("sentinel failed", { error: String(error) });
13641365 }
1366 // A hold marked as red lets go on green or after a day (`hold_sweep.ts`).
1367 try {
1368 sweepRedHolds(app, now.getTime());

This runs every tick but nothing in production can create a row it would release. Wire the placer in the same pull, or defer this hook until it exists.

1369 } catch (error) {
1370 log.error("hold sweep failed", { error: String(error) });
1371 }
13651372 // Review is not autonomy's alone: an agent's green pull is read by the
13661373 // crew's reviewer in every repository that has one.
13671374 try {
src/crew/hold_sweep.ts added +50
@@ -0,0 +1,50 @@
1 /**
2 * A hold the forge marked as red lets itself go (#6847): when the pull's
3 * check on that same head is green, or after a day whatever the check says.
4 * Green on an until-green hold is `releaseOnGreen`'s (`actions/holds.ts`).
5 * A person's hold (no `red_head`) is never read here, and a green on an
6 * older or newer head is not the head the hold was red on.
7 */
8
9 import type { App } from "../app.ts";
10 import { redHolds, releaseRedHold } from "../core/holds.ts";
11 import { getIssueById } from "../core/issues.ts";
12 import { getPull } from "../core/pulls.ts";
13 import { verdictForHead } from "../core/runs.ts";
14
15 /** How long a marked hold may stand. */
16 export const RED_HOLD_MS = 24 * 60 * 60 * 1000;
17
18 /** Release every marked hold that is green on its head or a day old; how many went. */
19 export function sweepRedHolds(app: App, now: number): number {
20 let released = 0;
21 for (const hold of redHolds(app.db)) {
22 const issue = getIssueById(app.db, hold.pullId);
23 if (!issue) continue;
24 const pull = getPull(app.db, issue.repoId, issue.number);
25 if (!pull) continue;
26 const checks = verdictForHead(app.db, pull.id, hold.redHead);
27 // An until-green hold is let go on green by `releaseOnGreen`; here it only ages out.
28 const green =
29 !hold.untilGreen &&
30 checks.length > 0 &&
31 checks.every((c) => c.state === "ok");
32 const aged = now - Date.parse(hold.heldAt) >= RED_HOLD_MS;
33 if (!green && !aged) continue;
outdated

This posts the forge note as heldBy, a real person. Misattributes the words; use a system author.

34 if (!releaseRedHold(app.db, hold.pullId, new Date(now).toISOString()))
35 continue;
36 released++;
37 // The forge's own words, as an event with no actor, as `releaseOnGreen` does.
38 app.bus.append({
39 kind: "pull.released",
40 repoId: pull.repoId,
41 payload: {
42 number: pull.number,
43 note: green
44 ? `hold released: green on ${hold.redHead.slice(0, 7)}`
45 : "hold released: it had stood for 24 hours",
46 },
47 });
48 }
49 return released;
50 }
test/holds.test.ts +104
@@ -32,6 +32,7 @@ import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts";
3232 import { createToken } from "../src/core/tokens.ts";
3333 import { createUser, getUserByName, type User } from "../src/core/users.ts";
3434 import { landIfGreen, parkPull } from "../src/crew/autonomy.ts";
35 import { RED_HOLD_MS, sweepRedHolds } from "../src/crew/hold_sweep.ts";
3536 import { sendToReview, sweepReviews } from "../src/crew/review.ts";
3637 import { serve, type Servers } from "../src/http/server.ts";
3738 import { recordAskLedger } from "../src/web/studio/asks.ts";
@@ -975,3 +976,106 @@ describe("a hold set until green", () => {
975976 expect(releaseOnGreen(app, verdict(pull, "ok"))).toBe(true);
976977 });
977978 });
979
980 describe("a hold marked as red lets itself go (#6847)", () => {
981 type Forge = Awaited<ReturnType<typeof forge>>;
982 /** A passing check on `commitOid`, which need not be the pull's head. */
983 const passOn = (f: Forge, pull: Pull, oid: string) => {
984 const run = createRun(f.app.db, {
985 repoId: f.repo.id,
986 kind: "check",
987 issueId: pull.id,
988 env: `pull-${pull.number}`,
989 commitOid: oid,
990 reason: "pull",
991 });
992 claimRun(f.app.db, run.id);
993 finishRun(f.app.db, run.id, { state: "ok", summary: "check passed" });
994 };
995 const releaseNotes = (f: Forge, pull: Pull) =>
996 f.app.db
997 .query<{ payload: string }, []>(
998 "SELECT payload FROM event WHERE kind = 'pull.released'",
999 )
1000 .all()
1001 .map((r) => JSON.parse(r.payload))
1002 .filter((p) => p.number === pull.number)
1003 .map((p) => p.note as string);
1004 const heldAt = (f: Forge, pull: Pull) =>
1005 Date.parse(getHold(f.app.db, pull.id)!.heldAt);
1006
1007 test("green on the same head releases it, with one line on the pull", async () => {
1008 const f = await forge("forge-hold-red-", "public");
1009 try {
1010 const pull = f.passCheck(await f.pullOn("same"));
1011 setHold(f.app.db, pull.id, f.alice.id, "red", undefined, pull.headOid);
1012 expect(sweepRedHolds(f.app, heldAt(f, pull) + 1000)).toBe(1);
1013 expect(getHold(f.app.db, pull.id)).toBeUndefined();
1014 expect(releaseNotes(f, pull)).toEqual([
1015 `hold released: green on ${pull.headOid.slice(0, 7)}`,
1016 ]);
1017 } finally {
1018 await f.close();
1019 }
1020 });
1021
1022 test("green on an older head does not release it", async () => {
1023 const f = await forge("forge-hold-red-", "public");
1024 try {
1025 const pull = await f.pullOn("older");
1026 passOn(f, pull, "0".repeat(40));
1027 setHold(f.app.db, pull.id, f.alice.id, "red", undefined, pull.headOid);
1028 expect(sweepRedHolds(f.app, heldAt(f, pull) + 1000)).toBe(0);
1029 expect(getHold(f.app.db, pull.id)).toBeDefined();
1030 } finally {
1031 await f.close();
1032 }
1033 });
1034
1035 test("24 hours releases it, whatever the check says", async () => {
1036 const f = await forge("forge-hold-red-", "public");
1037 try {
1038 const pull = await f.pullOn("aged");
1039 setHold(f.app.db, pull.id, f.alice.id, "red", undefined, pull.headOid);
1040 const at = heldAt(f, pull);
1041 expect(sweepRedHolds(f.app, at + RED_HOLD_MS - 1000)).toBe(0);
1042 expect(sweepRedHolds(f.app, at + RED_HOLD_MS)).toBe(1);
1043 expect(getHold(f.app.db, pull.id)).toBeUndefined();
1044 expect(releaseNotes(f, pull)[0]).toContain("24 hours");
1045 } finally {
1046 await f.close();
1047 }
1048 });
1049
1050 test("a person's hold stays, plain or until green, green or old; a re-hold unmarks a marked one", async () => {
1051 const f = await forge("forge-hold-red-", "public");
1052 try {
1053 const plain = f.passCheck(await f.pullOn("plain"));
1054 setHold(f.app.db, plain.id, f.alice.id, "wait for design");
1055 const green = f.passCheck(await f.pullOn("green"));
1056 expect(holdPull(f.app, f.alice, f.repo, green, "on purpose", false, "green").ok).toBe(true);
1057 const again = f.passCheck(await f.pullOn("again"));
1058 setHold(f.app.db, again.id, f.alice.id, "red", undefined, again.headOid);
1059 setHold(f.app.db, again.id, f.alice.id, "mine now");
1060 const far = Date.now() + RED_HOLD_MS * 10;
1061 expect(sweepRedHolds(f.app, far)).toBe(0);
1062 for (const p of [plain, green, again])
1063 expect(getHold(f.app.db, p.id)).toBeDefined();
1064 } finally {
1065 await f.close();
1066 }
1067 });
1068
1069 test("an until-green hold marked red ages out but is not green-released here", async () => {
1070 const f = await forge("forge-hold-red-", "public");
1071 try {
1072 const pull = f.passCheck(await f.pullOn("both"));
1073 setHold(f.app.db, pull.id, f.alice.id, "red", "green", pull.headOid);
1074 const at = heldAt(f, pull);
1075 expect(sweepRedHolds(f.app, at + 1000)).toBe(0);
1076 expect(sweepRedHolds(f.app, at + RED_HOLD_MS)).toBe(1);
1077 } finally {
1078 await f.close();
1079 }
1080 });
1081 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs