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

The picker skips an issue the dispatcher will refuse, so one fresh pull does not idle every builder #8476

Parked opened this in answer to
Closed without merging

mason/forge:mason/8473-the-picker-skips-an-issue-the-dispatcher → main 6 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +147 −28
src/crew/autonomy.ts +17 −1
@@ -404,6 +404,22 @@ export function smallShareWords(app: App, repo: Repo, now = new Date()): string
404404
405405 export type Drove = "off" | "paused" | "waiting" | "full" | "picked" | "planned" | "idle";
406406
407 /**
408 * A tick fills every free slot (#8473): one pick per turn of `driveRepo`,
409 * turned again while it picks, at most once per free slot. Returns "picked"
410 * if any turn did, else what the first turn did.
411 */

No test calls fillSlots, so the loop bound (free slots) and the early break are unproven. Add tests in test/crew-held.test.ts or an autonomy test: N free slots with N startable issues assigns N, and the loop stops at the slot count.

412 export async function fillSlots(app: App, repo: Repo, now = new Date()): Promise<Drove> {
413 const first = await driveRepo(app, repo, now);
414 if (first !== "picked") return first;
415 const free = Math.max(0, repoPace(app, repo, now).slots - slottedRuns(activeRuns(app, repo.id), listCrewRoles(app.db, repo.id)).length);
416 for (let n = 0; n < free; n++) {
417 const next = await driveRepo(app, repo, now);
418 if (next !== "picked") break;
419 }
420 return "picked";
421 }
422
407423 /**
408424 * One turn of the loop for one repository. Returns what it did, for the
409425 * log and for tests; the effects are an assignment (which the crew turns
@@ -1251,7 +1267,7 @@ export async function autonomyTick(
12511267 // work takes the slots it needs (#2172).
12521268 const requested = await moveRequested(app, current, freeSlots(app, current, now), now);
12531269 const unstuck = unstickNow(app, current, now);
1254 const did = await driveRepo(app, current, now);
1270 const did = await fillSlots(app, current, now);
12551271 // Rules only, no run: an issue noted already done on the base closes.
12561272 const triaged = await liveTriage(app, current, now).catch((error) => {
12571273 log.error("live triage failed", {
src/crew/dispatch.ts +2 −24
@@ -117,6 +117,7 @@ import {
117117 steerRun,
118118 } from "./runner.ts";
119119 import { STALE_AFTER_MS } from "../runs/lease.ts";
120 import { freshPullFor } from "./fresh_pull.ts";
120121 import { abortAgentRun } from "./running.ts";
121122
122123 const log = createLog("crew");
@@ -358,29 +359,6 @@ function unstickDue(app: App, repo: Repo, issue: Issue): boolean {
358359 return unstickOwed(app.db, repo, last?.at ?? "");
359360 }
360361
361 /** How long after a crew pull opens its issue takes no second build run. */
362 export const BUILD_SETTLE_MS = 10 * 60_000;
363
364 /** The crew pull opened for the issue within `BUILD_SETTLE_MS`, if any. */
365 function freshPullFor(
366 app: App,
367 issue: Issue,
368 agent: Agent,
369 ): { number: number } | undefined {
370 const since = new Date(Date.now() - BUILD_SETTLE_MS).toISOString();
371 const name = agent.user.username.replace(/[\\%_]/g, "\\$&");
372 return (
373 app.db
374 .query<{ number: number }, [string, string, string, string]>(
375 `SELECT i.number FROM pull p JOIN issue i ON i.id = p.issue_id
376 WHERE i.repo_id = ?1 AND i.created_at > ?2
377 AND (p.head_branch = ?3 OR p.head_branch LIKE ?4 ESCAPE '\\')
378 LIMIT 1`,
379 )
380 .get(issue.repoId, since, `${agent.user.username}/${issue.number}`, `${name}/${issue.number}-%`) ?? undefined
381 );
382 }
383
384362 /** Whether `spentUsd` has reached the repository's daily ceiling: the test that stops a run being queued. */
385363 export function atCeiling(
386364 repo: Pick<Repo, "autonomyCeilingUsd">,
@@ -485,7 +463,7 @@ export function queueAgentRun(
485463 // One build run per issue: a pull opened a moment ago is that run's
486464 // answer, and a second build beside it is the same work twice.
487465 if (reason === "assigned") {
488 const fresh = freshPullFor(app, issue, agent);
466 const fresh = freshPullFor(app.db, issue.repoId, issue.number, [agent.user.username]);
489467 if (fresh !== undefined) {
490468 log.info("build run refused: a pull opened in the last 10 minutes", {
491469 repo: `${repo.ownerName}/${repo.name}`,
src/crew/fresh_pull.ts added +51
@@ -0,0 +1,51 @@
1 /**
2 * The one test for "a crew pull opened a moment ago answers this issue"
3 * (#8473). The dispatcher refuses a second build run on such an issue
4 * (`queueAgentRun`), and the picker holds it as `open_pull` so it never
5 * takes the head of the line only to be refused.
6 */
7
8 import type { Database } from "bun:sqlite";
9
10 /** How long after a crew pull opens its issue takes no second build run. */
11 export const BUILD_SETTLE_MS = 10 * 60_000;
12
13 /**
14 * The crew pulls opened within `BUILD_SETTLE_MS`, by the issue number their
15 * head branch names (`<agent>/<n>` or `<agent>/<n>-slug`), for branches of
16 * the given agents. Value: the pull's number.
17 */
18 export function freshPulls(
19 db: Database,
20 repoId: string,
21 usernames: readonly string[],
22 now = Date.now(),
23 ): Map<number, number> {
24 const fresh = new Map<number, number>();
25 if (usernames.length === 0) return fresh;
26 const since = new Date(now - BUILD_SETTLE_MS).toISOString();
27 const rows = db
28 .query<{ number: number; head_branch: string }, [string, string]>(
29 `SELECT i.number, p.head_branch FROM pull p JOIN issue i ON i.id = p.issue_id
30 WHERE i.repo_id = ?1 AND i.created_at > ?2`,
31 )
32 .all(repoId, since);
33 for (const row of rows)
34 for (const name of usernames) {
35 if (!row.head_branch.startsWith(`${name}/`)) continue;
36 const named = /^(\d+)(?:-.*)?$/.exec(row.head_branch.slice(name.length + 1));
37 if (named !== null && !fresh.has(Number(named[1]))) fresh.set(Number(named[1]), row.number);
38 }
39 return fresh;
40 }
41
42 /** The crew pull opened for the issue within `BUILD_SETTLE_MS`, if any. */
43 export function freshPullFor(
44 db: Database,
45 repoId: string,
46 issueNumber: number,
47 usernames: readonly string[],
48 ): { number: number } | undefined {
49 const number = freshPulls(db, repoId, usernames).get(issueNumber);
50 return number === undefined ? undefined : { number };
51 }
src/crew/held.ts +12 −1
@@ -7,7 +7,9 @@
77 *
88 * The filters, in the order they are asked:
99 * - `running`: a run is in flight on it.
10 * - `open_pull`: an open pull already proposes it (`direct.ts`).
10 * - `open_pull`: an open pull already proposes it (`direct.ts`), or a crew
11 * pull opened in the last 10 minutes does, which the dispatcher would
12 * refuse a build for (`fresh_pull.ts`).
1113 * - `planner`: the planner or a reviewer holds it, not a builder. The tick
1214 * frees these (`freeParked` in `autonomy.ts`): neither ever builds it.
1315 * - `with_planner`: its limb was handed to the planner by a person and the
@@ -49,6 +51,7 @@ import { landedUnanswered } from "./landed.ts";
4951 import { PARK_STRIKES, parkedCount } from "./parks.ts";
5052 import { heldFiles, type FileHold } from "./file_holds.ts";
5153 import { expectedFiles, hotFiles, hotOverlap, pullsTouching, relievedFiles } from "./hot_files.ts";
54 import { freshPulls } from "./fresh_pull.ts";
5255 import { coveredByRedFix } from "./red_fix.ts";
5356 import { landedUnder } from "./stalls.ts";
5457
@@ -140,6 +143,12 @@ export function heldBy(
140143 const planners = new Set(
141144 crew.filter((c) => c.role !== "builder").map((c) => c.agent.user.id),
142145 );
146 // The dispatcher refuses a build on an issue a crew pull answered a moment ago.
147 const fresh = freshPulls(
148 app.db,
149 repo.id,
150 crew.map((c) => c.agent.user.username),
151 );
143152 const handed = withPlanner(app.db, repo);
144153 const hot = hotFiles(repo.id, admit);
145154 const pins = new Map(heldFiles(app.db, repo.id).map((h) => [h.path, h]));
@@ -188,6 +197,8 @@ export function heldBy(
188197 if (atRunCap(app, repo, issue)) return "run_cap";
189198 if (parkedCount(app.db, repo, issue.id) >= PARK_STRIKES) return "parked_twice";
190199 if (coveredByRedFix(app.db, repo.id, issue)) return "red_fix";
200 // Last of the pull holds: a stalled or capped issue is named for that, not for its closed pull.
201 if (fresh.has(issue.number)) return "open_pull";
191202 const pin = pinOf(issue);
192203 if (pin !== undefined) {
193204 pinned.set(issue.number, pin);
test/crew-held.test.ts +65 −2
@@ -16,13 +16,13 @@ import { addComment, assignIssue, openIssue } from "../src/actions/issues.ts";
1616 import { openPull } from "../src/actions/pulls.ts";
1717 import { commitToBranch, createRepo, updateRepoSettings } from "../src/actions/repos.ts";
1818 import { getAgentByName, setCrewRole } from "../src/core/agents.ts";
19 import { getIssueById, listIssues, type Issue } from "../src/core/issues.ts";
19 import { getIssue, getIssueById, listIssues, type Issue } from "../src/core/issues.ts";
2020 import { getRepoById, type Repo } from "../src/core/repos.ts";
2121 import { DONE_NOTE } from "../src/core/done_on_main.ts";
2222 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
2323 import { createToken } from "../src/core/tokens.ts";
2424 import { createUser, getUserByName, type User } from "../src/core/users.ts";
25 import { driveRepo } from "../src/crew/autonomy.ts";
25 import { driveRepo, fillSlots } from "../src/crew/autonomy.ts";
2626 import { taskPrompt } from "../src/crew/brief.ts";
2727 import { ISSUE_RUN_CAP } from "../src/crew/guard.ts";
2828 import { heldCounts, heldNow, heldWords, replannable, stuckOf } from "../src/crew/held.ts";
@@ -450,6 +450,69 @@ describe("the tick says why each ready issue cannot start", () => {
450450 expect(heldLines([], 58)).toEqual(["Ready, and why it waits: no repository runs on its own."]);
451451 });
452452
453 test("a fresh pull on the first issue holds it as open_pull and the second starts in the same tick (#8473)", async () => {
454 const { repo, mason } = await crewed(app, alice, "fresh");
455 const first = issue(repo, "First in line");
456 const second = issue(repo, "Second in line");
457 const seeded = await commitToBranch(app, alice, repo, {
458 message: "Start",
459 files: [{ path: "README.md", content: "# fresh\n" }],
460 });
461 if (!seeded.ok) throw new Error(JSON.stringify(seeded.error));
462 const head = `mason/${first.number}-first-in-line`;
463 const branch = await commitToBranch(app, alice, repo, {
464 branch: head,
465 base: "main",
466 message: "First",
467 files: [{ path: "first.ts", content: "export const n = 1;\n" }],
468 });
469 if (!branch.ok) throw new Error(JSON.stringify(branch.error));
470 const pull = await openPull(app, alice, repo, {
471 title: "First",
472 body: "No closing keyword",
473 baseBranch: "main",
474 headBranch: head,
475 });
476 if (!pull.ok) throw new Error(String(pull.error));
477
478 const held = heldNow(app, getRepoById(app.db, repo.id)!);
479 expect(held.by.open_pull.map((i) => i.number)).toEqual([first.number]);
480 expect(heldWords(held)).toContain("open_pull=1");
481 expect(held.startable.map((r) => r.issue.number)).toEqual([second.number]);
482
483 expect(await driveRepo(app, getRepoById(app.db, repo.id)!)).toBe("picked");
484 expect(getIssueById(app.db, second.id)!.assigneeId).toBe(mason.id);
485 expect(getIssueById(app.db, first.id)!.assigneeId).toBeNull();
486 });
487
488 test("a tick fills every free slot and no more (#8473)", async () => {
489 const slotsOf = async (name: string, slots: number) => {
490 const { repo } = await crewed(app, alice, name);
491 const set = await updateRepoSettings(app, alice, repo, { autonomySlots: slots });
492 if (!set.ok) throw new Error(JSON.stringify(set.error));
493 const fresh = () => getRepoById(app.db, repo.id)!;
494 const issues = [issue(repo, "One"), issue(repo, "Two")];
495 const assigned = () => issues.filter((i) => getIssueById(app.db, i.id)!.assigneeId !== null).length;
496 return { fresh, assigned };
497 };
498
499 // The crew is not running here: stand in for it, one builder run per assignment to mason.
500 const unsubscribe = app.bus.subscribe((event) => {
501 if (event.kind !== "issue.assigned" || event.repoId === null) return;
502 const issue = getIssue(app.db, event.repoId, event.payload["number"] as number);
503 if (issue?.assigneeId !== getUserByName(app.db, "mason")!.id) return;
504 createRun(app.db, { repoId: event.repoId, kind: "agent", issueId: issue.id, actorId: issue.assigneeId, reason: "assigned" });
505 });
506 const two = await slotsOf("two-slots", 2);
507 expect(await fillSlots(app, two.fresh())).toBe("picked");
508 expect(two.assigned()).toBe(2);
509
510 const one = await slotsOf("one-slot", 1);
511 expect(await fillSlots(app, one.fresh())).toBe("picked");
512 expect(one.assigned()).toBe(1);
513 unsubscribe();
514 });
515
453516 test("the rows are kept a few seconds, then read afresh", async () => {
454517 const { repo, mason } = await crewed(app, alice, "kept");
455518 const t0 = Date.now() + 60 * 60_000;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs