fix: avoid MySQL thread_meta refresh races and harden roundtable bootstrap
Skip post-commit ORM refresh on threads_meta creates so concurrent MySQL read/write-split deployments no longer fail with Could not refresh instance; clean up partial roundtable threads on checkpoint failure, and ship related position-roundtable/frontend build fixes. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
e52261fc3d
commit
a9d66536c3
12 files changed
+503
-127
No files matched your search
@@ -2,19 +2,24 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.gateway import deps as gateway_deps
|
||||
from app.gateway import services as gateway_services
|
||||
from app.gateway.app import create_app
|
||||
from app.gateway.routers import intent as intent_router
|
||||
from app.gateway.routers import multi_agent
|
||||
from app.gateway.routers import position_roundtable as position_roundtable_router
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.position_roundtable import PositionRoundtableRepository
|
||||
from deerflow.persistence.thread_meta.sql import ThreadMetaRepository
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
@@ -971,3 +976,120 @@ async def test_locked_node_cannot_bind_a_thread_before_its_stage_is_ready():
|
||||
)
|
||||
assert error.value.status_code == 409
|
||||
assert "未解锁" in str(error.value.detail)
|
||||
|
||||
|
||||
class _ThreadMetaSessionProbe:
|
||||
def __init__(self, events: list[str]) -> None:
|
||||
self._events = events
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||
return None
|
||||
|
||||
def add(self, row) -> None:
|
||||
self._events.append(f"add:{row.thread_id}")
|
||||
|
||||
async def commit(self) -> None:
|
||||
self._events.append("commit")
|
||||
|
||||
async def refresh(self, row) -> None:
|
||||
self._events.append(f"refresh:{row.thread_id}")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_thread_meta_create_never_reads_back_after_commit():
|
||||
"""No caller may depend on a post-commit re-SELECT of the new row.
|
||||
|
||||
On MySQL behind a read/write-splitting endpoint that read can land on a
|
||||
lagging replica, come back empty, and raise "Could not refresh instance".
|
||||
"""
|
||||
|
||||
events: list[str] = []
|
||||
repo = ThreadMetaRepository(lambda: _ThreadMetaSessionProbe(events)) # type: ignore[arg-type]
|
||||
|
||||
direct = await repo.create(
|
||||
"roundtable-thread",
|
||||
user_id=None,
|
||||
metadata={"thread_type": "roundtable"},
|
||||
)
|
||||
ordinary = await repo.create("ordinary-thread", user_id=None, display_name="对话")
|
||||
|
||||
assert events == [
|
||||
"add:roundtable-thread",
|
||||
"commit",
|
||||
"add:ordinary-thread",
|
||||
"commit",
|
||||
]
|
||||
# The returned dict is assembled from the in-memory row, so it must still be
|
||||
# complete for every column -- including ones no caller passed in.
|
||||
assert direct["status"] == "idle"
|
||||
assert direct["metadata"] == {"thread_type": "roundtable"}
|
||||
assert direct["created_at"] and direct["updated_at"]
|
||||
assert ordinary["status"] == "idle"
|
||||
assert ordinary["display_name"] == "对话"
|
||||
|
||||
|
||||
class _ThreadStoreProbe:
|
||||
def __init__(self) -> None:
|
||||
self.created: list[tuple[str, dict[str, Any]]] = []
|
||||
self.deleted: list[str] = []
|
||||
|
||||
async def create(self, thread_id: str, **kwargs) -> dict[str, Any]:
|
||||
self.created.append((thread_id, kwargs))
|
||||
return {"thread_id": thread_id}
|
||||
|
||||
async def delete(self, thread_id: str) -> None:
|
||||
self.deleted.append(thread_id)
|
||||
|
||||
|
||||
class _FailingCheckpointer:
|
||||
def __init__(self, exc: BaseException) -> None:
|
||||
self._exc = exc
|
||||
self.deleted: list[str] = []
|
||||
|
||||
async def aput(self, *args, **kwargs):
|
||||
raise self._exc
|
||||
|
||||
async def adelete_thread(self, thread_id: str) -> None:
|
||||
self.deleted.append(thread_id)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_direct_thread_creation_cleans_failed_checkpoint(monkeypatch):
|
||||
store = _ThreadStoreProbe()
|
||||
checkpointer = _FailingCheckpointer(RuntimeError("checkpoint unavailable"))
|
||||
monkeypatch.setattr(gateway_deps, "get_thread_store", lambda request: store)
|
||||
monkeypatch.setattr(gateway_deps, "get_checkpointer", lambda request: checkpointer)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await multi_agent._create_thread_direct(
|
||||
SimpleNamespace(),
|
||||
metadata={"thread_type": "roundtable"},
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 500
|
||||
assert len(store.created) == 1
|
||||
thread_id, create_kwargs = store.created[0]
|
||||
assert create_kwargs["metadata"] == {"thread_type": "roundtable"}
|
||||
assert store.deleted == [thread_id]
|
||||
assert checkpointer.deleted == [thread_id]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_direct_thread_creation_cleans_metadata_when_cancelled(monkeypatch):
|
||||
store = _ThreadStoreProbe()
|
||||
checkpointer = _FailingCheckpointer(asyncio.CancelledError())
|
||||
monkeypatch.setattr(gateway_deps, "get_thread_store", lambda request: store)
|
||||
monkeypatch.setattr(gateway_deps, "get_checkpointer", lambda request: checkpointer)
|
||||
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await multi_agent._create_thread_direct(
|
||||
SimpleNamespace(),
|
||||
metadata={"thread_type": "roundtable"},
|
||||
)
|
||||
|
||||
thread_id, _ = store.created[0]
|
||||
assert store.deleted == [thread_id]
|
||||
assert checkpointer.deleted == [thread_id]
|
||||
Reference in new issue
Block a user