c810e9f809
* fix(harness): hydrate run history from RunStore and persist cancellation status fix: - Make RunManager.get() async and hydrate from RunStore when in-memory record is missing - Merge store rows into list_by_thread() with in-memory precedence for active runs - Persist interrupted status to RunStore in cancel() and create_or_reject(interrupt|rollback) - Extract _persist_status() to reuse the best-effort store update pattern - Await run_mgr.get() in all gateway endpoints - Return 409 with distinct message for store-only runs not active on current worker Closes #2812, Closes #2813 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(harness): consistent sort and guarded hydration in RunManager fix: - list_by_thread() now sorts by created_at desc (newest first) even when no RunStore is configured, matching the store-backed code path - guard _record_from_store() call sites in get() and list_by_thread() with best-effort error handling so a single malformed store row cannot turn read paths into 500s test: - update test_list_by_thread assertion to expect newest-first order - seed MemoryRunStore via public put() API instead of writing to _runs * fix(harness): guard store-only runs from streaming and fix get() TOCTOU Add RunRecord.store_only flag set by _record_from_store so callers can distinguish hydrated history from live in-memory runs. join_run and stream_existing_run (action=None) now return 409 instead of hanging forever on an empty MemoryStreamBridge channel. Re-check _runs under lock after the store await in RunManager.get() so a concurrent create() that lands between the two checks returns the authoritative in-memory record rather than a stale store-hydrated copy. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com> * fix(harness): reorder bridge fetch in join_run and make list_by_thread limit explicit Move get_stream_bridge() after the store_only guard in join_run so a missing bridge cannot produce 503 for historical runs before the 409 guard fires. Add limit parameter to RunManager.list_by_thread (default 100, matching the store's page size) and pass it explicitly to the store call. Update docstring to document the limit instead of claiming all runs are returned. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com> * fix(harness): cap list_by_thread result to limit after merge Apply [:limit] to all return paths in list_by_thread so the method consistently returns at most limit records regardless of how many in-memory runs exist, making the limit parameter a true upper bound on the response size rather than just a store-query hint. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com> * fix `list_by_thread` docstring Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * fix(runtime): add update_model_name to RunStore to prevent SQL integrity errors RunManager.update_model_name() was calling _persist_to_store() which uses RunStore.put(), but RunRepository.put() is insert-only. This caused integrity errors when updating model_name for existing runs in SQL-backed stores. fix: - Add abstract update_model_name method to RunStore base class - Implement update_model_name in MemoryRunStore - Implement update_model_name in RunRepository with proper normalization - Add _persist_model_name helper in RunManager - Update RunManager.update_model_name to use the new method test: - Add tests for update_model_name functionality - Add integration tests for RunManager with SQL-backed store Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(runtime): handle NULL status/on_disconnect in _record_from_store `dict.get(key, default)` only uses the default when the key is absent, so a SQL row with an explicit NULL status would pass `None` to `RunStatus(None)` and raise, breaking hydration for otherwise valid rows. Switch to `row.get(...) or fallback` so both missing and NULL values get a safe default. Add tests for get() and list_by_thread() with a NULL status row to prevent regression. Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com> * fix(runs): address PR review feedback on store consistency changes - Fix list_by_thread limit semantics: pass store_limit = max(0, limit - len(memory_records)) to store so newer store records are not crowded out by in-memory records - Remove dead code: cancelled guard after raise is always True, simplify to if wait and record.task - Document _record_from_store NULL fallback policy (status→pending, on_disconnect→cancel) in docstring Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
196 lines
6.9 KiB
Python
196 lines
6.9 KiB
Python
"""Tests for paginated GET /api/threads/{thread_id}/runs/{run_id}/messages endpoint."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
from _router_auth_helpers import make_authed_test_app
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.gateway.routers import thread_runs
|
|
from deerflow.runtime import RunManager
|
|
from deerflow.runtime.runs.store.memory import MemoryRunStore
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _make_app(event_store=None, run_manager=None):
|
|
"""Build a test FastAPI app with stub auth and mocked state."""
|
|
app = make_authed_test_app()
|
|
app.include_router(thread_runs.router)
|
|
|
|
if event_store is not None:
|
|
app.state.run_event_store = event_store
|
|
if run_manager is not None:
|
|
app.state.run_manager = run_manager
|
|
|
|
return app
|
|
|
|
|
|
def _make_event_store(rows: list[dict]):
|
|
"""Return an AsyncMock event store whose list_messages_by_run() returns rows."""
|
|
store = MagicMock()
|
|
store.list_messages_by_run = AsyncMock(return_value=rows)
|
|
return store
|
|
|
|
|
|
def _make_message(seq: int) -> dict:
|
|
return {"seq": seq, "event_type": "ai_message", "category": "message", "content": f"msg-{seq}"}
|
|
|
|
|
|
def _make_store_only_run_manager() -> RunManager:
|
|
store = MemoryRunStore()
|
|
asyncio.run(
|
|
store.put(
|
|
"store-only-run",
|
|
thread_id="thread-store",
|
|
assistant_id="lead_agent",
|
|
status="running",
|
|
multitask_strategy="reject",
|
|
metadata={},
|
|
kwargs={},
|
|
created_at="2026-01-01T00:00:00+00:00",
|
|
)
|
|
)
|
|
return RunManager(store=store)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Tests
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def test_returns_paginated_envelope():
|
|
"""GET /api/threads/{tid}/runs/{rid}/messages returns {data: [...], has_more: bool}."""
|
|
rows = [_make_message(i) for i in range(1, 4)]
|
|
app = _make_app(event_store=_make_event_store(rows))
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-1/runs/run-1/messages")
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert "data" in body
|
|
assert "has_more" in body
|
|
assert body["has_more"] is False
|
|
assert len(body["data"]) == 3
|
|
|
|
|
|
def test_has_more_true_when_extra_row_returned():
|
|
"""has_more=True when event store returns limit+1 rows."""
|
|
# Default limit is 50; provide 51 rows
|
|
rows = [_make_message(i) for i in range(1, 52)] # 51 rows
|
|
app = _make_app(event_store=_make_event_store(rows))
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-2/runs/run-2/messages")
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["has_more"] is True
|
|
assert len(body["data"]) == 50 # trimmed to limit
|
|
|
|
|
|
def test_after_seq_forwarded_to_event_store():
|
|
"""after_seq query param is forwarded to event_store.list_messages_by_run."""
|
|
rows = [_make_message(10)]
|
|
event_store = _make_event_store(rows)
|
|
app = _make_app(event_store=event_store)
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-3/runs/run-3/messages?after_seq=5")
|
|
assert response.status_code == 200
|
|
event_store.list_messages_by_run.assert_awaited_once_with(
|
|
"thread-3",
|
|
"run-3",
|
|
limit=51, # default limit(50) + 1
|
|
before_seq=None,
|
|
after_seq=5,
|
|
)
|
|
|
|
|
|
def test_before_seq_forwarded_to_event_store():
|
|
"""before_seq query param is forwarded to event_store.list_messages_by_run."""
|
|
rows = [_make_message(3)]
|
|
event_store = _make_event_store(rows)
|
|
app = _make_app(event_store=event_store)
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-4/runs/run-4/messages?before_seq=10")
|
|
assert response.status_code == 200
|
|
event_store.list_messages_by_run.assert_awaited_once_with(
|
|
"thread-4",
|
|
"run-4",
|
|
limit=51,
|
|
before_seq=10,
|
|
after_seq=None,
|
|
)
|
|
|
|
|
|
def test_custom_limit_forwarded_to_event_store():
|
|
"""Custom limit is forwarded as limit+1 to the event store."""
|
|
rows = [_make_message(i) for i in range(1, 6)]
|
|
event_store = _make_event_store(rows)
|
|
app = _make_app(event_store=event_store)
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-5/runs/run-5/messages?limit=10")
|
|
assert response.status_code == 200
|
|
event_store.list_messages_by_run.assert_awaited_once_with(
|
|
"thread-5",
|
|
"run-5",
|
|
limit=11, # 10 + 1
|
|
before_seq=None,
|
|
after_seq=None,
|
|
)
|
|
|
|
|
|
def test_empty_data_when_no_messages():
|
|
"""Returns empty data list with has_more=False when no messages exist."""
|
|
app = _make_app(event_store=_make_event_store([]))
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-6/runs/run-6/messages")
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["data"] == []
|
|
assert body["has_more"] is False
|
|
|
|
|
|
def test_get_run_hydrates_store_only_run():
|
|
"""GET /api/threads/{tid}/runs/{rid} should read historical store rows."""
|
|
app = _make_app(run_manager=_make_store_only_run_manager())
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-store/runs/store-only-run")
|
|
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["run_id"] == "store-only-run"
|
|
assert body["thread_id"] == "thread-store"
|
|
assert body["status"] == "running"
|
|
|
|
|
|
def test_cancel_store_only_run_returns_409():
|
|
"""Store-only runs are readable but not cancellable by this worker."""
|
|
app = _make_app(run_manager=_make_store_only_run_manager())
|
|
with TestClient(app) as client:
|
|
response = client.post("/api/threads/thread-store/runs/store-only-run/cancel")
|
|
|
|
assert response.status_code == 409
|
|
assert "not active on this worker" in response.json()["detail"]
|
|
|
|
|
|
def test_join_store_only_run_returns_409():
|
|
"""join endpoint should return 409 for store-only runs (no local stream state)."""
|
|
app = _make_app(run_manager=_make_store_only_run_manager())
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-store/runs/store-only-run/join")
|
|
|
|
assert response.status_code == 409
|
|
assert "not active on this worker" in response.json()["detail"]
|
|
|
|
|
|
def test_stream_store_only_run_returns_409():
|
|
"""stream endpoint (action=None) should return 409 for store-only runs."""
|
|
app = _make_app(run_manager=_make_store_only_run_manager())
|
|
with TestClient(app) as client:
|
|
response = client.get("/api/threads/thread-store/runs/store-only-run/stream")
|
|
|
|
assert response.status_code == 409
|
|
assert "not active on this worker" in response.json()["detail"]
|