Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: refuse launch-video delivery before capture, not after (#9033)

[._.]/ Adam Eivy5cdb4f51a84e
diff --git a/server/services/htmlComposition/index.js b/server/services/htmlComposition/index.js
index 10f22c96da..e14dad8998 100644
--- a/server/services/htmlComposition/index.js
+++ b/server/services/htmlComposition/index.js
@@ -1,5 +1,5 @@
import { dirname, join, resolve } from 'node:path';
-import { mkdtemp, open, realpath, rm, writeFile } from 'node:fs/promises';
+import { lstat, mkdtemp, open, realpath, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { createReadStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
@@ -45,6 +45,17 @@ function renderTargets(contract, jobId, formats) {
});
}
+// Every name deliver() will exclusively create for this run, single- and
+// multi-format alike, in the same write order as the delivery loop below.
+function deliveryNames(targets) {
+ const names = ['plan.md', 'storyboard.json', 'caption.txt'];
+ for (const target of targets) {
+ const suffix = target.format ? `-${target.format}` : '';
+ names.push(`video${suffix}.mp4`, `poster${suffix}.jpg`);
+ }
+ return names;
+}
+
export async function renderComposition({ jobId, ...input }) {
const job = { controller: new AbortController(), committing: false };
active.set(jobId, job);
@@ -118,6 +129,22 @@ export async function renderComposition({ jobId, ...input }) {
...(target.format ? { format: target.format } : {}), ...frameOf(target.contract) } };
} else {
const targets = renderTargets(contract, jobId, formats);
+ if (deliveryRoot) {
+ // Refuse before spending a capture. deliver()'s exclusive open('wx')
+ // below is still the race-safe authority; this only saves the retry
+ // a full render before it learns what that open would have told it.
+ // lstat also refuses a symlinked delivery name.
+ for (const name of deliveryNames(targets)) {
+ const target = join(deliveryRoot, name);
+ let exists = true;
+ try { await lstat(target); } catch (error) { if (error.code !== 'ENOENT') throw error; exists = false; }
+ if (exists) {
+ const error = new Error(`EEXIST: file already exists, open '${target}'`);
+ error.code = 'EEXIST';
+ throw error;
+ }
+ }
+ }
if (synthesizeMusic) {
// One score for every format: they share the timeline and duration.
const wav = await synthesizeCompositionMusic(page, contract.durationSec);
diff --git a/server/services/htmlComposition/index.test.js b/server/services/htmlComposition/index.test.js
index 6f93278ad9..8a9dad898d 100644
--- a/server/services/htmlComposition/index.test.js
+++ b/server/services/htmlComposition/index.test.js
@@ -144,7 +144,10 @@ describe.skipIf(!chrome || !ffmpeg)('HTML composition with real Chrome and ffmpe
expect(await readFile(join(runRoot, 'video.mp4'))).toEqual(await readFile(join(PATHS.videos, result.filename)));
expect((await readdir(PATHS.videos)).some(name => name.includes(retryId))).toBe(false);
expect(await loadHistory()).not.toContainEqual(expect.objectContaining({ id: retryId }));
- }, 60000);
+ // One capture (180 frames) plus a refused, capture-free retry: about a
+ // third of the three-format test's budget below, which renders three
+ // 180-frame targets from one timeline.
+ }, 40000);
it('renders one launch timeline in three declared formats, each reframed by layout, as one take', async () => {
// layout() recolors the frame per orientation, so each poster proves the
diff --git a/server/services/htmlComposition/renderComposition.preflight.test.js b/server/services/htmlComposition/renderComposition.preflight.test.js
new file mode 100644
index 0000000000..24c724ef83
--- /dev/null
+++ b/server/services/htmlComposition/renderComposition.preflight.test.js
@@ -0,0 +1,58 @@
+// A retry into an already-delivered launch run must refuse before it spends
+// a capture (#9033). This is the fast unit-level counterpart to the real
+// Chrome/ffmpeg retry assertion in index.test.js: it mocks the browser and
+// encoder so it can assert the *reason* the retry stays cheap — encoding is
+// never reached — without paying for a real render.
+import { describe, expect, it, vi } from 'vitest';
+import { mkdir, writeFile } from 'node:fs/promises';
+import { join } from 'node:path';
+import { randomUUID } from 'node:crypto';
+import { lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';
+
+vi.mock('../../lib/fileUtils.js', async importOriginal => makePathsProxy(await importOriginal(), {
+ dataRoot: () => lazyTempDataRoot('portos-html-composition-preflight-'),
+}));
+
+const CONTRACT = { durationSec: 15, fps: 12, width: 1280, height: 720, motionBlur: 1, layout: false };
+
+vi.mock('./browser.js', () => ({
+ openComposition: async (directory, { validateAssets } = {}) => {
+ validateAssets?.(new Map([
+ ['/index.html', Buffer.from('<html></html>')],
+ ['/plan.md', Buffer.from('A fictional product demonstration.')],
+ ['/caption.txt', Buffer.from('Make a clear plan.')],
+ ['/storyboard.json', Buffer.from(JSON.stringify({ posterSec: 11, scenes: [{ durationSec: 15, lines: [] }] }))],
+ ]));
+ return { evaluate: async () => CONTRACT, check: () => {}, close: async () => {} };
+ },
+}));
+
+const encodeComposition = vi.fn(async () => { throw new Error('encodeComposition must not run on a refused delivery'); });
+vi.mock('./encode.js', () => ({
+ encodeComposition, encodeContactSheet: vi.fn(), proofTimes: vi.fn(), synthesizeCompositionMusic: vi.fn(),
+}));
+
+const { PATHS } = await import('../../lib/fileUtils.js');
+const { renderComposition } = await import('./index.js');
+
+describe('renderComposition delivery preflight', () => {
+ it('refuses with EEXIST before encoding when a delivery file already exists', async () => {
+ const runId = randomUUID();
+ const directory = `launch-videos/example/${runId}/composition`;
+ await mkdir(join(PATHS.data, directory), { recursive: true });
+ await writeFile(join(PATHS.data, directory, 'index.html'), '<html></html>');
+
+ // Simulate an already-delivered run: only `video.mp4` need exist for the
+ // preflight to refuse, since it checks every name deliver() would write.
+ const runRoot = join(PATHS.data, 'launch-videos', 'example', runId);
+ await mkdir(runRoot, { recursive: true });
+ await writeFile(join(runRoot, 'video.mp4'), 'already delivered');
+
+ const jobId = randomUUID();
+ await expect(renderComposition({
+ directory, jobId, launchVideo: { targetDurationSec: 15, appId: 'example', runId },
+ })).rejects.toThrow(/EEXIST/);
+
+ expect(encodeComposition).not.toHaveBeenCalled();
+ });
+});
diff --git a/server/vitest.config.js b/server/vitest.config.js
index 070f3c544e..47ab4a651a 100644
--- a/server/vitest.config.js
+++ b/server/vitest.config.js
@@ -114,6 +114,11 @@ export default defineConfig({
'services/sprites/atlas.test.js',
'services/sprites/walk.test.js',
'lib/gitTestRepo.test.js',
+ // Real-Chrome-and-ffmpeg gated renders (#9033): each spends minutes on
+ // headless capture/encode even when only their pure-logic cases would
+ // otherwise run under --fast.
+ 'services/htmlComposition/index.test.js',
+ 'services/musicVideo/compositionRender.test.js',
] : []),
],
coverage: {