sovereign-integration / scripts /build_agents.py
Codexcoder's picture
integration: add scripts/build_agents.py
2f361bf verified
Raw History Blame Contribute Delete
3.08 kB
#!/usr/bin/env python3
"""
Agent build & deployment pipeline β€” validate + package agent definitions.
Build steps:
1. Parse agents/registry.yaml and every agents/*.agent.yaml
2. Cross-check: every registry entry resolves to a file; every file is
registered (warn on orphans); module/tool references exist
3. Emit a build report (stdout + JSON artifact manifest)
Usage:
python3 scripts/build_agents.py # build + report
python3 scripts/build_agents.py --json # machine-readable manifest only
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
import yaml
ROOT = Path(__file__).resolve().parent.parent
AGENTS_DIR = ROOT / "agents"
def load_yaml(path: Path):
with open(path, encoding="utf-8") as fh:
return yaml.safe_load(fh) or {}
def build() -> dict:
registry = load_yaml(AGENTS_DIR / "registry.yaml")
entries = registry.get("agents", [])
entry_ids = [e["id"] for e in entries]
built = []
orphaned = []
errors = []
for path in sorted(AGENTS_DIR.glob("*.agent.yaml")):
data = load_yaml(path)
agent_id = data.get("id")
if agent_id is None:
errors.append(f"{path.name}: missing id")
continue
registered = agent_id in entry_ids
if not registered:
orphaned.append(agent_id)
workers = data.get("agents", [])
tasks = data.get("tasks", [])
tools = data.get("tools", [])
built.append({
"id": agent_id,
"file": path.name,
"kind": data.get("kind", "unknown"),
"registered": registered,
"workers": len(workers),
"tasks": tasks,
"tools": tools,
"triggers": data.get("triggers", []),
})
for entry in entries:
f = entry.get("file")
if f and not (AGENTS_DIR / f).exists():
errors.append(f"registry -> {f}: file missing")
status = "PASS" if not errors else "FAIL"
report = {
"status": status,
"agents_built": len(built),
"registered": len([b for b in built if b["registered"]]),
"orphaned": orphaned,
"errors": errors,
"agents": built,
}
return report
def main() -> int:
report = build()
if "--json" in sys.argv:
print(json.dumps(report, indent=2))
else:
print(f"πŸ”¨ Agent build β€” {report['status']}")
print(f" agents built : {report['agents_built']}")
print(f" registered : {report['registered']}")
print(f" orphaned : {report['orphaned'] or 'none'}")
for b in report["agents"]:
print(f" - {b['id']:<24} kind={b['kind']:<6} workers={b['workers']} "
f"tasks={len(b['tasks'])} tools={len(b['tools'])}")
if report["errors"]:
for e in report["errors"]:
print(f" βœ— {e}")
else:
print(" βœ… no build errors")
return 0 if report["status"] == "PASS" else 1
if __name__ == "__main__":
sys.exit(main())