Public: anyone can read this; the crew can change it.
Sibling pulls of one tell share a group: `variants: n` on tell and the MCP starts n builders, and each pull and run carries the group id #1467
scout/forge:scout/1409-sibling-pulls-of-one-tell-share-a-group → main 4 commits fork
13 files
changed +362
−29
scripts/gate.ts +1
| @@ -139,6 +139,7 @@ export const GATE: readonly string[] = [ | ||
| 139 | 139 | "test/forge-cloudflare.test.ts", |
| 140 | 140 | "test/forge-cloudflare-celld.test.ts", |
| 141 | 141 | "test/branch_tell.test.ts", |
| 142 | "test/variants.test.ts", | |
| 142 | 143 | "test/usage.test.ts", |
| 143 | 144 | "test/tokens.test.ts", |
| 144 | 145 | "test/credentials.test.ts", |
src/actions/pulls.ts +3
| @@ -85,6 +85,8 @@ export interface OpenPullInput { | ||
| 85 | 85 | /** The issue id of the pull this one reverts ("Take it back"). Native |
| 86 | 86 | * repos only — a mirror's pull is the origin's, which has no such field. */ |
| 87 | 87 | revertsIssueId?: string; |
| 88 | /** The group of sibling variants this pull belongs to (`actions/tell.ts`). */ | |
| 89 | variantGroup?: string; | |
| 88 | 90 | /** |
| 89 | 91 | * A person's deliberate yes to fixing a sealed report from a branch that |
| 90 | 92 | * is already public (this repository, or a public fork). Without it such |
| @@ -217,6 +219,7 @@ export async function openPull( | ||
| 217 | 219 | } |
| 218 | 220 | : {}), |
| 219 | 221 | ...(input.revertsIssueId ? { revertsIssueId: input.revertsIssueId } : {}), |
| 222 | ...(input.variantGroup ? { variantGroup: input.variantGroup } : {}), | |
| 220 | 223 | }, |
| 221 | 224 | ); |
| 222 | 225 | if (!created.ok) return created; |
src/actions/tell.ts +73 −17
| @@ -10,10 +10,11 @@ import type { App } from "../app.ts"; | ||
| 10 | 10 | import { listCrewRoles } from "../core/agents.ts"; |
| 11 | 11 | import type { Issue, TitleError } from "../core/issues.ts"; |
| 12 | 12 | import type { Repo } from "../core/repos.ts"; |
| 13 | import { listRuns } from "../core/runs.ts"; | |
| 13 | import { activeRunFor, listRuns, setRunGroup } from "../core/runs.ts"; | |
| 14 | 14 | import type { User } from "../core/users.ts"; |
| 15 | 15 | import { mayDirectCrew } from "../crew/direct.ts"; |
| 16 | 16 | import { routeIssue, type Routed } from "../crew/route.ts"; |
| 17 | import { id } from "../kernel/id.ts"; | |
| 17 | 18 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 18 | 19 | import { requireParticipant } from "./access.ts"; |
| 19 | 20 | import { assignIssue, openIssue, type MirrorError } from "./issues.ts"; |
| @@ -25,10 +26,36 @@ export interface Told { | ||
| 25 | 26 | readonly routed: Routed | undefined; |
| 26 | 27 | /** Whether the builder could be assigned (and so a run will start). */ |
| 27 | 28 | readonly assigned: boolean; |
| 29 | /** The group the sibling variants share; null for a plain tell. */ | |
| 30 | readonly variantGroup: string | null; | |
| 31 | /** Every issue opened, the first being `issue`; one for a plain tell. */ | |
| 32 | readonly issues: readonly Issue[]; | |
| 33 | } | |
| 34 | ||
| 35 | /** Most builders one tell may start. */ | |
| 36 | export const VARIANTS_MAX = 4; | |
| 37 | ||
| 38 | /** What every door says when `variants` is refused. */ | |
| 39 | export const VARIANTS_WORDS = `variants is a whole number from 1 to ${VARIANTS_MAX}`; | |
| 40 | ||
| 41 | /** | |
| 42 | * `variants` as a door received it (a number, a form's string, or nothing): | |
| 43 | * a whole number 1–4, else undefined. Nothing at all is one. | |
| 44 | */ | |
| 45 | export function parseVariants(raw: unknown): number | undefined { | |
| 46 | if (raw === undefined || raw === null || raw === "") return 1; | |
| 47 | const n = | |
| 48 | typeof raw === "number" | |
| 49 | ? raw | |
| 50 | : typeof raw === "string" && /^[0-9]{1,3}$/.test(raw.trim()) | |
| 51 | ? Number(raw.trim()) | |
| 52 | : NaN; | |
| 53 | return Number.isInteger(n) && n >= 1 && n <= VARIANTS_MAX ? n : undefined; | |
| 28 | 54 | } |
| 29 | 55 | |
| 30 | 56 | export type TellError = |
| 31 | 57 | | "empty" |
| 58 | | "invalid_variants" | |
| 32 | 59 | | "not_found" |
| 33 | 60 | | "unauthorized" |
| 34 | 61 | /** Someone who may not direct the crew here (`crew/direct.ts`). */ |
| @@ -44,7 +71,10 @@ export async function tell( | ||
| 44 | 71 | actor: User | undefined, |
| 45 | 72 | repo: Repo, |
| 46 | 73 | said: string, |
| 74 | variants: number = 1, | |
| 47 | 75 | ): Promise<Result<Told, TellError>> { |
| 76 | if (!Number.isInteger(variants) || variants < 1 || variants > VARIANTS_MAX) | |
| 77 | return err("invalid_variants"); | |
| 48 | 78 | // A tell is a run: only someone who may direct the crew here says one, |
| 49 | 79 | // and a refused tell opens nothing (`crew/direct.ts`). |
| 50 | 80 | const participant = requireParticipant(app, actor, repo); |
| @@ -58,29 +88,53 @@ export async function tell( | ||
| 58 | 88 | const firstLine = text.split(/\n/)[0]!.trim(); |
| 59 | 89 | const title = |
| 60 | 90 | firstLine.length <= 80 ? firstLine : `${firstLine.slice(0, 77).trimEnd()}…`; |
| 61 | const opened = openIssue(app, actor, repo, { | |
| 62 | title, | |
| 63 | body: text === firstLine ? "" : text, | |
| 64 | }); | |
| 65 | if (!opened.ok) return opened; | |
| 91 | const group = variants > 1 ? id() : null; | |
| 66 | 92 | const crew = listCrewRoles(app.db, repo.id); |
| 67 | 93 | const inFlight = new Map<string, number>(); |
| 68 | 94 | for (const r of listRuns(app.db, repo.id, { kind: "agent", limit: 50 })) |
| 69 | 95 | if ((r.state === "running" || r.state === "queued") && r.actorId !== null) |
| 70 | 96 | inFlight.set(r.actorId, (inFlight.get(r.actorId) ?? 0) + 1); |
| 71 | const routed = await routeIssue(app, repo, opened.value, crew, inFlight); | |
| 72 | if (routed === undefined) | |
| 73 | return ok({ issue: opened.value, routed: undefined, assigned: false }); | |
| 74 | const assigned = assignIssue( | |
| 75 | app, | |
| 76 | actor, | |
| 77 | repo, | |
| 78 | opened.value, | |
| 79 | routed.builder.user, | |
| 80 | ); | |
| 81 | return ok({ issue: opened.value, routed, assigned: assigned.ok }); | |
| 97 | const told: Told[] = []; | |
| 98 | for (let i = 0; i < variants; i++) { | |
| 99 | const opened = openIssue(app, actor, repo, { | |
| 100 | title, | |
| 101 | body: text === firstLine ? "" : text, | |
| 102 | }); | |
| 103 | if (!opened.ok) return told.length === 0 ? opened : ok(whole(told, group)); | |
| 104 | const routed = await routeIssue(app, repo, opened.value, crew, inFlight); | |
| 105 | if (routed === undefined) { | |
| 106 | told.push({ ...bare(opened.value, group), routed: undefined }); | |
| 107 | break; | |
| 108 | } | |
| 109 | const builderId = routed.builder.user.id; | |
| 110 | // The next variant sees this builder as busier, so the n spread. | |
| 111 | inFlight.set(builderId, (inFlight.get(builderId) ?? 0) + 1); | |
| 112 | const assigned = assignIssue(app, actor, repo, opened.value, routed.builder.user); | |
| 113 | // Assigning queued the run; the group is stamped on it now. | |
| 114 | if (group !== null && assigned.ok) { | |
| 115 | const run = activeRunFor(app.db, "agent", opened.value.id, builderId); | |
| 116 | if (run !== undefined) setRunGroup(app.db, run.id, group); | |
| 117 | } | |
| 118 | told.push({ ...bare(opened.value, group), routed, assigned: assigned.ok }); | |
| 119 | } | |
| 120 | return ok(whole(told, group)); | |
| 82 | 121 | } |
| 83 | 122 | |
| 123 | const bare = (issue: Issue, variantGroup: string | null): Told => ({ | |
| 124 | issue, | |
| 125 | routed: undefined, | |
| 126 | assigned: false, | |
| 127 | variantGroup, | |
| 128 | issues: [issue], | |
| 129 | }); | |
| 130 | ||
| 131 | /** The first variant's answer, carrying every issue opened. */ | |
| 132 | const whole = (told: readonly Told[], group: string | null): Told => ({ | |
| 133 | ...told[0]!, | |
| 134 | variantGroup: group, | |
| 135 | issues: told.map((t) => t.issue), | |
| 136 | }); | |
| 137 | ||
| 84 | 138 | /** What every door says when a tell is refused for an unverified address. */ |
| 85 | 139 | export const UNVERIFIED_WORDS = "verify your email first"; |
| 86 | 140 | |
| @@ -90,6 +144,8 @@ export function toldWords(told: Told): string { | ||
| 90 | 144 | return `Told as #${told.issue.number}. Nobody is on the crew to take it yet — add someone under Settings → Crew.`; |
| 91 | 145 | const r = told.routed; |
| 92 | 146 | const who = r.builder.user.username; |
| 147 | if (told.issues.length > 1) | |
| 148 | return `${told.issues.length} variants told (${told.issues.map((i) => `#${i.number}`).join(", ")}); each builder opens a pull of its own.`; | |
| 93 | 149 | return told.assigned |
| 94 | 150 | ? `${who} is on it — ${r.size} ${r.kind}${r.by === "rule" ? "" : `, ${r.by} decided in ${r.ms} ms`}.` |
| 95 | 151 | : `Told as #${told.issue.number}, but ${who} could not take it.`; |
src/core/pulls.ts +26 −2
| @@ -56,6 +56,8 @@ export interface Pull extends Issue { | ||
| 56 | 56 | readonly originHeadRepo: string | null; |
| 57 | 57 | /** The pull this one reverts, when it was opened by "Take it back". */ |
| 58 | 58 | readonly revertsPullId: string | null; |
| 59 | /** The group of sibling variants of one tell; null for an ordinary pull. */ | |
| 60 | readonly variantGroup: string | null; | |
| 59 | 61 | } |
| 60 | 62 | |
| 61 | 63 | interface PullRow { |
| @@ -68,6 +70,7 @@ interface PullRow { | ||
| 68 | 70 | head_repo_id: string | null; |
| 69 | 71 | origin_head_repo: string | null; |
| 70 | 72 | reverts_issue_id: string | null; |
| 73 | variant_group: string | null; | |
| 71 | 74 | } |
| 72 | 75 | |
| 73 | 76 | function fromRow(issue: Issue, row: PullRow): Pull { |
| @@ -84,6 +87,7 @@ function fromRow(issue: Issue, row: PullRow): Pull { | ||
| 84 | 87 | crossRepo: headRepoId !== issue.repoId, |
| 85 | 88 | originHeadRepo: row.origin_head_repo, |
| 86 | 89 | revertsPullId: row.reverts_issue_id, |
| 90 | variantGroup: row.variant_group, | |
| 87 | 91 | }; |
| 88 | 92 | } |
| 89 | 93 | |
| @@ -119,6 +123,8 @@ export interface CreatePullInput { | ||
| 119 | 123 | headDir?: string; |
| 120 | 124 | /** The issue id of the pull this one reverts ("Take it back"). */ |
| 121 | 125 | revertsIssueId?: string; |
| 126 | /** The group of sibling variants this pull belongs to (`actions/tell.ts`). */ | |
| 127 | variantGroup?: string; | |
| 122 | 128 | } |
| 123 | 129 | |
| 124 | 130 | /** |
| @@ -233,8 +239,8 @@ export async function createPull( | ||
| 233 | 239 | |
| 234 | 240 | const headRepoId = input.headRepoId ?? input.repoId; |
| 235 | 241 | db.query( |
| 236 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, reverts_issue_id) | |
| 237 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)`, | |
| 242 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, reverts_issue_id, variant_group) | |
| 243 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)`, | |
| 238 | 244 | ).run( |
| 239 | 245 | issue.value.id, |
| 240 | 246 | input.baseBranch, |
| @@ -243,6 +249,7 @@ export async function createPull( | ||
| 243 | 249 | headOid, |
| 244 | 250 | headRepoId, |
| 245 | 251 | input.revertsIssueId ?? null, |
| 252 | input.variantGroup ?? null, | |
| 246 | 253 | ); |
| 247 | 254 | |
| 248 | 255 | // A durable, fetchable ref for the PR head — the way tooling addresses it, |
| @@ -252,6 +259,23 @@ export async function createPull( | ||
| 252 | 259 | return ok(join(db, issue.value)!); |
| 253 | 260 | } |
| 254 | 261 | |
| 262 | /** The numbers of the other pulls in a variant group, oldest first. */ | |
| 263 | export function siblingNumbers( | |
| 264 | db: Database, | |
| 265 | repoId: string, | |
| 266 | pull: Pick<Pull, "id" | "variantGroup">, | |
| 267 | ): number[] { | |
| 268 | if (pull.variantGroup === null) return []; | |
| 269 | return db | |
| 270 | .query<{ number: number }, [string, string, string]>( | |
| 271 | `SELECT i.number FROM pull p JOIN issue i ON i.id = p.issue_id | |
| 272 | WHERE p.variant_group = ?1 AND i.repo_id = ?2 AND i.id != ?3 | |
| 273 | ORDER BY i.number`, | |
| 274 | ) | |
| 275 | .all(pull.variantGroup, repoId, pull.id) | |
| 276 | .map((r) => r.number); | |
| 277 | } | |
| 278 | ||
| 255 | 279 | export function getPull( |
| 256 | 280 | db: Database, |
| 257 | 281 | repoId: string, |
src/core/runs.ts +9
| @@ -82,9 +82,12 @@ export interface Run { | ||
| 82 | 82 | /** When this deploy's environment started serving — set once, at the swap-in. */ |
| 83 | 83 | readonly servingAt: string | null; |
| 84 | 84 | readonly finishedAt: string | null; |
| 85 | /** The group of sibling variants this run belongs to; null for an ordinary run. */ | |
| 86 | readonly variantGroup: string | null; | |
| 85 | 87 | } |
| 86 | 88 | |
| 87 | 89 | interface RunRow { |
| 90 | variant_group: string | null; | |
| 88 | 91 | id: string; |
| 89 | 92 | repo_id: string; |
| 90 | 93 | kind: RunKind; |
| @@ -137,6 +140,7 @@ const toRun = (r: RunRow): Run => ({ | ||
| 137 | 140 | startedAt: r.started_at, |
| 138 | 141 | servingAt: r.serving_at, |
| 139 | 142 | finishedAt: r.finished_at, |
| 143 | variantGroup: r.variant_group, | |
| 140 | 144 | }); |
| 141 | 145 | |
| 142 | 146 | export interface CreateRunInput { |
| @@ -178,6 +182,11 @@ export function createRun(db: Database, input: CreateRunInput): Run { | ||
| 178 | 182 | return getRun(db, runId)!; |
| 179 | 183 | } |
| 180 | 184 | |
| 185 | /** Put a run in a group of sibling variants (`actions/tell.ts`). */ | |
| 186 | export function setRunGroup(db: Database, runId: string, group: string): void { | |
| 187 | db.query("UPDATE run SET variant_group = ?2 WHERE id = ?1").run(runId, group); | |
| 188 | } | |
| 189 | ||
| 181 | 190 | export function getRun(db: Database, runId: string): Run | undefined { |
| 182 | 191 | const row = db |
| 183 | 192 | .query<RunRow, [string]>("SELECT * FROM run WHERE id = ?1") |
src/core/schema.ts +8
| @@ -1584,6 +1584,14 @@ export const migrations: readonly string[] = [ | ||
| 1584 | 1584 | ALTER TABLE look ADD COLUMN attached_by TEXT; |
| 1585 | 1585 | ALTER TABLE look ADD COLUMN note TEXT; |
| 1586 | 1586 | `, |
| 1587 | // variant_group — the id the sibling pulls of one tell share (#1409). Null | |
| 1588 | // for an ordinary run or pull. | |
| 1589 | ` | |
| 1590 | ALTER TABLE run ADD COLUMN variant_group TEXT; | |
| 1591 | ALTER TABLE pull ADD COLUMN variant_group TEXT; | |
| 1592 | CREATE INDEX run_variant_group ON run(variant_group) WHERE variant_group IS NOT NULL; | |
| 1593 | CREATE INDEX pull_variant_group ON pull(variant_group) WHERE variant_group IS NOT NULL; | |
| 1594 | `, | |
| 1587 | 1595 | // 75 — a limb's place among its siblings (#1064), set by a person. `parent_id` |
| 1588 | 1596 | // is the issue whose limbs these are, or '' for the top of the plan. |
| 1589 | 1597 | ` |
src/crew/runner.ts +3
| @@ -1187,6 +1187,7 @@ async function work( | ||
| 1187 | 1187 | : `Budget reached at $${stoppedAt.toFixed(2)}; this is where it stopped.`, |
| 1188 | 1188 | write, |
| 1189 | 1189 | Object.values(grant.env), |
| 1190 | run.variantGroup, | |
| 1190 | 1191 | ); |
| 1191 | 1192 | return { |
| 1192 | 1193 | state: result.ok || landed.pushed ? "ok" : "failed", |
| @@ -1420,6 +1421,7 @@ async function landWork( | ||
| 1420 | 1421 | note: string | undefined, |
| 1421 | 1422 | write: (line: string) => void, |
| 1422 | 1423 | granted: readonly string[] = [], |
| 1424 | variantGroup: string | null = null, | |
| 1423 | 1425 | ): Promise<{ pushed: boolean; summary: string }> { |
| 1424 | 1426 | // A model may echo a granted value into a file; the tree is scrubbed |
| 1425 | 1427 | // before the forge commits it, and the agent's own commits are searched |
| @@ -1564,6 +1566,7 @@ async function landWork( | ||
| 1564 | 1566 | baseBranch: repo.defaultBranch, |
| 1565 | 1567 | headBranch: branch, |
| 1566 | 1568 | headRepo: fork, |
| 1569 | ...(variantGroup === null ? {} : { variantGroup }), | |
| 1567 | 1570 | }); |
| 1568 | 1571 | if (!opened.ok) { |
| 1569 | 1572 | write(`could not open the pull: ${JSON.stringify(opened.error)}`); |
src/http/api.ts +3 −1
| @@ -158,6 +158,7 @@ import { | ||
| 158 | 158 | import { |
| 159 | 159 | getPull, |
| 160 | 160 | listPulls, |
| 161 | siblingNumbers, | |
| 161 | 162 | pullDiff, |
| 162 | 163 | pullDir, |
| 163 | 164 | type Pull, |
| @@ -2723,7 +2724,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2723 | 2724 | ...query, |
| 2724 | 2725 | ...sealedScope(ctx.app.db, repo, ctx.auth.user), |
| 2725 | 2726 | }), |
| 2726 | ); | |
| 2727 | ).map((p) => ({ ...p, siblings: siblingNumbers(ctx.app.db, repo.id, p) })); | |
| 2727 | 2728 | return json(paged(items, query.limit, (p) => p.number)); |
| 2728 | 2729 | }); |
| 2729 | 2730 | |
| @@ -2785,6 +2786,7 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2785 | 2786 | const hold = getHold(ctx.app.db, found.pull.id); |
| 2786 | 2787 | return json({ |
| 2787 | 2788 | ...withLabels(ctx, found.pull), |
| 2789 | siblings: siblingNumbers(ctx.app.db, found.repo.id, found.pull), | |
| 2788 | 2790 | status, |
| 2789 | 2791 | diff, |
| 2790 | 2792 | hold: hold === undefined ? null : holdJson(hold), |
src/http/feed.ts +22 −3
| @@ -3,7 +3,12 @@ | ||
| 3 | 3 | * are the signed-in viewer's own — there is no way to ask for another's. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | import { tell, UNVERIFIED_WORDS } from "../actions/tell.ts"; | |
| 6 | import { | |
| 7 | parseVariants, | |
| 8 | tell, | |
| 9 | UNVERIFIED_WORDS, | |
| 10 | VARIANTS_WORDS, | |
| 11 | } from "../actions/tell.ts"; | |
| 7 | 12 | import { DIRECTION_NOTE } from "../crew/direct.ts"; |
| 8 | 13 | import { audienceFilter } from "../core/event_audience.ts"; |
| 9 | 14 | import { getEvent } from "../core/events.ts"; |
| @@ -154,20 +159,34 @@ export function feedRoutes(router: Router<Handler>): void { | ||
| 154 | 159 | const text = str(input.text).trim(); |
| 155 | 160 | if (text === "" || text.length > 2000) |
| 156 | 161 | return apiError("invalid_text", 422, "text is 1 to 2000 characters"); |
| 162 | const variants = parseVariants(input.variants); | |
| 163 | if (variants === undefined) | |
| 164 | return apiError("invalid_variants", 422, VARIANTS_WORDS); | |
| 157 | 165 | const [owner, name, extra] = str(input.app).split("/"); |
| 158 | 166 | const repo = |
| 159 | 167 | owner && name && extra === undefined |
| 160 | 168 | ? getRepo(ctx.app.db, owner, name) |
| 161 | 169 | : undefined; |
| 162 | 170 | if (!repo || !canRead(ctx.app.db, repo, viewer)) return notFound(); |
| 163 | const told = await tell(ctx.app, viewer, repo, text); | |
| 171 | const told = await tell(ctx.app, viewer, repo, text, variants); | |
| 164 | 172 | if (!told.ok) |
| 165 | 173 | return told.error === "forbidden" |
| 166 | 174 | ? apiError("forbidden", 403, DIRECTION_NOTE) |
| 167 | 175 | : told.error === "unverified" |
| 168 | 176 | ? apiError("unverified", 403, UNVERIFIED_WORDS) |
| 169 | 177 | : apiError("refused", told.error === "unauthorized" ? 403 : 422, "that could not be told"); |
| 170 | return json({ issue: told.value.issue.number }, 201); | |
| 178 | return json( | |
| 179 | { | |
| 180 | issue: told.value.issue.number, | |
| 181 | ...(told.value.variantGroup === null | |
| 182 | ? {} | |
| 183 | : { | |
| 184 | variantGroup: told.value.variantGroup, | |
| 185 | issues: told.value.issues.map((i) => i.number), | |
| 186 | }), | |
| 187 | }, | |
| 188 | 201, | |
| 189 | ); | |
| 171 | 190 | }); |
| 172 | 191 | |
| 173 | 192 | router.add("POST", "/api/v1/share", async (ctx) => { |
src/http/mcp.ts +44 −4
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | * no page. `ro` tokens read; `rw` tokens act. |
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | import type { Database } from "bun:sqlite"; | |
| 20 | 21 | import { mayView, mayViewId, sealedScope } from "../core/sealed.ts"; |
| 21 | 22 | import { changeCapacity, listCapacity } from "../actions/capacity.ts"; |
| 22 | 23 | import { relationOf, takeUpdates } from "../actions/forks.ts"; |
| @@ -37,7 +38,14 @@ import { openPlan, parsePlan, PLAN_MAX } from "../actions/plan.ts"; | ||
| 37 | 38 | import { cancelRepoRun } from "../actions/runs.ts"; |
| 38 | 39 | import { clientRows } from "../spawn/clients.ts"; |
| 39 | 40 | import { reissueDoor, spawnDoor } from "../spawn/door.ts"; |
| 40 | import { tell, toldWords, UNVERIFIED_WORDS } from "../actions/tell.ts"; | |
| 41 | import { | |
| 42 | parseVariants, | |
| 43 | tell, | |
| 44 | toldWords, | |
| 45 | UNVERIFIED_WORDS, | |
| 46 | VARIANTS_MAX, | |
| 47 | VARIANTS_WORDS, | |
| 48 | } from "../actions/tell.ts"; | |
| 41 | 49 | import { readReturns, reportSpend } from "../actions/returns.ts"; |
| 42 | 50 | import { |
| 43 | 51 | readUsage, |
| @@ -59,7 +67,7 @@ import { getIssue, getIssueById, listOpenAgentPullsVisibleTo } from "../core/iss | ||
| 59 | 67 | import { waitsForPerson } from "../crew/review.ts"; |
| 60 | 68 | import { attachLook } from "../actions/attach_look.ts"; |
| 61 | 69 | import { lookUrl } from "../core/looks.ts"; |
| 62 | import { getPull } from "../core/pulls.ts"; | |
| 70 | import { getPull, siblingNumbers } from "../core/pulls.ts"; | |
| 63 | 71 | import { repoGraph } from "../core/dependencies.ts"; |
| 64 | 72 | import { pillarView } from "../core/pillars.ts"; |
| 65 | 73 | import { askToLiveFor } from "../web/ask_to_live.ts"; |
| @@ -128,6 +136,18 @@ import { nextStepsFor } from "../core/next-steps.ts"; | ||
| 128 | 136 | |
| 129 | 137 | const PROTOCOLS = ["2025-06-18", "2025-03-26", "2024-11-05"] as const; |
| 130 | 138 | /** Who answers: the forge, at the version its build stamped. */ |
| 139 | /** A pull's variant group and the other pulls in it; nothing for an ordinary pull. */ | |
| 140 | const variantOf = ( | |
| 141 | ctx: { app: { db: Database } }, | |
| 142 | pull: Parameters<typeof siblingNumbers>[2] & { repoId: string }, | |
| 143 | ) => | |
| 144 | pull.variantGroup === null | |
| 145 | ? {} | |
| 146 | : { | |
| 147 | variantGroup: pull.variantGroup, | |
| 148 | siblings: siblingNumbers(ctx.app.db, pull.repoId, pull), | |
| 149 | }; | |
| 150 | ||
| 131 | 151 | const server = () => ({ name: "forge", version: running().version }); |
| 132 | 152 | |
| 133 | 153 | type Args = Record<string, unknown>; |
| @@ -286,14 +306,26 @@ const TOOLS: readonly Tool[] = [ | ||
| 286 | 306 | "Tell an app's crew what should change. One sentence or several; the first line becomes the title. A builder is picked and starts on the crew's next tick.", |
| 287 | 307 | inputSchema: { |
| 288 | 308 | type: "object", |
| 289 | properties: { repo: REPO_ARG, text: { type: "string", maxLength: 8000 } }, | |
| 309 | properties: { | |
| 310 | repo: REPO_ARG, | |
| 311 | text: { type: "string", maxLength: 8000 }, | |
| 312 | variants: { | |
| 313 | type: "integer", | |
| 314 | minimum: 1, | |
| 315 | maximum: VARIANTS_MAX, | |
| 316 | description: | |
| 317 | "How many builders try it, each in a pull of its own; the pulls share a variantGroup. Default 1.", | |
| 318 | }, | |
| 319 | }, | |
| 290 | 320 | required: ["repo", "text"], |
| 291 | 321 | }, |
| 292 | 322 | writes: true, |
| 293 | 323 | async run(ctx, user, args) { |
| 294 | 324 | const repo = repoArg(ctx, user, args); |
| 295 | 325 | if (isAnswer(repo)) return repo; |
| 296 | const told = await tell(ctx.app, user, repo, str(args["text"])); | |
| 326 | const variants = parseVariants(args["variants"]); | |
| 327 | if (variants === undefined) return bad("invalid_variants", VARIANTS_WORDS); | |
| 328 | const told = await tell(ctx.app, user, repo, str(args["text"]), variants); | |
| 297 | 329 | if (!told.ok) |
| 298 | 330 | return bad( |
| 299 | 331 | told.error, |
| @@ -308,6 +340,12 @@ const TOOLS: readonly Tool[] = [ | ||
| 308 | 340 | return good({ |
| 309 | 341 | says: toldWords(told.value), |
| 310 | 342 | issue: told.value.issue.number, |
| 343 | ...(told.value.variantGroup === null | |
| 344 | ? {} | |
| 345 | : { | |
| 346 | variantGroup: told.value.variantGroup, | |
| 347 | issues: told.value.issues.map((i) => i.number), | |
| 348 | }), | |
| 311 | 349 | page: `${ctx.url.origin}/${repo.ownerName}/${repo.name}/issues/${told.value.issue.number}`, |
| 312 | 350 | }); |
| 313 | 351 | }, |
| @@ -549,6 +587,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 549 | 587 | says: wait.why === "lapsed" ? "the reviewer did not answer" : "no reviewer on the crew", |
| 550 | 588 | since: wait.since, |
| 551 | 589 | costUsd: 0, |
| 590 | ...(pull ? variantOf(ctx, pull) : {}), | |
| 552 | 591 | }, |
| 553 | 592 | ]; |
| 554 | 593 | }); |
| @@ -571,6 +610,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 571 | 610 | since: r.startedAt ?? r.createdAt, |
| 572 | 611 | // What a run costs is the owner's fact (`canSeeOwnerFacts`). |
| 573 | 612 | costUsd: owned.has(r.repoId) ? r.costUsd : 0, |
| 613 | ...(r.variantGroup === null ? {} : { variantGroup: r.variantGroup }), | |
| 574 | 614 | }; |
| 575 | 615 | }), |
| 576 | 616 | ]); |
src/web/studio/actions.ts +12 −2
| @@ -11,7 +11,12 @@ | ||
| 11 | 11 | import { mayView } from "../../core/sealed.ts"; |
| 12 | 12 | import { holdPull, releasePull } from "../../actions/holds.ts"; |
| 13 | 13 | import { addComment } from "../../actions/issues.ts"; |
| 14 | import { tell } from "../../actions/tell.ts"; | |
| 14 | import { | |
| 15 | parseVariants, | |
| 16 | tell, | |
| 17 | toldWords, | |
| 18 | VARIANTS_WORDS, | |
| 19 | } from "../../actions/tell.ts"; | |
| 15 | 20 | import { DIRECTION_NOTE } from "../../crew/direct.ts"; |
| 16 | 21 | import { mergePull } from "../../actions/pulls.ts"; |
| 17 | 22 | import { revertPull, type RevertPullError } from "../../actions/revert.ts"; |
| @@ -52,7 +57,10 @@ async function tellFrom( | ||
| 52 | 57 | tone: "danger", |
| 53 | 58 | message: "Say something first.", |
| 54 | 59 | }); |
| 55 | const told = await tell(ctx.app, ctx.auth.user, repo, text); | |
| 60 | const variants = parseVariants(field(form, "variants")); | |
| 61 | if (variants === undefined) | |
| 62 | return flashTo(ctx, back, { tone: "danger", message: VARIANTS_WORDS }); | |
| 63 | const told = await tell(ctx.app, ctx.auth.user, repo, text, variants); | |
| 56 | 64 | if (!told.ok) |
| 57 | 65 | return flashTo(ctx, back, { |
| 58 | 66 | tone: "danger", |
| @@ -68,6 +76,8 @@ async function tellFrom( | ||
| 68 | 76 | return flashTo(ctx, back, { |
| 69 | 77 | message: `Told. Nobody is on the crew to take it yet — add someone under Settings → Crew.`, |
| 70 | 78 | }); |
| 79 | if (told.value.issues.length > 1) | |
| 80 | return flashTo(ctx, back, { message: toldWords(told.value) }); | |
| 71 | 81 | const builder = routed.builder; |
| 72 | 82 | return flashTo(ctx, back, { |
| 73 | 83 | message: `${builder.user.username} is on it — ${routed.size} ${routed.kind}${routed.by === "rule" ? "" : `, ${routed.by} decided in ${routed.ms} ms`}. Watch it work below.`, |
src/web/studio/app.ts +6
| @@ -299,6 +299,12 @@ export async function studioApp( | ||
| 299 | 299 | ${builders.length === 1 ? "takes it" : "share the work"}`} |
| 300 | 300 | </span> |
| 301 | 301 | <span class="spacer"></span> |
| 302 | <select class="input" name="variants" aria-label="How many builders try it"> | |
| 303 | <option value="1">1 try</option> | |
| 304 | <option value="2">2 tries</option> | |
| 305 | <option value="3">3 tries</option> | |
| 306 | <option value="4">4 tries</option> | |
| 307 | </select> | |
| 302 | 308 | ${btn("Tell the crew", { variant: "primary", type: "submit" })} |
| 303 | 309 | </div> |
| 304 | 310 | </form>`; |
test/variants.test.ts added +152
| @@ -0,0 +1,152 @@ | ||
| 1 | /** | |
| 2 | * Sibling pulls of one tell (#1409): `variants: n` starts n builders whose | |
| 3 | * runs and pulls share one group id; a plain tell has none. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 11 | import { openPull } from "../src/actions/pulls.ts"; | |
| 12 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { parseVariants, tell } from "../src/actions/tell.ts"; | |
| 14 | import { getPull, siblingNumbers } from "../src/core/pulls.ts"; | |
| 15 | import type { Repo } from "../src/core/repos.ts"; | |
| 16 | import { listRuns } from "../src/core/runs.ts"; | |
| 17 | import { createUser, type User } from "../src/core/users.ts"; | |
| 18 | import { startCrew, type CrewHandle } from "../src/crew/dispatch.ts"; | |
| 19 | ||
| 20 | let dataDir: string; | |
| 21 | let app: App; | |
| 22 | let crew: CrewHandle; | |
| 23 | let alice: User; | |
| 24 | let repo: Repo; | |
| 25 | ||
| 26 | const runsOf = (numbers: readonly number[]) => | |
| 27 | listRuns(app.db, repo.id, { kind: "agent" }).filter((r) => | |
| 28 | numbers.some((n) => r.issueId !== null && issueNumber(r.issueId) === n), | |
| 29 | ); | |
| 30 | const issueNumber = (issueId: string): number | undefined => | |
| 31 | app.db | |
| 32 | .query<{ number: number }, [string]>("SELECT number FROM issue WHERE id = ?1") | |
| 33 | .get(issueId)?.number; | |
| 34 | ||
| 35 | beforeAll(async () => { | |
| 36 | dataDir = await mkdtemp(join(tmpdir(), "forge-variants-")); | |
| 37 | app = createApp({ dataDir, port: 0 }); | |
| 38 | // No usable agent binary: runs are queued (and fail) but nothing is spent. | |
| 39 | crew = await startCrew(app, { | |
| 40 | forgeUrl: "http://localhost:0", | |
| 41 | localUrl: "http://localhost:0", | |
| 42 | binary: join(dataDir, "no-such-claude"), | |
| 43 | concurrency: 1, | |
| 44 | budgetUsd: 1, | |
| 45 | runTimeoutMs: 60_000, | |
| 46 | }); | |
| 47 | const a = await createUser(app.db, { | |
| 48 | trusted: true, | |
| 49 | username: "alice", | |
| 50 | email: "[email protected]", | |
| 51 | password: "a-good-passphrase", | |
| 52 | adminIfFirst: true, | |
| 53 | }); | |
| 54 | if (!a.ok) throw new Error(a.error); | |
| 55 | alice = a.value; | |
| 56 | for (const username of ["bot", "bot2"]) { | |
| 57 | const b = await createAgent(app, alice, { username, brief: "Be brief." }); | |
| 58 | if (!b.ok) throw new Error(String(b.error)); | |
| 59 | } | |
| 60 | const created = await createRepo(app, alice, { name: "square", visibility: "public" }); | |
| 61 | if (!created.ok) throw new Error(String(created.error)); | |
| 62 | repo = created.value; | |
| 63 | const seeded = await commitToBranch(app, alice, repo, { | |
| 64 | message: "Start", | |
| 65 | files: [{ path: "README.md", content: "# square\n" }], | |
| 66 | }); | |
| 67 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 68 | expect(addToCrew(app, alice, repo, "bot").ok).toBe(true); | |
| 69 | expect(addToCrew(app, alice, repo, "bot2").ok).toBe(true); | |
| 70 | }, 30_000); | |
| 71 | ||
| 72 | afterAll(async () => { | |
| 73 | await crew?.stop(); | |
| 74 | app?.close(); | |
| 75 | await rm(dataDir, { recursive: true, force: true }); | |
| 76 | }); | |
| 77 | ||
| 78 | describe("variants on a tell", () => { | |
| 79 | test("variants: 2 makes two runs in one group", async () => { | |
| 80 | const told = await tell(app, alice, repo, "Make it purple", 2); | |
| 81 | expect(told.ok).toBe(true); | |
| 82 | if (!told.ok) return; | |
| 83 | const group = told.value.variantGroup; | |
| 84 | expect(group).not.toBeNull(); | |
| 85 | expect(told.value.issues).toHaveLength(2); | |
| 86 | const runs = runsOf(told.value.issues.map((i) => i.number)); | |
| 87 | expect(runs).toHaveLength(2); | |
| 88 | expect(new Set(runs.map((r) => r.variantGroup))).toEqual(new Set([group])); | |
| 89 | expect(new Set(runs.map((r) => r.issueId)).size).toBe(2); | |
| 90 | }); | |
| 91 | ||
| 92 | test("a plain tell has no group", async () => { | |
| 93 | for (const told of [ | |
| 94 | await tell(app, alice, repo, "Make it green"), | |
| 95 | await tell(app, alice, repo, "Make it blue", 1), | |
| 96 | ]) { | |
| 97 | expect(told.ok).toBe(true); | |
| 98 | if (!told.ok) return; | |
| 99 | expect(told.value.variantGroup).toBeNull(); | |
| 100 | expect(told.value.issues).toHaveLength(1); | |
| 101 | const runs = runsOf([told.value.issue.number]); | |
| 102 | expect(runs).toHaveLength(1); | |
| 103 | expect(runs[0]!.variantGroup).toBeNull(); | |
| 104 | } | |
| 105 | }); | |
| 106 | ||
| 107 | test("0 and 9 are refused, and open nothing", async () => { | |
| 108 | const before = listRuns(app.db, repo.id, { kind: "agent" }).length; | |
| 109 | for (const n of [0, 9, 1.5, -1]) { | |
| 110 | const told = await tell(app, alice, repo, "Nope", n); | |
| 111 | expect(told.ok).toBe(false); | |
| 112 | if (!told.ok) expect(told.error).toBe("invalid_variants"); | |
| 113 | } | |
| 114 | expect(listRuns(app.db, repo.id, { kind: "agent" }).length).toBe(before); | |
| 115 | }); | |
| 116 | ||
| 117 | test("the boundary reads numbers and form strings, nothing else", () => { | |
| 118 | expect(parseVariants(undefined)).toBe(1); | |
| 119 | expect(parseVariants("")).toBe(1); | |
| 120 | expect(parseVariants("3")).toBe(3); | |
| 121 | expect(parseVariants(4)).toBe(4); | |
| 122 | for (const bad of [0, 5, 9, "0", "9", "2.5", "abc", "1e1", true, [2], {}]) | |
| 123 | expect(parseVariants(bad)).toBeUndefined(); | |
| 124 | }); | |
| 125 | ||
| 126 | test("pulls opened with a group name their siblings", async () => { | |
| 127 | const group = "grp-1"; | |
| 128 | const numbers: number[] = []; | |
| 129 | for (const branch of ["bot/1-a", "bot2/2-a", "bot/3-other"]) { | |
| 130 | const c = await commitToBranch(app, alice, repo, { | |
| 131 | message: branch, | |
| 132 | branch, | |
| 133 | files: [{ path: `${branch.replace("/", "-")}.txt`, content: branch }], | |
| 134 | }); | |
| 135 | expect(c.ok).toBe(true); | |
| 136 | const pull = await openPull(app, alice, repo, { | |
| 137 | title: branch, | |
| 138 | baseBranch: repo.defaultBranch, | |
| 139 | headBranch: branch, | |
| 140 | ...(branch === "bot/3-other" ? {} : { variantGroup: group }), | |
| 141 | }); | |
| 142 | expect(pull.ok).toBe(true); | |
| 143 | if (pull.ok) numbers.push(pull.value.number); | |
| 144 | } | |
| 145 | const [a, b, other] = numbers.map((n) => getPull(app.db, repo.id, n)!); | |
| 146 | expect(a!.variantGroup).toBe(group); | |
| 147 | expect(siblingNumbers(app.db, repo.id, a!)).toEqual([b!.number]); | |
| 148 | expect(siblingNumbers(app.db, repo.id, b!)).toEqual([a!.number]); | |
| 149 | expect(other!.variantGroup).toBeNull(); | |
| 150 | expect(siblingNumbers(app.db, repo.id, other!)).toEqual([]); | |
| 151 | }); | |
| 152 | }); |
new above
offline