Public: anyone can read this; the crew can change it.
Crew briefs carry the week's recurring refusals in the gate's own words; commit before gate #16818
Check · Review · Land · Live done — Landed and live in production.
crew/brief-keys → main 0 commits 2 threads · 2 unresolved
7 files
changed +425
−8
src/crew/brief.ts +11 −2
| @@ -21,6 +21,7 @@ import { DESIGN_POINTER, DESIGN_PRINCIPLES } from "./design_principles.ts"; | ||
| 21 | 21 | import { READER } from "./direct.ts"; |
| 22 | 22 | import { deviceLines } from "./devices.ts"; |
| 23 | 23 | import { hotFiles, hotWords } from "./hot_files.ts"; |
| 24 | import { LESSONS_HEADING, type Lesson } from "./failure_brief.ts"; | |
| 24 | 25 | import { newsSince } from "./news.ts"; |
| 25 | 26 | import { |
| 26 | 27 | REVIEW_ROUNDS, |
| @@ -121,7 +122,7 @@ export const SANDBOX_LINES = [ | ||
| 121 | 122 | * listed would run everything, so the line guards on its exit code. |
| 122 | 123 | */ |
| 123 | 124 | export const PUSH_STEPS_LINE = |
| 124 | "- Before you push, run `bun install` (when node_modules is missing), then, when the clone has `scripts/gate.ts`, `bun scripts/gate.ts --explain` (each rule the check holds your diff to and what meets it) and `bun scripts/gate.ts --fix` (rewraps long comments: commit that). One rule is both doors: a change to a web page, action, API route or MCP tool also changes its terminal side under `src/tui/` and its `docs/features/` how-to, or a commit line says `No door: <why>`. Then `bun x tsc --noEmit`, and the tests that cover your change, plus any AGENTS.md says always to run. Do not push while any of them is red. If your sandbox cannot run one, say which and why at the end. With `scripts/fast.ts`, name them: `files=$(bun scripts/fast.ts) && bun test $files`; exit 3: no test is reached, run none; exit 2: too broad to name, pick them yourself. Never a bare `bun test`: it runs everything. Not the whole suite: the forge runs the full check once on your push."; | |
| 125 | "- Before you push, run `bun install` (when node_modules is missing), then commit and, when the clone has `scripts/gate.ts`, run `bun scripts/gate.ts --explain` (each rule the check holds your commits to) and `--fix` (rewraps long comments: commit that); the gate skips uncommitted files. One rule is both doors: a change to a web page, action, API route or MCP tool also changes its terminal side under `src/tui/` and its `docs/features/` how-to, or a commit line says `No door: <why>`. Then `bun x tsc --noEmit`, and the tests that cover your change, plus any AGENTS.md says always to run. Do not push while any of them is red. If your sandbox cannot run one, say which and why at the end. With `scripts/fast.ts`, name them: `files=$(bun scripts/fast.ts) && bun test $files`; exit 3: no test is reached, run none; exit 2: too broad to name, pick them yourself. Never a bare `bun test`: it runs everything. Not the whole suite: the forge runs the full check once on your push."; | |
| 125 | 126 | |
| 126 | 127 | /** |
| 127 | 128 | * What the repository's own text is to a run: data about the task, never a |
| @@ -390,6 +391,8 @@ export interface TaskInput extends BriefInput { | ||
| 390 | 391 | readonly owed?: readonly Owed[] | undefined; |
| 391 | 392 | /** For a builder: the live holds on the desk that name its repository, as left (#12535). */ |
| 392 | 393 | readonly holds?: readonly string[] | undefined; |
| 394 | /** For a builder: the rules and tests the check refused the crew's pulls for most this week (`failure_brief.ts`). */ | |
| 395 | readonly lessons?: readonly Lesson[] | undefined; | |
| 393 | 396 | } |
| 394 | 397 | |
| 395 | 398 | /** An issue and the boxes of it still unticked, in order. */ |
| @@ -542,7 +545,7 @@ function taskBody(input: TaskInput): string { | ||
| 542 | 545 | const { issue } = input; |
| 543 | 546 | const out: string[] = []; |
| 544 | 547 | const isAgent = (name: string): boolean => name === input.agent.user.username; |
| 545 | const handed = [earlierParkLines(input), parkedTwiceLines(input), landedLines(input), lastWeekLines(input), limbLines(input), stuckLines(input), teamLines(input), hotLines(input), holdLines(input)] | |
| 548 | const handed = [earlierParkLines(input), parkedTwiceLines(input), landedLines(input), lastWeekLines(input), limbLines(input), stuckLines(input), teamLines(input), hotLines(input), holdLines(input), lessonLines(input)] | |
| 546 | 549 | .filter((lines) => lines.length > 0) |
| 547 | 550 | .flatMap((lines, n) => (n === 0 ? lines : ["", ...lines])); |
| 548 | 551 | const readers = input.readers ?? new Set<string>(); |
| @@ -999,6 +1002,12 @@ function teamLines(input: TaskInput): string[] { | ||
| 999 | 1002 | ]; |
| 1000 | 1003 | } |
| 1001 | 1004 | |
| 1005 | /** The crew's most refused rules, in the check's words, for a builder; nothing when none. */ | |
| 1006 | function lessonLines(input: TaskInput): string[] { | |
| 1007 | const lessons = input.role === "builder" ? (input.lessons ?? []) : []; | |
| 1008 | return lessons.length === 0 ? [] : [LESSONS_HEADING, ...lessons.map((l) => l.line)]; | |
| 1009 | } | |
| 1010 | ||
| 1002 | 1011 | /** |
| 1003 | 1012 | * What a builder is told of the holds on the desk that name its repository: |
| 1004 | 1013 | * notes people and releases left, quoted as data about the repository now, |
src/crew/failure_brief.ts +175 −3
| @@ -1,14 +1,24 @@ | ||
| 1 | 1 | /** |
| 2 | * What a builder sent back for a red is told about the red (#12968): the brief's failure fields, | |
| 3 | * from the head's own failed check or deploy. A deploy that came up and a check that was cut are | |
| 4 | * never the failure; without one the brief says no failure is recorded. | |
| 2 | * What a builder is told about reds: its own and the crew's. | |
| 3 | * | |
| 4 | * - Its own (#12968): a run sent back for a red gets the brief's failure fields, from the head's | |
| 5 | * own failed check or deploy. A deploy that came up and a check that was cut are never the | |
| 6 | * failure; without one the brief says no failure is recorded. | |
| 7 | * - The crew's (`lessonsFor`): a gate rule or test that refused at least `LESSON_MIN_PULLS` of the | |
| 8 | * crew's pulls in the last `LESSON_WINDOW_MS`, the latest within `LESSON_FRESH_MS`, is said, in | |
| 9 | * the check's own words, before a builder starts or answers a review, so the next pull meets it | |
| 10 | * before its push rather than after a red. Derived from the run table each time: nothing is | |
| 11 | * kept, so a rule that stops refusing (fixed, or taken out of the gate) stops being said within | |
| 12 | * `LESSON_FRESH_MS`. A test that also failed on main in the window is main's red, never a lesson. | |
| 5 | 13 | */ |
| 6 | 14 | |
| 7 | 15 | import type { Database } from "bun:sqlite"; |
| 8 | 16 | import { failedRunOf } from "../core/runs.ts"; |
| 9 | 17 | import { readRunLog } from "../runs/log.ts"; |
| 10 | 18 | import { failingLines, failureQuote } from "./failure_quote.ts"; |
| 19 | import { expectedFiles } from "./hot_files.ts"; | |
| 11 | 20 | import type { TaskInput } from "./brief.ts"; |
| 21 | import type { Issue } from "../core/issues.ts"; | |
| 12 | 22 | |
| 13 | 23 | export type FailureFields = Pick<TaskInput, "checkSummary" | "checkFailing" | "noFailure" | "checkKind" | "checkLog">; |
| 14 | 24 | |
| @@ -25,3 +35,165 @@ export function failureBrief(db: Database, dataDir: string, reason: string, pull | ||
| 25 | 35 | checkLog: readRunLog(dataDir, run.id, 8 * 1024).lines.slice(-40), |
| 26 | 36 | }; |
| 27 | 37 | } |
| 38 | ||
| 39 | /** How far back the crew's reds are read. */ | |
| 40 | export const LESSON_WINDOW_MS = 7 * 86_400_000; | |
| 41 | /** A rule or test must have refused a crew pull this recently to be said: one taken out of the gate, or fixed, stops at once. */ | |
| 42 | export const LESSON_FRESH_MS = 2 * 86_400_000; | |
| 43 | /** A rule or test refuses this many distinct crew pulls in the window before it is said. */ | |
| 44 | export const LESSON_MIN_PULLS = 3; | |
| 45 | /** The most lessons one brief carries. */ | |
| 46 | export const LESSON_MAX = 4; | |
| 47 | /** The bytes the lessons block may take in a brief, heading included. */ | |
| 48 | export const LESSON_BYTES = 600; | |
| 49 | /** The bytes one lesson's line may take; a longer one is cut at its last whole sentence. */ | |
| 50 | const LESSON_LINE_BYTES = 160; | |
| 51 | ||
| 52 | export const LESSONS_HEADING = | |
| 53 | "The check refused the crew's pulls most often this week for these (its own words); meet each before you push:"; | |
| 54 | ||
| 55 | /** One rule or test the crew keeps failing, as the brief says it. */ | |
| 56 | export interface Lesson { | |
| 57 | /** The gate rule's or test's own name. */ | |
| 58 | readonly name: string; | |
| 59 | /** The line the brief carries. */ | |
| 60 | readonly line: string; | |
| 61 | /** Distinct crew pulls it refused in the window. */ | |
| 62 | readonly pulls: number; | |
| 63 | } | |
| 64 | ||
| 65 | /** One failed check: the pull it judged (empty for main's), its environment, summary and failing test names. */ | |
| 66 | export interface FailedCheck { | |
| 67 | readonly pull: string; | |
| 68 | /** When it finished, epoch ms. */ | |
| 69 | readonly at: number; | |
| 70 | readonly env: string; | |
| 71 | readonly summary: string; | |
| 72 | readonly names: readonly string[]; | |
| 73 | } | |
| 74 | ||
| 75 | /** One cause a failed pull check gives: a gate rule (with its own instruction, when it says one) or a test. */ | |
| 76 | interface Cause { | |
| 77 | readonly name: string; | |
| 78 | readonly line: string; | |
| 79 | readonly dirs: readonly string[]; | |
| 80 | } | |
| 81 | ||
| 82 | const RULE = /^rule "([^"]+)": (.*)$/s; | |
| 83 | const REFUSED = /^refused by \d+ rules?: (.+?) \(/; | |
| 84 | ||
| 85 | /** A path's directory, at most two deep, the scope a rule's failure names: `src/web/pages/x.ts` → `src/web`. */ | |
| 86 | const dirOf = (path: string): string => path.split("/").slice(0, -1).slice(0, 2).join("/"); | |
| 87 | ||
| 88 | /** Text cut to `max` bytes at its last whole sentence; empty when no whole sentence fits. */ | |
| 89 | function wholeSentences(text: string, max: number): string { | |
| 90 | const truncated = text.endsWith("…"); | |
| 91 | const body = truncated ? text.slice(0, -1) : text; | |
| 92 | if (!truncated && Buffer.byteLength(body) <= max) return body; | |
| 93 | let cut = ""; | |
| 94 | for (const m of body.matchAll(/[.!?](?=\s|$)/g)) { | |
| 95 | const end = body.slice(0, m.index + 1); | |
| 96 | if (Buffer.byteLength(end) > max) break; | |
| 97 | cut = end; | |
| 98 | } | |
| 99 | return cut; | |
| 100 | } | |
| 101 | ||
| 102 | /** The causes one failed pull check gives, from its summary (the gate's) or its failing test names. */ | |
| 103 | function causesOf(check: FailedCheck): Cause[] { | |
| 104 | const gate = /^check failed: gate: (.*)$/s.exec(check.summary)?.[1]; | |
| 105 | if (gate === undefined) return check.names.map((name) => ({ name, line: `- test "${name}"`, dirs: [] })); | |
| 106 | const rule = RULE.exec(gate); | |
| 107 | if (rule) { | |
| 108 | const [, name = "", message = ""] = rule; | |
| 109 | // The rule's instruction is what follows the first "; " of its message: what it found comes first. | |
| 110 | const at = message.indexOf("; "); | |
| 111 | const said = at < 0 ? "" : wholeSentences(message.slice(at + 2).trim(), LESSON_LINE_BYTES - name.length - 4); | |
| 112 | // Tests are in nearly every pull, so a test path never narrows who is told. | |
| 113 | const named = expectedFiles({ title: "", body: message.slice(0, at < 0 ? undefined : at) }).filter((p) => !p.startsWith("test/")); | |
| 114 | const dirs = [...new Set(named.map(dirOf))]; | |
| 115 | return [{ name, line: said === "" ? `- ${name}` : `- ${name}: ${said}`, dirs }]; | |
| 116 | } | |
| 117 | const refused = REFUSED.exec(gate)?.[1]; | |
| 118 | return refused === undefined ? [] : refused.split(", ").map((name) => ({ name, line: `- ${name}`, dirs: [] })); | |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * The lessons for a run expected to touch `likely`, from the window's failed checks in time order. | |
| 123 | * A cause counts its distinct pulls; one also failed on main (a check with no pull) is skipped. | |
| 124 | * A cause whose failures named paths is said only to a run whose likely paths share a directory, | |
| 125 | * or that names none. Most pulls first, then by name; `LESSON_MAX` and `LESSON_BYTES` bound it. | |
| 126 | */ | |
| 127 | export function rankLessons(checks: readonly FailedCheck[], likely: readonly string[], now: number): Lesson[] { | |
| 128 | const mainRed = new Set(checks.filter((c) => c.pull === "").flatMap((c) => c.names)); | |
| 129 | const seen = new Map<string, { line: string; pulls: Set<string>; dirs: Set<string>; last: number }>(); | |
| 130 | for (const check of checks) { | |
| 131 | if (check.pull === "") continue; | |
| 132 | for (const cause of causesOf(check)) { | |
| 133 | if (mainRed.has(cause.name)) continue; | |
| 134 | const entry = seen.get(cause.name) ?? { line: cause.line, pulls: new Set<string>(), dirs: new Set<string>(), last: 0 }; | |
| 135 | entry.pulls.add(check.pull); | |
| 136 | entry.last = Math.max(entry.last, check.at); | |
| 137 | for (const dir of cause.dirs) entry.dirs.add(dir); | |
| 138 | // The newest failure's words win, but a bare name never replaces an instruction. | |
| 139 | if (cause.line.length > cause.name.length + 2 || entry.line === `- ${cause.name}`) entry.line = cause.line; | |
| 140 | seen.set(cause.name, entry); | |
| 141 | } | |
| 142 | } | |
| 143 | const near = new Set(likely.map(dirOf)); | |
| 144 | const ranked = [...seen] | |
| 145 | .filter(([, e]) => e.pulls.size >= LESSON_MIN_PULLS && now - e.last <= LESSON_FRESH_MS) | |
| 146 | .filter(([, e]) => e.dirs.size === 0 || near.size === 0 || [...e.dirs].some((d) => near.has(d))) | |
| 147 | .sort(([a, x], [b, y]) => y.pulls.size - x.pulls.size || (a < b ? -1 : a > b ? 1 : 0)); | |
| 148 | const out: Lesson[] = []; | |
| 149 | let bytes = Buffer.byteLength(LESSONS_HEADING) + 1; | |
| 150 | for (const [name, e] of ranked) { | |
| 151 | if (out.length === LESSON_MAX) break; | |
| 152 | const size = Buffer.byteLength(e.line) + 1; | |
| 153 | if (bytes + size > LESSON_BYTES) continue; | |
| 154 | bytes += size; | |
| 155 | out.push({ name, line: e.line, pulls: e.pulls.size }); | |
| 156 | } | |
| 157 | return out; | |
| 158 | } | |
| 159 | ||
| 160 | /** | |
| 161 | * The window's failed checks of a repository, oldest first: a pull's when an agent wrote the pull, | |
| 162 | * main's (`pull` empty) when it judged main or a verification. Walks `run_kind_finished` (`test/hot-reads-indexed.test.ts`). | |
| 163 | */ | |
| 164 | export function failedChecks(db: Database, repoId: string, now: number): FailedCheck[] { | |
| 165 | const rows = db | |
| 166 | .query<{ pull: string | null; agent: number | null; env: string; summary: string; detail: string; at: string }, [string, string]>( | |
| 167 | `SELECT r.issue_id AS pull, u.is_agent AS agent, r.env, r.summary, r.check_detail AS detail, r.finished_at AS at | |
| 168 | FROM run r LEFT JOIN issue i ON i.id = r.issue_id LEFT JOIN user u ON u.id = i.author_id | |
| 169 | WHERE r.state = 'failed' AND r.finished_at >= ?2 AND r.repo_id = ?1 AND r.kind = 'check' | |
| 170 | ORDER BY r.finished_at`, | |
| 171 | ) | |
| 172 | .all(repoId, new Date(now - LESSON_WINDOW_MS).toISOString()); | |
| 173 | const out: FailedCheck[] = []; | |
| 174 | for (const row of rows) { | |
| 175 | const onMain = row.env === "verify" || row.env === "main"; | |
| 176 | if (!onMain && (row.agent !== 1 || !row.env.startsWith("pull-") || row.pull === null)) continue; | |
| 177 | out.push({ pull: onMain ? "" : row.pull!, at: Date.parse(row.at), env: row.env, summary: row.summary, names: namesOf(row.detail) }); | |
| 178 | } | |
| 179 | return out; | |
| 180 | } | |
| 181 | ||
| 182 | /** The failing test names a check's detail records. */ | |
| 183 | function namesOf(detail: string): string[] { | |
| 184 | try { | |
| 185 | const names = (JSON.parse(detail || "{}") as { names?: unknown }).names; | |
| 186 | return Array.isArray(names) ? names.filter((n): n is string => typeof n === "string") : []; | |
| 187 | } catch { | |
| 188 | return []; | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | /** The lessons a builder's run on `issue` carries, scoped by the paths the issue names. */ | |
| 193 | export function lessonsFor(db: Database, repoId: string, issue: Pick<Issue, "title" | "body">, now: number): Lesson[] { | |
| 194 | return rankLessons(failedChecks(db, repoId, now), expectedFiles(issue), now); | |
| 195 | } | |
| 196 | ||
| 197 | /** The run log's one line naming what a brief carried. */ | |
| 198 | export const carriedLine = (lessons: readonly Lesson[]): string => | |
| 199 | `Lessons carried: ${lessons.map((l) => `${l.name} (${l.pulls} pulls)`).join(", ")}`; |
src/crew/run_prompt.ts +5 −2
| @@ -23,7 +23,7 @@ import { boxesOf, landedBoxes } from "./box_pass.ts"; | ||
| 23 | 23 | import { taskPrompt, type BriefInput, type LastTry, type RunReason, type TaskInput } from "./brief.ts"; |
| 24 | 24 | import { holdsNaming } from "./desk_posts.ts"; |
| 25 | 25 | import { readersAmong, voiceOf } from "./direct.ts"; |
| 26 | import { failureBrief } from "./failure_brief.ts"; | |
| 26 | import { failureBrief, type Lesson } from "./failure_brief.ts"; | |
| 27 | 27 | import { RETRY_SUMMARY, handedOverPulls } from "./guard.ts"; |
| 28 | 28 | import { handedLimbs } from "./handed_limbs.ts"; |
| 29 | 29 | import { heldNow, stuckOf } from "./held.ts"; |
| @@ -59,10 +59,12 @@ export interface PromptState { | ||
| 59 | 59 | readonly sealedHead: Repo | undefined; |
| 60 | 60 | readonly since: ReturnType<typeof feedbackSince>; |
| 61 | 61 | readonly signal: AbortSignal; |
| 62 | /** For a builder: the crew's most refused rules this week (`lessonsFor`), read by the runner. */ | |
| 63 | readonly lessons?: readonly Lesson[] | undefined; | |
| 62 | 64 | } |
| 63 | 65 | |
| 64 | 66 | export async function promptFor(s: PromptState): Promise<string> { |
| 65 | const { app, run, repo, issue, agent, pull, foreign, reason, brief, branch, bare, forkBare, record, sealedHead, since, signal } = s; | |
| 67 | const { app, run, repo, issue, agent, pull, foreign, reason, brief, branch, bare, forkBare, record, sealedHead, since, signal, lessons } = s; | |
| 66 | 68 | const role = brief.role; |
| 67 | 69 | // Only a check or deploy that failed is handed back as the failure (#12968): never the |
| 68 | 70 | // success summary of a preview that came up. |
| @@ -87,6 +89,7 @@ export async function promptFor(s: PromptState): Promise<string> { | ||
| 87 | 89 | issue: voiceOf(brief.issue), |
| 88 | 90 | reason, |
| 89 | 91 | ...(role === "builder" ? { holds: holdsNaming(app.db, repo) } : {}), |
| 92 | ...(lessons !== undefined && lessons.length > 0 ? { lessons } : {}), | |
| 90 | 93 | paused: run.attempt > 0, |
| 91 | 94 | stranger: |
| 92 | 95 | reason === "review" && pull !== undefined |
src/crew/runner.ts +7
| @@ -69,6 +69,7 @@ import { checkoutReviewHead, noteUnreviewed } from "./review_head.ts"; | ||
| 69 | 69 | import { CHECK_PENDING_SUMMARY, NOT_REVIEWED_PREFIX, SEALED_FETCH_PREFIX, overranWhere, waitingOnCheck } from "./review.ts"; |
| 70 | 70 | import { limitEnding } from "./run_limits.ts"; |
| 71 | 71 | import { prepareAgent, type Held } from "./run_prepare.ts"; |
| 72 | import { carriedLine, lessonsFor } from "./failure_brief.ts"; | |
| 72 | 73 | import { promptFor } from "./run_prompt.ts"; |
| 73 | 74 | import { outagePause, retryNetwork, type RetryState } from "./run_retry.ts"; |
| 74 | 75 | import { settleRun } from "./run_settle.ts"; |
| @@ -872,6 +873,11 @@ async function work( | ||
| 872 | 873 | // The documents a first run reads, named only where the clone has them. |
| 873 | 874 | docs: ["docs/VISION.md", "docs/PRINCIPLES.md"].filter((p) => existsSync(join(dir, p))), |
| 874 | 875 | }; |
| 876 | // A builder starting or answering a review is told what the check refused the crew for | |
| 877 | // most this week, and the run's log names what it carried. | |
| 878 | const lessons = | |
| 879 | role === "builder" && (reason === "assigned" || reason === "feedback") ? lessonsFor(app.db, repo.id, issue, Date.now()) : []; | |
| 880 | if (lessons.length > 0) appendRunLog(app.config.dataDir, run.id, carriedLine(lessons)); | |
| 875 | 881 | const prompt = await promptFor({ |
| 876 | 882 | app, |
| 877 | 883 | run, |
| @@ -889,6 +895,7 @@ async function work( | ||
| 889 | 895 | sealedHead, |
| 890 | 896 | since, |
| 891 | 897 | signal, |
| 898 | lessons, | |
| 892 | 899 | }); |
| 893 | 900 | |
| 894 | 901 | const spend = await settleSpend({ |
test/brief-lessons.test.ts added +211
| @@ -0,0 +1,211 @@ | ||
| 1 | /** | |
| 2 | * The crew's recurring reds become the brief's key: a gate rule or test that refused at least | |
| 3 | * LESSON_MIN_PULLS crew pulls this week is said, in the check's own words, to the next builder, and | |
| 4 | * nothing else is. Bounded, self-pruning, deterministic, and named in the run's log. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { createUser, type User } from "../src/core/users.ts"; | |
| 13 | import { taskPrompt, PUSH_STEPS_LINE, type TaskInput } from "../src/crew/brief.ts"; | |
| 14 | import { | |
| 15 | carriedLine, | |
| 16 | LESSON_BYTES, | |
| 17 | LESSON_MAX, | |
| 18 | LESSON_MIN_PULLS, | |
| 19 | LESSON_WINDOW_MS, | |
| 20 | LESSONS_HEADING, | |
| 21 | lessonsFor, | |
| 22 | rankLessons, | |
| 23 | LESSON_FRESH_MS, | |
| 24 | type FailedCheck, | |
| 25 | } from "../src/crew/failure_brief.ts"; | |
| 26 | ||
| 27 | const DOORS = | |
| 28 | 'check failed: gate: rule "both doors": this pull edits src/http/api/runners.ts but has no change under src/tui/; the terminal must reach what the API and the MCP reach. Add it, or put a "No door: <why>" line (10+ …'; | |
| 29 | const TIMER = | |
| 30 | 'check failed: gate: rule "a test does not wait on a real timer": test/x.test.ts:50 sleeps; inject a clock and step it'; | |
| 31 | ||
| 32 | const NOW = Date.parse("2026-11-30T06:00:00Z"); | |
| 33 | const rank = (checks: readonly FailedCheck[], likely: readonly string[]) => rankLessons(checks, likely, NOW); | |
| 34 | const onPull = (pull: string, summary: string, names: string[] = [], at = NOW): FailedCheck => ({ pull, at, env: `pull-${pull}`, summary, names }); | |
| 35 | const onMain = (names: string[]): FailedCheck => ({ pull: "", at: NOW, env: "verify", summary: "check failed", names }); | |
| 36 | const tests = (pull: string, ...names: string[]) => onPull(pull, `check failed: ${names.length} of 900 tests`, names); | |
| 37 | const pulls = (n: number, make: (p: string) => FailedCheck) => Array.from({ length: n }, (_, i) => make(`p${i}`)); | |
| 38 | ||
| 39 | describe("which reds become lessons", () => { | |
| 40 | test("a rule is said once it refused LESSON_MIN_PULLS distinct pulls, in the gate's own instruction", () => { | |
| 41 | expect( | |
| 42 | rank( | |
| 43 | pulls(LESSON_MIN_PULLS - 1, (p) => onPull(p, DOORS)), | |
| 44 | [], | |
| 45 | ), | |
| 46 | ).toEqual([]); | |
| 47 | // The same pull failing again is one pull, not two. | |
| 48 | expect(rank([...pulls(LESSON_MIN_PULLS - 1, (p) => onPull(p, DOORS)), onPull("p0", DOORS)], [])).toEqual([]); | |
| 49 | const said = rank( | |
| 50 | pulls(LESSON_MIN_PULLS, (p) => onPull(p, DOORS)), | |
| 51 | [], | |
| 52 | ); | |
| 53 | expect(said).toEqual([ | |
| 54 | { name: "both doors", line: "- both doors: the terminal must reach what the API and the MCP reach.", pulls: LESSON_MIN_PULLS }, | |
| 55 | ]); | |
| 56 | }); | |
| 57 | ||
| 58 | test("a refused-by summary counts each rule it names; a later bare name keeps the instruction", () => { | |
| 59 | const refused = (p: string) => | |
| 60 | onPull( | |
| 61 | p, | |
| 62 | "check failed: gate: refused by 2 rules: both doors, name the issue (bun scripts/gate.ts --explain says how to satisfy each)", | |
| 63 | ); | |
| 64 | const said = rank([...pulls(3, (p) => onPull(p, DOORS)), ...pulls(3, (p) => refused(`r${p}`))], []); | |
| 65 | expect(said.map((l) => [l.name, l.pulls])).toEqual([ | |
| 66 | ["both doors", 6], | |
| 67 | ["name the issue", 3], | |
| 68 | ]); | |
| 69 | expect(said[0]!.line).toContain("the terminal must reach"); | |
| 70 | expect(said[1]!.line).toBe("- name the issue"); | |
| 71 | }); | |
| 72 | ||
| 73 | test("a test that also failed on main is main's red, never a lesson; one that did not is said by name", () => { | |
| 74 | const checks = [ | |
| 75 | ...pulls(5, (p) => tests(p, "budget > stays under", "dead code: every export has a caller")), | |
| 76 | onMain(["budget > stays under"]), | |
| 77 | ]; | |
| 78 | expect(rank(checks, []).map((l) => l.line)).toEqual(['- test "dead code: every export has a caller"']); | |
| 79 | }); | |
| 80 | ||
| 81 | test("most pulls first, then by name, and the same input always gives the same lessons", () => { | |
| 82 | const checks = [...pulls(3, (p) => tests(p, "b")), ...pulls(3, (p) => tests(p, "a")), ...pulls(5, (p) => onPull(p, TIMER))]; | |
| 83 | const said = rank(checks, []); | |
| 84 | expect(said.map((l) => l.name)).toEqual(["a test does not wait on a real timer", "a", "b"]); | |
| 85 | expect(rank([...checks].reverse(), [])).toEqual(said); | |
| 86 | }); | |
| 87 | ||
| 88 | test("a rule that has not refused within LESSON_FRESH_MS is not said, however often it did before", () => { | |
| 89 | const stale = pulls(9, (p) => onPull(p, DOORS, [], NOW - LESSON_FRESH_MS - 1)); | |
| 90 | expect(rank(stale, [])).toEqual([]); | |
| 91 | expect(rank([...stale, onPull("late", DOORS)], []).map((l) => l.pulls)).toEqual([10]); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a test path in a rule's message never narrows who is told", () => { | |
| 95 | const checks = pulls(3, (p) => onPull(p, TIMER)); | |
| 96 | expect(rank(checks, ["src/crew/brief.ts"])).toHaveLength(1); | |
| 97 | }); | |
| 98 | ||
| 99 | test("a rule whose failures named paths is said only to a run near them, or to one that names none", () => { | |
| 100 | const checks = pulls(3, (p) => onPull(p, DOORS)); | |
| 101 | expect(rank(checks, ["src/http/api/keys.ts"])).toHaveLength(1); | |
| 102 | expect(rank(checks, [])).toHaveLength(1); | |
| 103 | expect(rank(checks, ["src/crew/brief.ts"])).toEqual([]); | |
| 104 | }); | |
| 105 | ||
| 106 | test("bounded: at most LESSON_MAX lines and LESSON_BYTES with the heading, however many recur", () => { | |
| 107 | const long = (i: number) => `test ${i} ${"x".repeat(150)}`; | |
| 108 | const checks = Array.from({ length: 12 }, (_, i) => pulls(3 + i, (p) => tests(p, long(i)))).flat(); | |
| 109 | const said = rank(checks, []); | |
| 110 | expect(said.length).toBeLessThanOrEqual(LESSON_MAX); | |
| 111 | expect(said.length).toBeGreaterThan(0); | |
| 112 | expect(Buffer.byteLength([LESSONS_HEADING, ...said.map((l) => l.line)].join("\n"))).toBeLessThanOrEqual(LESSON_BYTES); | |
| 113 | expect(said[0]!.name).toBe(long(11)); | |
| 114 | }); | |
| 115 | }); | |
| 116 | ||
| 117 | describe("the brief and the run log", () => { | |
| 118 | const base = { | |
| 119 | agent: { user: { username: "bot", displayName: "" }, brief: "" }, | |
| 120 | repo: { ownerName: "alice", name: "app", defaultBranch: "main", autonomyDirection: "" }, | |
| 121 | issue: { number: 1, title: "t", body: "b", authorName: "alice" }, | |
| 122 | comments: [], | |
| 123 | reviews: [], | |
| 124 | branch: "bot/1", | |
| 125 | forgeUrl: "http://forge.test", | |
| 126 | reason: "assigned", | |
| 127 | }; | |
| 128 | const lessons = rank( | |
| 129 | pulls(4, (p) => onPull(p, DOORS)), | |
| 130 | [], | |
| 131 | ); | |
| 132 | ||
| 133 | test("a builder's brief carries the heading and each line; a reviewer's does not", () => { | |
| 134 | const builder = taskPrompt({ ...base, role: "builder", lessons } as unknown as TaskInput); | |
| 135 | expect(builder).toContain(`${LESSONS_HEADING}\n- both doors: the terminal must reach`); | |
| 136 | const reviewer = taskPrompt({ ...base, role: "reviewer", reason: "review", pull: { number: 2 }, lessons } as unknown as TaskInput); | |
| 137 | expect(reviewer).not.toContain(LESSONS_HEADING); | |
| 138 | expect(taskPrompt({ ...base, role: "builder" } as unknown as TaskInput)).not.toContain(LESSONS_HEADING); | |
| 139 | }); | |
| 140 | ||
| 141 | test("the run log names what it carried, with each one's pull count", () => { | |
| 142 | expect(carriedLine(lessons)).toBe("Lessons carried: both doors (4 pulls)"); | |
| 143 | }); | |
| 144 | ||
| 145 | test("the push steps commit before the gate, which judges commits, not the working tree", () => { | |
| 146 | expect(PUSH_STEPS_LINE.indexOf("then commit")).toBeGreaterThan(-1); | |
| 147 | expect(PUSH_STEPS_LINE.indexOf("then commit")).toBeLessThan(PUSH_STEPS_LINE.indexOf("bun scripts/gate.ts --explain")); | |
| 148 | expect(PUSH_STEPS_LINE).toContain("the gate skips uncommitted files"); | |
| 149 | }); | |
| 150 | }); | |
| 151 | ||
| 152 | describe("read from the run table", () => { | |
| 153 | let root = ""; | |
| 154 | let app: App; | |
| 155 | let repoId = ""; | |
| 156 | let bot = ""; | |
| 157 | let alice: User | undefined; | |
| 158 | let n = 0; | |
| 159 | ||
| 160 | beforeAll(async () => { | |
| 161 | root = mkdtempSync(join(tmpdir(), "forge-lessons-")); | |
| 162 | app = createApp({ dataDir: root, port: 0 }); | |
| 163 | const a = await createUser(app.db, { | |
| 164 | origin: "seed", | |
| 165 | trusted: true, | |
| 166 | username: "alice", | |
| 167 | email: "[email protected]", | |
| 168 | password: "a-good-passphrase", | |
| 169 | adminIfFirst: true, | |
| 170 | }); | |
| 171 | if (!a.ok) throw new Error(a.error); | |
| 172 | alice = a.value; | |
| 173 | const b = await createUser(app.db, { origin: "seed", username: "builder", email: "[email protected]", agent: true }); | |
| 174 | if (!b.ok) throw new Error(b.error); | |
| 175 | bot = b.value.id; | |
| 176 | const made = await createRepo(app, alice, { name: "square" }); | |
| 177 | if (!made.ok) throw new Error(String(made.error)); | |
| 178 | repoId = made.value.id; | |
| 179 | }); | |
| 180 | afterAll(() => rmSync(root, { recursive: true, force: true })); | |
| 181 | ||
| 182 | /** A pull by `author` whose check failed `ago` ms before now with `summary`. */ | |
| 183 | function failed(author: string, summary: string, ago: number, names: string[] = []): void { | |
| 184 | n++; | |
| 185 | const issue = `i${n}`; | |
| 186 | app.db | |
| 187 | .query( | |
| 188 | "INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state) VALUES (?1, ?2, ?3, 'pull', 't', '', ?4, 'open')", | |
| 189 | ) | |
| 190 | .run(issue, repoId, 100 + n, author); | |
| 191 | const at = new Date(Date.now() - ago).toISOString(); | |
| 192 | app.db | |
| 193 | .query( | |
| 194 | "INSERT INTO run (id, repo_id, kind, issue_id, env, commit_oid, state, summary, check_detail, created_at, finished_at) VALUES (?1, ?2, 'check', ?3, ?4, 'c', 'failed', ?5, ?6, ?7, ?7)", | |
| 195 | ) | |
| 196 | .run(`r${n}`, repoId, issue, `pull-${100 + n}`, summary, JSON.stringify({ names }), at); | |
| 197 | } | |
| 198 | ||
| 199 | test("only the crew's pulls inside the window count, so an old or a person's red falls out", () => { | |
| 200 | const issue = { title: "Add runners", body: "Files: src/http/api/runners.ts" }; | |
| 201 | failed(bot, DOORS, 60_000); | |
| 202 | failed(bot, DOORS, 120_000); | |
| 203 | failed(alice!.id, DOORS, 60_000); | |
| 204 | failed(bot, DOORS, LESSON_WINDOW_MS + 60_000); | |
| 205 | expect(lessonsFor(app.db, repoId, issue, Date.now())).toEqual([]); | |
| 206 | failed(bot, DOORS, 180_000); | |
| 207 | expect(lessonsFor(app.db, repoId, issue, Date.now()).map((l) => [l.name, l.pulls])).toEqual([["both doors", 3]]); | |
| 208 | // A week on, nothing new refused: the lesson has left by itself. | |
| 209 | expect(lessonsFor(app.db, repoId, issue, Date.now() + LESSON_WINDOW_MS)).toEqual([]); | |
| 210 | }); | |
| 211 | }); |
test/hot-reads-indexed.test.ts +7
| @@ -108,3 +108,10 @@ test("the home's ship time reads each issue's first agent run by index, with no | ||
| 108 | 108 | expect(lines.join("\n")).toContain("run_agent_issue_latest"); |
| 109 | 109 | expect(lines.join("\n")).not.toContain("run_kind_started"); |
| 110 | 110 | }); |
| 111 | ||
| 112 | test("the crew's lessons read the week's failed checks by finish time from an index, not the run table", async () => { | |
| 113 | const { lessonsFor } = await import("../src/crew/failure_brief.ts"); | |
| 114 | const lines = plans(() => void lessonsFor(app.db, "r", { title: "", body: "" }, Date.now())); | |
| 115 | expect(lines.join("\n")).toMatch(/run_(kind|failed)_finished/); | |
| 116 | noScan(lines); | |
| 117 | }); |
test/run-steps.test.ts +9 −1
| @@ -30,6 +30,7 @@ import { outagePause, retryNetwork, type RetryState } from "../src/crew/run_retr | ||
| 30 | 30 | import { settleSpend, type Spend } from "../src/crew/run_spend.ts"; |
| 31 | 31 | import { settleReview, verdictOf } from "../src/crew/run_verdict.ts"; |
| 32 | 32 | import { wrapUp } from "../src/crew/run_wrapup.ts"; |
| 33 | import { LESSONS_HEADING, type Lesson } from "../src/crew/failure_brief.ts"; | |
| 33 | 34 | import { CREDIT_SPENT_SAID } from "../src/spawn/funding.ts"; |
| 34 | 35 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 35 | 36 | |
| @@ -496,7 +497,7 @@ describe("promptFor", () => { | ||
| 496 | 497 | await rm(dir, { recursive: true, force: true }); |
| 497 | 498 | }); |
| 498 | 499 | |
| 499 | const promptOf = async (over: { reason?: RunReason; attempt?: number; summary?: string; issue?: Issue } = {}): Promise<string> => { | |
| 500 | const promptOf = async (over: { reason?: RunReason; attempt?: number; summary?: string; issue?: Issue; lessons?: readonly Lesson[] } = {}): Promise<string> => { | |
| 500 | 501 | const issue = over.issue ?? (() => { |
| 501 | 502 | const made = createIssue(app.db, { repoId: repo.id, authorId: owner.id, title: "Water the beans", body: "Every morning, before nine." }); |
| 502 | 503 | if (!made.ok) throw new Error("issue"); |
| @@ -519,9 +520,16 @@ describe("promptFor", () => { | ||
| 519 | 520 | sealedHead: undefined, |
| 520 | 521 | since: feedbackSince(app.db, repo, issue, agent), |
| 521 | 522 | signal: new AbortController().signal, |
| 523 | ...(over.lessons === undefined ? {} : { lessons: over.lessons }), | |
| 522 | 524 | }); |
| 523 | 525 | }; |
| 524 | 526 | |
| 527 | test("the lessons the runner read reach a builder's brief, and none means no heading", async () => { | |
| 528 | const lessons = [{ name: "both doors", line: "- both doors: the terminal must reach what the API and the MCP reach.", pulls: 3 }]; | |
| 529 | expect(await promptOf({ lessons })).toContain(`${LESSONS_HEADING}\n${lessons[0]!.line}`); | |
| 530 | expect(await promptOf()).not.toContain(LESSONS_HEADING); | |
| 531 | }); | |
| 532 | ||
| 525 | 533 | test("an issue's title, body and discussion reach the agent", async () => { |
| 526 | 534 | const issue = createIssue(app.db, { repoId: repo.id, authorId: owner.id, title: "Mulch the beds", body: "Use the bark pile." }); |
| 527 | 535 | if (!issue.ok) throw new Error("issue"); |
new above
offline
This block splits
holdLinesfrom its doc comment (lines above). PutlessonLinesbefore that comment so each function keeps its own.