Skip to content

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

11.1 KB
Raw Blame
from __future__ import annotations

from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
from unittest.mock import ANY, AsyncMock, Mock

import pytest
from fastapi import FastAPI

from tests.shared.db_teardown import drop_all_tables
from xagent.web import app as app_module
from xagent.web.models.agent import Agent
from xagent.web.models.database import (
    Base,
    configure_db,
    get_engine,
    get_session_local,
)
from xagent.web.models.trigger import AgentTrigger, TriggerType
from xagent.web.models.user import User
from xagent.web.startup_admission import register_host_startup_admission


def _patch_runtime_starts(
    monkeypatch: pytest.MonkeyPatch,
    events: list[str],
) -> None:
    monkeypatch.setattr(
        app_module, "register_local_browser_runtime", lambda: events.append("runtime")
    )

    from xagent.web.services.task_execution import background_task_manager

    monkeypatch.setattr(
        background_task_manager,
        "start_accepting",
        lambda: events.append("task admission"),
    )
    for name, event in (
        ("start_file_storage_startup_sync_task", "file sync"),
        ("start_trigger_dispatcher_task", "trigger dispatcher"),
        ("start_task_lease_recovery_task", "lease recovery"),
        ("start_task_auto_resume_task", "auto resume"),
        ("start_uploaded_file_recovery_task", "file recovery"),
        ("start_orphan_upload_gc_task", "upload gc"),
    ):
        monkeypatch.setattr(
            app_module,
            name,
            lambda _app, event=event, **_kw: events.append(event),
        )


