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

A crew pull that has stayed conflicted for 3 days with no push is closed with one line, and its issue goes back to ready for a fresh run from main #5945

Parked opened this
Closed without merging

scout/forge:scout/5940-a-crew-pull-that-has-stayed-conflicted-f → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +400 −0
src/crew/autonomy.ts +4
@@ -12,6 +12,7 @@
1212 * repository's origin.
1313 */
1414
15 import { closedToday } from "./stale_pulls.ts";
1516 import type { Database } from "bun:sqlite";
1617 import type { App } from "../app.ts";
1718 import { ceilingReached, forgeCeiling } from "../flow/ceiling.ts";
@@ -1257,6 +1258,7 @@ export async function autonomyTick(
12571258 activeRuns(app, current.id),
12581259 );
12591260 const boots = bootCounts(app.config.dataDir);
1261 const staleToday = closedToday(app, current.id, now);
12601262 // Says when the governor lowers the slots (#6375); one feed item per change.
12611263 const governor = announceGovernor(app, current, now);
12621264 log.info("tick", {
@@ -1274,6 +1276,8 @@ export async function autonomyTick(
12741276 ...(held.by.hot_file.length === 0 ? {} : { hot: hotWords(hotFiles(current.id)) }),
12751277 // Stalled issues this tick parked and freed for another builder (#4073).
12761278 unstuck,
1279 // Crew pulls closed today after 3 days in conflict (#5940).
1280 ...(staleToday === 0 ? {} : { staleClosed: staleToday }),
12771281 // Open pulls, opened/landed per hour and per 6 h, conflicted (#5935).
12781282 ...flowVitals(app.db, now),
12791283 active: status.active,
src/crew/stale_pulls.ts added +193
@@ -0,0 +1,193 @@
1 /**
2 * A crew pull that stays conflicted is only a longer rebase (#5940). One
3 * that has been conflicted for 72 hours, with no push in that time, is
4 * closed with one line, and the issue it closed goes back to ready,
5 * unassigned, to be built fresh from main.
6 *
7 * Never closed: a person's pull, a held pull, a pull whose head carries an
8 * approval newer than the conflict (it is waiting on a person or the
9 * queue, not on the crew), or a pull that names no issue to build again.
10 *
11 * `watchConflicts` learns, off the request path, since when each open crew
12 * pull has conflicted at its present head; a push moves the head and the
13 * clock starts again. The clock lives in this process: a restart gives
14 * every conflicted pull a fresh 72 hours, which only delays a close.
15 * `closeStalePulls` runs the daily pass.
16 */
17
18 import type { Database } from "bun:sqlite";
19 import type { App } from "../app.ts";
20 import { assignIssue, addComment, setIssueState } from "../actions/issues.ts";
21 import { crewRole } from "../core/agents.ts";
22 import { listEvents } from "../core/events.ts";
23 import { getHold } from "../core/holds.ts";
24 import { getIssue, getIssueById, getIssuesByIds } from "../core/issues.ts";
25 import { joinMany, closesFromBody, pullStatus, type Pull } from "../core/pulls.ts";
26 import { getRepoById } from "../core/repos.ts";
27 import { listReviews } from "../core/reviews.ts";
28 import { getUserById } from "../core/users.ts";
29 import { resolveOid } from "../git/merge.ts";
30 import { repoDir } from "../git/repos.ts";
31 import { createLog } from "../kernel/log.ts";
32
33 const log = createLog("stale-pulls");
34
35 /** How long a pull may stay conflicted, unpushed, before it is closed. */
36 export const STALE_MS = 72 * 3_600_000;
37
38 /** The event each close writes. */
39 export const STALE_CLOSED = "pull.stale_closed";
40
41 /** The most open pulls one look reads: enough for a busy forge, bounded for a strange one. */
42 const LOOK_LIMIT = 300;
43
44 interface Conflict {
45 readonly head: string;
46 /** `head:base`, the pair of tips the answer was taken between. */
47 readonly between: string;
48 /** Whether it conflicts as of that pair. */
49 readonly conflicted: boolean;
50 /** When it was first found conflicted at this head. */
51 readonly since: string;
52 }
53
54 const seenByDb = new WeakMap<Database, Map<string, Conflict>>();
55 const dayByDb = new WeakMap<Database, string>();
56
57 const seenIn = (db: Database): Map<string, Conflict> => {
58 let seen = seenByDb.get(db);
59 if (seen === undefined) seenByDb.set(db, (seen = new Map()));
60 return seen;
61 };
62
63 /** Record that this pull, at this head, is conflicted since `at` (the watch's own write; tests use it to age a pull). */
64 export function noteConflict(db: Database, pull: Pull, at: string): void {
65 seenIn(db).set(pull.id, {
66 head: pull.headOid,
67 between: `${pull.headOid}:`,
68 conflicted: true,
69 since: at,
70 });
71 }
72
73 /** Since when this pull has conflicted at its present head; undefined when it does not, or is not known to. */
74 export function conflictedSince(db: Database, pull: Pull): string | undefined {
75 const known = seenByDb.get(db)?.get(pull.id);
76 return known !== undefined && known.conflicted && known.head === pull.headOid
77 ? known.since
78 : undefined;
79 }
80
81 /** The open pulls a crew agent wrote, newest touched first. */
82 function crewPulls(db: Database): Pull[] {
83 const ids = db
84 .query<{ id: string }, [number]>(
85 `SELECT issue.id AS id FROM issue JOIN user ON user.id = issue.author_id
86 WHERE issue.kind = 'pull' AND issue.state = 'open' AND user.is_agent = 1
87 ORDER BY issue.updated_at DESC LIMIT ?1`,
88 )
89 .all(LOOK_LIMIT)
90 .map((r) => r.id);
91 return joinMany(db, [...getIssuesByIds(db, ids).values()]).filter(
92 (p) =>
93 p.authorIsAgent &&
94 p.mergedAt === null &&
95 crewRole(db, p.repoId, p.authorId) !== undefined,
96 );
97 }
98
99 /**
100 * Look again at every open crew pull: git is asked once per pair of tips,
101 * and a pull that merges forgets its clock. Never throws.
102 */
103 export async function watchConflicts(app: App, now: Date = new Date()): Promise<void> {
104 const seen = seenIn(app.db);
105 const pulls = crewPulls(app.db);
106 const open = new Set(pulls.map((p) => p.id));
107 for (const id of seen.keys()) if (!open.has(id)) seen.delete(id);
108 for (const pull of pulls) {
109 const repo = getRepoById(app.db, pull.repoId);
110 if (!repo) continue;
111 try {
112 const dir = repoDir(app.reposRoot, repo.ownerName, repo.name);
113 const base = (await resolveOid(dir, `refs/heads/${pull.baseBranch}`)) ?? "";
114 const between = `${pull.headOid}:${base}`;
115 const was = seen.get(pull.id);
116 if (was?.between === between) continue;
117 const status = await pullStatus(dir, pull);
118 const conflicted = !status.mergeable && status.conflicts.length > 0;
119 const since =
120 conflicted && was?.conflicted === true && was.head === pull.headOid
121 ? was.since
122 : now.toISOString();
123 seen.set(pull.id, { head: pull.headOid, between, conflicted, since });
124 } catch (error) {
125 log.warn("conflict watch skipped a pull", { pull: pull.number, error: String(error) });
126 }
127 }
128 }
129
130 /** Whether a person's approval of the pull came after the conflict began. */
131 function approvedSince(db: Database, pull: Pull, since: string): boolean {
132 return listReviews(db, pull.id).some((r) => r.state === "approved" && r.createdAt > since);
133 }
134
135 /** The one line the pull is closed with. */
136 export const closedLine = (issueNumber: number): string =>
137 `Closed after 3 days in conflict; #${issueNumber} is ready again and will be built fresh from main.`;
138
139 /** Close one stale pull and free its issue. False when it is not to be closed. */
140 function close(app: App, pull: Pull, now: Date): boolean {
141 const since = conflictedSince(app.db, pull);
142 if (since === undefined || now.getTime() - Date.parse(since) < STALE_MS) return false;
143 if (getHold(app.db, pull.id) !== undefined) return false;
144 if (approvedSince(app.db, pull, since)) return false;
145 const repo = getRepoById(app.db, pull.repoId);
146 const by = repo?.autonomyBy ? getUserById(app.db, repo.autonomyBy) : undefined;
147 const issue = getIssueById(app.db, pull.id);
148 if (!repo || !by || !issue || issue.state !== "open") return false;
149 const about = closesFromBody(pull.body)
150 .map((n) => getIssue(app.db, repo.id, n))
151 .find((i) => i !== undefined && i.kind === "issue" && i.state === "open");
152 if (about === undefined) return false;
153 addComment(app, by, repo, issue, closedLine(about.number));
154 const closed = setIssueState(app, by, repo, issue, "closed", { reason: "parked" });
155 if (!closed.ok) return false;
156 if (about.assigneeId !== null) assignIssue(app, by, repo, about, null);
157 app.bus.append({
158 kind: STALE_CLOSED,
159 repoId: repo.id,
160 actorId: by.id,
161 payload: { number: pull.number, issue: about.number, since },
162 });
163 log.info("stale pull closed", { repo: `${repo.ownerName}/${repo.name}`, pull: pull.number });
164 return true;
165 }
166
167 /**
168 * The daily pass: close every crew pull that has stayed conflicted for 72
169 * hours. It runs once per UTC day per database, however often it is asked.
170 * Returns how many it closed.
171 */
172 export function closeStalePulls(app: App, now: Date = new Date()): number {
173 const day = now.toISOString().slice(0, 10);
174 if (dayByDb.get(app.db) === day) return 0;
175 dayByDb.set(app.db, day);
176 let closed = 0;
177 for (const pull of crewPulls(app.db)) {
178 try {
179 if (close(app, pull, now)) closed++;
180 } catch (error) {
181 log.warn("stale pull close skipped a pull", { pull: pull.number, error: String(error) });
182 }
183 }
184 return closed;
185 }
186
187 /** How many stale pulls the crew closed in this repository today (UTC), for the tick line. */
188 export function closedToday(app: App, repoId: string, now: Date = new Date()): number {
189 const from = `${now.toISOString().slice(0, 10)}T00:00:00.000Z`;
190 return listEvents(app.db, { repoId, kind: STALE_CLOSED, limit: 500 }).filter(
191 (e) => e.createdAt >= from,
192 ).length;
193 }
src/main.ts +8
@@ -52,6 +52,7 @@ import { graphInstagram } from "./crew/instagram.ts";
5252 import { peopleOnCrews } from "./core/agents.ts";
5353 import { startNotifier } from "./crew/notify.ts";
5454 import { sweepStaleAsks } from "./crew/stale_asks.ts";
55 import { closeStalePulls, watchConflicts } from "./crew/stale_pulls.ts";
5556 import { recordAskLedger } from "./web/studio/asks.ts";
5657 import { startPromptRoutine } from "./crew/prompts.ts";
5758 import { pruneTokens } from "./core/tokens.ts";
@@ -471,6 +472,13 @@ runSync(app); // one pass on boot: a forge that was down has fetching to do
471472 // expired: what asked each person, and how it ended.
472473 const tickAsks = async (): Promise<void> => {
473474 await sweepStaleAsks(app);
475 // Crew pulls conflicted for 72 h are closed once a day (`crew/stale_pulls.ts`).
476 try {
477 await watchConflicts(app);
478 closeStalePulls(app);
479 } catch (error) {
480 app.log.error("stale pulls sweep failed", { error: String(error) });
481 }
474482 // Repositories ruled "decider" have their hand-overs answered here.
475483 try {
476484 await sweepStanding(app);
test/stale_pulls.test.ts added +195
@@ -0,0 +1,195 @@
1 /**
2 * A crew pull conflicted for 72 hours with no push is closed once a day and
3 * its issue made ready (#5940); a person's, a held one, and one approved
4 * after the conflict began are left alone.
5 */
6 import { afterAll, beforeAll, 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 { openIssue } from "../src/actions/issues.ts";
13 import { openPull } from "../src/actions/pulls.ts";
14 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
15 import { listEvents } from "../src/core/events.ts";
16 import { setHold } from "../src/core/holds.ts";
17 import { getIssueById, listComments } from "../src/core/issues.ts";
18 import { getPull } from "../src/core/pulls.ts";
19 import { type Repo } from "../src/core/repos.ts";
20 import { submitReview } from "../src/core/reviews.ts";
21 import { createUser, getUserByName, type User } from "../src/core/users.ts";
22 import {
23 closeStalePulls,
24 closedLine,
25 closedToday,
26 conflictedSince,
27 noteConflict,
28 STALE_CLOSED,
29 watchConflicts,
30 } from "../src/crew/stale_pulls.ts";
31
32 let dataDir: string;
33 let app: App;
34 let alice: User;
35 let mason: User;
36 let repo: Repo;
37 let n = 0;
38
39 const HOUR = 3_600_000;
40 const NOW = new Date();
41 let day = 0;
42 /** The pass's clock: each test moves it to a later UTC day so the daily pass runs again. */
43 let clock = NOW;
44 const advance = () => (clock = new Date(NOW.getTime() + ++day * 24 * HOUR));
45 const ago = (hours: number) => new Date(clock.getTime() - hours * HOUR).toISOString();
46
47 beforeAll(async () => {
48 dataDir = await mkdtemp(join(tmpdir(), "forge-stale-pulls-"));
49 app = createApp({ dataDir, port: 0 });
50 const a = await createUser(app.db, {
51 origin: "seed",
52 trusted: true,
53 username: "alice",
54 email: "[email protected]",
55 password: "a-good-passphrase",
56 adminIfFirst: true,
57 });
58 if (!a.ok) throw new Error(a.error);
59 alice = a.value;
60 const made = await createRepo(app, alice, { name: "engine", visibility: "private" });
61 if (!made.ok) throw new Error(String(made.error));
62 repo = made.value;
63 const seeded = await commitToBranch(app, alice, repo, {
64 message: "Start",
65 files: [{ path: "shared.ts", content: "export const shared = 0;\n" }],
66 });
67 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
68 const agent = await createAgent(app, alice, { username: "mason" });
69 if (!agent.ok) throw new Error(String(agent.error));
70 expect(addToCrew(app, alice, repo, "mason").ok).toBe(true);
71 mason = getUserByName(app.db, "mason")!;
72 app.db
73 .query("UPDATE repo SET autonomy = 'autonomous', autonomy_by = ?2 WHERE id = ?1")
74 .run(repo.id, alice.id);
75 });
76
77 afterAll(async () => {
78 app.db.close();
79 await rm(dataDir, { recursive: true, force: true });
80 });
81
82 /** An open pull by `who` that closes a fresh issue assigned to mason. */
83 async function pullBy(who: User) {
84 const k = ++n;
85 const ask = openIssue(app, alice, repo, { title: `Ask ${k}` });
86 if (!ask.ok) throw new Error(String(ask.error));
87 app.db.query("UPDATE issue SET assignee_id = ?2 WHERE id = ?1").run(ask.value.id, mason.id);
88 const branch = await commitToBranch(app, alice, repo, {
89 branch: `feature/s${k}`,
90 base: "main",
91 message: `Shared ${k}`,
92 files: [{ path: "shared.ts", content: `export const shared = 'branch ${k}';\n` }],
93 });
94 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
95 const opened = await openPull(app, who, repo, {
96 title: `Shared ${k}`,
97 body: `Closes #${ask.value.number}`,
98 baseBranch: "main",
99 headBranch: `feature/s${k}`,
100 });
101 if (!opened.ok) throw new Error(String(opened.error));
102 return { pull: opened.value, ask: ask.value };
103 }
104
105 const open = (id: string) => getIssueById(app.db, id)!.state === "open";
106
107 test("a pull main moved under is watched, and a 73-hour crew pull is closed with its issue made ready", async () => {
108 const { pull, ask } = await pullBy(mason);
109 const moved = await commitToBranch(app, alice, repo, {
110 branch: "main",
111 message: "Shared, main's way",
112 files: [{ path: "shared.ts", content: "export const shared = 'main';\n" }],
113 });
114 if (!moved.ok) throw new Error(JSON.stringify(moved.error));
115 await watchConflicts(app, clock);
116 const since = conflictedSince(app.db, getPull(app.db, repo.id, pull.number)!);
117 expect(since).toBeDefined();
118 // Seen just now: not yet three days.
119 advance();
120 expect(closeStalePulls(app, clock)).toBe(0);
121 expect(open(pull.id)).toBe(true);
122
123 advance();
124 noteConflict(app.db, getPull(app.db, repo.id, pull.number)!, ago(73));
125 expect(closeStalePulls(app, clock)).toBe(1);
126 expect(open(pull.id)).toBe(false);
127 const freed = getIssueById(app.db, ask.id)!;
128 expect(freed.state).toBe("open");
129 expect(freed.assigneeId).toBeNull();
130 const said = listComments(app.db, pull.id).map((c) => c.body);
131 expect(said).toContain(closedLine(ask.number));
132 expect(closedLine(ask.number)).toBe(
133 `Closed after 3 days in conflict; #${ask.number} is ready again and will be built fresh from main.`,
134 );
135 const events = listEvents(app.db, { repoId: repo.id, kind: STALE_CLOSED });
136 expect(events.length).toBe(1);
137 expect(events[0]!.payload.number).toBe(pull.number);
138 expect(closedToday(app, repo.id)).toBe(1);
139 });
140
141 test("a 71-hour pull is left alone", async () => {
142 const { pull, ask } = await pullBy(mason);
143 advance();
144 noteConflict(app.db, getPull(app.db, repo.id, pull.number)!, ago(71));
145 expect(closeStalePulls(app, clock)).toBe(0);
146 expect(open(pull.id)).toBe(true);
147 expect(getIssueById(app.db, ask.id)!.assigneeId).toBe(mason.id);
148 // Leave it out of the later passes: it would be stale by then.
149 app.db.query("UPDATE issue SET state = 'closed' WHERE id = ?1").run(pull.id);
150 });
151
152 test("a person's pull, a held pull and a pull approved after the conflict are never closed", async () => {
153 const person = await pullBy(alice);
154 const held = await pullBy(mason);
155 const approved = await pullBy(mason);
156 const older = await pullBy(mason);
157 advance();
158 for (const { pull } of [person, held, approved, older])
159 noteConflict(app.db, getPull(app.db, repo.id, pull.number)!, ago(100));
160 const held1 = setHold(app.db, held.pull.id, alice.id, "wait for the design");
161 expect(held1.pullId).toBe(held.pull.id);
162 // An approval after the conflict began protects it...
163 submitReview(app.db, {
164 issueId: approved.pull.id,
165 authorId: alice.id,
166 state: "approved",
167 commitOid: approved.pull.headOid,
168 createdAt: ago(10),
169 });
170 // ...one from before it does not.
171 submitReview(app.db, {
172 issueId: older.pull.id,
173 authorId: alice.id,
174 state: "approved",
175 commitOid: older.pull.headOid,
176 createdAt: ago(200),
177 });
178 expect(closeStalePulls(app, clock)).toBe(1);
179 expect(open(person.pull.id)).toBe(true);
180 expect(open(held.pull.id)).toBe(true);
181 expect(open(approved.pull.id)).toBe(true);
182 expect(open(older.pull.id)).toBe(false);
183 });
184
185 test("the pass runs once a day", async () => {
186 const { pull } = await pullBy(mason);
187 advance();
188 expect(closeStalePulls(app, clock)).toBe(0);
189 noteConflict(app.db, getPull(app.db, repo.id, pull.number)!, ago(100));
190 expect(closeStalePulls(app, clock)).toBe(0);
191 expect(open(pull.id)).toBe(true);
192 advance();
193 noteConflict(app.db, getPull(app.db, repo.id, pull.number)!, ago(100));
194 expect(closeStalePulls(app, clock)).toBe(1);
195 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs