From 7272077b6512da26f61ec41608776fecfa684c81 Mon Sep 17 00:00:00 2001 From: Accusys Date: Fri, 31 Jul 2026 10:46:31 +0800 Subject: [PATCH] docs: trace_profile UPSERT fix plan with delete/undo/restore flow diagrams --- ...026-07-31_trace_profile_upsert_fix_plan.md | 222 ++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 docs_v1.0/M4_workspace/2026-07-31_trace_profile_upsert_fix_plan.md diff --git a/docs_v1.0/M4_workspace/2026-07-31_trace_profile_upsert_fix_plan.md b/docs_v1.0/M4_workspace/2026-07-31_trace_profile_upsert_fix_plan.md new file mode 100644 index 00000000..d6cb7f13 --- /dev/null +++ b/docs_v1.0/M4_workspace/2026-07-31_trace_profile_upsert_fix_plan.md @@ -0,0 +1,222 @@ +# Trace Profile UPSERT 修復計畫 + +**Date**: 2026-07-31 +**Author**: Core Team +**Status**: 📋 計畫(待執行) + +--- + +## 問題描述 + +`trace_profiles` 有 `UNIQUE(file_uuid, trace_id)`,但所有 name 寫入都用 **UPDATE** 而非 **UPSERT**。當該 (file_uuid, trace_id) 缺列時(DB 重建、清理、新 trace 尚未建 row),UPDATE 影響 0 列、靜默失敗,前端離開再回來就恢復原名。 + +### 為什麼以前「看起來可以」 +- 如果 trace_profiles 表格剛好有該 trace 的資料,UPDATE 會成功 +- 如果表格沒有該 trace 的資料(常見情況),UPDATE 影響 0 列,名字根本沒被儲存 +- 離開再回來時,loadClusterNames 從 API 讀取,因為資料庫沒更新,就恢復原名稱 + +### 為什麼現在發現 +- 可能是最近重設過資料庫或清過 trace_profiles 表格 +- 或是之前測試的 trace 剛好有資料,現在測試的沒有 + +**這是 Core API 的 bug,不是前端問題。** + +--- + +## 核心原則(使用者定義) + +1. **FS 產生後就要非常穩定** — trace_profiles 存在性與 FS 一致 +2. **除了 delete 外 FS 不會消失** +3. **所有的操作都是 face profile 的內容修改** + +衍生決策: +- delete 連 profile 一起刪(hard delete;soft delete 保留以便 restore) +- UPSERT 缺列 INSERT 時從 tkg_nodes.properties 補填欄位 +- 所有 trace_profiles name 寫入都改 UPSERT + +--- + +## 現有 delete/undo 行為對照 + +| 操作 | 檔案 | Qdrant | tkg_nodes | trace_profiles | +|------|------|--------|-----------|----------------| +| Soft delete | trace_agent_api.rs:1815 | 標記 status=deleted | 標記 properties.status | ❌ 不刪 | +| Hard delete | trace_agent_api.rs:1716 | 移到 _faces_trash | DELETE | ❌ 不刪(**要改**) | +| Undo hard delete | profile.rs:671 | trash→_faces | re-INSERT | ❌ 不動(**要改**) | +| Merge trace | trace_agent_api.rs:1888 | source→target | DELETE source | ❌ 不刪(**要改**) | +| Undo merge | profile.rs:587 | 還原 points | re-INSERT | ❌ 不動(**要改**) | +| Restore | trace_agent_api.rs:2014 | 清 status | 清 status | ❌ 不動 | +| Rename single | profile.rs:249 | - | - | UPDATE(缺列靜默失敗) | +| Rename group | profile.rs:327 | - | - | UPDATE(缺列靜默失敗) | +| Merge groups | profile.rs:421 | - | - | name=ANY UPDATE | +| Undo rename | profile.rs:540 | - | - | UPDATE(缺列失敗) | +| Cluster agent | cluster_agent.rs:126/269/286 | - | - | UPDATE name | +| Job worker | job_worker.rs:3024/3052 | - | - | UPDATE name | + +--- + +## 修復計畫 + +### A. 新增共用 UPSERT helper(`src/core/db/schema.rs`) + +`upsert_trace_profile(pool, file_uuid, trace_id, name)` — 從 tkg_nodes.properties 補填欄位: + +```sql +INSERT INTO trace_profiles (file_uuid, trace_id, name, start_frame, end_frame, + frame_count, key_frame, key_face, status, avg_confidence, properties) +SELECT $1, $2, $3, + (tn.properties->>'start_frame')::bigint, + (tn.properties->>'end_frame')::bigint, + (tn.properties->>'frame_count')::int, + tn.properties->>'key_frame', + tn.properties->>'key_face', + 'pending', + (tn.properties->>'avg_confidence')::double precision, + tn.properties +FROM tkg_nodes tn +WHERE tn.file_uuid=$1 AND tn.node_type='face_track' AND (tn.properties->>'trace_id')::int=$2 +ON CONFLICT (file_uuid, trace_id) +DO UPDATE SET name=EXCLUDED.name, updated_at=NOW() +``` + +### B. `src/api/profile.rs` — rename 核心修復 + +| # | Handler | 位置 | 修改 | +|---|---------|------|------| +| 1 | `update_trace_profile_group_handler` | line 327-340 | UPDATE → UPSERT | +| 2 | `update_trace_profile_handler`(單筆) | line 249-290 | 改 UPSERT;缺列不再 NOT_FOUND,INSERT 補完整 row(properties 合併以 tkg 為基底) | +| 3 | `undo_face_operation_handler` | line 540 | rename 還原改 UPSERT;若 before_names 不含該 trace(原本無 row)→ DELETE 還原 | +| 4 | `merge_groups_handler` | line 421 | name=ANY 更新 → 先查 matching trace_ids 再逐個 UPSERT | + +### C. `src/api/trace_agent_api.rs` — delete/merge 連 profile 刪 + +| # | Handler | 位置 | 修改 | +|---|---------|------|------| +| 5 | `delete_trace` hard 分支 | line 1716 | before_snapshot 納入 trace_profiles;加 DELETE trace_profiles | +| 6 | `merge_trace` | line 1888 | source trace_profiles 一併刪除,snapshot 納入(FS 消失 = profile 消失) | +| 7 | Soft delete | line 1815 | 維持標記不刪 profile(restore 依賴名稱,不需重建) | + +### D. `src/api/profile.rs` — undo 重建 profile + +| # | Handler | 位置 | 修改 | +|---|---------|------|------| +| 8 | `undo_hard_delete_trace` | line 671 | 重建 trace_profiles(從 snapshot 或 tkg 補填) | +| 9 | `undo_merge_trace` | line 587 | 重建 source trace_profiles | + +### E/F. 背景 name 寫入全改 UPSERT + +| # | 檔案 | 位置 | +|---|------|------| +| 10 | `cluster_agent.rs` | line 126(reset names) | +| 11 | `cluster_agent.rs` | line 269(update labels) | +| 12 | `cluster_agent.rs` | line 286(reset unclustered) | +| 13 | `job_worker.rs` | line 3024(sync name) | +| 14 | `job_worker.rs` | line 3052(reset unclustered) | + +--- + +## 資料流圖示 + +### Soft Delete → Restore + +``` +Soft Delete Restore +──────────── ─────── +Qdrant _faces: Qdrant _faces: + payload.status "deleted" ─────▶ payload.status null (清除) +tkg_nodes: tkg_nodes: + properties.status "deleted" ─────▶ properties - 'status' (移除) +trace_profiles: trace_profiles: + ❌ 不刪 ❌ 不動 +trace_operation_history: trace_operation_history: + ❌ 不記錄 ❌ 不動 +``` + +### Hard Delete → Undo Hard Delete + +``` +Hard Delete Undo Hard Delete +──────────── ───────────────── +Qdrant _faces: Qdrant _faces: + ① scroll points (含 vector) ① 從 _faces_trash scroll + ② 複製 → _faces_trash ② 複製回 _faces (移除 trashed_at) + ③ delete from _faces ③ 從 _faces_trash 刪除 +tkg_nodes: tkg_nodes: + ④ SELECT snapshot (external_id, ④ re-INSERT (ON CONFLICT DO NOTHING) + label, properties) trace_profiles: + ⑤ DELETE face_track nodes ⑤ 重建(需新增,從 snapshot/tkg 補填) +trace_profiles: + ⑥ DELETE(需新增) +trace_operation_history: + ⑦ INSERT (operation='hard_delete', + before_snapshot 含 tkg_nodes + + trace_profiles【需新增】) +``` + +### Merge Trace → Undo Merge + +``` +Merge Trace Undo Merge +──────────── ────────── +Qdrant _faces: Qdrant _faces: + ① snapshot source points ① delete points WHERE merged_from=source + ② source trace_id → target ② re-INSERT 原 source points + + payload.merged_from=source +tkg_nodes: tkg_nodes: + ③ DELETE source face_track ③ re-INSERT source (ON CONFLICT DO NOTHING) +trace_profiles: trace_profiles: + ④ DELETE source(需新增) ④ 重建 source(需新增) +trace_operation_history: trace_operation_history: + ⑤ INSERT (operation='merge_trace', ⑥ is_undone=true + before_snapshot 含 source/target + + qdrant_points + tkg + + trace_profiles【需新增】) +``` + +### Rename / Merge Groups → Undo + +``` +Rename (single/group) Undo +──────────────────── ──── +trace_profiles: trace_profiles: + name ← 新名(改 UPSERT) name ← 還原名 + (原本無 row → DELETE;有 row → UPSERT) +trace_operation_history: trace_operation_history: + INSERT (rename_single/batch, is_undone=true + before_snapshot 含 traces 舊名) +``` + +--- + +## 修復後對照 + +| 操作 | 現況 trace_profiles | 修復後 trace_profiles | +|------|---------------------|------------------------| +| Soft delete | 保留 | 保留(restore 不需重建) | +| Hard delete | ❌ 保留(幽靈) | ✅ DELETE + snapshot 納入 | +| Merge trace | ❌ source 保留(幽靈) | ✅ DELETE source + snapshot 納入 | +| Undo hard delete | ❌ 不重建 | ✅ 重建(從 snapshot/tkg 補填) | +| Undo merge | ❌ 不重建 | ✅ 重建 source | +| Restore | 不動 | 不動 | +| Rename (single/group) | UPDATE(缺列靜默失敗) | ✅ UPSERT(缺列 INSERT 從 tkg 補填) | +| Undo rename | UPDATE | ✅ 缺列時若原本無 row → DELETE;有 row → UPSERT 還原 | + +--- + +## 驗證計畫 + +1. `cargo check` / `cargo clippy --lib` / `cargo test --lib` / `cargo fmt` +2. 實測(備份後,public schema): + - 刪 profile row → rename → row 重建且欄位完整 + - hard delete → profile 刪 → undo → 重建 + - merge → source profile 刪 → undo → 重建 +3. Studio 端到端 rename 驗證 + +--- + +## 注意事項 + +- Soft delete 保留 profile 是為讓 restore 不需重建;若希望 soft delete 也刪 profile,restore 需另加 snapshot 才能還原名稱 +- `trace_profiles` 的 `id` 是 `nextval('trace_profiles_id_seq')`,UPSERT INSERT 自動產生 +- `(file_uuid, trace_id)` 已有 UNIQUE constraint,UPSERT 可行 +- 所有操作必須在 Dev (3003) 或備份後 public schema 測試,禁止直接對未備份資料操作