mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-20 03:10:34 +00:00
fix(gateway): read the live config off the loop in the remaining routers (#8298)
Console pricing, the agent model check, the browser tool switch and session config, the projects instructions cap, the channel connection configs, scheduled task writes, the memory config routes and artifact updates read get_app_config() inline on the event loop. Each now goes through asyncio.to_thread (safe_app_config_async for artifacts). Several of these helpers catch Exception, which also hides the gate's BlockingError, so the anchors record where each read ran.
This commit is contained in:
parent
557161c474
commit
4cd47819a6
@ -174,6 +174,8 @@ def _validate_model_exists(model: str | None) -> None:
|
||||
Best-effort: if the app config cannot be loaded (e.g. no ``config.yaml`` on
|
||||
disk in a bare/test deployment), skip the check rather than failing the
|
||||
write — the runtime still falls back to the default for an unknown model.
|
||||
It reads the live config, so async routes call it through
|
||||
``asyncio.to_thread``.
|
||||
"""
|
||||
if not model:
|
||||
return
|
||||
@ -386,7 +388,7 @@ async def import_agent_package(body: AgentPackage, request: Request, name: str |
|
||||
_require_agents_api_enabled()
|
||||
target_name = name or body.agent.name
|
||||
_validate_agent_name(target_name)
|
||||
_validate_model_exists(body.agent.model)
|
||||
await asyncio.to_thread(_validate_model_exists, body.agent.model)
|
||||
normalized_name = _normalize_agent_name(target_name)
|
||||
user_id = get_effective_user_id()
|
||||
|
||||
@ -516,7 +518,7 @@ async def create_agent_endpoint(body: AgentCreateRequest, request: Request) -> A
|
||||
"""
|
||||
_require_agents_api_enabled()
|
||||
_validate_agent_name(body.name)
|
||||
_validate_model_exists(body.model)
|
||||
await asyncio.to_thread(_validate_model_exists, body.model)
|
||||
normalized_name = _normalize_agent_name(body.name)
|
||||
user_id = get_effective_user_id()
|
||||
|
||||
@ -588,7 +590,7 @@ async def update_agent(name: str, body: AgentUpdateRequest, request: Request) ->
|
||||
)
|
||||
|
||||
if "model" in body.model_fields_set:
|
||||
_validate_model_exists(body.model)
|
||||
await asyncio.to_thread(_validate_model_exists, body.model)
|
||||
|
||||
try:
|
||||
# Update config if any config fields changed
|
||||
|
||||
@ -24,7 +24,7 @@ from app.gateway.authz import SandboxRequestLease, require_permission, try_acqui
|
||||
from app.gateway.deps import get_run_manager
|
||||
from app.gateway.internal_auth import get_trusted_internal_owner_user_id
|
||||
from app.gateway.path_utils import normalize_outputs_virtual_path, resolve_outputs_confined_path, resolve_thread_virtual_path
|
||||
from deerflow.authz.sandbox_authz import safe_app_config
|
||||
from deerflow.authz.sandbox_authz import safe_app_config_async
|
||||
from deerflow.config.paths import make_safe_user_id
|
||||
from deerflow.runtime import ConflictError, ThreadOperationKind
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
@ -848,7 +848,7 @@ async def update_artifact(
|
||||
sandbox_provider,
|
||||
thread_id,
|
||||
user_id=effective_user_id,
|
||||
app_config=safe_app_config(),
|
||||
app_config=await safe_app_config_async(),
|
||||
owner_prefix="gateway:artifact",
|
||||
)
|
||||
sandbox = sandbox_lease.sandbox
|
||||
|
||||
@ -85,7 +85,7 @@ async def navigate_browser(thread_id: ThreadId, body: BrowserNavigateRequest, re
|
||||
if thread_store is None or not await _browser_thread_owned_by(thread_store, thread_id, user_id):
|
||||
raise HTTPException(status_code=404, detail=f"Thread {thread_id} not found")
|
||||
|
||||
if not _browser_tools_enabled():
|
||||
if not await asyncio.to_thread(_browser_tools_enabled):
|
||||
raise HTTPException(status_code=404, detail="Browser automation is not enabled")
|
||||
|
||||
try:
|
||||
@ -260,7 +260,7 @@ async def browser_stream(websocket: WebSocket, thread_id: ThreadId) -> None:
|
||||
await websocket.close(code=4404)
|
||||
return
|
||||
|
||||
if not _browser_tools_enabled():
|
||||
if not await asyncio.to_thread(_browser_tools_enabled):
|
||||
await websocket.close(code=4404)
|
||||
return
|
||||
|
||||
@ -310,7 +310,7 @@ async def browser_stream(websocket: WebSocket, thread_id: ThreadId) -> None:
|
||||
# the user's real Chrome when configured.
|
||||
from deerflow.config import get_app_config
|
||||
|
||||
tool_cfg = get_app_config().get_tool_config("browser_navigate")
|
||||
tool_cfg = (await asyncio.to_thread(get_app_config)).get_tool_config("browser_navigate")
|
||||
extra = (tool_cfg.model_extra or {}) if tool_cfg is not None else {}
|
||||
|
||||
def _cfg_int(key: str, default: int) -> int:
|
||||
|
||||
@ -178,6 +178,7 @@ def _get_user_id(request: Request) -> str:
|
||||
|
||||
|
||||
def _get_app_config():
|
||||
"""The live app config. It stats and hashes the config files, so async callers run it through ``asyncio.to_thread``."""
|
||||
from deerflow.config.app_config import get_app_config
|
||||
|
||||
return get_app_config()
|
||||
@ -188,9 +189,10 @@ async def _get_runtime_config_store(request: Request) -> RuntimeConfigStore:
|
||||
store = getattr(request.app.state, "channel_runtime_config_store", None)
|
||||
if store is not None:
|
||||
return store
|
||||
# Resolving reads paths (and the JSON store's file) from disk; keep it off
|
||||
# the event loop. The database store holds no state of its own.
|
||||
store = await asyncio.to_thread(resolve_runtime_config_store, _get_app_config(), cipher=getattr(request.app.state, "credentials_cipher", None))
|
||||
# Resolving reads the config, paths and the JSON store's file from disk;
|
||||
# keep it off the event loop. The database store holds no state of its own.
|
||||
app_config = await asyncio.to_thread(_get_app_config)
|
||||
store = await asyncio.to_thread(resolve_runtime_config_store, app_config, cipher=getattr(request.app.state, "credentials_cipher", None))
|
||||
request.app.state.channel_runtime_config_store = store
|
||||
return store
|
||||
|
||||
@ -201,7 +203,7 @@ async def _get_channel_connections_config(request: Request) -> ChannelConnection
|
||||
# since the last one.
|
||||
config = getattr(request.app.state, "channel_connections_config", None)
|
||||
if not isinstance(config, ChannelConnectionsConfig):
|
||||
config = _get_app_config().channel_connections
|
||||
config = (await asyncio.to_thread(_get_app_config)).channel_connections
|
||||
if not (config.enabled and config.telegram.enabled):
|
||||
# Only the Telegram deep-link username comes from the runtime store.
|
||||
return config
|
||||
@ -214,7 +216,7 @@ async def _get_channels_config(request: Request) -> dict[str, Any]:
|
||||
|
||||
|
||||
async def _load_channels_config(request: Request, config: ChannelConnectionsConfig) -> dict[str, Any]:
|
||||
app_config = _get_app_config()
|
||||
app_config = await asyncio.to_thread(_get_app_config)
|
||||
extra = app_config.model_extra or {}
|
||||
channels_config = extra.get("channels")
|
||||
result = dict(channels_config) if isinstance(channels_config, dict) else {}
|
||||
@ -799,7 +801,7 @@ async def _require_wechat_qr_login(request: Request) -> ChannelConnectionsConfig
|
||||
except ValueError:
|
||||
workers = 0
|
||||
# Kubernetes replicas run one worker each; they declare their peers instead.
|
||||
if workers != 1 or multi_instance_declaration(_get_app_config()) is not None:
|
||||
if workers != 1 or multi_instance_declaration(await asyncio.to_thread(_get_app_config)) is not None:
|
||||
raise HTTPException(
|
||||
status_code=503,
|
||||
detail=(
|
||||
|
||||
@ -160,6 +160,8 @@ def _build_pricing_map() -> dict[str, _ModelPricing]:
|
||||
schema change. Entries are keyed by both the config ``name`` and the
|
||||
provider ``model`` id (plus lowercase variants), because
|
||||
``token_usage_by_model`` buckets carry the provider-reported model name.
|
||||
It reads the live config, so async routes call it through
|
||||
``asyncio.to_thread``.
|
||||
"""
|
||||
try:
|
||||
models = get_app_config().models
|
||||
@ -339,7 +341,7 @@ async def console_stats(request: Request) -> ConsoleStatsResponse:
|
||||
run_where += (RunRow.user_id == user_id,)
|
||||
thread_where = (ThreadMetaRow.user_id == user_id,) if user_id else ()
|
||||
|
||||
pricing = _build_pricing_map()
|
||||
pricing = await asyncio.to_thread(_build_pricing_map)
|
||||
|
||||
async with sf() as session:
|
||||
total_runs = await session.scalar(select(func.count()).select_from(RunRow).where(*run_where)) or 0
|
||||
@ -430,7 +432,7 @@ async def console_runs(
|
||||
async with sf() as session:
|
||||
rows = (await session.execute(stmt)).all()
|
||||
|
||||
pricing = _build_pricing_map()
|
||||
pricing = await asyncio.to_thread(_build_pricing_map)
|
||||
has_more = len(rows) > limit
|
||||
now = datetime.now(UTC)
|
||||
items: list[ConsoleRunItem] = []
|
||||
@ -501,7 +503,7 @@ async def console_usage(
|
||||
d = (start_local + timedelta(days=i)).isoformat()
|
||||
day_buckets[d] = ConsoleUsageDay(date=d)
|
||||
|
||||
pricing = _build_pricing_map()
|
||||
pricing = await asyncio.to_thread(_build_pricing_map)
|
||||
by_model: dict[str, ConsoleUsageModelBreakdown] = {}
|
||||
total_tokens = 0
|
||||
total_runs = 0
|
||||
|
||||
@ -645,7 +645,7 @@ async def get_memory_config_endpoint(request: Request) -> MemoryConfigResponse:
|
||||
}
|
||||
```
|
||||
"""
|
||||
config = get_memory_config()
|
||||
config = await asyncio.to_thread(get_memory_config)
|
||||
return MemoryConfigResponse(
|
||||
enabled=config.enabled,
|
||||
mode=config.mode,
|
||||
@ -670,7 +670,7 @@ async def get_memory_status(request: Request, agent_name: str | None = None) ->
|
||||
Returns:
|
||||
Combined memory configuration and current data.
|
||||
"""
|
||||
config = get_memory_config()
|
||||
config = await asyncio.to_thread(get_memory_config)
|
||||
manager = await asyncio.to_thread(get_memory_manager)
|
||||
selected_agent = _management_agent_name_or_501(manager, agent_name)
|
||||
memory_data = await _get_memory_or_501(
|
||||
|
||||
@ -109,7 +109,8 @@ async def delete_project_memory(project_id: str, fact_id: str, request: Request)
|
||||
@require_permission("projects", "write")
|
||||
async def promote_project_memory(project_id: str, fact_id: str, body: ProjectMemoryPromotion, request: Request) -> ProjectMemoryPromotionResponse:
|
||||
try:
|
||||
result = await _memory_call("promote", project_id, fact_id, max_bytes=_projects_config().instructions_max_bytes, expected_instructions=body.expected_instructions)
|
||||
max_bytes = (await asyncio.to_thread(_projects_config)).instructions_max_bytes
|
||||
result = await _memory_call("promote", project_id, fact_id, max_bytes=max_bytes, expected_instructions=body.expected_instructions)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=409, detail=str(exc)) from exc
|
||||
return ProjectMemoryPromotionResponse(instructions=result["instructions"], memory=ProjectMemoryResponse(**result["memory"]))
|
||||
@ -163,7 +164,11 @@ def _not_found() -> HTTPException:
|
||||
|
||||
|
||||
def _projects_config() -> ProjectsConfig:
|
||||
"""Projects config, falling back to defaults when the app config is unavailable."""
|
||||
"""Projects config, falling back to defaults when the app config is unavailable.
|
||||
|
||||
It reads the live config, so async routes call it, and the validator below
|
||||
that uses it, through ``asyncio.to_thread``.
|
||||
"""
|
||||
try:
|
||||
return get_app_config().projects
|
||||
except (FileNotFoundError, RuntimeError):
|
||||
@ -186,7 +191,7 @@ def _validate_instructions_length(instructions: str | None) -> None:
|
||||
@router.post("", response_model=ProjectResponse, status_code=201)
|
||||
@require_permission("projects", "write")
|
||||
async def create_project(body: ProjectCreateRequest, request: Request) -> ProjectResponse:
|
||||
_validate_instructions_length(body.instructions)
|
||||
await asyncio.to_thread(_validate_instructions_length, body.instructions)
|
||||
repo = get_project_repo(request)
|
||||
return _to_response(await repo.create(name=body.name, instructions=body.instructions, presentation=body.presentation))
|
||||
|
||||
@ -225,7 +230,7 @@ async def get_project(project_id: str, request: Request) -> ProjectResponse:
|
||||
@router.patch("/{project_id}", response_model=ProjectResponse)
|
||||
@require_permission("projects", "write")
|
||||
async def patch_project(project_id: str, body: ProjectPatchRequest, request: Request) -> ProjectResponse:
|
||||
_validate_instructions_length(body.instructions)
|
||||
await asyncio.to_thread(_validate_instructions_length, body.instructions)
|
||||
row = await get_project_repo(request).patch(project_id, name=body.name, instructions=body.instructions, presentation=body.presentation)
|
||||
if row is None:
|
||||
raise _not_found()
|
||||
|
||||
@ -257,7 +257,7 @@ async def list_scheduled_tasks(request: Request):
|
||||
@require_permission("runs", "create")
|
||||
async def create_scheduled_task(request: Request, body: ScheduledTaskCreateRequest):
|
||||
"""Create a task (also used by the page's Duplicate)."""
|
||||
config = get_config()
|
||||
config = await asyncio.to_thread(get_config)
|
||||
repo = get_scheduled_task_repo(request)
|
||||
thread_store = get_thread_store(request)
|
||||
user = await get_optional_user_from_request(request)
|
||||
@ -314,7 +314,7 @@ async def update_scheduled_task(task_id: str, request: Request, body: ScheduledT
|
||||
checked); changing only the limits of a finished task leaves it finished,
|
||||
since reactivating is an explicit Resume.
|
||||
"""
|
||||
config = get_config()
|
||||
config = await asyncio.to_thread(get_config)
|
||||
repo = get_scheduled_task_repo(request)
|
||||
user = await get_optional_user_from_request(request)
|
||||
if user is None:
|
||||
|
||||
@ -79,6 +79,9 @@ workflow's test-file list in step with the marker. Run the bundled suites with
|
||||
Every test under `blocking_io/` runs with a detector that fails on a blocking
|
||||
call made on the event loop through DeerFlow code. Add a case there when you add
|
||||
an async entry point that touches files, subprocesses or a synchronous SDK.
|
||||
The detector raises `BlockingError`, which is an `Exception`, so code that
|
||||
catches `Exception` hides a blocking call from it. On such a path, assert where
|
||||
the call ran, not only that the request succeeded.
|
||||
`@pytest.mark.allow_blocking_io` is for a test that needs the unguarded behaviour
|
||||
and states why.
|
||||
|
||||
|
||||
381
backend/tests/blocking_io/test_router_config_reads.py
Normal file
381
backend/tests/blocking_io/test_router_config_reads.py
Normal file
@ -0,0 +1,381 @@
|
||||
"""Regression anchors: authenticated routers must read the live config off the loop.
|
||||
|
||||
``get_app_config()`` stats and hashes ``config.yaml`` and
|
||||
``extensions_config.json`` on every call and reparses them after an edit, and
|
||||
``get_config()``, ``get_memory_config()`` and ``safe_app_config()`` go through
|
||||
it. Each route below calls one of them, or a router helper built on one, through
|
||||
``asyncio.to_thread``.
|
||||
|
||||
Several of those helpers catch ``Exception`` so a config problem degrades the
|
||||
answer instead of failing the request (no prices, no model check, browser off).
|
||||
The gate's ``BlockingError`` is an ``Exception`` too, so a read left on the loop
|
||||
would pass the gate unnoticed. Each case therefore replaces the read with a
|
||||
recorder that notes whether it ran on the event loop before reading a real
|
||||
file, and asserts the route used the recorder's answer.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
from contextlib import asynccontextmanager
|
||||
from inspect import unwrap
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from blockbuster import BlockingError
|
||||
from fastapi import HTTPException
|
||||
|
||||
# Import every module up front: importing on the loop is itself blocking IO.
|
||||
import deerflow.authz.sandbox_authz as sandbox_authz
|
||||
import deerflow.community.browser_automation # noqa: F401 (imported lazily by browser_stream)
|
||||
import deerflow.config as deerflow_config
|
||||
from app.gateway.authz import Permissions
|
||||
from app.gateway.routers import agents, artifacts, browser, channel_connections, console, memory, projects, scheduled_tasks
|
||||
from deerflow.config.agents_api_config import load_agents_api_config_from_dict
|
||||
from deerflow.config.channel_connections_config import ChannelConnectionsConfig
|
||||
from deerflow.config.memory_config import MemoryConfig
|
||||
from deerflow.config.paths import get_paths
|
||||
from deerflow.config.projects_config import ProjectsConfig
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
|
||||
|
||||
class _ConfigRead:
|
||||
"""Stands in for one config read and records whether it ran on the loop."""
|
||||
|
||||
def __init__(self, result: Any = None) -> None:
|
||||
self.on_loop: list[bool] = []
|
||||
self._result = result
|
||||
|
||||
def __call__(self, *_args: Any, **_kwargs: Any) -> Any:
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
self.on_loop.append(False)
|
||||
else:
|
||||
self.on_loop.append(True)
|
||||
# Record before reading: a read the gate stops on the loop still counts.
|
||||
Path(__file__).read_bytes()
|
||||
if isinstance(self._result, BaseException):
|
||||
raise self._result
|
||||
return self._result
|
||||
|
||||
|
||||
class _Stop(Exception):
|
||||
"""Ends a route right after the config read under test."""
|
||||
|
||||
|
||||
def _request(**state: Any) -> SimpleNamespace:
|
||||
return SimpleNamespace(app=SimpleNamespace(state=SimpleNamespace(**state)), state=SimpleNamespace(), headers={}, cookies={})
|
||||
|
||||
|
||||
class _EmptyResult:
|
||||
def all(self) -> list[Any]:
|
||||
return []
|
||||
|
||||
def scalars(self) -> _EmptyResult:
|
||||
return self
|
||||
|
||||
|
||||
class _EmptySession:
|
||||
async def scalar(self, *_args: Any, **_kwargs: Any) -> int:
|
||||
return 0
|
||||
|
||||
async def execute(self, *_args: Any, **_kwargs: Any) -> _EmptyResult:
|
||||
return _EmptyResult()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def _empty_session():
|
||||
yield _EmptySession()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"call",
|
||||
[
|
||||
pytest.param(lambda: unwrap(console.console_stats)(_request()), id="stats"),
|
||||
pytest.param(lambda: unwrap(console.console_runs)(_request(), limit=20, offset=0, status=None), id="runs"),
|
||||
pytest.param(lambda: unwrap(console.console_usage)(_request(), days=14, tz_offset_minutes=0), id="usage"),
|
||||
],
|
||||
)
|
||||
async def test_console_routes_read_model_pricing_off_the_loop(call, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("DEER_FLOW_HOME", str(tmp_path))
|
||||
monkeypatch.setattr("deerflow.config.paths._paths", None)
|
||||
reads = _ConfigRead({})
|
||||
monkeypatch.setattr(console, "_build_pricing_map", reads)
|
||||
monkeypatch.setattr(console, "_session_factory_or_503", lambda: _empty_session)
|
||||
monkeypatch.setattr(console, "get_current_user", AsyncMock(return_value="user-1"))
|
||||
|
||||
await call()
|
||||
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def agents_api(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.delenv("DEER_FLOW_CONFIG_PATH", raising=False)
|
||||
monkeypatch.setenv("DEER_FLOW_PROJECT_ROOT", str(tmp_path))
|
||||
monkeypatch.setenv("DEER_FLOW_HOME", str(tmp_path))
|
||||
monkeypatch.setattr("deerflow.config.app_config._legacy_config_candidates", lambda: ())
|
||||
monkeypatch.setattr("deerflow.config.paths._paths", None)
|
||||
load_agents_api_config_from_dict({"enabled": True})
|
||||
reads = _ConfigRead(HTTPException(status_code=422, detail="Unknown model 'missing'."))
|
||||
monkeypatch.setattr(agents, "_validate_model_exists", reads)
|
||||
try:
|
||||
yield reads
|
||||
finally:
|
||||
load_agents_api_config_from_dict({})
|
||||
|
||||
|
||||
async def _seed_agent(name: str) -> None:
|
||||
agent_dir = await asyncio.to_thread(get_paths().user_agent_dir, get_effective_user_id(), name)
|
||||
await asyncio.to_thread(agent_dir.mkdir, parents=True, exist_ok=True)
|
||||
await asyncio.to_thread((agent_dir / "config.yaml").write_text, f"name: {name}\n", encoding="utf-8")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("route", ["create", "import", "update"])
|
||||
async def test_agent_writes_check_the_model_off_the_loop(route: str, agents_api: _ConfigRead) -> None:
|
||||
fields = {"name": "loop-agent", "soul": "You are a test agent.", "model": "missing"}
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
if route == "create":
|
||||
await agents.create_agent_endpoint(agents.AgentCreateRequest(**fields))
|
||||
elif route == "import":
|
||||
await agents.import_agent_package(agents.AgentPackage(format="deerflow.custom-agent", version=1, agent=agents.AgentPackageAgent(**fields)))
|
||||
else:
|
||||
await _seed_agent("loop-agent")
|
||||
await agents.update_agent("loop-agent", agents.AgentUpdateRequest(model="missing"))
|
||||
|
||||
assert exc_info.value.status_code == 422
|
||||
assert agents_api.on_loop == [False]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def browser_access(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(browser, "_browser_thread_owned_by", AsyncMock(return_value=True))
|
||||
monkeypatch.setattr(browser, "_authenticate_ws", AsyncMock(return_value=SimpleNamespace(id="user-1")))
|
||||
monkeypatch.setattr(browser, "_ws_origin_allowed", lambda _websocket: True)
|
||||
monkeypatch.setattr(browser, "resolve_route_permissions", AsyncMock(return_value={Permissions.THREADS_WRITE}))
|
||||
|
||||
|
||||
def _websocket() -> SimpleNamespace:
|
||||
return SimpleNamespace(app=SimpleNamespace(state=SimpleNamespace(thread_store=object())), close=AsyncMock())
|
||||
|
||||
|
||||
async def test_browser_navigate_checks_the_tool_switch_off_the_loop(browser_access, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = _ConfigRead(False)
|
||||
monkeypatch.setattr(browser, "_browser_tools_enabled", reads)
|
||||
request = _request(thread_store=object())
|
||||
request.state.auth = SimpleNamespace(user=SimpleNamespace(id="user-1"))
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await unwrap(browser.navigate_browser)("thread-1", browser.BrowserNavigateRequest(url="https://example.com/"), request)
|
||||
|
||||
assert exc_info.value.status_code == 404
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_browser_stream_checks_the_tool_switch_off_the_loop(browser_access, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = _ConfigRead(False)
|
||||
monkeypatch.setattr(browser, "_browser_tools_enabled", reads)
|
||||
websocket = _websocket()
|
||||
|
||||
await browser.browser_stream(websocket, "thread-1")
|
||||
|
||||
websocket.close.assert_awaited_once_with(code=4404)
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_browser_stream_reads_the_session_config_off_the_loop(browser_access, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = _ConfigRead(_Stop())
|
||||
monkeypatch.setattr(browser, "_browser_tools_enabled", lambda: True)
|
||||
monkeypatch.setattr(browser, "_negotiate_browser_frame_format", AsyncMock(return_value=False))
|
||||
monkeypatch.setattr(deerflow_config, "get_app_config", reads)
|
||||
|
||||
with pytest.raises(_Stop):
|
||||
await browser.browser_stream(_websocket(), "thread-1")
|
||||
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def small_projects_config(monkeypatch: pytest.MonkeyPatch) -> _ConfigRead:
|
||||
reads = _ConfigRead(ProjectsConfig(instructions_max_bytes=256))
|
||||
monkeypatch.setattr(projects, "_projects_config", reads)
|
||||
return reads
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"call",
|
||||
[
|
||||
pytest.param(lambda: unwrap(projects.create_project)(projects.ProjectCreateRequest(name="p", instructions="x" * 257), _request()), id="create"),
|
||||
pytest.param(lambda: unwrap(projects.patch_project)("project-1", projects.ProjectPatchRequest(instructions="x" * 257), _request()), id="patch"),
|
||||
],
|
||||
)
|
||||
async def test_project_writes_read_the_instructions_cap_off_the_loop(call, small_projects_config: _ConfigRead) -> None:
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await call()
|
||||
|
||||
assert exc_info.value.status_code == 422
|
||||
assert "256-byte" in exc_info.value.detail
|
||||
assert small_projects_config.on_loop == [False]
|
||||
|
||||
|
||||
async def test_project_memory_promotion_reads_the_instructions_cap_off_the_loop(small_projects_config: _ConfigRead, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
memory_call = AsyncMock(side_effect=ValueError("instructions changed"))
|
||||
monkeypatch.setattr(projects, "_memory_call", memory_call)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await unwrap(projects.promote_project_memory)("project-1", "fact-1", projects.ProjectMemoryPromotion(expected_instructions=""), _request())
|
||||
|
||||
assert exc_info.value.status_code == 409
|
||||
assert memory_call.await_args.kwargs["max_bytes"] == 256
|
||||
assert small_projects_config.on_loop == [False]
|
||||
|
||||
|
||||
async def test_channel_runtime_store_resolves_from_a_config_read_off_the_loop(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
app_config = object()
|
||||
store = object()
|
||||
reads = _ConfigRead(app_config)
|
||||
resolved: list[object] = []
|
||||
|
||||
def resolve(config: object, *, cipher: object) -> object:
|
||||
resolved.append(config)
|
||||
return store
|
||||
|
||||
monkeypatch.setattr(channel_connections, "_get_app_config", reads)
|
||||
monkeypatch.setattr(channel_connections, "resolve_runtime_config_store", resolve)
|
||||
|
||||
assert await channel_connections._get_runtime_config_store(_request()) is store
|
||||
assert resolved == [app_config]
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_channel_connections_config_is_read_off_the_loop(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
config = ChannelConnectionsConfig()
|
||||
reads = _ConfigRead(SimpleNamespace(channel_connections=config))
|
||||
monkeypatch.setattr(channel_connections, "_get_app_config", reads)
|
||||
|
||||
assert await channel_connections._get_channel_connections_config(_request()) is config
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_channels_config_is_read_off_the_loop(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = _ConfigRead(SimpleNamespace(model_extra={"channels": {"slack": {"enabled": True}}}))
|
||||
monkeypatch.setattr(channel_connections, "_get_app_config", reads)
|
||||
monkeypatch.setattr(channel_connections, "merge_runtime_channel_configs", lambda *_args, **_kwargs: None)
|
||||
|
||||
result = await channel_connections._load_channels_config(_request(channel_runtime_config_store=object()), ChannelConnectionsConfig())
|
||||
|
||||
assert result == {"slack": {"enabled": True}}
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_wechat_qr_login_reads_the_deployment_config_off_the_loop(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = _ConfigRead(object())
|
||||
monkeypatch.setattr(channel_connections, "_get_app_config", reads)
|
||||
monkeypatch.setattr(channel_connections, "require_admin_user", AsyncMock())
|
||||
monkeypatch.setattr(channel_connections, "multi_instance_declaration", lambda _config: "deployment.multi_instance")
|
||||
monkeypatch.setenv("GATEWAY_WORKERS", "1")
|
||||
config = ChannelConnectionsConfig(enabled=True, wechat={"enabled": True})
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await channel_connections._require_wechat_qr_login(_request(channel_connections_config=config))
|
||||
|
||||
assert exc_info.value.status_code == 503
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"call",
|
||||
[
|
||||
pytest.param(
|
||||
lambda: unwrap(scheduled_tasks.create_scheduled_task)(
|
||||
_request(),
|
||||
scheduled_tasks.ScheduledTaskCreateRequest(title="t", prompt="p", schedule_type="once", schedule_spec={}, timezone="UTC"),
|
||||
),
|
||||
id="create",
|
||||
),
|
||||
pytest.param(lambda: unwrap(scheduled_tasks.update_scheduled_task)("task-1", _request(), scheduled_tasks.ScheduledTaskUpdateRequest()), id="update"),
|
||||
],
|
||||
)
|
||||
async def test_scheduled_task_writes_read_the_config_off_the_loop(call, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = _ConfigRead(_Stop())
|
||||
monkeypatch.setattr(scheduled_tasks, "get_config", reads)
|
||||
|
||||
with pytest.raises(_Stop):
|
||||
await call()
|
||||
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_memory_config_endpoint_reads_the_config_off_the_loop(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = _ConfigRead(MemoryConfig(enabled=False, injection_enabled=False))
|
||||
monkeypatch.setattr(memory, "get_memory_config", reads)
|
||||
|
||||
response = await unwrap(memory.get_memory_config_endpoint)(_request())
|
||||
|
||||
assert (response.enabled, response.injection_enabled) == (False, False)
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_memory_status_reads_the_config_off_the_loop(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
reads = _ConfigRead(_Stop())
|
||||
monkeypatch.setattr(memory, "get_memory_config", reads)
|
||||
|
||||
with pytest.raises(_Stop):
|
||||
await unwrap(memory.get_memory_status)(_request())
|
||||
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_artifact_update_reads_the_sandbox_policy_off_the_loop(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("DEER_FLOW_HOME", str(tmp_path))
|
||||
monkeypatch.setattr("deerflow.config.paths._paths", None)
|
||||
vpath = "/mnt/user-data/outputs/notes.txt"
|
||||
target = await asyncio.to_thread(artifacts.resolve_outputs_confined_path, "t1", vpath, user_id=get_effective_user_id())
|
||||
await asyncio.to_thread(target.parent.mkdir, parents=True, exist_ok=True)
|
||||
await asyncio.to_thread(target.write_bytes, b"hello")
|
||||
|
||||
@asynccontextmanager
|
||||
async def allow_write(*_args, **_kwargs):
|
||||
yield
|
||||
|
||||
app_config = object()
|
||||
reads = _ConfigRead(app_config)
|
||||
acquire = AsyncMock(side_effect=HTTPException(status_code=403, detail="stop after the policy read"))
|
||||
monkeypatch.setattr(sandbox_authz, "safe_app_config", reads)
|
||||
monkeypatch.setattr(artifacts, "reserve_artifact_write", allow_write)
|
||||
monkeypatch.setattr(artifacts, "get_sandbox_provider", lambda: SimpleNamespace(uses_thread_data_mounts=False))
|
||||
monkeypatch.setattr(artifacts, "try_acquire_sandbox_for_request", acquire)
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await unwrap(artifacts.update_artifact)(
|
||||
"t1",
|
||||
vpath,
|
||||
artifacts.ArtifactUpdateRequest(content="updated", expected_sha256=hashlib.sha256(b"hello").hexdigest()),
|
||||
request=None,
|
||||
)
|
||||
|
||||
assert exc_info.value.status_code == 403
|
||||
assert acquire.await_args.kwargs["app_config"] is app_config
|
||||
assert reads.on_loop == [False]
|
||||
|
||||
|
||||
async def test_config_read_recorder_trips_the_gate_on_the_loop(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Meta-check: a read left on the loop inside router code fails the gate and is recorded.
|
||||
|
||||
The gate only flags IO whose stack passes through ``app.*`` or
|
||||
``deerflow.*``, so the probe goes through a router's own sync helper.
|
||||
"""
|
||||
reads = _ConfigRead(SimpleNamespace(projects=ProjectsConfig()))
|
||||
monkeypatch.setattr(projects, "get_app_config", reads)
|
||||
|
||||
with pytest.raises(BlockingError):
|
||||
projects._projects_config()
|
||||
|
||||
assert reads.on_loop == [True]
|
||||
Loading…
x
Reference in New Issue
Block a user