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([pipeline-audio-phase-4c-2-4c-3-4d-2-local-oss]): extract selectPlacedVoLines, fix null-offset coercion

[._.]/ Adam Eivy51e8a8a0bfcd
diff --git a/server/services/creativeDirector/stitchRunner.js b/server/services/creativeDirector/stitchRunner.js
index 3c7fe476b5..fa796de9ed 100644
--- a/server/services/creativeDirector/stitchRunner.js
+++ b/server/services/creativeDirector/stitchRunner.js
@@ -24,7 +24,7 @@ import { addItem as addCollectionItem } from '../mediaCollections.js';
import { buildTimelineClips } from './orchestrator.js';
import { getProject, updateProject } from './local.js';
import { getIssue } from '../pipeline/issues.js';
-import { muxMusicBed, muxVoLines, resolveMusicTrackPath } from '../pipeline/audioMux.js';
+import { muxMusicBed, muxVoLines, resolveMusicTrackPath, selectPlacedVoLines } from '../pipeline/audioMux.js';
import { PATHS } from '../../lib/fileUtils.js';
const FINAL_RENDER_POLL_MS = 3000;
@@ -143,14 +143,10 @@ async function maybeMuxPipelineAudio(project, finalEntry) {
const musicFilename = issue.stages?.audio?.music?.trackFilename;
const musicPath = await resolveMusicTrackPath(musicFilename);
- // Collect placed VO lines — rendered (have an audioFilename) AND positioned
- // (have a numeric offsetSec). Un-placed lines stay silent rather than
- // stacking at t=0. When any exist, the VO pass (which also ducks the music
- // bed under dialogue) supersedes the music-only bed.
- const lines = Array.isArray(issue.stages?.audio?.lines) ? issue.stages.audio.lines : [];
- const voLines = lines
- .filter((l) => l?.audioFilename && Number.isFinite(Number(l?.offsetSec)) && Number(l.offsetSec) >= 0)
- .map((l) => ({ path: join(PATHS.audio, l.audioFilename), offsetSec: Number(l.offsetSec) }));
+ // Placed VO lines — rendered AND positioned (see selectPlacedVoLines). When
+ // any exist, the VO pass (which also ducks the music bed under dialogue)
+ // supersedes the music-only bed.
+ const voLines = selectPlacedVoLines(issue.stages?.audio?.lines);
// Nothing to overlay — no placed VO and no music. Leave the stitch as-is.
if (!voLines.length && !musicPath) return;
diff --git a/server/services/pipeline/audioMux.js b/server/services/pipeline/audioMux.js
index 9814f46b62..d887d5eac6 100644
--- a/server/services/pipeline/audioMux.js
+++ b/server/services/pipeline/audioMux.js
@@ -56,6 +56,24 @@ export async function resolveMusicTrackPath(trackFilename) {
return entry ? join(PATHS.music, entry.filename) : null;
}
+/**
+ * Pure: map an issue's raw `stages.audio.lines` to the VO lines that are
+ * actually muxable — *rendered* (have an `audioFilename`) AND *placed* (have a
+ * finite, >= 0 `offsetSec`). Returns `[{ path, offsetSec }]` with each
+ * filename resolved under PATHS.audio. The single source of truth for "what
+ * counts as a placed VO line", shared by the CD stitch and any future batch
+ * export so the predicate can't drift across call sites.
+ */
+export function selectPlacedVoLines(lines) {
+ if (!Array.isArray(lines)) return [];
+ // `typeof === 'number'` (not Number(x)) so a null offset — the sanitizer's
+ // "not placed yet" sentinel — is excluded rather than coerced to 0 and
+ // stacked at the episode start (Number(null) === 0).
+ return lines
+ .filter((l) => l?.audioFilename && typeof l.offsetSec === 'number' && Number.isFinite(l.offsetSec) && l.offsetSec >= 0)
+ .map((l) => ({ path: join(PATHS.audio, l.audioFilename), offsetSec: l.offsetSec }));
+}
+
/**
* Mix a music bed onto `inputVideoPath`, replacing the file in place.
* Returns `{ ok: true }` on success, `{ ok: false, reason }` on failure.
@@ -184,10 +202,10 @@ export async function muxVoLines(inputVideoPath, { voLines = [], musicPath = nul
if (!inputVideoPath || !existsSync(inputVideoPath)) {
return { ok: false, reason: 'input video missing' };
}
- const placed = (Array.isArray(voLines) ? voLines : []).filter((l) => {
- const off = Number(l?.offsetSec);
- return l?.path && existsSync(l.path) && Number.isFinite(off) && off >= 0;
- });
+ const placed = (Array.isArray(voLines) ? voLines : []).filter((l) => (
+ // `typeof === 'number'` so a null offset isn't coerced to 0 (Number(null) === 0).
+ l?.path && existsSync(l.path) && typeof l.offsetSec === 'number' && Number.isFinite(l.offsetSec) && l.offsetSec >= 0
+ ));
if (!placed.length) return { ok: false, reason: 'no placed VO lines' };
// A missing music file is not fatal — drop to a VO-only mux rather than
// failing, so a stale `music.trackFilename` pointer can't suppress dialogue.
diff --git a/server/services/pipeline/audioMux.test.js b/server/services/pipeline/audioMux.test.js
index 1ea6c3da6b..21676feb53 100644
--- a/server/services/pipeline/audioMux.test.js
+++ b/server/services/pipeline/audioMux.test.js
@@ -60,7 +60,7 @@ vi.mock('child_process', async () => {
};
});
-const { muxMusicBed, muxVoLines, buildVoMuxArgs, resolveMusicTrackPath, DEFAULT_MUSIC_GAIN } = await import('./audioMux.js');
+const { muxMusicBed, muxVoLines, buildVoMuxArgs, selectPlacedVoLines, resolveMusicTrackPath, DEFAULT_MUSIC_GAIN } = await import('./audioMux.js');
beforeEach(async () => {
spawnCalls.length = 0;
@@ -180,6 +180,27 @@ describe('muxMusicBed', () => {
});
});
+describe('selectPlacedVoLines', () => {
+ it('keeps only rendered + placed lines and resolves paths under PATHS.audio', () => {
+ const out = selectPlacedVoLines([
+ { audioFilename: 'a.wav', offsetSec: 2 }, // rendered + placed ✓
+ { audioFilename: 'b.wav', offsetSec: null }, // rendered, not placed ✗
+ { audioFilename: null, offsetSec: 5 }, // placed, not rendered ✗
+ { audioFilename: 'c.wav', offsetSec: -1 }, // negative offset ✗
+ { audioFilename: 'd.wav', offsetSec: 0 }, // offset 0 is valid ✓
+ ]);
+ expect(out).toEqual([
+ { path: expect.stringMatching(/a\.wav$/), offsetSec: 2 },
+ { path: expect.stringMatching(/d\.wav$/), offsetSec: 0 },
+ ]);
+ });
+ it('returns [] for non-array / empty input', () => {
+ expect(selectPlacedVoLines(null)).toEqual([]);
+ expect(selectPlacedVoLines(undefined)).toEqual([]);
+ expect(selectPlacedVoLines([])).toEqual([]);
+ });
+});
+
describe('buildVoMuxArgs', () => {
it('delays each VO line to its offset and pads to video length (no music)', () => {
const args = buildVoMuxArgs({