Skip to content

fix(backend): cancel the replaced tasks' reminders when a conversation is reprocessed - #15180

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
kduailabs:fix/reprocess-cancels-replaced-task-reminders
Sep 21, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
kduailabs:fix/reprocess-cancels-replaced-task-reminders

Conversation

@kduailabs

@kduailabs kduailabs commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

_write_action_items (the function that persists a conversation's extracted tasks) deletes every task the conversation wrote before and re-creates it under a fresh id, but never cancels the replaced rows' reminders.

Impact

Reprocessing a conversation (POST /v1/conversations/{id}/reprocess, or any re-run of processing) replaces its tasks: the old rows are deleted and the new extraction is created under new ids, with a reminder scheduled for each dated one (send_action_item_data_message). The deleted rows still owned client-scheduled local notifications, and the mobile client cancels those only on the backend's deletion data message (app/lib/services/notifications/action_item_notification_handler.dart → handleDeletionMessage). So after a reprocess:

  • a task that survives re-extraction gets two reminders for the same commitment (old id + new id), and
  • a task the new extraction drops keeps a reminder for a task that no longer exists.

Same class as the sibling paths fixed in #15041 (conversation cascade delete), #15043 (developer API delete), #14983/#14984 (checking a task off inside a conversation) and #15028/#15029 (MCP task writes).

Fix

Read the previous rows before deleting them, then send sync_action_item_reminder(..., completed=True, due_at=None) for each open dated row, gated exactly like the conversation-delete path. sync_action_item_reminder joins the existing utils.notifications import at the top of the module; the old-ids list is derived from the same read, so no extra query.

Tests

backend/tests/unit/test_write_action_items_cancels_replaced_reminders.py (new, hermetic: the real writer with every I/O seam recorded):

  • replacing an open dated task, a completed dated task and an undated task cancels only the open dated one
  • a first processing with no previous tasks sends nothing

Verification

cd backend
python -m pytest tests/unit/test_write_action_items_cancels_replaced_reminders.py -q            # 3 passed
python -m pytest tests/unit/test_process_conversation_usage_context.py tests/unit/test_backend_candidate_capture.py tests/unit/test_process_conversation_free_tier_branch.py tests/unit/test_conversation_memories_telemetry.py tests/unit/test_first_open_effect_resume.py -q   # 168 passed
python -m black --check utils/conversations/process_conversation.py tests/unit/test_write_action_items_cancels_replaced_reminders.py   # clean

Against origin/main process_conversation.py the new positive test fails (sync_action_item_reminder called 0 times).

Failure-Class: none

Line-Count-Exception: backend/utils/conversations/process_conversation.py | 3187 -> 3207 | cancel the replaced rows' client-scheduled reminders inside the existing replacement block; the old-ids read already existed, so the growth is the cancel loop and its comment

Review in cubic

Product invariants affected

  • INV-MEM-4
  • INV-TASK-2

Neither invariant is altered: this change does not touch memory intake or promotion,
and it does not add or route any capture-to-task path. It only cancels the mobile
client's already-scheduled reminder for rows that were deleted through the existing
replacement writer.

@kduailabs

Copy link
Copy Markdown
Contributor Author

Bounty proposal for this fix: #15181 ($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.

Verified on ef46b545f3f9.

Two things before this can land:

  1. The cancel loop sits between delete_action_items_for_conversation and create_action_items_batch with no try. sync_action_item_reminder ends in FCM; if it raises, the old tasks are already gone and the new extraction is never written. Wrap the cancel (log and continue) so a reminder send cannot skip recreate — the merge/swipe-delete siblings already swallow that inside their existing except.

  2. Hygiene fails product-invariants: process_conversation.py is a locked path and the body never names INV-MEM-4 or INV-TASK-2. Add:

## Product invariants affected

- INV-MEM-4
- INV-TASK-2

The reprocess hole itself is real (main still drops old ids without a cancel). I am not merging.

@kduailabs

Copy link
Copy Markdown
Contributor Author

Both points are addressed:

  1. The cancel loop now runs inside its own try/except that logs and continues (cf7dbd4b12), so a failed reminder send can no longer skip create_action_items_batch. Added a regression test where sync_action_item_reminder raises and the new extraction is still written (that test fails on the previous head).

  2. ## Product invariants affected with INV-MEM-4 and INV-TASK-2 is now in the PR body.

Re-requesting review at cf7dbd4b122bc9522d3aef28faeec3c1587fac0b.

@Git-on-my-level

Copy link
Copy Markdown
Member

Verified both fixes from the prior review on cf7dbd4b:

  • process_conversation.py _write_action_items: the cancel loop now sits in its own try/except between delete_action_items_for_conversation and create_action_items_batch, logging and continuing — a failed reminder send can no longer skip the new extraction. The new test_a_failing_reminder_cancel_does_not_skip_the_new_tasks exercises exactly this, and the invariants section (INV-MEM-4, INV-TASK-2) is in the PR body.
  • The gating if item.get('due_at') and not item.get('completed') matches the sibling delete paths, and reusing the existing old_items read means no extra query.

Two minor, non-blocking notes:

  1. The except wraps the whole loop, so one FCM failure skips the remaining cancels for that pass (those reminders stay armed until the next reprocess). A per-item try would cancel as many as possible; fine to leave for symmetry with the siblings.
  2. For conversations with many dated tasks the loop sends one FCM data message per replaced item; send_action_items_batch_deletion_message chunks 100 ids per message if this path ever gets hot — not needed now since the per-item helper matches the sibling paths.

The hermetic test follows the existing unit-suite conventions (real writer, I/O seams recorded, first-processing negative case). Checks green including the hermetic backend gates. Nice fix — re-review from @aryanorastar needed to clear the prior change request on this head.

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

Verified on cf7dbd4b12.

Both items from the earlier request are addressed:

  1. The reminder cancel sits in its own try/except between delete and create_action_items_batch. test_a_failing_reminder_cancel_does_not_skip_the_new_tasks pins that a raising sync_action_item_reminder still writes the new extraction.
  2. The PR body names INV-MEM-4 and INV-TASK-2.

Gating matches the delete siblings (due_at and not completed). Current main still drops old ids without a cancel. I am not merging this SHA.

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

5/5 confidence: real bug (reprocessing a conversation replaces its tasks without cancelling the replaced rows' client-scheduled reminders, causing duplicate/stale reminders), same class as merged siblings (#15041/#15043/#14983/#15028), scoped with new regression tests that fail against origin/main; Hygiene/Formatting checks were stale cancelled runs, reran green, all CI now passing.

@kodjima33
kodjima33 merged commit aa0e01c into BasedHardware:main Sep 21, 2026
51 of 55 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.

4 participants