Repository navigation
fix(retention): enforce Node 22 operator runtime #1078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
qnbs
wants to merge
3
commits into
main
Choose a base branch
from
codex/retention-node22-guard-20261017
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { spawnSync } from 'node:child_process'; | ||
| import { readFileSync } from 'node:fs'; | ||
| import test from 'node:test'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { assertRuntime, main } from '../../../scripts/vercel-retention/cli.mjs'; | ||
| import { | ||
| makeDeps, | ||
| standardWorld, | ||
| withRetentionRuntime, | ||
| } from './support/vercelRetentionFixture.mjs'; | ||
|
|
||
| const cli = fileURLToPath(new URL('../../../scripts/vercel-retention/cli.mjs', import.meta.url)); | ||
|
|
||
| test('the operator contract matches .nvmrc without narrowing application engines or CI', () => { | ||
| assert.equal(readFileSync(new URL('../../../.nvmrc', import.meta.url), 'utf8').trim(), '22'); | ||
| const pkg = JSON.parse(readFileSync(new URL('../../../package.json', import.meta.url), 'utf8')); | ||
| assert.equal(pkg.engines.node, '>=22.19.0'); | ||
| for (const version of ['22.19.0', '22.19.1', '22.23.3', '22.100.0']) | ||
| assert.doesNotThrow(() => assertRuntime(version)); | ||
| for (const version of [ | ||
| null, | ||
| '', | ||
| 'unknown', | ||
| 'v22.23.3', | ||
| '22.18.99', | ||
| '22.19', | ||
| '22.19.0-rc.1', | ||
| '22.19.0\n', | ||
| '22.019.0', | ||
| '22.19.00', | ||
| '22.19.9007199254740992', | ||
| '22.9007199254740992.0', | ||
| '20.19.0', | ||
| '21.0.0', | ||
| '23.0.0', | ||
| '24.18.0', | ||
| ]) { | ||
| assert.throws(() => assertRuntime(version), { reason: 'runtime-mismatch' }); | ||
| } | ||
| }); | ||
|
|
||
| function guardedChild(argv, version) { | ||
| // QNBS-v3: test-only preload instruments every CLI/lock boundary and runner resolution; production has no runtime override or environment escape hatch. | ||
| const preload = ` | ||
| import cp from 'node:child_process'; | ||
| import fs from 'node:fs'; | ||
| import { registerHooks, syncBuiltinESMExports } from 'node:module'; | ||
| const calls = { child: 0, lock: 0, runner: 0, fetch: 0 }; | ||
| const deny = (kind) => () => { calls[kind]++; throw new Error('unexpected-side-effect'); }; | ||
| cp.execFileSync = deny('child'); | ||
| for (const key of ['writeFileSync', 'linkSync', 'renameSync', 'rmSync']) fs[key] = deny('lock'); | ||
| globalThis.fetch = deny('fetch'); | ||
| syncBuiltinESMExports(); | ||
| registerHooks({ resolve(specifier, context, next) { | ||
| if (specifier === './apply.mjs') { calls.runner++; throw new Error('runner-loaded'); } | ||
| return next(specifier, context); | ||
| }}); | ||
| ${version === undefined ? '' : `Object.defineProperty(process.versions, 'node', {value: ${JSON.stringify(version)}});`} | ||
| process.on('exit', () => process.stderr.write('GUARD_CALLS ' + JSON.stringify(calls) + '\\n')); | ||
| `; | ||
| const result = spawnSync( | ||
| process.execPath, | ||
| [`--import=data:text/javascript,${encodeURIComponent(preload)}`, cli, ...argv], | ||
| { encoding: 'utf8', timeout: 15000 }, | ||
| ); | ||
| assert.ifError(result.error); | ||
| assert.equal(result.status, 1); | ||
| assert.equal(result.stdout, ''); | ||
| const counts = /GUARD_CALLS (\{[^\n]+\})/.exec(result.stderr); | ||
| assert.ok(counts, result.stderr); | ||
| assert.deepEqual(JSON.parse(counts[1]), { child: 0, lock: 0, runner: 0, fetch: 0 }); | ||
| return result.stderr; | ||
| } | ||
|
|
||
| for (const mode of ['dry-run', 'reconcile']) { | ||
| test(`${mode} rejects unsupported and malformed runtimes before all external boundaries`, () => { | ||
| for (const version of [ | ||
| '24.18.0', | ||
| '20.19.0', | ||
| '21.9.0', | ||
| '23.0.0', | ||
| '22.18.9', | ||
| 'private-invalid-value', | ||
| ]) { | ||
| const stderr = guardedChild([mode], version); | ||
| assert.match(stderr, /RETENTION_STOP: runtime-mismatch/); | ||
| assert.ok(!stderr.includes(version)); | ||
| } | ||
| }); | ||
|
|
||
| test(`${mode} accepts supported runtime with mocked provider and unchanged source checks`, async () => { | ||
| const world = standardWorld(); | ||
| const lines = []; | ||
| await withRetentionRuntime(() => | ||
| main([mode], makeDeps(world), { log: (line) => lines.push(line) }), | ||
| ); | ||
| const summary = JSON.parse( | ||
| lines.find((line) => line.startsWith('RETENTION_SUMMARY ')).slice(18), | ||
| ); | ||
| assert.equal(summary.stop, null); | ||
| assert.equal(summary.writeAttempts, mode === 'dry-run' ? 0 : world.writes.length); | ||
| assert.equal(world.released, mode === 'dry-run' ? 0 : 1); | ||
| assert.ok(world.calls.some((call) => call.startsWith('vercel api'))); | ||
| if (mode === 'reconcile') { | ||
| const expectedDeleted = ['dpl_prev12', 'dpl_prev11', 'dpl_prev2', 'dpl_prev1']; | ||
| const deploymentWrites = world.writes | ||
| .filter((write) => write.startsWith('remove dpl_')) | ||
| .map((write) => write.split(' ')[1]); | ||
| assert.ok(summary.writeAttempts > 0); | ||
| assert.deepEqual(summary.deleted, expectedDeleted); | ||
| assert.deepEqual(deploymentWrites, expectedDeleted); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| test('the actual matrix runtime refuses before tools, locks and runner imports', () => { | ||
| let expectedStop = 'invalid-arguments'; | ||
| try { | ||
| assertRuntime(); | ||
| } catch (error) { | ||
| expectedStop = error.reason; | ||
| } | ||
| const stderr = guardedChild(['--execute']); | ||
| assert.ok(stderr.includes(`RETENTION_STOP: ${expectedStop}`)); | ||
| }); | ||
|
|
||
| test('supported runtime still rejects a modified source before provider calls or locks', async () => { | ||
| const world = standardWorld(); | ||
| world.differs = true; | ||
| await assert.rejects( | ||
| withRetentionRuntime(() => main(['reconcile'], makeDeps(world), { log() {} })), | ||
| { reason: 'runner-source-differs-from-main' }, | ||
| ); | ||
| assert.ok(world.calls.every((call) => call.startsWith('git '))); | ||
| assert.deepEqual(world.writes, []); | ||
| assert.equal(world.released, 0); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.