sync update from LAN repo (source master @ ca48c4ec)
This commit is contained in:
1 parent
8bc8b0e082
commit
ea9dcff9c0
873 files changed
+107866
-14011
No files matched your search
+458
@@ -0,0 +1,458 @@
|
||||
"""Slow agent initialization must not monopolize the gateway event loop."""
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextvars import ContextVar
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.runtime.runs import worker
|
||||
from deerflow.runtime.runs.manager import RunManager
|
||||
from deerflow.runtime.runs.schemas import RunStatus
|
||||
from deerflow.runtime.stream_bridge.memory import MemoryStreamBridge
|
||||
from deerflow.tools.builtins.tool_search import DeferredToolRegistry, get_deferred_registry, reset_deferred_registry, set_deferred_registry
|
||||
from deerflow.utils import background_init
|
||||
from deerflow.utils.file_io import FileIOPool
|
||||
|
||||
|
||||
def _background(factory):
|
||||
factory._deerflow_background_build = True
|
||||
return factory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def build_pool(monkeypatch):
|
||||
pool = FileIOPool(1, 1, "test-agent-init")
|
||||
monkeypatch.setattr(background_init, "_INITIALIZATION_IO", pool)
|
||||
yield pool
|
||||
pool._pool.shutdown(wait=True)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_shared_initializer_forwards_arguments_and_commits_context(build_pool):
|
||||
from deerflow.mcp.cache import _owner_initialization_loop
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
caller_thread = threading.get_ident()
|
||||
state = ContextVar("shared-initializer-context", default="before")
|
||||
|
||||
def initialize(value, *, suffix):
|
||||
assert threading.get_ident() != caller_thread
|
||||
assert _owner_initialization_loop.get() is loop
|
||||
assert state.get() == "before"
|
||||
state.set(None)
|
||||
return value + suffix
|
||||
|
||||
assert await background_init.run_background_init(initialize, "tools", suffix=" ready") == "tools ready"
|
||||
assert state.get() is None
|
||||
assert _owner_initialization_loop.get() is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_failed_shared_initializer_does_not_commit_context(build_pool):
|
||||
state = ContextVar("failed-shared-initializer-context", default="before")
|
||||
|
||||
def fail():
|
||||
state.set("partially initialized")
|
||||
raise ValueError("initialization failed")
|
||||
|
||||
with pytest.raises(ValueError, match="initialization failed"):
|
||||
await background_init.run_background_init(fail)
|
||||
assert state.get() == "before"
|
||||
assert await background_init.run_background_init(lambda: state.get()) == "before"
|
||||
|
||||
|
||||
class FinishedAgent:
|
||||
async def astream(self, *args, **kwargs):
|
||||
yield {"messages": []}
|
||||
|
||||
|
||||
async def _make_run(monkeypatch, factory, *, config=None, app_config=None, thread_id="build-responsiveness"):
|
||||
manager = RunManager()
|
||||
record = await manager.create(thread_id)
|
||||
bridge = MemoryStreamBridge()
|
||||
monkeypatch.setattr(bridge, "cleanup", AsyncMock())
|
||||
task = asyncio.create_task(
|
||||
worker.run_agent(
|
||||
bridge,
|
||||
manager,
|
||||
record,
|
||||
ctx=worker.RunContext(checkpointer=None, app_config=app_config),
|
||||
agent_factory=factory,
|
||||
graph_input={},
|
||||
config=config or {},
|
||||
)
|
||||
)
|
||||
record.task = task
|
||||
return SimpleNamespace(task=task, manager=manager, record=record, bridge=bridge)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_slow_agent_build_keeps_parallel_requests_responsive(monkeypatch, build_pool):
|
||||
loop = asyncio.get_running_loop()
|
||||
entered = asyncio.Event()
|
||||
factory_finished = threading.Event()
|
||||
factory_started_at = []
|
||||
|
||||
@_background
|
||||
def factory(*, config):
|
||||
factory_started_at.append(time.perf_counter())
|
||||
loop.call_soon_threadsafe(entered.set)
|
||||
deadline = time.perf_counter() + 0.18
|
||||
while time.perf_counter() < deadline:
|
||||
pass
|
||||
factory_finished.set()
|
||||
return FinishedAgent()
|
||||
|
||||
run = await _make_run(monkeypatch, factory)
|
||||
await asyncio.wait_for(entered.wait(), 3)
|
||||
probe_delay = time.perf_counter() - factory_started_at[0]
|
||||
responsive = not factory_finished.is_set()
|
||||
await asyncio.wait_for(run.task, 3)
|
||||
|
||||
assert run.record.status == RunStatus.success
|
||||
assert run.bridge._streams[run.record.run_id].ended
|
||||
print(f"agent build concurrent probe delay = {probe_delay:.3f}s")
|
||||
assert responsive, f"Agent initialization blocked parallel requests for {probe_delay:.3f}s"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_custom_factory_keeps_caller_loop_and_context_mutations(monkeypatch):
|
||||
"""Third-party synchronous factories have no worker-safety declaration."""
|
||||
loop = asyncio.get_running_loop()
|
||||
caller_thread = threading.get_ident()
|
||||
context = ContextVar("custom-build-context", default="before")
|
||||
observed = []
|
||||
app_config = object()
|
||||
|
||||
class Agent:
|
||||
async def astream(self, *args, **kwargs):
|
||||
observed.append(context.get())
|
||||
assert asyncio.get_running_loop() is loop
|
||||
yield {"messages": []}
|
||||
|
||||
def factory(*, config, app_config):
|
||||
assert asyncio.get_running_loop() is loop
|
||||
assert threading.get_ident() == caller_thread
|
||||
assert app_config is expected_app_config
|
||||
assert config["context"]["app_config"] is app_config
|
||||
context.set("set by custom factory")
|
||||
return Agent()
|
||||
|
||||
expected_app_config = app_config
|
||||
run = await _make_run(monkeypatch, factory, app_config=app_config)
|
||||
await asyncio.wait_for(run.task, 3)
|
||||
|
||||
assert run.record.status == RunStatus.success
|
||||
assert observed == ["set by custom factory"]
|
||||
assert context.get() == "before", "Run-local state must not leak to its caller"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_factory_failure_ends_stream_and_does_not_poison_next_run(monkeypatch, build_pool):
|
||||
@_background
|
||||
def failing_factory(*, config):
|
||||
raise ValueError("synthetic initialization failure")
|
||||
|
||||
failed = await _make_run(monkeypatch, failing_factory)
|
||||
await asyncio.wait_for(failed.task, 3)
|
||||
assert failed.record.status == RunStatus.error
|
||||
assert failed.record.error == "synthetic initialization failure"
|
||||
stream = failed.bridge._streams[failed.record.run_id]
|
||||
assert stream.ended
|
||||
assert any(event.event == "error" for event in stream.events)
|
||||
|
||||
recovered = await _make_run(monkeypatch, _background(lambda **kwargs: FinishedAgent()))
|
||||
await asyncio.wait_for(recovered.task, 3)
|
||||
assert recovered.record.status == RunStatus.success
|
||||
assert recovered.bridge._streams[recovered.record.run_id].ended
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("clear_registry", [False, True])
|
||||
async def test_background_build_preserves_user_config_and_deferred_registry(monkeypatch, build_pool, clear_registry):
|
||||
loop = asyncio.get_running_loop()
|
||||
loop_thread = threading.get_ident()
|
||||
request_user = ContextVar("build-request-user", default="outside")
|
||||
inherited_registry = DeferredToolRegistry()
|
||||
previous_registry = get_deferred_registry()
|
||||
set_deferred_registry(inherited_registry)
|
||||
observations = {}
|
||||
app_configs = {name: object() for name in ("alice", "bob")}
|
||||
registries = {name: DeferredToolRegistry() for name in app_configs}
|
||||
|
||||
class Agent:
|
||||
def __init__(self, user):
|
||||
self.user = user
|
||||
|
||||
async def astream(self, *args, config, **kwargs):
|
||||
assert asyncio.get_running_loop() is loop
|
||||
assert threading.get_ident() == loop_thread
|
||||
observations[self.user] = (request_user.get(), get_deferred_registry(), config["configurable"]["built_for"])
|
||||
yield {"messages": []}
|
||||
|
||||
@_background
|
||||
def factory(*, config, app_config):
|
||||
user = request_user.get()
|
||||
assert threading.get_ident() != loop_thread
|
||||
assert app_config is app_configs[user]
|
||||
assert config["context"]["app_config"] is app_config
|
||||
assert get_deferred_registry() is inherited_registry
|
||||
config["configurable"]["built_for"] = user
|
||||
if clear_registry:
|
||||
reset_deferred_registry()
|
||||
else:
|
||||
set_deferred_registry(registries[user])
|
||||
return Agent(user)
|
||||
|
||||
runs = []
|
||||
try:
|
||||
for user in app_configs:
|
||||
token = request_user.set(user)
|
||||
try:
|
||||
runs.append(await _make_run(monkeypatch, factory, app_config=app_configs[user], thread_id=user))
|
||||
finally:
|
||||
request_user.reset(token)
|
||||
await asyncio.wait_for(asyncio.gather(*(run.task for run in runs)), 3)
|
||||
assert all(run.record.status == RunStatus.success for run in runs)
|
||||
assert request_user.get() == "outside"
|
||||
assert get_deferred_registry() is inherited_registry
|
||||
for user in app_configs:
|
||||
assert observations[user] == (user, None if clear_registry else registries[user], user)
|
||||
finally:
|
||||
set_deferred_registry(previous_registry)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_queue_serializes_initialization_without_consuming_default_executor(monkeypatch, build_pool):
|
||||
loop = asyncio.get_running_loop()
|
||||
first_entered = asyncio.Event()
|
||||
release = threading.Event()
|
||||
counter_lock = threading.Lock()
|
||||
active = peak = entered = 0
|
||||
default_pool = ThreadPoolExecutor(max_workers=1, thread_name_prefix="test-general-worker")
|
||||
loop.set_default_executor(default_pool)
|
||||
|
||||
@_background
|
||||
def factory(*, config):
|
||||
nonlocal active, peak, entered
|
||||
with counter_lock:
|
||||
active += 1
|
||||
entered += 1
|
||||
peak = max(active, peak)
|
||||
loop.call_soon_threadsafe(first_entered.set)
|
||||
try:
|
||||
assert release.wait(5), "Test did not release initialization worker"
|
||||
return FinishedAgent()
|
||||
finally:
|
||||
with counter_lock:
|
||||
active -= 1
|
||||
|
||||
runs = [await _make_run(monkeypatch, factory, thread_id=f"queue-{index}") for index in range(4)]
|
||||
try:
|
||||
await asyncio.wait_for(first_entered.wait(), 3)
|
||||
assert await asyncio.wait_for(asyncio.to_thread(lambda: "authentication worker available"), 1) == "authentication worker available"
|
||||
await asyncio.sleep(0.03)
|
||||
assert entered == 1, "Cold builders must wait without competing for shared initialization state"
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.wait_for(asyncio.gather(*(run.task for run in runs)), 3)
|
||||
default_pool.shutdown(wait=True)
|
||||
|
||||
assert peak == 1
|
||||
assert entered == 4
|
||||
assert all(run.record.status == RunStatus.success for run in runs)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_repeated_cancel_during_build_drains_worker_without_starting_graph(monkeypatch, build_pool):
|
||||
loop = asyncio.get_running_loop()
|
||||
entered = asyncio.Event()
|
||||
release = threading.Event()
|
||||
graph_started = []
|
||||
finalization_registries = []
|
||||
original_finalize = worker._finalize_run
|
||||
previous_registry = get_deferred_registry()
|
||||
caller_registry = DeferredToolRegistry()
|
||||
set_deferred_registry(caller_registry)
|
||||
|
||||
async def finalize(**kwargs):
|
||||
finalization_registries.append(get_deferred_registry())
|
||||
await original_finalize(**kwargs)
|
||||
|
||||
monkeypatch.setattr(worker, "_finalize_run", finalize)
|
||||
|
||||
class Agent:
|
||||
async def astream(self, *args, **kwargs):
|
||||
graph_started.append(True)
|
||||
yield {"messages": []}
|
||||
|
||||
@_background
|
||||
def factory(*, config):
|
||||
set_deferred_registry(DeferredToolRegistry())
|
||||
loop.call_soon_threadsafe(entered.set)
|
||||
assert release.wait(5), "Test did not release cancelled builder"
|
||||
return Agent()
|
||||
|
||||
run = await _make_run(monkeypatch, factory)
|
||||
try:
|
||||
await asyncio.wait_for(entered.wait(), 3)
|
||||
assert await run.manager.cancel(run.record.run_id)
|
||||
await asyncio.sleep(0)
|
||||
assert not run.task.done(), "Cancellation must not abandon a live initialization worker"
|
||||
run.task.cancel()
|
||||
await asyncio.sleep(0)
|
||||
assert not run.task.done(), "Repeated cancellation must still drain the builder"
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.wait_for(run.task, 3)
|
||||
set_deferred_registry(previous_registry)
|
||||
|
||||
assert not graph_started
|
||||
assert run.record.status == RunStatus.interrupted
|
||||
assert run.bridge._streams[run.record.run_id].ended
|
||||
assert finalization_registries == [caller_registry], "Cancelled build context must not be committed to the run"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_queued_cancel_does_not_build_or_wait_for_active_factory(monkeypatch, build_pool):
|
||||
loop = asyncio.get_running_loop()
|
||||
entered = asyncio.Event()
|
||||
release = threading.Event()
|
||||
queued_built = []
|
||||
|
||||
@_background
|
||||
def first_factory(*, config):
|
||||
loop.call_soon_threadsafe(entered.set)
|
||||
assert release.wait(5)
|
||||
return FinishedAgent()
|
||||
|
||||
@_background
|
||||
def queued_factory(*, config):
|
||||
queued_built.append(True)
|
||||
return FinishedAgent()
|
||||
|
||||
first = await _make_run(monkeypatch, first_factory, thread_id="active")
|
||||
queued = None
|
||||
try:
|
||||
await asyncio.wait_for(entered.wait(), 3)
|
||||
queued = await _make_run(monkeypatch, queued_factory, thread_id="queued")
|
||||
await asyncio.sleep(0.02)
|
||||
await queued.manager.cancel(queued.record.run_id)
|
||||
await asyncio.wait_for(queued.task, 1)
|
||||
assert queued.record.status == RunStatus.interrupted
|
||||
assert queued.bridge._streams[queued.record.run_id].ended
|
||||
assert not queued_built
|
||||
assert not first.task.done()
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.wait_for(asyncio.gather(first.task, *([queued.task] if queued else [])), 3)
|
||||
|
||||
assert first.record.status == RunStatus.success
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_abort_flag_set_during_build_prevents_late_stream_start(monkeypatch, build_pool):
|
||||
loop = asyncio.get_running_loop()
|
||||
entered = asyncio.Event()
|
||||
release = threading.Event()
|
||||
graph_started = []
|
||||
|
||||
class Agent:
|
||||
async def astream(self, *args, **kwargs):
|
||||
graph_started.append(True)
|
||||
yield {"messages": []}
|
||||
|
||||
@_background
|
||||
def factory(*, config):
|
||||
loop.call_soon_threadsafe(entered.set)
|
||||
assert release.wait(5)
|
||||
return Agent()
|
||||
|
||||
run = await _make_run(monkeypatch, factory)
|
||||
try:
|
||||
await asyncio.wait_for(entered.wait(), 3)
|
||||
run.record.abort_event.set()
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.wait_for(run.task, 3)
|
||||
|
||||
assert graph_started == []
|
||||
assert run.record.status == RunStatus.interrupted
|
||||
assert run.bridge._streams[run.record.run_id].ended
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_existing_conversation_keeps_streaming_while_another_agent_initializes(monkeypatch, build_pool):
|
||||
loop = asyncio.get_running_loop()
|
||||
stream_started = asyncio.Event()
|
||||
continue_stream = asyncio.Event()
|
||||
build_entered = asyncio.Event()
|
||||
release_build = threading.Event()
|
||||
build_finished = threading.Event()
|
||||
|
||||
class StreamingAgent:
|
||||
async def astream(self, *args, **kwargs):
|
||||
stream_started.set()
|
||||
yield {"messages": [], "answer": "first delta"}
|
||||
await continue_stream.wait()
|
||||
yield {"messages": [], "answer": "second delta"}
|
||||
|
||||
@_background
|
||||
def slow_factory(*, config):
|
||||
loop.call_soon_threadsafe(build_entered.set)
|
||||
assert release_build.wait(5), "Test did not release the second conversation's initializer"
|
||||
build_finished.set()
|
||||
return FinishedAgent()
|
||||
|
||||
existing = await _make_run(monkeypatch, lambda **kwargs: StreamingAgent(), thread_id="already-streaming")
|
||||
initializing = None
|
||||
try:
|
||||
await asyncio.wait_for(stream_started.wait(), 3)
|
||||
initializing = await _make_run(monkeypatch, slow_factory, thread_id="cold-conversation")
|
||||
await asyncio.wait_for(build_entered.wait(), 3)
|
||||
continue_stream.set()
|
||||
await asyncio.wait_for(existing.task, 1)
|
||||
|
||||
stream = existing.bridge._streams[existing.record.run_id]
|
||||
answers = [event.data["answer"] for event in stream.events if event.event == "values"]
|
||||
assert answers == ["first delta", "second delta"]
|
||||
assert stream.ended
|
||||
assert existing.record.status == RunStatus.success
|
||||
assert not initializing.task.done()
|
||||
assert not build_finished.is_set(), "The existing answer must finish before the cold build is released"
|
||||
finally:
|
||||
continue_stream.set()
|
||||
release_build.set()
|
||||
await asyncio.wait_for(asyncio.gather(existing.task, *([initializing.task] if initializing else [])), 3)
|
||||
|
||||
assert initializing.record.status == RunStatus.success
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_builtin_lead_factory_uses_background_initializer(monkeypatch, build_pool):
|
||||
from deerflow.agents.lead_agent import agent as lead_agent
|
||||
|
||||
loop_thread = threading.get_ident()
|
||||
app_config = object()
|
||||
observed_build_threads = []
|
||||
|
||||
def build_body(config, *, app_config):
|
||||
assert app_config is expected_config
|
||||
observed_build_threads.append(threading.get_ident())
|
||||
return FinishedAgent()
|
||||
|
||||
expected_config = app_config
|
||||
monkeypatch.setattr(lead_agent, "_make_lead_agent", build_body)
|
||||
# Exercise the real exported factory: a missing production opt-in must fail
|
||||
# even if all tests using synthetic marked factories continue to pass.
|
||||
run = await _make_run(monkeypatch, lead_agent.make_lead_agent, app_config=app_config)
|
||||
await asyncio.wait_for(run.task, 3)
|
||||
|
||||
assert run.record.status == RunStatus.success
|
||||
assert len(observed_build_threads) == 1
|
||||
assert observed_build_threads[0] != loop_thread
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
"""SQL-backed retirement preserves shared/private reads and terminal reconnects."""
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from langgraph.checkpoint.base import empty_checkpoint
|
||||
from langgraph.checkpoint.memory import InMemorySaver
|
||||
from test_thread_lazy_checkpoint import client_for
|
||||
|
||||
from app.gateway.routers import artifacts, thread_runs
|
||||
from deerflow.persistence.run.model import RunRow
|
||||
from deerflow.persistence.run.sql import RunRepository
|
||||
from deerflow.runtime.events.store.spool import SpoolingRunEventStore
|
||||
from deerflow.runtime.runs.manager import RunManager
|
||||
from deerflow.runtime.runs.schemas import RunStatus
|
||||
from deerflow.runtime.runs.store.memory import MemoryRunStore
|
||||
from deerflow.runtime.stream_bridge.memory import MemoryStreamBridge
|
||||
from deerflow.runtime.user_context import reset_current_user, set_current_user
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("shared", [False, True])
|
||||
async def test_retired_sql_run_history_and_reconnect_keep_authorization(tmp_path, shared):
|
||||
saver = InMemorySaver()
|
||||
async with client_for(saver) as (client, app):
|
||||
sf = app.state.thread_store._sf
|
||||
async with sf.kw["bind"].begin() as connection:
|
||||
await connection.run_sync(RunRow.__table__.create)
|
||||
manager = RunManager(RunRepository(sf))
|
||||
app.state.run_manager = manager
|
||||
app.state.stream_bridge = MemoryStreamBridge()
|
||||
app.state.run_event_store = SpoolingRunEventStore(tmp_path / "events.db")
|
||||
app.include_router(thread_runs.router)
|
||||
metadata = {"taskId": "task", "agent_id": "agent", "actionId": "action"} if shared else {}
|
||||
assert (await client.post("/api/threads", json={"thread_id": "t", "metadata": metadata})).status_code == 200
|
||||
token = set_current_user(SimpleNamespace(id="owner"))
|
||||
try:
|
||||
record = await manager.create("t", metadata={"user_id": "owner"}, kwargs={"input": {"messages": ["original user question"]}})
|
||||
record.metadata["late_artifact"] = "report.md"
|
||||
await manager.set_status(record.run_id, RunStatus.success)
|
||||
await app.state.run_event_store.put(thread_id="t", run_id=record.run_id, event_type="answer", category="message", content="answer must survive retirement")
|
||||
checkpoint = empty_checkpoint()
|
||||
checkpoint["channel_values"] = {"messages": [{"type": "ai", "content": "full checkpoint answer"}]}
|
||||
checkpoint["channel_versions"] = {"messages": 1}
|
||||
await saver.aput({"configurable": {"thread_id": "t", "checkpoint_ns": ""}}, checkpoint, {}, {"messages": 1})
|
||||
await manager.reap_completed(retention_seconds=0)
|
||||
assert manager.get(record.run_id) is None
|
||||
persisted = await manager.get_persisted(record.run_id)
|
||||
assert persisted.kwargs == record.kwargs
|
||||
assert persisted.metadata["late_artifact"] == "report.md"
|
||||
finally:
|
||||
reset_current_user(token)
|
||||
for user in ["owner", "other", "anonymous"]:
|
||||
headers = {"X-User": user}
|
||||
expected = 401 if user == "anonymous" else 200 if shared or user == "owner" else 404
|
||||
for suffix in [f"runs/{record.run_id}", "runs", f"runs/{record.run_id}/stream", f"runs/{record.run_id}/messages"]:
|
||||
response = await asyncio.wait_for(client.get(f"/api/threads/t/{suffix}", headers=headers), 2)
|
||||
assert response.status_code == expected, response.text
|
||||
if response.status_code == 200 and suffix.endswith("/stream"):
|
||||
assert "event: end" in response.text
|
||||
if response.status_code == 200 and suffix == "runs":
|
||||
assert record.run_id in [row["run_id"] for row in response.json()]
|
||||
state = await client.get("/api/threads/t/state", headers=headers)
|
||||
assert state.status_code == expected
|
||||
if expected == 200:
|
||||
assert state.json()["values"]["messages"][0]["content"] == "full checkpoint answer"
|
||||
assert await manager.get_persisted(record.run_id, authorized_thread_id="wrong-thread") is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_retirement_keeps_live_task_and_unpersisted_or_failed_snapshot():
|
||||
store = MemoryRunStore()
|
||||
manager = RunManager(store)
|
||||
record = await manager.create("t", kwargs={"input": "keep"})
|
||||
await manager.set_status(record.run_id, RunStatus.interrupted)
|
||||
release = asyncio.Event()
|
||||
record.task = asyncio.create_task(release.wait())
|
||||
manager.watch_completion(record)
|
||||
await manager.cleanup(record.run_id, delay=0)
|
||||
assert manager.get(record.run_id) is record
|
||||
release.set()
|
||||
await record.task
|
||||
original_get = store.get
|
||||
store.get = AsyncMock(side_effect=OSError("database unavailable"))
|
||||
await manager.cleanup(record.run_id, delay=0)
|
||||
assert manager.get(record.run_id) is record
|
||||
store.get = AsyncMock(return_value=None)
|
||||
await manager.cleanup(record.run_id, delay=0)
|
||||
assert manager.get(record.run_id) is record
|
||||
store.get = original_get
|
||||
await manager.cleanup(record.run_id, delay=0)
|
||||
assert manager.get(record.run_id) is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_retention_preserves_five_minute_grace_and_close_stops_sweeper():
|
||||
manager = RunManager(MemoryRunStore())
|
||||
record = await manager.create("t")
|
||||
await manager.set_status(record.run_id, RunStatus.success)
|
||||
await manager.reap_completed()
|
||||
assert manager.get(record.run_id) is record
|
||||
manager.start_retention(retention_seconds=0, interval_seconds=0.01)
|
||||
async with asyncio.timeout(2):
|
||||
while manager.get(record.run_id):
|
||||
await asyncio.sleep(0.005)
|
||||
await manager.aclose()
|
||||
assert manager._retention_task.done()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_artifact_directory_scan_does_not_block_other_user_metadata(monkeypatch):
|
||||
import threading
|
||||
|
||||
entered, release = threading.Event(), threading.Event()
|
||||
|
||||
def slow_scan(_):
|
||||
entered.set()
|
||||
assert release.wait(3)
|
||||
return []
|
||||
|
||||
monkeypatch.setattr(artifacts, "collect_outputs_listing", slow_scan)
|
||||
monkeypatch.setattr(artifacts, "aresolve_path_user_id", AsyncMock(return_value="owner"))
|
||||
async with client_for(InMemorySaver()) as (client, app):
|
||||
app.include_router(artifacts.router)
|
||||
assert (await client.post("/api/threads", json={"thread_id": "scan"})).status_code == 200
|
||||
scanning = asyncio.create_task(client.get("/api/threads/scan/artifacts"))
|
||||
try:
|
||||
async with asyncio.timeout(2):
|
||||
while not entered.is_set():
|
||||
await asyncio.sleep(0.005)
|
||||
assert not scanning.done()
|
||||
response = await asyncio.wait_for(client.post("/api/threads", json={"thread_id": "unrelated"}, headers={"X-User": "other"}), 1)
|
||||
assert response.status_code == 200
|
||||
denied = await client.get("/api/threads/scan/artifacts", headers={"X-User": "other"})
|
||||
assert denied.status_code == 404
|
||||
finally:
|
||||
release.set()
|
||||
assert (await scanning).status_code == 200
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_finished_run_rejoin_after_buffer_expiry_ends_instead_of_hanging():
|
||||
from app.gateway.services import sse_consumer
|
||||
|
||||
manager = RunManager(MemoryRunStore())
|
||||
record = await manager.create("t")
|
||||
await manager.set_status(record.run_id, RunStatus.success)
|
||||
bridge = MemoryStreamBridge()
|
||||
await bridge.publish(record.run_id, "values", {"messages": []})
|
||||
await bridge.publish_end(record.run_id)
|
||||
await bridge.cleanup(record.run_id)
|
||||
frames = [frame async for frame in sse_consumer(bridge, record, SimpleNamespace(headers={}), manager)]
|
||||
assert len(frames) == 1 and "event: end" in frames[0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_retirement_preserves_last_event_id_replay_and_fences_old_completion():
|
||||
from app.gateway.services import sse_consumer
|
||||
from deerflow.runtime.runs.schemas import DisconnectMode
|
||||
|
||||
manager = RunManager(MemoryRunStore())
|
||||
older = await manager.create("t", on_disconnect=DisconnectMode.continue_)
|
||||
latest = await manager.create("t", on_disconnect=DisconnectMode.continue_)
|
||||
await manager.set_status(latest.run_id, RunStatus.success)
|
||||
bridge = MemoryStreamBridge()
|
||||
await bridge.publish(latest.run_id, "messages", {"content": "first"})
|
||||
first_id = bridge._streams[latest.run_id].events[-1].id
|
||||
await bridge.publish(latest.run_id, "messages", {"content": "second"})
|
||||
await bridge.publish_end(latest.run_id)
|
||||
await manager.cleanup(latest.run_id, delay=0)
|
||||
assert not manager.is_latest(older), "Retiring a newer run must not un-fence an old finalizer"
|
||||
frames = [frame async for frame in sse_consumer(bridge, latest, SimpleNamespace(headers={"Last-Event-ID": first_id}), manager)]
|
||||
assert len(frames) == 2
|
||||
assert '"second"' in frames[0] and '"first"' not in frames[0]
|
||||
assert "event: end" in frames[1]
|
||||
assert bridge.has_stream(latest.run_id)
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
"""Pool checkout/command deadlines survive ORM bootstrap and DB-create retry."""
|
||||
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from deerflow.config.database_config import DatabaseConfig
|
||||
from deerflow.persistence import engine as persistence
|
||||
|
||||
|
||||
def test_backend_defaults_and_explicit_overrides_are_preserved():
|
||||
assert DatabaseConfig(backend="postgres").pool_recycle == 300
|
||||
assert DatabaseConfig(backend="postgres").command_timeout == 30
|
||||
assert DatabaseConfig(backend="mysql").pool_recycle == 1800
|
||||
assert DatabaseConfig(backend="gauss").pool_recycle == 1800
|
||||
config = DatabaseConfig(backend="postgres", pool_recycle=120, command_timeout=None)
|
||||
assert config.pool_recycle == 120
|
||||
assert config.command_timeout is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", [True, False, 0, -1, float("nan"), float("inf"), float("-inf")])
|
||||
def test_invalid_command_timeouts_fail_config_validation(value):
|
||||
with pytest.raises(ValidationError):
|
||||
DatabaseConfig(command_timeout=value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("retry", [False, True])
|
||||
@pytest.mark.parametrize("timeout", [7.5, None])
|
||||
def test_postgres_uses_same_deadline_on_initial_and_retry_engines(monkeypatch, retry, timeout):
|
||||
async def run():
|
||||
created = []
|
||||
|
||||
class Engine:
|
||||
dialect = SimpleNamespace(name="postgresql")
|
||||
|
||||
def __init__(self, fails):
|
||||
self.fails = fails
|
||||
self.dispose = AsyncMock()
|
||||
|
||||
@asynccontextmanager
|
||||
async def begin(self):
|
||||
async def run_sync(fn):
|
||||
if self.fails:
|
||||
self.fails = False
|
||||
raise RuntimeError('database "review" does not exist')
|
||||
|
||||
yield SimpleNamespace(run_sync=run_sync)
|
||||
|
||||
def create(url, **kwargs):
|
||||
instance = Engine(retry and not created)
|
||||
created.append((url, kwargs, instance))
|
||||
return instance
|
||||
|
||||
for name in ("_engine", "_session_factory", "_engine_loop"):
|
||||
monkeypatch.setattr(persistence, name, None)
|
||||
monkeypatch.setattr(persistence, "create_async_engine", create)
|
||||
create_database = AsyncMock()
|
||||
monkeypatch.setattr(persistence, "_auto_create_postgres_db", create_database)
|
||||
await persistence.init_engine_from_config(
|
||||
DatabaseConfig(
|
||||
backend="postgres",
|
||||
postgres_url="postgresql://user:pass@localhost/review",
|
||||
pool_size=11,
|
||||
max_overflow=3,
|
||||
pool_timeout=9,
|
||||
pool_recycle=120,
|
||||
command_timeout=timeout,
|
||||
)
|
||||
)
|
||||
assert len(created) == (2 if retry else 1)
|
||||
assert create_database.await_count == int(retry)
|
||||
for url, kwargs, _ in created:
|
||||
assert url.startswith("postgresql+asyncpg://")
|
||||
assert kwargs["connect_args"] == {"command_timeout": timeout}
|
||||
assert kwargs["pool_pre_ping"] is True
|
||||
assert kwargs["pool_recycle"] == 120
|
||||
assert kwargs["pool_size"] == 11
|
||||
assert kwargs["max_overflow"] == 3
|
||||
assert kwargs["pool_timeout"] == 9
|
||||
if retry:
|
||||
created[0][2].dispose.assert_awaited_once()
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"backend,url",
|
||||
[
|
||||
("mysql", "mysql+asyncmy://user:pass@localhost/review"),
|
||||
("postgres", "postgresql+psycopg://user:pass@localhost/review"),
|
||||
],
|
||||
)
|
||||
def test_asyncpg_arguments_are_not_sent_to_other_drivers(backend, url):
|
||||
kwargs = persistence._pooled_engine_kwargs(
|
||||
backend=backend,
|
||||
url=url,
|
||||
echo=False,
|
||||
pool_size=5,
|
||||
max_overflow=10,
|
||||
pool_timeout=30,
|
||||
pool_recycle=1800,
|
||||
command_timeout=7.5,
|
||||
)
|
||||
assert "connect_args" not in kwargs
|
||||
assert kwargs["pool_recycle"] == 1800
|
||||
|
||||
|
||||
def test_gauss_asyncpg_receives_command_timeout():
|
||||
kwargs = persistence._pooled_engine_kwargs(
|
||||
backend="gauss",
|
||||
url="opengauss+asyncpg://user:pass@localhost/review",
|
||||
echo=False,
|
||||
pool_size=5,
|
||||
max_overflow=10,
|
||||
pool_timeout=30,
|
||||
pool_recycle=1800,
|
||||
command_timeout=10,
|
||||
)
|
||||
assert kwargs["connect_args"] == {"command_timeout": 10}
|
||||
|
||||
|
||||
def test_sqlite_initialization_never_receives_network_pool_arguments(monkeypatch, tmp_path):
|
||||
async def run():
|
||||
actual_create = persistence.create_async_engine
|
||||
calls = []
|
||||
|
||||
def capture(url, **kwargs):
|
||||
calls.append(kwargs)
|
||||
return actual_create(url, **kwargs)
|
||||
|
||||
for name in ("_engine", "_session_factory", "_engine_loop"):
|
||||
monkeypatch.setattr(persistence, name, None)
|
||||
monkeypatch.setattr(persistence, "create_async_engine", capture)
|
||||
try:
|
||||
await persistence.init_engine(
|
||||
"sqlite",
|
||||
url=f"sqlite+aiosqlite:///{tmp_path / 'review.db'}",
|
||||
sqlite_dir=str(tmp_path),
|
||||
command_timeout=0.01,
|
||||
)
|
||||
assert len(calls) == 1
|
||||
assert "connect_args" not in calls[0]
|
||||
assert "pool_recycle" not in calls[0]
|
||||
finally:
|
||||
if persistence.get_engine() is not None:
|
||||
await persistence.get_engine().dispose()
|
||||
|
||||
asyncio.run(run())
|
||||
@@ -0,0 +1,81 @@
|
||||
from deerflow.runtime.display_payload import (
|
||||
DISPLAY_TOOL_TEXT_MAX_CHARS,
|
||||
slim_display_messages,
|
||||
)
|
||||
from deerflow.runtime.serialization import serialize_channel_values
|
||||
|
||||
|
||||
def test_slim_display_truncates_write_file_body_and_keeps_visible_answer() -> None:
|
||||
answer = "这是用户要看的完整回答。"
|
||||
file_body = "x" * (DISPLAY_TOOL_TEXT_MAX_CHARS + 800)
|
||||
messages = slim_display_messages(
|
||||
[
|
||||
{"type": "human", "id": "h1", "content": "写一份长报告"},
|
||||
{
|
||||
"type": "ai",
|
||||
"id": "a1",
|
||||
"content": answer,
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "w1",
|
||||
"name": "write_file",
|
||||
"args": {"path": "/mnt/user-data/outputs/report.html", "content": file_body},
|
||||
}
|
||||
],
|
||||
"additional_kwargs": {
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "w1",
|
||||
"function": {"name": "write_file", "arguments": file_body},
|
||||
}
|
||||
]
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "tool",
|
||||
"id": "t1",
|
||||
"tool_call_id": "w1",
|
||||
"content": "y" * (DISPLAY_TOOL_TEXT_MAX_CHARS + 50),
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
write = messages[1]["tool_calls"][0]
|
||||
assert write["args"]["path"] == "/mnt/user-data/outputs/report.html"
|
||||
assert len(write["args"]["content"]) < len(file_body)
|
||||
assert "display truncated" in write["args"]["content"]
|
||||
assert messages[1]["content"] == answer
|
||||
assert "display truncated" in messages[2]["content"]
|
||||
assert "display truncated" in messages[1]["additional_kwargs"]["tool_calls"][0]["function"]["arguments"]
|
||||
|
||||
|
||||
def test_serialize_channel_values_slim_strips_viewed_image_bodies() -> None:
|
||||
values = {
|
||||
"viewed_images": {
|
||||
"/mnt/user-data/uploads/a.png": {
|
||||
"base64": "iVBORw0KGgo=" * 200,
|
||||
"mime_type": "image/png",
|
||||
}
|
||||
}
|
||||
}
|
||||
full = serialize_channel_values(values)["viewed_images"]["/mnt/user-data/uploads/a.png"]
|
||||
slim = serialize_channel_values(values, slim_display=True)["viewed_images"]["/mnt/user-data/uploads/a.png"]
|
||||
assert full["base64"].startswith("iVBORw0KGgo")
|
||||
assert slim == {"mime_type": "image/png"}
|
||||
|
||||
|
||||
def test_serialize_channel_values_slim_flag_does_not_change_default() -> None:
|
||||
body = "z" * (DISPLAY_TOOL_TEXT_MAX_CHARS + 20)
|
||||
values = {
|
||||
"messages": [
|
||||
{
|
||||
"type": "ai",
|
||||
"content": "ok",
|
||||
"tool_calls": [{"id": "w1", "name": "write_file", "args": {"content": body}}],
|
||||
}
|
||||
]
|
||||
}
|
||||
full = serialize_channel_values(values)["messages"][0]["tool_calls"][0]["args"]["content"]
|
||||
slim = serialize_channel_values(values, slim_display=True)["messages"][0]["tool_calls"][0]["args"]["content"]
|
||||
assert full == body
|
||||
assert "display truncated" in slim
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
"""New threads must not scan legacy SQLite sources on empty primary reads."""
|
||||
|
||||
import asyncio
|
||||
from types import SimpleNamespace
|
||||
|
||||
from deerflow.runtime.checkpointer.checkpoint_migration import DualReadCheckpointer
|
||||
|
||||
|
||||
class _Primary:
|
||||
def __init__(self):
|
||||
self.tuple_calls = 0
|
||||
self.list_calls = 0
|
||||
|
||||
async def aget_tuple(self, config):
|
||||
self.tuple_calls += 1
|
||||
return None
|
||||
|
||||
async def aget(self, config):
|
||||
return None
|
||||
|
||||
async def alist(self, config, *, filter=None, before=None, limit=None):
|
||||
self.list_calls += 1
|
||||
if False:
|
||||
yield None
|
||||
|
||||
async def aput(self, *args, **kwargs):
|
||||
return None
|
||||
|
||||
|
||||
class _Legacy:
|
||||
def __init__(self):
|
||||
self.tuple_calls = 0
|
||||
self.list_calls = 0
|
||||
self.closed = False
|
||||
|
||||
async def aget_tuple(self, config):
|
||||
self.tuple_calls += 1
|
||||
return {"legacy": True}
|
||||
|
||||
async def alist(self, config, *, filter=None, before=None, limit=None):
|
||||
self.list_calls += 1
|
||||
if False:
|
||||
yield None
|
||||
|
||||
|
||||
def test_remembered_thread_skips_empty_sqlite_fallback():
|
||||
async def run():
|
||||
primary = _Primary()
|
||||
legacy = _Legacy()
|
||||
saver = DualReadCheckpointer(primary, [SimpleNamespace(saver=legacy, closed=False)])
|
||||
config = {"configurable": {"thread_id": "new-hello"}}
|
||||
|
||||
assert await saver.aget_tuple(config) == {"legacy": True}
|
||||
assert legacy.tuple_calls == 1
|
||||
|
||||
saver.remember_primary_thread("new-hello")
|
||||
assert await saver.aget_tuple(config) is None
|
||||
assert legacy.tuple_calls == 1
|
||||
|
||||
chunks = [item async for item in saver.alist(config, limit=1)]
|
||||
assert chunks == []
|
||||
assert legacy.list_calls == 0
|
||||
|
||||
await saver.aput(config, {}, {}, {})
|
||||
assert saver._skip_legacy_fallback(config) is True
|
||||
|
||||
asyncio.run(run())
|
||||
+1
-1
@@ -132,7 +132,7 @@ def test_configured_executor_runs_requested_blocking_tools_and_runner_cleans_up(
|
||||
def test_gateway_configures_executor_after_config_before_langgraph_startup():
|
||||
app_source = Path(runtime_executor.__file__).with_name("app.py").read_text(encoding="utf-8")
|
||||
tree = ast.parse(app_source)
|
||||
lifespan = next(node for node in tree.body if isinstance(node, ast.AsyncFunctionDef) and node.name == "lifespan")
|
||||
lifespan = next(node for node in tree.body if isinstance(node, ast.AsyncFunctionDef) and node.name == "_gateway_lifespan")
|
||||
calls = {node.func.id: node for node in ast.walk(lifespan) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id in {"get_app_config", "configure_runtime_executor", "langgraph_runtime"}}
|
||||
assert calls["get_app_config"].lineno < calls["configure_runtime_executor"].lineno < calls["langgraph_runtime"].lineno
|
||||
assert any(isinstance(node, ast.Await) and node.value is calls["configure_runtime_executor"] for node in ast.walk(lifespan))
|
||||
@@ -0,0 +1,174 @@
|
||||
"""Background graph construction keeps MCP discovery on the request loop."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.mcp import cache, tools
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def isolated_mcp_cache(monkeypatch):
|
||||
monkeypatch.setattr(cache, "_mcp_tools_cache", None)
|
||||
monkeypatch.setattr(cache, "_cache_initialized", False)
|
||||
monkeypatch.setattr(cache, "_config_mtime", None)
|
||||
monkeypatch.setattr(cache, "_initialization_lock", asyncio.Lock())
|
||||
monkeypatch.setattr(cache, "_get_config_mtime", lambda: 1.0)
|
||||
|
||||
|
||||
def _background_get(loop):
|
||||
with cache.mcp_initialization_loop(loop):
|
||||
return cache.get_cached_mcp_tools()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_background_discovery_uses_owner_loop_and_does_not_block_it(monkeypatch):
|
||||
loop = asyncio.get_running_loop()
|
||||
owner_thread = threading.get_ident()
|
||||
started = asyncio.Event()
|
||||
release = asyncio.Event()
|
||||
expected = [object()]
|
||||
|
||||
async def discover():
|
||||
assert asyncio.get_running_loop() is loop
|
||||
assert threading.get_ident() == owner_thread
|
||||
started.set()
|
||||
await release.wait()
|
||||
return expected
|
||||
|
||||
monkeypatch.setattr(tools, "get_mcp_tools", discover)
|
||||
job = asyncio.create_task(asyncio.to_thread(_background_get, loop))
|
||||
try:
|
||||
await asyncio.wait_for(started.wait(), timeout=3)
|
||||
# The owner loop can execute another request while the factory waits.
|
||||
await asyncio.sleep(0)
|
||||
assert not job.done()
|
||||
finally:
|
||||
release.set()
|
||||
result = await asyncio.wait_for(job, timeout=3)
|
||||
assert result is expected
|
||||
assert cache._cache_initialized is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_two_background_builds_share_the_owner_loop_initialization(monkeypatch):
|
||||
loop = asyncio.get_running_loop()
|
||||
started = asyncio.Event()
|
||||
release = asyncio.Event()
|
||||
waiter_ready = threading.Event()
|
||||
calls = 0
|
||||
expected = [object()]
|
||||
|
||||
async def discover():
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
started.set()
|
||||
await release.wait()
|
||||
return expected
|
||||
|
||||
def second_get():
|
||||
waiter_ready.set()
|
||||
return _background_get(loop)
|
||||
|
||||
monkeypatch.setattr(tools, "get_mcp_tools", discover)
|
||||
first = asyncio.create_task(asyncio.to_thread(_background_get, loop))
|
||||
second = None
|
||||
try:
|
||||
await asyncio.wait_for(started.wait(), timeout=3)
|
||||
second = asyncio.create_task(asyncio.to_thread(second_get))
|
||||
while not waiter_ready.is_set():
|
||||
await asyncio.sleep(0)
|
||||
await asyncio.sleep(0.02)
|
||||
assert calls == 1
|
||||
assert not first.done() and not second.done()
|
||||
finally:
|
||||
release.set()
|
||||
results = await asyncio.wait_for(asyncio.gather(first, *([second] if second else [])), timeout=3)
|
||||
assert len(results) == 2 and all(result is expected for result in results)
|
||||
assert calls == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_background_cache_reloads_after_explicit_reset_and_config_change(monkeypatch):
|
||||
loop = asyncio.get_running_loop()
|
||||
calls = 0
|
||||
modified = [1.0]
|
||||
|
||||
async def discover():
|
||||
nonlocal calls
|
||||
assert asyncio.get_running_loop() is loop
|
||||
calls += 1
|
||||
return [calls]
|
||||
|
||||
monkeypatch.setattr(tools, "get_mcp_tools", discover)
|
||||
monkeypatch.setattr(cache, "_get_config_mtime", lambda: modified[0])
|
||||
assert await asyncio.to_thread(_background_get, loop) == [1]
|
||||
assert await asyncio.to_thread(_background_get, loop) == [1]
|
||||
cache.reset_mcp_tools_cache()
|
||||
assert await asyncio.to_thread(_background_get, loop) == [2]
|
||||
modified[0] = 2.0
|
||||
assert await asyncio.to_thread(_background_get, loop) == [3]
|
||||
assert cache._config_mtime == 2.0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_background_discovery_failure_returns_empty_and_can_retry(monkeypatch):
|
||||
loop = asyncio.get_running_loop()
|
||||
calls = 0
|
||||
|
||||
async def discover():
|
||||
nonlocal calls
|
||||
assert asyncio.get_running_loop() is loop
|
||||
calls += 1
|
||||
if calls == 1:
|
||||
raise RuntimeError("discovery unavailable")
|
||||
return ["recovered"]
|
||||
|
||||
monkeypatch.setattr(tools, "get_mcp_tools", discover)
|
||||
assert await asyncio.to_thread(_background_get, loop) == []
|
||||
assert cache._cache_initialized is False
|
||||
assert await asyncio.to_thread(_background_get, loop) == ["recovered"]
|
||||
|
||||
|
||||
def test_initialization_loop_context_restores_after_nested_failure():
|
||||
first = asyncio.new_event_loop()
|
||||
second = asyncio.new_event_loop()
|
||||
try:
|
||||
assert cache._owner_initialization_loop.get() is None
|
||||
with cache.mcp_initialization_loop(first):
|
||||
with pytest.raises(ValueError), cache.mcp_initialization_loop(second):
|
||||
assert cache._owner_initialization_loop.get() is second
|
||||
raise ValueError("factory failed")
|
||||
assert cache._owner_initialization_loop.get() is first
|
||||
assert cache._owner_initialization_loop.get() is None
|
||||
finally:
|
||||
first.close()
|
||||
second.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("closed", [False, True])
|
||||
def test_inactive_owner_loop_fails_without_submitting_or_falling_back(monkeypatch, closed):
|
||||
loop = asyncio.new_event_loop()
|
||||
if closed:
|
||||
loop.close()
|
||||
|
||||
async def unexpected():
|
||||
raise AssertionError("inactive owner loop must not initialize elsewhere")
|
||||
|
||||
monkeypatch.setattr(cache, "initialize_mcp_tools", unexpected)
|
||||
try:
|
||||
assert _background_get(loop) == []
|
||||
assert cache._cache_initialized is False
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
|
||||
def test_unowned_sync_caller_retains_lazy_initialization(monkeypatch):
|
||||
async def discover():
|
||||
return ["sync tool"]
|
||||
|
||||
monkeypatch.setattr(tools, "get_mcp_tools", discover)
|
||||
assert cache.get_cached_mcp_tools() == ["sync tool"]
|
||||
@@ -0,0 +1,277 @@
|
||||
"""Model-specific administrator instructions: persistence, authorization and calls."""
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
from langchain_core.language_models.fake_chat_models import FakeListChatModel
|
||||
from langchain_core.messages import HumanMessage, SystemMessage
|
||||
from pydantic import Field
|
||||
|
||||
from app.gateway.routers import system_settings as router
|
||||
from deerflow.config import system_settings as ss
|
||||
from deerflow.models.prompt_suffix import append_model_prompt_suffix, with_model_prompt_suffix
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def runtime(monkeypatch, tmp_path):
|
||||
monkeypatch.setattr(ss, "runtime_home", lambda: tmp_path)
|
||||
|
||||
|
||||
def save(prompt="请直接回答问题", enabled=True):
|
||||
settings = ss.load_system_settings()
|
||||
settings.model_prompt_suffixes["provider/model-a"] = ss.ModelPromptSuffix(enabled=enabled, prompt=prompt)
|
||||
ss.save_system_settings(settings)
|
||||
|
||||
|
||||
def test_matching_order_and_no_state_mutation():
|
||||
save()
|
||||
messages = [SystemMessage(content="基础要求"), HumanMessage(content="问题"), SystemMessage(content="补充上下文")]
|
||||
assert append_model_prompt_suffix(messages, "model-b") is messages
|
||||
result = append_model_prompt_suffix(messages, "provider/model-a")
|
||||
assert result[0].content == "基础要求\n\n---\n\n补充上下文\n\n请直接回答问题"
|
||||
assert result[1:] == [messages[1]]
|
||||
assert messages[0].content == "基础要求"
|
||||
assert len(messages) == 3
|
||||
assert append_model_prompt_suffix(result, "provider/model-a") == result
|
||||
|
||||
|
||||
def test_blocks_no_system_and_disabled():
|
||||
save()
|
||||
original = SystemMessage(content=[{"type": "text", "text": "原始要求"}])
|
||||
result = append_model_prompt_suffix([original], "provider/model-a")
|
||||
assert result[0].content[-1]["text"] == "\n\n请直接回答问题"
|
||||
assert len(original.content) == 1
|
||||
assert append_model_prompt_suffix(result, "provider/model-a") == result
|
||||
assert append_model_prompt_suffix([HumanMessage(content="问题")], "provider/model-a")[0].content == "请直接回答问题"
|
||||
save(enabled=False)
|
||||
assert append_model_prompt_suffix([original], "provider/model-a") == [original]
|
||||
save(prompt=" ")
|
||||
assert append_model_prompt_suffix([original], "provider/model-a") == [original]
|
||||
|
||||
|
||||
class RecordingModel(FakeListChatModel):
|
||||
seen: list = Field(default_factory=list)
|
||||
|
||||
def _generate(self, messages, *args, **kwargs):
|
||||
self.seen.append(messages)
|
||||
return super()._generate(messages, *args, **kwargs)
|
||||
|
||||
def _stream(self, messages, *args, **kwargs):
|
||||
self.seen.append(messages)
|
||||
yield from super()._stream(messages, *args, **kwargs)
|
||||
|
||||
async def _astream(self, messages, *args, **kwargs):
|
||||
self.seen.append(messages)
|
||||
async for chunk in super()._astream(messages, *args, **kwargs):
|
||||
yield chunk
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_all_invocation_paths_and_live_updates():
|
||||
model = with_model_prompt_suffix(RecordingModel)(responses=["ok"], deerflow_prompt_model_name="provider/model-a")
|
||||
messages = [SystemMessage(content="base"), HumanMessage(content="question")]
|
||||
save()
|
||||
model.invoke(messages)
|
||||
await model.ainvoke(messages)
|
||||
list(model.stream(messages))
|
||||
_ = [chunk async for chunk in model.astream(messages)]
|
||||
model.generate([messages])
|
||||
await model.agenerate([messages])
|
||||
model.bind(temperature=0).invoke(messages)
|
||||
assert len(model.seen) == 7
|
||||
assert all(batch[0].content == "base\n\n请直接回答问题" for batch in model.seen)
|
||||
save(prompt="新的要求")
|
||||
model.invoke(messages)
|
||||
assert model.seen[-1][0].content == "base\n\n新的要求"
|
||||
save(enabled=False)
|
||||
model.invoke(messages)
|
||||
assert model.seen[-1][0].content == "base"
|
||||
assert messages[0].content == "base"
|
||||
assert "deerflow_prompt_model_name" not in model.model_dump()
|
||||
|
||||
|
||||
def test_per_model_instance_switch_skips_suffix():
|
||||
save()
|
||||
model = with_model_prompt_suffix(RecordingModel)(
|
||||
responses=["ok"],
|
||||
deerflow_prompt_model_name="provider/model-a",
|
||||
deerflow_prompt_suffix_enabled=False,
|
||||
)
|
||||
|
||||
model.invoke([SystemMessage(content="base"), HumanMessage(content="question")])
|
||||
|
||||
assert model.seen[-1][0].content == "base"
|
||||
assert "deerflow_prompt_suffix_enabled" not in model.model_dump()
|
||||
|
||||
|
||||
def test_factory_preserves_provider_and_tool_binding(monkeypatch):
|
||||
from deerflow.config.app_config import AppConfig
|
||||
from deerflow.config.model_config import ModelConfig
|
||||
from deerflow.models.factory import create_chat_model
|
||||
from deerflow.models.patched_openai import PatchedChatOpenAI
|
||||
|
||||
config = AppConfig(sandbox={"use": "deerflow.sandbox.local:LocalSandboxProvider"}, models=[ModelConfig(name="provider/model-a", model="test", use="langchain_openai:ChatOpenAI", api_key="test", base_url="http://localhost:9999/v1")])
|
||||
model = create_chat_model(app_config=config)
|
||||
assert isinstance(model, PatchedChatOpenAI)
|
||||
save()
|
||||
bound = model.bind_tools([{"type": "function", "function": {"name": "test", "parameters": {"type": "object", "properties": {}}}}])
|
||||
payload = bound.bound._get_request_payload([HumanMessage(content="hello")], **bound.kwargs)
|
||||
assert payload["messages"][0]["content"] == "请直接回答问题"
|
||||
assert payload["tools"][0]["function"]["name"] == "test"
|
||||
assert "deerflow_prompt_model_name" not in payload
|
||||
responses_model = create_chat_model(app_config=config, use_responses_api=True)
|
||||
responses_payload = responses_model._get_request_payload([SystemMessage(content="base"), HumanMessage(content="hello")])
|
||||
system_content = responses_payload["input"][0]["content"]
|
||||
assert "请直接回答问题" in str(system_content)
|
||||
|
||||
|
||||
def test_admin_api_validation_and_persistence(monkeypatch):
|
||||
app = FastAPI()
|
||||
app.include_router(router.router)
|
||||
client = TestClient(app)
|
||||
user = SimpleNamespace(system_role="user")
|
||||
|
||||
async def current_user(_request):
|
||||
return user
|
||||
|
||||
monkeypatch.setattr(router, "get_optional_user_from_request", current_user)
|
||||
monkeypatch.setattr(router, "get_app_config", lambda: SimpleNamespace(get_model_config=lambda name: object() if name == "provider/model-a" else None))
|
||||
endpoint = "/api/system-settings/model-prompt-suffixes"
|
||||
url = endpoint + "/provider%2Fmodel-a"
|
||||
assert client.get(endpoint).status_code == 403
|
||||
assert client.put(url, json={"enabled": True, "prompt": "要求"}).status_code == 403
|
||||
user.system_role = "admin"
|
||||
assert client.put(url, json={"enabled": True, "prompt": " "}).status_code == 422
|
||||
assert client.put(url, json={"enabled": True, "prompt": "x" * 20001}).status_code == 422
|
||||
assert client.put(endpoint + "/unknown", json={"enabled": True, "prompt": "要求"}).status_code == 404
|
||||
settings = ss.load_system_settings()
|
||||
settings.conversation_context.max_conversation_rounds = 12
|
||||
ss.save_system_settings(settings)
|
||||
assert client.put(url, json={"enabled": True, "prompt": "要求"}).status_code == 200
|
||||
assert client.get(endpoint).json()["provider/model-a"] == {"enabled": True, "prompt": "要求"}
|
||||
assert ss.load_system_settings().conversation_context.max_conversation_rounds == 12
|
||||
user = None
|
||||
assert client.get(endpoint).status_code == 401
|
||||
assert client.put(url, json={"enabled": False, "prompt": ""}).status_code == 401
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("custom_agent", [False, True], ids=["ordinary-chat", "agent-chat"])
|
||||
@pytest.mark.parametrize("agent_prompt_suffix_enabled", [True, False], ids=["agent-suffix-on", "agent-suffix-off"])
|
||||
@pytest.mark.parametrize("selected", [None, "provider/model-a", "model-b"], ids=["default-model", "selected-target", "selected-other"])
|
||||
@pytest.mark.parametrize("streaming", [False, True], ids=["invoke", "stream"])
|
||||
async def test_chat_graph_final_http_payload(monkeypatch, custom_agent, agent_prompt_suffix_enabled, selected, streaming):
|
||||
"""Run both lead-agent branches through LangGraph to the actual HTTP boundary.
|
||||
|
||||
Replace external skill/sandbox middleware with a late context injector, and
|
||||
use distinguishable base prompt fixtures. Model selection, graph execution,
|
||||
factory, provider serialization and HTTP transport remain real.
|
||||
"""
|
||||
import json
|
||||
|
||||
import httpx
|
||||
from langchain.agents.middleware import AgentMiddleware
|
||||
|
||||
from deerflow.agents.lead_agent import agent as lead
|
||||
from deerflow.agents.lead_agent import prompt as prompts
|
||||
from deerflow.config.app_config import AppConfig
|
||||
from deerflow.config.model_config import ModelConfig
|
||||
from deerflow.models.factory import create_chat_model
|
||||
|
||||
save()
|
||||
captured = []
|
||||
|
||||
def respond(request):
|
||||
payload = json.loads(request.content)
|
||||
captured.append(payload)
|
||||
if payload.get("stream"):
|
||||
chunks = [
|
||||
{"id": "chat-test", "object": "chat.completion.chunk", "model": "test", "choices": [{"index": 0, "delta": {"role": "assistant", "content": "ok"}, "finish_reason": None}]},
|
||||
{"id": "chat-test", "object": "chat.completion.chunk", "model": "test", "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]},
|
||||
]
|
||||
content = "".join("data: " + json.dumps(chunk) + "\n\n" for chunk in chunks) + "data: [DONE]\n\n"
|
||||
return httpx.Response(200, headers={"Content-Type": "text/event-stream"}, content=content)
|
||||
return httpx.Response(200, json={"id": "chat-test", "object": "chat.completion", "model": "test", "choices": [{"index": 0, "message": {"role": "assistant", "content": "ok"}, "finish_reason": "stop"}]})
|
||||
|
||||
class LateContext(AgentMiddleware):
|
||||
async def awrap_model_call(self, request, handler):
|
||||
return await handler(
|
||||
request.override(
|
||||
system_message=SystemMessage(content=request.system_message.content + "\n最后一个中间件的要求"),
|
||||
messages=[*request.messages, SystemMessage(content="运行时检索上下文")],
|
||||
)
|
||||
)
|
||||
|
||||
config = AppConfig(
|
||||
sandbox={"use": "deerflow.sandbox.local:LocalSandboxProvider"},
|
||||
models=[ModelConfig(name=name, model="test", use="langchain_openai:ChatOpenAI", api_key="test", base_url="http://model.test/v1") for name in ["model-b", "provider/model-a"]],
|
||||
)
|
||||
agent_config = SimpleNamespace(name="测试智能体", model="provider/model-a", model_prompt_suffix_enabled=agent_prompt_suffix_enabled, skills=[], tool_groups=None) if custom_agent else None
|
||||
monkeypatch.setattr(lead, "load_agent_config", lambda _: agent_config)
|
||||
monkeypatch.setattr(lead, "load_agent_soul", lambda _: "SOUL.md 的要求" if custom_agent else "")
|
||||
monkeypatch.setattr(lead, "apply_prompt_template", lambda **_: "普通问答模板")
|
||||
monkeypatch.setattr(prompts, "apply_agent_only_prompt_template", lambda **_: "SOUL.md 的要求")
|
||||
monkeypatch.setattr(lead, "_build_middlewares", lambda *args, **kwargs: [LateContext()])
|
||||
runtime = {"disable_tools": True, "thinking_enabled": False, "agent_id": "test-agent" if custom_agent else "lead"}
|
||||
if selected:
|
||||
runtime["model_name"] = selected
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(respond)) as client:
|
||||
monkeypatch.setattr(lead, "create_chat_model", lambda **kwargs: create_chat_model(**kwargs, http_async_client=client))
|
||||
graph = lead._make_lead_agent({"configurable": runtime}, app_config=config)
|
||||
inputs = {"messages": [HumanMessage(content="用户问题")]}
|
||||
if streaming:
|
||||
_ = [chunk async for chunk in graph.astream(inputs, stream_mode="messages")]
|
||||
else:
|
||||
await graph.ainvoke(inputs)
|
||||
|
||||
assert len(captured) == 1
|
||||
messages = captured[0]["messages"]
|
||||
systems = [message for message in messages if message["role"] in {"system", "developer"}]
|
||||
assert len(systems) == 1
|
||||
text = systems[0]["content"]
|
||||
assert ("SOUL.md 的要求" if custom_agent else "普通问答模板") in text
|
||||
assert "最后一个中间件的要求" in text
|
||||
assert "运行时检索上下文" in text
|
||||
effective_model = selected or ("provider/model-a" if custom_agent else "model-b")
|
||||
if effective_model == "provider/model-a" and (not custom_agent or agent_prompt_suffix_enabled):
|
||||
assert text.endswith("运行时检索上下文\n\n请直接回答问题")
|
||||
assert text.count("请直接回答问题") == 1
|
||||
else:
|
||||
assert "请直接回答问题" not in text
|
||||
assert [message["content"] for message in messages if message["role"] == "user"] == ["用户问题"]
|
||||
assert inputs["messages"][0].content == "用户问题"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("transport", ["text_bridge", "buffered_gateway", "artifact_renderer"])
|
||||
def test_suffix_follows_file_transport_instructions(transport):
|
||||
from deerflow.models import patched_openai as provider
|
||||
|
||||
save()
|
||||
model = with_model_prompt_suffix(provider.PatchedChatOpenAI)(
|
||||
model="test",
|
||||
api_key="test",
|
||||
deerflow_prompt_model_name="provider/model-a",
|
||||
)
|
||||
tool = {"type": "function", "function": {"name": "write_file", "description": "Write a file", "parameters": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}}}
|
||||
tool["function"]["parameters"]["properties"].update({"description": {"type": "string"}, "append": {"type": "boolean"}})
|
||||
begin_tool = {
|
||||
"type": "function",
|
||||
"function": {"name": "begin_write_file", "description": "Prepare a file", "parameters": {"type": "object", "properties": {"path": {"type": "string"}, "description": {"type": "string"}, "append": {"type": "boolean"}}}},
|
||||
}
|
||||
token = provider._ACTIVE_WRITE_FILE_BRIDGE.set(provider._WriteFileBridgeStreamState(transport=transport))
|
||||
try:
|
||||
payload = model._get_request_payload([SystemMessage(content="SOUL 要求"), HumanMessage(content="写文件")], tools=[tool, begin_tool])
|
||||
finally:
|
||||
provider._ACTIVE_WRITE_FILE_BRIDGE.reset(token)
|
||||
system = payload["messages"][0]["content"]
|
||||
instruction = {
|
||||
"text_bridge": provider._TEXT_BRIDGE_SELECTION_INSTRUCTION,
|
||||
"buffered_gateway": provider._BUFFERED_GATEWAY_SELECTION_INSTRUCTION,
|
||||
"artifact_renderer": provider._ARTIFACT_RENDERER_SELECTION_INSTRUCTION,
|
||||
}[transport]
|
||||
assert instruction in system
|
||||
assert system.startswith("SOUL 要求")
|
||||
assert system.endswith("\n\n请直接回答问题")
|
||||
assert system.count("请直接回答问题") == 1
|
||||
+213
@@ -0,0 +1,213 @@
|
||||
"""DeepSeek DSML fallback for OpenAI-compatible gateways without a parser."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from deerflow.config.app_config import AppConfig
|
||||
from deerflow.config.model_config import ModelConfig
|
||||
from deerflow.models.dsml_tool_calls import (
|
||||
DSMLToolCallProtocolError,
|
||||
parse_dsml_content,
|
||||
)
|
||||
from deerflow.models.factory import create_chat_model
|
||||
from deerflow.models.patched_openai import PatchedChatOpenAI
|
||||
|
||||
TOOL_NAME = "agent_orchestration"
|
||||
TOOL_SCHEMA = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": TOOL_NAME,
|
||||
"description": "Dispatch a roundtable seat",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"agent_name": {"type": "string"},
|
||||
"task": {"type": "string"},
|
||||
"round": {"type": "integer"},
|
||||
},
|
||||
"required": ["agent_name", "task"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _dsml(*, ascii_pipe: bool = False) -> str:
|
||||
token = "|DSML|" if ascii_pipe else "|DSML|"
|
||||
return (
|
||||
f"<{token}tool_calls>"
|
||||
f'<{token}invoke name="{TOOL_NAME}">'
|
||||
f'<{token}parameter name="agent_name" string="true">主持人</{token}parameter>'
|
||||
f'<{token}parameter name="task" string="true">识别议题</{token}parameter>'
|
||||
f'<{token}parameter name="round" string="false">1</{token}parameter>'
|
||||
f"</{token}invoke>"
|
||||
f"</{token}tool_calls>"
|
||||
)
|
||||
|
||||
|
||||
def _model(handler, **kwargs) -> PatchedChatOpenAI:
|
||||
transport = httpx.MockTransport(handler)
|
||||
return PatchedChatOpenAI(
|
||||
model="deepseek-ai/DeepSeek-V4-Flash",
|
||||
api_key="unused-local-key",
|
||||
base_url="https://local.invalid/v1",
|
||||
max_retries=0,
|
||||
http_client=httpx.Client(transport=transport),
|
||||
http_async_client=httpx.AsyncClient(transport=transport),
|
||||
tool_call_transport="deepseek_dsml",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
def _chat_response(content: str) -> httpx.Response:
|
||||
return httpx.Response(
|
||||
200,
|
||||
json={
|
||||
"id": "chatcmpl-dsml",
|
||||
"object": "chat.completion",
|
||||
"created": 1,
|
||||
"model": "deepseek-v4",
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {"role": "assistant", "content": content},
|
||||
"finish_reason": "stop",
|
||||
}
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _sse_response(parts: list[str]) -> httpx.Response:
|
||||
events: list[bytes] = []
|
||||
for index, part in enumerate(parts):
|
||||
body = {
|
||||
"id": "chatcmpl-dsml-stream",
|
||||
"object": "chat.completion.chunk",
|
||||
"created": 1,
|
||||
"model": "deepseek-v4",
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"delta": {
|
||||
"role": "assistant" if index == 0 else None,
|
||||
"content": part,
|
||||
},
|
||||
"finish_reason": None,
|
||||
}
|
||||
],
|
||||
}
|
||||
events.append(f"data: {json.dumps(body, ensure_ascii=False)}\n\n".encode())
|
||||
terminal = {
|
||||
"id": "chatcmpl-dsml-stream",
|
||||
"object": "chat.completion.chunk",
|
||||
"created": 1,
|
||||
"model": "deepseek-v4",
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"delta": {},
|
||||
"finish_reason": "stop",
|
||||
}
|
||||
],
|
||||
}
|
||||
events.append(f"data: {json.dumps(terminal)}\n\n".encode())
|
||||
events.append(b"data: [DONE]\n\n")
|
||||
return httpx.Response(
|
||||
200,
|
||||
content=b"".join(events),
|
||||
headers={"content-type": "text/event-stream"},
|
||||
)
|
||||
|
||||
|
||||
def _collect_stream(model: PatchedChatOpenAI, *, asynchronous: bool):
|
||||
runnable = model.bind_tools([TOOL_SCHEMA])
|
||||
|
||||
async def collect_async():
|
||||
return [chunk async for chunk in runnable.astream("开始会商")]
|
||||
|
||||
if asynchronous:
|
||||
return asyncio.run(collect_async())
|
||||
return list(runnable.stream("开始会商"))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ascii_pipe", [False, True])
|
||||
def test_parse_complete_dsml_call(ascii_pipe: bool):
|
||||
parsed = parse_dsml_content(
|
||||
f"前文{_dsml(ascii_pipe=ascii_pipe)}",
|
||||
allowed_tools={TOOL_NAME},
|
||||
)
|
||||
|
||||
assert parsed.content == "前文"
|
||||
assert len(parsed.calls) == 1
|
||||
assert parsed.calls[0].name == TOOL_NAME
|
||||
assert parsed.calls[0].arguments == {
|
||||
"agent_name": "主持人",
|
||||
"task": "识别议题",
|
||||
"round": 1,
|
||||
}
|
||||
|
||||
|
||||
def test_parser_rejects_unknown_or_truncated_calls():
|
||||
misspelled = _dsml().replace(TOOL_NAME, "agent_orchrestration")
|
||||
with pytest.raises(DSMLToolCallProtocolError, match="unadvertised tool"):
|
||||
parse_dsml_content(misspelled, allowed_tools={TOOL_NAME})
|
||||
|
||||
truncated = _dsml().removesuffix("</|DSML|tool_calls>")
|
||||
with pytest.raises(DSMLToolCallProtocolError, match="not closed"):
|
||||
parse_dsml_content(truncated, allowed_tools={TOOL_NAME})
|
||||
|
||||
|
||||
def test_nonstream_response_becomes_standard_tool_call():
|
||||
model = _model(lambda _: _chat_response(_dsml()))
|
||||
|
||||
message = model.bind_tools([TOOL_SCHEMA]).invoke("开始会商")
|
||||
|
||||
assert message.content == ""
|
||||
assert len(message.tool_calls) == 1
|
||||
assert message.tool_calls[0]["name"] == TOOL_NAME
|
||||
assert message.tool_calls[0]["args"]["agent_name"] == "主持人"
|
||||
assert message.response_metadata["finish_reason"] == "tool_calls"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
def test_streamed_dsml_is_suppressed_and_becomes_tool_call(asynchronous: bool):
|
||||
text = _dsml()
|
||||
parts = [text[:5], text[5:31], text[31:79], text[79:]]
|
||||
model = _model(lambda _: _sse_response(parts))
|
||||
|
||||
chunks = _collect_stream(model, asynchronous=asynchronous)
|
||||
merged = sum(chunks[1:], chunks[0])
|
||||
|
||||
assert "DSML" not in str(merged.content)
|
||||
assert len(merged.tool_calls) == 1
|
||||
assert merged.tool_calls[0]["name"] == TOOL_NAME
|
||||
assert merged.tool_calls[0]["args"]["round"] == 1
|
||||
assert merged.response_metadata["finish_reason"] == "tool_calls"
|
||||
|
||||
|
||||
def test_factory_keeps_transport_local_to_adapter():
|
||||
config = AppConfig.model_construct(
|
||||
models=[
|
||||
ModelConfig(
|
||||
name="deepseek-v4",
|
||||
model="deepseek-ai/DeepSeek-V4-Flash",
|
||||
use="langchain_openai:ChatOpenAI",
|
||||
api_key="unused",
|
||||
base_url="https://local.invalid/v1",
|
||||
tool_call_transport="deepseek_dsml",
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
model = create_chat_model("deepseek-v4", app_config=config)
|
||||
|
||||
assert isinstance(model, PatchedChatOpenAI)
|
||||
assert model.tool_call_transport == "deepseek_dsml"
|
||||
assert "tool_call_transport" not in model._default_params
|
||||
assert "tool_call_transport" not in model.model_kwargs
|
||||
|
||||
+31
@@ -4543,6 +4543,37 @@ def test_factory_global_switch_overrides_every_compatible_model(
|
||||
assert second.write_file_transport == "buffered_gateway"
|
||||
|
||||
|
||||
def test_roundtable_override_enables_buffered_gateway_while_global_is_off(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(
|
||||
model_factory,
|
||||
"get_write_file_compatibility_settings",
|
||||
lambda: SimpleNamespace(enabled=False),
|
||||
)
|
||||
config = AppConfig.model_construct(
|
||||
models=[
|
||||
ModelConfig(
|
||||
name="native-model",
|
||||
use="langchain_openai:ChatOpenAI",
|
||||
model="native-model",
|
||||
api_key="test-key",
|
||||
base_url=None,
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
ordinary = model_factory.create_chat_model("native-model", app_config=config)
|
||||
roundtable = model_factory.create_chat_model(
|
||||
"native-model",
|
||||
app_config=config,
|
||||
write_file_compatibility=True,
|
||||
)
|
||||
|
||||
assert ordinary.write_file_transport == "native"
|
||||
assert roundtable.write_file_transport == "buffered_gateway"
|
||||
|
||||
|
||||
def test_factory_rejects_bridge_for_non_patched_provider(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
config = AppConfig.model_construct(
|
||||
models=[
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
"""Cross-user isolation, fair admission and durable event retention."""
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
from contextvars import copy_context
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.config.runtime_contention_config import RuntimeContentionConfig
|
||||
from deerflow.runtime.events.store.jsonl import JsonlRunEventStore
|
||||
from deerflow.runtime.events.store.spool import SpoolingRunEventStore
|
||||
from deerflow.runtime.fair_queue import FairAsyncGate, FairBackgroundScheduler
|
||||
from deerflow.utils.file_io import FileIOPool
|
||||
|
||||
|
||||
async def until(predicate, timeout=2):
|
||||
async with asyncio.timeout(timeout):
|
||||
while not predicate():
|
||||
await asyncio.sleep(0.005)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cancelled_disk_caller_does_not_release_a_still_running_job():
|
||||
pool = FileIOPool(1, 1, "file-test")
|
||||
entered, release, second = threading.Event(), threading.Event(), threading.Event()
|
||||
|
||||
def blocked():
|
||||
entered.set()
|
||||
assert release.wait(3)
|
||||
|
||||
task = asyncio.create_task(pool(blocked))
|
||||
await until(entered.is_set)
|
||||
task.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await task
|
||||
queued = asyncio.create_task(pool(second.set))
|
||||
await asyncio.sleep(0.02)
|
||||
assert not second.is_set()
|
||||
release.set()
|
||||
await asyncio.wait_for(queued, 2)
|
||||
assert second.is_set()
|
||||
pool._pool.shutdown()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("kind", ["spool", "jsonl"])
|
||||
async def test_event_store_concurrent_cursors_restart_pagination_and_explicit_deletion(tmp_path, kind):
|
||||
def create():
|
||||
return SpoolingRunEventStore(tmp_path / "events.db") if kind == "spool" else JsonlRunEventStore(tmp_path)
|
||||
store = create()
|
||||
content = "完整消息" * 6000
|
||||
rows = await asyncio.gather(*(store.put(thread_id="thread", run_id=f"r{i % 3}", event_type="answer", category="message", content=content, metadata={"id": i}) for i in range(60)))
|
||||
assert sorted(row["seq"] for row in rows) == list(range(1, 61))
|
||||
reopened = create()
|
||||
latest = await reopened.list_messages("thread", limit=5)
|
||||
assert [row["seq"] for row in latest] == [56, 57, 58, 59, 60]
|
||||
assert latest[-1]["content"] == content
|
||||
assert [row["seq"] for row in await reopened.list_messages("thread", after_seq=3, limit=4)] == [4, 5, 6, 7]
|
||||
assert [row["seq"] for row in await reopened.list_messages("thread", before_seq=8, limit=3)] == [5, 6, 7]
|
||||
assert len(await reopened.list_messages_by_run("thread", "r1", limit=50)) == 20
|
||||
assert await reopened.count_messages("thread") == 60
|
||||
assert await reopened.list_events("thread", "r1", event_types=["missing"]) == []
|
||||
assert await reopened.delete_by_run("thread", "r1") == 20
|
||||
assert await reopened.count_messages("thread") == 40
|
||||
last = await reopened.put(thread_id="thread", run_id="new", event_type="answer", category="message", content="new")
|
||||
assert last["seq"] == 61
|
||||
assert await reopened.delete_by_thread("thread") == 41
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_spool_failed_batch_rolls_back_events_and_cursor(tmp_path):
|
||||
store = SpoolingRunEventStore(tmp_path / "events.db")
|
||||
with pytest.raises(KeyError):
|
||||
await store.put_batch([
|
||||
dict(thread_id="t", run_id="r", event_type="answer", category="message", content="must rollback"),
|
||||
dict(thread_id="t", run_id="r", event_type="invalid"),
|
||||
])
|
||||
assert await store.count_messages("t") == 0
|
||||
saved = await store.put(thread_id="t", run_id="r", event_type="answer", category="message", content="retained")
|
||||
assert saved["seq"] == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_event_backend_memory_factory_spools_without_losing_messages(tmp_path, monkeypatch):
|
||||
from deerflow.config import runtime_paths
|
||||
from deerflow.runtime.events.store import make_run_event_store
|
||||
|
||||
monkeypatch.setattr(runtime_paths, "runtime_home", lambda: tmp_path)
|
||||
first = make_run_event_store(SimpleNamespace(backend="memory"))
|
||||
await first.put(thread_id="t", run_id="r", event_type="answer", category="message", content="saved")
|
||||
second = make_run_event_store(SimpleNamespace(backend="memory"))
|
||||
assert (await second.list_messages("t"))[0]["content"] == "saved"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_gate_reserves_capacity_for_other_user_and_removes_cancelled_waiters():
|
||||
gate = FairAsyncGate(3, 2, 20)
|
||||
entered = []
|
||||
release = asyncio.Event()
|
||||
|
||||
async def work(user, index):
|
||||
async with gate.slot(user):
|
||||
entered.append((user, index))
|
||||
await release.wait()
|
||||
|
||||
tasks = [asyncio.create_task(work("A", i)) for i in range(4)]
|
||||
await until(lambda: len(entered) == 2)
|
||||
tasks.append(asyncio.create_task(work("B", 0)))
|
||||
await until(lambda: len(entered) == 3)
|
||||
assert ("B", 0) in entered
|
||||
tasks[3].cancel()
|
||||
await asyncio.gather(tasks[3], return_exceptions=True)
|
||||
release.set()
|
||||
await asyncio.gather(*tasks, return_exceptions=True)
|
||||
assert gate._active == {} and gate._queues == {}
|
||||
assert ("A", 3) not in entered
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scheduler_fairness_queued_timeout_and_cancellation():
|
||||
scheduler = FairBackgroundScheduler(3, 2, 30)
|
||||
release, cancel = threading.Event(), threading.Event()
|
||||
entered, expired = [], []
|
||||
lock = threading.Lock()
|
||||
|
||||
def run(name):
|
||||
def operation(remaining):
|
||||
assert remaining > 0
|
||||
with lock:
|
||||
entered.append(name)
|
||||
assert release.wait(3)
|
||||
return operation
|
||||
|
||||
try:
|
||||
for i in range(4):
|
||||
scheduler.submit("A", run(f"A{i}"), timeout=2, cancelled=lambda: False, expired=lambda cancelled: expired.append(cancelled))
|
||||
await until(lambda: len(entered) == 2)
|
||||
scheduler.submit("B", run("B"), timeout=2, cancelled=lambda: False, expired=lambda cancelled: expired.append(cancelled))
|
||||
await until(lambda: len(entered) == 3)
|
||||
assert "B" in entered
|
||||
scheduler.submit("C", run("expired"), timeout=0.02, cancelled=lambda: False, expired=lambda cancelled: expired.append("timeout"))
|
||||
scheduler.submit("D", run("cancelled"), timeout=2, cancelled=cancel.is_set, expired=lambda cancelled: expired.append("cancel" if cancelled else "wrong"))
|
||||
cancel.set()
|
||||
await until(lambda: len(expired) == 2)
|
||||
assert set(expired) == {"timeout", "cancel"}
|
||||
assert "expired" not in entered and "cancelled" not in entered
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.to_thread(scheduler.shutdown)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_subagent_user_context_and_cancellation_ack_keep_real_slot(monkeypatch):
|
||||
# Initialize the normal agent/tool import order before loading the executor.
|
||||
import deerflow.agents # noqa: F401
|
||||
from deerflow.runtime.user_context import get_effective_user_id, reset_current_user, set_current_user
|
||||
from deerflow.subagents import executor
|
||||
|
||||
scheduler = FairBackgroundScheduler(1, 1, 20)
|
||||
monkeypatch.setattr(executor, "_scheduler_pool", scheduler)
|
||||
release = threading.Event()
|
||||
started, cancelled = [], threading.Event()
|
||||
|
||||
async def fake(self, task, holder):
|
||||
started.append((task, get_effective_user_id()))
|
||||
try:
|
||||
await asyncio.sleep(10)
|
||||
except asyncio.CancelledError:
|
||||
cancelled.set()
|
||||
while not release.is_set():
|
||||
await asyncio.sleep(0.005)
|
||||
holder.status = executor.SubagentStatus.COMPLETED
|
||||
return holder
|
||||
|
||||
monkeypatch.setattr(executor.SubagentExecutor, "_aexecute", fake)
|
||||
first = object.__new__(executor.SubagentExecutor)
|
||||
first.config = SimpleNamespace(name="fixture", timeout_seconds=0.05)
|
||||
first.trace_id = "test"
|
||||
token = set_current_user(SimpleNamespace(id="A"))
|
||||
try:
|
||||
first_id = first.execute_async("first")
|
||||
finally:
|
||||
reset_current_user(token)
|
||||
second = object.__new__(executor.SubagentExecutor)
|
||||
second.config, second.trace_id = SimpleNamespace(name="fixture", timeout_seconds=2), "test"
|
||||
try:
|
||||
await until(cancelled.is_set)
|
||||
assert executor.get_background_task_result(first_id).status == executor.SubagentStatus.TIMED_OUT
|
||||
executor.cleanup_background_task(first_id)
|
||||
assert executor.get_background_task_result(first_id) is not None
|
||||
token = set_current_user(SimpleNamespace(id="B"))
|
||||
try:
|
||||
second_id = second.execute_async("second")
|
||||
finally:
|
||||
reset_current_user(token)
|
||||
await asyncio.sleep(0.02)
|
||||
assert started == [("first", "A")]
|
||||
release.set()
|
||||
await until(lambda: len(started) == 2)
|
||||
assert started[-1] == ("second", "B")
|
||||
executor.request_cancel_background_task(second_id)
|
||||
# The fake intentionally waits for its deadline to exercise the bridge.
|
||||
await until(lambda: executor.get_background_task_result(second_id).execution_finished.is_set(), timeout=3)
|
||||
executor.cleanup_background_task(second_id)
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.to_thread(scheduler.shutdown)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_isolated_cancellation_before_first_coroutine_step_acknowledges():
|
||||
import deerflow.agents # noqa: F401
|
||||
from deerflow.subagents import executor
|
||||
|
||||
stopped = threading.Event()
|
||||
future = executor._submit_to_isolated_loop_in_context(copy_context(), lambda: asyncio.sleep(5), on_finished=stopped.set)
|
||||
future.cancel()
|
||||
await until(stopped.is_set)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_background_roundtable_cancel_waits_for_actual_run_before_next_user(monkeypatch):
|
||||
from app.gateway import roundtable_inprocess_gateway as gateway
|
||||
from app.gateway import services
|
||||
|
||||
gate = FairAsyncGate(1, 1)
|
||||
started, cancelled = [], []
|
||||
release = asyncio.Event()
|
||||
|
||||
async def run_child(thread_id):
|
||||
started.append(thread_id)
|
||||
try:
|
||||
await asyncio.Event().wait()
|
||||
except asyncio.CancelledError:
|
||||
cancelled.append(thread_id)
|
||||
await release.wait()
|
||||
|
||||
async def start(body, thread_id, request, **kwargs):
|
||||
return SimpleNamespace(task=asyncio.create_task(run_child(thread_id)))
|
||||
|
||||
async def watch(*args, **kwargs):
|
||||
await asyncio.Event().wait()
|
||||
|
||||
monkeypatch.setattr(gateway, "_background_gate", lambda: gate)
|
||||
monkeypatch.setattr(gateway, "_await_run_with_idle_guard", watch)
|
||||
monkeypatch.setattr(services, "start_run", start)
|
||||
app = SimpleNamespace(state=SimpleNamespace())
|
||||
|
||||
def launch(user):
|
||||
return asyncio.create_task(gateway._run_agent_turn(app, user_id=user, thread_id=user, agent_name="seat", message="question",
|
||||
model="test", excluded_tools=[], skill_stop_names=[], thinking_enabled=False, reasoning_effort="low"))
|
||||
|
||||
first = launch("A")
|
||||
second = None
|
||||
try:
|
||||
await until(lambda: started == ["A"])
|
||||
first.cancel()
|
||||
await until(lambda: cancelled == ["A"])
|
||||
second = launch("B")
|
||||
await asyncio.sleep(0.02)
|
||||
assert started == ["A"] and not first.done()
|
||||
release.set()
|
||||
await until(lambda: started == ["A", "B"])
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await first
|
||||
finally:
|
||||
release.set()
|
||||
for task in [first, second]:
|
||||
if task and not task.done():
|
||||
task.cancel()
|
||||
await asyncio.gather(*(task for task in [first, second] if task), return_exceptions=True)
|
||||
assert gate._active == {}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_failed_event_disk_write_preserves_journal_buffer_for_retry(tmp_path, monkeypatch):
|
||||
from deerflow.runtime.journal import RunJournal
|
||||
|
||||
store = SpoolingRunEventStore(tmp_path / "events.db")
|
||||
await store.put(thread_id="t", run_id="old", event_type="answer", category="message", content="old answer")
|
||||
journal = RunJournal("new", "t", store, flush_threshold=100)
|
||||
journal._put(event_type="answer", category="message", content="new answer" * 6000)
|
||||
original = store._connect
|
||||
with monkeypatch.context() as patch:
|
||||
patch.setattr(store, "_connect", lambda: (_ for _ in ()).throw(OSError("disk unavailable")))
|
||||
with pytest.raises(OSError):
|
||||
await journal.flush()
|
||||
assert len(journal._buffer) == 1
|
||||
assert store._connect == original
|
||||
await journal.flush()
|
||||
rows = await store.list_messages("t")
|
||||
assert [row["seq"] for row in rows] == [1, 2]
|
||||
assert [row["content"] for row in rows] == ["old answer", "new answer" * 6000]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field", ["subagent_concurrency", "subagent_per_user", "background_concurrency", "queue_limit"])
|
||||
def test_contention_budget_validation(field):
|
||||
with pytest.raises(ValueError):
|
||||
RuntimeContentionConfig(**{field: 0})
|
||||
+3
-3
@@ -4,9 +4,8 @@ import pytest
|
||||
|
||||
from deerflow.config.app_config import AppConfig
|
||||
|
||||
|
||||
_SKILL_VIEW_KEYS = ("skill_view_reuse_enabled", "skill_view_cache_enabled")
|
||||
_OPT_IN_KEYS = ("model_timing_nonblocking_enabled", "asyncio_thread_pool_enabled")
|
||||
_OPT_IN_KEYS = ("model_timing_nonblocking_enabled", "asyncio_thread_pool_enabled", "tool_metrics_enabled")
|
||||
|
||||
|
||||
def test_missing_runtime_performance_enables_skill_view_only():
|
||||
@@ -14,6 +13,7 @@ def test_missing_runtime_performance_enables_skill_view_only():
|
||||
assert config.runtime_performance.model_dump() == {
|
||||
"skill_view_reuse_enabled": True,
|
||||
"skill_view_cache_enabled": True,
|
||||
"tool_metrics_enabled": False,
|
||||
"model_timing_nonblocking_enabled": False,
|
||||
"asyncio_thread_pool_enabled": False,
|
||||
"asyncio_thread_pool_size": 100,
|
||||
@@ -23,7 +23,7 @@ def test_missing_runtime_performance_enables_skill_view_only():
|
||||
@pytest.mark.parametrize("key", [*_SKILL_VIEW_KEYS, *_OPT_IN_KEYS])
|
||||
def test_runtime_performance_switches_are_independent(tmp_path: Path, key: str):
|
||||
# Skill-view switches default on, so independence is checked by turning
|
||||
# one of them off. The other two switches default off and are turned on.
|
||||
# one of them off. The opt-in switches default off and are turned on.
|
||||
enabled = key not in _SKILL_VIEW_KEYS
|
||||
config_path = tmp_path / "config.yaml"
|
||||
config_path.write_text(
|
||||
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.sandbox import sandbox_provider
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def isolated_provider(monkeypatch):
|
||||
monkeypatch.setattr(sandbox_provider, "_default_sandbox_provider", None)
|
||||
monkeypatch.setattr(sandbox_provider, "_sandbox_provider_lock", threading.RLock())
|
||||
monkeypatch.setattr(
|
||||
sandbox_provider,
|
||||
"get_app_config",
|
||||
lambda: SimpleNamespace(sandbox=SimpleNamespace(use="test-provider")),
|
||||
)
|
||||
|
||||
|
||||
def test_parallel_first_get_constructs_only_one_provider(monkeypatch):
|
||||
entered = threading.Event()
|
||||
release = threading.Event()
|
||||
second_attempted = threading.Event()
|
||||
duplicate_constructor = threading.Event()
|
||||
constructed = []
|
||||
|
||||
class Provider:
|
||||
def __init__(self, *, marker):
|
||||
self.marker = marker
|
||||
constructed.append(self)
|
||||
if len(constructed) > 1:
|
||||
duplicate_constructor.set()
|
||||
entered.set()
|
||||
assert release.wait(3), "test did not release provider construction"
|
||||
|
||||
monkeypatch.setattr(sandbox_provider, "resolve_class", lambda *_: Provider)
|
||||
|
||||
def second_get():
|
||||
second_attempted.set()
|
||||
return sandbox_provider.get_sandbox_provider(marker="second")
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
first = executor.submit(sandbox_provider.get_sandbox_provider, marker="first")
|
||||
try:
|
||||
assert entered.wait(3)
|
||||
second = executor.submit(second_get)
|
||||
assert second_attempted.wait(3)
|
||||
assert not duplicate_constructor.wait(0.1)
|
||||
finally:
|
||||
release.set()
|
||||
provider = first.result(timeout=3)
|
||||
assert second.result(timeout=3) is provider
|
||||
|
||||
assert constructed == [provider]
|
||||
assert provider.marker == "first"
|
||||
assert sandbox_provider.get_sandbox_provider(marker="later") is provider
|
||||
|
||||
|
||||
def test_failed_constructor_can_be_retried(monkeypatch):
|
||||
calls = 0
|
||||
|
||||
class Provider:
|
||||
def __init__(self):
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
if calls == 1:
|
||||
raise RuntimeError("temporary initialization failure")
|
||||
|
||||
monkeypatch.setattr(sandbox_provider, "resolve_class", lambda *_: Provider)
|
||||
with pytest.raises(RuntimeError, match="temporary initialization failure"):
|
||||
sandbox_provider.get_sandbox_provider()
|
||||
|
||||
assert sandbox_provider._default_sandbox_provider is None
|
||||
provider = sandbox_provider.get_sandbox_provider()
|
||||
assert sandbox_provider.get_sandbox_provider() is provider
|
||||
assert calls == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize("operation", ["reset", "set", "shutdown"])
|
||||
def test_lifecycle_mutation_waits_for_construction(monkeypatch, operation):
|
||||
entered = threading.Event()
|
||||
release = threading.Event()
|
||||
mutation_started = threading.Event()
|
||||
mutation_finished = threading.Event()
|
||||
replacement = object()
|
||||
|
||||
class Provider:
|
||||
def __init__(self):
|
||||
self.stopped = False
|
||||
entered.set()
|
||||
assert release.wait(3), "test did not release provider construction"
|
||||
|
||||
def shutdown(self):
|
||||
self.stopped = True
|
||||
|
||||
monkeypatch.setattr(sandbox_provider, "resolve_class", lambda *_: Provider)
|
||||
|
||||
def mutate():
|
||||
mutation_started.set()
|
||||
if operation == "reset":
|
||||
sandbox_provider.reset_sandbox_provider()
|
||||
elif operation == "set":
|
||||
sandbox_provider.set_sandbox_provider(replacement)
|
||||
else:
|
||||
sandbox_provider.shutdown_sandbox_provider()
|
||||
mutation_finished.set()
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
first = executor.submit(sandbox_provider.get_sandbox_provider)
|
||||
try:
|
||||
assert entered.wait(3)
|
||||
mutation = executor.submit(mutate)
|
||||
assert mutation_started.wait(3)
|
||||
assert not mutation_finished.wait(0.1)
|
||||
finally:
|
||||
release.set()
|
||||
provider = first.result(timeout=3)
|
||||
mutation.result(timeout=3)
|
||||
|
||||
expected = replacement if operation == "set" else None
|
||||
assert sandbox_provider._default_sandbox_provider is expected
|
||||
assert provider.stopped is (operation == "shutdown")
|
||||
|
||||
|
||||
def test_shutdown_callback_can_reenter_lifecycle_helpers():
|
||||
finished = threading.Event()
|
||||
observed = []
|
||||
|
||||
class Provider:
|
||||
def shutdown(self):
|
||||
observed.append(sandbox_provider.get_sandbox_provider())
|
||||
sandbox_provider.reset_sandbox_provider()
|
||||
|
||||
provider = Provider()
|
||||
sandbox_provider.set_sandbox_provider(provider)
|
||||
|
||||
def shutdown():
|
||||
try:
|
||||
sandbox_provider.shutdown_sandbox_provider()
|
||||
finally:
|
||||
finished.set()
|
||||
|
||||
worker = threading.Thread(target=shutdown, daemon=True)
|
||||
worker.start()
|
||||
assert finished.wait(3), "same-thread lifecycle callback deadlocked"
|
||||
worker.join(timeout=3)
|
||||
assert observed == [provider]
|
||||
assert sandbox_provider._default_sandbox_provider is None
|
||||
+51
@@ -125,3 +125,54 @@ def test_cold_skill_view_uses_the_canonical_directory(tmp_path: Path, monkeypatc
|
||||
prompt._enabled_skills_cache_root = None
|
||||
|
||||
assert "target skill body" in skill_tools.skill_view_impl("target", None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cache_state", ["populated", "empty", "cold", "different_root"])
|
||||
def test_configured_prompt_reuses_only_matching_warm_catalogue(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, prompt_cache, cache_state):
|
||||
root = tmp_path / "skills"
|
||||
storage = LocalSkillStorage(host_path=str(root))
|
||||
_skill(root, "online", description="available")
|
||||
_skill(root, "offline", description="disabled")
|
||||
_skill(root, "archived", description="archived")
|
||||
extensions = tmp_path / "extensions.json"
|
||||
extensions.write_text(json.dumps({"skills": {"offline": {"enabled": False}}}), encoding="utf-8")
|
||||
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(extensions))
|
||||
monkeypatch.setattr("deerflow.skills.usage.all_entries", lambda: {"archived": {"state": "archived"}})
|
||||
loads = []
|
||||
database_reads = []
|
||||
original_load = storage.load_skills
|
||||
|
||||
def load(*, enabled_only):
|
||||
loads.append(enabled_only)
|
||||
return original_load(enabled_only=enabled_only)
|
||||
|
||||
def always_on_names():
|
||||
database_reads.append(True)
|
||||
return {"online"}
|
||||
|
||||
config = object()
|
||||
|
||||
def configured_storage(*, app_config):
|
||||
assert app_config is config
|
||||
return storage
|
||||
|
||||
monkeypatch.setattr(storage, "load_skills", load)
|
||||
monkeypatch.setattr(prompt, "get_or_new_skill_storage", configured_storage)
|
||||
monkeypatch.setattr(prompt, "_load_always_on_names", always_on_names)
|
||||
cached_skill = _skill(tmp_path / "cached", "cached", description="already parsed")
|
||||
cached_skill.always_on = True
|
||||
cached = [] if cache_state == "empty" else [cached_skill]
|
||||
with prompt._enabled_skills_lock:
|
||||
prompt._enabled_skills_cache = None if cache_state == "cold" else cached
|
||||
prompt._enabled_skills_cache_root = str((tmp_path / "other" if cache_state == "different_root" else root).resolve())
|
||||
|
||||
skills = prompt._get_enabled_skills_for_config(config)
|
||||
|
||||
if cache_state in {"populated", "empty"}:
|
||||
assert skills == cached
|
||||
assert skills is not cached
|
||||
assert loads == database_reads == []
|
||||
else:
|
||||
assert [skill.name for skill in skills] == ["online"]
|
||||
assert skills[0].always_on is True
|
||||
assert loads == database_reads == [True]
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
from deerflow.persistence.thread_meta.base import is_excluded_system_thread
|
||||
from deerflow.runtime.task_analysis.thread_metadata import task_analysis_thread_metadata
|
||||
|
||||
|
||||
def test_task_analysis_thread_metadata_links_task_and_run_and_is_hidden_from_chats():
|
||||
metadata = task_analysis_thread_metadata(
|
||||
{"id": "task-108", "source_task_id": "cop-108"},
|
||||
run_id="run-1",
|
||||
role="seat",
|
||||
seat_id="researcher",
|
||||
)
|
||||
|
||||
assert metadata == {
|
||||
"thread_type": "task-analysis",
|
||||
"system": True,
|
||||
"task_analysis_task_id": "task-108",
|
||||
"task_analysis_source_task_id": "cop-108",
|
||||
"task_analysis_run_id": "run-1",
|
||||
"task_analysis_role": "seat",
|
||||
"task_analysis_seat": "researcher",
|
||||
}
|
||||
assert is_excluded_system_thread(metadata) is True
|
||||
|
||||
|
||||
def test_task_analysis_markers_hide_a_legacy_runtime_thread_without_system_flag():
|
||||
assert is_excluded_system_thread({"task_analysis_task_id": "task-108"}) is True
|
||||
+184
-2
@@ -3,8 +3,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from langgraph.store.memory import InMemoryStore
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
@@ -75,11 +79,12 @@ async def test_dedicated_history_is_partitioned_by_original_task_id_and_go_path(
|
||||
metadata={"taskId": "108", "agent_id": "rwfx-agent"},
|
||||
)
|
||||
|
||||
# Listing orders by the canonical thread metadata timestamp/title,
|
||||
# not stale duplicated presentation data in the dedicated index.
|
||||
# Listing reads the dedicated card, not ``threads_meta``. Touching
|
||||
# only the generic row must not change order or title.
|
||||
async with session_factory() as session:
|
||||
row = await session.get(ThreadMetaRow, "rwfx-oldest")
|
||||
assert row is not None
|
||||
row.display_name = "通用表里的新标题"
|
||||
row.updated_at = now + timedelta(minutes=1)
|
||||
await session.commit()
|
||||
|
||||
@@ -92,6 +97,12 @@ async def test_dedicated_history_is_partitioned_by_original_task_id_and_go_path(
|
||||
assert [row["thread_id"] for row in three_q] == ["three-q"]
|
||||
assert [row["thread_id"] for row in xdfx] == ["xdfx-action"]
|
||||
|
||||
await thread_repo.update_display_name("rwfx-oldest", "卡片同步后的标题", user_id=None)
|
||||
await thread_repo.update_status("rwfx-oldest", "running", user_id=None)
|
||||
synced = await history_repo.list_threads("108", "rwfx")
|
||||
assert synced[0]["values"] == {"title": "卡片同步后的标题"}
|
||||
assert synced[0]["status"] == "running"
|
||||
|
||||
async with session_factory() as session:
|
||||
indexed_ids = set((await session.scalars(select(TaskDeeplinkThreadRow.thread_id))).all())
|
||||
assert indexed_ids == {"rwfx-oldest", "three-q", "xdfx-action"}
|
||||
@@ -173,5 +184,176 @@ async def test_memory_mode_uses_the_same_dedicated_partition_contract():
|
||||
assert rwfx[0]["values"] == {"title": "内存任务会话"}
|
||||
assert [row["thread_id"] for row in xdfx] == ["memory-xdfx"]
|
||||
|
||||
await thread_store.update_display_name("memory-rwfx", "内存同步标题", user_id=None)
|
||||
await thread_store.update_status("memory-rwfx", "running", user_id=None)
|
||||
refreshed = await history_store.list_threads("108", "rwfx")
|
||||
assert refreshed[0]["values"] == {"title": "内存同步标题"}
|
||||
assert refreshed[0]["status"] == "running"
|
||||
|
||||
await thread_store.delete("memory-rwfx", user_id=None)
|
||||
assert await history_store.list_threads("108", "rwfx") == []
|
||||
|
||||
|
||||
def _b51_metadata(task_id: str, task_type: str | None) -> dict:
|
||||
metadata: dict = {
|
||||
"taskId": task_id,
|
||||
"taskRecordId": task_id,
|
||||
"taskGoPath": "b5-1",
|
||||
"thread_type": "task_deeplink",
|
||||
}
|
||||
if task_type is not None:
|
||||
metadata["taskType"] = task_type
|
||||
return metadata
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_b51_sub_type_partitions_one_task_history():
|
||||
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
|
||||
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
||||
try:
|
||||
async with engine.begin() as connection:
|
||||
await connection.run_sync(ThreadMetaRow.__table__.create)
|
||||
await connection.run_sync(TaskDeeplinkThreadRow.__table__.create)
|
||||
|
||||
thread_repo = ThreadMetaRepository(session_factory)
|
||||
history_repo = TaskDeeplinkThreadRepository(session_factory)
|
||||
await thread_repo.create("b51-yuqing", user_id="u1", metadata=_b51_metadata("300", "yuqing"))
|
||||
await thread_repo.create("b51-other", user_id="u2", metadata=_b51_metadata("300", "other"))
|
||||
await thread_repo.create("b51-untyped", user_id="u3", metadata=_b51_metadata("300", None))
|
||||
|
||||
# Omitted sub_type keeps the legacy all-sub-types listing.
|
||||
assert sorted(row["thread_id"] for row in await history_repo.list_threads("300", "b5-1")) == [
|
||||
"b51-other",
|
||||
"b51-untyped",
|
||||
"b51-yuqing",
|
||||
]
|
||||
assert [
|
||||
row["thread_id"] for row in await history_repo.list_threads("300", "b5-1", sub_type="yuqing")
|
||||
] == ["b51-yuqing"]
|
||||
# Explicit "" selects the no-type partition, not "every sub-type".
|
||||
assert [
|
||||
row["thread_id"] for row in await history_repo.list_threads("300", "b5-1", sub_type="")
|
||||
] == ["b51-untyped"]
|
||||
assert await history_repo.list_threads("300", "b5-1", sub_type="missing") == []
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_memory_mode_b51_sub_type_partition_contract():
|
||||
raw_store = InMemoryStore()
|
||||
thread_store = MemoryThreadMetaStore(raw_store)
|
||||
history_store = MemoryTaskDeeplinkThreadStore(raw_store, thread_store)
|
||||
|
||||
await thread_store.create("mem-yuqing", user_id="u1", metadata=_b51_metadata("300", "yuqing"))
|
||||
await thread_store.create("mem-untyped", user_id="u2", metadata=_b51_metadata("300", None))
|
||||
|
||||
assert [
|
||||
row["thread_id"] for row in await history_store.list_threads("300", "b5-1", sub_type="yuqing")
|
||||
] == ["mem-yuqing"]
|
||||
assert [
|
||||
row["thread_id"] for row in await history_store.list_threads("300", "b5-1", sub_type="")
|
||||
] == ["mem-untyped"]
|
||||
assert sorted(row["thread_id"] for row in await history_store.list_threads("300", "b5-1")) == [
|
||||
"mem-untyped",
|
||||
"mem-yuqing",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_does_not_join_generic_threads_meta():
|
||||
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
|
||||
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
||||
try:
|
||||
async with engine.begin() as connection:
|
||||
await connection.run_sync(ThreadMetaRow.__table__.create)
|
||||
await connection.run_sync(TaskDeeplinkThreadRow.__table__.create)
|
||||
|
||||
thread_repo = ThreadMetaRepository(session_factory)
|
||||
history_repo = TaskDeeplinkThreadRepository(session_factory)
|
||||
await thread_repo.create(
|
||||
"card-only",
|
||||
user_id="creator",
|
||||
display_name="独立卡片",
|
||||
metadata={
|
||||
"taskId": "77",
|
||||
"taskRecordId": "77",
|
||||
"taskGoPath": "rwfx",
|
||||
"thread_type": "task_deeplink",
|
||||
},
|
||||
)
|
||||
async with session_factory() as session:
|
||||
generic = await session.get(ThreadMetaRow, "card-only")
|
||||
assert generic is not None
|
||||
await session.delete(generic)
|
||||
await session.commit()
|
||||
|
||||
listed = await history_repo.list_threads("77", "rwfx")
|
||||
assert [row["thread_id"] for row in listed] == ["card-only"]
|
||||
assert listed[0]["values"] == {"title": "独立卡片"}
|
||||
assert listed[0]["metadata"]["taskGoPath"] == "rwfx"
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def _migration_config(database: Path) -> Config:
|
||||
migrations = Path(__file__).parents[3] / "packages" / "harness" / "deerflow" / "persistence" / "migrations"
|
||||
config = Config()
|
||||
config.set_main_option("script_location", str(migrations))
|
||||
config.set_main_option("sqlalchemy.url", f"sqlite+aiosqlite:///{database.resolve().as_posix()}")
|
||||
return config
|
||||
|
||||
|
||||
def test_card_migration_backfills_title_status_and_metadata(tmp_path: Path):
|
||||
database = tmp_path / "deeplink-cards.db"
|
||||
engine = sa.create_engine(f"sqlite:///{database.resolve().as_posix()}")
|
||||
try:
|
||||
with engine.begin() as connection:
|
||||
connection.exec_driver_sql(
|
||||
"CREATE TABLE threads_meta ("
|
||||
"thread_id VARCHAR(64) PRIMARY KEY, display_name VARCHAR(256), "
|
||||
"status VARCHAR(20) NOT NULL, metadata_json JSON, "
|
||||
"created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL)"
|
||||
)
|
||||
connection.exec_driver_sql(
|
||||
"CREATE TABLE task_deeplink_threads ("
|
||||
"thread_id VARCHAR(64) PRIMARY KEY, task_id VARCHAR(128) NOT NULL, "
|
||||
"source_task_id VARCHAR(128) NOT NULL, go_path VARCHAR(16) NOT NULL, "
|
||||
"sub_type VARCHAR(64) NOT NULL DEFAULT '', agent_id VARCHAR(128), "
|
||||
"created_by VARCHAR(64), created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL)"
|
||||
)
|
||||
connection.exec_driver_sql(
|
||||
"INSERT INTO threads_meta "
|
||||
"(thread_id, display_name, status, metadata_json, created_at, updated_at) VALUES "
|
||||
"('rwfx-1', '任务分析一', 'running', "
|
||||
"'{\"taskId\":\"108\",\"taskGoPath\":\"rwfx\"}', "
|
||||
"CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
|
||||
)
|
||||
connection.exec_driver_sql(
|
||||
"INSERT INTO task_deeplink_threads "
|
||||
"(thread_id, task_id, source_task_id, go_path, sub_type, created_at, updated_at) "
|
||||
"VALUES ('rwfx-1', '108', '108', 'rwfx', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
|
||||
)
|
||||
connection.exec_driver_sql(
|
||||
"CREATE TABLE alembic_version (version_num VARCHAR(32) NOT NULL PRIMARY KEY)"
|
||||
)
|
||||
connection.exec_driver_sql(
|
||||
"INSERT INTO alembic_version (version_num) VALUES ('20261022_01')"
|
||||
)
|
||||
|
||||
command.upgrade(_migration_config(database), "head")
|
||||
|
||||
with engine.connect() as connection:
|
||||
row = connection.execute(
|
||||
sa.text(
|
||||
"SELECT display_name, status, metadata_json "
|
||||
"FROM task_deeplink_threads WHERE thread_id = 'rwfx-1'"
|
||||
)
|
||||
).one()
|
||||
version = connection.execute(sa.text("SELECT version_num FROM alembic_version")).scalar_one()
|
||||
assert row.display_name == "任务分析一"
|
||||
assert row.status == "running"
|
||||
assert "rwfx" in str(row.metadata_json)
|
||||
assert version == "20261023_03"
|
||||
finally:
|
||||
engine.dispose()
|
||||
+284
@@ -0,0 +1,284 @@
|
||||
"""Lazy creation and history isolation through real HTTP, SQL and graph paths."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from contextlib import asynccontextmanager
|
||||
from types import SimpleNamespace
|
||||
from typing import Annotated, TypedDict
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
from langgraph.checkpoint.base import empty_checkpoint
|
||||
from langgraph.checkpoint.memory import InMemorySaver
|
||||
from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
|
||||
from langgraph.graph import END, START, StateGraph
|
||||
from langgraph.graph.message import add_messages
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.gateway.authz import AuthContext
|
||||
from app.gateway.routers import threads
|
||||
from deerflow.persistence.task_deeplink_threads.model import TaskDeeplinkThreadRow
|
||||
from deerflow.persistence.thread_meta.model import ThreadMetaRow
|
||||
from deerflow.persistence.thread_meta.sql import ThreadMetaRepository
|
||||
from deerflow.runtime.user_context import reset_current_user, set_current_user
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def client_for(saver):
|
||||
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(ThreadMetaRow.__table__.create)
|
||||
await conn.run_sync(TaskDeeplinkThreadRow.__table__.create)
|
||||
app = FastAPI()
|
||||
app.state.thread_store = ThreadMetaRepository(async_sessionmaker(engine, expire_on_commit=False))
|
||||
app.state.checkpointer = saver
|
||||
app.include_router(threads.router)
|
||||
|
||||
@app.middleware("http")
|
||||
async def authenticated(request, call_next):
|
||||
name = request.headers.get("X-User", "owner")
|
||||
user = SimpleNamespace(id=name) if name != "anonymous" else None
|
||||
request.state.auth = AuthContext(user, ["threads:read", "threads:write", "runs:read", "runs:create", "runs:cancel"] if user else [])
|
||||
token = set_current_user(user) if user else None
|
||||
try:
|
||||
return await call_next(request)
|
||||
finally:
|
||||
if token:
|
||||
reset_current_user(token)
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as client:
|
||||
yield client, app
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def test_create_list_and_metadata_do_not_touch_checkpoint(monkeypatch):
|
||||
async def run():
|
||||
async with client_for(InMemorySaver()) as (client, app):
|
||||
def unavailable(_):
|
||||
raise AssertionError("Metadata operation accessed the checkpoint backend")
|
||||
|
||||
monkeypatch.setattr(threads, "get_checkpointer", unavailable)
|
||||
metadata = {"taskId": "task", "actionId": "action", "agent_id": "agent", "owner_id": "forged"}
|
||||
created = await client.post("/api/threads", json={"thread_id": "lazy", "metadata": metadata})
|
||||
assert created.status_code == 200, created.text
|
||||
assert created.json()["metadata"] == {k: v for k, v in metadata.items() if k != "owner_id"}
|
||||
assert "checkpoint_create" not in created.headers["server-timing"]
|
||||
duplicate = await client.post("/api/threads", json={"thread_id": "lazy", "metadata": {"changed": True}})
|
||||
assert duplicate.json()["metadata"] == created.json()["metadata"]
|
||||
record = await app.state.thread_store.get("lazy", user_id=None)
|
||||
assert record["user_id"] == "owner"
|
||||
listing = await client.post("/api/threads/search", json={"metadata": {"taskId": "task", "actionId": "action", "agent_id": "agent"}}, headers={"X-User": "other"})
|
||||
assert [row["thread_id"] for row in listing.json()] == ["lazy"]
|
||||
info = await client.get("/api/threads/lazy", headers={"X-User": "other"})
|
||||
assert info.status_code == 200
|
||||
assert info.json()["metadata"] == created.json()["metadata"]
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_roundtable_init_stream_creates_only_metadata_and_keeps_seat_mapping(monkeypatch):
|
||||
from app.gateway.routers import multi_agent
|
||||
|
||||
monkeypatch.setattr(multi_agent, "ensure_roundtable_functional_agents", lambda: None)
|
||||
monkeypatch.setattr(multi_agent, "_get_agents_batch_direct", AsyncMock(return_value={"seat-a": {"name": "A"}, "seat-b": {"name": "B"}}))
|
||||
monkeypatch.setattr(multi_agent, "_make_loopback_client", lambda *_: pytest.fail("init must not seed checkpoint via HTTP"))
|
||||
async with client_for(InMemorySaver()) as (client, app):
|
||||
del app.state.checkpointer
|
||||
app.include_router(multi_agent.router)
|
||||
response = await client.post("/api/multi-agent/init", json={"agent_names": ["seat-a", "seat-b"]})
|
||||
assert response.status_code == 200
|
||||
frames = [json.loads(line.removeprefix("data: ")) for line in response.text.splitlines() if line.startswith("data: ")]
|
||||
assert [frame["event"] for frame in frames] == ["seat_ready", "seat_ready", "coordinator_ready", "init_done"]
|
||||
mapping = frames[-1]["thread_ids"]
|
||||
assert set(mapping) == {"seat-a", "seat-b", multi_agent.COORDINATOR_AGENT_ID}
|
||||
for thread_id in mapping.values():
|
||||
record = await app.state.thread_store.get(thread_id, user_id=None)
|
||||
assert record["metadata"]["thread_type"] == "roundtable"
|
||||
|
||||
|
||||
def test_lazy_create_completes_while_real_saver_history_lock_is_held():
|
||||
async def run():
|
||||
async with AsyncSqliteSaver.from_conn_string(":memory:") as saver:
|
||||
await saver.aput({"configurable": {"thread_id": "old", "checkpoint_ns": ""}}, empty_checkpoint(), {}, {})
|
||||
history = saver.alist({"configurable": {"thread_id": "old"}})
|
||||
await anext(history)
|
||||
try:
|
||||
assert saver.lock.locked()
|
||||
async with client_for(saver) as (client, _):
|
||||
results = await asyncio.wait_for(asyncio.gather(*(client.post("/api/threads", json={"thread_id": f"new-{i}"}) for i in range(20))), timeout=3)
|
||||
assert all(response.status_code == 200 for response in results)
|
||||
assert saver.lock.locked()
|
||||
finally:
|
||||
await history.aclose()
|
||||
assert await saver.aget_tuple({"configurable": {"thread_id": "new-0"}}) is None
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_first_run_initializes_checkpoint_and_second_run_keeps_conversation():
|
||||
class State(TypedDict):
|
||||
messages: Annotated[list, add_messages]
|
||||
|
||||
async def run():
|
||||
async with AsyncSqliteSaver.from_conn_string(":memory:") as saver, client_for(saver) as (client, _):
|
||||
response = await client.post("/api/threads", json={"thread_id": "conversation"})
|
||||
assert response.status_code == 200
|
||||
config = {"configurable": {"thread_id": "conversation", "checkpoint_ns": ""}}
|
||||
assert await saver.aget_tuple(config) is None
|
||||
graph = StateGraph(State)
|
||||
graph.add_node("answer", lambda state: {"messages": [AIMessage(content=f"reply {len(state['messages'])}")]})
|
||||
graph.add_edge(START, "answer")
|
||||
graph.add_edge("answer", END)
|
||||
compiled = graph.compile(checkpointer=saver)
|
||||
first = await compiled.ainvoke({"messages": [HumanMessage(content="one")]}, config)
|
||||
assert len(first["messages"]) == 2
|
||||
second = await compiled.ainvoke({"messages": [HumanMessage(content="two")]}, config)
|
||||
assert [message.content for message in second["messages"]] == ["one", "reply 1", "two", "reply 3"]
|
||||
history = await client.post("/api/threads/conversation/history", json={"limit": 1})
|
||||
assert [message["content"] for message in history.json()[0]["values"]["messages"]] == ["one", "reply 1", "two", "reply 3"]
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("shared", [False, True])
|
||||
def test_metadata_only_threads_keep_private_and_task_sharing_rules(shared):
|
||||
async def run():
|
||||
async with client_for(InMemorySaver()) as (client, _):
|
||||
metadata = {"taskId": "task", "actionId": "action", "agent_id": "agent"} if shared else {"agent_id": "agent"}
|
||||
assert (await client.post("/api/threads", json={"thread_id": "lazy", "metadata": metadata})).status_code == 200
|
||||
for user, expected in [("owner", 200), ("other", 200 if shared else 404), ("anonymous", 401)]:
|
||||
headers = {"X-User": user}
|
||||
info = await client.get("/api/threads/lazy", headers=headers)
|
||||
state = await client.get("/api/threads/lazy/state", headers=headers)
|
||||
history = await client.post("/api/threads/lazy/history", json={"limit": 1}, headers=headers)
|
||||
assert info.status_code == state.status_code == history.status_code == expected
|
||||
if expected == 200:
|
||||
assert state.json()["values"] == {}
|
||||
assert state.json()["metadata"] == metadata
|
||||
assert history.json() == []
|
||||
assert (await client.get("/api/threads/missing/state")).status_code == 404
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_limit_one_history_uses_latest_tuple_not_the_iterator(monkeypatch):
|
||||
class State(TypedDict):
|
||||
messages: Annotated[list, add_messages]
|
||||
|
||||
async def run():
|
||||
async with client_for(InMemorySaver()) as (client, app):
|
||||
config = {"configurable": {"thread_id": "hello", "checkpoint_ns": ""}}
|
||||
graph = StateGraph(State)
|
||||
graph.add_node("answer", lambda state: {"messages": [AIMessage(content="你好")]})
|
||||
graph.add_edge(START, "answer")
|
||||
graph.add_edge("answer", END)
|
||||
compiled = graph.compile(checkpointer=app.state.checkpointer)
|
||||
await compiled.ainvoke({"messages": [HumanMessage(content="hi")]}, config)
|
||||
|
||||
async def forbidden(*_args, **_kwargs):
|
||||
raise AssertionError("limit=1 history must not walk alist")
|
||||
yield None
|
||||
|
||||
monkeypatch.setattr(app.state.checkpointer, "alist", forbidden)
|
||||
result = await client.post("/api/threads/hello/history", json={"limit": 1})
|
||||
assert result.status_code == 200, result.text
|
||||
contents = [message["content"] for message in result.json()[0]["values"]["messages"]]
|
||||
assert contents == ["hi", "你好"]
|
||||
assert "checkpoint_load" in result.headers["server-timing"]
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_history_releases_saver_lock_before_serialization_and_ignores_subgraphs(monkeypatch):
|
||||
async def run():
|
||||
async with AsyncSqliteSaver.from_conn_string(":memory:") as saver, client_for(saver) as (client, _):
|
||||
for namespace, title in [("", "root"), ("agent:subgraph", "child")]:
|
||||
checkpoint = empty_checkpoint()
|
||||
checkpoint["channel_values"] = {"title": title, "messages": [AIMessage(content=title)], "todos": [title]}
|
||||
await saver.aput({"configurable": {"thread_id": "thread", "checkpoint_ns": namespace}}, checkpoint, {}, {})
|
||||
serialize = threads.serialize_channel_values
|
||||
|
||||
def after_release(values, **kwargs):
|
||||
assert not saver.lock.locked(), "Serialization held the shared saver lock"
|
||||
return serialize(values, **kwargs)
|
||||
|
||||
monkeypatch.setattr(threads, "serialize_channel_values", after_release)
|
||||
result = await client.post("/api/threads/thread/history", json={"limit": 10})
|
||||
assert result.status_code == 200, result.text
|
||||
assert len(result.json()) == 1
|
||||
assert result.json()[0]["values"]["todos"] == ["root"]
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_cancelled_history_closes_iterator():
|
||||
async def run():
|
||||
closed = asyncio.Event()
|
||||
entered = asyncio.Event()
|
||||
|
||||
class Saver:
|
||||
async def alist(self, *args, **kwargs):
|
||||
try:
|
||||
entered.set()
|
||||
await asyncio.Event().wait()
|
||||
yield None
|
||||
finally:
|
||||
closed.set()
|
||||
|
||||
async with client_for(Saver()) as (client, _):
|
||||
task = asyncio.create_task(client.post("/api/threads/thread/history", json={"limit": 1}))
|
||||
await entered.wait()
|
||||
task.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await task
|
||||
assert closed.is_set()
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kind", ["foreground", "background"])
|
||||
def test_roundtable_internal_creation_defers_checkpoint_and_can_run_twice(kind):
|
||||
from starlette.requests import Request
|
||||
|
||||
from app.gateway import roundtable_inprocess_gateway
|
||||
from app.gateway.routers import multi_agent
|
||||
|
||||
class State(TypedDict):
|
||||
messages: Annotated[list, add_messages]
|
||||
|
||||
async def run():
|
||||
saver = InMemorySaver()
|
||||
async with client_for(saver) as (client, app):
|
||||
token = set_current_user(SimpleNamespace(id="owner"))
|
||||
try:
|
||||
# Both helpers must work without even obtaining a checkpointer.
|
||||
del app.state.checkpointer
|
||||
if kind == "foreground":
|
||||
tid = await multi_agent._create_thread_direct(Request({"type": "http", "app": app}), metadata={"thread_type": "roundtable", "taskId": "shared", "agent_id": "agent"})
|
||||
else:
|
||||
tid = await roundtable_inprocess_gateway._create_thread(app)
|
||||
record = await app.state.thread_store.get(tid)
|
||||
assert record["metadata"]["thread_type"] == "roundtable"
|
||||
assert record["user_id"] == "owner"
|
||||
config = {"configurable": {"thread_id": tid, "checkpoint_ns": ""}}
|
||||
assert await saver.aget_tuple(config) is None
|
||||
app.state.checkpointer = saver
|
||||
assert (await client.get(f"/api/threads/{tid}", headers={"X-User": "other"})).status_code == 200
|
||||
graph = StateGraph(State)
|
||||
graph.add_node("answer", lambda state: {"messages": [AIMessage(content="reply")]})
|
||||
graph.add_edge(START, "answer")
|
||||
graph.add_edge("answer", END)
|
||||
compiled = graph.compile(checkpointer=saver)
|
||||
await compiled.ainvoke({"messages": [HumanMessage(content="first")]}, config)
|
||||
result = await compiled.ainvoke({"messages": [HumanMessage(content="second")]}, config)
|
||||
assert [message.content for message in result["messages"]] == ["first", "reply", "second", "reply"]
|
||||
finally:
|
||||
reset_current_user(token)
|
||||
|
||||
asyncio.run(run())
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
"""Search/count parity, ownership and shared-task regressions for upstream #2865."""
|
||||
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import UTC, datetime
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import event, select
|
||||
from sqlalchemy.dialects import mysql, postgresql, sqlite
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from deerflow.persistence.json_compat import JsonPredicate
|
||||
from deerflow.persistence.task_deeplink_threads.model import TaskDeeplinkThreadRow
|
||||
from deerflow.persistence.thread_meta.base import is_excluded_system_thread
|
||||
from deerflow.persistence.thread_meta.model import ThreadMetaRow
|
||||
from deerflow.persistence.thread_meta.sql import ThreadMetaRepository, _indexed_thread_metadata
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def repository(rows):
|
||||
engine = create_async_engine("sqlite+aiosqlite://")
|
||||
try:
|
||||
async with engine.begin() as connection:
|
||||
await connection.run_sync(ThreadMetaRow.__table__.create)
|
||||
await connection.run_sync(TaskDeeplinkThreadRow.__table__.create)
|
||||
sf = async_sessionmaker(engine, expire_on_commit=False)
|
||||
now = datetime.now(UTC)
|
||||
async with sf() as session:
|
||||
session.add_all(
|
||||
[
|
||||
ThreadMetaRow(
|
||||
thread_id=f"t{i:04}",
|
||||
user_id=owner,
|
||||
metadata_json=metadata,
|
||||
**_indexed_thread_metadata(metadata),
|
||||
display_name=f"Analysis {i}",
|
||||
status="idle",
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
for i, (owner, metadata) in enumerate(rows)
|
||||
]
|
||||
)
|
||||
await session.commit()
|
||||
yield ThreadMetaRepository(sf), engine
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("wanted", [None, False, True, 0, 1, 1.0, 1.5, "1", "Task", "task", "task ", 2**53 + 1])
|
||||
def test_scalar_filters_preserve_python_matching_and_count(wanted):
|
||||
async def run():
|
||||
values = [None, False, True, 0, 1, 1.0, 1.5, "1", "Task", "task", "task ", [], {}, 2**53, 2**53 + 1]
|
||||
rows = [("a", {"key": value}) for value in values] + [("a", {})]
|
||||
expected = [f"t{i:04}" for i, (_, md) in enumerate(rows) if md.get("key") == wanted][::-1]
|
||||
async with repository(rows) as (repo, _):
|
||||
matches = await repo.search(user_id="a", metadata={"key": wanted})
|
||||
assert [r["thread_id"] for r in matches] == expected
|
||||
assert await repo.count(user_id="a", metadata={"key": wanted}) == len(expected)
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_system_exclusion_matches_legacy_truthiness_and_stripping():
|
||||
async def run():
|
||||
metadata = [
|
||||
{},
|
||||
*({"system": v} for v in [None, False, 0, 0.0, "", [], {}, True, 1, "false", [0], {"x": 0}, "\0"]),
|
||||
*({"task_analysis_seat": v} for v in [None, 1, "", " \t\n", "\u3000", " seat ", "\u3000seat", "\0"]),
|
||||
{"thread_type": "roundtable"},
|
||||
{"thread_type": "task-analysis-qa"},
|
||||
{"thread_type": "ordinary"},
|
||||
{"thread_type": 1},
|
||||
]
|
||||
expected = [f"t{i:04}" for i, md in enumerate(metadata) if not is_excluded_system_thread(md)][::-1]
|
||||
async with repository([("a", md) for md in metadata]) as (repo, _):
|
||||
results = await repo.search(user_id="a", exclude_system=True)
|
||||
assert [r["thread_id"] for r in results] == expected
|
||||
assert await repo.count(user_id="a", exclude_system=True) == len(expected)
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_shared_task_keeps_agent_and_action_filters_owner_checks_and_pagination():
|
||||
async def run():
|
||||
rows = [
|
||||
("a", {"taskId": "42", "agent_id": "agent-a", "actionId": "action-a"}),
|
||||
("b", {"taskId": "42", "agent_id": "agent-a", "actionId": "action-a"}),
|
||||
("b", {"taskId": "42", "agent_id": "agent-b", "actionId": "action-a"}),
|
||||
("b", {"taskId": "42", "agent_id": "agent-a", "actionId": "action-b"}),
|
||||
("b", {"taskId": "43", "agent_id": "agent-a", "actionId": "action-a"}),
|
||||
("b", {"agent_id": "agent-a"}),
|
||||
("a", {"agent_id": "agent-a"}),
|
||||
("b", {"taskId": "42", "agent_id": "agent-a", "actionId": "action-a", "system": True}),
|
||||
]
|
||||
async with repository(rows) as (repo, _):
|
||||
filters = dict(user_id="viewer", metadata={"taskId": "42", "agent_id": "agent-a", "actionId": "action-a"}, exclude_system=True)
|
||||
assert [r["thread_id"] for r in await repo.search(**filters)] == ["t0001", "t0000"]
|
||||
assert await repo.count(**filters) == 2
|
||||
assert [r["thread_id"] for r in await repo.search(**filters, limit=1, offset=1)] == ["t0000"]
|
||||
assert await repo.count(**filters, query="Analysis 1", status="idle") == 1
|
||||
assert await repo.count(**filters, status="busy") == 0
|
||||
ordinary = await repo.search(user_id="a", metadata={"agent_id": "agent-a"}, exclude_system=True)
|
||||
assert {r["thread_id"] for r in ordinary} == {"t0000", "t0006"}
|
||||
assert await repo.check_access("t0000", "viewer", require_existing=True)
|
||||
assert not await repo.check_access("t0005", "viewer", require_existing=True)
|
||||
assert await repo.get("t0005", user_id="viewer") is None
|
||||
await repo.update_display_name("t0000", "shared edit", user_id="viewer")
|
||||
assert (await repo.get("t0000", user_id=None))["display_name"] == "shared edit"
|
||||
await repo.update_display_name("t0005", "private edit", user_id="viewer")
|
||||
assert (await repo.get("t0005", user_id=None))["display_name"] == "Analysis 5"
|
||||
assert not await repo.check_access("absent", "viewer", require_existing=True)
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("metadata", [{"nested": {"a": [1, 2]}}, {"odd.key": "value"}, {"key' OR 1=1 --": "value"}, {"big": 2**70}])
|
||||
def test_residual_filters_are_never_dropped(metadata):
|
||||
async def run():
|
||||
async with repository([("a", {}), ("a", metadata), ("a", {"nested": {"a": [2, 1]}})]) as (repo, _):
|
||||
assert [r["thread_id"] for r in await repo.search(user_id="a", metadata=metadata)] == ["t0001"]
|
||||
assert await repo.count(user_id="a", metadata=metadata) == 1
|
||||
assert await repo.search(user_id="a", metadata=metadata, offset=1) == []
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
def test_sparse_task_search_and_count_each_execute_one_select():
|
||||
async def run():
|
||||
rows = [("a", {"taskId": "other", "system": True}) for _ in range(1200)]
|
||||
rows += [("b", {"taskId": "target", "agent_id": "agent", "actionId": "action"}) for _ in range(3)]
|
||||
async with repository(rows) as (repo, engine):
|
||||
statements = []
|
||||
event.listen(engine.sync_engine, "before_cursor_execute", lambda conn, cursor, statement, parameters, context, many: statements.append(statement))
|
||||
filters = dict(user_id="viewer", metadata={"taskId": "target", "agent_id": "agent", "actionId": "action"}, exclude_system=True)
|
||||
assert [r["thread_id"] for r in await repo.search(**filters, limit=1, offset=1)] == ["t1201"]
|
||||
assert await repo.count(**filters) == 3
|
||||
assert len(statements) == 2
|
||||
assert "count(*)" in statements[1].lower()
|
||||
assert "json_" in statements[0].lower()
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("dialect", [sqlite.dialect(), postgresql.dialect(), mysql.dialect()])
|
||||
def test_sql_binds_user_values_and_compiles_on_supported_dialects(dialect):
|
||||
value = "Task' OR 1=1 --"
|
||||
conditions, residual, filter_system = ThreadMetaRepository._query_conditions(
|
||||
dialect=dialect.name,
|
||||
metadata={"taskId": value, "agent_id": "Agent"},
|
||||
status=None,
|
||||
user_id="owner",
|
||||
exclude_system=True,
|
||||
query=None,
|
||||
)
|
||||
assert not residual and not filter_system
|
||||
compiled = select(ThreadMetaRow.thread_id).where(*conditions).compile(dialect=dialect)
|
||||
assert value not in str(compiled)
|
||||
assert value in compiled.params.values()
|
||||
assert "owner" in compiled.params.values()
|
||||
if dialect.name == "mysql":
|
||||
assert "AS BINARY" in str(compiled)
|
||||
|
||||
|
||||
def test_gauss_and_server_numeric_filters_keep_exact_compatibility_path():
|
||||
for dialect, metadata in [
|
||||
("opengauss", {"taskId": "42"}),
|
||||
("mysql", {"taskId": 42, "flag": True, "fraction": 1e-30}),
|
||||
("postgresql", {"taskId": 1000000000000000100, "fraction": 1.0000000000000001e18}),
|
||||
]:
|
||||
_, residual, filter_system = ThreadMetaRepository._query_conditions(
|
||||
dialect=dialect,
|
||||
metadata=metadata,
|
||||
status=None,
|
||||
user_id=None,
|
||||
exclude_system=True,
|
||||
query=None,
|
||||
)
|
||||
assert residual == metadata
|
||||
assert filter_system is (dialect == "opengauss")
|
||||
|
||||
|
||||
def test_compiled_cache_does_not_mix_bool_and_integer_predicates():
|
||||
column = ThreadMetaRow.metadata_json
|
||||
assert JsonPredicate(column, "key", value=True)._generate_cache_key().key != JsonPredicate(column, "key", value=1)._generate_cache_key().key
|
||||
+4
-4
@@ -50,7 +50,7 @@ def test_twenty_creates_preserve_immediate_checkpoints_and_do_not_log_content(ca
|
||||
async def run():
|
||||
saver = InMemorySaver()
|
||||
async with client_for(saver) as client:
|
||||
responses = await asyncio.gather(*(client.post("/api/threads", json={"metadata": {"private_content": "do-not-log-me"}}) for _ in range(20)))
|
||||
responses = await asyncio.gather(*(client.post("/api/threads", json={"initialize_checkpoint": True, "metadata": {"private_content": "do-not-log-me"}}) for _ in range(20)))
|
||||
ids = set()
|
||||
for response in responses:
|
||||
assert response.status_code == 200
|
||||
@@ -125,7 +125,7 @@ def test_browser_context_calls_real_create_thread_without_response(monkeypatch,
|
||||
result = await browser_context.ask_browser_context.__wrapped__(body, request, config)
|
||||
assert result.success
|
||||
assert result.thread_id in app.state.thread_store.rows
|
||||
assert await app.state.checkpointer.aget_tuple({"configurable": {"thread_id": result.thread_id}}) is not None
|
||||
assert await app.state.checkpointer.aget_tuple({"configurable": {"thread_id": result.thread_id}}) is None
|
||||
|
||||
asyncio.run(run())
|
||||
|
||||
@@ -154,7 +154,7 @@ def test_sqlite_history_lock_blocks_create_but_not_metadata_search_or_idempotenc
|
||||
await anext(history)
|
||||
assert saver.lock.locked()
|
||||
async with client_for(saver, store) as client:
|
||||
create = asyncio.create_task(client.post("/api/threads", json={"thread_id": "new"}))
|
||||
create = asyncio.create_task(client.post("/api/threads", json={"thread_id": "new", "initialize_checkpoint": True}))
|
||||
try:
|
||||
search = await asyncio.wait_for(client.post("/api/threads/search", json={}), timeout=2)
|
||||
existing = await asyncio.wait_for(client.post("/api/threads", json={"thread_id": "existing"}), timeout=2)
|
||||
@@ -192,7 +192,7 @@ def test_failures_keep_timing_headers_and_existing_error_contract(stage, caplog)
|
||||
saver = InMemorySaver() if stage == "metadata_create" else FailingSaver()
|
||||
store = FailingStore() if stage == "metadata_create" else ThreadStore()
|
||||
async with client_for(saver, store) as client:
|
||||
response = await client.post("/api/threads", json={})
|
||||
response = await client.post("/api/threads", json={"initialize_checkpoint": True})
|
||||
assert response.status_code == 500
|
||||
assert response.json() == {"detail": "Failed to create thread"}
|
||||
assert stage in phase_ms(response)
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
"""Pausing audit writes must preserve tool results and runtime error handling."""
|
||||
|
||||
from functools import partial
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
from langchain_core.messages import ToolMessage
|
||||
from langgraph.errors import GraphBubbleUp
|
||||
|
||||
from deerflow.agents.middlewares import tool_metrics_middleware as metrics
|
||||
from deerflow.agents.middlewares.llm_metrics_middleware import LlmMetricsMiddleware
|
||||
from deerflow.agents.middlewares.tool_error_handling_middleware import (
|
||||
ToolErrorHandlingMiddleware,
|
||||
build_lead_runtime_middlewares,
|
||||
build_subagent_runtime_middlewares,
|
||||
)
|
||||
from deerflow.config.app_config import AppConfig
|
||||
|
||||
BUILDERS = [build_lead_runtime_middlewares, build_subagent_runtime_middlewares]
|
||||
|
||||
|
||||
def _config(enabled=None):
|
||||
options = {} if enabled is None else {"tool_metrics_enabled": enabled}
|
||||
return AppConfig(
|
||||
sandbox={"use": "deerflow.sandbox.local:LocalSandboxProvider"},
|
||||
runtime_performance=options,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("builder", BUILDERS)
|
||||
@pytest.mark.parametrize("enabled", [None, False])
|
||||
def test_paused_runtime_never_constructs_a_recorder(builder, enabled, monkeypatch):
|
||||
constructor = Mock(side_effect=AssertionError("paused runtime constructed a DB recorder"))
|
||||
monkeypatch.setattr(metrics, "ToolMetricsMiddleware", constructor)
|
||||
|
||||
middlewares = builder(app_config=_config(enabled))
|
||||
|
||||
constructor.assert_not_called()
|
||||
assert any(isinstance(m, ToolErrorHandlingMiddleware) for m in middlewares)
|
||||
assert any(isinstance(m, LlmMetricsMiddleware) for m in middlewares)
|
||||
|
||||
|
||||
def _tool_wrappers(builder, enabled):
|
||||
# Exercise the actual error/audit wrappers selected by both runtime builders.
|
||||
return [m for m in builder(app_config=_config(enabled)) if isinstance(m, (ToolErrorHandlingMiddleware, metrics.ToolMetricsMiddleware))]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("builder", BUILDERS)
|
||||
@pytest.mark.parametrize("enabled", [False, True])
|
||||
@pytest.mark.parametrize("fails", [False, True])
|
||||
def test_sync_tool_execution_and_error_conversion_survive_pause(builder, enabled, fails, monkeypatch):
|
||||
flushed = Mock()
|
||||
monkeypatch.setattr(metrics.ToolMetricsMiddleware, "_flush", lambda _, record: flushed(record))
|
||||
request = SimpleNamespace(tool_call={"id": "call-1", "name": "bash", "args": {}})
|
||||
expected = ToolMessage(content="actual tool output", tool_call_id="call-1", name="bash")
|
||||
|
||||
def tool(req):
|
||||
assert req is request
|
||||
if fails:
|
||||
raise ValueError("tool failed")
|
||||
return expected
|
||||
|
||||
handler = tool
|
||||
wrappers = _tool_wrappers(builder, enabled)
|
||||
if enabled:
|
||||
assert isinstance(wrappers[-1], metrics.ToolMetricsMiddleware)
|
||||
for middleware in reversed(wrappers):
|
||||
handler = partial(middleware.wrap_tool_call, handler=handler)
|
||||
result = handler(request)
|
||||
|
||||
assert result.status == ("error" if fails else "success")
|
||||
if not fails:
|
||||
assert result is expected
|
||||
assert flushed.call_count == int(enabled)
|
||||
if enabled:
|
||||
assert flushed.call_args.args[0].status == result.status
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("builder", BUILDERS)
|
||||
@pytest.mark.parametrize("enabled", [False, True])
|
||||
@pytest.mark.parametrize("fails", [False, True])
|
||||
async def test_async_tool_execution_and_error_conversion_survive_pause(builder, enabled, fails, monkeypatch):
|
||||
flushed = Mock()
|
||||
monkeypatch.setattr(metrics.ToolMetricsMiddleware, "_flush", lambda _, record: flushed(record))
|
||||
request = SimpleNamespace(tool_call={"id": "call-1", "name": "bash", "args": {}})
|
||||
expected = ToolMessage(content="actual tool output", tool_call_id="call-1", name="bash")
|
||||
|
||||
async def tool(req):
|
||||
assert req is request
|
||||
if fails:
|
||||
raise ValueError("tool failed")
|
||||
return expected
|
||||
|
||||
handler = tool
|
||||
for middleware in reversed(_tool_wrappers(builder, enabled)):
|
||||
handler = partial(middleware.awrap_tool_call, handler=handler)
|
||||
result = await handler(request)
|
||||
|
||||
assert result.status == ("error" if fails else "success")
|
||||
if not fails:
|
||||
assert result is expected
|
||||
assert flushed.call_count == int(enabled)
|
||||
if enabled:
|
||||
assert flushed.call_args.args[0].status == result.status
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("builder", BUILDERS)
|
||||
@pytest.mark.parametrize("enabled", [False, True])
|
||||
async def test_pause_does_not_intercept_graph_control_flow(builder, enabled, monkeypatch):
|
||||
flushed = Mock()
|
||||
monkeypatch.setattr(metrics.ToolMetricsMiddleware, "_flush", lambda _, record: flushed(record))
|
||||
request = SimpleNamespace(tool_call={"id": "call-1", "name": "bash", "args": {}})
|
||||
signal = GraphBubbleUp("pause")
|
||||
|
||||
async def tool(_):
|
||||
raise signal
|
||||
|
||||
handler = tool
|
||||
for middleware in reversed(_tool_wrappers(builder, enabled)):
|
||||
handler = partial(middleware.awrap_tool_call, handler=handler)
|
||||
|
||||
with pytest.raises(GraphBubbleUp) as exc:
|
||||
await handler(request)
|
||||
assert exc.value is signal
|
||||
flushed.assert_not_called()
|
||||
Reference in new issue
Block a user