Public: anyone can read this; the crew can change it.
Variants of one intent: sibling pulls grouped under one tell, and a swipe deck that records likes and dislikes in the decision ledger #1301
scout/forge:scout/1051-variants-of-one-intent-sibling-pulls-gro → main 2 commits fork
20 files
changed +828
−32
docs/STUDIO.md +16
| @@ -520,3 +520,19 @@ It never says _shipped_ before production redeployed, never hides that a | ||
| 520 | 520 | run cost money, never shows a preview that is not the pull's real head, and |
| 521 | 521 | never invents an answer for a card. The complexity is removed from the |
| 522 | 522 | surface, not from the system. |
| 523 | ||
| 524 | ## Variants: one tell, several ways | |
| 525 | ||
| 526 | A tell may ask for `variants: n` (1 to 4; the box's "Ways", the MCP `tell` | |
| 527 | tool, `POST /api/v1/feed/tells`). Each variant is its own sibling issue with | |
| 528 | its own run and pull, all tied by one group id — the first issue's — on the | |
| 529 | issue, the run and the pull (`core/variants.ts`). | |
| 530 | ||
| 531 | The deck at `/:owner/:name/choose/:group` shows one card per open sibling | |
| 532 | (its look and its preview). Swipe right or press → to like the first | |
| 533 | undecided card, left or ← to dislike it; each card has the same two buttons. | |
| 534 | A like is a decision-ledger row (`lean`, answer `like`); a dislike is one | |
| 535 | too (`dislike`) and closes the pull and its variant issue with a one-line | |
| 536 | reason the builder reads as a comment. The most recently liked open change | |
| 537 | gets Ship (the ordinary ship). While siblings are open the deck is also a | |
| 538 | feed item of type `choose`, for people who can write there. |
src/actions/choose.ts added +77
| @@ -0,0 +1,77 @@ | ||
| 1 | /** | |
| 2 | * Choosing among the variants of one tell. A like or a dislike is a person's | |
| 3 | * lean on one sibling pull, kept in the decision ledger; a dislike also | |
| 4 | * closes the pull with a one-line reason the builder reads on it, and the | |
| 5 | * variant issue it answered. Landing the liked one is the ordinary ship. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import { recordLean, type Lean } from "../core/variants.ts"; | |
| 10 | import type { Decision } from "../core/decisions.ts"; | |
| 11 | import { getIssue } from "../core/issues.ts"; | |
| 12 | import { closesFromBody, type Pull } from "../core/pulls.ts"; | |
| 13 | import { canWrite, type Repo } from "../core/repos.ts"; | |
| 14 | import type { User } from "../core/users.ts"; | |
| 15 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 16 | import { groupOfIssue } from "../core/variants.ts"; | |
| 17 | import { requireParticipant, type AccessError } from "./access.ts"; | |
| 18 | import { addComment, setIssueState } from "./issues.ts"; | |
| 19 | ||
| 20 | export type LeanError = AccessError | "not_a_variant" | "not_open"; | |
| 21 | ||
| 22 | export const REASON_MAX = 200; | |
| 23 | export const DEFAULT_REASON = "another variant was chosen instead"; | |
| 24 | ||
| 25 | /** One line, bounded: what a person typed, or the default. */ | |
| 26 | export function oneLineReason(raw: string): string { | |
| 27 | const line = raw.replace(/\s+/g, " ").trim().slice(0, REASON_MAX).trim(); | |
| 28 | return line === "" ? DEFAULT_REASON : line; | |
| 29 | } | |
| 30 | ||
| 31 | export function leanOn( | |
| 32 | app: App, | |
| 33 | actor: User | undefined, | |
| 34 | repo: Repo, | |
| 35 | pull: Pull, | |
| 36 | answer: Lean, | |
| 37 | reasonRaw = "", | |
| 38 | ): Result<Decision, LeanError> { | |
| 39 | const user = requireParticipant(app, actor, repo); | |
| 40 | if (!user.ok) return user; | |
| 41 | // Choosing is the crew's direction: a person who can write, never an agent. | |
| 42 | if (user.value.kind === "agent" || !canWrite(app.db, repo, user.value)) | |
| 43 | return err("forbidden"); | |
| 44 | if (pull.repoId !== repo.id || pull.groupId === null) | |
| 45 | return err("not_a_variant"); | |
| 46 | if (pull.state !== "open" || pull.mergedAt !== null) return err("not_open"); | |
| 47 | const reason = answer === "dislike" ? oneLineReason(reasonRaw) : ""; | |
| 48 | if (answer === "dislike") { | |
| 49 | const closed = setIssueState(app, user.value, repo, pull, "closed"); | |
| 50 | if (!closed.ok) return closed.error === "already_merged" ? err("not_open") : err("forbidden"); | |
| 51 | addComment(app, user.value, repo, pull, `Closed, not this variant: ${reason}`); | |
| 52 | // The sibling issue this pull answered is done with too. | |
| 53 | for (const n of closesFromBody(pull.body)) { | |
| 54 | const issue = getIssue(app.db, repo.id, n); | |
| 55 | if ( | |
| 56 | issue !== undefined && | |
| 57 | issue.kind === "issue" && | |
| 58 | issue.state === "open" && | |
| 59 | groupOfIssue(app.db, issue.id) === pull.groupId | |
| 60 | ) | |
| 61 | setIssueState(app, user.value, repo, issue, "closed", { | |
| 62 | reason: "dropped", | |
| 63 | }); | |
| 64 | } | |
| 65 | } | |
| 66 | return ok( | |
| 67 | recordLean( | |
| 68 | app.db, | |
| 69 | repo.id, | |
| 70 | pull, | |
| 71 | pull.groupId, | |
| 72 | answer, | |
| 73 | user.value.username, | |
| 74 | reason, | |
| 75 | ), | |
| 76 | ); | |
| 77 | } |
src/actions/tell.ts +64 −22
| @@ -13,6 +13,7 @@ import type { Repo } from "../core/repos.ts"; | ||
| 13 | 13 | import { listRuns } from "../core/runs.ts"; |
| 14 | 14 | import type { User } from "../core/users.ts"; |
| 15 | 15 | import { mayDirectCrew } from "../crew/direct.ts"; |
| 16 | import { MAX_VARIANTS, setIssueGroup } from "../core/variants.ts"; | |
| 16 | 17 | import { routeIssue, type Routed } from "../crew/route.ts"; |
| 17 | 18 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 18 | 19 | import { requireParticipant } from "./access.ts"; |
| @@ -25,6 +26,8 @@ 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 other candidates, when the tell asked for variants (`core/variants.ts`). */ | |
| 30 | readonly siblings: readonly Issue[]; | |
| 28 | 31 | } |
| 29 | 32 | |
| 30 | 33 | export type TellError = |
| @@ -44,6 +47,7 @@ export async function tell( | ||
| 44 | 47 | actor: User | undefined, |
| 45 | 48 | repo: Repo, |
| 46 | 49 | said: string, |
| 50 | variants = 1, | |
| 47 | 51 | ): Promise<Result<Told, TellError>> { |
| 48 | 52 | // A tell is a run: only someone who may direct the crew here says one, |
| 49 | 53 | // and a refused tell opens nothing (`crew/direct.ts`). |
| @@ -56,29 +60,63 @@ export async function tell( | ||
| 56 | 60 | const text = said.trim(); |
| 57 | 61 | if (text === "") return err("empty"); |
| 58 | 62 | const firstLine = text.split(/\n/)[0]!.trim(); |
| 63 | const count = Math.min(MAX_VARIANTS, Math.max(1, Math.trunc(variants) || 1)); | |
| 64 | // Variants are told apart by a suffix, so the line leaves room for it. | |
| 65 | const room = count > 1 ? 62 : 80; | |
| 59 | 66 | const title = |
| 60 | 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; | |
| 66 | const crew = listCrewRoles(app.db, repo.id); | |
| 67 | firstLine.length <= room | |
| 68 | ? firstLine | |
| 69 | : `${firstLine.slice(0, room - 3).trimEnd()}…`; | |
| 70 | const candidates: Issue[] = []; | |
| 71 | let routedFirst: Routed | undefined; | |
| 72 | let assignedFirst = false; | |
| 67 | 73 | const inFlight = new Map<string, number>(); |
| 68 | for (const r of listRuns(app.db, repo.id, { kind: "agent", limit: 50 })) | |
| 69 | if ((r.state === "running" || r.state === "queued") && r.actorId !== null) | |
| 70 | 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 }); | |
| 74 | for (let k = 1; k <= count; k++) { | |
| 75 | const opened = openIssue(app, actor, repo, { | |
| 76 | title: count > 1 ? `${title} (variant ${k} of ${count})` : title, | |
| 77 | body: | |
| 78 | count > 1 | |
| 79 | ? `${text === firstLine ? "" : `${text}\n\n`}Variant ${k} of ${count} of one tell: make your own take on it; the others are made apart from yours and a person picks among them.` | |
| 80 | : text === firstLine | |
| 81 | ? "" | |
| 82 | : text, | |
| 83 | }); | |
| 84 | if (!opened.ok) { | |
| 85 | if (k === 1) return opened; | |
| 86 | break; | |
| 87 | } | |
| 88 | candidates.push(opened.value); | |
| 89 | if (count > 1) setIssueGroup(app.db, opened.value.id, candidates[0]!.id); | |
| 90 | const crew = listCrewRoles(app.db, repo.id); | |
| 91 | if (k === 1) | |
| 92 | for (const r of listRuns(app.db, repo.id, { kind: "agent", limit: 50 })) | |
| 93 | if ( | |
| 94 | (r.state === "running" || r.state === "queued") && | |
| 95 | r.actorId !== null | |
| 96 | ) | |
| 97 | inFlight.set(r.actorId, (inFlight.get(r.actorId) ?? 0) + 1); | |
| 98 | const routed = await routeIssue(app, repo, opened.value, crew, inFlight); | |
| 99 | if (routed === undefined) continue; | |
| 100 | const id = routed.builder.user.id; | |
| 101 | inFlight.set(id, (inFlight.get(id) ?? 0) + 1); | |
| 102 | const assigned = assignIssue( | |
| 103 | app, | |
| 104 | actor, | |
| 105 | repo, | |
| 106 | opened.value, | |
| 107 | routed.builder.user, | |
| 108 | ); | |
| 109 | if (k === 1) { | |
| 110 | routedFirst = routed; | |
| 111 | assignedFirst = assigned.ok; | |
| 112 | } | |
| 113 | } | |
| 114 | return ok({ | |
| 115 | issue: candidates[0]!, | |
| 116 | routed: routedFirst, | |
| 117 | assigned: assignedFirst, | |
| 118 | siblings: candidates.slice(1), | |
| 119 | }); | |
| 82 | 120 | } |
| 83 | 121 | |
| 84 | 122 | /** What every door says when a tell is refused for an unverified address. */ |
| @@ -86,11 +124,15 @@ export const UNVERIFIED_WORDS = "verify your email first"; | ||
| 86 | 124 | |
| 87 | 125 | /** What happened, in the sentence both doors say. */ |
| 88 | 126 | export function toldWords(told: Told): string { |
| 127 | const more = | |
| 128 | told.siblings.length === 0 | |
| 129 | ? "" | |
| 130 | : ` ${told.siblings.length + 1} variants were asked for; pick among them when they are ready.`; | |
| 89 | 131 | if (told.routed === undefined) |
| 90 | 132 | return `Told as #${told.issue.number}. Nobody is on the crew to take it yet — add someone under Settings → Crew.`; |
| 91 | 133 | const r = told.routed; |
| 92 | 134 | const who = r.builder.user.username; |
| 93 | 135 | return told.assigned |
| 94 | ? `${who} is on it — ${r.size} ${r.kind}${r.by === "rule" ? "" : `, ${r.by} decided in ${r.ms} ms`}.` | |
| 136 | ? `${who} is on it — ${r.size} ${r.kind}${r.by === "rule" ? "" : `, ${r.by} decided in ${r.ms} ms`}.${more}` | |
| 95 | 137 | : `Told as #${told.issue.number}, but ${who} could not take it.`; |
| 96 | 138 | } |
src/core/decisions.ts +4 −3
| @@ -13,9 +13,10 @@ import { id as newId } from "../kernel/id.ts"; | ||
| 13 | 13 | * a follow-up with nothing new (`skipped`), an issue at its run cap |
| 14 | 14 | * (`capped`) — counted by `guardCounts` (`crew/guard.ts`). |
| 15 | 15 | * |
| 16 | * `lean` was which option on a studio ask card a careful owner would pick. | |
| 17 | * Nothing records one any more; older ledgers still hold rows of it, and the | |
| 18 | * summaries that read them keep the word. */ | |
| 16 | * `lean` is a person's swipe on a variant (`core/variants.ts`): the answer | |
| 17 | * is `like` or `dislike`, the row's issue is the sibling pull. Older ledgers | |
| 18 | * also hold `lean` rows from the studio's ask cards, which no longer record | |
| 19 | * one; the summaries that read them keep the word. */ | |
| 19 | 20 | export type DecisionKind = "next" | "land" | "retry" | "lean" | "guard"; |
| 20 | 21 | |
| 21 | 22 | export interface Decision { |
src/core/pulls.ts +4
| @@ -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 tell this pull is one variant of, when it has siblings (`variants.ts`). */ | |
| 60 | readonly groupId: 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 | group_id: 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 | groupId: row.group_id, | |
| 87 | 91 | }; |
| 88 | 92 | } |
| 89 | 93 |
src/core/runs.ts +9 −2
| @@ -53,6 +53,8 @@ export interface Run { | ||
| 53 | 53 | readonly commitOid: string; |
| 54 | 54 | /** For an agent run: the branch it works on. */ |
| 55 | 55 | readonly branch: string; |
| 56 | /** The tell this run is one variant of, when it has siblings. */ | |
| 57 | readonly groupId: string | null; | |
| 56 | 58 | readonly state: RunState; |
| 57 | 59 | /** One line: what happened, in words a person reads on a card. */ |
| 58 | 60 | readonly summary: string; |
| @@ -102,6 +104,7 @@ interface RunRow { | ||
| 102 | 104 | attempt: number; |
| 103 | 105 | heartbeat_at: string | null; |
| 104 | 106 | turns: number; |
| 107 | group_id: string | null; | |
| 105 | 108 | sandbox_enforced: number | null; |
| 106 | 109 | sandbox_note: string; |
| 107 | 110 | check_detail: string; |
| @@ -119,6 +122,7 @@ const toRun = (r: RunRow): Run => ({ | ||
| 119 | 122 | env: r.env, |
| 120 | 123 | commitOid: r.commit_oid, |
| 121 | 124 | branch: r.branch, |
| 125 | groupId: r.group_id, | |
| 122 | 126 | state: r.state, |
| 123 | 127 | summary: r.summary, |
| 124 | 128 | actorId: r.actor_id, |
| @@ -153,14 +157,16 @@ export interface CreateRunInput { | ||
| 153 | 157 | attempt?: number | undefined; |
| 154 | 158 | /** What the card says while it waits. */ |
| 155 | 159 | summary?: string | undefined; |
| 160 | /** The tell whose variant this run makes (`core/variants.ts`). */ | |
| 161 | groupId?: string | undefined; | |
| 156 | 162 | } |
| 157 | 163 | |
| 158 | 164 | export function createRun(db: Database, input: CreateRunInput): Run { |
| 159 | 165 | const runId = id(); |
| 160 | 166 | db.query( |
| 161 | 167 | `INSERT INTO run (id, repo_id, kind, issue_id, env, commit_oid, branch, actor_id, reason, |
| 162 | not_before, attempt, summary) | |
| 163 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)`, | |
| 168 | not_before, attempt, summary, group_id) | |
| 169 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)`, | |
| 164 | 170 | ).run( |
| 165 | 171 | runId, |
| 166 | 172 | input.repoId, |
| @@ -174,6 +180,7 @@ export function createRun(db: Database, input: CreateRunInput): Run { | ||
| 174 | 180 | input.notBefore ?? null, |
| 175 | 181 | input.attempt ?? 0, |
| 176 | 182 | input.summary ?? "", |
| 183 | input.groupId ?? null, | |
| 177 | 184 | ); |
| 178 | 185 | return getRun(db, runId)!; |
| 179 | 186 | } |
src/core/schema.ts +10
| @@ -1563,6 +1563,16 @@ export const migrations: readonly string[] = [ | ||
| 1563 | 1563 | WHERE id = NEW.id; |
| 1564 | 1564 | END; |
| 1565 | 1565 | `, |
| 1566 | // 73 — variants of one intent (#1051). A tell asked for n alternatives | |
| 1567 | // opens n sibling issues; `variant_group` on each, and `group_id` on the | |
| 1568 | // runs and the pull they produce, is the id of the first one — the tell. | |
| 1569 | ` | |
| 1570 | ALTER TABLE issue ADD COLUMN variant_group TEXT; | |
| 1571 | ALTER TABLE pull ADD COLUMN group_id TEXT; | |
| 1572 | ALTER TABLE run ADD COLUMN group_id TEXT; | |
| 1573 | CREATE INDEX issue_variant_group ON issue(variant_group) WHERE variant_group IS NOT NULL; | |
| 1574 | CREATE INDEX pull_group ON pull(group_id) WHERE group_id IS NOT NULL; | |
| 1575 | `, | |
| 1566 | 1576 | ]; |
| 1567 | 1577 | |
| 1568 | 1578 | /** |
src/core/variants.ts added +168
| @@ -0,0 +1,168 @@ | ||
| 1 | /** | |
| 2 | * Variants of one intent. A tell may ask for several candidate changes; each | |
| 3 | * becomes a sibling issue, and every sibling — issue, run and pull — carries | |
| 4 | * the id of the first one, the tell. A person chooses among the pulls with | |
| 5 | * likes and dislikes, each kept in the decision ledger as a `lean` row. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { listDecisions, recordDecision, type Decision } from "./decisions.ts"; | |
| 10 | import { joinMany, type Pull } from "./pulls.ts"; | |
| 11 | import { getIssuesByIds } from "./issues.ts"; | |
| 12 | ||
| 13 | export const MAX_VARIANTS = 4; | |
| 14 | ||
| 15 | export type Lean = "like" | "dislike"; | |
| 16 | ||
| 17 | /** `variants: n` as a door sends it: a whole number from 1 to the cap, else undefined. */ | |
| 18 | export function parseVariants(raw: unknown): number | undefined { | |
| 19 | const n = | |
| 20 | typeof raw === "string" && /^\d{1,2}$/.test(raw.trim()) | |
| 21 | ? Number(raw) | |
| 22 | : raw; | |
| 23 | return typeof n === "number" && | |
| 24 | Number.isInteger(n) && | |
| 25 | n >= 1 && | |
| 26 | n <= MAX_VARIANTS | |
| 27 | ? n | |
| 28 | : undefined; | |
| 29 | } | |
| 30 | ||
| 31 | export function setIssueGroup( | |
| 32 | db: Database, | |
| 33 | issueId: string, | |
| 34 | groupId: string, | |
| 35 | ): void { | |
| 36 | db.query("UPDATE issue SET variant_group = ?2 WHERE id = ?1").run( | |
| 37 | issueId, | |
| 38 | groupId, | |
| 39 | ); | |
| 40 | } | |
| 41 | ||
| 42 | export function groupOfIssue(db: Database, issueId: string): string | null { | |
| 43 | return ( | |
| 44 | db | |
| 45 | .query< | |
| 46 | { variant_group: string | null }, | |
| 47 | [string] | |
| 48 | >("SELECT variant_group FROM issue WHERE id = ?1") | |
| 49 | .get(issueId)?.variant_group ?? null | |
| 50 | ); | |
| 51 | } | |
| 52 | ||
| 53 | export function setPullGroup( | |
| 54 | db: Database, | |
| 55 | pullId: string, | |
| 56 | groupId: string, | |
| 57 | ): void { | |
| 58 | db.query("UPDATE pull SET group_id = ?2 WHERE issue_id = ?1").run( | |
| 59 | pullId, | |
| 60 | groupId, | |
| 61 | ); | |
| 62 | } | |
| 63 | ||
| 64 | /** The pulls of one tell, oldest first. */ | |
| 65 | export function siblingPulls( | |
| 66 | db: Database, | |
| 67 | repoId: string, | |
| 68 | groupId: string, | |
| 69 | ): Pull[] { | |
| 70 | const ids = db | |
| 71 | .query< | |
| 72 | { issue_id: string }, | |
| 73 | [string, string] | |
| 74 | >( | |
| 75 | `SELECT p.issue_id FROM pull p JOIN issue i ON i.id = p.issue_id | |
| 76 | WHERE p.group_id = ?2 AND i.repo_id = ?1 ORDER BY i.number`, | |
| 77 | ) | |
| 78 | .all(repoId, groupId) | |
| 79 | .map((r) => r.issue_id); | |
| 80 | const issues = getIssuesByIds(db, ids); | |
| 81 | return joinMany( | |
| 82 | db, | |
| 83 | ids.flatMap((id) => issues.get(id) ?? []), | |
| 84 | ); | |
| 85 | } | |
| 86 | ||
| 87 | /** The open sibling issues of a tell, by number: what the pulls say they close. */ | |
| 88 | export function groupIssueNumbers( | |
| 89 | db: Database, | |
| 90 | repoId: string, | |
| 91 | groupId: string, | |
| 92 | ): number[] { | |
| 93 | return db | |
| 94 | .query< | |
| 95 | { number: number }, | |
| 96 | [string, string] | |
| 97 | >("SELECT number FROM issue WHERE repo_id = ?1 AND variant_group = ?2 AND kind = 'issue' ORDER BY number") | |
| 98 | .all(repoId, groupId) | |
| 99 | .map((r) => r.number); | |
| 100 | } | |
| 101 | ||
| 102 | /** The repo's tells that have more than one sibling pull still open. */ | |
| 103 | export function openGroups( | |
| 104 | db: Database, | |
| 105 | repoIds: readonly string[], | |
| 106 | ): { repoId: string; groupId: string; open: number; at: string }[] { | |
| 107 | if (repoIds.length === 0) return []; | |
| 108 | const marks = repoIds.map((_, n) => `?${n + 1}`).join(","); | |
| 109 | return db | |
| 110 | .query< | |
| 111 | { repo_id: string; group_id: string; open: number; at: string }, | |
| 112 | string[] | |
| 113 | >( | |
| 114 | `SELECT i.repo_id, p.group_id, SUM(i.state = 'open') AS open, MAX(i.updated_at) AS at | |
| 115 | FROM pull p JOIN issue i ON i.id = p.issue_id | |
| 116 | WHERE p.group_id IS NOT NULL AND i.repo_id IN (${marks}) | |
| 117 | GROUP BY i.repo_id, p.group_id | |
| 118 | HAVING COUNT(*) >= 2 AND SUM(i.state = 'open') >= 1 | |
| 119 | ORDER BY at DESC LIMIT 20`, | |
| 120 | ) | |
| 121 | .all(...repoIds) | |
| 122 | .map((r) => ({ | |
| 123 | repoId: r.repo_id, | |
| 124 | groupId: r.group_id, | |
| 125 | open: r.open, | |
| 126 | at: r.at, | |
| 127 | })); | |
| 128 | } | |
| 129 | ||
| 130 | /** Which way each sibling was last leaned, by pull id; the newest row wins. */ | |
| 131 | export function leansOf( | |
| 132 | db: Database, | |
| 133 | repoId: string, | |
| 134 | groupId: string, | |
| 135 | ): Map<string, Decision> { | |
| 136 | const out = new Map<string, Decision>(); | |
| 137 | for (const d of listDecisions(db, repoId, { kind: "lean", limit: 500 })) | |
| 138 | if ( | |
| 139 | d.issueId !== null && | |
| 140 | !out.has(d.issueId) && | |
| 141 | typeof d.state === "object" && | |
| 142 | d.state !== null && | |
| 143 | (d.state as { group?: unknown }).group === groupId | |
| 144 | ) | |
| 145 | out.set(d.issueId, d); | |
| 146 | return out; | |
| 147 | } | |
| 148 | ||
| 149 | export function recordLean( | |
| 150 | db: Database, | |
| 151 | repoId: string, | |
| 152 | pull: Pull, | |
| 153 | groupId: string, | |
| 154 | answer: Lean, | |
| 155 | by: string, | |
| 156 | reason: string, | |
| 157 | ): Decision { | |
| 158 | return recordDecision(db, { | |
| 159 | kind: "lean", | |
| 160 | repoId, | |
| 161 | issueId: pull.id, | |
| 162 | state: { group: groupId, pull: pull.number, title: pull.title, reason }, | |
| 163 | answer, | |
| 164 | p: 1, | |
| 165 | by, | |
| 166 | ms: 0, | |
| 167 | }); | |
| 168 | } |
src/crew/dispatch.ts +3
| @@ -44,6 +44,7 @@ import { | ||
| 44 | 44 | listQueued, |
| 45 | 45 | type Run, |
| 46 | 46 | } from "../core/runs.ts"; |
| 47 | import { groupOfIssue } from "../core/variants.ts"; | |
| 47 | 48 | import { getUserById } from "../core/users.ts"; |
| 48 | 49 | import { createLog } from "../kernel/log.ts"; |
| 49 | 50 | import { issueOfBranch, type RunReason } from "./brief.ts"; |
| @@ -281,6 +282,7 @@ export function queueAgentRun( | ||
| 281 | 282 | // forge processes share this database, and both may react to the same |
| 282 | 283 | // thing — a run checked for by one and inserted by the other would be the |
| 283 | 284 | // same work twice (docs/ARCHITECTURE.md, "Two processes at once"). |
| 285 | const group = groupOfIssue(app.db, issue.id); | |
| 284 | 286 | const run = transact(app.db, () => { |
| 285 | 287 | // A follow-up is the builder's: a reviewer's own run on the pull, still |
| 286 | 288 | // running as it posts its verdict, is not the run that hears it. |
| @@ -311,6 +313,7 @@ export function queueAgentRun( | ||
| 311 | 313 | issueId: issue.id, |
| 312 | 314 | actorId: agent.user.id, |
| 313 | 315 | reason, |
| 316 | ...(group === null ? {} : { groupId: group }), | |
| 314 | 317 | ...again, |
| 315 | 318 | }); |
| 316 | 319 | }); |
src/crew/runner.ts +4
| @@ -16,6 +16,7 @@ import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; | ||
| 16 | 16 | import { join } from "node:path"; |
| 17 | 17 | import type { App } from "../app.ts"; |
| 18 | 18 | import { addComment } from "../actions/issues.ts"; |
| 19 | import { groupOfIssue, setPullGroup } from "../core/variants.ts"; | |
| 19 | 20 | import { openPull } from "../actions/pulls.ts"; |
| 20 | 21 | import { forkRepo, sealedForkFor } from "../actions/repos.ts"; |
| 21 | 22 | import { announceRun } from "../actions/runs.ts"; |
| @@ -1561,6 +1562,9 @@ async function landWork( | ||
| 1561 | 1562 | }; |
| 1562 | 1563 | } |
| 1563 | 1564 | write(`opened pull #${opened.value.number}`); |
| 1565 | // A pull made for one variant of a tell joins its siblings (`core/variants.ts`). | |
| 1566 | const group = groupOfIssue(app.db, issue.id); | |
| 1567 | if (group !== null) setPullGroup(app.db, opened.value.id, group); | |
| 1564 | 1568 | return { |
| 1565 | 1569 | pushed: true, |
| 1566 | 1570 | summary: `opened pull #${opened.value.number}${committedForIt ? ", committing its leftover changes" : ""}`, |
src/feed/items.ts +26 −1
| @@ -24,6 +24,7 @@ import { | ||
| 24 | 24 | type Repo, |
| 25 | 25 | } from "../core/repos.ts"; |
| 26 | 26 | import { getUserById, type User } from "../core/users.ts"; |
| 27 | import { openGroups, siblingPulls } from "../core/variants.ts"; | |
| 27 | 28 | import { watchedBy } from "../core/watches.ts"; |
| 28 | 29 | import { askLinesFor } from "../web/studio/asks.ts"; |
| 29 | 30 | |
| @@ -39,7 +40,9 @@ export type FeedKind = | ||
| 39 | 40 | | "tell" |
| 40 | 41 | | "reply" |
| 41 | 42 | | "look" |
| 42 | | "milestone"; | |
| 43 | | "milestone" | |
| 44 | // A deck: sibling variants of one tell waiting for a like or a dislike (#1051). | |
| 45 | | "choose"; | |
| 43 | 46 | |
| 44 | 47 | /** What a candidate is, before it is ranked and worded. */ |
| 45 | 48 | export interface Candidate { |
| @@ -272,6 +275,28 @@ export function candidatesFor( | ||
| 272 | 275 | urgency: 0, |
| 273 | 276 | }); |
| 274 | 277 | } |
| 278 | for (const g of openGroups( | |
| 279 | db, | |
| 280 | writable.map((r) => r.id), | |
| 281 | )) { | |
| 282 | const repo = repos.get(g.repoId); | |
| 283 | const first = siblingPulls(db, g.repoId, g.groupId)[0]; | |
| 284 | if (!repo || !first) continue; | |
| 285 | out.push({ | |
| 286 | id: `choose:${g.groupId}`, | |
| 287 | kind: "choose", | |
| 288 | repoId: repo.id, | |
| 289 | app: slug(repo), | |
| 290 | actorId: null, | |
| 291 | actor: null, | |
| 292 | at: g.at, | |
| 293 | link: `${base(repo)}/choose/${g.groupId}`, | |
| 294 | what: clip( | |
| 295 | `${g.open} ways to do “${first.title.replace(/ \(variant \d+ of \d+\)$/, "")}” are waiting for you to choose one`, | |
| 296 | ), | |
| 297 | urgency: LANDED_URGENCY, | |
| 298 | }); | |
| 299 | } | |
| 275 | 300 | return out; |
| 276 | 301 | } |
| 277 | 302 |
src/http/feed.ts +6 −1
| @@ -26,6 +26,7 @@ import { | ||
| 26 | 26 | previewAsksBulk, |
| 27 | 27 | } from "../actions/asks_bulk.ts"; |
| 28 | 28 | import type { FleetError } from "../actions/fleet.ts"; |
| 29 | import { MAX_VARIANTS, parseVariants } from "../core/variants.ts"; | |
| 29 | 30 | import type { Handler } from "./context.ts"; |
| 30 | 31 | import type { Router } from "./router.ts"; |
| 31 | 32 | import { |
| @@ -160,7 +161,11 @@ export function feedRoutes(router: Router<Handler>): void { | ||
| 160 | 161 | ? getRepo(ctx.app.db, owner, name) |
| 161 | 162 | : undefined; |
| 162 | 163 | if (!repo || !canRead(ctx.app.db, repo, viewer)) return notFound(); |
| 163 | const told = await tell(ctx.app, viewer, repo, text); | |
| 164 | const variants = | |
| 165 | input.variants === undefined ? 1 : parseVariants(input.variants); | |
| 166 | if (variants === undefined) | |
| 167 | return apiError("invalid_variants", 422, `variants is 1 to ${MAX_VARIANTS}`); | |
| 168 | const told = await tell(ctx.app, viewer, repo, text, variants); | |
| 164 | 169 | if (!told.ok) |
| 165 | 170 | return told.error === "forbidden" |
| 166 | 171 | ? apiError("forbidden", 403, DIRECTION_NOTE) |
src/http/mcp.ts +19 −2
| @@ -37,6 +37,7 @@ import { openPlan, parsePlan, PLAN_MAX } from "../actions/plan.ts"; | ||
| 37 | 37 | import { cancelRepoRun } from "../actions/runs.ts"; |
| 38 | 38 | import { clientRows } from "../spawn/clients.ts"; |
| 39 | 39 | import { reissueDoor, spawnDoor } from "../spawn/door.ts"; |
| 40 | import { MAX_VARIANTS, parseVariants } from "../core/variants.ts"; | |
| 40 | 41 | import { tell, toldWords, UNVERIFIED_WORDS } from "../actions/tell.ts"; |
| 41 | 42 | import { readReturns, reportSpend } from "../actions/returns.ts"; |
| 42 | 43 | import { |
| @@ -272,14 +273,30 @@ const TOOLS: readonly Tool[] = [ | ||
| 272 | 273 | "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.", |
| 273 | 274 | inputSchema: { |
| 274 | 275 | type: "object", |
| 275 | properties: { repo: REPO_ARG, text: { type: "string", maxLength: 8000 } }, | |
| 276 | properties: { | |
| 277 | repo: REPO_ARG, | |
| 278 | text: { type: "string", maxLength: 8000 }, | |
| 279 | variants: { | |
| 280 | type: "integer", | |
| 281 | minimum: 1, | |
| 282 | maximum: MAX_VARIANTS, | |
| 283 | description: | |
| 284 | "Ask for this many alternative changes to the same thing; each becomes a sibling pull a person chooses among with likes and dislikes. Default 1.", | |
| 285 | }, | |
| 286 | }, | |
| 276 | 287 | required: ["repo", "text"], |
| 277 | 288 | }, |
| 278 | 289 | writes: true, |
| 279 | 290 | async run(ctx, user, args) { |
| 280 | 291 | const repo = repoArg(ctx, user, args); |
| 281 | 292 | if (isAnswer(repo)) return repo; |
| 282 | const told = await tell(ctx.app, user, repo, str(args["text"])); | |
| 293 | const variants = | |
| 294 | args["variants"] === undefined | |
| 295 | ? 1 | |
| 296 | : parseVariants(args["variants"]); | |
| 297 | if (variants === undefined) | |
| 298 | return bad("invalid", `variants is a whole number from 1 to ${MAX_VARIANTS}`); | |
| 299 | const told = await tell(ctx.app, user, repo, str(args["text"]), variants); | |
| 283 | 300 | if (!told.ok) |
| 284 | 301 | return bad( |
| 285 | 302 | told.error, |
src/http/server.ts +4
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | * verdict. Auth is resolved once per request and handed to handlers as data. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import { deckPage, leanSubmit } from "../web/studio/choose.ts"; | |
| 8 | 9 | import { unsealReleased } from "../actions/sealed.ts"; |
| 9 | 10 | import { capacityPage, capacitySet } from "../web/pages/capacity.ts"; |
| 10 | 11 | import { |
| @@ -873,6 +874,9 @@ export function buildRouter(): Router<Handler> { | ||
| 873 | 874 | // The home's composer without a script: a tell to the app it names (#599). |
| 874 | 875 | router.add("POST", "/tell", composerSubmit); |
| 875 | 876 | router.add("POST", "/:owner/:name/pulls/:number/ship", shipSubmit); |
| 877 | // The variants deck of one tell: look at the siblings, like or dislike each. | |
| 878 | router.add("GET", "/:owner/:name/choose/:group", deckPage); | |
| 879 | router.add("POST", "/:owner/:name/choose/:group/:number/:way", leanSubmit); | |
| 876 | 880 | router.add("POST", "/:owner/:name/pulls/:number/say", saySubmit); |
| 877 | 881 | router.add("POST", "/:owner/:name/pulls/:number/take-back", takeBackSubmit); |
| 878 | 882 | router.add("POST", "/:owner/:name/pulls/:number/hold", holdSubmit); |
src/web/client.ts +30
| @@ -1743,6 +1743,36 @@ export const shellJs = /* js */ ` | ||
| 1743 | 1743 | if (!out) return; |
| 1744 | 1744 | i.addEventListener('input', function () { out.textContent = i.value; }); |
| 1745 | 1745 | }); |
| 1746 | ||
| 1747 | // ---- the variants deck: a swipe is a click on the card's own button ---- | |
| 1748 | var deck = doc.querySelector('[data-deck]'); | |
| 1749 | if (deck) { | |
| 1750 | var lean = function (card, way) { | |
| 1751 | var b = card && card.querySelector('button[name="way"][value="' + way + '"]'); | |
| 1752 | if (b) b.click(); | |
| 1753 | }; | |
| 1754 | deck.querySelectorAll('[data-deck-card]').forEach(function (card) { | |
| 1755 | var x0 = null, y0 = 0; | |
| 1756 | card.addEventListener('pointerdown', function (e) { | |
| 1757 | if (e.pointerType === 'mouse' || e.target.closest('a, button, input')) return; | |
| 1758 | x0 = e.clientX; y0 = e.clientY; | |
| 1759 | }); | |
| 1760 | card.addEventListener('pointerup', function (e) { | |
| 1761 | if (x0 === null) return; | |
| 1762 | var dx = e.clientX - x0, dy = e.clientY - y0; | |
| 1763 | x0 = null; | |
| 1764 | if (Math.abs(dx) > 80 && Math.abs(dx) > 2 * Math.abs(dy)) lean(card, dx > 0 ? 'like' : 'dislike'); | |
| 1765 | }); | |
| 1766 | card.addEventListener('pointercancel', function () { x0 = null; }); | |
| 1767 | }); | |
| 1768 | doc.addEventListener('keydown', function (e) { | |
| 1769 | if (e.metaKey || e.ctrlKey || e.altKey || e.target.closest('input, textarea, select')) return; | |
| 1770 | var way = e.key === 'ArrowRight' ? 'like' : e.key === 'ArrowLeft' ? 'dislike' : null; | |
| 1771 | if (!way) return; | |
| 1772 | var card = deck.querySelector('[data-deck-card][data-undecided]'); | |
| 1773 | if (card) { e.preventDefault(); lean(card, way); } | |
| 1774 | }); | |
| 1775 | } | |
| 1746 | 1776 | })(); |
| 1747 | 1777 | `; |
| 1748 | 1778 |
src/web/studio/actions.ts +3 −1
| @@ -12,6 +12,7 @@ import { mayView } from "../../core/sealed.ts"; | ||
| 12 | 12 | import { holdPull, releasePull } from "../../actions/holds.ts"; |
| 13 | 13 | import { addComment } from "../../actions/issues.ts"; |
| 14 | 14 | import { tell } from "../../actions/tell.ts"; |
| 15 | import { parseVariants } from "../../core/variants.ts"; | |
| 15 | 16 | import { DIRECTION_NOTE } from "../../crew/direct.ts"; |
| 16 | 17 | import { mergePull } from "../../actions/pulls.ts"; |
| 17 | 18 | import { revertPull, type RevertPullError } from "../../actions/revert.ts"; |
| @@ -52,7 +53,8 @@ async function tellFrom( | ||
| 52 | 53 | tone: "danger", |
| 53 | 54 | message: "Say something first.", |
| 54 | 55 | }); |
| 55 | const told = await tell(ctx.app, ctx.auth.user, repo, text); | |
| 56 | const variants = parseVariants(field(form, "variants")) ?? 1; | |
| 57 | const told = await tell(ctx.app, ctx.auth.user, repo, text, variants); | |
| 56 | 58 | if (!told.ok) |
| 57 | 59 | return flashTo(ctx, back, { |
| 58 | 60 | tone: "danger", |
src/web/studio/app.ts +4
| @@ -299,6 +299,10 @@ export async function studioApp( | ||
| 299 | 299 | ${builders.length === 1 ? "takes it" : "share the work"}`} |
| 300 | 300 | </span> |
| 301 | 301 | <span class="spacer"></span> |
| 302 | <label class="muted small" for="variants">Ways</label> | |
| 303 | <select class="input" id="variants" name="variants" aria-label="How many alternatives to make"> | |
| 304 | ${[1, 2, 3, 4].map((n) => html`<option value="${n}">${n}</option>`)} | |
| 305 | </select> | |
| 302 | 306 | ${btn("Tell the crew", { variant: "primary", type: "submit" })} |
| 303 | 307 | </div> |
| 304 | 308 | </form>`; |
src/web/studio/choose.ts added +156
| @@ -0,0 +1,156 @@ | ||
| 1 | /** | |
| 2 | * The deck: the sibling pulls of one tell, one card each — its picture and | |
| 3 | * its preview — to like or dislike. Swiping right or pressing → likes the | |
| 4 | * first undecided card, left or ← dislikes it; every card also has the two | |
| 5 | * buttons, so nothing needs a script. The last liked change gets Ship. | |
| 6 | */ | |
| 7 | ||
| 8 | import { leanOn, type LeanError } from "../../actions/choose.ts"; | |
| 9 | import { getAppByRepo } from "../../core/apps.ts"; | |
| 10 | import { getPull, type Pull } from "../../core/pulls.ts"; | |
| 11 | import { canWrite, type Repo } from "../../core/repos.ts"; | |
| 12 | import { siblingPulls, leansOf, type Lean } from "../../core/variants.ts"; | |
| 13 | import { mayView } from "../../core/sealed.ts"; | |
| 14 | import type { Ctx } from "../../http/context.ts"; | |
| 15 | import { | |
| 16 | crumbsFor, | |
| 17 | loadRepo, | |
| 18 | notFoundPage, | |
| 19 | repoBase, | |
| 20 | repoHeader, | |
| 21 | repoPalette, | |
| 22 | signInFirst, | |
| 23 | } from "../chrome.ts"; | |
| 24 | import { field, readForm } from "../form.ts"; | |
| 25 | import { html, when, type Html } from "../html.ts"; | |
| 26 | import { flashTo, page } from "../layout.ts"; | |
| 27 | import { previewCard } from "../pages/apps.ts"; | |
| 28 | import { lookCard } from "../pages/looks.ts"; | |
| 29 | import { btn, card } from "../ui.ts"; | |
| 30 | import { changeHref } from "./nouns.ts"; | |
| 31 | ||
| 32 | const GROUP = /^[0-9a-z]{16,40}$/; | |
| 33 | ||
| 34 | export const deckHref = (repo: Repo, groupId: string): string => | |
| 35 | `${repoBase(repo)}/choose/${groupId}`; | |
| 36 | ||
| 37 | /** GET /:owner/:name/choose/:group — the deck. */ | |
| 38 | export function deckPage(ctx: Ctx): Response { | |
| 39 | const repo = loadRepo(ctx); | |
| 40 | const group = ctx.params.group ?? ""; | |
| 41 | if (!repo || !GROUP.test(group)) return notFoundPage(ctx); | |
| 42 | const pulls = siblingPulls(ctx.app.db, repo.id, group).filter((p) => | |
| 43 | mayView(ctx.app.db, repo, p, ctx.auth.user), | |
| 44 | ); | |
| 45 | if (pulls.length === 0) return notFoundPage(ctx); | |
| 46 | const writer = canWrite(ctx.app.db, repo, ctx.auth.user); | |
| 47 | const leans = leansOf(ctx.app.db, repo.id, group); | |
| 48 | const record = getAppByRepo(ctx.app.db, repo.id); | |
| 49 | const open = pulls.filter((p) => p.state === "open" && p.mergedAt === null); | |
| 50 | const liked = open | |
| 51 | .filter((p) => leans.get(p.id)?.answer === "like") | |
| 52 | .sort((a, b) => | |
| 53 | leans.get(b.id)!.createdAt.localeCompare(leans.get(a.id)!.createdAt), | |
| 54 | ); | |
| 55 | const shippable = liked[0]; | |
| 56 | const base = deckHref(repo, group); | |
| 57 | const form = (p: Pull, way: Lean, label: string, variant: "primary" | "secondary") => | |
| 58 | html`<form method="post" action="${base}/${p.number}/${way}" class="deck-lean"> | |
| 59 | ${way === "dislike" | |
| 60 | ? html`<input class="input" name="reason" maxlength="200" aria-label="Why not (one line)" placeholder="Why not? (one line, the builder reads it)" />` | |
| 61 | : null} | |
| 62 | ${btn(label, { variant, type: "submit", name: "way", value: way })} | |
| 63 | </form>`; | |
| 64 | const cardOf = (p: Pull): Html => { | |
| 65 | const lean = leans.get(p.id)?.answer; | |
| 66 | const isOpen = open.includes(p); | |
| 67 | const status = p.mergedAt !== null | |
| 68 | ? "shipped" | |
| 69 | : !isOpen | |
| 70 | ? "closed" | |
| 71 | : lean === "like" | |
| 72 | ? "liked" | |
| 73 | : "undecided"; | |
| 74 | return html`<section | |
| 75 | class="deck-card" | |
| 76 | data-deck-card | |
| 77 | ${status === "undecided" ? html`data-undecided` : null} | |
| 78 | > | |
| 79 | <h2><a href="${changeHref(repoBase(repo), p.number)}">${p.title}</a> | |
| 80 | <span class="muted small">#${p.number} · ${status}</span></h2> | |
| 81 | ${isOpen && record | |
| 82 | ? html`${lookCard(ctx, repo, record, p, p.headOid)}${previewCard(ctx, repo, p)}` | |
| 83 | : null} | |
| 84 | ${when(isOpen && writer && status !== "liked", () => | |
| 85 | html`<div class="bar">${form(p, "dislike", "Dislike", "secondary")}${form(p, "like", "Like", "primary")}</div>`, | |
| 86 | )} | |
| 87 | ${when(isOpen && writer && status === "liked" && p !== shippable, () => | |
| 88 | html`<div class="bar">${form(p, "dislike", "Dislike", "secondary")}</div>`, | |
| 89 | )} | |
| 90 | ${when(isOpen && writer && p === shippable, () => | |
| 91 | html`<div class="bar"> | |
| 92 | ${form(p, "dislike", "Dislike", "secondary")} | |
| 93 | <form method="post" action="${repoBase(repo)}/pulls/${p.number}/ship"> | |
| 94 | ${btn("Ship", { variant: "primary", type: "submit" })} | |
| 95 | </form> | |
| 96 | </div>`, | |
| 97 | )} | |
| 98 | </section>`; | |
| 99 | }; | |
| 100 | const body = html`${repoHeader(ctx, repo, "pulls", { compact: true, ownTitle: true })} | |
| 101 | <div data-deck> | |
| 102 | <h1>Choose one</h1> | |
| 103 | <p class="muted small"> | |
| 104 | ${pulls.length} ways to do the same thing. Swipe right or press → to | |
| 105 | like the next one, left or ← to dislike it; the last one you like can | |
| 106 | ship. | |
| 107 | </p> | |
| 108 | ${card(html`${pulls.map(cardOf)}`, { extra: "deck" })} | |
| 109 | </div>`; | |
| 110 | return page({ | |
| 111 | ctx, | |
| 112 | title: `Choose one · ${repo.name}`, | |
| 113 | crumbs: crumbsFor(repo), | |
| 114 | palette: repoPalette(ctx, repo), | |
| 115 | body, | |
| 116 | }); | |
| 117 | } | |
| 118 | ||
| 119 | const WORDS: Readonly<Record<LeanError, string>> = { | |
| 120 | unauthorized: "Sign in first.", | |
| 121 | forbidden: "Only a person who can write here chooses.", | |
| 122 | not_found: "That change was not found.", | |
| 123 | not_a_variant: "That change is not one of several.", | |
| 124 | not_open: "That change is no longer open.", | |
| 125 | }; | |
| 126 | ||
| 127 | /** POST /:owner/:name/choose/:group/:number/:way — like or dislike one sibling. */ | |
| 128 | export async function leanSubmit(ctx: Ctx): Promise<Response> { | |
| 129 | const repo = loadRepo(ctx); | |
| 130 | if (!repo) return notFoundPage(ctx); | |
| 131 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 132 | const form = await readForm(ctx, "rw"); | |
| 133 | if (form instanceof Response) return form; | |
| 134 | const way = ctx.params.way; | |
| 135 | const group = ctx.params.group ?? ""; | |
| 136 | const number = Number(ctx.params.number); | |
| 137 | const pull = Number.isInteger(number) | |
| 138 | ? getPull(ctx.app.db, repo.id, number) | |
| 139 | : undefined; | |
| 140 | if ( | |
| 141 | (way !== "like" && way !== "dislike") || | |
| 142 | !GROUP.test(group) || | |
| 143 | !pull || | |
| 144 | pull.groupId !== group || | |
| 145 | !mayView(ctx.app.db, repo, pull, ctx.auth.user) | |
| 146 | ) | |
| 147 | return notFoundPage(ctx); | |
| 148 | const done = leanOn(ctx.app, ctx.auth.user, repo, pull, way, field(form, "reason")); | |
| 149 | return flashTo( | |
| 150 | ctx, | |
| 151 | deckHref(repo, group), | |
| 152 | done.ok | |
| 153 | ? { message: way === "like" ? `Liked #${pull.number}.` : `Closed #${pull.number}.` } | |
| 154 | : { tone: "danger", message: WORDS[done.error] }, | |
| 155 | ); | |
| 156 | } |
src/web/studio/feed.ts +1
| @@ -38,6 +38,7 @@ const KIND_LABEL: Record<FeedItem["kind"], string> = { | ||
| 38 | 38 | tell: "Told", |
| 39 | 39 | reply: "Reply", |
| 40 | 40 | look: "Look", |
| 41 | choose: "Choose", | |
| 41 | 42 | }; |
| 42 | 43 | |
| 43 | 44 | /** |
test/variants.test.ts added +220
| @@ -0,0 +1,220 @@ | ||
| 1 | /** | |
| 2 | * Variants of one intent (#1051): a tell that asks for n alternatives opens | |
| 3 | * n sibling issues under one group id; their pulls are chosen among with | |
| 4 | * likes (ledger `lean`, answer like) and dislikes (the pull closes with a | |
| 5 | * one-line reason), in a deck, and the deck is a feed item of type `choose`. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 13 | import { leanOn } from "../src/actions/choose.ts"; | |
| 14 | import { openPull } from "../src/actions/pulls.ts"; | |
| 15 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 16 | import { listCrewRoles } from "../src/core/agents.ts"; | |
| 17 | import { queueAgentRun } from "../src/crew/dispatch.ts"; | |
| 18 | import { tell } from "../src/actions/tell.ts"; | |
| 19 | import { listComments, getIssueById } from "../src/core/issues.ts"; | |
| 20 | import { listDecisions } from "../src/core/decisions.ts"; | |
| 21 | import { getPull } from "../src/core/pulls.ts"; | |
| 22 | import type { Repo } from "../src/core/repos.ts"; | |
| 23 | import { createToken } from "../src/core/tokens.ts"; | |
| 24 | import { createUser, type User } from "../src/core/users.ts"; | |
| 25 | import { | |
| 26 | groupOfIssue, | |
| 27 | parseVariants, | |
| 28 | setPullGroup, | |
| 29 | siblingPulls, | |
| 30 | } from "../src/core/variants.ts"; | |
| 31 | import { candidatesFor } from "../src/feed/items.ts"; | |
| 32 | import { forgeHttp, type ForgeHttp } from "../src/http/server.ts"; | |
| 33 | import { listRuns } from "../src/core/runs.ts"; | |
| 34 | ||
| 35 | let dataDir: string; | |
| 36 | let app: App; | |
| 37 | let http: ForgeHttp; | |
| 38 | let alice: User; | |
| 39 | let mallory: User; | |
| 40 | let repo: Repo; | |
| 41 | let group: string; | |
| 42 | let numbers: number[]; | |
| 43 | ||
| 44 | beforeAll(async () => { | |
| 45 | dataDir = await mkdtemp(join(tmpdir(), "forge-variants-")); | |
| 46 | app = createApp({ dataDir, port: 0 }); | |
| 47 | http = forgeHttp(app); | |
| 48 | const mk = async (username: string, first = false): Promise<User> => { | |
| 49 | const u = await createUser(app.db, { | |
| 50 | trusted: true, | |
| 51 | username, | |
| 52 | email: `${username}@forge.local`, | |
| 53 | password: "a-good-passphrase", | |
| 54 | adminIfFirst: first, | |
| 55 | }); | |
| 56 | if (!u.ok) throw new Error(u.error); | |
| 57 | return u.value; | |
| 58 | }; | |
| 59 | alice = await mk("alice", true); | |
| 60 | mallory = await mk("mallory"); | |
| 61 | const b = await createAgent(app, alice, { username: "bot", brief: "Be brief." }); | |
| 62 | if (!b.ok) throw new Error(String(b.error)); | |
| 63 | const created = await createRepo(app, alice, { name: "square", visibility: "public" }); | |
| 64 | if (!created.ok) throw new Error(String(created.error)); | |
| 65 | repo = created.value; | |
| 66 | const seeded = await commitToBranch(app, alice, repo, { | |
| 67 | message: "Start", | |
| 68 | files: [{ path: "README.md", content: "# square\n" }], | |
| 69 | }); | |
| 70 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 71 | expect(addToCrew(app, alice, repo, "bot").ok).toBe(true); | |
| 72 | }, 30_000); | |
| 73 | ||
| 74 | afterAll(async () => { | |
| 75 | app?.close(); | |
| 76 | await rm(dataDir, { recursive: true, force: true }); | |
| 77 | }); | |
| 78 | ||
| 79 | describe("variants: n", () => { | |
| 80 | test("is a whole number from 1 to 4, nothing else", () => { | |
| 81 | expect(parseVariants(3)).toBe(3); | |
| 82 | expect(parseVariants("2")).toBe(2); | |
| 83 | for (const bad of [0, 5, 1.5, -1, "x", "", null, undefined, "2; drop"]) | |
| 84 | expect(parseVariants(bad)).toBeUndefined(); | |
| 85 | }); | |
| 86 | ||
| 87 | test("a tell for three opens three siblings under the first one's id, each with a run", async () => { | |
| 88 | const told = await tell(app, alice, repo, "Make the header bolder", 3); | |
| 89 | if (!told.ok) throw new Error(told.error); | |
| 90 | const all = [told.value.issue, ...told.value.siblings]; | |
| 91 | expect(all).toHaveLength(3); | |
| 92 | group = told.value.issue.id; | |
| 93 | numbers = all.map((i) => i.number); | |
| 94 | for (const i of all) expect(groupOfIssue(app.db, i.id)).toBe(group); | |
| 95 | expect(all[1]!.title).toContain("variant 2 of 3"); | |
| 96 | expect(all[1]!.title.length).toBeLessThanOrEqual(80); | |
| 97 | // The runs the crew queues for them carry the group too. | |
| 98 | const bot = listCrewRoles(app.db, repo.id).find((a) => a.agent.user.username === "bot")!.agent; | |
| 99 | for (const i of all) queueAgentRun(app, repo, i, bot, "assigned"); | |
| 100 | const runs = listRuns(app.db, repo.id, { kind: "agent" }); | |
| 101 | expect(runs.filter((r) => r.groupId === group)).toHaveLength(3); | |
| 102 | }); | |
| 103 | ||
| 104 | test("a plain tell has no group", async () => { | |
| 105 | const told = await tell(app, alice, repo, "One thing"); | |
| 106 | if (!told.ok) throw new Error(told.error); | |
| 107 | expect(told.value.siblings).toHaveLength(0); | |
| 108 | expect(groupOfIssue(app.db, told.value.issue.id)).toBeNull(); | |
| 109 | }); | |
| 110 | }); | |
| 111 | ||
| 112 | describe("the deck", () => { | |
| 113 | const pullOf = async (n: number) => { | |
| 114 | const branch = `bot/${n}-variant`; | |
| 115 | const c = await commitToBranch(app, alice, repo, { | |
| 116 | branch, | |
| 117 | base: "main", | |
| 118 | message: `variant ${n}`, | |
| 119 | files: [{ path: `v${n}.txt`, content: `${n}\n` }], | |
| 120 | }); | |
| 121 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 122 | const opened = await openPull(app, alice, repo, { | |
| 123 | title: `Bolder ${n}`, | |
| 124 | body: `Closes #${n}`, | |
| 125 | baseBranch: "main", | |
| 126 | headBranch: branch, | |
| 127 | }); | |
| 128 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 129 | setPullGroup(app.db, opened.value.id, group); | |
| 130 | return getPull(app.db, repo.id, opened.value.number)!; | |
| 131 | }; | |
| 132 | let pulls: Awaited<ReturnType<typeof pullOf>>[] = []; | |
| 133 | ||
| 134 | beforeAll(async () => { | |
| 135 | for (const n of numbers) pulls.push(await pullOf(n)); | |
| 136 | }); | |
| 137 | ||
| 138 | test("the pulls are siblings of the tell", () => { | |
| 139 | expect(siblingPulls(app.db, repo.id, group).map((p) => p.number)).toEqual( | |
| 140 | pulls.map((p) => p.number), | |
| 141 | ); | |
| 142 | expect(pulls.every((p) => p.groupId === group)).toBe(true); | |
| 143 | }); | |
| 144 | ||
| 145 | test("it is a feed item of type choose, for a writer only", () => { | |
| 146 | const mine = candidatesFor(app.db, alice, Date.now()).filter((c) => c.kind === "choose"); | |
| 147 | expect(mine.map((c) => c.id)).toEqual([`choose:${group}`]); | |
| 148 | expect(mine[0]!.link).toBe(`/alice/square/choose/${group}`); | |
| 149 | expect(candidatesFor(app.db, mallory, Date.now()).some((c) => c.kind === "choose")).toBe(false); | |
| 150 | }); | |
| 151 | ||
| 152 | test("a like is a ledger row of kind lean, answer like", () => { | |
| 153 | const done = leanOn(app, alice, repo, pulls[0]!, "like"); | |
| 154 | expect(done.ok).toBe(true); | |
| 155 | const rows = listDecisions(app.db, repo.id, { kind: "lean" }); | |
| 156 | expect(rows).toHaveLength(1); | |
| 157 | expect(rows[0]).toMatchObject({ answer: "like", issueId: pulls[0]!.id, by: "alice" }); | |
| 158 | expect(getPull(app.db, repo.id, pulls[0]!.number)!.state).toBe("open"); | |
| 159 | }); | |
| 160 | ||
| 161 | test("a dislike closes the pull and its issue with a one-line reason the builder sees", () => { | |
| 162 | const done = leanOn(app, alice, repo, pulls[1]!, "dislike", "too\n loud\t for the page"); | |
| 163 | expect(done.ok).toBe(true); | |
| 164 | expect(getPull(app.db, repo.id, pulls[1]!.number)!.state).toBe("closed"); | |
| 165 | const says = listComments(app.db, pulls[1]!.id).map((c) => c.body); | |
| 166 | expect(says.some((b) => b.includes("too loud for the page") && !b.includes("\n"))).toBe(true); | |
| 167 | const issue = getIssueById(app.db, siblingIssueId(numbers[1]!))!; | |
| 168 | expect(issue.state).toBe("closed"); | |
| 169 | expect(listDecisions(app.db, repo.id, { kind: "lean" })[0]!.answer).toBe("dislike"); | |
| 170 | }); | |
| 171 | ||
| 172 | test("a reader, an agent and a closed pull cannot choose", () => { | |
| 173 | expect(leanOn(app, mallory, repo, pulls[2]!, "like")).toEqual({ ok: false, error: "forbidden" }); | |
| 174 | expect(leanOn(app, undefined, repo, pulls[2]!, "like").ok).toBe(false); | |
| 175 | const closed = getPull(app.db, repo.id, pulls[1]!.number)!; | |
| 176 | expect(leanOn(app, alice, repo, closed, "like")).toEqual({ ok: false, error: "not_open" }); | |
| 177 | }); | |
| 178 | ||
| 179 | test("the page shows a card per open sibling, Ship on the last liked one, and refuses strangers' posts", async () => { | |
| 180 | const token = createToken(app.db, alice.id, "t", "rw").token; | |
| 181 | const h = { authorization: `Bearer ${token}` }; | |
| 182 | const origin = "http://localhost"; | |
| 183 | const url = `${origin}/alice/square/choose/${group}`; | |
| 184 | const fetch = (u: string, init?: RequestInit) => http.handle(new Request(u, init)); | |
| 185 | const res = await fetch(url, { headers: h }); | |
| 186 | expect(res.status).toBe(200); | |
| 187 | const text = await res.text(); | |
| 188 | expect(text).toContain("Bolder " + numbers[0]); | |
| 189 | expect(text).toContain("data-deck-card"); | |
| 190 | expect(text).toContain(`/pulls/${pulls[0]!.number}/ship`); | |
| 191 | expect(text).not.toContain(`/pulls/${pulls[2]!.number}/ship`); | |
| 192 | // A like through the page; a made-up group is not found. | |
| 193 | const liked = await fetch(`${url}/${pulls[2]!.number}/like`, { | |
| 194 | method: "POST", | |
| 195 | headers: h, | |
| 196 | redirect: "manual", | |
| 197 | }); | |
| 198 | expect(liked.status).toBe(303); | |
| 199 | const rows = listDecisions(app.db, repo.id, { kind: "lean" }); | |
| 200 | expect(rows[0]).toMatchObject({ answer: "like", issueId: pulls[2]!.id }); | |
| 201 | const after = await (await fetch(url, { headers: h })).text(); | |
| 202 | // The newest like is the one that ships. | |
| 203 | expect(after).toContain(`/pulls/${pulls[2]!.number}/ship`); | |
| 204 | expect(after).not.toContain(`/pulls/${pulls[0]!.number}/ship`); | |
| 205 | const nope = await fetch(`${origin}/alice/square/choose/${"z".repeat(26)}`, { headers: h }); | |
| 206 | expect(nope.status).toBe(404); | |
| 207 | const cross = await fetch(`${url}/${pulls[0]!.number}/dislike`, { | |
| 208 | method: "POST", | |
| 209 | headers: { ...h, origin: "https://evil.example" }, | |
| 210 | }); | |
| 211 | expect(cross.status).toBe(403); | |
| 212 | }); | |
| 213 | }); | |
| 214 | ||
| 215 | function siblingIssueId(number: number): string { | |
| 216 | return app.db | |
| 217 | .query<{ id: string }, [string, number]>("SELECT id FROM issue WHERE repo_id = ?1 AND number = ?2") | |
| 218 | .get(repo.id, number)!.id; | |
| 219 | } | |
| 220 |
new above
offline