Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python
17.1 KB
Raw Blame
from __future__ import annotations

import asyncio
import os
import threading
from pathlib import Path

import pytest

from jiuwenswarm.server.runtime.agent_warm_pool import AgentWarmPool


class _FakeRootAgent:
    def __init__(self, *, fail: bool = False) -> None:
        self.fail = fail
        self.prepared: list[str] = []
        self.prepare_calls: list[dict] = []
        self.cleaned: list[str] = []

    async def prepare_session(self, *, session_id: str, **_kwargs) -> None:
        await asyncio.sleep(0)
        if self.fail:
            raise RuntimeError("prepare failed")
        self.prepare_calls.append(_kwargs)
        self.prepared.append(session_id)

    async def cleanup_session_runtime(self, session_id: str) -> bool:
        self.cleaned.append(session_id)
        return True


class _ControlledRootAgent(_FakeRootAgent):
    def __init__(self) -> None:
        super().__init__()
        self.started: list[str] = []
        self.cancelled: list[str] = []
        self.gates: dict[str, asyncio.Event] = {}

    async def prepare_session(self, *, session_id: str, **_kwargs) -> None:
        gate = asyncio.Event()
        self.gates[session_id] = gate
        self.started.append(session_id)
        try:
            await gate.wait()
        except asyncio.CancelledError:
            self.cancelled.append(session_id)
            raise
        self.prepared.append(session_id)


class _FakeManager:
    def __init__(self, agent: _FakeRootAgent) -> None:
        self.agent = agent
        self.pins = 0
        self.get_agent_calls: list[dict] = []

    async def get_agent(self, **kwargs):
        self.get_agent_calls.append(kwargs)
        return self.agent

    def pin_agent(self, _agent) -> None:
        self.pins += 1

    def unpin_agent(self, _agent) -> None:
        self.pins -= 1


async def _wait_until(predicate, *, attempts: int = 100) -> None:
    for _ in range(attempts):
        if predicate():
            return
        await asyncio.sleep(0.01)
    raise AssertionError("condition did not become true")


@pytest.fixture
def isolated_pool(monkeypatch, tmp_path: Path):
    monkeypatch.delenv("JIUWENSWARM_FLASH_ENABLED", raising=False)
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
        lambda: tmp_path,
    )
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.project_store.list_projects",
        lambda **_kwargs: [],
    )

    def factory(agent: _FakeRootAgent) -> AgentWarmPool:
        # Prewarming is off by default; stay explicit so a developer environment
        # that opts in cannot silently turn these cases into no-ops.
        return AgentWarmPool(_FakeManager(agent), max_concurrency=4, enabled=True)

    yield factory


def test_prewarm_is_disabled_unless_the_environment_opts_in(
    monkeypatch, tmp_path: Path
) -> None:
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
        lambda: tmp_path,
    )
    monkeypatch.delenv("JIUWENSWARM_EDITION", raising=False)

    def build() -> AgentWarmPool:
        return AgentWarmPool(_FakeManager(_FakeRootAgent()))

    monkeypatch.delenv("JIUWENSWARM_AGENT_PREWARM", raising=False)
    assert build()._enabled is False

    monkeypatch.setenv("JIUWENSWARM_AGENT_PREWARM", " OFF ")
    assert build()._enabled is False

    monkeypatch.setenv("JIUWENSWARM_AGENT_PREWARM", "1")
    assert build()._enabled is True


@pytest.mark.asyncio
async def test_disabled_pool_skips_foreground_bookkeeping(
    monkeypatch, tmp_path: Path
) -> None:
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
        lambda: tmp_path,
    )
    pool = AgentWarmPool(_FakeManager(_FakeRootAgent()), enabled=False)

    await pool.begin_foreground()
    await pool.end_foreground()

    assert pool._foreground_count == 0
    assert pool._foreground_idle.is_set()
    assert pool._background_pump_task is None
    await pool.close()


def test_prewarm_disabled_on_enterprise_even_if_env_opts_in(
    monkeypatch, tmp_path: Path
) -> None:
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
        lambda: tmp_path,
    )
    monkeypatch.setenv("JIUWENSWARM_EDITION", "enterprise")
    monkeypatch.setenv("JIUWENSWARM_AGENT_PREWARM", "1")
    pool = AgentWarmPool(_FakeManager(_FakeRootAgent()))
    assert pool._enabled is False


