test(backend): prune 32 zero-unique redundant suites and rewrite 9 source-inspection tests (#8362)

* test(backend): prune 32 zero-unique redundant suites and rewrite 9 source-inspection tests

- Remove 32 superseded test files proven lossless across statement/branch coverage (per-test dynamic contexts), hotspot mutation testing (mutmut), and 20 historical bug replays.
- Replace all inspect.getsource and brittle source-string assertions across 9 test files with behavioral runtime assertions.

* test(backend): consolidate hidden-answer and compaction-hash guards and assert protocol defaults

- Fold hidden human-input turn-boundary regressions into tests/test_channels.py while keeping test_channel_hidden_human_input.py removed.
- Fold multimodal source content hashing and unobserved-skip regressions into tests/test_compaction_event_identity.py.
- Assert contract-specific default return values/types (False for bool registrations, empty sequence for middleware contributions, explicit return None for void hooks) in test_extension_api_contracts.py and add stub rejection tests.
This commit is contained in:
Xinmin Zeng 2026-11-18 05:13:19 -08:00 committed by GitHub
parent 340822f2c1
commit df544f126a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
43 changed files with 720 additions and 3307 deletions

View File

@ -1,194 +0,0 @@
from langchain_core.messages import ToolMessage
from langgraph.prebuilt.tool_node import ToolCallRequest
from deerflow.agents.middlewares.artifact_resolution_middleware import ArtifactResolutionMiddleware
from deerflow.config.tool_artifact_config import ToolArtifactConfig
from deerflow.tools.artifact_registry import generate_handle
class _FakeToolCallRequest:
def __init__(self, tool_call: dict, state: dict) -> None:
self.tool_call = tool_call
self.state = state
def override(self, **overrides):
return _FakeToolCallRequest(
overrides.get("tool_call", self.tool_call),
overrides.get("state", self.state),
)
def _request(tool_call: dict, artifacts: list[dict]):
return _FakeToolCallRequest(tool_call, {"tool_artifacts": artifacts})
def _artifact(thread_id: str, call_index: int, real_ref: str) -> dict:
return {
"handle": generate_handle(thread_id, f"call_{call_index}", call_index),
"artifact_type": "file",
"real_ref": real_ref,
"display_name": real_ref.rsplit("/", 1)[-1],
"tool_name": "write_file",
"mime_type": None,
"consumed_by": [],
}
class TestResolution:
def test_resolves_bare_handle_in_args(self):
middleware = ArtifactResolutionMiddleware()
handle = generate_handle("thread_1", "call_0", 0)
request = _request(
{"name": "read_file", "args": {"path": handle}, "id": "call_2", "type": "tool_call"},
[_artifact("thread_1", 0, "/mnt/user-data/outputs/report.md")],
)
seen = {}
def handler(req):
seen["args"] = req.tool_call["args"]
return ToolMessage(content="ok", tool_call_id="call_2")
result = middleware.wrap_tool_call(request, handler)
assert seen["args"] == {"path": "/mnt/user-data/outputs/report.md"}
assert result.content == "ok"
def test_resolves_backticked_handle_in_args(self):
middleware = ArtifactResolutionMiddleware()
handle = generate_handle("thread_1", "call_0", 0)
request = _request(
{"name": "bash", "args": {"command": f"cat `{handle}`"}, "id": "call_2", "type": "tool_call"},
[_artifact("thread_1", 0, "/mnt/user-data/outputs/report.md")],
)
seen = {}
def handler(req):
seen["args"] = req.tool_call["args"]
return ToolMessage(content="ok", tool_call_id="call_2")
middleware.wrap_tool_call(request, handler)
assert seen["args"] == {"command": "cat /mnt/user-data/outputs/report.md"}
def test_resolves_nested_handle(self):
middleware = ArtifactResolutionMiddleware()
handle = generate_handle("thread_1", "call_0", 0)
request = _request(
{
"name": "bash",
"args": {"command": "echo hi", "cwd": None, "files": [{"path": handle, "mode": "r"}]},
"id": "call_2",
"type": "tool_call",
},
[_artifact("thread_1", 0, "/mnt/user-data/outputs/report.md")],
)
seen = {}
def handler(req):
seen["args"] = req.tool_call["args"]
return ToolMessage(content="ok", tool_call_id="call_2")
middleware.wrap_tool_call(request, handler)
assert seen["args"]["files"][0]["path"] == "/mnt/user-data/outputs/report.md"
def test_unknown_handle_returns_error(self):
middleware = ArtifactResolutionMiddleware()
request = _request(
{"name": "read_file", "args": {"path": "art_00000000"}, "id": "call_2", "type": "tool_call"},
[_artifact("thread_1", 0, "/mnt/user-data/outputs/report.md")],
)
def handler(req):
raise AssertionError("Unknown handle must not reach the tool")
result = middleware.wrap_tool_call(request, handler)
assert result.status == "error"
assert "art_00000000" in result.content
assert request.tool_call["args"] == {"path": "art_00000000"}
def test_empty_registry_rejects_handles_but_preserves_concrete_args(self):
middleware = ArtifactResolutionMiddleware()
request = _request({"name": "read_file", "args": {"path": "art_00000000"}, "id": "call_2", "type": "tool_call"}, [])
called = []
def handler(req):
called.append(req)
return ToolMessage(content="ok", tool_call_id="call_2")
result = middleware.wrap_tool_call(request, handler)
assert not called and result.status == "error"
concrete = _request({"name": "read_file", "args": {"path": "/tmp/a.txt"}, "id": "call_2"}, [])
assert middleware.wrap_tool_call(concrete, handler).content == "ok"
assert called == [concrete]
def test_disabled_config_skips_resolution(self):
middleware = ArtifactResolutionMiddleware(config=ToolArtifactConfig(resolve_handles_in_args=False))
request = _request(
{"name": "read_file", "args": {"path": "art_00000000"}, "id": "call_2", "type": "tool_call"},
[_artifact("thread_1", 0, "/mnt/user-data/outputs/report.md")],
)
called = []
def handler(req):
called.append(req)
return ToolMessage(content="ok", tool_call_id="call_2")
middleware.wrap_tool_call(request, handler)
assert called[0] is request
def test_feature_disabled_flag_blocks_resolution_even_when_args_flag_on(self):
middleware = ArtifactResolutionMiddleware(config=ToolArtifactConfig(enabled=False, resolve_handles_in_args=True))
handle = generate_handle("thread_1", "call_0", 0)
request = _request(
{"name": "read_file", "args": {"path": handle}, "id": "call_2", "type": "tool_call"},
[_artifact("thread_1", 0, "/mnt/user-data/outputs/report.md")],
)
seen = {}
def handler(req):
seen["args"] = req.tool_call["args"]
return ToolMessage(content="ok", tool_call_id="call_2")
middleware.wrap_tool_call(request, handler)
assert seen["args"] == {"path": handle}
class TestRealToolCallRequest:
"""Exercise the real ``ToolCallRequest.override()`` dataclass path."""
def _real_request(self, args: dict) -> ToolCallRequest:
return ToolCallRequest(
tool_call={"name": "read_file", "args": args, "id": "call_real", "type": "tool_call"},
tool=None,
state={"tool_artifacts": [_artifact("thread_1", 0, "/mnt/user-data/outputs/report.md")]},
runtime=None,
)
def test_override_produces_resolved_request(self):
middleware = ArtifactResolutionMiddleware()
handle = generate_handle("thread_1", "call_0", 0)
request = self._real_request({"path": handle})
resolved = middleware._resolve_request(request)
assert resolved is not request
assert resolved.tool_call["args"] == {"path": "/mnt/user-data/outputs/report.md"}
assert request.tool_call["args"] == {"path": handle}, "original request must stay untouched"
def test_end_to_end_through_wrap_tool_call(self):
middleware = ArtifactResolutionMiddleware()
handle = generate_handle("thread_1", "call_0", 0)
request = self._real_request({"path": f"`{handle}`"})
seen = {}
def handler(req):
seen["args"] = req.tool_call["args"]
return ToolMessage(content="ok", tool_call_id="call_real")
result = middleware.wrap_tool_call(request, handler)
assert seen["args"] == {"path": "/mnt/user-data/outputs/report.md"}
assert result.content == "ok"

View File

@ -1,120 +0,0 @@
"""Pin durable worker plugin tools and execution to one app generation."""
from copy import deepcopy
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from deerflow_extension_api.plugins import ModelTool, PluginContribution
from test_extension_task_store_runtime import _CapturingSubagent
from test_extension_task_store_runtime import _subagent_env as _subagent_env
from deerflow.config.subagent_batches_config import SubagentBatchesConfig
from deerflow.config.subagent_runtime_config import SubagentRuntimeConfig
from deerflow.extensions import get_loaded_extensions, set_loaded_extensions
from deerflow.extensions.plugin_tools import plugin_tool_name
from deerflow.extensions.registry import ExtensionRegistry
from deerflow.subagents import batch_service as service_module
from deerflow.tools import tools as assembly
def _generation(name):
async def identify(payload, context):
return name
registry = ExtensionRegistry()
with registry.attributed_to("probe"):
registry.plugin(PluginContribution(namespace=f"probe.{name}", title=name, enabled=True, tools=(ModelTool("identify", "Identify generation", {"type": "object", "properties": {}, "additionalProperties": False}, identify),)))
return registry.build()
@pytest.mark.asyncio
@pytest.mark.parametrize("replacement,explicit_snapshot,late_replace,empty", [(False, False, False, False), (True, False, False, False), (True, True, False, False), (False, False, True, False), (True, True, True, True)])
async def test_batch_keeps_its_construction_snapshot(monkeypatch, _subagent_env, replacement, explicit_snapshot, late_replace, empty):
original = get_loaded_extensions()
a, b = _generation("alpha"), _generation("beta")
if empty:
a = ExtensionRegistry().build()
monkeypatch.setattr("deerflow.extensions._loaded", a)
if explicit_snapshot:
set_loaded_extensions(b)
config = SimpleNamespace(tools=[], models=[], acp_agents={}, get_model_config=lambda name: None, authorization=SimpleNamespace(enabled=False), tool_search=SimpleNamespace(enabled=False))
seen = {}
captured = {}
repo = SimpleNamespace(renew_item_lease=AsyncMock(return_value={"valid": True}), finalize_item=AsyncMock(return_value=True))
service = service_module.SubagentBatchService(repository=repo, config=SubagentBatchesConfig(), runtime_config=SubagentRuntimeConfig(), app_config=config, **({"extensions": a} if explicit_snapshot else {}))
item = {
"id": "item-1",
"item_key": "key-1",
"prompt": "identify",
"batch": {
"id": "batch-1",
"thread_id": "thread-1",
"user_id": "user-1",
"run_id": None,
"execution_spec": {"subagent_config": {"name": "researcher", "description": "d", "system_prompt": "p"}, "parent_model": "model-a", "mcp_plugins": [], "tool_groups": ["extensions"]},
},
}
real_assembly = assembly.get_available_tools
monkeypatch.setattr(assembly, "BUILTIN_TOOLS", [])
monkeypatch.setattr(assembly, "is_host_bash_allowed", lambda config: False)
monkeypatch.setattr(assembly, "is_mcp_task_runtime_available", lambda: False)
monkeypatch.setattr("deerflow.mcp.cache.get_cached_mcp_tools", lambda: [])
monkeypatch.setattr(service_module, "resolve_subagent_model_name", lambda *args, **kwargs: "model-a")
def assemble(**kwargs):
kwargs["include_mcp"] = False
tools = real_assembly(**kwargs)
# This test isolates extension snapshots; the run-status built-in is
# assembled independently of the monkeypatched BUILTIN_TOOLS list.
captured["tool_names"] = [t.name for t in tools if t.name != "inspect_run_status"]
return tools
monkeypatch.setattr("deerflow.tools.get_available_tools", assemble)
class CaptureExecutor:
def __init__(self, **kwargs):
captured["executor_kwargs"] = kwargs
def execute_async(self, prompt, task_id=None):
return "exec-1"
monkeypatch.setattr(service_module, "SubagentExecutor", CaptureExecutor)
monkeypatch.setattr(service_module, "SubagentStatus", _subagent_env.SubagentStatus)
monkeypatch.setattr(service_module, "get_background_task_result", lambda _: SimpleNamespace(status=service_module.SubagentStatus.COMPLETED, result="done", error=None, stop_reason=None, token_usage_records=None))
monkeypatch.setattr(service_module, "cleanup_background_task", lambda _: None)
async def initial(self, task):
return ({}, self.tools, None)
async def create(self, tools, *, deferred_setup=None, extensions=None):
seen["extensions"] = extensions
return _CapturingSubagent(seen)
monkeypatch.setattr(_subagent_env.SubagentExecutor, "_build_initial_state", initial)
monkeypatch.setattr(_subagent_env.SubagentExecutor, "_create_agent", create)
try:
set_loaded_extensions(b if replacement else a)
await service._execute_item(item, lease_owner="original-claim")
assert repo.finalize_item.await_args.kwargs["succeeded"]
if late_replace:
set_loaded_extensions(b)
executor = _subagent_env.SubagentExecutor(**captured["executor_kwargs"])
result = await executor._aexecute("identify")
assert result.status == _subagent_env.SubagentStatus.COMPLETED, result.error
assert captured["tool_names"] == ([] if empty else [plugin_tool_name("probe.alpha", "identify")])
assert seen["extensions"] is a
# Replay the same durable record under a new worker after a restart.
# The record carries no Python snapshot; the new worker owns generation B.
original_spec = deepcopy(item["batch"]["execution_spec"])
restarted = service_module.SubagentBatchService(repository=repo, config=SubagentBatchesConfig(), runtime_config=SubagentRuntimeConfig(), app_config=config, extensions=b)
set_loaded_extensions(a)
await restarted._execute_item(item, lease_owner="restarted-claim")
executor = _subagent_env.SubagentExecutor(**captured["executor_kwargs"])
result = await executor._aexecute("identify")
assert result.status == _subagent_env.SubagentStatus.COMPLETED, result.error
assert seen["extensions"] is b
assert captured["tool_names"] == [plugin_tool_name("probe.beta", "identify")]
assert item["batch"]["execution_spec"] == original_spec
finally:
set_loaded_extensions(original)

View File

@ -3,7 +3,6 @@
from __future__ import annotations
import inspect
import logging
import os
import stat
@ -168,21 +167,47 @@ def test_publish_permission_failure_keeps_previous_file(tmp_path, monkeypatch, p
assert list(tmp_path.iterdir()) == [target]
def test_screenshot_writes_go_through_the_atomic_publish():
source = inspect.getsource(browser_automation_tools)
def test_screenshot_writes_preserve_existing_output_on_replace_failure(tmp_path, monkeypatch):
outputs_path = tmp_path / "outputs"
outputs_path.mkdir()
target = outputs_path / "shot.png"
target.write_bytes(b"old screenshot")
# A truncating write left a partial capture behind while the function returned
# the name as if it had been saved.
assert "(outputs_path / name).write_bytes(" not in source
assert "_publish_bytes_atomically(outputs_path / name, content)" in source
def fail_replace(*args):
raise OSError("replace interrupted")
with monkeypatch.context() as patches:
patches.setattr(os, "replace", fail_replace)
with pytest.raises(OSError, match="replace interrupted"):
browser_automation_tools._write_screenshot(outputs_path, "shot.png", b"new screenshot")
assert target.read_bytes() == b"old screenshot"
assert list(outputs_path.iterdir()) == [target]
saved_name = browser_automation_tools._write_screenshot(outputs_path, "shot.png", b"new screenshot")
assert saved_name == "shot.png"
assert target.read_bytes() == b"new screenshot"
assert list(outputs_path.iterdir()) == [target]
def test_capture_writes_go_through_the_atomic_publish():
source = inspect.getsource(browserless_tools)
def test_capture_writes_release_reserved_name_on_replace_failure_and_publish_atomically(tmp_path, monkeypatch):
outputs_path = tmp_path / "outputs"
outputs_path.mkdir()
assert "(outputs_path / final_name).write_bytes(" not in source
assert "destination.unlink(missing_ok=True)" in source
assert "_publish_bytes_atomically(destination, content)" in source
def fail_replace(*args):
raise OSError("replace interrupted")
with monkeypatch.context() as patches:
patches.setattr(os, "replace", fail_replace)
with pytest.raises(OSError, match="replace interrupted"):
browserless_tools._write_capture_output(outputs_path, "capture.png", b"new capture")
assert list(outputs_path.iterdir()) == []
saved_name = browserless_tools._write_capture_output(outputs_path, "capture.png", b"new capture")
assert saved_name == "capture.png"
assert (outputs_path / saved_name).read_bytes() == b"new capture"
assert list(outputs_path.iterdir()) == [outputs_path / saved_name]
def test_both_tools_share_the_atomic_publisher():

View File

@ -1,131 +0,0 @@
"""Hidden user answers form turn boundaries; internal control messages do not."""
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from app.channels.manager import ChannelManager, _extract_artifacts, _extract_response_text
from app.channels.message_bus import HUMAN_INPUT_RESPONSE_METADATA_KEY, InboundMessage, MessageBus
from app.channels.store import JsonChannelStore
def _hidden_answer() -> dict:
return {
"type": "human",
"content": "Alpha",
"additional_kwargs": {
"hide_from_ui": True,
"human_input_response": {
"version": 1,
"kind": "human_input_response",
"source": "ask_clarification",
"request_id": "clarification:call-1",
"response_kind": "option",
"option_id": "alpha",
"value": "Alpha",
},
},
}
def test_hidden_answer_stops_previous_clarification_text():
result = {
"messages": [
{"type": "human", "content": "Original question"},
{"type": "tool", "name": "ask_clarification", "content": "Pick one"},
_hidden_answer(),
]
}
assert _extract_response_text(result) == ""
def _file_response(content: str, path: str) -> dict:
return {"type": "ai", "content": content, "tool_calls": [{"name": "present_files", "args": {"filepaths": [path]}}]}
@pytest.mark.parametrize("response_kind", ["option", "text"])
def test_hidden_answer_stops_previous_response_and_artifacts(response_kind):
answer = _hidden_answer()
response = answer["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY]
response["response_kind"] = response_kind
if response_kind == "text":
response.pop("option_id")
result = {"messages": [{"type": "human", "content": "Original question"}, _file_response("Previous answer", "/old.txt"), answer], "artifacts": ["/old.txt"]}
assert _extract_response_text(result) == ""
assert _extract_artifacts(result) == []
def test_hidden_answer_keeps_current_response_and_artifacts():
result = {
"messages": [
{"type": "human", "content": "Original question"},
_file_response("Previous answer", "/old.txt"),
_hidden_answer(),
_file_response("Current answer", "/new.txt"),
{"type": "human", "content": "Internal context", "additional_kwargs": {"hide_from_ui": True}},
]
}
assert _extract_response_text(result) == "Current answer"
assert _extract_artifacts(result) == ["/new.txt"]
@pytest.mark.parametrize("invalid_response", [None, {}, "invalid", {"version": 2}, {"option_id": ""}, {"value": " "}])
def test_invalid_hidden_answer_remains_a_control_message(invalid_response):
hidden = _hidden_answer()
if invalid_response is None:
hidden["additional_kwargs"].pop(HUMAN_INPUT_RESPONSE_METADATA_KEY)
elif isinstance(invalid_response, dict):
hidden["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY].update(invalid_response)
if not invalid_response:
hidden["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY] = {}
else:
hidden["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY] = invalid_response
result = {"messages": [{"type": "human", "content": "Current question"}, _file_response("Current answer", "/current.txt"), hidden]}
assert _extract_response_text(result) == "Current answer"
assert _extract_artifacts(result) == ["/current.txt"]
@pytest.mark.asyncio
@pytest.mark.parametrize("failed", [False, True])
async def test_card_answer_without_fresh_ai_preserves_terminal_result(tmp_path, monkeypatch, failed):
monkeypatch.delenv("DEER_FLOW_AUTH_DISABLED", raising=False)
bus = MessageBus()
store = JsonChannelStore(path=tmp_path / "routing.json")
await store.set_thread_id("feishu", "chat-1", "thread-1", topic_id="topic-1")
manager = ChannelManager(bus=bus, store=store)
inbound = InboundMessage(
channel_name="feishu", chat_id="chat-1", user_id="ou-originator", topic_id="topic-1", text="Alpha", metadata={HUMAN_INPUT_RESPONSE_METADATA_KEY: _hidden_answer()["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY]}
)
client = MagicMock()
client.threads.get = AsyncMock(return_value={"thread_id": "thread-1"})
client.threads.update = AsyncMock(return_value={"thread_id": "thread-1"})
async def stream(*args, **kwargs):
answer = dict(kwargs["input"]["messages"][0], type="human")
yield SimpleNamespace(
event="values",
data={"messages": [{"type": "human", "content": "Original question"}, _file_response("Previous answer", "/old.txt"), {"type": "tool", "name": "ask_clarification", "content": "Pick one"}, answer], "artifacts": ["/old.txt"]},
)
if failed:
yield SimpleNamespace(event="error", data={"name": "ModelError", "message": "offline failure"})
client.runs.stream = MagicMock(side_effect=stream)
manager._client = client
outbound = []
async def capture(message):
outbound.append(message)
bus.subscribe_outbound(capture)
await manager._handle_chat(inbound)
assert len(outbound) == 1
assert outbound[0].is_final is True
assert outbound[0].text == ("An error occurred while processing your request. Please try again." if failed else "(No response from agent)")
assert outbound[0].artifacts == []
assert outbound[0].attachments == []

View File

@ -14645,3 +14645,172 @@ def test_importing_channel_manager_loads_no_sandbox_provider_stack() -> None:
check=True,
)
assert result.stdout.strip() == "", "ChannelManager import pulled in the sandbox provider stack"
# --- Hidden human-input turn boundaries (consolidated from test_channel_hidden_human_input.py) ---
def _hidden_human_input_answer() -> dict:
return {
"type": "human",
"content": "Alpha",
"additional_kwargs": {
"hide_from_ui": True,
"human_input_response": {
"version": 1,
"kind": "human_input_response",
"source": "ask_clarification",
"request_id": "clarification:call-1",
"response_kind": "option",
"option_id": "alpha",
"value": "Alpha",
},
},
}
def _presented_file_response(content: str, path: str) -> dict:
return {
"type": "ai",
"content": content,
"tool_calls": [{"name": "present_files", "args": {"filepaths": [path]}}],
}
class TestHiddenHumanInputTurnBoundaries:
"""Valid hidden human-input answers start a new turn; internal hidden control messages do not."""
def test_hidden_answer_stops_previous_clarification_text(self) -> None:
from app.channels.manager import _extract_response_text
result = {
"messages": [
{"type": "human", "content": "Original question"},
{"type": "tool", "name": "ask_clarification", "content": "Pick one"},
_hidden_human_input_answer(),
]
}
assert _extract_response_text(result) == ""
@pytest.mark.parametrize("response_kind", ["option", "text"])
def test_hidden_answer_stops_previous_response_and_artifacts(self, response_kind: str) -> None:
from app.channels.manager import _extract_artifacts, _extract_response_text
from app.channels.message_bus import HUMAN_INPUT_RESPONSE_METADATA_KEY
answer = _hidden_human_input_answer()
response = answer["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY]
response["response_kind"] = response_kind
if response_kind == "text":
response.pop("option_id")
result = {
"messages": [
{"type": "human", "content": "Original question"},
_presented_file_response("Previous answer", "/old.txt"),
answer,
],
"artifacts": ["/old.txt"],
}
assert _extract_response_text(result) == ""
assert _extract_artifacts(result) == []
def test_hidden_answer_keeps_current_response_and_artifacts(self) -> None:
from app.channels.manager import _extract_artifacts, _extract_response_text
result = {
"messages": [
{"type": "human", "content": "Original question"},
_presented_file_response("Previous answer", "/old.txt"),
_hidden_human_input_answer(),
_presented_file_response("Current answer", "/new.txt"),
{"type": "human", "content": "Internal context", "additional_kwargs": {"hide_from_ui": True}},
]
}
assert _extract_response_text(result) == "Current answer"
assert _extract_artifacts(result) == ["/new.txt"]
@pytest.mark.parametrize(
"invalid_response",
[None, {}, "invalid", {"version": 2}, {"option_id": ""}, {"value": " "}],
)
def test_invalid_hidden_answer_remains_a_control_message(self, invalid_response: object) -> None:
from app.channels.manager import _extract_artifacts, _extract_response_text
from app.channels.message_bus import HUMAN_INPUT_RESPONSE_METADATA_KEY
hidden = _hidden_human_input_answer()
if invalid_response is None:
hidden["additional_kwargs"].pop(HUMAN_INPUT_RESPONSE_METADATA_KEY)
elif isinstance(invalid_response, dict):
hidden["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY].update(invalid_response)
if not invalid_response:
hidden["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY] = {}
else:
hidden["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY] = invalid_response
result = {
"messages": [
{"type": "human", "content": "Current question"},
_presented_file_response("Current answer", "/current.txt"),
hidden,
]
}
assert _extract_response_text(result) == "Current answer"
assert _extract_artifacts(result) == ["/current.txt"]
@pytest.mark.asyncio
@pytest.mark.parametrize("failed", [False, True])
async def test_card_answer_without_fresh_ai_preserves_terminal_result(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, failed: bool) -> None:
from app.channels.manager import ChannelManager
from app.channels.message_bus import HUMAN_INPUT_RESPONSE_METADATA_KEY
monkeypatch.delenv("DEER_FLOW_AUTH_DISABLED", raising=False)
bus = MessageBus()
store = JsonChannelStore(path=tmp_path / "routing.json")
await store.set_thread_id("feishu", "chat-1", "thread-1", topic_id="topic-1")
manager = ChannelManager(bus=bus, store=store)
inbound = InboundMessage(
channel_name="feishu",
chat_id="chat-1",
user_id="ou-originator",
topic_id="topic-1",
text="Alpha",
metadata={HUMAN_INPUT_RESPONSE_METADATA_KEY: _hidden_human_input_answer()["additional_kwargs"][HUMAN_INPUT_RESPONSE_METADATA_KEY]},
)
client = MagicMock()
client.threads.get = AsyncMock(return_value={"thread_id": "thread-1"})
client.threads.update = AsyncMock(return_value={"thread_id": "thread-1"})
async def stream(*_args: object, **kwargs: object):
answer = dict(kwargs["input"]["messages"][0], type="human") # type: ignore[index]
yield SimpleNamespace(
event="values",
data={
"messages": [
{"type": "human", "content": "Original question"},
_presented_file_response("Previous answer", "/old.txt"),
{"type": "tool", "name": "ask_clarification", "content": "Pick one"},
answer,
],
"artifacts": ["/old.txt"],
},
)
if failed:
yield SimpleNamespace(event="error", data={"name": "ModelError", "message": "offline failure"})
client.runs.stream = MagicMock(side_effect=stream)
manager._client = client
outbound: list[OutboundMessage] = []
async def capture(message: OutboundMessage) -> None:
outbound.append(message)
bus.subscribe_outbound(capture)
await manager._handle_chat(inbound)
assert len(outbound) == 1
assert outbound[0].is_final is True
assert outbound[0].text == ("An error occurred while processing your request. Please try again." if failed else "(No response from agent)")
assert outbound[0].artifacts == []
assert outbound[0].attachments == []

View File

@ -1,53 +0,0 @@
"""Tests for DeerFlowClient message serialization helpers."""
from langchain_core.messages import AIMessage, HumanMessage
from deerflow.client import DeerFlowClient
def test_serialize_ai_message_preserves_additional_kwargs():
message = AIMessage(
content="done",
additional_kwargs={
"token_usage_attribution": {
"version": 1,
"kind": "final_answer",
"shared_attribution": False,
"actions": [],
}
},
usage_metadata={"input_tokens": 12, "output_tokens": 3, "total_tokens": 15},
)
serialized = DeerFlowClient._serialize_message(message)
assert serialized["type"] == "ai"
assert serialized["usage_metadata"] == {
"input_tokens": 12,
"output_tokens": 3,
"total_tokens": 15,
}
assert serialized["additional_kwargs"] == {
"token_usage_attribution": {
"version": 1,
"kind": "final_answer",
"shared_attribution": False,
"actions": [],
}
}
def test_serialize_human_message_preserves_additional_kwargs():
message = HumanMessage(
content="hello",
additional_kwargs={"files": [{"name": "diagram.png"}]},
)
serialized = DeerFlowClient._serialize_message(message)
assert serialized == {
"type": "human",
"content": "hello",
"id": None,
"additional_kwargs": {"files": [{"name": "diagram.png"}]},
}

View File

@ -14,7 +14,13 @@ from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from deerflow_extension_api import EXTENSION_TASK_STORE_KEY, CompactionEvent, ExtensionData, TaskInfo
from deerflow_extension_api import (
EXTENSION_TASK_STORE_KEY,
CompactionEvent,
ExtensionData,
TaskInfo,
canonical_hash,
)
from langchain_core.messages import AIMessage, HumanMessage
from langgraph.checkpoint.memory import InMemorySaver
@ -284,3 +290,28 @@ class TestManualCompaction:
assert store.scope_id == "detached"
assert store.get(TaskInfo) is None
assert _identity(event) == (None, None, None)
class TestCompactionSourceHashing:
"""Consolidated from test_context_compaction_observation.py."""
def test_source_hashes_are_computed_on_content_directly_not_a_stringified_copy(self):
"""Hashing ``str(message.content)`` would defeat ``canonical_hash`` key-order normalization
for multimodal (``list[dict]``) content."""
a = HumanMessage(content=[{"type": "text", "text": "hi"}, {"b": 1, "a": 2}])
b = HumanMessage(content=[{"type": "text", "text": "hi"}, {"a": 2, "b": 1}])
middleware = _middleware(_extensions(_CompactionRecorder()))
hashes = middleware._freeze_compaction_sources([a, b])
assert hashes[0] == hashes[1]
assert hashes[0] == canonical_hash(a.content)
assert str(a.content) != str(b.content)
def test_the_sources_are_not_hashed_when_nothing_observes(self):
from dataclasses import replace
from deerflow.extensions import get_agent_build_extensions
unobserved = replace(get_agent_build_extensions(), context_compaction_observers=())
assert _middleware(unobserved)._freeze_compaction_sources(_messages()) == ()

View File

@ -1,215 +0,0 @@
"""Compaction destroys the mapping it is observed by.
Summarization replaces N messages with one summary. After the fact, only the
summary survives, so 'which messages became this summary' is not reconstructible
from state — it has to be emitted at the moment of the transform.
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from deerflow_extension_api import CompactionEvent, canonical_hash
def test_event_records_both_ends_of_the_transform():
event = CompactionEvent(
transform_kind="summarization",
transform_version="1",
source_content_hashes=("h1", "h2"),
output_content_hash="h3",
compacted_message_count=2,
kept_message_count=4,
)
assert event.source_content_hashes == ("h1", "h2")
assert event.output_content_hash == "h3"
def test_source_hashes_are_a_tuple_so_the_event_cannot_be_mutated_after_emission():
event = CompactionEvent(
transform_kind="summarization",
transform_version="1",
source_content_hashes=("h1",),
output_content_hash="h3",
compacted_message_count=1,
kept_message_count=1,
)
with pytest.raises(AttributeError):
event.output_content_hash = "other"
_UNOBSERVED = object()
def _observed_extensions(observer=None):
"""A real ``LoadedExtensions`` carrying one compaction observer.
``replace`` on the ambient set rather than a hand-built stub: the
middleware reads other fields off ``_extensions`` too (the system-model
call path), so a namespace carrying only the observer tuple would pass
these tests while diverging from what the middleware is handed in
production.
"""
from dataclasses import replace
from deerflow.extensions import get_agent_build_extensions
return replace(get_agent_build_extensions(), context_compaction_observers=(("test-source", observer or (lambda event, context=None: None)),))
def test_source_hashes_are_computed_on_content_directly_not_a_stringified_copy():
"""Regression: hashing ``str(message.content)`` would defeat canonical_hash's
key-order normalization for multimodal (``list[dict]``) content, which
``view_image_middleware`` and other producers routinely inject. Two
logically identical messages whose dict content differs only in key
insertion order must hash the same.
"""
from langchain_core.messages import HumanMessage
from deerflow.agents.middlewares.summarization_middleware import DeerFlowSummarizationMiddleware
a = HumanMessage(content=[{"type": "text", "text": "hi"}, {"b": 1, "a": 2}])
b = HumanMessage(content=[{"type": "text", "text": "hi"}, {"a": 2, "b": 1}])
middleware = DeerFlowSummarizationMiddleware(model=MagicMock(), extensions=_observed_extensions())
hashes = middleware._freeze_compaction_sources([a, b])
assert hashes[0] == hashes[1]
assert hashes[0] == canonical_hash(a.content)
# str() on a dict renders insertion order, so the pre-stringified form
# this guards against would not have matched.
assert str(a.content) != str(b.content)
# --- Driving a real compaction --------------------------------------------
#
# Mirrors tests/test_summarization_middleware.py's `_messages` / `_middleware` /
# `_runtime` fixture helpers rather than inventing a second way to drive the
# middleware: a static model, `token_counter=len`, and a runtime carrying a
# plain `context` mapping.
def _messages() -> list:
from langchain_core.messages import AIMessage, HumanMessage
return [
HumanMessage(content="user-1"),
AIMessage(content="assistant-1"),
HumanMessage(content="user-2"),
AIMessage(content="assistant-2"),
]
def _runtime(thread_id: str | None = "thread-1") -> SimpleNamespace:
context = {}
if thread_id is not None:
context["thread_id"] = thread_id
return SimpleNamespace(context=context)
def _middleware(*, trigger=("messages", 4), keep=("messages", 2), extensions=_UNOBSERVED):
from deerflow.agents.middlewares.summarization_middleware import DeerFlowSummarizationMiddleware
model = MagicMock()
model.invoke.return_value = SimpleNamespace(text="compressed summary")
model.ainvoke = AsyncMock(return_value=SimpleNamespace(text="compressed summary"))
model.with_config.return_value = model
return DeerFlowSummarizationMiddleware(
model=model,
trigger=trigger,
keep=keep,
token_counter=len,
extensions=_observed_extensions() if extensions is _UNOBSERVED else extensions,
)
class TestSummarizationEmitsTheEvent:
@pytest.mark.asyncio
async def test_a_compaction_notifies_observers_once(self, monkeypatch):
from deerflow.agents.middlewares import summarization_middleware
events = []
monkeypatch.setattr(
summarization_middleware,
"notify_context_compacted",
lambda event, extensions=None, task_store=None: events.append(event),
)
middleware = _middleware()
result = await middleware.abefore_model({"messages": _messages()}, _runtime())
assert result is not None
assert len(events) == 1
event = events[0]
assert event.transform_kind == "summarization"
assert event.compacted_message_count == 2
assert event.kept_message_count == 2
assert event.source_content_hashes == (
canonical_hash("user-1"),
canonical_hash("assistant-1"),
)
assert event.output_content_hash == canonical_hash("compressed summary")
@pytest.mark.asyncio
async def test_no_event_is_emitted_when_the_trigger_does_not_fire(self, monkeypatch):
from deerflow.agents.middlewares import summarization_middleware
events = []
monkeypatch.setattr(
summarization_middleware,
"notify_context_compacted",
lambda event, extensions=None, task_store=None: events.append(event),
)
# A trigger threshold far above the message count never fires, so
# compaction never runs and the record half is never reached.
middleware = _middleware(trigger=("messages", 100))
result = await middleware.abefore_model({"messages": _messages()}, _runtime())
assert result is None
assert events == []
class TestAnInstallWithNoObserverPaysNothing:
"""Hashing the sources is an O(context-size) canonical-JSON pass.
Every install runs this middleware; almost none of them register a
compaction observer. The check cannot live in ``notify_context_compacted``
— by the time it is called the hashing has already happened — so the freeze
site has to make it itself.
"""
def test_the_sources_are_not_hashed_when_nothing_observes(self):
from dataclasses import replace
from deerflow.extensions import get_agent_build_extensions
unobserved = replace(get_agent_build_extensions(), context_compaction_observers=())
middleware = _middleware(extensions=unobserved)
assert middleware._freeze_compaction_sources(_messages()) == ()
def test_the_sources_are_hashed_when_an_observer_is_registered(self):
middleware = _middleware(extensions=_observed_extensions())
assert middleware._freeze_compaction_sources(_messages()) == tuple(canonical_hash(m.content) for m in _messages())
@pytest.mark.asyncio
async def test_the_compaction_itself_still_happens_unobserved(self, monkeypatch):
"""The skip must cost the run nothing but the hashes."""
from dataclasses import replace
from deerflow.agents.middlewares import summarization_middleware
from deerflow.extensions import get_agent_build_extensions
events = []
monkeypatch.setattr(summarization_middleware, "notify_context_compacted", lambda event, extensions=None, task_store=None: events.append(event))
unobserved = replace(get_agent_build_extensions(), context_compaction_observers=())
result = await _middleware(extensions=unobserved).abefore_model({"messages": _messages()}, _runtime())
assert result is not None, "compaction must still run; only the observation bookkeeping is skipped"
# The middleware still calls notify (which would itself no-op on the
# empty observer tuple); what it must not do is compute the hashes.
assert [e.source_content_hashes for e in events] == [()]

View File

@ -10,8 +10,11 @@ from __future__ import annotations
import asyncio
import dataclasses
import dis
import importlib.resources
import inspect
from collections.abc import Sequence
from typing import Any, Protocol
import pytest
from deerflow_extension_api import (
@ -123,6 +126,82 @@ def test_agent_build_context_optional_fields_keep_their_defaults():
assert isinstance(ctx.policy, HostPolicySnapshot)
def _dummy_args_for_protocol_method(func):
sig = inspect.signature(func)
kwargs = {}
positional = []
sample_by_name = {
"app_store": ExtensionData("app"),
"task_store": ExtensionData("task-1"),
"ctx": AgentBuildContext(scope=AgentScope.LEAD),
"info": TaskInfo(task_id="t", run_id="r", thread_id="th", kind="lead"),
"outcome": TaskOutcome.COMPLETED,
"kind": SystemOperationKind.GOAL,
"request": SystemModelRequest(messages=("prompt",)),
"result": SystemModelResult(response="ok", duration_ms=1.0),
"deps": ExtensionRuntimeDeps(),
"cursor": None,
"limit": 10,
"thread_id": "th",
"run_id": "r",
"after_seq": None,
"name": "demo",
"routers": (),
}
for idx, (pname, param) in enumerate(sig.parameters.items()):
if idx == 0:
continue # self
val = sample_by_name.get(pname, object())
if param.kind == inspect.Parameter.KEYWORD_ONLY:
kwargs[pname] = val
elif param.kind in (
inspect.Parameter.POSITIONAL_ONLY,
inspect.Parameter.POSITIONAL_OR_KEYWORD,
):
positional.append(val)
return positional, kwargs
def _assert_protocol_has_defaults(protocol: type, *, expects_not_implemented: bool = False) -> None:
"""Invoke every public Protocol method on a bare instance and verify its documented default."""
class _Bare:
pass
bare = _Bare()
checked = 0
for name, member in vars(protocol).items():
if name.startswith("_") or not inspect.isfunction(member):
continue
checked += 1
sig = inspect.signature(member)
ret_ann = sig.return_annotation
pos_args, kw_args = _dummy_args_for_protocol_method(member)
if expects_not_implemented:
with pytest.raises(NotImplementedError):
if inspect.iscoroutinefunction(member):
asyncio.run(member(bare, *pos_args, **kw_args))
else:
member(bare, *pos_args, **kw_args)
continue
result = asyncio.run(member(bare, *pos_args, **kw_args)) if inspect.iscoroutinefunction(member) else member(bare, *pos_args, **kw_args)
if ret_ann in ("bool", bool):
assert result is False, f"{protocol.__name__}.{name} must default to False (got {result!r}); an unimplemented `...` stub implicitly returns None"
elif (isinstance(ret_ann, str) and ret_ann.startswith("Sequence[")) or ret_ann is Sequence:
assert isinstance(result, Sequence) and not isinstance(result, str | bytes) and tuple(result) == (), f"{protocol.__name__}.{name} must default to an empty sequence (got {result!r})"
elif ret_ann in ("None", None):
assert result is None, f"{protocol.__name__}.{name} must return None (got {result!r})"
ret_spans = [(ins.positions.end_col_offset or 0) - (ins.positions.col_offset or 0) for ins in dis.get_instructions(member) if ins.opname in ("RETURN_VALUE", "RETURN_CONST") and ins.positions is not None]
assert ret_spans and all(span not in (0, 3) for span in ret_spans), f"{protocol.__name__}.{name} uses an unimplemented `...` stub instead of an explicit `return None` default"
else:
raise AssertionError(f"Unhandled return annotation {ret_ann!r} on {protocol.__name__}.{name}; assert its contract-specific default explicitly")
assert checked > 0, f"{protocol.__name__} declared no methods to check"
@pytest.mark.parametrize(
"protocol",
[
@ -135,22 +214,41 @@ def test_agent_build_context_optional_fields_keep_their_defaults():
],
)
def test_every_protocol_method_has_a_default_implementation(protocol):
"""Adding a method to a Protocol is only additive when it has a default.
"""Adding a method to a Protocol is only additive when it carries a concrete default."""
_assert_protocol_has_defaults(protocol, expects_not_implemented=(protocol is RunEvidenceReader))
Without this, shipping a new contract method breaks every already-released
extension that does not implement it.
"""
checked = 0
for name, member in vars(protocol).items():
if name.startswith("_") or not inspect.isfunction(member):
continue
checked += 1
body = inspect.getsource(member).split("\n", 1)[1]
if protocol is RunEvidenceReader:
assert "raise NotImplementedError" in body
else:
assert "return" in body, f"{protocol.__name__}.{name} has no default implementation. Adding a contract method is only additive when it returns a default; otherwise every already-released extension breaks on upgrade."
assert checked > 0, f"{protocol.__name__} declared no methods to check"
def test_protocol_default_guard_rejects_unimplemented_ellipsis_stubs():
"""Verify the default-implementation guard catches `...` stubs across bool, sequence, void, and reader contracts."""
class _StubBoolProtocol(Protocol):
def knowledge_provider(self, name: str, provider: Any) -> bool: ...
class _StubSequenceProtocol(Protocol):
def contribute_middlewares(self) -> Sequence[Any]: ...
class _StubInlineVoidProtocol(Protocol):
def on_event(self) -> None: ...
class _StubMultilineVoidProtocol(Protocol):
async def start(self) -> None:
"""Unimplemented async hook stub."""
...
class _StubReaderProtocol(Protocol):
def get_run(self, run_id: str) -> Any: ...
for broken_proto in (
_StubBoolProtocol,
_StubSequenceProtocol,
_StubInlineVoidProtocol,
_StubMultilineVoidProtocol,
):
with pytest.raises(AssertionError):
_assert_protocol_has_defaults(broken_proto)
with pytest.raises(pytest.fail.Exception):
_assert_protocol_has_defaults(_StubReaderProtocol, expects_not_implemented=True)
def test_contributor_defaults_return_empty():

View File

@ -1,48 +0,0 @@
"""Memory retention must not depend on lexical guesses about user intent."""
from copy import deepcopy
import pytest
from langchain_core.messages import AIMessage, HumanMessage
from test_memory_updater import _make_memory, _make_updater, _memory_config
from deerflow.agents.memory.backends.deermem.deer_mem import DeerMem
from deerflow.agents.memory.backends.deermem.deermem.core.updater import _strip_upload_mentions_from_memory
def test_short_contextual_confirmation_is_preserved(tmp_path):
messages = [HumanMessage(content="What are my preferences?"), AIMessage(content="Do you always prefer Chinese replies?"), HumanMessage(content="\u662f\u7684"), AIMessage(content="Understood.")]
memory = DeerMem(backend_config={"storage_path": str(tmp_path)})
prepared = memory._prepare_update(messages)
assert prepared is not None
assert prepared[0] == messages
@pytest.mark.parametrize("content", ["User builds file upload systems for a living.", "User prefers uploading documents through the browser."])
def test_upload_words_cannot_delete_stored_memory(content):
memory = _make_memory([{"id": "a", "content": content}])
memory["user"]["workContext"]["summary"] = content
expected = deepcopy(memory)
assert _strip_upload_mentions_from_memory(memory) == expected
def test_regex_reinforcement_cannot_confirm_existing_fact():
updater = _make_updater(config=_memory_config(fact_eviction_policy="hybrid-v1"))
memory = _make_memory([{"id": "a", "content": "User prefers concise replies", "category": "preference", "confidence": 0.9}])
result = updater._apply_updates(memory, {"factsToReinforce": [{"id": "a", "scope": "user", "reason": "quoted positive phrase"}]}, signals=frozenset({"reinforcement"}))
assert result["facts"][0].get("confirmationCount", 0) == 0
def test_extraction_prompt_does_not_prescribe_confirmation_or_confidence():
updater = _make_updater()
_, prompt = updater._prepare_update_prompt(
[HumanMessage(content="The phrase 'yes, exactly' was quoted from a book.")],
agent_name=None,
signals=frozenset({"reinforcement"}),
)
system = "\n".join(message.content for message in prompt if message.type == "system")
assert '"factsToReinforce": []' in system
assert "confidence >= 0.95" not in system
assert "3-5 concurrent focus themes" not in system

View File

@ -1,27 +0,0 @@
"""Evidence and proportional interaction regressions."""
from deerflow.agents.interaction_policy import RunInteractionPolicy
from deerflow.agents.middlewares.receipt_verification import render_citation_verdict, verify_receipt_citations
def test_negated_action_text_is_not_semantically_classified():
verdict = verify_receipt_citations("I did not modify report.py or run tests.", [])
assert verdict["no_citation_claims"] is False
assert verdict["citation_resolved"] is False
assert "no receipt citations" in render_citation_verdict(verdict)
def test_uncited_tool_execution_is_unresolved_regardless_of_language_or_length():
receipt = {"id": "r1", "status": "success", "tool_name": "bash"}
for text in ("42", "No changes were needed.", "??", "x" * 500):
verdict = verify_receipt_citations(text, [receipt])
assert verdict["citation_resolved"] is False
assert "action claims" not in render_citation_verdict(verdict)
def test_interactive_policy_allows_inspection_and_reversible_assumptions():
policy = RunInteractionPolicy.interactive()
assert "reversible" in policy.clarification_system
assert "independent" in policy.clarification_system
assert "Clarification ALWAYS comes BEFORE action" not in policy.clarification_system
assert "If anything is unclear" not in policy.thinking_guidance

View File

@ -1,54 +0,0 @@
"""Runtime decisions require structured evidence, not prose matches."""
import httpx
import pytest
from langchain_core.messages import HumanMessage
from deerflow.agents.middlewares.llm_error_handling_middleware import LLMErrorHandlingMiddleware, _extract_in_band_status
from deerflow.agents.middlewares.mcp_routing_middleware import McpRoutingMiddleware
from deerflow.config.app_config import AppConfig
from deerflow.config.sandbox_config import SandboxConfig
class ProviderError(Exception):
def __init__(self, text, status=None, code=None, body=None):
super().__init__(text)
self.status_code = status
self.code = code
self.body = body
@pytest.mark.parametrize("text", ["No billing or permission issue; unknown failure", "server busy is only a quoted example", "HTTP 503 is not the actual response", "余额不足只是示例"])
def test_provider_prose_does_not_establish_error_category(text):
middleware = LLMErrorHandlingMiddleware(app_config=AppConfig(sandbox=SandboxConfig(use="test")))
assert middleware._classify_error(ProviderError(text)) == (False, "generic")
@pytest.mark.parametrize(
("error", "expected"),
[
(ProviderError("quota billing permission", status=503), (True, "transient")),
(ProviderError("server busy", status=401), (False, "auth")),
(ProviderError("server busy", status=400), (False, "generic")),
(ProviderError("anything", status=429, code="insufficient_quota"), (False, "quota")),
(ProviderError("anything", status=429, code="limit_burst_rate"), (True, "burst_rate")),
(IndexError("list index out of range"), (False, "generic")),
(httpx.ReadError("peer closed"), (True, "transient")),
(ProviderError("not an HTTP status", status=True), (False, "generic")),
],
)
def test_provider_classification_uses_status_codes_and_real_types(error, expected):
middleware = LLMErrorHandlingMiddleware(app_config=AppConfig(sandbox=SandboxConfig(use="test")))
assert middleware._classify_error(error) == expected
def test_in_band_status_does_not_parse_numbers_from_prose():
assert _extract_in_band_status(ProviderError("Example [504], not an actual status")) is None
assert _extract_in_band_status(ProviderError("anything", body={"error": {"status": 504}})) == 504
@pytest.mark.parametrize("text", ["orders", "do not query orders", "订单只是引用", "What does orders mean?", "unrelated preorders text"])
def test_mcp_routing_keywords_never_promote_schemas(text):
middleware = McpRoutingMiddleware({"postgres_query": {"priority": 100, "keywords": ["orders", "订单"]}}, "hash", 3)
state = {"messages": [HumanMessage(content=text)]}
assert middleware.before_model(state, None) is None

View File

@ -1,31 +0,0 @@
from deerflow.agents.human_input import read_human_input_response
def _text_response(value: str):
return {
"version": 1,
"kind": "human_input_response",
"source": "ask_clarification",
"request_id": "clarification:call-abc",
"response_kind": "text",
"value": value,
}
def test_read_human_input_response_requires_non_empty_value():
assert read_human_input_response({"human_input_response": _text_response("")}) is None
assert read_human_input_response({"human_input_response": _text_response(" ")}) is None
def test_read_human_input_response_preserves_non_empty_value():
response = read_human_input_response({"human_input_response": _text_response(" staging ")})
assert response is not None
assert response["value"] == " staging "
def test_read_human_input_response_rejects_unknown_kind():
raw = _text_response("staging")
raw["response_kind"] = "unknown"
assert read_human_input_response({"human_input_response": raw}) is None

View File

@ -124,12 +124,37 @@ def test_truncating_write_would_have_destroyed_it(tmp_path):
assert target.read_bytes() != b"old runtime"
def test_the_install_path_uses_the_atomic_publish():
import inspect
def test_the_install_path_preserves_previous_binary_on_publish_failure(tmp_path, monkeypatch):
import io
import tarfile
from deerflow.integrations import lark_cli
from deerflow.integrations.lark_cli import _extract_lark_cli_runtime_binary
source = inspect.getsource(lark_cli)
buf = io.BytesIO()
payload = b"#!/bin/sh\necho new-runtime\n"
with tarfile.open(fileobj=buf, mode="w:gz") as tf:
info = tarfile.TarInfo(name="bin/lark-cli")
info.size = len(payload)
info.mode = 0o755
tf.addfile(info, io.BytesIO(payload))
archive_bytes = buf.getvalue()
assert "destination.write_bytes(" not in source
assert "_publish_bytes_atomically(destination" in source
destination = tmp_path / "runtime" / "lark-cli"
destination.parent.mkdir(parents=True)
destination.write_bytes(b"old runtime")
def fail_replace(*args):
raise OSError("replace failed mid-install")
with monkeypatch.context() as patches:
patches.setattr(os, "replace", fail_replace)
with pytest.raises(OSError, match="replace failed mid-install"):
_extract_lark_cli_runtime_binary(archive_bytes, destination)
assert destination.read_bytes() == b"old runtime"
assert list(destination.parent.glob("*.tmp")) == []
_extract_lark_cli_runtime_binary(archive_bytes, destination)
assert destination.read_bytes() == payload
if os.name != "nt":
assert stat.S_IMODE(destination.stat().st_mode) == 0o755

View File

@ -1,222 +0,0 @@
"""Issue #3647 — LocalSandbox caches stable forward-path data while dynamic
host-path masking avoids process-global regex retention.
"""
from __future__ import annotations
import os
import re
from pathlib import Path
import pytest
from deerflow.sandbox.local import local_sandbox as local_sandbox_module
from deerflow.sandbox.local.local_sandbox import LocalSandbox, PathMapping
def _make_sandbox(tmp_path: Path) -> LocalSandbox:
ws = tmp_path / "workspace"
skills = tmp_path / "skills"
ws.mkdir()
skills.mkdir()
return LocalSandbox(
id="test",
path_mappings=[
PathMapping(container_path="/mnt/user-data/workspace", local_path=str(ws)),
PathMapping(container_path="/mnt/skills", local_path=str(skills), read_only=True),
],
)
def test_patterns_are_compiled_once_and_cached(tmp_path):
sb = _make_sandbox(tmp_path)
# Each cached_property returns the identical object across accesses.
assert sb._command_pattern is sb._command_pattern
assert sb._content_pattern is sb._content_pattern
assert sb._resolved_local_paths is sb._resolved_local_paths
def test_reverse_output_does_not_compile_regexes_for_unique_sandbox_paths(tmp_path):
"""Evicted sandboxes must not leave their thread paths in ``re``'s caches."""
resolved_tmp_path = str(tmp_path.resolve())
for index in range(32):
workspace = tmp_path / f"thread-{index}" / "workspace"
workspace.mkdir(parents=True)
sandbox = LocalSandbox(
id=f"thread-{index}",
path_mappings=[PathMapping(container_path="/mnt/user-data/workspace", local_path=str(workspace))],
)
assert sandbox._reverse_resolve_paths_in_output(f"wrote {workspace}/result.txt") == "wrote /mnt/user-data/workspace/result.txt"
assert all(resolved_tmp_path not in str(cache_key) for cache_key in re._cache)
def test_empty_mappings_yield_no_pattern(tmp_path):
sb = LocalSandbox(id="empty", path_mappings=[])
assert sb._command_pattern is None
assert sb._content_pattern is None
assert sb._resolved_local_paths == {}
# No mappings -> command/content pass through unchanged.
assert sb._resolve_paths_in_command("echo hello") == "echo hello"
assert sb._resolve_paths_in_content("plain text") == "plain text"
def test_command_paths_resolved_to_local(tmp_path):
sb = _make_sandbox(tmp_path)
# Command resolution spells the local path with forward slashes so bash
# never sees backslash escape sequences on Windows hosts.
ws_local = str((tmp_path / "workspace").resolve()).replace("\\", "/")
out = sb._resolve_paths_in_command("cat /mnt/user-data/workspace/foo.txt")
assert out == f"cat {ws_local}/foo.txt"
# Calling again uses the cached pattern and produces the same result.
assert sb._resolve_paths_in_command("cat /mnt/user-data/workspace/foo.txt") == out
def test_segment_boundary_not_matched_inside_longer_name(tmp_path):
sb = _make_sandbox(tmp_path)
# "/mnt/skills-extra" must NOT be rewritten by the "/mnt/skills" mapping.
out = sb._resolve_paths_in_command("ls /mnt/skills-extra/data")
assert out == "ls /mnt/skills-extra/data"
def test_reverse_resolve_output_maps_local_back_to_container(tmp_path):
sb = _make_sandbox(tmp_path)
ws_local = str((tmp_path / "workspace").resolve())
out = sb._reverse_resolve_paths_in_output(f"wrote {ws_local}/foo.txt ok")
assert out == "wrote /mnt/user-data/workspace/foo.txt ok"
@pytest.mark.parametrize("suffix", ["-extra/data.txt", "2/x", ".bak", "foo", "_backup/y"])
def test_reverse_resolve_does_not_match_inside_longer_sibling(tmp_path, suffix):
"""Mirror of test_segment_boundary_not_matched_inside_longer_name, reverse direction.
Without a segment-boundary lookahead the pattern matches the bare mount root
inside a sibling that shares its prefix. The extracted text then *equals* the
mount root, so ``_reverse_resolve_path``'s own ``+ "/"`` guard is satisfied and
the sibling is rewritten to ``/mnt/skills<suffix>`` — a container path forward
resolution refuses to map back, so the model can never read it.
"""
sb = _make_sandbox(tmp_path)
skills_local = str((tmp_path / "skills").resolve())
sibling = f"{skills_local}{suffix}"
out = sb._reverse_resolve_paths_in_output(f"see {sibling}")
assert out == f"see {sibling}"
assert "/mnt/skills" not in out
@pytest.mark.parametrize(
("trailer", "expected_trailer"),
[
(", ok", ", ok"), # comma — a path can end a clause in prose output
(":/other", ":/other"), # colon — PATH-style concatenation
("\\win\\p", "/win/p"), # backslash — Windows-style separator
(" done", " done"), # whitespace
("' ", "' "), # quote
],
)
def test_reverse_resolve_still_matches_root_before_non_slash_boundaries(tmp_path, trailer, expected_trailer):
"""The narrowing must not drop boundaries the old pattern accepted.
``_reverse_output_patterns`` runs over arbitrary command output, so the mount
root can legitimately be followed by ``,``, ``:`` or ``\\``. Copying
``_command_pattern``'s shell-oriented boundary class here would silently stop
translating all three; this pins the ``_content_pattern`` class that does not.
"""
sb = _make_sandbox(tmp_path)
skills_local = str((tmp_path / "skills").resolve())
out = sb._reverse_resolve_paths_in_output(f"{skills_local}{trailer}")
assert out == f"/mnt/skills{expected_trailer}"
@pytest.mark.parametrize("prefix", ["", "cwd: ", "see "])
def test_reverse_resolve_translates_a_bare_root_at_end_of_output(tmp_path, prefix):
"""The lookahead's ``$`` alternative, pinned on its own.
Output ending exactly at a mount root (no trailing separator, no newline —
``printf '%s' "$PWD"``, a stripped last line, a truncated buffer) satisfies
neither ``/`` nor ``[^\\w./-]``. Drop ``$`` and the match fails, so the raw
host path is handed to the model instead of the container path: the leak
this whole function exists to prevent. The suite is otherwise blind to it —
removing ``$`` leaves all 6866 tests green.
"""
sb = _make_sandbox(tmp_path)
skills_local = str((tmp_path / "skills").resolve())
out = sb._reverse_resolve_paths_in_output(f"{prefix}{skills_local}")
assert out == f"{prefix}/mnt/skills"
assert skills_local not in out
def test_reverse_resolve_path_matches_windows_backslash_containment(monkeypatch):
"""Regression for the os.sep containment fix in ``_reverse_resolve_path``.
``Path.resolve()`` always renders with the native separator (backslash on
Windows). The containment check used to hardcode a ``"/"`` suffix, so a
backslash-joined nested path could never satisfy
``path_str.startswith(local_path_resolved + "/")`` on Windows and silently
fell through to the "no mapping found" branch, leaking the raw host path
(real username, full directory tree) instead of the virtual
``/mnt/user-data/...`` path.
CI runs only on POSIX, so the test stubs ``os.path.realpath`` and the cached
roots to reproduce Windows' backslash-joined result.
"""
sb = LocalSandbox(
id="windows-sep-test",
path_mappings=[
PathMapping(container_path="/mnt/user-data/workspace", local_path="C:\\Users\\test\\workspace"),
],
)
mapping = sb.path_mappings[0]
monkeypatch.setattr(local_sandbox_module.os, "sep", "\\")
# Bypass the real (POSIX) filesystem resolution this cached_property would
# otherwise perform and pin it directly to the Windows-resolved root.
sb._resolved_local_paths = {mapping: "C:\\Users\\test\\workspace"}
monkeypatch.setattr(local_sandbox_module.os.path, "realpath", lambda raw: raw.replace("/", "\\"))
result = sb._reverse_resolve_path("C:\\Users\\test\\workspace\\sub\\f.txt")
assert result == "/mnt/user-data/workspace/sub/f.txt"
def test_resolved_paths_and_sorted_views_are_cached(tmp_path):
sb = _make_sandbox(tmp_path)
# Resolved-local map and sorted views are computed once and reused.
assert sb._resolved_local_paths is sb._resolved_local_paths
assert sb._mappings_by_container_specificity is sb._mappings_by_container_specificity
assert sb._mappings_by_local_specificity is sb._mappings_by_local_specificity
# Map covers every mapping with its filesystem-resolved local root.
assert set(sb._resolved_local_paths.values()) == {
str((tmp_path / "workspace").resolve()),
str((tmp_path / "skills").resolve()),
}
# Most-specific (longest) container path is ordered first.
assert sb._mappings_by_container_specificity[0].container_path == "/mnt/user-data/workspace"
def test_forward_resolution_behavior_unchanged(tmp_path):
sb = _make_sandbox(tmp_path)
ws_local = str((tmp_path / "workspace").resolve())
# _resolve_path feeds file operations, so the resolved path keeps the
# native separator spelling (os.path.join/realpath).
expected = os.path.join(ws_local, "sub", "foo.txt")
# Container path resolves to the mapped local path.
assert sb._resolve_path("/mnt/user-data/workspace/sub/foo.txt") == expected
# An unmapped path is returned unchanged.
assert sb._resolve_path("/etc/hosts") == "/etc/hosts"
def test_read_only_mount_detected(tmp_path):
sb = _make_sandbox(tmp_path)
skills_local = str((tmp_path / "skills").resolve())
ws_local = str((tmp_path / "workspace").resolve())
assert sb._is_read_only_path(f"{skills_local}/a.md") is True
assert sb._is_read_only_path(f"{ws_local}/a.txt") is False

View File

@ -1,116 +0,0 @@
"""Configured stdio working directories must reach discovery and pooled calls."""
import json
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
from deerflow.config.paths import Paths
from deerflow.mcp import tools as mcp_tools
from deerflow.mcp.session_pool import MCPSessionPool
@pytest.mark.asyncio
@pytest.mark.parametrize("thread_incarnation", [None, "incarnation-1"], ids=["legacy", "versioned"])
@pytest.mark.parametrize("relative_script", [True, False], ids=["relative-entrypoint", "relative-tool-input"])
async def test_stdio_cwd_from_config_reaches_discovery_and_tool_calls(tmp_path, monkeypatch, relative_script, thread_incarnation):
server_dir = tmp_path / "mcp server"
server_dir.mkdir()
(server_dir / "marker.txt").write_text("configured-directory", encoding="utf-8")
server_path = server_dir / "server.py"
server_path.write_text(
"""
from pathlib import Path
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("cwd-test")
@mcp.tool()
def read_marker() -> str:
return Path("marker.txt").read_text(encoding="utf-8")
mcp.run(transport="stdio")
""",
encoding="utf-8",
)
config_path = tmp_path / "extensions_config.json"
config_path.write_text(
json.dumps(
{
"mcpServers": {
"local": {
"command": sys.executable,
"args": ["server.py" if relative_script else str(server_path)],
"cwd": "$TEST_MCP_CWD",
}
}
}
),
encoding="utf-8",
)
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(config_path))
monkeypatch.setenv("TEST_MCP_CWD", str(server_dir))
monkeypatch.setattr(mcp_tools, "get_paths", lambda: Paths(tmp_path / "deerflow"))
pool = MCPSessionPool()
monkeypatch.setattr(mcp_tools, "get_session_pool", lambda: pool)
runtime = SimpleNamespace(context={"thread_id": "thread", "user_id": "user", "thread_incarnation": thread_incarnation}, config={})
try:
tools = await mcp_tools.get_mcp_tools()
assert [tool.name for tool in tools] == ["local_read_marker"]
content, _artifact = await tools[0].coroutine(deerflow_runtime=runtime)
assert content[0]["text"] == "configured-directory"
finally:
await pool.close_all()
@pytest.mark.asyncio
@pytest.mark.parametrize("thread_incarnation", [None, "incarnation-1"], ids=["legacy", "versioned"])
@pytest.mark.parametrize(
"cwd_config",
[{}, {"cwd": None}, {"cwd": ""}, {"cwd": "$TEST_UNSET_MCP_CWD"}],
ids=["omitted", "null", "empty", "unset-env"],
)
async def test_empty_stdio_cwd_preserves_default_working_directories(tmp_path, monkeypatch, cwd_config, thread_incarnation):
launch_dir = tmp_path / "gateway"
launch_dir.mkdir()
monkeypatch.chdir(launch_dir)
monkeypatch.delenv("TEST_UNSET_MCP_CWD", raising=False)
server_path = tmp_path / "cwd_server.py"
server_path.write_text(
"""
from pathlib import Path
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("cwd-defaults-test")
@mcp.tool(description=str(Path.cwd()))
def read_cwd() -> str:
return str(Path.cwd())
mcp.run(transport="stdio")
""",
encoding="utf-8",
)
config_path = tmp_path / "extensions_config.json"
config_path.write_text(
json.dumps({"mcpServers": {"local": {"command": sys.executable, "args": [str(server_path)], **cwd_config}}}),
encoding="utf-8",
)
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(config_path))
paths = Paths(tmp_path / "deerflow")
monkeypatch.setattr(mcp_tools, "get_paths", lambda: paths)
pool = MCPSessionPool()
monkeypatch.setattr(mcp_tools, "get_session_pool", lambda: pool)
runtime = SimpleNamespace(context={"thread_id": "thread", "user_id": "user", "thread_incarnation": thread_incarnation}, config={})
try:
tools = await mcp_tools.get_mcp_tools()
assert [tool.name for tool in tools] == ["local_read_cwd"]
assert Path(tools[0].description) == launch_dir.resolve()
content, _artifact = await tools[0].coroutine(deerflow_runtime=runtime)
assert Path(content[0]["text"]) == paths.sandbox_work_dir("thread", user_id="user").resolve()
finally:
await pool.close_all()

View File

@ -1,222 +0,0 @@
"""Gateway writer coverage for the shared ``mcpLifecycle`` generation."""
from __future__ import annotations
import json
import pytest
# Reuse the process-local isolation fixture so these writer tests do not leak
# cache globals or the session-pool singleton.
from test_mcp_cache_reconciliation import reconciler # noqa: F401
from app.gateway.routers import mcp as mcp_router
from app.gateway.routers.mcp import (
McpConfigUpdateRequest,
McpServerConfigResponse,
McpServerConfigUpdateRequest,
McpServerStateUpdateRequest,
)
from deerflow.config.extensions_config import ExtensionsConfig
from deerflow.mcp.lifecycle import _stdio_fingerprints, legacy_token, parse_mcp_lifecycle
def _stdio(command: str) -> dict:
return {"enabled": True, "type": "stdio", "command": command}
def _write(cfg, servers: dict) -> None:
cfg.write_text(json.dumps({"mcpServers": servers, "skills": {}}), encoding="utf-8")
def _ledger(cfg):
return parse_mcp_lifecycle(json.loads(cfg.read_text(encoding="utf-8")))
def _legacy(cfg, name: str) -> str:
fingerprint = _stdio_fingerprints(ExtensionsConfig.from_file(str(cfg)))[name]
return legacy_token(name, fingerprint)
def test_state_disable_and_reenable_advance_the_generation(
reconciler, # noqa: F811 - fixture imported from the sibling suite
monkeypatch,
tmp_path,
):
"""Raw in-place mutation must not hide the enabled -> disabled transition."""
cfg = tmp_path / "extensions_config.json"
_write(cfg, {"A": _stdio("npx")})
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(cfg))
baseline = _legacy(cfg, "A")
mcp_router._apply_mcp_server_state_update(McpServerStateUpdateRequest(server_name="A", enabled=False))
disabled = _ledger(cfg).servers["A"]
assert disabled != baseline
mcp_router._apply_mcp_server_state_update(McpServerStateUpdateRequest(server_name="A", enabled=True))
reenabled = _ledger(cfg).servers["A"]
assert reenabled not in {baseline, disabled}
def test_delete_then_identical_readd_advance_the_generation(
reconciler, # noqa: F811 - fixture imported from the sibling suite
monkeypatch,
tmp_path,
):
"""Delete tombstones the token and an identical re-add must rotate it again."""
cfg = tmp_path / "extensions_config.json"
_write(cfg, {"A": _stdio("npx")})
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(cfg))
mcp_router._apply_mcp_server_delete("A")
tombstone = _ledger(cfg).servers["A"]
mcp_router._apply_mcp_servers_create(
McpConfigUpdateRequest(
mcp_servers={"A": McpServerConfigResponse(enabled=True, type="stdio", command="npx")},
)
)
rebound = _ledger(cfg).servers["A"]
assert rebound != tombstone
def test_commit_helper_requires_previous_raw():
"""A controlled write must not be able to skip the lifecycle commit."""
import inspect
parameters = inspect.signature(mcp_router._commit_mcp_config_write).parameters
assert parameters["previous_raw"].default is inspect.Parameter.empty
def test_bulk_update_rotates_only_the_changed_server_token(
reconciler, # noqa: F811 - fixture imported from the sibling suite
monkeypatch,
tmp_path,
):
"""A connection change rotates only that server's token; $VAR stays raw."""
cfg = tmp_path / "extensions_config.json"
cfg.write_text(
json.dumps(
{
"mcpServers": {
"A": {"enabled": True, "type": "stdio", "command": "npx", "args": ["pkg-v1"]},
"B": {"enabled": True, "type": "stdio", "command": "npx", "args": ["pkg-b"], "env": {"TOKEN": "$PE_VAR"}},
},
"skills": {},
}
),
encoding="utf-8",
)
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(cfg))
monkeypatch.setenv("PE_VAR", "secret-value")
baseline_a = _legacy(cfg, "A")
mcp_router._apply_mcp_config_update(
McpConfigUpdateRequest(
mcp_servers={
"A": McpServerConfigResponse(enabled=True, type="stdio", command="npx", args=["pkg-v2"]),
"B": McpServerConfigResponse(enabled=True, type="stdio", command="npx", args=["pkg-b"], env={"TOKEN": "$PE_VAR"}),
}
)
)
ledger = _ledger(cfg)
assert ledger is not None
assert ledger.servers["A"] != baseline_a # the connection changed
assert "B" not in ledger.servers # unchanged legacy server stays derived
written = cfg.read_text(encoding="utf-8")
assert "$PE_VAR" in written
assert "secret-value" not in written
@pytest.mark.asyncio
async def test_targeted_metadata_update_keeps_token_and_session(
reconciler, # noqa: F811 - fixture imported from the sibling suite
monkeypatch,
tmp_path,
):
"""A metadata-only targeted update must not rotate the token or the session."""
import test_mcp_cache_reconciliation as harness
from deerflow.mcp import session_pool as session_pool_module
from deerflow.mcp.session_pool import MCPSessionPool
cfg = tmp_path / "extensions_config.json"
pool = MCPSessionPool()
session_pool_module._pool = pool
log = harness._session_log()
await harness._initialize(monkeypatch, cfg, {"A": _stdio("npx")}, log)
binding_before = harness._binding(pool, "A")
entry_before = harness._entry(pool, "A")
mcp_router._apply_mcp_server_config_update(
McpServerConfigUpdateRequest(
server_name="A",
server=McpServerConfigResponse(enabled=True, type="stdio", command="npx", description="renamed"),
)
)
ledger = _ledger(cfg)
assert ledger is not None and "A" not in ledger.servers # metadata-only: no token
assert harness._binding(pool, "A") is binding_before
assert harness._entry(pool, "A") is entry_before
assert json.loads(cfg.read_text(encoding="utf-8"))["mcpServers"]["A"]["description"] == "renamed"
def test_skills_writer_preserves_the_lifecycle_ledger(
reconciler, # noqa: F811 - fixture imported from the sibling suite
monkeypatch,
tmp_path,
):
"""A skills-only write must round-trip mcpLifecycle exactly (no recompute)."""
from unittest.mock import MagicMock
from app.gateway.routers import skills as skills_router
cfg = tmp_path / "extensions_config.json"
ledger = {"version": 1, "servers": {"A": "a" * 32, "B": "b" * 32}}
cfg.write_text(
json.dumps(
{
"mcpServers": {"A": {"enabled": True, "type": "stdio", "command": "npx"}},
"skills": {"demo": {"enabled": True}},
"mcpLifecycle": ledger,
}
),
encoding="utf-8",
)
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(cfg))
monkeypatch.setattr("app.gateway.routers.skills.reload_extensions_config", lambda *a, **k: None)
skills_router._write_extensions_skill_state(MagicMock(_app_config=None), "demo", False, rebuild_public_projection=False)
written = json.loads(cfg.read_text(encoding="utf-8"))
assert written["mcpLifecycle"] == ledger # preserved byte-for-value, never recomputed
assert written["skills"]["demo"]["enabled"] is False
def test_removed_servers_drop_their_stored_oauth_tokens_and_disabled_ones_keep_them(
reconciler, # noqa: F811 - fixture imported from the sibling suite
monkeypatch,
tmp_path,
):
"""A removed server's mcp_oauth_tokens row can never be read again; a disabled one may be re-enabled."""
from unittest.mock import MagicMock
from deerflow.mcp.oauth_store import set_mcp_oauth_token_store
cfg = tmp_path / "extensions_config.json"
_write(cfg, {"A": _stdio("npx"), "B": _stdio("uvx")})
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(cfg))
store = MagicMock()
store.forget_unconfigured.return_value = 0
previous = set_mcp_oauth_token_store(store)
try:
mcp_router._apply_mcp_server_state_update(McpServerStateUpdateRequest(server_name="A", enabled=False))
mcp_router._apply_mcp_server_delete("B")
finally:
set_mcp_oauth_token_store(previous)
calls = [(call.args[0], set(call.args[1])) for call in store.forget_unconfigured.call_args_list]
assert calls == [(None, {"A", "B"}), (None, {"A"})]

View File

@ -1,32 +0,0 @@
import pytest
from pydantic import ValidationError
from deerflow.config.app_config import AppConfig
from deerflow.config.mcp_tasks_config import McpTasksConfig
from deerflow.config.reload_boundary import STARTUP_ONLY_FIELDS, STARTUP_ONLY_PREFIX
def test_mcp_task_runtime_is_disabled_by_default_and_bounded():
config = McpTasksConfig()
assert config.enabled is False
assert config.poll_interval_seconds == 5
assert config.lease_seconds == 120
assert config.max_concurrent_polls == 8
assert config.max_poll_backoff_seconds == 300
assert config.input_required_poll_interval_seconds == 60
assert config.tracking_degraded_after_errors == 3
assert config.max_result_bytes == 65_536
assert config.result_preview_max_chars == 2_000
with pytest.raises(ValidationError):
McpTasksConfig(poll_interval_seconds=0)
with pytest.raises(ValidationError):
McpTasksConfig(max_concurrent_polls=0)
with pytest.raises(ValidationError):
McpTasksConfig(max_result_bytes=10)
def test_mcp_task_runtime_is_registered_as_startup_only():
assert "mcp_tasks" in STARTUP_ONLY_FIELDS
field = AppConfig.model_fields["mcp_tasks"]
assert (field.description or "").startswith(STARTUP_ONLY_PREFIX)

View File

@ -1,75 +0,0 @@
"""Tests for user_id propagation through memory queue (DI)."""
from unittest.mock import MagicMock, patch
from deerflow.agents.memory.backends.deermem.deermem.config import DeerMemConfig
from deerflow.agents.memory.backends.deermem.deermem.core.queue import ConversationContext, MemoryUpdateQueue
def _queue(updater: MagicMock | None = None) -> MemoryUpdateQueue:
return MemoryUpdateQueue(DeerMemConfig(), updater or MagicMock())
def test_conversation_context_has_user_id():
ctx = ConversationContext(thread_id="t1", messages=[], user_id="alice")
assert ctx.user_id == "alice"
def test_conversation_context_user_id_default_none():
ctx = ConversationContext(thread_id="t1", messages=[])
assert ctx.user_id is None
def test_queue_add_stores_user_id():
q = _queue()
with patch.object(q, "_rearm_timer_locked"):
q.add(thread_id="t1", messages=["msg"], user_id="alice")
assert len(q._items) == 1
assert q._items[0].user_id == "alice"
q.clear()
def test_queue_process_passes_user_id_to_updater():
mock_updater = MagicMock()
mock_updater.update_memory.return_value = True
q = _queue(mock_updater)
with patch.object(q, "_rearm_timer_locked"):
q.add(thread_id="t1", messages=["msg"], user_id="alice")
q.flush()
mock_updater.update_memory.assert_called_once()
assert mock_updater.update_memory.call_args.kwargs["user_id"] == "alice"
def test_queue_keeps_updates_for_different_users_in_same_thread_and_agent():
q = _queue()
with patch.object(q, "_rearm_timer_locked"):
q.add(thread_id="main", messages=["alice update"], agent_name="researcher", user_id="alice")
q.add(thread_id="main", messages=["bob update"], agent_name="researcher", user_id="bob")
assert q.pending_count == 2
assert [context.user_id for context in q._items] == ["alice", "bob"]
assert [context.messages for context in q._items] == [["alice update"], ["bob update"]]
def test_queue_still_coalesces_updates_for_same_user_thread_and_agent():
q = _queue()
with patch.object(q, "_rearm_timer_locked"):
q.add(thread_id="main", messages=["first"], agent_name="researcher", user_id="alice")
q.add(thread_id="main", messages=["second"], agent_name="researcher", user_id="alice")
assert q.pending_count == 1
assert q._items[0].messages == ["second"]
assert q._items[0].user_id == "alice"
assert q._items[0].agent_name == "researcher"
def test_add_nowait_keeps_different_users_separate():
q = _queue()
with patch.object(q, "_schedule_timer"):
q.add_nowait(thread_id="main", messages=["alice update"], agent_name="researcher", user_id="alice")
q.add_nowait(thread_id="main", messages=["bob update"], agent_name="researcher", user_id="bob")
assert q.pending_count == 2
assert [context.user_id for context in q._items] == ["alice", "bob"]

View File

@ -357,12 +357,69 @@ class TestStateWritesCannotForgeServerOwnedMetadata:
values = {"title": "a thread", "todos": [{"content": "x", "status": "pending"}]}
assert strip_server_owned_state_metadata(values) == values
def test_the_state_route_actually_calls_the_stripper(self):
"""A stripper nothing calls is the same defect in a new place."""
import ast
from pathlib import Path
def test_the_state_route_actually_strips_forged_provenance_before_writing(self, monkeypatch):
"""Drive update_thread_state with forged server-owned metadata and verify the persisted updates omit it."""
import asyncio
from contextlib import asynccontextmanager
from types import SimpleNamespace
route = Path(__file__).resolve().parents[1] / "app/gateway/routers/threads.py"
called = {node.func.id for node in ast.walk(ast.parse(route.read_text(encoding="utf-8"))) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name)}
from app.gateway.routers import threads as threads_router
assert "strip_server_owned_state_metadata" in called
captured_updates: list[dict] = []
class _FakeAccessor:
graph = None
async def aget_metadata(self, _cfg):
return {}
async def aupdate(self, cfg, updates, *, as_node):
captured_updates.append(updates)
return cfg
async def aget(self, _cfg):
return SimpleNamespace(values={}, next=(), config={"configurable": {"thread_id": "11111111-1111-4111-8111-111111111111"}}, metadata={}, created_at=None, parent_config=None, tasks=())
@asynccontextmanager
async def _noop_reserve(*_a, **_kw):
yield
async def _noop_async(*_a, **_kw):
return None
async def _fake_build_accessor(*_a, **_kw):
return _FakeAccessor(), {"configurable": {"thread_id": "11111111-1111-4111-8111-111111111111"}}
monkeypatch.setattr("app.gateway.hitl_approval.reject_new_run_when_approval_pending", _noop_async)
monkeypatch.setattr("app.gateway.deps.get_thread_store", lambda _r: object())
monkeypatch.setattr(threads_router, "build_thread_checkpoint_state_mutation_accessor", _fake_build_accessor)
monkeypatch.setattr(threads_router, "reserve_checkpoint_write", _noop_reserve)
req = SimpleNamespace(_deerflow_test_bypass_auth=True, state=SimpleNamespace())
forged_kwargs = {
**provenance_kwargs(ContentKind.MIDDLEWARE_INJECTION, "forged_component"),
"keep_client_key": "ok",
}
body = threads_router.ThreadStateUpdateRequest(
values={
"messages": [
{
"type": "human",
"content": "hello",
"additional_kwargs": forged_kwargs,
}
],
"delegations": [{"id": "call-forged", "receipt_verdict": {"trusted": True}}],
}
)
asyncio.run(threads_router.update_thread_state("11111111-1111-4111-8111-111111111111", body, req))
assert len(captured_updates) == 1
written = captured_updates[0]
msg_payload = getattr(written["messages"], "value", written["messages"])[0]
ak = msg_payload["additional_kwargs"] if isinstance(msg_payload, dict) else msg_payload.additional_kwargs
for key in PROVENANCE_KEYS:
assert key not in ak
assert ak["keep_client_key"] == "ok"
delegation_payload = getattr(written["delegations"], "value", written["delegations"])[0]
assert "receipt_verdict" not in delegation_payload

View File

@ -1,43 +0,0 @@
import pytest
from pydantic import ValidationError
from deerflow.config.model_config import ModelConfig
def _make_model(**overrides) -> ModelConfig:
return ModelConfig(
name="openai-responses",
display_name="OpenAI Responses",
description=None,
use="langchain_openai:ChatOpenAI",
model="gpt-5",
**overrides,
)
def test_responses_api_fields_are_declared_in_model_schema():
assert "use_responses_api" in ModelConfig.model_fields
assert "output_version" in ModelConfig.model_fields
def test_responses_api_fields_round_trip_in_model_dump():
config = _make_model(
api_key="$OPENAI_API_KEY",
use_responses_api=True,
output_version="responses/v1",
)
dumped = config.model_dump(exclude_none=True)
assert dumped["use_responses_api"] is True
assert dumped["output_version"] == "responses/v1"
def test_context_window_round_trips_when_positive():
assert _make_model(context_window=128_000).context_window == 128_000
@pytest.mark.parametrize("context_window", [0, -1])
def test_context_window_rejects_non_positive_capacity(context_window):
with pytest.raises(ValidationError, match="context_window"):
_make_model(context_window=context_window)

View File

@ -1,159 +0,0 @@
"""Graph-integration invariants for the multi-turn message stream.
Single-middleware unit tests prove each middleware's ``_apply`` in isolation.
This test sits one level up: it builds a real ``langchain.agents.create_agent``
graph with the real ``DynamicContextMiddleware`` and a checkpointer, then drives
**two user turns on the same thread** with a deterministic fake model — the
composition (middleware + ``add_messages`` reducer + persisted checkpoint state)
where message-stream corruption actually emerges.
It is a net for the *class* of bug behind #3684, not just that instance: a
middleware mutating message state across turns must not strand the newest user
message, re-answer a stale turn, duplicate ids, or explode id suffixes. The
trigger condition is memory injection enabled (a separate dateless ``<memory>``
reminder lands in history) — so memory is stubbed on, deterministically.
Why here and not e2e replay: replay disables memory, uses a single-turn golden,
and replays recorded model output by input-hash while asserting SSE *shape* — so
it cannot reproduce or detect this class. This runs at unit speed in ``make test``
(the ``backend-unit-tests`` workflow) with no gateway, SSE, fixtures, or API key.
To widen the net, add more state-touching middlewares (input sanitization,
summarization, uploads) to ``_STREAM_MIDDLEWARES`` and keep the invariants.
"""
from __future__ import annotations
from typing import Any
from unittest import mock
from langchain.agents import create_agent
from langchain.agents.middleware import AgentMiddleware
from langchain.agents.middleware.types import ModelRequest, ModelResponse
from langchain_core.language_models.fake_chat_models import FakeMessagesListChatModel
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.runnables import Runnable
from langgraph.checkpoint.memory import InMemorySaver
from deerflow.agents.middlewares.dynamic_context_middleware import (
DynamicContextMiddleware,
is_dynamic_context_reminder,
)
_TURN_1 = "test"
_TURN_2 = "tell me the weather of next week in berlin"
_FIXED_DATE = "2026-05-08, Friday"
_MEMORY = "<memory>\nUser prefers concise answers.\n</memory>"
class _FakeModel(FakeMessagesListChatModel):
"""Deterministic model with the no-op ``bind_tools`` ``create_agent`` needs."""
def bind_tools(self, tools: Any, *, tool_choice: Any = None, **kwargs: Any) -> Runnable: # type: ignore[override]
return self
class _RecordModelInput(AgentMiddleware):
"""Capture the message list handed to the model on each call.
The bug is observable here: on turn 2 the model must receive the new user
message as its latest human turn, not a re-injected stale one.
"""
def __init__(self) -> None:
super().__init__()
self.calls: list[list[Any]] = []
def wrap_model_call(self, request: ModelRequest, handler) -> ModelResponse:
self.calls.append(list(request.messages))
return handler(request)
async def awrap_model_call(self, request: ModelRequest, handler) -> ModelResponse:
self.calls.append(list(request.messages))
return await handler(request)
def _msg_text(msg: Any) -> str:
"""Flatten a message's content (string or list-of-blocks) to plain text."""
content = msg.content
if isinstance(content, list):
return "\n".join(b.get("text", "") for b in content if isinstance(b, dict))
return content
def _last_human_text(messages: list[Any]) -> str:
"""Text of the last genuine (non-hidden, non-reminder) human message."""
for msg in reversed(messages):
if not isinstance(msg, HumanMessage):
continue
if msg.additional_kwargs.get("hide_from_ui") or is_dynamic_context_reminder(msg):
continue
return _msg_text(msg)
return ""
def _assert_stream_well_formed(messages: list[Any], *, newest_user_text: str) -> None:
"""Structural invariants the multi-turn message stream must satisfy.
Checked semantic-first: the primary guarantee (the newest user message is the
latest human turn) fails before the structural id checks, so the regression
surfaces as a meaning-level failure rather than only an id-shape artifact.
"""
# The newest user message must be the latest human turn the model reasons about.
assert _last_human_text(messages) == newest_user_text, "newest user message is not the latest human turn (stranded / stale re-answer)"
# ...and it must appear exactly once (not stranded earlier + re-appended).
occurrences = sum(1 for m in messages if isinstance(m, HumanMessage) and _msg_text(m) == newest_user_text)
assert occurrences == 1, f"newest user message appears {occurrences} times, expected 1"
ids = [m.id for m in messages if m.id is not None]
assert len(ids) == len(set(ids)), f"duplicate message ids in stream: {ids}"
# ID-swap derives one ``__user`` suffix per reminder injection. A doubled
# ``__user__user`` means a turn was re-injected onto an already-injected
# message — the #3684 signature.
assert not any("__user__user" in (mid or "") for mid in ids), f"id-suffix explosion (re-injection): {ids}"
# State-touching middlewares under test, as zero-arg factories. Widen the net by
# adding more here (e.g. InputSanitizationMiddleware, SummarizationMiddleware) — the
# invariants in _assert_stream_well_formed apply to the whole composition.
_STREAM_MIDDLEWARES: tuple[type[AgentMiddleware], ...] = (DynamicContextMiddleware,)
def _run_two_turns() -> tuple[dict, _RecordModelInput]:
recorder = _RecordModelInput()
agent = create_agent(
model=_FakeModel(responses=[AIMessage(content="ack-1"), AIMessage(content="ack-2")]),
tools=[],
# Recorder first so its wrap_model_call observes the final request;
# the state-touching middlewares do their work in before_agent.
middleware=[recorder, *(make() for make in _STREAM_MIDDLEWARES)],
checkpointer=InMemorySaver(),
)
cfg = {"configurable": {"thread_id": "stream-invariants-1"}}
with (
mock.patch("deerflow.agents.lead_agent.prompt._get_memory_context", return_value=_MEMORY),
mock.patch("deerflow.agents.middlewares.dynamic_context_middleware.datetime") as mock_dt,
):
mock_dt.now.return_value.strftime.return_value = _FIXED_DATE
agent.invoke({"messages": [HumanMessage(content=_TURN_1, id="u1")]}, cfg)
final = agent.invoke({"messages": [HumanMessage(content=_TURN_2, id="u2")]}, cfg)
return final, recorder
def test_second_turn_model_receives_newest_user_message():
"""The model on turn 2 must reason about the new message, not a stale one."""
_final, recorder = _run_two_turns()
assert len(recorder.calls) >= 2, f"expected a model call per turn, got {len(recorder.calls)}"
turn_2_request = recorder.calls[-1]
_assert_stream_well_formed(turn_2_request, newest_user_text=_TURN_2)
def test_second_turn_persisted_state_is_well_formed():
"""The persisted checkpoint state after turn 2 stays ordered and de-duplicated."""
final, _recorder = _run_two_turns()
_assert_stream_well_formed(final["messages"], newest_user_text=_TURN_2)

View File

@ -1,128 +0,0 @@
"""Notification obligations must share the occurrence's commit boundary."""
from datetime import UTC, datetime
import pytest
from sqlalchemy import func, select
from deerflow.persistence.notification_deliveries import NotificationDeliveryRepository, NotificationDeliveryRow
@pytest.fixture
async def delivery_repo(tmp_path):
from deerflow.persistence.engine import close_engine, get_session_factory, init_engine
await init_engine("sqlite", url=f"sqlite+aiosqlite:///{tmp_path / 'notifications.db'}", sqlite_dir=str(tmp_path))
try:
yield NotificationDeliveryRepository(get_session_factory())
finally:
await close_engine()
def delivery_args(**overrides):
return {
"task_id": "task-a",
"task_run_id": "occurrence-a",
"run_id": "run-a",
"event": "task_paused",
"provider": "wecom",
"target": "alice-external",
"owner_user_id": "alice",
"payload": {"reason_code": "consecutive_unmet"},
**overrides,
}
@pytest.mark.anyio
async def test_delivery_is_not_visible_until_occurrence_transaction_commits(delivery_repo):
async with delivery_repo.session_factory() as session:
inserted = await delivery_repo.enqueue_in_session(session, **delivery_args())
assert inserted["status"] == "pending"
async with delivery_repo.session_factory() as reader:
assert await reader.scalar(select(func.count()).select_from(NotificationDeliveryRow)) == 0
await session.commit()
claimed = await delivery_repo.claim_due_deliveries(now=datetime.now(UTC), limit=5)
assert [row["id"] for row in claimed] == [inserted["id"]]
@pytest.mark.anyio
async def test_rollback_cannot_leave_a_pause_notification_without_the_pause(delivery_repo):
async with delivery_repo.session_factory() as session:
await delivery_repo.enqueue_in_session(session, **delivery_args())
await session.rollback()
assert await delivery_repo.claim_due_deliveries(now=datetime.now(UTC), limit=5) == []
@pytest.mark.anyio
async def test_completion_replay_preserves_the_first_delivery_payload(delivery_repo):
async with delivery_repo.session_factory() as session:
first = await delivery_repo.enqueue_in_session(session, **delivery_args())
second = await delivery_repo.enqueue_in_session(session, **delivery_args(payload={"reason_code": "different"}))
assert first["id"] == second["id"]
assert second["payload"] == first["payload"]
await session.commit()
assert len(await delivery_repo.claim_due_deliveries(now=datetime.now(UTC), limit=5)) == 1
@pytest.mark.anyio
async def test_one_occurrence_can_commit_unmet_and_pause_notices_together(delivery_repo):
async with delivery_repo.session_factory() as session:
await delivery_repo.enqueue_in_session(session, **delivery_args(event="run_unmet"))
await delivery_repo.enqueue_in_session(session, **delivery_args(event="task_paused"))
await session.commit()
claimed = await delivery_repo.claim_due_deliveries(now=datetime.now(UTC), limit=5)
assert {row["event"] for row in claimed} == {"run_unmet", "task_paused"}
@pytest.mark.anyio
async def test_observer_notice_commits_and_rolls_back_with_complete_run(delivery_repo):
"""The finalization observer stages the v2 notice inside complete_run's transaction."""
from app.scheduler.service import ScheduledTaskService
from deerflow.persistence.channel_connections import ChannelConnectionRepository
from deerflow.persistence.channel_connections.model import ChannelConnectionRow
from deerflow.persistence.scheduled_task_runs import ScheduledTaskRunRepository
from deerflow.persistence.scheduled_tasks import ScheduledTaskRepository
sf = delivery_repo.session_factory
tasks, runs = ScheduledTaskRepository(sf), ScheduledTaskRunRepository(sf)
service = ScheduledTaskService(task_repo=tasks, task_run_repo=runs, launch_run=None, poll_interval_seconds=60, lease_seconds=30, max_concurrent_runs=3, connection_repo=ChannelConnectionRepository(sf), notification_repo=delivery_repo)
now = datetime(2026, 10, 6, 8, tzinfo=UTC)
await tasks.create(
task_id="task-a",
user_id="alice",
thread_id=None,
context_mode="fresh_thread_per_run",
assistant_id=None,
title="Digest",
prompt="Summarize.",
schedule_type="interval",
schedule_spec={"every_seconds": 3600},
timezone="UTC",
next_run_at=now,
)
await runs.create(run_record_id="occurrence-a", task_id="task-a", thread_id="thread-a", scheduled_for=now, trigger="scheduled", status="running")
await runs.update_status("occurrence-a", status="running", run_id="run-a", started_at=now)
async with sf() as session:
session.add(ChannelConnectionRow(id="binding", owner_user_id="alice", provider="wecom", status="connected", external_account_id="alice-external"))
await session.commit()
async def fails_after_enqueue(session, task, occurrence, *, events):
await service._on_finalization(session, task, occurrence, events=events)
assert await session.scalar(select(func.count()).select_from(NotificationDeliveryRow)) == 1
raise RuntimeError("commit interrupted")
def complete():
return tasks.complete_run("task-a", user_id="alice", task_run_id="occurrence-a", run_id="run-a", status="success", error=None, finished_at=now)
tasks.set_finalization_observer(fails_after_enqueue)
with pytest.raises(RuntimeError, match="commit interrupted"):
await complete()
# The notice rolled back with the outcome; the occurrence is still active.
assert await delivery_repo.claim_due_deliveries(now=datetime.now(UTC), limit=5) == []
assert (await runs.list_by_task("task-a"))[0]["status"] == "running"
tasks.set_finalization_observer(service._on_finalization)
assert await complete() is True
(claimed,) = await delivery_repo.claim_due_deliveries(now=datetime.now(UTC), limit=5)
assert (claimed["event"], claimed["task_run_id"], claimed["run_id"]) == ("run_completed", "occurrence-a", "run-a")
assert claimed["payload"] == {"payload_version": 2, "task_id": "task-a", "task_title": "Digest", "locale": None, "reason_code": None, "latest_reason_code": None, "run_status": "success"}

View File

@ -753,27 +753,12 @@ def test_runs_create_only_pat_cannot_use_mutating_multitask_strategy(client):
assert session_allowed.status_code == 200
def test_start_run_gates_mutating_strategies_at_the_choke_point():
"""The strategy gate lives inside start_run itself — the single choke point
every run-creation path (all five HTTP entrypoints plus internal
launchers) flows through — so no entry point can bypass it. Mirrored
routes prove the middleware path; this anchor proves the choke point."""
import inspect
from app.gateway.services import start_run
source = inspect.getsource(start_run)
assert "require_cancel_permission_if" in source
assert "multitask_strategy" in source
def test_start_run_gate_denies_create_only_credential_behaviorally():
"""Behavioral pin on the real start_run (the mirror route and source
anchor above prove wiring, but this drives the production choke point
itself): a create-only auth context gets 403 for a mutating strategy,
and the gate never misfires on "reject" — with no cancel permission at
all, the call proceeds past the gate (failing later on missing test
wiring, never with a permission 403)."""
@pytest.mark.parametrize("strategy", ["interrupt", "rollback"])
def test_start_run_gate_denies_create_only_credential_behaviorally(strategy):
"""Behavioral pin on the real start_run choke point: a create-only auth
context gets 403 for every mutating multitask strategy, credentials with
runs:cancel pass the gate, and non-cancelling strategies ("reject",
"enqueue") never misfire with a permission 403."""
from fastapi import HTTPException
from app.gateway.authz import AuthContext
@ -785,22 +770,24 @@ def test_start_run_gate_denies_create_only_credential_behaviorally():
async def _denied():
with pytest.raises(HTTPException) as exc:
await start_run(RunCreateRequest(multitask_strategy="interrupt"), "t1", _request(["runs:create"]))
await start_run(RunCreateRequest(multitask_strategy=strategy), "t1", _request(["runs:create"]))
return exc.value
exc = asyncio.run(_denied())
assert exc.status_code == 403
assert exc.detail == "Permission denied: runs:cancel"
async def _allowed_past_gate():
async def _allowed_past_gate(req_body, perms):
try:
await start_run(RunCreateRequest(), "t1", _request([]))
await start_run(req_body, "t1", _request(perms))
except HTTPException as gate_misfire:
pytest.fail(f"gate misfired on reject: {gate_misfire.status_code} {gate_misfire.detail}")
pytest.fail(f"gate misfired: {gate_misfire.status_code} {gate_misfire.detail}")
except Exception:
pass # expected wiring failure past the gate — the gate let it through
asyncio.run(_allowed_past_gate())
asyncio.run(_allowed_past_gate(RunCreateRequest(multitask_strategy=strategy), ["runs:create", "runs:cancel"]))
for non_cancelling in (RunCreateRequest(), RunCreateRequest(multitask_strategy="reject")):
asyncio.run(_allowed_past_gate(non_cancelling, []))
def test_auth_disabled_mode_ignores_bearer_header(monkeypatch, tmp_path):

View File

@ -17,6 +17,7 @@ from __future__ import annotations
from types import SimpleNamespace
import pytest
from sqlalchemy.engine.url import make_url
from deerflow.persistence.bootstrap import _alembic_safe_url, _escape_url_for_alembic, _get_alembic_config
@ -86,17 +87,62 @@ def test_alembic_config_omits_schema_option_when_unset() -> None:
assert cfg.get_main_option("deerflow_pg_schema") is None
def test_env_module_pins_search_path_from_schema_option() -> None:
"""env.py must read ``deerflow_pg_schema`` and pin the alembic-spawned
engine's search_path. That engine is built from the bare URL and does not
inherit the app engine's asyncpg ``server_settings``, so without this both
``alembic_version`` and migration DDL would land in ``public`` while the
ORM tables land in the custom schema. Source-parity check mirroring
``test_env_module_wires_busy_timeout_for_sqlite``.
"""
@pytest.mark.parametrize("scheme", ["postgresql+asyncpg", "postgresql", "postgres"])
def test_env_module_pins_search_path_from_schema_option(monkeypatch: pytest.MonkeyPatch, scheme: str) -> None:
"""Behavioral verification on migrations/env.py: run_migrations_online reads
``deerflow_pg_schema`` from the Alembic Config and passes the pinned asyncpg
``search_path`` connect_args to ``create_async_engine``."""
import asyncio
import importlib.util
from contextlib import asynccontextmanager
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock
from alembic.config import Config
from deerflow.persistence.postgres_schema import build_asyncpg_connect_args
alembic_cfg = Config()
dsn = f"{scheme}://deerflow:deerflow@localhost:5432/deerflow_test"
alembic_cfg.set_main_option("sqlalchemy.url", dsn)
alembic_cfg.set_main_option("deerflow_pg_schema", "tenant_isolated")
captured: dict[str, object] = {}
class _FakeAsyncEngine:
url = make_url(dsn)
sync_engine = SimpleNamespace(dialect=SimpleNamespace(name="postgresql"))
@asynccontextmanager
async def connect(self):
class _Conn:
async def run_sync(self, _fn):
return None
yield _Conn()
async def dispose(self):
return None
def _fake_create_async_engine(url, **kwargs):
captured["url"] = url
captured["connect_args"] = kwargs.get("connect_args")
return _FakeAsyncEngine()
fake_ctx = MagicMock()
fake_ctx.config = alembic_cfg
fake_ctx.is_offline_mode.return_value = True # avoid running online at import time
monkeypatch.setattr("alembic.context", fake_ctx)
env_path = Path(__file__).resolve().parents[1] / "packages/harness/deerflow/persistence/migrations/env.py"
src = env_path.read_text(encoding="utf-8")
assert 'get_main_option("deerflow_pg_schema")' in src, "env.py must read the deerflow_pg_schema option set by _get_alembic_config"
assert "build_asyncpg_connect_args" in src, "env.py must pin the alembic engine's search_path via build_asyncpg_connect_args"
spec = importlib.util.spec_from_file_location("_test_migrations_env_schema", env_path)
assert spec is not None and spec.loader is not None
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
monkeypatch.setattr(mod, "create_async_engine", _fake_create_async_engine)
asyncio.run(mod.run_migrations_online())
assert captured["url"] == dsn
assert captured["connect_args"] == build_asyncpg_connect_args("tenant_isolated")

View File

@ -1,130 +0,0 @@
"""Regression tests for MemoryRunEventStore's run-keyed event/message index.
``list_events`` and ``list_messages_by_run`` are served from per-run
projections so a single run's reads cost O(events-in-run) instead of
re-scanning O(events-in-thread). These tests pin the indexed implementation to
the exact semantics of a brute-force full-thread scan -- including interleaved
trace events (non-contiguous message seqs), both cursors supplied at once, and
index upkeep after ``delete_by_run`` -- so the optimization can never silently
drift from the reference behavior.
"""
import pytest
from deerflow.runtime.events.store.memory import MemoryRunEventStore
def _ref_messages_by_run(records, thread_id, run_id, *, limit=50, before_seq=None, after_seq=None):
"""Brute-force reference: the pre-index full-thread scan it replaced."""
filtered = [e for e in records if e["thread_id"] == thread_id and e["run_id"] == run_id and e["category"] == "message"]
if before_seq is not None:
filtered = [e for e in filtered if e["seq"] < before_seq]
if after_seq is not None:
filtered = [e for e in filtered if e["seq"] > after_seq]
if after_seq is not None:
return filtered[:limit]
return filtered[-limit:] if len(filtered) > limit else filtered
def _ref_events(records, thread_id, run_id, *, event_types=None, limit=500):
filtered = [e for e in records if e["thread_id"] == thread_id and e["run_id"] == run_id]
if event_types is not None:
filtered = [e for e in filtered if e["event_type"] in event_types]
return filtered[:limit]
async def _seed(store):
"""Two runs interleaved within one thread; messages and traces mixed so
each run's message seqs are non-contiguous (the bisect must handle gaps)."""
plan = [
("run-a", "message"),
("run-a", "trace"),
("run-b", "message"),
("run-a", "message"),
("run-b", "trace"),
("run-b", "message"),
("run-a", "trace"),
("run-a", "message"),
("run-b", "message"),
("run-a", "message"),
("run-b", "message"),
("run-a", "message"),
]
records = []
for i, (run_id, category) in enumerate(plan):
rec = await store.put(thread_id="t1", run_id=run_id, event_type=f"e{i}", category=category, content=str(i))
records.append(rec)
return records
@pytest.mark.anyio
async def test_list_messages_by_run_matches_reference_across_cursors():
store = MemoryRunEventStore()
records = await _seed(store)
seqs = [r["seq"] for r in records]
cursors = [None, 0, *seqs, max(seqs) + 1]
for run_id in ("run-a", "run-b", "run-missing"):
for limit in (1, 2, 3, 50):
for before_seq in cursors:
for after_seq in cursors:
got = await store.list_messages_by_run("t1", run_id, limit=limit, before_seq=before_seq, after_seq=after_seq)
want = _ref_messages_by_run(records, "t1", run_id, limit=limit, before_seq=before_seq, after_seq=after_seq)
assert got == want, (run_id, limit, before_seq, after_seq)
@pytest.mark.anyio
async def test_list_events_matches_reference_with_filters():
store = MemoryRunEventStore()
records = await _seed(store)
all_types = sorted({r["event_type"] for r in records})
for run_id in ("run-a", "run-b", "run-missing"):
assert await store.list_events("t1", run_id) == _ref_events(records, "t1", run_id)
assert await store.list_events("t1", run_id, limit=2) == _ref_events(records, "t1", run_id, limit=2)
for et in all_types:
assert await store.list_events("t1", run_id, event_types=[et]) == _ref_events(records, "t1", run_id, event_types=[et])
@pytest.mark.anyio
async def test_run_keyed_index_partitions_every_event():
"""Every stored event is filed under exactly its (thread, run), each run's
list is seq-ordered, and the union reconstructs the flat event log."""
store = MemoryRunEventStore()
records = await _seed(store)
indexed = [e for run_events in store._events_by_run["t1"].values() for e in run_events]
assert sorted(e["seq"] for e in indexed) == sorted(r["seq"] for r in records)
for run_id, run_events in store._events_by_run["t1"].items():
assert all(e["run_id"] == run_id for e in run_events)
assert [e["seq"] for e in run_events] == sorted(e["seq"] for e in run_events)
for run_id, run_msgs in store._messages_by_run["t1"].items():
assert all(e["run_id"] == run_id and e["category"] == "message" for e in run_msgs)
@pytest.mark.anyio
async def test_run_index_stays_in_lockstep_after_delete_by_run():
store = MemoryRunEventStore()
await _seed(store)
removed = await store.delete_by_run("t1", "run-a")
assert removed == 7 # run-a: 5 messages + 2 traces
# The deleted run vanishes from both per-run reads.
assert await store.list_events("t1", "run-a") == []
assert await store.list_messages_by_run("t1", "run-a") == []
assert "run-a" not in store._events_by_run.get("t1", {})
assert "run-a" not in store._messages_by_run.get("t1", {})
# The surviving run is untouched, and the thread-wide projection agrees.
msgs_b = await store.list_messages_by_run("t1", "run-b")
assert len(msgs_b) == 4
assert all(m["run_id"] == "run-b" for m in msgs_b)
assert all(m["run_id"] == "run-b" for m in await store.list_messages("t1"))
@pytest.mark.anyio
async def test_delete_by_thread_clears_run_indexes():
store = MemoryRunEventStore()
await _seed(store)
await store.delete_by_thread("t1")
assert "t1" not in store._events_by_run
assert "t1" not in store._messages_by_run
assert await store.list_events("t1", "run-a") == []
assert await store.list_messages_by_run("t1", "run-b") == []

View File

@ -1,24 +0,0 @@
"""Operator controls bound speculative sandbox work without enabling it by default."""
import pytest
from pydantic import ValidationError
from deerflow.config.sandbox_config import SandboxConfig
def test_prewarm_defaults_are_opt_in_and_bounded():
config = SandboxConfig(use="deerflow.community.aio_sandbox:AioSandboxProvider")
assert config.prewarm is False
assert config.prewarm_max_concurrent == 1
@pytest.mark.parametrize("value", [1, 4, "3"])
def test_prewarm_limit_accepts_positive_integers_and_environment_strings(value):
config = SandboxConfig(use="example:Provider", prewarm_max_concurrent=value)
assert config.prewarm_max_concurrent == int(value)
@pytest.mark.parametrize("value", [True, False, 0, -1, 1.5, "bad", None])
def test_prewarm_limit_rejects_invalid_values(value):
with pytest.raises(ValidationError):
SandboxConfig(use="example:Provider", prewarm_max_concurrent=value)

View File

@ -1,152 +0,0 @@
from datetime import UTC, datetime, timedelta
import pytest
from deerflow.config.database_config import DatabaseConfig
from deerflow.persistence.engine import close_engine, get_session_factory, init_engine_from_config
from deerflow.persistence.scheduled_tasks import ScheduledTaskRepository
@pytest.mark.asyncio
async def test_claim_due_tasks_claims_only_due_rows(tmp_path):
await init_engine_from_config(DatabaseConfig(backend="sqlite", sqlite_dir=str(tmp_path)))
sf = get_session_factory()
assert sf is not None
repo = ScheduledTaskRepository(sf)
due = datetime.now(UTC) - timedelta(minutes=1)
future = datetime.now(UTC) + timedelta(hours=1)
await repo.create(
task_id="due-1",
user_id="user-1",
thread_id="thread-1",
context_mode="reuse_thread",
assistant_id="lead_agent",
title="Due",
prompt="Prompt",
schedule_type="cron",
schedule_spec={"cron": "0 9 * * *"},
timezone="UTC",
next_run_at=due,
)
await repo.create(
task_id="future-1",
user_id="user-1",
thread_id="thread-1",
context_mode="reuse_thread",
assistant_id="lead_agent",
title="Future",
prompt="Prompt",
schedule_type="cron",
schedule_spec={"cron": "0 9 * * *"},
timezone="UTC",
next_run_at=future,
)
claimed = await repo.claim_due_tasks(
now=datetime.now(UTC),
lease_owner="worker-1",
lease_seconds=120,
limit=10,
)
assert [task["id"] for task in claimed] == ["due-1"]
await close_engine()
@pytest.mark.asyncio
async def test_claim_reclaims_task_stuck_in_running_with_expired_lease(tmp_path):
"""A task whose claiming process died mid-dispatch must stay reclaimable.
Regression for the lease dead-end bug: claim flips status to ``running``,
and the old claim query only selected ``status == 'enabled'``, so a crash
between claim and dispatch left the task permanently un-triggerable.
"""
await init_engine_from_config(DatabaseConfig(backend="sqlite", sqlite_dir=str(tmp_path)))
sf = get_session_factory()
assert sf is not None
repo = ScheduledTaskRepository(sf)
now = datetime.now(UTC)
due = now - timedelta(minutes=5)
await repo.create(
task_id="stuck-1",
user_id="user-1",
thread_id="thread-1",
context_mode="reuse_thread",
assistant_id="lead_agent",
title="Stuck",
prompt="Prompt",
schedule_type="cron",
schedule_spec={"cron": "0 9 * * *"},
timezone="UTC",
next_run_at=due,
)
first_claim = await repo.claim_due_tasks(
now=now,
lease_owner="dead-worker",
lease_seconds=60,
limit=10,
)
assert first_claim[0]["id"] == "stuck-1"
assert first_claim[0]["status"] == "running"
# Simulate the claiming process dying: lease expires, status stays "running".
expired_now = now + timedelta(seconds=120)
reclaimed = await repo.claim_due_tasks(
now=expired_now,
lease_owner="new-worker",
lease_seconds=60,
limit=10,
)
assert [task["id"] for task in reclaimed] == ["stuck-1"]
assert reclaimed[0]["lease_owner"] == "new-worker"
await close_engine()
@pytest.mark.asyncio
async def test_claim_skips_task_with_active_lease(tmp_path):
"""A task whose lease has not expired must not be reclaimed."""
await init_engine_from_config(DatabaseConfig(backend="sqlite", sqlite_dir=str(tmp_path)))
sf = get_session_factory()
assert sf is not None
repo = ScheduledTaskRepository(sf)
now = datetime.now(UTC)
due = now - timedelta(minutes=5)
await repo.create(
task_id="active-1",
user_id="user-1",
thread_id="thread-1",
context_mode="reuse_thread",
assistant_id="lead_agent",
title="Active",
prompt="Prompt",
schedule_type="cron",
schedule_spec={"cron": "0 9 * * *"},
timezone="UTC",
next_run_at=due,
)
await repo.claim_due_tasks(
now=now,
lease_owner="worker-1",
lease_seconds=300,
limit=10,
)
# Lease still valid — second claim within the same process must not re-grab it.
reclaimed = await repo.claim_due_tasks(
now=now + timedelta(seconds=10),
lease_owner="worker-2",
lease_seconds=300,
limit=10,
)
assert reclaimed == []
await close_engine()

View File

@ -1,7 +0,0 @@
from app.gateway.app import create_app
def test_gateway_app_includes_scheduled_task_router():
app = create_app()
paths = {route.path for route in app.routes}
assert "/api/scheduled-tasks" in paths

View File

@ -1,38 +0,0 @@
from fastapi import FastAPI
from fastapi.testclient import TestClient
from app.gateway.routers import scheduled_tasks
def test_router_registers_list_endpoint():
app = FastAPI()
app.include_router(scheduled_tasks.router)
client = TestClient(app)
response = client.get("/api/scheduled-tasks")
assert response.status_code != 404
def test_router_registers_trigger_route():
app = FastAPI()
app.include_router(scheduled_tasks.router)
client = TestClient(app)
response = client.post("/api/scheduled-tasks/task-1/trigger")
assert response.status_code != 404
def test_router_registers_create_route():
app = FastAPI()
app.include_router(scheduled_tasks.router)
client = TestClient(app)
response = client.post(
"/api/scheduled-tasks",
json={
"thread_id": "thread-1",
"title": "Daily summary",
"prompt": "Summarize thread",
"schedule_type": "cron",
"schedule_spec": {"cron": "0 9 * * *"},
"timezone": "UTC",
},
)
assert response.status_code != 404

View File

@ -1,28 +0,0 @@
"""Serialized scheduler evidence must not grant run authority."""
from app.gateway import services
def test_untrusted_scheduler_metadata_is_removed_without_losing_user_data():
supplied = {
"scheduled_task_id": "foreign",
"scheduled_task_run_id": "foreign-occurrence",
"scheduled_goal_objective": "Read private data",
"scheduled_previous_thread_id": "foreign-thread",
"scheduled_tool_created": True,
"scheduled_context_mode": "fresh_thread_per_run",
"user_label": "keep",
}
result = services._admit_scheduler_metadata(supplied, trusted=False)
assert result == {"user_label": "keep"}
assert supplied["scheduled_tool_created"] is True
def test_internal_launch_retains_only_its_data_snapshot():
supplied = {"scheduled_task_id": "task", "scheduled_task_run_id": "occurrence", "scheduled_goal_objective": "Report exists", "scheduled_trigger": "manual"}
assert services._admit_scheduler_metadata(supplied, trusted=True) == supplied
def test_scheduling_metadata_never_recovers_a_callable_capability():
result = services._admit_scheduler_metadata({"__scheduler_capability": {"mode": "scheduled"}, "user_label": "keep"}, trusted=False)
assert "__scheduler_capability" not in result

View File

@ -1,151 +0,0 @@
"""HTTP quota conflicts use real SQLite task admission, with stubbed auth."""
from datetime import UTC, datetime
from types import SimpleNamespace
from uuid import UUID
import httpx
import pytest
import pytest_asyncio
from _router_auth_helpers import make_authed_test_app
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.gateway.auth.models import User
from app.gateway.auth_disabled import AUTH_SOURCE_SESSION
from app.gateway.routers import scheduled_tasks
from deerflow.persistence.base import Base
from deerflow.persistence.scheduled_task_runs.model import ScheduledTaskRunRow
from deerflow.persistence.scheduled_tasks import ScheduledTaskRepository
from deerflow.persistence.scheduled_tasks.model import ScheduledTaskRow
@pytest_asyncio.fixture
async def quota_routes(tmp_path, monkeypatch):
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'quota-routes.db'}")
try:
async with engine.begin() as connection:
await connection.run_sync(lambda sync: Base.metadata.create_all(sync, tables=[ScheduledTaskRow.__table__, ScheduledTaskRunRow.__table__]))
repo = ScheduledTaskRepository(async_sessionmaker(engine, expire_on_commit=False))
user = User(id=UUID("12345678-1234-1234-1234-123456789abc"), email="quota-test@example.com", password_hash="unused", system_role="user")
app = make_authed_test_app(user_factory=lambda: user, bind_current_user=True)
@app.middleware("http")
async def mark_session_source(request, call_next):
request.state.auth_source = AUTH_SOURCE_SESSION
return await call_next(request)
app.state.scheduled_task_repo = repo
app.include_router(scheduled_tasks.router)
monkeypatch.setattr(scheduled_tasks, "get_config", lambda: SimpleNamespace(scheduler=SimpleNamespace(min_once_delay_seconds=60)))
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app, raise_app_exceptions=False), base_url="http://quota.test") as client:
yield client, repo, str(user.id)
finally:
await engine.dispose()
async def create_task(repo, owner, task_id, *, tool_created=True):
return await repo.create(
task_id=task_id,
user_id=owner,
thread_id=None,
context_mode="fresh_thread_per_run",
assistant_id=None,
title="Report",
prompt="Prepare report",
schedule_type="interval",
schedule_spec={"every_seconds": 3600},
timezone="UTC",
next_run_at=datetime.now(UTC),
origin_thread_id="origin" if tool_created else None,
)
async def reactivate(client, task_id, action):
if action == "resume":
return await client.post(f"/api/scheduled-tasks/{task_id}/resume")
return await client.patch(f"/api/scheduled-tasks/{task_id}", json={"schedule_spec": {"every_seconds": 7200}})
@pytest.mark.asyncio
@pytest.mark.parametrize("action", ["resume", "patch"])
async def test_terminal_tool_task_quota_is_http_409_and_keeps_task_unchanged(quota_routes, action):
client, repo, owner = quota_routes
await create_task(repo, owner, "old-terminal")
await repo.update("old-terminal", user_id=owner, updates={"status": "completed"})
for index in range(20):
await create_task(repo, owner, f"active-{index}")
before = await repo.get("old-terminal", user_id=owner)
response = await reactivate(client, "old-terminal", action)
assert response.status_code == 409
assert response.json()["detail"]["code"] == "task_quota_exceeded"
assert response.json()["detail"]["params"] == {"limit": 20}
assert "20 live conversation-created" in response.json()["detail"]["message"]
assert await repo.get("old-terminal", user_id=owner) == before
@pytest.mark.asyncio
async def test_paused_tool_task_resumes_successfully_at_full_quota(quota_routes):
client, repo, owner = quota_routes
for index in range(20):
await create_task(repo, owner, f"active-{index}")
await repo.update("active-0", user_id=owner, updates={"status": "paused"})
response = await reactivate(client, "active-0", "resume")
assert response.status_code == 200
assert response.json()["status"] == "enabled"
@pytest.mark.asyncio
@pytest.mark.parametrize("action", ["resume", "patch"])
async def test_legacy_task_reactivation_ignores_conversation_quota(quota_routes, action):
client, repo, owner = quota_routes
for index in range(20):
await create_task(repo, owner, f"active-{index}")
await create_task(repo, owner, "legacy", tool_created=False)
await repo.update("legacy", user_id=owner, updates={"status": "completed"})
response = await reactivate(client, "legacy", action)
assert response.status_code == 200
assert response.json()["status"] == "enabled"
@pytest.mark.asyncio
async def test_goal_schedule_patch_to_reused_thread_is_422_and_preserves_row(quota_routes):
client, repo, owner = quota_routes
await create_task(repo, owner, "goal-task")
await repo.update("goal-task", user_id=owner, updates={"goal_objective": "deliver report"}, require_mutable=True)
before = await repo.get("goal-task", user_id=owner)
response = await client.patch("/api/scheduled-tasks/goal-task", json={"context_mode": "reuse_thread", "thread_id": "reused-thread"})
assert response.status_code == 422
assert response.json()["detail"]["code"] == "goal_requires_fresh_thread"
assert "fresh_thread_per_run" in response.json()["detail"]["message"]
assert await repo.get("goal-task", user_id=owner) == before
@pytest.mark.asyncio
async def test_schedule_without_goal_can_patch_to_reused_thread(quota_routes):
client, repo, owner = quota_routes
await create_task(repo, owner, "ordinary-task")
response = await client.patch("/api/scheduled-tasks/ordinary-task", json={"context_mode": "reuse_thread", "thread_id": "reused-thread"})
assert response.status_code == 200
assert response.json()["context_mode"] == "reuse_thread"
@pytest.mark.asyncio
@pytest.mark.parametrize("live_count", [19, 20])
async def test_pause_route_never_reactivates_a_finished_task(quota_routes, live_count):
# A finished task has no schedule left to pause; Resume reactivates it.
# Pause therefore never moves a terminal task into a live (quota) state.
client, repo, owner = quota_routes
await create_task(repo, owner, "terminal")
await repo.update("terminal", user_id=owner, updates={"status": "completed"})
for index in range(live_count):
await create_task(repo, owner, f"live-{index}")
before = await repo.get("terminal", user_id=owner)
response = await client.post("/api/scheduled-tasks/terminal/pause")
assert response.status_code == 409
assert response.json()["detail"]["code"] == "task_finished"
assert await repo.get("terminal", user_id=owner) == before
await create_task(repo, owner, "legacy", tool_created=False)
await repo.update("legacy", user_id=owner, updates={"status": "completed"})
legacy = await client.post("/api/scheduled-tasks/legacy/pause")
assert legacy.status_code == 409
assert legacy.json()["detail"]["code"] == "task_finished"

