Skip to content

fix(sync): guard KeyError and None person fields during speaker identification - #19009

Merged
kodjima33 merged 1 commit into
BasedHardware:mainfrom
kduailabs:fix/sync-pipeline-person-name-keyerror-guards
Oct 2, 2026
Merged

kodjima33 merged 1 commit into
BasedHardware:mainfrom
kduailabs:fix/sync-pipeline-person-name-keyerror-guards

Conversation

@kduailabs

@kduailabs kduailabs commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #19008

Summary of Changes

  1. backend/utils/sync/pipeline.py:

    • In build_person_embeddings_cache, guard person.get('id') and fallback name to person.get('name') or 'Unknown', preventing unhandled KeyError: 'name' or KeyError: 'id'.
    • In identify_speakers_for_segments, guard if person and person.get('id'): and fallback speaker name to person.get('name') or detected_name.
  2. backend/tests/unit/test_sync_pipeline_contact_guards.py:

    • Added unit test suite verifying build_person_embeddings_cache handles missing id, missing name, and name: None.
    • Added test verifying identify_speakers_for_segments handles person documents without a name key during text-based speaker detection.

Verification

  • Unit tests passed: pytest backend/tests/unit/test_sync_pipeline_contact_guards.py backend/tests/unit/test_sync_speaker_evidence.py backend/tests/unit/test_sync_ordered_assignment.py backend/tests/unit/test_sync_cross_job_assignment.py (73 passed).
  • Ratchet verified: 0 net lines increase across modified product files.

Product invariants affected

none

Failure-Class: none

Review in cubic

@kduailabs

Copy link
Copy Markdown
Contributor Author

Bounty Program Claim

@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks @kduailabs — verified against the pipeline code and this fix is correct.

backend/utils/sync/pipeline.py

  • build_person_embeddings_cache: the person.get('name') or 'Unknown' fallback addresses the real crash — get_people()/get_person_by_name() only setdefault('id') from the Firestore doc id and never backfill 'name', so a person doc with a voiceprint but no name field raises KeyError('name') today. The added person.get('id') guard is sensible defensive hardening.
  • identify_speakers_for_segments: if person and person.get('id') plus the person.get('name') or detected_name fallback is the right choice — detected_name is exactly the name that matched, so the speaker map keeps a meaningful label. Also checked the downstream consumer: process_speaker_assigned_segments only reads the person_id element of the (id, name) tuple, so the fallback name never leaks into assignment state.

backend/tests/unit/test_sync_pipeline_contact_guards.py

  • Good targeted coverage: missing-id skip, missing/None name → 'Unknown', and the text-detection path asserting the speaker map receives ('person-bob', 'Bob') — which matches the new fallback exactly. Patch targets resolve correctly in both the database.users and utils.sync.pipeline namespaces.
  • Minor: these tests stub usable_person_voiceprint, so they exercise the guards but not voiceprint extraction itself — fine for unit scope since existing suites cover that helper.

Also confirmed main still has the unguarded person['name'] here, so this isn't duplicating an already-merged fix, and the backend CI gates are green. Leaving merge sign-off to a human maintainer.


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 positive-signal Automation verified a genuine fix/quality contribution backend Backend Task (python) labels Sep 26, 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.

Trusted backend fixer (kduailabs), scoped KeyError/None guard in speaker sync with tests, CI green; mergeStateStatus UNKNOWN so approve-only.

@kduailabs
kduailabs force-pushed the fix/sync-pipeline-person-name-keyerror-guards branch from 4a2657c to 976dc95 Compare September 26, 2026 16:47
@Git-on-my-level

Copy link
Copy Markdown
Member