@pytest.mark.asyncio
async def test_disabled_pool_never_warms_and_always_bypasses(
    monkeypatch, tmp_path: Path
) -> None:
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
        lambda: tmp_path,
    )
    monkeypatch.setenv("JIUWENSWARM_AGENT_PREWARM", "0")
    agent = _FakeRootAgent()
    pool = AgentWarmPool(_FakeManager(agent))

    stats = await pool.sync(["web"], config={"model": "a"})
    assert stats == {"target": 0, "ready": 0, "warming": 0, "failed": 0, "stale": 0}

    key = pool.make_key(
        channel_id="web",
        project_id="default",
        project_dir="",
        work_mode="work",
    )
    claim = await pool.claim(key)
    assert claim.prewarm_hit is False
    assert claim.prewarm_status == "bypassed"
    assert claim.session_id.startswith("web_")
    assert not pool._slots
    assert not pool._tasks
    assert agent.prepared == []
    await pool.wait_for_session(claim.session_id)
    await pool.close()


def test_warm_key_normalizes_project_directory(tmp_path: Path) -> None:
    key = AgentWarmPool.make_key(
        channel_id=" web ",
        project_id="project-a",
        project_dir=str(tmp_path / ".." / tmp_path.name),
        work_mode="CODE",
    )
    assert key.channel_id == "web"
    assert key.project_dir == os.path.normcase(str(tmp_path.resolve()))
    assert key.agent_mode == "code"
    assert key.agent_sub_mode == "normal"


@pytest.mark.asyncio
async def test_code_prewarm_uses_same_manager_cache_identity_as_code_chat(
    isolated_pool,
) -> None:
    pool = isolated_pool(_FakeRootAgent())
    key = pool.make_key(
        channel_id="web",
        project_id="project-code",
        project_dir="/tmp/code-project",
        work_mode="code",
    )

    claim = await pool.claim(key)
    await pool.wait_for_session(claim.session_id)

    assert pool._manager.get_agent_calls == [
        {
            "channel_id": "web",
            "mode": "code",
            "project_dir": key.project_dir,
            "sub_mode": "normal",
        }
    ]
    await pool.end_foreground()
    await pool.close()


@pytest.mark.asyncio
async def test_flash_work_prewarm_uses_flash_adapter_and_business_mode(
    isolated_pool,
) -> None:
    agent = _FakeRootAgent()
    pool = isolated_pool(agent)
    await pool.sync(["web"], config={"flash": {"enabled": True}})
    await _wait_until(lambda: len(pool._slots) == 1)

    flash_key = pool.make_key(
        channel_id="web",
        project_id="default",
        project_dir="",
        work_mode="work",
        adapter_mode="flash",
    )
    assert flash_key in pool._slots
    assert pool._manager.get_agent_calls == [
        {
            "channel_id": "web",
            "mode": "flash",
            "project_dir": None,
            "sub_mode": None,
        }
    ]
    assert agent.prepare_calls == [
        {
            "channel_id": "web",
            "mode": "agent",
            "adapter_mode": "flash",
            "project_dir": None,
        }
    ]
    await pool.close()


@pytest.mark.asyncio
async def test_flash_reload_replaces_unclaimed_prewarm_slot_without_history_loss(
    isolated_pool,
) -> None:
    agent = _FakeRootAgent()
    pool = isolated_pool(agent)
    await pool.sync(["web"], config={"flash": {"enabled": True}})
    await _wait_until(lambda: len(pool._slots) == 1)
    flash_slot_ids = {slot.session_id for slot in pool._slots.values()}

    await pool.sync(["web"], config={"flash": {"enabled": False}})
    agent_key = pool.make_key(
        channel_id="web",
        project_id="default",
        project_dir="",
        work_mode="work",
        adapter_mode="agent",
    )
    await _wait_until(
        lambda: agent_key in pool._slots
        and not flash_slot_ids.intersection(
            slot.session_id for slot in pool._slots.values()
        )
    )

    assert [call["mode"] for call in pool._manager.get_agent_calls] == [
        "flash",
        "agent",
    ]
    assert [call["adapter_mode"] for call in agent.prepare_calls] == [
        "flash",
        "agent",
    ]
    assert flash_slot_ids.issubset(set(agent.cleaned))
    await pool.close()


