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

The engine picks the same issue every tick, is refused because a twin holds it, and starts nothing else #12379

Merged opened this in answer to

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

Merged

scout/forge:scout/12376-the-engine-picks-the-same-issue-every-ti → main 5 commits fork 2 threads · 2 unresolved

  • Merged. scout/12376-the-engine-picks-the-same-issue-every-ti was merged into main as 9b1761d.

Review

Sign in to review this change.
9 files changed +190 −13
src/crew/dispatch.ts +31 −6
@@ -436,6 +436,28 @@ export function outsideSeat(
436436 export const NOT_A_BUILDER_NOTE =
437437 "I am not a builder here, so I have not started on this. Assign it to a builder on the crew, or change my roles in Settings.";
438438
439 /** How long a twin refusal stays quiet in the log. */
440 const TWIN_LOG_MS = 3_600_000;
441
442 /**
443 * Per app, when each issue's twin refusal was last logged; expired entries are dropped as new
444 * ones are written.
445 */
446 const twinLogged = new WeakMap<App, Map<string, number>>();
447
448 /**
449 * Whether the refusal of `issueId` should be logged now; says so once per hour per issue.
450 */
451 function twinLogDue(app: App, issueId: string, at: number): boolean {
452 let seen = twinLogged.get(app);
453 if (seen === undefined) twinLogged.set(app, (seen = new Map()));
454 const last = seen.get(issueId);
455 if (last !== undefined && at - last < TWIN_LOG_MS) return false;
456 for (const [id, t] of seen) if (at - t >= TWIN_LOG_MS) seen.delete(id);
457 seen.set(issueId, at);
458 return true;
459 }
460
439461 /** Queue a run about an issue for an agent, unless one is already waiting. */
440462 export function queueAgentRun(
441463 app: App,
@@ -564,12 +586,15 @@ export function queueAgentRun(
564586 if (reason === "assigned" && group === undefined) {
565587 const twin = twinOf(app.db, issue);
566588 if (twin !== undefined) {
567 log.info("build run refused: a twin holds the work", {
568 repo: `${repo.ownerName}/${repo.name}`,
569 issue: issue.number,
570 twin: twin.number,
571 standing: twin.standing,
572 });
589 // One line per issue an hour, not one a tick.
590 if (twinLogDue(app, issue.id, Date.now())) {
591 log.info("build run refused: a twin holds the work", {
592 repo: `${repo.ownerName}/${repo.name}`,
593 issue: issue.number,
594 twin: twin.number,
595 standing: twin.standing,
596 });
597 }
573598 refusedAsTwin = twin;
574599 return undefined;
575600 }
src/crew/held.ts +26 −1
@@ -26,6 +26,9 @@
2626 * Only under ask: a repository on go's builders retry it, bounded by `run_cap`.
2727 * Never for good: a change in its inputs, or a day, tries it again (`retry.ts`).
2828 * - `red_fix`: a fix for a red main already covers it (`red_fix.ts`).
29 * - `twin`: another issue with the same title holds the work: a run going, a
30 * pull open, or a pull landed since this one was opened (`twin.ts`). The
31 * dispatcher would refuse the build, so the tick never picks it.
2932 * - `hot_file`: a file it names is touched by `HOT_PULLS` or more open
3033 * pulls, or an admin holds it (`file_holds.ts`); it starts when that pile
3134 * drains under the limit or the hold is released (`hot_files.ts`).
@@ -56,6 +59,8 @@ import { heldFiles, type FileHold } from "./file_holds.ts";
5659 import { expectedFiles, hotFiles, hotOverlap, pullsTouching, relievedFiles } from "./hot_files.ts";
5760 import { coveredByRedFix } from "./red_fix.ts";
5861 import { landedUnder } from "./stalls.ts";
62 import { isShared, sharedTitles, twinOf, type Twin } from "./twin.ts";
63 import { variantGroupOf } from "../core/runs.ts";
5964
6065 export const HOLDS = [
6166 "running",
@@ -68,6 +73,7 @@ export const HOLDS = [
6873 "run_cap",
6974 "parked_twice",
7075 "red_fix",
76 "twin",
7177 "hot_file",
7278 ] as const;
7379
@@ -85,6 +91,8 @@ export interface Held {
8591 readonly pinned: ReadonlyMap<number, FileHold>;
8692 /** Of the `hot_file` ones, those that wait because another issue already relieves the same path: issue number to that path and issue. */
8793 readonly relieving: ReadonlyMap<number, { path: string; by: number }>;
94 /** Of the `twin` ones: issue number to the twin that holds the same work. */
95 readonly twins: ReadonlyMap<number, Twin>;
8896 }
8997
9098 export interface LastRun {
@@ -187,6 +195,7 @@ export function heldBy(
187195 const hot = hotFiles(repo.id, admit);
188196 const pins = new Map(heldFiles(app.db, repo.id).map((h) => [h.path, h]));
189197 const pinned = new Map<number, FileHold>();
198 const twins = new Map<number, Twin>();
190199 // The hold an admin set on a file the issue names, if any: the first by hold order.
191200 const pinOf = (issue: Issue): FileHold | undefined =>
192201 pins.size === 0 ? undefined : expectedFiles(issue).map((f) => pins.get(f)).find((h) => h !== undefined);
@@ -231,6 +240,7 @@ export function heldBy(
231240 (i) => runCapOf(repo, i, app.config),
232241 ));
233242 const lastRuns = lastAgentRuns(app, ready.map((r) => r.issue.id));
243 let shared: Set<string> | undefined;
234244 const holdOf = (issue: Issue): HoldName | null => {
235245 if (busyIssues.has(issue.id)) return "running";
236246 if (proposed.has(issue.number)) return "open_pull";
@@ -243,6 +253,16 @@ export function heldBy(
243253 if (!isGo(repo) && parkedCount(app.db, repo, issue.id) >= PARK_STRIKES && retryReason(app.db, repo, issue, now) === undefined)
244254 return "parked_twice";
245255 if (coveredByRedFix(app.db, repo.id, issue)) return "red_fix";
256 // Variants share a title on purpose; any other same-title work in hand holds this one.
257 // Only a title another issue or pull bears can have a twin: one scan names them.
outdated

twinOf runs per candidate per tick and per map render; show it stays cheap, and test the variantGroupOf exemption.

258 shared ??= sharedTitles(app.db, repo.id);
259 if (isShared(shared, issue.title) && variantGroupOf(app.db, issue.id) === undefined) {
260 const twin = twinOf(app.db, issue);
261 if (twin !== undefined) {
262 twins.set(issue.number, twin);
263 return "twin";
264 }
265 }
246266 const pin = pinOf(issue);
247267 if (pin !== undefined) {
248268 pinned.set(issue.number, pin);
@@ -265,7 +285,7 @@ export function heldBy(
265285 if (hold === null) startable.push(r);
266286 else by[hold].push(r.issue);
267287 }
268 return { startable, by, pinned, relieving };
288 return { startable, by, pinned, relieving, twins };
269289 }
270290
271291 /** The count per filter, in `HOLDS` order. */
@@ -335,6 +355,7 @@ const HOLD_WORDS: Readonly<Record<HoldName, string>> = {
335355 run_cap: "it has used its runs for the day",
336356 parked_twice: "parked twice; it is the planner's",
337357 red_fix: "a fix for a red main covers it",
358 twin: "another issue holds the same work",
338359 hot_file: "a file it names is hot",
339360 };
340361
@@ -367,6 +388,10 @@ export function mapHolds(
367388 pulls = pullsTouching(repo.id, top.path, admit);
368389 }
369390 }
391 if (name === "twin") {
392 const twin = held.twins.get(issue.number);
393 if (twin !== undefined) detail = `waits for #${twin.number}, which holds the same work`;
394 }
370395 holds.set(issue.number, { by: name, detail, ...(pulls === undefined ? {} : { pulls }) });
371396 }
372397 }
src/crew/twin.ts +17
@@ -27,6 +27,23 @@ const TWINS_ASKED = 8;
2727
2828 const same = (title: string): string => title.trim().toLowerCase();
2929
30 /**
31 * The titles more than one issue or pull of the repository bears, as `twinOf`
32 * reads them. No title outside this set has a twin, so a pass over many
33 * candidates asks `twinOf` only of the few inside it: one scan, not one per issue.
34 */
35 export function sharedTitles(db: Database, repoId: string): Set<string> {
36 const rows = db
37 .query<{ t: string }, [string]>(
38 "SELECT lower(trim(title)) AS t FROM issue WHERE repo_id = ?1 GROUP BY t HAVING count(*) > 1",
39 )
40 .all(repoId);
41 return new Set(rows.map((r) => r.t));
42 }
43
44 /** Whether `title` is one `sharedTitles` names. */
45 export const isShared = (shared: ReadonlySet<string>, title: string): boolean => shared.has(same(title));
46
3047 /** The twin, if any, already holding `issue`'s work: running before open before landed. */
3148 export function twinOf(db: Database, issue: Pick<Issue, "id" | "repoId" | "number" | "title" | "createdAt">): Twin | undefined {
3249 const t = same(issue.title);
src/flow/why.ts +12
@@ -29,6 +29,7 @@ export const CAUSES = [
2929 "wall",
3030 "ceiling",
3131 "hot_file",
32 "twin",
3233 "blocked_by",
3334 "later_gate",
3435 "no_builder",
@@ -45,6 +46,8 @@ export interface WhyFacts {
4546 readonly actor: Actor | null;
4647 readonly ceilingUsd?: number;
4748 readonly hotFile?: string;
49 /** The words of the hold another issue with the same title puts on it. */
50 readonly twin?: string;
4851 readonly blockedBy?: readonly number[];
4952 readonly laterGate?: string;
5053 readonly builders?: number;
@@ -88,6 +91,13 @@ const RULES: Readonly<Record<CauseName, (f: WhyFacts) => Omit<Cause, "name"> | n
8891 clears: "land or close the open pulls on that file, or release an admin's hold on it.",
8992 }
9093 : null,
94 twin: (f) =>
95 f.twin !== undefined
96 ? {
97 says: `Another issue holds the same work: ${f.twin}.`,
98 clears: "close this issue as a duplicate, or retitle it if it is different work.",
99 }
100 : null,
91101 blocked_by: (f) =>
92102 f.blockedBy !== undefined && f.blockedBy.length > 0
93103 ? {
@@ -184,11 +194,13 @@ export function whyIssue(app: App, repo: Repo, issue: Issue, now = new Date()):
184194 const held = heldNow(app, repo);
185195 const holdOf = mapHolds(repo, held).holds.get(issue.number);
186196 const hot = holdOf?.by === "hot_file" ? holdOf.detail : undefined;
197 const twin = holdOf?.by === "twin" ? holdOf.detail : undefined;
187198 const later = graph.deferred.get(issue.id);
188199 return {
189200 cause: firstCause({
190201 actor: "builder",
191202 ...(hot !== undefined ? { hotFile: hot } : {}),
203 ...(twin !== undefined ? { twin } : {}),
192204 blockedBy: openBlockers,
193205 ...(later !== undefined ? { laterGate: later } : {}),
194206 ...sharedFacts(app, repo, now),
test/boards-held.test.ts +1 −1
@@ -16,7 +16,7 @@ let app: App;
1616 let alice = "";
1717
1818 function issue(id: string, repo: string, n: number): void {
19 app.db.run("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state) VALUES (?1, ?2, ?3, 'issue', 't', '', ?4, 'open')", [id, repo, n, alice]);
19 app.db.run("INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state) VALUES (?1, ?2, ?3, 'issue', ?5, '', ?4, 'open')", [id, repo, n, alice, `t ${id}`]);
2020 }
2121 function running(repo: string, issueId: string): void {
2222 app.db.run("INSERT INTO run (id, repo_id, kind, issue_id, state) VALUES (?1, ?2, 'agent', ?3, 'running')", [`run-${issueId}`, repo, issueId]);
test/crew-direction-autonomy.test.ts +1 −1
@@ -360,7 +360,7 @@ describe("a reader's pull from a fork", () => {
360360 );
361361 const pull = ok(
362362 await openPull(app, mallory, repo, {
363 title: "Rounder corners",
363 title: "Rounder corners, mine",
364364 body: `Closes #${wanted.number}`,
365365 baseBranch: "main",
366366 headBranch: "mine",
test/held-relieves-map.test.ts +1 −1
@@ -119,7 +119,7 @@ test("a second issue relieving the same path waits on the one in flight, and the
119119 const repoId = await hotRepo("relieving");
120120 const first = open(repoId, TUI);
121121 createRun(app.db, { repoId, kind: "agent", issueId: first.id });
122 const second = open(repoId, TUI);
122 const second = open(repoId, { ...TUI, title: `${TUI.title} (again)` });
123123 for (const map of [await apiMap("relieving"), await mcpMap("relieving")]) {
124124 expect(heldOf(map, first.number)?.by).toBe("running");
125125 expect(heldOf(map, second.number)).toEqual({
test/mcp-flow-why.test.ts +13
@@ -26,6 +26,7 @@ describe("the list of causes", () => {
2626 ["wall", { actor: "person", wall: ["anyone signed out could read pack records"] }],
2727 ["ceiling", { actor: "builder", ceilingUsd: 20 }],
2828 ["hot_file", { actor: "builder", hotFile: "src/a.ts is in 3 open pulls" }],
29 ["twin", { actor: "builder", twin: "waits for #3, which holds the same work" }],
2930 ["blocked_by", { actor: "builder", blockedBy: [4, 9] }],
3031 ["later_gate", { actor: "builder", laterGate: "behind the Later gate" }],
3132 ["no_builder", { actor: "builder", builders: 0 }],
@@ -167,6 +168,18 @@ describe("MCP flow with a subject", () => {
167168 expect(r.value.says).toContain("src/pinned.ts");
168169 });
169170
171 test("an issue whose twin has a run going is held by twin, and says which", async () => {
172 const first = issueOf("Twin work");
173 const second = issueOf("twin work");
174 const bot = getUserByName(app.db, "bot")!;
175 app.db
176 .query(`INSERT INTO run (id, repo_id, kind, issue_id, actor_id, state) VALUES (?1, ?2, 'agent', ?3, ?4, 'running')`)
177 .run(crypto.randomUUID(), repo.id, first.id, bot.id);
178 const r = await call(ownerTok, { repo: "alice/one", issue: second.number });
179 expect(r.value).toMatchObject({ moving: false, cause: "twin" });
180 expect(r.value.says).toContain(`#${first.number}`);
181 });
182
170183 test("an issue filed under Later is held by later_gate, not blocked_by", async () => {
171184 const gate = issueOf("Later gate: not yet");
172185 const umbrella = issueOf("Later: someday");
test/twin-runs.test.ts +88 −3
@@ -3,7 +3,7 @@
33 * issue whose twin (same title) has a run going, a pull open or a pull
44 * merged is refused; and wasted runs are counted per day.
55 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
6 import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test";
77 import { mkdtemp, rm } from "node:fs/promises";
88 import { tmpdir } from "node:os";
99 import { join } from "node:path";
@@ -13,9 +13,13 @@ import { assignIssue, openIssue } from "../src/actions/issues.ts";
1313 import { createRepo } from "../src/actions/repos.ts";
1414 import { getRepoById, type Repo } from "../src/core/repos.ts";
1515 import { createUser, getUserByName, type User } from "../src/core/users.ts";
16 import { createIssue, listComments, type Issue } from "../src/core/issues.ts";
17 import { sweepAssigned } from "../src/crew/dispatch.ts";
16 import { createIssue, getIssueById, listComments, type Issue } from "../src/core/issues.ts";
17 import { queueAgentRun, sweepAssigned } from "../src/crew/dispatch.ts";
18 import { getAgent } from "../src/core/agents.ts";
19 import { heldNow, mapHolds } from "../src/crew/held.ts";
1820 import { twinOf } from "../src/crew/twin.ts";
21 import { once } from "./helpers/statements.ts";
22 import { expectWork } from "./helpers/work.ts";
1923 import { isWasted, wastedDays } from "../src/core/wasted_days.ts";
2024
2125 describe("twins", () => {
@@ -106,6 +110,24 @@ describe("twins", () => {
106110 expect(twinOf(app.db, again)?.standing).toBe("landed");
107111 });
108112
113 test("a twin-held issue is held under its own reason, and the free one stays startable", () => {
114 const first = open("Harbour light");
115 const twin = open("Harbour light");
116 const free = open("Quite another job");
117 assign(first);
118 sweepAssigned(app);
119 const held = heldNow(app, fresh());
120 const nums = held.startable.map((r) => r.issue.number);
121 expect(nums).not.toContain(twin.number);
122 expect(nums).toContain(free.number);
123 expect(held.by.twin.map((i) => i.number)).toContain(twin.number);
124 const { holds } = mapHolds(fresh(), held);
125 expect(holds.get(twin.number)).toMatchObject({
126 by: "twin",
127 detail: `waits for #${first.number}, which holds the same work`,
128 });
129 });
130
109131 test("a pull titled the same stands in front, and a different title does not", () => {
110132 const dup = open("Domino Run");
111133 pullOn("Domino Run", "bot/unrelated", true);
@@ -142,6 +164,69 @@ describe("twins", () => {
142164 expect(said[0]!.authorId).toBe(bot.id);
143165 });
144166
167 test("the twin refusal is logged once an hour per issue, and again when the hour is up", () => {
168 const first = open("Beacon");
169 assign(first);
170 sweepAssigned(app);
171 const second = open("Beacon");
172 const lines: string[] = [];
173 const spy = spyOn(console, "info").mockImplementation((msg: string) => {
174 if (String(msg).includes("a twin holds the work")) lines.push(String(msg));
175 });
176 const realNow = Date.now;
177 let clock = realNow();
178 Date.now = () => clock;
179 try {
180 const agent = getAgent(app.db, bot.id)!;
181 queueAgentRun(app, fresh(), second, agent, "assigned");
182 queueAgentRun(app, fresh(), second, agent, "assigned");
183 expect(lines).toHaveLength(1);
184 clock += 3_600_001;
185 queueAgentRun(app, fresh(), second, agent, "assigned");
186 expect(lines).toHaveLength(2);
187 } finally {
188 Date.now = realNow;
189 spy.mockRestore();
190 }
191 });
192
193 test("a variant-group issue sharing a title is not held as a twin", () => {
194 const first = open("Mosaic");
195 assign(first);
196 sweepAssigned(app);
197 const variant = open("Mosaic");
198 app.db
199 .query(
200 `INSERT INTO run (id, repo_id, kind, issue_id, actor_id, state, variant_group)
201 VALUES (?1, ?2, 'agent', ?3, ?4, 'ok', 'try-1')`,
202 )
203 .run(crypto.randomUUID(), repo.id, variant.id, bot.id);
204 expect(twinOf(app.db, variant)?.number).toBe(first.number);
205 const held = heldNow(app, fresh());
206 expect(held.by.twin.map((i) => i.number)).not.toContain(variant.number);
207 });
208
209 test("the twin hold stays cheap with hundreds of open issues", async () => {
210 for (let i = 0; i < 400; i++) open(`Bulk job ${i}`);
211 const all = app.db
212 .query<{ id: string }, [string]>("SELECT id FROM issue WHERE repo_id = ?1 AND kind = 'issue' AND state = 'open'")
213 .all(repo.id)
214 .map((r) => getIssueById(app.db, r.id)!);
215 const perIssue = await once(app.db, () => {
216 for (const i of all) twinOf(app.db, i);
217 });
218 console.log(`twinOf over ${all.length} open issues: ${perIssue} statements`);
219 expectWork(perIssue / all.length, { max: 3 });
220 // The pass itself asks twinOf only of titles that repeat: none of the 400 added do.
221 let held!: ReturnType<typeof heldNow>;
222 const pass = await once(app.db, () => {
223 held = heldNow(app, fresh());
224 });
225 console.log(`heldNow over ${all.length} open issues: ${pass} statements`);
226 expect(held.by.twin.length).toBeLessThan(20);
227 expectWork(pass, { max: 5 * all.length });
228 });
229
145230 test("an issue's own pull, though titled the same, is not its twin", () => {
146231 const mine = open("Own pull");
147232 pullOn("Own pull", `bot/${mine.number}-own-pull`);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs