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): key in-flight beat-grid analysis by mtime too

[._.]/ Adam Eivy87d72ba69a07
diff --git a/server/lib/beatGrid.js b/server/lib/beatGrid.js
index 9a0a4fb3a9..40520d133f 100644
--- a/server/lib/beatGrid.js
+++ b/server/lib/beatGrid.js
@@ -427,9 +427,12 @@ 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.
+// run the DSP independently. Tracks the in-flight analysis promise per
+// (path, mtime) — not path alone — so a track overwritten WHILE a measurement
+// is running starts its own fresh analysis instead of a second caller
+// receiving a grid for bytes that are no longer on disk.
const beatGridInFlight = new Map();
+const inFlightKey = (audioPath, mtimeMs) => `${audioPath}\u0000${mtimeMs}`;
/**
* Resolve the beat grid for an absolute audio file path, cached in memory per
@@ -447,7 +450,8 @@ export async function getBeatGrid(audioPath, { signal } = {}) {
const cached = beatGridCache.get(audioPath);
if (cached && cached.mtimeMs === stats.mtimeMs) return cached.result;
- const inFlight = beatGridInFlight.get(audioPath);
+ const key = inFlightKey(audioPath, stats.mtimeMs);
+ const inFlight = beatGridInFlight.get(key);
if (inFlight) return inFlight;
const analysis = (async () => {
@@ -457,11 +461,11 @@ export async function getBeatGrid(audioPath, { signal } = {}) {
beatGridCache.set(audioPath, { mtimeMs: stats.mtimeMs, result });
return result;
})();
- beatGridInFlight.set(audioPath, analysis);
+ beatGridInFlight.set(key, analysis);
try {
return await analysis;
} finally {
- beatGridInFlight.delete(audioPath);
+ beatGridInFlight.delete(key);
}
}
diff --git a/server/lib/beatGrid.test.js b/server/lib/beatGrid.test.js
index d4e696e80a..cd68626b84 100644
--- a/server/lib/beatGrid.test.js
+++ b/server/lib/beatGrid.test.js
@@ -230,4 +230,18 @@ describe('getBeatGrid (mtime-keyed cache)', () => {
expect(updated.bpm).toBeGreaterThan(130 - BPM_TOLERANCE);
expect(updated.bpm).toBeLessThan(130 + BPM_TOLERANCE);
});
+
+ it('shares one in-flight analysis across concurrent calls for the same unmeasured track (skipped without ffmpeg)', async () => {
+ if (!ffmpeg) { console.log('⏭️ ffmpeg not found — skipping beat-grid in-flight dedup'); return; }
+ const wavSampleRate = 44100;
+ const wavPath = join(tmpDir, 'concurrent-track.wav');
+ await writeFile(wavPath, encodeWav(clickTrack({ bpm: 120, durationSec: 6, sampleRate: wavSampleRate }), wavSampleRate));
+
+ // Two concurrent callers against the same never-yet-cached path must
+ // resolve to the SAME object — proof they shared one decode+analysis
+ // rather than each spawning their own ffmpeg process.
+ const [a, b] = await Promise.all([getBeatGrid(wavPath), getBeatGrid(wavPath)]);
+ expect(a).not.toBeNull();
+ expect(b).toBe(a);
+ });
});