fix: Library Re-run 完整重建搜尋索引 (ASR/ASRX/OCR/Scene VLM)

- trigger_processing: force 時僅刪除指定 processor 的輸出檔與 processor_results
  (其他 processor 資料保留),並設定 rerun_rebuild Redis flag 要求重建索引
- worker: 單一 processor Re-run 時強制 Rule 1 重新 chunk + vectorize
  (清除 momentry_rule1 舊 vectors,避免 stale 搜尋結果)
- Scene VLM Re-run: job_processors 含 scene_vlm 即 dispatch,
  scene-only job 完成條件改以 scenes.json 完整度判定
- 前端 redoProcessor 傳 force=true,Scene VLM 新增 🔄 Re-run 按鈕
This commit is contained in:
Accusys
2026-11-10 07:59:31 +08:00
parent 043dbbf24a
commit d21d1ab960
2 changed files with 199 additions and 13 deletions
+115 -4
View File
@@ -44,6 +44,39 @@ fn delete_output_files_for_uuid(uuid: &str, output_dir: &str) -> u64 {
deleted_count
}
/// Delete only the output files of the given processors (single-processor re-run).
/// Other processors' outputs (and the combined `{uuid}.json`) are preserved so the
/// rest of the pipeline data stays intact.
fn delete_processor_output_files(uuid: &str, processors: &[String], output_dir: &str) -> u64 {
let mut files: Vec<String> = Vec::new();
for proc in processors {
match proc.as_str() {
"asr" => files.push(format!("{}.asr.json", uuid)),
"asr_asrx" | "asrx" => files.push(format!("{}.asrx.json", uuid)),
"ocr" => files.push(format!("{}.ocr.json", uuid)),
"cut" => files.push(format!("{}.cut.json", uuid)),
"face" => files.push(format!("{}.face.json", uuid)),
"face_cluster" => files.push(format!("{}.face_cluster.json", uuid)),
"face_trace" => files.push(format!("{}.face_traced.json", uuid)),
"scene_vlm" | "scene" => files.push(format!("{}.scenes.json", uuid)),
"pose" => files.push(format!("{}.pose.json", uuid)),
"hand" => files.push(format!("{}.hand.json", uuid)),
"appearance" => files.push(format!("{}.appearance.json", uuid)),
"tkg" => files.push(format!("{}.tkg.json", uuid)),
_ => continue,
}
}
let mut deleted_count = 0u64;
for name in files {
let path = std::path::Path::new(output_dir).join(&name);
if path.exists() && std::fs::remove_file(&path).is_ok() {
tracing::info!("[FORCE_RESCAN] Deleted output file: {}", name);
deleted_count += 1;
}
}
deleted_count
}
#[derive(Debug, Serialize)]
struct JobListResponse {
jobs: Vec<JobInfoResponse>,
@@ -327,9 +360,28 @@ async fn trigger_processing(
// Clean up stale processor_results from previous runs
// Old entries with status='running' from a dead worker session
// would block the worker from actually running processors.
// For single-processor re-runs, only the requested processors are reset
// so other processors' completed results are preserved.
let pr_table = schema::table_name("processor_results");
sqlx::query(&format!("DELETE FROM {pr_table} WHERE job_id = $1"))
if processors_to_run.is_empty() {
sqlx::query(&format!("DELETE FROM {pr_table} WHERE job_id = $1"))
.bind(job_id)
.execute(state.db.pool())
.await
.map_err(|e| {
tracing::error!(
"[TRIGGER] Failed to clean processor_results for job {}: {}",
job_id,
e
);
StatusCode::INTERNAL_SERVER_ERROR
})?;
} else {
sqlx::query(&format!(
"DELETE FROM {pr_table} WHERE job_id = $1 AND (processor = ANY($2) OR processor_type = ANY($2))"
))
.bind(job_id)
.bind(&processors_to_run)
.execute(state.db.pool())
.await
.map_err(|e| {
@@ -340,17 +392,76 @@ async fn trigger_processing(
);
StatusCode::INTERNAL_SERVER_ERROR
})?;
// Scene VLM rows are file-keyed with job_id = NULL, delete them separately
if processors_to_run
.iter()
.any(|p| p == "scene_vlm" || p == "scene")
{
sqlx::query(&format!(
"DELETE FROM {pr_table} WHERE file_uuid = $1 AND processor_type = 'scene_vlm'"
))
.bind(&file_uuid)
.execute(state.db.pool())
.await
.map_err(|e| {
tracing::error!(
"[TRIGGER] Failed to clean scene_vlm results for {}: {}",
file_uuid,
e
);
StatusCode::INTERNAL_SERVER_ERROR
})?;
}
}
tracing::info!("[TRIGGER] Cleaned processor_results for job {}", job_id);
// If force=true, delete output files to force re-processing
if req.force {
let output_dir = crate::core::config::OUTPUT_DIR.to_string();
let deleted_count = delete_output_files_for_uuid(&file_uuid, &output_dir);
let deleted_count = if processors_to_run.is_empty() {
// Full re-scan: delete all output files
delete_output_files_for_uuid(&file_uuid, &output_dir)
} else {
// Single-processor re-run: delete only that processor's output
delete_processor_output_files(&file_uuid, &processors_to_run, &output_dir)
};
tracing::info!(
"[TRIGGER] Force re-scan: deleted {} output files for {}",
"[TRIGGER] Force re-scan: deleted {} output files for {} (processors={:?})",
deleted_count,
file_uuid
file_uuid,
processors_to_run
);
// Re-running ASR/OCR/Scene VLM must also rebuild the search index:
// - asr/asrx/ocr → Rule 1 re-chunking + re-vectorize (chunk table + momentry_rule1)
// - scene_vlm → _scenes collection regeneration
// Signal the worker via Redis so the Rule 1 guard doesn't skip re-chunking
// just because old chunks still exist.
let rebuild_procs = ["asr", "asr_asrx", "asrx", "ocr", "scene_vlm", "scene"];
let needs_rebuild = processors_to_run.is_empty()
|| processors_to_run
.iter()
.any(|p| rebuild_procs.contains(&p.as_str()));
if needs_rebuild {
let key = format!("{}rerun_rebuild:{}", REDIS_KEY_PREFIX.as_str(), file_uuid);
let flag_value = if processors_to_run.is_empty() {
"all".to_string()
} else {
processors_to_run.join(",")
};
let _: Result<String, _> = redis::cmd("SET")
.arg(&key)
.arg(&flag_value)
.arg("EX")
.arg(86400)
.query_async(&mut conn)
.await;
tracing::info!(
"[TRIGGER] Set rerun_rebuild flag for {} = {}",
file_uuid,
flag_value
);
}
}
// Update existing job with new face_settings if provided