Public: anyone can read this; the crew can change it.
The runtime's approval gate reads a pull's combined tip without a blocking git call #17898
Merged 22h 39m ago, live in production
scout/forge:scout/17883-the-runtime-s-approval-gate-reads-a-pull → main 0 commits fork 1 thread · 1 unresolved
13 files
changed +232
−40
- src/actions/apps.ts
- src/apps/runtime.ts
- src/apps/runtime_events.ts
- src/apps/runtime_sleep.ts
- src/core/following.ts
- src/core/pulls.ts
- src/core/recombine.ts
- src/git/combined.ts
- test/combined-gate.test.ts
- test/combined-git.test.ts
- test/git-spawn-failure.test.ts
- test/spawn-pipes.test.ts
- test/spawn-sites.test.ts
src/actions/apps.ts +2 −2
| @@ -76,7 +76,7 @@ import type { Run } from "../core/runs.ts"; | ||
| 76 | 76 | import { type User } from "../core/users.ts"; |
| 77 | 77 | import { resolveOid } from "../git/merge.ts"; |
| 78 | 78 | import { exactRefOid } from "../git/branches.ts"; |
| 79 | import { previewCommit } from "../git/combined.ts"; | |
| 79 | import { carriedOf, previewCommit } from "../git/combined.ts"; | |
| 80 | 80 | import { repoDir } from "../git/repos.ts"; |
| 81 | 81 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 82 | 82 | import { |
| @@ -891,7 +891,7 @@ async function commitFor( | ||
| 891 | 891 | if (!pull || pull.state !== "open") return undefined; |
| 892 | 892 | const at = await pullDir(app.db, app.reposRoot, repo, pull); |
| 893 | 893 | // A fork's pull combined on our side previews the combined build (#9012). |
| 894 | return previewCommit(at, pull); | |
| 894 | return previewCommit(at, pull, carriedOf(app.db)); | |
| 895 | 895 | } |
| 896 | 896 | |
| 897 | 897 | export { previewEnv }; |
src/apps/runtime.ts +8
| @@ -39,6 +39,7 @@ import { Gate } from "../git/env.ts"; | ||
| 39 | 39 | import { resolveOid } from "../git/merge.ts"; |
| 40 | 40 | import { exactRefOid } from "../git/branches.ts"; |
| 41 | 41 | import { repoDir } from "../git/repos.ts"; |
| 42 | import { carriedOf, type Carried } from "../git/combined.ts"; | |
| 42 | 43 | import { STALE_AFTER_MS } from "../runs/lease.ts"; |
| 43 | 44 | import { appendRunLog } from "../runs/log.ts"; |
| 44 | 45 | import { primaryDomain, primaryHosts } from "../core/domains.ts"; |
| @@ -75,12 +76,19 @@ export class AppsRuntime { | ||
| 75 | 76 | stopped = false; |
| 76 | 77 | /** Counts build previews as they start, so the oldest is the lowest. */ |
| 77 | 78 | buildCount = 0; |
| 79 | /** | |
| 80 | * Heads seen carried by their combined tips, this forge's: the approval gate reads it with no | |
| 81 | * git call (#17883). `combine` fills it as it writes a tip; at boot, `reconcileApp` reads each | |
| 82 | * open pull's head (`pullHead`) before it asks `mayRun`. | |
| 83 | */ | |
| 84 | readonly carried: Carried; | |
| 78 | 85 | |
| 79 | 86 | constructor( |
| 80 | 87 | readonly app: App, |
| 81 | 88 | readonly options: RuntimeOptions, |
| 82 | 89 | ) { |
| 83 | 90 | this.gate = options.gate ?? new Gate(options.concurrency ?? 2); |
| 91 | this.carried = carriedOf(app.db); | |
| 84 | 92 | registerRuntime(app, this); |
| 85 | 93 | // Closing the forge closes the database next: end every open awake |
| 86 | 94 | // interval now, while it can be written, and write nothing after. |
src/apps/runtime_events.ts +1 −1
| @@ -89,7 +89,7 @@ export function pruneDirs(rt: AppsRuntime, known: Set<string>): void { | ||
| 89 | 89 | export async function pullHead(rt: AppsRuntime, repo: Repo, pull: Pull): Promise<string | undefined> { |
| 90 | 90 | const dir = await pullDir(rt.app.db, rt.app.reposRoot, repo, pull); |
| 91 | 91 | // A fork's pull combined on our side previews the combined build (#9012). |
| 92 | return previewCommit(dir, pull); | |
| 92 | return previewCommit(dir, pull, rt.carried); | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // ---- events ---- |
src/apps/runtime_sleep.ts +2 −2
| @@ -112,11 +112,11 @@ export function mayRun( | ||
| 112 | 112 | number === undefined ? undefined : getPull(rt.app.db, repo.id, number); |
| 113 | 113 | // A closed or merged pull has no preview: nothing starts, wakes or redeploys one (#16594). |
| 114 | 114 | if (pull !== undefined && pull.state !== "open") return false; |
| 115 | // A combined preview runs under the head it carries, read from git (#9012). | |
| 115 | // A combined preview runs under the head it carries (#9012), read without a git process (#17883). | |
| 116 | 116 | const head = |
| 117 | 117 | pull === undefined |
| 118 | 118 | ? commitOid |
| 119 | : headUnder(repoDir(rt.app.reposRoot, repo.ownerName, repo.name), pull, commitOid); | |
| 119 | : headUnder(repoDir(rt.app.reposRoot, repo.ownerName, repo.name), pull, commitOid, rt.carried); | |
| 120 | 120 | // An outside agent's branch (`contrib/…`) is previewed only once a person who can write has |
| 121 | 121 | // approved the pull (`origins/guest.ts`); its check, in a cell, is what runs before that. |
| 122 | 122 | if (pull !== undefined && previewMayRun(rt.app.db, repo, pull, head)) return true; |
src/core/following.ts +2 −2
| @@ -42,7 +42,7 @@ import { describeLink, syncLink, type Reconcile } from "../git/sync_link.ts"; | ||
| 42 | 42 | import { readTipOrAbsent, readTipSync } from "../git/tip.ts"; |
| 43 | 43 | import { readTips } from "../git/tips.ts"; |
| 44 | 44 | import { repoDir } from "../git/repos.ts"; |
| 45 | import { combine } from "../git/combined.ts"; | |
| 45 | import { carriedOf, combine } from "../git/combined.ts"; | |
| 46 | 46 | import { fromKeptFork } from "./landing_kind.ts"; |
| 47 | 47 | import { forkFollowMode, forkFollowModes, lastTookAt, FORK_DEFAULT_MODE, type ForkFollowMode } from "./fork_follow.ts"; |
| 48 | 48 | import { linkWord, sayLink } from "../flows/link_word.ts"; |
| @@ -562,7 +562,7 @@ export async function refreshPullHead( | ||
| 562 | 562 | if (!apart && fromKeptFork(forge.db, pull)) { |
| 563 | 563 | const baseTip = await resolveOid(baseDir, `refs/heads/${pull.baseBranch}`); |
| 564 | 564 | if (baseTip !== undefined) |
| 565 | await combine(baseDir, { number: pull.number, baseOid: baseTip, headOid: tip }); | |
| 565 | await combine(baseDir, { number: pull.number, baseOid: baseTip, headOid: tip }, carriedOf(forge.db)); | |
| 566 | 566 | } |
| 567 | 567 | return tip; |
| 568 | 568 | } |
src/core/pulls.ts +3 −3
| @@ -16,7 +16,7 @@ import { OVER_CAP } from "../git/env.ts"; | ||
| 16 | 16 | import { diffRange, type Diff, type DiffLaps } from "../git/diff.ts"; |
| 17 | 17 | import { isSafeRef } from "../git/guard.ts"; |
| 18 | 18 | import { refTip } from "../git/read.ts"; |
| 19 | import { combine, combinedTip } from "../git/combined.ts"; | |
| 19 | import { carriedOf, combine, combinedTip } from "../git/combined.ts"; | |
| 20 | 20 | import { fromKeptFork } from "./landing_kind.ts"; |
| 21 | 21 | import { readTips } from "../git/tips.ts"; |
| 22 | 22 | import { |
| @@ -356,7 +356,7 @@ export async function createPull( | ||
| 356 | 356 | const pull = join(db, issue.value)!; |
| 357 | 357 | // A kept fork's pull is combined with main on our side (#9012). |
| 358 | 358 | if (at === dir && fromKeptFork(db, pull)) |
| 359 | await combine(dir, { number: pull.number, baseOid, headOid }); | |
| 359 | await combine(dir, { number: pull.number, baseOid, headOid }, carriedOf(db)); | |
| 360 | 360 | return ok(pull); |
| 361 | 361 | } |
| 362 | 362 | |
| @@ -1341,7 +1341,7 @@ export async function mergePull( | ||
| 1341 | 1341 | // the author's head is its ancestor, so it lands too (#9012). |
| 1342 | 1342 | const combined = |
| 1343 | 1343 | input.sealedFrom === undefined && input.strategy === "merge" && fromKeptFork(db, pull) |
| 1344 | ? await combine(dir, { number: pull.number, baseOid: status.baseOid, headOid: status.headOid }) | |
| 1344 | ? await combine(dir, { number: pull.number, baseOid: status.baseOid, headOid: status.headOid }, carriedOf(db)) | |
| 1345 | 1345 | : undefined; |
| 1346 | 1346 | // Overlapping edits are never settled by picking a side: the pull is held. |
| 1347 | 1347 | if (combined?.held === true) |
src/core/recombine.ts +2 −2
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { REPO_KEYED } from "../kernel/repo_keyed.ts"; |
| 10 | 10 | import type { Database } from "bun:sqlite"; |
| 11 | import { combine, readFailure, setFailure } from "../git/combined.ts"; | |
| 11 | import { carriedOf, combine, readFailure, setFailure } from "../git/combined.ts"; | |
| 12 | 12 | import { repoDir } from "../git/repos.ts"; |
| 13 | 13 | import { resolveOid } from "../git/merge.ts"; |
| 14 | 14 | import type { Log } from "../kernel/log.ts"; |
| @@ -55,7 +55,7 @@ async function pass(forge: RecombineForge, repoId: string): Promise<void> { | ||
| 55 | 55 | try { |
| 56 | 56 | const headOid = await resolveOid(dir, `refs/pull/${pull.number}/head`); |
| 57 | 57 | const made = |
| 58 | headOid === undefined ? undefined : await combine(dir, { number: pull.number, baseOid, headOid }); | |
| 58 | headOid === undefined ? undefined : await combine(dir, { number: pull.number, baseOid, headOid }, carriedOf(forge.db)); | |
| 59 | 59 | if (made === undefined) { |
| 60 | 60 | forge.log.warn("recombine skipped a pull", { repo: repoId, pull: pull.number }); |
| 61 | 61 | // A refusal has written its own sentence (naming the files); |
src/git/combined.ts +60 −17
| @@ -13,9 +13,11 @@ | ||
| 13 | 13 | * still carries the author's head. |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | import { gitEnv, runGit, runGitCapture } from "./env.ts"; | |
| 16 | import type { Database } from "bun:sqlite"; | |
| 17 | import { runGit, runGitCapture } from "./env.ts"; | |
| 17 | 18 | import { isSafeOid, isSafeTreePath } from "./guard.ts"; |
| 18 | 19 | import { refTip } from "./read.ts"; |
| 20 | import { readTipSync } from "./tip.ts"; | |
| 19 | 21 | import { mergeBase, resolveOid } from "./merge.ts"; |
| 20 | 22 | import { unionTree } from "./union.ts"; |
| 21 | 23 | |
| @@ -42,6 +44,42 @@ const IDENTITY = { | ||
| 42 | 44 | const isAncestor = async (dir: string, a: string, b: string): Promise<boolean> => |
| 43 | 45 | a === b || (await mergeBase(dir, a, b)) === a; |
| 44 | 46 | |
| 47 | /** | |
| 48 | * Heads seen carried by a combined tip, as `head tip` (#17883), for one forge: kept on the two | |
| 49 | * oids, never on time. A commit's ancestry never changes, and a force-push moves the ref or the | |
| 50 | * head to a new oid, so a new key. Filled wherever the forge writes or reads a combined tip | |
| 51 | * (`combine`, `combinedTip`), read by `headUnder` without a git process. | |
| 52 | */ | |
| 53 | export class Carried { | |
| 54 | private readonly pairs = new Set<string>(); | |
| 55 | /** Heads whose tip is being read now: one reading at a time, however often the gate asks. */ | |
| 56 | private readonly reading = new Set<string>(); | |
| 57 | constructor(private readonly max = 4096) {} | |
| 58 | note(head: string, tip: string): void { | |
| 59 | if (this.pairs.size >= this.max) this.pairs.clear(); | |
| 60 | this.pairs.add(`${head} ${tip}`); | |
| 61 | } | |
| 62 | has(head: string, tip: string): boolean { | |
| 63 | return this.pairs.has(`${head} ${tip}`); | |
| 64 | } | |
| 65 | /** Start `read` for this key unless one is already under way. */ | |
| 66 | readOnce(key: string, read: () => Promise<unknown>): void { | |
| 67 | if (this.reading.has(key)) return; | |
| 68 | this.reading.add(key); | |
| 69 | void read() | |
| 70 | .catch(() => undefined) | |
| 71 | .finally(() => this.reading.delete(key)); | |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | /** Each forge's `Carried`, by its database: two forges in one process never share one. */ | |
| 76 | const carriedBy = new WeakMap<Database, Carried>(); | |
| 77 | export function carriedOf(db: Database): Carried { | |
| 78 | let kept = carriedBy.get(db); | |
| 79 | if (kept === undefined) carriedBy.set(db, (kept = new Carried())); | |
| 80 | return kept; | |
| 81 | } | |
| 82 | ||
| 45 | 83 | /** |
| 46 | 84 | * Whether the branch has a commit beyond the head and main that the forge's own combining did not |
| 47 | 85 | * make: main's own commits ride in through the combining merge and are no writer's (#14780). |
| @@ -137,8 +175,12 @@ export function heldSentence(number: number, files: readonly Kept[]): string { | ||
| 137 | 175 | export async function combine( |
| 138 | 176 | dir: string, |
| 139 | 177 | input: { readonly number: number; readonly baseOid: string; readonly headOid: string }, |
| 178 | carried?: Carried, | |
| 140 | 179 | ): Promise<Combined | undefined> { |
| 141 | 180 | const made = await build(dir, input); |
| 181 | // Every tip a combining answers carries the head (it is the head, the branch that carries it, | |
| 182 | // or a merge with it as first parent): the approval gate may run it under the head. | |
| 183 | if (made !== undefined && made.oid !== input.headOid) carried?.note(input.headOid, made.oid); | |
| 142 | 184 | // A rebuild that worked ends any earlier failure's sentence, whoever asked for it; |
| 143 | 185 | // one that held the pull says why in its place. |
| 144 | 186 | if (made !== undefined) |
| @@ -211,10 +253,12 @@ export async function combinedTip( | ||
| 211 | 253 | dir: string, |
| 212 | 254 | number: number, |
| 213 | 255 | headOid: string, |
| 256 | carried?: Carried, | |
| 214 | 257 | ): Promise<string | undefined> { |
| 215 | 258 | if (!Number.isInteger(number) || number < 1 || !isSafeOid(headOid)) return undefined; |
| 216 | 259 | const tip = await refTip(dir, combinedRef(number)); |
| 217 | 260 | if (tip === undefined || !(await isAncestor(dir, headOid, tip))) return undefined; |
| 261 | carried?.note(headOid, tip); | |
| 218 | 262 | return tip; |
| 219 | 263 | } |
| 220 | 264 | |
| @@ -235,28 +279,25 @@ export async function readCombined( | ||
| 235 | 279 | } |
| 236 | 280 | |
| 237 | 281 | /** |
| 238 | * The head a preview commit runs under, read from git as it stands: the | |
| 239 | * pull's head when `commit` is its combined tip and still carries it, the | |
| 240 | * commit itself otherwise. Synchronous, for the runtime's approval gate. | |
| 282 | * The head a preview commit runs under: the pull's head when `commit` is its combined tip and | |
| 283 | * carries it, the commit itself otherwise. For the runtime's approval gate, which is synchronous: | |
| 284 | * it starts no git process (#17883). The tip is read from the ref's own files on every call; that | |
| 285 | * it carries the head is what `combine` or `combinedTip` saw in this forge. Unseen, the commit | |
| 286 | * answers for itself, so its own approval is asked, and a reading starts for the next ask. | |
| 241 | 287 | */ |
| 242 | 288 | export function headUnder( |
| 243 | 289 | dir: string, |
| 244 | 290 | pull: { readonly crossRepo: boolean; readonly number: number; readonly headOid: string }, |
| 245 | 291 | commit: string, |
| 292 | carried: Carried, | |
| 246 | 293 | ): string { |
| 247 | 294 | if (!pull.crossRepo || commit === pull.headOid || !isSafeOid(commit) || !isSafeOid(pull.headOid)) |
| 248 | 295 | return commit; |
| 249 | const git = (args: string[]) => { | |
| 250 | try { | |
| 251 | return Bun.spawnSync(["git", ...args], { cwd: dir, env: gitEnv(), stdout: "pipe", stderr: "ignore" }); | |
| 252 | } catch { | |
| 253 | // git cannot start (the directory is gone): the commit stands as given. | |
| 254 | return { exitCode: 1, stdout: Buffer.alloc(0) }; | |
| 255 | } | |
| 256 | }; | |
| 257 | const tip = git(["rev-parse", "--verify", "--quiet", combinedRef(pull.number)]); | |
| 258 | if (tip.exitCode !== 0 || tip.stdout.toString().trim() !== commit) return commit; | |
| 259 | return git(["merge-base", "--is-ancestor", pull.headOid, commit]).exitCode === 0 ? pull.headOid : commit; | |
| 296 | if (!Number.isInteger(pull.number) || pull.number < 1) return commit; | |
| 297 | if (readTipSync(dir, combinedRef(pull.number)) !== commit) return commit; | |
| 298 | if (carried.has(pull.headOid, commit)) return pull.headOid; | |
| 299 | carried.readOnce(`${dir} ${pull.number} ${pull.headOid}`, () => combinedTip(dir, pull.number, pull.headOid, carried)); | |
| 300 | return commit; | |
| 260 | 301 | } |
| 261 | 302 | |
| 262 | 303 | /** |
| @@ -266,19 +307,21 @@ export function headUnder( | ||
| 266 | 307 | export async function previewCommit( |
| 267 | 308 | dir: string, |
| 268 | 309 | pull: { readonly crossRepo: boolean; readonly number: number; readonly headBranch: string; readonly headOid: string }, |
| 310 | carried?: Carried, | |
| 269 | 311 | ): Promise<string | undefined> { |
| 270 | return (await previewHead(dir, pull))?.commit; | |
| 312 | return (await previewHead(dir, pull, carried))?.commit; | |
| 271 | 313 | } |
| 272 | 314 | |
| 273 | 315 | /** The head a preview commit stands for: a combined tip answers as the head it carries. */ |
| 274 | 316 | export async function previewHead( |
| 275 | 317 | dir: string, |
| 276 | 318 | pull: { readonly crossRepo: boolean; readonly number: number; readonly headBranch: string; readonly headOid: string }, |
| 319 | carried?: Carried, | |
| 277 | 320 | ): Promise<{ commit: string; head: string } | undefined> { |
| 278 | 321 | const ref = pull.crossRepo ? `refs/pull/${pull.number}/head` : `refs/heads/${pull.headBranch}`; |
| 279 | 322 | const head = (await resolveOid(dir, ref)) ?? (pull.headOid || undefined); |
| 280 | 323 | if (head === undefined) return undefined; |
| 281 | const combined = pull.crossRepo ? await combinedTip(dir, pull.number, head) : undefined; | |
| 324 | const combined = pull.crossRepo ? await combinedTip(dir, pull.number, head, carried) : undefined; | |
| 282 | 325 | return { commit: combined ?? head, head }; |
| 283 | 326 | } |
| 284 | 327 |
test/combined-gate.test.ts added +108
| @@ -0,0 +1,108 @@ | ||
| 1 | /** | |
| 2 | * The runtime's approval gate and a fork's combined tip (#17883): what the forge has read or | |
| 3 | * written of the combined branch is what lets `mayRun` answer under the pull's head, with no git | |
| 4 | * process on the serving thread; a tip it never saw carry the head answers for itself. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, 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 { newApp } from "../src/actions/apps.ts"; | |
| 11 | import { openPull, submitReview } from "../src/actions/pulls.ts"; | |
| 12 | import { commitToBranch, forkRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { previewEnv } from "../src/apps/data.ts"; | |
| 15 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 16 | import type { AppRecord } from "../src/core/apps.ts"; | |
| 17 | import { getPull, type Pull } from "../src/core/pulls.ts"; | |
| 18 | import { recombine } from "../src/core/recombine.ts"; | |
| 19 | import type { Repo } from "../src/core/repos.ts"; | |
| 20 | import { createUser, type User } from "../src/core/users.ts"; | |
| 21 | import { repoDir } from "../src/git/repos.ts"; | |
| 22 | import { gitClientEnv } from "./helpers/git-client.ts"; | |
| 23 | import { quietLogs } from "./helpers/globals.ts"; | |
| 24 | import { promote } from "./helpers/members.ts"; | |
| 25 | ||
| 26 | quietLogs(); | |
| 27 | ||
| 28 | let dataDir = ""; | |
| 29 | let app: App; | |
| 30 | let runtime: AppsRuntime; | |
| 31 | let alice: User; | |
| 32 | let bob: User; | |
| 33 | let repo: Repo; | |
| 34 | let record: AppRecord; | |
| 35 | let pull: Pull; | |
| 36 | let bare = ""; | |
| 37 | ||
| 38 | const env = gitClientEnv({ | |
| 39 | GIT_AUTHOR_NAME: "Jay Moran", | |
| 40 | GIT_AUTHOR_EMAIL: "[email protected]", | |
| 41 | GIT_COMMITTER_NAME: "Jay Moran", | |
| 42 | GIT_COMMITTER_EMAIL: "[email protected]", | |
| 43 | }); | |
| 44 | function git(...args: string[]): string { | |
| 45 | const p = Bun.spawnSync(["git", ...args], { cwd: bare, env, stdout: "pipe", stderr: "pipe" }); | |
| 46 | if (p.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${p.stderr.toString()}`); | |
| 47 | return p.stdout.toString().trim(); | |
| 48 | } | |
| 49 | function ok<T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T { | |
| 50 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 51 | return r.value; | |
| 52 | } | |
| 53 | const gate = (commit: string): boolean => runtime.mayRun(record, repo, previewEnv(pull.number), commit); | |
| 54 | ||
| 55 | beforeAll(async () => { | |
| 56 | dataDir = await mkdtemp(join(tmpdir(), "forge-combined-gate-")); | |
| 57 | app = createApp({ dataDir, port: 0 }); | |
| 58 | runtime = new AppsRuntime(app, { domain: "apps.test", scheme: "http", publicPort: 0, forgeUrl: "http://forge.test", concurrency: 1 }); | |
| 59 | alice = ok(await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true })); | |
| 60 | bob = ok(await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" })); | |
| 61 | // Bob is a member who cannot write the repository: his pull waits for a writer's approval. | |
| 62 | promote(app.db, bob); | |
| 63 | const made = ok(await newApp(app, alice, { name: "gate", visibility: "public" })); | |
| 64 | repo = made.repo; | |
| 65 | record = made.app; | |
| 66 | ok(await commitToBranch(app, alice, repo, { message: "seed", files: [{ path: "a.txt", content: "a\n" }] })); | |
| 67 | const fork = ok(await forkRepo(app, bob, repo, "gate-bob")); | |
| 68 | ok(await commitToBranch(app, bob, fork, { branch: "work", base: "main", message: "bob's b", files: [{ path: "b.txt", content: "b\n" }] })); | |
| 69 | const opened = ok(await openPull(app, bob, repo, { title: "Bob's b", baseBranch: "main", headBranch: "work", headRepo: fork })); | |
| 70 | bare = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 71 | pull = getPull(app.db, repo.id, opened.number)!; | |
| 72 | // A writer approves bob's head: his code may run at it, and at nothing else. | |
| 73 | ok(await submitReview(app, alice, repo, pull, { state: "approved", body: "" })); | |
| 74 | pull = getPull(app.db, repo.id, opened.number)!; | |
| 75 | }); | |
| 76 | ||
| 77 | afterAll(async () => { | |
| 78 | await runtime.stop(); | |
| 79 | app.close(); | |
| 80 | await rm(dataDir, { recursive: true, force: true }); | |
| 81 | }); | |
| 82 | ||
| 83 | test("a combined tip the runtime read through pullHead runs under the approved head", async () => { | |
| 84 | expect(gate(pull.headOid)).toBe(true); | |
| 85 | // Written past the forge (a hand on the branch): nothing has read that it carries the head yet. | |
| 86 | const tip = git("commit-tree", `${pull.headOid}^{tree}`, "-p", pull.headOid, "-m", "by hand"); | |
| 87 | git("update-ref", `refs/heads/combined/${pull.number}`, tip); | |
| 88 | expect(gate(tip)).toBe(false); | |
| 89 | // The runtime reads the pull's head, as every door does before it deploys, prepares or boots one. | |
| 90 | expect(await runtime.pullHead(repo, pull)).toBe(tip); | |
| 91 | expect(gate(tip)).toBe(true); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a tip that does not carry the head never runs under it", async () => { | |
| 95 | const stray = git("commit-tree", `${pull.headOid}^{tree}`, "-m", "not the head's"); | |
| 96 | git("update-ref", `refs/heads/combined/${pull.number}`, stray); | |
| 97 | expect(await runtime.pullHead(repo, pull)).toBe(pull.headOid); | |
| 98 | expect(gate(stray)).toBe(false); | |
| 99 | }); | |
| 100 | ||
| 101 | test("a tip the forge's own combining wrote runs under the head with no read first", async () => { | |
| 102 | git("update-ref", "-d", `refs/heads/combined/${pull.number}`); | |
| 103 | ok(await commitToBranch(app, alice, repo, { message: "main moves", files: [{ path: "c.txt", content: "c\n" }] })); | |
| 104 | await recombine({ db: app.db, reposRoot: app.reposRoot, log: app.log }, repo.id); | |
| 105 | const tip = git("rev-parse", `refs/heads/combined/${pull.number}`); | |
| 106 | expect(tip).not.toBe(pull.headOid); | |
| 107 | expect(gate(tip)).toBe(true); | |
| 108 | }); |
test/combined-git.test.ts +42 −6
| @@ -3,7 +3,7 @@ import { afterAll, beforeAll, expect, test } from "bun:test"; | ||
| 3 | 3 | import { mkdtemp, rm } from "node:fs/promises"; |
| 4 | 4 | import { tmpdir } from "node:os"; |
| 5 | 5 | import { join } from "node:path"; |
| 6 | import { combine, headUnder, parseCombined, readCombined, readFailure } from "../src/git/combined.ts"; | |
| 6 | import { Carried, combine, combinedTip, headUnder, parseCombined, readCombined, readFailure } from "../src/git/combined.ts"; | |
| 7 | 7 | import { runGit } from "../src/git/env.ts"; |
| 8 | 8 | import { gitClientEnv } from "./helpers/git-client.ts"; |
| 9 | 9 | |
| @@ -105,19 +105,55 @@ test("a writer's commit on the combined branch survives main moving on", async ( | ||
| 105 | 105 | expect(git("show", `${again!.oid}:two.txt`)).toBe("two"); |
| 106 | 106 | }); |
| 107 | 107 | |
| 108 | test("a combined preview runs under the head it carries, and nothing else does", () => { | |
| 108 | test("a combined preview runs under the head it carries, and nothing else does", async () => { | |
| 109 | 109 | const head = git("rev-parse", "adds"); |
| 110 | 110 | const tip = git("rev-parse", "combined/9"); |
| 111 | 111 | const pull = { crossRepo: true, number: 9, headOid: git("rev-parse", "adds") }; |
| 112 | expect(headUnder(gitDir(), pull, tip)).toBe(head); | |
| 112 | const carried = new Carried(); | |
| 113 | expect(await combinedTip(gitDir(), 9, head, carried)).toBe(tip); | |
| 114 | expect(headUnder(gitDir(), pull, tip, carried)).toBe(head); | |
| 113 | 115 | // Not the combined tip: it answers for itself, so its own approval is asked. |
| 114 | 116 | const main = git("rev-parse", "main"); |
| 115 | expect(headUnder(gitDir(), pull, main)).toBe(main); | |
| 117 | expect(headUnder(gitDir(), pull, main, carried)).toBe(main); | |
| 116 | 118 | // A pull whose head the branch no longer carries: the tip answers for itself. |
| 117 | 119 | const elsewhere = git("commit-tree", "4b825dc642cb6eb9a060e54bf8d69288fbee4904", "-m", "elsewhere"); |
| 118 | expect(headUnder(gitDir(), { ...pull, headOid: elsewhere }, tip)).toBe(tip); | |
| 120 | expect(headUnder(gitDir(), { ...pull, headOid: elsewhere }, tip, carried)).toBe(tip); | |
| 119 | 121 | // A branch pull is never combined. |
| 120 | expect(headUnder(gitDir(), { ...pull, crossRepo: false }, tip)).toBe(tip); | |
| 122 | expect(headUnder(gitDir(), { ...pull, crossRepo: false }, tip, carried)).toBe(tip); | |
| 123 | }); | |
| 124 | ||
| 125 | test("the approval gate reads the combined tip without a synchronous git process (#17883)", async () => { | |
| 126 | const head = git("rev-parse", "adds"); | |
| 127 | const pull = { crossRepo: true, number: 10, headOid: head }; | |
| 128 | const carried = new Carried(); | |
| 129 | // A combined branch nobody here has read yet: written by hand, it carries the head. | |
| 130 | const tip = git("commit-tree", `${head}^{tree}`, "-p", head, "-m", "by hand"); | |
| 131 | git("update-ref", "refs/heads/combined/10", tip); | |
| 132 | const real = Bun.spawnSync; | |
| 133 | let spawns = 0; | |
| 134 | const count = ((...a: Parameters<typeof Bun.spawnSync>) => { | |
| 135 | spawns++; | |
| 136 | return real(...a); | |
| 137 | }) as typeof Bun.spawnSync; | |
| 138 | Bun.spawnSync = count; | |
| 139 | let unseen = "", seen = ""; | |
| 140 | try { | |
| 141 | // Unseen, the tip answers for itself: its own approval is asked, never a guess. | |
| 142 | unseen = headUnder(gitDir(), pull, tip, carried); | |
| 143 | Bun.spawnSync = real; | |
| 144 | expect(await combinedTip(gitDir(), 10, head, carried)).toBe(tip); | |
| 145 | Bun.spawnSync = count; | |
| 146 | seen = headUnder(gitDir(), pull, tip, carried); | |
| 147 | } finally { | |
| 148 | Bun.spawnSync = real; | |
| 149 | } | |
| 150 | expect([unseen, seen, spawns]).toEqual([tip, head, 0]); | |
| 151 | // Force-pushed elsewhere: the tip that was seen is no longer the branch, so it answers for | |
| 152 | // itself. | |
| 153 | const moved = git("commit-tree", `${head}^{tree}`, "-p", head, "-m", "again"); | |
| 154 | git("update-ref", "refs/heads/combined/10", moved); | |
| 155 | expect(headUnder(gitDir(), pull, tip, carried)).toBe(tip); | |
| 156 | expect(headUnder(gitDir(), pull, moved, carried)).toBe(moved); | |
| 121 | 157 | }); |
| 122 | 158 | |
| 123 | 159 | test("trailers that are not ours are not read as facts", () => { |
test/git-spawn-failure.test.ts +2 −2
| @@ -8,7 +8,7 @@ import { createApp, type App } from "../src/app.ts"; | ||
| 8 | 8 | import { serve, type Servers } from "../src/http/server.ts"; |
| 9 | 9 | import { runGit, runGitCapture, runGitRaw } from "../src/git/env.ts"; |
| 10 | 10 | import { diff, runLocal } from "../src/git/local.ts"; |
| 11 | import { headUnder } from "../src/git/combined.ts"; | |
| 11 | import { Carried, headUnder } from "../src/git/combined.ts"; | |
| 12 | 12 | import { blobHead, blobStream, readerFor, closeReader } from "../src/git/read.ts"; |
| 13 | 13 | import { mediaGate } from "../src/git/env.ts"; |
| 14 | 14 | import { repoDir } from "../src/git/repos.ts"; |
| @@ -62,7 +62,7 @@ test("a work-screen diff of a gone directory is an Err with the sentence", async | ||
| 62 | 62 | |
| 63 | 63 | test("headUnder of a gone directory leaves the commit as given", () => { |
| 64 | 64 | const pull = { crossRepo: true, number: 1, headOid: OTHER }; |
| 65 | expect(headUnder(gone, pull, OID)).toBe(OID); | |
| 65 | expect(headUnder(gone, pull, OID, new Carried())).toBe(OID); | |
| 66 | 66 | }); |
| 67 | 67 | |
| 68 | 68 | let dataDir = ""; |
test/spawn-pipes.test.ts −1
| @@ -35,7 +35,6 @@ const PIPING: Record<string, { sites: string[]; reason: string }> = { | ||
| 35 | 35 | "src/crew/workdir.ts": { sites: ["stderr"], reason: "stderr read to its end alongside the exit; stdout ignored" }, |
| 36 | 36 | "src/data/query.ts": { sites: ["stdout"], reason: "stdout read to its end for the one JSON answer, under a timer that kills the child; stderr ignored" }, |
| 37 | 37 | "src/git/env.ts": { sites: ["stdout+stderr", "stdout+stderr"], reason: "stdout and stderr both read to the end, finally clears the timer" }, |
| 38 | "src/git/combined.ts": { sites: ["stdout"], reason: "spawnSync buffers rev-parse stdout; stderr ignored" }, | |
| 39 | 38 | "src/git/local.ts": { sites: ["stdout+stderr", "stdout+stderr"], reason: "stdout and stderr both read to the end" }, |
| 40 | 39 | "src/git/own_refs.ts": { sites: ["stdout", "stdout", "stdout", "stdout", "stdout"], reason: "each git stdout is read to the end (four as text alongside exited, cat-file and show-index under a kill timer, the rev-list walk chunk by chunk); stderr ignored; a timer kills a walk that hangs" }, |
| 41 | 40 | "src/git/read.ts": { sites: ["stdout", "stdout", "stdout", "stdout", "stdout"], reason: "cat-file stdout is read by the long-lived reader, by blobStream until its window ends or the reader hangs up, and by blobHead until its bytes are in (then killed), by blobRange until its range is in (then killed), and by blobTail until the blob ends, keeping a ring of its last bytes; stderr ignored" }, |
test/spawn-sites.test.ts −2
| @@ -63,8 +63,6 @@ const SITES: Record<string, string> = { | ||
| 63 | 63 | "runBounded is called only with the fixed `bun install --frozen-lockfile --ignore-scripts` argv", |
| 64 | 64 | "src/data/query.ts": |
| 65 | 65 | "bun on the forge's own query_child.ts, a fixed argv with an empty environment; the SQL a person typed travels on stdin, never argv", |
| 66 | "src/git/combined.ts": | |
| 67 | "git plumbing with oids checked by isSafeOid before they reach argv", | |
| 68 | 66 | "src/git/env.ts": |
| 69 | 67 | "the one git runner: argv to the git binary, no shell; callers pass refs and paths validated at their boundary and after `--` where git accepts it", |
| 70 | 68 | "src/git/local.ts": |
new above
offline