|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// Merge-base guard for the Runtime Host compatibility epoch (#3313). |
| 4 | +// |
| 5 | +// Two branches that each bump the epoch write the same text to the same line, |
| 6 | +// so git's three-way merge resolves them without a conflict and two |
| 7 | +// incompatible protocols end up advertising one epoch. This check runs on the |
| 8 | +// PR merge result and fails when anything under the protocol directory changed |
| 9 | +// while the epoch still equals the merge base's — which is exactly the state a |
| 10 | +// silent same-number merge produces. |
| 11 | + |
| 12 | +import { execFileSync } from 'node:child_process'; |
| 13 | +import { dirname, resolve } from 'node:path'; |
| 14 | +import { fileURLToPath } from 'node:url'; |
| 15 | + |
| 16 | +const scriptPath = fileURLToPath(import.meta.url); |
| 17 | +const defaultRepoRoot = dirname(dirname(scriptPath)); |
| 18 | + |
| 19 | +export const EPOCH_FILE = 'packages/runtime-host/src/protocol/index.ts'; |
| 20 | +export const PROTOCOL_DIR = 'packages/runtime-host/src/protocol/'; |
| 21 | + |
| 22 | +const EPOCH_PATTERN = /^export const RUNTIME_HOST_COMPATIBILITY_EPOCH = (\d+) as const;$/gm; |
| 23 | + |
| 24 | +export function extractCompatibilityEpoch(source) { |
| 25 | + const matches = [...source.matchAll(EPOCH_PATTERN)]; |
| 26 | + if (matches.length !== 1) { |
| 27 | + throw new Error( |
| 28 | + `Expected exactly one RUNTIME_HOST_COMPATIBILITY_EPOCH declaration in ${EPOCH_FILE}, found ${matches.length}`, |
| 29 | + ); |
| 30 | + } |
| 31 | + return Number(matches[0][1]); |
| 32 | +} |
| 33 | + |
| 34 | +export function evaluateEpochCheck({ baseEpoch, headEpoch, changedProtocolFiles }) { |
| 35 | + if (headEpoch < baseEpoch) { |
| 36 | + return { |
| 37 | + ok: false, |
| 38 | + reason: |
| 39 | + `RUNTIME_HOST_COMPATIBILITY_EPOCH went backward: ${baseEpoch} -> ${headEpoch}. ` + |
| 40 | + `The epoch never decreases — a peer that saw ${baseEpoch} would admit an ` + |
| 41 | + `incompatible protocol. Bump it forward instead, even for a revert.`, |
| 42 | + }; |
| 43 | + } |
| 44 | + if (changedProtocolFiles.length > 0 && headEpoch === baseEpoch) { |
| 45 | + return { |
| 46 | + ok: false, |
| 47 | + reason: |
| 48 | + `Protocol files changed but RUNTIME_HOST_COMPATIBILITY_EPOCH is still ${baseEpoch}, ` + |
| 49 | + `the merge base's value. Same-number bumps on sibling branches merge without a git ` + |
| 50 | + `conflict (#3313), so every protocol change must land with an epoch the merge base ` + |
| 51 | + `has not seen: rebase onto current main and set the epoch past ${baseEpoch}. ` + |
| 52 | + `Changed files:\n${changedProtocolFiles.map((file) => ` ${file}`).join('\n')}`, |
| 53 | + }; |
| 54 | + } |
| 55 | + return { |
| 56 | + ok: true, |
| 57 | + reason: |
| 58 | + changedProtocolFiles.length > 0 |
| 59 | + ? `Protocol changed and the epoch moved: ${baseEpoch} -> ${headEpoch}.` |
| 60 | + : `No protocol changes against the merge base (epoch ${headEpoch}).`, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +function git(args, exec = execFileSync) { |
| 65 | + return exec('git', args, { cwd: defaultRepoRoot, encoding: 'utf8' }); |
| 66 | +} |
| 67 | + |
| 68 | +export function changedProtocolFilesBetween(base, head, exec = execFileSync) { |
| 69 | + return git(['diff', '--no-renames', '--name-only', base, head, '--', PROTOCOL_DIR], exec) |
| 70 | + .split('\n') |
| 71 | + .filter(Boolean); |
| 72 | +} |
| 73 | + |
| 74 | +export function epochAtRevision(revision, exec = execFileSync) { |
| 75 | + return extractCompatibilityEpoch(git(['show', `${revision}:${EPOCH_FILE}`], exec)); |
| 76 | +} |
| 77 | + |
| 78 | +function parseArgs(args) { |
| 79 | + const parsed = { base: undefined, head: 'HEAD' }; |
| 80 | + for (let index = 0; index < args.length; index += 1) { |
| 81 | + if (args[index] === '--base') parsed.base = args[++index]; |
| 82 | + else if (args[index] === '--head') parsed.head = args[++index]; |
| 83 | + else throw new Error(`Unknown argument: ${args[index]}`); |
| 84 | + } |
| 85 | + if (!parsed.base) throw new Error('Expected --base <rev> (and optionally --head <rev>)'); |
| 86 | + return parsed; |
| 87 | +} |
| 88 | + |
| 89 | +function main(args) { |
| 90 | + const { base, head } = parseArgs(args); |
| 91 | + const verdict = evaluateEpochCheck({ |
| 92 | + baseEpoch: epochAtRevision(base), |
| 93 | + headEpoch: epochAtRevision(head), |
| 94 | + changedProtocolFiles: changedProtocolFilesBetween(base, head), |
| 95 | + }); |
| 96 | + process.stderr.write(`Protocol epoch guard: ${verdict.reason}\n`); |
| 97 | + if (!verdict.ok) process.exitCode = 1; |
| 98 | +} |
| 99 | + |
| 100 | +if (process.argv[1] && resolve(process.argv[1]) === scriptPath) { |
| 101 | + try { |
| 102 | + main(process.argv.slice(2)); |
| 103 | + } catch (error) { |
| 104 | + process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); |
| 105 | + process.exitCode = 2; |
| 106 | + } |
| 107 | +} |
0 commit comments