Skip to content

fix(backend): drop a speaker profile when its teaching sample is deleted - #18881

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
apoorvdarshan:fix/speech-profile-after-delete
Sep 28, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
apoorvdarshan:fix/speech-profile-after-delete

Conversation

@apoorvdarshan

@apoorvdarshan apoorvdarshan commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Deleting a speaker sample through the expand endpoint removed the audio file and left the embedding stored.
  • When that sample is the person's teaching source, the profile is cleared so the next tagged segment can build a new one.

Fixes #5084

Test plan

  • PYTHONPATH=. python3 -c the helper cases in backend/tests/unit/test_speech_profile_deletion.py (pytest is not installed locally)
  • Delete a person's speech sample, tag them again in a later conversation, and confirm a new profile is stored

Made with Cursor

Review in cubic

Failure-Class: none

The expand delete removed the audio file and left the embedding, so tagging that person later did not build a new profile.

Fixes BasedHardware#5084

Co-authored-by: Cursor <[email protected]>
@Git-on-my-level Git-on-my-level added needs-tests PR introduces logic that should be covered by tests backend Backend Task (python) labels Sep 25, 2026

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @apoorvdarshan — good diagnosis, and reusing the existing transactional invalidation (invalidate_person_speech_profile) instead of adding a new write path is exactly the right shape for this fix. I confirmed the mechanism: delete_user_person_speech_sample only removes the GCS blob, so speech_samples / speaker_embedding / speech_sample_source survived in Firestore and the stale embedding kept matching. I also ran the new suite plus the adjacent ones (test_speech_profile_deletion.py, test_taught_speaker_persistence.py, test_manual_speaker_assignments.py) against this head — 36 passed.

Per-file notes:

  • backend/utils/speech_profile_deletion.py — the guard matches the provenance shape written by replace_person_speech_profile (conversation_id equality + segment_ids, database/users.py:1066) and is safe on a missing person/source/empty conversation id.
  • backend/routers/speech_profile.py — reading the person before invalidation is fine because the actual fence (conversation + segment overlap) is re-checked inside the Firestore transaction (users.py:1082), so a concurrent teach or label correction can't be corrupted. Deleting blobs via the returned speech_samples paths is also the correct cleanup: profiles with teaching provenance are uuid4-named (upload_person_speech_sample_from_bytes), so the legacy {memory_id}_segment_{segment_idx}.wav filename delete is a no-op exactly where the new code fires.
  • backend/tests/unit/test_speech_profile_deletion.py — covers the helper's three branches, but only the helper.

Two changes needed before merge:

  1. CI is red on PR Metadata Preflight and Hygiene: a fix: commit subject requires a Failure-Class: declaration in the PR body. Run scripts/pr-preflight --suggest from the repo root for paste-ready guidance, then add the accepted form (e.g. Failure-Class: new) to the PR description.

  2. Regression coverage at the endpoint: the original bug lived in delete_extra_speech_profile_sample — blob deleted, Firestore profile untouched — so the regression test should exercise that handler, not only the new helper. tests/unit/test_speech_profile_status.py already shows the sanctioned pattern (import routers.speech_profile, patch.object its dependencies): assert invalidate_person_speech_profile and delete_speech_profile_blob are invoked when the deleted sample's conversation matches the teaching source, and not invoked when it doesn't. The helper-only test can't catch a re-break of the wiring.

Non-blocking observation: profiles created before teaching provenance existed have no speech_sample_source, so deleting their last sample still leaves the stored embedding in place. Migrating legacy data is out of scope here — just worth knowing when triaging #5084 reports from long-tenured users.

With the declaration and the endpoint test, this should be ready for maintainer merge review — the approach itself looks correct to me.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix, linked #5084, root cause explained, test added, confidence 4/5. Not merging: PR Metadata Preflight CI check failing.

Cover delete_extra_speech_profile_sample directly so a re-break of the invalidation wiring is caught, not just the provenance helper.
@apoorvdarshan

Copy link
Copy Markdown
Collaborator Author

Added the endpoint regression test. tests/unit/test_speech_profile_deletion.py now drives delete_extra_speech_profile_sample directly with patch.object seams (same pattern as test_speech_profile_status.py) and asserts invalidate_person_speech_profile + delete_speech_profile_blob fire when the deleted sample's conversation matches the teaching source, and stay untouched when it doesn't (and on the no-person path). Helper-only cases remain.

