Public: anyone can read this; the crew can change it.
Watch a check the way you watch an Actions run: named on the pull, steps and live progress on its page, in the list and the feed #1103
scout/forge:scout/1102-watch-a-check-the-way-you-watch-an-actio → main 1 commit fork
20 files
changed +1174
−55
docs/MCP.md +1
| @@ -53,6 +53,7 @@ for a tunnel and a forge that sleeps. | ||
| 53 | 53 | | `milestones` | `{ app, issue? }`: how far each thing asked for is (and, with `issue`, whether you watch it: `watching`), from the dependency tree: the plan's limbs with their weights, or one milestone opened (who is building, what is blocked and on what, what merged in 7 days, an estimate) | | |
| 54 | 54 | | `limb` | `{ app, issue, action, reason?, pillar?, operation?, dryRun? }`: defer, resume or close a whole limb as not doing, or undo an operation; plans unless `dryRun` is false, and applying takes an admin token | yes | |
| 55 | 55 | | `watch` | `{ app, issue, watch? }`: watch a milestone, or stop with `watch: false`: one item in your feed that follows it, and one push to your phone when it lands | yes | |
| 56 | | `check` | `{ repo, pull?, run? }`: how a check is doing — its state, seconds, `steps` (install, typecheck, tests) and `progress` (`files` started of the gate's `total`, `passed`, `failed`, the file `current`ly running, `failures` so far with their first lines), read from the check's own log; the pull's newest check, one run, or the repository's newest | | | |
| 56 | 57 | | `usage` | `{ repo, days? }`: what the crew's runs spent — tokens, cost, wall time — by subscription, agent, issue, and each account's 5-hour and weekly standing; a writer's | | |
| 57 | 58 | | `link` | `{ app, issue, blockedBy }`: the issue waits for another; loops and cross-repository are refused | yes | |
| 58 | 59 | | `unlink` | `{ app, issue, blockedBy }`: the issue stops waiting for it | yes | |
scripts/gate.ts +1
| @@ -186,6 +186,7 @@ export const GATE: readonly string[] = [ | ||
| 186 | 186 | "test/overlap.test.ts", |
| 187 | 187 | "test/outsiders-switch.test.ts", |
| 188 | 188 | "test/check-list.test.ts", |
| 189 | "test/check-watch.test.ts", | |
| 189 | 190 | "test/holds.test.ts", |
| 190 | 191 | "test/hollow.test.ts", |
| 191 | 192 | "test/home-feed.test.ts", |
scripts/shards.ts +3 −3
| @@ -219,7 +219,7 @@ export interface Tally { | ||
| 219 | 219 | readonly lines: string[]; |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | const RAN = /^Ran (\d+) tests? across (\d+) files?\./m; | |
| 222 | const RAN = /^Ran (\d+) tests? across (\d+) files?\./gm; | |
| 223 | 223 | const FAIL = /^\(fail\) .*$/gm; |
| 224 | 224 | |
| 225 | 225 | /** |
| @@ -234,8 +234,8 @@ export function tally(results: readonly ShardResult[], seconds: number): Tally { | ||
| 234 | 234 | const failing: string[] = []; |
| 235 | 235 | const unnamed: string[] = []; |
| 236 | 236 | results.forEach((result, i) => { |
| 237 | const ran = RAN.exec(result.output); | |
| 238 | if (ran) { | |
| 237 | // A shard runs a `bun test` per file, each with its own count. | |
| 238 | for (const ran of result.output.matchAll(RAN)) { | |
| 239 | 239 | tests += Number(ran[1]); |
| 240 | 240 | files += Number(ran[2]); |
| 241 | 241 | } |
scripts/test-shards.ts +55 −23
| @@ -8,11 +8,11 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * `--gate` runs the files `scripts/gate.ts` admits, which is how |
| 10 | 10 | * `forge.json`'s `check.run` names them; files on the command line are for |
| 11 | * running a slice by hand. Each shard is its own `bun test` | |
| 12 | * over a disjoint slice (`scripts/shards.ts` decides the slices); a | |
| 13 | * shard's output is written through whole when it finishes, so the log | |
| 14 | * reads one shard at a time, and the tail names every failing test from | |
| 15 | * every shard. Exits non-zero if any shard does. | |
| 11 | * running a slice by hand. Each shard walks a disjoint slice | |
| 12 | * (`scripts/shards.ts` decides the slices), one `bun test` per file; a | |
| 13 | * shard's lines are written as they land, each behind `[n/m]` for its | |
| 14 | * shard, a file's name opens its report, a `── shard` line closes it, and the tail names every failing | |
| 15 | * test from every shard. Exits non-zero if any shard does. | |
| 16 | 16 | * |
| 17 | 17 | * Files are weighed by how long they took, from `scripts/test-times.json` |
| 18 | 18 | * (a file it does not name, or a table it refuses, falls back to bytes). |
| @@ -116,41 +116,73 @@ for (const signal of ["SIGINT", "SIGTERM"] as const) | ||
| 116 | 116 | process.exit(130); |
| 117 | 117 | }); |
| 118 | 118 | |
| 119 | /** | |
| 120 | * Reads a stream, handing each whole line to `onLine` as it lands (and | |
| 121 | * keeping every byte in `into`, for the tail that names failures). | |
| 122 | */ | |
| 119 | 123 | async function drain( |
| 120 | 124 | stream: ReadableStream<Uint8Array>, |
| 121 | 125 | into: string[], |
| 126 | onLine: (line: string) => void, | |
| 122 | 127 | ): Promise<void> { |
| 123 | 128 | const decoder = new TextDecoder(); |
| 124 | for await (const chunk of stream) | |
| 125 | into.push(decoder.decode(chunk, { stream: true })); | |
| 126 | into.push(decoder.decode()); | |
| 129 | let pending = ""; | |
| 130 | const take = (text: string): void => { | |
| 131 | into.push(text); | |
| 132 | pending += text; | |
| 133 | let at = pending.indexOf("\n"); | |
| 134 | while (at !== -1) { | |
| 135 | onLine(pending.slice(0, at)); | |
| 136 | pending = pending.slice(at + 1); | |
| 137 | at = pending.indexOf("\n"); | |
| 138 | } | |
| 139 | }; | |
| 140 | for await (const chunk of stream) take(decoder.decode(chunk, { stream: true })); | |
| 141 | take(decoder.decode()); | |
| 142 | if (pending !== "") onLine(pending); | |
| 127 | 143 | } |
| 128 | 144 | |
| 129 | 145 | type Timed = ShardResult & { seconds: number }; |
| 130 | 146 | |
| 131 | 147 | async function runShard(slice: string[], index: number): Promise<Timed> { |
| 132 | 148 | const started = performance.now(); |
| 133 | // An exact path, never a filter: `test/web.test.ts` alone would also | |
| 134 | // match any file whose path contains it, in some other shard. | |
| 135 | const paths = slice.map((file) => | |
| 136 | file.startsWith("/") || file.startsWith("./") ? file : `./${file}`, | |
| 137 | ); | |
| 138 | const child = Bun.spawn(["bun", "test", ...flags, ...paths], { | |
| 139 | stdin: "ignore", | |
| 140 | stdout: "pipe", | |
| 141 | stderr: "pipe", | |
| 142 | }); | |
| 143 | children.add(child); | |
| 149 | const tag = `[${index + 1}/${slices.length}] `; | |
| 150 | const live = (line: string): void => { | |
| 151 | process.stdout.write(`${tag}${line}\n`); | |
| 152 | }; | |
| 144 | 153 | // One buffer, in arrival order: `bun test` splits its report across |
| 145 | 154 | // stdout and stderr. |
| 146 | 155 | const output: string[] = []; |
| 147 | await Promise.all([drain(child.stdout, output), drain(child.stderr, output)]); | |
| 148 | const code = await child.exited; | |
| 149 | children.delete(child); | |
| 156 | let code = 0; | |
| 157 | // A shard's files run one `bun test` at a time, each announced by its | |
| 158 | // name before it starts and its counts when it ends: `bun test` itself | |
| 159 | // says nothing of a file that passes, so this is what lets the log (and | |
| 160 | // the forge, which reads it) see the gate move file by file. Each line is | |
| 161 | // written as it lands, behind the shard it came from. | |
| 162 | for (const file of slice) { | |
| 163 | // An exact path, never a filter: `test/web.test.ts` alone would also | |
| 164 | // match any file whose path contains it, in some other shard. | |
| 165 | const path = | |
| 166 | file.startsWith("/") || file.startsWith("./") ? file : `./${file}`; | |
| 167 | live(`${file}:`); | |
| 168 | const child = Bun.spawn(["bun", "test", ...flags, path], { | |
| 169 | stdin: "ignore", | |
| 170 | stdout: "pipe", | |
| 171 | stderr: "pipe", | |
| 172 | }); | |
| 173 | children.add(child); | |
| 174 | await Promise.all([ | |
| 175 | drain(child.stdout, output, live), | |
| 176 | drain(child.stderr, output, live), | |
| 177 | ]); | |
| 178 | const exit = await child.exited; | |
| 179 | children.delete(child); | |
| 180 | if (exit !== 0 && code === 0) code = exit; | |
| 181 | } | |
| 150 | 182 | const text = output.join(""); |
| 151 | 183 | const seconds = ((performance.now() - started) / 1000).toFixed(1); |
| 152 | 184 | process.stdout.write( |
| 153 | `\n── shard ${index + 1}/${slices.length} (exit ${code}, ${seconds}s): ${slice.join(" ")}\n${text}${text.endsWith("\n") ? "" : "\n"}`, | |
| 185 | `── shard ${index + 1}/${slices.length} (exit ${code}, ${seconds}s): ${slice.join(" ")}\n`, | |
| 154 | 186 | ); |
| 155 | 187 | return { |
| 156 | 188 | files: slice, |
src/checks/bun-test.ts +9 −5
| @@ -9,7 +9,9 @@ import type { CheckDetail } from "../core/runs.ts"; | ||
| 9 | 9 | |
| 10 | 10 | // The duration is bun's to print or not: under CI it leaves it off a test |
| 11 | 11 | // that took next to no time, so a name is a name with or without one. |
| 12 | const FAIL_LINE = /^\(fail\) (.+?)(?: \[[0-9.]+m?s\])?$/; | |
| 12 | const FAIL_LINE = /^\(fail\) (.+?)(?: \[[0-9.]+m?s\])?(?: \[shard \d+\])?$/; | |
| 13 | // A sharded gate writes each line behind `[n/m]`; each shard counts its own. | |
| 14 | const SHARD_PREFIX = /^\[\d+\/\d+\] /; | |
| 13 | 15 | const PASS_COUNT = /^\s*(\d+) pass$/; |
| 14 | 16 | const FAIL_COUNT = /^\s*(\d+) fail$/; |
| 15 | 17 | |
| @@ -27,19 +29,21 @@ export function parseBunTestFailure( | ||
| 27 | 29 | const names: string[] = []; |
| 28 | 30 | let passed: number | undefined; |
| 29 | 31 | let failed: number | undefined; |
| 30 | for (const line of lines) { | |
| 32 | for (const raw of lines) { | |
| 33 | const line = raw.replace(SHARD_PREFIX, ""); | |
| 31 | 34 | const fail = FAIL_LINE.exec(line); |
| 32 | 35 | if (fail) { |
| 33 | if (names.length < MAX_FAILING_NAMES) names.push(fail[1]!); | |
| 36 | if (names.length < MAX_FAILING_NAMES && !names.includes(fail[1]!)) | |
| 37 | names.push(fail[1]!); | |
| 34 | 38 | continue; |
| 35 | 39 | } |
| 36 | 40 | const pass = PASS_COUNT.exec(line); |
| 37 | 41 | if (pass) { |
| 38 | passed = Number(pass[1]); | |
| 42 | passed = (passed ?? 0) + Number(pass[1]); | |
| 39 | 43 | continue; |
| 40 | 44 | } |
| 41 | 45 | const failCount = FAIL_COUNT.exec(line); |
| 42 | if (failCount) failed = Number(failCount[1]); | |
| 46 | if (failCount) failed = (failed ?? 0) + Number(failCount[1]); | |
| 43 | 47 | } |
| 44 | 48 | if (passed === undefined || failed === undefined || failed === 0) |
| 45 | 49 | return undefined; |
src/checks/progress.ts added +213
| @@ -0,0 +1,213 @@ | ||
| 1 | /** | |
| 2 | * How far a check has got, read from the lines its runner already writes: | |
| 3 | * `test-shards: N files in …` (the gate's count), `test/x.test.ts:` headers | |
| 4 | * (a file started), each file's `N pass` / `M fail` counts and its `(fail)` | |
| 5 | * lines, `── shard …` summaries, and | |
| 6 | * the `step <name> <state> <n>s` lines `checks/service.ts` writes as an | |
| 7 | * install or the typecheck ends. Plain lines in, plain data out — there is | |
| 8 | * no second channel, so the same lines give the same answer on every render. | |
| 9 | */ | |
| 10 | ||
| 11 | import { isTerminal, type Run } from "../core/runs.ts"; | |
| 12 | import { readRunLog } from "../runs/log.ts"; | |
| 13 | ||
| 14 | export type StepState = "pending" | "running" | "ok" | "failed"; | |
| 15 | ||
| 16 | export interface CheckStep { | |
| 17 | readonly name: "install" | "typecheck" | "tests"; | |
| 18 | readonly state: StepState; | |
| 19 | /** Whole seconds, once the step has ended; null while it has not. */ | |
| 20 | readonly seconds: number | null; | |
| 21 | } | |
| 22 | ||
| 23 | export interface CheckFailure { | |
| 24 | readonly name: string; | |
| 25 | readonly file: string | null; | |
| 26 | /** The output just before the `(fail)` line — what the test said. */ | |
| 27 | readonly lines: readonly string[]; | |
| 28 | } | |
| 29 | ||
| 30 | export interface CheckProgress { | |
| 31 | /** Test files started so far. */ | |
| 32 | readonly files: number; | |
| 33 | /** Files the gate runs; 0 when the runner never said. */ | |
| 34 | readonly total: number; | |
| 35 | readonly passed: number; | |
| 36 | readonly failed: number; | |
| 37 | /** The file running now, or null. */ | |
| 38 | readonly current: string | null; | |
| 39 | readonly failures: readonly CheckFailure[]; | |
| 40 | } | |
| 41 | ||
| 42 | export interface CheckView { | |
| 43 | readonly steps: readonly CheckStep[]; | |
| 44 | readonly progress: CheckProgress; | |
| 45 | } | |
| 46 | ||
| 47 | /** Shards stream their lines as `[2/3] …`; the prefix is the runner's, not the test's. */ | |
| 48 | const PREFIX = /^\[\d+\/\d+\] /; | |
| 49 | const HEADER = /^(\S+\.test\.[cm]?[jt]sx?):$/; | |
| 50 | const PASSED = /^\s*(\d+) pass$/; | |
| 51 | const FAILED = /^\s*(\d+) fail$/; | |
| 52 | const FAIL = /^\(fail\) (.+?)(?: \[[0-9.]+m?s\])?$/; | |
| 53 | const TOTAL = /^test-shards: (\d+) files? in /; | |
| 54 | const STEP = /^step (install|typecheck|check) (ok|failed) (\d+)s$/; | |
| 55 | const TALLIED = /\[shard \d+\]$/; | |
| 56 | const COMMAND = /^\$ (.*)$/; | |
| 57 | const TESTING = /^(test-shards: \d+ files? in |bun test v)/; | |
| 58 | ||
| 59 | export const MAX_FAILURES = 10; | |
| 60 | const CONTEXT_LINES = 3; | |
| 61 | const LINE_MAX = 200; | |
| 62 | ||
| 63 | const clip = (s: string): string => | |
| 64 | s.length > LINE_MAX ? `${s.slice(0, LINE_MAX - 1)}…` : s; | |
| 65 | ||
| 66 | /** | |
| 67 | * `over` is whether the run has ended and `ok` whether it passed; they | |
| 68 | * settle a step the lines leave open (a check that failed in `tsc` never | |
| 69 | * wrote a tests line). | |
| 70 | */ | |
| 71 | export function checkView( | |
| 72 | lines: readonly string[], | |
| 73 | over: boolean, | |
| 74 | ok: boolean, | |
| 75 | ): CheckView { | |
| 76 | const seen = new Set<string>(); | |
| 77 | const failures: CheckFailure[] = []; | |
| 78 | let total = 0; | |
| 79 | let passed = 0; | |
| 80 | let failed = 0; | |
| 81 | let current: string | null = null; | |
| 82 | let recent: string[] = []; | |
| 83 | let installed = false; | |
| 84 | let checkStarted = false; | |
| 85 | let typechecks = false; | |
| 86 | let testsStarted = false; | |
| 87 | const ended = new Map<string, { ok: boolean; seconds: number }>(); | |
| 88 | ||
| 89 | for (const raw of lines) { | |
| 90 | const line = raw.replace(PREFIX, ""); | |
| 91 | // The ending's own list of failing tests repeats what the shards wrote. | |
| 92 | if (TALLIED.test(line)) continue; | |
| 93 | const step = STEP.exec(line); | |
| 94 | if (step) { | |
| 95 | ended.set(step[1]!, { ok: step[2] === "ok", seconds: Number(step[3]) }); | |
| 96 | continue; | |
| 97 | } | |
| 98 | const command = COMMAND.exec(line); | |
| 99 | if (command && !testsStarted) { | |
| 100 | const text = command[1]!; | |
| 101 | if (/\btsc\b|test-shards|\bbun test\b/.test(text)) { | |
| 102 | checkStarted = true; | |
| 103 | typechecks = /\btsc\b/.test(text); | |
| 104 | } else if (!checkStarted) installed = true; | |
| 105 | continue; | |
| 106 | } | |
| 107 | if (!testsStarted && TESTING.test(line)) testsStarted = true; | |
| 108 | const gate = TOTAL.exec(line); | |
| 109 | if (gate) total = Number(gate[1]); | |
| 110 | const header = HEADER.exec(line); | |
| 111 | if (header) { | |
| 112 | testsStarted = true; | |
| 113 | seen.add(header[1]!); | |
| 114 | current = header[1]!; | |
| 115 | recent = []; | |
| 116 | continue; | |
| 117 | } | |
| 118 | const pass = PASSED.exec(line); | |
| 119 | if (pass) { | |
| 120 | passed += Number(pass[1]); | |
| 121 | recent = []; | |
| 122 | continue; | |
| 123 | } | |
| 124 | const count = FAILED.exec(line); | |
| 125 | if (count) { | |
| 126 | failed += Number(count[1]); | |
| 127 | recent = []; | |
| 128 | continue; | |
| 129 | } | |
| 130 | const fail = FAIL.exec(line); | |
| 131 | if (fail) { | |
| 132 | if (failures.length < MAX_FAILURES) | |
| 133 | failures.push({ name: clip(fail[1]!), file: current, lines: recent }); | |
| 134 | recent = []; | |
| 135 | continue; | |
| 136 | } | |
| 137 | if (line.startsWith("── shard ")) { | |
| 138 | recent = []; | |
| 139 | continue; | |
| 140 | } | |
| 141 | const text = line.trim(); | |
| 142 | if (text !== "") { | |
| 143 | recent.push(clip(text)); | |
| 144 | if (recent.length > CONTEXT_LINES) recent.shift(); | |
| 145 | } | |
| 146 | } | |
| 147 | ||
| 148 | // Stages in order; the last one reached is the one running (or the one | |
| 149 | // the run ended in), every earlier one is behind it. | |
| 150 | const stages: { name: CheckStep["name"]; key: string }[] = []; | |
| 151 | if (installed) stages.push({ name: "install", key: "install" }); | |
| 152 | if (typechecks) stages.push({ name: "typecheck", key: "typecheck" }); | |
| 153 | stages.push({ name: "tests", key: "check" }); | |
| 154 | const reached = testsStarted | |
| 155 | ? stages.length - 1 | |
| 156 | : checkStarted | |
| 157 | ? typechecks | |
| 158 | ? stages.findIndex((s) => s.name === "typecheck") | |
| 159 | : stages.length - 1 | |
| 160 | : installed | |
| 161 | ? 0 | |
| 162 | : -1; | |
| 163 | const steps: CheckStep[] = stages.map((stage, i) => { | |
| 164 | const end = ended.get(stage.key); | |
| 165 | const state: StepState = | |
| 166 | i < reached | |
| 167 | ? "ok" | |
| 168 | : i === reached | |
| 169 | ? over | |
| 170 | ? ok | |
| 171 | ? "ok" | |
| 172 | : "failed" | |
| 173 | : "running" | |
| 174 | : "pending"; | |
| 175 | return { | |
| 176 | name: stage.name, | |
| 177 | state: end ? (end.ok ? "ok" : "failed") : state, | |
| 178 | seconds: end?.seconds ?? null, | |
| 179 | }; | |
| 180 | }); | |
| 181 | return { | |
| 182 | steps, | |
| 183 | progress: { | |
| 184 | files: seen.size, | |
| 185 | total, | |
| 186 | passed, | |
| 187 | failed, | |
| 188 | current: over ? null : current, | |
| 189 | failures, | |
| 190 | }, | |
| 191 | }; | |
| 192 | } | |
| 193 | ||
| 194 | /** The line a step ending leaves in the log, for `checkView` to read back. */ | |
| 195 | export function stepLine( | |
| 196 | name: "install" | "typecheck" | "check", | |
| 197 | ok: boolean, | |
| 198 | startedMs: number, | |
| 199 | ): string { | |
| 200 | return `step ${name} ${ok ? "ok" : "failed"} ${Math.round((Date.now() - startedMs) / 1000)}s`; | |
| 201 | } | |
| 202 | ||
| 203 | /** Whether this line is the test runner's first word. */ | |
| 204 | export const testsBegin = (line: string): boolean => TESTING.test(line); | |
| 205 | ||
| 206 | /** A check's log, however long a forty-minute gate has made it. */ | |
| 207 | const READ_MAX = 4 * 1024 * 1024; | |
| 208 | ||
| 209 | /** What `run`'s own log says about how far it has got. */ | |
| 210 | export function viewOfRun(dataDir: string, run: Run): CheckView { | |
| 211 | const { lines } = readRunLog(dataDir, run.id, READ_MAX); | |
| 212 | return checkView(lines, isTerminal(run.state), run.state === "ok"); | |
| 213 | } |
src/checks/service.ts +12
| @@ -58,6 +58,7 @@ import { | ||
| 58 | 58 | STEP_HOSTS, |
| 59 | 59 | type CellSpec, |
| 60 | 60 | } from "../apps/contain.ts"; |
| 61 | import { stepLine, testsBegin } from "./progress.ts"; | |
| 61 | 62 | import { summariseCheckFailure } from "./summary.ts"; |
| 62 | 63 | import type { CheckDetail } from "../core/runs.ts"; |
| 63 | 64 | |
| @@ -427,6 +428,7 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 427 | 428 | FORGE_COMMIT: run.commitOid, |
| 428 | 429 | }); |
| 429 | 430 | if (manifest.install !== null) { |
| 431 | const installStart = Date.now(); | |
| 430 | 432 | const installed = await runStep("install", manifest.install, { |
| 431 | 433 | cwd: dir, |
| 432 | 434 | env, |
| @@ -435,6 +437,7 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 435 | 437 | timeoutMs: manifest.timeout * 1000, |
| 436 | 438 | cell, |
| 437 | 439 | }); |
| 440 | write(stepLine("install", installed.ok, installStart)); | |
| 438 | 441 | if (!installed.ok) return { ok: false, error: installed.error }; |
| 439 | 442 | } |
| 440 | 443 | // The check step's own lines are kept beside what `write` already |
| @@ -444,10 +447,18 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 444 | 447 | // command's own; that echo is not output to summarise from. |
| 445 | 448 | const checkLines: string[] = []; |
| 446 | 449 | let sawEcho = false; |
| 450 | const checkStart = Date.now(); | |
| 451 | // The typecheck ends where the tests begin: the first line the test | |
| 452 | // runner writes, if the command typechecks at all. | |
| 453 | let typechecking = /\btsc\b/.test(manifest.run); | |
| 447 | 454 | const ran = await runStep("check", manifest.run, { |
| 448 | 455 | cwd: dir, |
| 449 | 456 | env, |
| 450 | 457 | log: (line) => { |
| 458 | if (typechecking && sawEcho && testsBegin(line)) { | |
| 459 | typechecking = false; | |
| 460 | write(stepLine("typecheck", true, checkStart)); | |
| 461 | } | |
| 451 | 462 | if (sawEcho) checkLines.push(line); |
| 452 | 463 | sawEcho = true; |
| 453 | 464 | write(line); |
| @@ -456,6 +467,7 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 456 | 467 | timeoutMs: manifest.timeout * 1000, |
| 457 | 468 | cell, |
| 458 | 469 | }); |
| 470 | write(stepLine("check", ran.ok, checkStart)); | |
| 459 | 471 | if (!ran.ok) { |
| 460 | 472 | const { summary, detail } = summariseCheckFailure( |
| 461 | 473 | checkLines, |
src/feed/items.ts +62 −3
| @@ -10,8 +10,9 @@ import type { Database } from "bun:sqlite"; | ||
| 10 | 10 | import { listDecidedIn } from "../core/decisions.ts"; |
| 11 | 11 | import { audienceFilter } from "../core/event_audience.ts"; |
| 12 | 12 | import { listEventsVisibleTo, type ForgeEvent } from "../core/events.ts"; |
| 13 | import { getIssue } from "../core/issues.ts"; | |
| 13 | import { getIssue, getIssueById } from "../core/issues.ts"; | |
| 14 | 14 | import { getPull, toldBy } from "../core/pulls.ts"; |
| 15 | import { getRun } from "../core/runs.ts"; | |
| 15 | 16 | import { |
| 16 | 17 | canSeeOwnerFacts, |
| 17 | 18 | getReposByIds, |
| @@ -19,7 +20,7 @@ import { | ||
| 19 | 20 | type Repo, |
| 20 | 21 | } from "../core/repos.ts"; |
| 21 | 22 | import { getUserById, type User } from "../core/users.ts"; |
| 22 | import { watchedBy } from "../core/watches.ts"; | |
| 23 | import { isWatching, watchedBy } from "../core/watches.ts"; | |
| 23 | 24 | import { askLinesFor } from "../web/studio/asks.ts"; |
| 24 | 25 | |
| 25 | 26 | export type FeedKind = |
| @@ -154,7 +155,7 @@ export function candidatesFor( | ||
| 154 | 155 | for (const e of events) { |
| 155 | 156 | const repo = repos.get(e.repoId!); |
| 156 | 157 | if (!repo) continue; |
| 157 | const c = fromEvent(db, e, repo, owns(repo)); | |
| 158 | const c = checkOnMine(db, e, repo, viewer) ?? fromEvent(db, e, repo, owns(repo)); | |
| 158 | 159 | if (c) out.push({ ...c, actor: nameOf(e.actorId) }); |
| 159 | 160 | } |
| 160 | 161 | for (const a of asks) { |
| @@ -203,6 +204,64 @@ export function candidatesFor( | ||
| 203 | 204 | |
| 204 | 205 | type Partial = Omit<Candidate, "actor">; |
| 205 | 206 | |
| 207 | /** `38 min`, `2h 5m` — the gate is minutes long, so seconds are noise. */ | |
| 208 | function took(ms: number): string { | |
| 209 | const minutes = Math.max(1, Math.round(ms / 60_000)); | |
| 210 | return minutes < 120 ? `${minutes} min` : `${Math.floor(minutes / 60)}h ${minutes % 60}m`; | |
| 211 | } | |
| 212 | ||
| 213 | /** | |
| 214 | * A check starting or ending on a pull the viewer opened or watches: "Checks | |
| 215 | * on #1101 passed in 38 min", "failed: 2 of 2052 tests — \"name\"". Anyone | |
| 216 | * else's check falls through to the plain run item. A start is only news | |
| 217 | * while the check is still going; the finish replaces it. | |
| 218 | */ | |
| 219 | function checkOnMine( | |
| 220 | db: Database, | |
| 221 | e: ForgeEvent, | |
| 222 | repo: Repo, | |
| 223 | viewer: User, | |
| 224 | ): Partial | undefined { | |
| 225 | if (e.kind !== "run.started" && e.kind !== "run.finished") return undefined; | |
| 226 | const p = e.payload; | |
| 227 | if (p["kind"] !== "check" || typeof p["issueId"] !== "string") return undefined; | |
| 228 | const run = getRun(db, String(p["runId"] ?? "")); | |
| 229 | if (run === undefined || run.issueId === null) return undefined; | |
| 230 | const issue = getIssueById(db, run.issueId); | |
| 231 | const pull = issue === undefined ? undefined : getPull(db, repo.id, issue.number); | |
| 232 | if (pull === undefined) return undefined; | |
| 233 | if (pull.authorId !== viewer.id && !isWatching(db, viewer.id, pull.id)) | |
| 234 | return undefined; | |
| 235 | const on = `Checks on #${pull.number}`; | |
| 236 | const link = `${base(repo)}/runs/${run.id}`; | |
| 237 | const common = { | |
| 238 | id: `event:${e.id}`, | |
| 239 | kind: "run" as const, | |
| 240 | repoId: repo.id, | |
| 241 | app: slug(repo), | |
| 242 | actorId: e.actorId, | |
| 243 | at: e.createdAt, | |
| 244 | link, | |
| 245 | }; | |
| 246 | if (e.kind === "run.started") | |
| 247 | return run.state === "running" | |
| 248 | ? { ...common, urgency: WATCH_URGENCY, what: `${on} are running` } | |
| 249 | : undefined; | |
| 250 | if (run.state !== "ok" && run.state !== "failed") return undefined; | |
| 251 | const ms = | |
| 252 | run.startedAt === null || run.finishedAt === null | |
| 253 | ? undefined | |
| 254 | : Date.parse(run.finishedAt) - Date.parse(run.startedAt); | |
| 255 | const after = ms === undefined ? "" : ` in ${took(ms)}`; | |
| 256 | return run.state === "ok" | |
| 257 | ? { ...common, urgency: WATCH_URGENCY, what: `${on} passed${after}` } | |
| 258 | : { | |
| 259 | ...common, | |
| 260 | urgency: LANDED_URGENCY, | |
| 261 | what: `${on} failed${after}${run.summary === "" ? "" : `: ${clip(run.summary)}`}`, | |
| 262 | }; | |
| 263 | } | |
| 264 | ||
| 206 | 265 | function fromEvent( |
| 207 | 266 | db: Database, |
| 208 | 267 | e: ForgeEvent, |
src/http/api.ts +7 −1
| @@ -282,6 +282,7 @@ import { resolveOid } from "../git/merge.ts"; | ||
| 282 | 282 | import { readUsage, usageDays, USAGE_DAYS_MAX } from "../actions/usage.ts"; |
| 283 | 283 | import { readReturns, reportSpend, RETURNS_DAYS } from "../actions/returns.ts"; |
| 284 | 284 | import { fleetSelection, readFleetGraph } from "../actions/fleet-graph.ts"; |
| 285 | import { viewOfRun } from "../checks/progress.ts"; | |
| 285 | 286 | import { readRunLog, subscribeRunLog } from "../runs/log.ts"; |
| 286 | 287 | import { blobForRender, commitLog, listRefs, listTree } from "../git/read.ts"; |
| 287 | 288 | import { repoDir } from "../git/repos.ts"; |
| @@ -4365,7 +4366,12 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 4365 | 4366 | const repo = loadRepo(ctx); |
| 4366 | 4367 | if (!repo) return notFound(); |
| 4367 | 4368 | const run = getRepoRun(ctx.app, ctx.auth.user, repo, ctx.params.id ?? ""); |
| 4368 | return run.ok ? json(runForViewer(ctx, repo)(run.value)) : notFound(); | |
| 4369 | if (!run.ok) return notFound(); | |
| 4370 | const shown = runForViewer(ctx, repo)(run.value); | |
| 4371 | // A check says how far it is, read from its own log (`checks/progress.ts`). | |
| 4372 | if (shown.kind !== "check") return json(shown); | |
| 4373 | const { steps, progress } = viewOfRun(ctx.app.config.dataDir, shown); | |
| 4374 | return json({ ...shown, steps, progress }); | |
| 4369 | 4375 | }); |
| 4370 | 4376 | |
| 4371 | 4377 | // The log: text, or a live tail as server-sent events (`?follow=1`, or |
src/http/mcp.ts +52 −1
| @@ -34,7 +34,10 @@ import { | ||
| 34 | 34 | } from "../actions/dependencies.ts"; |
| 35 | 35 | import { holdPull, releasePull } from "../actions/holds.ts"; |
| 36 | 36 | import { openPlan, parsePlan, PLAN_MAX } from "../actions/plan.ts"; |
| 37 | import { cancelRepoRun } from "../actions/runs.ts"; | |
| 37 | import { cancelRepoRun, getRepoRun, listRepoRuns } from "../actions/runs.ts"; | |
| 38 | import type { Run } from "../core/runs.ts"; | |
| 39 | import { viewOfRun } from "../checks/progress.ts"; | |
| 40 | import { elapsedMs } from "../web/studio/check.ts"; | |
| 38 | 41 | import { clientRows } from "../spawn/clients.ts"; |
| 39 | 42 | import { reissueDoor, spawnDoor } from "../spawn/door.ts"; |
| 40 | 43 | import { tell, toldWords, UNVERIFIED_WORDS } from "../actions/tell.ts"; |
| @@ -561,6 +564,54 @@ const TOOLS: readonly Tool[] = [ | ||
| 561 | 564 | ]); |
| 562 | 565 | }, |
| 563 | 566 | }, |
| 567 | { | |
| 568 | name: "check", | |
| 569 | description: | |
| 570 | "How a check is doing: its state, how long it has gone, its steps (install, typecheck, tests) and how far the tests are (`files` started of the gate's `total`, `passed`, `failed`, the file `current`ly running, and the `failures` so far with their first lines). Name a `pull` number for that pull's newest check, a `run` id for one check, or neither for the repository's newest.", | |
| 571 | inputSchema: { | |
| 572 | type: "object", | |
| 573 | properties: { | |
| 574 | repo: REPO_ARG, | |
| 575 | pull: { type: "number" }, | |
| 576 | run: { type: "string" }, | |
| 577 | }, | |
| 578 | required: ["repo"], | |
| 579 | }, | |
| 580 | writes: false, | |
| 581 | run(ctx, user, args) { | |
| 582 | const repo = repoArg(ctx, user, args); | |
| 583 | if (isAnswer(repo)) return repo; | |
| 584 | const pull = | |
| 585 | args["pull"] === undefined | |
| 586 | ? undefined | |
| 587 | : getPull(ctx.app.db, repo.id, Number(args["pull"])); | |
| 588 | if (args["pull"] !== undefined && pull === undefined) | |
| 589 | return bad("not_found", "no such pull here"); | |
| 590 | let run: Run | undefined; | |
| 591 | if (args["run"] === undefined) { | |
| 592 | const listed = listRepoRuns(ctx.app, user, repo, { | |
| 593 | kind: "check", | |
| 594 | limit: 1, | |
| 595 | ...(pull === undefined ? {} : { issueId: pull.id }), | |
| 596 | }); | |
| 597 | run = listed.ok ? listed.value[0] : undefined; | |
| 598 | } else { | |
| 599 | const one = getRepoRun(ctx.app, user, repo, str(args["run"])); | |
| 600 | run = one.ok ? one.value : undefined; | |
| 601 | } | |
| 602 | if (run === undefined || run.kind !== "check") | |
| 603 | return bad("not_found", "no such check here"); | |
| 604 | const ms = elapsedMs(run); | |
| 605 | return good({ | |
| 606 | run: run.id, | |
| 607 | commit: run.commitOid, | |
| 608 | state: run.state, | |
| 609 | summary: run.summary, | |
| 610 | seconds: ms === undefined ? null : Math.round(ms / 1000), | |
| 611 | ...viewOfRun(ctx.app.config.dataDir, run), | |
| 612 | }); | |
| 613 | }, | |
| 614 | }, | |
| 564 | 615 | { |
| 565 | 616 | name: "stop", |
| 566 | 617 | description: |
src/http/server.ts +4
| @@ -224,6 +224,7 @@ import { | ||
| 224 | 224 | import { |
| 225 | 225 | runCancel, |
| 226 | 226 | runPage, |
| 227 | runProgressFragment, | |
| 227 | 228 | runRedirect, |
| 228 | 229 | runSteer, |
| 229 | 230 | runsList, |
| @@ -251,6 +252,7 @@ import { | ||
| 251 | 252 | pullMergeSubmit, |
| 252 | 253 | pullReviewStateSubmit, |
| 253 | 254 | pullReviewSubmit, |
| 255 | pullChecksFragment, | |
| 254 | 256 | pullThreadFragment, |
| 255 | 257 | threadResolveSubmit, |
| 256 | 258 | } from "../web/pages/pull.ts"; |
| @@ -911,6 +913,7 @@ export function buildRouter(): Router<Handler> { | ||
| 911 | 913 | // box, one tab under the change itself. |
| 912 | 914 | router.add("GET", "/:owner/:name/pulls/:number/files", pullFilesPage); |
| 913 | 915 | router.add("GET", "/:owner/:name/pulls/:number/thread", pullThreadFragment); |
| 916 | router.add("GET", "/:owner/:name/pulls/:number/checks", pullChecksFragment); | |
| 914 | 917 | router.add("POST", "/:owner/:name/pulls/:number/comments", pullCommentSubmit); |
| 915 | 918 | router.add("POST", "/:owner/:name/pulls/:number/review", pullReviewSubmit); |
| 916 | 919 | // The verdict on the change as a whole, and the threads it settles. |
| @@ -958,6 +961,7 @@ export function buildRouter(): Router<Handler> { | ||
| 958 | 961 | router.add("POST", "/:owner/:name/app/:env/stop", appStop); |
| 959 | 962 | router.add("GET", "/:owner/:name/runs", runsList); |
| 960 | 963 | router.add("GET", "/:owner/:name/runs/:id", runPage); |
| 964 | router.add("GET", "/:owner/:name/runs/:id/progress", runProgressFragment); | |
| 961 | 965 | router.add("POST", "/:owner/:name/runs/:id/cancel", runCancel); |
| 962 | 966 | router.add("POST", "/:owner/:name/runs/:id/steer", runSteer); |
| 963 | 967 | // The pictures of an environment, read like the repository. |
src/web/client.ts +41
| @@ -2019,3 +2019,44 @@ export const liveRegionsJs = /* js */ ` | ||
| 2019 | 2019 | }); |
| 2020 | 2020 | })(); |
| 2021 | 2021 | `; |
| 2022 | ||
| 2023 | /** | |
| 2024 | * A region that moves between events asks for itself on a beat | |
| 2025 | * (`data-live-poll` seconds, at its `data-live-src`): a check's progress | |
| 2026 | * writes no event, only lines. Conditional on the etag, so a quiet check | |
| 2027 | * costs a 304; the region drops the attribute when it is over, and the | |
| 2028 | * beat stops. | |
| 2029 | */ | |
| 2030 | export const checkPollJs = /* js */ ` | |
| 2031 | (function () { | |
| 2032 | var tags = {}; | |
| 2033 | function pull(box) { | |
| 2034 | var src = box.getAttribute('data-live-src'); | |
| 2035 | if (!src) return; | |
| 2036 | var headers = { accept: 'text/html' }; | |
| 2037 | if (tags[box.id]) headers['if-none-match'] = tags[box.id]; | |
| 2038 | fetch(src, { headers: headers, credentials: 'same-origin' }) | |
| 2039 | .then(function (r) { | |
| 2040 | if (!r.ok) return null; | |
| 2041 | var tag = r.headers.get('etag'); | |
| 2042 | if (tag) tags[box.id] = tag; | |
| 2043 | return r.text(); | |
| 2044 | }) | |
| 2045 | .then(function (text) { | |
| 2046 | if (!text) return; | |
| 2047 | var holder = document.createElement('template'); | |
| 2048 | holder.innerHTML = text; | |
| 2049 | var next = holder.content.firstElementChild; | |
| 2050 | var mine = document.getElementById(box.id); | |
| 2051 | if (next && mine && next.id === box.id) mine.replaceWith(next); | |
| 2052 | }) | |
| 2053 | .catch(function () {}); | |
| 2054 | } | |
| 2055 | var beat = window.setInterval(function () { | |
| 2056 | var boxes = document.querySelectorAll('[data-live-poll]'); | |
| 2057 | if (boxes.length === 0) { window.clearInterval(beat); return; } | |
| 2058 | if (document.hidden) return; | |
| 2059 | Array.prototype.forEach.call(boxes, pull); | |
| 2060 | }, 5000); | |
| 2061 | })(); | |
| 2062 | `; |
src/web/pages/pull.ts +44 −3
| @@ -42,6 +42,7 @@ import { | ||
| 42 | 42 | } from "../../core/reviews.ts"; |
| 43 | 43 | import { getUsersByIds } from "../../core/users.ts"; |
| 44 | 44 | import { canWrite, getRepoById, type Repo } from "../../core/repos.ts"; |
| 45 | import { viewOfRun } from "../../checks/progress.ts"; | |
| 45 | 46 | import { commitLog } from "../../git/read.ts"; |
| 46 | 47 | import { repoDir } from "../../git/repos.ts"; |
| 47 | 48 | import { wallOf } from "../../contract/policy.ts"; |
| @@ -54,6 +55,7 @@ import { | ||
| 54 | 55 | repoPalette, |
| 55 | 56 | signInFirst, |
| 56 | 57 | } from "../chrome.ts"; |
| 58 | import { checkPollJs } from "../client.ts"; | |
| 57 | 59 | import { diffSyntax } from "../diff-syntax.ts"; |
| 58 | 60 | import { |
| 59 | 61 | renderDiff, |
| @@ -62,7 +64,7 @@ import { | ||
| 62 | 64 | } from "../diff-view.ts"; |
| 63 | 65 | import { field, readForm } from "../form.ts"; |
| 64 | 66 | import { html, raw, when, type Html } from "../html.ts"; |
| 65 | import { flashTo, page, redirect } from "../layout.ts"; | |
| 67 | import { flashTo, fragment, page, redirect } from "../layout.ts"; | |
| 66 | 68 | import { liveClientJs, liveMount } from "../live.ts"; |
| 67 | 69 | import { |
| 68 | 70 | avatar, |
| @@ -83,6 +85,7 @@ import { | ||
| 83 | 85 | threadFragment, |
| 84 | 86 | } from "./thread.ts"; |
| 85 | 87 | import { crewActivityCard } from "./runs.ts"; |
| 88 | import { checkRegion } from "../studio/check.ts"; | |
| 86 | 89 | import { holdCard } from "../studio/hold.ts"; |
| 87 | 90 | import { |
| 88 | 91 | changeHead, |
| @@ -189,6 +192,39 @@ function reviewsChip( | ||
| 189 | 192 | const WAITING_WORDS = |
| 190 | 193 | "Checks and the preview wait for a writer to approve this head"; |
| 191 | 194 | |
| 195 | /** | |
| 196 | * The region that watches the head's newest check, when that is a check | |
| 197 | * (`studio/check.ts`); undefined when the newest run is a deploy or there | |
| 198 | * is none, and the plain line says what it always said. | |
| 199 | */ | |
| 200 | export function pullCheckRegion( | |
| 201 | ctx: Ctx, | |
| 202 | repo: Repo, | |
| 203 | pull: Pull, | |
| 204 | headOid: string, | |
| 205 | ): Html | undefined { | |
| 206 | const run = checkSummary(ctx.app.db, pull.id, headOid).run; | |
| 207 | if (run === null || run.kind !== "check") return undefined; | |
| 208 | return checkRegion( | |
| 209 | repoBase(repo), | |
| 210 | pull.number, | |
| 211 | run, | |
| 212 | viewOfRun(ctx.app.config.dataDir, run), | |
| 213 | ); | |
| 214 | } | |
| 215 | ||
| 216 | // GET /:owner/:name/pulls/:number/checks — the region alone, for the page | |
| 217 | // that polls it while the check goes. | |
| 218 | export function pullChecksFragment(ctx: Ctx): Response { | |
| 219 | const found = pullContext(ctx); | |
| 220 | if (found instanceof Response) return fragment(html``, { status: 404 }); | |
| 221 | const { repo, pull } = found; | |
| 222 | const region = pullCheckRegion(ctx, repo, pull, pull.headOid); | |
| 223 | return region === undefined | |
| 224 | ? fragment(html``, { status: 404 }) | |
| 225 | : fragment(region, { request: ctx.request }); | |
| 226 | } | |
| 227 | ||
| 192 | 228 | /** |
| 193 | 229 | * The checks line: where the deploy of this head stands (docs/APPS.md, rule |
| 194 | 230 | * 4). Rendered whenever the repository runs as an app or requires checks — |
| @@ -202,6 +238,8 @@ function checksLine( | ||
| 202 | 238 | isApp: boolean, |
| 203 | 239 | /** The head is a reader's and no writer has approved it yet. */ |
| 204 | 240 | waiting = false, |
| 241 | /** The newest run is a check: its own region, which names it and says how far it is. */ | |
| 242 | region?: Html, | |
| 205 | 243 | ): Html { |
| 206 | 244 | if (!isApp && !required && check.state === "none" && !waiting) return html``; |
| 207 | 245 | const run = check.run; |
| @@ -213,7 +251,9 @@ function checksLine( | ||
| 213 | 251 | >deploy ${run.commitOid.slice(0, 7)}</a |
| 214 | 252 | >`; |
| 215 | 253 | const body = |
| 216 | check.state === "ok" | |
| 254 | region !== undefined | |
| 255 | ? region | |
| 256 | : check.state === "ok" | |
| 217 | 257 | ? html`<span class="ok">${icon("check")}Checks passed</span>${link}` |
| 218 | 258 | : check.state === "failed" |
| 219 | 259 | ? html`<span class="bad">${icon("x")}Checks failed</span>${link}` |
| @@ -265,6 +305,7 @@ function mergeBox( | ||
| 265 | 305 | repo.requireChecks, |
| 266 | 306 | isApp, |
| 267 | 307 | !pullMayRun(ctx.app.db, repo, pull, status.headOid), |
| 308 | pullCheckRegion(ctx, repo, pull, status.headOid), | |
| 268 | 309 | ); |
| 269 | 310 | const reopen = html`<form method="post" action="${thread}/state"> |
| 270 | 311 | <input type="hidden" name="state" value="open" /> |
| @@ -778,7 +819,7 @@ export async function pullFilesPage(ctx: Ctx): Promise<Response> { | ||
| 778 | 819 | palette: repoPalette(ctx, repo), |
| 779 | 820 | body, |
| 780 | 821 | styleCss: labelCss([...allLabels, ...applied]), |
| 781 | scriptJs: liveClientJs, | |
| 822 | scriptJs: `${liveClientJs}\n${checkPollJs}`, | |
| 782 | 823 | }); |
| 783 | 824 | } |
| 784 | 825 |
src/web/pages/runs.ts +22 −1
| @@ -32,7 +32,8 @@ import type { Ctx } from "../../http/context.ts"; | ||
| 32 | 32 | import { loadRepo, notFoundPage, repoBase, signInFirst } from "../chrome.ts"; |
| 33 | 33 | import { field, readForm } from "../form.ts"; |
| 34 | 34 | import { html, when, type Html } from "../html.ts"; |
| 35 | import { flashTo, page, redirect } from "../layout.ts"; | |
| 35 | import { flashTo, fragment, page, redirect } from "../layout.ts"; | |
| 36 | import { checkStepsRegion } from "../studio/check.ts"; | |
| 36 | 37 | import { runsListPage } from "../studio/lists.ts"; |
| 37 | 38 | import { runView } from "../studio/run.ts"; |
| 38 | 39 | import { |
| @@ -70,6 +71,13 @@ export function runTitle( | ||
| 70 | 71 | : `#${issue.number} ${issue.title}`; |
| 71 | 72 | if (run.kind === "deploy") |
| 72 | 73 | return about === "" ? "Deploy production" : `Deploy preview ${about}`; |
| 74 | if (run.kind === "check") { | |
| 75 | const number = | |
| 76 | issue === undefined ? envNumber(run.env) : issue.number; | |
| 77 | const head = run.commitOid.slice(0, 7); | |
| 78 | const of = head === "" ? "Check" : `Check of ${head}`; | |
| 79 | return number === undefined ? of : `${of} for #${number}`; | |
| 80 | } | |
| 73 | 81 | const who = actor ?? "an agent"; |
| 74 | 82 | return about === "" ? `${who} at work` : `${who} on ${about}`; |
| 75 | 83 | } |
| @@ -302,6 +310,19 @@ export async function runPage(ctx: Ctx): Promise<Response> { | ||
| 302 | 310 | return await runView(ctx, repo, run, lines, truncated); |
| 303 | 311 | } |
| 304 | 312 | |
| 313 | // GET /:owner/:name/runs/:id/progress — a check's steps alone, for the | |
| 314 | // region on its page that asks for itself while the check goes. | |
| 315 | export function runProgressFragment(ctx: Ctx): Response { | |
| 316 | const repo = loadRepo(ctx); | |
| 317 | if (!repo) return fragment(html``, { status: 404 }); | |
| 318 | const found = runLog(ctx.app, ctx.auth.user, repo, ctx.params.id ?? ""); | |
| 319 | if (!found.ok || found.value.run.kind !== "check") | |
| 320 | return fragment(html``, { status: 404 }); | |
| 321 | return fragment(checkStepsRegion(ctx, repoBase(repo), found.value.run), { | |
| 322 | request: ctx.request, | |
| 323 | }); | |
| 324 | } | |
| 325 | ||
| 305 | 326 | // POST /:owner/:name/runs/:id/steer { text } |
| 306 | 327 | export async function runSteer(ctx: Ctx): Promise<Response> { |
| 307 | 328 | const repo = loadRepo(ctx); |
src/web/studio/change.ts +19 −2
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | * bigger. |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | import { viewOfRun } from "../../checks/progress.ts"; | |
| 12 | 13 | import { mayEditIssue } from "../../actions/issues.ts"; |
| 13 | 14 | import { marksByName, type Mark } from "../../core/marks.ts"; |
| 14 | 15 | import { mayView } from "../../core/sealed.ts"; |
| @@ -41,7 +42,9 @@ import { html, when, type Html } from "../html.ts"; | ||
| 41 | 42 | import type { IconName as Glyph } from "../icons.ts"; |
| 42 | 43 | import { page, redirect } from "../layout.ts"; |
| 43 | 44 | import { renderMarkdown } from "../markdown.ts"; |
| 45 | import { checkPollJs } from "../client.ts"; | |
| 44 | 46 | import { liveClientJs, liveMount } from "../live.ts"; |
| 47 | import { checkRegion } from "./check.ts"; | |
| 45 | 48 | import { |
| 46 | 49 | btn, |
| 47 | 50 | card, |
| @@ -325,9 +328,19 @@ async function changeConversation( | ||
| 325 | 328 | // after, it is production's — the preview's run may already be torn |
| 326 | 329 | // down, and the word above is judged by production regardless. |
| 327 | 330 | let deploy: { id: string; commitOid: string } | undefined; |
| 331 | // A check is not a deploy: when the head's newest run is one, the line | |
| 332 | // names it and says how far it is, and keeps saying so (`studio/check.ts`). | |
| 333 | let watched: Html | undefined; | |
| 328 | 334 | if (pull.mergedAt === null) { |
| 329 | 335 | const run = checkSummary(db, pull.id, pull.headOid).run; |
| 330 | if (run) deploy = { id: run.id, commitOid: run.commitOid }; | |
| 336 | if (run?.kind === "check") | |
| 337 | watched = checkRegion( | |
| 338 | base, | |
| 339 | pull.number, | |
| 340 | run, | |
| 341 | viewOfRun(ctx.app.config.dataDir, run), | |
| 342 | ); | |
| 343 | else if (run) deploy = { id: run.id, commitOid: run.commitOid }; | |
| 331 | 344 | } else { |
| 332 | 345 | const production = |
| 333 | 346 | record === undefined |
| @@ -405,6 +418,10 @@ async function changeConversation( | ||
| 405 | 418 | (t) => html`<span class="where-sep" aria-hidden="true">·</span |
| 406 | 419 | >${tellLine(base, t)}`, |
| 407 | 420 | )} |
| 421 | ${when( | |
| 422 | watched, | |
| 423 | (w) => html`<span class="where-sep" aria-hidden="true">·</span>${w}`, | |
| 424 | )} | |
| 408 | 425 | ${when( |
| 409 | 426 | deploy, |
| 410 | 427 | (d) => |
| @@ -534,6 +551,6 @@ async function changeConversation( | ||
| 534 | 551 | crumbs: crumbsFor(repo), |
| 535 | 552 | palette: repoPalette(ctx, repo), |
| 536 | 553 | body, |
| 537 | scriptJs: liveClientJs, | |
| 554 | scriptJs: `${liveClientJs}\n${checkPollJs}`, | |
| 538 | 555 | }); |
| 539 | 556 | } |
src/web/studio/check.ts added +189
| @@ -0,0 +1,189 @@ | ||
| 1 | /** | |
| 2 | * A check, watched: the line on a pull that names it and says how far it | |
| 3 | * is, and the run page's steps, progress bar and pinned failures. Both read | |
| 4 | * `checks/progress.ts`, which reads the run's own log — the same lines the | |
| 5 | * API's run json carries — and both are regions that ask for themselves | |
| 6 | * (`data-live-src`, `data-live-poll`) because a check's progress writes no | |
| 7 | * event of its own. | |
| 8 | */ | |
| 9 | ||
| 10 | import type { CheckFailure, CheckProgress, CheckStep, CheckView } from "../../checks/progress.ts"; | |
| 11 | import { viewOfRun } from "../../checks/progress.ts"; | |
| 12 | import { isTerminal, type Run } from "../../core/runs.ts"; | |
| 13 | import type { Ctx } from "../../http/context.ts"; | |
| 14 | import { html, when, type Html } from "../html.ts"; | |
| 15 | import { icon, listCard, row } from "../ui.ts"; | |
| 16 | ||
| 17 | /** `12 min`, `1h 5m` — a gate is minutes long, so seconds are noise. */ | |
| 18 | export function minutesText(ms: number): string { | |
| 19 | const minutes = Math.floor(Math.max(0, ms) / 60_000); | |
| 20 | if (minutes < 1) return "under a minute"; | |
| 21 | if (minutes < 120) return `${minutes} min`; | |
| 22 | return `${Math.floor(minutes / 60)}h ${minutes % 60}m`; | |
| 23 | } | |
| 24 | ||
| 25 | /** How long the run has gone, or went. */ | |
| 26 | export function elapsedMs(run: Run, now = Date.now()): number | undefined { | |
| 27 | if (run.startedAt === null) return undefined; | |
| 28 | const end = run.finishedAt === null ? now : Date.parse(run.finishedAt); | |
| 29 | return end - Date.parse(run.startedAt); | |
| 30 | } | |
| 31 | ||
| 32 | const PHASE: Record<CheckStep["name"], string> = { | |
| 33 | install: "installing", | |
| 34 | typecheck: "typechecking", | |
| 35 | tests: "testing", | |
| 36 | }; | |
| 37 | ||
| 38 | /** `61 of 170 files · 812 passed, 0 failed` — only what the lines have said. */ | |
| 39 | export function progressWords(p: CheckProgress): string { | |
| 40 | const files = | |
| 41 | p.total > 0 ? `${p.files} of ${p.total} files` : `${p.files} files`; | |
| 42 | return `${files} · ${p.passed} passed, ${p.failed} failed`; | |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * The checks line for a run of kind `check`: its state, how long, how far, | |
| 47 | * linked to the run. `base` is the repository's address. | |
| 48 | */ | |
| 49 | export function checkBody(base: string, run: Run, view: CheckView): Html { | |
| 50 | const href = `${base}/runs/${run.id}`; | |
| 51 | const short = run.commitOid.slice(0, 7); | |
| 52 | const ms = elapsedMs(run); | |
| 53 | const sep = html`<span class="where-sep" aria-hidden="true">·</span>`; | |
| 54 | const took = ms === undefined ? null : html`${sep}<span>${minutesText(ms)}</span>`; | |
| 55 | const p = view.progress; | |
| 56 | const running = view.steps.find((s) => s.state === "running"); | |
| 57 | switch (run.state) { | |
| 58 | case "queued": | |
| 59 | return html`<span class="now">${icon("loader")}<a href="${href}">Check queued</a></span>${sep}<span>${short}</span>`; | |
| 60 | case "running": | |
| 61 | return html`<span class="now">${icon("loader")}<a href="${href}">Check running</a></span>${took}${sep}<span class="${p.failed > 0 ? "bad" : ""}">${p.files > 0 | |
| 62 | ? progressWords(p) | |
| 63 | : running | |
| 64 | ? PHASE[running.name] | |
| 65 | : "starting"}</span>`; | |
| 66 | case "ok": | |
| 67 | return html`<span class="ok">${icon("check")}<a href="${href}">Check passed</a></span>${took}${sep}<span>${p.passed} passed</span>`; | |
| 68 | case "failed": | |
| 69 | return html`<span class="bad">${icon("x")}<a href="${href}">Check failed</a></span>${took}${when( | |
| 70 | run.summary, | |
| 71 | (s) => html`${sep}<span>${s}</span>`, | |
| 72 | )}`; | |
| 73 | case "cancelled": | |
| 74 | return html`<span>${icon("ban")}<a href="${href}">Check cancelled</a></span>${took}`; | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | /** The inline region a pull carries for its newest check: it asks for itself while the check goes. */ | |
| 79 | export function checkRegion( | |
| 80 | base: string, | |
| 81 | pullNumber: number, | |
| 82 | run: Run, | |
| 83 | view: CheckView, | |
| 84 | ): Html { | |
| 85 | return html`<span | |
| 86 | id="live-check" | |
| 87 | data-live-src="${base}/pulls/${pullNumber}/checks" | |
| 88 | ${isTerminal(run.state) ? null : html`data-live-poll="5"`} | |
| 89 | >${checkBody(base, run, view)}</span | |
| 90 | >`; | |
| 91 | } | |
| 92 | ||
| 93 | const STEP_ICON = { | |
| 94 | pending: "clock", | |
| 95 | running: "loader", | |
| 96 | ok: "check", | |
| 97 | failed: "x", | |
| 98 | } as const; | |
| 99 | ||
| 100 | const STEP_WORD: Record<CheckStep["name"], string> = { | |
| 101 | install: "Install", | |
| 102 | typecheck: "Typecheck", | |
| 103 | tests: "Tests", | |
| 104 | }; | |
| 105 | ||
| 106 | function failureRow(f: CheckFailure): Html { | |
| 107 | return row({ | |
| 108 | lead: icon("x"), | |
| 109 | title: f.name, | |
| 110 | meta: html`${f.file ?? ""}${f.lines.length > 0 ? html` · <span class="mono">${f.lines.join(" ⏎ ")}</span>` : null}`, | |
| 111 | }); | |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * A check's steps as rows, the tests step carrying the bar, and every | |
| 116 | * failure so far pinned above the full tail the page keeps below. | |
| 117 | */ | |
| 118 | export function checkSteps(base: string, run: Run, view: CheckView): Html { | |
| 119 | const p = view.progress; | |
| 120 | const ms = elapsedMs(run); | |
| 121 | // The step running has no end line yet: its time is what is left of the | |
| 122 | // run's once the finished ones are taken out. | |
| 123 | const done = view.steps.reduce((sum, s) => sum + (s.seconds ?? 0), 0); | |
| 124 | const secondsOf = (s: CheckStep): number | null => | |
| 125 | s.seconds !== null | |
| 126 | ? s.seconds | |
| 127 | : s.state === "running" && ms !== undefined | |
| 128 | ? Math.max(0, Math.round(ms / 1000) - done) | |
| 129 | : null; | |
| 130 | const rows = view.steps.map((s) => { | |
| 131 | const seconds = secondsOf(s); | |
| 132 | const bar = | |
| 133 | s.name === "tests" && (p.files > 0 || s.state !== "pending") | |
| 134 | ? html`<progress | |
| 135 | value="${p.files}" | |
| 136 | max="${Math.max(p.total, p.files, 1)}" | |
| 137 | aria-label="Test files started" | |
| 138 | ></progress> | |
| 139 | <span class="muted small" | |
| 140 | >${progressWords(p)}${p.current && s.state === "running" | |
| 141 | ? html` · now ${p.current}` | |
| 142 | : null}</span | |
| 143 | >` | |
| 144 | : undefined; | |
| 145 | return row({ | |
| 146 | lead: icon(STEP_ICON[s.state]), | |
| 147 | title: STEP_WORD[s.name], | |
| 148 | ...(bar ? { meta: bar } : {}), | |
| 149 | trail: html`<span class="num muted small" | |
| 150 | >${seconds === null ? "" : durationSeconds(seconds)}</span | |
| 151 | >`, | |
| 152 | }); | |
| 153 | }); | |
| 154 | return html`${listCard(rows, { | |
| 155 | head: html`${icon("check")} | |
| 156 | <h2>Steps</h2>`, | |
| 157 | })} | |
| 158 | ${when( | |
| 159 | p.failures.length > 0, | |
| 160 | () => | |
| 161 | html`<div class="mt"> | |
| 162 | ${listCard(p.failures.map(failureRow), { | |
| 163 | head: html`${icon("x")} | |
| 164 | <h2>${p.failed} failing so far</h2>`, | |
| 165 | })} | |
| 166 | </div>`, | |
| 167 | )}`; | |
| 168 | } | |
| 169 | ||
| 170 | const durationSeconds = (s: number): string => | |
| 171 | s < 60 ? `${s}s` : `${Math.floor(s / 60)}m ${s % 60}s`; | |
| 172 | ||
| 173 | /** The run page's steps region, asking for itself while the check goes. */ | |
| 174 | export function checkStepsRegion( | |
| 175 | ctx: Ctx, | |
| 176 | base: string, | |
| 177 | run: Run, | |
| 178 | ): Html { | |
| 179 | const view = viewOfRun(ctx.app.config.dataDir, run); | |
| 180 | return html`<div | |
| 181 | id="live-steps" | |
| 182 | class="mb" | |
| 183 | data-live-src="${base}/runs/${run.id}/progress" | |
| 184 | ${isTerminal(run.state) ? null : html`data-live-poll="5"`} | |
| 185 | > | |
| 186 | ${checkSteps(base, run, view)} | |
| 187 | </div>`; | |
| 188 | } | |
| 189 |
src/web/studio/lists.ts +21 −10
| @@ -53,6 +53,8 @@ import { | ||
| 53 | 53 | time, |
| 54 | 54 | } from "../ui.ts"; |
| 55 | 55 | import { actorsFor, issuesFor, runTitleFrom } from "../pages/runs.ts"; |
| 56 | import type { Run } from "../../core/runs.ts"; | |
| 57 | import { elapsedMs, minutesText } from "./check.ts"; | |
| 56 | 58 | import { originGlyph } from "../pages/thread.ts"; |
| 57 | 59 | import { changeHref, PAGE_TITLES, pageTitle, tellHref } from "./nouns.ts"; |
| 58 | 60 | import { tellLine, tellsAnswered } from "./branch_tell.ts"; |
| @@ -553,14 +555,22 @@ export async function changesList(ctx: Ctx): Promise<Response> { | ||
| 553 | 555 | }); |
| 554 | 556 | } |
| 555 | 557 | |
| 556 | /** Which runs a list shows: the crew's by default, or the deploys or the | |
| 557 | * checks that `?kind=` asks for — the narrowing dev's list offered, kept | |
| 558 | * so a link written to it keeps meaning what it said. */ | |
| 559 | type RunsKind = "agent" | "deploy" | "check"; | |
| 558 | /** Which runs a list shows: all of them by default, or the crew's, the | |
| 559 | * deploys or the checks that `?kind=` narrows to — a link written to the | |
| 560 | * old default (`?kind=agent` was implicit) keeps meaning the crew's. */ | |
| 561 | type RunsKind = "all" | "agent" | "deploy" | "check"; | |
| 560 | 562 | |
| 561 | 563 | function readRunsKind(ctx: Ctx): RunsKind { |
| 562 | 564 | const raw = ctx.url.searchParams.get("kind"); |
| 563 | return raw === "deploy" || raw === "check" ? raw : "agent"; | |
| 565 | return raw === "deploy" || raw === "check" || raw === "agent" ? raw : "all"; | |
| 566 | } | |
| 567 | ||
| 568 | /** A check's row says how long it went (or has gone) beside its word. */ | |
| 569 | function checkMeta(run: Run): string { | |
| 570 | const ms = elapsedMs(run); | |
| 571 | const state = | |
| 572 | run.state === "ok" ? "passed" : run.state === "running" ? "running" : run.state; | |
| 573 | return ms === undefined ? state : `${state} · ${minutesText(ms)}`; | |
| 564 | 574 | } |
| 565 | 575 | |
| 566 | 576 | /** The runs list's own address with a word, a kind and a cursor. */ |
| @@ -571,7 +581,7 @@ function runsHref( | ||
| 571 | 581 | cursor?: string, |
| 572 | 582 | ): string { |
| 573 | 583 | const params = new URLSearchParams(); |
| 574 | if (kind !== "agent") params.set("kind", kind); | |
| 584 | if (kind !== "all") params.set("kind", kind); | |
| 575 | 585 | if (word !== undefined) params.set("word", word); |
| 576 | 586 | if (cursor !== undefined) params.set("cursor", cursor); |
| 577 | 587 | const text = params.toString(); |
| @@ -579,6 +589,7 @@ function runsHref( | ||
| 579 | 589 | } |
| 580 | 590 | |
| 581 | 591 | const RUNS_KIND_WORDS: Record<RunsKind, string> = { |
| 592 | all: "everything", | |
| 582 | 593 | agent: "the crew's runs", |
| 583 | 594 | deploy: "deploys", |
| 584 | 595 | check: "checks", |
| @@ -601,7 +612,7 @@ export async function runsListPage(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 601 | 612 | // A private app's runs are for those who may enter it, and a sealed |
| 602 | 613 | // report's for those who may see it (`listRepoRuns`). |
| 603 | 614 | const listed = listRepoRuns(ctx.app, ctx.auth.user, repo, { |
| 604 | kind, | |
| 615 | ...(kind === "all" ? {} : { kind }), | |
| 605 | 616 | limit: LIST_PAGE, |
| 606 | 617 | ...(cursor ? { cursor } : {}), |
| 607 | 618 | }); |
| @@ -630,7 +641,7 @@ export async function runsListPage(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 630 | 641 | note: emptyNote( |
| 631 | 642 | filter, |
| 632 | 643 | "No run", |
| 633 | kind === "agent" | |
| 644 | kind === "agent" || kind === "all" | |
| 634 | 645 | ? "Nothing open. A run is one go the crew takes at a job; each one shows up here." |
| 635 | 646 | : `Nothing open. Every ${kind === "deploy" ? "deploy" : "check"} shows up here while it runs.`, |
| 636 | 647 | ), |
| @@ -649,14 +660,14 @@ export async function runsListPage(ctx: Ctx, repo: Repo): Promise<Response> { | ||
| 649 | 660 | ? avatar(who) |
| 650 | 661 | : icon(run.kind === "check" ? "check" : "rocket"), |
| 651 | 662 | title: runTitleFrom(run, actors, issues), |
| 652 | meta: html`${run.kind === "agent" ? html`${who} · ` : null}${info.note} | |
| 663 | meta: html`${run.kind === "agent" ? html`${who} · ` : null}${run.kind === "check" ? checkMeta(run) : info.note} | |
| 653 | 664 | · ${time(run.finishedAt ?? run.createdAt)}`, |
| 654 | 665 | trail: wordBadge(info.word, undefined, record === undefined), |
| 655 | 666 | }); |
| 656 | 667 | }), |
| 657 | 668 | ); |
| 658 | 669 | const last = fetched[fetched.length - 1]; |
| 659 | const others = (["agent", "deploy", "check"] as const).filter( | |
| 670 | const others = (["all", "agent", "deploy", "check"] as const).filter( | |
| 660 | 671 | (k) => k !== kind, |
| 661 | 672 | ); |
| 662 | 673 |
src/web/studio/run.ts +4 −2
| @@ -20,12 +20,13 @@ import { getUserById } from "../../core/users.ts"; | ||
| 20 | 20 | import { repoDir } from "../../git/repos.ts"; |
| 21 | 21 | import type { Ctx } from "../../http/context.ts"; |
| 22 | 22 | import { crumbsFor, repoBase, repoHeader, repoPalette } from "../chrome.ts"; |
| 23 | import { studioRunJs } from "../client.ts"; | |
| 23 | import { checkPollJs, studioRunJs } from "../client.ts"; | |
| 24 | 24 | import { html, when, type Html } from "../html.ts"; |
| 25 | 25 | import { page } from "../layout.ts"; |
| 26 | 26 | import { btn, sha } from "../ui.ts"; |
| 27 | 27 | import { previewCard } from "../pages/apps.ts"; |
| 28 | 28 | import { costText, durationText, runTitle, runTokens } from "../pages/runs.ts"; |
| 29 | import { checkStepsRegion } from "./check.ts"; | |
| 29 | 30 | import { sandboxLine } from "./sandbox.ts"; |
| 30 | 31 | import { logBox, runVoice, RUN_TRANSCRIPT_CAP } from "./log.ts"; |
| 31 | 32 | import { changeHref, tellHref } from "./nouns.ts"; |
| @@ -202,6 +203,7 @@ export async function runView( | ||
| 202 | 203 | </form> |
| 203 | 204 | </div>`, |
| 204 | 205 | )} |
| 206 | ${when(run.kind === "check", () => checkStepsRegion(ctx, base, run))} | |
| 205 | 207 | ${when(pull, (p) => previewCard(ctx, repo, p))} |
| 206 | 208 | ${when( |
| 207 | 209 | truncated, |
| @@ -293,7 +295,7 @@ export async function runView( | ||
| 293 | 295 | crumbs: crumbsFor(repo), |
| 294 | 296 | palette: repoPalette(ctx, repo), |
| 295 | 297 | body, |
| 296 | scriptJs: studioRunJs, | |
| 298 | scriptJs: run.kind === "check" ? `${studioRunJs}\n${checkPollJs}` : studioRunJs, | |
| 297 | 299 | // The no-script half of a live log: while the run is going, the page |
| 298 | 300 | // refreshes itself; the script above removes this the moment it runs. |
| 299 | 301 | ...(unfinished ? { refresh: { seconds: 5 } } : {}), |
test/check-watch.test.ts added +371
| @@ -0,0 +1,371 @@ | ||
| 1 | /** | |
| 2 | * A check, watched (#1102): its steps and progress read from the lines the | |
| 3 | * runner writes, the title and the line that name it, the runs list that | |
| 4 | * carries it, the feed that tells its owner, and the API that answers | |
| 5 | * "how is it doing". | |
| 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 { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { parseBunTestFailure } from "../src/checks/bun-test.ts"; | |
| 14 | import { checkView } from "../src/checks/progress.ts"; | |
| 15 | import { createIssue } from "../src/core/issues.ts"; | |
| 16 | import type { Repo } from "../src/core/repos.ts"; | |
| 17 | import { claimRun, createRun, finishRun, type Run } from "../src/core/runs.ts"; | |
| 18 | import { createToken } from "../src/core/tokens.ts"; | |
| 19 | import { createUser, type User } from "../src/core/users.ts"; | |
| 20 | import { readFeed } from "../src/feed/feed.ts"; | |
| 21 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 22 | import { appendRunLog } from "../src/runs/log.ts"; | |
| 23 | import { runTitle } from "../src/web/pages/runs.ts"; | |
| 24 | import { checkBody, checkSteps } from "../src/web/studio/check.ts"; | |
| 25 | ||
| 26 | const GATE_CMD = | |
| 27 | "$ bun x tsc --noEmit && FORGE_CHECK_SHARDS=2 bun scripts/test-shards.ts --timeout 60000 --gate"; | |
| 28 | const RUNNING = [ | |
| 29 | "checking out a0451d6", | |
| 30 | "$ bun install", | |
| 31 | "step install ok 12s", | |
| 32 | GATE_CMD, | |
| 33 | "step typecheck ok 40s", | |
| 34 | "test-shards: 4 files in 2 shards, expected 9s 8s", | |
| 35 | "[1/2] test/a.test.ts:", | |
| 36 | "[1/2] bun test v1.3.14 (0d9b296a)", | |
| 37 | "[1/2] ", | |
| 38 | "[1/2] 2 pass", | |
| 39 | "[1/2] 0 fail", | |
| 40 | "[1/2] Ran 2 tests across 1 file. [105.00ms]", | |
| 41 | "[2/2] test/b.test.ts:", | |
| 42 | "[2/2] error: expected 1 to be 2", | |
| 43 | "[2/2] at b.test.ts:9", | |
| 44 | "[2/2] (fail) b breaks [3.00ms]", | |
| 45 | "[2/2] ", | |
| 46 | "[2/2] 0 pass", | |
| 47 | "[2/2] 1 fail", | |
| 48 | ]; | |
| 49 | ||
| 50 | describe("checkView", () => { | |
| 51 | test("steps, files against the gate's count, passes, failures and the file running", () => { | |
| 52 | const { steps, progress } = checkView(RUNNING, false, false); | |
| 53 | expect(steps).toEqual([ | |
| 54 | { name: "install", state: "ok", seconds: 12 }, | |
| 55 | { name: "typecheck", state: "ok", seconds: 40 }, | |
| 56 | { name: "tests", state: "running", seconds: null }, | |
| 57 | ]); | |
| 58 | expect(progress).toMatchObject({ | |
| 59 | files: 2, | |
| 60 | total: 4, | |
| 61 | passed: 2, | |
| 62 | failed: 1, | |
| 63 | current: "test/b.test.ts", | |
| 64 | }); | |
| 65 | expect(progress.failures).toEqual([ | |
| 66 | { | |
| 67 | name: "b breaks", | |
| 68 | file: "test/b.test.ts", | |
| 69 | lines: ["error: expected 1 to be 2", "at b.test.ts:9"], | |
| 70 | }, | |
| 71 | ]); | |
| 72 | }); | |
| 73 | ||
| 74 | test("the ending's list of failures does not count them twice", () => { | |
| 75 | const { progress } = checkView( | |
| 76 | [...RUNNING, "── shard 2/2 (exit 1, 3.0s): test/b.test.ts", "(fail) b breaks [3.00ms] [shard 2]"], | |
| 77 | true, | |
| 78 | false, | |
| 79 | ); | |
| 80 | expect(progress.failures).toHaveLength(1); | |
| 81 | }); | |
| 82 | ||
| 83 | test("a check that dies in the typecheck never reached the tests", () => { | |
| 84 | const { steps, progress } = checkView( | |
| 85 | ["$ bun x tsc --noEmit && bun test", "src/x.ts(1,1): error TS2304"], | |
| 86 | true, | |
| 87 | false, | |
| 88 | ); | |
| 89 | expect(steps.map((s) => [s.name, s.state])).toEqual([ | |
| 90 | ["typecheck", "failed"], | |
| 91 | ["tests", "pending"], | |
| 92 | ]); | |
| 93 | expect(progress.files).toBe(0); | |
| 94 | }); | |
| 95 | ||
| 96 | test("a finished green check has every step ok and no file running", () => { | |
| 97 | const { steps, progress } = checkView( | |
| 98 | [...RUNNING.slice(0, 11), "step check ok 600s"], | |
| 99 | true, | |
| 100 | true, | |
| 101 | ); | |
| 102 | expect(steps.map((s) => s.state)).toEqual(["ok", "ok", "ok"]); | |
| 103 | expect(steps[2]!.seconds).toBe(600); | |
| 104 | expect(progress.current).toBeNull(); | |
| 105 | }); | |
| 106 | ||
| 107 | test("a check with no install and nothing yet written has only tests, pending", () => { | |
| 108 | expect(checkView([], false, false).steps).toEqual([ | |
| 109 | { name: "tests", state: "pending", seconds: null }, | |
| 110 | ]); | |
| 111 | }); | |
| 112 | ||
| 113 | test("failures are bounded, and their lines are the output just before", () => { | |
| 114 | const lines = [ | |
| 115 | ...Array.from({ length: 30 }, (_, i) => [ | |
| 116 | `[1/1] noise ${i}`, | |
| 117 | `[1/1] (fail) t${i}`, | |
| 118 | ]).flat(), | |
| 119 | "[1/1] 30 fail", | |
| 120 | ]; | |
| 121 | expect(checkView(lines, false, false).progress.failures).toHaveLength(10); | |
| 122 | expect(checkView(lines, false, false).progress.failed).toBe(30); | |
| 123 | }); | |
| 124 | }); | |
| 125 | ||
| 126 | describe("bun test's summary through a sharded log", () => { | |
| 127 | test("shard prefixes are read through, counts add up across shards, names are not doubled", () => { | |
| 128 | const detail = parseBunTestFailure([ | |
| 129 | "[1/2] (fail) t1 [2.0ms]", | |
| 130 | "[1/2] 10 pass", | |
| 131 | "[1/2] 1 fail", | |
| 132 | "[2/2] 20 pass", | |
| 133 | "[2/2] 0 fail", | |
| 134 | "(fail) t1 [shard 1]", | |
| 135 | ]); | |
| 136 | expect(detail).toEqual({ passed: 30, failed: 1, names: ["t1"] }); | |
| 137 | }); | |
| 138 | }); | |
| 139 | ||
| 140 | const run = (over: Partial<Run>): Run => | |
| 141 | ({ | |
| 142 | id: "r1", | |
| 143 | repoId: "x", | |
| 144 | kind: "check", | |
| 145 | issueId: null, | |
| 146 | env: "", | |
| 147 | commitOid: "a0451d6aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
| 148 | state: "running", | |
| 149 | summary: "", | |
| 150 | startedAt: new Date(Date.now() - 12 * 60_000 - 5000).toISOString(), | |
| 151 | finishedAt: null, | |
| 152 | ...over, | |
| 153 | }) as Run; | |
| 154 | ||
| 155 | describe("what a check is called and how its line reads", () => { | |
| 156 | test("the title names the commit and the pull, not who pushed", () => { | |
| 157 | expect( | |
| 158 | runTitle(run({}), "trevato", { number: 1101, title: "x" } as never), | |
| 159 | ).toBe("Check of a0451d6 for #1101"); | |
| 160 | expect(runTitle(run({}), "trevato", undefined)).toBe("Check of a0451d6"); | |
| 161 | }); | |
| 162 | ||
| 163 | test("the line says running, how long, how far and what has passed, and links the run", () => { | |
| 164 | const view = checkView( | |
| 165 | [ | |
| 166 | GATE_CMD, | |
| 167 | "test-shards: 170 files in 2 shards, expected 1s", | |
| 168 | ...Array.from({ length: 61 }, (_, i) => `[1/2] test/f${i}.test.ts:`), | |
| 169 | "[1/2] 812 pass", | |
| 170 | "[1/2] 0 fail", | |
| 171 | ], | |
| 172 | false, | |
| 173 | false, | |
| 174 | ); | |
| 175 | const text = checkBody("/trevato/forge", run({}), view).text; | |
| 176 | expect(text).toContain("Check running"); | |
| 177 | expect(text).toContain('href="/trevato/forge/runs/r1"'); | |
| 178 | expect(text).toContain("12 min"); | |
| 179 | expect(text).toContain("61 of 170 files · 812 passed, 0 failed"); | |
| 180 | }); | |
| 181 | ||
| 182 | test("a failed check says so with its summary; a passed one how long", () => { | |
| 183 | const view = checkView([], true, false); | |
| 184 | const failed = checkBody( | |
| 185 | "/o/n", | |
| 186 | run({ | |
| 187 | state: "failed", | |
| 188 | summary: '2 of 2052 tests — "x"', | |
| 189 | finishedAt: new Date().toISOString(), | |
| 190 | }), | |
| 191 | view, | |
| 192 | ).text; | |
| 193 | expect(failed).toContain("Check failed"); | |
| 194 | expect(failed).toContain("2 of 2052 tests"); | |
| 195 | }); | |
| 196 | ||
| 197 | test("the steps card shows each step with its state and time, the bar, and pins failures", () => { | |
| 198 | const text = checkSteps("/o/n", run({}), checkView(RUNNING, false, false)).text; | |
| 199 | expect(text).toContain("Install"); | |
| 200 | expect(text).toContain("12s"); | |
| 201 | expect(text).toContain("Typecheck"); | |
| 202 | expect(text).toContain("<progress"); | |
| 203 | expect(text).toContain('value="2"'); | |
| 204 | expect(text).toContain('max="4"'); | |
| 205 | expect(text).toContain("now test/b.test.ts"); | |
| 206 | expect(text).toContain("b breaks"); | |
| 207 | expect(text).toContain("1 failing so far"); | |
| 208 | }); | |
| 209 | }); | |
| 210 | ||
| 211 | describe("a check on a pull, in the feed, the list, the page and the API", () => { | |
| 212 | let dataDir: string; | |
| 213 | let app: App; | |
| 214 | let servers: Servers; | |
| 215 | let alice: User; | |
| 216 | let bob: User; | |
| 217 | let aliceToken: string; | |
| 218 | let repo: Repo; | |
| 219 | let checkRun: Run; | |
| 220 | ||
| 221 | beforeAll(async () => { | |
| 222 | dataDir = await mkdtemp(join(tmpdir(), "forge-checkwatch-")); | |
| 223 | app = createApp({ dataDir, port: 0 }); | |
| 224 | servers = await serve(app); | |
| 225 | const mk = async (username: string, first = false) => { | |
| 226 | const r = await createUser(app.db, { | |
| 227 | trusted: true, | |
| 228 | username, | |
| 229 | allowReserved: true, | |
| 230 | email: `${username}@forge.local`, | |
| 231 | password: "a-good-passphrase", | |
| 232 | adminIfFirst: first, | |
| 233 | }); | |
| 234 | if (!r.ok) throw new Error(r.error); | |
| 235 | return r.value; | |
| 236 | }; | |
| 237 | await mk("root", true); | |
| 238 | alice = await mk("alice"); | |
| 239 | bob = await mk("bob"); | |
| 240 | aliceToken = createToken(app.db, alice.id, "t", "rw").token; | |
| 241 | const made = await createRepo(app, alice, { name: "game", visibility: "public" }); | |
| 242 | if (!made.ok) throw new Error("repo"); | |
| 243 | repo = made.value; | |
| 244 | // A pull is its issue plus a row; the check needs no git behind it. | |
| 245 | const issue = createIssue(app.db, { | |
| 246 | repoId: repo.id, | |
| 247 | authorId: alice.id, | |
| 248 | title: "Make it faster", | |
| 249 | kind: "pull", | |
| 250 | }); | |
| 251 | if (!issue.ok) throw new Error("issue"); | |
| 252 | app.db | |
| 253 | .query( | |
| 254 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'x', ?2, ?3)", | |
| 255 | ) | |
| 256 | .run(issue.value.id, "0".repeat(40), "a0451d6" + "0".repeat(33)); | |
| 257 | checkRun = createRun(app.db, { | |
| 258 | repoId: repo.id, | |
| 259 | kind: "check", | |
| 260 | issueId: issue.value.id, | |
| 261 | commitOid: "a0451d6" + "0".repeat(33), | |
| 262 | reason: "push", | |
| 263 | }); | |
| 264 | claimRun(app.db, checkRun.id); | |
| 265 | for (const line of RUNNING) appendRunLog(dataDir, checkRun.id, line); | |
| 266 | }); | |
| 267 | ||
| 268 | afterAll(async () => { | |
| 269 | servers.stop(); | |
| 270 | app.close(); | |
| 271 | await rm(dataDir, { recursive: true, force: true }); | |
| 272 | }); | |
| 273 | ||
| 274 | const get = (path: string, token = aliceToken) => | |
| 275 | fetch(`${servers.url}${path}`, { headers: { authorization: `Bearer ${token}` } }); | |
| 276 | ||
| 277 | test("the API's run carries steps and progress while the check goes", async () => { | |
| 278 | const res = await get(`/api/v1/repos/alice/game/runs/${checkRun.id}`); | |
| 279 | expect(res.status).toBe(200); | |
| 280 | const body = (await res.json()) as { | |
| 281 | state: string; | |
| 282 | steps: { name: string; state: string }[]; | |
| 283 | progress: { files: number; total: number; passed: number; failed: number; current: string }; | |
| 284 | }; | |
| 285 | expect(body.state).toBe("running"); | |
| 286 | expect(body.steps.map((s) => `${s.name}:${s.state}`)).toEqual([ | |
| 287 | "install:ok", | |
| 288 | "typecheck:ok", | |
| 289 | "tests:running", | |
| 290 | ]); | |
| 291 | expect(body.progress).toMatchObject({ | |
| 292 | files: 2, | |
| 293 | total: 4, | |
| 294 | passed: 2, | |
| 295 | failed: 1, | |
| 296 | current: "test/b.test.ts", | |
| 297 | }); | |
| 298 | }); | |
| 299 | ||
| 300 | test("an agent's run json stays as it was", async () => { | |
| 301 | const agent = createRun(app.db, { repoId: repo.id, kind: "agent" }); | |
| 302 | const body = (await (await get(`/api/v1/repos/alice/game/runs/${agent.id}`)).json()) as object; | |
| 303 | expect("steps" in body).toBe(false); | |
| 304 | }); | |
| 305 | ||
| 306 | test("the run page is titled for the check and shows its steps and the log", async () => { | |
| 307 | const res = await fetch(`${servers.url}/alice/game/runs/${checkRun.id}`); | |
| 308 | const text = await res.text(); | |
| 309 | expect(text).toContain("Check of a0451d6 for #1"); | |
| 310 | expect(text).not.toContain("alice on #"); | |
| 311 | expect(text).toContain('id="live-steps"'); | |
| 312 | expect(text).toContain('data-live-poll="5"'); | |
| 313 | expect(text).toContain("<progress"); | |
| 314 | expect(text).toContain("b breaks"); | |
| 315 | }); | |
| 316 | ||
| 317 | test("the steps region answers for itself, and only for a check", async () => { | |
| 318 | const res = await fetch(`${servers.url}/alice/game/runs/${checkRun.id}/progress`); | |
| 319 | expect(res.status).toBe(200); | |
| 320 | const text = await res.text(); | |
| 321 | expect(text.trimStart().startsWith("<div")).toBe(true); | |
| 322 | expect(text).toContain("Tests"); | |
| 323 | const agent = createRun(app.db, { repoId: repo.id, kind: "agent" }); | |
| 324 | expect((await fetch(`${servers.url}/alice/game/runs/${agent.id}/progress`)).status).toBe(404); | |
| 325 | }); | |
| 326 | ||
| 327 | test("the pull's checks line names the check and updates from its own address", async () => { | |
| 328 | const page = await (await fetch(`${servers.url}/alice/game/pulls/1/files`)).text(); | |
| 329 | expect(page).toContain('id="live-check"'); | |
| 330 | expect(page).toContain("Check running"); | |
| 331 | expect(page).toContain("2 of 4 files · 2 passed, 1 failed"); | |
| 332 | expect(page).toContain('data-live-src="/alice/game/pulls/1/checks"'); | |
| 333 | const part = await fetch(`${servers.url}/alice/game/pulls/1/checks`); | |
| 334 | expect(part.status).toBe(200); | |
| 335 | expect(await part.text()).toContain("Check running"); | |
| 336 | }); | |
| 337 | ||
| 338 | test("the runs list carries checks and deploys with the crew's by default, and filters", async () => { | |
| 339 | createRun(app.db, { repoId: repo.id, kind: "deploy", env: "production" }); | |
| 340 | const all = await (await fetch(`${servers.url}/alice/game/runs`)).text(); | |
| 341 | expect(all).toContain("Check of a0451d6 for #1"); | |
| 342 | expect(all).toContain("Deploy production"); | |
| 343 | const deploys = await (await fetch(`${servers.url}/alice/game/runs?kind=deploy`)).text(); | |
| 344 | expect(deploys).not.toContain("Check of a0451d6"); | |
| 345 | const checks = await (await fetch(`${servers.url}/alice/game/runs?kind=check`)).text(); | |
| 346 | expect(checks).toContain("Check of a0451d6"); | |
| 347 | expect(checks).not.toContain("Deploy production"); | |
| 348 | }); | |
| 349 | ||
| 350 | test("the feed tells the pull's author when its check ends, and not a stranger", () => { | |
| 351 | app.db | |
| 352 | .query( | |
| 353 | "UPDATE run SET started_at = '2026-10-12T10:00:00.000Z' WHERE id = ?1", | |
| 354 | ) | |
| 355 | .run(checkRun.id); | |
| 356 | const done = finishRun(app.db, checkRun.id, { state: "ok", summary: "passed" }); | |
| 357 | app.db | |
| 358 | .query("UPDATE run SET finished_at = '2026-10-12T10:38:00.000Z' WHERE id = ?1") | |
| 359 | .run(checkRun.id); | |
| 360 | app.bus.append({ | |
| 361 | kind: "run.finished", | |
| 362 | repoId: repo.id, | |
| 363 | actorId: alice.id, | |
| 364 | payload: { runId: done!.id, kind: "check", state: "ok", issueId: done!.issueId }, | |
| 365 | }); | |
| 366 | const mine = readFeed(app.db, alice).items.map((i) => i.reason + " " + i.link); | |
| 367 | expect(mine.some((t) => t.includes("Checks on #1 passed in 38 min"))).toBe(true); | |
| 368 | const theirs = readFeed(app.db, bob).items.map((i) => i.reason); | |
| 369 | expect(theirs.some((t) => t.includes("Checks on #1"))).toBe(false); | |
| 370 | }); | |
| 371 | }); |
test/mcp.test.ts +44 −1
| @@ -14,7 +14,8 @@ import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; | ||
| 14 | 14 | import { createApp as insertApp } from "../src/core/apps.ts"; |
| 15 | 15 | import { listIssues } from "../src/core/issues.ts"; |
| 16 | 16 | import { getRepoById, type Repo } from "../src/core/repos.ts"; |
| 17 | import { createRun, listRuns } from "../src/core/runs.ts"; | |
| 17 | import { claimRun, createRun, listRuns } from "../src/core/runs.ts"; | |
| 18 | import { appendRunLog } from "../src/runs/log.ts"; | |
| 18 | 19 | import { createToken } from "../src/core/tokens.ts"; |
| 19 | 20 | import { createUser, type User } from "../src/core/users.ts"; |
| 20 | 21 | import { serve, type Servers } from "../src/http/server.ts"; |
| @@ -164,6 +165,7 @@ describe("the forge over MCP", () => { | ||
| 164 | 165 | "asks_act", |
| 165 | 166 | "seal", |
| 166 | 167 | "now", |
| 168 | "check", | |
| 167 | 169 | "stop", |
| 168 | 170 | "health", |
| 169 | 171 | "audit", |
| @@ -402,6 +404,47 @@ describe("the forge over MCP", () => { | ||
| 402 | 404 | ).toContain("not_found"); |
| 403 | 405 | }); |
| 404 | 406 | |
| 407 | test("check says how far a check is, from its own log, and only to who may read it", async () => { | |
| 408 | const run = createRun(app.db, { | |
| 409 | repoId: game.id, | |
| 410 | kind: "check", | |
| 411 | commitOid: "a0451d6" + "0".repeat(33), | |
| 412 | reason: "push", | |
| 413 | }); | |
| 414 | claimRun(app.db, run.id); | |
| 415 | for (const line of [ | |
| 416 | "$ bun x tsc --noEmit && bun scripts/test-shards.ts --gate", | |
| 417 | "test-shards: 5 files in 1 shard, expected 1s", | |
| 418 | "[1/1] test/a.test.ts:", | |
| 419 | "[1/1] (pass) one", | |
| 420 | "[1/1] (fail) two", | |
| 421 | ]) | |
| 422 | appendRunLog(dataDir, run.id, line); | |
| 423 | const said = await tool(ro, "check", { repo: "alice/game", run: run.id }); | |
| 424 | expect(said.value.state).toBe("running"); | |
| 425 | expect(said.value.steps.map((s: { name: string }) => s.name)).toEqual([ | |
| 426 | "typecheck", | |
| 427 | "tests", | |
| 428 | ]); | |
| 429 | expect(said.value.progress).toMatchObject({ | |
| 430 | files: 1, | |
| 431 | total: 5, | |
| 432 | passed: 1, | |
| 433 | failed: 1, | |
| 434 | current: "test/a.test.ts", | |
| 435 | }); | |
| 436 | // With no run named, the newest check; an agent's run is not a check. | |
| 437 | const newest = await tool(ro, "check", { repo: "alice/game" }); | |
| 438 | expect(newest.value.run).toBe(run.id); | |
| 439 | const agent = createRun(app.db, { repoId: game.id, kind: "agent" }); | |
| 440 | expect( | |
| 441 | (await tool(ro, "check", { repo: "alice/game", run: agent.id })).error, | |
| 442 | ).toContain("not_found"); | |
| 443 | expect( | |
| 444 | (await tool(bobRw, "check", { repo: "alice/nothing" })).error, | |
| 445 | ).toContain("not_found"); | |
| 446 | }); | |
| 447 | ||
| 405 | 448 | test("stop takes a run id from now, and refuses a stale or foreign one", async () => { |
| 406 | 449 | const run = createRun(app.db, { |
| 407 | 450 | repoId: game.id, |
new above
offline