Post-rebase check on 976dc95: the rebase onto current main left the fix untouched. The diff is still exactly the four guards in backend/utils/sync/speaker_identity.py — the person.get('id') skip and person.get('name') or 'Unknown' fallback in build_person_embeddings_cache, and the person.get('id') check with the person.get('name') or detected_name fallback in identify_speakers_for_segments — plus the new guard tests in backend/tests/unit/test_sync_pipeline_contact_guards.py. One note for the record: the PR description attributes the change to backend/utils/sync/pipeline.py, but that file is unchanged on this branch (main now re-exports these helpers from utils/sync/speaker_identity.py), so the guards land in the module that actually holds the code from issue #19008. Also confirmed main still has the unguarded accesses and #19008 remains open, and that the pre-rebase maintainer approval covers the same content — these files are unchanged by the rebase. All backend gates are green on the new head; nothing further needed from my side.


by AI on behalf of David

@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.

976dc95a34db6c3466cf3ec683b883275ef57222 backend/utils/sync/speaker_identity.py a person missing id or name still raises KeyError. test_sync_pipeline_contact_guards.py pins that hole. Hygiene passed.

@kduailabs

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (fedf258be3) — the branch was dirty (conflict in backend/utils/sync/speaker_identity.py).

Resolved by keeping main's newer additions and layering this PR's guards on top:

  • build_person_embeddings_cache: kept main's 'pinned': person.get('pinned') is True and applied the guard to 'name': person.get('name') or 'Unknown'.
  • text-detection loop: kept main's text_assignments.extend(...) block and guarded on if person and person.get('id'):.

Tests: test_sync_pipeline_contact_guards.py + pinned-prior / transcription-shadow / sync cross-job / merge-target / nonowning-writers → 91 passed. Head af99d08d2e.

@kduailabs
kduailabs force-pushed the fix/sync-pipeline-person-name-keyerror-guards branch from 976dc95 to af99d08 Compare October 2, 2026 10:56
@Git-on-my-level

Copy link
Copy Markdown
Member

Verified against the current head (af99d08):

backend/utils/sync/speaker_identity.py

  • build_person_embeddings_cache: if emb and person.get('id') prevents KeyError: 'id' when a person doc lacks an id, and person.get('name') or 'Unknown' prevents KeyError: 'name' (it also coerces empty-string names to 'Unknown', a consistent fallback). Malformed docs are now skipped instead of aborting the whole cache build mid-loop.
  • identify_speakers_for_segments: if person and person.get('id') guards the text-detection assignment path, and the speaker map now uses person.get('name') or detected_name — sensible, since detected_name is non-empty by construction at that point.
  • No behavior change for well-formed person docs; purely defensive. For what it's worth, main still has the unguarded person['name'] (lines 80/294), so this fix is live, not superseded.

backend/tests/unit/test_sync_pipeline_contact_guards.py

  • Patch targets check out: utils.sync.pipeline imports usable_person_voiceprint and detect_speaker_from_text at module level and _speaker_identity_dependencies() resolves them from that namespace, so patch('utils.sync.pipeline.…') intercepts the real dependency path the production wrapper uses.
  • Coverage spans missing id, missing name, name: None, and the text-detection path asserting the ('person-bob', 'Bob') speaker-map fallback. Backend unit suite passes on this head.

One merge-logistics note: both approvals (kodjima33 on 4a2657c, aryanorastar on 976dc95) predate the current head af99d08, which added the test suite and rebased — a quick maintainer re-confirm on this head before merging would be good. Nothing blocking from my side; nice defensive fix with solid regression coverage.


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.

Confidence 5/5: resolves #19008, guards KeyError/None person fields during speaker identification, scoped diff (77 loc), tests added, CLEAN + CI green.

@kodjima33
kodjima33 merged commit fd7b727 into BasedHardware:main Oct 2, 2026
45 checks passed
Git-on-my-level added a commit that referenced this pull request Oct 2, 2026
These tests arrived from main (#19009) and exercise naming other people in
sync, which this branch gates on the plan. Without a declared plan they read
the subscription and hit the blocked network.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]: Sync Audio Pipeline KeyError on Missing Contact Name or ID

4 participants