Public: anyone can read this; the crew can change it.
The lane-stopped card carries a Merge action for each green pull waiting on a person #18348
Merged 2h 38m ago, live in production
torque/forge:torque/18339-the-lane-stopped-card-carries-a-merge-ac → main 0 commits fork
12 files
changed +481
−26
- docs/features/lane-stopped.md
- src/core/pulls.ts
- src/crew/sentinel.ts
- src/flow/alarm_lane.ts
- src/flow/alarms.ts
- src/flow/lane_conflicts.ts
- src/flow/pages_read.ts
- src/tui/screens/flow.ts
- src/web/pages/boards-view.ts
- src/web/studio/alarm_lines.ts
- test/flow-alarm-lane.test.ts
- test/flow-lane-conflicts.test.ts
docs/features/lane-stopped.md +9
| @@ -13,4 +13,13 @@ It is an ask for the owner and one Sentinel issue (`src/crew/sentinel_lane.ts`), | ||
| 13 | 13 | |
| 14 | 14 | **A stop that is the review queue's (#14337).** When the lane has stopped, the alarm reads the review queue (`reviewQueue`): an open pull is green when its newest finished check passed on the pull's current head and finished after the last merge. A pass on a head that has since moved, or on a base a merge has since moved, is stale and not counted (#3316); a person's direct push to main is not seen. The alarm says how many pulls are green and names them (up to ten), so the owner knows what to merge. When green pulls outnumber the open pulls whose newest check failed, and no one cause is failing many, the stop is the owner's ask on /flow only: no Sentinel issue is filed, because no builder can merge and the issue only parked runs that declined. An open lane issue closes, saying so, at the next hunt. One green pull among many reds is still filed, with the green pulls in its evidence. The hunt reads the alarm once and both files and closes from it. |
| 15 | 15 | |
| 16 | **A Merge action for each green pull (#18339).** On 2026-12-05 the alarm said three pulls were green and waiting on a person, and nothing merged for 29 hours: it named them and gave no way to act. Now the alarm lists the green pulls one by one (`waiting` on the alarm): | |
| 17 | ||
| 18 | - **On the web**, under the alarm line on /boards, each green pull that merges is a row titled with the pull's own title, its `#N` beside it, linking to its page with a **Merge #N** button. The button posts to the pull's own merge route, `POST /<owner>/<name>/pulls/<N>/merge`, the same one the pull page uses. There is no other merge path, and the route checks again who may merge and whether the pull still merges. Only someone who can write the forge's repository sees the buttons, and never an agent: merging is a person's act. Anyone else sees the pulls without buttons. | |
| 19 | - **A green pull that no longer merges into main** is listed apart, in a second list after the mergeable ones under the label "Green pulls that need bringing up to date", with the files it conflicts in, and has no button: it merges once it is brought up to date. Before /boards draws the alarm, `readLaneConflicts` (`src/flow/lane_conflicts.ts`) asks git's merge-tree about each green pull against main's tip. The work is bounded: at most a few readings at once, within the 300 ms budget, and each (head, base) pair is read once. A pull still being read when the budget runs out is listed as mergeable until the next draw. The alarm itself stays a pure read of the database: it reads the forge's last reading of each pull (`lastConflicts` in `src/core/pulls.ts`), which the pull page also leaves behind. | |
| 20 | - **In the terminal**, the flow screen prints the same pulls under the alarm, in the same order: `#N <title>: green, merge it at <origin>/<owner>/<name>/pulls/N` for each that merges, then the label and `#N <title>: conflicts with main in <files>` for each that does not. The flow screen links to the pull rather than merging from the alarm, and the terminal dashboard shows only the alarm sentence, not these lines: the pull's own terminal screen carries the merge, so opening that link or that screen is how to merge from the terminal. | |
| 21 | - **In the Sentinel issue's evidence**, each pull is a link to its page. A conflicting pull is on its own line: `N pulls green but conflicting with main, not mergeable until brought up to date: #N (conflicts in …)`. | |
| 22 | ||
| 23 | The alarm clears itself on the next merge: the merge moves "the last merge", so the lane has moved, and the list of pulls goes with it. | |
| 24 | ||
| 16 | 25 | Left out: the Sentinel issue's evidence carries the failure count, so it is commented again as that count moves. |
src/core/pulls.ts +14 −5
| @@ -974,11 +974,10 @@ export async function pullStatus( | ||
| 974 | 974 | const lands = pull.crossRepo |
| 975 | 975 | ? ((await combinedTip(dir, pull.number, headOid)) ?? headOid) |
| 976 | 976 | : headOid; |
| 977 | const { ahead, mergeable: clean, conflicts } = await mergeReading( | |
| 978 | dir, | |
| 979 | baseOid, | |
| 980 | lands, | |
| 981 | ); | |
| 977 | const reading = await mergeReading(dir, baseOid, lands); | |
| 978 | // The lane alarm names what the last reading of an open pull found (#18339). | |
| 979 | if (pull.state === "open") remember(`${dir}:${pull.number}`, headOid, reading); | |
| 980 | const { ahead, mergeable: clean, conflicts } = reading; | |
| 982 | 981 | return { baseOid, headOid, ahead, mergeable: clean, conflicts, reviews }; |
| 983 | 982 | } |
| 984 | 983 | |
| @@ -1099,6 +1098,16 @@ export async function mergeReadingsSettled(): Promise<void> { | ||
| 1099 | 1098 | */ |
| 1100 | 1099 | const lastByPull = new Map<string, { headOid: string; reading: MergeReading }>(); |
| 1101 | 1100 | |
| 1101 | /** | |
| 1102 | * The files the forge's last merge reading of pull `number` in `dir` found conflicting, when that | |
| 1103 | * reading was of `headOid`; none when it merged or was never read. No git runs: the lane alarm | |
| 1104 | * (`flow/alarm_lane.ts`, #18339) reads it on every request. | |
| 1105 | */ | |
| 1106 | export function lastConflicts(dir: string, number: number, headOid: string): readonly string[] { | |
| 1107 | const last = lastByPull.get(`${dir}:${number}`); | |
| 1108 | return last === undefined || last.headOid !== headOid ? [] : last.reading.conflicts; | |
| 1109 | } | |
| 1110 | ||
| 1102 | 1111 | function remember(key: string, headOid: string, reading: MergeReading): void { |
| 1103 | 1112 | lastByPull.delete(key); |
| 1104 | 1113 | if (lastByPull.size >= READINGS_MAX) |
src/crew/sentinel.ts +2 −2
| @@ -53,7 +53,7 @@ import { personSpoke } from "./person_spoke.ts"; | ||
| 53 | 53 | import { closeCaughtUp, mainAhead } from "./sentinel_main_ahead.ts"; |
| 54 | 54 | import { mayClose, sayAndClose } from "./sentinel_close.ts"; |
| 55 | 55 | import { closeLaneMoving, laneStopped } from "./sentinel_lane.ts"; |
| 56 | import { type LaneAlarm, laneAlarm } from "../flow/alarm_lane.ts"; | |
| 56 | import { keptConflicts, type LaneAlarm, laneAlarm } from "../flow/alarm_lane.ts"; | |
| 57 | 57 | import type { FindingKind } from "../hunt/finding.ts"; |
| 58 | 58 | import { addLabels, kindLabels } from "../hunt/label.ts"; |
| 59 | 59 | import { evidenceBlock, type Evidence } from "../maintain/evidence.ts"; |
| @@ -1226,7 +1226,7 @@ export function hunt( | ||
| 1226 | 1226 | const repo = getRepo(app.db, owner, name); |
| 1227 | 1227 | const ceilingUsd = forgeCeiling(app.config); |
| 1228 | 1228 | // Read once: the lane finding and the lane issue's closing both follow it. |
| 1229 | const lane = laneAlarm(app.db, new Date(now), { ceilingUsd }); | |
| 1229 | const lane = laneAlarm(app.db, new Date(now), { ceilingUsd, conflictsOf: keptConflicts(app.reposRoot) }); | |
| 1230 | 1230 | const findings = find(app.db, now, repo?.id, app.config.dataDir, ceilingUsd, app.signals.errors, lane); |
| 1231 | 1231 | const actor = sentinelActor(app.db); |
| 1232 | 1232 | if (repo !== undefined && actor !== undefined) { |
src/flow/alarm_lane.ts +77 −5
| @@ -19,7 +19,9 @@ | ||
| 19 | 19 | import type { Database } from "bun:sqlite"; |
| 20 | 20 | import { spentTodayEverywhere } from "../crew/autonomy_spend.ts"; |
| 21 | 21 | import type { Run } from "../core/runs.ts"; |
| 22 | import { lastConflicts } from "../core/pulls.ts"; | |
| 22 | 23 | import { getRepo } from "../core/repos.ts"; |
| 24 | import { repoDir } from "../git/repos.ts"; | |
| 23 | 25 | import { agoWords } from "../keeper/beat.ts"; |
| 24 | 26 | import { ownRepoPath } from "../kernel/own_repo.ts"; |
| 25 | 27 | import { ceilingReached } from "./ceiling.ts"; |
| @@ -48,6 +50,12 @@ export interface LaneAlarm { | ||
| 48 | 50 | * has stopped. |
| 49 | 51 | */ |
| 50 | 52 | readonly green?: number | undefined; |
| 53 | /** | |
| 54 | * The green pulls one by one (#18339): each one's page, and the files the forge last read it | |
| 55 | * conflicting in. A pull with none is a person's to merge from the alarm; one with some is listed | |
| 56 | * apart and is not. Set when the lane has stopped. | |
| 57 | */ | |
| 58 | readonly waiting?: readonly Waiting[] | undefined; | |
| 51 | 59 | /** The open pulls by state: green, stale, red and never checked; set when the alarm fired. */ |
| 52 | 60 | readonly queue?: Queue | undefined; |
| 53 | 61 | /** |
| @@ -213,6 +221,52 @@ export function reviewQueue(db: Database, repoId: string, since: string): Queue | ||
| 213 | 221 | return { green, stale, red, unchecked }; |
| 214 | 222 | } |
| 215 | 223 | |
| 224 | /** A green pull waiting on a person: its page, and what stops it merging (none: it merges). */ | |
| 225 | export interface Waiting { | |
| 226 | readonly number: number; | |
| 227 | /** The pull's title, as its author wrote it: what a person reads to decide what to merge. */ | |
| 228 | readonly title: string; | |
| 229 | /** The pull's page; its merge route is this path with `/merge`. */ | |
| 230 | readonly href: string; | |
| 231 | readonly conflicts: readonly string[]; | |
| 232 | } | |
| 233 | ||
| 234 | /** The files the forge last read a pull's head conflicting in, or none: no git runs here. */ | |
| 235 | export type ConflictsOf = (number: number, headOid: string) => readonly string[]; | |
| 236 | ||
| 237 | /** | |
| 238 | * The forge's own repository under `reposRoot`, read for the merge readings `core/pulls.ts` kept. | |
| 239 | */ | |
| 240 | export function keptConflicts(reposRoot: string): ConflictsOf { | |
| 241 | const [owner, name] = ownRepoPath().split("/"); | |
| 242 | if (!owner || !name) return () => []; | |
| 243 | const dir = repoDir(reposRoot, owner, name); | |
| 244 | return (number, headOid) => lastConflicts(dir, number, headOid); | |
| 245 | } | |
| 246 | ||
| 247 | /** The green pulls of `queue`, oldest first, each with its page and its conflicts. */ | |
| 248 | function waitingPulls(db: Database, repoId: string, green: readonly number[], conflictsOf: ConflictsOf): Waiting[] { | |
| 249 | if (green.length === 0) return []; | |
| 250 | const open = new Map( | |
| 251 | db | |
| 252 | .query<{ number: number; title: string; head_oid: string }, [string]>( | |
| 253 | `SELECT i.number, i.title, p.head_oid FROM issue i JOIN pull p ON p.issue_id = i.id WHERE i.repo_id = ?1 AND i.kind = 'pull' AND i.state = 'open'`, | |
| 254 | ) | |
| 255 | .all(repoId) | |
| 256 | .map((r) => [r.number, r]), | |
| 257 | ); | |
| 258 | const base = `/${ownRepoPath()}/pulls`; | |
| 259 | return green.map((number) => { | |
| 260 | const r = open.get(number); | |
| 261 | return { number, title: r?.title ?? `#${number}`, href: `${base}/${number}`, conflicts: conflictsOf(number, r?.head_oid ?? "") }; | |
| 262 | }); | |
| 263 | } | |
| 264 | ||
| 265 | /** At most this many conflicting files are named per pull. */ | |
| 266 | const FILES_MAX = 5; | |
| 267 | const filesWords = (files: readonly string[]): string => | |
| 268 | files.slice(0, FILES_MAX).join(", ") + (files.length > FILES_MAX ? ` and ${files.length - FILES_MAX} more` : ""); | |
| 269 | ||
| 216 | 270 | /** At most this many green pulls are named. */ |
| 217 | 271 | const NAMED_MAX = 10; |
| 218 | 272 | const named = (numbers: readonly number[]): string => |
| @@ -220,11 +274,21 @@ const named = (numbers: readonly number[]): string => | ||
| 220 | 274 | |
| 221 | 275 | const count = (n: number, noun: string) => `${n} ${noun}${n === 1 ? "" : "s"}`; |
| 222 | 276 | |
| 277 | /** The pulls as Markdown links to their pages, for the Sentinel issue's evidence. */ | |
| 278 | const linked = (pulls: readonly Waiting[]): string => | |
| 279 | pulls.slice(0, NAMED_MAX).map((p) => `[#${p.number}](${p.href})`).join(", ") + (pulls.length > NAMED_MAX ? ` and ${pulls.length - NAMED_MAX} more` : ""); | |
| 280 | const conflicting = (pulls: readonly Waiting[], name: (p: Waiting) => string): string => | |
| 281 | pulls.slice(0, NAMED_MAX).map((p) => `${name(p)} (conflicts in ${filesWords(p.conflicts)})`).join("; ") + (pulls.length > NAMED_MAX ? `; and ${pulls.length - NAMED_MAX} more` : ""); | |
| 282 | ||
| 223 | 283 | /** The four counts of the open pulls, in one line. */ |
| 224 | 284 | export const queueWords = (q: Queue): string => |
| 225 | 285 | `${q.green.length} green since the last merge, ${q.stale} stale (passed on an older head or before the last merge), ${q.red} red, ${q.unchecked} never checked`; |
| 226 | 286 | |
| 227 | export function laneAlarm(db: Database, now: Date, forge: { readonly ceilingUsd: number | null } = { ceilingUsd: null }): LaneAlarm { | |
| 287 | export function laneAlarm( | |
| 288 | db: Database, | |
| 289 | now: Date, | |
| 290 | forge: { readonly ceilingUsd: number | null; readonly conflictsOf?: ConflictsOf } = { ceilingUsd: null }, | |
| 291 | ): LaneAlarm { | |
| 228 | 292 | const quiet = { fired: false, says: "Pulls are landing, or there is nothing to land, or the crew is paused on purpose." } as const; |
| 229 | 293 | const repoId = ownRepoId(db); |
| 230 | 294 | if (repoId === undefined) return quiet; |
| @@ -249,6 +313,7 @@ export function laneAlarm(db: Database, now: Date, forge: { readonly ceilingUsd: | ||
| 249 | 313 | let line: string | undefined; |
| 250 | 314 | let since: string; |
| 251 | 315 | let queue: Queue | undefined; |
| 316 | let waiting: Waiting[] | undefined; | |
| 252 | 317 | let failedSince = -1; |
| 253 | 318 | if (stopped) { |
| 254 | 319 | const failed = failuresSince(db, repoId, last!); |
| @@ -263,10 +328,17 @@ export function laneAlarm(db: Database, now: Date, forge: { readonly ceilingUsd: | ||
| 263 | 328 | facts.push(`last merge at ${last}`, `${count(open, "pull")} open`, `${failed.length} checks failed since the last merge`); |
| 264 | 329 | if (top !== undefined) facts.push(`commonest failure, ${top.count} times: ${top.line}`); |
| 265 | 330 | queue = reviewQueue(db, repoId, last!); |
| 266 | const g = queue.green.length; | |
| 331 | waiting = waitingPulls(db, repoId, queue.green, forge.conflictsOf ?? (() => [])); | |
| 332 | const ready = waiting.filter((p) => p.conflicts.length === 0); | |
| 333 | const stuck = waiting.filter((p) => p.conflicts.length > 0); | |
| 334 | const g = ready.length; | |
| 267 | 335 | if (g > 0) { |
| 268 | parts.push(`${count(g, "pull")} passed ${g === 1 ? "its check and waits" : "their checks and wait"} on a person to merge: ${named(queue.green)}`); | |
| 269 | facts.push(`${count(g, "pull")} green, waiting on a person to merge: ${named(queue.green)}`); | |
| 336 | parts.push(`${count(g, "pull")} passed ${g === 1 ? "its check and waits" : "their checks and wait"} on a person to merge: ${named(ready.map((p) => p.number))}`); | |
| 337 | facts.push(`${count(g, "pull")} green, waiting on a person to merge: ${linked(ready)}`); | |
| 338 | } | |
| 339 | if (stuck.length > 0) { | |
| 340 | parts.push(`${count(stuck.length, "green pull")} no longer ${stuck.length === 1 ? "merges" : "merge"} into main: ${conflicting(stuck, (p) => `#${p.number}`)}`); | |
| 341 | facts.push(`${count(stuck.length, "pull")} green but conflicting with main, not mergeable until brought up to date: ${conflicting(stuck, (p) => `[#${p.number}](${p.href})`)}`); | |
| 270 | 342 | } |
| 271 | 343 | } else { |
| 272 | 344 | since = shared!.since; |
| @@ -291,5 +363,5 @@ export function laneAlarm(db: Database, now: Date, forge: { readonly ceilingUsd: | ||
| 291 | 363 | // (#16569). |
| 292 | 364 | const green = queue.green.length; |
| 293 | 365 | const onPeople = shared === undefined && (queue.green.length > queue.red || queue.red <= 1 || (failedSince >= 0 && failedSince <= 1)); |
| 294 | return { fired: true, since, says: `${parts.join(". ")}.`.replace(/\.\.$/, "."), line, forge: shared !== undefined, facts, green, queue, onPeople }; | |
| 366 | return { fired: true, since, says: `${parts.join(". ")}.`.replace(/\.\.$/, "."), line, forge: shared !== undefined, facts, green, queue, ...(waiting === undefined ? {} : { waiting }), onPeople }; | |
| 295 | 367 | } |
src/flow/alarms.ts +14 −3
| @@ -16,7 +16,8 @@ import { alarmBackup } from "./alarm_backup.ts"; | ||
| 16 | 16 | import { alarmFromApp } from "./alarm_from_app.ts"; |
| 17 | 17 | import { alarmPaidOrder } from "./alarm_paid_order.ts"; |
| 18 | 18 | import { alarmIdleCrew } from "./alarm_idle_crew.ts"; |
| 19 | import { laneAlarm } from "./alarm_lane.ts"; | |
| 19 | import { join } from "node:path"; | |
| 20 | import { keptConflicts, laneAlarm, type Waiting } from "./alarm_lane.ts"; | |
| 20 | 21 | import { mainAheadAlarm } from "./alarm_main_ahead.ts"; |
| 21 | 22 | import { noReleaseAlarm } from "./alarm_no_release.ts"; |
| 22 | 23 | import { redMainAlarm } from "./alarm_red_main.ts"; |
| @@ -35,6 +36,11 @@ export interface FiredAlarm { | ||
| 35 | 36 | readonly href: string; |
| 36 | 37 | /** The label of the one action that unblocks it, when the alarm names one. */ |
| 37 | 38 | readonly action?: string; |
| 39 | /** | |
| 40 | * The lane alarm's green pulls (#18339), each with its page; one with no conflicts is a person's | |
| 41 | * to merge from the alarm, through the pull's own merge route. | |
| 42 | */ | |
| 43 | readonly waiting?: readonly Waiting[]; | |
| 38 | 44 | } |
| 39 | 45 | |
| 40 | 46 | function redMainHref(db: Database): string { |
| @@ -98,8 +104,13 @@ export function firedAlarms( | ||
| 98 | 104 | if (ahead.fired) out.push({ key: "main_ahead", says: ahead.says, since: ahead.since!, href: "/releases" }); |
| 99 | 105 | const quiet = releaseQuietAlarm(db, now); |
| 100 | 106 | if (quiet.fired) out.push({ key: "release_quiet", says: quiet.says, since: quiet.since!, href: "/releases" }); |
| 101 | const lane = laneAlarm(db, now, { ceilingUsd: opts.ceilingUsd ?? null }); | |
| 102 | if (lane.fired) out.push({ key: "lane_stopped", says: lane.says, since: lane.since!, href: "/flow" }); | |
| 107 | const lane = laneAlarm(db, now, { | |
| 108 | ceilingUsd: opts.ceilingUsd ?? null, | |
| 109 | ...(opts.dataDir === null ? {} : { conflictsOf: keptConflicts(join(opts.dataDir, "repos")) }), | |
| 110 | }); | |
| 111 | if (lane.fired) { | |
| 112 | out.push({ key: "lane_stopped", says: lane.says, since: lane.since!, href: "/flow", ...(lane.waiting === undefined || lane.waiting.length === 0 || opts.publicOnly ? {} : { waiting: lane.waiting }) }); | |
| 113 | } | |
| 103 | 114 | const bug = bugClockAlarm(db, now, opts.publicOnly ? { publicOnly: true } : {}); |
| 104 | 115 | if (bug.fired) out.push({ key: "bug_clock", says: bug.says, since: bug.since!, href: bug.href }); |
| 105 | 116 | return out; |
src/flow/lane_conflicts.ts added +25
| @@ -0,0 +1,25 @@ | ||
| 1 | /** | |
| 2 | * Before the lane alarm is drawn with a Merge button per green pull (#18339), read whether each of | |
| 3 | * them still merges into main. The alarm itself stays pure over the db (`alarm_lane.ts`); this | |
| 4 | * asks `pullMergeStates`, which runs git's merge-tree for a (head, base) pair it has not read, | |
| 5 | * at most a few at once and within its budget, and keeps each answer where `keptConflicts` | |
| 6 | * reads it. A pair still reading past the budget is left to the next draw; the merge route reads | |
| 7 | * the pair afresh before it merges, so a button drawn early never lands a conflict. | |
| 8 | */ | |
| 9 | ||
| 10 | import type { Database } from "bun:sqlite"; | |
| 11 | import { getPull, pullDir, pullMergeStates, type Pull } from "../core/pulls.ts"; | |
| 12 | import { getRepo } from "../core/repos.ts"; | |
| 13 | import { ownRepoPath } from "../kernel/own_repo.ts"; | |
| 14 | import { laneAlarm } from "./alarm_lane.ts"; | |
| 15 | ||
| 16 | export async function readLaneConflicts(db: Database, reposRoot: string, now: Date, ceilingUsd: number | null): Promise<void> { | |
| 17 | const waiting = laneAlarm(db, now, { ceilingUsd }).waiting ?? []; | |
| 18 | if (waiting.length === 0) return; | |
| 19 | const [owner, name] = ownRepoPath().split("/"); | |
| 20 | const repo = owner && name ? getRepo(db, owner, name) : undefined; | |
| 21 | if (repo === undefined) return; | |
| 22 | const pulls = waiting.map((w) => getPull(db, repo.id, w.number)).filter((p): p is Pull => p !== undefined); | |
| 23 | const dirs = await Promise.all(pulls.map((p) => pullDir(db, reposRoot, repo, p))); | |
| 24 | await pullMergeStates(pulls.map((pull, i) => ({ dir: dirs[i]!, pull }))); | |
| 25 | } |
src/flow/pages_read.ts +2
| @@ -39,6 +39,7 @@ import { rememberPullFiles } from "../crew/hot_files.ts"; | ||
| 39 | 39 | import { driftingPulls, readDrifts, type DriftingPull, type RepoDrift } from "./alarm_drift.ts"; |
| 40 | 40 | import { alarmUnproposed, type UnproposedAlarm } from "./alarm_unproposed.ts"; |
| 41 | 41 | import { firedAlarms, type FiredAlarm } from "./alarms.ts"; |
| 42 | import { readLaneConflicts } from "./lane_conflicts.ts"; | |
| 42 | 43 | import { readClogView, type ClogView } from "./clog_view.ts"; |
| 43 | 44 | import { cpuClosedPulls, type CpuClosedPull } from "./cpu_closed.ts"; |
| 44 | 45 | import { readHandoverLists, type HandoverLists } from "./handover_read.ts"; |
| @@ -306,6 +307,7 @@ export async function readBoardsPicture(env: PagesEnv, job: BoardsJob): Promise< | ||
| 306 | 307 | const fig = env.inline ? panelFigures(db, job.viewer, at) : readPanelFigures(db, at, publicOnly); |
| 307 | 308 | const flow = panelFlow(readThroughput(db, PIPELINE, at), job.out); |
| 308 | 309 | const repeats = readRepeats(db, "", at, publicOnly); |
| 310 | if (!publicOnly) await readLaneConflicts(db, env.reposRoot, at, env.ceilingUsd); | |
| 309 | 311 | const alarms = publicOnly ? [] : alarmsOf(env, at); |
| 310 | 312 | const held = (await later(env, `held:${job.viewer}`, HELD_KEEP_MS, job.now, "first", () => readHeld(appOf(env), !publicOnly, job.now))) ?? []; |
| 311 | 313 | const ms = performance.now() - t0; |
src/tui/screens/flow.ts +31 −1
| @@ -42,6 +42,7 @@ import { pileSays } from "../../flow/pile.ts"; | ||
| 42 | 42 | import { panelFigures } from "../../flow/panel_cache.ts"; |
| 43 | 43 | import { readPipeline, readStepStats, STAGE_HOLDS, STAGE_LABEL, statWords, verdict } from "../../flow/pipeline.ts"; |
| 44 | 44 | import { parkedSays } from "../../flow/parked.ts"; |
| 45 | import type { Waiting } from "../../flow/alarm_lane.ts"; | |
| 45 | 46 | import { firedAlarms } from "../../flow/alarms.ts"; |
| 46 | 47 | import { readVitals, vitalsFor, vitalsLine } from "../../flow/vitals.ts"; |
| 47 | 48 | import { readSilentChecks, silentWords } from "../../flow/silent_checks.ts"; |
| @@ -164,7 +165,10 @@ export function flowLines( | ||
| 164 | 165 | ...(agent === undefined ? clogLines(readClog(db, now), now.getTime()).flatMap((l) => wrap(l, inner)) : []), |
| 165 | 166 | ...(agent === undefined ? clogViewLines(readClogView(db, now), inner, wrap) : []), |
| 166 | 167 | ...(agent === undefined ? cpuStopLines(cpuStops(db, now).stopped, cpuClosedPulls(db, now), inner) : []), |
| 167 | ...firedAlarms(db, now, { publicOnly: agent !== undefined, ceilingUsd, dataDir }).flatMap((a) => wrap(a.action === undefined ? a.says : `${a.says} ${a.action}: ${origin}${a.href}`, inner)), | |
| 168 | ...firedAlarms(db, now, { publicOnly: agent !== undefined, ceilingUsd, dataDir }).flatMap((a) => [ | |
| 169 | ...wrap(a.action === undefined ? a.says : `${a.says} ${a.action}: ${origin}${a.href}`, inner), | |
| 170 | ...waitingLines(a.waiting ?? [], origin).flatMap((l) => wrap(l, inner)), | |
| 171 | ]), | |
| 168 | 172 | "", |
| 169 | 173 | ...wrap(parkedSays(fig.parked), inner), |
| 170 | 174 | "", |
| @@ -439,3 +443,29 @@ function silentLines(db: Database, dataDir: string, now: Date, publicOnly: boole | ||
| 439 | 443 | const silent = readSilentChecks(db, dataDir, now, publicOnly); |
| 440 | 444 | return silent.count === 0 ? [] : fold(`Miss: ${silentWords(silent)}.`, width); |
| 441 | 445 | } |
| 446 | ||
| 447 | /** | |
| 448 | * The lane alarm's green pulls (#18339), as /boards draws them: the ones that merge first, then, | |
| 449 | * under their own label, the ones that conflict with main. | |
| 450 | */ | |
| 451 | export function waitingLines(waiting: readonly Waiting[], origin: string): string[] { | |
| 452 | const ready = waiting.filter((p) => p.conflicts.length === 0); | |
| 453 | const stuck = waiting.filter((p) => p.conflicts.length > 0); | |
| 454 | return [ | |
| 455 | ...ready.map((p) => waitingLine(p, origin)), | |
| 456 | ...(stuck.length === 0 ? [] : [" Green pulls that need bringing up to date:", ...stuck.map((p) => waitingLine(p, origin))]), | |
| 457 | ]; | |
| 458 | } | |
| 459 | ||
| 460 | /** | |
| 461 | * One green pull under the lane alarm (#18339), as the web draws it under the alarm on /boards: | |
| 462 | * one that merges says where to merge it (the pull's screen and page carry the merge), one that | |
| 463 | * conflicts names its files. | |
| 464 | */ | |
| 465 | function waitingLine(p: Waiting, origin: string): string { | |
| 466 | // A title is an author's text: no control byte reaches the terminal. | |
| 467 | const title = p.title.replace(/[\x00-\x1f\x7f-\x9f]+/g, " "); | |
| 468 | return p.conflicts.length > 0 | |
| 469 | ? ` #${p.number} ${title}: conflicts with main in ${p.conflicts.join(", ")}: it merges once brought up to date: ${origin}${p.href}` | |
| 470 | : ` #${p.number} ${title}: green, merge it at ${origin}${p.href}`; | |
| 471 | } |
src/web/pages/boards-view.ts +2 −2
| @@ -31,7 +31,7 @@ import { formatValue } from "../../boards/chart.ts"; | ||
| 31 | 31 | import { boardsIndexLiveJs } from "../boards-index-live.ts"; |
| 32 | 32 | import { flowPanel, outAsked } from "./flow-panel.ts"; |
| 33 | 33 | import { boardsHoldsCard } from "./boards-holds.ts"; |
| 34 | import { alarmLines } from "../studio/alarm_lines.ts"; | |
| 34 | import { alarmLines, mayMergeOwn } from "../studio/alarm_lines.ts"; | |
| 35 | 35 | import { boardPicture, boardsPicture, isFailed, LONG_WAIT_MS, waitAnswer } from "../../flow/pages.ts"; |
| 36 | 36 | import { isPinned, listPins } from "../../boards/pins.ts"; |
| 37 | 37 | import { boardMarks } from "../../boards/marks.ts"; |
| @@ -163,7 +163,7 @@ export async function boardsList(ctx: Ctx): Promise<Response> { | ||
| 163 | 163 | </section>`; |
| 164 | 164 | const drift = driftSection(picture.drifting ?? []); |
| 165 | 165 | const flow = timePhase(ctx.request, "flow", () => |
| 166 | flowPanel(ctx.app, { kind: "owner" }, ctx.url, "/boards", { picture, refetch: false, now: html`${liveLine(ctx.url.searchParams.has("pause"))}${alarmLines(picture.alarms)}`, between: boards, first: drift, last: html`${checkSlotsFold(ctx.app)}${boardsHoldsCard(ctx.app.db, user, true)}` })); | |
| 166 | flowPanel(ctx.app, { kind: "owner" }, ctx.url, "/boards", { picture, refetch: false, now: html`${liveLine(ctx.url.searchParams.has("pause"))}${alarmLines(picture.alarms, mayMergeOwn(ctx.app.db, user))}`, between: boards, first: drift, last: html`${checkSlotsFold(ctx.app)}${boardsHoldsCard(ctx.app.db, user, true)}` })); | |
| 167 | 167 | const rendering = performance.now(); |
| 168 | 168 | const response = page({ |
| 169 | 169 | ctx, |
src/web/studio/alarm_lines.ts +60 −3
| @@ -6,10 +6,15 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | import type { Database } from "bun:sqlite"; |
| 8 | 8 | import { alarmBackup } from "../../flow/alarm_backup.ts"; |
| 9 | import type { Waiting } from "../../flow/alarm_lane.ts"; | |
| 9 | 10 | import { firedAlarms, type FiredAlarm } from "../../flow/alarms.ts"; |
| 11 | import { canWrite, getRepo } from "../../core/repos.ts"; | |
| 12 | import type { User } from "../../core/users.ts"; | |
| 10 | 13 | import { writeStamp } from "../../core/write_clock.ts"; |
| 14 | import { ownRepoPath } from "../../kernel/own_repo.ts"; | |
| 11 | 15 | import { Stamped } from "../../kernel/stamped.ts"; |
| 12 | 16 | import { html, type Html } from "../html.ts"; |
| 17 | import { btn, row, rowList } from "../ui.ts"; | |
| 13 | 18 | import { HOME_MAX_AGE_MS } from "./home_cache.ts"; |
| 14 | 19 | |
| 15 | 20 | const caches = new WeakMap<Database, Stamped<FiredAlarm[]>>(); |
| @@ -28,11 +33,63 @@ export function cachedAlarms(db: Database, now: Date, ceilingUsd: number | null, | ||
| 28 | 33 | return cache.get(`alarms:${backup.fired ? backup.since : "-"}:${backup.cause ?? ""}`, stamp, () => firedAlarms(db, now, { ceilingUsd, dataDir })); |
| 29 | 34 | } |
| 30 | 35 | |
| 31 | export function alarmLines(alarms: readonly FiredAlarm[]): Html { | |
| 36 | /** | |
| 37 | * The lane alarm's green pulls (#18339), under it in two lists: first the pulls that merge, each | |
| 38 | * with a Merge button posting to its own merge route (`/pulls/N/merge`, the one the pull page uses, | |
| 39 | * which checks again who may merge and whether it still merges); then, apart and labelled, the ones | |
| 40 | * that conflict with main, naming their files, with no button. Each row's title is the pull's own | |
| 41 | * title, its `#N` in the meta. Buttons only for `mayMerge`: a viewer who can write the forge's | |
| 42 | * repository. | |
| 43 | */ | |
| 44 | function waitingLines(waiting: readonly Waiting[], mayMerge: boolean): Html { | |
| 45 | const ready = waiting.filter((p) => p.conflicts.length === 0); | |
| 46 | const stuck = waiting.filter((p) => p.conflicts.length > 0); | |
| 47 | const readyList = | |
| 48 | ready.length === 0 | |
| 49 | ? null | |
| 50 | : rowList( | |
| 51 | ready.map((p) => | |
| 52 | row({ | |
| 53 | href: p.href, | |
| 54 | title: p.title, | |
| 55 | num: p.number, | |
| 56 | state: "open", | |
| 57 | meta: html`green, waiting on a person ${mayMerge ? "to merge" : "who can merge"}`, | |
| 58 | ...(mayMerge | |
| 59 | ? { after: html`<form method="post" action="${p.href}/merge"><input type="hidden" name="strategy" value="merge" />${btn(`Merge #${p.number}`, { size: "sm", icon: "merge" })}</form>` } | |
| 60 | : {}), | |
| 61 | })), | |
| 62 | { label: "Green pulls waiting on a person" }, | |
| 63 | ); | |
| 64 | const stuckList = | |
| 65 | stuck.length === 0 | |
| 66 | ? null | |
| 67 | : html`<p class="small muted">${STUCK_LABEL}</p>${rowList( | |
| 68 | stuck.map((p) => row({ href: p.href, title: p.title, num: p.number, state: "open", meta: html`conflicts with main in ${p.conflicts.join(", ")}: it merges once brought up to date` })), | |
| 69 | { label: STUCK_LABEL }, | |
| 70 | )}`; | |
| 71 | return html`${readyList}${stuckList}`; | |
| 72 | } | |
| 73 | ||
| 74 | /** The label over the green pulls that no longer merge into main. */ | |
| 75 | const STUCK_LABEL = "Green pulls that need bringing up to date"; | |
| 76 | ||
| 77 | export function alarmLines(alarms: readonly FiredAlarm[], mayMerge = false): Html { | |
| 32 | 78 | if (alarms.length === 0) return html``; |
| 33 | 79 | return html`<div class="stack"> |
| 34 | ${alarms.map((a) => a.action === undefined | |
| 80 | ${alarms.map((a) => html`${a.action === undefined | |
| 35 | 81 | ? html`<p class="small"><a href="${a.href}">${a.says}</a></p>` |
| 36 | : html`<p class="small">${a.says} <a href="${a.href}">${a.action}</a></p>`)} | |
| 82 | : html`<p class="small">${a.says} <a href="${a.href}">${a.action}</a></p>`}${waitingLines(a.waiting ?? [], mayMerge)}`)} | |
| 37 | 83 | </div>`; |
| 38 | 84 | } |
| 85 | ||
| 86 | /** | |
| 87 | * Whether `user` may merge into the forge's own repository: the lane alarm's Merge buttons are | |
| 88 | * theirs. Merging is a person's act; an agent proposes and is never shown one. | |
| 89 | */ | |
| 90 | export function mayMergeOwn(db: Database, user: User): boolean { | |
| 91 | if (user.kind === "agent") return false; | |
| 92 | const [owner, name] = ownRepoPath().split("/"); | |
| 93 | const repo = owner && name ? getRepo(db, owner, name) : undefined; | |
| 94 | return repo !== undefined && canWrite(db, repo, user); | |
| 95 | } |
test/flow-alarm-lane.test.ts +99 −5
| @@ -5,7 +5,7 @@ import { tmpdir } from "node:os"; | ||
| 5 | 5 | import { join } from "node:path"; |
| 6 | 6 | import { createApp, type App } from "../src/app.ts"; |
| 7 | 7 | import { createRepo, getRepo } from "../src/core/repos.ts"; |
| 8 | import { createUser } from "../src/core/users.ts"; | |
| 8 | import { createUser, getUserById } from "../src/core/users.ts"; | |
| 9 | 9 | import { getRun } from "../src/core/runs.ts"; |
| 10 | 10 | import { ownLandingStopped } from "../src/checks/gate_stopped.ts"; |
| 11 | 11 | import { hunt } from "../src/crew/sentinel.ts"; |
| @@ -13,6 +13,8 @@ import { laneStopped } from "../src/crew/sentinel_lane.ts"; | ||
| 13 | 13 | import { firedAlarms } from "../src/flow/alarms.ts"; |
| 14 | 14 | import { forgeFailed, laneAlarm, laneMinutes, maskLine } from "../src/flow/alarm_lane.ts"; |
| 15 | 15 | import { id } from "../src/kernel/id.ts"; |
| 16 | import { alarmLines, mayMergeOwn } from "../src/web/studio/alarm_lines.ts"; | |
| 17 | import { waitingLines } from "../src/tui/screens/flow.ts"; | |
| 16 | 18 | |
| 17 | 19 | const NOW = new Date("2026-11-12T17:11:00.000Z"); |
| 18 | 20 | const ago = (m: number) => new Date(NOW.getTime() - m * 60_000).toISOString(); |
| @@ -45,10 +47,10 @@ afterAll(() => { | ||
| 45 | 47 | else process.env[k] = v; |
| 46 | 48 | }); |
| 47 | 49 | |
| 48 | function pull(state: "open" | "closed", mergedMinAgo?: number): string { | |
| 50 | function pull(state: "open" | "closed", mergedMinAgo?: number, title = "P"): string { | |
| 49 | 51 | const issueId = id(); |
| 50 | 52 | n++; |
| 51 | app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at) VALUES (?1, ?2, ?3, 'pull', 'P', '', ?4, ?5, ?6)`).run(issueId, repoId, 2000 + n, userId, state, ago(600)); | |
| 53 | app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at) VALUES (?1, ?2, ?3, 'pull', ?7, '', ?4, ?5, ?6)`).run(issueId, repoId, 2000 + n, userId, state, ago(600), title); | |
| 52 | 54 | app.db |
| 53 | 55 | .query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, merged_at, merged_oid) VALUES (?1, 'main', ?2, '', 'c', ?3, ?4, ?5)`) |
| 54 | 56 | .run(issueId, `b/${n}`, repoId, mergedMinAgo === undefined ? null : ago(mergedMinAgo), mergedMinAgo === undefined ? "" : "9".repeat(40)); |
| @@ -194,7 +196,9 @@ describe("a stop that is the review queue's (#14337)", () => { | ||
| 194 | 196 | expect(a.green).toBe(2); |
| 195 | 197 | expect(a.onPeople).toBe(true); |
| 196 | 198 | expect(a.says).toContain(`2 pulls passed their checks and wait on a person to merge: ${names}`); |
| 197 | expect(a.facts).toContain(`2 pulls green, waiting on a person to merge: ${names}`); | |
| 199 | // The Sentinel issue's evidence links each pull to its page (#18339). | |
| 200 | const links = pulls.slice(0, 2).map((p) => `[#${numberOf(p)}](/trevato/forge/pulls/${numberOf(p)})`).join(", "); | |
| 201 | expect(a.facts).toContain(`2 pulls green, waiting on a person to merge: ${links}`); | |
| 198 | 202 | // The owner's /flow alarm carries the count and the pulls to merge. |
| 199 | 203 | const flow = lane(); |
| 200 | 204 | expect(flow).toHaveLength(1); |
| @@ -212,7 +216,7 @@ describe("a stop that is the review queue's (#14337)", () => { | ||
| 212 | 216 | const a = laneAlarm(app.db, NOW); |
| 213 | 217 | expect(a.green).toBe(1); |
| 214 | 218 | expect(a.onPeople).toBe(false); |
| 215 | expect(a.facts).toContain(`1 pull green, waiting on a person to merge: #${numberOf(green)}`); | |
| 219 | expect(a.facts).toContain(`1 pull green, waiting on a person to merge: [#${numberOf(green)}](/trevato/forge/pulls/${numberOf(green)})`); | |
| 216 | 220 | hunt(app, "trevato/forge", NOW.getTime()); |
| 217 | 221 | expect(open()).toHaveLength(1); |
| 218 | 222 | }); |
| @@ -384,3 +388,93 @@ describe("one cause failing many pulls", () => { | ||
| 384 | 388 | expect(maskLine(LINE)).toBe("stopped: used # cores for #s, over its limit of #"); |
| 385 | 389 | }); |
| 386 | 390 | }); |
| 391 | ||
| 392 | describe("the lane alarm's green pulls carry a Merge action (#18339)", () => { | |
| 393 | /** A stopped lane: one merge 200 min ago, two green pulls and one red one after it. */ | |
| 394 | function waitingLane(): { a: string; b: string } { | |
| 395 | reset(); | |
| 396 | pull("closed", 200); | |
| 397 | const a = pull("open", undefined, "Boards: name the <alarm> page"); | |
| 398 | const b = pull("open", undefined, "Games: lock the door"); | |
| 399 | passed(a, 120); | |
| 400 | passed(b, 110); | |
| 401 | failed(pull("open"), 100, "gate: rule broke"); | |
| 402 | return { a, b }; | |
| 403 | } | |
| 404 | const owner = () => getUserById(app.db, userId)!; | |
| 405 | ||
| 406 | test("green: each pull is listed with its page and a Merge form that posts to the pull's own merge route", () => { | |
| 407 | const { a, b } = waitingLane(); | |
| 408 | const [na, nb] = [numberOf(a), numberOf(b)]; | |
| 409 | const alarm = laneAlarm(app.db, NOW); | |
| 410 | expect(alarm.waiting).toEqual([ | |
| 411 | { number: na, title: "Boards: name the <alarm> page", href: `/trevato/forge/pulls/${na}`, conflicts: [] }, | |
| 412 | { number: nb, title: "Games: lock the door", href: `/trevato/forge/pulls/${nb}`, conflicts: [] }, | |
| 413 | ]); | |
| 414 | const fired = lane(); | |
| 415 | expect(fired[0]!.waiting).toEqual(alarm.waiting!); | |
| 416 | expect(mayMergeOwn(app.db, owner())).toBe(true); | |
| 417 | const out = String(alarmLines(fired, true)); | |
| 418 | for (const n of [na, nb]) { | |
| 419 | expect(out).toContain(`<a href="/trevato/forge/pulls/${n}" class="row"`); | |
| 420 | expect(out).toContain(`<form method="post" action="/trevato/forge/pulls/${n}/merge">`); | |
| 421 | expect(out).toContain(`<span class="row-num">#${n}</span>`); | |
| 422 | } | |
| 423 | // Each row is titled with the pull's own title, escaped. | |
| 424 | expect(out).toContain("Boards: name the <alarm> page"); | |
| 425 | expect(out).not.toContain("<alarm>"); | |
| 426 | expect(out).toContain("Games: lock the door"); | |
| 427 | // Someone who may not merge sees the pulls, never a button. | |
| 428 | const looking = String(alarmLines(fired, false)); | |
| 429 | expect(looking).toContain(`href="/trevato/forge/pulls/${na}"`); | |
| 430 | expect(looking).not.toContain("/merge"); | |
| 431 | // The terminal says the same pulls and where to merge them. | |
| 432 | expect(waitingLines(alarm.waiting!, "https://f.test")).toEqual([ | |
| 433 | ` #${na} Boards: name the <alarm> page: green, merge it at https://f.test/trevato/forge/pulls/${na}`, | |
| 434 | ` #${nb} Games: lock the door: green, merge it at https://f.test/trevato/forge/pulls/${nb}`, | |
| 435 | ]); | |
| 436 | // A reader of public alarms only is not handed the list. | |
| 437 | expect(firedAlarms(app.db, NOW, { dataDir: null, publicOnly: true }).find((x) => x.key === "lane_stopped")?.waiting).toBeUndefined(); | |
| 438 | }); | |
| 439 | ||
| 440 | test("green but conflicting: listed apart with its file named, and no Merge button", () => { | |
| 441 | // The conflicting pull is the older one: listed apart, it still comes after every Merge form. | |
| 442 | reset(); | |
| 443 | pull("closed", 200); | |
| 444 | const b = pull("open", undefined, "Games: lock the door"); | |
| 445 | const a = pull("open", undefined, "Boards: name the alarm page"); | |
| 446 | passed(b, 120); | |
| 447 | passed(a, 110); | |
| 448 | const [na, nb] = [numberOf(a), numberOf(b)]; | |
| 449 | const alarm = laneAlarm(app.db, NOW, { ceilingUsd: null, conflictsOf: (n, head) => (n === nb && head === "c" ? ["src/web/pages/games_door.ts"] : []) }); | |
| 450 | expect(alarm.says).toContain(`1 pull passed its check and waits on a person to merge: #${na}`); | |
| 451 | expect(alarm.says).toContain(`1 green pull no longer merges into main: #${nb} (conflicts in src/web/pages/games_door.ts)`); | |
| 452 | expect(alarm.facts).toContain(`1 pull green, waiting on a person to merge: [#${na}](/trevato/forge/pulls/${na})`); | |
| 453 | expect(alarm.facts).toContain( | |
| 454 | `1 pull green but conflicting with main, not mergeable until brought up to date: [#${nb}](/trevato/forge/pulls/${nb}) (conflicts in src/web/pages/games_door.ts)`, | |
| 455 | ); | |
| 456 | const out = String(alarmLines([{ key: "lane_stopped", says: alarm.says, since: alarm.since!, href: "/flow", waiting: alarm.waiting! }], true)); | |
| 457 | expect(out).toContain(`action="/trevato/forge/pulls/${na}/merge"`); | |
| 458 | expect(out).not.toContain(`action="/trevato/forge/pulls/${nb}/merge"`); | |
| 459 | expect(out).toContain("conflicts with main in src/web/pages/games_door.ts: it merges once brought up to date"); | |
| 460 | // Two lists: the mergeable pulls, then the conflicting ones under their own label. | |
| 461 | const lists = out.split(/<ul class="list"/).slice(1); | |
| 462 | expect(lists).toHaveLength(2); | |
| 463 | expect(lists[0]).toContain(`aria-label="Green pulls waiting on a person"`); | |
| 464 | expect(lists[0]).toContain(`action="/trevato/forge/pulls/${na}/merge"`); | |
| 465 | expect(lists[0]).not.toContain(`/pulls/${nb}"`); | |
| 466 | expect(lists[1]).toContain(`aria-label="Green pulls that need bringing up to date"`); | |
| 467 | expect(lists[1]).toContain(`href="/trevato/forge/pulls/${nb}"`); | |
| 468 | expect(lists[1]).not.toContain("<form"); | |
| 469 | expect(out.indexOf(`href="/trevato/forge/pulls/${nb}"`)).toBeGreaterThan(out.lastIndexOf("<form")); | |
| 470 | expect(out).toContain(`<p class="small muted">Green pulls that need bringing up to date</p>`); | |
| 471 | // The terminal lists them apart the same way. | |
| 472 | expect(waitingLines(alarm.waiting!, "")).toEqual([ | |
| 473 | ` #${na} Boards: name the alarm page: green, merge it at /trevato/forge/pulls/${na}`, | |
| 474 | " Green pulls that need bringing up to date:", | |
| 475 | ` #${nb} Games: lock the door: conflicts with main in src/web/pages/games_door.ts: it merges once brought up to date: /trevato/forge/pulls/${nb}`, | |
| 476 | ]); | |
| 477 | }); | |
| 478 | // Cleared by a real merge: `test/flow-lane-conflicts.test.ts`, which has a repository to merge | |
| 479 | // in. | |
| 480 | }); |
test/flow-lane-conflicts.test.ts added +146
| @@ -0,0 +1,146 @@ | ||
| 1 | /** | |
| 2 | * The lane alarm's green pulls are read against main before they are drawn (#18339): a green pull | |
| 3 | * whose branch no longer merges is named with its conflicting file and carries no Merge action; | |
| 4 | * one that merges does. A real repository, git's own merge-tree. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, 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/core/repos.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { getPull, lastConflicts, mergePull, pullStatus } from "../src/core/pulls.ts"; | |
| 14 | import { laneAlarm, lastMerge } from "../src/flow/alarm_lane.ts"; | |
| 15 | import { firedAlarms } from "../src/flow/alarms.ts"; | |
| 16 | import { readLaneConflicts } from "../src/flow/lane_conflicts.ts"; | |
| 17 | import { repoDir } from "../src/git/repos.ts"; | |
| 18 | import { id } from "../src/kernel/id.ts"; | |
| 19 | import { gitClientEnv } from "./helpers/git-client.ts"; | |
| 20 | ||
| 21 | const env = gitClientEnv(); | |
| 22 | const NOW = new Date("2026-12-05T12:00:00.000Z"); | |
| 23 | const ago = (m: number) => new Date(NOW.getTime() - m * 60_000).toISOString(); | |
| 24 | const saved = process.env["FORGE_SENTINEL_REPO"]; | |
| 25 | ||
| 26 | let root: string; | |
| 27 | let app: App; | |
| 28 | let repoId: string; | |
| 29 | let userId: string; | |
| 30 | let dir: string; | |
| 31 | const heads: Record<string, string> = {}; | |
| 32 | ||
| 33 | async function git(cwd: string, ...args: string[]): Promise<string> { | |
| 34 | const p = Bun.spawn(["git", ...args], { cwd, env, stdout: "pipe", stderr: "pipe" }); | |
| 35 | const [out, err, code] = await Promise.all([new Response(p.stdout).text(), new Response(p.stderr).text(), p.exited]); | |
| 36 | if (code !== 0) throw new Error(`git ${args.join(" ")}: ${err}`); | |
| 37 | return out.trim(); | |
| 38 | } | |
| 39 | ||
| 40 | function pullRow(number: number, state: "open" | "closed", branch: string, head: string, mergedMinAgo?: number): string { | |
| 41 | const issueId = id(); | |
| 42 | app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at) VALUES (?1, ?2, ?3, 'pull', 'P', '', ?4, ?5, ?6)`).run(issueId, repoId, number, userId, state, ago(600)); | |
| 43 | app.db | |
| 44 | .query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, merged_at, merged_oid) VALUES (?1, 'main', ?2, '', ?3, ?4, ?5, ?6)`) | |
| 45 | .run(issueId, branch, head, repoId, mergedMinAgo === undefined ? null : ago(mergedMinAgo), mergedMinAgo === undefined ? "" : "9".repeat(40)); | |
| 46 | return issueId; | |
| 47 | } | |
| 48 | ||
| 49 | function passed(issueId: string, minAgo: number, oid: string): void { | |
| 50 | app.db | |
| 51 | .query(`INSERT INTO run (id, repo_id, issue_id, kind, env, state, summary, check_detail, commit_oid, created_at, finished_at) VALUES (?1, ?2, ?3, 'check', 'pull-1', 'ok', 'check passed', '{}', ?5, ?4, ?4)`) | |
| 52 | .run(id(), repoId, issueId, ago(minAgo), oid); | |
| 53 | } | |
| 54 | ||
| 55 | beforeAll(async () => { | |
| 56 | process.env["FORGE_SENTINEL_REPO"] = "trevato/forge"; | |
| 57 | root = mkdtempSync(join(tmpdir(), "forge-lane-conflicts-")); | |
| 58 | app = createApp({ dataDir: root, port: 0 }); | |
| 59 | const u = await createUser(app.db, { username: "trevato", email: "[email protected]", password: "a-good-passphrase", origin: "seed", trusted: true, adminIfFirst: true }); | |
| 60 | if (!u.ok) throw new Error(u.error); | |
| 61 | userId = u.value.id; | |
| 62 | const repo = await createRepo(app.db, app.reposRoot, { ownerId: userId, name: "forge", visibility: "public", description: "f" }); | |
| 63 | if (!repo.ok) throw new Error(repo.error); | |
| 64 | repoId = repo.value.id; | |
| 65 | dir = repoDir(app.reposRoot, "trevato", "forge"); | |
| 66 | const work = join(root, "work"); | |
| 67 | await git(root, "init", "-q", "-b", "main", work); | |
| 68 | await Bun.write(join(work, "door.ts"), "one\n"); | |
| 69 | await git(work, "add", "."); | |
| 70 | await git(work, "commit", "-q", "-m", "base"); | |
| 71 | // `clean` adds a file of its own; `clash` and main both rewrite door.ts. | |
| 72 | await git(work, "checkout", "-q", "-b", "clean"); | |
| 73 | await Bun.write(join(work, "new.ts"), "x\n"); | |
| 74 | await git(work, "add", "."); | |
| 75 | await git(work, "commit", "-q", "-m", "clean"); | |
| 76 | await git(work, "checkout", "-q", "-b", "clash", "main"); | |
| 77 | await Bun.write(join(work, "door.ts"), "theirs\n"); | |
| 78 | await git(work, "commit", "-q", "-am", "clash"); | |
| 79 | await git(work, "checkout", "-q", "main"); | |
| 80 | await Bun.write(join(work, "door.ts"), "ours\n"); | |
| 81 | await git(work, "commit", "-q", "-am", "main moved"); | |
| 82 | // Fetched, not pushed: the forge's pre-receive hook is not what this tests. | |
| 83 | await git(dir, "fetch", "-q", work, "main:main", "clean:clean", "clash:clash"); | |
| 84 | for (const b of ["clean", "clash"]) heads[b] = await git(work, "rev-parse", b); | |
| 85 | }); | |
| 86 | ||
| 87 | afterAll(() => { | |
| 88 | app.close(); | |
| 89 | rmSync(root, { recursive: true, force: true }); | |
| 90 | if (saved === undefined) delete process.env["FORGE_SENTINEL_REPO"]; | |
| 91 | else process.env["FORGE_SENTINEL_REPO"] = saved; | |
| 92 | }); | |
| 93 | ||
| 94 | test("a green pull that no longer merges is named with its file and loses its Merge action; one that merges keeps it", async () => { | |
| 95 | pullRow(1, "closed", "old", "o", 200); | |
| 96 | passed(pullRow(2, "open", "clean", heads["clean"]!), 120, heads["clean"]!); | |
| 97 | passed(pullRow(3, "open", "clash", heads["clash"]!), 110, heads["clash"]!); | |
| 98 | // Nothing read yet: both are listed as mergeable, and the merge route would refuse the clash. | |
| 99 | const before = firedAlarms(app.db, NOW, { dataDir: root }).find((a) => a.key === "lane_stopped"); | |
| 100 | expect(before?.waiting?.map((w) => w.conflicts)).toEqual([[], []]); | |
| 101 | ||
| 102 | await readLaneConflicts(app.db, app.reposRoot, NOW, null); | |
| 103 | const lane = firedAlarms(app.db, NOW, { dataDir: root }).find((a) => a.key === "lane_stopped"); | |
| 104 | expect(lane?.waiting).toEqual([ | |
| 105 | { number: 2, title: "P", href: "/trevato/forge/pulls/2", conflicts: [] }, | |
| 106 | { number: 3, title: "P", href: "/trevato/forge/pulls/3", conflicts: ["door.ts"] }, | |
| 107 | ]); | |
| 108 | expect(lane?.says).toContain("1 green pull no longer merges into main: #3 (conflicts in door.ts)"); | |
| 109 | }); | |
| 110 | ||
| 111 | test("the pull page's own reading is what the alarm reads next", async () => { | |
| 112 | const pull = getPull(app.db, repoId, 3)!; | |
| 113 | const status = await pullStatus(dir, pull); | |
| 114 | expect(status.conflicts).toEqual(["door.ts"]); | |
| 115 | expect(lastConflicts(dir, 3, heads["clash"]!)).toEqual(["door.ts"]); | |
| 116 | // A reading of another head says nothing about this one. | |
| 117 | expect(lastConflicts(dir, 3, "f".repeat(40))).toEqual([]); | |
| 118 | }); | |
| 119 | ||
| 120 | test("cleared by a real merge: the merge's row moves the lane's last merge, and the alarm with its list ends", async () => { | |
| 121 | const before = laneAlarm(app.db, NOW); | |
| 122 | expect(before.fired).toBe(true); | |
| 123 | expect(before.since).toBe(ago(200)); | |
| 124 | const merged = await mergePull(app.db, dir, getPull(app.db, repoId, 2)!, { | |
| 125 | strategy: "merge", | |
| 126 | message: "Merge #2", | |
| 127 | actorId: userId, | |
| 128 | author: { name: "trevato", email: "[email protected]" }, | |
| 129 | }); | |
| 130 | if (!merged.ok) throw new Error(JSON.stringify(merged.error)); | |
| 131 | // The row the merge wrote is the lane's last merge now. | |
| 132 | const row = getPull(app.db, repoId, 2)!; | |
| 133 | expect(row.mergedAt).not.toBeNull(); | |
| 134 | expect(lastMerge(app.db, repoId)).toBe(row.mergedAt); | |
| 135 | // A minute after the merge the lane has moved: no alarm, no list of pulls to merge. | |
| 136 | const at = Date.parse(row.mergedAt!); | |
| 137 | const after = laneAlarm(app.db, new Date(at + 60_000)); | |
| 138 | expect(after.fired).toBe(false); | |
| 139 | expect(after.waiting).toBeUndefined(); | |
| 140 | // Stopped again 91 minutes on, the alarm counts from that merge: its \`since\` is the merge | |
| 141 | // row's. | |
| 142 | const again = laneAlarm(app.db, new Date(at + 91 * 60_000)); | |
| 143 | expect(again.fired).toBe(true); | |
| 144 | expect(again.since).toBe(row.mergedAt!); | |
| 145 | expect(again.facts).toContain(`last merge at ${row.mergedAt}`); | |
| 146 | }); |
new above
offline