DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(video-lesson): isolate rendering without requiring Docker
Bingxi Zhao (Frank)e38a1f63a13f
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.ymlindex 75abd4762..f7b3ea587 100644--- a/.github/workflows/tests.yml+++ b/.github/workflows/tests.yml@@ -352,8 +352,12 @@ jobs:DEEPTUTOR_TEST_REDIS_URL: redis://127.0.0.1:6379/0video-lesson-sandbox:- name: Video lesson isolation- runs-on: ubuntu-24.04+ name: Video lesson isolation (${{ matrix.os }})+ runs-on: ${{ matrix.os }}+ strategy:+ fail-fast: false+ matrix:+ os: [ubuntu-24.04, macos-latest, windows-latest]steps:- uses: actions/checkout@v4- uses: actions/setup-node@v4@@ -369,7 +373,9 @@ jobs:DEEPTUTOR_TEST_VIDEO_SANDBOX: "1"DEEPTUTOR_REMOTION_RUNTIME: ${{ runner.temp }}/remotionPYTHONPATH: ${{ github.workspace }}- run: pytest -q tests/agents/video_lesson/test_isolation.py tests/services/sandbox/test_process_cancellation.py+ run: |+ pytest -q tests/agents/video_lesson tests/services/sandbox/test_process_cancellation.py+ node --test tests/services/sandbox/javascript.test.mjstest-summary:name: Test Summarydiff --git a/README.md b/README.mdindex 4c1fb35a9..cfa7e10d5 100644--- a/README.md+++ b/README.md@@ -391,7 +391,7 @@ pip install -e ".[math-animator]" # Manim addon; requires LaTeX/ffmpeg/systempip install -e ".[edge-tts]" # Optional free speech provider and video-lesson narration```-**Video Lesson:** choose Video Lesson in chat or run `deeptutor run video_lesson "Explain a concept"`. The backend needs Node.js 22+, npm, and access to a running Docker daemon using Linux containers. First use downloads pinned Remotion packages and a Node container image. Frame checks run without network access, host mounts, or host credentials, and stop when the turn is cancelled; there is no unsandboxed fallback. The resulting lesson plays in the browser, with captions when optional narration is unavailable.+**Video Lesson:** choose Video Lesson in chat or run `deeptutor run video_lesson "Explain a concept"`. The backend needs Node.js 22+ and npm. First use downloads pinned Remotion packages and a WebAssembly JavaScript interpreter; Docker is not required. Frame checks run inside the interpreter with no host filesystem, credentials, network or process APIs, under memory and execution limits. Cancellation stops the build process. Local and container deployments use the same execution path, with no unsandboxed fallback. The resulting lesson plays in the browser, with captions when optional narration is unavailable.</details>diff --git a/deeptutor/agents/video_lesson/remotion/build.mjs b/deeptutor/agents/video_lesson/remotion/build.mjsindex 0a8ef15b2..a1548d76e 100644--- a/deeptutor/agents/video_lesson/remotion/build.mjs+++ b/deeptutor/agents/video_lesson/remotion/build.mjs@@ -1,5 +1,5 @@// Compile a model-written Remotion lesson (Lesson.tsx + the shipped kit) into a browser player-// script. Before bundling, sample frames are rendered in a network-isolated Docker container so+// script. Before bundling, sample frames are rendered in a WebAssembly JavaScript sandbox so// runtime errors come back to the model instead of reaching the learner.//// node build.mjs <Lesson.tsx> <out.js>@@ -8,16 +8,17 @@// esbuild (see package.json next to this file). Prints one JSON line: {ok, stage, errors, frames}.import {createRequire} from "node:module";-import {mkdtempSync, writeFileSync, readFileSync, rmSync} from "node:fs";+import {mkdtempSync, writeFileSync, readFileSync, rmSync, realpathSync} from "node:fs";import {tmpdir} from "node:os";import {dirname, join, resolve} from "node:path";-import {spawnSync} from "node:child_process";+import {fileURLToPath} from "node:url";+import {evaluateJavaScript} from "../../../services/sandbox/javascript.mjs";const runtime = resolve(process.env.DEEPTUTOR_REMOTION_RUNTIME || ".");const require = createRequire(join(runtime, "package.json"));const esbuild = require("esbuild");const [lessonArg, outArg] = process.argv.slice(2);-const lesson = resolve(lessonArg).replace(/\\/g, "/");+const lesson = realpathSync(resolve(lessonArg)).replace(/\\/g, "/");const nodePaths = [join(runtime, "node_modules")];const report = (o) => { console.log(JSON.stringify(o)); process.exit(o.ok ? 0 : 1); };@@ -36,7 +37,15 @@ const onlyAllowedImports = {// packages pulling in their own dependencies are not the lesson's importsif (!args.importer || !inside(args.importer)) return undefined;const target = resolve(args.resolveDir, args.path).replace(/\\/g, "/");- if (target === lesson || (args.path.startsWith(".") && inside(target))) return undefined;+ if (target === lesson) return undefined;+ // Never let the trusted compiler inline arbitrary local files or symlink+ // targets into the bundle before the interpreter even sees the code.+ const kit = {"./kit": "kit.tsx", "./kit.tsx": "kit.tsx", "./voice": "voice.ts",+ "./voice.ts": "voice.ts", "./china_outline.json": "china_outline.json"}[args.path];+ if (kit) {+ const path = join(home, kit);+ if (realpathSync(path).replace(/\\/g, "/") === path.replace(/\\/g, "/")) return {path};+ }return {errors: [{text: `import "${args.path}" is not allowed; use react, remotion and ./kit only`}]};});},@@ -45,27 +54,30 @@ const onlyAllowedImports = {const common = {bundle: true, jsx: "automatic", nodePaths, logLevel: "silent", plugins: [onlyAllowedImports],loader: {".tsx": "tsx", ".ts": "ts"}, define: {"process.env.NODE_ENV": '"production"'}};-// 1. Node check: render sample frames through @remotion/player's <Thumbnail>.+// 1. Isolated check: render sample frames through @remotion/player's <Thumbnail>.const tmp = mkdtempSync(join(tmpdir(), "dt-lesson-"));const checkEntry = join(tmp, "check.tsx");writeFileSync(checkEntry, `-import {renderToPipeableStream} from "react-dom/server";-import {Writable} from "node:stream";+import ${JSON.stringify(fileURLToPath(new URL("./render-platform.ts", import.meta.url)))};+import {renderToReadableStream} from "react-dom/server.edge";import {Thumbnail} from "@remotion/player";import {Lesson, LESSON} from ${JSON.stringify(lesson)};// Streamed rendering reports errors thrown below a Suspense boundary through onError;// renderToString would silently render the fallback instead.-const render = (element) => new Promise((done) => {- let html = "";+const render = async (element) => {const thrown = [];- const sink = new Writable({write(chunk, _enc, next) { html += chunk; next(); }});- sink.on("finish", () => done({html, thrown}));- const stream = renderToPipeableStream(element, {- onError(error) { thrown.push(error); },- onShellError(error) { thrown.push(error); done({html, thrown}); },- onAllReady() { stream.pipe(sink); },- });-});+ const stream = await renderToReadableStream(element, {onError(error) { thrown.push(error); }});+ await stream.allReady;+ let chars = 0;+ const reader = stream.getReader();+ while (true) {+ const {done, value} = await reader.read();+ if (done) break;+ chars += value.length;+ }+ return {chars, thrown};+};+export default async function check() {const errors = [], frames = [];const cfg = LESSON || {};for (const k of ["durationInFrames", "fps", "width", "height"])@@ -77,44 +89,41 @@ if (!errors.length) {const realError = console.error;const logged = [];console.error = (...a) => logged.push(a.map(String).join(" ").slice(0, 300));- const {html, thrown} = await render(<Thumbnail component={Lesson} compositionWidth={cfg.width}+ const {chars, thrown} = await render(<Thumbnail component={Lesson} compositionWidth={cfg.width}compositionHeight={cfg.height} durationInFrames={n} fps={cfg.fps} frameToDisplay={f} />);console.error = realError;- frames.push({frame: f, chars: html.length});+ frames.push({frame: f, chars});for (const e of thrown) errors.push("frame " + f + ": " + ((e && e.message) || e));- if (!thrown.length && html.length < 400) errors.push("frame " + f + ": almost nothing rendered");+ if (!thrown.length && chars < 400) errors.push("frame " + f + ": almost nothing rendered");for (const l of logged) if (!/Warning: Each child|useLayoutEffect|The above error occurred/.test(l)) errors.push("frame " + f + " console.error: " + l);}}-console.log(JSON.stringify({errors: [...new Set(errors)].slice(0, 20), frames}));+return JSON.stringify({errors: [...new Set(errors)].slice(0, 20), frames});+}`);try {- await esbuild.build({...common, entryPoints: [checkEntry], outfile: join(tmp, "check.mjs"), platform: "node",- format: "esm", banner: {js: "import {createRequire as __cr} from 'module'; const require = __cr(import.meta.url);"}});+ await esbuild.build({...common, entryPoints: [checkEntry], outfile: join(tmp, "check.js"), platform: "browser",+ format: "iife", globalName: "__lessonCheck", define: {...common.define, global: "globalThis"}});} catch (e) {rmSync(tmp, {recursive: true, force: true});report({ok: false, stage: "compile", errors: fmtErrors(e)});}-// No generated code executes in this process. The Python host owns a hardened-// container and removes it on success, timeout, failure, or cancellation.-const docker = process.env.DEEPTUTOR_RENDER_DOCKER;-const container = process.env.DEEPTUTOR_RENDER_CONTAINER;-if (!docker || !/^dt-lesson-[a-f0-9]{32}$/.test(container || "")) {+// Only source text enters the interpreter. No host functions, objects, imports,+// environment, filesystem or network capabilities are exposed to generated code.+let checked, renderError;+try {+ const {getQuickJS} = require("quickjs-emscripten");+ const source = readFileSync(join(tmp, "check.js"), "utf8") + "\n__lessonCheck.default()";+ const value = await evaluateJavaScript(await getQuickJS(), source, {+ timeoutMs: Number(process.env.DEEPTUTOR_RENDER_TIMEOUT_MS) || 180000,+ });+ checked = JSON.parse(value);+} catch (e) {+ renderError = String(e.message || e).slice(-1500);+} finally {rmSync(tmp, {recursive: true, force: true});- report({ok: false, stage: "sandbox", errors: ["An isolated render container is required"]});-}-const run = spawnSync(docker, ["start", "--attach", "--interactive", container], {- input: readFileSync(join(tmp, "check.mjs")), encoding: "utf8",- timeout: Number(process.env.DEEPTUTOR_RENDER_TIMEOUT_MS) || 180000,- maxBuffer: 1024 * 1024,-});-rmSync(tmp, {recursive: true, force: true});-if (run.error || run.signal || run.status !== 0) {- report({ok: false, stage: "render", errors: [(run.error?.message || run.stderr || "Render process failed").slice(-1500)]});}-let checked;-try { checked = JSON.parse(run.stdout.trim().split("\n").pop()); }-catch { report({ok: false, stage: "render", errors: [(run.stderr || run.stdout || "no output").slice(-1500)]}); }+if (renderError) report({ok: false, stage: "render", errors: [renderError]});if (checked.errors.length) report({ok: false, stage: "render", errors: checked.errors, frames: checked.frames});// 2. Browser bundle: the composition inside a <Player> with controls; ?frame=N opens paused there.diff --git a/deeptutor/agents/video_lesson/remotion/package-lock.json b/deeptutor/agents/video_lesson/remotion/package-lock.jsonnew file mode 100644index 000000000..e58f465b5--- /dev/null+++ b/deeptutor/agents/video_lesson/remotion/package-lock.json@@ -0,0 +1,614 @@+{+ "name": "deeptutor-video-lesson-runtime",+ "lockfileVersion": 3,+ "requires": true,+ "packages": {+ "": {+ "name": "deeptutor-video-lesson-runtime",+ "dependencies": {+ "@remotion/player": "4.0.537",+ "esbuild": "0.28.2",+ "fast-text-encoding": "1.0.6",+ "quickjs-emscripten": "0.32.0",+ "react": "19.3.1",+ "react-dom": "19.3.1",+ "remotion": "4.0.537",+ "web-streams-polyfill": "4.3.0"+ }+ },+ "node_modules/@esbuild/aix-ppc64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz",+ "integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==",+ "cpu": [+ "ppc64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "aix"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/android-arm": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz",+ "integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==",+ "cpu": [+ "arm"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/android-arm64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz",+ "integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/android-x64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz",+ "integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "android"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/darwin-arm64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz",+ "integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/darwin-x64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz",+ "integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/freebsd-arm64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz",+ "integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "freebsd"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/freebsd-x64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz",+ "integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "freebsd"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-arm": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz",+ "integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==",+ "cpu": [+ "arm"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-arm64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz",+ "integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-ia32": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz",+ "integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==",+ "cpu": [+ "ia32"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-loong64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz",+ "integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==",+ "cpu": [+ "loong64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-mips64el": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz",+ "integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==",+ "cpu": [+ "mips64el"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-ppc64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz",+ "integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==",+ "cpu": [+ "ppc64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-riscv64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz",+ "integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==",+ "cpu": [+ "riscv64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-s390x": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz",+ "integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==",+ "cpu": [+ "s390x"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/linux-x64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz",+ "integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/netbsd-arm64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz",+ "integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "netbsd"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/netbsd-x64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz",+ "integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "netbsd"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/openbsd-arm64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz",+ "integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "openbsd"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/openbsd-x64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz",+ "integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "openbsd"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/openharmony-arm64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz",+ "integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "openharmony"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/sunos-x64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz",+ "integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "sunos"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/win32-arm64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz",+ "integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/win32-ia32": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz",+ "integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==",+ "cpu": [+ "ia32"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@esbuild/win32-x64": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz",+ "integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">=18"+ }+ },+ "node_modules/@jitl/quickjs-ffi-types": {+ "version": "0.32.0",+ "resolved": "https://registry.npmjs.org/@jitl/quickjs-ffi-types/-/quickjs-ffi-types-0.32.0.tgz",+ "integrity": "sha512-v9T+GQpmk43VDJ7d72sf0Nexhk+ArvtUihW27dy7lqAl0zBObFKtSBBIm5RBjwIhE8VwsPPm9PNuvPvNqLWUEg==",+ "license": "MIT"+ },+ "node_modules/@jitl/quickjs-wasmfile-debug-asyncify": {+ "version": "0.32.0",+ "resolved": "https://registry.npmjs.org/@jitl/quickjs-wasmfile-debug-asyncify/-/quickjs-wasmfile-debug-asyncify-0.32.0.tgz",+ "integrity": "sha512-EX8zbXwGqCgAE764M+qvkHtyXDi/FUoMBea0JnES7vCM3P7a2+EOZOjGv85wtZ2sJhI1oJ+nekmqpOODFDY+hw==",+ "license": "MIT",+ "dependencies": {+ "@jitl/quickjs-ffi-types": "0.32.0"+ }+ },+ "node_modules/@jitl/quickjs-wasmfile-debug-sync": {+ "version": "0.32.0",+ "resolved": "https://registry.npmjs.org/@jitl/quickjs-wasmfile-debug-sync/-/quickjs-wasmfile-debug-sync-0.32.0.tgz",+ "integrity": "sha512-LeYWrPGC1uNCTBWvibo3ZLJj0CSVNYUXvJpXMCmuQ5Sap2cCACc3uvGvYV4homHHBAzfw5akoTqMMS4YFRtw+Q==",+ "license": "MIT",+ "dependencies": {+ "@jitl/quickjs-ffi-types": "0.32.0"+ }+ },+ "node_modules/@jitl/quickjs-wasmfile-release-asyncify": {+ "version": "0.32.0",+ "resolved": "https://registry.npmjs.org/@jitl/quickjs-wasmfile-release-asyncify/-/quickjs-wasmfile-release-asyncify-0.32.0.tgz",+ "integrity": "sha512-3oSwPfja12ICz4aIblB58cuY8JlEq5Txt8Cut4VLo+LH47QN+mzCnSgnbB03hWzg1LBcc+VyyI9UOag7a1NF+Q==",+ "license": "MIT",+ "dependencies": {+ "@jitl/quickjs-ffi-types": "0.32.0"+ }+ },+ "node_modules/@jitl/quickjs-wasmfile-release-sync": {+ "version": "0.32.0",+ "resolved": "https://registry.npmjs.org/@jitl/quickjs-wasmfile-release-sync/-/quickjs-wasmfile-release-sync-0.32.0.tgz",+ "integrity": "sha512-BKNDI/TPBfGlLNGYpLrhcDGXmIk4xHm4MRAisOBnOzpXVn9HZWsfmMAc9WMBrAHjvvds6HOikKeaOBKdPdpVrg==",+ "license": "MIT",+ "dependencies": {+ "@jitl/quickjs-ffi-types": "0.32.0"+ }+ },+ "node_modules/@remotion/player": {+ "version": "4.0.537",+ "resolved": "https://registry.npmjs.org/@remotion/player/-/player-4.0.537.tgz",+ "integrity": "sha512-zXY13uIqBr6q1fHAaiDvnXVMB8Wqs2BVoJTcXHlhhpoPoH4tGByzLc+961hhxMJjUR4Ubc2JRIaNbUckHEh46A==",+ "license": "SEE LICENSE IN LICENSE.md",+ "dependencies": {+ "remotion": "4.0.537"+ },+ "peerDependencies": {+ "react": ">=16.8.0",+ "react-dom": ">=16.8.0"+ }+ },+ "node_modules/esbuild": {+ "version": "0.28.2",+ "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz",+ "integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==",+ "hasInstallScript": true,+ "license": "MIT",+ "bin": {+ "esbuild": "bin/esbuild"+ },+ "engines": {+ "node": ">=18"+ },+ "optionalDependencies": {+ "@esbuild/aix-ppc64": "0.28.2",+ "@esbuild/android-arm": "0.28.2",+ "@esbuild/android-arm64": "0.28.2",+ "@esbuild/android-x64": "0.28.2",+ "@esbuild/darwin-arm64": "0.28.2",+ "@esbuild/darwin-x64": "0.28.2",+ "@esbuild/freebsd-arm64": "0.28.2",+ "@esbuild/freebsd-x64": "0.28.2",+ "@esbuild/linux-arm": "0.28.2",+ "@esbuild/linux-arm64": "0.28.2",+ "@esbuild/linux-ia32": "0.28.2",+ "@esbuild/linux-loong64": "0.28.2",+ "@esbuild/linux-mips64el": "0.28.2",+ "@esbuild/linux-ppc64": "0.28.2",+ "@esbuild/linux-riscv64": "0.28.2",+ "@esbuild/linux-s390x": "0.28.2",+ "@esbuild/linux-x64": "0.28.2",+ "@esbuild/netbsd-arm64": "0.28.2",+ "@esbuild/netbsd-x64": "0.28.2",+ "@esbuild/openbsd-arm64": "0.28.2",+ "@esbuild/openbsd-x64": "0.28.2",+ "@esbuild/openharmony-arm64": "0.28.2",+ "@esbuild/sunos-x64": "0.28.2",+ "@esbuild/win32-arm64": "0.28.2",+ "@esbuild/win32-ia32": "0.28.2",+ "@esbuild/win32-x64": "0.28.2"+ }+ },+ "node_modules/fast-text-encoding": {+ "version": "1.0.6",+ "resolved": "https://registry.npmjs.org/fast-text-encoding/-/fast-text-encoding-1.0.6.tgz",+ "integrity": "sha512-VhXlQgj9ioXCqGstD37E/HBeqEGV/qOD/kmbVG8h5xKBYvM1L3lR1Zn4555cQ8GkYbJa8aJSipLPndE1k6zK2w==",+ "license": "Apache-2.0"+ },+ "node_modules/quickjs-emscripten": {+ "version": "0.32.0",+ "resolved": "https://registry.npmjs.org/quickjs-emscripten/-/quickjs-emscripten-0.32.0.tgz",+ "integrity": "sha512-So0Sqw869y/S2oE3Nuc0uT3Dhqgvsj8FSrwBdsuTosVsG8ME5/OcudU1GxsrIFdFABgy17GHnTVO9TYV/bLQcA==",+ "license": "MIT",+ "dependencies": {+ "@jitl/quickjs-wasmfile-debug-asyncify": "0.32.0",+ "@jitl/quickjs-wasmfile-debug-sync": "0.32.0",+ "@jitl/quickjs-wasmfile-release-asyncify": "0.32.0",+ "@jitl/quickjs-wasmfile-release-sync": "0.32.0",+ "quickjs-emscripten-core": "0.32.0"+ },+ "engines": {+ "node": ">=16.0.0"+ }+ },+ "node_modules/quickjs-emscripten-core": {+ "version": "0.32.0",+ "resolved": "https://registry.npmjs.org/quickjs-emscripten-core/-/quickjs-emscripten-core-0.32.0.tgz",+ "integrity": "sha512-QFnPfjFey8EqknSrSxe1hZrf1/8z7/6s1QzGOmKo6++02r7QRRX7ZoyNaZh7JuVjWsVW87KnQrbZqnHkOAzUyg==",+ "license": "MIT",+ "dependencies": {+ "@jitl/quickjs-ffi-types": "0.32.0"+ }+ },+ "node_modules/react": {+ "version": "19.3.1",+ "resolved": "https://registry.npmjs.org/react/-/react-19.3.1.tgz",+ "integrity": "sha512-QNhWdt7nrgVWWQG58QuGsAR3jv3Hbe4mb4vz7w2csnHavciZJgda1CPNaldF6eVxv90ud3jUXpKlHL69gO38yw==",+ "license": "MIT",+ "engines": {+ "node": ">=0.10.0"+ }+ },+ "node_modules/react-dom": {+ "version": "19.3.1",+ "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.3.1.tgz",+ "integrity": "sha512-Y0bARz53lhEC1NMCASs0/j+faB/zqt3R8hpvg1e8+61ZVJ4UL0UdC4cBbXfZMcIuhrfgeILTLe+ZeBCavQPtKQ==",+ "license": "MIT",+ "dependencies": {+ "scheduler": "^0.28.0"+ },+ "peerDependencies": {+ "react": "^19.3.1"+ }+ },+ "node_modules/remotion": {+ "version": "4.0.537",+ "resolved": "https://registry.npmjs.org/remotion/-/remotion-4.0.537.tgz",+ "integrity": "sha512-chlB4+lnCWnDr6Pk+fDh3vWgac/ntGduq4VUpDsXzY1Pzu6URdnl40LUClK4ZlS3mwMv2U0oflE7qjZ9LGpVWg==",+ "license": "SEE LICENSE IN LICENSE.md",+ "peerDependencies": {+ "react": ">=16.8.0",+ "react-dom": ">=16.8.0"+ }+ },+ "node_modules/scheduler": {+ "version": "0.28.0",+ "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.28.0.tgz",+ "integrity": "sha512-juorfCmIkIw8tT+p5BXSm6PJjQF/ycEYmKyzURCIt/RaZIhL+PulbQ9Yu2z1HdOJDdqDTlxA1+xKBmHXJsczAw==",+ "license": "MIT"+ },+ "node_modules/web-streams-polyfill": {+ "version": "4.3.0",+ "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-4.3.0.tgz",+ "integrity": "sha512-/Gnggvj9oSrEvJbDyyPtAnxBt5fGQM2iWOKQNu7ie1OxDgK40iZpyV3TKaRiEzVj1oA1UxKnEy9XPXh6PW3eVw==",+ "license": "MIT",+ "workspaces": [+ "test/benchmark-test",+ "test/rollup-test",+ "test/webpack-test"+ ],+ "engines": {+ "node": ">= 8"+ }+ }+ }+}diff --git a/deeptutor/agents/video_lesson/remotion/package.json b/deeptutor/agents/video_lesson/remotion/package.jsonindex 6c7f68be7..f90b005cf 100644--- a/deeptutor/agents/video_lesson/remotion/package.json+++ b/deeptutor/agents/video_lesson/remotion/package.json@@ -7,6 +7,9 @@"esbuild": "0.28.2","react": "19.3.1","react-dom": "19.3.1",- "remotion": "4.0.537"+ "remotion": "4.0.537",+ "quickjs-emscripten": "0.32.0",+ "web-streams-polyfill": "4.3.0",+ "fast-text-encoding": "1.0.6"}}diff --git a/deeptutor/agents/video_lesson/remotion/render-platform.ts b/deeptutor/agents/video_lesson/remotion/render-platform.tsnew file mode 100644index 000000000..e1819b407--- /dev/null+++ b/deeptutor/agents/video_lesson/remotion/render-platform.ts@@ -0,0 +1,19 @@+// Pure-JavaScript platform support for React's streaming renderer inside QuickJS.+// These objects live entirely in guest memory; none bridges to the host runtime.+import "fast-text-encoding";+import {ReadableStream} from "web-streams-polyfill";+const started = Date.now();+Object.assign(globalThis, {+ performance: {now: () => Date.now() - started},+ ReadableStream,+ console: {log() {}, warn() {}, error() {}, info() {}, debug() {}},+ queueMicrotask: (callback: () => void) => Promise.resolve().then(callback),+ // Frame checks don't wait for real-world I/O. React scheduling runs on the+ // guest promise queue, which the interpreter pumps under the same deadline.+ setTimeout: (callback: () => void) => {+ const timer = {cancelled: false};+ Promise.resolve().then(() => { if (!timer.cancelled) callback(); });+ return timer;+ },+ clearTimeout: (timer: {cancelled: boolean}) => { if (timer) timer.cancelled = true; },+});diff --git a/deeptutor/agents/video_lesson/runtime.py b/deeptutor/agents/video_lesson/runtime.pyindex ecf6293e8..bfc235b86 100644--- a/deeptutor/agents/video_lesson/runtime.py+++ b/deeptutor/agents/video_lesson/runtime.py@@ -2,7 +2,7 @@The model writes ``Lesson.tsx`` against the shipped kit (``remotion/kit.tsx``).``build.mjs`` compiles it with esbuild, renders sample frames in a-network-isolated, read-only Docker container, and bundles it with ``@remotion/player``.+WebAssembly JavaScript sandbox, and bundles it with ``@remotion/player``.The page this module writes is self-contained: the learner's browser plays thecomposition live, so no video file is ever encoded on the server."""@@ -16,7 +16,6 @@ import jsonimport osfrom pathlib import Pathimport shutil-import uuidfrom deeptutor.services.path_service import get_path_servicefrom deeptutor.services.sandbox.backends import RestrictedSubprocessBackend@@ -26,10 +25,6 @@ ASSETS_DIR = Path(__file__).resolve().parent / "remotion"KIT_FILES = ("kit.tsx", "china_outline.json", "voice.ts")BUILD_TIMEOUT_S = 600INSTALL_TIMEOUT_S = 900-# Official multi-platform Node 22 image, pinned so render checks use one runtime.-RENDER_IMAGE = (- "node:22-slim@sha256:de170b39439eb0e5a3444ad7d60b015614a381e10fecae2e30ec9e735122f38d"-)RENDER_TIMEOUT_S = 180_install_lock = asyncio.Lock()@@ -80,33 +75,19 @@ async def _run_tool() -> ExecResult:"""Run trusted tooling with bounded output and cancellation of its process tree.- Model code never runs through this host subprocess backend. Only the- compiler and Docker client do; the frame-check bundle goes to the container.+ Model code runs inside WebAssembly, never in the host JavaScript context.+ This subprocess owns the trusted compiler and the Wasm interpreter."""- docker_env = {- key: os.environ[key]- for key in ("DOCKER_HOST", "DOCKER_CONTEXT", "DOCKER_CONFIG", "XDG_RUNTIME_DIR")- if key in os.environ- }return await RestrictedSubprocessBackend(inherit_virtualenv=False).exec(ExecRequest.of_argv(argv,workdir=str(cwd) if cwd else "",- env={**docker_env, **(env or {})},+ env=env or {},limits=ResourceLimits(timeout_s=timeout, max_output_chars=64_000),))-def _docker() -> str:- docker = shutil.which("docker")- if not docker:- raise VideoLessonRuntimeError(- "Video lessons need Docker with a running Linux-container daemon for isolated render checks."- )- return docker--def _require_success(result: ExecResult, action: str) -> None:if not result.ok or result.exit_code != 0:detail = result.error or result.stderr or result.stdout@@ -118,7 +99,6 @@ def _require_success(result: ExecResult, action: str) -> None:async def ensure_runtime() -> Path:"""Prepare pinned tooling and the isolated renderer; never fall back to host execution."""_node()- docker = _docker()root = runtime_dir()async with _install_lock:if not _installed(root):@@ -126,9 +106,10 @@ async def ensure_runtime() -> Path:if not npm:raise VideoLessonRuntimeError("Video lessons need npm to install Remotion.")root.mkdir(parents=True, exist_ok=True)- shutil.copy(ASSETS_DIR / "package.json", root / "package.json")+ for name in ("package.json", "package-lock.json"):+ shutil.copy(ASSETS_DIR / name, root / name)result = await _run_tool(- [npm, "install", "--no-audit", "--no-fund", "--omit=dev"],+ [npm, "ci", "--no-audit", "--no-fund", "--omit=dev"],timeout=INSTALL_TIMEOUT_S,cwd=root,)@@ -137,16 +118,6 @@ async def ensure_runtime() -> Path:raise VideoLessonRuntimeError("Installed Remotion packages do not match the manifest.")- result = await _run_tool([docker, "info", "--format", "{{.OSType}}"], timeout=15)- _require_success(result, "Connecting to the Docker sandbox")- if result.stdout.strip() != "linux":- raise VideoLessonRuntimeError("Video lesson render checks need Linux containers.")- image = await _run_tool([docker, "image", "inspect", RENDER_IMAGE], timeout=15)- if not image.ok or image.exit_code != 0:- _require_success(- await _run_tool([docker, "pull", RENDER_IMAGE], timeout=INSTALL_TIMEOUT_S),- "Preparing the isolated Node renderer",- )return root@@ -165,67 +136,17 @@ def prepare_workdir(work_dir: Path, code: str, voice: str | None = None) -> Pathreturn lesson-def _container_argv(docker: str, name: str) -> list[str]:- # No bind mounts, host environment, published ports, or Docker socket.- # The self-contained check bundle is streamed over stdin by build.mjs.- return [- docker,- "create",- "--name",- name,- "--interactive",- "--init",- "--network",- "none",- "--read-only",- "--cap-drop",- "ALL",- "--security-opt",- "no-new-privileges",- "--user",- "65534:65534",- "--pids-limit",- "64",- "--memory",- "512m",- "--memory-swap",- "512m",- "--cpus",- "1",- RENDER_IMAGE,- "node",- "--input-type=module",- ]--async def build_lesson(lesson: Path, bundle: Path) -> BuildResult:- """Compile on the host; execute generated code only in a disposable container."""+ """Compile the lesson and validate it through the deployment-independent JS sandbox."""root = await ensure_runtime()- docker = _docker()- name = "dt-lesson-" + uuid.uuid4().hex- try:- _require_success(- await _run_tool(_container_argv(docker, name), timeout=30),- "Creating the isolated renderer",- )- result = await _run_tool(- [_node(), str(ASSETS_DIR / "build.mjs"), str(lesson), str(bundle)],- timeout=BUILD_TIMEOUT_S,- env={- "DEEPTUTOR_REMOTION_RUNTIME": str(root),- "DEEPTUTOR_RENDER_DOCKER": docker,- "DEEPTUTOR_RENDER_CONTAINER": name,- "DEEPTUTOR_RENDER_TIMEOUT_MS": str(RENDER_TIMEOUT_S * 1000),- },- )- finally:- # Killing the Docker client does not stop a daemon-owned container.- # Always remove this turn's container, including cancelled create/start calls.- cleanup = await _run_tool([docker, "rm", "--force", name], timeout=15)- if not cleanup.ok or (- cleanup.exit_code and "no such container" not in cleanup.stderr.lower()- ):- _require_success(cleanup, "Removing the isolated renderer")+ result = await _run_tool(+ [_node(), str(ASSETS_DIR / "build.mjs"), str(lesson), str(bundle)],+ timeout=BUILD_TIMEOUT_S,+ env={+ "DEEPTUTOR_REMOTION_RUNTIME": str(root),+ "DEEPTUTOR_RENDER_TIMEOUT_MS": str(RENDER_TIMEOUT_S * 1000),+ },+ )if result.timed_out:return BuildResult(False, "timeout", [f"build did not finish within {BUILD_TIMEOUT_S}s"])lines = result.stdout.strip().splitlines()diff --git a/deeptutor/app/facade.py b/deeptutor/app/facade.pyindex 9fb4959af..e3dd1d5d3 100644--- a/deeptutor/app/facade.py+++ b/deeptutor/app/facade.py@@ -128,14 +128,14 @@ class DeepTutorApp:),)if resolved == "video_lesson":- available = all(shutil.which(tool) is not None for tool in ("node", "npm", "docker"))+ available = all(shutil.which(tool) is not None for tool in ("node", "npm"))return CapabilityAvailability(name=resolved,available=available,install_hint=(""if available- else "Install Node.js 22+ (with npm) and Docker with a running Linux-container daemon. Remotion and the isolated renderer are prepared on first use."+ else "Install Node.js 22+ (with npm). Remotion and its WebAssembly sandbox are prepared on first use."),)return CapabilityAvailability(name=resolved, available=True)diff --git a/deeptutor/services/sandbox/javascript.mjs b/deeptutor/services/sandbox/javascript.mjsnew file mode 100644index 000000000..85460ab05--- /dev/null+++ b/deeptutor/services/sandbox/javascript.mjs@@ -0,0 +1,70 @@+/** Evaluate a self-contained JS expression in a capability-free WebAssembly VM.+ *+ * The expression may return a string or a Promise<string>. Only the result string+ * crosses the boundary: no host callbacks, module loader, Node/browser objects,+ * filesystem, environment, sockets or subprocess APIs are registered. Function()+ * and eval stay inside the guest. This is separate from shell sandbox selection:+ * a language VM does not grant permission to execute arbitrary host commands.+ *+ * The caller owns a disposable subprocess for a hard wall-clock deadline and+ * cancellation. Interpreter limits also bound guest memory, stack and execution.+ */+export async function evaluateJavaScript(QuickJS, source, {+ timeoutMs = 180000,+ memoryLimitBytes = 128 * 1024 * 1024,+ maxOutputChars = 64000,+} = {}) {+ const runtime = QuickJS.newRuntime();+ runtime.setMemoryLimit(memoryLimitBytes);+ runtime.setMaxStackSize(1024 * 1024);+ const deadline = Date.now() + timeoutMs;+ runtime.setInterruptHandler(() => Date.now() >= deadline);+ const context = runtime.newContext();+ let handle;+ const unwrap = (result) => {+ if (result.error) {+ // Error serialization also executes only in the bounded guest interpreter.+ const error = context.dump(result.error);+ result.error.dispose();+ throw new Error(error?.message || String(error));+ }+ return result.value;+ };+ try {+ handle = unwrap(context.evalCode(source, "sandbox.js"));+ while (true) {+ if (Date.now() >= deadline) throw new Error("JavaScript execution timed out");+ const state = context.getPromiseState(handle);+ if (state.type === "rejected") {+ const error = context.dump(state.error);+ state.error.dispose();+ throw new Error(error?.message || String(error));+ }+ if (state.type === "fulfilled") {+ try {+ if (context.typeof(state.value) !== "string") throw new Error("Sandbox must return a string");+ const length = context.getProp(state.value, "length");+ try {+ if (context.getNumber(length) > maxOutputChars) throw new Error("Sandbox output limit exceeded");+ } finally {+ length.dispose();+ }+ return context.getString(state.value);+ } finally {+ if (!state.notAPromise) state.value.dispose();+ }+ }+ if (!runtime.hasPendingJob()) throw new Error("Sandbox promise did not settle");+ const jobs = runtime.executePendingJobs(1);+ if (jobs.error) {+ const error = context.dump(jobs.error);+ jobs.error.dispose();+ throw new Error(error?.message || String(error));+ }+ }+ } finally {+ handle?.dispose();+ context.dispose();+ runtime.dispose();+ }+}diff --git a/tests/agents/video_lesson/test_isolation.py b/tests/agents/video_lesson/test_isolation.pyindex ea7fd54a8..19d2e0e10 100644--- a/tests/agents/video_lesson/test_isolation.py+++ b/tests/agents/video_lesson/test_isolation.py@@ -1,4 +1,4 @@-"""Real container checks; CI enables these with DEEPTUTOR_TEST_VIDEO_SANDBOX=1."""+"""Real Wasm checks; CI enables these with DEEPTUTOR_TEST_VIDEO_SANDBOX=1."""from __future__ import annotations@@ -15,7 +15,7 @@ pytestmark = [pytest.mark.asyncio,pytest.mark.skipif(os.environ.get("DEEPTUTOR_TEST_VIDEO_SANDBOX") != "1",- reason="requires Docker and the Remotion npm runtime",+ reason="requires Node and the Remotion npm runtime",),]@@ -23,113 +23,137 @@ CONFIG = "export const LESSON = {durationInFrames:60, fps:30, width:1920, heightVALID = CONFIG + 'export const Lesson = () => <div>{"A safe lesson. ".repeat(50)}</div>;'[email protected](autouse=True)+def no_docker(monkeypatch: pytest.MonkeyPatch):+ """Every integration test must work with Docker unavailable, even on CI hosts."""+ which = runtime.shutil.which++ def without_docker(name, *args, **kwargs):+ if name in {"docker", "podman", "bwrap", "sandbox-exec"}:+ pytest.fail(f"Video lessons must not probe deployment tooling: {name}")+ return which(name, *args, **kwargs)++ monkeypatch.setattr(runtime.shutil, "which", without_docker)+ # A stray Docker invocation would also fail even on a runner with Docker installed.+ monkeypatch.setenv("DOCKER_HOST", "tcp://127.0.0.1:1")++async def test_real_render_and_runtime_error(tmp_path: Path) -> None:lesson = runtime.prepare_workdir(tmp_path, VALID)result = await runtime.build_lesson(lesson, tmp_path / "player.js")- assert result.ok and len(result.frames) == 25+ assert result.ok and len(result.frames) == 25, result.errorsassert (tmp_path / "player.js").stat().st_size > 0lesson.write_text(CONFIG + 'export const Lesson = () => {throw new Error("fixture failure")};')result = await runtime.build_lesson(lesson, tmp_path / "player.js")assert not result.ok and any("fixture failure" in error for error in result.errors)+async def test_real_kit_animation_and_captions(tmp_path: Path) -> None:+ code = (+ CONFIG+ + """+import {Sequence} from "remotion";+import {Stage, Title, Body, Narration} from "./kit";+export const Lesson = () => <Stage eyebrow="Test">+ <Sequence durationInFrames={60}>+ <Title>安全运行 · Safe rendering</Title>+ <Body x={120} y={300}>A complete lesson with animation and captions.</Body>+ <Narration lines={[{at: 0, text: "不需要 Docker"}, {at: 30, text: "Second caption"}]} />+ </Sequence>+</Stage>;+"""+ )+ lesson = runtime.prepare_workdir(tmp_path, code)+ result = await runtime.build_lesson(lesson, tmp_path / "player.js")+ assert result.ok and len(result.frames) == 25, result.errors+ assert len({frame["chars"] for frame in result.frames}) > 1++async def test_model_cannot_read_host_secrets_files_or_connect_to_network(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:marker = tmp_path / "host-secret.txt"marker.write_text("dummy-host-secret")monkeypatch.setenv("DT_REVIEW_SENTINEL", "dummy-environment-secret")- code = """import React from "react";-let probe = Promise.resolve();-if (typeof process !== "undefined") {- if (process.env.DT_REVIEW_SENTINEL) throw new Error("host environment leaked");- const fs = process.getBuiltinModule("fs");- if (fs.existsSync(HOST_FILE)) throw new Error("host file exposed");- let writable = false;- try { fs.writeFileSync("/tmp/should-not-exist", "x"); writable = true; } catch {}- if (writable) throw new Error("container filesystem is writable");- probe = fetch("http://1.1.1.1", {signal: AbortSignal.timeout(500)}).then(- () => {throw new Error("outbound network allowed");}, () => undefined);+ code = """+for (const name of ["process", "require", "fetch", "XMLHttpRequest", "WebSocket", "Worker"]) {+ if (typeof globalThis[name] !== "undefined") throw new Error("host API exposed: " + name);}-export const Lesson = () => {- React.use(probe);- return <div>{"Isolated lesson. ".repeat(50)}</div>;-};-""".replace("HOST_FILE", json.dumps(str(marker)))+if (({}).constructor.constructor("return typeof process")() !== "undefined")+ throw new Error("constructor escaped to the host");+if (eval("typeof process") !== "undefined") throw new Error("eval escaped to the host");+export const Lesson = () => <div>{"Isolated lesson. ".repeat(50)}</div>;+"""lesson = runtime.prepare_workdir(tmp_path, CONFIG + code)result = await runtime.build_lesson(lesson, tmp_path / "player.js")assert result.ok, result.errorsassert marker.read_text() == "dummy-host-secret"+ assert "dummy-host-secret" not in (tmp_path / "player.js").read_text()+ assert "dummy-environment-secret" not in (tmp_path / "player.js").read_text()-async def test_cancelled_render_removes_running_container([email protected]("specifier", ["node:fs", "node:child_process", "../host-secret.json"])+async def test_host_imports_are_rejected(tmp_path: Path, specifier: str) -> None:+ (tmp_path / "host-secret.json").write_text('{"secret":"dummy-host-secret"}')+ lesson = runtime.prepare_workdir(+ tmp_path / "source",+ CONFIG + f"import secret from {json.dumps(specifier)};\n"+ "export const Lesson = () => <div>{JSON.stringify(secret)}</div>;",+ )+ result = await runtime.build_lesson(lesson, tmp_path / "source" / "player.js")+ assert not result.ok and result.stage == "compile"+ assert not (tmp_path / "source" / "player.js").exists()+++async def test_cancelled_render_reaps_build_process(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:await runtime.ensure_runtime()- original = runtime._run_tool- created = asyncio.Event()- names: list[str] = []-- async def observe(argv, **kwargs):- result = await original(argv, **kwargs)- if "create" in argv:- names.append(argv[argv.index("--name") + 1])- created.set()- return result-- monkeypatch.setattr(runtime, "_run_tool", observe)+ original = asyncio.create_subprocess_exec+ started = asyncio.Event()+ processes = []++ async def observe(*argv, **kwargs):+ process = await original(*argv, **kwargs)+ if str(runtime.ASSETS_DIR / "build.mjs") in argv:+ processes.append(process)+ started.set()+ return process++ monkeypatch.setattr(asyncio, "create_subprocess_exec", observe)lesson = runtime.prepare_workdir(tmp_path, CONFIG + "export const Lesson = () => { while (true) {} };")task = asyncio.create_task(runtime.build_lesson(lesson, tmp_path / "player.js"))try:- await asyncio.wait_for(created.wait(), 30)- for _ in range(100):- state = await original(- [runtime._docker(), "inspect", "--format", "{{.State.Running}}", names[0]],- timeout=5,- )- if state.stdout.strip() == "true":- break- await asyncio.sleep(0.05)- else:- pytest.fail("renderer never started")+ await asyncio.wait_for(started.wait(), 30)+ await asyncio.sleep(1)+ assert not task.done(), "infinite renderer should still be running"task.cancel()with pytest.raises(asyncio.CancelledError):- await asyncio.wait_for(task, 20)- state = await original([runtime._docker(), "inspect", names[0]], timeout=5)- assert state.exit_code != 0 and "no such" in state.stderr.lower()+ await asyncio.wait_for(task, 10)+ assert processes and all(p.returncode is not None for p in processes)+ assert not (tmp_path / "player.js").exists()finally:if not task.done():task.cancel()await asyncio.gather(task, return_exceptions=True)- for name in names:- await original([runtime._docker(), "rm", "--force", name], timeout=10)-async def test_timed_out_render_is_rejected_and_removed(+async def test_timed_out_render_is_rejected(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(runtime, "RENDER_TIMEOUT_S", 1)- original = runtime._run_tool- names: list[str] = []-- async def observe(argv, **kwargs):- if "create" in argv:- names.append(argv[argv.index("--name") + 1])- return await original(argv, **kwargs)-- monkeypatch.setattr(runtime, "_run_tool", observe)lesson = runtime.prepare_workdir(tmp_path, CONFIG + "export const Lesson = () => { while (true) {} };")result = await runtime.build_lesson(lesson, tmp_path / "player.js")assert not result.ok and result.stage == "render"- state = await original([runtime._docker(), "inspect", names[0]], timeout=5)- assert state.exit_code != 0 and "no such" in state.stderr.lower()+ assert any("interrupt" in e or "timed out" in e for e in result.errors)+ assert not (tmp_path / "player.js").exists()-async def test_build_script_refuses_to_execute_without_container(tmp_path: Path) -> None:+async def test_build_script_uses_isolation_without_container_configuration(tmp_path: Path) -> None:root = await runtime.ensure_runtime()lesson = runtime.prepare_workdir(tmp_path, VALID)result = await runtime._run_tool(@@ -142,6 +166,54 @@ async def test_build_script_refuses_to_execute_without_container(tmp_path: Path)timeout=30,env={"DEEPTUTOR_REMOTION_RUNTIME": str(root)},)- assert result.exit_code != 0- assert json.loads(result.stdout.strip().splitlines()[-1])["stage"] == "sandbox"+ assert result.exit_code == 0, result.stderr+ assert json.loads(result.stdout.strip().splitlines()[-1])["ok"]+ assert (tmp_path / "player.js").exists()++[email protected]("with_audio", [False, True])+async def test_narrated_scenes_with_and_without_audio(tmp_path: Path, with_audio: bool) -> None:+ code = """+import {Stage, Title, Scenes, lessonLength} from "./kit";+const scenes = [{voice: "s1", frames: 60, content: <Stage><Title>声音与字幕</Title></Stage>}];+export const LESSON = {durationInFrames:lessonLength(scenes), fps:30, width:1920, height:1080};+export const Lesson = () => <Scenes scenes={scenes} />;+"""+ clip = {"text": "声音与字幕", "frames": 45, "captions": [{"at": 0, "text": "声音与字幕"}]}+ if with_audio:+ clip["src"] = "data:audio/mpeg;base64,SUQz"+ voice = "export const VOICE = " + json.dumps({"s1": clip}) + ";"+ lesson = runtime.prepare_workdir(tmp_path, code, voice=voice)+ result = await runtime.build_lesson(lesson, tmp_path / "player.js")+ assert result.ok and len(result.frames) == 25, result.errors+++async def test_memory_exhaustion_is_rejected(tmp_path: Path) -> None:+ lesson = runtime.prepare_workdir(+ tmp_path,+ CONFIG+ + "export const Lesson = () => { const a=[]; while(true) a.push(new Uint8Array(1024*1024)); };",+ )+ result = await runtime.build_lesson(lesson, tmp_path / "player.js")+ assert not result.ok and result.stage == "render"+ assert not (tmp_path / "player.js").exists()+++async def test_compiler_rejects_local_file_and_symlink_imports(tmp_path: Path) -> None:+ lesson = runtime.prepare_workdir(+ tmp_path,+ CONFIG+ + 'import secret from "./secret.json"; export const Lesson = () => <div>{secret}</div>;',+ )+ (tmp_path / "secret.json").write_text('"dummy-host-secret"')+ result = await runtime.build_lesson(lesson, tmp_path / "player.js")+ assert not result.ok and result.stage == "compile"+ (tmp_path / "china_outline.json").unlink()+ try:+ (tmp_path / "china_outline.json").symlink_to(tmp_path / "secret.json")+ except OSError:+ pytest.skip("symlinks unavailable on this Windows account")+ lesson.write_text(lesson.read_text().replace("./secret.json", "./china_outline.json"))+ result = await runtime.build_lesson(lesson, tmp_path / "player.js")+ assert not result.ok and result.stage == "compile"assert not (tmp_path / "player.js").exists()diff --git a/tests/agents/video_lesson/test_lesson_writer.py b/tests/agents/video_lesson/test_lesson_writer.pyindex b782e7410..f8d94c605 100644--- a/tests/agents/video_lesson/test_lesson_writer.py+++ b/tests/agents/video_lesson/test_lesson_writer.py@@ -34,7 +34,11 @@ def test_parse_draft_without_code_is_unusable(reply: str) -> None:assert draft.title == ""-def test_writer_agent_starts_a_conversation_with_the_kit() -> None:+def test_writer_agent_starts_a_conversation_with_the_kit(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(+ "deeptutor.agents.base_agent.get_agent_params",+ lambda _: {"temperature": 0.7, "max_tokens": 16000},+ )agent = LessonWriterAgent(api_key="test", base_url="http://localhost", language="en")agent.start(user_input="Explain a cobweb plot", history="")system, user = agent._messagesdiff --git a/tests/agents/video_lesson/test_runtime.py b/tests/agents/video_lesson/test_runtime.pyindex ae672c492..a4f4b46cd 100644--- a/tests/agents/video_lesson/test_runtime.py+++ b/tests/agents/video_lesson/test_runtime.py@@ -9,7 +9,13 @@ from deeptutor.agents.video_lesson.runtime import ASSETS_DIR, KIT_FILES, preparedef test_shipped_runtime_assets_exist() -> None:- for name in (*KIT_FILES, "build.mjs", "package.json"):+ for name in (+ *KIT_FILES,+ "build.mjs",+ "render-platform.ts",+ "package.json",+ "package-lock.json",+ ):assert (ASSETS_DIR / name).is_file(), namedeps = json.loads((ASSETS_DIR / "package.json").read_text(encoding="utf-8"))["dependencies"]# Pinned exactly: a lesson that compiled once must compile the same way later.@@ -39,11 +45,15 @@ def test_prepare_workdir_writes_the_voice_module(tmp_path: Path) -> None:assert "s1" in (tmp_path / "source" / "voice.ts").read_text(encoding="utf-8")-def test_video_lesson_requires_a_docker_runtime(monkeypatch) -> None:- import pytest+def test_video_lesson_available_without_docker(monkeypatch) -> None:+ from types import SimpleNamespace- from deeptutor.agents.video_lesson import runtime+ from deeptutor.app.facade import DeepTutorApp- monkeypatch.setattr(runtime.shutil, "which", lambda _name: None)- with pytest.raises(runtime.VideoLessonRuntimeError, match="Docker"):- runtime._docker()+ monkeypatch.setattr(+ "deeptutor.app.facade.shutil.which", lambda name: name if name in {"node", "npm"} else None+ )+ app = SimpleNamespace(resolve_capability=lambda value: value)+ availability = DeepTutorApp.get_capability_availability(app, "video_lesson")+ assert availability.available+ assert not availability.install_hintdiff --git a/tests/services/sandbox/javascript.test.mjs b/tests/services/sandbox/javascript.test.mjsnew file mode 100644index 000000000..dbdc4da9d--- /dev/null+++ b/tests/services/sandbox/javascript.test.mjs@@ -0,0 +1,51 @@+import assert from "node:assert/strict";+import {createRequire} from "node:module";+import {join} from "node:path";+import {test} from "node:test";+import {evaluateJavaScript} from "../../../deeptutor/services/sandbox/javascript.mjs";+const require = createRequire(join(process.env.DEEPTUTOR_REMOTION_RUNTIME, "package.json"));+const {getQuickJS} = require("quickjs-emscripten");+const vm = await getQuickJS();+const run = (source, options) => evaluateJavaScript(vm, source, options);++test("returns only strings, including async results", async () => {+ assert.equal(await run('"同步"'), "同步");+ assert.equal(await run('(async () => {await Promise.resolve(); return "async"})()'), "async");+ await assert.rejects(run('({host: "object"})'), /must return a string/);+ await assert.rejects(run('new Promise(() => {})'), /did not settle/);+});++test("no environment, filesystem, networking, workers or process APIs", async () => {+ process.env.DT_SANDBOX_TEST_SECRET = "dummy-host-secret";+ try {+ for (const name of ["process", "require", "fetch", "WebSocket", "XMLHttpRequest", "Worker", "Deno", "Bun", "std", "os", "WebAssembly"]) {+ assert.equal(await run(`typeof globalThis[${JSON.stringify(name)}]`), "undefined", name);+ }+ assert.equal(await run('Function("return typeof process")()'), "undefined");+ assert.equal(await run('({}).constructor.constructor("return typeof process")()'), "undefined");+ assert.equal(await run('eval("typeof process")'), "undefined");+ await assert.rejects(run('import("node:fs")'), /module|load/i);+ } finally { delete process.env.DT_SANDBOX_TEST_SECRET; }+});++test("guest prototype changes never reach the host or the next evaluation", async () => {+ assert.equal(await run('Object.prototype.polluted = "guest"; "done"'), "done");+ assert.equal(({}).polluted, undefined);+ assert.equal(await run('typeof ({}).polluted'), "undefined");+});++test("limits synchronous loops, async loops, allocation and output", async () => {+ for (const source of ['while(true) {}', '(async () => {while(true) await Promise.resolve()})()']) {+ await assert.rejects(run(source, {timeoutMs: 100}), /interrupt|timed out/);+ }+ await assert.rejects(run('const a=[]; while(true) a.push(new Uint8Array(1024*1024))', {+ memoryLimitBytes: 8 * 1024 * 1024, timeoutMs: 2000,+ }), /memory|alloc|null/i);+ await assert.rejects(run('"x".repeat(1001)', {maxOutputChars: 1000}), /output limit/);+ assert.equal(await run('"still usable"'), "still usable");+});++test("reports synchronous and async guest exceptions", async () => {+ await assert.rejects(run('throw new Error("sync failure")'), /sync failure/);+ await assert.rejects(run('(async () => {throw new Error("async failure")})()'), /async failure/);+});