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: cancelling an auto-review run closes its revision in the same write; a song can't be its own sound bed (#8988)

[._.]/ Adam Eivy9bf850ff4f8f
diff --git a/server/routes/musicVideoAutoReview.test.js b/server/routes/musicVideoAutoReview.test.js
index b01fd9e0df..c107c6c813 100644
--- a/server/routes/musicVideoAutoReview.test.js
+++ b/server/routes/musicVideoAutoReview.test.js
@@ -283,6 +283,22 @@ describe('opt-in automatic review/retries (#8988)', () => {
expect(run(await projects.getProject(p.id)).usage.generations).toBe(1);
});
+ it('cancelling a run closes its open revision in the same write — nothing more is charged or allowed', async () => {
+ const p = await project();
+ h.verdicts.push(FAIL_S2);
+ await start(p.id, { maxAttempts: 2, maxGenerations: 2 });
+ await finishDraft(p.id, 1);
+ const before = await settled(p.id, (x) => expect(run(x).attempts[0].revisionId).toBeTruthy());
+ const revisionId = run(before).attempts[0].revisionId;
+
+ const r = await request(app).post(`${base(p.id)}/auto-reviews/${run(before).id}/cancel`);
+ expect(r.status).toBe(200);
+ const after = await projects.getProject(p.id);
+ expect(run(after).status).toBe('canceled');
+ expect(after.revisions.find((rv) => rv.id === revisionId).status).toBe('canceled');
+ await expect(assertRevisionOpen(p.id, revisionId, { sceneId: 's2', kind: 'video' })).rejects.toMatchObject({ code: 'REVISION_CLOSED' });
+ });
+
it('stops before handing out a revision the remaining spend cannot cover', async () => {
const p = await project();
h.verdicts.push(FAIL_S2);
diff --git a/server/services/musicVideo/autoReview.js b/server/services/musicVideo/autoReview.js
index 320b127ad9..3ac8d56c62 100644
--- a/server/services/musicVideo/autoReview.js
+++ b/server/services/musicVideo/autoReview.js
@@ -229,7 +229,10 @@ export function beginNextAttempt(project, runId, excerptId, now = new Date().toI
/**
* Spend one review from the attempt budget BEFORE the provider call. Throws
* 409 when the budget is spent, the run is no longer running, or the attempt
- * already holds a review — so an overlapping advance can never pay twice.
+ * already holds a review — so an overlapping advance can never pay twice
+ * (advances of one run are also coalesced in-process). A review interrupted
+ * by a restart left `reviewStartedAt` with no review: the director's next
+ * Continue/Resume reviews that draft again, and that call counts too.
*/
export function beginAttemptReview(project, runId, now = new Date().toISOString()) {
return mutateRun(project, runId, (run) => {
@@ -245,7 +248,9 @@ export function beginAttemptReview(project, runId, now = new Date().toISOString(
const cleanFinding = (f, spanSec) => {
if (!f || typeof f !== 'object' || !isNonBlankStr(f.note)) return null;
- const atSec = typeof f.atSec === 'number' && Number.isFinite(f.atSec) ? Math.min(Math.max(0, f.atSec), spanSec) : null;
+ // Clamped INSIDE the half-open excerpt, so a finding at (or past) its end
+ // still lands in the last section rather than in none.
+ const atSec = typeof f.atSec === 'number' && Number.isFinite(f.atSec) ? Math.min(Math.max(0, f.atSec), Math.max(0, spanSec - 0.001)) : null;
if (atSec === null) return null;
return {
atSec: Math.round(atSec * 1000) / 1000,
diff --git a/server/services/musicVideo/autoReviewService.js b/server/services/musicVideo/autoReviewService.js
index 534d6b2cd7..056e268080 100644
--- a/server/services/musicVideo/autoReviewService.js
+++ b/server/services/musicVideo/autoReviewService.js
@@ -342,14 +342,22 @@ export async function stopAutoReview(projectId, runId) {
return out;
}
-/** Cancel a run, and the revision it has open (which stops its generation jobs). */
+/**
+ * Cancel a run, and the revision it has open (which stops its generation
+ * jobs) — both in one write, so the revision is never left open without its
+ * run guarding the spend.
+ */
export async function cancelAutoReview(projectId, runId) {
- const { revisionId } = await mutateProjectRecord(projectId, (current) => cancelAutoReviewOnProject(current, runId));
- if (revisionId) {
- await cancelRevision(projectId, revisionId).catch((err) => {
- console.error(`❌ Music Video auto-review ${short(runId)} could not cancel its revision: ${err.message}`);
- });
- }
+ const before = await requireProject(projectId);
+ const { revisionId } = cancelAutoReviewOnProject(before, runId); // validates; the real write is below
+ const cancelRun = (p) => cancelAutoReviewOnProject(p, runId).project;
+ const withRevision = revisionId
+ ? await cancelRevision(projectId, revisionId, { alsoOnProject: cancelRun }).then(() => true, (err) => {
+ if (err?.code !== 'REVISION_CLOSED') throw err;
+ return false; // the revision closed meanwhile — cancel the run alone
+ })
+ : false;
+ if (!withRevision) await mutateProjectRecord(projectId, (current) => ({ project: cancelRun(current) }));
const project = await requireProject(projectId);
const run = findRun(project, runId);
publish(projectId, project, run, { type: 'idle' });
diff --git a/server/services/musicVideo/projectsLogic.js b/server/services/musicVideo/projectsLogic.js
index 161a680ce7..d949f2f227 100644
--- a/server/services/musicVideo/projectsLogic.js
+++ b/server/services/musicVideo/projectsLogic.js
@@ -245,6 +245,11 @@ export function applyProjectPatch(project, patch) {
// #8988 — an explicitly chosen sound-design bed; null clears it.
...('soundBed' in patch ? { soundBed: normalizeSoundBed(patch.soundBed) } : {}),
};
+ // #8988: a bed is mixed UNDER the song — the song itself can't be its bed.
+ const masterTrackId = 'trackId' in patch ? patch.trackId : project.trackId;
+ if (timedPatch.soundBed && masterTrackId && timedPatch.soundBed.trackId === masterTrackId) {
+ throw new ServerError('The sound-design bed must be a different track from the song', { status: 422, code: 'SOUND_BED_IS_MASTER' });
+ }
const conceptMergedPatch = ('concept' in timedPatch && timedPatch.concept && project.concept)
? { ...timedPatch, concept: { ...project.concept, ...timedPatch.concept } }
: timedPatch;
diff --git a/server/services/musicVideo/projectsLogic.test.js b/server/services/musicVideo/projectsLogic.test.js
index c77155c896..ca361bf8f3 100644
--- a/server/services/musicVideo/projectsLogic.test.js
+++ b/server/services/musicVideo/projectsLogic.test.js
@@ -142,6 +142,14 @@ describe('cloneProjectRecord', () => {
});
describe('applyProjectPatch', () => {
+ it('stores an explicit sound-design bed (level clamped), clears it with null, and never lets the song be its own bed (#8988)', () => {
+ const withSong = applyProjectPatch(baseProject(), { trackId: 'trk-song' });
+ const bedded = applyProjectPatch(withSong, { soundBed: { trackId: 'trk-rain', volume: 0.333 } });
+ expect(bedded.soundBed).toEqual({ trackId: 'trk-rain', volume: 0.33 });
+ expect(applyProjectPatch(bedded, { soundBed: null }).soundBed).toBeNull();
+ expect(() => applyProjectPatch(withSong, { soundBed: { trackId: 'trk-song' } })).toThrow(expect.objectContaining({ code: 'SOUND_BED_IS_MASTER' }));
+ });
+
it('merges fields and bumps updatedAt', () => {
const next = applyProjectPatch(baseProject(), { name: 'Renamed', trackId: 't2' });
expect(next.name).toBe('Renamed');
diff --git a/server/services/musicVideo/render.js b/server/services/musicVideo/render.js
index 09517733ea..93e14ba1a9 100644
--- a/server/services/musicVideo/render.js
+++ b/server/services/musicVideo/render.js
@@ -166,7 +166,8 @@ export async function resolveMasterAudioPath(project) {
// song is the sole audio master (the default).
async function resolveSoundBed(project) {
const bed = projectSoundBed(project);
- if (!bed) return null;
+ // A (synced) record naming the song itself as its bed mixes nothing.
+ if (!bed || bed.trackId === project.trackId) return null;
const track = await getTrack(bed.trackId);
const safe = track?.audioFilename ? safeUnder(PATHS.music, track.audioFilename) : null;
if (!safe || !existsSync(safe)) {
diff --git a/server/services/musicVideo/revisionService.js b/server/services/musicVideo/revisionService.js
index 07902e0b3b..8236083c20 100644
--- a/server/services/musicVideo/revisionService.js
+++ b/server/services/musicVideo/revisionService.js
@@ -115,9 +115,15 @@ export async function resumeRevision(projectId, revisionId) {
* Cancel a revision. Its draft render and any generation job it started that
* is still queued/running are cancelled too, so a closed revision incurs no
* further paid work. Returns `{ project, revision, canceledJobIds }`.
+ * `alsoOnProject` (#8988) applies a further transform in the SAME write — an
+ * auto-review run cancels itself with its revision, so no window exists in
+ * which the revision is open but its run no longer guards its spend.
*/
-export async function cancelRevision(projectId, revisionId) {
- const { project, revision, renderExcerptId } = await mutateProjectRecord(projectId, (current) => cancelRevisionOnProject(current, revisionId));
+export async function cancelRevision(projectId, revisionId, { alsoOnProject = null } = {}) {
+ const { project, revision, renderExcerptId } = await mutateProjectRecord(projectId, (current) => {
+ const out = cancelRevisionOnProject(current, revisionId);
+ return alsoOnProject ? { ...out, project: alsoOnProject(out.project) } : out;
+ });
if (renderExcerptId) cancelExcerptRender(renderExcerptId);
const { listJobs, cancelJob } = await import('../mediaJobQueue/index.js');
const live = revisionGenerationJobs(project, revision, [...listJobs({ kind: 'video' }), ...listJobs({ kind: 'image' })]);