Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions .github/workflows/mobile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ on:
- 'config/scripts/mobile-test-change-scope*'
- 'config/scripts/pr-code-change-scope.mjs'
- 'config/scripts/mobile-recording-pin-checkout.test.mjs'
# Why main too: a behaviour-change branch legitimately pins its own last fenced commit, and that
# commit only stops being reachable when the branch squash-merges. The pull_request run cannot
# see that; this one is where the pin guard finds it.
# Why main too: a squash is where a spliced corpus lands, and where a behaviour-change branch's
# own pinned commit leaves main's history for its pull request's head ref, which the guard follows.
push:
branches:
- main
Expand Down Expand Up @@ -191,7 +190,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v6
with:
# The ancestry verdict is read straight off history. On a shallow checkout
# The reachability verdict is read straight off history. On a shallow checkout
# `git merge-base --is-ancestor` answers from grafted parents, so the guard refuses to
# answer at all rather than reporting a pass it has no evidence for -- and the pinned tree
# below has to be checkable out.
Expand All @@ -211,12 +210,11 @@ jobs:
# Seconds. No `--ref`, so the pin is judged against the same tree it was read out of. On a
# pull request that is the merge preview, which already carries main's repins; judging the
# branch head instead fails every branch cut before the day's repin, and its instruction would
# tell the author to pin their own head -- creating the break this guard exists to catch. A
# branch that pins its own commit passes here and fails on the push after the squash, which is
# where the pin actually leaves the history.
# tell the author to pin their own head. A branch that pins its own commit still passes on the
# push after the squash: the guard fetches `refs/pull/<n>/head`, which GitHub keeps for good.
- name: Check the recording pin is reachable
shell: bash
run: pnpm exec tsx scripts/rpc-recording-pin-guard.mts ancestry
run: pnpm exec tsx scripts/rpc-recording-pin-guard.mts reachable

