feat: Phase 2.6 edges migration to Qdrant (TKG-only architecture)

Phase 2.6.1: co_occurrence_edges migration
- build_co_occurrence_edges_from_qdrant()
- Qdrant embeddings → frame grouping → YOLO objects
- Result: 6679 edges (vs 6701 PostgreSQL)

Phase 2.6.2: face_face_edges migration
- build_face_face_edges_from_qdrant()
- Qdrant embeddings → frame grouping → face pairs
- mutual_gaze detection preserved
- Result: 6 edges (exact match)

Phase 2.6.3: speaker_face_edges migration
- build_speaker_face_edges_from_qdrant()
- Qdrant embeddings → trace_id frame ranges
- SPEAKS_AS edge creation

Architecture:
- All edges use Qdrant payload (no face_detections queries)
- PostgreSQL fallback for empty Qdrant
- Estimated 3.6x performance improvement

Testing:
- Playground (3003): ✓ All Phase 2.6 logs verified
- Edge counts: ✓ Close match with PostgreSQL
- Fallback: ✓ Working

Docs:
- docs_v1.0/DESIGN/TKG_PHASE2_6_EDGES_MIGRATION.md
- docs_v1.0/M4_workspace/2026-06-21_phase2_6_test.md
This commit is contained in:
Accusys
2026-06-21 04:47:49 +08:00
parent 0afc70fc5b
commit 2cfcfdd1af
2926 changed files with 8311058 additions and 1394 deletions

View File

@@ -0,0 +1,76 @@
#!/opt/homebrew/bin/python3.11
"""Backfill face_id for existing face_detections rows using trace_id.
face_id is generated as 'face_{trace_id}' for each unique trace.
This covers past data where face_id was never written.
"""
import os
import psycopg2
DB_URL = os.environ.get("DATABASE_URL", "postgresql://accusys@localhost:5432/momentry")
SCHEMA = os.environ.get("MOMENTRY_DB_SCHEMA", "dev")
def get_conn():
return psycopg2.connect(DB_URL)
def backfill_by_trace(file_uuid: str, schema: str = SCHEMA) -> int:
"""Set face_id = 'face_{trace_id}' for all rows with NULL face_id and non-NULL trace_id."""
conn = get_conn()
cur = conn.cursor()
cur.execute(
f"""
UPDATE {schema}.face_detections
SET face_id = 'face_' || trace_id::text
WHERE file_uuid = %s
AND face_id IS NULL
AND trace_id IS NOT NULL
""",
(file_uuid,),
)
updated = cur.rowcount
conn.commit()
cur.close()
conn.close()
return updated
def main():
conn = get_conn()
cur = conn.cursor()
# Count rows that need backfill
cur.execute(
f"""SELECT COUNT(*) FROM {SCHEMA}.face_detections
WHERE face_id IS NULL AND trace_id IS NOT NULL"""
)
total_rows = cur.fetchone()[0]
cur.execute(
f"""SELECT DISTINCT file_uuid FROM {SCHEMA}.face_detections
WHERE face_id IS NULL AND trace_id IS NOT NULL"""
)
uuids = [row[0] for row in cur.fetchall()]
cur.close()
conn.close()
if not uuids:
print("No rows need backfill (all face_id already set or no trace_id).")
return
print(f"Found {total_rows} rows across {len(uuids)} files to backfill")
total_all = 0
for uuid in uuids:
count = backfill_by_trace(uuid)
total_all += count
print(f" [{uuid}] updated {count} rows")
print(f"\nDone: {len(uuids)} files, {total_all} rows updated")
if __name__ == "__main__":
main()