fix(discovery): make retroactive policy apply work — fix embedding table + query #682
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/retroactive-apply"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Fixes the 'Apply retroactively returns 0' bug in the face recognition auto-approval system.
Root causes
Wrong embeddings table:
matchCandidateToExistingInstancequeriedclass_occurrence_embeddings(0 rows in prod) for the candidate's own embedding. Pending candidates' face embeddings are buffered inpending_face_embeddings(5,864 rows in prod) during candidate creation.Impossible state filter: Retrospective SQL queries in
genericNamedAutoApprovalCandidatesandgenericClassInstanceUnnamedCandidateAssignmentslooked forstate='pending' AND existing_instance_id IS NOT NULL— a combination that never exists. Pending candidates haveexisting_instance_id IS NULL.Changes
ClassInstanceDiscoveryService.matchCandidateToExistingInstance: Changed step 3 to query
pending_face_embeddingsbyface_annotation_idinstead of joining throughclass_occurrence_embeddings→class_instance_occurrences.ClassInstanceService.genericNamedAutoApprovalCandidates: Changed WHERE to
existing_instance_id IS NULL, removed JOIN toclass_instances(no existing instance to join to).ClassInstanceService.applyAutoApprovalRetrospectively: Now calls
matchCandidateToExistingInstancefor each candidate, converts cosine distance to confidence, and auto-approves high-confidence matches. Unmatched candidates are counted as unnamed retargeted.Test schema: Added
pending_face_embeddingsandclass_occurrence_embeddingstables.Tests: Added 3 tests: high-confidence match approval, low-confidence pending retention, empty dataset handling.