|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Delta-only high-confidence gate. Fails the workflow when the PR introduces |
| 4 | + * NEW high-confidence agent-doc findings vs base. |
| 5 | + * |
| 6 | + * High-confidence classes: |
| 7 | + * - broken @imports |
| 8 | + * - broken symlink targets |
| 9 | + * - linked-inverted pairs |
| 10 | + * - unexpected-duplicate pairs |
| 11 | + * |
| 12 | + * Heuristic / advisory classes are explicitly excluded to keep the false- |
| 13 | + * positive rate near zero: brokenPathRefs (backtick regex), budget warnings, |
| 14 | + * unresolvedCommands. |
| 15 | + * |
| 16 | + * Writes the result to GATE_RESULT_PATH so the comment step can surface |
| 17 | + * "Blocking" state inline. Exits 1 if blocking, 0 otherwise. |
| 18 | + */ |
| 19 | + |
| 20 | +import { execFileSync } from 'node:child_process'; |
| 21 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 22 | +import { tmpdir } from 'node:os'; |
| 23 | +import { dirname, join, resolve } from 'node:path'; |
| 24 | +import { runL1Scan } from './agent-docs-l1.mjs'; |
| 25 | + |
| 26 | +const REPO_ROOT = resolve(process.env.REPO_ROOT ?? process.cwd()); |
| 27 | +const BASE_REF = process.env.BASE_REF || 'main'; |
| 28 | +const PR = process.env.PR_NUMBER; |
| 29 | +const REPO = process.env.REPO ?? 'superdoc-dev/superdoc'; |
| 30 | +const RESULT_PATH = process.env.GATE_RESULT_PATH || '/tmp/agent-docs-gate.json'; |
| 31 | +const DRY_RUN = process.argv.includes('--dry-run'); |
| 32 | + |
| 33 | +function isAgentDocPath(path) { |
| 34 | + if (/(?:^|\/)(?:AGENTS|CLAUDE)(?:\.local)?\.md$/.test(path)) return true; |
| 35 | + return /(?:^|\/)\.claude\/rules\/.+\.md$/.test(path); |
| 36 | +} |
| 37 | + |
| 38 | +function getChangedAgentDocs() { |
| 39 | + if (DRY_RUN) { |
| 40 | + const idx = process.argv.indexOf('--files'); |
| 41 | + if (idx < 0) return []; |
| 42 | + return (process.argv[idx + 1] || '').split(',').map((s) => s.trim()).filter(Boolean).filter(isAgentDocPath); |
| 43 | + } |
| 44 | + if (!PR) return []; |
| 45 | + try { |
| 46 | + const out = execFileSync('gh', ['pr', 'diff', PR, '--repo', REPO, '--name-only'], { encoding: 'utf-8' }); |
| 47 | + return out.split('\n').map((s) => s.trim()).filter(Boolean).filter(isAgentDocPath); |
| 48 | + } catch (err) { |
| 49 | + console.log(`Could not list PR changed files: ${err.message}`); |
| 50 | + return []; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function changedPairDirs(paths) { |
| 55 | + const dirs = new Set(); |
| 56 | + for (const path of paths) { |
| 57 | + if (/(?:^|\/)(?:AGENTS|CLAUDE)(?:\.local)?\.md$/.test(path)) { |
| 58 | + dirs.add(dirname(path)); |
| 59 | + } |
| 60 | + } |
| 61 | + return dirs; |
| 62 | +} |
| 63 | + |
| 64 | +function highConfidenceFindings(scan) { |
| 65 | + const findings = []; |
| 66 | + for (const file of scan.files) { |
| 67 | + if (file.brokenSymlinkTarget) { |
| 68 | + findings.push({ |
| 69 | + type: 'broken-symlink', |
| 70 | + relPath: file.relPath, |
| 71 | + target: file.brokenSymlinkTarget, |
| 72 | + id: `symlink:${file.relPath}`, |
| 73 | + }); |
| 74 | + } |
| 75 | + if (file.isSymlink) continue; |
| 76 | + for (const importPath of file.brokenImports) { |
| 77 | + findings.push({ |
| 78 | + type: 'broken-import', |
| 79 | + relPath: file.relPath, |
| 80 | + importPath, |
| 81 | + id: `import:${file.relPath}:${importPath}`, |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | + for (const pair of scan.pairs) { |
| 86 | + if (pair.classification === 'linked-inverted' || pair.classification === 'unexpected-duplicate') { |
| 87 | + findings.push({ |
| 88 | + type: 'pair', |
| 89 | + dir: pair.dir, |
| 90 | + classification: pair.classification, |
| 91 | + detail: pair.detail, |
| 92 | + id: `pair:${pair.dir}:${pair.classification}`, |
| 93 | + }); |
| 94 | + } |
| 95 | + } |
| 96 | + return findings; |
| 97 | +} |
| 98 | + |
| 99 | +function prepareBaseSnapshot() { |
| 100 | + execFileSync('git', ['fetch', '--no-tags', '--depth=1', 'origin', BASE_REF], { cwd: REPO_ROOT, stdio: 'inherit' }); |
| 101 | + const baseDir = mkdtempSync(join(tmpdir(), 'agent-docs-base-')); |
| 102 | + execFileSync('git', ['worktree', 'add', '--detach', baseDir, `origin/${BASE_REF}`], { cwd: REPO_ROOT, stdio: 'inherit' }); |
| 103 | + return baseDir; |
| 104 | +} |
| 105 | + |
| 106 | +function cleanupBaseSnapshot(baseDir) { |
| 107 | + try { |
| 108 | + execFileSync('git', ['worktree', 'remove', '--force', baseDir], { cwd: REPO_ROOT, stdio: 'ignore' }); |
| 109 | + } catch { |
| 110 | + rmSync(baseDir, { recursive: true, force: true }); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function writeResult(result) { |
| 115 | + writeFileSync(RESULT_PATH, JSON.stringify(result, null, 2)); |
| 116 | +} |
| 117 | + |
| 118 | +const changed = getChangedAgentDocs(); |
| 119 | +if (changed.length === 0) { |
| 120 | + console.log('No agent-doc files changed; gate is a no-op.'); |
| 121 | + writeResult({ blocking: false, newFindings: [], changed: [] }); |
| 122 | + process.exit(0); |
| 123 | +} |
| 124 | + |
| 125 | +console.log(`Changed agent-doc files: ${changed.join(', ')}`); |
| 126 | + |
| 127 | +const headScan = runL1Scan(REPO_ROOT); |
| 128 | +const headFindings = highConfidenceFindings(headScan); |
| 129 | + |
| 130 | +let baseFindings = []; |
| 131 | +let baseDir = null; |
| 132 | +try { |
| 133 | + if (DRY_RUN) { |
| 134 | + const baseFromFlag = process.argv.indexOf('--base-root'); |
| 135 | + if (baseFromFlag >= 0 && process.argv[baseFromFlag + 1]) { |
| 136 | + baseFindings = highConfidenceFindings(runL1Scan(resolve(process.argv[baseFromFlag + 1]))); |
| 137 | + } |
| 138 | + } else { |
| 139 | + baseDir = prepareBaseSnapshot(); |
| 140 | + baseFindings = highConfidenceFindings(runL1Scan(baseDir)); |
| 141 | + } |
| 142 | +} finally { |
| 143 | + if (baseDir) cleanupBaseSnapshot(baseDir); |
| 144 | +} |
| 145 | + |
| 146 | +const baseIds = new Set(baseFindings.map((f) => f.id)); |
| 147 | +const newFindings = headFindings.filter((f) => !baseIds.has(f.id)); |
| 148 | + |
| 149 | +const changedSet = new Set(changed); |
| 150 | +const dirSet = changedPairDirs(changed); |
| 151 | + |
| 152 | +const scoped = newFindings.filter((f) => { |
| 153 | + if (f.type === 'pair') return dirSet.has(f.dir); |
| 154 | + return changedSet.has(f.relPath); |
| 155 | +}); |
| 156 | + |
| 157 | +const result = { blocking: scoped.length > 0, newFindings: scoped, changed }; |
| 158 | +writeResult(result); |
| 159 | + |
| 160 | +if (result.blocking) { |
| 161 | + console.log('\nBlocking — new high-confidence findings introduced by this PR:'); |
| 162 | + for (const f of scoped) { |
| 163 | + if (f.type === 'broken-import') console.log(` - broken @import in ${f.relPath}: ${f.importPath}`); |
| 164 | + else if (f.type === 'broken-symlink') console.log(` - broken symlink ${f.relPath} → ${f.target}`); |
| 165 | + else if (f.type === 'pair') console.log(` - pair ${f.dir} ${f.classification}: ${f.detail}`); |
| 166 | + } |
| 167 | + console.log(`\nWrote ${RESULT_PATH}`); |
| 168 | + process.exit(1); |
| 169 | +} |
| 170 | + |
| 171 | +console.log('No new high-confidence findings introduced by this PR. Gate passes.'); |
| 172 | +process.exit(0); |
0 commit comments