Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/app-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ jobs:
run: |
python -m pip install --disable-pip-version-check --require-hashes -r backend/requirements-hashes.txt

- name: Install Noema agent optional dependency
# Installs pydantic-ai (services/noema_agent.py) so the real build-path
# test (test_agent_runs_tools_with_test_model) executes instead of being
# skipped. Keep the core and agent lock files on the same pip command so
# --require-hashes can validate both preinstalled shared dependencies and
# agent-only packages.
run: |
python -m pip install --disable-pip-version-check --require-hashes \
-r backend/requirements-hashes.txt \
-r backend/requirements-agent.txt

- name: Run backend lint
run: |
cd backend
Expand Down
40 changes: 40 additions & 0 deletions backend/requirements-agent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Optional dependencies for the Noema general agent (services/noema_agent.py).
#
# Pydantic-AI is MIT licensed and builds on the OpenAI-compatible provider layer
# already used by naruon (openai, httpx, pydantic). Installing this enables the
# agent; without it, services/noema_agent.py degrades to a structured no-op.
#
# Kept in a small hash-pinned optional set so the core runtime lock remains
# focused while CI can still run the real agent build-path test deterministically.
# Install together with the core lock file:
# pip install --require-hashes -r backend/requirements-hashes.txt -r backend/requirements-agent.txt
#
# Pinned to the pydantic-ai 2.x line: services/noema_agent.py imports
# ``OpenAIChatModel`` from ``pydantic_ai.models.openai`` (renamed from
# ``OpenAIModel`` in 2.x) and constructs the model via ``OpenAIProvider``.
# Version 2.8.0 is the newest 2.x release compatible with the core
# requirements-hashes.txt pin of openai==2.44.0; 2.9.0 requires openai>=2.45.0.
pydantic-ai-slim[openai]==2.8.0 \
--hash=sha256:ddf8ac31442d5433ffff7c47af895088c230448fd28926a713c0f47aa2affc12
# via -r backend/requirements-agent.txt
genai-prices==0.0.71 \
--hash=sha256:1d13111563af2b1ce43ccfacf77b7ac3216ad704c644408a56e11b181fe0d128
# via pydantic-ai-slim
griffelib==2.1.0 \
--hash=sha256:cc7b3d2d2865ad0b909fcc38086e3f554b5ea7acbaa7bbb7ecaa3f5dfb7d9f00
# via pydantic-ai-slim
httpcore2==2.5.0 \
--hash=sha256:5ce35188de461d31e8d000bfb8ef8bf22c6c16587a211e5571deaa5e9bdf842a
# via httpx2
httpx2==2.5.0 \
--hash=sha256:3d2d4d9cf4b61f1a1f46a95947cfdb47e80cb56a2f91c6256ac8f58e4891df41
# via genai-prices
logfire-api==4.37.0 \
--hash=sha256:1d756f8ba23aa56d438e0ba2c0f529a00fcac975b8785c561b058267f9465088
# via pydantic-graph
pydantic-graph==2.8.0 \
--hash=sha256:9b44a06d413586299a2d9ffb76efc1aa1d3b2268984332b03af24dd0c32dad35
# via pydantic-ai-slim
truststore==0.10.4 \
--hash=sha256:adaeaecf1cbb5f4de3b1959b42d41f6fab57b2b1666adb59e89cb0b53361d981
# via httpcore2, httpx2
4 changes: 4 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ PyJWT==2.13.0 # latest verified 2026-06-12; do not downgrade to 2.8.0
icalendar==7.2.0
ruff==0.15.20
defusedxml==0.7.1
# Optional: the Noema general agent (services/noema_agent.py) uses pydantic-ai
# (MIT). It is intentionally NOT in the hashed runtime set — install it
# separately via backend/requirements-agent.txt to enable the agent. The agent
# degrades to a no-op notice when the package is absent.
149 changes: 149 additions & 0 deletions backend/services/agent_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"""Loader for the workspace agent registry.

The repository ships two registration files at its root:

* ``registered_agents.json`` — the catalog of available agents keyed by agent id.
* ``task_agent_mapping.json`` — a mapping from a task type to the agent id that
should handle it.

These files are the intended registration point for pluggable agents. This
module loads them lazily, validates their shape defensively, and exposes small
lookup helpers so callers never have to touch the filesystem directly.
"""

from __future__ import annotations

