Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

6.9 KB
Raw Blame
"""Issue #935 regression tests for legacy WebSocket task execution."""

from __future__ import annotations

import asyncio
import threading
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock, Mock

import pytest
from sqlalchemy import event

from xagent.web.api import websocket as websocket_api
from xagent.web.models.database import get_engine
from xagent.web.models.task import Task, TaskStatus
from xagent.web.models.user import User
from xagent.web.services import agent_service_manager as agent_runtime_service
from xagent.web.services import task_command_execution as command_execution_service
from xagent.web.services import task_orchestrator as orchestrator_module
from xagent.web.services.task_orchestrator import (
    TaskTurnPayload,
)

from .conftest import _direct_db_session


@pytest.mark.asyncio
async def test_legacy_execute_uses_worker_snapshot_and_primitive_scheduler_boundary(
    _test_db: None,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """No request Session or ORM row may survive into async execution."""
    db = _direct_db_session()
    try:
        user = User(username="legacy-execute-owner", password_hash="hash")
        db.add(user)
        db.commit()
        task = Task(
            user_id=int(user.id),
            title="Legacy execution",
            description="Run the existing task",
            status=TaskStatus.PENDING,
            execution_mode="balanced",
            process_description="Follow the saved process",
            examples=[{"input": "a", "output": "b"}],
            source="internal",
            agent_config={"runtime_extension_bindings": ["local_browser"]},
        )
        db.add(task)
        db.commit()
        task_id = int(task.id)
        user_id = int(user.id)
    finally:
        db.close()

    # The pre-fix handler directly calls these collaborators while retaining
    # its request Session. Keeping them harmless lets the assertion below
    # fail specifically because the primitive scheduler boundary is absent.
    agent_service = MagicMock()
    agent_service.set_outbound_message_handler = MagicMock()
    agent_service.set_execution_context_messages = MagicMock()
    agent_service.set_recovered_skill_context = MagicMock()
    agent_manager = MagicMock()
    agent_manager.get_agent_for_task = AsyncMock(return_value=agent_service)
    agent_manager.execute_task = AsyncMock(
        return_value={"success": True, "output": "done", "file_outputs": []}
    )
    monkeypatch.setattr(
        agent_runtime_service, "get_agent_manager", lambda: agent_manager
    )

    connection_manager = MagicMock()
    connection_manager.send_personal_message = AsyncMock()
    connection_manager.broadcast_to_task = AsyncMock()
    monkeypatch.setattr(websocket_api, "manager", connection_manager)

    async def completed_execution() -> None:
        return None

    main_thread_id = threading.get_ident()
    database_thread_ids: list[int] = []
    checked_out_connections = 0

    def record_database_thread(*_args: object) -> None:
        database_thread_ids.append(threading.get_ident())

    def record_checkout(*_args: object) -> None:
        nonlocal checked_out_connections
        checked_out_connections += 1

    def record_checkin(*_args: object) -> None:
        nonlocal checked_out_connections
        checked_out_connections -= 1

    scheduled_background = asyncio.create_task(completed_execution())

    def schedule_at_closed_session_boundary(**_kwargs: object) -> asyncio.Task:
        assert checked_out_connections == 0
        return scheduled_background

    schedule = Mock(side_effect=schedule_at_closed_session_boundary)
    monkeypatch.setattr(orchestrator_module, "_schedule_bg", schedule)

    engine = get_engine()
    event.listen(engine, "before_cursor_execute", record_database_thread)
    event.listen(engine, "checkout", record_checkout)
    event.listen(engine, "checkin", record_checkin)  # codespell:ignore checkin
    try:
        await websocket_api.handle_execute_task(
            MagicMock(),
            task_id,
            {"user": SimpleNamespace(id=user_id, is_admin=False)},
        )
        # The handler only accepts the command; the dispatcher schedules it.
        # Wait here, with the listeners still attached, so the dispatcher's own
        # database I/O is checked too.
        for _ in range(100):
            if schedule.call_count:
                break
            await asyncio.sleep(0.05)
    finally:
        event.remove(engine, "before_cursor_execute", record_database_thread)
        event.remove(engine, "checkout", record_checkout)
        event.remove(engine, "checkin", record_checkin)  # codespell:ignore checkin

    assert database_thread_ids
    assert all(thread_id != main_thread_id for thread_id in database_thread_ids)
    schedule.assert_called_once()
    schedule_call = schedule.call_args
    assert schedule_call.kwargs["task_id"] == task_id
    assert schedule_call.kwargs["task_owner_user_id"] == user_id
    assert schedule_call.kwargs["task_source"] == "internal"
    assert schedule_call.kwargs["context"] == {
        "execution_mode": "balanced",
        "process_description": "Follow the saved process",
        "examples": [{"input": "a", "output": "b"}],
    }
    payload = schedule_call.kwargs["payload"]
    assert isinstance(payload, TaskTurnPayload)
    assert payload.transcript_message == "Run the existing task"
    assert payload.execution_message == "Run the existing task"

    # The legacy protocol still emits the immediate acknowledgement and
    # task-info event before waiting for the scheduled execution to finish.
    connection_manager.send_personal_message.assert_awaited_once()
    assert (
        connection_manager.send_personal_message.await_args.args[0]["type"]
        == "execution_started"
    )
    task_info = connection_manager.broadcast_to_task.await_args_list[0].args[0]
    assert task_info["event_type"] == "task_info"
    assert task_info["data"]["id"] == task_id
    assert task_info["data"]["status"] == TaskStatus.PENDING.value
    assert task_info["data"]["runtime_extension_bindings"] == ["local_browser"]


def test_websocket_task_info_exposes_persisted_runtime_extension_bindings(
    _test_db: None,
) -> None:
    db = _direct_db_session()
    try:
        user = User(username="runtime-binding-owner", password_hash="hash")
        db.add(user)
        db.commit()
        task = Task(
            user_id=int(user.id),
            title="Runtime-bound task",
            description="Use the selected computer",
            status=TaskStatus.COMPLETED,
            source="internal",
            agent_config={"runtime_extension_bindings": ["local_browser"]},
        )
        db.add(task)
        db.commit()
        db.refresh(task)

        routing = command_execution_service._load_task_command_routing_snapshot(
            db, task
        )

        assert routing.task_info["runtime_extension_bindings"] == ["local_browser"]
    finally:
        db.close()