# ~2 min locally for the record itself, so it is gated rather than run twice over. A pull
# request that moves none of the corpus, the manifest or the recorder cannot move this
Expand Down
2 changes: 1 addition & 1 deletion config/scripts/mobile-recording-pin-checkout.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ it('keeps full ancestry and credentials for lazy pinned-tree reads', () => {
expect(workflow.on.push.branches).toEqual(['main'])
expect(workflow.concurrency.group).toContain('github.sha')
expect(job.steps.find((step) => step.name === 'Check the recording pin is reachable').run).toBe(
'pnpm exec tsx scripts/rpc-recording-pin-guard.mts ancestry'
'pnpm exec tsx scripts/rpc-recording-pin-guard.mts reachable'
)
const reproduce = job.steps.find(
(step) => step.name === 'Reproduce the corpus from the pinned tree'
Expand Down
110 changes: 22 additions & 88 deletions mobile/scripts/rpc-recording-pin-guard.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* Guards the two claims `mobile/rpc-foundation/goldens` makes about its pin.
*
* `ancestry` — `baseline` names a commit in this history. A behaviour-change branch pins its own
* last fenced commit; that commit stops being reachable the moment the branch
* squash-merges, and nobody can run `--record` on main again until a hand-made repin
* lands. Ordinary product drift past a reachable pin is normal and is not a failure.
* `reachable` — `baseline` names a commit this repository keeps: one in this history, or one in
* the head of the pull request whose squash wrote it into the manifest. A
* behaviour-change branch pins its own last fenced commit, which the squash leaves out
* of main's history; `refs/pull/<n>/head` outlives the branch, so the pin stays
* checkable out. Ordinary product drift past a kept pin is normal and is not a failure.
* `reproduce` — the goldens on disk are what the recorder produces from the PINNED tree. The
* recording suites replay the corpus against the CURRENT tree on every run, which is
* the same claim only while the fenced tree still matches the pin.
Expand All @@ -17,6 +18,7 @@ import { join, resolve } from 'node:path'
import { runProcess } from '../../src/shared/child-process/run-process.ts'
import { RECORDING_DRIVERS } from '../src/test-support/rpc-recording/recording-drivers.ts'
import { readScenarios } from '../src/test-support/rpc-recording/scenario-input.ts'
import { checkPinReachable, PIN_MANIFEST } from './rpc-recording-pin-reachability.mts'

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 New module can skip replay The reproduction change gate still lists only the guard script, not this newly imported reachability module. If a future PR changes only the new module, reproduce --if-changed-since can skip the pinned-tree replay even though the code deciding whether reproduction can proceed has changed. Include the module in the gate's tracked paths.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting the pin logic into rpc-recording-pin-reachability.mts leaves the new file out of CORPUS_PROVENANCE_PATHS (lines 31-35), so a PR that changes only the reachability driver skips the reproduce gate even though it now decides which pin is fetched. Add mobile/scripts/rpc-recording-pin-reachability.mts to the list and to seedProvenance in the test so the "refuses to answer" check covers it.


/** The recorder is exempt from the fence, so a reproduction lays the candidate copy over the pin. */
const RECORDER_OVERLAY = 'mobile/src/test-support/rpc-recording'
Expand Down Expand Up @@ -67,90 +69,11 @@ export function assertReproductionSuitesExist(root: string): void {
}
}

export type PinAncestryFailure = 'shallow' | 'unreachable' | 'not-an-ancestor'
export type PinAncestryVerdict =
| { ok: true; baseline: string; ref: string }
| { ok: false; baseline: string; ref: string; failure: PinAncestryFailure; message: string }

async function git(cwd: string, args: readonly string[]) {
return await runProcess({ program: 'git', args: [...args], cwd })
}
function readPinnedBaseline(root: string): string {
return readScenarios(resolve(root, 'mobile/rpc-foundation/pilot-scenarios.json')).baseline
}
export function repinInstruction(baseline: string, ref: string, cause: string): string {
return [
`The RPC recording corpus is pinned to a commit that ${cause}.`,
'',
` baseline ${baseline} (mobile/rpc-foundation/pilot-scenarios.json)`,
` head ${ref}`,
'',
'Every golden under mobile/rpc-foundation/goldens claims it was recorded from that tree, and',
'`--record` refuses on any other tree, so the corpus cannot be refreshed until the pin names a',
'commit that is reachable from here. Repin and re-record, both in one commit:',
'',
` git switch -c repin-rpc-recording ${ref}`,
` # set "baseline" in mobile/rpc-foundation/pilot-scenarios.json to ${ref}`,
' ORCA_BACKGROUND_LAUNCH=1 RPC_FOUNDATION_RECORD=1 \\',
' pnpm --dir mobile exec tsx scripts/rpc-recording.mts --record',
'',
'Re-record everything: the repin rewrites the `baseline` header of every golden, so a partial',
'refresh leaves the corpus pinned to two different trees. See',
'mobile/src/test-support/rpc-recording/README.md, "Recording a behaviour change".'
].join('\n')
}
const SHALLOW_MESSAGE = [
'Cannot judge the recording pin: this is a shallow clone.',
'',
'`git merge-base --is-ancestor` answers from grafted history, so it would report a verdict this',
'guard has no evidence for. Check out with `fetch-depth: 0`.'
].join('\n')

export async function checkPinAncestry(
root: string,
baseline: string,
ref: string
): Promise<PinAncestryVerdict> {
const shallow = await git(root, ['rev-parse', '--is-shallow-repository'])
if (shallow.code !== 0) {
throw new Error(`Could not ask git whether the clone is shallow: ${shallow.stderr.trim()}`)
}
if (shallow.stdout.trim() !== 'false') {
return { ok: false, baseline, ref, failure: 'shallow', message: SHALLOW_MESSAGE }
}
const head = await git(root, ['rev-parse', '--verify', '--quiet', `${ref}^{commit}`])
if (head.code !== 0) {
throw new Error(`Cannot resolve ${ref} to a commit in this repository`)
}
// Resolved, because the instruction below is a command to paste: `HEAD` in it moves with whatever
// the reader has checked out by the time they read the log.
const resolved = head.stdout.trim()
const pinned = await git(root, ['rev-parse', '--verify', '--quiet', `${baseline}^{commit}`])
if (pinned.code !== 0) {
return {
ok: false,
baseline,
ref,
failure: 'unreachable',
message: repinInstruction(baseline, resolved, 'is not a commit in this repository at all')
}
}
const ancestor = await git(root, ['merge-base', '--is-ancestor', baseline, ref])
if (ancestor.code === 0) {
return { ok: true, baseline, ref }
}
// Why only 1: git reserves higher codes for real errors, and treating one as "not an ancestor"
// would turn a broken repository into a repin instruction nobody can act on.
if (ancestor.code !== 1) {
throw new Error(`git merge-base --is-ancestor failed: ${ancestor.stderr.trim()}`)
}
return {
ok: false,
baseline,
ref,
failure: 'not-an-ancestor',
message: repinInstruction(baseline, resolved, 'is not an ancestor of this commit')
}
return readScenarios(resolve(root, PIN_MANIFEST)).baseline
}

/** Whether anything a reproduction reads from the candidate tree moved since `since`. */
Expand Down Expand Up @@ -284,18 +207,22 @@ async function main(argv: readonly string[]): Promise<void> {
const check = argv[0]
const root = resolve(import.meta.dirname, '../..')
const baseline = readPinnedBaseline(root)
if (check === 'ancestry') {
if (check === 'reachable') {
const ref = argv.includes('--ref') ? argv[argv.indexOf('--ref') + 1] : 'HEAD'
if (!ref) {
throw new Error('--ref needs a commit')
}
const verdict = await checkPinAncestry(root, baseline, ref)
const verdict = await checkPinReachable(root, baseline, ref)
if (!verdict.ok) {
process.stderr.write(`${verdict.message}\n`)
process.exitCode = 1
return
}
process.stdout.write(`Recording pin ${baseline} is an ancestor of ${ref}.\n`)
process.stdout.write(
verdict.pullRequest === null
? `Recording pin ${baseline} is an ancestor of ${ref}.\n`
: `Recording pin ${baseline} is kept by refs/pull/${verdict.pullRequest}/head.\n`
)
return
}
if (check === 'reproduce') {
Expand All @@ -313,6 +240,13 @@ async function main(argv: readonly string[]): Promise<void> {
return
}
}
// Also fetches a pin that only its pull request's head still holds, so the checkout can find it.
const verdict = await checkPinReachable(root, baseline, 'HEAD')
if (!verdict.ok) {
process.stderr.write(`${verdict.message}\n`)
process.exitCode = 1
return
}
if (!(await reproduceFromPin(root, baseline))) {
process.stderr.write(`\n${REPRODUCTION_FAILURE}\n`)
process.exitCode = 1
Expand All @@ -322,7 +256,7 @@ async function main(argv: readonly string[]): Promise<void> {
return
}
throw new Error(
'Usage: rpc-recording-pin-guard.mts ancestry [--ref <commit>]\n | reproduce [--if-changed-since <commit>]'
'Usage: rpc-recording-pin-guard.mts reachable [--ref <commit>]\n | reproduce [--if-changed-since <commit>]'
)
}

Expand Down
Loading
Loading