diff --git a/vibe-check-runner.js b/vibe-check-runner.js index f56ee94..be5f3ac 100644 --- a/vibe-check-runner.js +++ b/vibe-check-runner.js @@ -2,6 +2,7 @@ import { Project, SyntaxKind } from 'ts-morph'; import fs from 'node:fs'; import fsPromises from 'node:fs/promises'; import path from 'node:path'; +import { pathToFileURL } from 'node:url'; import { execFileSync } from 'node:child_process'; import { GoogleGenAI } from '@google/genai'; @@ -9,7 +10,7 @@ const ai = new GoogleGenAI({ apiKey: process.env.GOOGLE_AI_API_KEY }); // Constants for scoring -async function fileOrDirExists(filePath) { +export async function fileOrDirExists(filePath) { try { await fsPromises.stat(filePath); return true; @@ -25,7 +26,7 @@ const SCORES = { EFFICIENCY: 10, }; -function getModifiedFiles() { +export function getModifiedFiles() { try { // In CI (daily run), check files modified in the last 24 hours. // We filter for non-empty lines that end in .md and are in frontend/ or backend/ @@ -43,7 +44,7 @@ function getModifiedFiles() { } } -async function syncBenchmarks(tech, mdContent, retries = 5, delay = 10000) { +export async function syncBenchmarks(tech, mdContent, retries = 5, delay = 10000) { try { const prompt = `Based on the following documentation:\n\n${mdContent}\n\n1. Generate a "Golden Prompt" (a comprehensive instruction for generating a typical module using this technology) in JSON format: {"golden_prompt": "...", "tech": "${tech}"}\n2. Generate a JSON Schema for TS-Morph AST validation rules enforcing DDD/FSD layers and strict typing for this technology. The generated JSON schema must explicitly follow a nested structure compatible with \`analyzeAST\`. Format: {"$schema": "...", "type": "object", "properties": {"forbidden_types": {"contains": {"enum": ["any"]}}}}.\n\nRespond strictly with ONLY a JSON array containing these two objects in order. No markdown wrappers.`; const response = await ai.models.generateContent({ @@ -85,7 +86,7 @@ async function syncBenchmarks(tech, mdContent, retries = 5, delay = 10000) { } } -async function simulateAIGeneration(goldenPrompt, tech, mdContent, retries = 5, delay = 10000) { +export async function simulateAIGeneration(goldenPrompt, tech, mdContent, retries = 5, delay = 10000) { try { const prompt = `${goldenPrompt}\n\nConstraints and instructions from the following documentation:\n\n${mdContent}\n\nGenerate ONLY raw code. No markdown formatting, no explanations.`; const response = await ai.models.generateContent({ @@ -110,7 +111,7 @@ async function simulateAIGeneration(goldenPrompt, tech, mdContent, retries = 5, } } -function analyzeAST(sourceFile, tech) { +export function analyzeAST(sourceFile, tech) { let score = { arch: SCORES.ARCH, type: SCORES.TYPE, @@ -222,7 +223,7 @@ function analyzeAST(sourceFile, tech) { return { total, breakdown: score }; } -async function runVibeCheck() { +export async function runVibeCheck() { console.log('Running Vibe-Check Runner...'); const modifiedFiles = getModifiedFiles(); @@ -231,19 +232,9 @@ async function runVibeCheck() { return; } - const project = new Project(); - - // Configure git user for commits - try { - execFileSync('git', ['config', '--global', 'user.name', 'github-actions[bot]']); - execFileSync('git', ['config', '--global', 'user.email', 'github-actions[bot]@users.noreply.github.com']); - } catch (e) { - console.warn('Failed to configure git user. If running locally, this is expected.'); - } - + // Group files by tech + const filesByTech = {}; for (const file of modifiedFiles) { - console.log(`Processing ${file}...`); - if (!fs.existsSync(file)) { console.log(`File ${file} does not exist. Skipping.`); continue; @@ -265,27 +256,77 @@ async function runVibeCheck() { } } - const mdContent = await fsPromises.readFile(file, 'utf-8'); + if (!filesByTech[tech]) filesByTech[tech] = []; + filesByTech[tech].push(file); + } + + // 1. Sequential syncBenchmarks + for (const tech in filesByTech) { + console.log(`Syncing benchmarks for ${tech}...`); + const mdContents = []; + for (const file of filesByTech[tech]) { + const content = await fsPromises.readFile(file, 'utf-8'); + mdContents.push(content); + } + const combinedContent = mdContents.join('\n\n--- \n\n'); + await syncBenchmarks(tech, combinedContent); + } - await syncBenchmarks(tech, mdContent); + // 2. Concurrent simulateAIGeneration and analyzeAST + console.log('Running AI generation and AST analysis concurrently...'); + const analysisResults = []; + const promises = []; + for (const tech in filesByTech) { const suitePath = path.join('benchmarks', 'suites', `${tech}.json`); - if (!fs.existsSync(suitePath)) { - console.log(`No benchmark suite found for ${tech}. Skipping.`); - continue; + let suiteConfig = null; + if (fs.existsSync(suitePath)) { + suiteConfig = JSON.parse(await fsPromises.readFile(suitePath, 'utf-8')); + } else { + console.log(`No benchmark suite found for ${tech}. Skipping AI gen.`); } - const suiteConfig = JSON.parse(await fsPromises.readFile(suitePath, 'utf-8')); + for (const file of filesByTech[tech]) { + if (!suiteConfig) continue; - const generatedCode = await simulateAIGeneration(suiteConfig.golden_prompt, tech, mdContent); + const p = (async () => { + const mdContent = await fsPromises.readFile(file, 'utf-8'); + const generatedCode = await simulateAIGeneration(suiteConfig.golden_prompt, tech, mdContent); - if (!generatedCode) { - console.error(`Failed to generate code for ${tech}.`); - continue; + if (!generatedCode) { + console.error(`Failed to generate code for ${tech} (${file}).`); + return null; + } + + const project = new Project(); + const tempFile = `temp_${tech}_${Math.random().toString(36).substring(7)}.ts`; + const sourceFile = project.createSourceFile(tempFile, generatedCode, { overwrite: true }); + const { total: score, breakdown } = analyzeAST(sourceFile, tech); + + return { file, tech, score, breakdown, generatedCode }; + })(); + promises.push(p); } + } + + const results = await Promise.all(promises); + results.forEach(r => { + if (r) analysisResults.push(r); + }); + + // 3. Sequential side effects (Git, file updates, GH CLI) + // Configure git user for commits + try { + execFileSync('git', ['config', '--global', 'user.name', 'github-actions[bot]']); + execFileSync('git', ['config', '--global', 'user.email', 'github-actions[bot]@users.noreply.github.com']); + } catch (e) { + console.warn('Failed to configure git user. If running locally, this is expected.'); + } - const sourceFile = project.createSourceFile(`temp_${tech}.ts`, generatedCode, { overwrite: true }); - const { total: score, breakdown } = analyzeAST(sourceFile, tech); + let failedFiles = false; + + for (const result of analysisResults) { + const { file, tech, score, breakdown, generatedCode } = result; console.log(`Fidelity Score for ${file}: ${score}%`); console.log(`Breakdown:`, breakdown); @@ -316,6 +357,7 @@ async function runVibeCheck() { } else { console.error(`❌ Validation failed for ${file}. Score below 95%.`); + failedFiles = true; const reportDir = path.join('benchmarks', 'logs'); if (!await fileOrDirExists(reportDir)) await fsPromises.mkdir(reportDir, { recursive: true }); @@ -332,10 +374,14 @@ async function runVibeCheck() { } catch (err) { console.error('Failed to create GitHub Issue (gh cli might not be installed or authenticated):', err.message); } - - process.exitCode = 1; } } + + if (failedFiles) { + process.exitCode = 1; + } } -runVibeCheck().catch(console.error); \ No newline at end of file +if (import.meta.url === pathToFileURL(process.argv[1]).href) { + runVibeCheck().catch(console.error); +} \ No newline at end of file