Public: anyone can read this; the crew can change it.
A push to main recombines every open kept-fork pull, so its combined preview is never stale for long #9976
scout/forge:scout/9931-a-push-to-main-recombines-every-open-kep → main 9 commits fork
9 files
changed +361
−11
docs/features/upstream-landing.md +10
| @@ -24,9 +24,15 @@ The rule lives in `landingKind` (`src/core/landing_kind.ts`) and `mergePull` app | ||
| 24 | 24 | |
| 25 | 25 | When main has moved since a kept fork's head, the forge merges main into that head in a branch of the base repository, `combined/N` (`src/git/combined.ts`), on opening the pull, on every push to the fork, and again just before landing. The author's branch is never written. Each conflicting block keeps main's side, with no review: the author's side of that block is dropped from the landed tree, and the note says so and how to get it back (merge main into their branch, resolve, push: a head that already has main is its own combined build). A conflict git cannot pick a side of (modify/delete, rename) is left open. The combined commit's message is the note, and its `Combined-Base` and `Combined-Kept` trailers are the facts the pull renders from (`src/core/combined_view.ts`): a "Combined with main" card on the page and the same section on the terminal's pull screen. The preview builds `combined/N`, under the approval of the head it carries (read from git, `headUnder`), and landing merges it, so the author's head stays an ancestor of main. Commits pushed on top of `combined/N` by a writer are kept by the next combining. |
| 26 | 26 | |
| 27 | <<<<<<< HEAD | |
| 28 | A push to main recombines every open kept-fork pull into it (`src/core/recombine.ts`, #9931): off the push's answer, one pass at a time per repository, a burst of pushes coalescing into one more pass. The card marks itself stale, naming the main commit it was built on, only while that runs; if a rebuild fails it stays stale and says why, until the next push to the fork or main, or the landing. Checks run on the pull's head, not on the combined commit, and the card says that too. | |
| 29 | ||
| 30 | Test: `test/combined-fork.test.ts` (open, page, terminal, a push recombining, landing), `test/combined-recombine.test.ts` (main alone moving, a failed rebuild, coalescing) and `test/combined-git.test.ts` (a writer's commit surviving, modify/delete left open, the head a preview runs under). | |
| 31 | ======= | |
| 27 | 32 | A push to main alone does not recombine: the card marks itself stale, naming the main commit it was built on, until the next push to the fork or the landing recombines. The pull's fast check runs on the combined commit, not the author's head alone (`checks/service.ts`): its log and the checks line say "checked as combined with main at abc1234", the run records that base, and the green is stale only when `combined/N` is built on an older main than the tip. A pull from inside one repository is still checked as its head merged into main. The run stays keyed to the head (`commit_oid`), so head lookups and the merge gate find it; the combined commit it tested is recorded on the run (`combined_oid`), and the checks line reads "as combined" from that, never from the tree, so a check that ran before `combined/N` existed says "against". |
| 28 | 33 | |
| 29 | 34 | Test: `test/combined-check.test.ts` (the check's source, its words, stale, an in-repository pull unchanged), `test/combined-fork.test.ts` (open, page, terminal, a push recombining, stale, landing) and `test/combined-git.test.ts` (a writer's commit surviving, modify/delete left open, the head a preview runs under). |
| 35 | >>>>>>> origin/main | |
| 30 | 36 | |
| 31 | 37 | ## A merge must bring the work |
| 32 | 38 | |
| @@ -38,4 +44,8 @@ Test: `test/source-merge-held.test.ts` (two forges, an `-s ours` merge held, a m | ||
| 38 | 44 | |
| 39 | 45 | ## Not yet |
| 40 | 46 | |
| 47 | <<<<<<< HEAD | |
| 48 | Landing a very large fork in reviewed slices that still end as a merge of the author's head; routing a pull opened on the Forgejo mirror to trunk; a crew run sent to refine `combined/N` beyond main's side; running the check on the combined commit; and running his code under the guest rule until a person approves (#8903 is not in this repository). Resolving conflicts on our side is today's resolve page (`docs/PULLS.md`). | |
| 49 | ======= | |
| 41 | 50 | Landing a very large fork in reviewed slices that still end as a merge of the author's head; routing a pull opened on the Forgejo mirror to trunk; a crew run sent to refine `combined/N` beyond main's side; recombining when main alone moves and running his code under the guest rule until a person approves (#8903 is not in this repository). Resolving conflicts on our side is today's resolve page (`docs/PULLS.md`). |
| 51 | >>>>>>> origin/main |
src/actions/pulls.ts +15 −1
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | * issues — a pull is an issue with a diff. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { exclusive } from "../core/recombine.ts"; | |
| 7 | 8 | import { isContributionFork, type GuestRefusal } from "../core/contribute.ts"; |
| 8 | 9 | import { takeGuestPull } from "./guests.ts"; |
| 9 | 10 | import { requireAbility } from "./roles.ts"; |
| @@ -468,7 +469,20 @@ function postLine( | ||
| 468 | 469 | return added; |
| 469 | 470 | } |
| 470 | 471 | |
| 471 | export async function mergePull( | |
| 472 | /** Landing takes the repository's turn: it never overlaps a recombine pass (#9931). */ | |
| 473 | export function mergePull( | |
| 474 | app: App, | |
| 475 | actor: User | undefined, | |
| 476 | repo: Repo, | |
| 477 | pull: Pull, | |
| 478 | requested: MergeStrategy, | |
| 479 | expect?: LandingExpect, | |
| 480 | opts: { readonly pastGate?: boolean } = {}, | |
| 481 | ): ReturnType<typeof mergeNow> { | |
| 482 | return exclusive(repo.id, () => mergeNow(app, actor, repo, pull, requested, expect, opts)); | |
| 483 | } | |
| 484 | ||
| 485 | async function mergeNow( | |
| 472 | 486 | app: App, |
| 473 | 487 | actor: User | undefined, |
| 474 | 488 | repo: Repo, |
src/core/combined_view.ts +8 −2
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import type { Pull } from "./pulls.ts"; |
| 9 | import { recombineFailure } from "./recombine.ts"; | |
| 9 | 10 | import { keptLine, readCombined, TAKE_BACK, type Kept } from "../git/combined.ts"; |
| 10 | 11 | |
| 11 | 12 | export interface CombinedView { |
| @@ -15,6 +16,8 @@ export interface CombinedView { | ||
| 15 | 16 | readonly builtOn: string; |
| 16 | 17 | /** Main has moved past `builtOn`: the preview is not what lands until it recombines. */ |
| 17 | 18 | readonly stale: boolean; |
| 19 | /** Why the rebuild on the new main failed, when it did. */ | |
| 20 | readonly failure?: string; | |
| 18 | 21 | readonly files: readonly Kept[]; |
| 19 | 22 | } |
| 20 | 23 | |
| @@ -29,11 +32,14 @@ export async function combinedView( | ||
| 29 | 32 | const read = await readCombined(dir, pull.number, headOid); |
| 30 | 33 | // Main already in the head: the preview is the head itself, nothing to say. |
| 31 | 34 | if (read === undefined || read.oid === headOid) return undefined; |
| 35 | const stale = read.baseOid !== "" && read.baseOid !== baseOid; | |
| 36 | const failure = stale ? recombineFailure(pull.repoId, pull.number) : undefined; | |
| 32 | 37 | return { |
| 38 | ...(failure === undefined ? {} : { failure }), | |
| 33 | 39 | branch: read.branch, |
| 34 | 40 | oid: read.oid, |
| 35 | 41 | builtOn: read.baseOid, |
| 36 | stale: read.baseOid !== "" && read.baseOid !== baseOid, | |
| 42 | stale, | |
| 37 | 43 | files: read.files, |
| 38 | 44 | }; |
| 39 | 45 | } |
| @@ -46,7 +52,7 @@ export function combinedWords( | ||
| 46 | 52 | const notes: string[] = []; |
| 47 | 53 | if (view.stale) |
| 48 | 54 | notes.push( |
| 49 | `Stale: main has moved since ${view.builtOn.slice(0, 10)}, which this was built on. It recombines on the next push to the fork, and merging recombines before it lands, so what lands may differ from the preview.`, | |
| 55 | `Stale: main has moved since ${view.builtOn.slice(0, 10)}, which this was built on. ${view.failure === undefined ? "It recombines on its own" : `Recombining failed: ${view.failure} It recombines on the next push`}, and merging recombines before it lands, so what lands may differ from the preview.`, | |
| 50 | 56 | ); |
| 51 | 57 | notes.push("The checks line says which commit the check ran on."); |
| 52 | 58 | if (view.files.some((f) => f.kept === "main")) notes.push(TAKE_BACK); |
src/core/recombine.ts added +109
| @@ -0,0 +1,109 @@ | ||
| 1 | /** | |
| 2 | * A push to a repository's default branch recombines every open kept-fork | |
| 3 | * pull into it (#9931): `combined/N` is rebuilt on the new tip, so the card | |
| 4 | * is stale only while the rebuild runs. Off the request path, one rebuild | |
| 5 | * pass at a time per repository; pushes that arrive during a pass coalesce | |
| 6 | * into one more pass that reads the tip as it then stands. A rebuild that | |
| 7 | * fails leaves the card stale and keeps the failure's sentence for it. | |
| 8 | */ | |
| 9 | ||
| 10 | import type { App } from "../app.ts"; | |
| 11 | import type { Repo } from "./repos.ts"; | |
| 12 | import { listOpenPulls } from "./pulls.ts"; | |
| 13 | import { fromKeptFork } from "./landing_kind.ts"; | |
| 14 | import { combine } from "../git/combined.ts"; | |
| 15 | import { resolveOid } from "../git/merge.ts"; | |
| 16 | import { repoDir } from "../git/repos.ts"; | |
| 17 | ||
| 18 | type Combine = typeof combine; | |
| 19 | ||
| 20 | interface Lane { | |
| 21 | running: Promise<void>; | |
| 22 | again: boolean; | |
| 23 | } | |
| 24 | ||
| 25 | const lanes = new Map<string, Lane>(); | |
| 26 | const failures = new Map<string, string>(); | |
| 27 | const turns = new Map<string, Promise<unknown>>(); | |
| 28 | ||
| 29 | /** | |
| 30 | * Run `work` when nothing else holds this repository's turn: a pass and a | |
| 31 | * landing never overlap, so a merge cannot lose its compare-and-set to a | |
| 32 | * rebuild and land the bare head instead of `combined/N`. | |
| 33 | */ | |
| 34 | export function exclusive<T>(repoId: string, work: () => Promise<T>): Promise<T> { | |
| 35 | const mine = (turns.get(repoId) ?? Promise.resolve()).then(work, work); | |
| 36 | const tail = mine.catch(() => undefined); | |
| 37 | turns.set(repoId, tail); | |
| 38 | void tail.then(() => { | |
| 39 | if (turns.get(repoId) === tail) turns.delete(repoId); | |
| 40 | }); | |
| 41 | return mine; | |
| 42 | } | |
| 43 | ||
| 44 | /** Resolves when no pass is running or waiting for this repository. */ | |
| 45 | export async function settled(repoId: string): Promise<void> { | |
| 46 | for (let lane = lanes.get(repoId); lane !== undefined; lane = lanes.get(repoId)) await lane.running; | |
| 47 | } | |
| 48 | ||
| 49 | const key = (repoId: string, number: number): string => `${repoId}:${number}`; | |
| 50 | ||
| 51 | /** Why the last rebuild of this pull failed, while it is still the last word. */ | |
| 52 | export const recombineFailure = (repoId: string, number: number): string | undefined => | |
| 53 | failures.get(key(repoId, number)); | |
| 54 | ||
| 55 | /** One pass: each open kept-fork pull into this repository, one after another. */ | |
| 56 | async function pass(app: App, repo: Repo, run: Combine): Promise<void> { | |
| 57 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 58 | const open = listOpenPulls(app.db, repo.id); | |
| 59 | // A pull that is no longer open keeps no sentence. | |
| 60 | for (const k of [...failures.keys()]) | |
| 61 | if (k.startsWith(`${repo.id}:`) && !open.some((p) => key(repo.id, p.number) === k)) failures.delete(k); | |
| 62 | for (const pull of open) { | |
| 63 | if (pull.baseBranch !== repo.defaultBranch || !fromKeptFork(app.db, pull)) continue; | |
| 64 | const k = key(repo.id, pull.number); | |
| 65 | try { | |
| 66 | const baseOid = await resolveOid(dir, `refs/heads/${pull.baseBranch}`); | |
| 67 | const headOid = (await resolveOid(dir, `refs/pull/${pull.number}/head`)) ?? pull.headOid; | |
| 68 | if (baseOid === undefined || !headOid) { | |
| 69 | failures.delete(k); | |
| 70 | continue; | |
| 71 | } | |
| 72 | const built = await run(dir, { number: pull.number, baseOid, headOid }); | |
| 73 | if (built === undefined) failures.set(k, "git could not merge main into this head."); | |
| 74 | else failures.delete(k); | |
| 75 | } catch (error) { | |
| 76 | failures.set(k, "the rebuild did not finish."); | |
| 77 | app.log.warn("could not recombine a pull", { | |
| 78 | repo: `${repo.ownerName}/${repo.name}`, | |
| 79 | pull: pull.number, | |
| 80 | error: String(error), | |
| 81 | }); | |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Ask for a recombine of this repository's pulls. Answers at once; the | |
| 88 | * promise settles when the pass that covers this request has finished. | |
| 89 | */ | |
| 90 | export function recombineOnMain(app: App, repo: Repo, run: Combine = combine): Promise<void> { | |
| 91 | const lane = lanes.get(repo.id); | |
| 92 | if (lane !== undefined) { | |
| 93 | lane.again = true; | |
| 94 | return lane.running; | |
| 95 | } | |
| 96 | const next: Lane = { again: false, running: Promise.resolve() }; | |
| 97 | next.running = (async () => { | |
| 98 | try { | |
| 99 | do { | |
| 100 | next.again = false; | |
| 101 | await exclusive(repo.id, () => pass(app, repo, run)); | |
| 102 | } while (next.again); | |
| 103 | } finally { | |
| 104 | lanes.delete(repo.id); | |
| 105 | } | |
| 106 | })(); | |
| 107 | lanes.set(repo.id, next); | |
| 108 | return next.running; | |
| 109 | } |
src/http/server.ts +15
| @@ -68,6 +68,7 @@ import { | ||
| 68 | 68 | getRepoById, |
| 69 | 69 | } from "../core/repos.ts"; |
| 70 | 70 | import { trackPullHead } from "../core/pulls.ts"; |
| 71 | import { recombineOnMain } from "../core/recombine.ts"; | |
| 71 | 72 | import { createRepo, repositoryLimitMessage, resolveOwner } from "../actions/repos.ts"; |
| 72 | 73 | import { sessionOf } from "../core/sessions.ts"; |
| 73 | 74 | import { scopeAllows, verifyToken } from "../core/tokens.ts"; |
| @@ -2031,6 +2032,20 @@ async function handleHook(app: App, request: Request): Promise<Response> { | ||
| 2031 | 2032 | ); |
| 2032 | 2033 | } |
| 2033 | 2034 | |
| 2035 | // A push to the default branch recombines the kept forks' pulls into it, | |
| 2036 | // off the hook's answer (`core/recombine.ts`, #9931). | |
| 2037 | if ( | |
| 2038 | input.updates.some( | |
| 2039 | (u) => u.ref === `refs/heads/${repo.defaultBranch}` && !ZERO_OID.test(u.newOid), | |
| 2040 | ) | |
| 2041 | ) | |
| 2042 | void recombineOnMain(app, repo).catch((error) => | |
| 2043 | app.log.warn("could not recombine pulls", { | |
| 2044 | repo: `${repo.ownerName}/${repo.name}`, | |
| 2045 | error: String(error), | |
| 2046 | }), | |
| 2047 | ); | |
| 2048 | ||
| 2034 | 2049 | // post-receive: the push is durable — record it and wake listeners. |
| 2035 | 2050 | app.bus.append({ |
| 2036 | 2051 | kind: "push", |
src/web/pages/pull.ts +6 −2
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * it needs. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { aheadCount } from "../../git/merge.ts"; | |
| 11 | 12 | import { pullRoster } from "../studio/pull_roster.ts"; |
| 12 | 13 | import { originCheckLead, originCheckOf } from "../../origins/pull-check.ts"; |
| 13 | 14 | import { askCheck } from "../../checks/again.ts"; |
| @@ -987,8 +988,11 @@ export async function pullFilesPage(ctx: Ctx): Promise<Response> { | ||
| 987 | 988 | ), |
| 988 | 989 | ) |
| 989 | 990 | : []; |
| 991 | // The author's own commits left: `status.ahead` counts the combined | |
| 992 | // branch, whose merge of main is not one of them (#9931). | |
| 993 | const own = pull.crossRepo ? await aheadCount(dir, status.baseOid, status.headOid) : status.ahead; | |
| 990 | 994 | const ends = |
| 991 | pull.crossRepo && mayLand && pull.state === "open" && pull.mergedAt === null && status.ahead > 1 | |
| 995 | pull.crossRepo && mayLand && pull.state === "open" && pull.mergedAt === null && own > 1 | |
| 992 | 996 | ? await lap("git", () => |
| 993 | 997 | keptRead(`slice-ends:${repo.id}:${status.baseOid}:${status.headOid}`, () => |
| 994 | 998 | sliceEnds(dir, status.baseOid, status.headOid), |
| @@ -1054,7 +1058,7 @@ export async function pullFilesPage(ctx: Ctx): Promise<Response> { | ||
| 1054 | 1058 | ${holdCard(ctx, repo, pull, { act: false })} |
| 1055 | 1059 | ${teamsCard(teamVerdicts(ctx.app.db, { ...pull, headOid: status.headOid }))} |
| 1056 | 1060 | ${mergeBox(ctx, repo, pull, status, mayMerge, by, check, isApp, tier, stale)} |
| 1057 | ${slicesCard(base, thread, slices, status.ahead, ends, pull.state === "open" && pull.mergedAt === null, mayLand)} | |
| 1061 | ${slicesCard(base, thread, slices, own, ends, pull.state === "open" && pull.mergedAt === null, mayLand)} | |
| 1058 | 1062 | ${await combinedCard(dir, pull, status.headOid, status.baseOid)} |
| 1059 | 1063 | <div class="split"> |
| 1060 | 1064 | <div> |
test/combined-fork.test.ts +1 −4
| @@ -181,14 +181,11 @@ describe("a conflicting fork pull is combined on our side (#9012)", () => { | ||
| 181 | 181 | expect((await git(["log", "-1", "--format=%P", retip], bare)).split(" ")).toEqual([bobNext, aliceMain]); |
| 182 | 182 | expect(await previewCommit(bare, getPull(app.db, repo.id, number)!)).toBe(retip); |
| 183 | 183 | |
| 184 | // Main moves on its own: the card says the preview is stale, and on what it was built. | |
| 184 | // Main moves on its own: it recombines by itself (test/combined-recombine.test.ts). | |
| 185 | 185 | await Bun.write(join(a, "four.txt"), "four\n"); |
| 186 | 186 | await git(["add", "."], a); |
| 187 | 187 | await git(["commit", "-m", "alice's four"], a); |
| 188 | 188 | await git(["push", "origin", "main"], a); |
| 189 | const stale = await (await call(`/alice/app/pulls/${number}`, alice)).text(); | |
| 190 | expect(stale).toContain(`Stale: main has moved since ${aliceMain.slice(0, 10)}`); | |
| 191 | expect(await screenText()).toContain("Combined with main · stale"); | |
| 192 | 189 | |
| 193 | 190 | // The merge lands the combined branch: his head is an ancestor of main. |
| 194 | 191 | const merged = await call(`/api/v1/repos/alice/app/pulls/${number}/merge`, alice, { strategy: "merge" }); |
test/combined-recombine.test.ts added +195
| @@ -0,0 +1,195 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { createApp, type App } from "../src/app.ts"; | |
| 6 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 7 | import { gitClientEnv } from "./helpers/git-client.ts"; | |
| 8 | import { proveAll } from "./helpers/verified.ts"; | |
| 9 | import { promote } from "./helpers/members.ts"; | |
| 10 | import { getRepo } from "../src/core/repos.ts"; | |
| 11 | import { repoDir } from "../src/git/repos.ts"; | |
| 12 | import { combinedWords } from "../src/core/combined_view.ts"; | |
| 13 | import { exclusive, recombineFailure, recombineOnMain, settled } from "../src/core/recombine.ts"; | |
| 14 | import { tokenFor } from "./helpers/tokens.ts"; | |
| 15 | ||
| 16 | const gitEnv = gitClientEnv({ | |
| 17 | GIT_AUTHOR_NAME: "Jay Moran", | |
| 18 | GIT_AUTHOR_EMAIL: "[email protected]", | |
| 19 | GIT_COMMITTER_NAME: "Jay Moran", | |
| 20 | GIT_COMMITTER_EMAIL: "[email protected]", | |
| 21 | }); | |
| 22 | ||
| 23 | describe("a push to main recombines every open kept-fork pull (#9931)", () => { | |
| 24 | let dataDir: string; | |
| 25 | let work: string; | |
| 26 | let app: App; | |
| 27 | let servers: Servers; | |
| 28 | let base: string; | |
| 29 | let alice = ""; | |
| 30 | let bob = ""; | |
| 31 | ||
| 32 | async function git(args: string[], cwd: string): Promise<string> { | |
| 33 | const p = Bun.spawn(["git", ...args], { cwd, env: gitEnv, stdout: "pipe", stderr: "pipe" }); | |
| 34 | const [out, err, code] = await Promise.all([ | |
| 35 | new Response(p.stdout).text(), | |
| 36 | new Response(p.stderr).text(), | |
| 37 | p.exited, | |
| 38 | ]); | |
| 39 | if (code !== 0) throw new Error(`git ${args.join(" ")}: ${err}`); | |
| 40 | return out.trim(); | |
| 41 | } | |
| 42 | const call = (path: string, token: string, body?: unknown): Promise<Response> => | |
| 43 | fetch(`${base}${path}`, { | |
| 44 | method: body === undefined ? "GET" : "POST", | |
| 45 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, | |
| 46 | ...(body === undefined ? {} : { body: JSON.stringify(body) }), | |
| 47 | }); | |
| 48 | const authed = (user: string, token: string) => | |
| 49 | `${base.replace("http://", `http://${user}:${token}@`)}`; | |
| 50 | ||
| 51 | async function signup(username: string): Promise<string> { | |
| 52 | await fetch(`${base}/signup`, { | |
| 53 | method: "POST", | |
| 54 | body: new URLSearchParams({ username, email: `${username}@f.dev`, password: "kept-fork-pass" }), | |
| 55 | redirect: "manual", | |
| 56 | }); | |
| 57 | return tokenFor(app, username, "admin", "k"); | |
| 58 | } | |
| 59 | ||
| 60 | beforeAll(async () => { | |
| 61 | dataDir = await mkdtemp(join(tmpdir(), "forge-cb-")); | |
| 62 | work = await mkdtemp(join(tmpdir(), "forge-cb-work-")); | |
| 63 | app = createApp({ dataDir, port: 0 }); | |
| 64 | servers = await serve(app); | |
| 65 | base = servers.url; | |
| 66 | alice = await signup("alice"); | |
| 67 | bob = await signup("bob"); | |
| 68 | proveAll(app); | |
| 69 | promote(app.db); | |
| 70 | await call("/api/v1/repos", alice, { name: "app", visibility: "public" }); | |
| 71 | await git(["clone", `${authed("alice", alice)}/alice/app.git`, "a"], work); | |
| 72 | await Bun.write(join(work, "a/one.txt"), "one\n"); | |
| 73 | await git(["add", "."], join(work, "a")); | |
| 74 | await git(["commit", "-m", "seed"], join(work, "a")); | |
| 75 | await git(["push", "origin", "main"], join(work, "a")); | |
| 76 | await call("/api/v1/repos/alice/app/forks", bob, {}); | |
| 77 | }); | |
| 78 | ||
| 79 | afterAll(async () => { | |
| 80 | servers.stop(); | |
| 81 | app.close(); | |
| 82 | await rm(dataDir, { recursive: true, force: true }); | |
| 83 | await rm(work, { recursive: true, force: true }); | |
| 84 | }); | |
| 85 | ||
| 86 | test("main alone moving rebuilds combined/N on the new tip, and the card clears", async () => { | |
| 87 | const a = join(work, "a"); | |
| 88 | const b = join(work, "b"); | |
| 89 | await git(["clone", `${authed("bob", bob)}/bob/app.git`, "b"], work); | |
| 90 | await Bun.write(join(b, "bob.txt"), "bob\n"); | |
| 91 | await git(["add", "."], b); | |
| 92 | await git(["commit", "-m", "bob's work"], b); | |
| 93 | await git(["push", "origin", "main"], b); | |
| 94 | ||
| 95 | await Bun.write(join(a, "two.txt"), "two\n"); | |
| 96 | await git(["add", "."], a); | |
| 97 | await git(["commit", "-m", "alice two"], a); | |
| 98 | await git(["push", "origin", "main"], a); | |
| 99 | ||
| 100 | const opened = await call("/api/v1/repos/alice/app/pulls", bob, { | |
| 101 | title: "work", base: "main", head: "main", headRepo: "bob/app", | |
| 102 | }); | |
| 103 | const { number } = (await opened.json()) as { number: number }; | |
| 104 | const bare = repoDir(app.reposRoot, "alice", "app"); | |
| 105 | const before = await git(["rev-parse", `refs/heads/combined/${number}`], bare); | |
| 106 | ||
| 107 | await Bun.write(join(a, "three.txt"), "three\n"); | |
| 108 | await git(["add", "."], a); | |
| 109 | await git(["commit", "-m", "alice three"], a); | |
| 110 | await git(["push", "origin", "main"], a); | |
| 111 | const main = await git(["rev-parse", "HEAD"], a); | |
| 112 | ||
| 113 | // The push's own pass, off the hook's answer: wait for the lane, never poll git. | |
| 114 | await settled(getRepo(app.db, "alice", "app")!.id); | |
| 115 | const after = await git(["rev-parse", `refs/heads/combined/${number}`], bare); | |
| 116 | expect(after).not.toBe(before); | |
| 117 | expect((await git(["log", "-1", "--format=%P", after], bare)).split(" ")).toContain(main); | |
| 118 | const page = await (await call(`/alice/app/pulls/${number}`, alice)).text(); | |
| 119 | expect(page).toContain(`combined/${number}`); | |
| 120 | expect(page).not.toContain("Stale: main has moved"); | |
| 121 | }, 30_000); | |
| 122 | ||
| 123 | test("a failed rebuild keeps its sentence for the card, and the next success clears it", async () => { | |
| 124 | const repo = getRepo(app.db, "alice", "app")!; | |
| 125 | const [pull] = [...app.db.query("SELECT i.number FROM issue i JOIN pull p ON p.issue_id = i.id WHERE i.repo_id = ?1 AND i.state = 'open'").all(repo.id)] as { number: number }[]; | |
| 126 | await recombineOnMain(app, repo, async () => undefined); | |
| 127 | expect(recombineFailure(repo.id, pull!.number)).toContain("could not merge main"); | |
| 128 | const view = { | |
| 129 | branch: "combined/1", oid: "a".repeat(40), builtOn: "b".repeat(40), stale: true, | |
| 130 | failure: recombineFailure(repo.id, pull!.number)!, files: [], | |
| 131 | }; | |
| 132 | expect(combinedWords(view, 1).notes[0]).toContain("Recombining failed: git could not merge main"); | |
| 133 | await recombineOnMain(app, repo); | |
| 134 | expect(recombineFailure(repo.id, pull!.number)).toBeUndefined(); | |
| 135 | }, 30_000); | |
| 136 | ||
| 137 | test("the card says a failed rebuild in plain words, never git's own", async () => { | |
| 138 | const repo = getRepo(app.db, "alice", "app")!; | |
| 139 | const bare = repoDir(app.reposRoot, "alice", "app"); | |
| 140 | const [pull] = [...app.db.query("SELECT i.number FROM issue i JOIN pull p ON p.issue_id = i.id WHERE i.repo_id = ?1 AND i.state = 'open'").all(repo.id)] as { number: number }[]; | |
| 141 | // Main moves without the hook, so the combined branch is behind it. | |
| 142 | const a = join(work, "a"); | |
| 143 | await Bun.write(join(a, "quiet.txt"), "quiet\n"); | |
| 144 | await git(["add", "."], a); | |
| 145 | await git(["commit", "-m", "quiet"], a); | |
| 146 | const tip = await git(["rev-parse", "HEAD"], a); | |
| 147 | await git(["fetch", join(work, "a"), "HEAD"], bare); | |
| 148 | await git(["update-ref", "refs/heads/main", tip], bare); | |
| 149 | await recombineOnMain(app, repo, async () => { | |
| 150 | throw new Error("fatal: /secret/path exploded"); | |
| 151 | }); | |
| 152 | const page = await (await call(`/alice/app/pulls/${pull!.number}`, alice)).text(); | |
| 153 | expect(page).toContain("Recombining failed: the rebuild did not finish."); | |
| 154 | expect(page).not.toContain("/secret/path"); | |
| 155 | await recombineOnMain(app, repo); | |
| 156 | const fine = await (await call(`/alice/app/pulls/${pull!.number}`, alice)).text(); | |
| 157 | expect(fine).not.toContain("Recombining failed"); | |
| 158 | }, 30_000); | |
| 159 | ||
| 160 | test("a landing waits for the pass in flight, and a pass for the landing", async () => { | |
| 161 | const repo = getRepo(app.db, "alice", "app")!; | |
| 162 | const log: string[] = []; | |
| 163 | let release = () => {}; | |
| 164 | const gate = new Promise<void>((r) => (release = r)); | |
| 165 | const held = recombineOnMain(app, repo, async () => { | |
| 166 | log.push("pass:start"); | |
| 167 | await gate; | |
| 168 | log.push("pass:end"); | |
| 169 | return undefined; | |
| 170 | }); | |
| 171 | while (!log.includes("pass:start")) await Bun.sleep(1); | |
| 172 | const landing = exclusive(repo.id, async () => void log.push("landing")); | |
| 173 | release(); | |
| 174 | await Promise.all([held, landing]); | |
| 175 | expect(log).toEqual(["pass:start", "pass:end", "landing"]); | |
| 176 | }); | |
| 177 | ||
| 178 | test("bursts coalesce: at most one pass at a time, one more after", async () => { | |
| 179 | const repo = getRepo(app.db, "alice", "app")!; | |
| 180 | let live = 0; | |
| 181 | let peak = 0; | |
| 182 | let calls = 0; | |
| 183 | const slow = async () => { | |
| 184 | calls++; | |
| 185 | live++; | |
| 186 | peak = Math.max(peak, live); | |
| 187 | await Bun.sleep(30); | |
| 188 | live--; | |
| 189 | return undefined; | |
| 190 | }; | |
| 191 | await Promise.all([1, 2, 3, 4].map(() => recombineOnMain(app, repo, slow))); | |
| 192 | expect(peak).toBe(1); | |
| 193 | expect(calls).toBe(2); // the pass that ran, and one more for the burst behind it | |
| 194 | }, 30_000); | |
| 195 | }); |
test/tui-crawlers.test.ts +2 −2
| @@ -11,7 +11,7 @@ import { createForgeApp, forgeCommands } from "../src/tui/forge.ts"; | ||
| 11 | 11 | import { crawlersScreen } from "../src/tui/screens/crawlers.ts"; |
| 12 | 12 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; |
| 13 | 13 | import { quietLogs } from "./helpers/globals.ts"; |
| 14 | import { settleApp } from "./helpers/tui.ts"; | |
| 14 | import { settleUntil } from "./helpers/tui.ts"; | |
| 15 | 15 | |
| 16 | 16 | quietLogs(); |
| 17 | 17 | |
| @@ -57,7 +57,7 @@ test("the screen shows the share, the budgets that refused and the agent refused | ||
| 57 | 57 | const { app: tui, session } = createForgeApp(app, identity(carol), { terminal: headlessTerminal(140, 20), exitHooks: false }); |
| 58 | 58 | tui.start(); |
| 59 | 59 | tui.ctx.push(crawlersScreen(session, tui.ctx)); |
| 60 | await settleApp(tui); | |
| 60 | await settleUntil(tui, "screenbot"); | |
| 61 | 61 | const text = tui.screen.toText(); |
| 62 | 62 | expect(text).toContain("Share of answering time"); |
| 63 | 63 | expect(text).toContain("address 2"); |
new above
offline