Skip to content

fix(backend): drop a merge source's task search vectors when the merge removes them - #15953

Merged
undivisible merged 1 commit into
BasedHardware:mainfrom
kduailabs:fix/merge-delete-action-item-vectors
Oct 19, 2026
Merged

undivisible merged 1 commit into
BasedHardware:mainfrom
kduailabs:fix/merge-delete-action-item-vectors

Conversation

@kduailabs

@kduailabs kduailabs commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Merging conversations deletes every source conversation and its tasks (_delete_conversation_and_related_data) but leaves the removed tasks' Pinecone vectors behind.

Impact

find_similar_action_items feeds the conversation-extraction prompt with the user's open tasks so the LLM can suppress duplicates. A merged-away task's orphaned vector still matches, so a real new task extracted later looks like a duplicate and is silently never created. The vectors accumulate in the action-items namespace with no other cleanup path.

Same class as the just-merged #15317 (swipe-delete) and the companion #15950 (cascade delete). Every other delete path already drops the vector; the merge cleanup was the remaining one.

Fix

The cleanup already reads the source's stored tasks before deleting them (for the reminder cancels from #15177). The same read now drives delete_action_item_vectors_batch, guarded so a source without tasks makes no call and logged so a vector failure cannot abort the rest of the cleanup (matching the reminder-cancel step above it).

Tests

  • backend/tests/unit/test_merge_source_delete_cancels_task_reminders.py: a merged-away source drops the vectors of every removed task; a source without tasks makes no call; a task without a due date still has its vector dropped (it had no reminder but does have a vector). The two reminder assertions still hold.
  • backend/tests/unit/test_merge_validation.py: the module-scoped database.vector_db stub now carries the new collaborator (one line).

Verification

cd backend
python -m pytest tests/unit/test_merge_source_delete_cancels_task_reminders.py -q   # 4 passed
python -m pytest tests/unit/test_merge_validation.py tests/unit/test_merge_skips_retraction_without_canonical_memories.py tests/unit/test_merge_conversations_canonical_delete.py tests/unit/test_merge_deleted_source_race.py tests/unit/test_merge_conversation_photos.py tests/unit/test_merge_audio_chunk_copy_failure.py -q   # 86 passed

Against origin/main merge_conversations.py the two new vector assertions fail.

Review in cubic

Product invariants affected

none

Failure-Class: none

@kduailabs

Copy link
Copy Markdown
Contributor Author

Bounty proposal for this fix: #15954 ($25 proposed).

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

Reviewed 47ab3f5.

Merge-source task vectors are still skipped on main. This head stops skipping them on delete. Not merging. This is a delete-data change.

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

Verified - this closes the last delete path that leaked task vectors, consistent with #15317 (merged) and the companion #15950.

What I checked:

  • backend/utils/conversations/merge_conversations.py - _delete_conversation_and_related_data now feeds the task rows it read before the Firestore delete into delete_action_item_vectors_batch, inside the same else branch as the reminder cancels and wrapped in its own try/except, so a Pinecone failure logs but cannot abort the remaining cleanup (photos, audio, conversation vector, doc) or fail the merge after the rows are gone. The delete is scoped to {uid}-ai-{id} vector IDs built from that source's own rows, so the surviving merged conversation's tasks and anyone else's data can't be touched, and re-deleting an already-absent vector is a Pinecone no-op.
  • backend/tests/unit/test_merge_source_delete_cancels_task_reminders.py - the new assertions cover the cases that matter: every removed task's vector drops in one batched call, an undated task with no reminder still drops its vector, and a source with no tasks makes no call. The pre-existing reminder-cancel assertions are retained.
  • backend/tests/unit/test_merge_validation.py - one-line addition of the new collaborator to the module-scoped database.vector_db stub, the right seam for the module-level import.

CI on this head (Backend Hermetic Merge Gate, Backend unit suite, Sync Cloud Tasks Stack Gauntlet, Hermetic Backend E2E) is green, and this head already carries a maintainer approval - no further action needed from my side. Thanks @kduailabs for the tight, well-explained fix.

🤖 Automated review 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 backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution labels Sep 22, 2026
@Git-on-my-level

Copy link
Copy Markdown
Member

Follow-up verification on 47ab3f5 — the change still checks out, with two accuracy notes and one follow-up opportunity.

Per-file:

  • backend/utils/conversations/merge_conversations.py — the new delete_action_item_vectors_batch call sits in the else-branch that runs only after the Firestore delete of the source rows succeeds, mirroring the reminder-cancel step exactly (own try/except + logger.error, so a Pinecone failure logs but cannot abort the remaining cleanup). The if source_items: guard is redundant with the batch helper's own empty-list early return, but harmless; delete-by-id in ns4 is idempotent, so re-running cleanup is safe. The retain_capture (sync-bridge) path reaches the same else-branch, so those merges are covered too.
  • backend/tests/unit/test_merge_source_delete_cancels_task_reminders.py — the patch target utils.conversations.merge_conversations.delete_action_item_vectors_batch matches the new module-level from-import, and the new assertions cover vectors-dropped-for-all-removed-tasks, no-call-when-no-tasks, and undated-task-vector-still-dropped, while the two reminder assertions still hold. Both touched test files pass at this head (44 passed).
  • backend/tests/unit/test_merge_validation.py — the one-line delete_action_item_vectors_batch = MagicMock() addition to the module-scoped database.vector_db stub is exactly what the fresh from-import needs to resolve. No mismatches.

