mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-13 12:13:46 +00:00
* feat(run): propagate model_name from gateway request context to persistence layer Pass model_name through the full run creation pipeline — from RunCreateRequest.context in the gateway, through RunManager, to the RunStore interface and SQL persistence. This enables client-specified model selection to be recorded per-run in the database. * feat(run): add model allowlist validation and effective model name capture - Validate model_name against allowlist in gateway services.py using get_app_config().get_model_config() - Truncate model_name to 128 chars to match DB column constraint - In worker.py, capture effective model name from agent.metadata after agent creation and persist if resolved differently than requested * feat(run): add defense-in-depth model_name normalization and round-trip persistence tests - Add _normalize_model_name() to RunRepository for whitespace stripping and 128-char truncation before DB writes. - Add round-trip unit tests for model_name creation and default None in test_run_manager.py. * fix(run): coerce non-string model_name values before strip/truncate in _normalize_model_name * fix(gateway): add runtime type guard for model_name coercion in gateway services Add isinstance check and str() coercion before calling .strip() to prevent AttributeError when non-string types (int, None, etc.) flow through the gateway. Paired with SQL integration test for end-to-end model_name persistence across gateway → langgraph → persistence layer. * fix(run): drop Alembic migration for model_name (no-op) and expose public update method on RunManager - Drop a1b2c3d4e5f6 migration: model_name already exists in RunRow schema and is auto-created via Base.metadata.create_all() at startup - Add update_model_name() public method to RunManager to replace the private _persist_to_store call in worker.py, preserving internal locking/persistence
195 lines
6.2 KiB
Python
195 lines
6.2 KiB
Python
"""Tests for RunManager."""
|
|
|
|
import re
|
|
|
|
import pytest
|
|
|
|
from deerflow.runtime import RunManager, RunStatus
|
|
from deerflow.runtime.runs.store.memory import MemoryRunStore
|
|
|
|
ISO_RE = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}")
|
|
|
|
|
|
@pytest.fixture
|
|
def manager() -> RunManager:
|
|
return RunManager()
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_create_and_get(manager: RunManager):
|
|
"""Created run should be retrievable with new fields."""
|
|
record = await manager.create(
|
|
"thread-1",
|
|
"lead_agent",
|
|
metadata={"key": "val"},
|
|
kwargs={"input": {}},
|
|
multitask_strategy="reject",
|
|
)
|
|
assert record.status == RunStatus.pending
|
|
assert record.thread_id == "thread-1"
|
|
assert record.assistant_id == "lead_agent"
|
|
assert record.metadata == {"key": "val"}
|
|
assert record.kwargs == {"input": {}}
|
|
assert record.multitask_strategy == "reject"
|
|
assert ISO_RE.match(record.created_at)
|
|
assert ISO_RE.match(record.updated_at)
|
|
|
|
fetched = manager.get(record.run_id)
|
|
assert fetched is record
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_status_transitions(manager: RunManager):
|
|
"""Status should transition pending -> running -> success."""
|
|
record = await manager.create("thread-1")
|
|
assert record.status == RunStatus.pending
|
|
|
|
await manager.set_status(record.run_id, RunStatus.running)
|
|
assert record.status == RunStatus.running
|
|
assert ISO_RE.match(record.updated_at)
|
|
|
|
await manager.set_status(record.run_id, RunStatus.success)
|
|
assert record.status == RunStatus.success
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_cancel(manager: RunManager):
|
|
"""Cancel should set abort_event and transition to interrupted."""
|
|
record = await manager.create("thread-1")
|
|
await manager.set_status(record.run_id, RunStatus.running)
|
|
|
|
cancelled = await manager.cancel(record.run_id)
|
|
assert cancelled is True
|
|
assert record.abort_event.is_set()
|
|
assert record.status == RunStatus.interrupted
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_cancel_not_inflight(manager: RunManager):
|
|
"""Cancelling a completed run should return False."""
|
|
record = await manager.create("thread-1")
|
|
await manager.set_status(record.run_id, RunStatus.success)
|
|
|
|
cancelled = await manager.cancel(record.run_id)
|
|
assert cancelled is False
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_list_by_thread(manager: RunManager):
|
|
"""Same thread should return multiple runs."""
|
|
r1 = await manager.create("thread-1")
|
|
r2 = await manager.create("thread-1")
|
|
await manager.create("thread-2")
|
|
|
|
runs = await manager.list_by_thread("thread-1")
|
|
assert len(runs) == 2
|
|
assert runs[0].run_id == r1.run_id
|
|
assert runs[1].run_id == r2.run_id
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_list_by_thread_is_stable_when_timestamps_tie(manager: RunManager, monkeypatch: pytest.MonkeyPatch):
|
|
"""Ordering should be stable (insertion order) even when timestamps tie."""
|
|
monkeypatch.setattr("deerflow.runtime.runs.manager._now_iso", lambda: "2026-01-01T00:00:00+00:00")
|
|
|
|
r1 = await manager.create("thread-1")
|
|
r2 = await manager.create("thread-1")
|
|
|
|
runs = await manager.list_by_thread("thread-1")
|
|
assert [run.run_id for run in runs] == [r1.run_id, r2.run_id]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_has_inflight(manager: RunManager):
|
|
"""has_inflight should be True when a run is pending or running."""
|
|
record = await manager.create("thread-1")
|
|
assert await manager.has_inflight("thread-1") is True
|
|
|
|
await manager.set_status(record.run_id, RunStatus.success)
|
|
assert await manager.has_inflight("thread-1") is False
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_cleanup(manager: RunManager):
|
|
"""After cleanup, the run should be gone."""
|
|
record = await manager.create("thread-1")
|
|
run_id = record.run_id
|
|
|
|
await manager.cleanup(run_id, delay=0)
|
|
assert manager.get(run_id) is None
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_set_status_with_error(manager: RunManager):
|
|
"""Error message should be stored on the record."""
|
|
record = await manager.create("thread-1")
|
|
await manager.set_status(record.run_id, RunStatus.error, error="Something went wrong")
|
|
assert record.status == RunStatus.error
|
|
assert record.error == "Something went wrong"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_get_nonexistent(manager: RunManager):
|
|
"""Getting a nonexistent run should return None."""
|
|
assert manager.get("does-not-exist") is None
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_create_defaults(manager: RunManager):
|
|
"""Create with no optional args should use defaults."""
|
|
record = await manager.create("thread-1")
|
|
assert record.metadata == {}
|
|
assert record.kwargs == {}
|
|
assert record.multitask_strategy == "reject"
|
|
assert record.assistant_id is None
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_model_name_create_or_reject():
|
|
"""create_or_reject should accept and persist model_name."""
|
|
from deerflow.runtime.runs.schemas import DisconnectMode
|
|
|
|
store = MemoryRunStore()
|
|
mgr = RunManager(store=store)
|
|
|
|
record = await mgr.create_or_reject(
|
|
"thread-1",
|
|
assistant_id="lead_agent",
|
|
on_disconnect=DisconnectMode.cancel,
|
|
metadata={"key": "val"},
|
|
kwargs={"input": {}},
|
|
multitask_strategy="reject",
|
|
model_name="anthropic.claude-sonnet-4-20250514-v1:0",
|
|
)
|
|
assert record.model_name == "anthropic.claude-sonnet-4-20250514-v1:0"
|
|
assert record.status == RunStatus.pending
|
|
|
|
# Verify model_name was persisted to store
|
|
stored = await store.get(record.run_id)
|
|
assert stored is not None
|
|
assert stored["model_name"] == "anthropic.claude-sonnet-4-20250514-v1:0"
|
|
|
|
# Verify retrieval returns the model_name via in-memory record
|
|
fetched = mgr.get(record.run_id)
|
|
assert fetched is not None
|
|
assert fetched.model_name == "anthropic.claude-sonnet-4-20250514-v1:0"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_model_name_default_is_none():
|
|
"""create_or_reject without model_name should default to None."""
|
|
from deerflow.runtime.runs.schemas import DisconnectMode
|
|
|
|
store = MemoryRunStore()
|
|
mgr = RunManager(store=store)
|
|
|
|
record = await mgr.create_or_reject(
|
|
"thread-1",
|
|
on_disconnect=DisconnectMode.cancel,
|
|
model_name=None,
|
|
)
|
|
assert record.model_name is None
|
|
|
|
stored = await store.get(record.run_id)
|
|
assert stored["model_name"] is None
|