Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

8.4 KB
Raw Blame
from __future__ import annotations

import asyncio
import os
import sys
import threading
import time
from pathlib import Path
from types import SimpleNamespace

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from core.runtime_commands import (
    _ERR_STALE_AGE_SECONDS,
    RuntimeCommandWatcher,
)


def _make_watcher(tmp_path: Path, handler) -> tuple[RuntimeCommandWatcher, Path]:
    """Build a watcher whose controller's auth service exposes ``handler``."""
    auth_service = SimpleNamespace(_refresh_backend_runtime=handler)
    controller = SimpleNamespace(agent_auth_service=auth_service)
    watcher = RuntimeCommandWatcher(controller, directory=tmp_path)  # type: ignore[arg-type]
    marker = tmp_path / "restart-opencode.cmd"
    marker.write_text('{"backend":"opencode"}', encoding="utf-8")
    return watcher, marker


def test_handle_restart_success_deletes_marker_no_err(tmp_path: Path) -> None:
    """Happy path: marker deleted, no ``.err`` companion."""

    async def handler(name: str) -> None:
        assert name == "opencode"

    watcher, marker = _make_watcher(tmp_path, handler)
    asyncio.run(watcher._handle_restart("opencode", marker))

    assert not marker.exists()
    assert not (tmp_path / "restart-opencode.cmd.err").exists()


