Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

10.0 KB
Raw Blame
from __future__ import annotations

import asyncio
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path

import httpx
import pytest

import host_agent_client as agent_client


def test_host_agent_url_usage_is_centralized():
    source_root = Path(__file__).resolve().parents[1]
    users = {
        path.relative_to(source_root).as_posix()
        for path in source_root.rglob("*.py")
        if "tests" not in path.parts and "AGENT_URL" in path.read_text(encoding="utf-8")
    }

    assert users == {"config.py", "host_agent_client.py", "main.py"}


def test_sync_client_is_singleton_with_bounded_limits(monkeypatch):
    created = []

    class _Client:
        is_closed = False

    def factory(**kwargs):
        created.append(kwargs)
        return _Client()

    monkeypatch.setattr(agent_client, "_sync_client", None)
    monkeypatch.setattr(agent_client.httpx, "Client", factory)
    with ThreadPoolExecutor(max_workers=16) as pool:
        clients = list(pool.map(lambda _: agent_client._get_sync_client(), range(32)))

    assert len(created) == 1
    assert all(client is clients[0] for client in clients)
    assert created[0]["trust_env"] is False
    assert created[0]["limits"].max_connections == 8
    assert created[0]["limits"].max_keepalive_connections == 4


@pytest.mark.asyncio
async def test_async_client_is_singleton_with_bounded_limits(monkeypatch):
    created = []

    class _Client:
        is_closed = False

    def factory(**kwargs):
        created.append(kwargs)
        return _Client()

    monkeypatch.setattr(agent_client, "_async_client", None)
    monkeypatch.setattr(agent_client.httpx, "AsyncClient", factory)
    clients = await asyncio.gather(*(agent_client._get_async_client() for _ in range(32)))

    assert len(created) == 1
    assert all(client is clients[0] for client in clients)
    assert created[0]["trust_env"] is False
    assert created[0]["limits"].max_connections == 2
    assert created[0]["limits"].max_keepalive_connections == 2


def test_async_client_is_recreated_after_its_event_loop_closes(monkeypatch):
    created = []

    class _Client:
        is_closed = False

    def factory(**_kwargs):
        client = _Client()
        created.append(client)
        return client

    async def get_client():
        return await agent_client._get_async_client()

    monkeypatch.setattr(agent_client, "_async_client", None)
    monkeypatch.setattr(agent_client.httpx, "AsyncClient", factory)
    first = asyncio.run(get_client())
    second = asyncio.run(get_client())

    assert first is not second
    assert created == [first, second]


def test_get_retries_one_stale_connection_and_uses_split_timeout(monkeypatch):
    calls = []

    def handler(request: httpx.Request) -> httpx.Response:
        calls.append(request)
        if len(calls) == 1:
            raise httpx.ReadError("stale keep-alive", request=request)
        return httpx.Response(200, json={"status": "ok"})

    client = httpx.Client(base_url="http://agent", transport=httpx.MockTransport(handler))
    monkeypatch.setattr(agent_client, "_sync_client", client)
    try:
        assert agent_client.request_json("GET", "/health", timeout=600) == {"status": "ok"}
        assert len(calls) == 2
        assert calls[-1].extensions["timeout"] == {
            "connect": 5.0,
            "read": 600.0,
            "write": 30.0,
            "pool": 5.0,
        }
    finally:
        client.close()


def test_post_never_retries_transport_failure(monkeypatch):
    calls = 0

    def handler(request: httpx.Request) -> httpx.Response:
        nonlocal calls
        calls += 1
        raise httpx.ReadError("connection lost", request=request)

    client = httpx.Client(base_url="http://agent", transport=httpx.MockTransport(handler))
    monkeypatch.setattr(agent_client, "_sync_client", client)
    try:
        with pytest.raises(agent_client.AgentUnavailable):
            agent_client.request_json("POST", "/v1/model/activate", payload={"model": "x"})
        assert calls == 1
    finally:
        client.close()


def test_post_retries_connect_failure_before_request_is_sent(monkeypatch):
    calls = 0
    sleeps = []

    def handler(request: httpx.Request) -> httpx.Response:
        nonlocal calls
        calls += 1
        if calls < 3:
            raise httpx.ConnectError(
                "[Errno 101] Network is unreachable", request=request
            )
        return httpx.Response(200, json={"status": "accepted"})

    client = httpx.Client(base_url="http://agent", transport=httpx.MockTransport(handler))
    monkeypatch.setattr(agent_client, "_sync_client", client)
    monkeypatch.setattr(agent_client.time, "sleep", sleeps.append)
    try:
        result = agent_client.request_json(
            "POST", "/v1/extension/install", payload={"service_id": "aider"}
        )
        assert result == {"status": "accepted"}
        assert calls == 3
        assert sleeps == list(agent_client._CONNECT_RETRY_DELAYS_SECONDS[:2])
    finally:
        client.close()


