import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const ASSESSMENTS = { approved: '✅ APPROVED', approved_with_notes: '⚠️ APPROVED WITH NOTES', changes_requested: '❌ CHANGES REQUESTED', }; const SEVERITY_ORDER = ['high', 'medium', 'low']; const SEVERITY_HEADERS = { high: '### 🔴 High', medium: '### 🟡 Medium', low: '### 🟢 Low', }; const STATUS_LABELS = { pass: '✅', fail: '⚠️', na: 'N/A', }; const REVIEW_MODE_DETAILS = { fast: 'diff-focused bounded review', triage: 'hotspot-based bounded review (non-exhaustive)', deep: 'expanded surrounding-code review', }; const RENDERER_OWNED_MARKUP_PATTERNS = [ { pattern: /^#{1,6}\s/u, reason: 'markdown heading' }, { pattern: /^\s*Verdict\s*:/iu, reason: 'verdict label' }, { pattern: /^\s*PR\s*#?\d+\s*Review(?:\s*[:.-]|$)/iu, reason: 'ad hoc PR heading' }, { pattern: /\|\s*[-:]+\s*\|/u, reason: 'markdown table' }, { pattern: /```/u, reason: 'code fence' }, ]; function cleanText(value) { return typeof value === 'string' ? value.trim().replace(/\s+/g, ' ') : ''; } function escapeMarkdownText(value) { return cleanText(value).replace(/\\/g, '\\\\').replace(/([`*_{}[\]<>|])/g, '\\$1'); } function renderCode(value) { const text = cleanText(value); const longestFence = Math.max(...[...text.matchAll(/`+/g)].map((match) => match[0].length), 0); const fence = '`'.repeat(longestFence + 1); return `${fence}${text}${fence}`; } function parsePositiveInteger(value) { if (value === null || value === undefined || value === '') { return null; } const parsed = typeof value === 'number' ? value : Number.parseInt(cleanText(value), 10); return Number.isInteger(parsed) && parsed > 0 ? parsed : null; } function normalizeReviewMode(value) { const mode = cleanText(value).toLowerCase(); return REVIEW_MODE_DETAILS[mode] ? mode : null; } function normalizeRenderingMetadata(raw) { if (!raw || typeof raw !== 'object' || Array.isArray(raw)) { return {}; } const mode = normalizeReviewMode(raw.mode); const maxTurns = parsePositiveInteger(raw.maxTurns); const timeoutMinutes = parsePositiveInteger(raw.timeoutMinutes); const timeoutSeconds = parsePositiveInteger(raw.timeoutSeconds); const selectedFiles = parsePositiveInteger(raw.selectedFiles); const reviewableFiles = parsePositiveInteger(raw.reviewableFiles); const selectedChanges = parsePositiveInteger(raw.selectedChanges); const reviewableChanges = parsePositiveInteger(raw.reviewableChanges); const scopeLabel = cleanText(raw.scopeLabel).toLowerCase(); const metadata = {}; if (mode) metadata.mode = mode; if (maxTurns) metadata.maxTurns = maxTurns; if (timeoutMinutes) metadata.timeoutMinutes = timeoutMinutes; if (timeoutSeconds) metadata.timeoutSeconds = timeoutSeconds; if (selectedFiles) metadata.selectedFiles = selectedFiles; if (reviewableFiles) metadata.reviewableFiles = reviewableFiles; if (selectedChanges) metadata.selectedChanges = selectedChanges; if (reviewableChanges) metadata.reviewableChanges = reviewableChanges; if (scopeLabel === 'reviewable files' || scopeLabel === 'changed files') metadata.scopeLabel = scopeLabel; return metadata; } function mergeRenderingMetadata(...sources) { const merged = {}; for (const source of sources) { Object.assign(merged, normalizeRenderingMetadata(source)); } return merged; } function formatTurnBudget(rendering) { return typeof rendering.maxTurns === 'number' ? `${rendering.maxTurns} turns` : null; } function formatTimeBudget(rendering) { if (typeof rendering.timeoutMinutes === 'number') { return `${rendering.timeoutMinutes} minute${rendering.timeoutMinutes === 1 ? '' : 's'}`; } if (typeof rendering.timeoutSeconds === 'number') { return `${rendering.timeoutSeconds} second${rendering.timeoutSeconds === 1 ? '' : 's'}`; } return null; } function formatCombinedBudget(rendering) { const parts = [formatTurnBudget(rendering), formatTimeBudget(rendering)].filter(Boolean); return parts.length > 0 ? parts.join(' / ') : null; } function formatScopeSummary(rendering) { if ( typeof rendering.selectedFiles !== 'number' || typeof rendering.reviewableFiles !== 'number' ) { return null; } const scopeLabel = rendering.scopeLabel || 'reviewable files'; const fileScope = `${rendering.selectedFiles}/${rendering.reviewableFiles} ${scopeLabel}`; if ( typeof rendering.selectedChanges === 'number' && typeof rendering.reviewableChanges === 'number' ) { const changeLabel = scopeLabel === 'reviewable files' ? 'reviewable changed lines' : 'changed lines'; return `${fileScope}; ${rendering.selectedChanges}/${rendering.reviewableChanges} ${changeLabel}`; } return fileScope; } function formatReviewContext(rendering) { const parts = []; if (rendering.mode) { parts.push(`mode ${renderCode(rendering.mode)}`); parts.push(REVIEW_MODE_DETAILS[rendering.mode]); } const scopeSummary = formatScopeSummary(rendering); if (scopeSummary) { parts.push(`scope ${scopeSummary}`); } const turnBudget = formatTurnBudget(rendering); if (turnBudget) { parts.push(`turn budget ${turnBudget}`); } const timeBudget = formatTimeBudget(rendering); if (timeBudget) { parts.push(`workflow cap ${timeBudget}`); } if (parts.length === 0) { return null; } return `> 🧭 Review context: ${parts.join('; ')}.`; } function classifyFallbackReason(reason) { const normalized = cleanText(reason).toLowerCase(); if (!normalized || normalized === 'missing structured output') { return 'missing'; } if (normalized === 'structured output is not valid json') { return 'invalid_json'; } return 'invalid_fields'; } function describeIncompleteOutcome({ reason, rendering, turnsUsed, status }) { const reviewLabel = rendering.mode ? `${renderCode(rendering.mode)} review` : 'bounded review'; const turnBudget = formatTurnBudget(rendering); const timeBudget = formatTimeBudget(rendering); const combinedBudget = formatCombinedBudget(rendering); const exhaustedTurnBudget = typeof turnsUsed === 'number' && typeof rendering.maxTurns === 'number' && turnsUsed >= rendering.maxTurns; if (status === 'cancelled' && timeBudget) { return `The ${reviewLabel} hit the workflow runtime cap before it produced validated structured output. The run stayed bounded to ${timeBudget}.`; } if (exhaustedTurnBudget) { return `The ${reviewLabel} reached its ${rendering.maxTurns}-turn runtime budget before it produced validated structured output.`; } if (combinedBudget && classifyFallbackReason(reason) === 'missing') { return `The ${reviewLabel} ended before it could produce validated structured output within the available ${combinedBudget} runtime budget.`; } if (classifyFallbackReason(reason) === 'missing' || classifyFallbackReason(reason) === 'invalid_json') { return `The ${reviewLabel} ended without validated structured output, so the normalizer published the safe fallback comment instead.`; } return `The ${reviewLabel} returned incomplete structured data, so the normalizer published the safe fallback comment instead.`; } function validatePlainTextField(fieldName, value) { const text = cleanText(value); if (!text) { return { ok: false, reason: `${fieldName} is required` }; } const match = RENDERER_OWNED_MARKUP_PATTERNS.find(({ pattern }) => pattern.test(text)); if (match) { return { ok: false, reason: `${fieldName} contains ${match.reason}` }; } return { ok: true, value: text }; } function normalizeStringList(fieldName, raw) { if (!Array.isArray(raw)) { return { ok: false, reason: `${fieldName} must be an array` }; } const values = []; for (const [index, item] of raw.entries()) { const validation = validatePlainTextField(`${fieldName}[${index}]`, item); if (!validation.ok) return validation; values.push(validation.value); } return { ok: true, value: values }; } function normalizeChecklistRows(fieldName, labelField, raw) { if (!Array.isArray(raw)) { return { ok: false, reason: `${fieldName} must be an array` }; } const rows = []; for (const [index, item] of raw.entries()) { const label = validatePlainTextField(`${fieldName}[${index}].${labelField}`, item?.[labelField]); if (!label.ok) return label; const notes = validatePlainTextField(`${fieldName}[${index}].notes`, item?.notes); if (!notes.ok) return notes; const status = cleanText(item?.status).toLowerCase(); if (!STATUS_LABELS[status]) { return { ok: false, reason: `${fieldName}[${index}].status is invalid` }; } rows.push({ [labelField]: label.value, status, notes: notes.value }); } if (rows.length === 0) { return { ok: false, reason: `${fieldName} must contain at least 1 item` }; } return { ok: true, value: rows }; } function readExecutionMetadata(executionFile) { if (!executionFile || !fs.existsSync(executionFile)) { return {}; } try { const turns = JSON.parse(fs.readFileSync(executionFile, 'utf8')); const init = turns.find((turn) => turn?.type === 'system' && turn?.subtype === 'init'); const result = [...turns].reverse().find((turn) => turn?.type === 'result'); return { runtimeTools: Array.isArray(init?.tools) ? init.tools : [], turnsUsed: typeof result?.num_turns === 'number' ? result.num_turns : null, }; } catch { return {}; } } export function normalizeStructuredOutput(raw) { if (!raw) { return { ok: false, reason: 'missing structured output' }; } let parsed; try { parsed = typeof raw === 'string' ? JSON.parse(raw) : raw; } catch { return { ok: false, reason: 'structured output is not valid JSON' }; } if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { return { ok: false, reason: 'structured output must be an object' }; } const summary = validatePlainTextField('summary', parsed.summary); if (!summary.ok) return summary; const overallAssessment = cleanText(parsed.overallAssessment).toLowerCase(); const overallRationale = validatePlainTextField('overallRationale', parsed.overallRationale); if (!overallRationale.ok) return overallRationale; const findings = Array.isArray(parsed.findings) ? parsed.findings : null; const securityChecklist = normalizeChecklistRows('securityChecklist', 'check', parsed.securityChecklist); if (!securityChecklist.ok) return securityChecklist; const ccsCompliance = normalizeChecklistRows('ccsCompliance', 'rule', parsed.ccsCompliance); if (!ccsCompliance.ok) return ccsCompliance; const informational = normalizeStringList('informational', parsed.informational); if (!informational.ok) return informational; const strengths = normalizeStringList('strengths', parsed.strengths); if (!strengths.ok) return strengths; const rendering = normalizeRenderingMetadata(parsed.rendering); if (!ASSESSMENTS[overallAssessment] || findings === null) { return { ok: false, reason: 'structured output is missing required review fields' }; } const normalizedFindings = []; for (const [index, finding] of findings.entries()) { const severity = cleanText(finding?.severity).toLowerCase(); const title = validatePlainTextField(`findings[${index}].title`, finding?.title); if (!title.ok) return title; const file = validatePlainTextField(`findings[${index}].file`, finding?.file); if (!file.ok) return file; const what = validatePlainTextField(`findings[${index}].what`, finding?.what); if (!what.ok) return what; const why = validatePlainTextField(`findings[${index}].why`, finding?.why); if (!why.ok) return why; const fix = validatePlainTextField(`findings[${index}].fix`, finding?.fix); if (!fix.ok) return fix; let line = null; if (finding && Object.hasOwn(finding, 'line')) { if (finding.line === null) { line = null; } else if (typeof finding.line === 'number' && Number.isInteger(finding.line) && finding.line > 0) { line = finding.line; } else { return { ok: false, reason: `findings[${index}].line is invalid` }; } } if (!SEVERITY_HEADERS[severity]) { return { ok: false, reason: `findings[${index}].severity is invalid` }; } normalizedFindings.push({ severity, title: title.value, file: file.value, line, what: what.value, why: why.value, fix: fix.value, }); } const value = { summary: summary.value, findings: normalizedFindings, overallAssessment, overallRationale: overallRationale.value, securityChecklist: securityChecklist.value, ccsCompliance: ccsCompliance.value, informational: informational.value, strengths: strengths.value, }; if (Object.keys(rendering).length > 0) { value.rendering = rendering; } return { ok: true, value }; } function renderChecklistTable(title, labelHeader, labelKey, rows) { const lines = ['', title, '', `| ${labelHeader} | Status | Notes |`, '|---|---|---|']; for (const row of rows) { lines.push( `| ${escapeMarkdownText(row[labelKey])} | ${STATUS_LABELS[row.status]} | ${escapeMarkdownText(row.notes)} |` ); } return lines; } function renderBulletSection(title, items) { if (items.length === 0) return []; return ['', title, ...items.map((item) => `- ${escapeMarkdownText(item)}`)]; } export function renderStructuredReview(review, { model, rendering: renderOptions } = {}) { const rendering = mergeRenderingMetadata(review?.rendering, renderOptions); const lines = ['### 📋 Summary', '', escapeMarkdownText(review.summary), '', '### 🔍 Findings']; const reviewContext = formatReviewContext(rendering); if (reviewContext) { lines.splice(4, 0, reviewContext, ''); } if (review.findings.length === 0) { lines.push('No confirmed issues found after reviewing the diff and surrounding code.'); } else { for (const severity of SEVERITY_ORDER) { const findings = review.findings.filter((finding) => finding.severity === severity); if (findings.length === 0) continue; lines.push('', SEVERITY_HEADERS[severity], ''); for (const finding of findings) { const location = finding.line ? `${finding.file}:${finding.line}` : finding.file; lines.push(`- **${renderCode(location)} — ${escapeMarkdownText(finding.title)}**`); lines.push(` Problem: ${escapeMarkdownText(finding.what)}`); lines.push(` Why it matters: ${escapeMarkdownText(finding.why)}`); lines.push(` Suggested fix: ${escapeMarkdownText(finding.fix)}`); lines.push(''); } } if (lines[lines.length - 1] === '') lines.pop(); } lines.push(...renderChecklistTable('### 🔒 Security Checklist', 'Check', 'check', review.securityChecklist)); lines.push(...renderChecklistTable('### 📊 CCS Compliance', 'Rule', 'rule', review.ccsCompliance)); lines.push(...renderBulletSection('### 💡 Informational', review.informational)); lines.push(...renderBulletSection("### ✅ What's Done Well", review.strengths)); lines.push( '', '### 🎯 Overall Assessment', '', `**${ASSESSMENTS[review.overallAssessment]}** — ${escapeMarkdownText(review.overallRationale)}`, '', `> 🤖 Reviewed by \`${model}\`` ); return lines.join('\n'); } export function renderIncompleteReview({ model, reason, runUrl, runtimeTools, turnsUsed, rendering: renderOptions, status, }) { const rendering = mergeRenderingMetadata(renderOptions); const lines = [ '### ⚠️ AI Review Incomplete', '', 'Claude did not return validated structured review output, so this workflow did not publish raw scratch text.', '', `- Outcome: ${describeIncompleteOutcome({ reason, rendering, turnsUsed, status })}`, ]; if (rendering.mode) { lines.push(`- Review mode: ${renderCode(rendering.mode)} (${escapeMarkdownText(REVIEW_MODE_DETAILS[rendering.mode])})`); } const scopeSummary = formatScopeSummary(rendering); if (scopeSummary) { lines.push(`- Review scope: ${escapeMarkdownText(scopeSummary)}`); } const runtimeBudget = formatCombinedBudget(rendering); if (runtimeBudget) { lines.push(`- Runtime budget: ${escapeMarkdownText(runtimeBudget)}`); } if (runtimeTools?.length) { lines.push(`- Runtime tools: ${runtimeTools.map(renderCode).join(', ')}`); } if (typeof turnsUsed === 'number') { lines.push(`- Turns used: ${turnsUsed}`); } lines.push('', `Re-run \`/review\` or inspect [the workflow run](${runUrl}).`, '', `> 🤖 Reviewed by \`${model}\``); return lines.join('\n'); } export function writeReviewFromEnv(env = process.env) { const outputFile = env.AI_REVIEW_OUTPUT_FILE || 'pr_review.md'; const model = env.AI_REVIEW_MODEL || 'unknown-model'; const runUrl = env.AI_REVIEW_RUN_URL || '#'; const validation = normalizeStructuredOutput(env.AI_REVIEW_STRUCTURED_OUTPUT); const metadata = readExecutionMetadata(env.AI_REVIEW_EXECUTION_FILE); const status = cleanText(env.AI_REVIEW_STATUS).toLowerCase() || null; const rendering = normalizeRenderingMetadata({ mode: env.AI_REVIEW_MODE, selectedFiles: env.AI_REVIEW_SELECTED_FILES, reviewableFiles: env.AI_REVIEW_REVIEWABLE_FILES, selectedChanges: env.AI_REVIEW_SELECTED_CHANGES, reviewableChanges: env.AI_REVIEW_REVIEWABLE_CHANGES, scopeLabel: env.AI_REVIEW_SCOPE_LABEL, maxTurns: env.AI_REVIEW_MAX_TURNS, timeoutMinutes: env.AI_REVIEW_TIMEOUT_MINUTES ?? env.AI_REVIEW_TIMEOUT_MINUTES_BUDGET, timeoutSeconds: env.AI_REVIEW_TIMEOUT_SECONDS ?? env.AI_REVIEW_TIMEOUT_SEC, }); const content = validation.ok ? renderStructuredReview(validation.value, { model, rendering }) : renderIncompleteReview({ model, reason: validation.reason, runUrl, runtimeTools: metadata.runtimeTools, turnsUsed: metadata.turnsUsed, rendering, status, }); fs.mkdirSync(path.dirname(outputFile), { recursive: true }); fs.writeFileSync(outputFile, `${content}\n`, 'utf8'); if (!validation.ok) { console.warn(`::warning::AI review output normalization fell back to incomplete comment: ${validation.reason}`); } return { usedFallback: !validation.ok, content }; } const isMain = process.argv[1] && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url)); if (isMain) { writeReviewFromEnv(); }