Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

8.6 KB
Raw Blame
import { mkdir, mkdtemp } from 'node:fs/promises';
import path from 'node:path';
import { parseArgs } from 'node:util';

import { fixtureIds, getStyleFixture } from './fixtures/fixtureRegistry';
import { runPreflight } from './pipeline/preflight';
import { armRunWatchdog } from './pipeline/runWatchdog';
import { fingerprintSources, writeJson } from './reporting/report';
import { recordSourceCleanup } from './source/cleanupSnapshots';
import { resolvePullRequest } from './source/pullRequest';

const help = `StyleX migration verification — calibration evidence collector (not a PR gate)

  bun run stylex:verify --base <ref> --head <ref> [--plan-only]
  bun run stylex:verify --pr <number> [--repo owner/name] [--plan-only]
  bun run stylex:verify --base <ref> --head <ref> --restore-dependencies [--dependency-store <path>]
  bun run stylex:verify --base <ref> --head <ref> --restore-dependencies --fixture LiteTable
  bun run stylex:verify --base <ref> --head <ref> --restore-dependencies --fixture LiteTable --fixture-dependencies
  bun run stylex:verify --base <ref> --head <ref> --restore-dependencies --fixtures DotsLoading,UnreadDot --fixture-dependencies
  bun run stylex:verify self-test [--browser <executable>]

Options: --output <parent-directory>, --repository <local-path>, --help
--screenshots captures up to three diagnostic A/A/B viewport image sets per component.
Temporary source/dependency exports are removed after success or failure; --keep-sources retains them for debugging.
Available fixtures: ${fixtureIds.join(', ')}
Revision objects must exist locally. No Git fetch, GitHub writes or PR merges.
Dependency restore is opt-in, offline by default; --dependency-network explicitly enables public npm preparation.
Dependency scripts/hooks and credentials are disabled; component builds run in an offline macOS sandbox.
Default: export both complete source trees and report remaining verification gaps.
The PR gate is bun run style-eq; this command only collects calibration evidence.
Exit: collection ran 0, self-test regression 1, ERROR 3.
`;

