fix: tkg face_track drift — trace stability check + repair script

Part B (source fix): Enhanced TKG build Qdrant readiness check in
job_worker.rs. Previously only checked point count >= expected_faces,
which could fire before short traces finished uploading. Now verifies
trace_id stability: two consecutive scroll checks (2s apart) with same
unique trace count before proceeding.

Part A (repair): New scripts/repair_tkg_face_tracks.py reconciles Qdrant
_faces traces with PG tkg_nodes face_track rows, inserts missing ones.
Fixed 233 missing traces in 44c37c (1197 → 1430).
This commit is contained in:
Accusys
2026-08-12 06:21:18 +08:00
parent c1e158a368
commit 2e4559715b
2 changed files with 404 additions and 17 deletions
+75 -17
View File
@@ -2686,29 +2686,51 @@ impl JobWorker {
0
};
// Wait for Qdrant to have all face points (up to 30 seconds)
// Wait for Qdrant to have all face points AND stable trace count
// (up to 30 seconds). Point count alone isn't enough — short traces
// may still be uploading. We verify trace_id stability: two consecutive
// checks (2s apart) with the same unique trace count.
let qdrant = crate::core::db::qdrant_db::QdrantDb::new();
let filter = serde_json::json!({
"must": [{"key": "file_uuid", "match": {"value": uuid}}]
});
let mut attempts = 0;
let max_attempts = 30;
let mut stable_trace_count: Option<i64> = None;
loop {
match qdrant
.count_points(
"_faces",
serde_json::json!({
"must": [{"key": "file_uuid", "match": {"value": uuid}}]
}),
)
.await
{
match qdrant.count_points("_faces", filter.clone()).await {
Ok(count) if count >= expected_faces && expected_faces > 0 => {
info!("📝 Prerequisites met for TKG Build (Qdrant has {} faces, expected {}): {}",
count, expected_faces, uuid);
break;
// Point count met — now verify trace stability
use std::collections::HashSet;
match qdrant.scroll_all_points("_faces", filter.clone(), 500).await {
Ok(pts) => {
let trace_count: i64 = pts.iter()
.filter_map(|p| p["payload"]["trace_id"].as_i64())
.filter(|&t| t >= 0)
.collect::<HashSet<_>>()
.len() as i64;
if let Some(prev) = stable_trace_count {
if trace_count == prev && trace_count > 0 {
info!("✅ TKG prerequisites met: {} faces, {} traces (stable): {}",
count, trace_count, uuid);
break;
}
}
stable_trace_count = Some(trace_count);
info!("⏳ TKG point count met ({} faces), verifying trace stability ({})...",
count, trace_count);
}
Err(e) => {
warn!("Qdrant scroll for trace check failed: {}, proceeding anyway", e);
break;
}
}
}
Ok(count) => {
attempts += 1;
if attempts >= max_attempts {
warn!("⚠️ TKG build proceeding with {} faces (expected {}) after 30s wait: {}",
warn!("⚠️ TKG build proceeding with {} faces (expected {}) after 30s wait: {}",
count, expected_faces, uuid);
break;
}
@@ -2722,7 +2744,7 @@ impl JobWorker {
break;
}
}
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}
let db_clone = self.db.clone();
@@ -2955,7 +2977,26 @@ impl JobWorker {
// Get total_frames for completion status
let video = self.db.get_video_by_uuid(uuid).await?;
let total_frames = video.map(|v| v.total_frames).unwrap_or(0);
let mut total_frames = video.map(|v| v.total_frames).unwrap_or(0);
// Backfill: registration may have stored 0 when nb_frames was missing and
// duration/fps weren't filled yet. If duration & fps are now known, derive frames.
if total_frames == 0 {
if let Some(v) = self.db.get_video_by_uuid(uuid).await? {
if v.duration > 0.0 && v.fps > 0.0 {
let derived = (v.duration * v.fps).floor() as u64;
if derived > 0 {
let _ = sqlx::query(
"UPDATE videos SET total_frames = $1 WHERE file_uuid = $2",
)
.bind(derived as i64)
.bind(&uuid)
.execute(self.db.pool())
.await;
total_frames = derived;
}
}
}
}
self.db
.update_processing_status_completed(uuid, total_frames)
@@ -2987,7 +3028,24 @@ impl JobWorker {
.await?;
let video = self.db.get_video_by_uuid(uuid).await?;
let total_frames = video.map(|v| v.total_frames).unwrap_or(0);
let mut total_frames = video.map(|v| v.total_frames).unwrap_or(0);
if total_frames == 0 {
if let Some(v) = self.db.get_video_by_uuid(uuid).await? {
if v.duration > 0.0 && v.fps > 0.0 {
let derived = (v.duration * v.fps).floor() as u64;
if derived > 0 {
let _ = sqlx::query(
"UPDATE videos SET total_frames = $1 WHERE file_uuid = $2",
)
.bind(derived as i64)
.bind(&uuid)
.execute(self.db.pool())
.await;
total_frames = derived;
}
}
}
}
self.db
.update_processing_status_completed(uuid, total_frames)