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51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env node
/**

 * PR #10 HTTP end-to-end acceptance runner.

 *

 * Requires a running ManimCat app with an isolated Redis, real Python/bubblewrap,

 * ffmpeg, and a deterministic OpenAI-compatible stub at 127.0.0.1:19001. It never

 * uses a real provider or credentials.

 */
import fs from 'node:fs'
import path from 'node:path'
import { spawnSync } from 'node:child_process'

const BASE = (process.env.BASE_URL || 'http://127.0.0.1:7860').replace(/\/$/, '')
const EVIDENCE = process.env.EVIDENCE_DIR || '/evidence'
const AUTH = `Bearer byok_${'a'.repeat(64)}`
const CLIENT = 'pr10-http-e2e'
const STUB_BASE = process.env.STUB_BASE_URL || 'http://127.0.0.1:19001/v1'
const CUSTOM_API = {
  apiUrl: STUB_BASE,
  apiKey: 'pr10-stub',
  model: 'pr10-stub',
}

const results = []
let failures = 0

function log(message) {
  console.log(message)
}

function record(name, pass, detail, evidence = undefined) {
  results.push({ name, pass, detail, ...(evidence ? { evidence } : {}) })
  if (!pass) failures += 1
  log(`${pass ? 'PASS' : 'FAIL'} ${name}${detail ? `: ${detail}` : ''}`)
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

async function api(route, options = {}) {
  const response = await fetch(`${BASE}${route}`, {
    method: options.method || 'GET',
    headers: {
      authorization: AUTH,
      'x-client-id': CLIENT,
      ...(options.body === undefined ? {} : { 'content-type': 'application/json' }),
      ...(options.headers || {}),
    },
    ...(options.body === undefined ? {} : { body: JSON.stringify(options.body) }),
  })
  const text = await response.text()
  let json
  try { json = text ? JSON.parse(text) : {} } catch { json = { raw: text } }
  if (!response.ok && options.allowError !== true) {
    throw new Error(`${options.method || 'GET'} ${route} -> ${response.status}: ${text.slice(0, 1200)}`)
  }
  return { status: response.status, json, text, headers: response.headers }
}

async function saveUrl(url, fileName) {
  const response = await fetch(`${BASE}${url}`)
  if (!response.ok) throw new Error(`fetch ${url} -> ${response.status}`)
  const bytes = Buffer.from(await response.arrayBuffer())
  const target = path.join(EVIDENCE, fileName)
  fs.writeFileSync(target, bytes)
  return { target, bytes }
}

function ffprobe(file) {
  const result = spawnSync('ffprobe', [
    '-v', 'error', '-print_format', 'json', '-show_format', '-show_streams', file,
  ], { encoding: 'utf8' })
  if (result.status !== 0) throw new Error(`ffprobe failed: ${result.stderr}`)
  return JSON.parse(result.stdout)
}

function audioLevel(file) {
  const result = spawnSync('ffmpeg', [
    '-hide_banner', '-i', file, '-af', 'volumedetect', '-f', 'null', '-',
  ], { encoding: 'utf8' })
  const match = /mean_volume:\s*(-?\d+(?:\.\d+)?) dB/.exec(result.stderr || '')
  return match ? Number(match[1]) : null
}

function assertVideo(file, options = {}) {
  const probe = ffprobe(file)
  const streams = probe.streams || []
  const video = streams.find(stream => stream.codec_type === 'video')
  if (!video) throw new Error('no video stream')
  if (!Number.isFinite(Number(probe.format?.duration)) || Number(probe.format.duration) <= 0) {
    throw new Error(`invalid duration ${probe.format?.duration}`)
  }
  if (options.audio) {
    const audio = streams.find(stream => stream.codec_type === 'audio')
    if (!audio) throw new Error('narrated output has no audio stream')
    const level = audioLevel(file)
    if (level === null) throw new Error('could not measure audio level')
    if (level <= -60) throw new Error(`audio track is effectively silent (${level} dB)`)
    return { probe, video, audio, audioLevelDb: level }
  }
  return { probe, video }
}

function assertPng(bytes) {
  const signature = [137, 80, 78, 71, 13, 10, 26, 10]
  if (bytes.length < 8 || signature.some((value, index) => bytes[index] !== value)) {
    throw new Error('not a PNG signature')
  }
  return bytes.length
}

async function waitForJob(jobId, timeoutMs = 240_000) {
  const deadline = Date.now() + timeoutMs
  let last
  while (Date.now() < deadline) {
    const response = await api(`/api/jobs/${jobId}`)
    last = response.json
    if (last.status === 'completed' || last.status === 'failed') return last
    await sleep(1000)
  }
  throw new Error(`job ${jobId} did not finish: ${JSON.stringify(last)}`)
}

async function submitJob(body) {
  const response = await api('/api/generate', { method: 'POST', body })
  if (response.status !== 202 || !response.json.jobId) {
    throw new Error(`generate did not return 202/jobId: ${response.status} ${response.text.slice(0, 800)}`)
  }
  return response.json.jobId
}

async function waitForStudioRun(runId, timeoutMs = 240_000) {
  const deadline = Date.now() + timeoutMs
  let last
  while (Date.now() < deadline) {
    const response = await api(`/api/studio-agent/runs/${runId}`)
    last = response.json?.data?.run
    if (!last) throw new Error(`studio run response shape: ${response.text.slice(0, 800)}`)
    if (['completed', 'failed', 'cancelled'].includes(last.status)) return last
    await sleep(1000)
  }
  throw new Error(`studio run ${runId} did not finish: ${JSON.stringify(last)}`)
}

async function studioSnapshot(sessionId) {
  const response = await api(`/api/studio-agent/sessions/${sessionId}`)
  if (!response.json?.ok) throw new Error(`studio snapshot: ${response.text.slice(0, 800)}`)
  return response.json.data
}

async function studioCase(name, studioKind, inputText, expected) {
  const sessionResponse = await api('/api/studio-agent/sessions', {
    method: 'POST',
    body: { studioKind, title: `PR10 ${studioKind}` },
  })
  if (sessionResponse.status !== 200 || !sessionResponse.json?.ok) {
    throw new Error(`studio session creation failed: ${sessionResponse.text.slice(0, 800)}`)
  }
  const session = sessionResponse.json.data.session
  const runResponse = await api('/api/studio-agent/runs', {
    method: 'POST',
    body: {
      sessionId: session.id,
      inputText,
      customApiConfig: CUSTOM_API,
      providerConfig: CUSTOM_API,
      toolChoice: 'auto',
    },
  })
  if (runResponse.status !== 202 || !runResponse.json?.ok) {
    throw new Error(`studio run start failed: ${runResponse.status} ${runResponse.text.slice(0, 1000)}`)
  }
  const run = runResponse.json.data.run
  const finalRun = await waitForStudioRun(run.id)
  const snapshot = await studioSnapshot(session.id)
  const render = [...(snapshot.renders || [])].sort((a, b) => String(b.createdAt).localeCompare(String(a.createdAt)))[0]
  if (expected.runStatus !== 'any' && finalRun.status !== expected.runStatus) {
    throw new Error(`studio run status ${finalRun.status} != ${expected.runStatus}; render=${JSON.stringify(render)}`)
  }
  if (expected.renderStatus && render?.status !== expected.renderStatus) {
    throw new Error(`studio render status ${render?.status} != ${expected.renderStatus}; runError=${finalRun.error}`)
  }
  if (expected.media === 'video') {
    const attachment = (render?.attachments || []).find(item => String(item.mimeType || '').startsWith('video/') || /\.mp4$/i.test(String(item.path || '')))
    if (!attachment) throw new Error(`no completed video attachment: ${JSON.stringify(render)}`)
    const saved = await saveUrl(attachment.path, `${name}.mp4`)
    const info = assertVideo(saved.target, { audio: false })
    return { name, detail: `${attachment.path} ${info.video.codec_name} ${info.probe.format.duration}s`, evidence: saved.target }
  }
  if (expected.media === 'image') {
    const attachment = (render?.attachments || []).find(item => String(item.mimeType || '').startsWith('image/') || /^data:image\/png/i.test(String(item.path || '')))
    if (!attachment) throw new Error(`no completed image attachment: ${JSON.stringify(render)}`)
    let bytes
    if (String(attachment.path).startsWith('data:image/png;base64,')) {
      bytes = Buffer.from(String(attachment.path).slice('data:image/png;base64,'.length), 'base64')
    } else {
      bytes = (await saveUrl(attachment.path, `${name}.png`)).bytes
    }
    const target = path.join(EVIDENCE, `${name}.png`)
    fs.writeFileSync(target, bytes)
    const size = assertPng(bytes)
    return { name, detail: `image ${size} bytes`, evidence: target }
  }
  if (expected.media === 'failed') {
    const failedRender = (snapshot.renders || []).find(item => item.status === 'failed')
    if (!failedRender) throw new Error(`no failed render recorded: ${JSON.stringify(snapshot.renders)}`)
    return { name, detail: `render failed as required: ${failedRender.error || 'no public error'}`, evidence: undefined }
  }
  return { name, detail: finalRun.status, evidence: undefined }
}

async function main() {
  fs.mkdirSync(EVIDENCE, { recursive: true })

  const health = await api('/health')
  record('health', health.status === 200, `${health.status}`)

  // 1. Workflow video with real narration/TTS/mux/audio.
  {
    const jobId = await submitJob({
      concept: 'PR10 narrated dot',
      outputMode: 'video',
      quality: 'low',
      videoConfig: {
        frameRate: 10,
        narration: { enabled: true, voice: 'en-US-AriaNeural', rate: '+0%' },
        timeout: 180000,
      },
      customApiConfig: CUSTOM_API,
      providerConfig: CUSTOM_API,
    })
    const job = await waitForJob(jobId)
    if (job.status !== 'completed') throw new Error(`narrated job failed: ${JSON.stringify(job).slice(0, 2000)}`)
    const video = await saveUrl(job.video_url, 'workflow-narration.mp4')
    const info = assertVideo(video.target, { audio: true })
    const vttResponse = await fetch(`${BASE}${job.video_url.replace(/\.mp4$/, '.vtt')}`)
    const vtt = vttResponse.ok ? await vttResponse.text() : ''
    if (!vtt.trim()) throw new Error('missing WebVTT sidecar')
    fs.writeFileSync(path.join(EVIDENCE, 'workflow-narration.vtt'), vtt)
    record('workflow video narration', true, `${jobId} ${info.probe.format.duration}s audio=${info.audio.codec_name} mean=${info.audioLevelDb}dB vtt=${vtt.length}B`, video.target)
  }

  // 2. Workflow image via the real AI->YON block->ManimPNG path.
  {
    const jobId = await submitJob({
      concept: 'PR10 image dot',
      outputMode: 'image',
      quality: 'low',
      customApiConfig: CUSTOM_API,
      providerConfig: CUSTOM_API,
    })
    const job = await waitForJob(jobId)
    if (job.status !== 'completed' || !Array.isArray(job.image_urls) || job.image_urls.length === 0) {
      throw new Error(`image job did not complete with an image: ${JSON.stringify(job).slice(0, 2000)}`)
    }
    const image = await saveUrl(job.image_urls[0], 'workflow-image.png')
    const size = assertPng(image.bytes)
    record('workflow image', true, `${jobId} ${job.image_urls[0]} ${size}B`, image.target)
  }

  // 3. Edited rerender through /api/modify (real AI edit + render).
  {
    const sourceCode = [
      'from manimgx import *',
      '',
      'class MainScene(Scene):',
      '    def construct(self):',
      '        self.add(Dot())',
      '        self.wait(0.2)',
    ].join('\n')
    const response = await api('/api/modify', {
      method: 'POST',
      body: {
        concept: 'PR10 edited rerender',
        outputMode: 'video',
        quality: 'low',
        instructions: 'Replace the dot with a square.',
        code: sourceCode,
        customApiConfig: CUSTOM_API,
      providerConfig: CUSTOM_API,
        videoConfig: { frameRate: 10, timeout: 180000 },
      },
    })
    if (response.status !== 202 || !response.json.jobId) throw new Error(`modify rejected: ${response.status} ${response.text.slice(0, 800)}`)
    const job = await waitForJob(response.json.jobId)
    if (job.status !== 'completed') throw new Error(`edited job failed: ${JSON.stringify(job).slice(0, 2000)}`)
    if (!String(job.code || '').includes('Square')) throw new Error(`edited code did not contain Square(): ${String(job.code).slice(0, 500)}`)
    const video = await saveUrl(job.video_url, 'workflow-edited.mp4')
    const info = assertVideo(video.target, { audio: false })
    record('edited rerender', true, `${response.json.jobId} ${info.probe.format.duration}s Square present`, video.target)
  }

  // 4. Code retry: first render raises, AI patch produces a working video.
  {
    const jobId = await submitJob({
      concept: 'PR10_RETRY retry marker',
      outputMode: 'video',
      quality: 'low',
      videoConfig: { frameRate: 10, timeout: 180000 },
      customApiConfig: CUSTOM_API,
      providerConfig: CUSTOM_API,
    })
    const job = await waitForJob(jobId)
    if (job.status !== 'completed') throw new Error(`retry job failed: ${JSON.stringify(job).slice(0, 2000)}`)
    if (String(job.code || '').includes('PR10_RETRY')) throw new Error('retry job still contains the failing line')
    const video = await saveUrl(job.video_url, 'workflow-retry.mp4')
    const info = assertVideo(video.target, { audio: false })
    record('code retry', true, `${jobId} retry=${job.timings?.retry ?? 'unknown'} ${info.probe.format.duration}s`, video.target)
  }

  // 5. Cancellation of a genuinely active render.
  {
    const longCode = [
      'from manimgx import *',
      'import time',
      '',
      'class MainScene(Scene):',
      '    def construct(self):',
      '        time.sleep(120)',
    ].join('\n')
    const jobId = await submitJob({
      concept: 'PR10 cancellation',
      outputMode: 'video',
      quality: 'low',
      code: longCode,
      videoConfig: { frameRate: 10, timeout: 180000 },
      customApiConfig: CUSTOM_API,
      providerConfig: CUSTOM_API,
    })
    const activeDeadline = Date.now() + 30_000
    let active
    while (Date.now() < activeDeadline) {
      const response = await api(`/api/jobs/${jobId}`)
      active = response.json
      if (active.status === 'processing' && (active.stage === 'rendering' || active.stage === 'analyzing')) break
      await sleep(500)
    }
    if (active?.status !== 'processing') throw new Error(`job never became active: ${JSON.stringify(active)}`)
    const cancel = await api(`/api/jobs/${jobId}/cancel`, { method: 'POST', body: {} })
    if (cancel.status !== 200) throw new Error(`cancel route failed: ${cancel.status} ${cancel.text.slice(0, 500)}`)
    const job = await waitForJob(jobId, 30_000)
    const cancelled = job.status === 'failed' && (job.cancel_reason || job.error || '').match(/cancel/i)
    if (!cancelled) throw new Error(`job was not reported cancelled: ${JSON.stringify(job).slice(0, 1200)}`)
    await sleep(2500)
    record('cancellation', true, `${jobId} status=${job.status} reason=${job.cancel_reason || job.error}`, undefined)
  }

  // 6. Hostile source through the actual generate route: host file/env/process/network/read-only input.
  {
    const isolationCode = [
      'import os',
      'import socket',
      'from manimgx import *',
      '',
      'def check_isolation():',
      "    assert not os.path.exists('/etc/passwd'), 'host /etc/passwd is visible'",
      "    assert not os.path.exists('/app/package.json'), 'host app tree is visible'",
      "    assert os.environ.get('PR10_HOST_SECRET') is None, 'host environment secret leaked'",
      '    try:',
      "        with open('/input/blocked.txt', 'w') as handle:",
      "            handle.write('blocked')",
      '    except OSError:',
      '        pass',
      '    else:',
      "        raise AssertionError('/input is writable')",
      '    probe = socket.socket(socket.AF_INET, socket.SOCK_STREAM)',
      '    probe.settimeout(1)',
      '    try:',
      "        probe.connect(('127.0.0.1', 19001))",
      '    except OSError:',
      '        pass',
      '    else:',
      "        raise AssertionError('sandbox reached the host fixture port')",
      '    finally:',
      '        probe.close()',
      '',
      'check_isolation()',
      '',
      'class MainScene(Scene):',
      '    def construct(self):',
      '        self.add(Dot())',
      '        self.wait(0.2)',
    ].join('\n')
    const jobId = await submitJob({
      concept: 'PR10_ISOLATION hostile source',
      outputMode: 'video',
      quality: 'low',
      code: isolationCode,
      videoConfig: { frameRate: 10, timeout: 180000 },
      customApiConfig: CUSTOM_API,
      providerConfig: CUSTOM_API,
    })
    const job = await waitForJob(jobId)
    if (job.status !== 'completed') throw new Error(`isolation probe failed inside sandbox: ${JSON.stringify(job).slice(0, 3000)}`)
    const video = await saveUrl(job.video_url, 'workflow-isolation.mp4')
    assertVideo(video.target, { audio: false })
    record('hostile isolation', true, `${jobId} host paths/env/network/read-only input denied`, video.target)
  }

  // 7. Studio Matplotlib render: real sandboxed Python, real PNG attachment.
  {
    const result = await studioCase('studio-matplotlib', 'matplotlib', 'Render a simple plot', {
      runStatus: 'completed',
      renderStatus: 'completed',
      media: 'image',
    })
    record('studio matplotlib render', true, result.detail, result.evidence)
  }

  // 8. Studio Manim render: real queue + bubblewrap + video.
  {
    const result = await studioCase('studio-manim', 'manim', 'Render a dot animation', {
      runStatus: 'completed',
      renderStatus: 'completed',
      media: 'video',
    })
    record('studio manim render', true, result.detail, result.evidence)
  }

  // 9. Hostile symlink export must be rejected by artifact publication, not delivered.
  {
    const result = await studioCase('studio-hostile-symlink', 'matplotlib', 'hostile symlink export', {
      runStatus: 'any',
      renderStatus: 'failed',
      media: 'failed',
    })
    record('hostile symlink export rejected', true, result.detail, result.evidence)
  }

  fs.writeFileSync(path.join(EVIDENCE, 'http-e2e-results.json'), JSON.stringify({
    base: BASE,
    results,
    failures,
    finishedAt: new Date().toISOString(),
  }, null, 2))
  if (failures > 0) {
    log(`RESULT FAIL ${failures} case(s)`)
    process.exit(1)
  }
  log(`RESULT PASS ${results.length} case(s)`)
}

main().catch(error => {
  console.error(`FATAL ${error instanceof Error ? error.stack : String(error)}`)
  try {
    fs.mkdirSync(EVIDENCE, { recursive: true })
    fs.writeFileSync(path.join(EVIDENCE, 'http-e2e-results.json'), JSON.stringify({
      base: BASE, results, failures: failures + 1, fatal: error instanceof Error ? error.message : String(error),
    }, null, 2))
  } catch { /* best effort */ }
  process.exit(1)
})