/** * Sandboxed Python for the assistant and generation checks. * * Python runs in Pyodide (CPython compiled to WebAssembly) inside a separate Node process * (`server/python-runner.mjs`) started with Node's permission model: it may read only * Pyodide's own files and its own script, cannot write files, start processes, use workers * or load native addons, and gets an empty environment. Inside Python the bridges back to * JavaScript are removed, so there is no network or host file access, only the standard * library (math, fractions, decimal, statistics, re, json, datetime, itertools...). * * One warm process serves runs one at a time. A run that exceeds its time limit kills the * process; the next run starts a fresh one. The process is also recycled every so often so * module-level state cannot pile up across runs. */ import { fork, type ChildProcess } from 'node:child_process'; import fs from 'node:fs'; import { createRequire } from 'node:module'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; export interface PythonResult { ok: boolean; stdout: string; stderr: string; /** repr-like text of the last expression's value, when there is one. */ result: string | null; error: string | null; ms: number; timedOut?: boolean; } export const PYTHON_TIMEOUT_MS = 10_000; const MAX_TIMEOUT_MS = 30_000; const START_TIMEOUT_MS = 60_000; const RECYCLE_AFTER = 50; const MAX_CODE = 50_000; function runnerPath(): string { const here = path.dirname(fileURLToPath(import.meta.url)); // Bundled: dist/server/main.js next to python-runner.mjs. Source: server/ai/sandbox.ts. for (const candidate of [path.join(here, 'python-runner.mjs'), path.join(here, '..', 'python-runner.mjs')]) { if (fs.existsSync(candidate)) return candidate; } throw new Error('python-runner.mjs is missing from this installation'); } function pyodideEntry(): string { const require = createRequire(import.meta.url); return path.join(path.dirname(require.resolve('pyodide/package.json')), 'pyodide.mjs'); } interface Pending { resolve: (r: PythonResult) => void; timer: ReturnType; } let child: ChildProcess | null = null; let ready: Promise | null = null; let runs = 0; let nextId = 1; const pending = new Map(); /** Runs are serialised: Pyodide is single-threaded and a shared process keeps memory flat. */ let queue: Promise = Promise.resolve(); function kill(reason: string): void { const c = child; child = null; ready = null; runs = 0; if (c && c.exitCode === null) c.kill('SIGKILL'); for (const [id, p] of pending) { clearTimeout(p.timer); p.resolve({ ok: false, stdout: '', stderr: '', result: null, error: reason, ms: 0 }); pending.delete(id); } } function start(): Promise { if (ready) return ready; ready = new Promise((resolve, reject) => { const entry = pyodideEntry(); const script = runnerPath(); const proc = fork(script, [entry], { // An empty environment: nothing secret to find even if the sandbox were escaped. env: {}, execArgv: ['--permission', `--allow-fs-read=${path.dirname(entry)}`, `--allow-fs-read=${script}`, '--max-old-space-size=256'], stdio: ['ignore', 'ignore', 'pipe', 'ipc'], serialization: 'json', }); let errText = ''; proc.stderr?.on('data', (d) => { if (errText.length < 4000) errText += String(d); }); const startTimer = setTimeout(() => { proc.kill('SIGKILL'); reject(new Error('The Python sandbox took too long to start.')); }, START_TIMEOUT_MS); let started = false; proc.on('message', (msg: any) => { if (msg?.ready) { started = true; clearTimeout(startTimer); resolve(proc); return; } const p = pending.get(msg?.id); if (!p) return; clearTimeout(p.timer); pending.delete(msg.id); const { id: _id, ...rest } = msg; p.resolve(rest as PythonResult); }); proc.on('exit', (code) => { clearTimeout(startTimer); if (!started) reject(new Error(`The Python sandbox could not start (exit ${code}). ${errText.trim().split('\n').slice(-3).join(' ')}`.trim())); if (child !== proc) return; // already replaced (killed after a timeout) child = null; ready = null; for (const [id, p] of pending) { clearTimeout(p.timer); p.resolve({ ok: false, stdout: '', stderr: '', result: null, error: 'The Python sandbox stopped unexpectedly.', ms: 0 }); pending.delete(id); } }); child = proc; }).catch((err) => { child = null; ready = null; throw err; }); return ready; } async function runOnce(code: string, timeoutMs: number): Promise { const proc = await start(); const id = nextId++; runs += 1; const result = await new Promise((resolve) => { const timer = setTimeout(() => { pending.delete(id); resolve({ ok: false, stdout: '', stderr: '', result: null, error: `Stopped after ${Math.round(timeoutMs / 1000)} s (time limit). Avoid long loops; compute directly.`, ms: timeoutMs, timedOut: true }); kill('The previous run timed out.'); }, timeoutMs); pending.set(id, { resolve, timer }); proc.send({ id, code }); }); if (runs >= RECYCLE_AFTER) kill('recycled'); return result; } /** Runs Python 3 code in the sandbox. Never throws for problems in the code itself. */ export function runPython(code: string, opts: { timeoutMs?: number } = {}): Promise { if (code.length > MAX_CODE) return Promise.resolve({ ok: false, stdout: '', stderr: '', result: null, error: `The code is too long (limit ${MAX_CODE} characters).`, ms: 0 }); const timeoutMs = Math.min(Math.max(opts.timeoutMs ?? PYTHON_TIMEOUT_MS, 1000), MAX_TIMEOUT_MS); const job = queue.then(() => runOnce(code, timeoutMs)); queue = job.catch(() => undefined); return job; } /** Stops the sandbox process (shutdown, tests). */ export function stopPython(): void { kill('The Python sandbox was stopped.'); }