import json
import logging
from dataclasses import dataclass, field
from functools import lru_cache
from pathlib import Path
from typing import Any

logger = logging.getLogger(__name__)

# backend/services/agent_registry.py -> parents[2] is the repository root.
_REPO_ROOT = Path(__file__).resolve().parents[2]
REGISTERED_AGENTS_PATH = _REPO_ROOT / "registered_agents.json"
TASK_AGENT_MAPPING_PATH = _REPO_ROOT / "task_agent_mapping.json"


@dataclass(frozen=True)
class RegisteredAgent:
"""A single entry from ``registered_agents.json``."""

agent_id: str
name: str
framework: str
entrypoint: str
description: str = ""
capabilities: tuple[str, ...] = ()
provider_source: str = ""
writeback_opt_in: bool = False
writeback_audit_logged: bool = False
degrades_gracefully: bool = False
enabled: bool = True
raw: dict[str, Any] = field(default_factory=dict)


def _coerce_capabilities(value: Any) -> tuple[str, ...]:
if not isinstance(value, list):
return ()
return tuple(item for item in value if isinstance(item, str) and item)


def _agent_from_entry(agent_id: str, entry: dict[str, Any]) -> RegisteredAgent | None:
entrypoint = entry.get("entrypoint")
name = entry.get("name")
framework = entry.get("framework")
if not isinstance(entrypoint, str) or not entrypoint:
logger.debug("Skipping agent %s: missing entrypoint", agent_id)
return None
if not isinstance(name, str) or not name:
name = agent_id
if not isinstance(framework, str):
framework = ""

writeback = entry.get("writeback")
writeback = writeback if isinstance(writeback, dict) else {}

return RegisteredAgent(
agent_id=agent_id,
name=name,
framework=framework,
entrypoint=entrypoint,
description=str(entry.get("description", "") or ""),
capabilities=_coerce_capabilities(entry.get("capabilities")),
provider_source=str(entry.get("provider_source", "") or ""),
writeback_opt_in=bool(writeback.get("opt_in", False)),
writeback_audit_logged=bool(writeback.get("audit_logged", False)),
degrades_gracefully=bool(entry.get("degrades_gracefully", False)),
enabled=bool(entry.get("enabled", True)),
raw=dict(entry),
)


def _load_json_object(path: Path) -> dict[str, Any]:
try:
text = path.read_text(encoding="utf-8")
except FileNotFoundError:
logger.debug("Registration file not found: %s", path)
return {}
except OSError:
logger.debug("Could not read registration file: %s", path, exc_info=True)
return {}

try:
parsed = json.loads(text or "{}")
except json.JSONDecodeError:
logger.debug("Malformed registration file: %s", path, exc_info=True)
return {}

return parsed if isinstance(parsed, dict) else {}


@lru_cache(maxsize=1)
def load_registered_agents() -> dict[str, RegisteredAgent]:
"""Return the registered agents keyed by ``agent_id`` (cached)."""
raw = _load_json_object(REGISTERED_AGENTS_PATH)
agents: dict[str, RegisteredAgent] = {}
for agent_id, entry in raw.items():
if not isinstance(agent_id, str) or not isinstance(entry, dict):
continue
agent = _agent_from_entry(agent_id, entry)
if agent is not None:
agents[agent_id] = agent
return agents


@lru_cache(maxsize=1)
def load_task_agent_mapping() -> dict[str, str]:
"""Return the task-type -> agent-id mapping (cached)."""
raw = _load_json_object(TASK_AGENT_MAPPING_PATH)
mapping: dict[str, str] = {}
for task_type, agent_id in raw.items():
if isinstance(task_type, str) and isinstance(agent_id, str) and agent_id:
mapping[task_type] = agent_id
return mapping


def get_registered_agent(agent_id: str) -> RegisteredAgent | None:
"""Look up a single registered agent by id."""
return load_registered_agents().get(agent_id)


def resolve_agent_for_task(task_type: str) -> RegisteredAgent | None:
"""Resolve the enabled agent registered to handle ``task_type``."""
agent_id = load_task_agent_mapping().get(task_type)
if not agent_id:
return None
agent = get_registered_agent(agent_id)
if agent is None or not agent.enabled:
return None
return agent


def clear_registry_cache() -> None:
"""Reset cached registry state (primarily for tests)."""
load_registered_agents.cache_clear()
load_task_agent_mapping.cache_clear()
Loading
Loading