Public: anyone can read this; the crew can change it.
A tell through the API with variants: 2 starts two builders whose runs and pulls share one group id #1574
scout/forge:scout/1571-a-tell-through-the-api-with-variants-2-s → main 3 commits fork
12 files
changed +308
−26
docs/FEED.md +3 −1
| @@ -88,7 +88,9 @@ a card, and paging past the first 20. | ||
| 88 | 88 | - Browser sessions now honour `Idempotency-Key` on `/api/v1/` mutations, scoped |
| 89 | 89 | to the person (as tokens' are to the token): a replay returns the first |
| 90 | 90 | answer. `POST /api/v1/feed/tells { app, text }` is the JSON tell the outbox |
| 91 | sends. | |
| 91 | sends. Add `variants` (an integer 1–4; anything else is a 400) and the | |
| 92 | tell opens that many issues, one builder run each, sharing a `variantGroup` | |
| 93 | id that the pulls they open carry, with their `siblings` listed. | |
| 92 | 94 | - The live client reopens `/events?since=<last id>` when the browser gives up |
| 93 | 95 | on the stream, and on `online`, and refreshes every region once. |
| 94 | 96 | - Not done: steering a run offline (a plain form post, needs the network), |
scripts/gate.ts +1
| @@ -71,6 +71,7 @@ export const GATE: readonly string[] = [ | ||
| 71 | 71 | "test/passport-edge.test.ts", |
| 72 | 72 | "test/crew-direction.test.ts", |
| 73 | 73 | "test/crew-direction-autonomy.test.ts", |
| 74 | "test/variants.test.ts", | |
| 74 | 75 | "test/crew-guard.test.ts", |
| 75 | 76 | "test/review-waits.test.ts", |
| 76 | 77 | "test/scale.test.ts", |
src/actions/issues.ts +2
| @@ -291,6 +291,7 @@ export function assignIssue( | ||
| 291 | 291 | repo: Repo, |
| 292 | 292 | issue: Issue, |
| 293 | 293 | assignee: User | null, |
| 294 | variantGroup?: string, | |
| 294 | 295 | ): Result< |
| 295 | 296 | Issue, |
| 296 | 297 | AccessError | "assignee_cannot_read" | "agent_not_on_crew" | "unverified" |
| @@ -322,6 +323,7 @@ export function assignIssue( | ||
| 322 | 323 | number: issue.number, |
| 323 | 324 | assignee: assignee?.username ?? null, |
| 324 | 325 | agent: assignee?.kind === "agent", |
| 326 | ...(variantGroup === undefined ? {} : { variantGroup }), | |
| 325 | 327 | }, |
| 326 | 328 | }); |
| 327 | 329 | return ok(updated); |
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 variant group of the run opening this pull (#1571). */ | |
| 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 +43 −15
| @@ -14,6 +14,7 @@ import { listRuns } 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,8 +26,14 @@ 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 | /** With variants: the shared group id, and every variant's tell (this one first). */ | |
| 30 | readonly variantGroup?: string; | |
| 31 | readonly variants?: readonly Told[]; | |
| 28 | 32 | } |
| 29 | 33 | |
| 34 | /** Most builders one tell may start side by side. */ | |
| 35 | export const VARIANTS_MAX = 4; | |
| 36 | ||
| 30 | 37 | export type TellError = |
| 31 | 38 | | "empty" |
| 32 | 39 | | "not_found" |
| @@ -44,6 +51,7 @@ export async function tell( | ||
| 44 | 51 | actor: User | undefined, |
| 45 | 52 | repo: Repo, |
| 46 | 53 | said: string, |
| 54 | variants = 1, | |
| 47 | 55 | ): Promise<Result<Told, TellError>> { |
| 48 | 56 | // A tell is a run: only someone who may direct the crew here says one, |
| 49 | 57 | // and a refused tell opens nothing (`crew/direct.ts`). |
| @@ -58,27 +66,47 @@ export async function tell( | ||
| 58 | 66 | const firstLine = text.split(/\n/)[0]!.trim(); |
| 59 | 67 | const title = |
| 60 | 68 | 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; | |
| 69 | const group = variants > 1 ? id() : undefined; | |
| 66 | 70 | const crew = listCrewRoles(app.db, repo.id); |
| 67 | 71 | const inFlight = new Map<string, number>(); |
| 68 | 72 | for (const r of listRuns(app.db, repo.id, { kind: "agent", limit: 50 })) |
| 69 | 73 | if ((r.state === "running" || r.state === "queued") && r.actorId !== null) |
| 70 | 74 | 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, | |
| 75 | // One issue per variant: an issue belongs to one builder, so each variant | |
| 76 | // is its own issue, run and pull, tied by the group id alone (#1571). | |
| 77 | const told: Told[] = []; | |
| 78 | for (let n = 0; n < variants; n++) { | |
| 79 | const opened = openIssue(app, actor, repo, { | |
| 80 | title, | |
| 81 | body: text === firstLine ? "" : text, | |
| 82 | }); | |
| 83 | if (!opened.ok) { | |
| 84 | if (told.length === 0) return opened; | |
| 85 | break; | |
| 86 | } | |
| 87 | const routed = await routeIssue(app, repo, opened.value, crew, inFlight); | |
| 88 | if (routed === undefined) { | |
| 89 | told.push({ issue: opened.value, routed: undefined, assigned: false }); | |
| 90 | break; | |
| 91 | } | |
| 92 | const taken = routed.builder.user.id; | |
| 93 | inFlight.set(taken, (inFlight.get(taken) ?? 0) + 1); | |
| 94 | const assigned = assignIssue( | |
| 95 | app, | |
| 96 | actor, | |
| 97 | repo, | |
| 98 | opened.value, | |
| 99 | routed.builder.user, | |
| 100 | group, | |
| 101 | ); | |
| 102 | told.push({ issue: opened.value, routed, assigned: assigned.ok }); | |
| 103 | } | |
| 104 | const first = told[0]!; | |
| 105 | return ok( | |
| 106 | group === undefined | |
| 107 | ? first | |
| 108 | : { ...first, variantGroup: group, variants: told }, | |
| 80 | 109 | ); |
| 81 | return ok({ issue: opened.value, routed, assigned: assigned.ok }); | |
| 82 | 110 | } |
| 83 | 111 | |
| 84 | 112 | /** What every door says when a tell is refused for an unverified address. */ |
src/core/pulls.ts +27 −5
| @@ -57,6 +57,10 @@ export interface Pull extends Issue { | ||
| 57 | 57 | readonly originHeadRepo: string | null; |
| 58 | 58 | /** The pull this one reverts, when it was opened by "Take it back". */ |
| 59 | 59 | readonly revertsPullId: string | null; |
| 60 | /** The group of variants this pull is one of (#1571); null for an ordinary pull. */ | |
| 61 | readonly variantGroup: string | null; | |
| 62 | /** The other pulls of that group, by number; empty for an ordinary pull. */ | |
| 63 | readonly siblings: readonly number[]; | |
| 60 | 64 | } |
| 61 | 65 | |
| 62 | 66 | interface PullRow { |
| @@ -69,9 +73,22 @@ interface PullRow { | ||
| 69 | 73 | head_repo_id: string | null; |
| 70 | 74 | origin_head_repo: string | null; |
| 71 | 75 | reverts_issue_id: string | null; |
| 76 | variant_group: string | null; | |
| 72 | 77 | } |
| 73 | 78 | |
| 74 | function fromRow(issue: Issue, row: PullRow): Pull { | |
| 79 | /** The numbers of the other pulls in a variant group, oldest first. */ | |
| 80 | function siblingsOf(db: Database, issue: Issue, group: string | null): number[] { | |
| 81 | if (group === null) return []; | |
| 82 | return db | |
| 83 | .query<{ number: number }, [string, string]>( | |
| 84 | `SELECT issue.number AS number FROM pull JOIN issue ON issue.id = pull.issue_id | |
| 85 | WHERE pull.variant_group = ?1 AND pull.issue_id != ?2 ORDER BY issue.number`, | |
| 86 | ) | |
| 87 | .all(group, issue.id) | |
| 88 | .map((r) => r.number); | |
| 89 | } | |
| 90 | ||
| 91 | function fromRow(db: Database, issue: Issue, row: PullRow): Pull { | |
| 75 | 92 | const headRepoId = row.head_repo_id ?? issue.repoId; |
| 76 | 93 | return { |
| 77 | 94 | ...issue, |
| @@ -85,6 +102,8 @@ function fromRow(issue: Issue, row: PullRow): Pull { | ||
| 85 | 102 | crossRepo: headRepoId !== issue.repoId, |
| 86 | 103 | originHeadRepo: row.origin_head_repo, |
| 87 | 104 | revertsPullId: row.reverts_issue_id, |
| 105 | variantGroup: row.variant_group, | |
| 106 | siblings: siblingsOf(db, issue, row.variant_group), | |
| 88 | 107 | }; |
| 89 | 108 | } |
| 90 | 109 | |
| @@ -92,7 +111,7 @@ function join(db: Database, issue: Issue): Pull | undefined { | ||
| 92 | 111 | const row = db |
| 93 | 112 | .query<PullRow, [string]>("SELECT * FROM pull WHERE issue_id = ?1") |
| 94 | 113 | .get(issue.id); |
| 95 | return row ? fromRow(issue, row) : undefined; | |
| 114 | return row ? fromRow(db, issue, row) : undefined; | |
| 96 | 115 | } |
| 97 | 116 | |
| 98 | 117 | export type CreatePullError = |
| @@ -120,6 +139,8 @@ export interface CreatePullInput { | ||
| 120 | 139 | headDir?: string; |
| 121 | 140 | /** The issue id of the pull this one reverts ("Take it back"). */ |
| 122 | 141 | revertsIssueId?: string; |
| 142 | /** The variant group of the run that opens this pull (#1571). */ | |
| 143 | variantGroup?: string; | |
| 123 | 144 | } |
| 124 | 145 | |
| 125 | 146 | /** |
| @@ -234,8 +255,8 @@ export async function createPull( | ||
| 234 | 255 | |
| 235 | 256 | const headRepoId = input.headRepoId ?? input.repoId; |
| 236 | 257 | db.query( |
| 237 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, reverts_issue_id) | |
| 238 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)`, | |
| 258 | `INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, reverts_issue_id, variant_group) | |
| 259 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)`, | |
| 239 | 260 | ).run( |
| 240 | 261 | issue.value.id, |
| 241 | 262 | input.baseBranch, |
| @@ -244,6 +265,7 @@ export async function createPull( | ||
| 244 | 265 | headOid, |
| 245 | 266 | headRepoId, |
| 246 | 267 | input.revertsIssueId ?? null, |
| 268 | input.variantGroup ?? null, | |
| 247 | 269 | ); |
| 248 | 270 | |
| 249 | 271 | // A durable, fetchable ref for the PR head — the way tooling addresses it, |
| @@ -427,7 +449,7 @@ export function joinMany(db: Database, issues: readonly Issue[]): Pull[] { | ||
| 427 | 449 | const out: Pull[] = []; |
| 428 | 450 | for (const issue of issues) { |
| 429 | 451 | const row = rows.get(issue.id); |
| 430 | if (row !== undefined) out.push(fromRow(issue, row)); | |
| 452 | if (row !== undefined) out.push(fromRow(db, issue, row)); | |
| 431 | 453 | } |
| 432 | 454 | return out; |
| 433 | 455 | } |
src/core/runs.ts +9 −2
| @@ -78,6 +78,8 @@ export interface Run { | ||
| 78 | 78 | readonly sandboxNote: string; |
| 79 | 79 | /** A failed check's own detail, when its output was recognised; null otherwise. */ |
| 80 | 80 | readonly checkDetail: CheckDetail | null; |
| 81 | /** The group a tell with variants started this run in; null for any other run. */ | |
| 82 | readonly variantGroup: string | null; | |
| 81 | 83 | readonly createdAt: string; |
| 82 | 84 | readonly startedAt: string | null; |
| 83 | 85 | /** When this deploy's environment started serving — set once, at the swap-in. */ |
| @@ -106,6 +108,7 @@ interface RunRow { | ||
| 106 | 108 | sandbox_enforced: number | null; |
| 107 | 109 | sandbox_note: string; |
| 108 | 110 | check_detail: string; |
| 111 | variant_group: string | null; | |
| 109 | 112 | created_at: string; |
| 110 | 113 | started_at: string | null; |
| 111 | 114 | serving_at: string | null; |
| @@ -134,6 +137,7 @@ const toRun = (r: RunRow): Run => ({ | ||
| 134 | 137 | r.sandbox_enforced === null ? null : r.sandbox_enforced !== 0, |
| 135 | 138 | sandboxNote: r.sandbox_note, |
| 136 | 139 | checkDetail: r.check_detail ? (JSON.parse(r.check_detail) as CheckDetail) : null, |
| 140 | variantGroup: r.variant_group, | |
| 137 | 141 | createdAt: r.created_at, |
| 138 | 142 | startedAt: r.started_at, |
| 139 | 143 | servingAt: r.serving_at, |
| @@ -154,14 +158,16 @@ export interface CreateRunInput { | ||
| 154 | 158 | attempt?: number | undefined; |
| 155 | 159 | /** What the card says while it waits. */ |
| 156 | 160 | summary?: string | undefined; |
| 161 | /** The variant group of the tell that started this run. */ | |
| 162 | variantGroup?: string | undefined; | |
| 157 | 163 | } |
| 158 | 164 | |
| 159 | 165 | export function createRun(db: Database, input: CreateRunInput): Run { |
| 160 | 166 | const runId = id(); |
| 161 | 167 | db.query( |
| 162 | 168 | `INSERT INTO run (id, repo_id, kind, issue_id, env, commit_oid, branch, actor_id, reason, |
| 163 | not_before, attempt, summary) | |
| 164 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)`, | |
| 169 | not_before, attempt, summary, variant_group) | |
| 170 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)`, | |
| 165 | 171 | ).run( |
| 166 | 172 | runId, |
| 167 | 173 | input.repoId, |
| @@ -175,6 +181,7 @@ export function createRun(db: Database, input: CreateRunInput): Run { | ||
| 175 | 181 | input.notBefore ?? null, |
| 176 | 182 | input.attempt ?? 0, |
| 177 | 183 | input.summary ?? "", |
| 184 | input.variantGroup ?? null, | |
| 178 | 185 | ); |
| 179 | 186 | return getRun(db, runId)!; |
| 180 | 187 | } |
src/core/schema.ts +7
| @@ -1614,6 +1614,13 @@ export const migrations: readonly string[] = [ | ||
| 1614 | 1614 | PRIMARY KEY (key, issue_id) |
| 1615 | 1615 | ); |
| 1616 | 1616 | `, |
| 1617 | // 77 — variants (#1571): one tell that starts several builders marks their | |
| 1618 | // runs and the pulls those open with one group id. Null for anything else. | |
| 1619 | ` | |
| 1620 | ALTER TABLE pull ADD COLUMN variant_group TEXT; | |
| 1621 | ALTER TABLE run ADD COLUMN variant_group TEXT; | |
| 1622 | CREATE INDEX pull_variant_group ON pull(variant_group) WHERE variant_group IS NOT NULL; | |
| 1623 | `, | |
| 1617 | 1624 | ]; |
| 1618 | 1625 | |
| 1619 | 1626 | /** |
src/crew/dispatch.ts +6
| @@ -259,6 +259,7 @@ export function queueAgentRun( | ||
| 259 | 259 | reason: RunReason, |
| 260 | 260 | actorId?: string, |
| 261 | 261 | again?: { notBefore: string; attempt: number; summary: string }, |
| 262 | variantGroup?: string, | |
| 262 | 263 | ): Run | undefined { |
| 263 | 264 | // A suspended or removed owner's repositories run nothing, whoever asks. |
| 264 | 265 | if (!accountActive(app.db, repo.ownerId)) return undefined; |
| @@ -327,6 +328,7 @@ export function queueAgentRun( | ||
| 327 | 328 | actorId: agent.user.id, |
| 328 | 329 | reason, |
| 329 | 330 | ...again, |
| 331 | ...(variantGroup === undefined ? {} : { variantGroup }), | |
| 330 | 332 | }); |
| 331 | 333 | }); |
| 332 | 334 | if (run !== undefined) announceRun(app, run, actorId); |
| @@ -401,6 +403,10 @@ function route(app: App, event: ForgeEvent): void { | ||
| 401 | 403 | agent, |
| 402 | 404 | "assigned", |
| 403 | 405 | event.actorId ?? undefined, |
| 406 | undefined, | |
| 407 | typeof event.payload["variantGroup"] === "string" | |
| 408 | ? event.payload["variantGroup"] | |
| 409 | : undefined, | |
| 404 | 410 | ); |
| 405 | 411 | return; |
| 406 | 412 | } |
src/crew/runner.ts +3
| @@ -1243,6 +1243,7 @@ async function work( | ||
| 1243 | 1243 | : `Budget reached at $${stoppedAt.toFixed(2)}; this is where it stopped.`, |
| 1244 | 1244 | write, |
| 1245 | 1245 | Object.values(grant.env), |
| 1246 | run.variantGroup ?? undefined, | |
| 1246 | 1247 | ); |
| 1247 | 1248 | return { |
| 1248 | 1249 | state: result.ok || landed.pushed ? "ok" : "failed", |
| @@ -1511,6 +1512,7 @@ async function landWork( | ||
| 1511 | 1512 | note: string | undefined, |
| 1512 | 1513 | write: (line: string) => void, |
| 1513 | 1514 | granted: readonly string[] = [], |
| 1515 | variantGroup?: string, | |
| 1514 | 1516 | ): Promise<{ pushed: boolean; summary: string }> { |
| 1515 | 1517 | // A model may echo a granted value into a file; the tree is scrubbed |
| 1516 | 1518 | // before the forge commits it, and the agent's own commits are searched |
| @@ -1655,6 +1657,7 @@ async function landWork( | ||
| 1655 | 1657 | baseBranch: repo.defaultBranch, |
| 1656 | 1658 | headBranch: branch, |
| 1657 | 1659 | headRepo: fork, |
| 1660 | ...(variantGroup === undefined ? {} : { variantGroup }), | |
| 1658 | 1661 | }); |
| 1659 | 1662 | if (!opened.ok) { |
| 1660 | 1663 | write(`could not open the pull: ${JSON.stringify(opened.error)}`); |
src/http/feed.ts +28 −3
| @@ -3,7 +3,7 @@ | ||
| 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 { tell, UNVERIFIED_WORDS, VARIANTS_MAX } from "../actions/tell.ts"; | |
| 7 | 7 | import { DIRECTION_NOTE } from "../crew/direct.ts"; |
| 8 | 8 | import { audienceFilter } from "../core/event_audience.ts"; |
| 9 | 9 | import { getEvent } from "../core/events.ts"; |
| @@ -154,20 +154,38 @@ export function feedRoutes(router: Router<Handler>): void { | ||
| 154 | 154 | const text = str(input.text).trim(); |
| 155 | 155 | if (text === "" || text.length > 2000) |
| 156 | 156 | return apiError("invalid_text", 422, "text is 1 to 2000 characters"); |
| 157 | const variants = parseVariants(input.variants); | |
| 158 | if (variants === undefined) | |
| 159 | return apiError( | |
| 160 | "invalid_variants", | |
| 161 | 400, | |
| 162 | `variants is an integer from 1 to ${VARIANTS_MAX}`, | |
| 163 | ); | |
| 157 | 164 | const [owner, name, extra] = str(input.app).split("/"); |
| 158 | 165 | const repo = |
| 159 | 166 | owner && name && extra === undefined |
| 160 | 167 | ? getRepo(ctx.app.db, owner, name) |
| 161 | 168 | : undefined; |
| 162 | 169 | if (!repo || !canRead(ctx.app.db, repo, viewer)) return notFound(); |
| 163 | const told = await tell(ctx.app, viewer, repo, text); | |
| 170 | const told = await tell(ctx.app, viewer, repo, text, variants); | |
| 164 | 171 | if (!told.ok) |
| 165 | 172 | return told.error === "forbidden" |
| 166 | 173 | ? apiError("forbidden", 403, DIRECTION_NOTE) |
| 167 | 174 | : told.error === "unverified" |
| 168 | 175 | ? apiError("unverified", 403, UNVERIFIED_WORDS) |
| 169 | 176 | : apiError("refused", told.error === "unauthorized" ? 403 : 422, "that could not be told"); |
| 170 | return json({ issue: told.value.issue.number }, 201); | |
| 177 | return json( | |
| 178 | { | |
| 179 | issue: told.value.issue.number, | |
| 180 | ...(told.value.variantGroup === undefined | |
| 181 | ? {} | |
| 182 | : { | |
| 183 | variantGroup: told.value.variantGroup, | |
| 184 | issues: (told.value.variants ?? []).map((t) => t.issue.number), | |
| 185 | }), | |
| 186 | }, | |
| 187 | 201, | |
| 188 | ); | |
| 171 | 189 | }); |
| 172 | 190 | |
| 173 | 191 | router.add("POST", "/api/v1/share", async (ctx) => { |
| @@ -190,6 +208,13 @@ export function feedRoutes(router: Router<Handler>): void { | ||
| 190 | 208 | }); |
| 191 | 209 | } |
| 192 | 210 | |
| 211 | /** `variants` as 1–4: absent is 1; a string, a fraction or out of range is undefined. */ | |
| 212 | export function parseVariants(raw: unknown): number | undefined { | |
| 213 | if (raw === undefined) return 1; | |
| 214 | if (typeof raw !== "number" || !Number.isInteger(raw)) return undefined; | |
| 215 | return raw >= 1 && raw <= VARIANTS_MAX ? raw : undefined; | |
| 216 | } | |
| 217 | ||
| 193 | 218 | /** `to` as a list of usernames: absent, one, or a few; anything else is no. */ |
| 194 | 219 | export function parseTargets(raw: unknown): string[] | undefined { |
| 195 | 220 | if (raw === undefined || raw === null) return []; |
test/variants.test.ts added +176
| @@ -0,0 +1,176 @@ | ||
| 1 | /** | |
| 2 | * A tell through the API with `variants` (#1571): n builders' runs share one | |
| 3 | * group id and the pulls they open carry it. No real agent runs here. | |
| 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 { getPull } from "../src/core/pulls.ts"; | |
| 14 | import { type Repo } from "../src/core/repos.ts"; | |
| 15 | import { listRuns } from "../src/core/runs.ts"; | |
| 16 | import { createToken } from "../src/core/tokens.ts"; | |
| 17 | import { createUser, type User } from "../src/core/users.ts"; | |
| 18 | import { startCrew } from "../src/crew/dispatch.ts"; | |
| 19 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | let dataDir: string; | |
| 25 | let app: App; | |
| 26 | let servers: Servers; | |
| 27 | let alice: User; | |
| 28 | let repo: Repo; | |
| 29 | let auth: Record<string, string>; | |
| 30 | let crew: Awaited<ReturnType<typeof startCrew>> | undefined; | |
| 31 | ||
| 32 | const ok = <T>( | |
| 33 | r: { ok: true; value: T } | { ok: false; error: unknown }, | |
| 34 | ): T => { | |
| 35 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 36 | return r.value; | |
| 37 | }; | |
| 38 | ||
| 39 | const say = (extra: Record<string, unknown>) => | |
| 40 | fetch(`${servers.url}/api/v1/feed/tells`, { | |
| 41 | method: "POST", | |
| 42 | headers: { ...auth, "content-type": "application/json" }, | |
| 43 | body: JSON.stringify({ app: "alice/square", text: "Round it", ...extra }), | |
| 44 | }); | |
| 45 | ||
| 46 | const runs = () => listRuns(app.db, repo.id, { kind: "agent" }); | |
| 47 | ||
| 48 | beforeAll(async () => { | |
| 49 | dataDir = await mkdtemp(join(tmpdir(), "forge-variants-")); | |
| 50 | app = createApp({ dataDir, port: 0 }); | |
| 51 | servers = await serve(app); | |
| 52 | // A crew whose Claude Code does not exist: dispatch queues for real and | |
| 53 | // no agent runs. | |
| 54 | crew = await startCrew(app, { | |
| 55 | forgeUrl: servers.url, | |
| 56 | localUrl: servers.url, | |
| 57 | binary: join(dataDir, "no-such-claude"), | |
| 58 | concurrency: 1, | |
| 59 | budgetUsd: 1, | |
| 60 | runTimeoutMs: 60_000, | |
| 61 | }); | |
| 62 | alice = ok( | |
| 63 | await createUser(app.db, { | |
| 64 | trusted: true, | |
| 65 | username: "alice", | |
| 66 | email: "[email protected]", | |
| 67 | password: "a-good-passphrase", | |
| 68 | adminIfFirst: true, | |
| 69 | }), | |
| 70 | ); | |
| 71 | ok(await createAgent(app, alice, { username: "mason" })); | |
| 72 | repo = ok(await createRepo(app, alice, { name: "square", visibility: "public" })); | |
| 73 | ok( | |
| 74 | await commitToBranch(app, alice, repo, { | |
| 75 | message: "Start", | |
| 76 | files: [{ path: "README.md", content: "# square\n" }], | |
| 77 | }), | |
| 78 | ); | |
| 79 | expect(addToCrew(app, alice, repo, "mason").ok).toBe(true); | |
| 80 | auth = { | |
| 81 | authorization: `Bearer ${createToken(app.db, alice.id, "t", "rw").token}`, | |
| 82 | }; | |
| 83 | }, 30_000); | |
| 84 | ||
| 85 | afterAll(async () => { | |
| 86 | await crew?.stop(); | |
| 87 | servers?.stop(); | |
| 88 | app?.close(); | |
| 89 | await rm(dataDir, { recursive: true, force: true }); | |
| 90 | }); | |
| 91 | ||
| 92 | describe("variants on a tell", () => { | |
| 93 | test("a plain tell has no group", async () => { | |
| 94 | const res = await say({}); | |
| 95 | expect(res.status).toBe(201); | |
| 96 | const body = (await res.json()) as Record<string, unknown>; | |
| 97 | expect(body["variantGroup"]).toBeUndefined(); | |
| 98 | expect(runs().length).toBe(1); | |
| 99 | expect(runs()[0]!.variantGroup).toBeNull(); | |
| 100 | }); | |
| 101 | ||
| 102 | test("variants: 2 makes two runs in one group", async () => { | |
| 103 | const before = runs().length; | |
| 104 | const res = await say({ variants: 2 }); | |
| 105 | expect(res.status).toBe(201); | |
| 106 | const body = (await res.json()) as { variantGroup: string; issues: number[] }; | |
| 107 | expect(body.issues.length).toBe(2); | |
| 108 | const made = runs().filter((r) => r.variantGroup === body.variantGroup); | |
| 109 | expect(runs().length).toBe(before + 2); | |
| 110 | expect(made.length).toBe(2); | |
| 111 | expect(new Set(made.map((r) => r.issueId)).size).toBe(2); | |
| 112 | }); | |
| 113 | ||
| 114 | test.each([0, 9, "2", 2.5, null, -1])("variants %p is a 400", async (bad) => { | |
| 115 | const before = runs().length; | |
| 116 | const res = await say({ variants: bad }); | |
| 117 | expect(res.status).toBe(400); | |
| 118 | expect(((await res.json()) as { error: string }).error).toBe( | |
| 119 | "invalid_variants", | |
| 120 | ); | |
| 121 | expect(runs().length).toBe(before); | |
| 122 | }); | |
| 123 | ||
| 124 | test("the pulls of one group carry it and name each other", async () => { | |
| 125 | const group = runs().find((r) => r.variantGroup !== null)!.variantGroup!; | |
| 126 | const numbers: number[] = []; | |
| 127 | for (const branch of ["one", "two"]) { | |
| 128 | ok( | |
| 129 | await commitToBranch(app, alice, repo, { | |
| 130 | branch, | |
| 131 | base: "main", | |
| 132 | message: branch, | |
| 133 | files: [{ path: `${branch}.txt`, content: `${branch}\n` }], | |
| 134 | }), | |
| 135 | ); | |
| 136 | const pull = ok( | |
| 137 | await openPull(app, alice, repo, { | |
| 138 | title: branch, | |
| 139 | baseBranch: "main", | |
| 140 | headBranch: branch, | |
| 141 | variantGroup: group, | |
| 142 | }), | |
| 143 | ); | |
| 144 | numbers.push(pull.number); | |
| 145 | } | |
| 146 | const [a, b] = numbers as [number, number]; | |
| 147 | expect(getPull(app.db, repo.id, a)!.siblings).toEqual([b]); | |
| 148 | const res = await fetch( | |
| 149 | `${servers.url}/api/v1/repos/alice/square/pulls/${b}`, | |
| 150 | { headers: auth }, | |
| 151 | ); | |
| 152 | const json = (await res.json()) as { variantGroup: string; siblings: number[] }; | |
| 153 | expect(json.variantGroup).toBe(group); | |
| 154 | expect(json.siblings).toEqual([a]); | |
| 155 | }); | |
| 156 | ||
| 157 | test("an ordinary pull has no group and no siblings", async () => { | |
| 158 | ok( | |
| 159 | await commitToBranch(app, alice, repo, { | |
| 160 | branch: "plain", | |
| 161 | base: "main", | |
| 162 | message: "plain", | |
| 163 | files: [{ path: "p.txt", content: "p\n" }], | |
| 164 | }), | |
| 165 | ); | |
| 166 | const pull = ok( | |
| 167 | await openPull(app, alice, repo, { | |
| 168 | title: "plain", | |
| 169 | baseBranch: "main", | |
| 170 | headBranch: "plain", | |
| 171 | }), | |
| 172 | ); | |
| 173 | expect(pull.variantGroup).toBeNull(); | |
| 174 | expect(pull.siblings).toEqual([]); | |
| 175 | }); | |
| 176 | }); |
new above
offline