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

refactor([issue-3168]): merge concept patches server-side, buffer inputs via useFieldDraft

[._.]/ Adam Eivy453ffe344c6b
diff --git a/client/src/pages/MusicVideo.jsx b/client/src/pages/MusicVideo.jsx
index a08fb18759..aa52558f28 100644
--- a/client/src/pages/MusicVideo.jsx
+++ b/client/src/pages/MusicVideo.jsx
@@ -22,6 +22,7 @@ import {
cancelMusicVideoMidiTranscription,
} from '../services/apiMusicVideo.js';
import useMidiTranscription from '../hooks/useMidiTranscription.js';
+import useFieldDraft from '../hooks/useFieldDraft.js';
import MidiInstallModal from '../components/install/MidiInstallModal.jsx';
import MidiGatedModal from '../components/install/MidiGatedModal.jsx';
import MidiVisualization from '../components/songs/MidiVisualization.jsx';
@@ -462,16 +463,21 @@ export default function MusicVideo() {
.catch((err) => toast.error(err?.message || 'Failed to save scene'));
};
- // Project-level concept/style (issue #3168) — same optimistic-local + silent-PATCH
- // blur pattern as scene fields. Consumed by buildScenePlanPrompt (AI Plan) and by
- // buildFramePrompt/buildShotPrompt's style suffix, both already reading concept.
- const editConceptLocal = (patch) => {
+ // Project-level concept/style (issue #3168) — optimistic-local + silent-PATCH on
+ // commit, same as commitSceneTiming below. Sends only the changed sub-field;
+ // the server merges it into the existing concept (applyProjectPatch), so a
+ // stale local copy can't clobber a sibling sub-field. Consumed by
+ // buildScenePlanPrompt (AI Plan) and by buildFramePrompt/buildShotPrompt's
+ // style suffix, both already reading concept.
+ const commitConcept = (patch) => {
replaceProject({ ...selected, concept: { ...selected.concept, ...patch } });
- };
- const saveConcept = (patch) => {
- updateMusicVideoProject(selected.id, { concept: { ...selected.concept, ...patch } }, { silent: true })
+ updateMusicVideoProject(selected.id, { concept: patch }, { silent: true })
.catch((err) => toast.error(err?.message || 'Failed to save concept'));
};
+ // Buffered so a concept/style keystroke doesn't fire a round-trip per character,
+ // and a focus-without-edit blur doesn't re-PATCH an unchanged value.
+ const conceptDraft = useFieldDraft(selected?.concept?.prompt, (v) => commitConcept({ prompt: v }));
+ const styleDraft = useFieldDraft(selected?.concept?.style, (v) => commitConcept({ style: v }));
// BeatTimeline drag commit — same optimistic-local + silent-PATCH pattern as
// the other scene field editors (#1854).
const commitSceneTiming = (sceneId, patch) => {
@@ -717,19 +723,17 @@ export default function MusicVideo() {
</div>
</div>
{/* Concept & style — optional global direction for the whole video,
- set before "AI Plan" so buildScenePlanPrompt (server) can build on
- it, and reused by buildFramePrompt/buildShotPrompt's style suffix
- (issue #3168). Local-edit + blur-save, same pattern as scene fields. */}
+ set before "AI Plan" (see commitConcept above for what reads it). */}
<div className="mt-2 grid grid-cols-1 sm:grid-cols-2 gap-2">
<div>
<label htmlFor="mv-concept" className="block text-xs text-port-text-muted mb-1">Concept</label>
<textarea
id="mv-concept"
- value={selected.concept?.prompt || ''}
+ value={conceptDraft.value}
rows={2}
maxLength={8000}
- onChange={(e) => editConceptLocal({ prompt: e.target.value })}
- onBlur={(e) => saveConcept({ prompt: e.target.value })}
+ onChange={conceptDraft.onChange}
+ onBlur={conceptDraft.onBlur}
placeholder="What is this video about — story, theme, or narrative thread for the AI plan to build on."
className="w-full bg-port-bg border border-port-border rounded px-2 py-1.5 text-sm"
/>
@@ -738,11 +742,11 @@ export default function MusicVideo() {
<label htmlFor="mv-style" className="block text-xs text-port-text-muted mb-1">Visual style</label>
<textarea
id="mv-style"
- value={selected.concept?.style || ''}
+ value={styleDraft.value}
rows={2}
maxLength={2000}
- onChange={(e) => editConceptLocal({ style: e.target.value })}
- onBlur={(e) => saveConcept({ style: e.target.value })}
+ onChange={styleDraft.onChange}
+ onBlur={styleDraft.onBlur}
placeholder="Art style, references, palette, mood — appended to every generated frame and shot prompt."
className="w-full bg-port-bg border border-port-border rounded px-2 py-1.5 text-sm"
/>
diff --git a/client/src/pages/MusicVideo.test.jsx b/client/src/pages/MusicVideo.test.jsx
index b2d65a261b..67e79bdcfa 100644
--- a/client/src/pages/MusicVideo.test.jsx
+++ b/client/src/pages/MusicVideo.test.jsx
@@ -262,7 +262,7 @@ describe('MusicVideo concept & style editor (#3168)', () => {
fireEvent.blur(conceptField);
await waitFor(() => expect(updateMusicVideoProject).toHaveBeenCalledWith(
'mv-2',
- { concept: { prompt: 'A heist across a dying star', style: 'Cyberpunk anime' } },
+ { concept: { prompt: 'A heist across a dying star' } },
{ silent: true },
));
@@ -270,11 +270,21 @@ describe('MusicVideo concept & style editor (#3168)', () => {
fireEvent.blur(styleField);
await waitFor(() => expect(updateMusicVideoProject).toHaveBeenCalledWith(
'mv-2',
- { concept: { prompt: 'A heist across a dying star', style: 'Watercolor noir' } },
+ { concept: { style: 'Watercolor noir' } },
{ silent: true },
));
});
+ it('does not re-PATCH when a field is focused and blurred without an edit', async () => {
+ const withConcept = { ...PROJECT_NO_CLIP, concept: { prompt: 'Unchanged', style: 'Unchanged' } };
+ await openProject(withConcept);
+ const conceptField = await screen.findByLabelText('Concept');
+ fireEvent.focus(conceptField);
+ fireEvent.blur(conceptField);
+ await settle();
+ expect(updateMusicVideoProject).not.toHaveBeenCalled();
+ });
+
it('starts empty and enables AI Plan to use them once set', async () => {
await openProject(PROJECT_ANALYZED);
const conceptField = await screen.findByLabelText('Concept');
diff --git a/server/services/musicVideo/projectsLogic.js b/server/services/musicVideo/projectsLogic.js
index bb34e2e9b9..a4f2e555c5 100644
--- a/server/services/musicVideo/projectsLogic.js
+++ b/server/services/musicVideo/projectsLogic.js
@@ -87,6 +87,14 @@ export function applyProjectPatch(project, patch) {
if (patch.status && !MUSIC_VIDEO_STATUSES.includes(patch.status)) {
throw new ServerError(`Invalid status: ${patch.status}`, { status: 400, code: 'VALIDATION_ERROR' });
}
+ // A `concept` patch merges into the existing concept sub-fields rather than
+ // replacing the whole object — mirrors applySceneUpdate's per-field scene merge
+ // below, so a caller sending only the sub-field it edited (e.g. { style: '…' })
+ // can't clobber a sibling sub-field (e.g. prompt) set concurrently by another
+ // sync peer (#3168). An explicit `concept: null` still clears it outright.
+ const mergedPatch = ('concept' in patch && patch.concept && project.concept)
+ ? { ...patch, concept: { ...project.concept, ...patch.concept } }
+ : patch;
// Changing the audio source invalidates the cached beat/tempo analysis —
// it was computed from the OLD track. AI Plan / Auto-arrange / BeatTimeline
// gate on `audioAnalysis` truthiness, and the render's beat-snap step reads
@@ -100,7 +108,7 @@ export function applyProjectPatch(project, patch) {
// the flag set would render the old song's cut points against new audio.
const trackChanged = ('trackId' in patch && patch.trackId !== project.trackId)
|| ('uploadedAudioFilename' in patch && patch.uploadedAudioFilename !== project.uploadedAudioFilename);
- if (!trackChanged) return touch(project, patch);
+ if (!trackChanged) return touch(project, mergedPatch);
const scenes = (project.scenes || []).map((s) => (s.beatAligned ? { ...s, beatAligned: false } : s));
// Clearing audioAnalysis means the project must be re-analyzed before it can be
// planned/arranged/rendered, so a status that implies analysis existed
@@ -112,7 +120,7 @@ export function applyProjectPatch(project, patch) {
const statusPatch = regressStatus ? { status: 'draft' } : {};
// The MIDI transcription was produced from the OLD audio too — clear it with
// the analysis so a stale .mid can't masquerade as the new track's score.
- return touch(project, { ...patch, ...statusPatch, audioAnalysis: null, midiTranscription: null, scenes });
+ return touch(project, { ...mergedPatch, ...statusPatch, audioAnalysis: null, midiTranscription: null, scenes });
}
/**
diff --git a/server/services/musicVideo/projectsLogic.test.js b/server/services/musicVideo/projectsLogic.test.js
index a4f39e5744..0f45ebd30c 100644
--- a/server/services/musicVideo/projectsLogic.test.js
+++ b/server/services/musicVideo/projectsLogic.test.js
@@ -142,6 +142,23 @@ describe('applyProjectPatch', () => {
const next = applyProjectPatch(withScenes, { name: 'Renamed' });
expect(next.scenes[0].beatAligned).toBe(true);
});
+
+ it('merges a concept patch into the existing concept instead of replacing it (#3168)', () => {
+ const withConcept = { ...baseProject(), concept: { prompt: 'A road trip', style: 'Cyberpunk anime' } };
+ const next = applyProjectPatch(withConcept, { concept: { style: 'Watercolor noir' } });
+ expect(next.concept).toEqual({ prompt: 'A road trip', style: 'Watercolor noir' });
+ });
+
+ it('sets a concept sub-field on a project with no existing concept', () => {
+ const next = applyProjectPatch(baseProject(), { concept: { prompt: 'Underwater festival' } });
+ expect(next.concept).toEqual({ prompt: 'Underwater festival' });
+ });
+
+ it('clears the concept outright when the patch sets it to null', () => {
+ const withConcept = { ...baseProject(), concept: { prompt: 'A road trip', style: 'Cyberpunk anime' } };
+ const next = applyProjectPatch(withConcept, { concept: null });
+ expect(next.concept).toBeNull();
+ });
});
describe('setAudioAnalysis', () => {