View File

@ -1,39 +0,0 @@
"""Tests for SchedulerConfig schema."""
import pytest
from pydantic import ValidationError
from deerflow.config.scheduler_config import SchedulerConfig
def test_scheduler_config_defaults():
config = SchedulerConfig()
assert config.enabled is False
assert config.recursion_limit == 1000
assert config.max_concurrent_runs == 3
assert config.max_concurrent_runs_per_user == 2
def test_scheduler_config_accepts_positive_recursion_limit():
assert SchedulerConfig(recursion_limit=1).recursion_limit == 1
assert SchedulerConfig(recursion_limit=1000).recursion_limit == 1000
def test_scheduler_config_rejects_non_positive_recursion_limit():
with pytest.raises(ValidationError):
SchedulerConfig(recursion_limit=0)
with pytest.raises(ValidationError):
SchedulerConfig(recursion_limit=-5)
def test_scheduler_config_per_user_cap_bounds():
assert SchedulerConfig(max_concurrent_runs_per_user=0).max_concurrent_runs_per_user == 0
assert SchedulerConfig(max_concurrent_runs_per_user=32).max_concurrent_runs_per_user == 32
with pytest.raises(ValidationError):
SchedulerConfig(max_concurrent_runs_per_user=-1)
with pytest.raises(ValidationError):
SchedulerConfig(max_concurrent_runs_per_user=33)

View File

@ -1,9 +1,9 @@
from __future__ import annotations
import asyncio
import inspect
import logging
from unittest.mock import AsyncMock, MagicMock
from contextlib import ExitStack, asynccontextmanager
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi import FastAPI
@ -32,6 +32,53 @@ async def test_scheduled_task_service_shutdown_is_bounded(monkeypatch: pytest.Mo
assert "Scheduled task service shutdown exceeded" in caplog.text
def test_lifespan_uses_bounded_scheduled_task_shutdown() -> None:
source = inspect.getsource(gateway_app._gateway_lifespan)
assert "await _shutdown_scheduled_task_service(app)" in source
@pytest.mark.asyncio
async def test_lifespan_bounds_hung_scheduled_task_shutdown(monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture) -> None:
async def hang() -> None:
await asyncio.sleep(3600)
service = MagicMock()
service.stop = AsyncMock(side_effect=hang)
@asynccontextmanager
async def _runtime_with_hung_scheduler(app: FastAPI, _startup_config: object):
app.state.scheduled_task_service = service
yield
app = FastAPI()
startup_config = MagicMock()
startup_config.log_level = "INFO"
startup_config.memory.enabled = False
startup_config.memory.shutdown_flush_timeout_seconds = 5.0
startup_config.scheduler.enabled = False
monkeypatch.setattr(gateway_app, "_SHUTDOWN_HOOK_TIMEOUT_SECONDS", 0.01)
channel_service = MagicMock()
channel_service.get_status.return_value = {}
patches = [
patch("app.gateway.app.get_app_config", return_value=startup_config),
patch("app.gateway.app.get_gateway_config", return_value=MagicMock(host="x", port=0)),
patch("app.gateway.app.langgraph_runtime", _runtime_with_hung_scheduler),
patch("app.gateway.app.auth.close_oidc_service", AsyncMock()),
patch("app.channels.service.start_channel_service", AsyncMock(return_value=channel_service)),
patch("app.channels.service.stop_channel_service", AsyncMock()),
patch("deerflow.skills.projection.ensure_public_skill_projection"),
patch("deerflow.agents.memory.get_memory_manager", return_value=MagicMock()),
patch("deerflow.mcp.session_pool.get_session_pool", return_value=MagicMock(close_all=AsyncMock())),
patch(
"deerflow.community.browser_automation.get_browser_session_manager",
return_value=MagicMock(close_all_sessions=AsyncMock(return_value=0)),
),
]
with ExitStack() as stack:
for patcher in patches:
stack.enter_context(patcher)
with caplog.at_level(logging.WARNING, logger="app.gateway.app"):
async with gateway_app.lifespan(app):
started = asyncio.get_running_loop().time()
elapsed = asyncio.get_running_loop().time() - started
assert elapsed < 0.5
service.stop.assert_awaited_once()
assert "Scheduled task service shutdown exceeded" in caplog.text

View File

@ -1,20 +0,0 @@
import threading
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
@pytest.mark.asyncio
async def test_gateway_catalog_never_takes_managed_guard_on_event_loop(monkeypatch):
from app.gateway.routers import skills
loop_thread = threading.get_ident()
def load_skills(**kwargs):
assert threading.get_ident() != loop_thread
return []
monkeypatch.setattr(skills, "_get_user_skill_storage", lambda _: SimpleNamespace(load_skills=load_skills))
monkeypatch.setattr(skills, "_filter_visible_skills", AsyncMock(return_value=[]))
assert (await skills.list_skills(SimpleNamespace(), SimpleNamespace())).skills == []

View File

@ -1,71 +0,0 @@
"""Tests for SSE frame formatting utilities."""
import json
def _format_sse(event: str, data, *, event_id: str | None = None) -> str:
from app.gateway.services import format_sse
return format_sse(event, data, event_id=event_id)
def test_sse_end_event_data_null():
"""End event should have data: null."""
frame = _format_sse("end", None)
assert "data: null" in frame
def test_sse_metadata_event():
"""Metadata event should include run_id and attempt."""
frame = _format_sse("metadata", {"run_id": "abc", "attempt": 1}, event_id="123-0")
assert "event: metadata" in frame
assert "id: 123-0" in frame
def test_sse_error_format():
"""Error event should use message/name format."""
frame = _format_sse("error", {"message": "boom", "name": "ValueError"})
parsed = json.loads(frame.split("data: ")[1].split("\n")[0])
assert parsed["message"] == "boom"
assert parsed["name"] == "ValueError"
def test_sse_frame_rejects_non_finite_floats():
"""A non-finite float must not reach the wire as a bare NaN/Infinity token.
The canonical serializer normalizes non-finite floats before they reach
``format_sse``; this guards the wire boundary directly so a regression in
either layer is caught. A strict ``parse_constant`` mirrors the JS
``useStream`` / ``langgraph-sdk`` SSE decoder, which rejects such tokens.
"""
from deerflow.runtime.serialization import serialize
payload = serialize({"messages": [], "metric": float("inf")}, mode="values")
frame = _format_sse("values", payload)
data_line = frame.split("data: ")[1].split("\n")[0]
def _reject_constant(token):
raise ValueError(f"non-finite JSON token: {token}")
json.loads(data_line, parse_constant=_reject_constant)
def test_sse_messages_frame_rejects_non_finite_floats_in_metadata():
"""A non-finite float inside messages-mode metadata must not reach the
wire as a bare NaN/Infinity token either (#7669)."""
from deerflow.runtime.serialization import serialize
payload = serialize(
(
{"type": "AIMessageChunk", "content": "hi"},
{"langgraph_node": "model", "score": float("inf")},
),
mode="messages",
)
frame = _format_sse("messages", payload)
data_line = frame.split("data: ")[1].split("\n")[0]
def _reject_constant(token):
raise ValueError(f"non-finite JSON token: {token}")
json.loads(data_line, parse_constant=_reject_constant)

View File

@ -11,7 +11,6 @@ consumer.
"""
import asyncio
import inspect
from types import SimpleNamespace
import anyio
@ -293,19 +292,102 @@ def test_creator_stream_close_after_orphan_recovery_end_does_not_cancel(monkeypa
assert asyncio.run(scenario()) == []
def test_join_routes_wire_sse_consumer_as_observers():
"""Both join surfaces must be wired as observers, and the creator's
create-and-stream endpoints must keep the creator policy (default)."""
def test_join_and_create_routes_apply_observer_vs_creator_disconnect_policies(monkeypatch):
"""Behavioral verification across the real route handlers:
- join_run, stream_existing_run (POST without action), and join_existing_run_stream (GET)
behave as observers and never cancel the running run on disconnect;
- stream_run and stateless_stream keep the creator cancel-on-disconnect policy;
- stream_run emits a missing-stream gap frame only when record.idempotency_reused is True.
"""
from app.gateway.routers import runs as runs_router
from app.gateway.routers import thread_runs
from app.gateway.run_models import RunCreateRequest
thread_runs_source = inspect.getsource(thread_runs)
# join_run + the shared existing-run stream implementation
assert thread_runs_source.count("sse_consumer(bridge, record, request, run_mgr, apply_on_disconnect=False)") == 2
# stream_run — creating retry opts into missing-stream gap; first create does not
assert "emit_gap_on_missing_stream=record.idempotency_reused" in thread_runs_source
class _RouteRunManager(_CancelRecorder):
def __init__(self, rec: RunRecord):
super().__init__()
self._rec = rec
runs_source = inspect.getsource(runs_router)
# stateless create-and-stream — creator on_disconnect policy, not the retry gap
assert "sse_consumer(bridge, record, request, run_mgr, apply_on_disconnect=False)" not in runs_source
assert "emit_gap_on_missing_stream" not in runs_source
async def get(self, run_id: str):
return self._rec if run_id == self._rec.run_id else None
class _MissingStreamBridge:
supports_cross_process = False
def subscribe(self, run_id, last_event_id=None):
return None
def _route_req():
req = _StubRequest()
req._deerflow_test_bypass_auth = True
req.state = SimpleNamespace()
return req
async def _noop_scope(*args, **kwargs):
return None
monkeypatch.setattr(thread_runs, "_require_run_visible_to_scope", _noop_scope)
monkeypatch.setattr(thread_runs, "_scope_http_run_idempotency_key", lambda req, tid, key: key)
async def exercise_observer_routes():
for call in (
lambda req: thread_runs.join_run("t1", "run-1", req),
lambda req: thread_runs.stream_existing_run("t1", "run-1", req, action=None, wait=0),
lambda req: thread_runs.join_existing_run_stream("t1", "run-1", req),
):
mgr = _RouteRunManager(_running_record())
monkeypatch.setattr(thread_runs, "get_run_manager", lambda _r, m=mgr: m)
monkeypatch.setattr(thread_runs, "get_stream_bridge", lambda _r: _StubBridge())
resp = await call(_route_req())
await _drive_disconnect(resp.body_iterator)
assert mgr.cancelled == []
asyncio.run(exercise_observer_routes())
async def exercise_creator_routes():
for router_mod, call in (
(thread_runs, lambda req: thread_runs.stream_run("t1", RunCreateRequest(), req, idempotency_key=None)),
(runs_router, lambda req: runs_router.stateless_stream(RunCreateRequest(), req)),
):
rec = _running_record()
mgr = _RouteRunManager(rec)
async def fake_start_run(*args, r=rec, **kwargs):
return r
monkeypatch.setattr(router_mod, "get_run_manager", lambda _r, m=mgr: m)
monkeypatch.setattr(router_mod, "get_stream_bridge", lambda _r: _StubBridge())
monkeypatch.setattr(router_mod, "start_run", fake_start_run)
resp = await call(_route_req())
await _drive_disconnect(resp.body_iterator)
assert mgr.cancelled == ["run-1"]
asyncio.run(exercise_creator_routes())
async def exercise_missing_stream_gap_policy():
async def _stream_missing(_b, _r):
return True
monkeypatch.setattr("app.gateway.services._terminal_record_stream_missing", _stream_missing)
for reused, expected_prefix in ((False, "event: end\n"), (True, "event: gap\n")):
rec = RunRecord(
run_id="run-done",
thread_id="t1",
assistant_id=None,
status=RunStatus.success,
on_disconnect=DisconnectMode.cancel,
idempotency_reused=reused,
)
mgr = _RouteRunManager(rec)
async def fake_start_run(*args, r=rec, **kwargs):
return r
monkeypatch.setattr(thread_runs, "get_run_manager", lambda _r, m=mgr: m)
monkeypatch.setattr(thread_runs, "get_stream_bridge", lambda _r: _MissingStreamBridge())
monkeypatch.setattr(thread_runs, "start_run", fake_start_run)
resp = await thread_runs.stream_run("t1", RunCreateRequest(), _route_req(), idempotency_key="k1")
frames = [frame async for frame in resp.body_iterator]
assert frames[0].startswith(expected_prefix), (reused, frames)
asyncio.run(exercise_missing_stream_gap_policy())

View File

@ -1,10 +1,10 @@
from __future__ import annotations
import asyncio
import inspect
import logging
from contextlib import ExitStack, asynccontextmanager
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi import FastAPI
@ -109,9 +109,61 @@ async def test_gateway_shutdown_timeout_does_not_cancel_batch_cleanup(
assert service._poller is None
def test_lifespan_uses_bounded_subagent_batch_shutdown() -> None:
source = inspect.getsource(gateway_app._gateway_lifespan)
assert "await _shutdown_subagent_batch_service(app)" in source
@pytest.mark.asyncio
async def test_lifespan_bounds_hung_subagent_batch_shutdown(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
) -> None:
async def hang() -> None:
await asyncio.sleep(3600)
service = MagicMock()
service.stop = AsyncMock(side_effect=hang)
@asynccontextmanager
async def _runtime_with_hung_batch_service(app: FastAPI, _startup_config: object):
app.state.subagent_batch_service = service
app.state.subagent_batches_available = True
yield
app = FastAPI()
startup_config = MagicMock()
startup_config.log_level = "INFO"
startup_config.memory.enabled = False
startup_config.memory.shutdown_flush_timeout_seconds = 5.0
startup_config.scheduler.enabled = False
monkeypatch.setattr(gateway_app, "_SHUTDOWN_HOOK_TIMEOUT_SECONDS", 0.01)
channel_service = MagicMock()
channel_service.get_status.return_value = {}
patches = [
patch("app.gateway.app.get_app_config", return_value=startup_config),
patch("app.gateway.app.get_gateway_config", return_value=MagicMock(host="x", port=0)),
patch("app.gateway.app.langgraph_runtime", _runtime_with_hung_batch_service),
patch("app.gateway.app.auth.close_oidc_service", AsyncMock()),
patch("app.channels.service.start_channel_service", AsyncMock(return_value=channel_service)),
patch("app.channels.service.stop_channel_service", AsyncMock()),
patch("deerflow.skills.projection.ensure_public_skill_projection"),
patch("deerflow.agents.memory.get_memory_manager", return_value=MagicMock()),
patch("deerflow.mcp.session_pool.get_session_pool", return_value=MagicMock(close_all=AsyncMock())),
patch(
"deerflow.community.browser_automation.get_browser_session_manager",
return_value=MagicMock(close_all_sessions=AsyncMock(return_value=0)),
),
]
with ExitStack() as stack:
for patcher in patches:
stack.enter_context(patcher)
with caplog.at_level(logging.WARNING, logger="app.gateway.app"):
async with gateway_app.lifespan(app):
started = asyncio.get_running_loop().time()
elapsed = asyncio.get_running_loop().time() - started
assert elapsed < 0.5
assert app.state.subagent_batches_available is False
service.stop.assert_awaited_once()
assert "Subagent batch service shutdown exceeded" in caplog.text
@pytest.mark.asyncio

View File

@ -1,182 +0,0 @@
"""End-to-end: the subagent deferral recipe hides then promotes an MCP tool (#3341).
#3272 wired deferred MCP loading into the lead agent only. #3341 extends it to
subagents. This locks the *subagent build recipe* - the shared helpers the
executor now calls (``assemble_deferred_tools`` + ``get_deferred_tools_prompt_section``)
plus the ``DeferredToolFilterMiddleware`` that ``build_subagent_runtime_middlewares``
attaches - composing into the same hide/promote loop the lead has, under the
subagent's build shape (``system_prompt=None`` + a single ``SystemMessage``).
The hide/promote mechanics themselves are also covered for the lead path by
tests/test_deferred_promotion_integration.py; this asserts the subagent recipe
produces an equivalent loop without binding MCP schemas before promotion.
A second test (``test_subagent_builder_emits_working_deferred_filter``) closes the
remaining seam: it sources the filter from the *real* ``build_subagent_runtime_middlewares``
(the exact call ``executor._create_agent`` makes) rather than hand-constructing it, so a
regression in how the builder wires the setup into the filter - wrong catalog hash,
dropped filter, wrong deferred set - is caught at runtime. (Running the full real stack
is intentionally avoided: the other runtime middlewares need sandbox/thread infra to
execute, which would make the test flaky; their attachment + ordering is locked in
tests/test_tool_error_handling_middleware.py instead.)
"""
import asyncio
from langchain.agents import create_agent
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_core.tools import tool as as_tool
from deerflow.agents.middlewares.deferred_tool_filter_middleware import DeferredToolFilterMiddleware
from deerflow.agents.thread_state import ThreadState
from deerflow.tools.builtins.tool_search import assemble_deferred_tools, get_deferred_tools_prompt_section
from deerflow.tools.mcp_metadata import tag_mcp_tool
@as_tool
def active_tool(x: str) -> str:
"An always-active tool."
return x
@as_tool
def mcp_calc(expression: str) -> str:
"Evaluate arithmetic."
return expression
@as_tool
def mcp_other(x: str) -> str:
"Another deferred MCP tool."
return x
def test_subagent_deferral_recipe_hides_then_promotes():
bound: list[list[str]] = []
class RecordingModel(GenericFakeChatModel):
def bind_tools(self, tools, **kwargs):
bound.append([getattr(t, "name", None) for t in tools])
return self
# The subagent build path (executor._build_initial_state): policy-filtered
# tools -> assemble_deferred_tools appends tool_search, fail-closed.
filtered = [active_tool, tag_mcp_tool(mcp_calc), tag_mcp_tool(mcp_other)]
final_tools, setup = assemble_deferred_tools(filtered, enabled=True)
assert "tool_search" in [t.name for t in final_tools]
assert setup.deferred_names == frozenset({"mcp_calc", "mcp_other"})
# The subagent injects the section into its single SystemMessage.
section = get_deferred_tools_prompt_section(deferred_names=setup.deferred_names)
assert "<available-deferred-tools>" in section
assert "mcp_calc" in section and "mcp_other" in section
turn1 = AIMessage(content="", tool_calls=[{"name": "tool_search", "args": {"query": "select:mcp_calc"}, "id": "c1", "type": "tool_call"}])
turn2 = AIMessage(content="done")
model = RecordingModel(messages=iter([turn1, turn2]))
# The middleware DeferredToolFilterMiddleware is exactly what
# build_subagent_runtime_middlewares attaches for this setup (locked by
# tests/test_tool_error_handling_middleware.py); the subagent build passes
# system_prompt=None with state_schema=ThreadState.
graph = create_agent(
model=model,
tools=final_tools,
middleware=[DeferredToolFilterMiddleware(setup.deferred_names, setup.catalog_hash)],
system_prompt=None,
state_schema=ThreadState,
)
result = asyncio.run(graph.ainvoke({"messages": [SystemMessage(content=section), HumanMessage(content="use the deferred calculator", id="user-1")]}))
assert len(bound) >= 2, f"expected >=2 model binds, got {bound}"
# Turn 1: both deferred MCP tools hidden from the subagent's model binding.
assert "mcp_calc" not in bound[0] and "mcp_other" not in bound[0]
# Turn 2: the searched tool is promoted; the un-searched one stays hidden.
assert "mcp_calc" in bound[1]
assert "mcp_other" not in bound[1]
# Promotion recorded in graph state, scoped by catalog hash.
assert result["promoted"] == {
"catalog_hash": setup.catalog_hash,
"names": ["mcp_calc"],
"turn_id": "user-1",
}
def test_subagent_builder_emits_working_deferred_filter():
"""The real build path the executor calls - ``build_subagent_runtime_middlewares`` -
must emit a ``DeferredToolFilterMiddleware`` that actually hides/promotes through a
graph. The recipe test above hand-builds the filter; this sources it from the real
builder given a real setup, so a regression in the builder's wiring is caught: a
wrong catalog hash silently stops promotion (turn 2 would keep mcp_calc hidden), a
dropped filter stops hiding (turn 1 would bind mcp_calc)."""
from deerflow.agents.middlewares.tool_error_handling_middleware import build_subagent_runtime_middlewares
from deerflow.config.app_config import AppConfig, CircuitBreakerConfig
from deerflow.config.guardrails_config import GuardrailsConfig
from deerflow.config.model_config import ModelConfig
from deerflow.config.sandbox_config import SandboxConfig
bound: list[list[str]] = []
class RecordingModel(GenericFakeChatModel):
def bind_tools(self, tools, **kwargs):
bound.append([getattr(t, "name", None) for t in tools])
return self
filtered = [active_tool, tag_mcp_tool(mcp_calc), tag_mcp_tool(mcp_other)]
final_tools, setup = assemble_deferred_tools(filtered, enabled=True)
section = get_deferred_tools_prompt_section(deferred_names=setup.deferred_names)
app_config = AppConfig(
models=[
ModelConfig(
name="test-model",
display_name="test-model",
description=None,
use="langchain_openai:ChatOpenAI",
model="test-model",
supports_vision=False,
)
],
sandbox=SandboxConfig(use="test"),
guardrails=GuardrailsConfig(enabled=False),
circuit_breaker=CircuitBreakerConfig(failure_threshold=7, recovery_timeout_sec=11),
)
# The exact call executor._create_agent makes. Pull the filter the builder
# produced (not a hand-rolled one) so its wiring - deferred set + catalog hash -
# is what's under test.
middlewares = build_subagent_runtime_middlewares(app_config=app_config, model_name="test-model", deferred_setup=setup)
deferred_filters = [m for m in middlewares if isinstance(m, DeferredToolFilterMiddleware)]
assert len(deferred_filters) == 1, f"builder must emit exactly one deferred filter, got {[type(m).__name__ for m in middlewares]}"
turn1 = AIMessage(content="", tool_calls=[{"name": "tool_search", "args": {"query": "select:mcp_calc"}, "id": "c1", "type": "tool_call"}])
turn2 = AIMessage(content="done")
model = RecordingModel(messages=iter([turn1, turn2]))
# Run only the builder-produced filter (the component under test). The other
# runtime middlewares need sandbox/thread infra to *execute*, so running the
# full stack here would be flaky; their attachment + ordering before Safety is
# locked in tests/test_tool_error_handling_middleware.py.
graph = create_agent(
model=model,
tools=final_tools,
middleware=deferred_filters,
system_prompt=None,
state_schema=ThreadState,
)
result = asyncio.run(graph.ainvoke({"messages": [SystemMessage(content=section), HumanMessage(content="use the deferred calculator", id="user-1")]}))
assert len(bound) >= 2, f"expected >=2 model binds, got {bound}"
# Turn 1: both deferred MCP tools hidden - the builder-produced filter is active.
assert "mcp_calc" not in bound[0] and "mcp_other" not in bound[0]
# Turn 2: the searched tool is promoted - proves the builder wired the catalog
# hash correctly (a wrong hash would leave mcp_calc hidden here).
assert "mcp_calc" in bound[1]
assert "mcp_other" not in bound[1]
assert result["promoted"] == {
"catalog_hash": setup.catalog_hash,
"names": ["mcp_calc"],
"turn_id": "user-1",
}

View File

@ -1,143 +0,0 @@
"""Native subagents enforce configured HITL approval without resume support."""
import importlib
import sys
from types import ModuleType
from unittest.mock import MagicMock
import pytest
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from langchain_core.tools import tool
from deerflow.config.app_config import AppConfig
_MOCKED_MODULE_NAMES = [
"deerflow.agents",
"deerflow.agents.thread_state",
"deerflow.agents.middlewares",
"deerflow.agents.middlewares.thread_data_middleware",
"deerflow.sandbox",
"deerflow.sandbox.middleware",
"deerflow.sandbox.security",
"deerflow.models",
"deerflow.skills.storage",
]
@pytest.fixture
def executor_module():
"""Import the real executor despite the suite's cycle-breaking doubles."""
originals = {name: sys.modules.get(name) for name in _MOCKED_MODULE_NAMES}
original_executor = sys.modules.get("deerflow.subagents.executor")
preloaded_names = (
"deerflow.agents.lead_agent.agent",
"deerflow.agents.thread_state",
"deerflow.agents.middlewares.audit_context",
"deerflow.agents.middlewares.tool_declarations",
"deerflow.agents.middlewares.tool_error_handling_middleware",
"deerflow.models.factory",
"deerflow.models.openai_codex_provider",
"deerflow.agents.assembly_descriptor",
"deerflow.sandbox.sandbox_provider",
"deerflow.sandbox.overwrite",
"deerflow.sandbox.prewarm",
)
original_preloaded = {name: sys.modules.get(name) for name in preloaded_names}
preloaded = {name: importlib.import_module(name) for name in preloaded_names}
sys.modules.pop("deerflow.subagents.executor", None)
subagents_package = sys.modules.get("deerflow.subagents")
if subagents_package is not None and hasattr(subagents_package, "executor"):
delattr(subagents_package, "executor")
for name in _MOCKED_MODULE_NAMES:
sys.modules[name] = MagicMock()
storage = ModuleType("deerflow.skills.storage")
storage.get_or_new_skill_storage = lambda **kwargs: None
sys.modules["deerflow.skills.storage"] = storage
sys.modules.update(preloaded)
module = importlib.import_module("deerflow.subagents.executor")
try:
yield module
finally:
for name, original in originals.items():
if original is None:
sys.modules.pop(name, None)
else:
sys.modules[name] = original
for name, original in original_preloaded.items():
if original is None:
sys.modules.pop(name, None)
else:
sys.modules[name] = original
if original_executor is None:
sys.modules.pop("deerflow.subagents.executor", None)
else:
sys.modules["deerflow.subagents.executor"] = original_executor
class _BindableFakeChatModel(GenericFakeChatModel):
def bind_tools(self, tools, **kwargs):
return self
async def _run_native_subagent(executor_module, monkeypatch, *, approval_enabled: bool):
executions = []
@tool
def bash(command: str) -> str:
"""Record a harmless command instead of executing a shell."""
executions.append(command)
return "executed"
app_config = AppConfig.model_validate({"sandbox": {"use": "deerflow.sandbox.local:LocalSandboxProvider"}})
app_config.hitl_approval.enabled = approval_enabled
app_config.hitl_approval.tools = ["bash"]
app_config.hitl_approval.non_interactive = "deny"
model = _BindableFakeChatModel(
messages=iter(
[
AIMessage(content="", tool_calls=[{"name": "bash", "args": {"command": "record"}, "id": "call-1"}]),
AIMessage(content="done"),
]
)
)
monkeypatch.setattr(executor_module, "create_chat_model", lambda **kwargs: model)
runtime_middlewares = sys.modules["deerflow.agents.middlewares.tool_error_handling_middleware"]
monkeypatch.setattr(runtime_middlewares, "build_subagent_runtime_middlewares", lambda **kwargs: [])
executor = executor_module.SubagentExecutor(
config=executor_module.SubagentConfig(name="worker", description="test", skills=[]),
tools=[bash],
app_config=app_config,
parent_model="test-model",
)
agent = await executor._create_agent([bash])
result = await agent.ainvoke(
{"messages": [HumanMessage(content="run the command")]},
context={"is_subagent": True, "app_config": app_config},
)
return executions, result
@pytest.mark.anyio
async def test_native_subagent_denies_configured_tool_without_resume(executor_module, monkeypatch):
executions, result = await _run_native_subagent(executor_module, monkeypatch, approval_enabled=True)
assert executions == []
assert "__interrupt__" not in result
tool_messages = [message for message in result["messages"] if isinstance(message, ToolMessage)]
assert len(tool_messages) == 1
assert tool_messages[0].name == "bash"
assert tool_messages[0].status == "error"
assert "No parent approval channel" in tool_messages[0].content
assert "tool was not executed" in tool_messages[0].content
@pytest.mark.anyio
async def test_native_subagent_default_keeps_tool_execution_enabled(executor_module, monkeypatch):
executions, result = await _run_native_subagent(executor_module, monkeypatch, approval_enabled=False)
assert executions == ["record"]
assert any(isinstance(message, ToolMessage) and message.content == "executed" for message in result["messages"])

View File

@ -1,292 +0,0 @@
"""Tests for token usage tracking in DeerFlowClient."""
from __future__ import annotations
from unittest.mock import MagicMock, patch
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from deerflow.client import DeerFlowClient
# ---------------------------------------------------------------------------
# _serialize_message — usage_metadata passthrough
# ---------------------------------------------------------------------------
class TestSerializeMessageUsageMetadata:
"""Verify _serialize_message includes usage_metadata when present."""
def test_ai_message_with_usage_metadata(self):
msg = AIMessage(
content="Hello",
id="msg-1",
usage_metadata={"input_tokens": 100, "output_tokens": 50, "total_tokens": 150},
)
result = DeerFlowClient._serialize_message(msg)
assert result["type"] == "ai"
assert result["usage_metadata"] == {
"input_tokens": 100,
"output_tokens": 50,
"total_tokens": 150,
}
def test_ai_message_without_usage_metadata(self):
msg = AIMessage(content="Hello", id="msg-2")
result = DeerFlowClient._serialize_message(msg)
assert result["type"] == "ai"
assert "usage_metadata" not in result
def test_tool_message_never_has_usage_metadata(self):
msg = ToolMessage(content="result", tool_call_id="tc-1", name="search")
result = DeerFlowClient._serialize_message(msg)
assert result["type"] == "tool"
assert "usage_metadata" not in result
def test_human_message_never_has_usage_metadata(self):
msg = HumanMessage(content="Hi")
result = DeerFlowClient._serialize_message(msg)
assert result["type"] == "human"
assert "usage_metadata" not in result
def test_ai_message_with_tool_calls_and_usage(self):
msg = AIMessage(
content="",
id="msg-3",
tool_calls=[{"name": "search", "args": {"q": "test"}, "id": "tc-1"}],
usage_metadata={"input_tokens": 200, "output_tokens": 30, "total_tokens": 230},
)
result = DeerFlowClient._serialize_message(msg)
assert result["type"] == "ai"
assert result["tool_calls"] == [{"name": "search", "args": {"q": "test"}, "id": "tc-1"}]
assert result["usage_metadata"]["input_tokens"] == 200
def test_ai_message_with_zero_usage(self):
"""usage_metadata with zero token counts should be included."""
msg = AIMessage(
content="Hello",
id="msg-4",
usage_metadata={"input_tokens": 0, "output_tokens": 0, "total_tokens": 0},
)
result = DeerFlowClient._serialize_message(msg)
assert result["usage_metadata"] == {
"input_tokens": 0,
"output_tokens": 0,
"total_tokens": 0,
}
# ---------------------------------------------------------------------------
# Cumulative usage tracking (simulated, no real agent)
# ---------------------------------------------------------------------------
class TestCumulativeUsageTracking:
"""Test cumulative usage aggregation logic."""
def test_single_message_usage(self):
"""Single AI message usage should be the total."""
cumulative = {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}
usage = {"input_tokens": 100, "output_tokens": 50, "total_tokens": 150}
cumulative["input_tokens"] += usage.get("input_tokens", 0) or 0
cumulative["output_tokens"] += usage.get("output_tokens", 0) or 0
cumulative["total_tokens"] += usage.get("total_tokens", 0) or 0
assert cumulative == {"input_tokens": 100, "output_tokens": 50, "total_tokens": 150}
def test_multiple_messages_usage(self):
"""Multiple AI messages should accumulate."""
cumulative = {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}
messages_usage = [
{"input_tokens": 100, "output_tokens": 50, "total_tokens": 150},
{"input_tokens": 200, "output_tokens": 30, "total_tokens": 230},
{"input_tokens": 150, "output_tokens": 80, "total_tokens": 230},
]
for usage in messages_usage:
cumulative["input_tokens"] += usage.get("input_tokens", 0) or 0
cumulative["output_tokens"] += usage.get("output_tokens", 0) or 0
cumulative["total_tokens"] += usage.get("total_tokens", 0) or 0
assert cumulative == {"input_tokens": 450, "output_tokens": 160, "total_tokens": 610}
def test_missing_usage_keys_treated_as_zero(self):
"""Missing keys in usage dict should be treated as 0."""
cumulative = {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}
usage = {"input_tokens": 50} # missing output_tokens, total_tokens
cumulative["input_tokens"] += usage.get("input_tokens", 0) or 0
cumulative["output_tokens"] += usage.get("output_tokens", 0) or 0
cumulative["total_tokens"] += usage.get("total_tokens", 0) or 0
assert cumulative == {"input_tokens": 50, "output_tokens": 0, "total_tokens": 0}
def test_empty_usage_metadata_stays_zero(self):
"""No usage metadata should leave cumulative at zero."""
cumulative = {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}
# Simulate: AI message without usage_metadata
usage = None
if usage:
cumulative["input_tokens"] += usage.get("input_tokens", 0) or 0
assert cumulative == {"input_tokens": 0, "output_tokens": 0, "total_tokens": 0}
# ---------------------------------------------------------------------------
# stream() integration — usage_metadata in end event and messages-tuple
# ---------------------------------------------------------------------------
def _make_agent_mock(chunks):
"""Create a mock agent whose .stream() yields the given chunks."""
agent = MagicMock()
agent.stream.return_value = iter(chunks)
return agent
def _mock_app_config():
"""Provide a minimal AppConfig mock."""
model = MagicMock()
model.name = "test-model"
model.model = "test-model"
model.supports_thinking = False
model.supports_reasoning_effort = False
model.model_dump.return_value = {"name": "test-model", "use": "langchain_openai:ChatOpenAI"}
config = MagicMock()
config.models = [model]
config.database.checkpoint_delta.snapshot_frequency = 10
return config
class TestStreamUsageIntegration:
"""Test that stream() emits usage_metadata in messages-tuple and end events."""
def _make_client(self):
with patch("deerflow.client.get_app_config", return_value=_mock_app_config()):
return DeerFlowClient()
def test_stream_emits_usage_in_messages_tuple(self):
"""messages-tuple AI event should include usage_metadata when present."""
client = self._make_client()
ai = AIMessage(
content="Hello!",
id="ai-1",
usage_metadata={"input_tokens": 100, "output_tokens": 50, "total_tokens": 150},
)
chunks = [
{"messages": [HumanMessage(content="hi", id="h-1"), ai]},
]
agent = _make_agent_mock(chunks)
with (
patch.object(client, "_ensure_agent"),
patch.object(client, "_agent", agent),
):
events = list(client.stream("hi", thread_id="t1"))
# Find the AI text messages-tuple event
ai_text_events = [e for e in events if e.type == "messages-tuple" and e.data.get("type") == "ai" and e.data.get("content") == "Hello!"]
assert len(ai_text_events) == 1
event_data = ai_text_events[0].data
assert "usage_metadata" in event_data
assert event_data["usage_metadata"] == {
"input_tokens": 100,
"output_tokens": 50,
"total_tokens": 150,
}
def test_stream_cumulative_usage_in_end_event(self):
"""end event should include cumulative usage across all AI messages."""
client = self._make_client()
ai1 = AIMessage(
content="First",
id="ai-1",
usage_metadata={"input_tokens": 100, "output_tokens": 50, "total_tokens": 150},
)
ai2 = AIMessage(
content="Second",
id="ai-2",
usage_metadata={"input_tokens": 200, "output_tokens": 30, "total_tokens": 230},
)
chunks = [
{"messages": [HumanMessage(content="hi", id="h-1"), ai1]},
{"messages": [HumanMessage(content="hi", id="h-1"), ai1, ai2]},
]
agent = _make_agent_mock(chunks)
with (
patch.object(client, "_ensure_agent"),
patch.object(client, "_agent", agent),
):
events = list(client.stream("hi", thread_id="t1"))
# Find the end event
end_events = [e for e in events if e.type == "end"]
assert len(end_events) == 1
end_data = end_events[0].data
assert "usage" in end_data
assert end_data["usage"] == {
"input_tokens": 300,
"output_tokens": 80,
"total_tokens": 380,
}
def test_stream_no_usage_metadata_no_usage_in_events(self):
"""When AI messages have no usage_metadata, events should not include it."""
client = self._make_client()
ai = AIMessage(content="Hello!", id="ai-1")
chunks = [
{"messages": [HumanMessage(content="hi", id="h-1"), ai]},
]
agent = _make_agent_mock(chunks)
with (
patch.object(client, "_ensure_agent"),
patch.object(client, "_agent", agent),
):
events = list(client.stream("hi", thread_id="t1"))
# messages-tuple AI event should NOT have usage_metadata
ai_text_events = [e for e in events if e.type == "messages-tuple" and e.data.get("type") == "ai" and e.data.get("content") == "Hello!"]
assert len(ai_text_events) == 1
assert "usage_metadata" not in ai_text_events[0].data
# end event should still exist but with zero usage
end_events = [e for e in events if e.type == "end"]
assert len(end_events) == 1
usage = end_events[0].data.get("usage", {})
assert usage.get("input_tokens", 0) == 0
assert usage.get("output_tokens", 0) == 0
assert usage.get("total_tokens", 0) == 0
def test_stream_usage_with_tool_calls(self):
"""Usage should be tracked even when AI message has tool calls."""
client = self._make_client()
ai_tool = AIMessage(
content="",
id="ai-1",
tool_calls=[{"name": "search", "args": {"q": "test"}, "id": "tc-1"}],
usage_metadata={"input_tokens": 150, "output_tokens": 25, "total_tokens": 175},
)
tool_result = ToolMessage(content="result", id="tm-1", tool_call_id="tc-1", name="search")
ai_final = AIMessage(
content="Here is the answer.",
id="ai-2",
usage_metadata={"input_tokens": 200, "output_tokens": 100, "total_tokens": 300},
)
chunks = [
{"messages": [HumanMessage(content="search", id="h-1"), ai_tool]},
{"messages": [HumanMessage(content="search", id="h-1"), ai_tool, tool_result]},
{"messages": [HumanMessage(content="search", id="h-1"), ai_tool, tool_result, ai_final]},
]
agent = _make_agent_mock(chunks)
with (
patch.object(client, "_ensure_agent"),
patch.object(client, "_agent", agent),
):
events = list(client.stream("search", thread_id="t1"))
# Final AI text event should have usage_metadata
ai_text_events = [e for e in events if e.type == "messages-tuple" and e.data.get("type") == "ai" and e.data.get("content") == "Here is the answer."]
assert len(ai_text_events) == 1
assert ai_text_events[0].data["usage_metadata"]["total_tokens"] == 300
# end event should have cumulative usage
end_events = [e for e in events if e.type == "end"]
assert end_events[0].data["usage"]["input_tokens"] == 350
assert end_events[0].data["usage"]["output_tokens"] == 125
assert end_events[0].data["usage"]["total_tokens"] == 475

View File

@ -1,5 +0,0 @@
from deerflow.config.token_usage_config import TokenUsageConfig
def test_token_usage_enabled_by_default():
assert TokenUsageConfig().enabled is True

View File

@ -1,54 +0,0 @@
"""Tests for the head500 + tail500 message truncation in format_conversation_for_update."""
from __future__ import annotations
from langchain_core.messages import AIMessage, HumanMessage
from deerflow.agents.memory.backends.deermem.deermem.core.prompt import format_conversation_for_update
def test_long_message_keeps_head_and_tail_drops_middle() -> None:
# 600 head chars + 400 middle + 600 tail = 1600 (> 1000 -> truncated).
long_content = "H" * 600 + "M" * 400 + "T" * 600
result = format_conversation_for_update([HumanMessage(content=long_content)])
assert "[truncated]" in result
# The first 500 and last 500 characters survive.
assert "H" * 500 in result
assert "T" * 500 in result
# The middle block is dropped.
assert "M" * 400 not in result
def test_message_under_threshold_is_not_truncated() -> None:
result = format_conversation_for_update([HumanMessage(content="a short message")])
assert "[truncated]" not in result
assert "a short message" in result
def test_message_exactly_1000_chars_is_not_truncated() -> None:
# The guard is strictly greater-than 1000, so 1000 chars pass through whole.
result = format_conversation_for_update([HumanMessage(content="x" * 1000)])
assert "[truncated]" not in result
def test_message_1001_chars_is_truncated() -> None:
result = format_conversation_for_update([HumanMessage(content="x" * 1001)])
assert "[truncated]" in result
def test_truncation_then_html_escape_preserves_head_marker() -> None:
# A leading "<b>" must be HTML-escaped after truncation (block-breakout
# defense), and the head is preserved up to the 500-char boundary.
long_content = "<b>" + "y" * 1500
result = format_conversation_for_update([HumanMessage(content=long_content)])
assert "&lt;b&gt;" in result
assert "[truncated]" in result
def test_truncation_applies_to_ai_messages_too() -> None:
long_content = "A" * 700 + "B" * 700
result = format_conversation_for_update([AIMessage(content=long_content)])
assert "[truncated]" in result
assert "A" * 500 in result
assert "B" * 500 in result