mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-08-16 01:38:53 +00:00
* fix(runs): cancel degrades to lease takeover for multi-worker Work item 4 of the multi-worker ownership epic (https://github.com/bytedance/deer-flow/issues/3948). Problem: POST /runs/{run_id}/cancel landing on a non-owning worker returns 409 — the cancel button silently fails under GATEWAY_WORKERS>1 with no sticky routing. cancel() required the current worker to hold the in-memory task/abort_event, which any non-owner pod cannot satisfy. Changes: - RunManager.cancel() returns CancelOutcome enum (cancelled / taken_over / lease_valid_elsewhere / not_active_locally / not_cancellable / unknown) instead of bool, so the router can map each outcome to the right HTTP response. - New store primitive claim_for_takeover(): a single atomic conditional UPDATE that marks a run as error only when status IN (pending, running) AND (lease IS NULL OR lease < now - grace). Closes the stale-read / concurrent-heartbeat race — if the owner renews between our read and write, the UPDATE matches 0 rows and we surface lease_valid_elsewhere. - HTTP cancel + stream-join endpoints route on CancelOutcome: cancelled -> 202 (or 204 with wait=true); taken_over -> 202 immediately (no SSE streaming — the run is terminal on another worker, streaming would hang); lease_valid_elsewhere -> 409 + Retry-After header computed from lease_expires_at + grace_seconds. - RunManager.grace_seconds exposed as a public property; the router no longer reaches into _run_ownership_config. - _is_lease_expired extracted to a module-level function, shared by RunManager.cancel() and MemoryRunStore.claim_for_takeover(). - GATEWAY_WORKERS=1 + heartbeat_enabled=false is zero-regression: the non-local path short-circuits to not_active_locally, preserving the original 409 behaviour the existing tests pin. Tests: 12 new (5 store primitive + 4 cancel-takeover unit + 3 HTTP including a regression guard verifying POST /stream?action=interrupt on a dead-owner run returns 202 instead of hanging on SSE). 244 directly-related tests pass; 36/36 blocking-IO gate pass. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(runs): guard update_status and self-terminate on takeover Two defenses close a split-brain window where the original owner could overwrite a peer's takeover status: - update_status (SQL + memory store) now guards on status IN ('pending','running'). When takeover already set the row to 'error', the owner's final status write matches 0 rows and is dropped. - _persist_status: when update_status returns False, check whether the row exists before attempting recovery via put(). If the row exists (takeover by another worker), skip recovery instead of blindly upserting over the takeover. - Heartbeat _renew_leases: when update_lease returns False (row no longer pending/running or owner changed), cancel the local task so wasted CPU is bounded to the next heartbeat tick (~10s) instead of the full task lifetime. Also fix three reviewer feedback items: - Re-fetch the store row when cancel() returns lease_valid_elsewhere, so Retry-After uses the owner's freshly-renewed lease instead of a stale value from request start. - Fallback 'unknown' in takeover error message when owner_worker_id is NULL (pre-ownership data). - Remove dead else-10 branch from grace_seconds property (unreachable — all callers are downstream of the heartbeat_enabled guard). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(runs): pin split-brain defences from update_status guard + heartbeat Three tests lock down the takeover authoritativeness so a late-running owner cannot overwrite a peer's claim: - update_status must reject writes when the store row is already terminal (taken over by another worker). - _persist_status must skip row-recovery via put() when the row exists but has been taken over. - Heartbeat _renew_leases must cancel the local task when update_lease returns False (row claimed by another worker). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(runs): precise outcome + log when local cancel loses to peer takeover Two reviewer precision nits on the split-brain defence: - _persist_status: branch the skip-reason log on existing["status"]. error → WARNING "peer takeover" (anomalous); interrupted/success → INFO "local cancel/completion race" (expected when user hits stop as the run finishes). Stops noisy false-positive takeover warnings in operator logs. - cancel() local path: when _persist_status returns False, re-check the store. If a peer's claim_for_takeover flipped the row to error between our in-memory cancel and the guarded update_status, surface taken_over instead of cancelled so the client sees a status consistent with the store. Test: test_cancel_returns_taken_over_when_peer_claims_during_local_cancel pins the race outcome. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(runs): widen update_status guard, de-duplicate lease helpers, add coverage Round 3 of reviewer feedback: - Widen update_status guard to status IN ('pending','running','interrupted'). The original guard blocked interrupted→error (the rollback finalize path), losing the "Rolled back by user" message. interrupted is now permitted while error/success stay locked — takeover protection unchanged. - claim_for_takeover False now re-reads the store row to distinguish causes: owner renewed lease → lease_valid_elsewhere; row went terminal → not_cancellable; another worker already took it over → taken_over. - Extract _raise_lease_valid_elsewhere() helper to de-duplicate the 409+Retry-After block shared across cancel_run and stream_existing_run. - Extract _lease_expired_or_null() in persistence/run/sql.py to de-duplicate the lease-expiry SQL WHERE clause shared by claim_for_takeover and list_inflight_with_expired_lease. - 11 new tests: 5 SQL-layer claim_for_takeover (expired/valid/NULL/ terminal/nonexistent), 3 _compute_retry_after unit (NULL/unparseable/ normal), 2 claim re-read precision (terminal/takeover), 1 stream endpoint 409+Retry-After. Not addressed (non-blocking, reviewer agreed): - The 2–3 store.gets in the takeover cold path: optimizing the API to accept a pre-fetched record would couple the router to the manager more tightly than justified by the perf gain. - The lease-expiry inline loop in MemoryRunStore.list_inflight_with_- expired_lease pre-computes cutoff once for all rows; switching to the shared _is_lease_expired helper would recompute datetime.now() per row with no real benefit. 260 related tests pass; 36/36 blocking-IO gate pass; ruff clean. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(runs): de-duplicate lease-expiry helper, restore defensive fallback Address final round of review feedback: - Extract is_lease_expired to deerflow.utils.time (no _ prefix, public utility). Manager and MemoryRunStore now import from the same place instead of the store reaching backward into the manager for a private function. - Restore defensive else-10 fallback in grace_seconds property (removed in an earlier round). The guard is unreachable for current callers but protects future ones from AttributeError. - Comment the transient in-memory interrupted vs store error state when a local cancel is superseded by a peer takeover. - Comment the max(1, ...) floor in _compute_retry_after — the floor is a lower bound, not a poll interval; clients should apply jitter. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com> Co-authored-by: rayhpeng <rayhpeng@gmail.com>
560 lines
23 KiB
Python
560 lines
23 KiB
Python
"""SQLAlchemy-backed RunStore implementation.
|
|
|
|
Each method acquires and releases its own short-lived session.
|
|
Run status updates happen from background workers that may live
|
|
minutes -- we don't hold connections across long execution.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from datetime import UTC, datetime, timedelta
|
|
from typing import Any
|
|
|
|
from sqlalchemy import or_, select, update
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
|
|
|
from deerflow.persistence.run.model import RunRow
|
|
from deerflow.runtime.runs.store.base import RunStore
|
|
from deerflow.runtime.user_context import AUTO, _AutoSentinel, resolve_user_id
|
|
from deerflow.utils.time import coerce_iso
|
|
|
|
|
|
def _lease_expired_or_null(lease_col, cutoff: datetime):
|
|
"""SQLAlchemy filter: True when the lease is NULL or has expired past *cutoff*."""
|
|
return or_(lease_col.is_(None), lease_col < cutoff)
|
|
|
|
|
|
class RunRepository(RunStore):
|
|
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
|
self._sf = session_factory
|
|
|
|
@staticmethod
|
|
def _normalize_model_name(model_name: str | None) -> str | None:
|
|
"""Normalize model_name for storage: strip whitespace, truncate to 128 chars."""
|
|
if model_name is None:
|
|
return None
|
|
if not isinstance(model_name, str):
|
|
model_name = str(model_name)
|
|
normalized = model_name.strip()
|
|
if len(normalized) > 128:
|
|
normalized = normalized[:128]
|
|
return normalized
|
|
|
|
@staticmethod
|
|
def _safe_json(obj: Any) -> Any:
|
|
"""Ensure obj is JSON-serializable. Falls back to model_dump() or str()."""
|
|
if obj is None:
|
|
return None
|
|
if isinstance(obj, (str, int, float, bool)):
|
|
return obj
|
|
if isinstance(obj, dict):
|
|
return {k: RunRepository._safe_json(v) for k, v in obj.items()}
|
|
if isinstance(obj, (list, tuple)):
|
|
return [RunRepository._safe_json(v) for v in obj]
|
|
if hasattr(obj, "model_dump"):
|
|
try:
|
|
return obj.model_dump()
|
|
except Exception:
|
|
pass
|
|
if hasattr(obj, "dict"):
|
|
try:
|
|
return obj.dict()
|
|
except Exception:
|
|
pass
|
|
try:
|
|
json.dumps(obj)
|
|
return obj
|
|
except (TypeError, ValueError):
|
|
return str(obj)
|
|
|
|
@staticmethod
|
|
def _row_to_dict(row: RunRow) -> dict[str, Any]:
|
|
d = row.to_dict()
|
|
# Remap JSON columns to match RunStore interface
|
|
d["metadata"] = d.pop("metadata_json", {})
|
|
d["kwargs"] = d.pop("kwargs_json", {})
|
|
# Convert datetime to ISO string for consistency with MemoryRunStore.
|
|
# SQLite drops tzinfo on read despite ``DateTime(timezone=True)`` —
|
|
# ``coerce_iso`` normalizes naive datetimes as UTC.
|
|
for key in ("created_at", "updated_at", "lease_expires_at"):
|
|
val = d.get(key)
|
|
if isinstance(val, datetime):
|
|
d[key] = coerce_iso(val)
|
|
return d
|
|
|
|
async def put(
|
|
self,
|
|
run_id,
|
|
*,
|
|
thread_id,
|
|
assistant_id=None,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
model_name: str | None = None,
|
|
status="pending",
|
|
multitask_strategy="reject",
|
|
metadata=None,
|
|
kwargs=None,
|
|
error=None,
|
|
created_at=None,
|
|
follow_up_to_run_id=None,
|
|
owner_worker_id: str | None = None,
|
|
lease_expires_at: str | None = None,
|
|
):
|
|
"""Insert or update a run row.
|
|
|
|
``RunManager`` retries ``put`` after transient SQLite failures. Making
|
|
this operation idempotent prevents a successful-but-unacknowledged first
|
|
commit from turning the retry into a primary-key failure.
|
|
"""
|
|
resolved_user_id = resolve_user_id(user_id, method_name="RunRepository.put")
|
|
now = datetime.now(UTC)
|
|
created = datetime.fromisoformat(created_at) if created_at else now
|
|
lease_dt = datetime.fromisoformat(lease_expires_at) if lease_expires_at else None
|
|
values = {
|
|
"thread_id": thread_id,
|
|
"assistant_id": assistant_id,
|
|
"user_id": resolved_user_id,
|
|
"model_name": self._normalize_model_name(model_name),
|
|
"status": status,
|
|
"multitask_strategy": multitask_strategy,
|
|
"metadata_json": self._safe_json(metadata) or {},
|
|
"kwargs_json": self._safe_json(kwargs) or {},
|
|
"error": error,
|
|
"follow_up_to_run_id": follow_up_to_run_id,
|
|
"owner_worker_id": owner_worker_id,
|
|
"lease_expires_at": lease_dt,
|
|
"updated_at": now,
|
|
}
|
|
async with self._sf() as session:
|
|
row = await session.get(RunRow, run_id)
|
|
if row is None:
|
|
session.add(RunRow(run_id=run_id, created_at=created, **values))
|
|
else:
|
|
for key, value in values.items():
|
|
setattr(row, key, value)
|
|
await session.commit()
|
|
|
|
async def get(
|
|
self,
|
|
run_id,
|
|
*,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
):
|
|
resolved_user_id = resolve_user_id(user_id, method_name="RunRepository.get")
|
|
async with self._sf() as session:
|
|
row = await session.get(RunRow, run_id)
|
|
if row is None:
|
|
return None
|
|
if resolved_user_id is not None and row.user_id != resolved_user_id:
|
|
return None
|
|
return self._row_to_dict(row)
|
|
|
|
async def list_by_thread(
|
|
self,
|
|
thread_id,
|
|
*,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
limit=100,
|
|
):
|
|
resolved_user_id = resolve_user_id(user_id, method_name="RunRepository.list_by_thread")
|
|
stmt = select(RunRow).where(RunRow.thread_id == thread_id)
|
|
if resolved_user_id is not None:
|
|
stmt = stmt.where(RunRow.user_id == resolved_user_id)
|
|
stmt = stmt.order_by(RunRow.created_at.desc()).limit(limit)
|
|
async with self._sf() as session:
|
|
result = await session.execute(stmt)
|
|
return [self._row_to_dict(r) for r in result.scalars()]
|
|
|
|
async def update_status(self, run_id, status, *, error=None) -> bool:
|
|
values: dict[str, Any] = {"status": status, "updated_at": datetime.now(UTC)}
|
|
if error is not None:
|
|
values["error"] = error
|
|
# Guard: only transition rows that are still active. ``interrupted`` is
|
|
# included because the rollback path goes ``running → interrupted``
|
|
# (cancel acknowledged) then ``interrupted → error`` (task finalize).
|
|
# ``error`` and ``success`` remain locked so a peer's takeover (or a
|
|
# completed run) cannot be overwritten by a late writer.
|
|
async with self._sf() as session:
|
|
result = await session.execute(update(RunRow).where(RunRow.run_id == run_id, RunRow.status.in_(("pending", "running", "interrupted"))).values(**values))
|
|
await session.commit()
|
|
return result.rowcount != 0
|
|
|
|
async def update_model_name(self, run_id, model_name):
|
|
async with self._sf() as session:
|
|
await session.execute(update(RunRow).where(RunRow.run_id == run_id).values(model_name=self._normalize_model_name(model_name), updated_at=datetime.now(UTC)))
|
|
await session.commit()
|
|
|
|
async def delete(
|
|
self,
|
|
run_id,
|
|
*,
|
|
user_id: str | None | _AutoSentinel = AUTO,
|
|
):
|
|
resolved_user_id = resolve_user_id(user_id, method_name="RunRepository.delete")
|
|
async with self._sf() as session:
|
|
row = await session.get(RunRow, run_id)
|
|
if row is None:
|
|
return
|
|
if resolved_user_id is not None and row.user_id != resolved_user_id:
|
|
return
|
|
await session.delete(row)
|
|
await session.commit()
|
|
|
|
async def list_pending(self, *, before=None):
|
|
if before is None:
|
|
before_dt = datetime.now(UTC)
|
|
elif isinstance(before, datetime):
|
|
before_dt = before
|
|
else:
|
|
before_dt = datetime.fromisoformat(before)
|
|
stmt = select(RunRow).where(RunRow.status == "pending", RunRow.created_at <= before_dt).order_by(RunRow.created_at.asc())
|
|
async with self._sf() as session:
|
|
result = await session.execute(stmt)
|
|
return [self._row_to_dict(r) for r in result.scalars()]
|
|
|
|
async def list_inflight(self, *, before=None):
|
|
"""Return persisted active runs for startup recovery."""
|
|
if before is None:
|
|
before_dt = datetime.now(UTC)
|
|
elif isinstance(before, datetime):
|
|
before_dt = before
|
|
else:
|
|
before_dt = datetime.fromisoformat(before)
|
|
stmt = (
|
|
select(RunRow)
|
|
.where(
|
|
RunRow.status.in_(("pending", "running")),
|
|
RunRow.created_at <= before_dt,
|
|
)
|
|
.order_by(RunRow.created_at.asc())
|
|
)
|
|
async with self._sf() as session:
|
|
result = await session.execute(stmt)
|
|
return [self._row_to_dict(r) for r in result.scalars()]
|
|
|
|
async def update_run_completion(
|
|
self,
|
|
run_id: str,
|
|
*,
|
|
status: str,
|
|
total_input_tokens: int = 0,
|
|
total_output_tokens: int = 0,
|
|
total_tokens: int = 0,
|
|
llm_call_count: int = 0,
|
|
lead_agent_tokens: int = 0,
|
|
subagent_tokens: int = 0,
|
|
middleware_tokens: int = 0,
|
|
token_usage_by_model: dict[str, dict[str, int]] | None = None,
|
|
message_count: int = 0,
|
|
last_ai_message: str | None = None,
|
|
first_human_message: str | None = None,
|
|
error: str | None = None,
|
|
) -> bool:
|
|
"""Update status + token usage + convenience fields on run completion.
|
|
|
|
Returns ``False`` when no run row matched the requested ``run_id``.
|
|
"""
|
|
values: dict[str, Any] = {
|
|
"status": status,
|
|
"total_input_tokens": total_input_tokens,
|
|
"total_output_tokens": total_output_tokens,
|
|
"total_tokens": total_tokens,
|
|
"llm_call_count": llm_call_count,
|
|
"lead_agent_tokens": lead_agent_tokens,
|
|
"subagent_tokens": subagent_tokens,
|
|
"middleware_tokens": middleware_tokens,
|
|
"token_usage_by_model": self._safe_json(token_usage_by_model) or {},
|
|
"message_count": message_count,
|
|
"updated_at": datetime.now(UTC),
|
|
}
|
|
if last_ai_message is not None:
|
|
values["last_ai_message"] = last_ai_message[:2000]
|
|
if first_human_message is not None:
|
|
values["first_human_message"] = first_human_message[:2000]
|
|
if error is not None:
|
|
values["error"] = error
|
|
async with self._sf() as session:
|
|
result = await session.execute(update(RunRow).where(RunRow.run_id == run_id).values(**values))
|
|
await session.commit()
|
|
return result.rowcount != 0
|
|
|
|
async def update_run_progress(
|
|
self,
|
|
run_id: str,
|
|
*,
|
|
total_input_tokens: int | None = None,
|
|
total_output_tokens: int | None = None,
|
|
total_tokens: int | None = None,
|
|
llm_call_count: int | None = None,
|
|
lead_agent_tokens: int | None = None,
|
|
subagent_tokens: int | None = None,
|
|
middleware_tokens: int | None = None,
|
|
token_usage_by_model: dict[str, dict[str, int]] | None = None,
|
|
message_count: int | None = None,
|
|
last_ai_message: str | None = None,
|
|
first_human_message: str | None = None,
|
|
) -> None:
|
|
"""Update token usage + convenience fields while a run is still active."""
|
|
values: dict[str, Any] = {"updated_at": datetime.now(UTC)}
|
|
optional_counters = {
|
|
"total_input_tokens": total_input_tokens,
|
|
"total_output_tokens": total_output_tokens,
|
|
"total_tokens": total_tokens,
|
|
"llm_call_count": llm_call_count,
|
|
"lead_agent_tokens": lead_agent_tokens,
|
|
"subagent_tokens": subagent_tokens,
|
|
"middleware_tokens": middleware_tokens,
|
|
"message_count": message_count,
|
|
}
|
|
for key, value in optional_counters.items():
|
|
if value is not None:
|
|
values[key] = value
|
|
if token_usage_by_model is not None:
|
|
values["token_usage_by_model"] = self._safe_json(token_usage_by_model) or {}
|
|
if last_ai_message is not None:
|
|
values["last_ai_message"] = last_ai_message[:2000]
|
|
if first_human_message is not None:
|
|
values["first_human_message"] = first_human_message[:2000]
|
|
async with self._sf() as session:
|
|
await session.execute(update(RunRow).where(RunRow.run_id == run_id, RunRow.status == "running").values(**values))
|
|
await session.commit()
|
|
|
|
async def aggregate_tokens_by_thread(self, thread_id: str, *, include_active: bool = False) -> dict[str, Any]:
|
|
"""Aggregate token usage for a thread.
|
|
|
|
``by_model`` is reduced in Python from each row's ``token_usage_by_model``
|
|
JSON column so subagent / middleware tokens land on the model that
|
|
actually produced them (issue #3645). Rows written before that column
|
|
existed fall back to ``RunRow.model_name`` + ``RunRow.total_tokens``,
|
|
preserving the legacy lead-only behavior instead of dropping the data.
|
|
|
|
Headline totals (``total_tokens``, ``total_input_tokens``,
|
|
``total_output_tokens``) and the ``by_caller`` bucket are summed from
|
|
their own columns and are therefore unaffected by the JSON column being
|
|
empty.
|
|
"""
|
|
statuses = ("success", "error", "running") if include_active else ("success", "error")
|
|
_completed = RunRow.status.in_(statuses)
|
|
_thread = RunRow.thread_id == thread_id
|
|
|
|
stmt = select(
|
|
RunRow.model_name,
|
|
RunRow.total_tokens,
|
|
RunRow.total_input_tokens,
|
|
RunRow.total_output_tokens,
|
|
RunRow.lead_agent_tokens,
|
|
RunRow.subagent_tokens,
|
|
RunRow.middleware_tokens,
|
|
RunRow.token_usage_by_model,
|
|
).where(_thread, _completed)
|
|
|
|
async with self._sf() as session:
|
|
rows = (await session.execute(stmt)).all()
|
|
|
|
total_tokens = total_input = total_output = total_runs = 0
|
|
lead_agent = subagent = middleware = 0
|
|
by_model: dict[str, dict] = {}
|
|
for r in rows:
|
|
total_runs += 1
|
|
total_tokens += r.total_tokens
|
|
total_input += r.total_input_tokens
|
|
total_output += r.total_output_tokens
|
|
lead_agent += r.lead_agent_tokens
|
|
subagent += r.subagent_tokens
|
|
middleware += r.middleware_tokens
|
|
|
|
# ``or {}`` covers rows written before ``token_usage_by_model``
|
|
# existed (the column is NULL on a manual ALTER ADD COLUMN without
|
|
# backfill); fresh rows always carry the journal-produced dict.
|
|
usage_by_model = r.token_usage_by_model or {}
|
|
if usage_by_model:
|
|
for model, usage in usage_by_model.items():
|
|
entry = by_model.setdefault(model, {"tokens": 0, "runs": 0})
|
|
entry["tokens"] += usage.get("total_tokens", 0)
|
|
entry["runs"] += 1
|
|
else:
|
|
model = r.model_name or "unknown"
|
|
entry = by_model.setdefault(model, {"tokens": 0, "runs": 0})
|
|
entry["tokens"] += r.total_tokens
|
|
entry["runs"] += 1
|
|
|
|
return {
|
|
"total_tokens": total_tokens,
|
|
"total_input_tokens": total_input,
|
|
"total_output_tokens": total_output,
|
|
"total_runs": total_runs,
|
|
"by_model": by_model,
|
|
"by_caller": {
|
|
"lead_agent": lead_agent,
|
|
"subagent": subagent,
|
|
"middleware": middleware,
|
|
},
|
|
}
|
|
|
|
# ------------------------------------------------------------------
|
|
# Multi-worker run ownership methods
|
|
# ------------------------------------------------------------------
|
|
|
|
async def update_lease(
|
|
self,
|
|
run_id: str,
|
|
*,
|
|
owner_worker_id: str,
|
|
lease_expires_at: str,
|
|
) -> bool:
|
|
lease_dt = datetime.fromisoformat(lease_expires_at)
|
|
values: dict[str, Any] = {
|
|
"owner_worker_id": owner_worker_id,
|
|
"lease_expires_at": lease_dt,
|
|
"updated_at": datetime.now(UTC),
|
|
}
|
|
async with self._sf() as session:
|
|
result = await session.execute(update(RunRow).where(RunRow.run_id == run_id, RunRow.owner_worker_id == owner_worker_id, RunRow.status.in_(("pending", "running"))).values(**values))
|
|
await session.commit()
|
|
return result.rowcount != 0
|
|
|
|
async def claim_for_takeover(
|
|
self,
|
|
run_id: str,
|
|
*,
|
|
grace_seconds: int,
|
|
error: str,
|
|
) -> bool:
|
|
cutoff = datetime.now(UTC) - timedelta(seconds=grace_seconds)
|
|
async with self._sf() as session:
|
|
result = await session.execute(
|
|
update(RunRow)
|
|
.where(
|
|
RunRow.run_id == run_id,
|
|
RunRow.status.in_(("pending", "running")),
|
|
_lease_expired_or_null(RunRow.lease_expires_at, cutoff),
|
|
)
|
|
.values(status="error", error=error, updated_at=datetime.now(UTC))
|
|
)
|
|
await session.commit()
|
|
return result.rowcount != 0
|
|
|
|
async def list_inflight_with_expired_lease(
|
|
self,
|
|
*,
|
|
before: str | None = None,
|
|
grace_seconds: int = 10,
|
|
) -> list[dict[str, Any]]:
|
|
if before is None:
|
|
before_dt = datetime.now(UTC)
|
|
elif isinstance(before, datetime):
|
|
before_dt = before
|
|
else:
|
|
before_dt = datetime.fromisoformat(before)
|
|
cutoff = datetime.now(UTC) - timedelta(seconds=grace_seconds)
|
|
stmt = (
|
|
select(RunRow)
|
|
.where(
|
|
RunRow.status.in_(("pending", "running")),
|
|
RunRow.created_at <= before_dt,
|
|
_lease_expired_or_null(RunRow.lease_expires_at, cutoff),
|
|
)
|
|
.order_by(RunRow.created_at.asc())
|
|
)
|
|
async with self._sf() as session:
|
|
result = await session.execute(stmt)
|
|
return [self._row_to_dict(r) for r in result.scalars()]
|
|
|
|
async def create_run_atomic(
|
|
self,
|
|
run_id: str,
|
|
*,
|
|
thread_id: str,
|
|
owner_worker_id: str,
|
|
lease_expires_at: str | None,
|
|
multitask_strategy: str = "reject",
|
|
assistant_id: str | None = None,
|
|
user_id: str | None = None,
|
|
model_name: str | None = None,
|
|
metadata: dict[str, Any] | None = None,
|
|
kwargs: dict[str, Any] | None = None,
|
|
created_at: str | None = None,
|
|
grace_seconds: int = 10,
|
|
) -> tuple[dict[str, Any], list[dict[str, Any]]]:
|
|
"""Atomically create a run with cross-process thread-uniqueness.
|
|
|
|
- For ``reject``: INSERT, let the partial unique index enforce
|
|
single-active-run. Returns ``(row_dict, [])`` on success, raises
|
|
``IntegrityError`` on conflict.
|
|
- For ``interrupt`` / ``rollback``: SELECT FOR UPDATE inflight
|
|
rows for the thread, cancel them (unless their lease is still valid),
|
|
then INSERT the new row — all in one transaction. Returns
|
|
``(row_dict, claimed_row_dicts)``.
|
|
|
|
Returns:
|
|
Tuple of ``(new_run_dict, claimed_run_dicts)``.
|
|
"""
|
|
from deerflow.runtime.runs.manager import ConflictError
|
|
|
|
resolved_user_id = resolve_user_id(user_id or AUTO, method_name="RunRepository.create_run_atomic")
|
|
now = datetime.now(UTC)
|
|
created = datetime.fromisoformat(created_at) if created_at else now
|
|
lease_dt = datetime.fromisoformat(lease_expires_at) if lease_expires_at else None
|
|
cutoff = now - timedelta(seconds=grace_seconds)
|
|
|
|
values = {
|
|
"thread_id": thread_id,
|
|
"assistant_id": assistant_id,
|
|
"user_id": resolved_user_id,
|
|
"model_name": self._normalize_model_name(model_name),
|
|
"status": "pending",
|
|
"multitask_strategy": multitask_strategy,
|
|
"metadata_json": self._safe_json(metadata) or {},
|
|
"kwargs_json": self._safe_json(kwargs) or {},
|
|
"owner_worker_id": owner_worker_id,
|
|
"lease_expires_at": lease_dt,
|
|
"created_at": created,
|
|
"updated_at": now,
|
|
}
|
|
|
|
async with self._sf() as session:
|
|
claimed: list[dict[str, Any]] = []
|
|
|
|
if multitask_strategy in ("interrupt", "rollback"):
|
|
stmt = (
|
|
select(RunRow)
|
|
.where(
|
|
RunRow.thread_id == thread_id,
|
|
RunRow.status.in_(("pending", "running")),
|
|
)
|
|
.with_for_update()
|
|
)
|
|
result = await session.execute(stmt)
|
|
for row in result.scalars():
|
|
if row.lease_expires_at is not None:
|
|
# SQLite drops tzinfo on read despite
|
|
# ``DateTime(timezone=True)`` (see ``_row_to_dict``).
|
|
# Treat naive values as UTC — same convention as
|
|
# ``coerce_iso`` — so the Python-side comparison
|
|
# against the aware ``cutoff`` does not raise
|
|
# ``TypeError: can't compare offset-naive and
|
|
# offset-aware datetimes`` when heartbeat is enabled
|
|
# on SQLite.
|
|
row_lease = row.lease_expires_at
|
|
if row_lease.tzinfo is None:
|
|
row_lease = row_lease.replace(tzinfo=UTC)
|
|
if row_lease >= cutoff and row.owner_worker_id != owner_worker_id:
|
|
# Live run owned by another worker — we cannot
|
|
# interrupt it and the partial unique index would
|
|
# reject our INSERT anyway. Surface as
|
|
# ConflictError so the caller gets a clean signal
|
|
# instead of a retry loop on IntegrityError.
|
|
raise ConflictError(f"Thread {thread_id} already has an active run owned by another worker")
|
|
row.status = "interrupted"
|
|
row.error = "Cancelled by newer run"
|
|
row.owner_worker_id = owner_worker_id
|
|
row.updated_at = now
|
|
claimed.append(self._row_to_dict(row))
|
|
|
|
session.add(RunRow(run_id=run_id, **values))
|
|
await session.commit()
|
|
|
|
new_row = await session.get(RunRow, run_id)
|
|
return self._row_to_dict(new_row), claimed
|