@pytest.mark.asyncio
async def test_pool_keeps_one_ready_slot_total_and_claim_is_atomic(isolated_pool) -> None:
    agent = _FakeRootAgent()
    pool = isolated_pool(agent)
    stats = await pool.sync(["web"], config={"model": "a"})
    assert stats["target"] == 2
    await _wait_until(lambda: len(pool._slots) == 1)

    work_key = pool.make_key(
        channel_id="web",
        project_id="default",
        project_dir="",
        work_mode="work",
    )
    claims = await asyncio.gather(pool.claim(work_key), pool.claim(work_key))
    assert len({claim.session_id for claim in claims}) == 2
    assert sum(claim.prewarm_hit for claim in claims) == 1
    assert {claim.prewarm_status for claim in claims} == {"ready", "warming"}
    await pool.end_foreground()
    await _wait_until(lambda: work_key in pool._slots)
    assert len([key for key in pool._slots if key == work_key]) == 1
    assert len(pool._slots) == 1
    await pool.close()


@pytest.mark.asyncio
async def test_sync_filters_non_user_protocol_channels(isolated_pool) -> None:
    pool = isolated_pool(_FakeRootAgent())
    stats = await pool.sync(["web", "acp", "a2a"], config={"model": "a"})
    assert stats["target"] == 2
    assert pool._enabled_channels == {"web"}
    await pool.close()


@pytest.mark.asyncio
async def test_foreground_bypasses_background_and_pauses_lazy_dispatch(
    monkeypatch, tmp_path: Path
) -> None:
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
        lambda: tmp_path,
    )
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.project_store.list_projects",
        lambda **_kwargs: [],
    )
    agent = _ControlledRootAgent()
    pool = AgentWarmPool(_FakeManager(agent), max_concurrency=1, enabled=True)
    await pool.sync(["web"], config={"model": "a"})
    await _wait_until(lambda: len(agent.started) == 1)
    assert len(pool._tasks) == 1
    assert len(pool._pending) == 1

    await pool.begin_foreground()
    key = pool.make_key(
        channel_id="web",
        project_id="default",
        project_dir="",
        work_mode="work",
    )
    claim = await pool.claim(key)
    assert claim.prewarm_hit is False
    await _wait_until(lambda: len(agent.started) == 2)

    background_id, foreground_id = agent.started
    await _wait_until(lambda: background_id in agent.cancelled)
    assert len(agent.started) == 2

    agent.gates[foreground_id].set()
    await pool.wait_for_session(foreground_id)
    assert len(agent.started) == 2

    await pool.end_foreground()
    await _wait_until(lambda: len(agent.started) == 3)
    for gate in agent.gates.values():
        gate.set()
    await pool.close()


@pytest.mark.asyncio
async def test_claim_promotes_matching_background_task_without_duplicate_prepare(
    monkeypatch, tmp_path: Path
) -> None:
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
        lambda: tmp_path,
    )
    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.agent_warm_pool.project_store.list_projects",
        lambda **_kwargs: [],
    )
    agent = _ControlledRootAgent()
    pool = AgentWarmPool(_FakeManager(agent), max_concurrency=1, enabled=True)
    await pool.sync(["web"], config={"model": "a"})
    await _wait_until(lambda: len(agent.started) == 1)
    background_id = agent.started[0]
    key = pool.make_key(
        channel_id="web",
        project_id="default",
        project_dir="",
        work_mode="work",
    )

    claim = await pool.claim(key)

    assert claim.session_id == background_id
    assert claim.prewarm_hit is False
    assert claim.prewarm_status == "warming"
    assert len(agent.started) == 1
    agent.gates[background_id].set()
    await pool.wait_for_session(background_id)
    assert len(agent.started) == 1
    await pool.close()


@pytest.mark.asyncio
async def test_config_revision_replaces_unclaimed_slots(isolated_pool) -> None:
    agent = _FakeRootAgent()
    pool = isolated_pool(agent)
    await pool.sync(["web"], config={"model": "old"})
    await _wait_until(lambda: len(pool._slots) == 1)
    old_ids = {slot.session_id for slot in pool._slots.values()}

    await pool.sync(["web"], config={"model": "new"})
    await _wait_until(
        lambda: len(pool._slots) == 1
        and not old_ids.intersection(slot.session_id for slot in pool._slots.values())
    )
    assert old_ids.issubset(set(agent.cleaned))
    await pool.close()


