Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-11713]): validate pregate arguments before checks (#11719)

[._.]/079f07cf7ab6
diff --git a/scripts/pregate.js b/scripts/pregate.js
index 39b25ab87f..9f0153ee13 100644
--- a/scripts/pregate.js
+++ b/scripts/pregate.js
@@ -199,18 +199,51 @@ function defaultBaseRef() {
return resolved || 'origin/main';
}
-/** Parse argv into the flags documented in the header. */
+export const USAGE = `Usage: node scripts/pregate.js [--base <ref>] [--full] [--skip-lint] [--plan-only]
+
+ --base <ref> compare against <ref> instead of the remote default branch
+ --full run the complete suite when the plan demands it
+ --skip-lint skip linting in both client and server workspaces
+ --plan-only print the plan and stages without running them
+ -h, --help display this usage`;
+
+/** Parse argv before doing any Git inspection or starting a child process. */
export function parseArgs(argv) {
- const baseIndex = argv.indexOf('--base');
- return {
- base: baseIndex === -1 ? null : argv[baseIndex + 1],
- full: argv.includes('--full'),
- skipLint: argv.includes('--skip-lint'),
- planOnly: argv.includes('--plan-only'),
- };
+ const options = { base: null, full: false, skipLint: false, planOnly: false, help: false };
+ for (let index = 0; index < argv.length; index += 1) {
+ const flag = argv[index];
+ switch (flag) {
+ case '--base': {
+ const value = argv[index + 1];
+ if (!value || value.startsWith('-')) throw new Error('--base requires a value.');
+ if (options.base === null) options.base = value;
+ index += 1;
+ break;
+ }
+ case '--full': options.full = true; break;
+ case '--skip-lint': options.skipLint = true; break;
+ case '--plan-only': options.planOnly = true; break;
+ case '--help':
+ case '-h': options.help = true; break;
+ default: throw new Error(`Unknown option: ${flag}.`);
+ }
+ }
+ return options;
}
function main() {
+ let options;
+ try {
+ options = parseArgs(process.argv.slice(2));
+ } catch (error) {
+ console.error(`❌ ${error.message}\n\n${USAGE}`);
+ process.exit(2);
+ }
+ if (options.help) {
+ console.log(USAGE);
+ process.exit(0);
+ }
+
assertNodeVersion();
const unmerged = spawnSync('git', ['ls-files', '-u', '-z'], { cwd: REPO_ROOT, encoding: 'utf8' });
@@ -224,7 +257,6 @@ function main() {
process.exit(1);
}
- const options = parseArgs(process.argv.slice(2));
const baseRef = options.base || defaultBaseRef();
const mergeBase = spawnSync('git', ['merge-base', baseRef, 'HEAD'], { cwd: REPO_ROOT, encoding: 'utf8' });
@@ -281,10 +313,14 @@ function main() {
const stages = resolvePlanStages(plan, { skipLint: options.skipLint, baseSha });
const uncovered = Object.keys(UNCOVERED_SUITES).filter((key) => plan[key]);
- uncovered.forEach((key) => console.log(`ℹ️ CI will also run: ${key} — not covered here; run ${UNCOVERED_SUITES[key]}`));
+ uncovered.forEach((key) => {
+ console.log(`ℹ️ CI will also run: ${key} — not covered here; run ${UNCOVERED_SUITES[key]}`);
+ });
if (options.planOnly) {
- stages.forEach((stage) => console.log(`• ${stage.name}: ${stage.script} ${stage.args.join(' ')}`));
+ stages.forEach((stage) => {
+ console.log(`• ${stage.name}: ${stage.script} ${stage.args.join(' ')}`);
+ });
process.exit(0);
}
diff --git a/scripts/pregate.test.js b/scripts/pregate.test.js
index 39b5798d78..cc292ede4e 100644
--- a/scripts/pregate.test.js
+++ b/scripts/pregate.test.js
@@ -304,12 +304,24 @@ describe('downgradeFullPlan', () => {
describe('parseArgs', () => {
it('reads --base as a value, not a boolean', () => {
expect(parseArgs(['--base', 'origin/release', '--full'])).toEqual({
- base: 'origin/release', full: true, skipLint: false, planOnly: false,
+ base: 'origin/release', full: true, skipLint: false, planOnly: false, help: false,
});
});
+ it('keeps the first --base when the option is repeated', () => {
+ expect(parseArgs(['--base', 'origin/release', '--base', 'origin/main']).base).toBe('origin/release');
+ });
+
it('defaults to a null base so the caller resolves the remote default branch', () => {
- expect(parseArgs([])).toEqual({ base: null, full: false, skipLint: false, planOnly: false });
+ expect(parseArgs([])).toEqual({ base: null, full: false, skipLint: false, planOnly: false, help: false });
+ });
+
+ it('recognizes both help flags and rejects unknown flags or a missing base value', () => {
+ expect(parseArgs(['--help']).help).toBe(true);
+ expect(parseArgs(['-h']).help).toBe(true);
+ expect(() => parseArgs(['--unexpected'])).toThrow('Unknown option: --unexpected.');
+ expect(() => parseArgs(['--base'])).toThrow('--base requires a value.');
+ expect(() => parseArgs(['--base', '--full'])).toThrow('--base requires a value.');
});
});
@@ -341,6 +353,37 @@ describe('UNCOVERED_SUITES', () => {
});
describe('the gate end to end', () => {
+ it.each([
+ { args: ['--help'], status: 0, expected: 'Usage: node scripts/pregate.js', stderr: false },
+ { args: ['-h'], status: 0, expected: 'Usage: node scripts/pregate.js', stderr: false },
+ { args: ['--unknown'], status: 2, expected: 'Unknown option: --unknown.', stderr: true },
+ { args: ['--base'], status: 2, expected: '--base requires a value.', stderr: true },
+ { args: ['--base', '--full'], status: 2, expected: '--base requires a value.', stderr: true },
+ ])('handles $args before starting any subprocess', ({ args, status, expected, stderr }) => {
+ const preload = `
+ import childProcess from 'node:child_process';
+ import { syncBuiltinESMExports } from 'node:module';
+ const unexpectedSubprocess = () => { throw new Error('Subprocess started before argument validation'); };
+ childProcess.spawn = unexpectedSubprocess;
+ childProcess.exec = unexpectedSubprocess;
+ childProcess.execFile = unexpectedSubprocess;
+ childProcess.fork = unexpectedSubprocess;
+ childProcess.execSync = unexpectedSubprocess;
+ childProcess.spawnSync = unexpectedSubprocess;
+ childProcess.execFileSync = unexpectedSubprocess;
+ syncBuiltinESMExports();
+ `;
+ const result = spawnSync(process.execPath, [
+ '--import', `data:text/javascript,${encodeURIComponent(preload)}`,
+ join(REPO_ROOT, 'scripts', 'pregate.js'), ...args,
+ ], { cwd: REPO_ROOT, encoding: 'utf8' });
+ expect(result.status, result.stderr).toBe(status);
+ expect(stderr ? result.stderr : result.stdout).toContain(expected);
+ expect(`${result.stdout}\n${result.stderr}`).toContain('Usage: node scripts/pregate.js');
+ expect(`${result.stdout}\n${result.stderr}`).not.toContain('Subprocess started before argument validation');
+ expect(result.stdout).not.toContain('Planning against');
+ });
+
// This catches validation deferred until after Git/planning, even when no
// expensive check would run (plan-only). Every subprocess is a tripwire.
it.each([{ flags: [] }, { flags: ['--full'] }, { flags: ['--plan-only'] }])('rejects unsupported Node before any subprocess with flags $flags', ({ flags }) => {