Public: anyone can read this; the crew can change it.
An issue whose body opens "Follow-up to #N" waits for #N on the map, and the page and the terminal say so #16762
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/12389-an-issue-whose-body-opens-follow-up-to-n → main 0 commits fork
5 files
changed +200
−0
src/actions/issues.ts +3
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | * "mirrored" on the others, which is honest rather than silently wrong. |
| 19 | 19 | */ |
| 20 | 20 | |
| 21 | import { linkFollowUp } from "../core/follow_up.ts"; | |
| 21 | 22 | import { isEngine } from "../core/engine.ts"; |
| 22 | 23 | import { mayView, setSealed } from "../core/sealed.ts"; |
| 23 | 24 | import { requireVerified } from "./verify.ts"; |
| @@ -141,6 +142,7 @@ export function openIssue( | ||
| 141 | 142 | sealed: input.sealed === true, |
| 142 | 143 | }); |
| 143 | 144 | if (!created.ok) return created; |
| 145 | linkFollowUp(app.db, created.value); | |
| 144 | 146 | // A chapter is open (#11603): the planner's issue waits in the next chapter, never in this one. |
| 145 | 147 | if (user.value.kind === "agent" && crewRole(app.db, repo.id, user.value.id) === "planner") |
| 146 | 148 | keepWithinChapter(app, repo, created.value, user.value); |
| @@ -294,6 +296,7 @@ export function editIssue( | ||
| 294 | 296 | if (changed.length === 0) return ok(issue); |
| 295 | 297 | const updated = updateIssueText(app.db, issue.id, input); |
| 296 | 298 | if (!updated.ok) return updated; |
| 299 | linkFollowUp(app.db, updated.value); | |
| 297 | 300 | if (issue.kind === "pull" && changed.includes("body")) adoptResolution(app.db, issue.id); |
| 298 | 301 | app.bus.append({ |
| 299 | 302 | kind: `${issue.kind}.edited`, |
src/actions/plan.ts +2
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | * nothing reaches the ready list without a reason to be built now. |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | import { linkFollowUp } from "../core/follow_up.ts"; | |
| 13 | 14 | import { ceilingHold, EVIDENCE_ROOM, hasEvidence, pausedUntil, PAUSE_REFUSAL } from "../core/planner_pause.ts"; |
| 14 | 15 | import { splitPending } from "./issues.ts"; |
| 15 | 16 | import { sealedScope } from "../core/sealed.ts"; |
| @@ -391,6 +392,7 @@ export function openPlan( | ||
| 391 | 392 | message: `"${item.key}": ${created.error}`, |
| 392 | 393 | }); |
| 393 | 394 | made.set(item.key, created.value); |
| 395 | linkFollowUp(app.db, created.value); | |
| 394 | 396 | } |
| 395 | 397 | for (const item of items) |
| 396 | 398 | for (const w of item.blockedBy) { |
src/core/follow_up.ts added +40
| @@ -0,0 +1,40 @@ | ||
| 1 | /** | |
| 2 | * "Follow-up to #N": an issue whose body opens with that line waits for #N. | |
| 3 | * The words are the plan, so the edge is written from them wherever words | |
| 4 | * arrive: opened, edited, planned, or imported from an origin. The map, the | |
| 5 | * page and the terminal then read the edge like any other; it stops holding | |
| 6 | * the issue when #N closes, as every wait does. | |
| 7 | */ | |
| 8 | ||
| 9 | import type { Database } from "bun:sqlite"; | |
| 10 | import { addDependency } from "./dependencies.ts"; | |
| 11 | import { getIssue, getIssueById, type Issue } from "./issues.ts"; | |
| 12 | ||
| 13 | const FOLLOW_UP = /^Follow-up to #(\d{1,9})[ \t]*$/; | |
| 14 | ||
| 15 | /** The number a body's first line says it follows up, if it does. */ | |
| 16 | export function followUpOf(body: string): number | undefined { | |
| 17 | const first = body.replace(/^/, "").split(/\r?\n/, 1)[0] ?? ""; | |
| 18 | const m = FOLLOW_UP.exec(first); | |
| 19 | return m === null ? undefined : Number(m[1]); | |
| 20 | } | |
| 21 | ||
| 22 | /** | |
| 23 | * Make `issue` wait for the issue its body follows up, while that is open. | |
| 24 | * A number that is no issue here, a closed one, itself, or one that would | |
| 25 | * make a loop is left alone: the words stay, the map is not changed. | |
| 26 | */ | |
| 27 | export function linkFollowUp(db: Database, issue: Issue): void { | |
| 28 | if (issue.kind !== "issue" || issue.state !== "open") return; | |
| 29 | const n = followUpOf(issue.body); | |
| 30 | if (n === undefined) return; | |
| 31 | const blocker = getIssue(db, issue.repoId, n); | |
| 32 | if (blocker === undefined || blocker.state !== "open") return; | |
| 33 | addDependency(db, issue, blocker, undefined); | |
| 34 | } | |
| 35 | ||
| 36 | /** `linkFollowUp` for an issue known by id (an import has only the id). */ | |
| 37 | export function linkFollowUpById(db: Database, issueId: string): void { | |
| 38 | const issue = getIssueById(db, issueId); | |
| 39 | if (issue !== undefined) linkFollowUp(db, issue); | |
| 40 | } |
src/origins/import.ts +2
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | |
| 20 | 20 | import type { Database } from "bun:sqlite"; |
| 21 | 21 | import type { Label } from "../core/issues.ts"; |
| 22 | import { linkFollowUpById } from "../core/follow_up.ts"; | |
| 22 | 23 | import { hashLine } from "../core/reviews.ts"; |
| 23 | 24 | import type { Diff } from "../git/diff.ts"; |
| 24 | 25 | import { isSafeOid, isSafeRef } from "../git/guard.ts"; |
| @@ -288,6 +289,7 @@ export function importIssue( | ||
| 288 | 289 | } |
| 289 | 290 | }); |
| 290 | 291 | write(); |
| 292 | linkFollowUpById(db, issueId); | |
| 291 | 293 | |
| 292 | 294 | return { |
| 293 | 295 | issueId, |
test/follow-up-waits.test.ts added +153
| @@ -0,0 +1,153 @@ | ||
| 1 | /** | |
| 2 | * An issue whose body opens "Follow-up to #N" waits for #N while #N is open, | |
| 3 | * through the action, the plan route and an origin's import, and the issue | |
| 4 | * page and the terminal each say so. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { editIssue, openIssue, setIssueState } from "../src/actions/issues.ts"; | |
| 11 | import { openPlan } from "../src/actions/plan.ts"; | |
| 12 | import { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { dependenciesOf } from "../src/core/dependencies.ts"; | |
| 15 | import { followUpOf } from "../src/core/follow_up.ts"; | |
| 16 | import { getIssue, type Issue } from "../src/core/issues.ts"; | |
| 17 | import type { Repo } from "../src/core/repos.ts"; | |
| 18 | import { createUser, type User } from "../src/core/users.ts"; | |
| 19 | import { importIssue } from "../src/origins/import.ts"; | |
| 20 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 21 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 22 | import { issueScreen } from "../src/tui/screens/issue.ts"; | |
| 23 | import { local } from "../src/tui/screens/where.ts"; | |
| 24 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 25 | import { quietLogs } from "./helpers/globals.ts"; | |
| 26 | import { settleApp } from "./helpers/tui.ts"; | |
| 27 | ||
| 28 | quietLogs(); | |
| 29 | ||
| 30 | describe("Follow-up to #N", () => { | |
| 31 | let dataDir = ""; | |
| 32 | let app: App; | |
| 33 | let servers: Servers; | |
| 34 | let alice: User; | |
| 35 | let repo: Repo; | |
| 36 | const openWith = (title: string, body = ""): Issue => { | |
| 37 | const made = openIssue(app, alice, repo, { title, body }); | |
| 38 | return made.ok ? made.value : (() => { throw new Error(made.error); })(); | |
| 39 | }; | |
| 40 | const waits = (i: Issue) => dependenciesOf(app.db, i.id).blockedBy.map((b) => b.number); | |
| 41 | ||
| 42 | beforeAll(async () => { | |
| 43 | dataDir = await mkdtemp(join(tmpdir(), "forge-follow-up-")); | |
| 44 | app = createApp({ dataDir, port: 0 }); | |
| 45 | servers = await serve(app); | |
| 46 | const made = await createUser(app.db, { | |
| 47 | origin: "seed", | |
| 48 | trusted: true, | |
| 49 | username: "alice", | |
| 50 | email: "[email protected]", | |
| 51 | password: "a-good-passphrase", | |
| 52 | adminIfFirst: true, | |
| 53 | }); | |
| 54 | if (!made.ok) throw new Error(made.error); | |
| 55 | alice = made.value; | |
| 56 | const r = await createRepo(app, alice, { name: "garden", visibility: "public" }); | |
| 57 | if (!r.ok) throw new Error(String(r.error)); | |
| 58 | repo = r.value; | |
| 59 | }, 30_000); | |
| 60 | ||
| 61 | afterAll(async () => { | |
| 62 | servers.stop(); | |
| 63 | app.close(); | |
| 64 | await rm(dataDir, { recursive: true, force: true }); | |
| 65 | }); | |
| 66 | ||
| 67 | test("only a first line of exactly that shape counts", () => { | |
| 68 | expect(followUpOf("Follow-up to #12\n\nmore")).toBe(12); | |
| 69 | expect(followUpOf("Follow-up to #12\r\nmore")).toBe(12); | |
| 70 | expect(followUpOf("Intro\nFollow-up to #12")).toBeUndefined(); | |
| 71 | expect(followUpOf("Follow-up to #12 and more")).toBeUndefined(); | |
| 72 | expect(followUpOf("")).toBeUndefined(); | |
| 73 | }); | |
| 74 | ||
| 75 | test("opening and editing link the wait; it holds only while #N is open", () => { | |
| 76 | const first = openWith("First"); | |
| 77 | const next = openWith("Next", `Follow-up to #${first.number}\n\nThe rest.`); | |
| 78 | expect(waits(next)).toEqual([first.number]); | |
| 79 | ||
| 80 | const later = openWith("Later"); | |
| 81 | expect(waits(later)).toEqual([]); | |
| 82 | const edited = editIssue(app, alice, repo, later, { body: `Follow-up to #${first.number}` }); | |
| 83 | expect(edited.ok).toBe(true); | |
| 84 | expect(waits(later)).toEqual([first.number]); | |
| 85 | ||
| 86 | const done = setIssueState(app, alice, repo, first, "closed"); | |
| 87 | expect(done.ok).toBe(true); | |
| 88 | // A closed issue is not waited for from the start. | |
| 89 | const after = openWith("After", `Follow-up to #${first.number}`); | |
| 90 | expect(waits(after)).toEqual([]); | |
| 91 | // Neither is a number that is no issue here, nor the issue itself. | |
| 92 | expect(waits(openWith("Nowhere", "Follow-up to #99999"))).toEqual([]); | |
| 93 | }); | |
| 94 | ||
| 95 | test("the plan route links it too", () => { | |
| 96 | const first = openWith("Plan base"); | |
| 97 | const planned = openPlan(app, alice, repo, [ | |
| 98 | { key: "a", title: "Planned follow-up", body: `Follow-up to #${first.number}`, blockedBy: [] }, | |
| 99 | ]); | |
| 100 | expect(planned.ok).toBe(true); | |
| 101 | if (!planned.ok) return; | |
| 102 | const number = planned.value.opened[0]!.number; | |
| 103 | expect(waits(getIssue(app.db, repo.id, number)!)).toEqual([first.number]); | |
| 104 | }); | |
| 105 | ||
| 106 | test("an origin's import links it", () => { | |
| 107 | const first = openWith("Mirrored base"); | |
| 108 | const got = importIssue( | |
| 109 | { db: app.db, repoId: repo.id, actorId: alice.id }, | |
| 110 | { | |
| 111 | remoteId: "r-follow", | |
| 112 | number: 500, | |
| 113 | title: "From the origin", | |
| 114 | body: `Follow-up to #${first.number}\n\ntext`, | |
| 115 | author: "someone", | |
| 116 | state: "open", | |
| 117 | labels: [], | |
| 118 | createdAt: "2026-01-01T00:00:00.000Z", | |
| 119 | updatedAt: "2026-01-01T00:00:00.000Z", | |
| 120 | closedAt: null, | |
| 121 | htmlUrl: "https://example.test/500", | |
| 122 | comments: 0, | |
| 123 | }, | |
| 124 | ); | |
| 125 | expect(dependenciesOf(app.db, got.issueId).blockedBy.map((b) => b.number)).toEqual([first.number]); | |
| 126 | }); | |
| 127 | ||
| 128 | test("the issue page says what it waits for", async () => { | |
| 129 | const first = openWith("Page base"); | |
| 130 | const next = openWith("Page next", `Follow-up to #${first.number}`); | |
| 131 | const html = await (await fetch(`${servers.url}/alice/garden/issues/${next.number}`)).text(); | |
| 132 | expect(html).toContain(`Waits for #${first.number}`); | |
| 133 | }); | |
| 134 | ||
| 135 | test("the terminal says what it waits for", async () => { | |
| 136 | const first = openWith("Terminal base"); | |
| 137 | const next = openWith("Terminal next", `Follow-up to #${first.number}`); | |
| 138 | const terminal = headlessTerminal(140, 40); | |
| 139 | const { app: tui, session } = createForgeApp( | |
| 140 | app, | |
| 141 | { sshUsername: "t", remoteAddress: "127.0.0.1", user: alice }, | |
| 142 | { terminal, exitHooks: false }, | |
| 143 | ); | |
| 144 | tui.start(); | |
| 145 | await settleApp(tui); | |
| 146 | tui.push(issueScreen(session, tui.ctx, local(session, repo), next.number)); | |
| 147 | await settleApp(tui); | |
| 148 | const text = tui.screen.toText(); | |
| 149 | tui.stop(); | |
| 150 | expect(text).toContain("Waits for"); | |
| 151 | expect(text).toContain(`#${first.number}`); | |
| 152 | }); | |
| 153 | }); |
new above
offline