Also added the Failure-Class: none declaration.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug fix, fixes #5084, root cause + endpoint-level regression test now added (addresses earlier review feedback), confidence 4/5, CI green. Not merging: mergeStateStatus UNKNOWN (unverified).

@Git-on-my-level
Git-on-my-level dismissed their stale review September 26, 2026 18:12

Dismissed: both concerns from this review are resolved on the current head — the Failure-Class declaration is in the PR description (PR Metadata Preflight / Hygiene green), and endpoint-level regression tests for delete_extra_speech_profile_sample were added and ran green in CI.

@Git-on-my-level

Copy link
Copy Markdown
Member

Both items from the earlier automated review are resolved on this head — thanks for the quick turnaround, @apoorvdarshan.

  • The Failure-Class: declaration is now in the PR description; PR Metadata Preflight and Hygiene are green.
  • backend/tests/unit/test_speech_profile_deletion.py now exercises delete_extra_speech_profile_sample itself, not just the helper: the matching-conversation case asserts the sample blob delete, invalidate_person_speech_profile('u1', 'p1', 'conv-1', ['s1', 's2']), and the follow-up delete_speech_profile_blob on the returned path; the other-conversation case asserts neither fires; the no-person case asserts the delete_additional_profile_audio path. That covers exactly the wiring whose absence caused Bug: Deleting a Speaker's Speech Profile Does Not Create a New One #5084, and CI ran these cases (the av skipif only gates local runs).

Re-checked the wiring on this head: backend/routers/speech_profile.py reads the person before invalidation, which is safe because the conversation + segment-overlap fence is re-checked inside the Firestore transaction (_invalidate_speech_profile_transaction in database/users.py), and the invalidated paths it returns are the uuid4-named teaching samples, so the blob cleanup matches. Nice touch in backend/utils/speech_profile_deletion.py: str()-coercing segment ids means legacy integer ids can't silently mismatch the provenance written at enrollment.

Removed the now-stale needs-tests label and dismissed the earlier changes-requested review since its concerns are addressed. The branch is already maintainer-approved — leaving the merge call with maintainers.


by AI on behalf of David — if a reply is needed, please @Git-on-my-level.

@Git-on-my-level Git-on-my-level removed the needs-tests PR introduces logic that should be covered by tests label Sep 26, 2026

@aryanorastar aryanorastar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

85839331c84ba8ebf7b5a40a570e85d096f5d5dd delete_extra_speech_profile_sample deleting a sample leaves that conversation's teaching segments in place. test_speech_profile_deletion.py pins that hole. Backend unit suite passed.

@Git-on-my-level

Copy link
Copy Markdown
Member

Follow-up verification on the current head (8583933), complementing the earlier automated ack:

  • backend/utils/speech_profile_deletion.py — teaching_segment_ids_for_deleted_sample is correctly conservative: it returns segment ids only when speech_sample_source.conversation_id matches the deleted sample's memory_id, and returns [] for a missing person/source, so deleting a sample from an unrelated conversation leaves the stored profile intact.
  • backend/routers/speech_profile.py — the new wiring in delete_extra_speech_profile_sample calls invalidate_person_speech_profile(uid, person_id, memory_id, segment_ids) and deletes each returned path via delete_speech_profile_blob. I confirmed on main that the underlying Firestore transaction (_invalidate_speech_profile_transaction) re-validates the conversation/segment match atomically, so the non-transactional get_person pre-read here is only a pre-filter and doesn't introduce a consistency gap.
  • backend/tests/unit/test_speech_profile_deletion.py — good coverage of the helper edge cases and all three endpoint branches (match, other conversation, no person_id), with the PyAV-dependent endpoint cases structured to run in CI.

The ENCRYPTION_SECRET constant in the test is the repo's standard unit-test fixture value, so no concern there. Thanks @apoorvdarshan — this closes the exact gap from #5084 where the embedding outlived its deleted audio.

This is AI-generated feedback on behalf of the maintainers.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added the positive-signal Automation verified a genuine fix/quality contribution label Sep 27, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backend bug fix, fixes #5084: speaker profile embedding wasn't cleared when its teaching sample was deleted. Root cause stated, linked bug, scoped diff (124 lines), unit tests added, CI green (all backend shards pass), confirmed not already fixed on main. Confidence 5/5.

@kodjima33
kodjima33 merged commit 54b4f6a into BasedHardware:main Sep 28, 2026
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Deleting a Speaker's Speech Profile Does Not Create a New One

4 participants