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

opened this

Merged 31h 35m ago, live in production

mason/forge:mason/17181-move-the-accessors-out-of-crew-dispatch → main 0 commits fork

  • Merged. mason/17181-move-the-accessors-out-of-crew-dispatch was merged into main as f81c34c.

Review

Sign in to review this change.
37 files changed +293 −98
src/actions/proposals.ts +1 −1
@@ -16,7 +16,7 @@ import { getProposal, moveProposal, type Proposal, type ProposalState } from "..
1616 import { ARCHIVED_WHY, getRepoById, ownerFactRepoIds, canRead, type Repo } from "../core/repos.ts";
1717 import type { User } from "../core/users.ts";
1818 import { ensureDirectionIssue } from "../crew/direction_issue.ts";
19 import { queueAgentRun } from "../crew/dispatch.ts";
19 import { queueAgentRun } from "../crew/registry.ts";
2020 import { err, ok, type Result } from "../kernel/result.ts";
2121 import { MAX_BODY } from "../core/issues.ts";
2222 import { NOT_A_MEMBER } from "../core/roles.ts";
src/actions/runs.ts +2 −1
@@ -38,7 +38,8 @@ import { abortAgentRun } from "../crew/running.ts";
3838 import { stopRun } from "../runs/stoppable.ts";
3939 import { whenEnded } from "../runs/ending.ts";
4040 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";
4243 import { err, ok, type Result } from "../kernel/result.ts";
4344 import { restoreRunLog } from "../runs/archive.ts";
4445 import { readRunLog, readRunLogTail, runLogPath } from "../runs/log.ts";
src/actions/upstream_follow.ts +1 −1
@@ -31,7 +31,7 @@ import {
3131 } from "../core/issues.ts";
3232 import { listCrewRoles, type Agent } from "../core/agents.ts";
3333 import { listRuns } from "../core/runs.ts";
34 import { queueAgentRun } from "../crew/dispatch.ts";
34 import { queueAgentRun } from "../crew/registry.ts";
3535 import { pickBuilder } from "../crew/route.ts";
3636 import { getOriginFor } from "../core/origins.ts";
3737 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";
3737 import { createLog } from "../kernel/log.ts";
3838 import { landIfGreen } from "./land_queue.ts";
3939 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";
4142 import { ownBuilder } from "./finishing.ts";
4243
4344 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
1414 import { PILLAR_MAX, type Repo } from "../core/repos.ts";
1515 import { repoDir } from "../git/repos.ts";
1616 import type { BoxList } from "./brief.ts";
17 import { queueAgentRun } from "./dispatch.ts";
17 import { queueAgentRun } from "./registry.ts";
1818 import { pickReviewer, reviewersOf, reviewLoad, reviewsInFlight } from "./review.ts";
1919 import { reviewRange } from "./review_range.ts";
2020
src/crew/conflict_release.ts +3 −1
@@ -12,7 +12,9 @@ import type { Repo } from "../core/repos.ts";
1212 import { activeRunFor, automaticRunsSince } from "../core/runs.ts";
1313 import { getUserById } from "../core/users.ts";
1414 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";
1618
1719 const log = createLog("autonomy");
1820
src/crew/conflict_sweep.ts +2 −1
@@ -11,7 +11,8 @@ import { activeRunFor } from "../core/runs.ts";
1111 import { mergeable, resolveOid, treeOf } from "../git/merge.ts";
1212 import { bringUpToDate, closeSuperseded, releaseConflicted } from "./conflict_release.ts";
1313 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";
1516 import { approvedOnHead } from "./finishing.ts";
1617 import { closeStale } from "./stale_pulls.ts";
1718
src/crew/converge.ts +1 −1
@@ -46,7 +46,7 @@ import { repoDir } from "../git/repos.ts";
4646 import { createLog } from "../kernel/log.ts";
4747 import { parkedCount } from "./park_issue.ts";
4848 import { PARK_STRIKES } from "./parks.ts";
49 import { queueAgentRun } from "./dispatch.ts";
49 import { queueAgentRun } from "./registry.ts";
5050 import { pickBuilder } from "./route.ts";
5151
5252 const log = createLog("converge");
src/crew/dispatch.ts +18 −71
@@ -8,6 +8,9 @@
88 * a run.
99 */
1010
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";
1114 import { isQuarantined } from "../core/quarantine.ts";
1215 import { ceilingHold, pausedUntil } from "../core/planner_pause.ts";
1316 import { pullsCeilingHold } from "../core/pulls_ceiling.ts";
@@ -72,7 +75,6 @@ import { inFocus } from "./focus.ts";
7275 import { inMaintenance } from "../maintenance/state.ts";
7376 import { gateQueue } from "../flow/valve_gate.ts";
7477 import { ARCHIVED_WHY, canWrite, contributes, getRepoById, type Repo } from "../core/repos.ts";
75 import { listReviews } from "../core/reviews.ts";
7678 import { mentionedReviewer, requestReview } from "../actions/review_requests.ts";
7779 import {
7880 crewReviews,
@@ -167,8 +169,16 @@ import { markRedRouted, redRouted } from "./red_routed.ts";
167169
168170 const log = createLog("crew");
169171
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
172182 /**
173183 * The dispatcher's pass. Under `bun test` (NODE_ENV=test) it is 50 ms: tests
174184 * that start the real dispatcher waited out five seconds per pass (#12570).
@@ -229,11 +239,6 @@ const DRAIN_MS = 3 * 60_000;
229239 /** How long `stop()` waits for the tick's own sweeps (converge, autonomy) to settle. */
230240 const SWEEPS_STOP_MS = 10_000;
231241
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
237242 export { runnerSettings } from "./runner_env.ts";
238243
239244 export async function startCrew(
@@ -442,7 +447,7 @@ export async function startCrew(
442447 setTimeout(() => resolve("late"), options.drainMs ?? DRAIN_MS),
443448 );
444449 if ((await Promise.race([done, late])) === "done") {
445 handles.delete(app);
450 forgetCrew(app);
446451 return;
447452 }
448453 }
@@ -455,10 +460,10 @@ export async function startCrew(
455460 await Promise.all(inFlight.values());
456461 for (const run of interrupted)
457462 requeue(app, run, "stopped for a restart; queued again", false);
458 handles.delete(app);
463 forgetCrew(app);
459464 },
460465 };
461 handles.set(app, handle);
466 registerCrew(app, handle);
462467 return handle;
463468 }
464469
@@ -1474,16 +1479,6 @@ export async function answerFailedCheck(
14741479 return "reworked";
14751480 }
14761481
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
14871482 /** What the crew makes of a red check: whose fault, and whether to try again. */
14881483 export interface CheckJudgement {
14891484 /** Whether another automatic try by the agent is worth its cost. */
@@ -1590,34 +1585,6 @@ export async function judgeFailedCheck(
15901585 return { retry, cause, failingTest };
15911586 }
15921587
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";
16211588 /** How many interruptions a task takes before it is given up (#11693). */
16221589 export const MAX_INTERRUPTIONS = 5;
16231590 const BACKOFF_FIRST_MS = 15_000;
@@ -1750,28 +1717,6 @@ function resumeLater(app: App, run: Run, pause: Pause): void {
17501717 });
17511718 }
17521719
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
17751720 /**
17761721 * Who last assigned it: the newest `issue.assigned` or `pull.assigned`. The
17771722 * `+ 0` drops the column's affinity so the event expression index
@@ -2046,3 +1991,5 @@ export function pump(
20461991 }
20471992 noteHeld(app.db, held);
20481993 }
1994
1995 provideCrewWork({ queueAgentRun, sweepAssigned });
src/crew/finishing.ts +3 −1
@@ -57,7 +57,9 @@ import { committedAt } from "../git/read.ts";
5757 import { createLog } from "../kernel/log.ts";
5858 import { noteWaitingToFinish, rebaseLaneSays } from "../flow/conflicted.ts";
5959 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";
6163 import { HANDOVER_NOTE } from "./guard.ts";
6264 import { interruptionOf } from "./interrupted.ts";
6365 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";
1919 import { runGitCapture } from "../git/env.ts";
2020 import { isSafeOid, isSafeTreePath } from "../git/guard.ts";
2121 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";
2324
2425 export type ProposeError =
2526 | "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";
3535 import { sliced } from "../kernel/breathe.ts";
3636 import { createLog } from "../kernel/log.ts";
3737 import { authorsInRepoPull, BACK_WITH_AUTHOR } from "./authors_pull.ts";
38 import { queueAgentRun } from "./dispatch.ts";
38 import { queueAgentRun } from "./registry.ts";
3939 import { interruptionOf } from "./interrupted.ts";
4040 import { HANDOVER_NOTE, handoverOf } from "./guard.ts";
4141 import { isFirstChange } from "./first_change.ts";
src/crew/review.ts +1 −1
@@ -34,7 +34,7 @@ import { createLog } from "../kernel/log.ts";
3434 import { guestWaits } from "../origins/guest.ts";
3535 import { reviewerPaused } from "./reviewer_paused.ts";
3636 import { crewFilters, isStrangerPull } from "./stranger.ts";
37 import { queueAgentRun } from "./dispatch.ts";
37 import { queueAgentRun } from "./registry.ts";
3838 import { type Handover, handoverOf, handoversOf } from "./guard.ts";
3939 import { crewDownNow, crewDownStretches, diedBeforeAgent, liveMs, type Stretch } from "./review_pause.ts";
4040 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";
3434 import { mergeable, resolveOid } from "../git/merge.ts";
3535 import { createLog } from "../kernel/log.ts";
3636 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";
3839 import { approvedOnHead } from "./finishing.ts";
3940 import { failingLine } from "./red_approved.ts";
4041
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";
2929 import { getRepoById } from "../core/repos.ts";
3030 import { plannedRepoIds, tickWeeks } from "../flow/retro.ts";
3131 import { during } from "../perf/jobs.ts";
32 import { sweepAssigned } from "../crew/dispatch.ts";
32 import { sweepAssigned } from "../crew/registry.ts";
3333 import { startClogTick } from "../flow/clog_tick.ts";
3434 import { startKeeper } from "../flow/keeper.ts";
3535 import { recordAskLedger } from "../web/studio/asks.ts";
src/runs/interrupted_out.ts +1 −1
@@ -6,7 +6,7 @@
66
77 import type { Database } from "bun:sqlite";
88 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";
1010
1111 const WINDOW_MS = 24 * 60 * 60 * 1000;
1212 export const MAX_STOPPED = 5;
src/tui/screens/runs.ts +1 −1
@@ -12,7 +12,7 @@ import { cancelRepoRun, listRepoRuns, requeueRepoRun } from "../../actions/runs.
1212 import { keyOf, oldKeys } from "../../web/keymap.ts";
1313 import { lacks } from "../../core/roles.ts";
1414 import { canWrite, type Repo } from "../../core/repos.ts";
15 import { taskOf } from "../../crew/dispatch.ts";
15 import { taskOf } from "../../crew/task.ts";
1616 import { words } from "../../fleet/verbs.ts";
1717 import type { Run, RunState } from "../../core/runs.ts";
1818 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";
6666 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
6767 import { listTeams, type Team } from "../../core/teams.ts";
6868 import { isHostedChild } from "../../spawn/hosted-mark.ts";
69 import { crewFor } from "../../crew/dispatch.ts";
69 import { crewFor } from "../../crew/registry.ts";
7070 import { tryAgain } from "../../actions/try_again.ts";
7171 import { crewHandover, type CrewHandover } from "../../crew/handed.ts";
7272 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";
1616 import { getPull, type Pull } from "../src/core/pulls.ts";
1717 import { getRepoById, type Repo } from "../src/core/repos.ts";
1818 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";
2020 import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts";
2121 import { setHold } from "../src/core/holds.ts";
2222 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", () => {
2929 // apps/runtime is out (#17179): its accessors live in apps/runtime_registry.ts.
3030 test("the boot's static imports do not reach the services only it starts", () => {
3131 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"];
3333 for (const file of only) expect(closure.some((f) => f.endsWith(`/src/${file}`)), file).toBe(false);
3434 });
3535
test/checks-disk-red.test.ts +2 −1
@@ -12,7 +12,8 @@ import { announceRun } from "../src/actions/runs.ts";
1212 import { commitToBranch } from "../src/actions/repos.ts";
1313 import { openPull } from "../src/actions/pulls.ts";
1414 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";
1617 import { getIssueById } from "../src/core/issues.ts";
1718 import { getPull, type Pull } from "../src/core/pulls.ts";
1819 import type { Repo } from "../src/core/repos.ts";
test/crew-cancel-by.test.ts +2
@@ -5,6 +5,8 @@
55 * is alive, and the run's log ends with one line saying it was cancelled,
66 * and by whom.
77 */
8 // Runs are queued through the crew registry, which holds the dispatcher once its module has loaded.
9 import "../src/crew/dispatch.ts";
810 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
911 import { existsSync, readFileSync } from "node:fs";
1012 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";
1111 import { commitToBranch } from "../src/actions/repos.ts";
1212 import { openPull } from "../src/actions/pulls.ts";
1313 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";
1515 import { getIssueById } from "../src/core/issues.ts";
1616 import { getPull, type Pull } from "../src/core/pulls.ts";
1717 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";
1010 import { createApp, type App } from "../src/app.ts";
1111 import { claimRun, createRun, getRun } from "../src/core/runs.ts";
1212 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";
1415 import { daySpent } from "../src/core/account_ceiling.ts";
1516 import { noteRunProgress } from "../src/core/runs.ts";
1617 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";
1212 import { commitToBranch } from "../src/actions/repos.ts";
1313 import { openPull } from "../src/actions/pulls.ts";
1414 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";
1616 import { getIssueById } from "../src/core/issues.ts";
1717 import { getPull, type Pull } from "../src/core/pulls.ts";
1818 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/crew-shared-red.test.ts +1 −1
@@ -14,7 +14,7 @@ import type { App } from "../src/app.ts";
1414 import { announceRun } from "../src/actions/runs.ts";
1515 import { commitToBranch } from "../src/actions/repos.ts";
1616 import { openPull } from "../src/actions/pulls.ts";
17 import { CHECK_FAILED_LIMIT } from "../src/crew/dispatch.ts";
17 import { CHECK_FAILED_LIMIT } from "../src/crew/give_up.ts";
1818 import { getIssueById } from "../src/core/issues.ts";
1919 import { getPull, type Pull } from "../src/core/pulls.ts";
2020 import type { Repo } from "../src/core/repos.ts";
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";
13 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
24 import { mkdtemp, rm } from "node:fs/promises";
35 import { tmpdir } from "node:os";
test/guests.test.ts +2 −1
@@ -34,7 +34,8 @@ import { cancelRun, createRun, listRuns } from "../src/core/runs.ts";
3434 import { addComment } from "../src/actions/issues.ts";
3535 import { requeueRepoRun } from "../src/actions/runs.ts";
3636 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";
3839 import { getIssueById } from "../src/core/issues.ts";
3940 import { convergeFork } from "../src/crew/converge.ts";
4041 import { convergeWords } from "../src/actions/follow.ts";
test/helpers/preload.ts +4
@@ -87,3 +87,7 @@ try {
8787 } catch {
8888 // Not a test run: nothing waits to be said.
8989 }
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";
99 import { interruptedOutRuns } from "../src/runs/interrupted_out.ts";
1010 import { interruptedOutScreen } from "../src/tui/screens/interrupted_out.ts";
1111 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";
1313 import { createIssue } from "../src/core/issues.ts";
1414 import { createSession } from "../src/core/sessions.ts";
1515 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";
1717 import { createAgent } from "../src/actions/crew.ts";
1818 import { createIssue, getIssue } from "../src/core/issues.ts";
1919 import { getRepo } from "../src/core/repos.ts";
20 import { agentFor } from "../src/crew/dispatch.ts";
20 import { agentFor } from "../src/crew/task.ts";
2121 import { ownRepoPath } from "../src/kernel/own_repo.ts";
2222
2323 const h = (o: Record<string, string>) => new Headers(o);
test/run-requeue-refusals.test.ts +3
@@ -4,6 +4,9 @@
44 * its own word, and none of them leaves a run behind.
55 */
66
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";
710 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
811 import { mkdtemp, rm } from "node:fs/promises";
912 import { tmpdir } from "node:os";
test/upstream_follow.test.ts +2
@@ -4,6 +4,8 @@
44 * the vetted address, a token only to its exact https origin.
55 */
66
7 // Runs are queued through the crew registry, which holds the dispatcher once its module has loaded.
8 import "../src/crew/dispatch.ts";
79 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
810 import { mkdtemp, rm } from "node:fs/promises";
911 import { tmpdir } from "node:os";

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs