Skip to content

The platform for continuously improving AI agents.

9.7 KB
Raw Blame
from __future__ import annotations

import json
import os
import re
import subprocess
import sys
import time
from datetime import UTC, datetime
from pathlib import Path

import pytest

SDK = Path(__file__).resolve().parents[2] / "overmind"
sys.path.insert(0, str(SDK))
os.environ["OVERMIND_ANALYTICS_ENABLED"] = "false"

from factories import make_user  # noqa: E402

import overmind  # noqa: E402
from overbae.models import APIToken  # noqa: E402
from overbae.tasks.research import advance  # noqa: E402

from .browser import (  # noqa: E402
    FRONTEND,
    PASSWORD,
    Browser,
    Console,
    build_console,
    serve_console,
)
from .stack import RUNS, LiveAPI, celery_worker, drain  # noqa: E402
from .surfaces import CliSurface, McpSurface, RestSurface, SampleAgent  # noqa: E402


def pytest_configure(config):
    config.addinivalue_line("markers", "journey: end-to-end journey on the live stack")
    if Path(overmind.__file__).resolve().parents[1] != SDK:
        raise pytest.UsageError(f"Journeys must use the in-repo SDK, got {overmind.__file__}.")


def pytest_collection_modifyitems(items):
    for item in items:
        item.add_marker(pytest.mark.journey)


@pytest.fixture(autouse=True)
def _inline_dataset_tasks():
    yield


@pytest.fixture(autouse=True)
def _journey_db(transactional_db):
    yield


@pytest.fixture(autouse=True)
def _only_fake_providers(monkeypatch):
    from overbae.core.model_registry import PROVIDERS

    for env in {provider.key_env for provider in PROVIDERS.values()}:
        monkeypatch.delenv(env, raising=False)
    monkeypatch.setenv("OPENROUTER_API_KEY", "sk-fake")


@pytest.fixture(scope="session")
def live_api():
    api = LiveAPI().start()
    yield api
    api.stop()


@pytest.fixture(scope="session")
def _worker_ledger():
    import redis

    redis.from_url(os.environ["TEST_REDIS_URL"]).flushdb()
    with celery_worker() as ledger:
        yield ledger


def _release_connector_chunks(ledger) -> None:
    from overbae.models import ConnectorCredential

    # A live chunk publishes its continuation with an eta. drain() ignores those,
    # and one already past its active check still calls out after the next test
    # installs a different vendor mock.
    ConnectorCredential.objects.filter(is_active=True).update(is_active=False)
    deadline = time.monotonic() + 8
    while ledger.unfinished("sync_connector_chunk") and time.monotonic() < deadline:
        time.sleep(0.05)


@pytest.fixture
def worker(_worker_ledger):
    failed_before = set(_worker_ledger.failed())
    yield _worker_ledger
    _release_connector_chunks(_worker_ledger)
    drain(_worker_ledger)
    _worker_ledger.revoke_scheduled()
    failed = {k: v for k, v in _worker_ledger.failed().items() if k not in failed_before}
    assert not failed, f"Background tasks failed: {failed}"


@pytest.fixture
def agent_wakes(monkeypatch) -> list:
    """A deployment hands each research agent's wake to its Temporal workflow; a journey has
    no Temporal server, so the wakes are recorded for the journey to run or ignore."""
    woken = []
    monkeypatch.setattr(advance, "delay", lambda agent, wake=True: woken.append(agent))
    return woken


@pytest.fixture
def sample_agent(tmp_path) -> SampleAgent:
    return SampleAgent.copy("support_desk", tmp_path)


@pytest.fixture
def account():
    user = make_user()
    raw, _ = APIToken.create_for_user(user)
    return user, raw


@pytest.fixture
def account_key(account) -> str:
    return account[1]


@pytest.fixture
def cli(live_api, account_key) -> CliSurface:
    return CliSurface(live_api.url, account_key)


@pytest.fixture
def mcp_for(live_api):
    return lambda key: McpSurface(live_api.url, key)


@pytest.fixture
def rest_for(live_api):
    return lambda key: RestSurface(live_api.url, key)


@pytest.fixture
def llm_url(fake_llm):
    with fake_llm.serve() as url:
        yield url


def answer_turn(request) -> dict:
    if not request.body.get("tools") or any(m.get("role") == "tool" for m in request.messages):
        if "Quote the order id" in request.system:
            return {"content": GOOD_REPLY}
        return {"content": "Your refund is on its way. The order was delivered."}
    return {
        "content": None,
        "tool_calls": [
            {
                "id": f"call_{len(request.messages)}",
                "type": "function",
                "function": {"name": "lookup_order", "arguments": '{"order_id": "42"}'},
            }
        ],
    }


GOOD_REPLY = "Order 42 was delivered. Your refund is on its way."


@pytest.fixture
def judges(fake_llm):
    """Jev meets an expectation only when the judged output is the good reply."""
    fake_llm.decide = lambda key, question, state: (
        "yes" if GOOD_REPLY in str(state.get("output") or "") else "no"
    )


