From a67a6c797787f55e4b398485e0902e886af5f553 Mon Sep 17 00:00:00 2001 From: kim Date: Sat, 17 Oct 2026 12:41:28 +0900 Subject: [PATCH] =?UTF-8?q?python=5Feval=20=EC=83=8C=EB=93=9C=EB=B0=95?= =?UTF-8?q?=EC=8A=A4:=20/tmp=EB=A5=BC=20=ED=98=B8=EC=B6=9C=20=EA=B0=84=20?= =?UTF-8?q?=EC=9C=A0=EC=A7=80(.sandbox-tmp=20=EB=B0=94=EC=9D=B8=EB=93=9C),?= =?UTF-8?q?=20URL=20=EB=9D=BC=EC=9D=B4=EB=B8=8C=EB=9F=AC=EB=A6=AC=20?= =?UTF-8?q?=EC=93=B0=EB=8A=94=20=EC=BD=94=EB=93=9C=EB=8A=94=20=EB=84=A4?= =?UTF-8?q?=ED=8A=B8=EC=9B=8C=ED=81=AC=20=EC=9E=90=EB=8F=99=20=ED=97=88?= =?UTF-8?q?=EC=9A=A9,=20=EB=9D=BC=EC=9A=B0=ED=8C=85=EC=9D=80=20Ollama=20pr?= =?UTF-8?q?ovider=EC=9D=BC=20=EB=95=8C=EB=A7=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- src/gateway/chat/model-routing.ts | 6 ++++++ src/tools/python.ts | 21 +++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/gateway/chat/model-routing.ts b/src/gateway/chat/model-routing.ts index 386cac9..29f367a 100644 --- a/src/gateway/chat/model-routing.ts +++ b/src/gateway/chat/model-routing.ts @@ -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) { diff --git a/src/tools/python.ts b/src/tools/python.ts index 45b48d2..feec03c 100644 --- a/src/tools/python.ts +++ b/src/tools/python.ts @@ -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); }