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
mason/forge:mason/8473-the-picker-skips-an-issue-the-dispatcher → main 6 commits fork 1 thread · 1 unresolved
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 | ||
| 404 | 404 | |
| 405 | 405 | export type Drove = "off" | "paused" | "waiting" | "full" | "picked" | "planned" | "idle"; |
| 406 | 406 | |
| 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 | */ | |
| 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 | ||
| 407 | 423 | /** |
| 408 | 424 | * One turn of the loop for one repository. Returns what it did, for the |
| 409 | 425 | * log and for tests; the effects are an assignment (which the crew turns |
| @@ -1251,7 +1267,7 @@ export async function autonomyTick( | ||
| 1251 | 1267 | // work takes the slots it needs (#2172). |
| 1252 | 1268 | const requested = await moveRequested(app, current, freeSlots(app, current, now), now); |
| 1253 | 1269 | const unstuck = unstickNow(app, current, now); |
| 1254 | const did = await driveRepo(app, current, now); | |
| 1270 | const did = await fillSlots(app, current, now); | |
| 1255 | 1271 | // Rules only, no run: an issue noted already done on the base closes. |
| 1256 | 1272 | const triaged = await liveTriage(app, current, now).catch((error) => { |
| 1257 | 1273 | log.error("live triage failed", { |
src/crew/dispatch.ts +2 −24
| @@ -117,6 +117,7 @@ import { | ||
| 117 | 117 | steerRun, |
| 118 | 118 | } from "./runner.ts"; |
| 119 | 119 | import { STALE_AFTER_MS } from "../runs/lease.ts"; |
| 120 | import { freshPullFor } from "./fresh_pull.ts"; | |
| 120 | 121 | import { abortAgentRun } from "./running.ts"; |
| 121 | 122 | |
| 122 | 123 | const log = createLog("crew"); |
| @@ -358,29 +359,6 @@ function unstickDue(app: App, repo: Repo, issue: Issue): boolean { | ||
| 358 | 359 | return unstickOwed(app.db, repo, last?.at ?? ""); |
| 359 | 360 | } |
| 360 | 361 | |
| 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 | ||
| 384 | 362 | /** Whether `spentUsd` has reached the repository's daily ceiling: the test that stops a run being queued. */ |
| 385 | 363 | export function atCeiling( |
| 386 | 364 | repo: Pick<Repo, "autonomyCeilingUsd">, |
| @@ -485,7 +463,7 @@ export function queueAgentRun( | ||
| 485 | 463 | // One build run per issue: a pull opened a moment ago is that run's |
| 486 | 464 | // answer, and a second build beside it is the same work twice. |
| 487 | 465 | if (reason === "assigned") { |
| 488 | const fresh = freshPullFor(app, issue, agent); | |
| 466 | const fresh = freshPullFor(app.db, issue.repoId, issue.number, [agent.user.username]); | |
| 489 | 467 | if (fresh !== undefined) { |
| 490 | 468 | log.info("build run refused: a pull opened in the last 10 minutes", { |
| 491 | 469 | 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 @@ | ||
| 7 | 7 | * |
| 8 | 8 | * The filters, in the order they are asked: |
| 9 | 9 | * - `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`). | |
| 11 | 13 | * - `planner`: the planner or a reviewer holds it, not a builder. The tick |
| 12 | 14 | * frees these (`freeParked` in `autonomy.ts`): neither ever builds it. |
| 13 | 15 | * - `with_planner`: its limb was handed to the planner by a person and the |
| @@ -49,6 +51,7 @@ import { landedUnanswered } from "./landed.ts"; | ||
| 49 | 51 | import { PARK_STRIKES, parkedCount } from "./parks.ts"; |
| 50 | 52 | import { heldFiles, type FileHold } from "./file_holds.ts"; |
| 51 | 53 | import { expectedFiles, hotFiles, hotOverlap, pullsTouching, relievedFiles } from "./hot_files.ts"; |
| 54 | import { freshPulls } from "./fresh_pull.ts"; | |
| 52 | 55 | import { coveredByRedFix } from "./red_fix.ts"; |
| 53 | 56 | import { landedUnder } from "./stalls.ts"; |
| 54 | 57 | |
| @@ -140,6 +143,12 @@ export function heldBy( | ||
| 140 | 143 | const planners = new Set( |
| 141 | 144 | crew.filter((c) => c.role !== "builder").map((c) => c.agent.user.id), |
| 142 | 145 | ); |
| 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 | ); | |
| 143 | 152 | const handed = withPlanner(app.db, repo); |
| 144 | 153 | const hot = hotFiles(repo.id, admit); |
| 145 | 154 | const pins = new Map(heldFiles(app.db, repo.id).map((h) => [h.path, h])); |
| @@ -188,6 +197,8 @@ export function heldBy( | ||
| 188 | 197 | if (atRunCap(app, repo, issue)) return "run_cap"; |
| 189 | 198 | if (parkedCount(app.db, repo, issue.id) >= PARK_STRIKES) return "parked_twice"; |
| 190 | 199 | 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"; | |
| 191 | 202 | const pin = pinOf(issue); |
| 192 | 203 | if (pin !== undefined) { |
| 193 | 204 | 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"; | ||
| 16 | 16 | import { openPull } from "../src/actions/pulls.ts"; |
| 17 | 17 | import { commitToBranch, createRepo, updateRepoSettings } from "../src/actions/repos.ts"; |
| 18 | 18 | 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"; | |
| 20 | 20 | import { getRepoById, type Repo } from "../src/core/repos.ts"; |
| 21 | 21 | import { DONE_NOTE } from "../src/core/done_on_main.ts"; |
| 22 | 22 | import { claimRun, createRun, finishRun } from "../src/core/runs.ts"; |
| 23 | 23 | import { createToken } from "../src/core/tokens.ts"; |
| 24 | 24 | 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"; | |
| 26 | 26 | import { taskPrompt } from "../src/crew/brief.ts"; |
| 27 | 27 | import { ISSUE_RUN_CAP } from "../src/crew/guard.ts"; |
| 28 | 28 | 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", () => { | ||
| 450 | 450 | expect(heldLines([], 58)).toEqual(["Ready, and why it waits: no repository runs on its own."]); |
| 451 | 451 | }); |
| 452 | 452 | |
| 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 | ||
| 453 | 516 | test("the rows are kept a few seconds, then read afresh", async () => { |
| 454 | 517 | const { repo, mason } = await crewed(app, alice, "kept"); |
| 455 | 518 | const t0 = Date.now() + 60 * 60_000; |
new above
offline
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.