const main = async () => {
  const { positionals, values } = parseArgs({
    allowPositionals: true,
    options: {
      'base': { type: 'string' },
      'browser': { type: 'string' },
      'dependency-network': { type: 'boolean' },
      'dependency-store': { type: 'string' },
      'head': { type: 'string' },
      'fixture': { type: 'string' },
      'fixtures': { type: 'string' },
      'fixture-dependencies': { type: 'boolean' },
      'help': { short: 'h', type: 'boolean' },
      'keep-sources': { type: 'boolean' },
      'output': { type: 'string' },
      'plan-only': { type: 'boolean' },
      'pr': { type: 'string' },
      'pnpm': { type: 'string' },
      'restore-dependencies': { type: 'boolean' },
      'screenshots': { type: 'boolean' },
      'repo': { type: 'string' },
      'repository': { type: 'string' },
    },
    strict: true,
  });
  if (values.help) {
    console.log(help);
    return;
  }
  if (positionals.length > 1 || (positionals.length && positionals[0] !== 'self-test'))
    throw new Error('UNSUPPORTED_COMMAND');
  const selfTest = positionals[0] === 'self-test';
  if (
    (values['dependency-network'] || values['dependency-store'] || values.pnpm) &&
    !values['restore-dependencies']
  )
    throw new Error('DEPENDENCY_OPTIONS_REQUIRE_RESTORE');
  if (values['restore-dependencies'] && (selfTest || values['plan-only']))
    throw new Error('DEPENDENCY_RESTORE_REQUIRES_FULL_SOURCE');
  if (values.fixture && values.fixtures) throw new Error('CONFLICTING_FIXTURE_OPTIONS');
  if (values.screenshots && !values.fixture && !values.fixtures)
    throw new Error('SCREENSHOTS_REQUIRE_FIXTURE');
  const selectedFixtures = values.fixtures?.split(',').map((id) => id.trim());
  if (
    selectedFixtures &&
    (!selectedFixtures.length ||
      selectedFixtures.length > 32 ||
      new Set(selectedFixtures).size !== selectedFixtures.length ||
      selectedFixtures.some((id) => !id))
  )
    throw new Error('INVALID_FIXTURE_SELECTION');
  if (
    (values.fixture || values.fixtures) &&
    (!values['restore-dependencies'] || selfTest || values['plan-only'])
  )
    throw new Error('FIXTURE_REQUIRES_ISOLATED_DEPENDENCIES');
  if (values.fixture) getStyleFixture(values.fixture);
  selectedFixtures?.forEach(getStyleFixture);
  if (values['fixture-dependencies'] && !values.fixture && !values.fixtures)
    throw new Error('FIXTURE_DEPENDENCIES_REQUIRE_FIXTURE');
  if (selfTest && (values.pr || values.base || values.head || values['plan-only']))
    throw new Error('INVALID_SELF_TEST_OPTIONS');
  if (
    !selfTest &&
    ((!values.pr && (!values.base || !values.head)) || (values.pr && (values.base || values.head)))
  )
    throw new Error('Specify --pr OR both --base and --head');
  if (!selfTest && values.browser && !values.fixture && !values.fixtures)
    throw new Error('--browser requires self-test or --fixture');
  const repository = path.resolve(
    values.repository ?? process.env.STYLEX_VERIFIER_REPOSITORY ?? process.cwd(),
  );
  const sourceDirectory = process.env.STYLEX_VERIFIER_SOURCE_DIRECTORY;
  if (!sourceDirectory)
    throw new Error('Run through bun run stylex:verify to bind verifier sources');
  const verifierHash = await fingerprintSources([sourceDirectory]);
  if (verifierHash !== process.env.STYLEX_VERIFIER_SOURCE_HASH)
    throw new Error('VERIFIER_SOURCE_HASH_MISMATCH');
  const parent = path.resolve(
    values.output ?? path.join(repository, '.records/stylex-verification'),
  );
  await mkdir(parent, { recursive: true });
  const output = await mkdtemp(path.join(parent, `${selfTest ? 'self-test' : 'run'}-`));
  console.log(`Evidence: ${output}`);
  // Bounded process watchdog also covers page.evaluate calls without protocol timeouts.
  let cleanupPromise: Promise<void> | undefined;
  const cleanup = () =>
    (cleanupPromise ??=
      selfTest || values['keep-sources'] ? Promise.resolve() : recordSourceCleanup(output));
  const stopWatchdog = armRunWatchdog({
    cleanup,
    durationMs: values['restore-dependencies'] ? 900_000 : 120_000,
  });
  try {
    if (selfTest) {
      const { runSelfTest } = await import('./selfTest');
      const result = await runSelfTest(output, verifierHash, values.browser);
      console.log(JSON.stringify(result));
      process.exitCode = result.status === 'PASS' ? 0 : 1;
      return;
    }
    let base = values.base;
    let head = values.head;
    let pullRequest: Awaited<ReturnType<typeof resolvePullRequest>> | undefined;
    if (values.pr) {
      if (!/^[1-9]\d*$/.test(values.pr) || !Number.isSafeInteger(Number(values.pr)))
        throw new Error('INVALID_PR_NUMBER');
      const pr = await resolvePullRequest(repository, Number(values.pr), values.repo);
      pullRequest = pr;
      await writeJson(output, 'pull-request.json', pr);
      base = pr.base;
      head = pr.head;
    }
    if (!base || !head) throw new Error('MISSING_REVISIONS');
    const result = await runPreflight({
      base,
      dependencyOptions: values['restore-dependencies']
        ? {
            allowNetwork: values['dependency-network'] ?? false,
            pnpm: values.pnpm ?? 'pnpm',
            store: path.resolve(
              values['dependency-store'] ?? path.join(output, 'dependency-store'),
            ),
          }
        : undefined,
      fixture: values.fixture
        ? {
            browser: values.browser,
            id: values.fixture,
            sourceDirectory,
          }
        : undefined,
      fixtureDependencies: values['fixture-dependencies'] ?? false,
      fixtureBatch: selectedFixtures
        ? { browser: values.browser, ids: selectedFixtures, sourceDirectory }
        : undefined,
      head,
      output,
      planOnly: values['plan-only'] ?? false,
      pullRequest,
      repository,
      verifierHash,
      screenshots: values.screenshots,
    });
    console.log(
      JSON.stringify({
        changedFiles: result.fixtures.unmappedFiles.length + result.fixtures.candidates.length,
        component: result.componentSummary,
        componentBatch: result.componentBatchSummary,
        reasonCodes: result.reasonCodes,
        report: path.join(output, 'report.html'),
        units: result.unresolvedUnitIds.length,
      }),
    );
    process.exitCode = 0;
  } catch (error) {
    console.error(error);
    await writeJson(output, 'error.json', {
      message: error instanceof Error ? error.message : String(error),
      mode: selfTest ? 'self-test' : 'preflight',
      schemaVersion: 1,
      status: 'ERROR',
      verifierHash,
    });
    process.exitCode = 3;
  } finally {
    stopWatchdog();
    await cleanup();
  }
};

main().catch((error) => {
  console.error(error);
  process.exitCode = 3;
});