diff --git a/src/worker/job_worker.rs b/src/worker/job_worker.rs index 7c52d900..328b7a27 100644 --- a/src/worker/job_worker.rs +++ b/src/worker/job_worker.rs @@ -1860,16 +1860,23 @@ impl JobWorker { } } - let all_ok = asr_done && trace_done && tkg_done && face_cluster_done && qdrant_face_done; + // VLM summary: blocking for files with face traces + let vlm_done_bool: bool = if has_face { + if let (Some(total), Some(done)) = (vlm_total, vlm_done_count) { + total == 0 || done == total + } else { true } + } else { true }; + + let all_ok = asr_done && trace_done && tkg_done && face_cluster_done && qdrant_face_done && vlm_done_bool; tracing::info!( - "[Ingestion] all_ok={} (asr={}, trace={}, tkg={}, face_cluster={}, rule3={}, scene_meta={}, qdrant_face={}) for uuid={}", - all_ok, asr_done, trace_done, tkg_done, face_cluster_done, rule3_done, scene_meta_done, qdrant_face_done, uuid + "[Ingestion] all_ok={} (asr={}, trace={}, tkg={}, face_cluster={}, rule3={}, scene_meta={}, qdrant_face={}, vlm={}) for uuid={}", + all_ok, asr_done, trace_done, tkg_done, face_cluster_done, rule3_done, scene_meta_done, qdrant_face_done, vlm_done_bool, uuid ); if !all_ok { tracing::info!( - "[Ingestion] waiting (uuid={}): asr={} trace={} tkg={} face_cluster={} rule3={} scene_meta={} qdrant_face={}", - uuid, asr_done, trace_done, tkg_done, face_cluster_done, rule3_done, scene_meta_done, qdrant_face_done + "[Ingestion] waiting (uuid={}): asr={} trace={} tkg={} face_cluster={} rule3={} scene_meta={} qdrant_face={} vlm={}", + uuid, asr_done, trace_done, tkg_done, face_cluster_done, rule3_done, scene_meta_done, qdrant_face_done, vlm_done_bool ); } all_ok @@ -2962,23 +2969,38 @@ impl JobWorker { } // 🚀 P5 Trigger: VLM Summary (after TKG, key_frame exists) + // Uses JOIN query to verify trace_profiles is consistent with tkg_nodes. + // Spawned with 5-min timeout to prevent blocking worker indefinitely. let db_p5 = db_clone.clone(); let redis_p5 = redis_clone.clone(); let uuid_p5 = uuid_clone.clone(); let output_dir_p5 = output_dir_clone.clone(); let pool_p5 = db_clone.pool().clone(); tokio::spawn(async move { - let face_count = Self::wait_for_traces(&pool_p5, &uuid_p5, 60).await; + // Verify data consistency: trace_profiles JOIN tkg_nodes + let face_count = Self::verify_traces_ready(&pool_p5, &uuid_p5).await; if face_count > 0 { - vlm_summary::generate_vlm_summaries( - &db_p5, - &uuid_p5, - &output_dir_p5, - Some(redis_p5), + info!("[VLM] Starting for {}: {} traces verified", uuid_p5, face_count); + // Apply timeout to prevent hanging indefinitely + let result = tokio::time::timeout( + std::time::Duration::from_secs(300), + vlm_summary::generate_vlm_summaries( + &db_p5, + &uuid_p5, + &output_dir_p5, + Some(redis_p5), + ), ) .await; + match result { + Ok(stats) => info!( + "[VLM] Completed for {}: {}/{} summarized, {} embedded", + uuid_p5, stats.summarized, stats.total, stats.embedded + ), + Err(_) => error!("[VLM] Timed out after 5 min for {}", uuid_p5), + } } else { - info!("[VLM] Skipping for {} (no face traces after 60s wait)", uuid_p5); + info!("[VLM] Skipping for {} (no verified face traces)", uuid_p5); } }); }); @@ -3071,17 +3093,29 @@ impl JobWorker { let output_dir_p5 = output_dir_clone.clone(); let pool_p5 = db_clone.pool().clone(); tokio::spawn(async move { - let face_count = Self::wait_for_traces(&pool_p5, &uuid_p5, 60).await; + // Verify data consistency: trace_profiles JOIN tkg_nodes + let face_count = Self::verify_traces_ready(&pool_p5, &uuid_p5).await; if face_count > 0 { - vlm_summary::generate_vlm_summaries( - &db_p5, - &uuid_p5, - &output_dir_p5, - Some(redis_p5), + info!("[VLM] Starting for {}: {} traces verified", uuid_p5, face_count); + let result = tokio::time::timeout( + std::time::Duration::from_secs(300), + vlm_summary::generate_vlm_summaries( + &db_p5, + &uuid_p5, + &output_dir_p5, + Some(redis_p5), + ), ) .await; + match result { + Ok(stats) => info!( + "[VLM] Completed for {}: {}/{} summarized, {} embedded", + uuid_p5, stats.summarized, stats.total, stats.embedded + ), + Err(_) => error!("[VLM] Timed out after 5 min for {}", uuid_p5), + } } else { - info!("[VLM] Skipping for {} (no face traces after 60s wait)", uuid_p5); + info!("[VLM] Skipping for {} (no verified face traces)", uuid_p5); } }); }); @@ -3389,32 +3423,27 @@ impl JobWorker { Ok(()) } - /// Poll trace_profiles table until rows appear (TKG may still be writing). - /// Returns count if found within timeout, 0 otherwise. - async fn wait_for_traces(pool: &PgPool, uuid: &str, max_secs: u64) -> i64 { + /// Verify trace_profiles data is consistent with tkg_nodes (JOIN check). + /// Ensures TKG has fully committed before P5 runs. + async fn verify_traces_ready(pool: &PgPool, uuid: &str) -> i64 { let tp_table = crate::core::db::schema::table_name("trace_profiles"); - let interval = std::time::Duration::from_secs(2); - let max_attempts = (max_secs / 2).max(1); - - for attempt in 0..max_attempts { - let count: i64 = sqlx::query_scalar(&format!( - "SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1" - )) - .bind(uuid) - .fetch_optional(pool) - .await - .ok() - .flatten() - .unwrap_or(0); - if count > 0 { - return count; - } - if attempt == 0 { - info!("[P5-wait] Waiting for trace_profiles to appear for {}", uuid); - } - tokio::time::sleep(interval).await; - } - 0 + let tkg_table = crate::core::db::schema::table_name("tkg_nodes"); + // JOIN ensures trace_profiles has matching tkg_nodes face_track entries + let count: i64 = sqlx::query_scalar(&format!( + "SELECT COUNT(*) FROM {tp_table} tp \ + INNER JOIN {tkg_table} tn \ + ON tn.file_uuid = tp.file_uuid \ + AND tn.node_type = 'face_track' \ + AND (tn.properties->>'trace_id')::int = tp.trace_id \ + WHERE tp.file_uuid = $1" + )) + .bind(uuid) + .fetch_optional(pool) + .await + .ok() + .flatten() + .unwrap_or(0); + count } async fn update_face_track_labels(