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): drop stale beats.json on revision, dedupe in-flight measurements

[._.]/ Adam Eivy401e8fd36b3f
diff --git a/server/lib/beatGrid.js b/server/lib/beatGrid.js
index e86cd89cc8..9a0a4fb3a9 100644
--- a/server/lib/beatGrid.js
+++ b/server/lib/beatGrid.js
@@ -424,6 +424,13 @@ async function analyzeBeatGridFile(audioPath, { signal } = {}) {
// re-measures on next request.
const beatGridCache = new Map();
+// Re-entrancy guard: two overlapping requests for the SAME unmeasured track
+// (e.g. the launch-video form re-submitted quickly, or two runs that pick the
+// same library track) would otherwise each spawn their own ffmpeg decode and
+// run the DSP independently. Tracks the in-flight analysis promise per path so
+// a second caller awaits the first's result instead of duplicating the work.
+const beatGridInFlight = new Map();
+
/**
* Resolve the beat grid for an absolute audio file path, cached in memory per
* (path, mtime). Returns `{ bpm, beats, downbeats, hits }` — `bpm` and its
@@ -439,12 +446,24 @@ export async function getBeatGrid(audioPath, { signal } = {}) {
if (!stats) return null;
const cached = beatGridCache.get(audioPath);
if (cached && cached.mtimeMs === stats.mtimeMs) return cached.result;
- const analyzed = await analyzeBeatGridFile(audioPath, { signal });
- if (!analyzed) return null;
- const result = { bpm: analyzed.bpm, beats: analyzed.beats, downbeats: analyzed.downbeats, hits: analyzed.hits };
- beatGridCache.set(audioPath, { mtimeMs: stats.mtimeMs, result });
- return result;
+
+ const inFlight = beatGridInFlight.get(audioPath);
+ if (inFlight) return inFlight;
+
+ const analysis = (async () => {
+ const analyzed = await analyzeBeatGridFile(audioPath, { signal });
+ if (!analyzed) return null;
+ const result = { bpm: analyzed.bpm, beats: analyzed.beats, downbeats: analyzed.downbeats, hits: analyzed.hits };
+ beatGridCache.set(audioPath, { mtimeMs: stats.mtimeMs, result });
+ return result;
+ })();
+ beatGridInFlight.set(audioPath, analysis);
+ try {
+ return await analysis;
+ } finally {
+ beatGridInFlight.delete(audioPath);
+ }
}
// Test seam — clears the in-memory cache between cases.
-export const __clearBeatGridCache = () => beatGridCache.clear();
+export const __clearBeatGridCache = () => { beatGridCache.clear(); beatGridInFlight.clear(); };
diff --git a/server/routes/apps/launchVideos.js b/server/routes/apps/launchVideos.js
index 14a48e5040..99eb045ac4 100644
--- a/server/routes/apps/launchVideos.js
+++ b/server/routes/apps/launchVideos.js
@@ -134,9 +134,15 @@ router.post('/:id/launch-videos', loadApp, asyncHandler(async (req, res) => {
// edited) copy and an older take without one gets it added.
await installMotionKit(join(outputRoot, 'composition'));
// Plain UTF-8 JSON, so it passes the launch-video asset gate like any
- // other composition source (see motionKit.js). Written only when the
- // track actually decoded — a missing beats.json means "no library track,
- // or it couldn't be measured", never a false/empty grid.
+ // other composition source (see motionKit.js). A revision's snapshotted
+ // assets can carry the SOURCE take's own beats.json (a prior successful
+ // measurement); drop it unconditionally first so a dropped/changed track,
+ // or a measurement that fails on this run, can't leave this run cutting
+ // against a stale grid that no longer matches its own musicTrack. Written
+ // only when the track actually decoded on THIS run — a missing beats.json
+ // means "no library track, or it couldn't be measured", never a false or
+ // stale grid.
+ await rm(join(outputRoot, 'composition', BEATS_FILENAME), { force: true });
if (musicTrackBeatGrid) {
await writeFile(join(outputRoot, 'composition', BEATS_FILENAME), JSON.stringify(musicTrackBeatGrid, null, 2));
}
diff --git a/server/routes/apps/launchVideos.test.js b/server/routes/apps/launchVideos.test.js
index c616704bca..cccc1065b1 100644
--- a/server/routes/apps/launchVideos.test.js
+++ b/server/routes/apps/launchVideos.test.js
@@ -262,6 +262,16 @@ describe('feedback revisions', () => {
expect(task.prompt).toContain('"format":"vertical"');
expect(task.prompt).toContain('"synthesizeMusic":true');
});
+ it('drops a stale beats.json copied from the source take when this revision has no musicTrack (#8958)', async () => {
+ // Simulates a source composition carrying a leftover beats.json from an
+ // earlier run whose musicTrack this revision no longer uses.
+ await writeFile(join(source, 'beats.json'), JSON.stringify({ bpm: 90, beats: [0], downbeats: [0], hits: [0] }));
+ const response = await submit({ sourceVideoId: video.id, feedback: 'Drop the soundtrack' });
+ expect(response.status).toBe(202);
+ const destination = join(PATHS.data, 'launch-videos/example', response.body.runId, 'composition');
+ expect(await readdir(destination)).not.toContain('beats.json');
+ expect(getBeatGrid).not.toHaveBeenCalled();
+ });
it('rejects invalid feedback, cross-app selections and missing editable source before dispatch', async () => {
expect((await submit({ sourceVideoId: video.id, feedback: ' ' })).status).toBe(400);
expect((await submit({ sourceVideoId: video.id })).status).toBe(400);