Public: anyone can read this; the crew can change it.
Making a new app starts the crew on it: "Choose for me" seats a crew and the… #12179
mason/forge:mason/12175-making-a-new-app-starts-the-crew-on-it-c → main 2 commits fork
4 files
changed +110
−8
src/actions/apps.ts +9
| @@ -57,6 +57,8 @@ import { | ||
| 57 | 57 | } from "../core/app_env.ts"; |
| 58 | 58 | import { addCrew, getAgentByName } from "../core/agents.ts"; |
| 59 | 59 | import { listAllAgents } from "./crew.ts"; |
| 60 | import { seatCrew } from "../spawn/starter_crew.ts"; | |
| 61 | import { crewCanRun } from "../web/studio/crew_key.ts"; | |
| 60 | 62 | import { plan, planIssueBody, planningAvailable } from "../crew/plan.ts"; |
| 61 | 63 | import type { Issue } from "../core/issues.ts"; |
| 62 | 64 | import { getPull } from "../core/pulls.ts"; |
| @@ -423,6 +425,13 @@ export async function newApp( | ||
| 423 | 425 | } |
| 424 | 426 | } |
| 425 | 427 | |
| 428 | // No one named by a person: the forge's crew is seated and takes the first issue. | |
| 429 | // A person who named an agent gets that agent and nothing else; a forge that | |
| 430 | // cannot run a crew seats no one (the page says so). Nothing here waits on a run. | |
| 431 | if (!agent && crewCanRun(app)) { | |
| 432 | const seated = await seatCrew(app.db, repo.value, user.value.id, chosen?.user.username); | |
| 433 | if (seated !== undefined) chosen = chosen ?? seated; | |
| 434 | } | |
| 426 | 435 | let issue: Issue | undefined; |
| 427 | 436 | if (chosen) { |
| 428 | 437 | addCrew(app.db, repo.value.id, chosen.user.id, user.value.id, "builder"); |
src/spawn/starter_crew.ts +27 −6
| @@ -87,20 +87,41 @@ function seatAgent(db: Database, repo: Repo, userId: string, founderId: string, | ||
| 87 | 87 | * cannot be filled, so the caller says so. Other apps are left as they are. |
| 88 | 88 | */ |
| 89 | 89 | export async function seatFirstAskCrew(db: Database, repo: Repo, founderId: string): Promise<boolean> { |
| 90 | if (listAgents(db).length === 0 && !(await makeStarterAgents(db, founderId))) return false; | |
| 91 | let builder = listAgents(db).find((a) => a.roles.includes("builder")); | |
| 90 | return (await seatCrew(db, repo, founderId)) !== undefined; | |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * `seatFirstAskCrew`, answering the builder seated (or undefined). `prefer` | |
| 95 | * names a builder the caller already picked; a planner is seated too where | |
| 96 | * the forge has one. | |
| 97 | */ | |
| 98 | export async function seatCrew( | |
| 99 | db: Database, | |
| 100 | repo: Repo, | |
| 101 | founderId: string, | |
| 102 | prefer?: string, | |
| 103 | ): Promise<ReturnType<typeof listAgents>[number] | undefined> { | |
| 104 | if (listAgents(db).length === 0 && !(await makeStarterAgents(db, founderId))) return undefined; | |
| 105 | let builder = | |
| 106 | listAgents(db).find((a) => a.user.username === prefer && a.roles.includes("builder")) ?? | |
| 107 | listAgents(db).find((a) => a.roles.includes("builder")); | |
| 92 | 108 | if (builder === undefined && (await makeSeat(db, STARTER_SEATS[0]!, founderId))) |
| 93 | 109 | builder = getAgentByName(db, STARTER_SEATS[0]!.name); |
| 94 | if (builder === undefined) return false; | |
| 110 | if (builder === undefined) return undefined; | |
| 95 | 111 | const other = (): ReturnType<typeof listAgents>[number] | undefined => |
| 96 | 112 | listAgents(db).find((a) => a.roles.includes("reviewer") && a.user.id !== builder!.user.id); |
| 97 | 113 | let reviewer = other(); |
| 98 | 114 | if (reviewer === undefined && (await makeSeat(db, STARTER_SEATS[1]!, founderId))) reviewer = other(); |
| 99 | if (reviewer === undefined) return false; | |
| 115 | if (reviewer === undefined) return undefined; | |
| 100 | 116 | seatAgent(db, repo, builder.user.id, founderId, "builder"); |
| 101 | 117 | seatAgent(db, repo, reviewer.user.id, founderId, "reviewer"); |
| 102 | updateRepoSettings(db, getRepoById(db, repo.id) ?? repo, { autonomy: "autonomous", autonomyBy: founderId }); | |
| 103 | return true; | |
| 118 | const planner = listAgents(db).find( | |
| 119 | (a) => a.roles.includes("planner") && a.user.id !== builder!.user.id && a.user.id !== reviewer!.user.id, | |
| 120 | ); | |
| 121 | if (planner !== undefined) seatAgent(db, repo, planner.user.id, founderId, "planner"); | |
| 122 | const fresh = getRepoById(db, repo.id) ?? repo; | |
| 123 | if (fresh.autonomy === "off") updateRepoSettings(db, fresh, { autonomy: "autonomous", autonomyBy: founderId }); | |
| 124 | return builder; | |
| 104 | 125 | } |
| 105 | 126 | |
| 106 | 127 | /** Seat the starter crew on one repository and turn autonomy on, unless a person already chose either. */ |
src/web/pages/apps.ts +3 −2
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | * 404 for everyone. |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | import { crewKeyCard } from "../studio/crew_key.ts"; | |
| 15 | import { NO_CREDIT_WORDS, crewCanRun, crewKeyCard } from "../studio/crew_key.ts"; | |
| 16 | 16 | import { storageLimitMessage } from "../../core/storage.ts"; |
| 17 | 17 | import { repositoryLimitMessage } from "../../actions/repos.ts"; |
| 18 | 18 | import { appLimitWords } from "../../actions/app_limit.ts"; |
| @@ -576,7 +576,8 @@ export async function newAppSubmit(ctx: Ctx): Promise<Response> { | ||
| 576 | 576 | : ` #${issue.number} is assigned to ${issue.assigneeName}.`; |
| 577 | 577 | // The repository's home is the app page (repoHome), once the record |
| 578 | 578 | // exists; the code is its Files step. |
| 579 | return flashTo(ctx, repoBase(repo), { message: `${first}${second}` }); | |
| 579 | const idle = crewCanRun(ctx.app) ? "" : ` ${NO_CREDIT_WORDS}`; | |
| 580 | return flashTo(ctx, repoBase(repo), { message: `${first}${second}${idle}` }); | |
| 580 | 581 | } |
| 581 | 582 | |
| 582 | 583 | // ---- the repo home's strip ---- |
test/new-app-crew.test.ts added +71
| @@ -0,0 +1,71 @@ | ||
| 1 | /** | |
| 2 | * Making an app seats the crew and assigns the first issue (#12175): a | |
| 3 | * builder and a reviewer from the forge, with no admin step; a person's own | |
| 4 | * pick is kept as chosen; a forge with nothing to run a crew on seats none. | |
| 5 | */ | |
| 6 | import { afterAll, expect, setDefaultTimeout, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { newApp } from "../src/actions/apps.ts"; | |
| 11 | import { setRole } from "../src/actions/roles.ts"; | |
| 12 | import { createApp } from "../src/app.ts"; | |
| 13 | import { createAgentRow, listCrewRoles } from "../src/core/agents.ts"; | |
| 14 | import { addSubscription } from "../src/core/subscriptions.ts"; | |
| 15 | import { createUser } from "../src/core/users.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | setDefaultTimeout(30_000); | |
| 20 | ||
| 21 | const dirs: string[] = []; | |
| 22 | afterAll(async () => { | |
| 23 | for (const d of dirs) await rm(d, { recursive: true, force: true }); | |
| 24 | }); | |
| 25 | ||
| 26 | async function world(withKey: boolean) { | |
| 27 | const dir = await mkdtemp(join(tmpdir(), "forge-newapp-crew-")); | |
| 28 | dirs.push(dir); | |
| 29 | const app = createApp({ dataDir: dir, port: 0 }); | |
| 30 | const mk = async (username: string, isAdmin = false) => { | |
| 31 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@x.test`, isAdmin }); | |
| 32 | if (!made.ok) throw new Error(made.error); | |
| 33 | return made.value; | |
| 34 | }; | |
| 35 | const admin = await mk("boss", true); | |
| 36 | const user = await mk("maker"); | |
| 37 | setRole(app, admin, user.username, "member"); | |
| 38 | if (withKey) addSubscription(app.db, dir, { name: "plan", token: "sk-ant-api-test-token" }); | |
| 39 | return { app, user }; | |
| 40 | } | |
| 41 | ||
| 42 | const seats = (app: ReturnType<typeof createApp>, repoId: string) => | |
| 43 | listCrewRoles(app.db, repoId).map((c) => `${c.agent.user.username}:${c.role}`).sort(); | |
| 44 | ||
| 45 | test("an app made with no agent named seats builder and reviewer and assigns the first issue", async () => { | |
| 46 | const { app, user } = await world(true); | |
| 47 | const made = await newApp(app, user, { name: "tides", description: "A tide table tracker", pickAgent: true }); | |
| 48 | if (!made.ok) throw new Error(made.error); | |
| 49 | expect(seats(app, made.value.repo.id)).toEqual(["builder:builder", "reviewer:reviewer"]); | |
| 50 | expect(made.value.issue?.assigneeName).toBe("builder"); | |
| 51 | expect(app.db.query("SELECT autonomy FROM repo WHERE id = ?").get(made.value.repo.id)).toEqual({ autonomy: "autonomous" }); | |
| 52 | }); | |
| 53 | ||
| 54 | test("a person's own pick gets exactly that agent and no other seat", async () => { | |
| 55 | const { app, user } = await world(true); | |
| 56 | const u = await createUser(app.db, { username: "ada", email: "[email protected]", agent: true, origin: "spawn" }); | |
| 57 | if (!u.ok) throw new Error("agent"); | |
| 58 | createAgentRow(app.db, { userId: u.value.id, roles: ["builder"] }); | |
| 59 | const made = await newApp(app, user, { name: "tides", description: "A tide table tracker", agent: "ada" }); | |
| 60 | if (!made.ok) throw new Error(made.error); | |
| 61 | expect(seats(app, made.value.repo.id)).toEqual(["ada:builder"]); | |
| 62 | expect(made.value.issue?.assigneeName).toBe("ada"); | |
| 63 | }); | |
| 64 | ||
| 65 | test("a forge with nothing to run a crew on seats none and leaves the issue unassigned", async () => { | |
| 66 | const { app, user } = await world(false); | |
| 67 | const made = await newApp(app, user, { name: "tides", description: "A tide table tracker" }); | |
| 68 | if (!made.ok) throw new Error(made.error); | |
| 69 | expect(seats(app, made.value.repo.id)).toEqual([]); | |
| 70 | expect(made.value.issue?.assigneeName).toBeNull(); | |
| 71 | }); |
new above
offline