@pytest.fixture
def support_desk_llm(fake_llm, llm_url):
    fake_llm.on("You triage customer support tickets.", "refund")
    fake_llm.on("You are a support agent.", answer_turn)
    return llm_url


@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport(item, call):
    report = yield
    if report.when in ("setup", "call") and (report.failed or report.when == "call"):
        item.journey_report = report
    if report.when == "teardown" and hasattr(item, "journey_report"):
        failed = report.failed or item.journey_report.failed
        report_for_record = report if report.failed else item.journey_report
        llm = item.funcargs.get("fake_llm")
        ledger = item.funcargs.get("worker")
        record = {
            "node": item.nodeid,
            "command": " ".join(sys.argv),
            "outcome": "failed" if failed else "passed",
            "seconds": round(item.journey_report.duration, 3),
            "finished_at": time.strftime("%Y-%m-%dT%H:%M:%S%z"),
            "env": {
                "database": os.environ.get("TEST_POSTGRES_HOST", "localhost"),
                "redis": os.environ.get("TEST_REDIS_URL"),
                "sdk": overmind.__version__ if hasattr(overmind, "__version__") else str(SDK),
            },
            "fixtures": {
                name: _digest(Path(__file__).parent / "agents" / name)
                for name in ("support_desk",)
                if "sample_agent" in item.funcargs
            },
            "llm": {
                "requests": [{"model": r.model, "url": r.url} for r in llm.requests] if llm else [],
                "unscripted": len(llm.unscripted) if llm else 0,
                "refused": list(llm.network.refused) if llm else [],
            },
            "tasks": {
                "failed": ledger.failed() if ledger else {},
                "scheduled": dict(ledger.scheduled) if ledger else {},
            },
            "error": str(report_for_record.longrepr)[-4000:] if failed else None,
        }
        RUNS.mkdir(parents=True, exist_ok=True)
        name = re.sub(r"[^\w.-]+", "_", item.nodeid)
        (RUNS / f"{name}.json").write_text(json.dumps(record, indent=2, default=str))
    return report


def _digest(path: Path) -> str:
    import hashlib

    digest = hashlib.sha256()
    for file in sorted(p for p in path.rglob("*") if p.is_file() and "__pycache__" not in p.parts):
        digest.update(file.relative_to(path).as_posix().encode())
        digest.update(file.read_bytes())
    return digest.hexdigest()[:16]


@pytest.fixture
def workshop(cli, mcp_for, sample_agent, worker, fake_llm, judges):
    fake_llm.extra_models.append("fake/candidate")
    fake_llm.on(lambda r: r.model == "fake/candidate", GOOD_REPLY)
    cli.scan(sample_agent)
    cli.sync(sample_agent)
    return mcp_for(cli.project_key(sample_agent))


@pytest.fixture
def clock():
    import time_machine

    traveller = time_machine.travel(datetime.now(UTC), tick=True)
    frozen = traveller.start()
    yield frozen
    traveller.stop()


@pytest.fixture
def beat(worker, clock):
    from overbae.celery import app

    def tick(*tasks: str) -> None:
        clock.shift(16)
        for task in tasks:
            app.send_task(task)
        drain(worker)

    return tick


@pytest.fixture(scope="session")
def replay_recorder(tmp_path_factory):
    bundle = tmp_path_factory.mktemp("replay-recorder") / "recorder.js"
    entry = bundle.with_suffix(".ts")
    rrweb = FRONTEND / "node_modules/posthog-js/dist/rrweb.js"
    entry.write_text(
        f"import {{ record }} from {json.dumps(str(rrweb))};window.ReplayRecorder = {{ record }};"
    )
    subprocess.run(
        ["bun", "build", str(entry), "--format=iife", "--outfile", str(bundle)],
        cwd=FRONTEND,
        check=True,
        capture_output=True,
        timeout=60,
    )
    return bundle


@pytest.fixture(scope="session")
def console_url(live_api, tmp_path_factory, worker_id):
    # xdist workers share this run's base temp parent; a serial run's parent outlives it.
    run = tmp_path_factory.getbasetemp()
    built = build_console((run if worker_id == "master" else run.parent) / "console")
    with serve_console(built, tmp_path_factory.mktemp("console"), live_api.url) as url:
        yield url


@pytest.fixture(scope="session")
def browser():
    browser = Browser()
    yield browser
    browser.close()


@pytest.fixture
def console(browser, console_url, account, settings, request) -> Console:
    settings.CORS_ALLOW_ALL_ORIGINS = True
    user, _key = account
    console = browser.page(console_url)
    console.goto("/login")
    console.fill("#local-email", user.email)
    console.fill("#local-password", PASSWORD)
    console.press("Continue")
    console.wait_until("() => !!localStorage.getItem('jwt_access')")
    console.goto("/onboarding")
    console._browser.call(
        lambda: console._page.get_by_role("heading", name="Agent", exact=True).wait_for()
    )
    yield console
    try:
        console.save_evidence(re.sub(r"[^\w.-]+", "_", request.node.nodeid))
        console.healthy()
    finally:
        console.close()