Download src/harness-executor.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 15.5 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/harness-executor.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/harness-executor.js
-
curl -L -o harness-executor.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/harness-executor.js
15.5 kB
| /** | |
| * ์ค๋ช ๋ฌธ์: /docs/specs/harness-validation.md (v1.5.0) | |
| * ํ๋ค์ค(์ธ๋ถ ๊ฒ์ฆ ์ปค๋งจ๋) ์คํ/ํ์ /๊ธฐ๋ก์ ๊ณตํต ๊ตฌํ. | |
| * | |
| * ๊ธฐ์กด์๋ ์๋ ๋ ๊ณณ์ ํ๋ค์ค ์คํ ๋ก์ง์ด ์ค๋ณต ๊ตฌํ๋์ด ์์๋ค. | |
| * 1) src/agent-runner.js runHarnessNode() โ ์ํฌํ๋ก์ฐ ๋ ธ๋๋ก์์ ํ๋ค์ค ์คํ(์ฌ์๋/๋กค๋ฐฑ/DB/Mories ํฌํจ) | |
| * 2) src/loop-controller.js harness_pass โ ๋ฃจํ goal ํ๊ฐ์ฉ ํ๋ค์ค ์คํ | |
| * WHY: ์ ธ ์์ ์ฑ ๊ฒ์ฌ ๋ชฉ๋ก, ReDoS ๋ฐฉ์ง ํจํด, success_criteria ํ์ , ์ปค๋งจ๋ ์คํ+ํ์์์์ด | |
| * ์๊ฐ์ด ์ง๋๋ฉฐ ๋ ๊ณณ์์ ์ด๊ธ๋๋(drift) ์ํ์ ์์ ๊ธฐ ์ํด ํต์ฌ ๋ก์ง์ ์ฌ๊ธฐ๋ก ์ถ์ถํ๋ค. | |
| * ์ฌ์๋/๋กค๋ฐฑ/์ํฌํ๋ก์ฐ ๋ ธ๋ ์ํ ๊ด๋ฆฌ์ฒ๋ผ ํธ์ถ ๋งฅ๋ฝ์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ถ๋ถ์ ๊ฐ ํธ์ถ๋ถ์ ๋จ๊ธด๋ค. | |
| */ | |
| const { runHarnessCommand, checkCancellation } = require('./utils/harness-command'); | |
| const { isSafeShellCommand } = require('./utils/shell-helper'); | |
| const { parseStages, configurationError } = require('./config/harness-configuration'); | |
| const { isHarnessStop } = require('./services/harness-stop-error'); | |
| // WHY: C-4 ์ ธ ์ธ์ ์ ๋ฐฉ์ด ๋ธ๋๋ฆฌ์คํธ โ ํ๊ดด์ /์๊ฒฉ์คํ ์ปค๋งจ๋ ํจํด ์ฐจ๋จ. ๋ ํธ์ถ๋ถ์ ๋จ์ผ ์ถ์ฒ. | |
| const UNSAFE_COMMAND_PATTERNS = [ | |
| /\brm\s+-[rf]+\b/, | |
| /\bmkfs\b/, | |
| /\bdd\s+if=\b/, | |
| /\bshutdown\b/, | |
| /\breboot\b/, | |
| /\bformat\s+[a-z]:/i, | |
| /del\s+\/s\b/i, | |
| /\$\([\s\S]*\)/, // Subshell expansion: $(...) | |
| /`[\s\S]*`/, // Backtick expansion: `...` | |
| ]; | |
| // WHY: C-3 success_criteria ์ ๊ท์์ ReDoS ์ทจ์ฝ ํจํด ์ ์ ์ฐจ๋จ ๋ชฉ๋ก (agent-runner ์๋ณธ). ๋ณ๊ฒฝ ๊ธ์ง. | |
| const UNSAFE_REGEX_PATTERNS = [ | |
| /\([^)]+[+*]\)[+*]/, // (a+)+ or (a*)* | |
| /\[[^\]]+\][+*]\?[+*]/, // [a-z]+?+ | |
| /\((?:[^)]+\|)+[^)]+\)[+*]/, // (a|b|a)* | |
| ]; | |
| // WHY: loop-controller ์๋ณธ isSafeRegex์ dangerousPatterns. agent-runner์ UNSAFE_REGEX_PATTERNS์๋ | |
| // ๋ณ๊ฐ์ ๊ฒ์ฌ ์ธํธ์๋ค(๊ธธ์ด ์ ํ + ์ฐ์ quantifier ์ฐจ๋จ). ํตํฉ ์ ์ด ์ธํธ๊ฐ ๋๋ฝ๋์ด | |
| // "x++", "a.*+b" ๊ฐ์ ํจํด์ด loop-controller์์ FAIL์ด์ด์ผ ํ๋๋ฐ PASS๋ก ๋ค์งํ๋ ํ๊ท๊ฐ ์์๋ค. | |
| // ๋ฆฌํฉํฐ ์ loop-controller ๋์์ ๊ทธ๋๋ก ๋ณต์ํ๊ธฐ ์ํด ๋ณ๋ ์ธํธ๋ก ๋ณด์กดํ๋ค. | |
| const LOOP_UNSAFE_REGEX_PATTERNS = [ | |
| /\([^)]*[*+?][^)]*\)[*+?]/, // ๊ดํธ ์ํ์ ์ค๋ณต ์๋์ | |
| /[*+?]{2,}/, // *+, ++์ ๊ฐ์ ์ฐ์ ์๋์ | |
| ]; | |
| const LOOP_REGEX_MAX_LENGTH = 150; | |
| /** | |
| * ํ๋ค์ค ๊ฒ์ฆ ์ปค๋งจ๋์ ์ ธ ์์ ์ฑ์ ๊ฒ์ฌํ๋ค. | |
| * @param {string} command - ๊ฒ์ฌํ ์ปค๋งจ๋ | |
| * @param {Object} [opts] | |
| * @param {boolean} [opts.applyBlacklist=true] - UNSAFE_COMMAND_PATTERNS ๋ธ๋๋ฆฌ์คํธ๊น์ง ์ ์ฉํ ์ง ์ฌ๋ถ. | |
| * - agent-runner(์ํฌํ๋ก์ฐ ๋ ธ๋): true (๋ธ๋๋ฆฌ์คํธ + isSafeShellCommand) | |
| * - loop-controller(goal ํ๊ฐ): false (isSafeShellCommand ๋ง) โ ๊ธฐ์กด ๋์ ๋ณด์กด | |
| * @returns {{ safe: boolean, reason: string|null }} | |
| */ | |
| function checkCommandSafety(command, opts = {}) { | |
| const { applyBlacklist = true } = opts; | |
| if (!command) return { safe: true, reason: null }; | |
| if (applyBlacklist) { | |
| if (UNSAFE_COMMAND_PATTERNS.some(p => p.test(command.toLowerCase())) || !isSafeShellCommand(command)) { | |
| return { safe: false, reason: `Unsafe command pattern detected in validation_command: ${command}` }; | |
| } | |
| return { safe: true, reason: null }; | |
| } | |
| if (!isSafeShellCommand(command)) { | |
| return { safe: false, reason: `Unsafe harness command blocked: "${command}"` }; | |
| } | |
| return { safe: true, reason: null }; | |
| } | |
| /** | |
| * harness.success_criteria(์ ๊ท์ ๋งค์นญ ๋ฑ)๋ฅผ ํ๊ฐํ๋ค. | |
| * WHY: exitCode===0 ์ดํ ์ถ๊ฐ success_criteria.regex_match ๋งค์นญ์ ํ์ . ReDoS ๋ฐฉ์ง ํ ์ ๊ท์ ํ ์คํธ. | |
| * @param {string|Object|null} successCriteria - harness.success_criteria (JSON ๋ฌธ์์ด ๋๋ ๊ฐ์ฒด) | |
| * @param {string} matchText - ์ ๊ท์์ ๋งค์นญํ ๋์ ํ ์คํธ | |
| * - agent-runner: stdout + '\n' + stderr (fullLog) | |
| * - loop-controller: stdout ๋ง โ ๊ธฐ์กด ๋์ ๋ณด์กด | |
| * @param {Object} [opts] | |
| * @param {'agent'|'loop'} [opts.regexSafetyMode='agent'] - ์ ๊ท์ ์ฌ์ ๊ฒ์ฌ ์ธํธ. | |
| * JSONยท์ ๊ท์ ๊ตฌ๋ฌธ ์ค๋ฅ์ ์ํ ํจํด์ ๋ชจ๋ ์คํ ๊ฒฝ๋ก์์ ์คํจ๋ก ํ์ ํ๋ค. | |
| * @returns {{ pass: boolean, redosDetected: boolean, error: string|null, regexMatch: string|null }} | |
| */ | |
| function evaluateSuccessCriteria(successCriteria, matchText, opts = {}) { | |
| const { regexSafetyMode = 'agent' } = opts; | |
| if (!successCriteria) { | |
| return { pass: true, redosDetected: false, error: null, regexMatch: null }; | |
| } | |
| try { | |
| const criteria = typeof successCriteria === 'string' ? JSON.parse(successCriteria) : successCriteria; | |
| if (!criteria || typeof criteria !== 'object' || Array.isArray(criteria)) { | |
| throw new Error('Success criteria must be an object'); | |
| } | |
| if (Object.prototype.hasOwnProperty.call(criteria, 'regex_match')) { | |
| if (typeof criteria.regex_match !== 'string') throw new Error('regex_match must be a string'); | |
| const regexMatch = criteria.regex_match; | |
| const isUnsafe = regexSafetyMode === 'loop' | |
| ? (typeof regexMatch !== 'string' || regexMatch.length > LOOP_REGEX_MAX_LENGTH || LOOP_UNSAFE_REGEX_PATTERNS.some(p => p.test(regexMatch))) | |
| : UNSAFE_REGEX_PATTERNS.some(p => p.test(regexMatch)); | |
| if (isUnsafe) { | |
| // ์ํ ํจํด์ ์คํํ์ง ์๊ณ ๊ฒ์ฆ ์คํจ๋ก ๋ฐํํ๋ค. | |
| return { | |
| pass: false, | |
| redosDetected: true, | |
| error: `๋ณด์ ์ํ: ReDoS ์ทจ์ฝ์ ์ด ์์ฌ๋๋ ์ ๊ท์ ํจํด์ ๋๋ค: ${regexMatch}`, | |
| regexMatch | |
| }; | |
| } | |
| const regex = new RegExp(regexMatch); | |
| const matched = regex.test(matchText); | |
| return { | |
| pass: matched, | |
| redosDetected: false, | |
| error: matched ? null : `output did not match regex "${regexMatch}"`, | |
| regexMatch | |
| }; | |
| } | |
| return { pass: true, redosDetected: false, error: null, regexMatch: null }; | |
| } catch (e) { | |
| // ๊ฒ์ฆ ์ค์ ์ ํด์ํ์ง ๋ชปํ ๊ฒฐ๊ณผ๋ ์ฑ๊ณต ์ฆ๊ฑฐ๊ฐ ์๋๋ค. | |
| return { pass: false, redosDetected: false, error: e.message, regexMatch: null }; | |
| } | |
| } | |
| // ์คํ ์๋ช ์ utils/harness-command์ ์์ํ๋ค. docs/specs/harness-validation.md v1.5.0 | |
| /** | |
| * ํ๋ค์ค ์คํ ๊ฒฐ๊ณผ๋ฅผ Mories ๋ถ์ API๋ก fire-and-forget ๋๊ธฐํํ๋ค. | |
| * WHY: ํ๋ค์ค ๋๋ฉ์ธ ๋ถ์ํจ๊ณผ. ๊ธฐ์กด agent-runner.js์์ ์ด๊ณณ์ผ๋ก ์ด๊ด(๋จ์ผ ์ถ์ฒ). | |
| */ | |
| async function syncHarnessRunToMories(harnessId, isSuccess, executionTimeMs, resultSummary, toolChainStr, logStream) { | |
| let toolChain = []; | |
| if (toolChainStr) { | |
| try { | |
| toolChain = typeof toolChainStr === 'string' ? JSON.parse(toolChainStr) : toolChainStr; | |
| } catch { | |
| toolChain = []; | |
| } | |
| } | |
| const result = await require('./services/mories-client').recordHarnessExecution(harnessId, { | |
| success: isSuccess, | |
| execution_time_ms: executionTimeMs, | |
| result_summary: resultSummary, | |
| new_tool_chain: toolChain | |
| }, { timeoutMs: 5000 }); | |
| if (logStream && logStream.writable) { | |
| logStream.write(result.synced | |
| ? `[Harness] Mories API Sync completed successfully.\n` | |
| : `[Harness] Mories API Sync failed: ${result.error || result.httpStatus}\n`); | |
| } | |
| return result; | |
| } | |
| /** | |
| * ๋ค๋จ๊ณ ํ๋ค์ค ์คํ ์ด์ง๋ฅผ ์์๋๋ก ์คํํ๋ ๊ฒ์ดํธ(VHK verify: tsc/lint/test/build ์์ฐจ ๊ฒ์ดํธ ๊ฐ๋ ์ฐธ๊ณ ). | |
| * WHY: ๋จ์ผ validation_command๋ง ํ์ ํ๋ ํ๋ค์ค๋ฅผ ์์ ํ์ฅํ๋ค(ํธ์ถ๋ถ๋ stages๊ฐ ์์ ๋๋ง ์ด ๊ฒฝ๋ก๋ฅผ ํ๋ค). | |
| * - required:true stage ์คํจ โ ์ฆ์ ์ค๋จ(์กฐ๊ธฐ ์ข ๋ฃ), ์ดํ stage๋ 'skipped'๋ก ๊ธฐ๋ก. | |
| * - required:false stage ์คํจ โ ๊ณ์ ์งํ(overall ํ์ ์ ์ํฅ ์์). | |
| * - ์ทจ์(shouldCancel) ๊ฐ์ง โ ์ ์ฒด ์ค๋จ, ์ดํ stage๋ 'skipped'. | |
| * ๊ฐ stage ์ปค๋งจ๋๋ checkCommandSafety + runHarnessCommand(๋จ์ผ ์ถ์ฒ)๋ก ์คํํ๋ค. | |
| * @param {Object} params | |
| * @param {Array<{name,command,required}>} params.stages - parseStages๋ก ์ ๊ทํ๋ stage ๋ฐฐ์ด | |
| * @param {string} params.cwd | |
| * @param {number} [params.timeoutMs=60000] - stage ํ๋๋น ํ์์์ | |
| * @param {Object} [params.env] | |
| * @param {AbortSignal} [params.signal] - ์คํ ์ ์ฒด์ ์ทจ์ ์ ํธ | |
| * @param {number} [params.maxBufferBytes=2097152] - ๋จ๊ณ๋ณ stdout+stderr ์์ ๋ฐ์ดํธ ์ํ | |
| * @param {function(string):void} [params.onData] | |
| * @param {function():boolean} [params.shouldCancel] | |
| * @param {function(ChildProcess):void} [params.registerProcess] | |
| * @param {function(ChildProcess):void} [params.unregisterProcess] | |
| * @param {'powershell'|'cmd'} [params.windowsShell='powershell'] | |
| * @param {string} [params.errorPrefix=''] | |
| * @param {boolean} [params.applyBlacklist=true] - ๊ฐ stage ์์ ์ฑ ๊ฒ์ฌ์ ๋ธ๋๋ฆฌ์คํธ ์ ์ฉ ์ฌ๋ถ | |
| * - agent-runner: true, loop-controller: false (๊ฐ ํธ์ถ๋ถ์ ๊ธฐ์กด ๋จ์ผ-์ปค๋งจ๋ ์ ์ฑ ๊ณผ ๋์ผํ๊ฒ ๋ง์ถ๋ค) | |
| * @returns {Promise<{ | |
| * success: boolean, | |
| * failedStage: string|null, | |
| * stages: Array<{name:string, command:string, required:boolean, status:('passed'|'failed'|'skipped'), exitCode:(number|null), stdout:string, stderr:string, error:(string|null)}> | |
| * }>} | |
| * NOTE: ๋ฐํ ํํ๋ B2(harness_runs ๊ตฌ์กฐํ ๊ธฐ๋ก + ๋ฆฌ์ํธ ์์ฑ)์์ ๊ทธ๋๋ก ์ฌ์ฌ์ฉํ๊ธฐ ์ํ ์ค๊ณ๋ค. | |
| * (์ด ํจ์ ์์ฒด๋ DB ์ ์ฅ์ ํ์ง ์๋๋ค โ B2 ๋ฒ์.) | |
| */ | |
| async function runHarnessStages(params) { | |
| const { | |
| stages: rawStages, | |
| cwd, | |
| timeoutMs = 60000, | |
| env, | |
| signal, | |
| onData, | |
| shouldCancel, | |
| registerProcess, | |
| unregisterProcess, | |
| windowsShell = 'powershell', | |
| errorPrefix = '', | |
| maxBufferBytes, | |
| applyBlacklist = true | |
| } = params; | |
| // ์ง์ ํธ์ถ๋ ์ ์ฒด ์ค์ ์ ๋จผ์ ๊ฒ์ฌํ๋ค. ๋ค์ชฝ์ ์์ ๋จ๊ณ๊ฐ ๋ฐ๊ฒฌ๋๊ธฐ ์ ์ | |
| // ์์ชฝ ๋ช ๋ น์ ๋ถ์ ํจ๊ณผ๋ฅผ ์คํํ๊ฑฐ๋ ๋น ๋ชฉ๋ก์ ๊ฒ์ฆ ์ฑ๊ณต์ผ๋ก ๋ฐํํ์ง ์๋๋ค. | |
| const stages = parseStages(rawStages); | |
| if (!stages) throw configurationError('at least one stage is required'); | |
| const identity = stage => stage.type === 'model' | |
| ? { name: stage.name, type: 'model', agentProfileId: stage.agentProfileId, required: stage.required } | |
| : { name: stage.name, command: stage.command, required: stage.required }; | |
| // ๋ค์ชฝ ๋ชจ๋ธ์ ์์๋ ์ ํ์ ์์ชฝ ๋ช ๋ น ์คํ ์ดํ์ ๋ฐ๊ฒฌํ์ง ์๋๋ก ๋จผ์ ๊ณ ์ ํ๋ค. | |
| const modelProfiles = new Map(); | |
| try { | |
| if (stages.some(stage => stage.type === 'model')) await params.beforeModelSpawn?.(); | |
| if (stages.some(stage => stage.type === 'model') && !require('./services/runtime-usage-ledger').isBound(params.usageOwner)) { | |
| throw Object.assign(new Error('Model harness requires the original usage owner'), { code: 'USAGE_LEDGER_FAILED', retryable: false }); | |
| } | |
| for (const stage of stages) if (stage.type === 'model') { | |
| modelProfiles.set(stage, require('./services/harness-model-stage').resolveModelProfile(stage, cwd, env, { sourceCwd: params.modelWorkspace?.source })); | |
| } | |
| } catch (error) { | |
| error.harnessStop = true; | |
| error.harnessResult = { success: false, failedStage: stages.find(stage => stage.type === 'model').name, | |
| stages: stages.map(stage => ({ ...identity(stage), status: 'skipped', exitCode: null, stdout: '', stderr: '', error: null })) }; | |
| throw error; | |
| } | |
| const results = []; | |
| let overallSuccess = true; | |
| let failedStage = null; | |
| let aborted = false, fatalError; // ๋น์ฉ/์์ ์คํจ๋ ๋จ๊ณ ๊ธฐ๋ก์ ๋ชจ์ ๋ค ์ ์ค๋ฅ๋ก ๋ฐํํ๋ค. | |
| for (const stage of stages) { | |
| if (!aborted) { | |
| try { checkCancellation(signal, shouldCancel, errorPrefix); } | |
| catch (error) { | |
| overallSuccess = false; failedStage = stage.name; aborted = true; | |
| if (stage.type === 'model') { error.harnessStop = true; fatalError ||= error; } | |
| results.push({ ...identity(stage), status: 'skipped', exitCode: null, stdout: '', stderr: '', error: error.message }); | |
| continue; | |
| } | |
| } | |
| if (aborted) { | |
| results.push({ ...identity(stage), status: 'skipped', exitCode: null, stdout: '', stderr: '', error: null }); | |
| continue; | |
| } | |
| // ๊ฐ stage ์ปค๋งจ๋ ์์ ์ฑ ๊ฒ์ฌ โ ๋จ์ผ ์ถ์ฒ checkCommandSafety | |
| const safety = stage.type === 'model' ? { safe: true } : checkCommandSafety(stage.command, { applyBlacklist }); | |
| if (!safety.safe) { | |
| results.push({ name: stage.name, command: stage.command, required: stage.required, status: 'failed', exitCode: null, stdout: '', stderr: '', error: safety.reason }); | |
| if (stage.required) { overallSuccess = false; failedStage = stage.name; aborted = true; } | |
| continue; | |
| } | |
| let exitCode, stdout, stderr, error = null, modelReceipt; | |
| try { | |
| const r = stage.type === 'model' ? await require('./services/harness-model-stage').runModelStage({ | |
| ...params, stage, modelProfile: modelProfiles.get(stage), cwd, timeoutMs, env, signal, onData, shouldCancel, maxBufferBytes, | |
| registerProcess, unregisterProcess | |
| }) : await runHarnessCommand({ | |
| command: stage.command, cwd, timeoutMs, env, signal, onData, shouldCancel, maxBufferBytes, | |
| registerProcess, unregisterProcess, windowsShell, errorPrefix | |
| }); | |
| exitCode = r.exitCode; stdout = r.stdout; stderr = r.stderr; | |
| if (stage.type === 'model') modelReceipt = r.modelReceipt; | |
| } catch (cmdErr) { | |
| // ํ์์์/์ทจ์/spawn ์ค๋ฅ โ reject๋ Error์ ์ค๋ฆฐ ๋ถ๋ถ ์ถ๋ ฅ์ ๋ณด์กด(๋จ์ผ-์ปค๋งจ๋ ๊ฒฝ๋ก์ ๋์ผ ์ ์ฑ ) | |
| exitCode = -1; | |
| stdout = cmdErr.stdout || ''; | |
| stderr = (cmdErr.stderr || '') + '\nExecution Error: ' + cmdErr.message; | |
| error = cmdErr.message; | |
| if (stage.type === 'model' && ['HARNESS_CANCELLED', 'HARNESS_CONTROL', 'HARNESS_CONFIGURATION'].includes(cmdErr.code)) cmdErr.harnessStop = true; | |
| if (isHarnessStop(cmdErr)) { fatalError ||= cmdErr; overallSuccess = false; failedStage = stage.name; aborted = true; } | |
| if (['HARNESS_CANCELLED', 'HARNESS_CONTROL', 'HARNESS_CONFIGURATION'].includes(cmdErr.code)) { overallSuccess = false; failedStage = stage.name; aborted = true; } | |
| } | |
| const passed = exitCode === 0; | |
| results.push({ ...identity(stage), status: passed ? 'passed' : 'failed', exitCode, stdout, stderr, error, ...(modelReceipt ? { modelReceipt } : {}) }); | |
| // ์ทจ์ ๊ฐ์ง ์ ์ ์ฒด ์ค๋จ(runHarnessCommand๊ฐ ์ด๋ฏธ rejectํ์ ์ ์์ผ๋ ์์ ๋ง์ผ๋ก ์ฌํ์ธ) | |
| let cancelled = false; | |
| try { checkCancellation(signal, shouldCancel, errorPrefix); } catch { cancelled = true; } | |
| if (cancelled) { | |
| overallSuccess = false; | |
| if (!failedStage) failedStage = stage.name; | |
| aborted = true; | |
| continue; | |
| } | |
| // required stage ์คํจ โ ์กฐ๊ธฐ ์ข ๋ฃ. optional ์คํจ๋ ๊ณ์ ์งํ. | |
| if (!passed && stage.required) { overallSuccess = false; failedStage = stage.name; aborted = true; } | |
| } | |
| const result = { success: overallSuccess, failedStage, stages: results }; | |
| if (fatalError) { fatalError.harnessResult = result; throw fatalError; } | |
| return result; | |
| } | |
| module.exports = { | |
| UNSAFE_COMMAND_PATTERNS, | |
| UNSAFE_REGEX_PATTERNS, | |
| LOOP_UNSAFE_REGEX_PATTERNS, | |
| LOOP_REGEX_MAX_LENGTH, | |
| checkCommandSafety, | |
| evaluateSuccessCriteria, | |
| runHarnessCommand, | |
| parseStages, | |
| runHarnessStages, | |
| syncHarnessRunToMories | |
| }; | |