agenthub/backend/tests/test_interop_nanobot.py

175 lines
6.0 KiB
Python

"""Interop against a real nanobot install, when one exists.
tests/fake_nanobot.py encodes our reading of nanobot/api/server.py; this file
checks that reading. It boots the upstream ``create_app()`` with a stubbed agent
loop (tests/nanobot_real_stub.py) using a real nanobot interpreter, then points
the driver and the whole task plane at it. Upstream code decides what is valid,
so a drift in our driver fails here even while the fake suite stays green.
Skipped unless a nanobot interpreter is found. To get one:
cp -R soucecode/nanobot /tmp/nanobot-build
uv venv .nanobot-venv
NANOBOT_SKIP_WEBUI_BUILD=1 uv pip install --python .nanobot-venv/bin/python "/tmp/nanobot-build[api]"
or point AGENTHUB_NANOBOT_PYTHON at any interpreter that can import nanobot.
"""
from __future__ import annotations
import json
import os
import subprocess
import threading
from pathlib import Path
from typing import Any
import pytest
from agenthub.adapters.base import AgentTarget, DriverError, Invocation
from agenthub.adapters.http_openai import HttpOpenAIDriver, NanobotDriver
from tests.conftest import BASE, add_agent, task_status, wait_for
HERE = Path(__file__).resolve().parent
STUB = HERE / "nanobot_real_stub.py"
MARKER = "AGENTHUB_STUB "
MODEL = "real-nanobot-model"
API_KEY = "interop-key"
def nanobot_python() -> str:
"""Interpreter that can import nanobot; empty when there is none."""
override = os.environ.get("AGENTHUB_NANOBOT_PYTHON", "").strip()
if override:
return override
bundled = HERE.parents[1] / ".nanobot-venv" / "bin" / "python"
return str(bundled) if bundled.exists() else ""
class RealNanobot:
"""Address of the upstream server, as the stub reported it."""
def __init__(self, info: dict[str, Any]) -> None:
self.info = info
self.base_url = str(info["base_url"])
def target(self, **fields: Any) -> AgentTarget:
fields.setdefault("driver", "nanobot")
# Rows normally hold the placeholder model from the docs; the driver is
# expected to ask the server which model it really is.
fields.setdefault("model", "nanobot")
fields.setdefault("auth", {"type": "bearer", "token": API_KEY})
return AgentTarget(id="agt_real", name="nanobot-real", base_url=self.base_url, **fields)
@pytest.fixture(scope="module")
def real_nanobot():
python = nanobot_python()
if not python:
pytest.skip("no nanobot interpreter (AGENTHUB_NANOBOT_PYTHON or .nanobot-venv)")
proc = subprocess.Popen(
[python, str(STUB), "--port", "0", "--api-key", API_KEY, "--model", MODEL],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
seen: dict[str, str] = {}
def read_line() -> None:
seen["line"] = proc.stdout.readline()
reader = threading.Thread(target=read_line, daemon=True)
reader.start()
reader.join(120) # a cold nanobot import is not fast
line = (seen.get("line") or "").strip()
if not line.startswith(MARKER):
reason = "the stub printed nothing"
if proc.poll() is not None:
reason = (proc.stderr.read() or "")[-500:]
proc.kill()
pytest.skip("nanobot stub did not start: " + reason)
try:
yield RealNanobot(json.loads(line[len(MARKER) :]))
finally:
proc.terminate()
try:
proc.wait(10)
except subprocess.TimeoutExpired: # pragma: no cover - stubborn child
proc.kill()
async def ask(driver, where: AgentTarget, prompt: str = "ship it", context=None, session: str = "run-7"):
async def emit(kind: str, payload: dict) -> None:
return None
invocation = Invocation(
task_id="task-1",
prompt=prompt,
target=where,
session_key=session,
context=dict(context or {}),
)
try:
return await driver.invoke(invocation, emit)
finally:
await driver.close()
async def test_health_and_models_come_from_upstream(real_nanobot):
driver = NanobotDriver()
try:
health = await driver.probe(real_nanobot.target())
finally:
await driver.close()
assert health["ok"] and health["runtime"] == "nanobot"
assert health["body"] == {"status": "ok"} # /health stays unauthenticated upstream
assert health["models"] == [MODEL]
async def test_a_task_round_trips_through_the_upstream_server(real_nanobot):
result = await ask(
NanobotDriver(),
real_nanobot.target(),
context={"instructions": "be terse", "history": [{"role": "user", "content": "earlier"}]},
)
assert result.ok, result.error
# upstream maps session_key run-7 to its own key, accepts the single user
# message, and the answer arrives over SSE
assert result.text == "real-nanobot api:run-7 streamed"
async def test_upstream_enforces_its_own_api_key(real_nanobot):
with pytest.raises(DriverError, match="401"):
await ask(NanobotDriver(), real_nanobot.target(auth={}))
async def test_upstream_still_rejects_a_generic_openai_request(real_nanobot):
"""The two nanobot quirks are upstream rules, not assumptions we invented."""
generic = real_nanobot.target(driver="openai_compatible", model="gpt-4o-mini")
with pytest.raises(DriverError, match="Only configured model"):
await ask(HttpOpenAIDriver(), generic)
def test_the_hub_can_drive_a_real_nanobot(hub, real_nanobot):
add_agent(
hub,
"nanobot-upstream",
driver="nanobot",
base_url=real_nanobot.base_url,
model="nanobot",
auth={"type": "bearer", "token": API_KEY},
)
task = hub.post(
f"{BASE}/tasks",
json={
"agent_id": "nanobot-upstream",
"prompt": "summarise the plan",
"context": {"instructions": "answer in one line"},
"session_key": "interop-1",
},
).json()
wait_for(lambda: task_status(hub, task["id"]) in ("completed", "failed"), timeout=30)
done = hub.get(f"{BASE}/tasks/" + task["id"]).json()
assert done["status"] == "completed", done.get("error")
assert done["result"].startswith("real-nanobot api:interop-1")