An open source design system that's fully customizable and agent ready
TypeScript
perf(vibe-tests): setup-integrity runs fewer git commands per analysis (#9918)
Christopher Chedeau8258e93cb42c
diff --git a/internal/vibe-tests/setup-test/setup-integrity.mjs b/internal/vibe-tests/setup-test/setup-integrity.mjsindex 4b1a6c9b03..47e0d15e59 100644--- a/internal/vibe-tests/setup-test/setup-integrity.mjs+++ b/internal/vibe-tests/setup-test/setup-integrity.mjs@@ -115,15 +115,27 @@ function git(appDir, args, options = {}) {}function assertRepositoryRoot(appDir) {- const root = path.resolve(- git(appDir, ['rev-parse', '--show-toplevel']).trim(),- );+ // One launch reads the top level and checks HEAD. When it fails, the two+ // checks run apart, so the first failure is reported as before.+ let topAndHead = null;+ try {+ topAndHead = git(appDir, [+ 'rev-parse',+ '--show-toplevel',+ '--verify',+ 'HEAD',+ ]);+ } catch {+ topAndHead = null;+ }+ const top = topAndHead ?? git(appDir, ['rev-parse', '--show-toplevel']);+ const root = path.resolve(top.split('\n')[0].trim());const requested = fs.realpathSync(appDir);const actual = fs.realpathSync(root);if (requested !== actual) {throw new Error(`--app must name the sandbox repository root: ${appDir}`);}- git(appDir, ['rev-parse', '--verify', 'HEAD']);+ if (topAndHead === null) git(appDir, ['rev-parse', '--verify', 'HEAD']);}function trackedPatch(appDir) {@@ -632,8 +644,8 @@ function addedLinesFromPatch(patch) {return lines;}-function trackedAddedLines(appDir, relativePath) {- const patch = git(appDir, [+function zeroContextPatch(appDir, relativePath) {+ return git(appDir, ['-c','core.quotePath=false','diff',@@ -646,7 +658,6 @@ function trackedAddedLines(appDir, relativePath) {'--',relativePath,]);- return addedLinesFromPatch(patch);}function removedLinesFromPatch(patch) {@@ -669,23 +680,6 @@ function removedLinesFromPatch(patch) {return lines;}-function trackedRemovedLines(appDir, relativePath) {- const patch = git(appDir, [- '-c',- 'core.quotePath=false',- 'diff',- '--unified=0',- '--no-color',- '--no-ext-diff',- '--no-textconv',- '--no-renames',- 'HEAD',- '--',- relativePath,- ]);- return removedLinesFromPatch(patch);-}-function untrackedAddedLines(file) {if (file.binary) return [];return file.bytes@@ -767,20 +761,39 @@ export function analyzeSetupIntegrity(appDir, attestedDiffSha256) {}));const untrackedByPath = new Map(untracked.map(file => [file.path, file]));const tracked = trackedNumstat(resolvedAppDir);+ // Each git command is a process launch, so a file's baseline and its+ // zero-context diff are read once per run.+ const baselines = new Map();+ const baselineOf = relativePath => {+ if (!baselines.has(relativePath)) {+ let bytes = null;+ try {+ bytes = baselineBytes(resolvedAppDir, relativePath);+ } catch {+ bytes = null;+ }+ baselines.set(relativePath, bytes);+ }+ return baselines.get(relativePath);+ };+ const patches = new Map();+ const zeroContextPatchOf = relativePath => {+ if (!patches.has(relativePath)) {+ patches.set(relativePath, zeroContextPatch(resolvedAppDir, relativePath));+ }+ return patches.get(relativePath);+ };+ const trackedAddedLines = relativePath =>+ addedLinesFromPatch(zeroContextPatchOf(relativePath));+ const trackedRemovedLines = relativePath =>+ removedLinesFromPatch(zeroContextPatchOf(relativePath));const changedFiles = [];const escapeHatches = [];const astryxEvidence = [];for (const file of tracked) {const absolutePath = path.join(resolvedAppDir, file.path);- const existedAtBaseline = (() => {- try {- baselineBytes(resolvedAppDir, file.path);- return true;- } catch {- return false;- }- })();+ const existedAtBaseline = baselineOf(file.path) !== null;const existsNow = fs.existsSync(absolutePath);const status = !existedAtBaseline? 'added'@@ -810,7 +823,7 @@ export function analyzeSetupIntegrity(appDir, attestedDiffSha256) {const currentSource = fs.readFileSync(absolutePath, 'utf8');const {baselineLines, deletedLines, deletedFraction} =substantiveLineDelta(- baselineBytes(resolvedAppDir, file.path).toString('utf8'),+ baselineOf(file.path).toString('utf8'),currentSource,);if (@@ -843,10 +856,7 @@ export function analyzeSetupIntegrity(appDir, attestedDiffSha256) {});}- const removedModeLines = trackedRemovedLines(- resolvedAppDir,- file.path,- ).filter(removed =>+ const removedModeLines = trackedRemovedLines(file.path).filter(removed =>/\b(?:dark|color-scheme|data-theme)\b/i.test(removed.text),);if (@@ -871,7 +881,7 @@ export function analyzeSetupIntegrity(appDir, attestedDiffSha256) {});}if (status !== 'deleted' && !file.binary) {- const addedLines = trackedAddedLines(resolvedAppDir, file.path);+ const addedLines = trackedAddedLines(file.path);if (isHostSource(file.path)) {const currentSource = fs.readFileSync(absolutePath, 'utf8');escapeHatches.push(@@ -882,9 +892,7 @@ export function analyzeSetupIntegrity(appDir, attestedDiffSha256) {resolvedAppDir,file.path,currentSource,- existedAtBaseline- ? trackedRemovedLines(resolvedAppDir, file.path)- : [],+ existedAtBaseline ? trackedRemovedLines(file.path) : [],),importantDeclarationLines(file.path, currentSource),),diff --git a/internal/vibe-tests/setup-test/setup-integrity.test.mjs b/internal/vibe-tests/setup-test/setup-integrity.test.mjsindex a512bdfafa..640d1f6f79 100644--- a/internal/vibe-tests/setup-test/setup-integrity.test.mjs+++ b/internal/vibe-tests/setup-test/setup-integrity.test.mjs@@ -5,7 +5,7 @@ import fs from 'node:fs';import os from 'node:os';import path from 'node:path';import {fileURLToPath} from 'node:url';-import {afterEach, describe, expect, it} from 'vitest';+import {afterAll, afterEach, describe, expect, it} from 'vitest';import {analyzeSetupIntegrity,pairedModeArmSpans,@@ -37,11 +37,31 @@ function write(directory, relativePath, content) {fs.writeFileSync(file, content);}+// The baseline repository is built once and copied for each test. A copy+// costs no git launch, and each launch is slow on a loaded machine.+let baselineRepository = null;+afterAll(() => {+ if (baselineRepository !== null) {+ fs.rmSync(baselineRepository, {recursive: true, force: true});+ }+});+function repository() {+ if (baselineRepository === null) {+ baselineRepository = baseline();+ }const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-integrity-test-'),);temporaryDirectories.push(directory);+ fs.cpSync(baselineRepository, directory, {recursive: true});+ return directory;+}++function baseline() {+ const directory = fs.mkdtempSync(+ path.join(os.tmpdir(), 'setup-integrity-baseline-'),+ );write(directory, '.gitignore', 'ignored.txt\n');write(directory,