From 25aa5d9313e993852663472b05eff89afbeab3e7 Mon Sep 17 00:00:00 2001 From: Mikhail Putilovskij Date: Fri, 24 Apr 2026 13:08:25 +0300 Subject: [PATCH] Make Matrix agent registry immutable --- adapter/matrix/agent_registry.py | 4 +-- tests/adapter/matrix/test_agent_registry.py | 36 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/adapter/matrix/agent_registry.py b/adapter/matrix/agent_registry.py index 1b03d77..28bfbed 100644 --- a/adapter/matrix/agent_registry.py +++ b/adapter/matrix/agent_registry.py @@ -19,8 +19,8 @@ class AgentDefinition: class AgentRegistry: def __init__(self, agents: list[AgentDefinition]) -> None: - self.agents = agents - self._by_id = {agent.agent_id: agent for agent in agents} + self.agents = tuple(agents) + self._by_id = {agent.agent_id: agent for agent in self.agents} def get(self, agent_id: str) -> AgentDefinition: try: diff --git a/tests/adapter/matrix/test_agent_registry.py b/tests/adapter/matrix/test_agent_registry.py index ce467cc..e48a4a5 100644 --- a/tests/adapter/matrix/test_agent_registry.py +++ b/tests/adapter/matrix/test_agent_registry.py @@ -22,6 +22,23 @@ def test_load_agent_registry_reads_yaml_entries(tmp_path: Path): 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): path = tmp_path / "agents.yaml" 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"): 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)