sync update from LAN repo (source dhk-1 @ 3b1ec5b4)
This commit is contained in:
1 parent
da6521b6cb
commit
264e2200d6
995 files changed
+148105
-13399
No files matched your search
@@ -6,11 +6,13 @@ from typing import NotRequired
|
||||
|
||||
import pytest
|
||||
from langchain.agents import AgentState, create_agent
|
||||
from langchain.agents.middleware.types import ModelRequest
|
||||
from langchain_core.language_models.fake_chat_models import FakeListChatModel
|
||||
from langchain_core.messages import HumanMessage
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
|
||||
from langgraph.checkpoint.memory import InMemorySaver
|
||||
|
||||
from deerflow.agents.middlewares.context_window_guard_middleware import ContextWindowGuardMiddleware
|
||||
from deerflow.agents.middlewares.recent_turns_middleware import RecentTurnsMiddleware, window_recent_turns
|
||||
from deerflow.agents.middlewares.summarization_middleware import DeerFlowSummarizationMiddleware, _clear_global_summary_cache
|
||||
|
||||
|
||||
@@ -131,6 +133,234 @@ def test_factory_respects_off_switch_and_explicit_enable(monkeypatch):
|
||||
assert config.summarization.enabled is False
|
||||
|
||||
|
||||
def _human(text: str, *, hidden: bool = False, name: str | None = None) -> HumanMessage:
|
||||
return HumanMessage(
|
||||
content=text,
|
||||
name=name,
|
||||
additional_kwargs={"hide_from_ui": True} if hidden else {},
|
||||
)
|
||||
|
||||
|
||||
def test_recent_turn_window_keeps_three_real_turns_context_and_tool_bundle():
|
||||
messages = [
|
||||
SystemMessage(content="system-one"),
|
||||
_human("question-1"),
|
||||
AIMessage(content="answer-1"),
|
||||
HumanMessage(content="legacy reminder", name="todo_reminder"),
|
||||
HumanMessage(content="<memory-context>legacy memory</memory-context>"),
|
||||
_human("hidden-for-question-2", hidden=True, name="business_context"),
|
||||
_human("question-2"),
|
||||
AIMessage(content="", tool_calls=[{"id": "call-2", "name": "lookup", "args": {}}]),
|
||||
ToolMessage(content="tool-result-2", tool_call_id="call-2"),
|
||||
AIMessage(content="answer-2"),
|
||||
_human("question-3"),
|
||||
AIMessage(content="answer-3"),
|
||||
SystemMessage(content="system-two"),
|
||||
_human("question-4"),
|
||||
]
|
||||
|
||||
windowed = window_recent_turns(messages)
|
||||
|
||||
assert windowed is not None
|
||||
assert [message.content for message in windowed if isinstance(message, SystemMessage)] == ["system-one", "system-two"]
|
||||
assert [message.content for message in windowed if isinstance(message, HumanMessage)] == [
|
||||
"legacy reminder",
|
||||
"<memory-context>legacy memory</memory-context>",
|
||||
"hidden-for-question-2",
|
||||
"question-2",
|
||||
"question-3",
|
||||
"question-4",
|
||||
]
|
||||
assert any(isinstance(message, ToolMessage) and message.tool_call_id == "call-2" for message in windowed)
|
||||
tool_index = next(index for index, message in enumerate(windowed) if isinstance(message, ToolMessage))
|
||||
preceding_call = windowed[tool_index - 1]
|
||||
assert isinstance(preceding_call, AIMessage)
|
||||
assert [call["id"] for call in preceding_call.tool_calls] == ["call-2"]
|
||||
assert messages[1].content == "question-1"
|
||||
|
||||
|
||||
def test_recent_turn_window_is_noop_for_three_or_fewer_real_turns():
|
||||
messages = [
|
||||
_human("hidden", hidden=True),
|
||||
_human("question-1"),
|
||||
AIMessage(content="answer-1"),
|
||||
_human("question-2"),
|
||||
AIMessage(content="answer-2"),
|
||||
_human("question-3"),
|
||||
]
|
||||
|
||||
assert window_recent_turns(messages) is None
|
||||
|
||||
|
||||
def test_recent_turn_window_folds_retained_system_messages_into_request_slot():
|
||||
model = FakeListChatModel(responses=["unused"])
|
||||
request = ModelRequest(
|
||||
model=model,
|
||||
system_message=SystemMessage(content="base-system"),
|
||||
messages=[
|
||||
SystemMessage(content="state-system"),
|
||||
_human("question-1"),
|
||||
AIMessage(content="answer-1"),
|
||||
_human("question-2"),
|
||||
AIMessage(content="answer-2"),
|
||||
_human("question-3"),
|
||||
AIMessage(content="answer-3"),
|
||||
_human("question-4"),
|
||||
],
|
||||
)
|
||||
|
||||
windowed = RecentTurnsMiddleware()._window_request(request)
|
||||
|
||||
assert windowed.system_message is not None
|
||||
assert "base-system" in str(windowed.system_message.content)
|
||||
assert "state-system" in str(windowed.system_message.content)
|
||||
assert not any(isinstance(message, SystemMessage) for message in windowed.messages)
|
||||
assert [message.content for message in windowed.messages if isinstance(message, HumanMessage)] == [
|
||||
"question-2",
|
||||
"question-3",
|
||||
"question-4",
|
||||
]
|
||||
|
||||
|
||||
class WindowRecordingModel(FakeListChatModel):
|
||||
inputs: list[list] = []
|
||||
|
||||
def _call(self, messages, stop=None, run_manager=None, **kwargs):
|
||||
self.inputs.append(list(messages))
|
||||
return "ACK"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
def test_recent_turn_window_is_transient_across_checkpointed_turns(asynchronous):
|
||||
model = WindowRecordingModel(responses=["unused"], inputs=[])
|
||||
saver = InMemorySaver()
|
||||
agent = create_agent(model=model, middleware=[RecentTurnsMiddleware()], checkpointer=saver)
|
||||
config = {"configurable": {"thread_id": f"recent-window-{asynchronous}"}}
|
||||
|
||||
state = None
|
||||
for turn in range(1, 6):
|
||||
payload = {"messages": [HumanMessage(content=f"question-{turn}")]}
|
||||
state = asyncio.run(agent.ainvoke(payload, config)) if asynchronous else agent.invoke(payload, config)
|
||||
|
||||
assert state is not None
|
||||
assert len(state["messages"]) == 10
|
||||
assert [message.content for message in state["messages"] if isinstance(message, HumanMessage)] == [
|
||||
"question-1",
|
||||
"question-2",
|
||||
"question-3",
|
||||
"question-4",
|
||||
"question-5",
|
||||
]
|
||||
assert [message.content for message in model.inputs[-1] if isinstance(message, HumanMessage)] == [
|
||||
"question-3",
|
||||
"question-4",
|
||||
"question-5",
|
||||
]
|
||||
|
||||
|
||||
def test_disabling_recent_turn_window_restores_full_checkpoint_history():
|
||||
saver = InMemorySaver()
|
||||
config = {"configurable": {"thread_id": "recent-window-disable"}}
|
||||
windowed_model = WindowRecordingModel(responses=["unused"], inputs=[])
|
||||
windowed_agent = create_agent(
|
||||
model=windowed_model,
|
||||
middleware=[RecentTurnsMiddleware()],
|
||||
checkpointer=saver,
|
||||
)
|
||||
for turn in range(1, 6):
|
||||
windowed_agent.invoke(
|
||||
{"messages": [HumanMessage(content=f"question-{turn}")]},
|
||||
config,
|
||||
)
|
||||
|
||||
full_model = WindowRecordingModel(responses=["unused"], inputs=[])
|
||||
full_agent = create_agent(model=full_model, checkpointer=saver)
|
||||
state = full_agent.invoke({"messages": [HumanMessage(content="question-6")]}, config)
|
||||
|
||||
assert [message.content for message in full_model.inputs[-1] if isinstance(message, HumanMessage)] == [
|
||||
"question-1",
|
||||
"question-2",
|
||||
"question-3",
|
||||
"question-4",
|
||||
"question-5",
|
||||
"question-6",
|
||||
]
|
||||
assert len(state["messages"]) == 12
|
||||
|
||||
|
||||
def test_embedded_client_rebuilds_agent_when_admin_window_setting_changes(monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
|
||||
import deerflow.client as client_module
|
||||
|
||||
enabled = {"value": False}
|
||||
created_agents: list[object] = []
|
||||
client = object.__new__(client_module.DeerFlowClient)
|
||||
client._agent = None
|
||||
client._agent_config_key = None
|
||||
client._agent_name = None
|
||||
client._available_skills = None
|
||||
client._middlewares = []
|
||||
client._checkpointer = object()
|
||||
|
||||
monkeypatch.setattr(
|
||||
client_module,
|
||||
"get_conversation_context_settings",
|
||||
lambda: SimpleNamespace(recent_turn_window_enabled=enabled["value"]),
|
||||
)
|
||||
monkeypatch.setattr(client_module, "create_chat_model", lambda **_kwargs: object())
|
||||
monkeypatch.setattr(client_module, "_build_middlewares", lambda *_args, **_kwargs: [])
|
||||
monkeypatch.setattr(client_module, "apply_prompt_template", lambda **_kwargs: "system")
|
||||
monkeypatch.setattr(client, "_get_tools", lambda **_kwargs: [])
|
||||
|
||||
def fake_create_agent(**_kwargs):
|
||||
agent = object()
|
||||
created_agents.append(agent)
|
||||
return agent
|
||||
|
||||
monkeypatch.setattr(client_module, "create_agent", fake_create_agent)
|
||||
config = {
|
||||
"configurable": {
|
||||
"model_name": None,
|
||||
"thinking_enabled": True,
|
||||
"is_plan_mode": False,
|
||||
"subagent_enabled": False,
|
||||
}
|
||||
}
|
||||
|
||||
client._ensure_agent(config)
|
||||
client._ensure_agent(config)
|
||||
assert len(created_agents) == 1
|
||||
|
||||
enabled["value"] = True
|
||||
client._ensure_agent(config)
|
||||
assert len(created_agents) == 2
|
||||
|
||||
|
||||
def test_admin_recent_turn_strategy_preempts_summarization(monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
|
||||
import deerflow.agents.lead_agent.agent as factory
|
||||
|
||||
monkeypatch.setattr(
|
||||
factory,
|
||||
"get_conversation_context_settings",
|
||||
lambda: SimpleNamespace(recent_turn_window_enabled=True),
|
||||
)
|
||||
|
||||
def unexpected_summary(**_kwargs):
|
||||
raise AssertionError("summary middleware must not be constructed")
|
||||
|
||||
monkeypatch.setattr(factory, "_create_summarization_middleware", unexpected_summary)
|
||||
middleware = factory._create_context_reduction_middleware(
|
||||
app_config=SimpleNamespace(),
|
||||
summarization_requested=True,
|
||||
)
|
||||
|
||||
assert isinstance(middleware, RecentTurnsMiddleware)
|
||||
assert middleware.max_turns == 3
|
||||
|
||||
|
||||
@pytest.mark.parametrize("asynchronous", [False, True])
|
||||
def test_multiturn_tool_calls_stay_paired_and_execute_once(asynchronous):
|
||||
from langchain_core.language_models.chat_models import BaseChatModel
|
||||
|
||||
Reference in new issue
Block a user