@pytest.mark.asyncio
async def test_async_post_retries_connect_failure_before_request_is_sent(monkeypatch):
    calls = 0
    sleeps = []

    def handler(request: httpx.Request) -> httpx.Response:
        nonlocal calls
        calls += 1
        if calls == 1:
            raise httpx.ConnectError(
                "[Errno 101] Network is unreachable", request=request
            )
        return httpx.Response(200, json={"status": "accepted"})

    async def fake_sleep(delay):
        sleeps.append(delay)

    client = httpx.AsyncClient(
        base_url="http://agent", transport=httpx.MockTransport(handler)
    )
    monkeypatch.setattr(agent_client, "_async_client", client)
    monkeypatch.setattr(agent_client.asyncio, "sleep", fake_sleep)
    try:
        result = await agent_client.async_request_json(
            "POST", "/v1/model/download", payload={"model": "x"}
        )
        assert result == {"status": "accepted"}
        assert calls == 2
        assert sleeps == [agent_client._CONNECT_RETRY_DELAYS_SECONDS[0]]
    finally:
        await client.aclose()


def test_post_does_not_retry_generic_connect_failure(monkeypatch):
    calls = 0

    def handler(request: httpx.Request) -> httpx.Response:
        nonlocal calls
        calls += 1
        raise httpx.ConnectError("connection refused", request=request)

    client = httpx.Client(base_url="http://agent", transport=httpx.MockTransport(handler))
    monkeypatch.setattr(agent_client, "_sync_client", client)
    try:
        with pytest.raises(agent_client.AgentUnavailable):
            agent_client.request_json(
                "POST", "/v1/extension/install", payload={"service_id": "aider"}
            )
        assert calls == 1
    finally:
        client.close()


@pytest.mark.parametrize(
    ("response", "error_type"),
    [
        (httpx.Response(409, json={"error": "busy"}), agent_client.AgentHTTPError),
        (httpx.Response(200, text="not-json"), agent_client.AgentProtocolError),
        (httpx.Response(200, json=["not", "an", "object"]), agent_client.AgentProtocolError),
    ],
)
def test_response_error_mapping(monkeypatch, response, error_type):
    def handler(request: httpx.Request) -> httpx.Response:
        response.request = request
        return response

    client = httpx.Client(base_url="http://agent", transport=httpx.MockTransport(handler))
    monkeypatch.setattr(agent_client, "_sync_client", client)
    try:
        with pytest.raises(error_type) as caught:
            agent_client.request_json("GET", "/v1/model/status")
        if isinstance(caught.value, agent_client.AgentHTTPError):
            assert caught.value.status_code == 409
            assert caught.value.detail == "busy"
    finally:
        client.close()


def test_timeout_is_distinct_from_unavailable(monkeypatch):
    def handler(request: httpx.Request) -> httpx.Response:
        raise httpx.ReadTimeout("slow", request=request)

    client = httpx.Client(base_url="http://agent", transport=httpx.MockTransport(handler))
    monkeypatch.setattr(agent_client, "_sync_client", client)
    try:
        with pytest.raises(agent_client.AgentTimeout):
            agent_client.request_json("GET", "/health")
    finally:
        client.close()


@pytest.mark.asyncio
async def test_async_request_and_shutdown_close_both_pools(monkeypatch):
    async_client = httpx.AsyncClient(
        base_url="http://agent",
        transport=httpx.MockTransport(lambda _request: httpx.Response(200, json={"ok": True})),
    )
    sync_client = httpx.Client(
        base_url="http://agent",
        transport=httpx.MockTransport(lambda _request: httpx.Response(200, json={"ok": True})),
    )
    monkeypatch.setattr(agent_client, "_async_client", async_client)
    monkeypatch.setattr(agent_client, "_sync_client", sync_client)

    assert await agent_client.async_request_json("GET", "/health") == {"ok": True}
    await agent_client.shutdown_clients()

    assert async_client.is_closed
    assert sync_client.is_closed
    assert agent_client._async_client is None
    assert agent_client._sync_client is None


def _agent_idle_socket_timeout() -> float:
    import ast

    agent = Path(__file__).resolve().parents[4] / "bin" / "ods-host-agent.py"
    tree = ast.parse(agent.read_text(encoding="utf-8"))
    for node in ast.walk(tree):
        if isinstance(node, ast.ClassDef) and node.name == "ThreadedHTTPServer":
            for stmt in node.body:
                if (
                    isinstance(stmt, ast.Assign)
                    and [getattr(t, "id", None) for t in stmt.targets] == ["request_socket_timeout"]
                ):
                    return float(ast.literal_eval(stmt.value))
    raise AssertionError("ThreadedHTTPServer.request_socket_timeout not found")


@pytest.mark.parametrize("limits", [agent_client._SYNC_LIMITS, agent_client._ASYNC_LIMITS])
def test_pooled_keepalive_expires_well_before_agent_idle_close(limits):
    # Equal timeouts let the pool reuse a socket the agent is closing; behind
    # Docker Desktop's proxy that close may never arrive and the GET hangs.
    assert limits.keepalive_expiry is not None
    assert limits.keepalive_expiry <= _agent_idle_socket_timeout() / 2