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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';
vi.mock('../../lib/paths.js', async (load) => makePathsProxy(await load(), { dataRoot: () => lazyTempDataRoot('portos-production-authoring-') }));
const localAuthor = vi.hoisted(() => ({ submit: null }));
vi.mock('../promptRunner.js', () => ({
assertProvider: () => {},
resolveProviderAndModel: async () => ({ provider: { id: 'local-fixture', type: 'api', endpoint: 'http://localhost:11434' }, selectedModel: 'fixture-model' }),
runPromptThroughProvider: async ({ prompt, beforeExecute }) => {
await beforeExecute?.({ provider: { id: 'local-fixture', type: 'api', endpoint: 'http://localhost:11434' }, model: 'fixture-model' });
return localAuthor.submit(prompt);
},
}));
// Existing engine cases isolate the creative review boundary; explicit approval-flow
// cases below switch to the real model and verify pause/resume without providers.
const creativeReview = vi.hoisted(() => ({ real: false }));
vi.mock('./productionReview.js', async (load) => {
const actual = await load();
return { ...actual,
assertProductionApproval: (...args) => creativeReview.real ? actual.assertProductionApproval(...args) : undefined,
productionReadiness: (...args) => creativeReview.real ? actual.productionReadiness(...args) : {
art: { approved: true }, storyboard: { approved: true }, proof: { approved: true }, basis: { proof: 'test-proof' }, readyForProduction: true,
},
};
});
vi.mock('./productionReviewService.js', () => ({
prepareProductionReview: vi.fn(async () => {}),
renderProductionProof: vi.fn(async () => ({ jobId: 'proof-example' })),
attachProductionPilotProof: vi.fn(async () => {}),
}));
import { startProductionOnProject, markProductionPlanned, reserveProductionStep } from './production.js';
import { plateReviewEvidence } from '../../lib/musicVideoPlateEvidence.js';
import { plateRequirements } from '../../lib/musicVideoPlateEvidence.js';
import { startAutoReviewOnProject, nextAutoReviewStep, beginAttemptReview, deferAttemptReview } from './autoReview.js';
import { pilotDependencies } from './productionPilot.js';
import { captureMusicVideoEvidence } from '../../lib/musicVideoDependencies.js';
/**
* Music Video production run (#9066) — orchestration contract with injected
* queue events. The project store is an in-memory double with the real
* store's serialized read-modify-write; the queue, generation lanes and
* review service are doubles; the checkpoint (production.js) and the pool
* rules (productionPool.js) are real.
*/
import { describe, it, expect, vi, beforeEach, afterEach, afterAll } from 'vitest';
import { EventEmitter } from 'events';
import { canonicalSnapshotChecksum } from '../../lib/snapshotChecksum.js';
// ---- in-memory project store with a single write tail ----------------------
const store = new Map();
// Fault switches for the halt-failure path: `failOnList` makes the queue unreadable, which also arms `failWrites` so the halt write fails.
const storeFault = { failOnList: false, failWrites: false };
let writeTail = Promise.resolve();
const clone = (v) => JSON.parse(JSON.stringify(v));
const mutateMock = (id, transform, options = {}) => {
const run = writeTail.then(async () => {
if (!store.has(id)) throw Object.assign(new Error('Project not found'), { status: 404 });
if (storeFault.failWrites) throw new Error('store write failed');
const current = clone(store.get(id));
const out = transform(current);
if (options?.source) {
const { preserveAutomatedShotLocks } = await import('./productionReview.js');
const guarded = preserveAutomatedShotLocks(current, out.project, options.source);
out.project = guarded.project;
if (guarded.rejected) {
out.rejected = true;
if (out.scenes) out.scenes = guarded.project.scenes;
if (out.scene) out.scene = guarded.project.scenes.find(scene => scene.sceneId === out.scene?.sceneId) || null;
}
}
store.set(id, clone(out.project));
return out;
});
writeTail = run.catch(() => {});
return run;
};
vi.mock('./projects.js', () => ({
getProject: async (id) => (store.has(id) ? clone(store.get(id)) : null),
listProjects: async () => [...store.values()].map(clone),
mutateProjectRecord: (...args) => mutateMock(...args),
appendSceneTakes: async (id, sceneId, inputs) => {
const { appendSceneTakes: append } = await import('./takes.js');
const outcome = await mutateMock(id, current => append(current, sceneId, inputs), {
source: inputs.some(input => input.source === 'generated') ? 'generation-completion' : null,
});
return { scene: outcome.project.scenes.find(scene => scene.sceneId === sceneId), appended: outcome.appended };
},
}));
const { musicVideoEvents } = await import('./events.js');
const service = await import('./productionService.js');
// ---- queue, lanes and review doubles ------------------------------------------
let jobs;
let nextJobId;
const mediaJobEvents = new EventEmitter();
const queue = {
listJobs: ({ kind } = {}) => {
if (storeFault.failOnList) { storeFault.failWrites = true; throw new Error('queue unreadable'); }
return jobs.filter((j) => !kind || j.kind === kind);
},
cancelJob: vi.fn(async (id) => {
const job = jobs.find((j) => j.id === id);
if (job) job.status = 'canceled';
}),
mediaJobEvents,
};
const dispatch = vi.fn();
const reviewPlate = vi.fn();
const resolvePlateReviewer = vi.fn();
const planProject = vi.fn();
const startCastAndSets = vi.fn();
const startAutoReview = vi.fn();
const autoReview = { startAutoReview, resumeAutoReview: vi.fn(), stopAutoReview: vi.fn(async () => {}), cancelAutoReview: vi.fn(async () => {}) };
const env = {
settings: { imageGen: { local: { pythonPath: '/opt/example/python' }, codex: { enabled: true, model: 'codex-image' }, grok: { enabled: false } } },
imageModels: [{ id: 'flux2-dev', runner: 'flux2' }, { id: 'sd-basic', runner: 'mflux' }],
resolveVideoModel: async (id) => ({ model: {
'ltx-example': { id }, 'ltx-alt': { id }, 'example-text': { id, name: 'Example Text Model', supportedModes: ['text'] },
}[id] || null }),
isVideoModeUsable: (_settings, mode) => mode === 'local',
};
const POOL = [
{ kind: 'image', mode: 'local', model: 'flux2-dev' },
{ kind: 'image', mode: 'codex', model: null },
{ kind: 'video', mode: 'local', model: 'ltx-example' },
];
const LIMITS = { maxGenerations: 10, maxReviewAttempts: 4 };
function seedProject(overrides = {}) {
const project = {
id: 'mv-example',
name: 'Example Video',
concept: { style: 'noir' },
audioAnalysis: { sections: [{ startSec: 0, endSec: 8, label: 'Verse' }], durationSec: 8 },
scenes: [
{ sceneId: 'mvs-a', label: 'A', framePrompt: 'a lighthouse', prompt: 'waves roll', startSec: 0, endSec: 4, takes: [] },
{ sceneId: 'mvs-b', label: 'B', framePrompt: 'a harbor', prompt: 'boats sway', direction: { camera: 'orbit' }, startSec: 4, endSec: 8, takes: [] },
],
...overrides,
};
store.set(project.id, project);
return project;
}
const current = () => clone(store.get('mv-example'));
const theRun = () => current().productionRuns[0];
const _liveSteps = () => theRun().steps.filter((s) => s.status === 'reserved' || s.status === 'queued');
// The queue double: a dispatch lands one queued job tagged with the step.
const enqueueing = ({ stepKind, tag }) => {
const id = `job-${nextJobId += 1}`;
jobs.push({ id, kind: stepKind === 'frame' ? 'image' : 'video', status: 'queued', queuedAt: new Date().toISOString(), params: { musicVideo: tag } });
return Promise.resolve({ jobId: id });
};
/** Complete a job and file its take the way the scene hooks do, then emit the hook event. */
function completeJob(jobId) {
const job = jobs.find((j) => j.id === jobId);
job.status = 'completed';
const project = store.get('mv-example');
const scene = project.scenes.find((s) => s.sceneId === job.params.musicVideo.sceneId);
if (job.kind === 'image') scene.referenceImageId = `${jobId}.png`;
else scene.videoHistoryId = jobId;
mediaJobEvents.emit('completed', job);
musicVideoEvents.emit(job.kind === 'image' ? 'scene-image' : 'scene-video', { projectId: 'mv-example', sceneId: scene.sceneId });
}
/** Complete a job through appendSceneTakes (with source: 'generation-completion'), as the real hook does. */
async function completeJobThroughHook(jobId) {
const job = jobs.find((j) => j.id === jobId);
job.status = 'completed';
const sceneId = job.params.musicVideo.sceneId;
const { appendSceneTakes } = await import('./projects.js');
await appendSceneTakes('mv-example', sceneId, [{
kind: job.kind === 'image' ? (job.params?.musicVideo?.slot === 'end' ? 'endImage' : 'image') : 'video',
assetId: job.kind === 'image' ? `${jobId}.png` : jobId,
source: 'generated',
}]);
mediaJobEvents.emit('completed', job);
musicVideoEvents.emit(job.kind === 'image' ? 'scene-image' : 'scene-video', { projectId: 'mv-example', sceneId });
}
const settle = async () => {
// Wait for every fire-and-forget continuation (not a guessed number of timer
// ticks, which a contended worker can outrun, #10629), take one more pass,
// and wait for whatever that pass set off.
await service.__settleProductionForTests();
const run = store.get('mv-example')?.productionRuns?.[0];
if (run) await service.__advanceProductionForTests('mv-example', run.id);
await service.__settleProductionForTests();
};
/** A gate a double waits at: `reached` resolves once the work under test is parked there. */
function holdAt() {
let arrive; let release;
const reached = new Promise((resolve) => { arrive = resolve; });
const opened = new Promise((resolve) => { release = resolve; });
return { reached, release, held: () => { arrive(); return opened; } };
}
async function start(input = {}) {
const out = await service.startProduction('mv-example', { directive: 'moody, slow', pool: POOL, limits: LIMITS, reviewer: {}, ...input });
await settle();
return out;
}
beforeEach(() => {
creativeReview.real = false;
store.clear();
jobs = [];
nextJobId = 0;
vi.clearAllMocks();
dispatch.mockImplementation(enqueueing);
resolvePlateReviewer.mockResolvedValue({ provider: { id: 'reviewer-example', type: 'api' }, model: 'vision-example', costUsd: 0 });
reviewPlate.mockImplementation(async ({ scene, assetId, runId, beforeExecute }) => {
await beforeExecute();
return plateReviewEvidence(scene, assetId, runId, JSON.stringify({ checks: plateRequirements(scene).map(({ id }) => ({ id, status: 'pass', note: 'Visible starting state' })) }));
});
planProject.mockResolvedValue({ scenesAdded: 0 });
startAutoReview.mockImplementation(async (projectId, input) => {
const pilot = input.productionPilotSceneId;
const reviewRun = { id: pilot ? 'mvar-pilot-' + pilot : 'mvar-example', status: pilot ? 'passed' : 'running', productionRunId: input.productionRunId, limits: input.limits, usage: { reviews: pilot ? 1 : 0, generations: 0 },
...(pilot ? { attempts: [{ n: 1, review: { verdict: 'pass', dependencies: pilotDependencies(store.get(projectId), pilot) } }] } : {}) };
if (pilot) await service.chargeProductionReview(projectId, input.productionRunId, { reviewRunId: reviewRun.id, attemptN: 1, costUsd: 0 });
const project = store.get(projectId);
project.autoReviews = [...(project.autoReviews || []), reviewRun];
return { run: reviewRun };
});
service.__setProductionDepsForTests({
loadEnv: async () => env,
reviewPlate, resolvePlateReviewer,
dispatch,
queue: async () => queue,
planProject,
startCastAndSets,
autoReview: async () => autoReview,
resolveAuthoring: async (input) => {
if (!input?.providerId || !input?.model) throw Object.assign(new Error('Select an authoring model'), { code: 'PRODUCTION_AUTHORING_REQUIRED' });
return { ...input, costUsd: 0 };
},
releaseRevisionSection: vi.fn(async () => {}),
});
});
// A test's background advances finish before the next test resets the doubles.
afterEach(settle);
afterAll(cleanupTempDataRoots);
describe('music video production run (#9066)', () => {
it('refuses mutable peer placement before starting an immutable production budget', async () => {
seedProject({ videoSettings: { renderPool: { mode: 'both', peers: [{ peerId: 'example-peer', modelId: 'example-model' }] } } });
await expect(start()).rejects.toMatchObject({ code: 'MUSIC_VIDEO_RENDER_POOL_UNAVAILABLE' });
expect(dispatch).not.toHaveBeenCalled();
expect(current().productionRuns).toBeUndefined();
});
// Uniquely catches a Supplied song audio project refused by production, or its clips losing the song window.
it('binds supplied song audio into the run\'s local video routes, and refuses it with a remote video route', async () => {
seedProject({ videoSettings: { generationMode: 'suppliedAudio', renderPool: { mode: 'local', peers: [] } } });
await expect(start({ pool: [...POOL, { kind: 'video', mode: 'fal' }] })).rejects.toMatchObject({ code: 'MUSIC_VIDEO_RENDER_POOL_UNAVAILABLE' });
expect(current().productionRuns).toBeUndefined();
await start();
expect(theRun().pool.filter((route) => route.kind === 'video').every((route) => route.audio === 'supplied')).toBe(true);
expect(theRun().pool.filter((route) => route.kind === 'image').some((route) => 'audio' in route)).toBe(false);
});
// Uniquely catches a failed halt being swallowed: the step failure and the halt failure both reach console.error with the project.
it('logs the step failure and the failed halt with the project, instead of swallowing the halt error', async () => {
seedProject();
const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
try {
storeFault.failOnList = true;
await service.startProduction('mv-example', { directive: 'moody, slow', pool: POOL, limits: LIMITS, reviewer: {} });
await service.__settleProductionForTests();
const lines = errors.mock.calls.map(([line]) => String(line));
expect(lines.some((line) => line.startsWith('❌') && line.includes('step failed: queue unreadable'))).toBe(true);
expect(lines.some((line) => line.startsWith('❌') && line.includes('could not halt project mv-example after the step failure: store write failed'))).toBe(true);
} finally {
storeFault.failOnList = false;
storeFault.failWrites = false;
errors.mockRestore();
}
});
it('produces a reviewed draft from one Start: frames, then clips, then the continuous-excerpt review', async () => {
seedProject();
await start();
// Both frames go out on the first eligible pool route; clips wait for their frame.
expect(dispatch.mock.calls.map(([a]) => [a.stepKind, a.scene.sceneId, a.route.mode])).toEqual([
['frame', 'mvs-a', 'local'], ['frame', 'mvs-b', 'local'],
]);
expect(dispatch.mock.calls[0][0].tag).toMatchObject({ projectId: 'mv-example', sceneId: 'mvs-a', productionRunId: theRun().id });
completeJob('job-1');
completeJob('job-2');
await settle();
expect(dispatch.mock.calls.slice(2).map(([a]) => [a.stepKind, a.scene.sceneId, a.route.mode])).toEqual([
['clip', 'mvs-a', 'local'], ['clip', 'mvs-b', 'local'],
]);
completeJob('job-3');
completeJob('job-4');
await settle();
expect(startAutoReview).toHaveBeenCalledTimes(3);
expect(startAutoReview.mock.calls.at(-1)[1]).toMatchObject({ startSec: 0, endSec: 8, productionRunId: theRun().id, limits: { maxAttempts: 2, maxGenerations: 6 } });
expect(theRun()).toMatchObject({ status: 'running', reviewRunId: 'mvar-example', usage: { generations: 4 } });
expect(theRun().steps.every((s) => s.status === 'completed' && s.rationale)).toBe(true);
// Only a passed continuous review completes the run — never the frames alone.
passOwnedReview();
musicVideoEvents.emit('auto-review', { projectId: 'mv-example', run: { ...store.get('mv-example').autoReviews[0] }, action: { type: 'idle' } });
await settle();
expect(theRun().status).toBe('completed');
});
// Uniquely catches a stills-only run spending on a clip (pilot or bulk) or stopping before every still exists.
it('makes every still and stops before any clip when told to, then goes on to the clips on Resume', async () => {
seedProject();
await start({ pool: POOL.filter((route) => route.kind === 'image'), stopBefore: 'clips' });
expect(dispatch.mock.calls.map(([a]) => [a.stepKind, a.scene.sceneId])).toEqual([['frame', 'mvs-a'], ['frame', 'mvs-b']]);
completeJob('job-1');
completeJob('job-2');
await settle();
expect(theRun()).toMatchObject({ status: 'awaiting-video', stopBefore: 'clips', pilot: null, stopReason: expect.stringContaining('still') });
expect(dispatch).toHaveBeenCalledTimes(2);
expect(startAutoReview).not.toHaveBeenCalled();
// A resume that keeps the stop makes only missing stills.
await service.resumeProduction('mv-example', theRun().id, { stopBefore: 'clips' });
await settle();
expect(theRun().status).toBe('awaiting-video');
expect(dispatch).toHaveBeenCalledTimes(2);
await service.resumeProduction('mv-example', theRun().id, { pool: POOL });
await settle();
expect(theRun()).toMatchObject({ status: 'running', stopBefore: null });
expect(dispatch.mock.calls.slice(2).every(([a]) => a.stepKind === 'clip')).toBe(true);
expect(dispatch.mock.calls.length).toBeGreaterThan(2);
});
// Uniquely catches a resumed run keeping its pending clip pilot, which would filter stills to the pilot scenes and park early.
it('drops a pending clip pilot when a normal run is resumed as stills-only', async () => {
seedProject();
await start();
await service.stopProduction('mv-example', theRun().id);
expect(theRun().pilot).not.toBeNull();
await service.resumeProduction('mv-example', theRun().id, { stopBefore: 'clips' });
await settle();
expect(theRun()).toMatchObject({ stopBefore: 'clips', pilot: null });
expect(dispatch.mock.calls.every(([a]) => a.stepKind === 'frame')).toBe(true);
expect(new Set(dispatch.mock.calls.map(([a]) => a.scene.sceneId))).toEqual(new Set(['mvs-a', 'mvs-b']));
});
it('in a composition document, skips frames for card scenes and clips for still/card scenes', async () => {
seedProject({
composition: { mode: 'document' },
scenes: [
{ sceneId: 'mvs-a', label: 'A', framePrompt: 'a lighthouse', prompt: 'waves roll', startSec: 0, endSec: 4, takes: [] },
{ sceneId: 'mvs-card', label: 'Card', visualLayer: 'card', startSec: 4, endSec: 6, takes: [] },
{ sceneId: 'mvs-still', label: 'Still', visualLayer: 'still', framePrompt: 'a harbor', referenceImageId: 'harbor.png', startSec: 6, endSec: 8, takes: [] },
],
});
await start();
// Only the footage scene needs a frame; the card needs none and the still already has one.
expect(dispatch.mock.calls.map(([a]) => [a.stepKind, a.scene.sceneId])).toEqual([['frame', 'mvs-a']]);
completeJob('job-1');
await settle();
// Only the footage scene needs a clip — the document draws the still and the card itself.
expect(dispatch.mock.calls.slice(1).map(([a]) => [a.stepKind, a.scene.sceneId])).toEqual([['clip', 'mvs-a']]);
});
it('plans an empty board once, with the directive, before generating', async () => {
seedProject({ scenes: [], castAndSets: { status: 'skipped' } });
planProject.mockImplementation(async () => {
store.get('mv-example').scenes = [{ sceneId: 'mvs-p', framePrompt: 'x', prompt: 'y', startSec: 0, endSec: 4, takes: [] }];
return { scenesAdded: 1 };
});
await start();
expect(planProject).toHaveBeenCalledWith('mv-example', { seedPrompts: true, directive: 'moody, slow' });
expect(dispatch).toHaveBeenCalledTimes(1);
});
it('has the autopilot cut on the song unless the director chose how it cuts (#9290)', async () => {
seedProject({ scenes: [], castAndSets: { status: 'skipped' } });
planProject.mockResolvedValue({ scenesAdded: 0 });
await start();
expect(store.get('mv-example').composition.cutting).toBe('intercut');
seedProject({ scenes: [], castAndSets: { status: 'skipped' }, composition: { mode: 'composed', cutting: 'scene', textCues: [] } });
await start();
expect(store.get('mv-example').composition.cutting).toBe('scene');
});
it('runs the Cast & Sets check-in before the plan and waits for the director in review mode', async () => {
seedProject({ scenes: [], visualSpec: { references: [] } });
startCastAndSets.mockImplementation(async (projectId, { productionRunId }) => {
store.get(projectId).castAndSets = { status: 'directing', productionRunId };
return {};
});
planProject.mockImplementation(async () => {
store.get('mv-example').scenes = [{ sceneId: 'mvs-p', framePrompt: 'x', prompt: 'y', startSec: 0, endSec: 4, takes: [] }];
return { scenesAdded: 1 };
});
await start();
expect(startCastAndSets).toHaveBeenCalledTimes(1);
expect(startCastAndSets.mock.calls[0][1]).toMatchObject({ productionRunId: theRun().id });
expect(planProject).not.toHaveBeenCalled();
// The sheet is waiting for its check-in: the run waits, it neither plans nor halts.
store.get('mv-example').castAndSets.status = 'review';
musicVideoEvents.emit('cast-and-sets', { projectId: 'mv-example', stage: { status: 'review' } });
await settle();
expect(planProject).not.toHaveBeenCalled();
expect(theRun()).toMatchObject({ status: 'running', planned: false, castAndSetsStarted: true });
// Approval writes new references (a creative-setup change) and re-bases the run in the same write.
const { rebaseProductionAfterCheckin } = await import('./production.js');
const project = store.get('mv-example');
project.castAndSets.status = 'approved';
project.visualSpec = { references: [{ id: 'mvr-cs-character', imageId: 'sheet.png', role: 'character', condition: true }] };
store.set('mv-example', rebaseProductionAfterCheckin(project).project);
musicVideoEvents.emit('cast-and-sets', { projectId: 'mv-example', stage: { status: 'approved' } });
await settle();
expect(planProject).toHaveBeenCalledTimes(1);
expect(theRun().status).toBe('running');
expect(dispatch).toHaveBeenCalledTimes(1);
});
it('halts blocked when the Cast & Sets check-in fails, and never plans past it', async () => {
seedProject({ scenes: [], castAndSets: { status: 'failed', stopReason: 'no image backend' } });
await start();
expect(startCastAndSets).not.toHaveBeenCalled();
expect(planProject).not.toHaveBeenCalled();
expect(theRun()).toMatchObject({ status: 'blocked' });
expect(theRun().stopReason).toMatch(/no image backend/);
});
it('refuses a route outside the allowed pool before anything is enqueued', async () => {
seedProject();
const chooseRoute = vi.fn(async () => ({ route: { kind: 'image', mode: 'grok', model: null }, rationale: 'model pick' }));
service.__setProductionDepsForTests({ loadEnv: async () => env, dispatch, queue: async () => queue, planProject, autoReview: async () => autoReview, chooseRoute });
await start();
expect(dispatch).not.toHaveBeenCalled();
expect(theRun()).toMatchObject({ status: 'blocked', usage: { generations: 0 } });
expect(theRun().stopReason).toMatch(/not in this run's allowed pool/);
});
it('lets the chooser pick any eligible member of the pool', async () => {
seedProject();
const chooseRoute = vi.fn(async () => ({ route: { kind: 'image', mode: 'codex', model: null }, rationale: 'model pick' }));
service.__setProductionDepsForTests({ loadEnv: async () => env, dispatch, queue: async () => queue, planProject, autoReview: async () => autoReview, chooseRoute });
await start();
expect(dispatch.mock.calls.map(([a]) => a.route.mode)).toEqual(['codex', 'codex']);
expect(theRun().steps[0].rationale).toBe('model pick');
});
it('refuses Start when a pool route lacks entitlement (disabled cloud backend, missing local model)', async () => {
seedProject();
await expect(service.startProduction('mv-example', {
pool: [{ kind: 'image', mode: 'grok' }, { kind: 'video', mode: 'local', model: 'not-installed' }], limits: LIMITS,
})).rejects.toMatchObject({ code: 'PRODUCTION_ROUTE_INELIGIBLE', message: expect.stringMatching(/grok.*not enabled.*not-installed.*not installed/s) });
expect(current().productionRuns).toBeUndefined();
});
it('skips a route that cannot condition on the visual-spec references', async () => {
seedProject({ visualSpec: { references: [{ imageId: 'ref.png', condition: true }] } });
await start({ pool: [{ kind: 'image', mode: 'local', model: 'sd-basic' }, { kind: 'image', mode: 'codex' }, POOL[2]] });
expect(dispatch.mock.calls.map(([a]) => a.route.mode)).toEqual(['codex', 'codex']);
expect(theRun().steps[0].rationale).toMatch(/skipped 1 earlier route.*cannot use reference images/);
});
it('halts with the capability reason when no pool route can condition on the references', async () => {
seedProject({ visualSpec: { references: [{ imageId: 'ref.png', condition: true }] } });
await start({ pool: [{ kind: 'image', mode: 'local', model: 'sd-basic' }, POOL[2]] });
expect(dispatch).not.toHaveBeenCalled();
expect(theRun()).toMatchObject({ status: 'blocked', stopReason: expect.stringMatching(/cannot use reference images/) });
});
it('refuses a performance shot on a pool with no lip-sync video route, naming why', async () => {
seedProject();
store.get('mv-example').scenes[0].shotMode = 'performance';
await start();
completeJob('job-1');
completeJob('job-2');
await settle();
expect(theRun()).toMatchObject({ status: 'blocked', stopReason: expect.stringMatching(/cannot lip-sync/) });
expect(dispatch.mock.calls.some(([a]) => a.stepKind === 'clip' && a.scene.sceneId === 'mvs-a')).toBe(false);
});
it('settles a duplicated completion event once and never re-dispatches for it', async () => {
seedProject();
await start();
const job = jobs[0];
job.status = 'failed';
job.error = 'out of memory';
mediaJobEvents.emit('failed', job);
mediaJobEvents.emit('failed', job);
await settle();
const steps = theRun().steps.filter((s) => s.sceneId === 'mvs-a');
expect(steps.map((s) => s.status)).toEqual(['failed']);
// Duplicate failure events cannot authorize another paid operation.
expect(dispatch.mock.calls.filter(([a]) => a.scene.sceneId === 'mvs-a')).toHaveLength(1);
expect(theRun().status).toBe('blocked');
});
it('pauses after a paid failure and retries only on explicit Resume, including a second failure', async () => {
seedProject();
await start();
const fail = (id) => {
const job = jobs.find((j) => j.id === id);
job.status = 'failed';
mediaJobEvents.emit('failed', job);
};
fail('job-1');
await settle();
expect(theRun()).toMatchObject({ status: 'blocked', usage: { generations: 2 } });
expect(dispatch).toHaveBeenCalledTimes(2);
await service.resumeProduction('mv-example', theRun().id);
await settle();
expect(dispatch.mock.calls.slice(2).map(([a]) => a.scene.sceneId)).toEqual(['mvs-a']);
fail('job-3');
await settle();
expect(theRun()).toMatchObject({ status: 'blocked', usage: { generations: 3 } });
expect(dispatch).toHaveBeenCalledTimes(3);
});
it('persists terminal refusal across Resume and incidental edits, but admits repaired inputs', async () => {
seedProject();
dispatch.mockRejectedValueOnce(Object.assign(new Error('Unsupported request'), { status: 422, code: 'VIDEO_MODE_UNSUPPORTED' }));
await start();
const refused = theRun().steps[0];
expect(refused).toMatchObject({ status: 'refused', retryBlocked: true, errorCode: 'VIDEO_MODE_UNSUPPORTED' });
expect(theRun().usage.generations).toBe(0);
expect(() => reserveProductionStep(current(), theRun().id, {
kind: 'frame', sceneId: 'mvs-a', revisionId: 'mvr-new', route: refused.route, processId: theRun().processId,
})).toThrow();
store.get('mv-example').scenes[0].label = 'Renamed';
store.get('mv-example').scenes[0].takes.push({ id: 'take-example', status: 'rejected' });
for (let i = 0; i < 2; i += 1) {
await service.resumeProduction('mv-example', theRun().id);
await settle();
}
expect(dispatch).toHaveBeenCalledTimes(1);
expect(theRun()).toMatchObject({ status: 'blocked', usage: { generations: 0 } });
store.get('mv-example').scenes[0].framePrompt = 'a repaired frame';
await service.resumeProduction('mv-example', theRun().id);
await settle();
expect(dispatch).toHaveBeenCalledTimes(3);
expect(theRun().usage.generations).toBe(2);
});
it('continues a run blocked on its video model once Resume swaps that model, refusing a text-only one like Start', async () => {
seedProject();
await start();
completeJob('job-1');
completeJob('job-2');
dispatch.mockRejectedValueOnce(Object.assign(new Error('Model does not support image-to-video'), { status: 422, code: 'VIDEO_MODE_UNSUPPORTED' }));
await settle();
expect(theRun()).toMatchObject({ status: 'blocked', stopReason: expect.stringMatching(/refused/) });
const clips = () => dispatch.mock.calls.filter(([a]) => a.stepKind === 'clip').map(([a]) => a.route.model);
expect(clips()).toEqual(['ltx-example']);
const swapTo = (model) => POOL.map((route) => (route.kind === 'video' ? { ...route, model } : route));
await expect(service.resumeProduction('mv-example', theRun().id, { pool: swapTo('example-text') }))
.rejects.toMatchObject({ code: 'PRODUCTION_ROUTE_INELIGIBLE', message: expect.stringMatching(/Example Text Model.*image-to-video/) });
expect(theRun()).toMatchObject({ status: 'blocked', pool: POOL });
await service.resumeProduction('mv-example', theRun().id, { pool: swapTo('ltx-alt') });
await settle();
expect(theRun()).toMatchObject({ status: 'running', pool: swapTo('ltx-alt') });
expect(theRun().pricing).toHaveProperty(['video:local:ltx-alt']);
expect(clips()).toEqual(['ltx-example', 'ltx-alt', 'ltx-alt']);
});
it('allows explicit Resume after a transient submission failure without spending on the refusal', async () => {
seedProject();
dispatch.mockRejectedValueOnce(Object.assign(new Error('Provider temporarily unavailable'), { status: 503 }));
await start();
expect(theRun().steps[0]).not.toHaveProperty('retryBlocked');
expect(theRun().usage.generations).toBe(0);
await service.resumeProduction('mv-example', theRun().id);
await settle();
expect(dispatch).toHaveBeenCalledTimes(3);
expect(theRun().usage.generations).toBe(2);
});
it('coalesces concurrent advances so each slot is dispatched once', async () => {
seedProject();
let release;
const gate = new Promise((r) => { release = r; });
dispatch.mockImplementation(async (args) => { await gate; return enqueueing(args); });
const started = service.startProduction('mv-example', { pool: POOL, limits: LIMITS });
const { run } = await started;
const racing = Promise.all([1, 2, 3].map(() => service.__advanceProductionForTests('mv-example', run.id)));
release();
await racing;
await settle();
expect(dispatch.mock.calls.map(([a]) => a.scene.sceneId)).toEqual(['mvs-a', 'mvs-b']);
expect(jobs).toHaveLength(2);
});
it('never re-dispatches a slot the queue already has a live job for (lost link after enqueue)', async () => {
seedProject();
jobs.push({ id: 'job-orphan', kind: 'image', status: 'running', params: { musicVideo: { projectId: 'mv-example', sceneId: 'mvs-a' } } });
await start();
expect(dispatch.mock.calls.map(([a]) => a.scene.sceneId)).toEqual(['mvs-b']);
});
it('Stop during a dispatch cancels the job that raced it and dispatches nothing more', async () => {
seedProject();
const { held, reached, release } = holdAt();
dispatch.mockImplementationOnce(async (args) => { await held(); return enqueueing(args); });
const { run } = await service.startProduction('mv-example', { pool: POOL, limits: LIMITS });
await reached;
await service.stopProduction('mv-example', run.id);
release();
await settle();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(queue.cancelJob).toHaveBeenCalledWith('job-1');
expect(jobs[0].status).toBe('canceled');
expect(theRun()).toMatchObject({ status: 'stopped' });
// Evidence stays: the step and its charge are kept.
expect(theRun().steps).toHaveLength(1);
});
it('stops at the generation limit instead of paying for the next job', async () => {
seedProject();
await start({ limits: { maxGenerations: 1, maxReviewAttempts: 1 } });
expect(dispatch).toHaveBeenCalledTimes(1);
expect(theRun()).toMatchObject({ status: 'limit-reached', usage: { generations: 1 } });
});
it('refuses a dollar cap when a metered route has no known price', async () => {
seedProject();
await expect(service.startProduction('mv-example', { pool: POOL, limits: { ...LIMITS, spendCapUsd: 5 } }))
.rejects.toMatchObject({ code: 'PRODUCTION_COST_UNKNOWN' });
// A local-only pool prices every route at $0, so the cap is enforceable.
await service.startProduction('mv-example', { pool: [POOL[0], POOL[2]], limits: { ...LIMITS, spendCapUsd: 5 } });
await settle();
expect(theRun().limits.spendCapUsd).toBe(5);
});
it('prices fal.ai clips from the catalog so a dollar cap bounds them, charging each scene its own take', async () => {
// Scene A is a 4s performance (lip-sync window padded to 5.05s), B a 4s
// cutaway on H3 Max (covered by its 5s minimum), both at 1080P list rates.
seedProject({
videoSettings: { backend: 'fal', falModelId: 'minimax/h3-max/image-to-video', falResolution: '1080P' },
audioAnalysis: { sections: [{ startSec: 0, endSec: 8, label: 'Verse' }], durationSec: 30 },
});
store.get('mv-example').scenes[0].shotMode = 'performance';
const falPool = [POOL[0], { kind: 'video', mode: 'fal', model: null }];
const usable = env.isVideoModeUsable;
env.isVideoModeUsable = (_settings, mode) => mode === 'local' || mode === 'fal';
try {
await start({ pool: falPool, limits: { ...LIMITS, spendCapUsd: 1.5 } });
// The run's start-time price for the route: a default-length cutaway.
expect(theRun().pricing).toEqual({ 'image:local:flux2-dev': 0, 'video:fal:': 0.8 });
completeJob('job-1');
completeJob('job-2');
await settle();
} finally {
env.isVideoModeUsable = usable;
}
const clips = theRun().steps.filter((s) => s.kind === 'clip');
// A's lip-sync take is charged 5.05s × $0.16; B's $0.80 would pass the cap.
expect(clips.map((s) => [s.sceneId, s.costUsd])).toEqual([['mvs-a', 0.808]]);
expect(theRun()).toMatchObject({ status: 'limit-reached', usage: { spentUsd: 0.808 }, stopReason: expect.stringMatching(/\$1\.5 cap/) });
expect(dispatch.mock.calls.filter(([a]) => a.stepKind === 'clip').map(([a]) => a.scene.sceneId)).toEqual(['mvs-a']);
});
it('after a restart nothing dispatches until an explicit resume, which neither duplicates live jobs nor re-charges them', async () => {
seedProject();
await start();
expect(dispatch).toHaveBeenCalledTimes(2);
// Simulate a restart: the checkpoint names another process; a job finished meanwhile.
store.get('mv-example').productionRuns[0].processId = 'proc-previous';
completeJob('job-1');
await settle();
expect(dispatch).toHaveBeenCalledTimes(2);
expect(theRun().steps.find((s) => s.jobId === 'job-1').status).toBe('completed');
const resumed = await service.resumeProduction('mv-example', theRun().id);
expect(resumed.run.interrupted).toBe(false);
await settle();
// job-2 is still queued, so only scene A's clip goes out.
expect(dispatch.mock.calls.slice(2).map(([a]) => [a.stepKind, a.scene.sceneId])).toEqual([['clip', 'mvs-a']]);
expect(theRun().usage.generations).toBe(3);
});
it('halts when the creative setup changes, and continues only when resume accepts the new basis', async () => {
seedProject();
await start();
store.get('mv-example').concept = { style: 'pastel' };
completeJob('job-1');
await settle();
expect(theRun()).toMatchObject({ status: 'needs-replan' });
const before = dispatch.mock.calls.length;
await expect(service.resumeProduction('mv-example', theRun().id)).rejects.toMatchObject({ code: 'PRODUCTION_BASIS_CHANGED' });
await service.resumeProduction('mv-example', theRun().id, { acceptBasis: true });
await settle();
expect(theRun()).toMatchObject({ status: 'running', basis: { capturedAt: expect.any(String) } });
expect(dispatch.mock.calls.length).toBe(before + 1);
});
it('dispatches the review\'s revised sections server-side, tagged with its revision', async () => {
seedProject();
await start();
completeJob('job-1'); completeJob('job-2');
await settle();
completeJob('job-3'); completeJob('job-4');
await settle();
const reviewRun = store.get('mv-example').autoReviews.find((r) => r.id === theRun().reviewRunId);
store.get('mv-example').scenes[0].videoHistoryId = null;
musicVideoEvents.emit('auto-review', { projectId: 'mv-example', run: { ...reviewRun }, action: { type: 'generate', revisionId: 'mvrv-example', sections: [{ sceneId: 'mvs-a', kind: 'video' }] } });
await settle();
const last = dispatch.mock.calls.at(-1)[0];
expect(last).toMatchObject({ stepKind: 'clip', tag: { revisionId: 'mvrv-example', sceneId: 'mvs-a' } });
});
it('does not start a code-first document run through legacy frame and clip lanes', async () => {
seedProject({ composition: { mode: 'document' }, productionPolicy: { strategy: 'code-first', maxGeneratedVideoPercent: 0 } });
await expect(service.startProduction('mv-example', { pool: POOL, limits: LIMITS }))
.rejects.toMatchObject({ code: 'PRODUCTION_AUTHORING_REQUIRED' });
expect(dispatch).not.toHaveBeenCalled();
});
it('refuses a generation if the director changes to zero code-first allowance during provider preparation', async () => {
seedProject();
const { held, reached, release } = holdAt();
dispatch.mockImplementationOnce(async (args) => {
await held();
await service.assertProductionSubmission('mv-example', args.tag.productionRunId, args.tag.productionStepKey,
{ sceneId: args.tag.sceneId, kind: 'image' });
return enqueueing(args);
});
await service.startProduction('mv-example', { pool: POOL, limits: LIMITS });
await reached;
expect(theRun().steps[0].status).toBe('reserved');
store.get('mv-example').productionPolicy = { strategy: 'code-first', maxGeneratedVideoPercent: 0 };
release();
await settle();
expect(jobs).toHaveLength(0);
expect(theRun()).toMatchObject({ status: 'blocked', usage: { generations: 0 } });
expect(theRun().steps[0]).toMatchObject({ status: 'refused', error: expect.stringMatching(/policy|plan/i) });
await expect(service.resumeProduction('mv-example', theRun().id, { acceptBasis: true }))
.rejects.toMatchObject({ code: 'PRODUCTION_AUTHORING_REQUIRED' });
});
it('keeps pre-upgrade legacy run checksums valid after adding the policy-aware basis', async () => {
seedProject();
await start();
const stored = store.get('mv-example');
// Reconstruct the version-one persisted basis from its old field contract.
const pick = (value) => value ?? null;
const oldRevision = canonicalSnapshotChecksum({
concept: pick(stored.concept), visualSpec: pick(stored.visualSpec),
...(stored.styleReferences?.length ? { styleReferences: stored.styleReferences } : {}),
brief: pick(stored.treatment?.brief), automation: pick(stored.automation),
audio: { trackId: pick(stored.trackId), uploadedAudioFilename: pick(stored.uploadedAudioFilename),
sections: pick(stored.audioAnalysis?.sections), durationSec: pick(stored.audioAnalysis?.durationSec) },
pacing: pick(stored.pacing), composition: pick(stored.composition?.mode),
});
stored.productionRuns[0].basis = { revision: oldRevision, capturedAt: stored.productionRuns[0].createdAt };
completeJob('job-1');
await settle();
expect(theRun().status).toBe('running');
expect(theRun().basis).not.toHaveProperty('version');
});
});
// The real state machine/pool run against controlled authors and encoders;
// collaborators must honor the same publication/submission guards as providers.
const AUTHORING = { providerId: 'local-fixture', model: 'fixture-model' };
const authorProvider = { id: AUTHORING.providerId, type: 'api', endpoint: 'http://localhost:11434' };
const storeMutation = async (transform) => (await import('./projects.js')).mutateProjectRecord('mv-example', transform);
function seedCodeFirst({ scenes, directions, percent = 0, ...patch } = {}) {
return seedProject({ composition: { mode: 'document' },
productionPolicy: { strategy: 'code-first', maxGeneratedVideoPercent: percent },
scenes: scenes || [{ sceneId: 'mvs-code', label: 'Code', startSec: 0, endSec: 8 }],
treatment: { revision: 1, appliedRevision: 1, shotDirections: directions || [{ sceneId: 'mvs-code', medium: 'procedural', mediumRationale: 'Typography' }] }, ...patch,
});
}
function documentDoubles(realDocuments = null) {
let version = 0;
const author = vi.fn(async (_id, input) => {
await input.beforeSubmit({ provider: authorProvider, model: AUTHORING.model });
return storeMutation((project) => {
input.verifyCurrent(project);
const document = { directory: `music-video/mv-example/composition/doc-${version += 1}`, source: { kind: 'generated' } };
return { project: { ...project, composition: { ...project.composition, documentDraft: document } }, document };
});
});
const regenerate = vi.fn(async (id, _sectionId, input) => author(id, input));
const accept = vi.fn(async (_id, directory, options) => storeMutation((project) => {
options.verifyCurrent(project);
if (project.composition.documentDraft?.directory !== directory) throw new Error('stale candidate');
const { documentDraft, ...composition } = project.composition;
return { project: { ...project, composition: { ...composition, document: documentDraft } }, document: documentDraft };
}));
const documents = { generateMixedMediaDocument: author, regenerateMixedMediaSection: regenerate, acceptMixedMediaDocument: accept,
readMixedMediaCandidate: async () => ({ stale: false, source: current().composition.documentDraft || current().composition.document, sections: [
{ id: 'verse', startSec: 0, endSec: 4 }, { id: 'hook', startSec: 4, endSec: 8 },
] }),
};
const render = vi.fn(async (_id, options) => { options.verifyCurrent(current()); return { jobId: 'final-render' }; });
const cancelRender = vi.fn();
service.__setProductionDepsForTests({ loadEnv: async () => env, dispatch, queue: async () => queue,
autoReview: async () => autoReview, documents: async () => realDocuments || documents,
render: async () => ({ renderMusicVideo: render, cancelRender }),
resolveAuthoring: async (input) => {
if (!input?.providerId) throw Object.assign(new Error('Select an authoring model'), { code: 'PRODUCTION_AUTHORING_REQUIRED' });
return { ...input, costUsd: 0 };
},
});
return { author, regenerate, accept, render, cancelRender };
}
function passOwnedReview() {
const project = store.get('mv-example');
const review = project.autoReviews.find((r) => r.id === project.productionRuns[0].reviewRunId);
review.status = 'passed';
review.attempts = [{ n: 1, review: { verdict: 'pass', dependencies: captureMusicVideoEvidence(project) } }];
musicVideoEvents.emit('auto-review', { projectId: project.id, run: clone(review), action: { type: 'idle' } });
}
function failOwnedReview(atSec = 6) {
const review = store.get('mv-example').autoReviews.find((r) => r.id === theRun().reviewRunId);
Object.assign(review, { startSec: 0, attempts: [{ n: 1, review: { verdict: 'revise', findings: [{ severity: 'blocking', atSec, note: 'Type transition jumps' }] } }] });
musicVideoEvents.emit('auto-review', { projectId: 'mv-example', run: clone(review), action: { type: 'revise-document' } });
}
describe('code-first production execution (#9301)', () => {
it('resumes production after a later local author batch fails without repeating completed sections (#10528)', async () => {
const ids = ['intro', 'verse', 'hook'];
const scenes = ids.map((id, i) => ({ sceneId: `mvs-${id}`, label: id, startSec: i * 10, endSec: (i + 1) * 10 }));
seedCodeFirst({ scenes, directions: scenes.map((scene) => ({ sceneId: scene.sceneId, medium: 'procedural', mediumRationale: 'Draw the approved geometry' })),
audioAnalysis: { durationSec: 30, sections: ids.map((id, i) => ({ id, label: id, startSec: i * 10, endSec: (i + 1) * 10 })) },
productionReview: { draft: { storyboard: scenes.map((scene) => ({ sceneId: scene.sceneId, action: 'Planned choreography. '.repeat(2000) })) } },
});
const documents = await import('./documentGeneration.js');
documentDoubles(documents);
const asked = [];
localAuthor.submit = async (prompt) => {
const sections = ids.filter((id) => prompt.includes(`"id":"${id}"`));
asked.push(sections);
if (asked.length === 2) throw new Error('Later local batch timed out');
return { text: JSON.stringify({ sections: sections.map((id) => ({ id, source: 'function render(ctx, env) { ctx.fillStyle = "#112233"; }' })) }) };
};
await start({ pool: [], authoring: AUTHORING });
expect(theRun().steps.filter((step) => step.kind === 'author')).toEqual([expect.objectContaining({ status: 'failed' })]);
const finished = asked[0];
expect(finished.length).toBeGreaterThan(0);
expect(finished.length).toBeLessThan(ids.length);
asked.length = 0;
localAuthor.submit = async (prompt) => {
const sections = ids.filter((id) => prompt.includes(`"id":"${id}"`));
asked.push(sections);
return { text: JSON.stringify({ sections: sections.map((id) => ({ id, source: 'function render(ctx, env) { ctx.fillStyle = "#aabbcc"; }' })) }) };
};
await service.resumeProduction('mv-example', theRun().id);
await settle();
expect(asked.flat()).toEqual(ids.filter((id) => !finished.includes(id)));
const { PATHS } = await import('../../lib/paths.js');
const manifest = JSON.parse(await readFile(join(PATHS.data, current().composition.document.directory, 'manifest.json'), 'utf8'));
expect(manifest.sections.filter((section) => finished.includes(section.id)).every((section) => section.source.includes('#112233'))).toBe(true);
expect(theRun().documentCheckpoint.directory).toBe(current().composition.document.directory);
expect(theRun().steps.filter((step) => step.kind === 'author').map((step) => step.status)).toEqual(['failed', 'completed']);
});
it('reserves one author step for a document authored in several local batches', async () => {
seedCodeFirst(); const { author } = documentDoubles();
const single = author.getMockImplementation();
// Batched local authoring calls beforeSubmit once per batch before returning the document.
author.mockImplementation(async (id, input) => {
await input.beforeSubmit({ provider: authorProvider, model: AUTHORING.model });
return single(id, input);
});
await start({ pool: [], authoring: AUTHORING });
expect(author).toHaveBeenCalledOnce();
expect(theRun()).toMatchObject({ status: 'running', usage: { generations: 1 }, documentCheckpoint: { directory: current().composition.document.directory } });
expect(theRun().steps.filter((step) => step.kind === 'author')).toEqual([expect.objectContaining({ status: 'completed' })]);
});
it('authors a code-only plan with no media routes/jobs and completes only after its reviewed final render', async () => {
seedCodeFirst(); const { author, render } = documentDoubles();
await start({ pool: [], authoring: AUTHORING });
expect(jobs).toEqual([]); expect(dispatch).not.toHaveBeenCalled(); expect(author).toHaveBeenCalledOnce();
expect(theRun()).toMatchObject({ usage: { generations: 1, spentUsd: 0 }, documentCheckpoint: { directory: current().composition.document.directory } });
expect(startAutoReview.mock.calls[0][1]).toMatchObject({ documentRevisions: true, startSec: 0, endSec: 8 });
passOwnedReview(); await settle();
expect(render).toHaveBeenCalledOnce(); expect(theRun().status).toBe('running');
musicVideoEvents.emit('document-render', { projectId: 'mv-example', runId: theRun().id, jobId: 'final-render', attemptId: theRun().finalRender.attemptId, status: 'completed' });
await settle(); expect(theRun().status).toBe('completed');
});
it('prepares only missing selected still/footage assets and reuses imported takes', async () => {
seedCodeFirst({ percent: 50,
scenes: [{ sceneId: 'code', startSec: 0, endSec: 1 }, { sceneId: 'still', startSec: 1, endSec: 2, framePrompt: 'still' },
{ sceneId: 'imported', startSec: 2, endSec: 4, videoHistoryId: 'imported-take' },
{ sceneId: 'generated', startSec: 4, endSec: 8, framePrompt: 'frame', prompt: 'motion' }],
directions: ['procedural', 'still', 'existing-footage', 'generated-footage'].map((medium, index) => ({ sceneId: ['code', 'still', 'imported', 'generated'][index], medium, mediumRationale: 'Selected look' })),
});
const { author } = documentDoubles();
await start({ pool: [POOL[0], POOL[2]], authoring: AUTHORING });
expect(dispatch.mock.calls.map(([args]) => [args.stepKind, args.scene.sceneId])).toEqual([['frame', 'still'], ['frame', 'generated']]);
completeJob('job-1'); completeJob('job-2'); await settle();
expect(dispatch.mock.calls.at(-1)[0]).toMatchObject({ stepKind: 'clip', scene: { sceneId: 'generated' } });
expect(theRun().steps.at(-1).editInterval).toEqual({ startSec: 4, endSec: 8 });
completeJob('job-3'); await settle();
expect(author).toHaveBeenCalledOnce(); expect(current().scenes[2].videoHistoryId).toBe('imported-take');
expect(theRun().usage.generations).toBe(4);
});
it('refuses over-allowance and missing-import plans before any provider submission', async () => {
seedCodeFirst({ directions: [{ sceneId: 'mvs-code', medium: 'generated-footage', mediumRationale: 'Motion' }] });
const { author } = documentDoubles();
await expect(start({ pool: [], authoring: AUTHORING })).rejects.toMatchObject({ code: 'PRODUCTION_MEDIUM_CONFLICT' });
expect(dispatch).not.toHaveBeenCalled(); expect(author).not.toHaveBeenCalled();
seedCodeFirst({ directions: [{ sceneId: 'mvs-code', medium: 'existing-footage', mediumRationale: 'Imported' }] });
await expect(start({ pool: POOL, authoring: AUTHORING })).rejects.toMatchObject({ code: 'PRODUCTION_MEDIUM_CONFLICT' });
});
it('requires the current plan to be applied before starting any provider work', async () => {
seedCodeFirst(); const { author } = documentDoubles();
store.get('mv-example').treatment.revision = 2;
await expect(start({ pool: [], authoring: AUTHORING })).rejects.toMatchObject({ code: 'PRODUCTION_MEDIUM_CONFLICT' });
expect(dispatch).not.toHaveBeenCalled(); expect(author).not.toHaveBeenCalled();
expect(current().productionRuns).toBeUndefined();
});
it('refuses an approved performance disguised by a legacy card layer before any provider call', async () => {
seedCodeFirst({ percent: 100,
scenes: [{ sceneId: 'mvs-code', shotMode: 'performance', visualLayer: 'card', startSec: 0, endSec: 8 }],
directions: [{ sceneId: 'mvs-code', mode: 'performance', medium: 'generated-footage', mediumRationale: 'Sung performance' }],
});
const { author } = documentDoubles();
await expect(start({ pool: POOL, authoring: AUTHORING })).rejects.toMatchObject({ code: 'PRODUCTION_MEDIUM_CONFLICT' });
expect(dispatch).not.toHaveBeenCalled(); expect(author).not.toHaveBeenCalled();
});
it('revises only the failed code section while retaining selected assets and a zero video allowance', async () => {
seedCodeFirst(); const { regenerate } = documentDoubles();
await start({ pool: [], authoring: AUTHORING });
failOwnedReview(); await settle();
expect(regenerate).toHaveBeenCalledOnce(); expect(regenerate.mock.calls[0][1]).toBe('hook');
expect(theRun().usage.generations).toBe(2); expect(jobs).toEqual([]);
expect(current().productionPolicy.maxGeneratedVideoPercent).toBe(0);
expect(current().autoReviews[0].attempts.at(-1)).toMatchObject({ n: 2, excerptId: null });
});
it('halts a review-driven code revision before a provider call past the generation cap', async () => {
seedCodeFirst(); const { author } = documentDoubles();
await start({ pool: [], authoring: AUTHORING, limits: { maxGenerations: 1, maxReviewAttempts: 2 } });
let submitted = 0;
author.mockImplementationOnce(async (_id, input) => { await input.beforeSubmit({ provider: authorProvider, model: AUTHORING.model }); submitted += 1; });
failOwnedReview(); await settle();
expect(submitted).toBe(0); expect(theRun().usage.generations).toBe(1); expect(theRun().status).toBe('limit-reached');
});
it('refuses an unpriced author at the actual submission boundary under a dollar cap', async () => {
seedCodeFirst(); const { author } = documentDoubles();
let submitted = 0;
author.mockImplementationOnce(async (_id, input) => {
await input.beforeSubmit({ provider: { ...authorProvider, endpoint: 'https://api.example.com/v1' }, model: AUTHORING.model });
submitted += 1;
});
await start({ pool: [], authoring: AUTHORING, limits: { ...LIMITS, spendCapUsd: 1 } });
expect(submitted).toBe(0); expect(theRun()).toMatchObject({ status: 'blocked', usage: { generations: 0, spentUsd: 0 } });
expect(theRun().stopReason).toMatch(/no known price/);
});
it('does not accept a completed revision after its owning review stops', async () => {
seedCodeFirst(); const { accept } = documentDoubles();
await start({ pool: [], authoring: AUTHORING });
const selected = current().composition.document.directory;
const normal = accept.getMockImplementation();
accept.mockImplementationOnce(async (...args) => {
store.get('mv-example').autoReviews[0].status = 'stopped';
return normal(...args);
});
failOwnedReview(); await settle();
expect(current().composition.document.directory).toBe(selected);
expect(current().composition.documentDraft.directory).not.toBe(selected);
expect(theRun().usage.generations).toBe(2);
expect(theRun().status).toBe('blocked');
});
it('does not submit late authoring after cancellation during preparation', async () => {
seedCodeFirst(); const { author } = documentDoubles();
const { held, reached, release } = holdAt();
const normal = author.getMockImplementation();
author.mockImplementationOnce(async (id, input) => { await held(); return normal(id, input); });
await service.startProduction('mv-example', { pool: [], authoring: AUTHORING, limits: LIMITS });
await reached;
await service.cancelProduction('mv-example', theRun().id);
release(); await settle();
expect(theRun().status).toBe('canceled'); expect(theRun().usage.generations).toBe(0);
expect(current().composition.documentDraft).toBeUndefined();
});
it('drops a paid authoring result when the policy changes before publication', async () => {
seedCodeFirst(); const { author } = documentDoubles();
const { held, reached, release } = holdAt();
author.mockImplementationOnce(async (_id, input) => {
await input.beforeSubmit({ provider: authorProvider, model: AUTHORING.model }); await held();
await storeMutation((project) => { input.verifyCurrent(project); return { project }; });
});
await service.startProduction('mv-example', { pool: [], authoring: AUTHORING, limits: LIMITS });
await reached;
store.get('mv-example').productionPolicy.maxGeneratedVideoPercent = 5;
release(); await settle();
expect(current().composition.document).toBeUndefined(); expect(theRun().usage.generations).toBe(1);
expect(theRun().steps[0].status).toBe('failed');
});
it('retains a fast final-render terminal event when its queue link arrives later', async () => {
seedCodeFirst(); const { render } = documentDoubles();
render.mockImplementationOnce(async () => {
musicVideoEvents.emit('document-render', { projectId: 'mv-example', runId: theRun().id, jobId: 'fast-render', attemptId: theRun().finalRender.attemptId, status: 'completed' });
await new Promise((resolve) => setTimeout(resolve, 0)); return { jobId: 'fast-render' };
});
await start({ pool: [], authoring: AUTHORING }); passOwnedReview(); await settle();
expect(theRun()).toMatchObject({ status: 'completed', finalRender: { status: 'completed', jobId: 'fast-render' } });
});
it('refuses a replaced selected document before final render', async () => {
seedCodeFirst(); const { render } = documentDoubles();
await start({ pool: [], authoring: AUTHORING });
store.get('mv-example').composition.document.directory = 'music-video/mv-example/composition/doc-replaced';
passOwnedReview(); await settle();
expect(render).not.toHaveBeenCalled(); expect(theRun().status).toBe('needs-replan');
});
it('stops a pending final-render preparation before encoding', async () => {
seedCodeFirst(); const { render } = documentDoubles();
const { held, reached, release } = holdAt();
let encoded = 0;
render.mockImplementationOnce(async (_id, options) => { await held(); options.verifyCurrent(current()); encoded += 1; return { jobId: 'late-render' }; });
await start({ pool: [], authoring: AUTHORING }); passOwnedReview();
await reached;
await service.stopProduction('mv-example', theRun().id); release(); await settle();
expect(encoded).toBe(0); expect(theRun().status).toBe('stopped');
});
});
it('rechecks a generated shot and zero allowance after asynchronous provider preparation', async () => {
seedCodeFirst({ percent: 100,
scenes: [{ sceneId: 'mvs-a', startSec: 0, endSec: 8, referenceImageId: 'frame.png', prompt: 'move' }],
directions: [{ sceneId: 'mvs-a', medium: 'generated-footage', mediumRationale: 'Selected exception' }],
});
documentDoubles();
const { held, reached, release } = holdAt();
dispatch.mockImplementationOnce(async (args) => {
await held();
await service.assertProductionSubmission('mv-example', args.tag.productionRunId, args.tag.productionStepKey, { sceneId: 'mvs-a', kind: 'video' });
return enqueueing(args);
});
await service.startProduction('mv-example', { pool: [POOL[2]], authoring: AUTHORING, limits: LIMITS });
await reached;
store.get('mv-example').productionPolicy.maxGeneratedVideoPercent = 0;
release(); await settle();
expect(jobs).toHaveLength(0); expect(theRun().usage.generations).toBe(0);
expect(theRun().steps[0].status).toBe('refused');
});
it('charges a selected performance retry by its actual padded provider duration without adding edit share', async () => {
seedCodeFirst({ percent: 100, videoSettings: { backend: 'fal', falModelId: 'minimax/h3-max/image-to-video' },
audioAnalysis: { durationSec: 30, sections: [{ startSec: 0, endSec: 30 }] },
scenes: [{ sceneId: 'performance', shotMode: 'performance', startSec: 0, endSec: 4, referenceImageId: 'frame.png', prompt: 'perform' }],
directions: [{ sceneId: 'performance', medium: 'generated-footage', mediumRationale: 'Sung performance' }],
});
documentDoubles();
const usable = env.isVideoModeUsable;
env.isVideoModeUsable = (_settings, mode) => mode === 'fal';
try {
await start({ pool: [{ kind: 'video', mode: 'fal' }], authoring: AUTHORING, limits: { ...LIMITS, spendCapUsd: 1.7 } });
const job = jobs[0]; job.status = 'failed'; mediaJobEvents.emit('failed', job); await settle();
expect(theRun().status).toBe('blocked');
await service.resumeProduction('mv-example', theRun().id); await settle();
const clips = theRun().steps.filter((step) => step.kind === 'clip');
expect(clips).toHaveLength(2);
expect(clips.map((step) => step.costUsd)).toEqual([0.808, 0.808]);
expect(clips.map((step) => step.editInterval)).toEqual([{ startSec: 0, endSec: 4 }, { startSec: 0, endSec: 4 }]);
jobs[1].status = 'failed'; mediaJobEvents.emit('failed', jobs[1]); await settle();
// Explicit resume resets slot failure count, but the paid retry budget stays spent.
await service.resumeProduction('mv-example', theRun().id); await settle();
expect(jobs).toHaveLength(2); expect(theRun()).toMatchObject({ status: 'limit-reached', usage: { generations: 2, spentUsd: 1.616 } });
} finally { env.isVideoModeUsable = usable; }
});
it.each([1_000, 120_000])('keeps interrupted authoring charged at %i ms without a queue job and requires explicit resume', async (reservationAge) => {
seedCodeFirst(); const { author } = documentDoubles();
const { held, reached, release } = holdAt();
author.mockImplementationOnce(async (_id, input) => {
await input.beforeSubmit({ provider: authorProvider, model: AUTHORING.model }); await held();
input.verifyCurrent(current());
});
await service.startProduction('mv-example', { pool: [], authoring: AUTHORING, limits: LIMITS });
await reached;
store.get('mv-example').productionRuns[0].processId = 'previous-process';
release(); await settle();
expect(author).toHaveBeenCalledOnce(); expect(current().composition.document).toBeUndefined();
expect(theRun().usage.generations).toBe(1);
// Persisted submission intent after a crash is not refundable even past the queue lease.
store.get('mv-example').productionRuns[0].steps[0].status = 'reserved';
store.get('mv-example').productionRuns[0].steps[0].reservedAt = new Date(Date.now() - reservationAge).toISOString();
await service.resumeProduction('mv-example', theRun().id); await settle();
expect(author).toHaveBeenCalledTimes(2); expect(theRun().usage.generations).toBe(2);
});
it('ignores an earlier failed render attempt completion after explicit retry', async () => {
seedCodeFirst(); documentDoubles();
await start({ pool: [], authoring: AUTHORING }); passOwnedReview(); await settle();
const oldAttempt = theRun().finalRender.attemptId;
musicVideoEvents.emit('document-render', { projectId: 'mv-example', runId: theRun().id, jobId: 'final-render', attemptId: oldAttempt, status: 'failed', error: 'encoder failed' });
await settle(); expect(theRun().status).toBe('blocked');
await service.resumeProduction('mv-example', theRun().id); await settle();
expect(theRun().finalRender.attemptId).not.toBe(oldAttempt);
musicVideoEvents.emit('document-render', { projectId: 'mv-example', runId: theRun().id, jobId: 'final-render', attemptId: oldAttempt, status: 'completed' });
await settle(); expect(theRun().status).toBe('running');
expect(theRun().finalRender.status).toBe('queued');
});
it('requires reauthoring and a new review after an accepted policy/selected-asset change', async () => {
seedCodeFirst(); const { author } = documentDoubles();
await start({ pool: [], authoring: AUTHORING });
store.get('mv-example').scenes[0].referenceImageId = 'new-selection.png';
await settle(); expect(theRun().status).toBe('needs-replan');
await service.resumeProduction('mv-example', theRun().id, { acceptBasis: true }); await settle();
expect(author).toHaveBeenCalledTimes(2);
expect(autoReview.cancelAutoReview).toHaveBeenCalledWith('mv-example', 'mvar-example');
expect(theRun().documentCheckpoint.directory).toBe(current().composition.document.directory);
});
// These workflow tests catch spending video budget before asset-bound admission,
// loss of candidate history, and stop/selection races while vision is in flight.
describe('authored plate preflight', () => {
const contract = { version: 1, purpose: 'Two people greet', actions: [{ startSec: 0, endSec: 2, subject: 'Person A', description: 'waves' }], reactions: [{ startSec: 2, endSec: 4, subject: 'Person B', description: 'waves back' }] };
const plateScene = () => ({ sceneId: 'mvs-a', label: 'Greeting', startSec: 0, endSec: 4, framePrompt: 'Two people greet', prompt: 'Both wave', referenceImageId: 'one.png', direction: { actionContract: contract }, takes: [{ takeId: 'take-one', kind: 'image', assetId: 'one.png', status: 'candidate' }] });
const verdict = (args, status) => plateReviewEvidence(args.scene, args.assetId, args.runId, JSON.stringify({ checks: plateRequirements(args.scene).map(({ id }) => ({ id, status: id === 'plate-2' ? status : 'pass', note: status === 'fail' ? 'Second person is missing' : 'Visible' })) }));
it('selects a qualifying existing candidate, retains the failed original, and charges every analysis before video', async () => {
const scene = plateScene();
scene.takes.push({ takeId: 'take-two', kind: 'image', assetId: 'two.png', status: 'candidate' });
seedProject({ scenes: [scene] });
reviewPlate.mockImplementation(async (args) => { await args.beforeExecute(); return verdict(args, args.assetId === 'one.png' ? 'fail' : 'pass'); });
await start();
expect(current().scenes[0].referenceImageId).toBe('two.png');
expect(current().scenes[0].takes.map((take) => take.assetId)).toEqual(['one.png', 'two.png']);
expect(current().scenes[0].takes[0].plateEvidence.verdict).toBe('fail');
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch.mock.calls[0][0]).toMatchObject({ stepKind: 'clip', scene: { referenceImageId: 'two.png' } });
expect(theRun().usage.generations).toBe(3);
});
it('repairs at most once through the allowed image queue and charges the repair plus both reviews', async () => {
seedProject({ scenes: [plateScene()] });
reviewPlate.mockImplementation(async (args) => { await args.beforeExecute(); return verdict(args, args.assetId === 'one.png' ? 'fail' : 'pass'); });
await start();
expect(dispatch.mock.calls.map(([args]) => args.stepKind)).toEqual(['frame']);
const repairJob = jobs[0];
repairJob.status = 'completed';
store.get('mv-example').scenes[0].takes.push({ takeId: 'take-repaired', kind: 'image', assetId: 'repaired.png', status: 'candidate' });
mediaJobEvents.emit('completed', repairJob);
musicVideoEvents.emit('scene-image', { projectId: 'mv-example', sceneId: 'mvs-a' });
await settle();
expect(dispatch.mock.calls.map(([args]) => args.stepKind)).toEqual(['frame', 'clip']);
expect(current().scenes[0].referenceImageId).toBe('repaired.png');
expect(current().scenes[0].takes.map((take) => take.assetId)).toEqual(['one.png', 'repaired.png']);
expect(theRun().usage.generations).toBe(4);
});
it('leaves inconclusive analysis resumable without a generation retry loop', async () => {
seedProject({ scenes: [plateScene()] });
reviewPlate.mockImplementation(async (args) => verdict(args, 'unverified'));
await start();
expect(theRun()).toMatchObject({ status: 'blocked', usage: { generations: 1 } });
expect(dispatch).not.toHaveBeenCalled();
await settle();
expect(reviewPlate).toHaveBeenCalledTimes(1);
expect(theRun().stopReason).toMatch(/inconclusive/);
});
it('blocks unknown-cost vision before a dollar-capped run spends, then resumes with a free reviewer', async () => {
seedProject({ scenes: [plateScene()] });
resolvePlateReviewer.mockResolvedValue({ provider: { id: 'reviewer-example' }, model: 'vision-example', costUsd: null });
await start({ pool: [POOL[0], POOL[2]], limits: { ...LIMITS, spendCapUsd: 5 } });
expect(theRun().status).toBe('blocked');
expect(reviewPlate).not.toHaveBeenCalled();
expect(theRun().usage.generations).toBe(0);
resolvePlateReviewer.mockResolvedValue({ provider: { id: 'reviewer-example' }, model: 'vision-example', costUsd: 0 });
reviewPlate.mockImplementation(async (args) => verdict(args, 'fail'));
await service.resumeProduction('mv-example', theRun().id, { limits: { maxGenerations: 10 } });
await settle();
expect(dispatch.mock.calls.map(([args]) => args.stepKind)).toEqual(['frame']);
expect(theRun().usage.generations).toBe(2);
});
it('refuses repair when the preceding analysis exhausted the generation budget', async () => {
seedProject({ scenes: [plateScene()] });
reviewPlate.mockImplementation(async (args) => verdict(args, 'fail'));
await start({ limits: { ...LIMITS, maxGenerations: 1 } });
expect(theRun()).toMatchObject({ status: 'limit-reached', usage: { generations: 1 } });
expect(dispatch).not.toHaveBeenCalled();
});
it('does not animate or store approval when the selection changes during vision', async () => {
seedProject({ scenes: [plateScene()] });
reviewPlate.mockImplementation(async (args) => {
store.get('mv-example').scenes[0].referenceImageId = 'other.png';
return verdict(args, 'pass');
});
await start();
expect(theRun().status).toBe('blocked');
expect(dispatch).not.toHaveBeenCalled();
expect(current().scenes[0].takes[0].plateEvidence).toBeUndefined();
});
it('rejects a selection race at the final video submission boundary', async () => {
seedProject({ scenes: [plateScene()] });
dispatch.mockImplementation(async ({ tag }) => {
store.get('mv-example').scenes[0].referenceImageId = 'changed-after-reservation.png';
await service.assertProductionSubmission('mv-example', tag.productionRunId, tag.productionStepKey, { sceneId: 'mvs-a', kind: 'video' });
return enqueueing({ stepKind: 'clip', tag });
});
await start();
expect(jobs).toHaveLength(0);
expect(theRun().status).toBe('blocked');
expect(theRun().usage.generations).toBe(1);
});
it('leaves interrupted plate reservations charged and calls no provider until explicit resume', async () => {
const project = seedProject({ scenes: [plateScene()] });
const started = startProductionOnProject(project, { pool: POOL, limits: LIMITS, processId: 'previous-process' });
const planned = markProductionPlanned(started.project, started.run.id);
const reserved = reserveProductionStep(planned.project, started.run.id, { kind: 'plate', sceneId: 'mvs-a', route: { kind: 'plate', mode: 'reviewer-example', model: 'vision-example' }, costUsd: 0, processId: 'previous-process' });
store.set(project.id, reserved.project);
await service.__advanceProductionForTests(project.id, started.run.id);
expect(reviewPlate).not.toHaveBeenCalled();
expect(dispatch).not.toHaveBeenCalled();
await service.resumeProduction(project.id, started.run.id);
await settle();
expect(theRun().steps[0]).toMatchObject({ kind: 'plate', status: 'failed' });
expect(theRun().usage.generations).toBe(3);
expect(reviewPlate).toHaveBeenCalledTimes(1);
expect(dispatch.mock.calls.map(([args]) => args.stepKind)).toEqual(['clip']);
});
it('honors cancellation while plate analysis is running, retaining its spend and no video job', async () => {
seedProject({ scenes: [plateScene()] });
const { held, reached, release } = holdAt();
reviewPlate.mockImplementation(async (args) => { await held(); return verdict(args, 'pass'); });
const { run } = await service.startProduction('mv-example', { pool: POOL, limits: LIMITS, reviewer: {} });
await reached;
await service.cancelProduction('mv-example', run.id);
release();
await settle();
expect(theRun()).toMatchObject({ status: 'canceled', usage: { generations: 1 } });
expect(dispatch).not.toHaveBeenCalled();
});
});
// These regressions pin the public production workflow, not helper internals.
describe('budgeted production pilots (#9351)', () => {
function board() {
seedProject({ scenes: Array.from({ length: 4 }, (_, i) => ({
sceneId: 'shot-' + i, label: 'Example shot ' + i, framePrompt: 'a stage', prompt: 'lights move',
startSec: i * 2, endSec: (i + 1) * 2, takes: [],
})) });
}
function pendingPilot() {
startAutoReview.mockImplementationOnce(async (id, input) => {
await service.chargeProductionReview(id, input.productionRunId, { reviewRunId: 'pilot-example', attemptN: 1, costUsd: 0 });
const project = store.get(id);
const review = { id: 'pilot-example', status: 'running', productionRunId: input.productionRunId,
productionPilotSceneId: input.productionPilotSceneId, attempts: [{ n: 1 }], usage: { reviews: 1, generations: 0 }, limits: input.limits };
project.autoReviews = [review];
return { run: review };
});
}
async function reachPilot() {
await start();
expect(dispatch.mock.calls.map(([a]) => a.scene.sceneId)).toEqual(['shot-0']);
completeJob('job-1'); await settle();
completeJob('job-2'); await settle();
}
function verdict(result, dependencies = pilotDependencies(current(), 'shot-0')) {
const review = store.get('mv-example').autoReviews[0];
review.status = result.verdict === 'pass' ? 'passed' : 'needs-human';
review.attempts = [{ n: 1, review: { ...result, dependencies, reviewedAt: new Date().toISOString() } }];
musicVideoEvents.emit('auto-review', { projectId: 'mv-example', run: clone(review), action: { type: 'idle' } });
}
it('selects performance, interaction and the more complex camera operation before ordinary shots', async () => {
const scenes = Array.from({ length: 6 }, (_, i) => ({ sceneId: 'shot-' + i, framePrompt: 'a stage', prompt: 'movement',
startSec: i, endSec: i + 1, takes: [] }));
scenes[1].direction = { actionContract: { cameraConstraints: ['locked'] } };
scenes[2].direction = { actionContract: { cameraConstraints: ['dolly inward', 'orbit the pair'] } };
scenes[3].direction = { actionContract: { actions: [{ subject: 'singer' }], reactions: [{ subject: 'listener' }] } };
scenes[4].shotMode = 'performance';
scenes[5].visualLayer = 'still';
seedProject({ scenes, composition: { mode: 'composed' } }); await start();
expect(theRun().pilot.scenes.map((p) => [p.operation, p.sceneId])).toEqual([
['performance', 'shot-4'], ['interaction', 'shot-3'], ['camera', 'shot-2'], ['cutaway', 'shot-0'], ['still', 'shot-5'],
]);
expect(dispatch.mock.calls.map(([a]) => a.scene.sceneId)).not.toContain('shot-1');
});
it('holds bulk reservations until a pass, then reuses the pilot without another submission', async () => {
board(); pendingPilot(); await reachPilot();
expect(dispatch).toHaveBeenCalledTimes(2);
expect(startAutoReview.mock.calls[0][1]).toMatchObject({ productionPilotSceneId: 'shot-0', startSec: 0, endSec: 2, limits: { maxAttempts: 1 } });
expect(() => reserveProductionStep(current(), theRun().id, { kind: 'frame', sceneId: 'shot-1', route: POOL[0], processId: theRun().processId })).toThrow(/pilot/i);
verdict({ verdict: 'pass', evidence: { continuous: true, continuousFrames: 12 } }); await settle();
expect(dispatch.mock.calls.slice(2).map(([a]) => a.scene.sceneId)).toEqual(['shot-1', 'shot-2', 'shot-3']);
expect(current().scenes[0].videoHistoryId).toBe('job-2');
expect(dispatch.mock.calls.filter(([a]) => a.scene.sceneId === 'shot-0')).toHaveLength(2);
const shown = (await service.getProduction('mv-example', theRun().id)).run;
expect(shown.pilot.scenes[0]).toMatchObject({ status: 'accepted', evidence: { continuous: true } });
expect(shown.accounting.reviews).toBe(1);
});
it.each([
[{ composition: 'fail' }, 'composition'],
[{ motion: 'fail' }, 'provider-capability'],
[{ lipSync: 'fail' }, 'temporal-alignment'],
])('blocks failed evidence and chooses an edit before any replacement spend (%j)', async (checks, category) => {
board(); pendingPilot(); await reachPilot();
verdict({ verdict: 'revise', checks, reason: 'Synthetic failed evidence' }); await settle();
expect(theRun().status).toBe('blocked');
await service.resumeProduction('mv-example', theRun().id); await settle();
expect(dispatch).toHaveBeenCalledTimes(2);
expect(startAutoReview).toHaveBeenCalledTimes(1);
expect((await service.getProduction('mv-example', theRun().id)).run.pilot.scenes[0].repair)
.toMatchObject({ category, expectedGenerationSpendUsd: 0 });
});
// Uniquely catches a pilot re-reviewing the take its old prompt made, or staying halted on that review.
it('makes the pilot again when its prompt changes after a failed review', async () => {
board(); pendingPilot(); await reachPilot();
verdict({ verdict: 'revise', checks: { motion: 'fail' }, reason: 'Synthetic failed evidence' }); await settle();
expect(theRun().status).toBe('blocked');
store.get('mv-example').scenes[0].prompt = 'lights sweep left';
await service.resumeProduction('mv-example', theRun().id, { acceptBasis: true }); await settle();
expect(dispatch.mock.calls[2][0]).toMatchObject({ stepKind: 'clip', scene: expect.objectContaining({ sceneId: 'shot-0', prompt: 'lights sweep left' }) });
expect(startAutoReview).toHaveBeenCalledTimes(1);
});
// Uniquely catches an orchestrator-accepted pilot still holding the bulk wave, or an acceptance surviving new media.
it('lets an accepted pilot release the bulk wave while its reviewed take stays selected', async () => {
const { acceptProductionPilot } = await import('./production.js');
board(); pendingPilot(); await reachPilot();
verdict({ verdict: 'revise', checks: { motion: 'fail' }, reason: 'Synthetic failed evidence' }); await settle();
store.set('mv-example', acceptProductionPilot(current(), theRun().id, 'shot-0', { by: 'orchestrator', note: 'Accepted at the limit' }).project);
await service.resumeProduction('mv-example', theRun().id); await settle();
expect(dispatch.mock.calls.map(([a]) => a.scene.sceneId)).toEqual(expect.arrayContaining(['shot-1', 'shot-2', 'shot-3']));
expect((await service.getProduction('mv-example', theRun().id)).run.pilot.scenes[0].status).toBe('accepted');
});
it('never treats missing provenance as an accepted pilot', async () => {
board(); pendingPilot(); await reachPilot();
verdict({ verdict: 'pass' }, null); await settle();
expect(theRun().status).toBe('blocked');
expect(dispatch).toHaveBeenCalledTimes(2);
});
it('stops late pilot completion and repins it only on explicit Resume after a restart', async () => {
board(); pendingPilot(); await reachPilot();
store.get('mv-example').productionRuns[0].processId = 'old-process';
verdict({ verdict: 'pass' }); await settle();
expect(dispatch).toHaveBeenCalledTimes(2);
await service.resumeProduction('mv-example', theRun().id); await settle();
expect(dispatch).toHaveBeenCalledTimes(5);
expect(theRun().usage.generations).toBe(5);
});
it('does not let a changed allowed route bypass its pilot even after a pass', async () => {
board(); pendingPilot(); await reachPilot();
service.__setProductionDepsForTests({ loadEnv: async () => env, dispatch, queue: async () => queue,
autoReview: async () => autoReview, chooseRoute: async () => ({ route: POOL[1], rationale: 'changed route' }) });
verdict({ verdict: 'pass' }); await settle();
expect(theRun().status).toBe('blocked');
expect(theRun().stopReason).toMatch(/no accepted pilot/);
expect(dispatch).toHaveBeenCalledTimes(2);
});
it('rechecks pilot assets at the final queue boundary and refunds a refused bulk reservation', async () => {
board(); pendingPilot(); await reachPilot();
dispatch.mockImplementationOnce(async (args) => {
store.get('mv-example').scenes[0].referenceImageId = 'changed-plate.png';
await service.assertProductionSubmission('mv-example', args.tag.productionRunId, args.tag.productionStepKey,
{ sceneId: args.tag.sceneId, kind: 'image' });
return enqueueing(args);
});
verdict({ verdict: 'pass' }); await settle();
expect(jobs).toHaveLength(2);
expect(theRun()).toMatchObject({ status: 'blocked', usage: { generations: 2 } });
expect(theRun().steps.at(-1)).toMatchObject({ status: 'refused', errorCode: 'PRODUCTION_PILOT_REQUIRED' });
});
it('keeps late pilot evidence after Stop without releasing the bulk wave', async () => {
board(); pendingPilot(); await reachPilot();
await service.stopProduction('mv-example', theRun().id);
expect(autoReview.stopAutoReview).toHaveBeenCalledWith('mv-example', 'pilot-example');
verdict({ verdict: 'pass' }); await settle();
expect(theRun().status).toBe('stopped');
expect(dispatch).toHaveBeenCalledTimes(2);
await service.cancelProduction('mv-example', theRun().id); await settle();
expect(theRun().status).toBe('canceled');
expect(dispatch).toHaveBeenCalledTimes(2);
});
it('resumes an exhausted shared review budget without regenerating any accepted asset', async () => {
board();
await start({ limits: { maxGenerations: 10, maxReviewAttempts: 1 } });
completeJob('job-1'); await settle();
completeJob('job-2'); await settle();
for (const id of ['job-3', 'job-4', 'job-5']) completeJob(id);
await settle();
for (const id of ['job-6', 'job-7', 'job-8']) completeJob(id);
await settle();
expect(theRun().status).toBe('limit-reached');
expect(dispatch).toHaveBeenCalledTimes(8);
await service.resumeProduction('mv-example', theRun().id, { limits: { maxReviewAttempts: 3 } }); await settle();
expect(theRun()).toMatchObject({ status: 'running', reviewRunId: 'mvar-example' });
expect(dispatch).toHaveBeenCalledTimes(8);
expect(startAutoReview.mock.calls.at(-1)[1].limits.maxAttempts).toBe(2);
});
it('bounds pilot and final review calls together, including unknown costs and concurrent admissions', async () => {
board(); pendingPilot(); await reachPilot();
await service.chargeProductionReview('mv-example', theRun().id, { reviewRunId: 'second', attemptN: 1, costUsd: 0 });
store.get('mv-example').productionRuns[0].limits.maxReviewAttempts = 3;
const results = await Promise.allSettled([1, 2].map((n) => service.chargeProductionReview('mv-example', theRun().id,
{ reviewRunId: 'race-' + n, attemptN: 1, costUsd: 0 })));
expect(results.map((r) => r.status).sort()).toEqual(['fulfilled', 'rejected']);
expect(theRun().steps.filter((s) => s.kind === 'review')).toHaveLength(3);
store.get('mv-example').productionRuns[0].limits = { ...theRun().limits, maxReviewAttempts: 4, spendCapUsd: 0 };
await expect(service.chargeProductionReview('mv-example', theRun().id, { reviewRunId: 'unpriced', attemptN: 1, costUsd: null }))
.rejects.toMatchObject({ code: 'PRODUCTION_COST_UNKNOWN' });
expect(theRun().steps.filter((s) => s.kind === 'review')).toHaveLength(3);
});
it('pauses a failed pilot rather than opening an identical paid revision', () => {
const project = seedProject();
const started = startAutoReviewOnProject(project, { startSec: 0, endSec: 4, limits: { maxAttempts: 1, maxGenerations: 2 },
productionRunId: 'production-example', productionPilotSceneId: 'mvs-a' });
const dependencies = pilotDependencies(project, 'mvs-a');
const run = { ...started.run, attempts: [{ n: 1, excerptId: 'draft-example', review: { verdict: 'revise', dependencies }, revisionId: null }] };
const reviewed = { ...started.project, excerpts: [{ id: 'draft-example', status: 'complete' }], autoReviews: [run] };
expect(nextAutoReviewStep(reviewed, run)).toMatchObject({ type: 'halt', status: 'needs-human' });
const begun = beginAttemptReview(started.project, started.run.id);
const deferred = deferAttemptReview(begun.project, begun.run.id, { code: 'PRODUCTION_BUDGET_EXHAUSTED', message: 'Raise cap and resume' });
expect(deferred.run).toMatchObject({ status: 'limit-reached', usage: { reviews: 0 }, attempts: [{ review: null }] });
});
});
it('blocks unreviewed production, resumes an approved board, and refuses a revocation before queue submission', async () => {
creativeReview.real = true;
const { approveProductionStage, productionReviewBasis } = await vi.importActual('./productionReview.js');
const seeded = seedProject({
devArtifacts: [{ id: 'guide', version: 1, file: 'guide.html', mimeType: 'text/html' }],
});
seeded.productionReview = { draft: {
cast: 'Paper dancer', environments: 'Theatre', visualLanguage: 'Ink silhouettes', motionLanguage: 'Slow orbit',
guideArtifactId: 'guide', lyricsMode: 'instrumental', timingNotes: 'Synthetic instrumental master checked.',
storyboard: seeded.scenes.map(scene => ({ sceneId: scene.sceneId, lyricCueIds: [], action: 'Dancer unfolds', staging: 'Wide theatre', camera: 'Orbit', transition: 'Fade' })),
} };
await start();
expect(theRun().status).toBe('blocked');
expect(dispatch).not.toHaveBeenCalled();
for (const stage of ['art', 'storyboard']) {
const project = current();
store.set(project.id, approveProductionStage(project, { stage, basis: productionReviewBasis(project)[stage] }));
}
dispatch.mockImplementation(async ({ tag }) => {
const project = store.get('mv-example');
delete project.productionReview.approvals.art;
await service.assertProductionSubmission(project.id, tag.productionRunId, tag.productionStepKey, {
sceneId: tag.sceneId, kind: 'image',
});
return enqueueing({ stepKind: 'frame', tag });
});
await service.resumeProduction('mv-example', theRun().id);
await settle();
expect(dispatch).toHaveBeenCalledOnce();
expect(jobs).toHaveLength(0);
expect(theRun().status).toBe('blocked');
expect(theRun().steps[0]).toMatchObject({ status: 'refused', errorCode: 'MUSIC_VIDEO_APPROVAL_REQUIRED' });
});
it('moves past the optional animated proof to review without rendering or waiting on one', async () => {
creativeReview.real = true;
const { approveProductionStage, productionReviewBasis } = await vi.importActual('./productionReview.js');
const { renderProductionProof } = await import('./productionReviewService.js');
let proofNumber = 0;
renderProductionProof.mockImplementation(async (id, window) => {
const project = store.get(id);
const excerptId = `proof-${++proofNumber}`;
project.excerpts = [{ id: excerptId, status: 'rendering', filename: null }];
project.productionReview.proof = { ...window, excerptId };
project.productionReview.proof.basis = productionReviewBasis(project).proof;
});
const seeded = seedProject({
composition: { mode: 'document' },
scenes: [{ sceneId: 'card', startSec: 0, endSec: 100, visualLayer: 'card' }],
audioAnalysis: { durationSec: 100, sections: [{ label: 'Chorus', startSec: 95, endSec: 100 }] },
devArtifacts: [{ id: 'guide', version: 1, file: 'guide.html', mimeType: 'text/html' }],
});
seeded.productionReview = { draft: {
cast: 'Paper figure', environments: 'Theatre', visualLanguage: 'Ink silhouettes', motionLanguage: 'Slow orbit',
guideArtifactId: 'guide', lyricsMode: 'instrumental', timingNotes: 'Synthetic instrumental master checked.',
storyboard: [{ sceneId: 'card', lyricCueIds: [], action: 'Figure unfolds', staging: 'Wide theatre', camera: 'Orbit', transition: 'Fade' }],
} };
for (const stage of ['art', 'storyboard']) {
const project = current();
store.set(project.id, approveProductionStage(project, { stage, basis: productionReviewBasis(project)[stage] }));
}
await start();
expect(renderProductionProof).not.toHaveBeenCalled();
expect(JSON.stringify(theRun())).not.toMatch(/animated proof/);
expect(startAutoReview).toHaveBeenCalled();
});
it('allows production to fill still-less locked shots on an approved board and proceed to clips without re-dispatching', async () => {
creativeReview.real = true;
const { approveProductionStage, productionReviewBasis, shotLocks } = await vi.importActual('./productionReview.js');
const seeded = seedProject({
devArtifacts: [{ id: 'guide', version: 1, file: 'guide.html', mimeType: 'text/html' }],
});
seeded.productionReview = { draft: {
cast: 'Paper dancer', environments: 'Theatre', visualLanguage: 'Ink silhouettes', motionLanguage: 'Slow orbit',
guideArtifactId: 'guide', lyricsMode: 'instrumental', timingNotes: 'Synthetic instrumental master checked.',
storyboard: seeded.scenes.map(scene => ({ sceneId: scene.sceneId, lyricCueIds: [], action: 'Dancer unfolds', staging: 'Wide theatre', camera: 'Orbit', transition: 'Fade' })),
} };
await start();
expect(theRun().status).toBe('blocked');
for (const stage of ['art', 'storyboard']) {
const project = current();
store.set(project.id, approveProductionStage(project, { stage, basis: productionReviewBasis(project)[stage] }));
}
// Board approved with still-less shots: every shot is locked with empty referenceImageId
expect(shotLocks(current())).toEqual({ 'mvs-a': 'locked', 'mvs-b': 'locked' });
expect(current().scenes[0].referenceImageId).toBeFalsy();
expect(current().scenes[1].referenceImageId).toBeFalsy();
await service.resumeProduction('mv-example', theRun().id);
await settle();
expect(theRun().status).toBe('running');
// Frames are dispatched for both scenes
expect(jobs).toHaveLength(2);
expect(jobs.every(j => j.kind === 'image')).toBe(true);
const frameJobA = jobs.find(j => j.params.musicVideo.sceneId === 'mvs-a');
const frameJobB = jobs.find(j => j.params.musicVideo.sceneId === 'mvs-b');
expect(frameJobA).toBeDefined();
expect(frameJobB).toBeDefined();
// Complete first frame through hook (generation-completion)
await completeJobThroughHook(frameJobA.id);
await settle();
// Slot for scene A is filled, lock still locked!
let updated = current();
const sceneA = updated.scenes.find(s => s.sceneId === 'mvs-a');
expect(sceneA.referenceImageId).toBe(`${frameJobA.id}.png`);
expect(shotLocks(updated)['mvs-a']).toBe('locked');
// No re-dispatch of frame A!
expect(jobs.filter(j => j.params.musicVideo.sceneId === 'mvs-a' && j.kind === 'image')).toHaveLength(1);
// Complete second frame through hook
await completeJobThroughHook(frameJobB.id);
await settle();
updated = current();
const sceneB = updated.scenes.find(s => s.sceneId === 'mvs-b');
expect(sceneB.referenceImageId).toBe(`${frameJobB.id}.png`);
expect(shotLocks(updated)).toEqual({ 'mvs-a': 'locked', 'mvs-b': 'locked' });
// No re-dispatch of frame B!
expect(jobs.filter(j => j.params.musicVideo.sceneId === 'mvs-b' && j.kind === 'image')).toHaveLength(1);
// Now both frames are filled, production proceeds to clip dispatch (no re-dispatch loop)!
expect(jobs.length).toBeGreaterThanOrEqual(3);
expect(jobs.some(j => j.kind === 'video')).toBe(true);
});