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): clear stale beat-aligned scene timing, bound yt-dlp resource use

[._.]/ Adam Eivybe329e923896
diff --git a/server/services/musicVideo/projectsLogic.js b/server/services/musicVideo/projectsLogic.js
index 9530fcf642..164a9ec7a2 100644
--- a/server/services/musicVideo/projectsLogic.js
+++ b/server/services/musicVideo/projectsLogic.js
@@ -90,10 +90,17 @@ export function applyProjectPatch(project, patch) {
// gate on `audioAnalysis` truthiness, and the render's beat-snap step reads
// its `beats` array; a stale analysis would silently apply the previous
// song's beat grid to the new audio (#1945 — both the manual "Change
- // track" picker and the YouTube-import attach PATCH through here).
+ // track" picker and the YouTube-import attach PATCH through here). Any
+ // scene already marked `beatAligned` also stops being true — its saved
+ // startSec/endSec were snapped to the OLD song's beat positions, and
+ // `beatSnapClips` honors a beat-aligned scene's saved bounds outright
+ // (skipping re-derivation against the new, absent beat grid), so leaving
+ // 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);
- return touch(project, trackChanged ? { ...patch, audioAnalysis: null } : patch);
+ if (!trackChanged) return touch(project, patch);
+ const scenes = (project.scenes || []).map((s) => (s.beatAligned ? { ...s, beatAligned: false } : s));
+ return touch(project, { ...patch, audioAnalysis: null, scenes });
}
/**
diff --git a/server/services/musicVideo/projectsLogic.test.js b/server/services/musicVideo/projectsLogic.test.js
index 6459c156f2..0bd14aa311 100644
--- a/server/services/musicVideo/projectsLogic.test.js
+++ b/server/services/musicVideo/projectsLogic.test.js
@@ -83,6 +83,30 @@ describe('applyProjectPatch', () => {
const next = applyProjectPatch(analyzed, { uploadedAudioFilename: 'b.mp3' });
expect(next.audioAnalysis).toBeNull();
});
+
+ it('clears beatAligned on scenes when the track changes — their bounds were snapped to the OLD beat grid', () => {
+ const withScenes = {
+ ...baseProject(),
+ trackId: 't1',
+ scenes: [
+ { sceneId: 's1', order: 0, startSec: 1, endSec: 2, beatAligned: true },
+ { sceneId: 's2', order: 1, startSec: 3, endSec: 4, beatAligned: false },
+ ],
+ };
+ const next = applyProjectPatch(withScenes, { trackId: 't2' });
+ expect(next.scenes[0]).toMatchObject({ beatAligned: false, startSec: 1, endSec: 2 });
+ expect(next.scenes[1]).toMatchObject({ beatAligned: false, startSec: 3, endSec: 4 });
+ });
+
+ it('leaves scene beatAligned flags untouched when the track does not change', () => {
+ const withScenes = {
+ ...baseProject(),
+ trackId: 't1',
+ scenes: [{ sceneId: 's1', order: 0, startSec: 1, endSec: 2, beatAligned: true }],
+ };
+ const next = applyProjectPatch(withScenes, { name: 'Renamed' });
+ expect(next.scenes[0].beatAligned).toBe(true);
+ });
});
describe('setAudioAnalysis', () => {
diff --git a/server/services/trackYoutubeImport.js b/server/services/trackYoutubeImport.js
index 2680f92eec..ffc6b84b5c 100644
--- a/server/services/trackYoutubeImport.js
+++ b/server/services/trackYoutubeImport.js
@@ -24,8 +24,8 @@ import { findFfmpeg, probeVideoDuration } from '../lib/ffmpeg.js';
import { findYtDlp } from '../lib/ytdlp.js';
import { broadcastSse, attachSseClient as attachSse, closeJobAfterDelay } from '../lib/sseUtils.js';
import { safeChildProcessEnv } from '../lib/processEnv.js';
-import { importUploadedTrack } from './pipeline/musicLibrary.js';
-import { createTrack } from './tracks/index.js';
+import { importUploadedTrack, MUSIC_UPLOAD_MAX_BYTES } from './pipeline/musicLibrary.js';
+import { createTrack, DURATION_MAX_SEC } from './tracks/index.js';
// youtube.com/watch, youtu.be, and m.youtube.com only (issue #1945 scope:
// "start narrow" — other video hosts are explicitly out of scope). This also
@@ -131,6 +131,12 @@ export async function startYoutubeImport(url) {
// title in one invocation.
'--no-simulate',
'--progress',
+ // Bound resource use to the same limits the manual upload path already
+ // enforces (MUSIC_UPLOAD_MAX_BYTES, DURATION_MAX_SEC) — without these,
+ // a long video or a livestream/archive URL downloads and transcodes
+ // unbounded, eating disk in tmpdir()/data/music and CPU with no cap.
+ '--max-filesize', String(MUSIC_UPLOAD_MAX_BYTES),
+ '--match-filters', `duration <= ${DURATION_MAX_SEC}`,
'-o', `${tempBase}.%(ext)s`,
url,
];
@@ -179,7 +185,15 @@ export async function startYoutubeImport(url) {
return;
}
if (exit.code !== 0 || !existsSync(outPath)) {
- throw new Error(exit.reason || `yt-dlp exited ${exit.code}`);
+ // A --match-filters/--max-filesize rejection exits 0 with no output
+ // file (yt-dlp treats a filtered-out video as "nothing to do", not an
+ // error) — `--print`'s suppression of normal reporting (see above)
+ // means the specific reason never reaches our stdout/stderr parsing,
+ // so name the two known bounds explicitly rather than a bare exit code.
+ const reason = exit.code === 0
+ ? `no audio was produced — the video may be longer than ${DURATION_MAX_SEC / 60} minutes or its audio larger than ${Math.round(MUSIC_UPLOAD_MAX_BYTES / 1024 / 1024)}MB, or it may be otherwise unavailable`
+ : (exit.reason || `yt-dlp exited ${exit.code}`);
+ throw new Error(reason);
}
broadcastSse(job, { type: 'progress', percent: 100, stage: 'importing' });
diff --git a/server/services/trackYoutubeImport.test.js b/server/services/trackYoutubeImport.test.js
index f33261d0e9..49dd589916 100644
--- a/server/services/trackYoutubeImport.test.js
+++ b/server/services/trackYoutubeImport.test.js
@@ -1,4 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { EventEmitter } from 'events';
vi.mock('../lib/ytdlp.js', () => ({ findYtDlp: vi.fn(async () => '/usr/local/bin/yt-dlp') }));
vi.mock('../lib/ffmpeg.js', () => ({
@@ -7,17 +8,31 @@ vi.mock('../lib/ffmpeg.js', () => ({
}));
vi.mock('./pipeline/musicLibrary.js', () => ({
importUploadedTrack: vi.fn(async () => ({ filename: 'music-x.mp3', sizeBytes: 10 })),
+ MUSIC_UPLOAD_MAX_BYTES: 50 * 1024 * 1024,
}));
vi.mock('./tracks/index.js', () => ({
createTrack: vi.fn(async (input) => ({ id: 'track-new', ...input })),
+ DURATION_MAX_SEC: 3600,
}));
+vi.mock('child_process', async (importOriginal) => ({ ...(await importOriginal()), spawn: vi.fn() }));
const { findYtDlp } = await import('../lib/ytdlp.js');
const { findFfmpeg } = await import('../lib/ffmpeg.js');
+const { spawn } = await import('child_process');
const {
YOUTUBE_URL_RE, assertYoutubeUrl, startYoutubeImport,
} = await import('./trackYoutubeImport.js');
+// A fake yt-dlp child that immediately closes with the given exit code —
+// enough to exercise the argv construction without a real download.
+function fakeChild(code = 0) {
+ const proc = new EventEmitter();
+ proc.stdout = new EventEmitter();
+ proc.stderr = new EventEmitter();
+ setImmediate(() => proc.emit('close', code, null));
+ return proc;
+}
+
beforeEach(() => {
vi.clearAllMocks();
findYtDlp.mockResolvedValue('/usr/local/bin/yt-dlp');
@@ -76,3 +91,19 @@ describe('startYoutubeImport — pre-spawn guards', () => {
expect(findYtDlp).not.toHaveBeenCalled();
});
});
+
+describe('startYoutubeImport — yt-dlp argv', () => {
+ it('bounds the download with --max-filesize / --match-filters mirroring the existing media limits', async () => {
+ spawn.mockReturnValue(fakeChild(0));
+ await startYoutubeImport('https://youtu.be/dQw4w9WgXcQ');
+ // The kickoff spawns inside a fire-and-forget IIFE — flush a couple of
+ // microtask turns so it runs past the spawn() call before asserting.
+ await new Promise((resolve) => setImmediate(resolve));
+ await new Promise((resolve) => setImmediate(resolve));
+
+ expect(spawn).toHaveBeenCalledOnce();
+ const [, args] = spawn.mock.calls[0];
+ expect(args).toEqual(expect.arrayContaining(['--max-filesize', String(50 * 1024 * 1024)]));
+ expect(args).toEqual(expect.arrayContaining(['--match-filters', 'duration <= 3600']));
+ });
+});