Tighten Matrix agent registry validation

This commit is contained in:
Mikhail Putilovskij 2026-04-24 13:02:19 +03:00
parent b53523ad6c
commit e801225220
4 changed files with 142 additions and 3 deletions

View file

@ -1,7 +1,7 @@
from __future__ import annotations
from dataclasses import dataclass
from collections.abc import Mapping
from dataclasses import dataclass
from pathlib import Path
import yaml
@ -29,8 +29,20 @@ class AgentRegistry:
raise AgentRegistryError(f"unknown agent id: {agent_id}") from exc
def _load_registry_data(path: str | Path) -> dict[str, object]:
try:
raw = yaml.safe_load(Path(path).read_text(encoding="utf-8"))
except yaml.YAMLError as exc:
raise AgentRegistryError("invalid agent registry YAML") from exc
if raw is None:
return {}
if not isinstance(raw, Mapping):
raise AgentRegistryError("agent registry must be a mapping with an agents list")
return dict(raw)
def load_agent_registry(path: str | Path) -> AgentRegistry:
raw = yaml.safe_load(Path(path).read_text(encoding="utf-8")) or {}
raw = _load_registry_data(path)
entries = raw.get("agents")
if not isinstance(entries, list) or not entries:
raise AgentRegistryError("agents registry must contain a non-empty agents list")