mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-22 16:06:50 +00:00
refactor(config): eliminate global mutable state, wire DeerFlowContext into runtime
- Freeze all config models (AppConfig + 15 sub-configs) with frozen=True - Purify from_file() — remove 9 load_*_from_dict() side-effect calls - Replace mtime/reload/push/pop machinery with single ContextVar + init_app_config() - Delete 10 sub-module globals and their getters/setters/loaders - Migrate 50+ consumers from get_*_config() to get_app_config().xxx - Expand DeerFlowContext: app_config + thread_id + agent_name (frozen dataclass) - Wire into Gateway runtime (worker.py) and DeerFlowClient via context= parameter - Remove sandbox_id from runtime.context — flows through ThreadState.sandbox only - Middleware/tools access runtime.context directly via Runtime[DeerFlowContext] generic - resolve_context() retained at server entry points for LangGraph Server fallback
This commit is contained in:
@@ -6,17 +6,20 @@ import pytest
|
||||
import yaml
|
||||
from pydantic import ValidationError
|
||||
|
||||
from deerflow.config.acp_config import ACPAgentConfig, get_acp_agents, load_acp_config_from_dict
|
||||
from deerflow.config.acp_config import ACPAgentConfig
|
||||
from deerflow.config.app_config import AppConfig
|
||||
from deerflow.config.sandbox_config import SandboxConfig
|
||||
|
||||
|
||||
def setup_function():
|
||||
"""Reset ACP config before each test."""
|
||||
load_acp_config_from_dict({})
|
||||
def _make_config(acp_agents: dict | None = None) -> AppConfig:
|
||||
return AppConfig(
|
||||
sandbox=SandboxConfig(use="test"),
|
||||
acp_agents={name: ACPAgentConfig(**cfg) for name, cfg in (acp_agents or {}).items()},
|
||||
)
|
||||
|
||||
|
||||
def test_load_acp_config_sets_agents():
|
||||
load_acp_config_from_dict(
|
||||
def test_acp_agents_via_app_config():
|
||||
cfg = _make_config(
|
||||
{
|
||||
"claude_code": {
|
||||
"command": "claude-code-acp",
|
||||
@@ -26,39 +29,33 @@ def test_load_acp_config_sets_agents():
|
||||
}
|
||||
}
|
||||
)
|
||||
agents = get_acp_agents()
|
||||
agents = cfg.acp_agents
|
||||
assert "claude_code" in agents
|
||||
assert agents["claude_code"].command == "claude-code-acp"
|
||||
assert agents["claude_code"].description == "Claude Code for coding tasks"
|
||||
assert agents["claude_code"].model is None
|
||||
|
||||
|
||||
def test_load_acp_config_multiple_agents():
|
||||
load_acp_config_from_dict(
|
||||
def test_multiple_agents():
|
||||
cfg = _make_config(
|
||||
{
|
||||
"claude_code": {"command": "claude-code-acp", "args": [], "description": "Claude Code"},
|
||||
"codex": {"command": "codex-acp", "args": ["--flag"], "description": "Codex CLI"},
|
||||
}
|
||||
)
|
||||
agents = get_acp_agents()
|
||||
agents = cfg.acp_agents
|
||||
assert len(agents) == 2
|
||||
assert agents["codex"].args == ["--flag"]
|
||||
|
||||
|
||||
def test_load_acp_config_empty_clears_agents():
|
||||
load_acp_config_from_dict({"agent": {"command": "cmd", "args": [], "description": "desc"}})
|
||||
assert len(get_acp_agents()) == 1
|
||||
|
||||
load_acp_config_from_dict({})
|
||||
assert len(get_acp_agents()) == 0
|
||||
def test_empty_acp_agents():
|
||||
cfg = _make_config({})
|
||||
assert cfg.acp_agents == {}
|
||||
|
||||
|
||||
def test_load_acp_config_none_clears_agents():
|
||||
load_acp_config_from_dict({"agent": {"command": "cmd", "args": [], "description": "desc"}})
|
||||
assert len(get_acp_agents()) == 1
|
||||
|
||||
load_acp_config_from_dict(None)
|
||||
assert get_acp_agents() == {}
|
||||
def test_default_acp_agents_empty():
|
||||
cfg = AppConfig(sandbox=SandboxConfig(use="test"))
|
||||
assert cfg.acp_agents == {}
|
||||
|
||||
|
||||
def test_acp_agent_config_defaults():
|
||||
@@ -79,8 +76,8 @@ def test_acp_agent_config_env_default_is_empty():
|
||||
assert cfg.env == {}
|
||||
|
||||
|
||||
def test_load_acp_config_preserves_env():
|
||||
load_acp_config_from_dict(
|
||||
def test_acp_agent_preserves_env():
|
||||
cfg = _make_config(
|
||||
{
|
||||
"codex": {
|
||||
"command": "codex-acp",
|
||||
@@ -90,8 +87,7 @@ def test_load_acp_config_preserves_env():
|
||||
}
|
||||
}
|
||||
)
|
||||
cfg = get_acp_agents()["codex"]
|
||||
assert cfg.env == {"OPENAI_API_KEY": "$OPENAI_API_KEY", "FOO": "bar"}
|
||||
assert cfg.acp_agents["codex"].env == {"OPENAI_API_KEY": "$OPENAI_API_KEY", "FOO": "bar"}
|
||||
|
||||
|
||||
def test_acp_agent_config_with_model():
|
||||
@@ -115,13 +111,7 @@ def test_acp_agent_config_missing_description_raises():
|
||||
ACPAgentConfig(command="my-agent")
|
||||
|
||||
|
||||
def test_get_acp_agents_returns_empty_by_default():
|
||||
"""After clearing, should return empty dict."""
|
||||
load_acp_config_from_dict({})
|
||||
assert get_acp_agents() == {}
|
||||
|
||||
|
||||
def test_app_config_reload_without_acp_agents_clears_previous_state(tmp_path, monkeypatch):
|
||||
def test_app_config_from_file_with_acp_agents(tmp_path, monkeypatch):
|
||||
config_path = tmp_path / "config.yaml"
|
||||
extensions_path = tmp_path / "extensions_config.json"
|
||||
extensions_path.write_text(json.dumps({"mcpServers": {}, "skills": {}}), encoding="utf-8")
|
||||
@@ -157,9 +147,9 @@ def test_app_config_reload_without_acp_agents_clears_previous_state(tmp_path, mo
|
||||
monkeypatch.setenv("DEER_FLOW_EXTENSIONS_CONFIG_PATH", str(extensions_path))
|
||||
|
||||
config_path.write_text(yaml.safe_dump(config_with_acp), encoding="utf-8")
|
||||
AppConfig.from_file(str(config_path))
|
||||
assert set(get_acp_agents()) == {"codex"}
|
||||
app = AppConfig.from_file(str(config_path))
|
||||
assert set(app.acp_agents) == {"codex"}
|
||||
|
||||
config_path.write_text(yaml.safe_dump(config_without_acp), encoding="utf-8")
|
||||
AppConfig.from_file(str(config_path))
|
||||
assert get_acp_agents() == {}
|
||||
app = AppConfig.from_file(str(config_path))
|
||||
assert app.acp_agents == {}
|
||||
|
||||
Reference in New Issue
Block a user