fix: P5 race condition — poll for trace_profiles before VLM generation

- Add wait_for_traces() helper: polls trace_profiles table every 2s
  up to 60s, returning when rows appear (TKG may still be writing)
- Replace one-shot face_count query in both P5 triggers (has_face +
  no-face paths) with polling
- Verified: 22/22 traces summarized for 9df18017, semantic search
  returns 10 VLM results with sim 0.601-0.696
This commit is contained in:
Accusys
2026-08-22 16:17:30 +08:00
parent 054545bccd
commit 978b2b338b
+34 -26
View File
@@ -2966,20 +2966,9 @@ impl JobWorker {
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 {
// Check if this file has face traces
let tp_table =
crate::core::db::schema::table_name("trace_profiles");
let face_count: i64 = sqlx::query_scalar(&format!(
"SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1"
))
.bind(&uuid_p5)
.fetch_optional(db_p5.pool())
.await
.ok()
.flatten()
.unwrap_or(0);
let face_count = Self::wait_for_traces(&pool_p5, &uuid_p5, 60).await;
if face_count > 0 {
vlm_summary::generate_vlm_summaries(
&db_p5,
@@ -2989,7 +2978,7 @@ impl JobWorker {
)
.await;
} else {
info!("[VLM] Skipping for {} (no face traces)", uuid_p5);
info!("[VLM] Skipping for {} (no face traces after 60s wait)", uuid_p5);
}
});
});
@@ -3080,18 +3069,9 @@ impl JobWorker {
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 tp_table = crate::core::db::schema::table_name("trace_profiles");
let face_count: i64 = sqlx::query_scalar(&format!(
"SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1"
))
.bind(&uuid_p5)
.fetch_optional(db_p5.pool())
.await
.ok()
.flatten()
.unwrap_or(0);
let face_count = Self::wait_for_traces(&pool_p5, &uuid_p5, 60).await;
if face_count > 0 {
vlm_summary::generate_vlm_summaries(
&db_p5,
@@ -3101,7 +3081,7 @@ impl JobWorker {
)
.await;
} else {
info!("[VLM] Skipping for {} (no face traces)", uuid_p5);
info!("[VLM] Skipping for {} (no face traces after 60s wait)", uuid_p5);
}
});
});
@@ -3409,6 +3389,34 @@ 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 {
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
}
async fn update_face_track_labels(
db: &crate::core::db::PostgresDb,
uuid: &str,