Files
chatgpt-on-wechat/tests/test_agent_initializer_routing.py
zhayujie fc7530e1c4 fix(agent): key cached runtime state by workspace, not by process
Three caches were written when one process meant one workspace, so
"the process" was an accidentally correct key. With several Agents in
one process each one hands the first Agent to start ownership of state
the others then read:

- ToolManager was a plain singleton holding booted MCP subprocesses.
  The first Agent's mcp.json decided which servers existed; every other
  Agent inherited its tools and their credentials, and its own servers
  never booted, because _mcp_loaded had already flipped.
- get_default_memory_config() had a single slot that each Agent's
  initializer overwrote with its own workspace on startup, so the last
  one to initialize decided where every Agent's memory and conversation
  history was written.
- initialize_agent resolved the workspace but dropped the profile it
  came from, raising NameError on every reply.
2026-08-02 17:33:06 +08:00

65 lines
2.0 KiB
Python

"""Covers the real initialize_agent body.
Every other multi-agent test replaces the initializer with a fake, which is how
`name 'profile' is not defined` reached a running gateway: nothing executed the
function. These are slow (they build a full agent) but they exercise the path a
real message takes.
"""
import os
import pytest
from agent.registry import AgentProfile, AgentRegistry, set_agent_registry
from common.runtime_identity import identity_scope
@pytest.fixture
def two_agents(tmp_path):
registry = AgentRegistry(
[
AgentProfile("sales", "Sales", str(tmp_path / "sales")),
AgentProfile("support", "Support", str(tmp_path / "support")),
],
default_agent_id="sales",
)
set_agent_registry(registry)
yield registry
set_agent_registry(None)
@pytest.fixture
def initializer():
from bridge.agent_bridge import AgentBridge
from bridge.bridge import Bridge
return AgentBridge(Bridge()).initializer
def _realpath(path):
return os.path.realpath(str(path))
def test_initialize_agent_follows_the_ambient_identity(two_agents, initializer, tmp_path):
with identity_scope(agent_id="support"):
agent = initializer.initialize_agent(session_id="s1")
assert agent.agent_id == "support"
assert agent.agent_profile.name == "Support"
assert _realpath(agent.workspace_dir) == _realpath(tmp_path / "support")
def test_explicit_agent_id_overrides_the_ambient_identity(two_agents, initializer, tmp_path):
with identity_scope(agent_id="support"):
agent = initializer.initialize_agent(session_id="s1", agent_id="sales")
assert agent.agent_id == "sales"
assert _realpath(agent.workspace_dir) == _realpath(tmp_path / "sales")
def test_absent_identity_uses_the_default_agent(two_agents, initializer, tmp_path):
agent = initializer.initialize_agent(session_id="s1")
assert agent.agent_id == "sales"
assert _realpath(agent.workspace_dir) == _realpath(tmp_path / "sales")