From b52b1eb32334e70446835d526da6558675895f25 Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Sat, 13 Jun 2026 23:14:39 -0400 Subject: [PATCH] Reels pre-gen: make dedup cache-key-aware so key changes regenerate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit exists_fresh only matched (span, library, render_version, age), so a cache-key change that doesn't bump RENDER_VERSION (e.g. the max_segments alignment, or any future selection-logic tweak) left last night's ledger row looking 'fresh' — the nightly run would skip and the orphaned reel would persist. Dedup now compares the stored cache_key to the freshly computed key (and confirms the mp4 exists), so a changed key forces a regen within the freshness window. exists_fresh stays as the HTTP endpoint's fast gate. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/reels/mod.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/reels/mod.rs b/src/reels/mod.rs index 2a4e6e9..c5d7492 100644 --- a/src/reels/mod.rs +++ b/src/reels/mod.rs @@ -1095,14 +1095,29 @@ async fn pregen_one( }; let min_generated_at = now - (max_age_hours as i64 * 3600); - let is_fresh = { + // Skip only when a fresh ledger row points at THIS exact cache key (same + // media, params, render version) and its file still exists. Comparing the + // stored cache_key — not just (span, library) — means a key change from + // selection-logic/params drift that doesn't bump RENDER_VERSION still forces + // a regen within the freshness window, instead of leaving a stale row that + // points at an orphaned reel. + let already_current = { let mut dao = app_state.precomputed_reel_dao.lock().expect("lock"); - dao.exists_fresh(&ctx, span, library, RENDER_VERSION as i32, min_generated_at) - .unwrap_or(false) + matches!( + dao.latest_for(&ctx, span, library), + Ok(Some(row)) + if row.cache_key == key + && row.render_version == RENDER_VERSION as i32 + && row.generated_at >= min_generated_at + ) && reel_mp4_path(app_state, &key).exists() }; - if is_fresh { - log::info!("Fresh precomputed reel exists for span={}, skipping", span); + if already_current { + log::info!( + "Fresh precomputed reel already current for span={} key={}, skipping", + span, + key + ); return Ok(()); }