@pytest.mark.asyncio
async def test_no_host_admission_preserves_runtime_startup_order(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    test_app = FastAPI()
    events: list[str] = []
    monkeypatch.setattr(app_module, "init_db", lambda: events.append("database"))
    _patch_runtime_starts(monkeypatch, events)

    await app_module._initialize_database_and_admit_runtime(test_app)

    assert events == [
        "database",
        "runtime",
        "task admission",
        "file sync",
        "trigger dispatcher",
        "lease recovery",
        "auto resume",
        "file recovery",
        "upload gc",
    ]


@pytest.mark.asyncio
async def test_host_admissions_run_after_database_and_before_runtime(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    test_app = FastAPI()
    events: list[str] = []
    monkeypatch.setattr(app_module, "init_db", lambda: events.append("database"))
    _patch_runtime_starts(monkeypatch, events)

    async def first() -> None:
        events.append("first admission")

    async def second() -> None:
        events.append("second admission")

    register_host_startup_admission(test_app, first)
    register_host_startup_admission(test_app, second)

    await app_module._initialize_database_and_admit_runtime(test_app)

    assert events[:5] == [
        "database",
        "first admission",
        "second admission",
        "runtime",
        "task admission",
    ]


@pytest.mark.asyncio
async def test_host_admission_stops_at_first_error_and_propagates_it(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    test_app = FastAPI()
    events: list[str] = []
    rejection = RuntimeError("host rejected startup")
    monkeypatch.setattr(app_module, "init_db", lambda: events.append("database"))
    _patch_runtime_starts(monkeypatch, events)

    async def first() -> None:
        events.append("first admission")

    async def reject() -> None:
        events.append("rejected admission")
        raise rejection

    async def never_runs() -> None:
        events.append("late admission")

    register_host_startup_admission(test_app, first)
    register_host_startup_admission(test_app, reject)
    register_host_startup_admission(test_app, never_runs)

    with pytest.raises(RuntimeError) as raised:
        await app_module._initialize_database_and_admit_runtime(test_app)

    assert raised.value is rejection
    assert events == ["database", "first admission", "rejected admission"]


@pytest.mark.asyncio
async def test_failed_admission_with_due_trigger_launches_no_background_work(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path,
) -> None:
    database_url = f"sqlite:///{tmp_path / 'startup-admission.db'}"
    configure_db(database_url)
    Base.metadata.create_all(bind=get_engine())
    SessionLocal = get_session_local()
    with SessionLocal() as db:
        user = User(username="admission-test", password_hash="hash")
        db.add(user)
        db.flush()
        agent = Agent(user_id=user.id, name="Admission test agent")
        db.add(agent)
        db.flush()
        due_at = datetime.now(timezone.utc) - timedelta(minutes=1)
        trigger = AgentTrigger(
            user_id=user.id,
            agent_id=agent.id,
            type=TriggerType.SCHEDULED.value,
            name="Due before startup",
            config={"interval_seconds": 60},
            next_run_at=due_at,
        )
        db.add(trigger)
        db.commit()
        trigger_id = trigger.id

    test_app = FastAPI()
    runtime_starts: list[str] = []
    observed_due_trigger: list[int] = []

    def initialize_existing_database() -> None:
        configure_db(database_url)

    async def reject_after_checking_database() -> None:
        SessionLocal = get_session_local()
        with SessionLocal() as db:
            due = (
                db.query(AgentTrigger)
                .filter(
                    AgentTrigger.id == trigger_id,
                    AgentTrigger.next_run_at <= datetime.now(timezone.utc),
                )
                .one()
            )
            observed_due_trigger.append(due.id)
        raise RuntimeError("admission denied")

    def unexpected_create_task(coroutine):
        coroutine.close()
        raise AssertionError("startup launched a background task before admission")

    monkeypatch.setattr(app_module, "app", test_app)
    monkeypatch.setattr(app_module, "init_db", initialize_existing_database)
    monkeypatch.setattr(
        app_module, "validate_interaction_rollout_at_startup", lambda: None
    )
    monkeypatch.setattr(app_module.asyncio, "create_task", unexpected_create_task)
    _patch_runtime_starts(monkeypatch, runtime_starts)
    register_host_startup_admission(test_app, reject_after_checking_database)

    try:
        with pytest.raises(RuntimeError, match="admission denied"):
            await app_module.startup_event()

        assert observed_due_trigger == [trigger_id]
        assert runtime_starts == []
        with SessionLocal() as db:
            persisted = db.get(AgentTrigger, trigger_id)
            assert persisted is not None
            assert persisted.last_run_at is None
    finally:
        drop_all_tables(get_engine())


@pytest.mark.asyncio
async def test_unconfigured_shared_execution_refuses_startup_before_database(
    monkeypatch,
):
    monkeypatch.setenv("XAGENT_SHARED_TASK_EXECUTION_ENABLED", "true")
    monkeypatch.delenv("XAGENT_REDIS_URL", raising=False)
    initialize = AsyncMock()
    monkeypatch.setattr(
        app_module, "_initialize_database_and_admit_runtime", initialize
    )
    with pytest.raises(ValueError, match="requires XAGENT_REDIS_URL"):
        await app_module.startup_event()
    initialize.assert_not_awaited()


@pytest.mark.asyncio
async def test_invalid_auth_configuration_refuses_startup_before_database(
    monkeypatch,
):
    initialize = AsyncMock()
    monkeypatch.setattr(
        app_module, "_initialize_database_and_admit_runtime", initialize
    )

    def reject_auth_configuration():
        raise RuntimeError("invalid auth configuration")

    monkeypatch.setattr(app_module, "validate_auth_config", reject_auth_configuration)

    with pytest.raises(RuntimeError, match="invalid auth configuration"):
        await app_module.startup_event()

    initialize.assert_not_awaited()


@pytest.mark.asyncio
async def test_trace_configuration_failure_stops_ingress_and_names_phase(
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    from xagent.web.services import trace_database

    events: list[str] = []
    test_app = FastAPI()
    monkeypatch.setattr(app_module, "init_db", lambda: events.append("database"))
    _patch_runtime_starts(monkeypatch, events)
    rejection = RuntimeError("invalid trace configuration")

    def reject():
        raise rejection

    monkeypatch.setattr(trace_database, "get_trace_database_runtime", reject)
    log = Mock()
    monkeypatch.setattr(app_module, "logger", log)
    with pytest.raises(RuntimeError) as raised:
        await app_module._initialize_database_and_admit_runtime(test_app)
    assert raised.value is rejection
    assert events == ["database"]
    log.error.assert_called_once_with(
        "startup phase failed: %s (after %.2fs)", "trace database init", ANY
    )


@pytest.mark.asyncio
async def test_invalid_conversation_storage_rollout_refuses_startup_before_database(
    monkeypatch,
):
    monkeypatch.setenv("XAGENT_EXECUTION_EVENT_STORAGE_ENTRIES", "not_an_entry")
    initialize = AsyncMock()
    monkeypatch.setattr(
        app_module, "_initialize_database_and_admit_runtime", initialize
    )
    from xagent.web.services.conversation_storage_rollout import (
        ConversationStorageConfigError,
    )

    with pytest.raises(ConversationStorageConfigError, match="not_an_entry"):
        await app_module.startup_event()

    initialize.assert_not_awaited()


@pytest.mark.asyncio
@pytest.mark.parametrize("telemetry_task", [None, object()])
async def test_startup_reports_unobserved_execution_events_with_telemetry_flag(
    monkeypatch, telemetry_task
):
    from xagent.skills import utils as skill_utils
    from xagent.templates import utils as template_utils
    from xagent.web.services import chrome_mcp_runtime  # noqa: F401

    manager = SimpleNamespace(
        initialize=AsyncMock(), list_skills=AsyncMock(return_value=[])
    )
    templates = SimpleNamespace(
        initialize=AsyncMock(), list_templates=AsyncMock(return_value=[])
    )
    test_app = FastAPI()
    monkeypatch.setattr(app_module, "app", test_app)
    monkeypatch.setattr(skill_utils, "create_skill_manager", lambda: manager)
    monkeypatch.setattr(template_utils, "create_template_manager", lambda: templates)
    monkeypatch.setattr(
        "xagent.core.tools.core.RAG_tools.storage.vector_backend."
        "lock_deployment_kb_engine",
        lambda: None,
    )
    monkeypatch.setattr(
        app_module, "_initialize_database_and_admit_runtime", AsyncMock()
    )
    monkeypatch.setattr(app_module, "_run_or_defer", AsyncMock())
    monkeypatch.setattr(app_module, "get_sandbox_manager", lambda: None, raising=False)
    monkeypatch.setattr("xagent.web.sandbox_manager.get_sandbox_manager", lambda: None)
    monkeypatch.setattr(app_module, "initialize_langfuse", lambda: None)
    monkeypatch.setattr(app_module, "start_app_process_diagnostics", lambda _app: None)

    def fake_monitor(app_instance):
        app_instance.state.runtime_performance_task = telemetry_task

    monkeypatch.setattr(app_module, "start_runtime_performance_monitor", fake_monitor)
    report = Mock()
    monkeypatch.setattr(app_module, "report_unobserved_execution_events", report)

    await app_module.startup_event()

    report.assert_called_once_with(telemetry_started=telemetry_task is not None)