The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
remotion: cover the pane server and $REMOTION, fix three ways the pane crashed
dee29298e03cfc6
diff --git a/store/agents/remotion/toolchain/remotion.mjs b/store/agents/remotion/toolchain/remotion.mjsindex 5ffa6ca06..4d7921cae 100644--- a/store/agents/remotion/toolchain/remotion.mjs+++ b/store/agents/remotion/toolchain/remotion.mjs@@ -19,7 +19,7 @@ const isMain = (() => { try { return realpathSync(process.argv[1]) === realpathSif (isMain && sub !== 'render' && sub !== 'still') {const child = spawn(cli, args, { stdio: 'inherit' })- child.on('exit', (code, signal) => process.exit(signal ? 1 : code ?? 1))+ child.on('exit', (code, signal) => process.exit(signal ? 1 : code))child.on('error', (error) => { console.error(`remotion could not start: ${error.message}`); process.exit(1) })} else if (isMain) {track()@@ -122,7 +122,7 @@ function track() {else state.error = tail.filter((l) => /error|Error|failed|✖/.test(l)).slice(-3).join('\n') || tail.slice(-3).join('\n') || `remotion exited ${code}`state.finishedAt = new Date().toISOString()write(true)- process.exit(code ?? 1)+ process.exit(code)}child.on('exit', (code, signal) => setTimeout(() => finish(signal ? 1 : code), 50))child.on('error', (error) => { tail.push(error.message); finish(1) })diff --git a/store/agents/remotion/toolchain/test_remotion.mjs b/store/agents/remotion/toolchain/test_remotion.mjsindex a648fc238..90831191d 100644--- a/store/agents/remotion/toolchain/test_remotion.mjs+++ b/store/agents/remotion/toolchain/test_remotion.mjs@@ -1,16 +1,30 @@-// $REMOTION's progress parsing, against the lines Remotion 4.0.525's CLI prints: node --test toolchain/test_remotion.mjs+// $REMOTION's progress parsing, against the lines Remotion 4.0.525's CLI prints, and $REMOTION itself+// run against a stand-in CLI (REMOTION_CLI): node --test toolchain/test_remotion.mjsimport assert from 'node:assert/strict'+import { spawn } from 'node:child_process'+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs'+import { createRequire } from 'node:module'+import net from 'node:net'+import { tmpdir } from 'node:os'+import { dirname, join } from 'node:path'import { test } from 'node:test'+import { fileURLToPath, pathToFileURL } from 'node:url'import { parseLine, shouldPrint } from './remotion.mjs'+const HERE = dirname(fileURLToPath(import.meta.url))+const wait = (ms) => new Promise((ok) => setTimeout(ok, ms))+test('reads the stages of a render', () => {assert.deepEqual(parseLine('Bundling 50%'), { stage: 'bundling', progress: 0.05 })assert.deepEqual(parseLine('Getting composition'), { stage: 'preparing' })assert.deepEqual(parseLine('\x1b[90mComposition Main\x1b[39m'), { composition: 'Main' })assert.deepEqual(parseLine('\x1b[90mOutput out/main.mp4\x1b[39m'), { output: 'out/main.mp4' })+ assert.deepEqual(parseLine('\x1b[90mCodec h264\x1b[39m'), { codec: 'h264' })assert.deepEqual(parseLine('Rendered 30/60, time remaining: 1s'), { stage: 'rendering', frame: 30, frames: 60, remaining: '1s', progress: 0.5 })assert.deepEqual(parseLine('Rendered 0/60'), { stage: 'rendering', frame: 0, frames: 60, remaining: null, progress: 0.1 })+ assert.deepEqual(parseLine('Rendered 0/0'), { stage: 'rendering', frame: 0, frames: 0, remaining: null, progress: 0.1 })assert.deepEqual(parseLine('Encoded 60/60'), { stage: 'encoding', frame: 60, frames: 60, progress: 1 })+ assert.deepEqual(parseLine('Encoded 0/0'), { stage: 'encoding', frame: 0, frames: 0, progress: 0.9 })assert.deepEqual(parseLine('\x1b[34m+ out/main.mp4\x1b[39m \x1b[90m391.6 kB\x1b[39m'), { output: 'out/main.mp4', stage: 'finishing', progress: 1 })assert.equal(parseLine('Cached bundle. Subsequent renders will be faster.'), null)})@@ -22,19 +36,218 @@ test('prints progress every tenth, and everything else always', () => {assert.deepEqual(printed, [0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60])assert.equal(shouldPrint('Getting composition', last), true)assert.equal(shouldPrint('Encoded 60/60', last), true)+ assert.equal(shouldPrint('Encoded 60/60', last), true, 'the end is always printed')+ const bundling = ['Bundling 1%', 'Bundling 5%', 'Bundling 10%', 'Bundling 100%'].filter((l) => shouldPrint(l, last))+ assert.deepEqual(bundling, ['Bundling 1%', 'Bundling 10%', 'Bundling 100%'])+ assert.equal(shouldPrint('Encoded 3/0', last), true, 'an unknown total counts as the start')+ assert.equal(shouldPrint('Encoded 4/0', last), false)})test('loopback.cjs keeps TCP listeners on 127.0.0.1 and leaves named hosts and pipes alone', async () => {- const { createRequire } = await import('node:module')const { loopbackArgs } = createRequire(import.meta.url)('./loopback.cjs')const f = () => {}assert.deepEqual(loopbackArgs([]), [0, '127.0.0.1'])assert.deepEqual(loopbackArgs([undefined]), [0, '127.0.0.1'])+ assert.deepEqual(loopbackArgs([null, f]), [0, '127.0.0.1', f])assert.deepEqual(loopbackArgs([f]), [0, '127.0.0.1', f])assert.deepEqual(loopbackArgs([3000, f]), [3000, '127.0.0.1', f])+ assert.deepEqual(loopbackArgs(['3000', 511, f]), ['3000', '127.0.0.1', 511, f])assert.deepEqual(loopbackArgs([3000, '::', f]), [3000, '127.0.0.1', f])+ assert.deepEqual(loopbackArgs([3000, '0.0.0.0']), [3000, '127.0.0.1'])assert.deepEqual(loopbackArgs([{ port: 0 }]), [{ port: 0, host: '127.0.0.1' }])+ assert.deepEqual(loopbackArgs([{ port: 0, host: '::' }, f]), [{ port: 0, host: '127.0.0.1' }, f])+ assert.deepEqual(loopbackArgs([{ port: 0, host: 'localhost' }]), [{ port: 0, host: 'localhost' }])assert.deepEqual(loopbackArgs([3000, 'localhost']), [3000, 'localhost'])assert.deepEqual(loopbackArgs(['/tmp/remotion.sock']), ['/tmp/remotion.sock'])- assert.deepEqual(loopbackArgs([{ path: '/tmp/remotion.sock' }]), [{ path: '/tmp/remotion.sock' }])+ for (const key of ['path', 'fd', 'handle', '_handle']) {+ assert.deepEqual(loopbackArgs([{ [key]: 3 }]), [{ [key]: 3 }], key)+ }++ // Required, it patches every server in the process: a listen() with no host lands on loopback.+ const server = net.createServer()+ await new Promise((ok) => server.listen(0, ok))+ try {+ assert.equal(server.address().address, '127.0.0.1')+ } finally {+ await new Promise((ok) => server.close(ok))+ }+})++// ---- $REMOTION against a stand-in CLI --------------------------------------------------------------++function scratch(t, prefix) {+ const dir = realpathSync(mkdtempSync(join(tmpdir(), prefix)))+ t.after(() => rmSync(dir, { recursive: true, force: true }))+ return dir+}++/** A CLI whose behaviour is the given module body; `process.env.SEEN` lets it record render.json mid-run. */+function fakeCli(t, body) {+ const dir = scratch(t, 'remotion-cli-')+ writeFileSync(join(dir, 'cli.mjs'), body)+ writeFileSync(join(dir, 'remotion'), `#!/bin/sh\nexec ${JSON.stringify(process.execPath)} ${JSON.stringify(join(dir, 'cli.mjs'))} "$@"\n`)+ chmodSync(join(dir, 'remotion'), 0o755)+ return join(dir, 'remotion')+}++const SEEN = `import { appendFileSync, readFileSync } from 'node:fs'+const seen = (label) => appendFileSync(process.env.SEEN, label + ' ' + readFileSync(process.env.HARNESS_WORKSPACE + '/.harness/render.json', 'utf8').replace(/\\n\\s*/g, '') + '\\n')+const out = (s) => process.stdout.write(s)+const pause = (ms) => new Promise((ok) => setTimeout(ok, ms))+`++function run(t, args, { cli, workspace, env = {}, onStart } = {}) {+ const ws = workspace ?? scratch(t, 'remotion-ws-')+ const seen = join(ws, 'seen.log')+ const base = { ...process.env }+ delete base.FORCE_COLOR+ const child = spawn(process.execPath, [join(HERE, 'remotion.mjs'), ...args], {+ cwd: ws, env: { ...base, HARNESS_WORKSPACE: ws, REMOTION_CLI: cli, SEEN: seen, ...env }, stdio: ['ignore', 'pipe', 'pipe'],+ })+ let stdout = '', stderr = ''+ child.stdout.on('data', (d) => { stdout += d })+ child.stderr.on('data', (d) => { stderr += d })+ onStart?.(child, () => stdout)+ return new Promise((ok) => child.on('exit', (code, signal) => {+ const file = join(ws, '.harness', 'render.json')+ ok({+ code, signal, stdout, stderr, ws,+ job: existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : null,+ seen: existsSync(seen) ? readFileSync(seen, 'utf8').trim().split('\n').map((l) => [l.slice(0, l.indexOf(' ')), JSON.parse(l.slice(l.indexOf(' ') + 1))]) : [],+ })+ }))+}++test('any other subcommand is Remotion itself: same arguments, same output, same exit code', async (t) => {+ const cli = fakeCli(t, "console.log(JSON.stringify(process.argv.slice(2))); process.exitCode = 3\n")+ const r = await run(t, ['compositions', 'src/index.ts', '--quiet'], { cli })+ assert.deepEqual([r.code, r.stdout], [3, '["compositions","src/index.ts","--quiet"]\n'])+ assert.equal(r.job, null, 'no render, no render.json')++ const killed = await run(t, ['--help'], { cli: fakeCli(t, "process.kill(process.pid, 'SIGKILL')\n") })+ assert.equal(killed.code, 1, 'killed by a signal is a failure')++ const missing = await run(t, ['studio'], { cli: join(scratch(t, 'remotion-none-'), 'remotion') })+ assert.equal(missing.code, 1)+ assert.match(missing.stderr, /^remotion could not start: spawn .*remotion ENOENT\n$/)+})++test('a render writes its progress to .harness/render.json as it goes, and prints every tenth', async (t) => {+ const cli = fakeCli(t, `${SEEN}+seen('start')+out('Bundling 5%\\nBundling 50%\\rBundling 100%\\r\\n')+out('Getting composition\\n')+out('\\x1b[90mComposition Main\\x1b[39m\\n')+out('Codec h264\\n\\n')+out('Output out/main.mp4\\n')+for (let i = 0; i <= 20; i++) out('Rendered ' + i + '/20, time remaining: ' + (20 - i) + 's\\n')+await pause(400)+seen('rendering')+out('Encoded 0/20\\nEncoded 20/20\\n')+process.stderr.write('a warning, on stderr\\n')+out('FORCE_COLOR=' + process.env.FORCE_COLOR + '\\n')+out('\\x1b[34m+ out/main.mp4\\x1b[39m 391.6 kB')+`)+ const r = await run(t, ['render', 'src/index.ts', 'Main', 'out/main.mp4', '--codec=h264'], { cli })+ assert.equal(r.code, 0, r.stderr)+ const [[, start], [, rendering]] = r.seen+ assert.deepEqual([start.state, start.stage, start.progress, start.composition, start.command], ['running', 'starting', 0, 'Main', 'render'])+ assert.deepEqual([rendering.stage, rendering.frame, rendering.frames, rendering.remaining, rendering.codec, rendering.output], ['rendering', 20, 20, '0s', 'h264', 'out/main.mp4'])+ assert.equal(rendering.progress, 0.9)+ const { job } = r+ assert.deepEqual([job.state, job.stage, job.progress, job.composition, job.output, job.frame, job.frames, job.error], ['done', 'done', 1, 'Main', 'out/main.mp4', 20, 20, null])+ assert.ok(job.finishedAt >= job.startedAt && job.updatedAt >= job.finishedAt)+ const lines = r.stdout.split('\n')+ assert.deepEqual(lines.filter((l) => l.startsWith('Rendered')).map((l) => l.split(',')[0]), [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20].map((i) => `Rendered ${i}/20`))+ assert.deepEqual(lines.filter((l) => l.startsWith('Bundling')), ['Bundling 5%', 'Bundling 50%', 'Bundling 100%'])+ assert.ok(lines.includes('FORCE_COLOR=1'), 'colour is kept on for a terminal that reads the output')+ assert.ok(lines.includes('\x1b[34m+ out/main.mp4\x1b[39m 391.6 kB'), 'a last line without a newline still arrives')+ assert.equal(r.stderr, 'a warning, on stderr\n')+})++test('the composition is guessed from the arguments until the CLI names it', async (t) => {+ const cli = fakeCli(t, `${SEEN}\nseen('start')\n`)+ const guess = async (...args) => (await run(t, args, { cli })).seen[0][1].composition+ assert.equal(await guess('still', 'Poster', 'out/poster.png', '--frame=30'), 'Poster')+ assert.equal(await guess('render', 'src/index.tsx', 'Intro'), 'Intro')+ assert.equal(await guess('render'), null)+})++test('an output path is kept relative to the workspace', async (t) => {+ const ws = scratch(t, 'remotion-ws-')+ mkdirSync(join(ws, 'sub'))+ const cli = fakeCli(t, "console.log('Output ../out/a.mp4'); console.log('Output .')\n")+ const r = await run(t, ['render', 'Main'], { cli, workspace: ws, env: { HARNESS_WORKSPACE: ws } })+ assert.equal(r.job.output, '.', 'the workspace itself is named, not left empty')+ const sub = await new Promise((ok) => {+ const child = spawn(process.execPath, [join(HERE, 'remotion.mjs'), 'render', 'Main'], { cwd: join(ws, 'sub'), env: { ...process.env, HARNESS_WORKSPACE: ws, REMOTION_CLI: fakeCli(t, "console.log('Output ../out/a.mp4')\n") }, stdio: 'ignore' })+ child.on('exit', () => ok(JSON.parse(readFileSync(join(ws, '.harness', 'render.json'), 'utf8'))))+ })+ assert.equal(sub.output, 'out/a.mp4')+})++test('a failed render keeps the lines that say why', async (t) => {+ const noisy = fakeCli(t, `+for (let i = 0; i < 25; i++) console.error('webpack ' + i)+console.error("Error: Cannot find module './Missing'")+console.error('✖ Render failed')+console.error('see above')+process.exitCode = 2+`)+ const r = await run(t, ['render', 'Main'], { cli: noisy })+ assert.equal(r.code, 2)+ assert.deepEqual([r.job.state, r.job.stage, r.job.progress], ['failed', 'failed', 0])+ assert.equal(r.job.error, "Error: Cannot find module './Missing'\n✖ Render failed")++ const plain = await run(t, ['render', 'Main'], { cli: fakeCli(t, "console.log('one\\ntwo\\nthree\\nfour'); process.exitCode = 4\n") })+ assert.equal(plain.job.error, 'two\nthree\nfour', 'no error line: the last three')+ const silent = await run(t, ['render', 'Main'], { cli: fakeCli(t, 'process.exit(5)\n') })+ assert.deepEqual([silent.code, silent.job.error], [5, 'remotion exited 5'])++ const missing = await run(t, ['still', 'Poster'], { cli: join(scratch(t, 'remotion-none-'), 'remotion') })+ assert.equal(missing.code, 1)+ assert.deepEqual([missing.job.command, missing.job.state], ['still', 'failed'])+ assert.match(missing.job.error, /spawn .*remotion ENOENT/)+})++test('stopping $REMOTION stops the render and records it as failed', async (t) => {+ const cli = fakeCli(t, "console.log('Rendered 1/10'); setInterval(() => {}, 1000)\n")+ const r = await run(t, ['render', 'Main'], {+ cli,+ onStart: async (child, stdout) => {+ while (!stdout().includes('Rendered 1/10')) await wait(20)+ child.kill('SIGTERM')+ },+ })+ assert.equal(r.code, 1)+ assert.deepEqual([r.job.state, r.job.frame, r.job.error], ['failed', 1, 'Rendered 1/10'])+})++test('without HARNESS_WORKSPACE the workspace is where $REMOTION runs', async (t) => {+ const ws = scratch(t, 'remotion-ws-')+ const child = spawn(process.execPath, [join(HERE, 'remotion.mjs'), 'render', 'Main'], {+ cwd: ws, env: { ...process.env, HARNESS_WORKSPACE: '', REMOTION_CLI: fakeCli(t, "console.log('Composition Main')\n") }, stdio: 'ignore',+ })+ assert.equal(await new Promise((ok) => child.on('exit', ok)), 0)+ assert.equal(JSON.parse(readFileSync(join(ws, '.harness', 'render.json'), 'utf8')).state, 'done')+})++test('a workspace whose .harness cannot be written still renders', async (t) => {+ const ws = scratch(t, 'remotion-ws-')+ writeFileSync(join(ws, '.harness'), 'not a folder')+ const r = await run(t, ['render', 'Main'], { cli: fakeCli(t, "console.log('Rendered 1/2'); console.log('FORCE_COLOR=' + process.env.FORCE_COLOR)\n"), workspace: ws, env: { FORCE_COLOR: '0' } })+ assert.equal(r.code, 0)+ assert.match(r.stdout, /FORCE_COLOR=0/, "the caller's colour setting wins")+ assert.equal(readFileSync(join(ws, '.harness'), 'utf8'), 'not a folder')+})++test('imported, or run without a script path, it does nothing', async (t) => {+ const r = await new Promise((ok) => {+ const child = spawn(process.execPath, ['--input-type=module', '-e', `await import(${JSON.stringify(pathToFileURL(join(HERE, 'remotion.mjs')).href)})`], { env: { ...process.env, REMOTION_CLI: join(scratch(t, 'remotion-none-'), 'remotion') } })+ let out = ''+ child.stdout.on('data', (d) => { out += d })+ child.stderr.on('data', (d) => { out += d })+ child.on('exit', (code) => ok({ code, out }))+ })+ assert.deepEqual(r, { code: 0, out: '' })})diff --git a/store/agents/remotion/toolchain/test_viewer.mjs b/store/agents/remotion/toolchain/test_viewer.mjsnew file mode 100644index 000000000..4ba4617d3--- /dev/null+++ b/store/agents/remotion/toolchain/test_viewer.mjs@@ -0,0 +1,390 @@+// The Remotion pane server (viewer.mjs), run for real against scratch workspaces, with a stand-in for+// Remotion Studio (REMOTION_STUDIO_BIN) so no bundler or browser is involved: node --test toolchain/test_viewer.mjs+import assert from 'node:assert/strict'+import { spawn } from 'node:child_process'+import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readlinkSync, realpathSync, rmSync, symlinkSync, utimesSync, writeFileSync, existsSync } from 'node:fs'+import http from 'node:http'+import net from 'node:net'+import { tmpdir } from 'node:os'+import { dirname, join } from 'node:path'+import { describe, test } from 'node:test'+import { fileURLToPath } from 'node:url'++const PKG = dirname(dirname(fileURLToPath(import.meta.url)))+const wait = (ms) => new Promise((ok) => setTimeout(ok, ms))++// Studio, played by a script: what it was started with goes to $STUB_STUDIO_CONTROL/starts, and+// $STUB_STUDIO_CONTROL/mode says how this start goes — exit (code 3), kill (a signal), serve, or stubborn+// (serve, but ignore the first SIGTERM).+const STUDIO = `#!/usr/bin/env node+import { createServer } from 'node:http'+import { appendFileSync, readFileSync, writeFileSync } from 'node:fs'+const control = process.env.STUB_STUDIO_CONTROL+let mode = 'serve'+try { mode = readFileSync(control + '/mode', 'utf8').trim() } catch {}+appendFileSync(control + '/starts', JSON.stringify({ args: process.argv.slice(2), cwd: process.cwd(), nodeOptions: process.env.NODE_OPTIONS, browser: process.env.BROWSER }) + '\\n')+if (mode === 'exit') { console.log('Studio could not start'); process.exit(3) }+if (mode === 'kill') process.kill(process.pid, 'SIGKILL')+let terms = 0+process.on('SIGTERM', () => { writeFileSync(control + '/stopped', String(++terms)); if (mode !== 'stubborn' || terms > 1) process.exit(0) })+for (let i = 0; i < 45; i++) console.error('\\x1b[90mwebpack line ' + i + '\\x1b[39m')+const port = Number(process.argv[process.argv.indexOf('--port') + 1])+const server = createServer((req, res) => {+ if (req.url === '/destroy') { req.socket.destroy(); return }+ if (req.url === '/reset') { res.writeHead(200, { 'content-type': 'text/plain' }); res.write('partial'); setTimeout(() => req.socket.resetAndDestroy(), 100); return }+ let body = ''+ req.on('data', (c) => { body += c })+ req.on('end', () => { res.writeHead(200, { 'content-type': 'text/plain', 'x-studio': 'stub' }); res.end(req.method + ' ' + req.url + ' ' + body) })+})+server.listen(port, () => { writeFileSync(control + '/address', server.address().address); console.log('Server ready - Local: http://localhost:' + port + ', Network: none') })+`++function scratch(t, prefix) {+ const dir = realpathSync(mkdtempSync(join(tmpdir(), prefix)))+ t.after(() => rmSync(dir, { recursive: true, force: true }))+ return dir+}++function write(root, rel, body = 'x', mtimeSeconds) {+ const file = join(root, rel)+ mkdirSync(dirname(file), { recursive: true })+ writeFileSync(file, body)+ if (mtimeSeconds !== undefined) utimesSync(file, mtimeSeconds, mtimeSeconds)+ return file+}++function freePort() {+ return new Promise((ok) => { const s = net.createServer(); s.listen(0, '127.0.0.1', () => { const p = s.address().port; s.close(() => ok(p)) }) })+}++function request(port, path, { method = 'GET', headers = {}, body } = {}) {+ return new Promise((ok, fail) => {+ const req = http.request({ host: '127.0.0.1', port, path, method, headers, agent: false }, (res) => {+ const chunks = []+ res.on('data', (c) => chunks.push(c))+ res.on('end', () => ok({ status: res.statusCode, headers: res.headers, body: Buffer.concat(chunks).toString() }))+ res.on('error', fail)+ })+ req.on('error', fail)+ req.end(body)+ })+}++async function until(check, { timeout = 15_000, every = 50 } = {}) {+ const end = Date.now() + timeout+ for (;;) {+ const value = await check().catch(() => undefined)+ if (value) return value+ if (Date.now() > end) throw new Error(`timed out waiting for ${check}`)+ await wait(every)+ }+}++const state = async (v) => JSON.parse((await request(v.port, '/__harness/state')).body)++async function startViewer(t, { workspace, mode, env = {} }) {+ const control = scratch(t, 'remotion-studio-')+ const stub = join(control, 'studio.mjs')+ writeFileSync(stub, STUDIO)+ chmodSync(stub, 0o755)+ if (mode) writeFileSync(join(control, 'mode'), mode)+ const port = await freePort()+ const child = spawn(process.execPath, [join(PKG, 'viewer.mjs')], {+ env: { ...process.env, HARNESS_VIEWER_PORT: String(port), HARNESS_WORKSPACE: workspace, REMOTION_STUDIO_BIN: stub, STUB_STUDIO_CONTROL: control, ...env },+ stdio: ['ignore', 'pipe', 'pipe'],+ })+ let output = ''+ child.stdout.on('data', (d) => { output += d })+ child.stderr.on('data', (d) => { output += d })+ const exited = new Promise((ok) => child.on('exit', (code, signal) => ok({ code, signal })))+ const viewer = {+ port, child, control, exited,+ output: () => output,+ starts: () => (existsSync(join(control, 'starts')) ? readFileSync(join(control, 'starts'), 'utf8').trim().split('\n').map((l) => JSON.parse(l)) : []),+ mode: (m) => writeFileSync(join(control, 'mode'), m),+ stop: async (signal = 'SIGTERM') => { if (child.exitCode === null && child.signalCode === null) child.kill(signal); return exited },+ }+ t.after(() => viewer.stop())+ await until(() => request(port, '/__harness/state'))+ return viewer+}++function events(t, port) {+ const seen = []+ const waiters = []+ let buffer = ''+ const req = http.get({ host: '127.0.0.1', port, path: '/__harness/events', agent: false }, (res) => {+ res.on('data', (chunk) => {+ buffer += chunk+ let i+ while ((i = buffer.indexOf('\n\n')) >= 0) {+ const block = buffer.slice(0, i)+ buffer = buffer.slice(i + 2)+ const data = /^data: (.*)$/m.exec(block)+ const item = block.startsWith(':') ? { comment: block } : { event: /^event: (.*)$/m.exec(block)?.[1], data: data && JSON.parse(data[1]) }+ seen.push(item)+ for (const w of [...waiters]) if (w.match(item)) { waiters.splice(waiters.indexOf(w), 1); w.ok(item) }+ }+ })+ })+ req.on('error', () => {})+ const stream = {+ seen,+ next: (match, timeout = 10_000) => new Promise((ok, fail) => {+ const found = seen.find(match)+ if (found) { seen.splice(seen.indexOf(found), 1); return ok(found) }+ const w = { match: (item) => { if (!match(item)) return false; seen.splice(seen.indexOf(item), 1); return true }, ok }+ waiters.push(w)+ setTimeout(() => { if (waiters.includes(w)) { waiters.splice(waiters.indexOf(w), 1); fail(new Error(`no matching event in ${timeout} ms; seen ${JSON.stringify(seen)}`)) } }, timeout)+ }),+ close: () => req.destroy(),+ }+ t.after(stream.close)+ return stream+}++describe('the Remotion pane', { concurrency: true }, () => {+ test('starts Studio on the entry point, on loopback, restarts it when it dies and proxies it once it is ready', async (t) => {+ const ws = scratch(t, 'remotion-ws-')+ write(ws, 'src/index.tsx', 'registerRoot(Root)')+ const v = await startViewer(t, { workspace: ws, mode: 'exit' })++ // Studio exits at once: the pane says so and keeps answering while it waits to restart.+ const stopped = await until(async () => { const s = await state(v); return s.studio.state === 'stopped' && s })+ assert.deepEqual(stopped.studio.log, ['Studio could not start', 'Remotion Studio exited (3).'])+ const html = await request(v.port, '/', { headers: { accept: 'text/html,application/xhtml+xml' } })+ assert.equal(html.status, 503)+ assert.match(html.headers['content-type'], /^text\/html/)+ assert.equal(html.headers['retry-after'], '1')+ assert.match(html.body, /Starting Remotion Studio…/)+ const plain = await request(v.port, '/api/anything')+ assert.deepEqual([plain.status, plain.headers['content-type'], plain.body], [503, 'text/plain', 'Studio is starting'])++ // Killed by a signal on the next start, then up for good.+ v.mode('kill')+ await until(async () => (await state(v)).studio.log.includes('Remotion Studio exited (signal).'))+ v.mode('serve')+ const ready = await until(async () => { const s = await state(v); return s.studio.state === 'ready' && s }, { timeout: 20_000 })+ assert.deepEqual(ready.studio.log, [], 'a ready Studio shows no log')++ const starts = v.starts()+ assert.equal(starts.length, 3)+ const { args, cwd, nodeOptions, browser } = starts[2]+ assert.deepEqual([args[0], args[1], args[3], args[4]], ['studio', '--port', '--no-open', 'src/index.tsx'])+ assert.equal(cwd, ws)+ assert.equal(browser, 'none')+ assert.ok(nodeOptions.includes(`--require ${JSON.stringify(join(PKG, 'toolchain', 'loopback.cjs'))}`), nodeOptions)+ assert.equal(readFileSync(join(v.control, 'address'), 'utf8'), '127.0.0.1', 'loopback.cjs kept a host-less listen() on loopback')+ assert.equal(readlinkSync(join(ws, 'node_modules')), join(PKG, 'node_modules'), 'the shared node_modules is linked in')+ assert.ok(existsSync(join(ws, 'out')))++ const get = await request(v.port, '/hello?x=1')+ assert.deepEqual([get.status, get.headers['x-studio'], get.body], [200, 'stub', 'GET /hello?x=1 '])+ const post = await request(v.port, '/echo', { method: 'POST', body: 'abc' })+ assert.equal(post.body, 'POST /echo abc')+ assert.deepEqual(await request(v.port, '/destroy').then((r) => [r.status, r.body]), [502, 'Studio did not answer'])+ const reset = await request(v.port, '/reset')+ assert.deepEqual([reset.status, reset.body], [200, 'partial'], 'a Studio that dies mid-response ends the response instead of hanging it')++ assert.deepEqual(await v.stop(), { code: 0, signal: null })+ assert.equal(readFileSync(join(v.control, 'stopped'), 'utf8'), '1', 'Studio is stopped with the pane')+ })++ test('a pinned Studio port, the default entry point, and a workspace that does not exist yet', async (t) => {+ const ws = join(scratch(t, 'remotion-missing-'), 'not-yet')+ const studioPort = await freePort()+ const v = await startViewer(t, { workspace: ws, env: { REMOTION_STUDIO_PORT: String(studioPort) } })+ await until(async () => (await state(v)).studio.state === 'ready')+ assert.deepEqual(v.starts()[0].args, ['studio', '--port', String(studioPort), '--no-open', 'src/index.ts'])+ assert.match(v.output(), /\[remotion pane\] watch failed: /)+ assert.ok(existsSync(join(ws, 'out')), 'out/ is made, and the workspace with it')+ const s = await state(v)+ assert.deepEqual([s.renders, s.job, s.compositions, s.active], [[], null, [], null])+ assert.deepEqual(await v.stop('SIGHUP'), { code: 0, signal: null })+ })++ test('Studio that is not installed leaves the pane up, saying so', async (t) => {+ // The package's own node_modules/.bin/remotion unless it is installed here; then a path that is not.+ const installed = existsSync(join(PKG, 'node_modules', '.bin', 'remotion'))+ const env = installed ? { REMOTION_STUDIO_BIN: join(scratch(t, 'remotion-none-'), 'remotion') } : { REMOTION_STUDIO_BIN: '' }+ const v = await startViewer(t, { workspace: scratch(t, 'remotion-ws-'), env })+ const s = await until(async () => { const s = await state(v); return s.studio.state === 'stopped' && s })+ assert.match(s.studio.log[0], /^Remotion Studio could not start: spawn .*remotion ENOENT\.$/)+ assert.equal((await request(v.port, '/')).status, 503)+ assert.deepEqual(await v.stop(), { code: 0, signal: null })+ })++ test('a Studio that ignores the first SIGTERM is asked again as the pane exits', async (t) => {+ const v = await startViewer(t, { workspace: scratch(t, 'remotion-ws-'), mode: 'stubborn' })+ await until(async () => (await state(v)).studio.state === 'ready')+ assert.deepEqual(await v.stop(), { code: 0, signal: null })+ await until(async () => readFileSync(join(v.control, 'stopped'), 'utf8') === '2', { timeout: 5_000 })+ })++ test('stopping while Studio waits to restart does not start it again', async (t) => {+ const ws = scratch(t, 'remotion-ws-')+ const v = await startViewer(t, { workspace: ws, mode: 'exit' })+ await until(async () => (await state(v)).studio.state === 'stopped')+ v.mode('serve')+ assert.deepEqual(await v.stop('SIGINT'), { code: 0, signal: null })+ await wait(1200)+ assert.equal(v.starts().length, 1)+ })++ test('state: the renders in out/, whether they are stale, the render job, and the compositions', async (t) => {+ const ws = scratch(t, 'remotion-ws-')+ symlinkSync(join(ws, 'nowhere'), join(ws, 'node_modules')) // a link already there is left alone+ const v = await startViewer(t, { workspace: ws })+ assert.equal(readlinkSync(join(ws, 'node_modules')), join(ws, 'nowhere'))++ write(ws, 'src/Main.tsx', 'export const Main = () => null', 1_000)+ symlinkSync(join(ws, 'gone.tsx'), join(ws, 'src', 'dangling.tsx')) // unreadable sources are skipped+ write(ws, 'out/main.mp4', 'video', 2_000) // newer than src/+ write(ws, 'out/anim.gif', 'gif', 900) // older than src/ by 100 s: stale+ write(ws, 'out/frame.png', 'png', 800)+ write(ws, 'out/voice.mp3', 'mp3', 700)+ write(ws, 'out/notes.txt', 'text', 3_000) // not media+ write(ws, 'out/empty.mp4', '', 3_000) // still being written+ write(ws, 'out/.partial.mp4', 'x', 3_000) // hidden+ write(ws, 'out/node_modules/cache.mp4', 'x', 3_000)+ write(ws, 'out/a/b/c/d/deep.mp4', 'deep', 600) // four folders down: found+ write(ws, 'out/a/b/c/d/e/deeper.mp4', 'x', 3_000) // five: not walked+ symlinkSync(join(ws, 'missing.mp4'), join(ws, 'out', 'broken.mp4'))+ let s = await state(v)+ assert.deepEqual(s.renders.map((r) => [r.path, r.kind, r.size, r.stale]), [+ ['out/main.mp4', 'video', 5, false],+ ['out/anim.gif', 'gif', 3, true],+ ['out/frame.png', 'image', 3, true],+ ['out/voice.mp3', 'audio', 3, true],+ ['out/a/b/c/d/deep.mp4', 'video', 4, true],+ ])+ assert.equal(s.renders[0].name, 'main.mp4')+ assert.equal(s.job, null)++ // A running job whose process is gone was interrupted; one still alive, or with no pid, is running.+ const dead = spawn(process.execPath, ['-e', ''])+ await new Promise((ok) => dead.on('exit', ok))+ write(ws, '.harness/render.json', JSON.stringify({ state: 'running', pid: dead.pid, progress: 0.4 }))+ assert.equal((await state(v)).job.state, 'interrupted')+ write(ws, '.harness/render.json', JSON.stringify({ state: 'running', pid: process.pid }))+ assert.equal((await state(v)).job.state, 'running')+ write(ws, '.harness/render.json', JSON.stringify({ state: 'running' }))+ assert.equal((await state(v)).job.state, 'running')+ write(ws, '.harness/render.json', JSON.stringify({ state: 'done', pid: dead.pid }))+ assert.equal((await state(v)).job.state, 'done')+ write(ws, '.harness/render.json', '{ half written')+ assert.equal((await state(v)).job, null)++ // Compositions as Root registers them; the one whose component file changed last is active.+ write(ws, 'src/Root.tsx', `import React from "react";+import { Composition, Still } from "remotion";+import { Main, Other as Renamed, } from "./Main";+import Intro from './scenes/Intro'+import { Missing } from "./Missing";+import { Folder } from "./folder";+export const Root = () => (<>+ <Composition id="Main" component={Main} durationInFrames={30} fps={30} width={1920} height={1080} />+ <Composition+ id='Intro'+ component={Intro}+ />+ <Still id={\`Poster\`} component={Renamed} />+ <Composition id={"Gone"} component={Missing} />+ <Composition id={ 'Dir' } component={Folder} />+ <Composition id="Inline" component={() => null} />+ <Composition component={Main} />+</>)`, 1_000)+ write(ws, 'src/scenes/Intro.tsx', 'export default () => null', 5_000)+ write(ws, 'src/folder/index.ts', 'export const Folder = () => null', 4_000)+ s = await state(v)+ assert.deepEqual(s.compositions, ['Main', 'Intro', 'Poster', 'Gone', 'Dir', 'Inline'])+ assert.equal(s.active, 'Intro')+ write(ws, 'src/folder/index.ts', 'export const Folder = () => null', 6_000)+ assert.equal((await state(v)).active, 'Dir', 'a component that is a folder resolves to its index')++ rmSync(join(ws, 'src', 'Root.tsx'))+ write(ws, 'src/Root.jsx', '<Still id="Only" component={Nothing} />')+ assert.deepEqual((await state(v)).compositions, ['Only'])+ rmSync(join(ws, 'src', 'Root.jsx'))+ mkdirSync(join(ws, 'src', 'Root.ts')) // exists, but cannot be read+ assert.deepEqual((await state(v)).compositions, [])+ assert.equal((await state(v)).active, null)+ })++ test('media: whole files, byte ranges and HEAD for the renders tab, and nothing outside the workspace', async (t) => {+ const ws = scratch(t, 'remotion-ws-')+ write(ws, 'out/clip.mp4', '0123456789')+ write(ws, 'out/notes.txt', 'text')+ mkdirSync(join(ws, 'out', 'folder.mp4'))+ write(ws, 'out/My Launch 100%.webm', 'webm')+ const v = await startViewer(t, { workspace: ws })+ const media = (path, opts) => request(v.port, `/__harness/media/${path}`, opts)++ const whole = await media('out/clip.mp4')+ assert.deepEqual([whole.status, whole.body, whole.headers['content-type'], whole.headers['content-length'], whole.headers['accept-ranges']], [200, '0123456789', 'video/mp4', '10', 'bytes'])+ const head = await media('out/clip.mp4', { method: 'HEAD' })+ assert.deepEqual([head.status, head.body, head.headers['content-length']], [200, '', '10'])+ assert.equal((await media('out/My%20Launch%20100%25.webm')).body, 'webm')++ const range = async (value, method) => { const r = await media('out/clip.mp4', { method, headers: { range: value } }); return [r.status, r.body, r.headers['content-range'] ?? null] }+ assert.deepEqual(await range('bytes=2-5'), [206, '2345', 'bytes 2-5/10'])+ assert.deepEqual(await range('bytes=7-'), [206, '789', 'bytes 7-9/10'])+ assert.deepEqual(await range('bytes=-3'), [206, '789', 'bytes 7-9/10'])+ assert.deepEqual(await range('bytes=-30'), [206, '0123456789', 'bytes 0-9/10'])+ assert.deepEqual(await range('bytes=4-400'), [206, '456789', 'bytes 4-9/10'])+ assert.deepEqual(await range('bytes=20-'), [416, '', 'bytes */10'])+ assert.deepEqual(await range('bytes=2-5', 'HEAD'), [206, '', 'bytes 2-5/10'])+ assert.deepEqual(await range('bytes=-'), [200, '0123456789', null])+ assert.deepEqual(await range('items=1-2'), [200, '0123456789', null])++ for (const path of ['out/missing.mp4', 'out/notes.txt', '', '..%2F..%2Fetc%2Fhosts.mp4', 'out/folder.mp4']) {+ assert.equal((await media(path)).status, 404, path)+ }+ assert.equal((await media('out/%E0%A4%A.mp4')).status, 400, 'a malformed escape is a bad request')+ assert.equal((await request(v.port, '/__harness/state')).status, 200, 'and the pane is still up')++ for (const path of ['/__harness', '/__harness/']) {+ const page = await request(v.port, path)+ assert.deepEqual([page.status, page.headers['content-type']], [200, 'text/html; charset=utf-8'])+ assert.equal(page.body, readFileSync(join(PKG, 'viewer.html'), 'utf8'))+ }+ })++ test('events: the state on connect, again when src/, out/ or the render job change, and when a render dies', async (t) => {+ const ws = scratch(t, 'remotion-ws-')+ mkdirSync(join(ws, 'node_modules')) // its own, so not linked+ const v = await startViewer(t, { workspace: ws })+ const stream = events(t, v.port)+ const first = await stream.next((e) => e.event === 'state')+ assert.deepEqual(first.data.renders, [])++ write(ws, 'src/Root.tsx', '<Composition id="Main" component={Main} />')+ write(ws, 'src/Root.tsx', '<Composition id="Main" component={Main} /><Composition id="Two" component={Main} />')+ assert.deepEqual((await stream.next((e) => e.data?.compositions?.length === 2)).data.compositions, ['Main', 'Two'])+ write(ws, 'out/main.mp4', 'video')+ await stream.next((e) => e.data?.renders?.length === 1)++ // A render whose process is killed never writes its own end: the slow tick notices.+ const renderer = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'])+ t.after(() => renderer.kill())+ write(ws, '.harness/render.json', JSON.stringify({ state: 'running', pid: renderer.pid, progress: 0.5 }))+ assert.equal((await stream.next((e) => e.data?.job?.state === 'running')).data.job.progress, 0.5)+ renderer.kill('SIGKILL')+ await stream.next((e) => e.data?.job?.state === 'interrupted', 5_000)++ // Dependencies, git and anything else in the workspace are not the pane's business.+ const before = stream.seen.filter((e) => e.event).length+ write(ws, 'node_modules/remotion/index.js', 'x')+ write(ws, '.git/HEAD', 'ref')+ write(ws, 'README.md', 'x')+ await wait(2_500)+ assert.equal(stream.seen.filter((e) => e.event).length, before, JSON.stringify(stream.seen))+ })++ test('a ping every 20 seconds keeps the event stream open', { timeout: 40_000 }, async (t) => {+ const v = await startViewer(t, { workspace: scratch(t, 'remotion-ws-') })+ const stream = events(t, v.port)+ await stream.next((e) => e.event === 'state')+ assert.deepEqual(await stream.next((e) => e.comment === ': ping', 25_000), { comment: ': ping' })+ })+})diff --git a/store/agents/remotion/viewer.mjs b/store/agents/remotion/viewer.mjsindex b4f4b6100..74530374d 100644--- a/store/agents/remotion/viewer.mjs+++ b/store/agents/remotion/viewer.mjs@@ -6,7 +6,8 @@// /__harness/media/<p> a render from the workspace, with Range support (a <video> needs it to seek)// everything else proxied to Studio, which runs on a private loopback port//-// Studio is started by this process (`remotion studio`, BROWSER=none) and restarted if it dies. It is+// Studio is started by this process (`remotion studio`, BROWSER=none; REMOTION_STUDIO_BIN names another+// binary, which is how the tests stand in for it) and restarted if it dies or cannot start. It is// proxied rather than framed cross-origin so it shares the pane's origin: its own same-origin checks// pass untouched, and the pane can follow which composition is open. Remotion binds Studio to every// interface and has no flag for it, so toolchain/loopback.cjs is preloaded into Studio's process to@@ -24,7 +25,7 @@ import { fileURLToPath } from 'node:url'const here = dirname(fileURLToPath(import.meta.url))const port = Number(process.env.HARNESS_VIEWER_PORT)const workspace = resolve(process.env.HARNESS_WORKSPACE)-const remotionBin = join(here, 'node_modules', '.bin', 'remotion')+const remotionBin = process.env.REMOTION_STUDIO_BIN || join(here, 'node_modules', '.bin', 'remotion')const VIDEO = new Set(['.mp4', '.webm', '.mov', '.mkv', '.m4v'])const IMAGE = new Set(['.gif', '.png', '.jpg', '.jpeg', '.webp'])const AUDIO = new Set(['.mp3', '.wav', '.aac', '.m4a'])@@ -43,6 +44,7 @@ if (!existsSync(join(workspace, 'node_modules'))) {// ---- Studio -------------------------------------------------------------------------------------const studio = { state: 'starting', port: null, log: [], restarts: 0, child: null, since: Date.now() }let stopping = false+let restart = nullfunction freePort() {const wanted = Number(process.env.REMOTION_STUDIO_PORT || 0)@@ -79,15 +81,19 @@ async function startStudio() {}child.stdout.on('data', onData)child.stderr.on('data', onData)- child.on('exit', (code) => {+ const ended = (why) => {studio.child = nullif (stopping) returnstudio.state = 'stopped'; studio.port = null- studio.log.push(`Remotion Studio exited (${code ?? 'signal'}).`)+ studio.log.push(`Remotion Studio ${why}.`)broadcast()const delay = Math.min(30_000, 1000 * 2 ** studio.restarts++)- setTimeout(() => { if (!stopping) startStudio() }, delay)- })+ restart = setTimeout(startStudio, delay)+ }+ child.on('exit', (code) => ended(`exited (${code ?? 'signal'})`))+ // A Studio that cannot be spawned at all (no node_modules yet) emits only 'error', and an 'error'+ // with no listener would take the pane down with it.+ child.on('error', (error) => ended(`could not start: ${error.message}`))}// ---- Workspace state ----------------------------------------------------------------------------@@ -194,7 +200,7 @@ let timer = nullconst soon = () => { if (timer) clearTimeout(timer); timer = setTimeout(broadcast, 120) }try {watch(workspace, { recursive: true }, (_e, name) => {- const n = String(name ?? '').split(sep).join('/')+ const n = String(name).split(sep).join('/')if (n.startsWith('node_modules') || n.startsWith('.git/')) returnif (n.startsWith('out/') || n === 'out' || n.startsWith('src/') || n === '.harness/render.json') soon()})@@ -213,7 +219,7 @@ function safe(rel) {function sendMedia(req, res, rel) {const full = safe(rel)const ext = extname(rel).toLowerCase()- if (!full || !TYPES[ext] || !existsSync(full)) { res.writeHead(404); res.end('not found'); return }+ if (!full || !TYPES[ext] || !existsSync(full) || !statSync(full).isFile()) { res.writeHead(404); res.end('not found'); return }const size = statSync(full).sizeconst headers = { 'content-type': TYPES[ext], 'accept-ranges': 'bytes', 'cache-control': 'no-store' }const range = req.headers.range && req.headers.range.match(/bytes=(\d*)-(\d*)/)@@ -240,7 +246,7 @@ function proxy(req, res) {return}const upstream = http.request({ host: '127.0.0.1', port: studio.port, method: req.method, path: req.url, headers: req.headers }, (up) => {- res.writeHead(up.statusCode ?? 502, up.headers)+ res.writeHead(up.statusCode, up.headers)up.pipe(res)})upstream.on('error', () => { if (!res.headersSent) { res.writeHead(502); res.end('Studio did not answer') } else res.end() })@@ -249,7 +255,7 @@ function proxy(req, res) {}const server = http.createServer((req, res) => {- const url = new URL(req.url ?? '/', 'http://127.0.0.1')+ const url = new URL(req.url, 'http://127.0.0.1')const path = url.pathnameif (path === '/__harness' || path === '/__harness/') {res.writeHead(200, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' })@@ -268,7 +274,13 @@ const server = http.createServer((req, res) => {res.end(JSON.stringify(state()))return}- if (path.startsWith('/__harness/media/')) { sendMedia(req, res, decodeURIComponent(path.slice('/__harness/media/'.length))); return }+ if (path.startsWith('/__harness/media/')) {+ // A malformed escape would throw out of the request handler and take the whole pane down.+ let rel+ try { rel = decodeURIComponent(path.slice('/__harness/media/'.length)) } catch { res.writeHead(400); res.end('bad path'); return }+ sendMedia(req, res, rel)+ return+ }proxy(req, res)})server.listen(port, '127.0.0.1', () => {@@ -280,8 +292,9 @@ server.listen(port, '127.0.0.1', () => {for (const sig of ['SIGINT', 'SIGTERM', 'SIGHUP']) {process.on(sig, () => {stopping = true- try { studio.child?.kill('SIGTERM') } catch {}+ clearTimeout(restart)+ studio.child?.kill('SIGTERM')setTimeout(() => process.exit(0), 300).unref()})}-process.on('exit', () => { try { studio.child?.kill('SIGTERM') } catch {} })+process.on('exit', () => { studio.child?.kill('SIGTERM') })