Merge remote-tracking branch 'origin/master' into ji

This commit is contained in:
吉鑫升 committed 2026-06-11 16:39:30 +08:00
commit 5daa4b3e20
62 files changed
+4230 -1025

No files matched your search

@@ -0,0 +1,156 @@
"""Tests that the curator scheduler does not block application startup.
Verifies:
1. _curator_scheduler_loop is launched as a non-blocking asyncio task.
2. The scheduler sleeps for the startup delay before doing any work.
3. maybe_run_curator is NOT called during the startup delay window.
4. When the lock is unavailable the loop exits immediately without calling the curator.
NOTE: ``import app.gateway.app as m`` resolves to the FastAPI *instance* because
``app/gateway/__init__.py`` re-exports ``app = FastAPI(...)``. The actual module
object lives in ``sys.modules["app.gateway.app"]`` and is used here via a helper.
"""
from __future__ import annotations
import asyncio
import sys
from unittest.mock import MagicMock, patch
def _gateway_module():
"""Return the real app.gateway.app *module* (not the FastAPI instance)."""
import importlib
importlib.import_module("app.gateway.app") # side-effect: populates sys.modules
return sys.modules["app.gateway.app"]
_STARTUP_DELAY = 300 # matches _CURATOR_STARTUP_DELAY_SECONDS in app.gateway.app
_CHECK_INTERVAL = 3600 # matches _CURATOR_CHECK_INTERVAL_SECONDS
def test_scheduler_does_not_block_startup():
"""create_task must return immediately — startup must not await the loop body."""
mod = _gateway_module()
curator_called = False
async def fake_sleep(_delay):
pass
async def fake_curator():
nonlocal curator_called
curator_called = True
async def run():
with (
patch.object(mod, "_acquire_singleton_lock", return_value=MagicMock()),
patch.object(mod, "_release_singleton_lock"),
patch.object(mod, "_CURATOR_STARTUP_DELAY_SECONDS", _STARTUP_DELAY),
patch.object(mod, "_CURATOR_CHECK_INTERVAL_SECONDS", _CHECK_INTERVAL),
patch("deerflow.skills.curator.maybe_run_curator", fake_curator),
patch("asyncio.sleep", fake_sleep),
):
task = asyncio.create_task(mod._curator_scheduler_loop())
# create_task must return immediately; the coroutine is still pending
assert not task.done(), "task should be running in the background, not finished"
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
asyncio.run(run())
assert not curator_called, "curator must not fire before the startup delay completes"
def test_scheduler_sleeps_startup_delay_before_first_curator_call():
"""The very first asyncio.sleep must use the startup-delay value (300 s)."""
mod = _gateway_module()
sleep_calls: list[float] = []
curator_called = False
async def fake_sleep(delay):
sleep_calls.append(delay)
raise asyncio.CancelledError # stop after recording first sleep
async def fake_curator():
nonlocal curator_called
curator_called = True
async def run():
with (
patch.object(mod, "_acquire_singleton_lock", return_value=MagicMock()),
patch.object(mod, "_release_singleton_lock"),
patch.object(mod, "_CURATOR_STARTUP_DELAY_SECONDS", _STARTUP_DELAY),
patch.object(mod, "_CURATOR_CHECK_INTERVAL_SECONDS", _CHECK_INTERVAL),
patch("deerflow.skills.curator.maybe_run_curator", fake_curator),
patch("asyncio.sleep", fake_sleep),
):
task = asyncio.create_task(mod._curator_scheduler_loop())
try:
await task
except asyncio.CancelledError:
pass
asyncio.run(run())
assert sleep_calls, "asyncio.sleep was never called"
assert sleep_calls[0] == _STARTUP_DELAY, (
f"first sleep must be the startup delay ({_STARTUP_DELAY}s), got {sleep_calls[0]}"
)
assert not curator_called, "curator must not be called before startup delay elapses"
def test_scheduler_actually_calls_curator_after_startup_delay():
"""After the startup delay elapses, maybe_run_curator must be called."""
mod = _gateway_module()
curator_call_count = 0
async def fake_sleep(*_):
pass # instant — simulate time passing
async def fake_curator():
nonlocal curator_call_count
curator_call_count += 1
# After first call cancel the loop so the test terminates
raise asyncio.CancelledError
async def run():
with (
patch.object(mod, "_acquire_singleton_lock", return_value=MagicMock()),
patch.object(mod, "_release_singleton_lock"),
patch.object(mod, "_CURATOR_STARTUP_DELAY_SECONDS", _STARTUP_DELAY),
patch.object(mod, "_CURATOR_CHECK_INTERVAL_SECONDS", _CHECK_INTERVAL),
patch("deerflow.skills.curator.maybe_run_curator", fake_curator),
patch("asyncio.sleep", fake_sleep),
):
task = asyncio.create_task(mod._curator_scheduler_loop())
try:
await task
except asyncio.CancelledError:
pass
asyncio.run(run())
assert curator_call_count >= 1, (
f"maybe_run_curator should have been called at least once, got {curator_call_count}"
)
def test_scheduler_skips_when_lock_unavailable():
"""When another worker holds the lock the loop must exit without doing any work."""
mod = _gateway_module()
curator_called = False
async def fake_curator():
nonlocal curator_called
curator_called = True
async def run():
with (
patch.object(mod, "_acquire_singleton_lock", return_value=None),
patch("deerflow.skills.curator.maybe_run_curator", fake_curator),
):
await mod._curator_scheduler_loop()
asyncio.run(run())
assert not curator_called, "curator must not run when lock is not acquired"
@@ -0,0 +1,108 @@
"""Tests for SkillReviewMiddleware."""
from unittest.mock import MagicMock, patch
from langchain_core.messages import AIMessage, HumanMessage
from deerflow.agents.middlewares.skill_review_middleware import SkillReviewMiddleware
def _make_state(messages):
state = MagicMock()
state.get.side_effect = lambda key, default=None: messages if key == "messages" else default
return state
def _make_runtime(thread_id="thread-abc"):
runtime = MagicMock()
runtime.context = {"thread_id": thread_id} if thread_id else {}
return runtime
def test_after_agent_calls_maybe_start_review():
"""When enabled and tool calls exist, maybe_start_review is called with correct thread_id."""
middleware = SkillReviewMiddleware()
ai_msg = MagicMock(spec=AIMessage)
tc = MagicMock()
tc.name = "bash"
ai_msg.tool_calls = [tc]
state = _make_state([HumanMessage(content="hi"), ai_msg])
runtime = _make_runtime("thread-xyz")
app_cfg = MagicMock()
with patch("deerflow.agents.middlewares.skill_review_middleware.get_app_config", return_value=app_cfg):
with patch("deerflow.agents.middlewares.skill_review_middleware.maybe_start_review") as mock_review:
middleware.after_agent(state, runtime)
mock_review.assert_called_once()
call_args = mock_review.call_args
assert call_args[0][0] == "thread-xyz" # thread_id
assert call_args[0][2] is app_cfg # app_config
def test_after_agent_no_thread_id_skips_review():
"""When thread_id is missing, maybe_start_review is NOT called."""
middleware = SkillReviewMiddleware()
state = _make_state([HumanMessage(content="hi")])
runtime = _make_runtime("")
app_cfg = MagicMock()
with patch("deerflow.agents.middlewares.skill_review_middleware.get_app_config", return_value=app_cfg):
with patch("deerflow.agents.middlewares.skill_review_middleware.maybe_start_review") as mock_review:
middleware.after_agent(state, runtime)
mock_review.assert_not_called()
def test_after_agent_exception_is_swallowed():
"""Exceptions in after_agent must not propagate."""
middleware = SkillReviewMiddleware()
state = _make_state([HumanMessage(content="hi")])
runtime = _make_runtime("thread-err")
with patch("deerflow.agents.middlewares.skill_review_middleware.get_app_config", side_effect=RuntimeError("boom")):
# Must not raise
middleware.after_agent(state, runtime)
def test_skill_review_middleware_in_chain_when_enabled():
"""SkillReviewMiddleware appears in middleware chain before LoopDetectionMiddleware when enabled."""
from unittest.mock import patch, MagicMock
from deerflow.agents.lead_agent.agent import _build_middlewares
from deerflow.agents.middlewares.skill_review_middleware import SkillReviewMiddleware
config = {
"configurable": {
"thread_id": "test",
"is_plan_mode": False,
"is_bootstrap": False,
"is_scheduled_run": False,
}
}
app_cfg = MagicMock()
app_cfg.skill_evolution.enabled = True
app_cfg.skill_evolution.review_worker.enabled = True
app_cfg.token_usage.enabled = False
app_cfg.tool_search.enabled = False
app_cfg.skill_stop.enabled = False
app_cfg.summarization.enabled = False
app_cfg.memory = MagicMock()
app_cfg.guardrails.enabled = False
app_cfg.title = MagicMock()
app_cfg.title.model_name = None
with patch("deerflow.agents.lead_agent.agent.get_app_config", return_value=app_cfg):
with patch("deerflow.agents.lead_agent.agent.build_lead_runtime_middlewares", return_value=[]):
with patch("deerflow.agents.lead_agent.agent._create_summarization_middleware", return_value=None):
with patch("deerflow.agents.lead_agent.agent._create_todo_list_middleware", return_value=None):
with patch("deerflow.models.vision.vision_enabled", return_value=False):
middlewares = _build_middlewares(config, model_name=None, app_config=app_cfg)
type_names = [type(m).__name__ for m in middlewares]
assert "SkillReviewMiddleware" in type_names, f"SkillReviewMiddleware not in chain: {type_names}"
review_idx = type_names.index("SkillReviewMiddleware")
loop_idx = type_names.index("LoopDetectionMiddleware")
assert review_idx < loop_idx, f"SkillReviewMiddleware ({review_idx}) must be before LoopDetectionMiddleware ({loop_idx})"
@@ -0,0 +1,102 @@
from deerflow.config.skill_evolution_config import SkillEvolutionConfig
def test_review_worker_config_defaults():
cfg = SkillEvolutionConfig()
assert cfg.review_worker.enabled is False
assert cfg.review_worker.min_tool_calls == 5
assert cfg.review_worker.timeout_seconds == 120
assert cfg.review_worker.model_name is None
assert cfg.review_worker.max_messages == 60
def test_review_worker_config_from_dict():
cfg = SkillEvolutionConfig.model_validate({
"enabled": True,
"review_worker": {"enabled": True, "min_tool_calls": 5, "model_name": "my-model"},
})
assert cfg.review_worker.enabled is True
assert cfg.review_worker.min_tool_calls == 5
assert cfg.review_worker.model_name == "my-model"
from unittest.mock import AsyncMock, MagicMock, patch
import asyncio
import pytest
from langchain_core.messages import HumanMessage
def _make_ai_message(tool_calls_data: list[dict]):
msg = MagicMock()
msg.__class__.__name__ = "AIMessage"
tcs = []
for tc in tool_calls_data:
m = MagicMock()
m.name = tc["name"]
m.args = tc.get("args", {})
tcs.append(m)
msg.tool_calls = tcs
return msg
def test_extract_tool_call_count_counts_all():
from deerflow.skills.review_worker import extract_tool_call_count
msgs = [
HumanMessage(content="do something"),
_make_ai_message([{"name": "bash"}, {"name": "read_file"}]),
_make_ai_message([{"name": "write_file"}]),
]
assert extract_tool_call_count(msgs) == 3
def test_extract_tool_call_count_empty():
from deerflow.skills.review_worker import extract_tool_call_count
assert extract_tool_call_count([]) == 0
def test_maybe_start_review_disabled_returns_false():
from deerflow.skills.review_worker import maybe_start_review
cfg = MagicMock()
cfg.skill_evolution.enabled = False
cfg.skill_evolution.review_worker.enabled = True
cfg.skill_evolution.review_worker.min_tool_calls = 2
msgs = [_make_ai_message([{"name": "bash"}, {"name": "read_file"}, {"name": "write_file"}])]
assert maybe_start_review("thread-1", msgs, cfg) is False
def test_maybe_start_review_too_few_tool_calls_returns_false():
from deerflow.skills.review_worker import maybe_start_review
cfg = MagicMock()
cfg.skill_evolution.enabled = True
cfg.skill_evolution.review_worker.enabled = True
cfg.skill_evolution.review_worker.min_tool_calls = 5
msgs = [_make_ai_message([{"name": "bash"}])]
assert maybe_start_review("thread-4", msgs, cfg) is False
def test_maybe_start_review_starts_task():
from deerflow.skills import review_worker as rw
# Reset module state so thread-3 has no lock
rw._locks.pop("thread-3", None)
rw._pending.pop("thread-3", None)
cfg = MagicMock()
cfg.skill_evolution.enabled = True
cfg.skill_evolution.review_worker.enabled = True
cfg.skill_evolution.review_worker.min_tool_calls = 2
cfg.skill_evolution.review_worker.timeout_seconds = 30
cfg.skill_evolution.review_worker.max_messages = 20
cfg.skill_evolution.review_worker.model_name = None
msgs = [_make_ai_message([{"name": "bash"}, {"name": "read_file"}, {"name": "write_file"}])]
with patch("deerflow.skills.review_worker.asyncio") as mock_asyncio:
fake_lock = MagicMock()
fake_lock.locked.return_value = False
mock_asyncio.Lock.return_value = fake_lock
mock_asyncio.create_task = MagicMock()
result = rw.maybe_start_review("thread-3", msgs, cfg)
assert result is True
mock_asyncio.create_task.assert_called_once()