sync update from LAN repo (source master-nj @ 067eb808)
This commit is contained in:
1 parent
1244fb1f2f
commit
1d5dd8fc90
182 files changed
+18973
-769
No files matched your search
@@ -0,0 +1,49 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from deerflow.config import memory_config as memory_config_module
|
||||
from deerflow.config.memory_config import (
|
||||
MemoryConfig,
|
||||
delete_recall_budget_cache,
|
||||
get_effective_memory_config,
|
||||
get_memory_config,
|
||||
set_memory_config,
|
||||
set_recall_budget_cache,
|
||||
)
|
||||
|
||||
|
||||
def test_runtime_disable_cannot_be_reactivated_by_config_or_user_override(monkeypatch):
|
||||
user_id = "memory-disabled-test-user"
|
||||
original = get_memory_config()
|
||||
try:
|
||||
monkeypatch.setattr(
|
||||
memory_config_module,
|
||||
"get_memory_runtime_settings",
|
||||
lambda: SimpleNamespace(enabled=False),
|
||||
)
|
||||
set_memory_config(MemoryConfig(enabled=True))
|
||||
set_recall_budget_cache(user_id, "high")
|
||||
|
||||
effective = get_effective_memory_config(user_id)
|
||||
|
||||
assert effective.enabled is False
|
||||
finally:
|
||||
delete_recall_budget_cache(user_id)
|
||||
set_memory_config(original)
|
||||
|
||||
|
||||
def test_admin_runtime_enable_activates_memory_even_when_yaml_default_is_off(monkeypatch):
|
||||
original = get_memory_config()
|
||||
try:
|
||||
monkeypatch.setattr(
|
||||
memory_config_module,
|
||||
"get_memory_runtime_settings",
|
||||
lambda: SimpleNamespace(enabled=True),
|
||||
)
|
||||
set_memory_config(MemoryConfig(enabled=False, version="v1"))
|
||||
|
||||
effective = get_effective_memory_config("memory-enabled-test-user")
|
||||
|
||||
assert effective.enabled is True
|
||||
assert effective.version == "v1"
|
||||
finally:
|
||||
set_memory_config(original)
|
||||
@@ -2,7 +2,7 @@ import pytest
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
||||
|
||||
from deerflow.agents.middlewares.writing_setup_round_cap_middleware import WritingSetupRoundCapMiddleware
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages, normalize_langchain_messages
|
||||
from deerflow.models.message_ordering import normalize_chat_payload_messages, normalize_langchain_messages, normalize_model_request_messages
|
||||
from deerflow.models.patched_openai import PatchedChatOpenAI
|
||||
|
||||
|
||||
@@ -49,6 +49,37 @@ def test_normalize_langchain_messages_moves_late_systems_before_history():
|
||||
assert normalized[1].content == "user 0"
|
||||
|
||||
|
||||
def test_normalize_chat_payload_messages_merges_adjacent_leading_systems():
|
||||
payload = {
|
||||
"messages": [
|
||||
{"role": "system", "content": "agent prompt"},
|
||||
{"role": "system", "content": "compaction summary"},
|
||||
{"role": "user", "content": "continue"},
|
||||
]
|
||||
}
|
||||
|
||||
normalize_chat_payload_messages(payload)
|
||||
|
||||
assert [message["role"] for message in payload["messages"]] == ["system", "user"]
|
||||
assert payload["messages"][0]["content"] == "agent prompt\n\n---\n\ncompaction summary"
|
||||
|
||||
|
||||
def test_normalize_model_request_messages_folds_compaction_summary_into_system_slot():
|
||||
system_message, messages = normalize_model_request_messages(
|
||||
SystemMessage(content="agent prompt"),
|
||||
[
|
||||
SystemMessage(content="compaction summary"),
|
||||
HumanMessage(content="continue"),
|
||||
],
|
||||
)
|
||||
|
||||
assert system_message is not None
|
||||
assert system_message.content == "agent prompt\n\n---\n\ncompaction summary"
|
||||
assert len(messages) == 1
|
||||
assert isinstance(messages[0], HumanMessage)
|
||||
assert all(not isinstance(message, SystemMessage) for message in messages)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("turns", [12, 25, 50])
|
||||
def test_patched_openai_payload_has_only_leading_system_after_many_turns(turns):
|
||||
model = PatchedChatOpenAI(
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import logging
|
||||
from uuid import uuid4
|
||||
|
||||
from deerflow.models.timing_callback import ModelTimingCallback
|
||||
|
||||
|
||||
def test_model_timing_callback_logs_connection_and_completion(caplog):
|
||||
callback = ModelTimingCallback(model_name="qwen", base_url="http://model/v1")
|
||||
run_id = uuid4()
|
||||
|
||||
with caplog.at_level(logging.INFO, logger="deerflow.models.timing_callback"):
|
||||
callback.on_chat_model_start({}, [[object()]], run_id=run_id)
|
||||
callback.on_llm_new_token("", run_id=run_id, chunk=object())
|
||||
callback.on_llm_end(object(), run_id=run_id)
|
||||
|
||||
messages = [record.getMessage() for record in caplog.records]
|
||||
assert any("LLM_CONNECT_START" in message for message in messages)
|
||||
assert any("LLM_CONNECT_OK" in message and "first_chunk_ms=" in message for message in messages)
|
||||
assert any("LLM_REQUEST_DONE" in message and "total_ms=" in message for message in messages)
|
||||
|
||||
|
||||
def test_model_timing_callback_logs_failure(caplog):
|
||||
callback = ModelTimingCallback(model_name="qwen", base_url="http://model/v1")
|
||||
run_id = uuid4()
|
||||
|
||||
with caplog.at_level(logging.INFO, logger="deerflow.models.timing_callback"):
|
||||
callback.on_chat_model_start({}, [[object()]], run_id=run_id)
|
||||
callback.on_llm_error(TimeoutError("upstream timeout"), run_id=run_id)
|
||||
|
||||
message = caplog.records[-1].getMessage()
|
||||
assert "LLM_REQUEST_FAILED" in message
|
||||
assert "TimeoutError" in message
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
from langchain_core.messages import AIMessage, AIMessageChunk
|
||||
|
||||
from deerflow.models.patched_openai import (
|
||||
_append_reasoning_content,
|
||||
_extract_reasoning_content,
|
||||
_restore_reasoning_content,
|
||||
)
|
||||
|
||||
|
||||
def test_extracts_legacy_reasoning_content():
|
||||
assert _extract_reasoning_content({"reasoning_content": "legacy"}) == "legacy"
|
||||
|
||||
|
||||
def test_extracts_vllm_reasoning():
|
||||
assert _extract_reasoning_content({"reasoning": "new spelling"}) == "new spelling"
|
||||
|
||||
|
||||
def test_normalizes_vllm_reasoning_for_existing_clients():
|
||||
message = AIMessageChunk(content="")
|
||||
|
||||
updated = _append_reasoning_content(message, "thinking", reasoning="thinking")
|
||||
|
||||
assert updated.additional_kwargs["reasoning_content"] == "thinking"
|
||||
assert updated.additional_kwargs["reasoning"] == "thinking"
|
||||
|
||||
|
||||
def test_restores_original_vllm_reasoning_spelling_for_follow_up_turn():
|
||||
original = AIMessage(
|
||||
content="answer",
|
||||
additional_kwargs={
|
||||
"reasoning": "thinking",
|
||||
"reasoning_content": "thinking",
|
||||
},
|
||||
)
|
||||
payload = {"role": "assistant", "content": "answer"}
|
||||
|
||||
_restore_reasoning_content(payload, original)
|
||||
|
||||
assert payload["reasoning"] == "thinking"
|
||||
assert "reasoning_content" not in payload
|
||||
Reference in new issue
Block a user