Make Matrix agent registry immutable
This commit is contained in:
parent
2fb6c10a5a
commit
25aa5d9313
2 changed files with 38 additions and 2 deletions
|
|
@ -19,8 +19,8 @@ class AgentDefinition:
|
||||||
|
|
||||||
class AgentRegistry:
|
class AgentRegistry:
|
||||||
def __init__(self, agents: list[AgentDefinition]) -> None:
|
def __init__(self, agents: list[AgentDefinition]) -> None:
|
||||||
self.agents = agents
|
self.agents = tuple(agents)
|
||||||
self._by_id = {agent.agent_id: agent for agent in agents}
|
self._by_id = {agent.agent_id: agent for agent in self.agents}
|
||||||
|
|
||||||
def get(self, agent_id: str) -> AgentDefinition:
|
def get(self, agent_id: str) -> AgentDefinition:
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,23 @@ def test_load_agent_registry_reads_yaml_entries(tmp_path: Path):
|
||||||
assert registry.get("agent-1").label == "Analyst"
|
assert registry.get("agent-1").label == "Analyst"
|
||||||
|
|
||||||
|
|
||||||
|
def test_agent_registry_agents_sequence_is_immutable(tmp_path: Path):
|
||||||
|
path = tmp_path / "agents.yaml"
|
||||||
|
path.write_text(
|
||||||
|
"agents:\n"
|
||||||
|
" - id: agent-1\n"
|
||||||
|
" label: Analyst\n",
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
|
||||||
|
registry = load_agent_registry(path)
|
||||||
|
|
||||||
|
with pytest.raises(AttributeError):
|
||||||
|
registry.agents.append( # type: ignore[attr-defined]
|
||||||
|
registry.agents[0]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_load_agent_registry_rejects_duplicate_ids(tmp_path: Path):
|
def test_load_agent_registry_rejects_duplicate_ids(tmp_path: Path):
|
||||||
path = tmp_path / "agents.yaml"
|
path = tmp_path / "agents.yaml"
|
||||||
path.write_text(
|
path.write_text(
|
||||||
|
|
@ -136,3 +153,22 @@ def test_load_agent_registry_rejects_null_id_or_label(tmp_path: Path, content: s
|
||||||
|
|
||||||
with pytest.raises(AgentRegistryError, match="each agent entry requires id and label"):
|
with pytest.raises(AgentRegistryError, match="each agent entry requires id and label"):
|
||||||
load_agent_registry(path)
|
load_agent_registry(path)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"content",
|
||||||
|
[
|
||||||
|
"agents:\n"
|
||||||
|
" - id: ' '\n"
|
||||||
|
" label: Analyst\n",
|
||||||
|
"agents:\n"
|
||||||
|
" - id: agent-1\n"
|
||||||
|
" label: ' '\n",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_load_agent_registry_rejects_blank_id_or_label(tmp_path: Path, content: str):
|
||||||
|
path = tmp_path / "agents.yaml"
|
||||||
|
path.write_text(content, encoding="utf-8")
|
||||||
|
|
||||||
|
with pytest.raises(AgentRegistryError, match="each agent entry requires id and label"):
|
||||||
|
load_agent_registry(path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue