spud 906c3d4554
fix(mcp): make durable task claims cancellation-safe (#4966)
* feat(mcp): re-scope to MCP task claim lifecycle only

Keep PR #4966 a small, closed MCP lease/cancellation state-machine change and
move RunJournal and Run lifecycle work into dedicated follow-ups. This branch
contains only the MCP task claim lifecycle:

- mcp task release/snapshot fencing by owner + per-claim lease token
- phase-level single-flight poll/cancel/notification owners with retained handoff
- routine cancellation no longer persisted as a task failure diagnostic
- bounded ordinary release ownership retention past the drain deadline
- 0018_mcp_task_lease_tokens migration + migration/bootstrap head assertions
- wait_for_task_until helper (MCP uses it); worker-specific capture helper moved
  to the run-finalization follow-up

RunJournal (journal.py + test_run_journal.py) and run lifecycle
(manager/worker/store/run sql + run tests) are preserved on
backup/cancellation-safety-full and will be raised as separate follow-ups.

* fix(mcp): unblock claims after ambiguous handoff resolves

A phase-level single-flight owner only guards an ambiguous claim outcome. Once
the claim resolves, the phase owner is released immediately; the handoff may
continue releasing returned rows as bounded, service-owned background work
(transferred to _compensation_tasks on timeout). Per-claim token fencing rejects
a late release against a newer claim generation, so a stuck release no longer
locks the whole phase until process restart.

- README: drop the stale progress-snapshot sentence from the bounded ordinary
  release description.
- service: pop the identity-checked phase owner as soon as the claim outcome is
  known, then release returned rows with the bounded path; carry the release in
  _compensation_tasks if it exceeds the drain deadline.
- mcp/AGENTS.md: document that only an unresolved claim outcome (not the handoff)
  blocks later phase scans, and that returned-row releases may continue in the
  background once the owner is released.
- tests: pin that the phase owner is released before a stuck release finishes
  while the release stays service strong-owned.

* refactor(mcp): remove unused single-record claim wrappers

_poll_one, _cancel_one, and _notify_one are unreachable in production: the
worker always processes claimed records through _run_claimed_batch, so these
wrappers preserved a second, dead single-record lifecycle (state is None)
whose only observable behavior was a wrapper-specific cancellation release.

Remove the three wrappers and migrate the regressions that guarded their
cancel/release invariants to exercise the production _run_claimed_batch path
(operation=_*_one_claimed, release=_release_*_after_cancellation). The single
wrapper-only "state is None" contract (test_poll_release_hang_without_batch)
is deleted; all 11 remaining invariants (CancelledError preservation, repeated
cancellation, poll-only token-fenced lease release, notification claimed vs
dispatched phase release, hung compensation -> service ownership, and
background compensation exactly-once observation) are now covered through the
real batch lifecycle.

* fix(mcp): fence claim-owned mutations against stale generations

The per-claim token check in the ORM release/apply paths was only in the
SELECT; the final write went out by primary key. On SQLite (where
with_for_update() is a no-op) a mutation from an older claim generation
could therefore clear a claim that a newer generation had reclaimed after lease
expiry — the exact distributed lease-fencing failure the per-claim token was
meant to prevent.

Make every claim-owned mutation a single atomic conditional UPDATE with the
owner and per-claim token in the WHERE clause (rowcount 0 => stale, return
False, no mutation):

- release_claim: atomic fence; record the poll-failure event after the fence
  wins (same transaction, holding the write lock).
- apply_snapshot / apply_cancel_snapshot: atomic fence; record the event after.
- finish_notification_run: atomic fence; use a CASE on event_version >>
  dispatch_version to keep a newer event pending for redelivery instead of
  swallowing it as delivered.

Add one regression per path: a stale generation's release/apply/finish after a
same-worker reclaim is rejected and never clears the newer claim.

* test(mcp): pin the migration chain head to the lease-token revision

0026_mcp_task_lease_tokens becomes the alembic head, so the chain-head pin in the 0025 repair test had to move on. Follow the 0023 precedent there (single head plus expected predecessor) instead of pinning a literal head, and give the new revision its own migration test, which owns the pin and covers the nullable claim-token columns on upgrade and their removal on downgrade.

* refactor(mcp): close cancellation cleanup leftovers

* fix(mcp): retain cancelled release diagnostics

* test(mcp): remove obsolete settled compensation case

* test(mcp): cover interleaved lease reclaim races

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
2026-09-20 19:11:42 +08:00

84 lines
5.0 KiB
Python

from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import JSON, Boolean, DateTime, Index, Integer, String, Text, UniqueConstraint, false
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.constants import (
MCP_TASK_NAME_MAX_LENGTH,
MCP_TASK_REMOTE_ID_MAX_LENGTH,
MCP_TASK_SERVER_NAME_MAX_LENGTH,
)
from deerflow.persistence.base import Base
class McpTaskRow(Base):
__tablename__ = "mcp_tasks"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
user_id: Mapped[str] = mapped_column(String(64), index=True)
thread_id: Mapped[str] = mapped_column(String(64), index=True)
thread_incarnation: Mapped[str | None] = mapped_column(String(32), nullable=True)
run_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
tool_call_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
server_name: Mapped[str] = mapped_column(String(MCP_TASK_SERVER_NAME_MAX_LENGTH))
driver_name: Mapped[str] = mapped_column(String(64))
remote_task_id: Mapped[str] = mapped_column(String(MCP_TASK_REMOTE_ID_MAX_LENGTH))
task_name: Mapped[str] = mapped_column(String(MCP_TASK_NAME_MAX_LENGTH))
status: Mapped[str] = mapped_column(String(32), index=True)
result: Mapped[Any | None] = mapped_column(JSON, nullable=True)
result_preview: Mapped[str | None] = mapped_column(Text, nullable=True)
result_truncated: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
result_artifact: Mapped[dict[str, str] | None] = mapped_column(JSON, nullable=True)
error: Mapped[str | None] = mapped_column(Text, nullable=True)
input_required: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
driver_data: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
notification_status: Mapped[str] = mapped_column(String(16), default="none", index=True)
event_fingerprint: Mapped[str | None] = mapped_column(String(64), nullable=True)
event_version: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
notified_version: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
dispatch_version: Mapped[int | None] = mapped_column(Integer, nullable=True)
dispatch_attempt: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
dispatch_event: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
notification_run_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
notification_error: Mapped[str | None] = mapped_column(Text, nullable=True)
notification_attempt_count: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
next_notification_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
notification_lease_owner: Mapped[str | None] = mapped_column(String(128), nullable=True)
notification_lease_expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
notification_lease_token: Mapped[str | None] = mapped_column(String(64), nullable=True)
next_poll_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), index=True, nullable=True)
last_polled_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
last_poll_error: Mapped[str | None] = mapped_column(Text, nullable=True)
poll_attempt_count: Mapped[int] = mapped_column(Integer, default=0)
consecutive_poll_error_count: Mapped[int] = mapped_column(Integer, default=0)
lease_owner: Mapped[str | None] = mapped_column(String(128), nullable=True)
lease_expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
lease_token: Mapped[str | None] = mapped_column(String(64), nullable=True)
cancel_requested_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
cancel_attempt_count: Mapped[int] = mapped_column(Integer, default=0, server_default="0")
next_cancel_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
last_cancel_error: Mapped[str | None] = mapped_column(Text, nullable=True)
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)
__table_args__ = (
UniqueConstraint(
"user_id",
"server_name",
"remote_task_id",
name="uq_mcp_tasks_user_server_remote",
),
Index("ix_mcp_tasks_thread_created", "thread_id", "created_at"),
Index("ix_mcp_tasks_due", "status", "next_poll_at"),
Index("ix_mcp_tasks_notification_due", "notification_status", "next_notification_at"),
Index("ix_mcp_tasks_cancel_due", "cancel_requested_at", "next_cancel_at"),
)