Reject null agent registry fields

This commit is contained in:
Mikhail Putilovskij 2026-04-24 13:05:26 +03:00
parent e801225220
commit 2fb6c10a5a
2 changed files with 31 additions and 4 deletions

View file

@ -29,6 +29,16 @@ class AgentRegistry:
raise AgentRegistryError(f"unknown agent id: {agent_id}") from exc raise AgentRegistryError(f"unknown agent id: {agent_id}") from exc
def _required_text(entry: Mapping[str, object], key: str) -> str:
value = entry.get(key)
if value is None:
raise AgentRegistryError("each agent entry requires id and label")
text = str(value).strip()
if not text:
raise AgentRegistryError("each agent entry requires id and label")
return text
def _load_registry_data(path: str | Path) -> dict[str, object]: def _load_registry_data(path: str | Path) -> dict[str, object]:
try: try:
raw = yaml.safe_load(Path(path).read_text(encoding="utf-8")) raw = yaml.safe_load(Path(path).read_text(encoding="utf-8"))
@ -52,10 +62,8 @@ def load_agent_registry(path: str | Path) -> AgentRegistry:
for entry in entries: for entry in entries:
if not isinstance(entry, Mapping): if not isinstance(entry, Mapping):
raise AgentRegistryError("each agent entry requires id and label") raise AgentRegistryError("each agent entry requires id and label")
agent_id = str(entry.get("id", "")).strip() agent_id = _required_text(entry, "id")
label = str(entry.get("label", "")).strip() label = _required_text(entry, "label")
if not agent_id or not label:
raise AgentRegistryError("each agent entry requires id and label")
if agent_id in seen: if agent_id in seen:
raise AgentRegistryError(f"duplicate agent id: {agent_id}") raise AgentRegistryError(f"duplicate agent id: {agent_id}")
seen.add(agent_id) seen.add(agent_id)

View file

@ -117,3 +117,22 @@ def test_load_agent_registry_rejects_malformed_yaml(tmp_path: Path):
with pytest.raises(AgentRegistryError, match="invalid agent registry YAML"): with pytest.raises(AgentRegistryError, match="invalid agent registry YAML"):
load_agent_registry(path) load_agent_registry(path)
@pytest.mark.parametrize(
"content",
[
"agents:\n"
" - id: null\n"
" label: Analyst\n",
"agents:\n"
" - id: agent-1\n"
" label: null\n",
],
)
def test_load_agent_registry_rejects_null_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)