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
scout/forge:scout/6847-a-hold-the-forge-marked-as-red-releases → main 4 commits fork 2 threads · 2 unresolved
6 files
changed +226
−4
src/core/holds.ts +59 −4
| @@ -92,6 +92,8 @@ export function holdsFor( | ||
| 92 | 92 | /** |
| 93 | 93 | * Hold a pull. A pull holds one hold: holding one that is already held puts |
| 94 | 94 | * 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`). | |
| 95 | 97 | */ |
| 96 | 98 | export function setHold( |
| 97 | 99 | db: Database, |
| @@ -99,18 +101,19 @@ export function setHold( | ||
| 99 | 101 | by: string, |
| 100 | 102 | why: string, |
| 101 | 103 | until?: "green", |
| 104 | redHead: string | null = null, | |
| 102 | 105 | ): Hold { |
| 103 | 106 | const green = until === "green" ? 1 : 0; |
| 104 | 107 | const changed = db |
| 105 | 108 | .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 | |
| 107 | 110 | WHERE issue_id = ?1 AND released_at IS NULL`, |
| 108 | 111 | ) |
| 109 | .run(pullId, by, why, green).changes; | |
| 112 | .run(pullId, by, why, green, redHead).changes; | |
| 110 | 113 | if (changed === 0) |
| 111 | 114 | 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); | |
| 114 | 117 | return getHold(db, pullId)!; |
| 115 | 118 | } |
| 116 | 119 | |
| @@ -127,6 +130,58 @@ export function releaseHold(db: Database, pullId: string, by: string): boolean { | ||
| 127 | 130 | ); |
| 128 | 131 | } |
| 129 | 132 | |
| 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 | ||
| 130 | 185 | /** |
| 131 | 186 | * The forge lets go of a hold set "until green". False when none stands or |
| 132 | 187 | * 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 { | ||
| 50 | 50 | stopStands, |
| 51 | 51 | } from "../core/decisions.ts"; |
| 52 | 52 | import { getHold } from "../core/holds.ts"; |
| 53 | import { sweepRedHolds } from "./hold_sweep.ts"; | |
| 53 | 54 | import { certain } from "./decide.ts"; |
| 54 | 55 | import { decideAndRecord } from "./ledger.ts"; |
| 55 | 56 | import { sentinelTick } from "./sentinel.ts"; |
| @@ -1362,6 +1363,12 @@ export async function autonomyTick( | ||
| 1362 | 1363 | } catch (error) { |
| 1363 | 1364 | log.error("sentinel failed", { error: String(error) }); |
| 1364 | 1365 | } |
| 1366 | // A hold marked as red lets go on green or after a day (`hold_sweep.ts`). | |
| 1367 | try { | |
| 1368 | sweepRedHolds(app, now.getTime()); | |
| 1369 | } catch (error) { | |
| 1370 | log.error("hold sweep failed", { error: String(error) }); | |
| 1371 | } | |
| 1365 | 1372 | // Review is not autonomy's alone: an agent's green pull is read by the |
| 1366 | 1373 | // crew's reviewer in every repository that has one. |
| 1367 | 1374 | 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"; | ||
| 32 | 32 | import { createToken } from "../src/core/tokens.ts"; |
| 33 | 33 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| 34 | 34 | import { landIfGreen, parkPull } from "../src/crew/autonomy.ts"; |
| 35 | import { RED_HOLD_MS, sweepRedHolds } from "../src/crew/hold_sweep.ts"; | |
| 35 | 36 | import { sendToReview, sweepReviews } from "../src/crew/review.ts"; |
| 36 | 37 | import { serve, type Servers } from "../src/http/server.ts"; |
| 37 | 38 | import { recordAskLedger } from "../src/web/studio/asks.ts"; |
| @@ -975,3 +976,106 @@ describe("a hold set until green", () => { | ||
| 975 | 976 | expect(releaseOnGreen(app, verdict(pull, "ok"))).toBe(true); |
| 976 | 977 | }); |
| 977 | 978 | }); |
| 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 | }); |
new above
offline
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.