Two accuracy notes (non-blocking):

  1. The PR description's failure mechanism is stronger than what the current code actually does. find_similar_action_items does return orphaned ns4 vectors, but its only consumer, _fetch_dedup_candidates_for_query in backend/utils/conversations/process_conversation.py, re-fetches the candidates via get_action_items_by_ids and drops anything missing from Firestore — so at this head a ghost vector cannot actually make a real new task look like a duplicate; it only wastes one of the top_k=10 slots. The cleanup is still right (namespace hygiene plus parity with every other delete path), just worth knowing the impact claim is theoretical unless that re-check is ever removed.
  2. Correcting my earlier comment, which said this closes the last delete path that leaked task vectors: the cascade delete branch in backend/routers/conversations.py (~L1243-1252, the fix(backend): drop a conversation's task search vectors when a cascade delete removes them #15950 path) still deletes a conversation's action items and cancels reminders without dropping their ns4 vectors. The same three-line pattern from this PR would close it — a nice, self-contained follow-up if you want to take it.

Minor edges observed, all bounded by the Firestore re-check above: soft-deleted (deleted: True) task rows are skipped by the list read but hard-deleted by delete_action_items_for_conversation, so retired rows' vectors can survive a merge; and the list read caps at 2000 rows while the hard delete is unbounded (theoretical at current per-conversation task counts).

No concerns on the merge path itself; @aryanorastar's approval on this head stands. The cascade-delete vector gap in note 2 is the one piece worth a maintainer's eye — a small hygiene follow-up, not urgent.

Feedback generated by the repo's automated review assistant 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.

@kduailabs
kduailabs force-pushed the fix/merge-delete-action-item-vectors branch from 47ab3f5 to 06a9095 Compare October 3, 2026 13:28
@kduailabs

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (35238e1dbe), conflict-free. The reminder-cancel half is already on main; this PR's residual diff is the ghost task-vector drop for merged-away source tasks (delete_action_item_vectors_batch), still absent from main. Tests → 52 passed. Re-requesting review.

@undivisible
undivisible merged commit 58c919d into BasedHardware:main Oct 19, 2026
45 checks passed
Git-on-my-level added a commit that referenced this pull request Oct 19, 2026
Five unit files fail on current main. Each one passed on its own PR's base
and broke when main moved underneath it; none is a product regression.

- test_request_context.py (#19738): tested the JIT budget surface that
  #22209 removed from llm_gateway/gateway/request_context.py. Keep the
  request-id tests; drop the tests for removed code.
- test_users_joan_followup_tombstone_guard.py (#19867): called
  delete_person_endpoint, but the joan followup route is
  delete_followup_question_endpoint.
- test_developer_from_segments_idempotency.py (#21265): process fakes
  rejected the trigger=CLIENT_FINALIZE kwarg the router passes since #19788.
- test_external_tools_sanitization.py (#19406): the stubbed utils package
  lacked utils.generated_compute (#21488) and utils.model_text. Stub the
  default-off gate as disabled and load the stdlib-only model_text for real.
- test_batch_upload_storage.py: the database.vector_db stub lacked
  delete_action_item_vectors_batch, which merge_conversations imports since
  #15953.

Failure-Class: none

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Git-on-my-level added a commit that referenced this pull request Oct 19, 2026
)

* fix(backend): restore pyright-clean main after #22207 and #22326

Main's backend unit shards fail the enforced pyright lane:

- database/desktop_previews.py (#22207): 16 reportUnnecessaryIsInstance
  errors. The runtime guards stay; they move into validators that take
  `object` (_payload, _string, _expected_generation, _datetime), so the
  checks are meaningful to pyright. Public signatures, error messages and
  check order are unchanged.
- utils/sync/pipeline.py:process_segment (#22326 on top of #22452):
  "code is too complex to analyze". Behaviour-neutral split: the two
  identical cap-hold defer blocks become one hold_for_cap closure, and the
  final-pass / speaker-identification block moves to
  _assign_segment_speakers with the same exception handling.

Failure-Class: none

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(backend): repair unit tests that drifted from main across merges

Five unit files fail on current main. Each one passed on its own PR's base
and broke when main moved underneath it; none is a product regression.

- test_request_context.py (#19738): tested the JIT budget surface that
  #22209 removed from llm_gateway/gateway/request_context.py. Keep the
  request-id tests; drop the tests for removed code.
- test_users_joan_followup_tombstone_guard.py (#19867): called
  delete_person_endpoint, but the joan followup route is
  delete_followup_question_endpoint.
- test_developer_from_segments_idempotency.py (#21265): process fakes
  rejected the trigger=CLIENT_FINALIZE kwarg the router passes since #19788.
- test_external_tools_sanitization.py (#19406): the stubbed utils package
  lacked utils.generated_compute (#21488) and utils.model_text. Stub the
  default-off gate as disabled and load the stdlib-only model_text for real.
- test_batch_upload_storage.py: the database.vector_db stub lacked
  delete_action_item_vectors_batch, which merge_conversations imports since
  #15953.

Failure-Class: none

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

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.

4 participants