From d5b5de830e4ae9197972eb7fe8d97c704f25746e Mon Sep 17 00:00:00 2001 From: Jakub Mucha Date: Fri, 30 Oct 2026 17:13:35 +0000 Subject: [PATCH 1/3] fix: build drift on the tip of main --- .github/workflows/drift.yml | 64 ++++++++--- docs/MAINTAINING.md | 34 +++--- docs/RELEASING.md | 2 + test/drift-workflow.test.ts | 210 ++++++++++++++++++++++++++++++++++-- 4 files changed, 275 insertions(+), 35 deletions(-) diff --git a/.github/workflows/drift.yml b/.github/workflows/drift.yml index 6a73430..18b849f 100644 --- a/.github/workflows/drift.yml +++ b/.github/workflows/drift.yml @@ -1,16 +1,16 @@ name: drift -# Pins the newest server, rebuilds index.db with it from the live wiki and compares its content -# with the committed index. When the content changed, it pushes drift/index with the rebuilt -# index and the next patch version, or the next minor when the schema grew, opens or updates its -# pull request as the tibia-sh App, turns on auto-merge and waits for the merge. The ruleset -# merges it once test and oldest-consumer pass, and that merge publishes through release.yml. -# When only the pin moved, the pull request carries package.json and pnpm-lock.yaml alone, at the -# same version, and its merge publishes nothing. Nothing here publishes by itself. A refresh that -# loses rows, or a table or column, as scripts/drift-guard.ts decides, is held instead: its pull -# request stays open with auto-merge off until a person merges or closes it. A run on main that -# fails or holds a refresh comments on the one alert issue, which alert.yml also writes for -# release.yml, assigned to the maintainer. +# Pins the newest server, rebuilds index.db with it from the live wiki on the tip of main and +# compares its content with the committed index. When the content changed, it pushes drift/index +# with the rebuilt index and the next patch version, or the next minor when the schema grew, opens +# or updates its pull request as the tibia-sh App, turns on auto-merge and waits for the merge. +# The ruleset merges it once test and oldest-consumer pass, and that merge publishes through +# release.yml. When only the pin moved, the pull request carries package.json and pnpm-lock.yaml +# alone, at the same version, and its merge publishes nothing. Nothing here publishes by itself. A +# refresh that loses rows, or a table or column, as scripts/drift-guard.ts decides, is held +# instead: its pull request stays open with auto-merge off until a person merges or closes it. A +# run on main that fails or holds a refresh comments on the one alert issue, which alert.yml also +# writes for release.yml, assigned to the maintainer. on: schedule: @@ -25,7 +25,9 @@ on: permissions: {} # Runs take turns, so two cannot push drift/index at once. One in progress is never -# cancelled. The group is not release.yml's, so a drift run never holds up a publish. +# cancelled. The group is not release.yml's, so a drift run never holds up a publish. A run +# that waited is still given main as it was at its event, and the run before it may have merged +# since, so the build job moves to the tip of main and the pr job to the build job's commit. concurrency: group: drift cancel-in-progress: false @@ -40,6 +42,9 @@ jobs: permissions: contents: read outputs: + # The commit the job built on, the tip of main when it started. A dispatch from another branch + # skips the move and hands on nothing, which is safe only while the pr job runs on main alone. + sha: ${{ steps.main.outputs.sha }} committed: ${{ steps.digests.outputs.committed }} rebuilt: ${{ steps.digests.outputs.rebuilt }} sha256: ${{ steps.digests.outputs.sha256 }} @@ -59,6 +64,19 @@ jobs: - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 with: persist-credentials: false + # The move to the tip of main, before the install and the scripts read anything, so a run that + # waited behind another one builds on what that run merged. The checkout kept no credentials, + # and the fetch needs none, since the repository is public. Three lines, not one joined with + # &&: under bash -e a failed fetch there would not end the step, which would then hand on the + # event's commit, and the pr job accepts that one. A dispatch from another branch builds that + # branch as it was. + - name: Move to the tip of main + id: main + if: ${{ github.ref == 'refs/heads/main' }} + run: | + git fetch --depth=1 origin main + git switch --detach FETCH_HEAD + echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" # No cache, restored or saved, but pnpm/setup's lockfile-verification record. A cache saved # after the generator ran would carry whatever that run left into later runs. This setup-node # caches by itself whenever package.json names a packageManager, so it is switched off by name. @@ -299,10 +317,30 @@ jobs: contents: read # the checkout. The App token does every write. pull-requests: read # the wait for the merge, which reads with github.token steps: - # No ref: the commit that triggered the run, which is the one the build job rebuilt. + # No ref: the checkout takes the event's commit, which main can have moved past while the run + # waited. The build job built index.db on the tip of main, so the job moves to that commit + # next, and commits on top of the main the refresh was built from. - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 with: persist-credentials: false + # Before any step reads the checkout. A job output is data, so it is checked whole before git + # reads it. The build job runs the generator, which could hand on any commit a fetch by SHA + # reaches, a pull request's head or an older commit of main among them, and this job commits + # on it with the App token. The build job moved to main's tip after the event, so before the + # switch the commit must be in main's history and descend from GITHUB_SHA, the event's commit, + # which the runner sets. The shallow checkout reaches back to the event's commit only, so the + # history is fetched whole, commits without their trees, and the switch fetches the trees it + # needs. + - name: Move to the build job's commit + id: move + env: + SHA: ${{ needs.build.outputs.sha }} + run: | + [[ $SHA =~ ^[0-9a-f]{40}$ ]] || { echo 'the build job handed on no full commit SHA' >&2; exit 1; } + git fetch --filter=tree:0 --unshallow origin +refs/heads/main:refs/remotes/origin/main + git merge-base --is-ancestor "$SHA" refs/remotes/origin/main || { echo 'not a commit of main' >&2; exit 1; } + git merge-base --is-ancestor "$GITHUB_SHA" "$SHA" || { echo "older than the event's commit" >&2; exit 1; } + git fetch origin "$SHA" && git switch --detach "$SHA" # Switched off by name here too. Left on, it would restore a pnpm store cache. - uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0 with: diff --git a/docs/MAINTAINING.md b/docs/MAINTAINING.md index 8a212ec..5437ce6 100644 --- a/docs/MAINTAINING.md +++ b/docs/MAINTAINING.md @@ -139,6 +139,11 @@ you run it by hand from the Actions tab, and when tibiawiki-mcp's release workfl `repository_dispatch` event `server-release`, with `{"version": ""}` as its payload, once npm accepted a server publish. +Runs take turns, and a run that waited is given `main` as it was at its event, which the run +before it can have moved with a merge. So the build job's first step after its checkout fetches +the tip of `main` and switches to it, before the install and every script, and hands that commit +to the `pr` job. A run on another branch builds that branch as it is. + Before the generator runs, its `Pin the server` step decides which server builds the index, with `scripts/server-pin.ts`: @@ -193,14 +198,18 @@ index added a table or a column, as [How the index is built](#how-the-index-is-b and `patch` otherwise. A changed `SCHEMA_VERSION` fails `pnpm test`, since the package major must equal it, so a new major ends the run red for a person rather than opening a pull request. -The `pr` job first checks each value the build job handed on whole: `changed` and `pin_moved` -are `true` or `false`, the level is `patch` or `minor`, and the server is `x.y.z`, above `main`'s -pin when it moved and `main`'s pin when it did not. The build job ran the generator, so the `pr` -job takes none of its files but the index. When the pin moved, it recomputes the pin itself with -`pnpm add --lockfile-only --ignore-scripts --ignore-pnpmfile`, which installs nothing and runs no -dependency code, requires that the recompute changed `package.json` and `pnpm-lock.yaml` alone, -and `package.json` only in the server's version, and requires both files to hash to what the -build job built with. Its `pnpm/setup` installs pnpm alone and restores no cache. +The `pr` job first moves to the commit the build job built on, before any step reads its +checkout. It switches only once the commit is a full SHA, is in `main`'s history and descends +from the commit of the run's event, so it never commits on a commit outside `main` or on one +older than the event's. It then checks each value the build job handed on whole: `changed` and +`pin_moved` are `true` or `false`, the level is `patch` or `minor`, and the server is `x.y.z`, +above `main`'s pin when it moved and `main`'s pin when it did not. The build job ran the +generator, so the `pr` job takes none of its files but the index. When the pin moved, it +recomputes the pin itself with `pnpm add --lockfile-only --ignore-scripts --ignore-pnpmfile`, +which installs nothing and runs no dependency code, requires that the recompute changed +`package.json` and `pnpm-lock.yaml` alone, and `package.json` only in the server's version, and +requires both files to hash to what the build job built with. Its `pnpm/setup` installs pnpm +alone and restores no cache. When the content changed, it sets `version` to the next patch or minor npm does not have, as the level says, pushes the rebuilt `index.db`, with the moved pin if there is one, to the @@ -237,10 +246,11 @@ refresh, comments on the issue `Automation needs a look`, or opens it assigned t [When automation needs a look](RELEASING.md#when-automation-needs-a-look) says what to do. - A tripped gate, a failing test, a guard that cannot read an index, an unreadable registry, a - dispatch that names no `x.y.z` version, a server npm does not serve within 45 minutes, a - build job output or a recomputed pin the `pr` job does not accept, or a `version` on `main` - that npm does not list yet each end the run red before anything is pushed. A pull request closed without merging, merged at another commit, left with auto-merge - off, or not merged within 60 minutes ends it red after the push. + fetch of `main` that fails, a dispatch that names no `x.y.z` version, a server npm does not + serve within 45 minutes, a build job commit, output or recomputed pin the `pr` job does not + accept, or a `version` on `main` that npm does not list yet each end the run red before + anything is pushed. A pull request closed without merging, merged at another commit, left with + auto-merge off, or not merged within 60 minutes ends it red after the push. - Each run that finds a change replaces `drift/index`, so an open pull request always carries the newest rebuild. A held pull request you leave open is not frozen: when a later run's guard finds no reason to hold, that run turns auto-merge on and it merges by itself. diff --git a/docs/RELEASING.md b/docs/RELEASING.md index 08909f9..dfe57b5 100644 --- a/docs/RELEASING.md +++ b/docs/RELEASING.md @@ -215,8 +215,10 @@ dropped. Each comment links the run and says what happened: |---|---| | `The build job did not succeed.` and its result | `cancelled` means the job hit its 90 minute bound, most often a slow wiki, or someone cancelled the run. Run drift again. For `failure`, open the run and read the red step. A tripped gate in `pnpm build-index`, a failing `pnpm test`, a wiki the build could not read, or `The guard could not compare the indexes.` each end it there, before anything is pushed. Fix the cause, or run drift again once the wiki is back. | | `The pr job did not succeed.` and its result | `cancelled` means the job hit its 75 minute bound or someone cancelled the run. Auto-merge stays on, so check the drift pull request. For `failure`, open the run and read the red step. `npm does not list X.Y.Z from package.json yet` means the release of the version on `main` is still running or failed, so follow [When a release run fails](#when-a-release-run-fails) and run drift again once npm has it. `was closed without merging` means someone closed the pull request. `merged , not ` means the pull request merged a commit other than the one this run pushed, so read `git log` on `main` for what landed. `is off. Merge it, or turn auto-merge on again, by hand` means someone or something turned auto-merge off during the wait: merge the pull request with Rebase and merge once its checks pass, or turn auto-merge on again with `gh pr merge --auto --rebase`, and the merge publishes. `failed 3 times in a row` means `gh` could not read the pull request, so check it by hand. `has not merged 3600 seconds after auto-merge was on` means its checks failed or are still running: auto-merge stays on, so it merges by itself once they pass, for example after you re-run a check that failed for a reason outside the repository. A red `Get a token of the tibia-sh App` step, or `Bad credentials` from `gh`, means the App's key or its installation no longer works, as [The tibia-sh App](#the-tibia-sh-app) describes. | +| `The build job did not succeed.`, with a red `Move to the tip of main` | The build job could not fetch the tip of `main`, or switch to it, so it built nothing and handed nothing on. Nothing was pushed. That is most often a passing GitHub outage, so run drift again. | | `The build job did not succeed.`, with `npm did not serve server X.Y.Z within 2700 seconds.` in `Pin the server` | The run was moving the pin to server X.Y.Z, and for 45 minutes npm either did not list that version's tarball or a `HEAD` on it did not answer 200. Nothing was built or pushed. Check the server's release run in tibiawiki-mcp and `npm view @tibia.sh/tibiawiki-mcp@X.Y.Z dist.tarball`. Once npm serves it, run drift again by hand. A scheduled run also picks it up when it is `latest`. | | `The build job did not succeed.`, with `The server-release dispatch asks for '…', not an x.y.z version.` in `Pin the server` | A `server-release` dispatch carried a payload version that is not `x.y.z`, and the step used none of it. Nothing was built or pushed. Fix the sender, tibiawiki-mcp's release workflow. Then run drift by hand, which moves the pin to npm's `latest`. | +| `The pr job did not succeed.`, with `the build job handed on no full commit SHA`, `not a commit of main` or `older than the event's commit` in `Move to the build job's commit` | The commit the build job handed on is not a full SHA, is not in `main`'s history, or is older than the commit of the run's event, so the job did not move to it. Nothing was pushed, and no token was minted. The build job moves to the tip of `main` after the event, so the commit it hands on is always a commit of `main` at or after the event's. A value like this means either a mistake in `drift.yml`, such as a misspelt output, or a build job something tampered with. Read the build job's log, and in particular `Move to the tip of main`. Run drift again only once you know which it was. | | `The pr job did not succeed.`, with a red `Check the build job's outputs` step | The build job handed on a value the `pr` job refuses: `changed`, `pin_moved` or `level` out of range, `Not a server version`, `moved the pin from A to B, which is not above it`, `kept the pin at B, but main pins A`, or `Neither the content nor the pin changed`. Nothing was pushed, and no token was minted. The build job runs the generator, so a value like this means either a mistake in `drift.yml`, such as a misspelt output, or a build job something tampered with. Read the build job's log, and in particular `Pin the server`. Run drift again only once you know which it was. | | `The pr job did not succeed.`, with `The recompute changed other files than package.json and pnpm-lock.yaml:` or `package.json changed in more than the pin of @tibia.sh/tibiawiki-mcp` in `Keep the recompute to the pin` | The `pr` job's own `pnpm add --lockfile-only` changed more than the server's pin. Nothing was pushed. That usually means a pnpm or `pnpm-workspace.yaml` change rewrote the manifests. Run the same command, `pnpm add -D --save-exact --lockfile-only --ignore-scripts --ignore-pnpmfile @tibia.sh/tibiawiki-mcp@X.Y.Z`, on a clean checkout of `main` and read `git diff`. When the extra change is right, land it in a reviewed pull request of its own, then run drift again. | | `The pr job did not succeed.`, with `The recomputed package.json hashes to …` or `The recomputed pnpm-lock.yaml hashes to …, but the build job built with …` in `Require the manifests the build job built with` | The manifests the `pr` job recomputed differ from the ones the build job installed and built `index.db` with, so the job refused to commit them. Nothing was pushed. A first-party release published between the two jobs can change the resolution, since `@tibia.sh/*` skips the cooldown, so run drift again. When it repeats, compare a full `pnpm add -D --save-exact` with the `--lockfile-only` one on a clean checkout. When those two agree, suspect the build job. | diff --git a/test/drift-workflow.test.ts b/test/drift-workflow.test.ts index cef74ba..9d6ae3c 100644 --- a/test/drift-workflow.test.ts +++ b/test/drift-workflow.test.ts @@ -1,7 +1,10 @@ -import { test } from 'node:test'; +import { after, test } from 'node:test'; import assert from 'node:assert/strict'; +import { execFileSync, spawnSync } from 'node:child_process'; import { createHash } from 'node:crypto'; -import { existsSync, readFileSync } from 'node:fs'; +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; import { APP_TOKEN_ACTION, appTokenInputs, type Call, code, keys, read, runScripts, runStep, scalar, sortedLines, stepBody, stepIf, stepOutputs, stepIndex, stepInputs, stepName, steps, stepScript, under } from './workflow.ts'; /** @@ -35,6 +38,24 @@ const RECOMPUTE_ADD = 'pnpm add -D --save-exact --lockfile-only --ignore-scripts */ const PR_NO_CACHE = '.pr-job-restores-no-cache'; +/** + * The moves to the tip of main, word for word. A run that waited behind another one is given main as it was at its + * event, so the build job fetches main's tip and builds on it, and the pr job moves to the commit the build job + * handed on once it is a commit of main no older than the event's. The build move is three lines: under bash -e a + * failed fetch in a `fetch && switch` line would not end the script, which would then hand on the event's commit, and + * the pr move accepts that one. A change to either has to change this test on purpose. + */ +const BUILD_MOVE_RUN = String.raw`git fetch --depth=1 origin main +git switch --detach FETCH_HEAD +echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" +`; +const PR_MOVE_RUN = String.raw`[[ $SHA =~ ^[0-9a-f]{40}$ ]] || { echo 'the build job handed on no full commit SHA' >&2; exit 1; } +git fetch --filter=tree:0 --unshallow origin +refs/heads/main:refs/remotes/origin/main +git merge-base --is-ancestor "$SHA" refs/remotes/origin/main || { echo 'not a commit of main' >&2; exit 1; } +git merge-base --is-ancestor "$GITHUB_SHA" "$SHA" || { echo "older than the event's commit" >&2; exit 1; } +git fetch origin "$SHA" && git switch --detach "$SHA" +`; + /** * The scripts of the pr job's steps that check what the build job handed on, word for word. The build job runs * dependency code and the generator, so its outputs are data: these steps check each one whole, recompute the pin @@ -389,13 +410,181 @@ test('no run script in the drift workflow interpolates an expression', () => { } }); -test('every checkout keeps no credentials and takes the commit that triggered the run', () => { - // The build job rebuilds that commit, and the pr job commits on top of the same one. - const checkouts = [...steps(buildJob()), ...steps(prJob())].filter((step) => /uses: *actions\/checkout@/.test(step)); - assert.equal(checkouts.length, 2, 'expected one checkout in each job'); - for (const step of checkouts) { - assert.match(step, /^ *persist-credentials: *false$/m, 'a checkout leaves the token in .git/config'); - assert.doesNotMatch(step, /^ *ref:/m, 'a checkout takes a ref instead of the triggering commit'); +test('every checkout keeps no credentials and takes no ref, and is the first step of its job', () => { + // Each checkout takes the event's commit, and the step right after it moves the job to the tip of main. + for (const [job, list] of [['build', steps(buildJob())], ['pr', steps(prJob())]] as const) { + const checkouts = list.filter((step) => /uses: *actions\/checkout@/.test(step)); + assert.equal(checkouts.length, 1, `expected one checkout in the ${job} job`); + assert.match(list[0]!, /uses: *actions\/checkout@/, `the ${job} job's first step is not its checkout`); + assert.match(checkouts[0]!, /^ *persist-credentials: *false$/m, `the ${job} job's checkout leaves the token in .git/config`); + assert.doesNotMatch(checkouts[0]!, /^ *ref:/m, `the ${job} job's checkout takes a ref instead of the event's commit`); + } +}); + +test('the build job moves to the tip of main right after its checkout, on main alone, and hands that commit on', () => { + // A run that waited behind another one is given main as it was at its event, which the run before it can have + // moved. The move comes before the install and every script, so all of them read the tip. A dispatch from another + // branch builds that branch, and hands on no commit, since the pr job runs on main alone. + const list = steps(buildJob()); + const move = stepIndex(list, 'main'); + assert.equal(move, 1, 'the move to the tip of main is not the first step after the checkout'); + assert.equal(scalar(stepBody(list[move]!), 'name'), 'Move to the tip of main'); + assert.equal(stepIf(list[move]!), "${{ github.ref == 'refs/heads/main' }}", 'the move does not run exactly on main'); + assert.equal(stepScript(workflow(), 'main'), BUILD_MOVE_RUN, 'the move runs another script'); + assert.equal(under(stepBody(list[move]!), 'env'), '', 'the move gets an env'); + assert.doesNotMatch(list[move]!, /\bsecrets\b|\bgithub\.token\b|\bsteps\.token\b|_TOKEN\b/, 'the move holds a token'); + assert.equal(scalar(under(buildJob(), 'outputs'), 'sha'), '${{ steps.main.outputs.sha }}', 'the build job hands on another commit'); +}); + +test("the pr job moves to the build job's commit right after its checkout, before anything reads the checkout", () => { + // The build job runs the generator, so the commit it hands on is data: the move checks it whole, and switches to it + // only once it is a commit of main that descends from the event's. Every step after the move reads that commit. + const list = steps(prJob()); + const move = stepIndex(list, 'move'); + assert.equal(move, 1, "the move to the build job's commit is not the first step after the checkout"); + assert.equal(scalar(stepBody(list[move]!), 'name'), "Move to the build job's commit"); + assert.equal(stepIf(list[move]!), undefined, 'the move runs only sometimes'); + assert.equal(stepScript(workflow(), 'move'), PR_MOVE_RUN, 'the move runs another script'); + assert.deepEqual(sortedLines(under(stepBody(list[move]!), 'env')), ['SHA: ${{ needs.build.outputs.sha }}'], + 'the move gets more than the commit the build job handed on'); + // The steps that read HEAD, the working tree or commit, by what they run. + const readers = list.flatMap((step, index) => (/\bgit\b|\bHEAD\b|package\.json/.test(step) && index !== move ? [index] : [])); + assert.ok(readers.length >= 3, 'the pr job has fewer steps that read the checkout than this test expects'); + for (const index of readers) { + assert.ok(index > move, `${stepName(list[index]!)} reads the checkout before the move`); + } +}); + +/** Runs git in `dir` with a fixed identity and none of the user's or the system's config. */ +const gitIn = (dir: string, args: string[]): string => + execFileSync('git', args, { + cwd: dir, + encoding: 'utf8', + env: { + PATH: process.env['PATH'] ?? '', + HOME: dir, + GIT_CONFIG_NOSYSTEM: '1', + GIT_AUTHOR_NAME: 'Drift Test', + GIT_AUTHOR_EMAIL: 'drift-test@example.com', + GIT_COMMITTER_NAME: 'Drift Test', + GIT_COMMITTER_EMAIL: 'drift-test@example.com', + }, + }).trim(); + +/** + * A repository served as origin, as GitHub serves this one: main at `root`, `event` and `tip`, in that order, and a + * side branch with one commit, `side`, on `event`. A run whose event was at `event` waited while `tip` landed. + */ +type MoveRepo = { origin: string; root: string; event: string; tip: string; side: string }; + +let moveDir: string | undefined; +let moveRepo: MoveRepo | undefined; +after(() => { + if (moveDir !== undefined) rmSync(moveDir, { recursive: true, force: true }); +}); + +/** A new directory under the one the move tests share, which is removed once the file's tests end. */ +const moveScratch = (name: string): string => { + moveDir ??= mkdtempSync(join(tmpdir(), 'tibiawiki-data-move-')); + return mkdtempSync(join(moveDir, `${name}-`)); +}; + +const moveFixture = (): MoveRepo => { + if (moveRepo !== undefined) return moveRepo; + const origin = moveScratch('origin'); + gitIn(origin, ['init', '--quiet', '--bare', '--initial-branch=main']); + // The pr move fetches with a filter, which a file:// origin refuses with a warning unless this allows it. + gitIn(origin, ['config', 'uploadpack.allowFilter', 'true']); + const src = moveScratch('src'); + gitIn(src, ['init', '--quiet', '--initial-branch=main']); + const commit = (name: string): string => { + writeFileSync(join(src, `${name}.txt`), `${name}\n`); + gitIn(src, ['add', '--all']); + gitIn(src, ['commit', '--quiet', '--message', name]); + return gitIn(src, ['rev-parse', 'HEAD']); + }; + const root = commit('root'); + const event = commit('event'); + const tip = commit('tip'); + gitIn(src, ['switch', '--quiet', '--detach', event]); + const side = commit('side'); + gitIn(src, ['push', '--quiet', `file://${origin}`, `${tip}:refs/heads/main`, `${side}:refs/heads/side`]); + moveRepo = { origin, root, event, tip, side }; + return moveRepo; +}; + +/** + * Runs one of the moves as the runner does, `bash -e` with GITHUB_SHA at the event's commit, in a checkout of that + * commit alone, as actions/checkout leaves it. `origin` serves the repository in place of the fixture's. git is real. + */ +const runMove = (script: string, { sha, origin }: { sha?: string; origin?: string } = {}) => { + const repo = moveFixture(); + const work = moveScratch('checkout'); + gitIn(work, ['init', '--quiet']); + gitIn(work, ['remote', 'add', 'origin', `file://${repo.origin}`]); + gitIn(work, ['fetch', '--quiet', '--no-tags', '--depth=1', 'origin', repo.event]); + gitIn(work, ['switch', '--quiet', '--detach', repo.event]); + if (origin !== undefined) gitIn(work, ['remote', 'set-url', 'origin', origin]); + const output = join(work, '.github-output'); + writeFileSync(output, ''); + const run = spawnSync('bash', ['-e', '-c', script], { + cwd: work, + encoding: 'utf8', + env: { + PATH: process.env['PATH'] ?? '', + HOME: work, + GIT_CONFIG_NOSYSTEM: '1', + GITHUB_SHA: repo.event, + GITHUB_OUTPUT: output, + ...(sha === undefined ? {} : { SHA: sha }), + }, + }); + return { + status: run.status, + stderr: run.stderr, + output: readFileSync(output, 'utf8'), + head: gitIn(work, ['rev-parse', 'HEAD']), + log: `${run.stdout}${run.stderr}`, + }; +}; + +test('the build move takes a checkout of the event commit to the tip of main, and hands the tip on', () => { + const { tip } = moveFixture(); + const run = runMove(stepScript(workflow(), 'main')); + assert.equal(run.status, 0, run.log); + assert.equal(run.head, tip, 'the build job did not move to the tip of main'); + assert.equal(run.output, `sha=${tip}\n`); +}); + +test('the build move ends red, and hands no commit on, when it cannot fetch main', () => { + // A failed fetch must not go on to hand on the event's commit, which the pr move would accept. + const { event } = moveFixture(); + const run = runMove(stepScript(workflow(), 'main'), { origin: `file://${moveScratch('missing')}/nowhere.git` }); + assert.notEqual(run.status, 0, `the move passed when the fetch failed\n${run.log}`); + assert.equal(run.output, '', 'the move handed a commit on when the fetch failed'); + assert.equal(run.head, event); +}); + +test("the pr move switches to a commit of main at or after the event's, and refuses any other", () => { + const repo = moveFixture(); + const script = stepScript(workflow(), 'move'); + for (const [what, sha] of [['the tip of main', repo.tip], ["the event's commit", repo.event]] as const) { + const run = runMove(script, { sha }); + assert.equal(run.status, 0, `the move refused ${what}\n${run.log}`); + assert.equal(run.head, sha, `the move did not switch to ${what}`); + } + const refused: Array<[string, string, string]> = [ + ['a commit outside main', repo.side, 'not a commit of main'], + ["a commit of main older than the event's", repo.root, "older than the event's commit"], + ['no commit SHA', 'nope', 'the build job handed on no full commit SHA'], + // A dispatch from another branch hands on no commit, and its pr job does not run. Were it to, it moves nowhere. + ['nothing', '', 'the build job handed on no full commit SHA'], + ]; + for (const [what, sha, message] of refused) { + const run = runMove(script, { sha }); + assert.equal(run.status, 1, `the move did not end red on ${what}\n${run.log}`); + assert.equal(run.head, repo.event, `the move left the event's commit on ${what}`); + assert.ok(run.stderr.split('\n').includes(message), `the move did not say ${message} on ${what}\n${run.log}`); } }); @@ -575,7 +764,7 @@ test('every output and step value the jobs pass along is one that is written', ( .map((match) => [match[1]!, { step: match[2]!, name: match[3]! }])); assert.equal(outputs.split('\n').filter((line) => line.trim() !== '').length, declared.size, 'the build job declares an output this test cannot read'); assert.deepEqual([...declared.keys()].sort(), - ['changed', 'committed', 'hold', 'level', 'package_json_sha256', 'pin_moved', 'pnpm_lock_sha256', 'reasons', 'rebuilt', 'server', 'sha256']); + ['changed', 'committed', 'hold', 'level', 'package_json_sha256', 'pin_moved', 'pnpm_lock_sha256', 'reasons', 'rebuilt', 'server', 'sha', 'sha256']); // The guard and the schema level run only when the content changed, and a skipped step's output is // empty, so hold falls back to false and level to patch. Nothing else may fall back. assert.equal(scalar(outputs, 'hold'), "${{ steps.guard.outputs.hold || 'false' }}", 'hold does not fall back to false'); @@ -1403,6 +1592,7 @@ test('the schema level fails, and hands nothing on, when the schemas cannot be c /** The pr job's steps, each named by its id, or by the action it uses when it has none. */ const PR_STEPS = [ 'actions/checkout', + 'move', 'actions/setup-node', 'check', 'pnpm/setup', From 2ce8bf3b115391abb407cf8b470dea0fb22850da Mon Sep 17 00:00:00 2001 From: Jakub Mucha Date: Fri, 30 Oct 2026 17:25:01 +0000 Subject: [PATCH 2/3] test: prove drift's ancestry check --- test/drift-workflow.test.ts | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/test/drift-workflow.test.ts b/test/drift-workflow.test.ts index 9d6ae3c..0f294c6 100644 --- a/test/drift-workflow.test.ts +++ b/test/drift-workflow.test.ts @@ -411,7 +411,9 @@ test('no run script in the drift workflow interpolates an expression', () => { }); test('every checkout keeps no credentials and takes no ref, and is the first step of its job', () => { - // Each checkout takes the event's commit, and the step right after it moves the job to the tip of main. + // Each checkout takes the event's commit. On main, the step right after it moves the build job to the tip of main, + // and the pr job to the commit the build job handed on, which main can have moved past since. A dispatch from + // another branch skips the build job's move, and runs no pr job. for (const [job, list] of [['build', steps(buildJob())], ['pr', steps(prJob())]] as const) { const checkouts = list.filter((step) => /uses: *actions\/checkout@/.test(step)); assert.equal(checkouts.length, 1, `expected one checkout in the ${job} job`); @@ -515,15 +517,23 @@ const moveFixture = (): MoveRepo => { /** * Runs one of the moves as the runner does, `bash -e` with GITHUB_SHA at the event's commit, in a checkout of that - * commit alone, as actions/checkout leaves it. `origin` serves the repository in place of the fixture's. git is real. + * commit alone, as actions/checkout leaves it. `fetch` is one more commit the checkout holds before the move, and + * `origin` serves the repository in place of the fixture's. git is real. */ -const runMove = (script: string, { sha, origin }: { sha?: string; origin?: string } = {}) => { +const runMove = (script: string, { sha, fetch, origin }: { sha?: string; fetch?: string; origin?: string } = {}) => { const repo = moveFixture(); const work = moveScratch('checkout'); gitIn(work, ['init', '--quiet']); gitIn(work, ['remote', 'add', 'origin', `file://${repo.origin}`]); gitIn(work, ['fetch', '--quiet', '--no-tags', '--depth=1', 'origin', repo.event]); gitIn(work, ['switch', '--quiet', '--detach', repo.event]); + if (fetch !== undefined) gitIn(work, ['fetch', '--quiet', '--no-tags', '--depth=1', 'origin', fetch]); + const has = (commit: string): boolean => + spawnSync('git', ['cat-file', '-e', `${commit}^{commit}`], { + cwd: work, + env: { PATH: process.env['PATH'] ?? '', HOME: work, GIT_CONFIG_NOSYSTEM: '1', GIT_NO_LAZY_FETCH: '1' }, + }).status === 0; + const held = fetch === undefined || has(fetch); if (origin !== undefined) gitIn(work, ['remote', 'set-url', 'origin', origin]); const output = join(work, '.github-output'); writeFileSync(output, ''); @@ -544,6 +554,8 @@ const runMove = (script: string, { sha, origin }: { sha?: string; origin?: strin stderr: run.stderr, output: readFileSync(output, 'utf8'), head: gitIn(work, ['rev-parse', 'HEAD']), + /** Whether the checkout held the commit `fetch` names before the move ran. */ + held, log: `${run.stdout}${run.stderr}`, }; }; @@ -573,8 +585,18 @@ test("the pr move switches to a commit of main at or after the event's, and refu assert.equal(run.status, 0, `the move refused ${what}\n${run.log}`); assert.equal(run.head, sha, `the move did not switch to ${what}`); } + // A commit outside main that the checkout holds, so the ancestry check itself refuses it, without an error from git. + const side = runMove(script, { sha: repo.side, fetch: repo.side }); + assert.ok(side.held, 'the checkout does not hold the side commit before the move'); + assert.equal(side.status, 1, `the move did not end red on a commit outside main it holds\n${side.log}`); + assert.equal(side.head, repo.event, "the move left the event's commit on a commit outside main it holds"); + assert.ok(side.stderr.split('\n').includes('not a commit of main'), `the move did not say not a commit of main\n${side.log}`); + assert.doesNotMatch(side.stderr, /fatal|error/i, `git failed on the side commit, so the ancestry check decided nothing\n${side.log}`); + // The move's filtered fetch makes origin a promisor, so git fetches a commit outside main it does not hold when the + // ancestry check asks for it, and one origin does not serve is refused as well. const refused: Array<[string, string, string]> = [ - ['a commit outside main', repo.side, 'not a commit of main'], + ['a commit outside main the checkout does not hold', repo.side, 'not a commit of main'], + ['a commit origin does not serve', 'f'.repeat(40), 'not a commit of main'], ["a commit of main older than the event's", repo.root, "older than the event's commit"], ['no commit SHA', 'nope', 'the build job handed on no full commit SHA'], // A dispatch from another branch hands on no commit, and its pr job does not run. Were it to, it moves nowhere. From 248fb1363eed3ddd0648e3e8b61af36ee7821b21 Mon Sep 17 00:00:00 2001 From: Jakub Mucha Date: Fri, 30 Oct 2026 17:32:09 +0000 Subject: [PATCH 3/3] fix: avoid git 2.56's crash in drift's move --- .github/workflows/drift.yml | 8 +++++--- test/drift-workflow.test.ts | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/drift.yml b/.github/workflows/drift.yml index 18b849f..bfd98c8 100644 --- a/.github/workflows/drift.yml +++ b/.github/workflows/drift.yml @@ -329,15 +329,17 @@ jobs: # on it with the App token. The build job moved to main's tip after the event, so before the # switch the commit must be in main's history and descend from GITHUB_SHA, the event's commit, # which the runner sets. The shallow checkout reaches back to the event's commit only, so the - # history is fetched whole, commits without their trees, and the switch fetches the trees it - # needs. + # history is fetched whole, commits and trees without their files, and the switch fetches the + # files it needs. The trees are small here, since index.db is a file. Not tree:0, which leaves + # the trees out too: git 2.56.0 crashes repacking that fetch once main has moved past the + # checkout's commit, which is the case this move is for. - name: Move to the build job's commit id: move env: SHA: ${{ needs.build.outputs.sha }} run: | [[ $SHA =~ ^[0-9a-f]{40}$ ]] || { echo 'the build job handed on no full commit SHA' >&2; exit 1; } - git fetch --filter=tree:0 --unshallow origin +refs/heads/main:refs/remotes/origin/main + git fetch --filter=blob:none --unshallow origin +refs/heads/main:refs/remotes/origin/main git merge-base --is-ancestor "$SHA" refs/remotes/origin/main || { echo 'not a commit of main' >&2; exit 1; } git merge-base --is-ancestor "$GITHUB_SHA" "$SHA" || { echo "older than the event's commit" >&2; exit 1; } git fetch origin "$SHA" && git switch --detach "$SHA" diff --git a/test/drift-workflow.test.ts b/test/drift-workflow.test.ts index 0f294c6..19207bf 100644 --- a/test/drift-workflow.test.ts +++ b/test/drift-workflow.test.ts @@ -43,14 +43,16 @@ const PR_NO_CACHE = '.pr-job-restores-no-cache'; * event, so the build job fetches main's tip and builds on it, and the pr job moves to the commit the build job * handed on once it is a commit of main no older than the event's. The build move is three lines: under bash -e a * failed fetch in a `fetch && switch` line would not end the script, which would then hand on the event's commit, and - * the pr move accepts that one. A change to either has to change this test on purpose. + * the pr move accepts that one. The pr move fetches main's history with blob:none, not tree:0: git 2.56.0 crashes + * repacking a tree:0 unshallow fetch once main has moved past the checkout's commit, and the fixture tests of the pr + * move, which run real git, catch that on the git CI has. A change to either has to change this test on purpose. */ const BUILD_MOVE_RUN = String.raw`git fetch --depth=1 origin main git switch --detach FETCH_HEAD echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" `; const PR_MOVE_RUN = String.raw`[[ $SHA =~ ^[0-9a-f]{40}$ ]] || { echo 'the build job handed on no full commit SHA' >&2; exit 1; } -git fetch --filter=tree:0 --unshallow origin +refs/heads/main:refs/remotes/origin/main +git fetch --filter=blob:none --unshallow origin +refs/heads/main:refs/remotes/origin/main git merge-base --is-ancestor "$SHA" refs/remotes/origin/main || { echo 'not a commit of main' >&2; exit 1; } git merge-base --is-ancestor "$GITHUB_SHA" "$SHA" || { echo "older than the event's commit" >&2; exit 1; } git fetch origin "$SHA" && git switch --detach "$SHA"