/** * Worker thread for Python execution via CPython Emscripten. * Creates a fresh CPython WASM instance per execution (EXIT_RUNTIME). * * Security model: CPython Emscripten has zero JS bridge code. * `import js` fails with `ModuleNotFoundError` — the module doesn't exist. * `os.system()` is patched to no-op at Emscripten level. * No sandbox code needed — isolation is by construction. * * Defense-in-depth activates BEFORE CPython loads to block dangerous Node.js APIs. */ import { type WorkerDefenseStats } from "../../security/index.js"; export interface WorkerInput { protocolToken: string; sharedBuffer: SharedArrayBuffer; pythonCode: string; cwd: string; env: Record; args: string[]; scriptPath?: string; timeoutMs?: number; persistent?: boolean; sessionId?: string; /** * Unguessable, per-execution token. The wrapped script writes this exact line * to stderr as its final action before `sys.exit()`. The worker uses it to * detect the boundary between genuine program stderr and CPython interpreter * finalization noise (a spurious partial `Traceback (most recent call last):` * header emitted by `Py_FinalizeEx` when it re-touches the HOSTFS bridge during * teardown). Everything the runtime writes to fd 2 after this sentinel is * teardown noise and is suppressed; genuine errors are printed before it. */ stderrSentinel?: string; } export interface WorkerOutput { success: boolean; error?: string; /** Defense-in-depth stats if enabled */ defenseStats?: WorkerDefenseStats; }