python_eval 샌드박스: /tmp를 호출 간 유지(.sandbox-tmp 바인드), URL 라이브러리 쓰는 코드는 네트워크 자동 허용, 라우팅은 Ollama provider일 때만

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-10-17 12:41:28 +09:00
1 parent b9f8025e34
commit a67a6c7977
2 files changed
+23 -4

No files matched your search

+6
View File
@@ -47,6 +47,12 @@ export interface RouteDecision { model?: string; reason?: string }
export function decideRoute(message: string, opts: { skip?: boolean; hasImages?: boolean } = {}): RouteDecision {
const cfg = getRoutingConfig();
if (!cfg.enabled || !cfg.hard_model || opts.skip || opts.hasImages) return {};
// hard_model은 Ollama 모델 이름이다. 활성 provider가 Ollama 계열이 아니면(OpenAI/Anthropic/Gemini 등)
// 그 이름을 다른 백엔드에 보내면 모델 없음 오류가 나므로 라우팅하지 않는다.
try {
const prov = String(((getConfig().getConfig() as any)?.llm?.provider) || 'ollama');
if (prov !== 'ollama') return {};
} catch { /* 설정을 못 읽으면 기본(ollama)으로 간주 */ }
const text = String(message || '');
for (const p of HARD_PATTERNS) if (p.re.test(text)) return { model: cfg.hard_model, reason: p.reason };
for (const src of cfg.extra_patterns) {
+17 -4
View File
@@ -53,13 +53,26 @@ function sandboxAvailable(): boolean {
} catch { sandboxOk = false; }
return sandboxOk;
}
// 코드가 URL 라이브러리를 쓰면 네트워크를 연다. 실제 로그에서 작업실 채팅이 GitHub에서 STL을 내려받는 코드를
// 네트워크 인자 없이 쓰는 게 흔했고(모델은 network 인자를 모른다), 막으면 그 흐름이 전부 깨진다.
// 호스트 파일·키는 여전히 보이지 않는다. 명시적으로 network:false를 주면 항상 닫는다.
function wantsNetwork(code: string, flag: any): boolean {
if (flag === false || flag === 'false') return false;
if (flag === true || flag === 'true') return true;
return /\b(requests|urllib|urlopen|httpx|aiohttp|http\.client|wget|curl)\b|https?:\/\//i.test(code);
}
function sandboxArgs(workspacePath: string, code: string, name: string, network: boolean): string[] {
const tmpDir = path.join(workspacePath, '.sandbox-tmp');
try { fs.mkdirSync(tmpDir, { recursive: true }); } catch {}
const uid = typeof process.getuid === 'function' ? process.getuid() : 1000;
const gid = typeof process.getgid === 'function' ? process.getgid() : 1000;
return [
'run', '--rm', '--name', name, '-i',
'--network', network ? 'bridge' : 'none',
'--read-only', '--tmpfs', '/tmp:rw,size=256m',
// /tmp는 워크스페이스 안 .sandbox-tmp에 바인드한다: 모델이 한 호출에서 /tmp에 쓰고 다음 호출에서 읽는
// 패턴이 실측(2026-10 로그: /tmp/robot_arm_animation.gif)돼서, 호출마다 사라지는 tmpfs면 깨진다.
'--read-only', '-v', `${tmpDir}:/tmp`,
'--cap-drop', 'ALL', '--security-opt', 'no-new-privileges',
'--memory', '1g', '--cpus', '2', '--pids-limit', '128',
'-u', `${uid}:${gid}`,
@@ -123,7 +136,7 @@ export const pythonEvalTool = {
for (const entry of entries) {
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
const full = path.join(dir, entry.name);
if (entry.isDirectory()) { scanDir(full, rel); }
if (entry.isDirectory()) { if (entry.name !== '.sandbox-tmp') scanDir(full, rel); }
else if (entry.isFile()) { try { beforeFiles.add(`${rel}:${fs.statSync(full).mtimeMs}`); } catch {} }
}
}
@@ -166,7 +179,7 @@ export const pythonEvalTool = {
let settled = false;
const child = useSandbox
? spawn('docker', sandboxArgs(workspacePath, code, containerName, args?.network === true || args?.network === 'true'))
? spawn('docker', sandboxArgs(workspacePath, code, containerName, wantsNetwork(code, args?.network)))
: spawn('python3', ['-u', '-c', code], {
cwd: workspacePath,
env: { ...process.env, PYTHONDONTWRITEBYTECODE: '1', PYTHONUNBUFFERED: '1' },
@@ -211,7 +224,7 @@ export const pythonEvalTool = {
for (const entry of entries) {
const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
const full = path.join(dir, entry.name);
if (entry.isDirectory()) { scanDirAfter(full, rel); }
if (entry.isDirectory()) { if (entry.name !== '.sandbox-tmp') scanDirAfter(full, rel); }
else if (entry.isFile()) {
const key = `${rel}:${fs.statSync(full).mtimeMs}`;
if (!beforeFiles.has(key)) { newFiles.push(rel); }