bada-terminal / src /cli /policy-guard-cli.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
6.14 kB
/**
* policy-guard-cli.js โ€” BADA Terminal ์ž๊ฐ€ ์น˜์œ  Pre-Commit & ์ •์ฑ… ๊ฐ€๋“œ CLI
*
* ์„ค๋ช… ๋ฌธ์„œ: /docs/specs/bada_trio_actionable_spec.md (SPEC-ACT-001 v1.0.0)
* ๊ด€๋ จ ๊ทœ์•ฝ: AGENTS.md, Zero-Trust DLP, Pre-Commit Hook Security
*
* ์ฃผ์š” ๊ธฐ๋Šฅ:
* 1. Git Staged Diff ๊ฒ€์‚ฌ: ์ปค๋ฐ‹ ์ „ staged ์ƒํƒœ์˜ ์ฝ”๋“œ diff๋ฅผ ValueAnchorEvaluator๋กœ ์ •๋ฐ€ ๋ถ„์„
* 2. ์ž๊ฐ€ ์น˜์œ  ํ”ผ๋“œ๋ฐฑ ์ œ๊ณต: ์œ„๋ฐ˜ ๋ฐœ์ƒ ์‹œ ๋‹จ์ˆœ ์—๋Ÿฌ ์ข…๋ฃŒ๊ฐ€ ์•„๋‹Œ ๊ตฌ์ฒด์ ์ธ ๊ต์ • ๊ฐ€์ด๋“œ ์ถœ๋ ฅ
* 3. ์›ํด๋ฆญ Git Hook ์„ค์น˜/์ œ๊ฑฐ: cross-platform ํ˜ธํ™˜ sh ๊ธฐ๋ฐ˜ pre-commit ํ›… ์ž๋™ ์ƒ์„ฑ
* 4. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ CLI ์–‘์šฉ ์ง€์›
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const valueAnchorEvaluator = require('../orchestration/value-anchor-evaluator');
/**
* Git staged ์ƒํƒœ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ˆ˜์ง‘ํ•˜์—ฌ ๊ฐ€์น˜ ์•ต์ปค ์ •์ฑ…์„ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
*
* @param {Object} [options]
* @param {string} [options.cwd=process.cwd()] - ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ
* @returns {Object} ํ‰๊ฐ€ ๊ฒฐ๊ณผ ๊ฐ์ฒด { passed, hardBlocked, score, violations, feedbackPrompt }
*/
function evaluateStaged(options = {}) {
const cwd = options.cwd || process.cwd();
// 1. Staged๋œ ํŒŒ์ผ ๋ชฉ๋ก ์ถ”์ถœ (git diff --cached --name-only)
let modifiedFiles = [];
try {
const filesOutput = execSync('git diff --cached --name-only', {
cwd,
encoding: 'utf8',
timeout: 10000,
maxBuffer: 10 * 1024 * 1024
});
modifiedFiles = filesOutput.split('\n').map(f => f.trim()).filter(Boolean);
} catch (err) {
// git ์ €์žฅ์†Œ๊ฐ€ ์•„๋‹ˆ๊ฑฐ๋‚˜ staged๋œ ๋‚ด์šฉ์ด ์—†๋Š” ๊ฒฝ์šฐ
return {
passed: true,
hardBlocked: false,
score: 100,
hardViolations: [],
softViolations: [],
feedbackPrompt: null,
message: 'Git ์ €์žฅ์†Œ๊ฐ€ ์•„๋‹ˆ๊ฑฐ๋‚˜ staged ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์—†์Šต๋‹ˆ๋‹ค.'
};
}
if (modifiedFiles.length === 0) {
return {
passed: true,
hardBlocked: false,
score: 100,
hardViolations: [],
softViolations: [],
feedbackPrompt: null,
message: 'Staged๋œ ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค.'
};
}
// 2. Staged๋œ ์ฝ”๋“œ ์ „์ฒด diff ์ˆ˜์ง‘
let diff = '';
let diffWarning = null;
try {
diff = execSync('git diff --cached', {
cwd,
encoding: 'utf8',
timeout: 15000,
maxBuffer: 15 * 1024 * 1024 // 15MB ๋ฒ„ํผ (๋Œ€ํ˜• ๋ณ€๊ฒฝ ๋Œ€๋น„)
});
} catch (err) {
// diff ์ˆ˜์ง‘ ์‹คํŒจ ์‹œ ํŒŒ์ผ ๊ฒฝ๋กœ ๊ธฐ๋ฐ˜ ๊ฒ€์‚ฌ๋งŒ ์ˆ˜ํ–‰๋จ์„ ๊ฒฝ๊ณ 
diffWarning = `[๊ฒฝ๊ณ ] git diff --cached ์ˆ˜์ง‘ ์‹คํŒจ (${err.message?.slice(0, 80)}). ์ฝ”๋“œ ๋‚ด์šฉ(์‹œํฌ๋ฆฟ ๋“ฑ) ๊ฒ€์‚ฌ๊ฐ€ ์ƒ๋žต๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.`;
diff = '';
}
// 3. ValueAnchorEvaluator ์‹คํ–‰
const result = valueAnchorEvaluator.evaluate({
diff,
modifiedFiles,
commands: []
});
return {
...result,
modifiedFiles,
diffWarning
};
}
/**
* .git/hooks/pre-commit ํ›…์„ ์›ํด๋ฆญ์œผ๋กœ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
* ํฌ๋กœ์Šค ํ”Œ๋žซํผ(macOS, Linux, Windows Git Bash) ํ˜ธํ™˜ sh ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ.
*
* @param {string} [targetGitDir] - .git ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ (์ƒ๋žต ์‹œ ์ž๋™ ๊ฐ์ง€)
* @returns {boolean} ์„ฑ๊ณต ์—ฌ๋ถ€
*/
function installHook(targetGitDir = null) {
const gitDir = targetGitDir || path.resolve(process.cwd(), '.git');
const hooksDir = path.join(gitDir, 'hooks');
if (!fs.existsSync(hooksDir)) {
fs.mkdirSync(hooksDir, { recursive: true });
}
const hookPath = path.join(hooksDir, 'pre-commit');
const scriptContent = `#!/bin/sh
# BADA Terminal Value Anchor & Policy Guard Pre-Commit Hook
# Auto-generated by BADA policy-guard-cli
# ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ๋กœ ์ด๋™ (git hook์€ GIT_DIR ๊ธฐ์ค€์œผ๋กœ ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Œ)
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"
if [ -n "$REPO_ROOT" ]; then
cd "$REPO_ROOT" || exit 1
fi
node src/cli/policy-guard-cli.js --staged
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo ""
echo "โŒ [BADA-GUARD] ์ •์ฑ… ์œ„๋ฐ˜์œผ๋กœ ์ธํ•ด ์ปค๋ฐ‹์ด ์ฐจ๋‹จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
echo "์œ„ ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•œ ๋’ค ๋‹ค์‹œ ์ปค๋ฐ‹ํ•˜์‹ญ์‹œ์˜ค."
exit 1
fi
exit 0
`;
fs.writeFileSync(hookPath, scriptContent, { encoding: 'utf8', mode: 0o755 });
// POSIX ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ ๊ถŒํ•œ ๋ถ€์—ฌ
try {
fs.chmodSync(hookPath, 0o755);
} catch (e) {
// Windows์—์„œ๋Š” ๋ฌด์‹œ
}
return true;
}
/**
* .git/hooks/pre-commit ํ›…์„ ์•ˆ์ „ํ•˜๊ฒŒ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
*
* @param {string} [targetGitDir]
* @returns {boolean}
*/
function uninstallHook(targetGitDir = null) {
const gitDir = targetGitDir || path.resolve(process.cwd(), '.git');
const hookPath = path.join(gitDir, 'hooks', 'pre-commit');
if (fs.existsSync(hookPath)) {
fs.unlinkSync(hookPath);
return true;
}
return false;
}
/**
* CLI ๋ฉ”์ธ ์ง„์ž…์ 
*/
function cliMain(args = process.argv.slice(2)) {
if (args.includes('--install-hook')) {
const ok = installHook();
console.log(ok ? 'โœ… BADA Policy Guard Git Pre-Commit Hook ์„ค์น˜ ์™„๋ฃŒ!' : 'โŒ ์„ค์น˜ ์‹คํŒจ');
process.exit(ok ? 0 : 1);
}
if (args.includes('--uninstall-hook')) {
const ok = uninstallHook();
console.log(ok ? 'โœ… BADA Policy Guard Git Pre-Commit Hook ์ œ๊ฑฐ ์™„๋ฃŒ!' : 'โ„น๏ธ ์„ค์น˜๋œ ํ›…์ด ์—†์Šต๋‹ˆ๋‹ค.');
process.exit(0);
}
if (args.includes('--staged') || args.length === 0) {
console.log('๐Ÿ›ก๏ธ [BADA Policy Guard] Git Staged ์ฝ”๋“œ ์ •์ฑ… ๊ฒ€์‚ฌ ์ค‘...');
const result = evaluateStaged();
if (result.passed) {
console.log(`โœ… [BADA-GUARD PASS] ๋ชจ๋“  ๋ณด์•ˆ ๋ฐ ๊ฐœ๋ฐœ ์ •์ฑ… ํ†ต๊ณผ! (์ ์ˆ˜: ${result.score}/100)`);
process.exit(0);
} else {
console.error(`\n๐Ÿšจ [BADA-GUARD BLOCKED] ์น˜๋ช…์  ์ •์ฑ… ์œ„๋ฐ˜์ด ๊ฐ์ง€๋˜์—ˆ์Šต๋‹ˆ๋‹ค! (์ ์ˆ˜: ${result.score}/100)`);
if (result.feedbackPrompt) {
console.error('\n' + result.feedbackPrompt + '\n');
}
process.exit(1);
}
}
}
// CLI ์ง์ ‘ ์‹คํ–‰ ์‹œ ์‹คํ–‰
if (require.main === module) {
cliMain();
}
module.exports = {
evaluateStaged,
installHook,
uninstallHook,
cliMain
};