#5907·deer-flow

[bug] MCP task notification recovery uses stale clocks and needs rollback-safe launch fencing

Author: CorgiBoyGCreated Sep 26, 2026Updated Sep 27, 2026

[bug] MCP task notification recovery uses stale clocks and needs rollback-safe launch fencing

Summary

The durable MCP task notification worker carries the batch scan timestamp through asynchronous run lookup and notification launch operations.

This creates three related correctness problems:

  1. retry and follow-up schedules are calculated from the scan start rather than the remote operation's completion time;
  2. an expired worker can start an Agent notification run before the repository rejects its stale completion;
  3. an unexpected failure is not released until every sibling in the claimed batch finishes.

Current behavior

McpTaskService._run_notifications() passes one scan timestamp into every claimed notification. _notify_one_claimed() then reuses it after awaiting _get_run() or _launch_notification().

For a slow launch, a configured 40-second retry can therefore already be due when it is written. The same stale timestamp is also passed to repository methods that fence writes with notification_lease_expires_at >= now, allowing a physically expired lease to appear valid.

The write-side fence alone is too late for a new notification. _launch_notification() creates an Agent run before mark_notification_dispatched() checks the lease. A worker that resumes after its lease expired can therefore launch an old event snapshot even if another worker has reclaimed the task and observed a newer event.

Finally, unexpected per-record failures are collected by the batch gather() and released only after all siblings finish, so one slow sibling delays another record's retry schedule.

Expected behavior

  • Notification retry and polling schedules should start from the corresponding asynchronous operation's completion time.
  • An unexpired owner/token/version fence should be acquired before creating an Agent notification run.
  • If a launch reservation is reclaimed, the next worker should retry the same immutable snapshot and idempotency key before delivering newer events.
  • One record's unexpected failure should be released immediately without waiting for unrelated siblings.

Reproduction

Deterministic regressions can reproduce the problem without network access:

  • advance a fake clock while _launch_notification() is blocked and verify that retry scheduling uses the completion time;
  • let a notification lease expire before launch completion and verify that the stale worker currently reaches the launch side effect;
  • block one notification lookup while a sibling fails and verify that the failed sibling's lease is not released until the whole batch completes.

Proposed fix

This needs a two-stage rollout because an older Gateway does not know how to claim a new persisted launching state:

  1. Land an expand-only compatibility change. It should use completion timestamps, isolate per-record failures, and teach repository, service, and frontend readers to recover and settle launching rows without creating any production writer for that state.
  2. After the compatibility change is available as the rollback baseline, add the atomic pre-launch reservation. It should validate owner, per-claim token, lease expiry, dispatch version, and whether an unstarted snapshot has already been superseded before entering launching.

Reclaiming launching work must preserve the original dispatch version, attempt, event, and idempotency key. Ambiguous launch failures and cancellation must release only the lease without consuming the terminal retry budget.