@pytest.mark.asyncio
async def test_slow_older_sync_cannot_overwrite_newer_revision(
    isolated_pool, monkeypatch
) -> None:
    pool = isolated_pool(_FakeRootAgent())
    original_desired_keys = pool._desired_keys
    first_started = threading.Event()
    release_first = threading.Event()
    call_count = 0
    call_lock = threading.Lock()

    def _controlled_desired_keys(channels, config=None):
        nonlocal call_count
        with call_lock:
            call_count += 1
            call_number = call_count
        if call_number == 1:
            first_started.set()
            assert release_first.wait(timeout=5)
        return original_desired_keys(channels, config=config)

    monkeypatch.setattr(pool, "_desired_keys", _controlled_desired_keys)
    older = asyncio.create_task(pool.sync(["web"], config={"model": "old"}))
    await asyncio.wait_for(asyncio.to_thread(first_started.wait), timeout=1)
    await pool.sync(["web"], config={"model": "new"})
    release_first.set()
    await older

    assert pool._revision.config_fingerprint == pool.config_fingerprint(
        {"model": "new"}
    )
    await pool.close()


@pytest.mark.asyncio
async def test_failed_prepare_never_becomes_ready(isolated_pool) -> None:
    pool = isolated_pool(_FakeRootAgent(fail=True))
    await pool.sync(["web"], config={"model": "broken"})
    await _wait_until(lambda: not pool._tasks)
    stats = await pool.stats()
    assert stats["ready"] == 0
    assert stats["failed"] == 1
    assert stats["warming"] == 1
    await pool.close()


@pytest.mark.asyncio
async def test_claim_marker_survives_until_metadata_activation(isolated_pool) -> None:
    pool = isolated_pool(_FakeRootAgent())
    await pool.sync(["web"], config={"model": "ready"})
    await _wait_until(lambda: len(pool._slots) == 1)
    key = pool.make_key(
        channel_id="web",
        project_id="default",
        project_dir="",
        work_mode="work",
    )

    claim = await pool.claim(key)
    marker = pool._marker_path(claim.session_id)
    assert claim.prewarm_hit is True
    assert marker.is_file()

    pool.clear_marker(claim.session_id)
    assert not marker.exists()
    await pool.close()


def test_new_boot_cleans_only_metadata_less_marked_workspace(
    isolated_pool, tmp_path: Path
) -> None:
    old_pool = isolated_pool(_FakeRootAgent())
    key = old_pool.make_key(
        channel_id="web",
        project_id="default",
        project_dir="",
        work_mode="work",
    )
    stale_id = "web_stale"
    persisted_id = "web_persisted"
    for session_id in (stale_id, persisted_id):
        (tmp_path / session_id).mkdir()
        old_pool._write_marker(session_id, key)
    (tmp_path / persisted_id / "metadata.json").write_text("{}", encoding="utf-8")

    isolated_pool(_FakeRootAgent())

    assert not (tmp_path / stale_id).exists()
    assert (tmp_path / persisted_id / "metadata.json").is_file()


@pytest.mark.asyncio
@pytest.mark.parametrize("env_value,config_enabled,expected", [
    ("true", False, "flash"), ("false", True, "agent"), (None, True, "flash"),
])
@pytest.mark.parametrize("snapshot", ["raw", "resolved"])
async def test_prewarm_uses_same_flash_environment_as_requests(
    isolated_pool, monkeypatch, tmp_path, env_value, config_enabled, expected, snapshot,
):
    from jiuwenswarm.common import config as config_module

    config_file = tmp_path / "config.yaml"
    config_file.write_text(f"flash:\n  enabled: {str(config_enabled).lower()}\n", encoding="utf-8")
    monkeypatch.setattr(config_module, "get_config_file", lambda: config_file)
    config_module.clear_config_cache()
    monkeypatch.delenv("JIUWENSWARM_FLASH_ENABLED", raising=False)
    if env_value is not None:
        monkeypatch.setenv("JIUWENSWARM_FLASH_ENABLED", env_value)
    agent = _FakeRootAgent()
    pool = isolated_pool(agent)
    try:
        config = config_module.get_config_raw() if snapshot == "raw" else config_module.get_config()
        await pool.sync(["web"], config=config)
        await _wait_until(lambda: len(pool._slots) == 1)
        assert pool._manager.get_agent_calls[0]["mode"] == expected
        assert agent.prepare_calls[0]["adapter_mode"] == expected
        desired = pool._desired_keys(config=config)
        assert all(key.adapter_mode == "code" for key in desired if key.work_mode == "code")
    finally:
        await pool.close()
        config_module.clear_config_cache()