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

A crew hold whose reason has not cleared in 24 h expires with a note that names the reason #6882

Closed opened this
Closed without merging

scout/forge:scout/6425-a-crew-hold-whose-reason-has-not-cleared → main 4 commits fork 4 threads · 4 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +192 −4
src/actions/holds.ts +57 −1
@@ -14,13 +14,20 @@
1414 import type { App } from "../app.ts";
1515 import {
1616 cleanWhy,
17 CREW_HOLD_MS,
18 crewHoldsPlacedBefore,
1719 getHold,
1820 releaseGreenHold,
1921 releaseHold,
2022 setHold,
2123 type Hold,
2224 } from "../core/holds.ts";
23 import { getIssueById } from "../core/issues.ts";
25 import { getIssueById, setAssignee } from "../core/issues.ts";
26 import { getRepoById } from "../core/repos.ts";
27 import { getUserById } from "../core/users.ts";
28 import { transact } from "../kernel/db.ts";
29 import { builderOf } from "./give_back.ts";
30 import { addComment } from "./issues.ts";
2431 import { getPull, type Pull } from "../core/pulls.ts";
2532 import { checksForHead, type Run } from "../core/runs.ts";
2633 import { canRead, canWrite, type Repo } from "../core/repos.ts";
@@ -141,3 +148,52 @@ export function releaseOnGreen(app: App, run: Run): boolean {
141148 });
142149 return true;
143150 }
151
152 /**
153 * The tick's sweep: a hold the crew placed (an agent's) that has stood 24 h
154 * is released with a comment that names its reason, and the pull goes back
155 * to its builder. A person's hold is never touched. Returns the pulls let go.
156 */
157 export function expireCrewHolds(app: App, now = new Date()): number {
158 const before = new Date(now.getTime() - CREW_HOLD_MS).toISOString();
159 let released = 0;
outdated

A missing row skips the hold on every tick with no trace. Log a warning with the pull id, or release it.

160 for (const hold of crewHoldsPlacedBefore(app.db, before)) {
161 const issue = getIssueById(app.db, hold.pullId);
162 const repo = issue ? getRepoById(app.db, issue.repoId) : undefined;
163 const holder = getUserById(app.db, hold.heldBy);
164 if (!issue || !repo || !holder) {
165 // Nothing to comment on or hand back: let it go, but not in silence.
166 app.log.warn("hold on a missing pull, issue or holder released", { pull: hold.pullId });
167 if (releaseHold(app.db, hold.pullId, hold.heldBy)) released++;
168 continue;
169 }
170 const pull = issue.kind === "pull" ? getPull(app.db, repo.id, issue.number) : undefined;
171 const open = pull !== undefined && pull.state === "open";
outdated

Medium: addComment returns a Result, and it is ignored here. If the holder has since left the crew of a private repo, requireParticipant refuses. The hold is then released and the pull reassigned, but the note naming the reason, which is the point of #6425, never appears, and nothing is logged. Check .ok. When it fails, either app.log.warn with the pull number, or post the note as the builder, who can still take part.

172 transact(app.db, () => {
173 if (!releaseHold(app.db, hold.pullId, hold.heldBy)) return;
174 released++;
175 app.bus.append({
outdated

This fallback is untested. Add a test with the holder removed from the crew and assert who posted the note.

176 kind: "pull.released",
177 repoId: repo.id,
178 payload: { number: issue.number, note: "hold expired after 24 h" },
179 });
180 if (!open) return;
181 const builder = builderOf(app.db, repo, issue);
182 const note = `Hold expired after 24 h: ${hold.why}.${builder ? ` Back to @${builder.username}.` : ""}`;
183 // The holder may have left the crew; the builder can still speak.
184 let said = addComment(app, holder, repo, issue, note);
185 if (!said.ok && builder) said = addComment(app, builder, repo, issue, note);
186 if (!said.ok)
187 app.log.warn("hold expiry note not posted", { pull: issue.number, repo: repo.id, error: said.error });
188 // The assignment event is what starts the builder's run.
189 if (builder && setAssignee(app.db, issue.id, builder.id))
190 app.bus.append({
191 kind: "pull.assigned",
192 repoId: repo.id,
193 actorId: holder.id,
194 payload: { number: issue.number, assignee: builder.username, agent: true },
195 });
196 });
197 }
198 return released;
199 }
src/core/holds.ts +14
@@ -142,6 +142,20 @@ export function releaseGreenHold(db: Database, pullId: string): boolean {
142142 );
143143 }
144144
145 /** How long a hold the crew placed may stand before the forge lets it go. */
146 export const CREW_HOLD_MS = 24 * 3_600_000;
147
148 /** Standing holds the crew placed (an agent's) at or before `before`, oldest first. */
149 export function crewHoldsPlacedBefore(db: Database, before: string): Hold[] {
150 return db
151 .query<Row, [string]>(
152 `${STANDING} AND user.is_agent = 1 AND pull_hold.held_at <= ?1
153 ORDER BY pull_hold.held_at, pull_hold.id`,
154 )
155 .all(before)
156 .map(fromRow);
157 }
158
145159 /** When a hold on this pull was last released, if one ever was. */
146160 export function lastRelease(db: Database, pullId: string): string | undefined {
147161 return (
src/main.ts +6
@@ -54,6 +54,7 @@ import { typeSafeDecider } from "./crew/decide.ts";
5454 import { graphInstagram } from "./crew/instagram.ts";
5555 import { peopleOnCrews } from "./core/agents.ts";
5656 import { startNotifier } from "./crew/notify.ts";
57 import { expireCrewHolds } from "./actions/holds.ts";
5758 import { tickClog } from "./flow/clog_tick.ts";
5859 import { sweepStaleAsks } from "./crew/stale_asks.ts";
5960 import { recordAskLedger } from "./web/studio/asks.ts";
@@ -558,6 +559,11 @@ const clogTick = (): void => {
558559 } catch (error) {
559560 app.log.warn("clog tick failed", { error: String(error) });
560561 }
562 try {
563 expireCrewHolds(app);
564 } catch (error) {
565 app.log.warn("hold expiry failed", { error: String(error) });
566 }
561567 };
562568 const clogTimer = setInterval(clogTick, 60_000);
563569 const processTimer = setInterval(processTick, 60_000);
test/holds.test.ts +115 −3
@@ -4,12 +4,12 @@
44 * or lands it. The refusals come first.
55 */
66
7 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test";
88 import { mkdtemp, rm } from "node:fs/promises";
99 import { tmpdir } from "node:os";
1010 import { join } from "node:path";
11 import { addToCrew, createAgent } from "../src/actions/crew.ts";
12 import { holdPull, releaseOnGreen, releasePull } from "../src/actions/holds.ts";
11 import { addToCrew, createAgent, removeFromCrew } from "../src/actions/crew.ts";
12 import { expireCrewHolds, holdPull, releaseOnGreen, releasePull } from "../src/actions/holds.ts";
1313 import { openIssue } from "../src/actions/issues.ts";
1414 import { mergePull, openPull } from "../src/actions/pulls.ts";
1515 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
@@ -25,6 +25,7 @@ import {
2525 setHold,
2626 WHY_MAX,
2727 } from "../src/core/holds.ts";
28 import { getIssueById, listComments } from "../src/core/issues.ts";
2829 import { getPull, type Pull } from "../src/core/pulls.ts";
2930 import { getRepoById, type Repo } from "../src/core/repos.ts";
3031 import { submitReview } from "../src/core/reviews.ts";
@@ -977,6 +978,117 @@ describe("a hold set until green", () => {
977978 });
978979 });
979980
981 describe("a crew hold that outlives its reason expires", () => {
982 const HOUR = 3_600_000;
983 async function setup() {
984 const f = await forge("forge-holds-expire-", "private");
985 const agent = await createAgent(f.app, f.alice, { username: "mason" });
986 if (!agent.ok) throw new Error(String(agent.error));
987 expect(addToCrew(f.app, f.alice, f.repo, "mason").ok).toBe(true);
988 const mason = getUserByName(f.app.db, "mason")!;
989 /** A pull mason built, held by `by` since `hours` ago. */
990 const held = async (branch: string, by: User, hours: number) => {
991 const pull = await f.pullOn(branch);
992 f.app.db.query("UPDATE issue SET author_id = ?2 WHERE id = ?1").run(pull.id, mason.id);
993 setHold(f.app.db, pull.id, by.id, "waiting on the vault key", undefined);
994 f.app.db
995 .query("UPDATE pull_hold SET held_at = ?2 WHERE issue_id = ?1")
996 .run(pull.id, new Date(Date.now() - hours * HOUR).toISOString());
997 return pull;
998 };
999 return { ...f, mason, held };
1000 }
1001
1002 test("a 25 h crew hold is released with a note naming the reason, and the pull goes back to its builder", async () => {
1003 const f = await setup();
1004 const pull = await f.held("old", f.mason, 25);
1005 expect(expireCrewHolds(f.app)).toBe(1);
1006 expect(getHold(f.app.db, pull.id)).toBeUndefined();
1007 const issue = getIssueById(f.app.db, pull.id)!;
1008 expect(issue.assigneeId).toBe(f.mason.id);
1009 const said = listComments(f.app.db, pull.id).map((c) => c.body);
1010 expect(said).toContain("Hold expired after 24 h: waiting on the vault key. Back to @mason.");
1011 expect(expireCrewHolds(f.app)).toBe(0);
1012 await f.close();
1013 });
1014
1015 test("a 25 h crew hold on a closed pull is released with no comment and no hand-back", async () => {
1016 const f = await setup();
1017 const pull = await f.held("closed", f.mason, 25);
1018 f.app.db.query("UPDATE issue SET state = 'closed' WHERE id = ?1").run(pull.id);
1019 const before = getIssueById(f.app.db, pull.id)!.assigneeId;
1020 expect(expireCrewHolds(f.app)).toBe(1);
1021 expect(getHold(f.app.db, pull.id)).toBeUndefined();
1022 expect(listComments(f.app.db, pull.id)).toHaveLength(0);
1023 expect(getIssueById(f.app.db, pull.id)!.assigneeId).toBe(before);
outdated

Add two tests next to this one: (1) a 25 h crew hold on a closed pull is released, with no comment and the assignee unchanged; (2) a 25 h crew hold on a pull a person authored is released, with the note Hold expired after 24 h: waiting on the vault key. and no assignee. These pin the !open and builder === undefined branches in expireCrewHolds.

1024 await f.close();
1025 });
1026
1027 test("a 25 h crew hold on a pull a person authored is released with a note that has no 'Back to'", async () => {
1028 const f = await setup();
1029 const pull = await f.held("person-built", f.mason, 25);
1030 f.app.db.query("UPDATE issue SET author_id = ?2 WHERE id = ?1").run(pull.id, f.alice.id);
1031 const before = getIssueById(f.app.db, pull.id)!.assigneeId;
1032 expect(expireCrewHolds(f.app)).toBe(1);
1033 expect(getHold(f.app.db, pull.id)).toBeUndefined();
1034 expect(listComments(f.app.db, pull.id).map((c) => c.body)).toEqual([
1035 "Hold expired after 24 h: waiting on the vault key.",
1036 ]);
1037 expect(getIssueById(f.app.db, pull.id)!.assigneeId).toBe(before);
1038 expect(before).not.toBe(f.mason.id);
1039 await f.close();
1040 });
1041
1042 test("when the holder has left the crew the note is still posted, by the builder", async () => {
1043 const f = await setup();
1044 const quill = await createAgent(f.app, f.alice, { username: "quill" });
1045 if (!quill.ok) throw new Error(String(quill.error));
1046 expect(addToCrew(f.app, f.alice, f.repo, "quill").ok).toBe(true);
1047 const pull = await f.held("left", getUserByName(f.app.db, "quill")!, 25);
1048 expect(removeFromCrew(f.app, f.alice, f.repo, "quill").ok).toBe(true);
1049 expect(expireCrewHolds(f.app)).toBe(1);
1050 const comments = listComments(f.app.db, pull.id);
1051 expect(comments.map((c) => c.body)).toEqual([
1052 "Hold expired after 24 h: waiting on the vault key. Back to @mason.",
1053 ]);
1054 expect(comments[0]!.authorId).toBe(f.mason.id);
1055 await f.close();
1056 });
1057
1058 test("when nobody can post the note the hold is still released and the loss is logged", async () => {
1059 const f = await setup();
1060 const quill = await createAgent(f.app, f.alice, { username: "quill" });
1061 if (!quill.ok) throw new Error(String(quill.error));
1062 expect(addToCrew(f.app, f.alice, f.repo, "quill").ok).toBe(true);
1063 const pull = await f.held("lost", getUserByName(f.app.db, "quill")!, 25);
1064 f.app.db.query("UPDATE issue SET author_id = ?2 WHERE id = ?1").run(pull.id, f.alice.id);
1065 expect(removeFromCrew(f.app, f.alice, f.repo, "quill").ok).toBe(true);
1066 const warn = spyOn(f.app.log, "warn").mockImplementation(() => {});
1067 expect(expireCrewHolds(f.app)).toBe(1);
1068 expect(getHold(f.app.db, pull.id)).toBeUndefined();
1069 expect(listComments(f.app.db, pull.id)).toHaveLength(0);
1070 expect(warn).toHaveBeenCalledWith("hold expiry note not posted", expect.objectContaining({ pull: pull.number }));
1071 await f.close();
1072 });
1073
1074 test("a 23 h crew hold stays", async () => {
1075 const f = await setup();
1076 const pull = await f.held("young", f.mason, 23);
1077 expect(expireCrewHolds(f.app)).toBe(0);
1078 expect(getHold(f.app.db, pull.id)).toBeDefined();
1079 expect(listComments(f.app.db, pull.id)).toHaveLength(0);
1080 await f.close();
1081 });
1082
1083 test("a person's hold never expires", async () => {
1084 const f = await setup();
1085 const pull = await f.held("person", f.alice, 500);
1086 expect(expireCrewHolds(f.app)).toBe(0);
1087 expect(getHold(f.app.db, pull.id)).toBeDefined();
1088 await f.close();
1089 });
1090 });
1091
9801092 describe("an until-green hold, through the checks service", () => {
9811093 let f: Awaited<ReturnType<typeof forge>>;
9821094 let servers: Servers;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs