mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-23 16:35:59 +00:00
fix: keep new agent bootstrap in user scope (#2784)
This commit is contained in:
@@ -324,6 +324,21 @@ def test_context_does_not_override_existing_configurable():
|
||||
assert config["configurable"]["subagent_enabled"] is True
|
||||
|
||||
|
||||
def test_inject_authenticated_user_context_overrides_client_user_id():
|
||||
"""Run context should carry the authenticated user, not client-supplied user_id."""
|
||||
from types import SimpleNamespace
|
||||
|
||||
from app.gateway.services import build_run_config, inject_authenticated_user_context
|
||||
|
||||
config = build_run_config("thread-1", None, None)
|
||||
config["context"] = {"user_id": "spoofed-client"}
|
||||
request = SimpleNamespace(state=SimpleNamespace(user=SimpleNamespace(id="auth-user-42")))
|
||||
|
||||
inject_authenticated_user_context(config, request)
|
||||
|
||||
assert config["context"]["user_id"] == "auth-user-42"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# build_run_config — context / configurable precedence (LangGraph >= 0.6.0)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@@ -6,6 +6,8 @@ from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from deerflow.tools.builtins.setup_agent_tool import setup_agent
|
||||
|
||||
# --- Helpers ---
|
||||
@@ -126,3 +128,23 @@ class TestSetupAgentNoDataLoss:
|
||||
assert agent_dir.exists()
|
||||
assert (agent_dir / "SOUL.md").read_text() == "# My Agent"
|
||||
assert (agent_dir / "config.yaml").exists()
|
||||
|
||||
@pytest.mark.no_auto_user
|
||||
def test_runtime_user_id_used_when_contextvar_missing(self, tmp_path: Path):
|
||||
"""setup_agent should not fall back to default when runtime carries user_id."""
|
||||
runtime = _DummyRuntime(
|
||||
context={"agent_name": "test-agent", "user_id": "auth-user-42"},
|
||||
tool_call_id="tool-3",
|
||||
)
|
||||
|
||||
with patch("deerflow.tools.builtins.setup_agent_tool.get_paths", return_value=_make_paths_mock(tmp_path)):
|
||||
setup_agent.func(
|
||||
soul="# My Agent",
|
||||
description="A test agent",
|
||||
runtime=runtime,
|
||||
)
|
||||
|
||||
expected_dir = tmp_path / "users" / "auth-user-42" / "agents" / "test-agent"
|
||||
default_dir = tmp_path / "users" / "default" / "agents" / "test-agent"
|
||||
assert (expected_dir / "SOUL.md").read_text() == "# My Agent"
|
||||
assert not default_dir.exists()
|
||||
|
||||
Reference in New Issue
Block a user