def test_handle_restart_reads_marker_metadata(tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:
    async def handler(name: str) -> None:
        assert name == "codex"

    auth_service = SimpleNamespace(_refresh_backend_runtime=handler)
    controller = SimpleNamespace(agent_auth_service=auth_service)
    watcher = RuntimeCommandWatcher(controller, directory=tmp_path)  # type: ignore[arg-type]
    marker = tmp_path / "restart-codex.abc123.cmd"
    marker.write_text(
        '{"backend":"codex","metadata":{"reason":"manual_backend_restart","route":"/api/backend/codex/restart"}}',
        encoding="utf-8",
    )

    caplog.set_level("INFO")
    asyncio.run(watcher._handle_restart("codex", marker))

    assert not marker.exists()
    assert any("manual_backend_restart" in record.message for record in caplog.records)


def test_handle_restart_failure_writes_err_sentinel(tmp_path: Path) -> None:
    """Handler raises → marker deleted AND ``.err`` companion written."""

    async def handler(name: str) -> None:
        raise RuntimeError("transport teardown failed mid-way")

    watcher, marker = _make_watcher(tmp_path, handler)
    asyncio.run(watcher._handle_restart("opencode", marker))

    assert not marker.exists()
    err = tmp_path / "restart-opencode.cmd.err"
    assert err.exists()
    assert "transport teardown failed mid-way" in err.read_text()


def test_handle_restart_missing_handler_writes_err_sentinel(tmp_path: Path) -> None:
    """No ``_refresh_backend_runtime`` on the service → also surface failure."""
    auth_service = SimpleNamespace()  # no handler attribute
    controller = SimpleNamespace(agent_auth_service=auth_service)
    watcher = RuntimeCommandWatcher(controller, directory=tmp_path)  # type: ignore[arg-type]
    marker = tmp_path / "restart-codex.cmd"
    marker.write_text("{}", encoding="utf-8")

    asyncio.run(watcher._handle_restart("codex", marker))

    assert not marker.exists()
    err = tmp_path / "restart-codex.cmd.err"
    assert err.exists()
    assert "refresh handler unavailable" in err.read_text()


def test_sweep_accepts_claude_restart_marker(tmp_path: Path) -> None:
    calls = []

    async def handler(name: str) -> None:
        calls.append(name)

    auth_service = SimpleNamespace(_refresh_backend_runtime=handler)
    controller = SimpleNamespace(agent_auth_service=auth_service)
    watcher = RuntimeCommandWatcher(controller, directory=tmp_path)  # type: ignore[arg-type]
    marker = tmp_path / "restart-claude.cmd"
    marker.write_text("{}", encoding="utf-8")

    asyncio.run(watcher._sweep_once())

    assert calls == ["claude"]
    assert not marker.exists()


def test_handle_restart_err_message_truncated(tmp_path: Path) -> None:
    """Pathological multi-MB exception messages must not blow up the disk."""

    async def handler(name: str) -> None:
        raise RuntimeError("x" * 5000)

    watcher, marker = _make_watcher(tmp_path, handler)
    asyncio.run(watcher._handle_restart("opencode", marker))

    err = tmp_path / "restart-opencode.cmd.err"
    assert err.exists()
    assert len(err.read_text()) <= 1024


def test_sweep_preserves_fresh_err_companion(tmp_path: Path) -> None:
    """Regression for P2: sweep must not delete a fresh ``.err`` before the requester reads it.

    Reproduces the race where the controller writes ``<marker>.cmd.err`` to
    signal a failure, then the next sweep tick deletes it because it does
    not match ``_MARKER_PATTERN``. If the UI server's
    ``_wait_for_controller_ack`` polls *after* that sweep, it sees marker
    absent + no ``.err`` and reports success — a false positive.
    """
    controller = SimpleNamespace(agent_auth_service=SimpleNamespace())
    watcher = RuntimeCommandWatcher(controller, directory=tmp_path)  # type: ignore[arg-type]
    err_marker = tmp_path / "restart-codex.abc123.cmd.err"
    err_marker.write_text("refresh failed", encoding="utf-8")

    asyncio.run(watcher._sweep_once())

    assert err_marker.exists(), ".err companion deleted by sweep — caller would see false success"
    assert err_marker.read_text(encoding="utf-8") == "refresh failed"


def test_sweep_evicts_orphaned_err_companion(tmp_path: Path) -> None:
    """An ``.err`` older than the stale window is safely past every caller and may be cleaned up."""
    controller = SimpleNamespace(agent_auth_service=SimpleNamespace())
    watcher = RuntimeCommandWatcher(controller, directory=tmp_path)  # type: ignore[arg-type]
    err_marker = tmp_path / "restart-codex.dead.cmd.err"
    err_marker.write_text("ancient", encoding="utf-8")
    # Backdate the mtime well past the eviction window so the sweep treats
    # it as orphaned (the original requester is long gone).
    old = time.time() - _ERR_STALE_AGE_SECONDS - 60
    os.utime(err_marker, (old, old))

    asyncio.run(watcher._sweep_once())

    assert not err_marker.exists()


def test_a_slow_directory_listing_never_holds_the_controller_loop(tmp_path: Path, monkeypatch) -> None:
    """#2768: the sweep reads the state disk off the loop, so a slow disk delays only the sweep.

    On a USB state disk the listing alone held the controller loop for 2.6 s,
    and with it every Session.
    """

    calls: list[str] = []

    async def handler(name: str) -> None:
        calls.append(name)

    watcher, marker = _make_watcher(tmp_path, handler)
    listing = threading.Event()
    loop_answered = threading.Event()
    answered_during_listing: list[bool] = []
    real_iterdir = Path.iterdir

    def slow_iterdir(self):
        if self == tmp_path:
            listing.set()
            answered_during_listing.append(loop_answered.wait(2))
        return real_iterdir(self)

    monkeypatch.setattr(Path, "iterdir", slow_iterdir)

    async def scenario() -> None:
        sweep = asyncio.create_task(watcher._sweep_once())
        while not listing.is_set():
            await asyncio.sleep(0.01)
        # The loop is free to serve other work while the listing waits on the disk.
        loop_answered.set()
        await sweep

    asyncio.run(asyncio.wait_for(scenario(), 10))

    assert answered_during_listing == [True]
    assert calls == ["opencode"]
    assert not marker.exists()


def test_a_marker_withdrawn_during_an_earlier_refresh_is_not_run(tmp_path: Path) -> None:
    """A requester whose acknowledgement wait expired while another backend refreshed removed its marker.

    The sweep found both markers before either refresh ran; the withdrawn one
    must not run after the first refresh finishes.
    """

    markers = {name: tmp_path / f"restart-{name}.cmd" for name in ("claude", "opencode")}
    calls: list[str] = []

    async def handler(name: str) -> None:
        calls.append(name)
        for other, marker in markers.items():
            if other != name:
                marker.unlink(missing_ok=True)

    auth_service = SimpleNamespace(_refresh_backend_runtime=handler)
    watcher = RuntimeCommandWatcher(SimpleNamespace(agent_auth_service=auth_service), directory=tmp_path)  # type: ignore[arg-type]
    for marker in markers.values():
        marker.write_text("{}", encoding="utf-8")

    asyncio.run(watcher._sweep_once())

    assert len(calls) == 1
    assert not any(marker.exists() for marker in markers.values())