// 공개 Google 계측만을 위한 임시 환경. 사용자 설정·자격의 원문은 복사/출력하지 않는다. // 설명 문서: docs/specs/bong-native-speech-benchmark.md (v1.0.0) const fs = require('fs'), path = require('path'), crypto = require('crypto'); const MODEL = 'google/gemini-3.8-flash'; const RESPONSE = '(감정: 차분함; 속도: 보통; 어투: 정중하게) 작업 계획을 먼저 확인하겠습니다. 실행 전에 승인을 받겠습니다.'; function claimOutput(root, requested) { const scratch = fs.realpathSync(path.join(root, 'scratch')), output = path.resolve(root, requested); const parent = fs.realpathSync(path.dirname(output)); if (!output.startsWith(path.join(root, 'scratch') + path.sep) || (parent !== scratch && !parent.startsWith(scratch + path.sep))) throw Error('BENCH_OUTPUT'); fs.writeFileSync(output, '', { flag: 'wx', mode: 0o600 }); return output; } function isolatedNative(home, nativeHome) { const installed = path.join(process.env.XDG_CONFIG_HOME || path.join(nativeHome, '.config'), 'opencode'); const data = process.env.XDG_DATA_HOME || path.join(nativeHome, '.local/share'); const protectedFiles = [path.join(installed, 'opencode.json'), path.join(nativeHome, '.opencode/opencode.json'), path.join(data, 'opencode/auth.json'), path.join(nativeHome, '.claude/settings.json')]; const hash = file => fs.existsSync(file) ? crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex') : null; const before = protectedFiles.map(hash), configHome = path.join(home, '.config'), config = path.join(configHome, 'opencode'); fs.mkdirSync(config, { recursive: true }); const dependencies = {}, packages = {}; // 설치된 공개 JS 패키지 두 개만 재사용하여 빈 HOME에서 네이티브 자동 npm 준비를 피한다. for (const name of ['@opencode-ai/plugin', '@opencode-ai/sdk']) { const source = path.join(installed, 'node_modules', name), pkg = JSON.parse(fs.readFileSync(path.join(source, 'package.json'))); fs.cpSync(source, path.join(config, 'node_modules', name), { recursive: true }); dependencies[name] = pkg.version; packages['node_modules/' + name] = { version: pkg.version }; } fs.writeFileSync(path.join(config, 'package.json'), JSON.stringify({ private: true, dependencies }), { mode: 0o600 }); fs.writeFileSync(path.join(config, 'package-lock.json'), JSON.stringify({ lockfileVersion: 3, packages: { '': { dependencies }, ...packages } }), { mode: 0o600 }); const cwd = path.join(home, 'public-project'); fs.mkdirSync(cwd); fs.mkdirSync(path.join(cwd, '.git')); fs.mkdirSync(path.join(home, '.claude')); const userSettings = path.join(nativeHome, '.claude/settings.json'); const headroom = fs.existsSync(userSettings) ? JSON.parse(fs.readFileSync(userSettings)).headroom : undefined; // 현재 마스킹 설정만 격리 복제한다. 임의 regex를 없애서 조기 공개를 강제하지 않는다. fs.writeFileSync(path.join(home, '.claude/settings.json'), JSON.stringify(headroom ? { headroom } : {}), { mode: 0o600 }); const env = { PATH: process.env.PATH, HOME: home, XDG_CONFIG_HOME: configHome, XDG_DATA_HOME: data, XDG_STATE_HOME: path.join(home, '.state'), XDG_CACHE_HOME: path.join(home, '.cache'), OPENCODE_DISABLE_AUTOUPDATE: 'true', OPENCODE_DISABLE_MODELS_FETCH: 'true', OPENCODE_DISABLE_DEFAULT_PLUGINS: 'true', OPENCODE_DISABLE_EXTERNAL_SKILLS: 'true', OPENCODE_DISABLE_CLAUDE_CODE: 'true', OPENCODE_DISABLE_PROJECT_CONFIG: 'true', OPENCODE_CONFIG_CONTENT: JSON.stringify({ $schema: 'https://opencode.ai/config.json', autoupdate: false, share: 'disabled', plugin: [], instructions: [], skills: { paths: [] }, enabled_providers: ['google'], disabled_providers: [], model: MODEL, provider: { google: { models: { 'gemini-3.8-flash': { name: 'Gemini 3.8 Flash', limit: { context: 1048576, output: 1024 } } }, options: { baseURL: 'https://generativelanguage.googleapis.com/v1beta' } } } }) }; for (const key of ['SystemRoot', 'COMSPEC', 'PATHEXT', 'TEMP', 'TMP', 'LANG']) if (process.env[key]) env[key] = process.env[key]; if (process.platform === 'win32') env.USERPROFILE = home; return { cwd, env, configsUnchanged: () => protectedFiles.every((file, i) => hash(file) === before[i]) }; } module.exports = { isolatedNative, claimOutput, MODEL, RESPONSE };