Public: anyone can read this; the crew can change it.
The planner is held while nothing lands or the plate is full, and the tick log says why #1350
scout/forge:scout/1127-the-planner-is-held-while-nothing-lands → main 2 commits fork
3 files
changed +243
−0
src/crew/autonomy.ts +102
| @@ -59,6 +59,7 @@ import { | ||
| 59 | 59 | pullDir, |
| 60 | 60 | } from "../core/pulls.ts"; |
| 61 | 61 | import { |
| 62 | canWrite, | |
| 62 | 63 | getRepoById, |
| 63 | 64 | listAutonomousRepos, |
| 64 | 65 | setDirectionIssue, |
| @@ -439,6 +440,105 @@ function notPlanned(repo: Repo, why: string): false { | ||
| 439 | 440 | return false; |
| 440 | 441 | } |
| 441 | 442 | |
| 443 | ||
| 444 | /** Nothing landing for this long, with a pull closed unmerged, holds the planner. */ | |
| 445 | const LANDING_WINDOW_MS = 48 * 60 * 60_000; | |
| 446 | /** More ready, unassigned issues than slots × this fill the plate. */ | |
| 447 | const PLATE_PER_SLOT = 3; | |
| 448 | ||
| 449 | export type PlannerHold = | |
| 450 | | { readonly kind: "nothing landed"; readonly since: string } | |
| 451 | | { readonly kind: "plate full"; readonly ready: number }; | |
| 452 | ||
| 453 | /** Why a hold is held, as the tick log says it. */ | |
| 454 | export function holdWhy(hold: PlannerHold): string { | |
| 455 | return hold.kind === "plate full" | |
| 456 | ? `plate full (${hold.ready} ready)` | |
| 457 | : `nothing landed since ${hold.since.slice(0, 10)}`; | |
| 458 | } | |
| 459 | ||
| 460 | /** The same reason in plain words, for the board. */ | |
| 461 | export function holdWords(hold: PlannerHold): string { | |
| 462 | if (hold.kind === "plate full") | |
| 463 | return `Planning paused: ${hold.ready} issues are ready and waiting`; | |
| 464 | const at = new Date(hold.since); | |
| 465 | const day = at.toLocaleDateString("en-US", { | |
| 466 | month: "short", | |
| 467 | day: "numeric", | |
| 468 | timeZone: "UTC", | |
| 469 | }); | |
| 470 | return `Planning paused: nothing has landed since ${day}`; | |
| 471 | } | |
| 472 | ||
| 473 | /** | |
| 474 | * A word on the direction issue from someone who may write here, newer than | |
| 475 | * the planner's last pass: the owner steering, which is never held. | |
| 476 | */ | |
| 477 | function steered(app: App, repo: Repo): boolean { | |
| 478 | if (repo.directionIssueId === null) return false; | |
| 479 | const last = app.db | |
| 480 | .query<{ created_at: string }, [string]>( | |
| 481 | `SELECT created_at FROM run WHERE issue_id = ?1 AND kind = 'agent' | |
| 482 | ORDER BY created_at DESC LIMIT 1`, | |
| 483 | ) | |
| 484 | .get(repo.directionIssueId); | |
| 485 | return app.db | |
| 486 | .query<{ id: string }, [string, string]>( | |
| 487 | `SELECT DISTINCT author_id AS id FROM comment | |
| 488 | WHERE issue_id = ?1 AND created_at > ?2 AND origin_author IS NULL`, | |
| 489 | ) | |
| 490 | .all(repo.directionIssueId, last?.created_at ?? "") | |
| 491 | .some((r) => { | |
| 492 | const who = getUserById(app.db, r.id); | |
| 493 | return who !== undefined && who.kind !== "agent" && canWrite(app.db, repo, who); | |
| 494 | }); | |
| 495 | } | |
| 496 | ||
| 497 | /** | |
| 498 | * Whether the planner is held for this repository: no pull merged in 48 h | |
| 499 | * while one closed unmerged, or more ready, unassigned issues than | |
| 500 | * `slots × 3`. A steer from a writer on the direction issue lifts it. | |
| 501 | */ | |
| 502 | export function plannerHold( | |
| 503 | app: App, | |
| 504 | repo: Repo, | |
| 505 | now = new Date(), | |
| 506 | ): PlannerHold | null { | |
| 507 | if (steered(app, repo)) return null; | |
| 508 | const cutoff = new Date(now.getTime() - LANDING_WINDOW_MS).toISOString(); | |
| 509 | const merged = app.db | |
| 510 | .query<{ at: string | null }, [string]>( | |
| 511 | `SELECT MAX(p.merged_at) AS at FROM pull p JOIN issue i ON i.id = p.issue_id | |
| 512 | WHERE i.repo_id = ?1`, | |
| 513 | ) | |
| 514 | .get(repo.id)?.at ?? null; | |
| 515 | if (merged === null || merged < cutoff) { | |
| 516 | const refused = app.db | |
| 517 | .query<{ n: number }, [string, string]>( | |
| 518 | `SELECT COUNT(*) AS n FROM issue i JOIN pull p ON p.issue_id = i.id | |
| 519 | WHERE i.repo_id = ?1 AND i.kind = 'pull' AND i.state = 'closed' | |
| 520 | AND p.merged_at IS NULL AND i.closed_at >= ?2`, | |
| 521 | ) | |
| 522 | .get(repo.id, cutoff)?.n ?? 0; | |
| 523 | if (refused > 0) return { kind: "nothing landed", since: merged ?? cutoff }; | |
| 524 | } | |
| 525 | const ready = readyNow(repoGraph(app.db, repo.id, { sealed: true })).filter( | |
| 526 | (r) => r.issue.id !== repo.directionIssueId && r.issue.assigneeId === null, | |
| 527 | ).length; | |
| 528 | if (ready > Math.max(1, repo.autonomySlots) * PLATE_PER_SLOT) | |
| 529 | return { kind: "plate full", ready }; | |
| 530 | return null; | |
| 531 | } | |
| 532 | ||
| 533 | function held(repo: Repo, why: string): false { | |
| 534 | log.info("tick", { | |
| 535 | repo: `${repo.ownerName}/${repo.name}`, | |
| 536 | planner: "held", | |
| 537 | why, | |
| 538 | }); | |
| 539 | return false; | |
| 540 | } | |
| 541 | ||
| 442 | 542 | async function planIfThin( |
| 443 | 543 | app: App, |
| 444 | 544 | repo: Repo, |
| @@ -472,6 +572,8 @@ async function planIfThin( | ||
| 472 | 572 | if (ready >= THIN_MAP && owed.length === 0) return false; |
| 473 | 573 | if (atRunCap(app, repo, direction)) |
| 474 | 574 | return notPlanned(repo, "parked: the direction is at its run cap"); |
| 575 | const hold = plannerHold(app, repo, now); | |
| 576 | if (hold !== null) return held(repo, holdWhy(hold)); | |
| 475 | 577 | if (lastPlan !== null && lastPlan !== undefined) { |
| 476 | 578 | // A planner that declined the direction is not asked again until a |
| 477 | 579 | // person has said something on it, or the crew hands it an issue. |
src/web/pages/board.ts +10
| @@ -58,6 +58,8 @@ import { listActiveRuns, listRuns, type Run } from "../../core/runs.ts"; | ||
| 58 | 58 | import { getUsersByIds } from "../../core/users.ts"; |
| 59 | 59 | import { |
| 60 | 60 | autonomyStatus, |
| 61 | holdWords, | |
| 62 | plannerHold, | |
| 61 | 63 | plannerDeclines, |
| 62 | 64 | startOfTodayUtc, |
| 63 | 65 | truncateAtWord, |
| @@ -919,6 +921,8 @@ function today(ctx: Ctx, scope: BoardScope): Html { | ||
| 919 | 921 | }).filter((p) => p.mergedAt !== null && p.mergedAt >= since).length; |
| 920 | 922 | const share = s.ceilingUsd > 0 ? s.spentTodayUsd / s.ceilingUsd : 0; |
| 921 | 923 | const review = reviewWords(reviewStats(db, repo.id, since)); |
| 924 | const hold = | |
| 925 | repo.autonomy === "autonomous" ? plannerHold(ctx.app, repo, now) : null; | |
| 922 | 926 | const note = |
| 923 | 927 | s.paused === "ceiling" |
| 924 | 928 | ? "at its ceiling until 00:00 UTC" |
| @@ -948,6 +952,12 @@ function today(ctx: Ctx, scope: BoardScope): Html { | ||
| 948 | 952 | <span class="board-app-week" |
| 949 | 953 | >${perShippedWords(week.get(repo.id))} this week</span |
| 950 | 954 | > |
| 955 | ${when( | |
| 956 | hold !== null, | |
| 957 | () => html`<span class="board-app-week board-app-hold" | |
| 958 | >${holdWords(hold!)}</span | |
| 959 | >`, | |
| 960 | )} | |
| 951 | 961 | ${when( |
| 952 | 962 | review !== null, |
| 953 | 963 | () => html`<span class="board-app-week board-app-review" |
test/autonomy.test.ts +131
| @@ -32,9 +32,13 @@ import { submitReview } from "../src/core/reviews.ts"; | ||
| 32 | 32 | import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; |
| 33 | 33 | import { createUser, type User } from "../src/core/users.ts"; |
| 34 | 34 | import { serve, type Servers } from "../src/http/server.ts"; |
| 35 | import { addComment, setIssueState } from "../src/actions/issues.ts"; | |
| 35 | 36 | import { |
| 36 | 37 | autonomyStatus, |
| 37 | 38 | driveRepo, |
| 39 | holdWhy, | |
| 40 | holdWords, | |
| 41 | plannerHold, | |
| 38 | 42 | landIfGreen, |
| 39 | 43 | pickBuilder, |
| 40 | 44 | sweepGreen, |
| @@ -750,3 +754,130 @@ describe("the crew in a preview", () => { | ||
| 750 | 754 | } |
| 751 | 755 | }); |
| 752 | 756 | }); |
| 757 | ||
| 758 | describe("the planner is held while nothing lands or the plate is full", () => { | |
| 759 | let dataDir: string; | |
| 760 | let app: App; | |
| 761 | let alice: User; | |
| 762 | let repo: Repo; | |
| 763 | let planner: User; | |
| 764 | const fresh = () => getRepoById(app.db, repo.id)!; | |
| 765 | const direction = () => getIssueById(app.db, fresh().directionIssueId!)!; | |
| 766 | let n = 0; | |
| 767 | const closedPull = async (merged: boolean) => { | |
| 768 | n += 1; | |
| 769 | const branch = await commitToBranch(app, alice, repo, { | |
| 770 | branch: `feature/p${n}`, | |
| 771 | base: "main", | |
| 772 | message: `P${n}`, | |
| 773 | files: [{ path: `p${n}.ts`, content: `export const p = ${n};\n` }], | |
| 774 | }); | |
| 775 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); | |
| 776 | const opened = await openPull(app, alice, repo, { | |
| 777 | title: `P${n}`, | |
| 778 | baseBranch: "main", | |
| 779 | headBranch: `feature/p${n}`, | |
| 780 | }); | |
| 781 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 782 | const closed = setIssueState(app, alice, repo, opened.value, "closed"); | |
| 783 | if (!closed.ok) throw new Error(String(closed.error)); | |
| 784 | if (merged) | |
| 785 | app.db | |
| 786 | .query("UPDATE pull SET merged_at = ?2 WHERE issue_id = ?1") | |
| 787 | .run(opened.value.id, new Date().toISOString()); | |
| 788 | }; | |
| 789 | ||
| 790 | beforeAll(async () => { | |
| 791 | dataDir = await mkdtemp(join(tmpdir(), "forge-held-")); | |
| 792 | app = createApp({ dataDir, port: 0 }); | |
| 793 | const a = await createUser(app.db, { | |
| 794 | trusted: true, | |
| 795 | username: "alice", | |
| 796 | email: "[email protected]", | |
| 797 | password: "a-good-passphrase", | |
| 798 | adminIfFirst: true, | |
| 799 | }); | |
| 800 | if (!a.ok) throw new Error(a.error); | |
| 801 | alice = a.value; | |
| 802 | const made = await createRepo(app, alice, { name: "held", visibility: "private" }); | |
| 803 | if (!made.ok) throw new Error(String(made.error)); | |
| 804 | repo = made.value; | |
| 805 | const seeded = await commitToBranch(app, alice, repo, { | |
| 806 | message: "Start", | |
| 807 | files: [{ path: "README.md", content: "# held\n" }], | |
| 808 | }); | |
| 809 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 810 | for (const name of ["brick", "chart"]) { | |
| 811 | const agent = await createAgent(app, alice, { username: name }); | |
| 812 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 813 | expect(addToCrew(app, alice, repo, name).ok).toBe(true); | |
| 814 | } | |
| 815 | const byName = (await import("../src/core/users.ts")).getUserByName; | |
| 816 | planner = byName(app.db, "chart")!; | |
| 817 | setCrewRole(app.db, repo.id, planner.id, "planner"); | |
| 818 | const on = await updateRepoSettings(app, alice, repo, { | |
| 819 | autonomy: "autonomous", | |
| 820 | autonomyDirection: "Weave the loom", | |
| 821 | autonomyCeilingUsd: 10, | |
| 822 | autonomySlots: 1, | |
| 823 | autonomyBy: alice.id, | |
| 824 | }); | |
| 825 | expect(on.ok).toBe(true); | |
| 826 | }); | |
| 827 | ||
| 828 | afterAll(async () => { | |
| 829 | app.close(); | |
| 830 | await rm(dataDir, { recursive: true, force: true }); | |
| 831 | }); | |
| 832 | ||
| 833 | test("nothing landed with a pull closed unmerged holds it; a merge releases it; the owner's steer goes through", async () => { | |
| 834 | // Nothing closed yet: a quiet repository is not a stuck one. | |
| 835 | expect(plannerHold(app, fresh())).toBeNull(); | |
| 836 | await closedPull(false); | |
| 837 | const hold = plannerHold(app, fresh()); | |
| 838 | expect(hold?.kind).toBe("nothing landed"); | |
| 839 | expect(holdWhy(hold!)).toMatch(/^nothing landed since \d{4}-\d\d-\d\d$/); | |
| 840 | expect(holdWords(hold!)).toMatch(/^Planning paused: nothing has landed since \w{3} \d+$/); | |
| 841 | await driveRepo(app, fresh()); | |
| 842 | expect(direction().assigneeId).toBeNull(); | |
| 843 | // The owner steers on the direction issue: never held. | |
| 844 | const said = addComment(app, alice, fresh(), direction(), "Try the shuttle first"); | |
| 845 | expect(said.ok).toBe(true); | |
| 846 | expect(plannerHold(app, fresh())).toBeNull(); | |
| 847 | // A reader's or an agent's word is not a steer. | |
| 848 | app.db.query("DELETE FROM comment WHERE issue_id = ?1").run(direction().id); | |
| 849 | expect(plannerHold(app, fresh())?.kind).toBe("nothing landed"); | |
| 850 | // A pull merges: the hold lifts and the planner is asked. | |
| 851 | await closedPull(true); | |
| 852 | expect(plannerHold(app, fresh())).toBeNull(); | |
| 853 | await driveRepo(app, fresh()); | |
| 854 | expect(direction().assigneeId).toBe(planner.id); | |
| 855 | }); | |
| 856 | ||
| 857 | test("a merge older than 48 h does not release it, and a closed pull older than that does not hold", async () => { | |
| 858 | const day3 = new Date(Date.now() + 72 * 60 * 60_000); | |
| 859 | expect(plannerHold(app, fresh(), day3)).toBeNull(); | |
| 860 | await closedPull(false); | |
| 861 | // Three days on, the old merge is stale; the unmerged pull is too. | |
| 862 | expect(plannerHold(app, fresh(), day3)).toBeNull(); | |
| 863 | // Two days after the last merge, with a fresh refusal: held. | |
| 864 | const day2 = new Date(Date.now() + 49 * 60 * 60_000); | |
| 865 | app.db.query("UPDATE issue SET closed_at = ?1 WHERE kind = 'pull'").run(new Date(day2.getTime() - 60_000).toISOString()); | |
| 866 | expect(plannerHold(app, fresh(), day2)?.kind).toBe("nothing landed"); | |
| 867 | }); | |
| 868 | ||
| 869 | test("more than slots × 3 ready unassigned issues is a full plate, and it drains", () => { | |
| 870 | app.db.query("DELETE FROM issue WHERE kind = 'pull'").run(); | |
| 871 | const made: Issue[] = []; | |
| 872 | for (let i = 0; i < 4; i++) { | |
| 873 | const o = openIssue(app, alice, fresh(), { title: `Thread ${i}` }); | |
| 874 | if (!o.ok) throw new Error(String(o.error)); | |
| 875 | made.push(o.value); | |
| 876 | } | |
| 877 | const hold = plannerHold(app, fresh()); | |
| 878 | expect(hold).toEqual({ kind: "plate full", ready: 4 }); | |
| 879 | expect(holdWhy(hold!)).toBe("plate full (4 ready)"); | |
| 880 | expect(setIssueState(app, alice, fresh(), made[0]!, "closed").ok).toBe(true); | |
| 881 | expect(plannerHold(app, fresh())).toBeNull(); | |
| 882 | }); | |
| 883 | }); |
new above
offline