Public: anyone can read this; the crew can change it.
Move the accessors out of crew/dispatch.ts (queueAgentRun, crewFor, taskOf) so it leaves the boot's static closure #17278
Merged 31h 35m ago, live in production
mason/forge:mason/17181-move-the-accessors-out-of-crew-dispatch → main 0 commits fork
37 files
changed +293
−98
- src/actions/proposals.ts
- src/actions/runs.ts
- src/actions/upstream_follow.ts
- src/crew/approved_week.ts
- src/crew/box_pass.ts
- src/crew/conflict_release.ts
- src/crew/conflict_sweep.ts
- src/crew/converge.ts
- src/crew/dispatch.ts
- src/crew/finishing.ts
- src/crew/give_up.ts
- src/crew/proposal.ts
- src/crew/registry.ts
- src/crew/requested.ts
- src/crew/review.ts
- src/crew/stale_reds.ts
- src/crew/task.ts
- src/engine/duties.ts
- src/runs/interrupted_out.ts
- src/tui/screens/runs.ts
- src/web/pages/crew.ts
- test/approved-week.test.ts
- test/boot-lazy.test.ts
- test/checks-disk-red.test.ts
- test/crew-cancel-by.test.ts
- test/crew-failed-while-building.test.ts
- test/crew-interrupted-out.test.ts
- test/crew-merge-conflict.test.ts
- test/crew-registry.test.ts
- test/crew-shared-red.test.ts
- test/forge-origin-follow.test.ts
- test/guests.test.ts
- test/helpers/preload.ts
- test/home-interrupted-out.test.ts
- test/maintain-edge-kept.test.ts
- test/run-requeue-refusals.test.ts
- test/upstream_follow.test.ts
src/actions/proposals.ts +1 −1
| @@ -16,7 +16,7 @@ import { getProposal, moveProposal, type Proposal, type ProposalState } from ".. | ||
| 16 | 16 | import { ARCHIVED_WHY, getRepoById, ownerFactRepoIds, canRead, type Repo } from "../core/repos.ts"; |
| 17 | 17 | import type { User } from "../core/users.ts"; |
| 18 | 18 | import { ensureDirectionIssue } from "../crew/direction_issue.ts"; |
| 19 | import { queueAgentRun } from "../crew/dispatch.ts"; | |
| 19 | import { queueAgentRun } from "../crew/registry.ts"; | |
| 20 | 20 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 21 | 21 | import { MAX_BODY } from "../core/issues.ts"; |
| 22 | 22 | import { NOT_A_MEMBER } from "../core/roles.ts"; |
src/actions/runs.ts +2 −1
| @@ -38,7 +38,8 @@ import { abortAgentRun } from "../crew/running.ts"; | ||
| 38 | 38 | import { stopRun } from "../runs/stoppable.ts"; |
| 39 | 39 | import { whenEnded } from "../runs/ending.ts"; |
| 40 | 40 | import { createLog } from "../kernel/log.ts"; |
| 41 | import { queueAgentRun, taskOf } from "../crew/dispatch.ts"; | |
| 41 | import { taskOf } from "../crew/task.ts"; | |
| 42 | import { queueAgentRun } from "../crew/registry.ts"; | |
| 42 | 43 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 43 | 44 | import { restoreRunLog } from "../runs/archive.ts"; |
| 44 | 45 | import { readRunLog, readRunLogTail, runLogPath } from "../runs/log.ts"; |
src/actions/upstream_follow.ts +1 −1
| @@ -31,7 +31,7 @@ import { | ||
| 31 | 31 | } from "../core/issues.ts"; |
| 32 | 32 | import { listCrewRoles, type Agent } from "../core/agents.ts"; |
| 33 | 33 | import { listRuns } from "../core/runs.ts"; |
| 34 | import { queueAgentRun } from "../crew/dispatch.ts"; | |
| 34 | import { queueAgentRun } from "../crew/registry.ts"; | |
| 35 | 35 | import { pickBuilder } from "../crew/route.ts"; |
| 36 | 36 | import { getOriginFor } from "../core/origins.ts"; |
| 37 | 37 | import { pullForBranch, retargetPull } from "../core/pulls.ts"; |
src/crew/approved_week.ts +2 −1
| @@ -37,7 +37,8 @@ import { mergeable, resolveOid } from "../git/merge.ts"; | ||
| 37 | 37 | import { createLog } from "../kernel/log.ts"; |
| 38 | 38 | import { landIfGreen } from "./land_queue.ts"; |
| 39 | 39 | import { readConflictRoom } from "./conflict_room.ts"; |
| 40 | import { CHECK_FAILED_LIMIT, queueAgentRun } from "./dispatch.ts"; | |
| 40 | import { CHECK_FAILED_LIMIT } from "./give_up.ts"; | |
| 41 | import { queueAgentRun } from "./registry.ts"; | |
| 41 | 42 | import { ownBuilder } from "./finishing.ts"; |
| 42 | 43 | |
| 43 | 44 | const log = createLog("approved-week"); |
src/crew/box_pass.ts +1 −1
| @@ -14,7 +14,7 @@ import { closesFromBody, getPull, mergedPullClosing, type Pull } from "../core/p | ||
| 14 | 14 | import { PILLAR_MAX, type Repo } from "../core/repos.ts"; |
| 15 | 15 | import { repoDir } from "../git/repos.ts"; |
| 16 | 16 | import type { BoxList } from "./brief.ts"; |
| 17 | import { queueAgentRun } from "./dispatch.ts"; | |
| 17 | import { queueAgentRun } from "./registry.ts"; | |
| 18 | 18 | import { pickReviewer, reviewersOf, reviewLoad, reviewsInFlight } from "./review.ts"; |
| 19 | 19 | import { reviewRange } from "./review_range.ts"; |
| 20 | 20 |
src/crew/conflict_release.ts +3 −1
| @@ -12,7 +12,9 @@ import type { Repo } from "../core/repos.ts"; | ||
| 12 | 12 | import { activeRunFor, automaticRunsSince } from "../core/runs.ts"; |
| 13 | 13 | import { getUserById } from "../core/users.ts"; |
| 14 | 14 | import { createLog } from "../kernel/log.ts"; |
| 15 | import { agentFor, CHECK_FAILED_LIMIT, lastHumanWord, queueAgentRun } from "./dispatch.ts"; | |
| 15 | import { agentFor, lastHumanWord } from "./task.ts"; | |
| 16 | import { CHECK_FAILED_LIMIT } from "./give_up.ts"; | |
| 17 | import { queueAgentRun } from "./registry.ts"; | |
| 16 | 18 | |
| 17 | 19 | const log = createLog("autonomy"); |
| 18 | 20 |
src/crew/conflict_sweep.ts +2 −1
| @@ -11,7 +11,8 @@ import { activeRunFor } from "../core/runs.ts"; | ||
| 11 | 11 | import { mergeable, resolveOid, treeOf } from "../git/merge.ts"; |
| 12 | 12 | import { bringUpToDate, closeSuperseded, releaseConflicted } from "./conflict_release.ts"; |
| 13 | 13 | import { readConflictRoom } from "./conflict_room.ts"; |
| 14 | import { agentFor, CHECK_FAILED_LIMIT, lastHumanWord } from "./dispatch.ts"; | |
| 14 | import { agentFor, lastHumanWord } from "./task.ts"; | |
| 15 | import { CHECK_FAILED_LIMIT } from "./give_up.ts"; | |
| 15 | 16 | import { approvedOnHead } from "./finishing.ts"; |
| 16 | 17 | import { closeStale } from "./stale_pulls.ts"; |
| 17 | 18 |
src/crew/converge.ts +1 −1
| @@ -46,7 +46,7 @@ import { repoDir } from "../git/repos.ts"; | ||
| 46 | 46 | import { createLog } from "../kernel/log.ts"; |
| 47 | 47 | import { parkedCount } from "./park_issue.ts"; |
| 48 | 48 | import { PARK_STRIKES } from "./parks.ts"; |
| 49 | import { queueAgentRun } from "./dispatch.ts"; | |
| 49 | import { queueAgentRun } from "./registry.ts"; | |
| 50 | 50 | import { pickBuilder } from "./route.ts"; |
| 51 | 51 | |
| 52 | 52 | const log = createLog("converge"); |
src/crew/dispatch.ts +18 −71
| @@ -8,6 +8,9 @@ | ||
| 8 | 8 | * a run. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { provideCrewWork, registerCrew, forgetCrew } from "./registry.ts"; | |
| 12 | import { agentFor, lastHumanWord, taskOf } from "./task.ts"; | |
| 13 | import { CHECK_FAILED_LIMIT, INTERRUPTED_OUT } from "./give_up.ts"; | |
| 11 | 14 | import { isQuarantined } from "../core/quarantine.ts"; |
| 12 | 15 | import { ceilingHold, pausedUntil } from "../core/planner_pause.ts"; |
| 13 | 16 | import { pullsCeilingHold } from "../core/pulls_ceiling.ts"; |
| @@ -72,7 +75,6 @@ import { inFocus } from "./focus.ts"; | ||
| 72 | 75 | import { inMaintenance } from "../maintenance/state.ts"; |
| 73 | 76 | import { gateQueue } from "../flow/valve_gate.ts"; |
| 74 | 77 | import { ARCHIVED_WHY, canWrite, contributes, getRepoById, type Repo } from "../core/repos.ts"; |
| 75 | import { listReviews } from "../core/reviews.ts"; | |
| 76 | 78 | import { mentionedReviewer, requestReview } from "../actions/review_requests.ts"; |
| 77 | 79 | import { |
| 78 | 80 | crewReviews, |
| @@ -167,8 +169,16 @@ import { markRedRouted, redRouted } from "./red_routed.ts"; | ||
| 167 | 169 | |
| 168 | 170 | const log = createLog("crew"); |
| 169 | 171 | |
| 170 | /** Automatic follow-ups after a failed check, before a person must speak. */ | |
| 171 | export const CHECK_FAILED_LIMIT = 2; | |
| 172 | function forgetWorkdir(app: App, repo: Repo, pull: Pull): void { | |
| 173 | const agent = agentFor(app, pull); | |
| 174 | if (!agent) return; | |
| 175 | const number = issueOfBranch(agent, pull.headBranch) ?? pull.number; | |
| 176 | rmSync(workdirFor(app.config.dataDir, agent, repo, number), { | |
| 177 | recursive: true, | |
| 178 | force: true, | |
| 179 | }); | |
| 180 | } | |
| 181 | ||
| 172 | 182 | /** |
| 173 | 183 | * The dispatcher's pass. Under `bun test` (NODE_ENV=test) it is 50 ms: tests |
| 174 | 184 | * that start the real dispatcher waited out five seconds per pass (#12570). |
| @@ -229,11 +239,6 @@ const DRAIN_MS = 3 * 60_000; | ||
| 229 | 239 | /** How long `stop()` waits for the tick's own sweeps (converge, autonomy) to settle. */ |
| 230 | 240 | const SWEEPS_STOP_MS = 10_000; |
| 231 | 241 | |
| 232 | const handles = new WeakMap<App, CrewHandle>(); | |
| 233 | ||
| 234 | /** The crew serving this forge, if one was started. */ | |
| 235 | export const crewFor = (app: App): CrewHandle | undefined => handles.get(app); | |
| 236 | ||
| 237 | 242 | export { runnerSettings } from "./runner_env.ts"; |
| 238 | 243 | |
| 239 | 244 | export async function startCrew( |
| @@ -442,7 +447,7 @@ export async function startCrew( | ||
| 442 | 447 | setTimeout(() => resolve("late"), options.drainMs ?? DRAIN_MS), |
| 443 | 448 | ); |
| 444 | 449 | if ((await Promise.race([done, late])) === "done") { |
| 445 | handles.delete(app); | |
| 450 | forgetCrew(app); | |
| 446 | 451 | return; |
| 447 | 452 | } |
| 448 | 453 | } |
| @@ -455,10 +460,10 @@ export async function startCrew( | ||
| 455 | 460 | await Promise.all(inFlight.values()); |
| 456 | 461 | for (const run of interrupted) |
| 457 | 462 | requeue(app, run, "stopped for a restart; queued again", false); |
| 458 | handles.delete(app); | |
| 463 | forgetCrew(app); | |
| 459 | 464 | }, |
| 460 | 465 | }; |
| 461 | handles.set(app, handle); | |
| 466 | registerCrew(app, handle); | |
| 462 | 467 | return handle; |
| 463 | 468 | } |
| 464 | 469 | |
| @@ -1474,16 +1479,6 @@ export async function answerFailedCheck( | ||
| 1474 | 1479 | return "reworked"; |
| 1475 | 1480 | } |
| 1476 | 1481 | |
| 1477 | export function agentFor(app: App, issue: Issue): Agent | undefined { | |
| 1478 | if (issue.assigneeId !== null) { | |
| 1479 | const assigned = getAgent(app.db, issue.assigneeId); | |
| 1480 | if (assigned) return assigned; | |
| 1481 | // A person holds it: nothing the agent's name is on starts a run. | |
| 1482 | return undefined; | |
| 1483 | } | |
| 1484 | return issue.kind === "pull" ? getAgent(app.db, issue.authorId) : undefined; | |
| 1485 | } | |
| 1486 | ||
| 1487 | 1482 | /** What the crew makes of a red check: whose fault, and whether to try again. */ |
| 1488 | 1483 | export interface CheckJudgement { |
| 1489 | 1484 | /** Whether another automatic try by the agent is worth its cost. */ |
| @@ -1590,34 +1585,6 @@ export async function judgeFailedCheck( | ||
| 1590 | 1585 | return { retry, cause, failingTest }; |
| 1591 | 1586 | } |
| 1592 | 1587 | |
| 1593 | /** When a person last said anything on the conversation, or its start. */ | |
| 1594 | export function lastHumanWord(app: App, issue: Issue, agent: Agent): string { | |
| 1595 | let latest = issue.createdAt; | |
| 1596 | for (const c of listComments(app.db, issue.id)) { | |
| 1597 | if (c.authorName !== agent.user.username && c.createdAt > latest) | |
| 1598 | latest = c.createdAt; | |
| 1599 | } | |
| 1600 | for (const r of listReviews(app.db, issue.id)) { | |
| 1601 | if (r.authorName !== agent.user.username && r.createdAt > latest) | |
| 1602 | latest = r.createdAt; | |
| 1603 | } | |
| 1604 | return latest; | |
| 1605 | } | |
| 1606 | ||
| 1607 | function forgetWorkdir(app: App, repo: Repo, pull: Pull): void { | |
| 1608 | const agent = agentFor(app, pull); | |
| 1609 | if (!agent) return; | |
| 1610 | const number = issueOfBranch(agent, pull.headBranch) ?? pull.number; | |
| 1611 | rmSync(workdirFor(app.config.dataDir, agent, repo, number), { | |
| 1612 | recursive: true, | |
| 1613 | force: true, | |
| 1614 | }); | |
| 1615 | } | |
| 1616 | ||
| 1617 | /** What runs said before the count was kept (#11583): still read as given up on. */ | |
| 1618 | export const STOPPED_TWICE_LEGACY = "stopped twice by its forge restarting; not tried again"; | |
| 1619 | /** What a run says when the host cut it off every time it was tried. */ | |
| 1620 | export const INTERRUPTED_OUT = "the host stopped this run five times; not tried again"; | |
| 1621 | 1588 | /** How many interruptions a task takes before it is given up (#11693). */ |
| 1622 | 1589 | export const MAX_INTERRUPTIONS = 5; |
| 1623 | 1590 | const BACKOFF_FIRST_MS = 15_000; |
| @@ -1750,28 +1717,6 @@ function resumeLater(app: App, run: Run, pause: Pause): void { | ||
| 1750 | 1717 | }); |
| 1751 | 1718 | } |
| 1752 | 1719 | |
| 1753 | /** The still-open task a run was about, and the reason to queue it under again. */ | |
| 1754 | export function taskOf( | |
| 1755 | app: App, | |
| 1756 | run: Run, | |
| 1757 | ): { issue: Issue; repo: Repo; agent: Agent; reason: RunReason } | undefined { | |
| 1758 | if (run.issueId === null) return undefined; | |
| 1759 | const issue = getIssueById(app.db, run.issueId); | |
| 1760 | const repo = getRepoById(app.db, run.repoId); | |
| 1761 | const agent = run.actorId ? getAgent(app.db, run.actorId) : undefined; | |
| 1762 | if (!issue || !repo || !agent || issue.state !== "open") return undefined; | |
| 1763 | const reason: RunReason = | |
| 1764 | run.reason === "feedback" || | |
| 1765 | run.reason === "check_failed" || | |
| 1766 | run.reason === "conflict" || | |
| 1767 | run.reason === "resolve" || | |
| 1768 | run.reason === "converge" || | |
| 1769 | run.reason === "review" | |
| 1770 | ? run.reason | |
| 1771 | : "assigned"; | |
| 1772 | return { issue, repo, agent, reason }; | |
| 1773 | } | |
| 1774 | ||
| 1775 | 1720 | /** |
| 1776 | 1721 | * Who last assigned it: the newest `issue.assigned` or `pull.assigned`. The |
| 1777 | 1722 | * `+ 0` drops the column's affinity so the event expression index |
| @@ -2046,3 +1991,5 @@ export function pump( | ||
| 2046 | 1991 | } |
| 2047 | 1992 | noteHeld(app.db, held); |
| 2048 | 1993 | } |
| 1994 | ||
| 1995 | provideCrewWork({ queueAgentRun, sweepAssigned }); |
src/crew/finishing.ts +3 −1
| @@ -57,7 +57,9 @@ import { committedAt } from "../git/read.ts"; | ||
| 57 | 57 | import { createLog } from "../kernel/log.ts"; |
| 58 | 58 | import { noteWaitingToFinish, rebaseLaneSays } from "../flow/conflicted.ts"; |
| 59 | 59 | import { type ConflictLane, readConflictRoom } from "./conflict_room.ts"; |
| 60 | import { CHECK_FAILED_LIMIT, lastHumanWord, queueAgentRun } from "./dispatch.ts"; | |
| 60 | import { lastHumanWord } from "./task.ts"; | |
| 61 | import { CHECK_FAILED_LIMIT } from "./give_up.ts"; | |
| 62 | import { queueAgentRun } from "./registry.ts"; | |
| 61 | 63 | import { HANDOVER_NOTE } from "./guard.ts"; |
| 62 | 64 | import { interruptionOf } from "./interrupted.ts"; |
| 63 | 65 | import { pickBuilder } from "./route.ts"; |
src/crew/give_up.ts added +9
| @@ -0,0 +1,9 @@ | ||
| 1 | /** Where the crew stops going on its own, and the words a run carries when it did. */ | |
| 2 | ||
| 3 | /** Automatic follow-ups after a failed check, before a person must speak. */ | |
| 4 | export const CHECK_FAILED_LIMIT = 2; | |
| 5 | ||
| 6 | /** What runs said before the count was kept (#11583): still read as given up on. */ | |
| 7 | export const STOPPED_TWICE_LEGACY = "stopped twice by its forge restarting; not tried again"; | |
| 8 | /** What a run says when the host cut it off every time it was tried. */ | |
| 9 | export const INTERRUPTED_OUT = "the host stopped this run five times; not tried again"; |
src/crew/proposal.ts +2 −1
| @@ -19,7 +19,8 @@ import { parseConflicts } from "../git/conflicts.ts"; | ||
| 19 | 19 | import { runGitCapture } from "../git/env.ts"; |
| 20 | 20 | import { isSafeOid, isSafeTreePath } from "../git/guard.ts"; |
| 21 | 21 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 22 | import { agentFor, queueAgentRun } from "./dispatch.ts"; | |
| 22 | import { agentFor } from "./task.ts"; | |
| 23 | import { queueAgentRun } from "./registry.ts"; | |
| 23 | 24 | |
| 24 | 25 | export type ProposeError = |
| 25 | 26 | | "forbidden" |
src/crew/registry.ts added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | /** | |
| 2 | * What the crew service offers the rest of the forge, filled when the service loads and starts. A | |
| 3 | * page or an action asks here, so none of them pulls the dispatcher into the boot's static imports. | |
| 4 | * Asked before the dispatcher loaded, there is nothing to ask: the boot imports the dispatcher when | |
| 5 | * it starts the service, and a test that wants the work imports the dispatcher first. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import type { Issue } from "../core/issues.ts"; | |
| 10 | import type { Agent } from "../core/agents.ts"; | |
| 11 | import type { Repo } from "../core/repos.ts"; | |
| 12 | import type { Run } from "../core/runs.ts"; | |
| 13 | import { createLog } from "../kernel/log.ts"; | |
| 14 | import type { CrewHandle, queueAgentRun as queueRun, sweepAssigned as sweep } from "./dispatch.ts"; | |
| 15 | ||
| 16 | const log = createLog("crew"); | |
| 17 | ||
| 18 | /** The dispatcher's own functions, handed over when its module loads. */ | |
| 19 | export interface CrewWork { | |
| 20 | queueAgentRun: typeof queueRun; | |
| 21 | sweepAssigned: typeof sweep; | |
| 22 | } | |
| 23 | ||
| 24 | /** What the dispatcher's own arguments after the first four are. */ | |
| 25 | type RestOfQueue = Parameters<typeof queueRun> extends [unknown, unknown, unknown, unknown, ...infer R] ? R : never; | |
| 26 | ||
| 27 | let work: CrewWork | undefined; | |
| 28 | let said = false; | |
| 29 | ||
| 30 | /** | |
| 31 | * The work on offer; none until the dispatcher's module has loaded, which is a wiring fault, said | |
| 32 | * once. | |
| 33 | */ | |
| 34 | function crewWork(): CrewWork | undefined { | |
| 35 | if (!work && !said) { | |
| 36 | said = true; | |
| 37 | log.error("crew work asked before the dispatcher loaded: queueing and sweeping do nothing"); | |
| 38 | } | |
| 39 | return work; | |
| 40 | } | |
| 41 | const handles = new WeakMap<App, CrewHandle>(); | |
| 42 | ||
| 43 | export function provideCrewWork(provided: CrewWork): void { | |
| 44 | work = provided; | |
| 45 | } | |
| 46 | ||
| 47 | /** Queue a run about an issue for an agent, unless one is already waiting. */ | |
| 48 | export function queueAgentRun( | |
| 49 | app: App, | |
| 50 | repo: Repo, | |
| 51 | issue: Issue, | |
| 52 | agent: Agent, | |
| 53 | ...rest: RestOfQueue | |
| 54 | ): Run | undefined { | |
| 55 | return crewWork()?.queueAgentRun(app, repo, issue, agent, ...rest); | |
| 56 | } | |
| 57 | ||
| 58 | /** Queue the open issues assigned to an agent that has never had a run. */ | |
| 59 | export function sweepAssigned(app: App): void { | |
| 60 | crewWork()?.sweepAssigned(app); | |
| 61 | } | |
| 62 | ||
| 63 | export function registerCrew(app: App, handle: CrewHandle): void { | |
| 64 | handles.set(app, handle); | |
| 65 | } | |
| 66 | ||
| 67 | export function forgetCrew(app: App): void { | |
| 68 | handles.delete(app); | |
| 69 | } | |
| 70 | ||
| 71 | /** The crew serving this forge, if one was started. */ | |
| 72 | export const crewFor = (app: App): CrewHandle | undefined => handles.get(app); |
src/crew/requested.ts +1 −1
| @@ -35,7 +35,7 @@ import { getUserById } from "../core/users.ts"; | ||
| 35 | 35 | import { sliced } from "../kernel/breathe.ts"; |
| 36 | 36 | import { createLog } from "../kernel/log.ts"; |
| 37 | 37 | import { authorsInRepoPull, BACK_WITH_AUTHOR } from "./authors_pull.ts"; |
| 38 | import { queueAgentRun } from "./dispatch.ts"; | |
| 38 | import { queueAgentRun } from "./registry.ts"; | |
| 39 | 39 | import { interruptionOf } from "./interrupted.ts"; |
| 40 | 40 | import { HANDOVER_NOTE, handoverOf } from "./guard.ts"; |
| 41 | 41 | import { isFirstChange } from "./first_change.ts"; |
src/crew/review.ts +1 −1
| @@ -34,7 +34,7 @@ import { createLog } from "../kernel/log.ts"; | ||
| 34 | 34 | import { guestWaits } from "../origins/guest.ts"; |
| 35 | 35 | import { reviewerPaused } from "./reviewer_paused.ts"; |
| 36 | 36 | import { crewFilters, isStrangerPull } from "./stranger.ts"; |
| 37 | import { queueAgentRun } from "./dispatch.ts"; | |
| 37 | import { queueAgentRun } from "./registry.ts"; | |
| 38 | 38 | import { type Handover, handoverOf, handoversOf } from "./guard.ts"; |
| 39 | 39 | import { crewDownNow, crewDownStretches, diedBeforeAgent, liveMs, type Stretch } from "./review_pause.ts"; |
| 40 | 40 | import { isLaneRevert } from "../checks/revert_lane.ts"; |
src/crew/stale_reds.ts +2 −1
| @@ -34,7 +34,8 @@ import { QUIET_MS, SWEEP_REASON } from "../flow/stale_reds.ts"; | ||
| 34 | 34 | import { mergeable, resolveOid } from "../git/merge.ts"; |
| 35 | 35 | import { createLog } from "../kernel/log.ts"; |
| 36 | 36 | import { authorsInRepoPull } from "./authors_pull.ts"; |
| 37 | import { agentFor, lastHumanWord, queueAgentRun } from "./dispatch.ts"; | |
| 37 | import { agentFor, lastHumanWord } from "./task.ts"; | |
| 38 | import { queueAgentRun } from "./registry.ts"; | |
| 38 | 39 | import { approvedOnHead } from "./finishing.ts"; |
| 39 | 40 | import { failingLine } from "./red_approved.ts"; |
| 40 | 41 |
src/crew/task.ts added +59
| @@ -0,0 +1,59 @@ | ||
| 1 | /** | |
| 2 | * The crew's reading of a task: whose it is, what a run was about, when a person last spoke. Plain | |
| 3 | * questions of the database, kept apart from the dispatcher so a page or an action asks them | |
| 4 | * without loading it. | |
| 5 | */ | |
| 6 | ||
| 7 | import type { App } from "../app.ts"; | |
| 8 | import { getAgent, type Agent } from "../core/agents.ts"; | |
| 9 | import { getIssueById, listComments, type Issue } from "../core/issues.ts"; | |
| 10 | import { getRepoById, type Repo } from "../core/repos.ts"; | |
| 11 | import { listReviews } from "../core/reviews.ts"; | |
| 12 | import type { Run } from "../core/runs.ts"; | |
| 13 | import type { RunReason } from "./brief.ts"; | |
| 14 | ||
| 15 | export function agentFor(app: App, issue: Issue): Agent | undefined { | |
| 16 | if (issue.assigneeId !== null) { | |
| 17 | const assigned = getAgent(app.db, issue.assigneeId); | |
| 18 | if (assigned) return assigned; | |
| 19 | // A person holds it: nothing the agent's name is on starts a run. | |
| 20 | return undefined; | |
| 21 | } | |
| 22 | return issue.kind === "pull" ? getAgent(app.db, issue.authorId) : undefined; | |
| 23 | } | |
| 24 | ||
| 25 | /** When a person last said anything on the conversation, or its start. */ | |
| 26 | export function lastHumanWord(app: App, issue: Issue, agent: Agent): string { | |
| 27 | let latest = issue.createdAt; | |
| 28 | for (const c of listComments(app.db, issue.id)) { | |
| 29 | if (c.authorName !== agent.user.username && c.createdAt > latest) | |
| 30 | latest = c.createdAt; | |
| 31 | } | |
| 32 | for (const r of listReviews(app.db, issue.id)) { | |
| 33 | if (r.authorName !== agent.user.username && r.createdAt > latest) | |
| 34 | latest = r.createdAt; | |
| 35 | } | |
| 36 | return latest; | |
| 37 | } | |
| 38 | ||
| 39 | /** The still-open task a run was about, and the reason to queue it under again. */ | |
| 40 | export function taskOf( | |
| 41 | app: App, | |
| 42 | run: Run, | |
| 43 | ): { issue: Issue; repo: Repo; agent: Agent; reason: RunReason } | undefined { | |
| 44 | if (run.issueId === null) return undefined; | |
| 45 | const issue = getIssueById(app.db, run.issueId); | |
| 46 | const repo = getRepoById(app.db, run.repoId); | |
| 47 | const agent = run.actorId ? getAgent(app.db, run.actorId) : undefined; | |
| 48 | if (!issue || !repo || !agent || issue.state !== "open") return undefined; | |
| 49 | const reason: RunReason = | |
| 50 | run.reason === "feedback" || | |
| 51 | run.reason === "check_failed" || | |
| 52 | run.reason === "conflict" || | |
| 53 | run.reason === "resolve" || | |
| 54 | run.reason === "converge" || | |
| 55 | run.reason === "review" | |
| 56 | ? run.reason | |
| 57 | : "assigned"; | |
| 58 | return { issue, repo, agent, reason }; | |
| 59 | } |
src/engine/duties.ts +1 −1
| @@ -29,7 +29,7 @@ import { pruneTokens } from "../core/tokens.ts"; | ||
| 29 | 29 | import { getRepoById } from "../core/repos.ts"; |
| 30 | 30 | import { plannedRepoIds, tickWeeks } from "../flow/retro.ts"; |
| 31 | 31 | import { during } from "../perf/jobs.ts"; |
| 32 | import { sweepAssigned } from "../crew/dispatch.ts"; | |
| 32 | import { sweepAssigned } from "../crew/registry.ts"; | |
| 33 | 33 | import { startClogTick } from "../flow/clog_tick.ts"; |
| 34 | 34 | import { startKeeper } from "../flow/keeper.ts"; |
| 35 | 35 | import { recordAskLedger } from "../web/studio/asks.ts"; |
src/runs/interrupted_out.ts +1 −1
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import type { Database } from "bun:sqlite"; |
| 8 | 8 | import type { Repo } from "../core/repos.ts"; |
| 9 | import { INTERRUPTED_OUT, STOPPED_TWICE_LEGACY } from "../crew/dispatch.ts"; | |
| 9 | import { INTERRUPTED_OUT, STOPPED_TWICE_LEGACY } from "../crew/give_up.ts"; | |
| 10 | 10 | |
| 11 | 11 | const WINDOW_MS = 24 * 60 * 60 * 1000; |
| 12 | 12 | export const MAX_STOPPED = 5; |
src/tui/screens/runs.ts +1 −1
| @@ -12,7 +12,7 @@ import { cancelRepoRun, listRepoRuns, requeueRepoRun } from "../../actions/runs. | ||
| 12 | 12 | import { keyOf, oldKeys } from "../../web/keymap.ts"; |
| 13 | 13 | import { lacks } from "../../core/roles.ts"; |
| 14 | 14 | import { canWrite, type Repo } from "../../core/repos.ts"; |
| 15 | import { taskOf } from "../../crew/dispatch.ts"; | |
| 15 | import { taskOf } from "../../crew/task.ts"; | |
| 16 | 16 | import { words } from "../../fleet/verbs.ts"; |
| 17 | 17 | import type { Run, RunState } from "../../core/runs.ts"; |
| 18 | 18 | import type { Ctx, Screen } from "../app/app.ts"; |
src/web/pages/crew.ts +1 −1
| @@ -66,7 +66,7 @@ import { modelDays, type ModelDay } from "../../core/model_days.ts"; | ||
| 66 | 66 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| 67 | 67 | import { listTeams, type Team } from "../../core/teams.ts"; |
| 68 | 68 | import { isHostedChild } from "../../spawn/hosted-mark.ts"; |
| 69 | import { crewFor } from "../../crew/dispatch.ts"; | |
| 69 | import { crewFor } from "../../crew/registry.ts"; | |
| 70 | 70 | import { tryAgain } from "../../actions/try_again.ts"; |
| 71 | 71 | import { crewHandover, type CrewHandover } from "../../crew/handed.ts"; |
| 72 | 72 | import { MODELS } from "../../crew/models.ts"; |
test/approved-week.test.ts +1 −1
| @@ -16,7 +16,7 @@ import { getIssueById, listComments } from "../src/core/issues.ts"; | ||
| 16 | 16 | import { getPull, type Pull } from "../src/core/pulls.ts"; |
| 17 | 17 | import { getRepoById, type Repo } from "../src/core/repos.ts"; |
| 18 | 18 | import { submitReview } from "../src/core/reviews.ts"; |
| 19 | import { CHECK_FAILED_LIMIT } from "../src/crew/dispatch.ts"; | |
| 19 | import { CHECK_FAILED_LIMIT } from "../src/crew/give_up.ts"; | |
| 20 | 20 | import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; |
| 21 | 21 | import { setHold } from "../src/core/holds.ts"; |
| 22 | 22 | import { createUser, getUserByName, hasPassword, verifyPasswordThrottled, type User } from "../src/core/users.ts"; |
test/boot-lazy.test.ts +1 −1
| @@ -29,7 +29,7 @@ test("the terminal's server is imported dynamically by the boot", () => { | ||
| 29 | 29 | // apps/runtime is out (#17179): its accessors live in apps/runtime_registry.ts. |
| 30 | 30 | test("the boot's static imports do not reach the services only it starts", () => { |
| 31 | 31 | const closure = [...staticClosure(resolve("src/main.ts"))]; |
| 32 | const only = ["maintain/runner.ts", "nightly/service.ts", "keeper/keeper.ts", "keeper/wire.ts", "crew/routines.ts", "checks/service.ts", "snapshot/service.ts", "apps/runtime.ts", "looks/service.ts"]; | |
| 32 | const only = ["maintain/runner.ts", "nightly/service.ts", "keeper/keeper.ts", "keeper/wire.ts", "crew/routines.ts", "crew/dispatch.ts", "checks/service.ts", "snapshot/service.ts", "apps/runtime.ts", "looks/service.ts"]; | |
| 33 | 33 | for (const file of only) expect(closure.some((f) => f.endsWith(`/src/${file}`)), file).toBe(false); |
| 34 | 34 | }); |
| 35 | 35 |
test/checks-disk-red.test.ts +2 −1
| @@ -12,7 +12,8 @@ import { announceRun } from "../src/actions/runs.ts"; | ||
| 12 | 12 | import { commitToBranch } from "../src/actions/repos.ts"; |
| 13 | 13 | import { openPull } from "../src/actions/pulls.ts"; |
| 14 | 14 | import { diskReading, withDisk } from "../src/checks/disk_red.ts"; |
| 15 | import { CHECK_FAILED_LIMIT, recheckForDisk } from "../src/crew/dispatch.ts"; | |
| 15 | import { recheckForDisk } from "../src/crew/dispatch.ts"; | |
| 16 | import { CHECK_FAILED_LIMIT } from "../src/crew/give_up.ts"; | |
| 16 | 17 | import { getIssueById } from "../src/core/issues.ts"; |
| 17 | 18 | import { getPull, type Pull } from "../src/core/pulls.ts"; |
| 18 | 19 | import type { Repo } from "../src/core/repos.ts"; |
test/crew-cancel-by.test.ts +2
| @@ -5,6 +5,8 @@ | ||
| 5 | 5 | * is alive, and the run's log ends with one line saying it was cancelled, |
| 6 | 6 | * and by whom. |
| 7 | 7 | */ |
| 8 | // Runs are queued through the crew registry, which holds the dispatcher once its module has loaded. | |
| 9 | import "../src/crew/dispatch.ts"; | |
| 8 | 10 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 9 | 11 | import { existsSync, readFileSync } from "node:fs"; |
| 10 | 12 | import { join } from "node:path"; |
test/crew-failed-while-building.test.ts +1 −1
| @@ -11,7 +11,7 @@ import { announceRun } from "../src/actions/runs.ts"; | ||
| 11 | 11 | import { commitToBranch } from "../src/actions/repos.ts"; |
| 12 | 12 | import { openPull } from "../src/actions/pulls.ts"; |
| 13 | 13 | import { openIssue, setIssueState } from "../src/actions/issues.ts"; |
| 14 | import { CHECK_FAILED_LIMIT } from "../src/crew/dispatch.ts"; | |
| 14 | import { CHECK_FAILED_LIMIT } from "../src/crew/give_up.ts"; | |
| 15 | 15 | import { getIssueById } from "../src/core/issues.ts"; |
| 16 | 16 | import { getPull, type Pull } from "../src/core/pulls.ts"; |
| 17 | 17 | import type { Repo } from "../src/core/repos.ts"; |
test/crew-interrupted-out.test.ts +2 −1
| @@ -10,7 +10,8 @@ import { createRepo } from "../src/actions/repos.ts"; | ||
| 10 | 10 | import { createApp, type App } from "../src/app.ts"; |
| 11 | 11 | import { claimRun, createRun, getRun } from "../src/core/runs.ts"; |
| 12 | 12 | import { createUser } from "../src/core/users.ts"; |
| 13 | import { INTERRUPTED_OUT, MAX_INTERRUPTIONS, interruptionBackoffMs, requeue, requeueInterrupted } from "../src/crew/dispatch.ts"; | |
| 13 | import { MAX_INTERRUPTIONS, interruptionBackoffMs, requeue, requeueInterrupted } from "../src/crew/dispatch.ts"; | |
| 14 | import { INTERRUPTED_OUT } from "../src/crew/give_up.ts"; | |
| 14 | 15 | import { daySpent } from "../src/core/account_ceiling.ts"; |
| 15 | 16 | import { noteRunProgress } from "../src/core/runs.ts"; |
| 16 | 17 | import { recordUsage, usageTotals } from "../src/core/usage.ts"; |
test/crew-merge-conflict.test.ts +1 −1
| @@ -12,7 +12,7 @@ import { announceRun } from "../src/actions/runs.ts"; | ||
| 12 | 12 | import { commitToBranch } from "../src/actions/repos.ts"; |
| 13 | 13 | import { openPull } from "../src/actions/pulls.ts"; |
| 14 | 14 | import { NO_MERGE } from "../src/checks/merged.ts"; |
| 15 | import { CHECK_FAILED_LIMIT } from "../src/crew/dispatch.ts"; | |
| 15 | import { CHECK_FAILED_LIMIT } from "../src/crew/give_up.ts"; | |
| 16 | 16 | import { getIssueById } from "../src/core/issues.ts"; |
| 17 | 17 | import { getPull, type Pull } from "../src/core/pulls.ts"; |
| 18 | 18 | import type { Repo } from "../src/core/repos.ts"; |
test/crew-registry.test.ts added +83
| @@ -0,0 +1,83 @@ | ||
| 1 | import { afterEach, beforeEach, expect, spyOn, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { createAgentRow } from "../src/core/agents.ts"; | |
| 7 | import { createIssue } from "../src/core/issues.ts"; | |
| 8 | import { createRepo } from "../src/core/repos.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import type { CrewHandle } from "../src/crew/dispatch.ts"; | |
| 11 | import type { CrewWork } from "../src/crew/registry.ts"; | |
| 12 | ||
| 13 | // A fresh copy of the registry per test: the preload has filled the shared one. | |
| 14 | type Registry = typeof import("../src/crew/registry.ts"); | |
| 15 | let n = 0; | |
| 16 | const fresh = async (): Promise<Registry> => (await import(`../src/crew/registry.ts?fresh=${n++}`)) as Registry; | |
| 17 | ||
| 18 | let dir: string; | |
| 19 | let app: App; | |
| 20 | let asked: Parameters<Registry["queueAgentRun"]>; | |
| 21 | beforeEach(async () => { | |
| 22 | dir = await mkdtemp(join(tmpdir(), "forge-crew-registry-")); | |
| 23 | app = createApp({ dataDir: dir, port: 0 }); | |
| 24 | const user = await createUser(app.db, { | |
| 25 | origin: "seed", | |
| 26 | trusted: true, | |
| 27 | username: "mason", | |
| 28 | email: "[email protected]", | |
| 29 | password: "a-good-passphrase", | |
| 30 | }); | |
| 31 | if (!user.ok) throw new Error(user.error); | |
| 32 | const repo = await createRepo(app.db, join(dir, "repos"), { ownerId: user.value.id, name: "forge" }); | |
| 33 | if (!repo.ok) throw new Error(repo.error); | |
| 34 | const issue = createIssue(app.db, { repoId: repo.value.id, authorId: user.value.id, title: "Seven" }); | |
| 35 | if (!issue.ok) throw new Error(issue.error); | |
| 36 | asked = [app, repo.value, issue.value, createAgentRow(app.db, { userId: user.value.id }), "assigned"]; | |
| 37 | }); | |
| 38 | afterEach(async () => { | |
| 39 | app.close(); | |
| 40 | await rm(dir, { recursive: true, force: true }); | |
| 41 | }); | |
| 42 | ||
| 43 | const stub = (calls: string[]): CrewWork => ({ | |
| 44 | queueAgentRun: (_app, _repo, issue) => { | |
| 45 | calls.push(`queue ${issue.title}`); | |
| 46 | return undefined; | |
| 47 | }, | |
| 48 | sweepAssigned: () => { | |
| 49 | calls.push("sweep"); | |
| 50 | }, | |
| 51 | }); | |
| 52 | ||
| 53 | test("work provided is what queueAgentRun and sweepAssigned delegate to", async () => { | |
| 54 | const registry = await fresh(); | |
| 55 | const calls: string[] = []; | |
| 56 | registry.provideCrewWork(stub(calls)); | |
| 57 | registry.queueAgentRun(...asked); | |
| 58 | registry.sweepAssigned(app); | |
| 59 | expect(calls).toEqual(["queue Seven", "sweep"]); | |
| 60 | }); | |
| 61 | ||
| 62 | test("asked before work is provided, it says so once and does nothing", async () => { | |
| 63 | const registry = await fresh(); | |
| 64 | const err = spyOn(console, "error").mockImplementation(() => {}); | |
| 65 | try { | |
| 66 | expect(registry.queueAgentRun(...asked)).toBeUndefined(); | |
| 67 | registry.sweepAssigned(app); | |
| 68 | expect(err).toHaveBeenCalledTimes(1); | |
| 69 | expect(String(err.mock.calls[0]?.[0])).toContain("before the dispatcher loaded"); | |
| 70 | } finally { | |
| 71 | err.mockRestore(); | |
| 72 | } | |
| 73 | }); | |
| 74 | ||
| 75 | test("crewFor follows registerCrew and forgetCrew", async () => { | |
| 76 | const registry = await fresh(); | |
| 77 | const handle = {} as CrewHandle; | |
| 78 | expect(registry.crewFor(app)).toBeUndefined(); | |
| 79 | registry.registerCrew(app, handle); | |
| 80 | expect(registry.crewFor(app)).toBe(handle); | |
| 81 | registry.forgetCrew(app); | |
| 82 | expect(registry.crewFor(app)).toBeUndefined(); | |
| 83 | }); |
test/forge-origin-follow.test.ts +2
| @@ -1,3 +1,5 @@ | ||
| 1 | // Runs are queued through the crew registry, which holds the dispatcher once its module has loaded. | |
| 2 | import "../src/crew/dispatch.ts"; | |
| 1 | 3 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 2 | 4 | import { mkdtemp, rm } from "node:fs/promises"; |
| 3 | 5 | import { tmpdir } from "node:os"; |
test/guests.test.ts +2 −1
| @@ -34,7 +34,8 @@ import { cancelRun, createRun, listRuns } from "../src/core/runs.ts"; | ||
| 34 | 34 | import { addComment } from "../src/actions/issues.ts"; |
| 35 | 35 | import { requeueRepoRun } from "../src/actions/runs.ts"; |
| 36 | 36 | import { proposeResolution } from "../src/crew/proposal.ts"; |
| 37 | import { agentFor, queueAgentRun, sweepAssigned } from "../src/crew/dispatch.ts"; | |
| 37 | import { queueAgentRun, sweepAssigned } from "../src/crew/dispatch.ts"; | |
| 38 | import { agentFor } from "../src/crew/task.ts"; | |
| 38 | 39 | import { getIssueById } from "../src/core/issues.ts"; |
| 39 | 40 | import { convergeFork } from "../src/crew/converge.ts"; |
| 40 | 41 | import { convergeWords } from "../src/actions/follow.ts"; |
test/helpers/preload.ts +4
| @@ -87,3 +87,7 @@ try { | ||
| 87 | 87 | } catch { |
| 88 | 88 | // Not a test run: nothing waits to be said. |
| 89 | 89 | } |
| 90 | ||
| 91 | // The boot imports the crew's dispatcher when it starts the service; a suite has no boot, so the | |
| 92 | // crew's work (queueing a run, sweeping the assigned) is loaded here, once (`crew/registry.ts`). | |
| 93 | await import("../../src/crew/dispatch.ts"); |
test/home-interrupted-out.test.ts +1 −1
| @@ -9,7 +9,7 @@ import { addToCrew, createAgent } from "../src/actions/crew.ts"; | ||
| 9 | 9 | import { interruptedOutRuns } from "../src/runs/interrupted_out.ts"; |
| 10 | 10 | import { interruptedOutScreen } from "../src/tui/screens/interrupted_out.ts"; |
| 11 | 11 | import { getRepo } from "../src/core/repos.ts"; |
| 12 | import { INTERRUPTED_OUT, STOPPED_TWICE_LEGACY } from "../src/crew/dispatch.ts"; | |
| 12 | import { INTERRUPTED_OUT, STOPPED_TWICE_LEGACY } from "../src/crew/give_up.ts"; | |
| 13 | 13 | import { createIssue } from "../src/core/issues.ts"; |
| 14 | 14 | import { createSession } from "../src/core/sessions.ts"; |
| 15 | 15 | import { createUser, type User } from "../src/core/users.ts"; |
test/maintain-edge-kept.test.ts +1 −1
| @@ -17,7 +17,7 @@ import { createRepo } from "../src/actions/repos.ts"; | ||
| 17 | 17 | import { createAgent } from "../src/actions/crew.ts"; |
| 18 | 18 | import { createIssue, getIssue } from "../src/core/issues.ts"; |
| 19 | 19 | import { getRepo } from "../src/core/repos.ts"; |
| 20 | import { agentFor } from "../src/crew/dispatch.ts"; | |
| 20 | import { agentFor } from "../src/crew/task.ts"; | |
| 21 | 21 | import { ownRepoPath } from "../src/kernel/own_repo.ts"; |
| 22 | 22 | |
| 23 | 23 | const h = (o: Record<string, string>) => new Headers(o); |
test/run-requeue-refusals.test.ts +3
| @@ -4,6 +4,9 @@ | ||
| 4 | 4 | * its own word, and none of them leaves a run behind. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | // The run is queued through the crew registry, which holds the dispatcher once its module has | |
| 8 | // loaded. | |
| 9 | import "../src/crew/dispatch.ts"; | |
| 7 | 10 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 8 | 11 | import { mkdtemp, rm } from "node:fs/promises"; |
| 9 | 12 | import { tmpdir } from "node:os"; |
test/upstream_follow.test.ts +2
| @@ -4,6 +4,8 @@ | ||
| 4 | 4 | * the vetted address, a token only to its exact https origin. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | // Runs are queued through the crew registry, which holds the dispatcher once its module has loaded. | |
| 8 | import "../src/crew/dispatch.ts"; | |
| 7 | 9 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 8 | 10 | import { mkdtemp, rm } from "node:fs/promises"; |
| 9 | 11 | import { tmpdir } from "node:os"; |
new above
offline