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

address review (codex): backing window must end before the voice enters (#2121)

Adam Eivy4296b9926e59
diff --git a/client/src/components/songs/ReferenceAnalysis.jsx b/client/src/components/songs/ReferenceAnalysis.jsx
index 766758de98..25d6081c47 100644
--- a/client/src/components/songs/ReferenceAnalysis.jsx
+++ b/client/src/components/songs/ReferenceAnalysis.jsx
@@ -364,6 +364,13 @@ export default function ReferenceAnalysis({
const seg = segments[idx];
const bgStartMs = field === 'bgStartMs' ? ms : seg.bgStartMs;
const bgEndMs = field === 'bgEndMs' ? ms : seg.bgEndMs;
+ // The backing window must end at or before the voice enters (seg.startMs) —
+ // a window overlapping the segment already contains the new voice, so
+ // subtracting it would cancel the part we're trying to recover.
+ if (bgEndMs > seg.startMs) {
+ toast.error('The backing reference must end before the voice enters (before this segment’s start).');
+ return;
+ }
if (bgEndMs - bgStartMs < MIN_BACKING_MS) {
toast.error(`The backing reference must be at least ${(MIN_BACKING_MS / 1000).toFixed(1)}s and end before the voice enters.`);
return;
@@ -408,9 +415,14 @@ export default function ReferenceAnalysis({
const extractSegment = useCallback((seg) => {
if (!decoded) return;
const stacked = isStacked(seg);
- // Defense-in-depth against a too-short backing window (e.g. a hand-edited or
- // peer-synced segment the server accepted but that can't hold an FFT frame):
- // don't let stacked extraction silently degrade to solo-on-the-mix.
+ // Guard the backing-window invariants before running the DSP (covers a
+ // too-short window and one that overlaps the segment because the segment
+ // start was later moved earlier) — either would make stacked extraction
+ // subtract the wrong audio and silently propose a wrong part.
+ if (stacked && seg.bgEndMs > seg.startMs) {
+ toast.error('The backing reference overlaps this segment — set it to end before the voice enters, then extract.');
+ return;
+ }
if (stacked && (seg.bgEndMs - seg.bgStartMs) < MIN_BACKING_MS) {
toast.error(`The backing reference is too short — give it at least ${(MIN_BACKING_MS / 1000).toFixed(1)}s of the layers before the voice enters.`);
return;
diff --git a/client/src/components/songs/ReferenceAnalysis.test.jsx b/client/src/components/songs/ReferenceAnalysis.test.jsx
index 0e644d2359..c89cb6f799 100644
--- a/client/src/components/songs/ReferenceAnalysis.test.jsx
+++ b/client/src/components/songs/ReferenceAnalysis.test.jsx
@@ -323,6 +323,30 @@ describe('ReferenceAnalysis — stacked-mix extraction (#2121)', () => {
expect(screen.getByLabelText(/backing reference end/i)).toBeTruthy();
});
+ it('rejects a backing window that overlaps the segment (must end before the voice enters)', async () => {
+ installFakeAudio();
+ render(
+ <ControlledAnalysis
+ initialRef={{ ...baseRef, segments: [{ layerId: 'bass', startMs: 3000, endMs: 6000 }] }}
+ layers={layers}
+ scoreParts={[]}
+ tempo={60}
+ songKey="C"
+ onApplyPart={vi.fn()}
+ onClose={vi.fn()}
+ />,
+ );
+ fireEvent.click(screen.getByRole('button', { name: /stacked/i }));
+ expect(await screen.findByRole('button', { name: /extract from mix/i })).toBeTruthy();
+ // Push backing end to 4s — past the segment start (3s), so it overlaps the
+ // voice. Rejected with a toast; stacked mode stays intact.
+ const bgEnd = screen.getByLabelText(/backing reference end/i);
+ fireEvent.change(bgEnd, { target: { value: '4' } });
+ fireEvent.blur(bgEnd);
+ expect(toastError).toHaveBeenCalledWith(expect.stringMatching(/must end before the voice enters/i));
+ expect(screen.getByRole('button', { name: /extract from mix/i })).toBeTruthy();
+ });
+
it('recovers a new voice from a stacked mix end-to-end (toggle → extract from mix)', async () => {
// [0, 0.8s]: C4 backing alone. [0.8, 1.8s]: C4 + a new A4 (440) on top.
const audio = concatArr(harmonicTone(262, 0.8), mixArr(harmonicTone(262, 1.0), harmonicTone(440, 1.0)));
diff --git a/server/services/rounds.js b/server/services/rounds.js
index 2f875f9085..d7b4c65bed 100644
--- a/server/services/rounds.js
+++ b/server/services/rounds.js
@@ -258,8 +258,11 @@ const sanitizeRecording = (r) => {
// Optional stacked-mix backing window (#2121): `bgStartMs`/`bgEndMs` mark a
// slice just BEFORE the voice enters (the earlier layers alone) so the client
// can spectral-subtract the backing and extract a voice that never appears
-// solo. Purely additive — the pair only persists together when it forms a
-// valid positive-length range, so a legacy solo segment round-trips unchanged.
+// solo. Purely additive — the pair only persists when it forms a valid range
+// that ENDS AT OR BEFORE the segment start (`bgEndMs <= startMs`); a window
+// overlapping the segment would already contain the new voice, so subtracting
+// it would cancel the very part being extracted. A legacy solo segment (no bg
+// fields) round-trips unchanged.
const sanitizeRefSegment = (s) => {
if (!s || typeof s !== 'object') return null;
const startRaw = finiteOrNull(s.startMs);
@@ -278,7 +281,7 @@ const sanitizeRefSegment = (s) => {
if (bgStartRaw !== null && bgEndRaw !== null) {
const bgStartMs = Math.max(0, Math.round(bgStartRaw));
const bgEndMs = Math.max(0, Math.round(bgEndRaw));
- if (bgEndMs > bgStartMs) {
+ if (bgEndMs > bgStartMs && bgEndMs <= startMs) {
seg.bgStartMs = bgStartMs;
seg.bgEndMs = bgEndMs;
}
diff --git a/server/services/rounds.test.js b/server/services/rounds.test.js
index 6bffcca457..78e13e13a4 100644
--- a/server/services/rounds.test.js
+++ b/server/services/rounds.test.js
@@ -398,9 +398,10 @@ describe('rounds service', () => {
url: 'https://example.com',
audioFilename: 'ref.wav',
segments: [
- { layerId: 'lead', startMs: 8000, endMs: 16000, bgStartMs: 4000.6, bgEndMs: 8000.2 }, // valid, rounded
+ { layerId: 'lead', startMs: 8000, endMs: 16000, bgStartMs: 4000.6, bgEndMs: 8000.2 }, // valid (bgEnd rounds to startMs)
{ layerId: 'bass', startMs: 20000, endMs: 24000, bgStartMs: 9000, bgEndMs: 9000 }, // zero-length bg → dropped
{ layerId: 'alto', startMs: 30000, endMs: 34000, bgStartMs: 'x', bgEndMs: 29000 }, // non-finite bg → dropped
+ { layerId: 'tenor', startMs: 40000, endMs: 44000, bgStartMs: 38000, bgEndMs: 41000 }, // bgEnd > startMs (overlaps) → dropped
],
}],
});
@@ -408,6 +409,7 @@ describe('rounds service', () => {
{ layerId: 'lead', startMs: 8000, endMs: 16000, bgStartMs: 4001, bgEndMs: 8000 },
{ layerId: 'bass', startMs: 20000, endMs: 24000 },
{ layerId: 'alto', startMs: 30000, endMs: 34000 },
+ { layerId: 'tenor', startMs: 40000, endMs: 44000 },
]);
});