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: let orchestrator repairs land on shots its own approval locked (#12065)

[._.]/ Adam Eivyf779c94f1bc9
diff --git a/server/services/musicVideo/autonomousService.js b/server/services/musicVideo/autonomousService.js
index d88c40c92b..35acefdbed 100644
--- a/server/services/musicVideo/autonomousService.js
+++ b/server/services/musicVideo/autonomousService.js
@@ -1,6 +1,6 @@
import { musicVideoMediaMode, musicVideoAllowsMedia } from '../../lib/musicVideoMediaPolicy.js';
import { prepareProductionReview } from './productionReviewService.js';
-import { ALIGNMENT_UNVERIFIED_PROBLEM, assertProductionApproval, carryProductionApprovals, productionAlignmentBasis, productionReadiness, productionReviewBasis } from './productionReview.js';
+import { ALIGNMENT_UNVERIFIED_PROBLEM, assertProductionApproval, carryProductionApprovals, nameShots, productionAlignmentBasis, productionReadiness, productionReviewBasis } from './productionReview.js';
import { CAST_SETS_WORKING } from './castAndSets.js';
import { applyTreatmentPatch, applyTreatmentToProject } from './treatment.js';
import { codeFirstProductionAssets, normalizeMusicVideoProductionPolicy } from '../../lib/musicVideoMediumPlan.js';
@@ -93,7 +93,8 @@ import {
} from '../../lib/musicVideoAutonomous.js';
import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';
import { getProject, mutateProjectRecord as mutateRecord } from './projects.js';
-const mutateProjectRecord = (id, transform) => mutateRecord(id, transform, { source: 'orchestrator' });
+// `runId` lets the run's repairs land on shots its own approval locked (preserveAutomatedShotLocks).
+const mutateProjectRecord = (id, transform, { runId = null } = {}) => mutateRecord(id, transform, { source: 'orchestrator', runId });
import { musicVideoEvents } from './events.js';
import { musicVideoDependencyChanges } from '../../lib/musicVideoDependencies.js';
import { isPerformanceScene, performanceCapability } from '../../lib/musicVideoShotTiming.js';
@@ -1264,11 +1265,13 @@ function stopsBeforeClips(project, run, pool) {
}
/**
- * One review round over every still. Returns the flagged shots that will be
- * regenerated (none once the rounds are spent, or when the reviewer could
- * not see the images).
+ * One review round over every still. Clears the flagged stills (their fixes
+ * ride the next prompt) and returns those shots, which production makes again:
+ * none once the rounds are spent, when the reviewer could not see the images,
+ * or when a lock the run did not give refuses the repair. Null when a stop or
+ * cancel released the run during the review, which then changes nothing.
*/
-async function reviewStills(projectId, run) {
+async function reviewStills(projectId, run, productionRunId) {
const { buildStillsReviewPrompt } = await reviewModule();
const project = await getProject(projectId);
const shots = (project.scenes || []).filter((scene) => scene.referenceImageId);
@@ -1293,14 +1296,23 @@ async function reviewStills(projectId, run) {
if (review.visual && review.verdict === 'revise') issues.push(...review.issues.filter((issue) => known.has(issue.sceneId)));
}
const spent = revisionsSpent(await latestRun(projectId), 'stills');
- const regenerate = spent < reviewLimit(run) ? issues : [];
+ const flagged = spent < reviewLimit(run) ? issues : [];
+ if (!await ownsStillsReview(projectId, productionRunId)) return null;
+ const fixes = new Map();
+ for (const issue of flagged) fixes.set(issue.sceneId, [fixes.get(issue.sceneId), issue.text].filter(Boolean).join(' '));
+ const at = new Date().toISOString();
+ // Clearing the still is what makes production generate it again; the fixes ride its next prompt.
+ const refused = fixes.size > 0 && (await mutateProjectRecord(projectId, (current) => ({ project: { ...current, scenes: (current.scenes || []).map((scene) => (fixes.has(scene.sceneId)
+ ? { ...scene, referenceImageId: null, stillFixes: { text: trimTo(fixes.get(scene.sceneId), STILL_FIXES_MAX), at } } : scene)) } }), { runId: run.id })).rejected === true;
+ const regenerate = refused ? [] : flagged;
const scored = reviews.map((r) => r.score).filter((n) => n != null);
// Approval needs every still seen and nothing flagged; an inconclusive or unresolved round is not one.
const conclusive = shots.length > 0 && shown === shots.length;
const unresolved = issues.length > 0 && !regenerate.length;
const why = !reviews.length ? 'No still could be shown to the reviewer. '
: reviews.some((r) => !r.visual) ? 'The reviewer could not see every still. '
- : unresolved ? 'Revision limit reached with changes still open. ' : '';
+ : refused ? `${nameShots(project, [...fixes.keys()])} locked by an approval this run did not give; unlock to regenerate. `
+ : unresolved ? 'Revision limit reached with changes still open. ' : '';
await recordReview(projectId, { checkpoint: 'stills', verdict: regenerate.length || unresolved || !conclusive ? 'revise' : 'approve',
score: scored.length ? Math.min(...scored) : null, route: reviews.at(-1)?.route || null, visual: reviews.length > 0 && reviews.every((r) => r.visual),
notes: trimTo(`${why}${regenerate.length ? `Regenerating ${regenerate.length} still${regenerate.length === 1 ? '' : 's'}. ` : ''}${reviews.map((r) => r.notes).filter(Boolean).join(' ')}`, 1500),
@@ -1336,15 +1348,9 @@ async function ownsStillsReview(projectId, productionRunId) {
async function reviewOrPark(projectId, run, productionRunId) {
if (isOrchestratedRun(run)) {
- const regenerate = await reviewStills(projectId, run);
- if (!await ownsStillsReview(projectId, productionRunId)) return;
+ const regenerate = await reviewStills(projectId, run, productionRunId);
+ if (!regenerate) return;
if (regenerate.length) {
- const fixes = new Map();
- for (const issue of regenerate) fixes.set(issue.sceneId, [fixes.get(issue.sceneId), issue.text].filter(Boolean).join(' '));
- const at = new Date().toISOString();
- // Clearing the still is what makes production generate it again; the fixes ride its next prompt.
- await mutateProjectRecord(projectId, (current) => ({ project: { ...current, scenes: (current.scenes || []).map((scene) => (fixes.has(scene.sceneId)
- ? { ...scene, referenceImageId: null, stillFixes: { text: trimTo(fixes.get(scene.sceneId), STILL_FIXES_MAX), at } } : scene)) } }));
await deps.resumeProduction(projectId, productionRunId, { acceptBasis: true, stopBefore: 'clips' });
return;
}
@@ -1951,12 +1957,19 @@ async function repairPilot(projectId, run, productionRunId, { pilot, result }) {
if (spent < reviewLimit(run)) {
const at = new Date().toISOString();
const slot = repair.category === 'plate' ? 'stillFixes' : 'clipFixes';
- await mutateProjectRecord(projectId, (current) => ({ project: { ...current, scenes: (current.scenes || []).map((s) => {
+ const { rejected } = await mutateProjectRecord(projectId, (current) => ({ project: { ...current, scenes: (current.scenes || []).map((s) => {
if (s.sceneId !== pilot.sceneId) return s;
// Earlier fixes stay in the prompt so a repair never undoes the last one; the newest win the length cap.
const text = [s[slot]?.text, problems].filter(Boolean).join(' ');
return { ...s, ...(slot === 'stillFixes' ? { referenceImageId: null } : {}), [slot]: { text: text.slice(-(slot === 'stillFixes' ? STILL_FIXES_MAX : CLIP_FIXES_MAX)), at } };
- }) } }));
+ }) } }), { runId: run.id });
+ if (rejected) {
+ // Nothing landed, so production would make the same take again: leave it to the director.
+ await recordReview(projectId, { checkpoint: 'pilot', sceneId: pilot.sceneId, verdict: 'revise',
+ notes: trimTo(`Pilot ${scene.label || pilot.sceneId}: ${repair.category} repair not applied; ${nameShots(project, [pilot.sceneId])} is locked by an approval this run did not give. Unlock it to repair. ${problems}`, 1500),
+ changes: [trimTo(`${pilot.sceneId}: ${problems}`, 300)] });
+ return false;
+ }
await recordReview(projectId, { checkpoint: 'pilot', sceneId: pilot.sceneId, verdict: 'revise',
notes: trimTo(`Pilot ${scene.label || pilot.sceneId}: ${repair.category} repair (${spent + 1} of ${reviewLimit(run)}), generated again with the fix. ${problems}`, 1500),
changes: [trimTo(`${pilot.sceneId}: ${problems}`, 300)] });
diff --git a/server/services/musicVideo/autonomousService.test.js b/server/services/musicVideo/autonomousService.test.js
index 75cebed0b8..3ccc6d1157 100644
--- a/server/services/musicVideo/autonomousService.test.js
+++ b/server/services/musicVideo/autonomousService.test.js
@@ -36,19 +36,29 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
const store = new Map();
let writeTail = Promise.resolve();
const clone = (v) => JSON.parse(JSON.stringify(v));
-vi.mock('./projects.js', () => ({
- getProject: async (id) => (store.has(id) ? clone(store.get(id)) : null),
- mutateProjectRecord: (id, transform) => {
- const run = writeTail.then(() => {
- if (!store.has(id)) throw Object.assign(new Error('Project not found'), { status: 404 });
- const out = transform(clone(store.get(id)));
- store.set(id, clone(out.project));
- return { ...out, project: clone(out.project) };
- });
- writeTail = run.catch(() => {});
- return run;
- },
-}));
+vi.mock('./projects.js', async () => {
+ const { preserveAutomatedShotLocks } = await vi.importActual('./productionReview.js');
+ return {
+ getProject: async (id) => (store.has(id) ? clone(store.get(id)) : null),
+ // Like the real store, an automation write passes the shot-lock guard.
+ mutateProjectRecord: (id, transform, { source = null, runId = null } = {}) => {
+ const run = writeTail.then(() => {
+ if (!store.has(id)) throw Object.assign(new Error('Project not found'), { status: 404 });
+ const current = clone(store.get(id));
+ const out = transform(current);
+ if (source) {
+ const guarded = preserveAutomatedShotLocks(current, out.project, source, { runId });
+ out.project = guarded.project;
+ if (guarded.rejected) out.rejected = true;
+ }
+ store.set(id, clone(out.project));
+ return { ...out, project: clone(out.project) };
+ });
+ writeTail = run.catch(() => {});
+ return run;
+ },
+ };
+});
const { musicVideoEvents } = await import('./events.js');
const service = await import('./autonomousService.js');
@@ -1627,6 +1637,52 @@ describe('orchestrated mode (brief.orchestrator)', () => {
expect(doubles.resumeProduction.mock.calls[0][2].stopBefore).toBeUndefined();
});
+ // Uniquely catches the run's still repairs being discarded by the locks its own storyboard approval set (#12065),
+ // and a repair a director's lock refused being logged as a regeneration.
+ it('lands still repairs on shots its own storyboard approval locked, but parks on a director\'s lock', async () => {
+ const actual = await vi.importActual('./productionReview.js');
+ await service.startAutonomousVideo({ prompt: 'p', tools: ['image:local'], orchestrator: ORCHESTRATOR }, { autoApproveAuthorized: true });
+ await vi.waitFor(() => expect(doubles.startProduction).toHaveBeenCalledOnce());
+ const runId = runOf().id;
+ Object.assign(store.get('mv-auto'), {
+ audioAnalysis: { durationSec: 20, sections: [{ id: 'chorus', label: 'Chorus', startSec: 0, endSec: 20 }] },
+ scenes: [{ sceneId: 'mvs-a', label: 'Intro', startSec: 0, endSec: 10, framePrompt: 'a skater at dusk', referenceImageId: 'a1.png' },
+ { sceneId: 'mvs-b', label: 'Verse', startSec: 10, endSec: 20, framePrompt: 'a boardroom', referenceImageId: 'b1.png' }],
+ devArtifacts: [{ id: 'guide', version: 1, file: 'guide.html', mimeType: 'text/html' }],
+ productionReview: { draft: { cast: 'A skater', environments: 'Skate park', visualLanguage: 'Ink', motionLanguage: 'Glide', guideArtifactId: 'guide',
+ lyricsMode: 'instrumental', timingNotes: '', storyboard: [
+ { sceneId: 'mvs-a', lyricCueIds: [], action: 'Skater rolls in', staging: 'Wide', camera: 'Track', transition: 'Cut' },
+ { sceneId: 'mvs-b', lyricCueIds: [], action: 'Board meets', staging: 'Medium', camera: 'Static', transition: 'Cut' }] } },
+ });
+ for (const stage of ['art', 'storyboard']) {
+ store.set('mv-auto', clone(actual.approveProductionStage(clone(store.get('mv-auto')), { stage, basis: actual.productionReadiness(store.get('mv-auto')).basis[stage],
+ approvedBy: 'orchestrator', reviewer: { kind: 'orchestrator', runId, providerId: 'orch', model: 'judge' } })));
+ }
+ expect(actual.shotLocks(store.get('mv-auto'))).toEqual({ 'mvs-a': 'locked', 'mvs-b': 'locked' });
+
+ answers.stills = [{ verdict: 'revise', score: 5, notes: 'One extra hand.', issues: [{ sceneId: 'mvs-b', text: 'The chair has three hands on it.' }] }];
+ await service.__testing.onProductionEvent({ projectId: 'mv-auto', runId: 'mvpr-1', run: { status: 'awaiting-video' } });
+ expect(store.get('mv-auto').scenes[1]).toMatchObject({ referenceImageId: null, stillFixes: { text: 'The chair has three hands on it.' } });
+ expect(actual.shotLocks(store.get('mv-auto'))).toEqual({ 'mvs-a': 'locked', 'mvs-b': 'locked' });
+ expect(store.get('mv-auto').productionReview.feedback || []).toEqual([]);
+ expect(runOf().orchestration.reviews.at(-1).notes).toContain('Regenerating 1 still');
+ expect(doubles.resumeProduction).toHaveBeenCalledWith('mv-auto', 'mvpr-1', { acceptBasis: true, stopBefore: 'clips' });
+
+ // The director locks the shots themselves: the run's next repair is refused, and it says so instead of regenerating.
+ store.get('mv-auto').scenes[1].referenceImageId = 'b2.png';
+ store.set('mv-auto', clone(actual.setShotLocks(clone(store.get('mv-auto')), { sceneIds: ['mvs-a', 'mvs-b'], locked: true, approvedBy: 'director' })));
+ doubles.resumeProduction.mockClear();
+ answers.stills = [{ verdict: 'revise', score: 5, notes: 'Wrong sky.', issues: [{ sceneId: 'mvs-a', text: 'The sky is noon, not dusk.' }] }];
+ await service.__testing.onProductionEvent({ projectId: 'mv-auto', runId: 'mvpr-1', run: { status: 'awaiting-video' } });
+ expect(store.get('mv-auto').scenes[0].referenceImageId).toBe('a1.png');
+ expect(store.get('mv-auto').scenes[0].stillFixes).toBeUndefined();
+ const last = runOf().orchestration.reviews.at(-1);
+ expect(last).toMatchObject({ checkpoint: 'stills', verdict: 'revise', notes: expect.stringContaining('Shot 1 locked by an approval this run did not give') });
+ expect(last.notes).not.toContain('Regenerating');
+ expect(doubles.resumeProduction).not.toHaveBeenCalled();
+ expect(runOf()).toMatchObject({ status: 'awaiting-approval', awaiting: 'stills' });
+ });
+
// Uniquely catches a 69-shot run parking at the default 40 generations before every shot has a still.
it('grows a default generation limit to the shots left, but parks at a limit the director chose', async () => {
await service.startAutonomousVideo({ prompt: 'p', tools: ['image:local'], orchestrator: ORCHESTRATOR }, { autoApproveAuthorized: true });
@@ -1695,6 +1751,31 @@ describe('orchestrated mode (brief.orchestrator)', () => {
expect(runOf().status).toBe('running');
});
+ // Uniquely catches a pilot repair discarded by the run's own shot lock, or one a director's lock refused logged as made again (#12065).
+ it('lands a pilot repair on a shot the run locked, and parks without regenerating on a director\'s lock', async () => {
+ const actual = await vi.importActual('./productionReview.js');
+ await pilotFixture();
+ const lock = (fields) => store.set('mv-auto', clone(actual.setShotLocks(clone(store.get('mv-auto')), { sceneIds: ['mvs-a'], locked: true, ...fields })));
+ lock({ approvedBy: 'orchestrator', reviewer: { kind: 'orchestrator', runId: runOf().id } });
+ reviewPilot('rev-1', { verdict: 'revise', reason: 'The skater floats above the ramp.' });
+ await blocked();
+ expect(store.get('mv-auto').scenes[0].clipFixes).toMatchObject({ text: 'The skater floats above the ramp.' });
+ expect(actual.shotLocks(store.get('mv-auto'))['mvs-a']).toBe('locked');
+ expect(doubles.resumeProduction).toHaveBeenLastCalledWith('mv-auto', 'mvpr-1', { acceptBasis: true });
+
+ lock({ approvedBy: 'director' });
+ store.get('mv-auto').scenes[0].videoHistoryId = 'clip-2';
+ reviewPilot('rev-2', { verdict: 'revise', reason: 'The landing is soft.' });
+ doubles.resumeProduction.mockClear();
+ await blocked();
+ expect(store.get('mv-auto').scenes[0].clipFixes).toMatchObject({ text: 'The skater floats above the ramp.' });
+ const last = runOf().orchestration.reviews.at(-1);
+ expect(last).toMatchObject({ checkpoint: 'pilot', verdict: 'revise', notes: expect.stringContaining('repair not applied') });
+ expect(last.notes).not.toContain('generated again');
+ expect(doubles.resumeProduction).not.toHaveBeenCalled();
+ expect(runOf()).toMatchObject({ status: 'needs-human', errorCode: 'PRODUCTION_PARKED' });
+ });
+
// Uniquely catches an orchestrated run parking on the proof gate after its pilot passed.
it('approves the passing pilot as the proof from its machine review', async () => {
await pilotFixture();
diff --git a/server/services/musicVideo/productionReview.js b/server/services/musicVideo/productionReview.js
index 56eb125d0d..7acf3f82f1 100644
--- a/server/services/musicVideo/productionReview.js
+++ b/server/services/musicVideo/productionReview.js
@@ -231,8 +231,13 @@ export function assertClipUnlocked(project, sceneId, patch = null) {
throw new ServerError('This shot\'s video is locked. Unlock the video to change it.', { status: 409, code: 'MUSIC_VIDEO_SHOT_LOCKED', context: { sceneId } });
}
-/** Apply an automation proposal under the store lock; locked render inputs stay intact. */
-export function preserveAutomatedShotLocks(before, after, source) {
+/**
+ * Apply an automation proposal under the store lock; locked render inputs stay intact.
+ * `runId` names the autonomous run writing as `source: 'orchestrator'`: on a shot
+ * that run's own approval locked, its repair (clear the still, set
+ * `stillFixes`/`clipFixes`) lands and the lock re-binds to the repaired shot.
+ */
+export function preserveAutomatedShotLocks(before, after, source, { runId = null } = {}) {
const locks = shotLocks(before) || {};
const locked = (before.scenes || []).filter(scene => locks[scene.sceneId] === 'locked');
if (!locked.length) return { project: after, rejected: false };
@@ -251,12 +256,24 @@ export function preserveAutomatedShotLocks(before, after, source) {
const synthetic = { ...scene, referenceImageId: proposed.referenceImageId, endImageId: proposed.endImageId };
return shotApprovalBasis(before, synthetic) === shotApprovalBasis(after, proposed);
};
+ // Repair notes feed generation too, but stay outside the persisted approval
+ // hash so existing shot locks keep their cross-version meaning.
+ const repairBasis = shot => hash({ stillFixes: shot.stillFixes, clipFixes: shot.clipFixes });
+ const lockOf = scene => before.productionReview?.shotApprovals?.[scene.sceneId] || before.productionReview?.approvals?.storyboard;
+ const isAllowedRunRepair = (scene, proposed) => {
+ if (source !== 'orchestrator' || !runId || !proposed || clipLocked(before, scene.sceneId)) return false;
+ if (lockOf(scene)?.reviewer?.runId !== runId) return false;
+ const refBefore = textId(scene.referenceImageId);
+ const refAfter = textId(proposed.referenceImageId);
+ if (refAfter && refAfter !== refBefore) return false;
+ if (refAfter === refBefore && repairBasis(scene) === repairBasis(proposed)) return false;
+ const synthetic = { ...scene, referenceImageId: proposed.referenceImageId };
+ return shotApprovalBasis(before, synthetic) === shotApprovalBasis(after, proposed);
+ };
const rejected = locked.filter(scene => {
const proposed = after.scenes?.find(next => next.sceneId === scene.sceneId);
- // Repair notes feed generation too, but stay outside the persisted approval
- // hash so existing shot locks keep their cross-version meaning.
- const repairBasis = shot => hash({ stillFixes: shot.stillFixes, clipFixes: shot.clipFixes });
if (!proposed) return true;
+ if (isAllowedRunRepair(scene, proposed)) return false;
if (repairBasis(scene) !== repairBasis(proposed)) return true;
if (clipLocked(before, scene.sceneId) && proposed.videoHistoryId !== scene.videoHistoryId) return true;
if (shotApprovalBasis(before, scene) === shotApprovalBasis(after, proposed)) return false;
@@ -264,8 +281,8 @@ export function preserveAutomatedShotLocks(before, after, source) {
});
// Materialize legacy whole-board locks before an open shot's edit can stale
// that approval. No new approval is granted: these shots were already locked.
- // When a generated still fills an empty slot on a locked shot, re-bind that
- // shot's lock to the new basis so it stays locked.
+ // When a generated still fills an empty slot on a locked shot, or the run that
+ // locked it repairs it, re-bind that shot's lock to the new basis so it stays locked.
const shotApprovals = { ...after.productionReview?.shotApprovals };
for (const scene of locked) {
const proposed = after.scenes?.find(next => next.sceneId === scene.sceneId);
@@ -273,6 +290,8 @@ export function preserveAutomatedShotLocks(before, after, source) {
|| { ...before.productionReview?.approvals?.storyboard, fromBoardApproval: true };
if (!rejected.length && proposed && isAllowedStillFill(scene, proposed)) {
shotApprovals[scene.sceneId] = stillLock(after, proposed, priorApproval);
+ } else if (!rejected.length && isAllowedRunRepair(scene, proposed)) {
+ shotApprovals[scene.sceneId] = stillLock(after, proposed, { ...priorApproval, relockedAt: new Date().toISOString() });
} else {
shotApprovals[scene.sceneId] = before.productionReview?.shotApprovals?.[scene.sceneId]
|| stillLock(before, scene, priorApproval);
diff --git a/server/services/musicVideo/projects.js b/server/services/musicVideo/projects.js
index 3848a099fe..a1534a6581 100644
--- a/server/services/musicVideo/projects.js
+++ b/server/services/musicVideo/projects.js
@@ -518,19 +518,21 @@ export async function reviewSceneTake(id, sceneId, takeId, review) {
* write tail / PG row lock). `transform(project)` returns `{ project, ...result }`;
* resolves to that outcome with the persisted project. Used by the treatment
* service (#8980) so its revision checks run against the freshest record.
+ * `options.source` marks an automation write for preserveAutomatedShotLocks;
+ * `options.runId` names the autonomous run behind an `orchestrator` write.
*/
export function mutateProjectRecord(id, transform, options = {}) {
return mutateProjectRecordAdmitted(id, transform, options);
}
-async function mutateProjectRecordAdmitted(id, transform, { source = null } = {}, mutate = null) {
+async function mutateProjectRecordAdmitted(id, transform, { source = null, runId = null } = {}, mutate = null) {
const { preserveAutomatedShotLocks } = source ? await import('./productionReview.js') : {};
let dropped = null;
const write = mutate || (await selectBackend()).mutateProjectRecord;
const outcome = await write(id, async (current) => {
const outcome = transform(current);
if (source) {
- const guarded = preserveAutomatedShotLocks(current, outcome.project, source);
+ const guarded = preserveAutomatedShotLocks(current, outcome.project, source, { runId });
outcome.project = guarded.project;
if (guarded.rejected) {
outcome.rejected = true;