Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

57.6 KB
Raw Blame
import asyncio
import copy
from datetime import datetime
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock

import pytest
from vikingbot.agent import loop as loop_module
from vikingbot.agent.context import ContextBuilder
from vikingbot.agent.loop import AgentLoop
from vikingbot.agent.tools.base import MultimodalToolResult
from vikingbot.agent.tools.registry import ToolExecutionResult
from vikingbot.bus.events import InboundMessage, OutboundEventType
from vikingbot.bus.queue import MessageBus
from vikingbot.config.schema import Config, SessionKey
from vikingbot.hooks.builtins.openviking_hooks import OpenVikingCompactHook
from vikingbot.openviking_mount.session_state import make_openviking_storage_session_id
from vikingbot.providers.base import LLMProvider, LLMResponse, ToolCallRequest
from vikingbot.session.manager import SessionManager


class _FakeProvider(LLMProvider):
    async def chat(self, *args, **kwargs):  # pragma: no cover - should not be called
        raise AssertionError("provider.chat should not be called in no-reply outcome test")

    def get_default_model(self) -> str:
        return "fake-model"


class _FakeSubagentManager:
    def __init__(self, **kwargs):
        self.kwargs = kwargs


class _RecordingProvider(LLMProvider):
    def __init__(self):
        super().__init__()
        self.calls = []

    async def chat(self, *args, **kwargs):
        self.calls.append((args, kwargs))
        return LLMResponse(content="ok")

    def get_default_model(self) -> str:
        return "fake-model"


class _FakeLangfuseClient:
    def __init__(self):
        self.calls = []

    def update_generation_metadata(self, response_id, metadata):
        self.calls.append((response_id, metadata))
        return metadata

    def update_response_outcome(self, response_id, outcome_label, outcome_payload):
        self.calls.append((response_id, outcome_label, outcome_payload))
        return outcome_payload


class _FakeOVClient:
    def __init__(self, *, context_payload):
        self.context_payload = context_payload
        self.context_calls = []

    async def get_session_context(self, session_id, token_budget):
        self.context_calls.append((session_id, token_budget))
        return self.context_payload


class _BlockingFirstProvider(_RecordingProvider):
    def __init__(self):
        super().__init__()
        self.started = asyncio.Event()

    async def chat(self, *args, **kwargs):
        self.calls.append((args, kwargs))
        if len(self.calls) == 1:
            self.started.set()
            await asyncio.Future()
        return LLMResponse(content="ok")


@pytest.fixture
def make_loop(temp_dir, monkeypatch):
    """Construct a real loop with external integrations stubbed for these tests."""
    monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
    monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)

    def create(*, config=None, provider=None, bus=None, register_tools=False, **kwargs):
        with monkeypatch.context() as setup:
            if not register_tools:
                setup.setattr(AgentLoop, "_register_default_tools", lambda self: None)
            return AgentLoop(
                bus=bus if bus is not None else MessageBus(),
                provider=provider if provider is not None else _FakeProvider(),
                workspace=temp_dir / "workspace",
                config=config if config is not None else Config(storage_workspace=str(temp_dir)),
                **kwargs,
            )

    return create


def _image_call(call_id, label=None):
    return ToolCallRequest(
        id=call_id, name="read_image", arguments={"label": label} if label else {}, tokens=1
    )


class _MediaProvider(_FakeProvider):
    def __init__(self, rounds, *, supports_media=True):
        super().__init__()
        self.calls = []
        self.responses = iter(
            [LLMResponse(content=None, tool_calls=calls) for calls in rounds]
            + [LLMResponse(content="done")]
        )
        self.supports_media = supports_media

    async def chat(self, messages, **kwargs):
        self.calls.append(copy.deepcopy(messages))
        return next(self.responses)

    def supports_tool_result_media(self, model=None):
        return self.supports_media


class _ImageRegistry:
    def __init__(
        self,
        content=None,
        *,
        preserve_complete_result=False,
        legacy_outcome=False,
    ):
        self.content = content
        self.preserve_complete_result = preserve_complete_result
        self.legacy_outcome = legacy_outcome

    def get_definitions(self, **kwargs):
        return [
            {
                "type": "function",
                "function": {
                    "name": "read_image",
                    "description": "Read an image",
                    "parameters": {"type": "object", "properties": {}},
                },
            }
        ]

    async def execute_detailed(self, name, params, **kwargs):
        label = params.get("label", "Image resource.")
        content = (
            self.content
            if self.content is not None
            else [
                {"type": "text", "text": label},
                {"type": "image_url", "image_url": {"url": "data:image/png;base64,ZGF0YQ=="}},
            ]
        )
        result = MultimodalToolResult(text=label, content=content)
        if self.legacy_outcome:
            return SimpleNamespace(result=result, effective_params=params, skill_uris=())
        return ToolExecutionResult(
            result=result,
            effective_params=params,
            preserve_complete_result=self.preserve_complete_result,
        )


class _SteerProvider(LLMProvider):
    def __init__(self):
        super().__init__()
        self.calls = []

    async def chat(self, messages, tools=None, **kwargs):
        self.calls.append(copy.deepcopy(messages))
        if len(self.calls) == 1:
            return LLMResponse(
                content="checking",
                tool_calls=[
                    ToolCallRequest(
                        id="call-1",
                        name="wait_for_steer",
                        arguments={},
                        tokens=1,
                    )
                ],
            )
        return LLMResponse(content="steered final")

    def get_default_model(self) -> str:
        return "fake-model"


class _BlockingSteerRegistry:
    def __init__(self):
        self.started = asyncio.Event()
        self.release = asyncio.Event()

    def get_definitions(self, **kwargs):
        return [
            {
                "type": "function",
                "function": {
                    "name": "wait_for_steer",
                    "description": "Wait until the test releases the tool.",
                    "parameters": {"type": "object", "properties": {}},
                },
            }
        ]

    async def execute(self, tool_name, arguments, **kwargs):
        self.started.set()
        await self.release.wait()
        return "tool result"


async def _consume_outbound_event(
    bus: MessageBus, event_type: OutboundEventType, timeout: float = 2
) -> object:
    deadline = asyncio.get_running_loop().time() + timeout
    while True:
        remaining = deadline - asyncio.get_running_loop().time()
        if remaining <= 0:
            raise TimeoutError(f"Timed out waiting for outbound event {event_type}")
        outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=remaining)
        if outbound.event_type == event_type:
            return outbound


async def _consume_outbound_until_response(bus: MessageBus, timeout: float = 2) -> list[object]:
    events = []
    deadline = asyncio.get_running_loop().time() + timeout
    while True:
        remaining = deadline - asyncio.get_running_loop().time()
        if remaining <= 0:
            raise TimeoutError("Timed out waiting for response event")
        outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=remaining)
        events.append(outbound)
        if outbound.event_type == OutboundEventType.RESPONSE:
            return events


def test_context_keeps_multimodal_tool_result_on_tool_message(temp_dir: Path):
    context = ContextBuilder(workspace=temp_dir / "workspace")
    content = [
        {"type": "text", "text": "Source: viking://resources/image.png"},
        {
            "type": "image_url",
            "image_url": {"url": "data:image/png;base64,aW1hZ2U="},
        },
    ]
    messages = context.add_tool_result(
        [],
        "call-1",
        "openviking_multi_read",
        MultimodalToolResult(text="Image resource.", content=content),
    )

    assert messages == [
        {
            "role": "tool",
            "tool_call_id": "call-1",
            "name": "openviking_multi_read",
            "content": content,
        }
    ]


@pytest.mark.asyncio
async def test_context_build_messages_can_skip_system_prompt(temp_dir: Path):
    context = ContextBuilder(workspace=temp_dir / "workspace", eval=True)

    messages = await context.build_messages(
        history=[],
        current_message="steer with image",
        media=[{"type": "image_url", "image_url": {"url": "https://example.com/image.png"}}],
        session_key=SessionKey(type="cli", channel_id="default", chat_id="steer-context"),
        ov_tools_enable=False,
        include_system=False,
    )

    assert [message["role"] for message in messages] == ["user", "user"]
    assert messages[-1]["content"] == [
        {"type": "image_url", "image_url": {"url": "https://example.com/image.png"}},
        {"type": "text", "text": "steer with image"},
    ]


@pytest.mark.asyncio
@pytest.mark.parametrize("supports_media", [False, True])
async def test_agent_loop_gates_multimodal_result_by_provider(make_loop, supports_media: bool):
    content = [
        {"type": "text", "text": "Source: viking://resources/image.png"},
        {
            "type": "image_url",
            "image_url": {"url": "data:image/png;base64,aW1hZ2U="},
        },
    ]

    provider = _MediaProvider([[_image_call("call-1")]], supports_media=supports_media)
    loop = make_loop(provider=provider, max_iterations=2)

    final, _reasoning, tools_used, _usage, _iteration = await loop._run_agent_loop(
        messages=[{"role": "user", "content": "read it"}],
        session_key=SessionKey(type="cli", channel_id="default", chat_id="multimodal"),
        publish_events=False,
        tool_registry=_ImageRegistry(content),
    )

    assert final == "done"
    tool_message = next(message for message in provider.calls[1] if message["role"] == "tool")
    if supports_media:
        assert tool_message["content"] == content
    else:
        assert tool_message["content"].startswith("Image resource.")
        assert "does not support media in tool results" in tool_message["content"]
    assert tools_used[0]["result"] == "Image resource."


@pytest.mark.asyncio
async def test_agent_loop_honors_tool_result_preservation_policy(make_loop, monkeypatch):
    provider = _MediaProvider([[_image_call("call-1")]])
    loop = make_loop(provider=provider, max_iterations=2)
    preview = AsyncMock(side_effect=lambda result, *_args, **_kwargs: result)
    monkeypatch.setattr(loop, "_preview_tool_result", preview)

    final, *_ = await loop._run_agent_loop(
        messages=[{"role": "user", "content": "read it"}],
        session_key=SessionKey(type="cli", channel_id="default", chat_id="result-policy"),
        publish_events=False,
        tool_registry=_ImageRegistry(preserve_complete_result=True),
    )

    assert final == "done"
    assert preview.await_args.kwargs["preserve_complete"] is True


@pytest.mark.asyncio
async def test_agent_loop_accepts_legacy_detailed_outcome(make_loop, monkeypatch):
    provider = _MediaProvider([[_image_call("call-1")]])
    loop = make_loop(provider=provider, max_iterations=2)
    preview = AsyncMock(side_effect=lambda result, *_args, **_kwargs: result)
    monkeypatch.setattr(loop, "_preview_tool_result", preview)

    final, *_ = await loop._run_agent_loop(
        messages=[{"role": "user", "content": "read it"}],
        session_key=SessionKey(type="cli", channel_id="default", chat_id="legacy-outcome"),
        publish_events=False,
        tool_registry=_ImageRegistry(legacy_outcome=True),
    )

    assert final == "done"
    assert preview.await_args.kwargs["preserve_complete"] is False


@pytest.mark.asyncio
async def test_agent_loop_limits_media_across_parallel_tool_results(make_loop, monkeypatch):
    monkeypatch.setattr(loop_module, "MAX_INLINE_TOOL_RESULT_MEDIA_BYTES", 5)

    provider = _MediaProvider([[_image_call("call-1", "first"), _image_call("call-2", "second")]])
    loop = make_loop(provider=provider, max_iterations=2)

    final, *_ = await loop._run_agent_loop(
        messages=[{"role": "user", "content": "read both"}],
        session_key=SessionKey(type="cli", channel_id="default", chat_id="media-budget"),
        publish_events=False,
        tool_registry=_ImageRegistry(),
    )

    assert final == "done"
    tool_messages = [message for message in provider.calls[1] if message["role"] == "tool"]
    assert isinstance(tool_messages[0]["content"], list)
    assert isinstance(tool_messages[1]["content"], str)
    assert "make this model request exceed" in tool_messages[1]["content"]


@pytest.mark.asyncio
async def test_agent_loop_prefers_new_media_across_consecutive_tool_rounds(make_loop, monkeypatch):
    monkeypatch.setattr(loop_module, "MAX_INLINE_TOOL_RESULT_MEDIA_BYTES", 5)

    provider = _MediaProvider(
        [[_image_call("call-1", "image-1")], [_image_call("call-2", "image-2")]]
    )
    loop = make_loop(provider=provider, max_iterations=3)

    final, *_ = await loop._run_agent_loop(
        messages=[{"role": "user", "content": "read two images in sequence"}],
        session_key=SessionKey(type="cli", channel_id="default", chat_id="media-rounds"),
        publish_events=False,
        tool_registry=_ImageRegistry(),
    )

    assert final == "done"
    first_round_tool = next(message for message in provider.calls[1] if message["role"] == "tool")
    assert isinstance(first_round_tool["content"], list)

    tool_messages = [message for message in provider.calls[-1] if message["role"] == "tool"]
    assert isinstance(tool_messages[0]["content"], str)
    assert "earlier tool result was omitted" in tool_messages[0]["content"]
    assert isinstance(tool_messages[1]["content"], list)


def test_agent_loop_omits_spawn_tool_when_subagents_disabled(make_loop, temp_dir: Path):
    bus = MessageBus()
    provider = _RecordingProvider()
    config = Config(storage_workspace=str(temp_dir), agents={"subagent_enabled": False})

    loop = make_loop(
        bus=bus,
        provider=provider,
        model=config.agents.model,
        temperature=config.agents.temperature,
        config=config,
        register_tools=True,
    )

    assert "spawn" not in loop.tools.tool_names


def test_agent_loop_standalone_omits_openviking_tools(make_loop, temp_dir: Path):
    config = Config(storage_workspace=str(temp_dir))
    loop = make_loop(provider=_RecordingProvider(), config=config, register_tools=True)

    assert config.ov_server.server_url == ""
    assert not any(name.startswith("openviking_") for name in loop.tools.tool_names)
    session_key = SessionKey(type="cli", channel_id="default", chat_id="standalone")
    assert loop._get_ov_tools_enable(session_key) is False


@pytest.mark.asyncio
async def test_agent_loop_passes_configured_temperature_to_provider(make_loop, temp_dir: Path):
    bus = MessageBus()
    provider = _RecordingProvider()
    config = Config(storage_workspace=str(temp_dir), agents={"temperature": 0.2})
    loop = make_loop(
        bus=bus,
        provider=provider,
        model=config.agents.model,
        temperature=config.agents.temperature,
        config=config,
    )

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-1")
    response, _, _ = await loop._chat_with_stream_events(
        messages=[{"role": "user", "content": "hello"}],
        tools=[],
        session_key=session_key,
        publish_events=False,
    )

    assert response.content == "ok"
    assert provider.calls[0][1]["temperature"] == 0.2
    assert loop.subagents.kwargs["temperature"] == 0.2


@pytest.mark.asyncio
async def test_agent_loop_interrupt_does_not_cancel_final_session_save(
    make_loop, temp_dir: Path, monkeypatch
):
    bus = MessageBus()
    config = Config(storage_workspace=str(temp_dir))
    loop = make_loop(bus=bus, config=config)
    session_key = SessionKey(type="cli", channel_id="default", chat_id="final-save")
    msg = InboundMessage(session_key=session_key, sender_id="user-1", content="finish cleanly")
    save_started = asyncio.Event()
    release_save = asyncio.Event()
    save_calls = 0
    original_save = loop.sessions.save

    async def fake_run_agent_loop(self, **kwargs):
        return "final answer", None, [], {"prompt_tokens": 1, "completion_tokens": 1}, 1

    async def blocking_save(session):
        nonlocal save_calls
        save_calls += 1
        if len(session.messages) >= 2 and session.messages[-1].get("role") == "assistant":
            save_started.set()
            await release_save.wait()
        await original_save(session)

    fake_langfuse = _FakeLangfuseClient()
    monkeypatch.setattr(AgentLoop, "_run_agent_loop", fake_run_agent_loop)
    monkeypatch.setattr(loop.sessions, "save", blocking_save)
    monkeypatch.setattr(
        "vikingbot.agent.loop.LangfuseClient.get_instance",
        staticmethod(lambda: fake_langfuse),
    )

    task = asyncio.create_task(loop._process_message_as_active_turn(msg))
    try:
        await asyncio.wait_for(save_started.wait(), timeout=2)

        assert await loop.interrupt(session_key) is False

        release_save.set()
        response = await asyncio.wait_for(task, timeout=2)

        assert response is not None
        assert response.content == "final answer"
        session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
        assert [message["role"] for message in session.messages] == ["user", "assistant"]
        assert [message["content"] for message in session.messages] == [
            "finish cleanly",
            "final answer",
        ]
        assert save_calls == 1
    finally:
        release_save.set()
        if not task.done():
            task.cancel()
            await asyncio.gather(task, return_exceptions=True)


@pytest.mark.asyncio
async def test_agent_loop_interrupt_marks_active_and_current_queued_messages(make_loop):
    bus = MessageBus()
    provider = _BlockingFirstProvider()
    loop = make_loop(bus=bus, provider=provider)
    session_key = SessionKey(type="cli", channel_id="default", chat_id="interrupt-count")

    run_task = asyncio.create_task(loop.run())
    try:
        await bus.publish_inbound(
            InboundMessage(session_key=session_key, sender_id="user-1", content="active")
        )
        await asyncio.wait_for(provider.started.wait(), timeout=2)
        for content in ("first queued", "second queued"):
            await bus.publish_inbound(
                InboundMessage(session_key=session_key, sender_id="user-1", content=content)
            )

        assert await loop.interrupt(session_key) is True
        await bus.publish_inbound(
            InboundMessage(session_key=session_key, sender_id="user-1", content="after interrupt")
        )

        for _ in range(3):
            await _consume_outbound_event(bus, OutboundEventType.INTERRUPT)
        response = await _consume_outbound_event(bus, OutboundEventType.RESPONSE)

        session = loop.sessions.get_or_create(session_key)
        assert response.content == "ok"
        assert [message["content"] for message in session.messages] == [
            "active",
            "first queued",
            "second queued",
            "after interrupt",
            "ok",
        ]
        assert len(provider.calls) == 2
    finally:
        run_task.cancel()
        await asyncio.gather(run_task, return_exceptions=True)


@pytest.mark.asyncio
async def test_agent_loop_steers_same_session_input_into_next_tool_turn(make_loop, temp_dir: Path):
    bus = MessageBus()
    provider = _SteerProvider()
    tools = _BlockingSteerRegistry()
    loop = make_loop(
        bus=bus,
        provider=provider,
        config=Config(storage_workspace=str(temp_dir)),
        max_iterations=3,
    )
    loop.tools = tools
    session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-session")

    run_task = asyncio.create_task(loop.run())
    try:
        await bus.publish_inbound(
            InboundMessage(session_key=session_key, sender_id="user-1", content="first request")
        )
        await asyncio.wait_for(tools.started.wait(), timeout=2)
        assert (
            loop.steer(
                InboundMessage(session_key=session_key, sender_id="user-1", content="steer update")
            )
            is True
        )

        tools.release.set()

        events = await _consume_outbound_until_response(bus)
        assert [event.event_type for event in events[:4]] == [
            OutboundEventType.ITERATION,
            OutboundEventType.TOOL_CALL,
            OutboundEventType.TOOL_RESULT,
            OutboundEventType.ITERATION,
        ]
        response = events[-1]

        assert response.content == "steered final"
        assert len(provider.calls) == 2
        second_call_user_text = "\n".join(
            str(message.get("content"))
            for message in provider.calls[1]
            if message.get("role") == "user"
        )
        assert "first request" in second_call_user_text
        assert "steer update" in second_call_user_text

        session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
        assert [message["role"] for message in session.messages] == [
            "user",
            "user",
            "assistant",
        ]
        assert [message["content"] for message in session.messages] == [
            "first request",
            "steer update",
            "steered final",
        ]
        assert bus.inbound_size == 0
    finally:
        tools.release.set()
        run_task.cancel()
        await asyncio.gather(run_task, return_exceptions=True)


@pytest.mark.asyncio
async def test_agent_loop_steers_same_session_input_before_final_response(
    make_loop, temp_dir: Path, monkeypatch
):
    bus = MessageBus()
    session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-final-boundary")
    provider_calls = []

    class _LateSteerProvider(_FakeProvider):
        async def chat(self, messages, tools=None, **kwargs):
            provider_calls.append(copy.deepcopy(messages))
            if len(provider_calls) == 1:
                assert (
                    loop.steer(
                        InboundMessage(
                            session_key=session_key,
                            sender_id="user-1",
                            content="late steer",
                        )
                    )
                    is True
                )
                return LLMResponse(content="first final")
            assert (
                loop.steer(
                    InboundMessage(
                        session_key=session_key,
                        sender_id="user-1",
                        content="too late",
                    )
                )
                is False
            )
            return LLMResponse(content="steered final")

    loop = make_loop(
        bus=bus,
        provider=_LateSteerProvider(),
        config=Config(storage_workspace=str(temp_dir)),
        max_iterations=2,
    )

    async def prepare_steer(steer_msg):
        return loop_module._PreparedSteer(
            msg=steer_msg,
            prompt_messages=[{"role": "user", "content": steer_msg.content}],
            newly_recalled_exp_uris=[],
            newly_recalled_exp_content="",
            auto_memory_tool={
                "tool_name": "auto_memory_search",
                "result": "recalled memory",
            },
        )

    monkeypatch.setattr(loop, "_prepare_steer_message", prepare_steer)

    run_task = asyncio.create_task(loop.run())
    try:
        await bus.publish_inbound(
            InboundMessage(session_key=session_key, sender_id="user-1", content="first request")
        )

        events = await _consume_outbound_until_response(bus)
        assert [event.event_type for event in events[:4]] == [
            OutboundEventType.ITERATION,
            OutboundEventType.ITERATION,
            OutboundEventType.TOOL_CALL,
            OutboundEventType.TOOL_RESULT,
        ]
        response = events[-1]

        assert response.content == "steered final"
        assert len(provider_calls) == 2
        final_call_user_text = "\n".join(
            str(message.get("content"))
            for message in provider_calls[1]
            if message.get("role") == "user"
        )
        assert "first request" in final_call_user_text
        assert "late steer" in final_call_user_text

        session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
        assert [message["content"] for message in session.messages] == [
            "first request",
            "first final",
            "late steer",
            "steered final",
        ]
        assert bus.inbound_size == 0
    finally:
        run_task.cancel()
        await asyncio.gather(run_task, return_exceptions=True)


@pytest.mark.asyncio
async def test_agent_loop_does_not_share_steers_with_concurrent_internal_loop(
    make_loop,
):
    provider = _BlockingFirstProvider()
    loop = make_loop(provider=provider, max_iterations=2)
    session_key = SessionKey(type="cli", channel_id="default", chat_id="concurrent-loop")
    active_task = asyncio.create_task(
        loop._process_message_as_active_turn(
            InboundMessage(session_key=session_key, sender_id="user-1", content="first request")
        )
    )
    try:
        await asyncio.wait_for(provider.started.wait(), timeout=2)
        assert loop.steer(
            InboundMessage(session_key=session_key, sender_id="user-1", content="steer update")
        )

        assert await loop.process_direct("internal task", session_key=session_key) == "ok"

        active = loop._active_turn
        assert active is not None
        assert active.accepting_steer is True
        assert [steer.msg.content for steer in active.accepted_steers] == ["steer update"]
    finally:
        active_task.cancel()
        await asyncio.gather(active_task, return_exceptions=True)


@pytest.mark.asyncio
async def test_agent_loop_interrupt_preserves_steers_during_batched_prepare(make_loop, monkeypatch):
    loop = make_loop()
    session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-batch-interrupt")
    first_started = asyncio.Event()
    first_finished = asyncio.Event()
    release_first = asyncio.Event()
    second_started = asyncio.Event()

    async def prepare(steer_msg):
        if steer_msg.content == "first steer":
            first_started.set()
            await release_first.wait()
            first_finished.set()
        else:
            second_started.set()
            await asyncio.Future()
        return loop_module._PreparedSteer(
            msg=steer_msg,
            prompt_messages=[{"role": "user", "content": steer_msg.content}],
            newly_recalled_exp_uris=[],
            newly_recalled_exp_content="",
        )

    monkeypatch.setattr(loop, "_prepare_steer_message", prepare)

    async def process(_msg, active_turn=None):
        assert loop.steer(
            InboundMessage(session_key=session_key, sender_id="user-1", content="first steer")
        )

        async def add_second_steer():
            await first_started.wait()
            assert loop.steer(
                InboundMessage(session_key=session_key, sender_id="user-1", content="second steer")
            )
            release_first.set()

        asyncio.create_task(add_second_steer())
        await loop._take_accepted_steers(active_turn)

    monkeypatch.setattr(loop, "_process_message", process)
    task = asyncio.create_task(
        loop._process_message_as_active_turn(
            InboundMessage(session_key=session_key, sender_id="user-1", content="first request")
        )
    )
    try:
        await asyncio.wait_for(second_started.wait(), timeout=2)
        await asyncio.wait_for(first_finished.wait(), timeout=2)
        await asyncio.sleep(0)
        assert await loop.interrupt(session_key)
        response = await task
        assert response is not None
        assert response.event_type == OutboundEventType.INTERRUPT
        session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
        assert [message["content"] for message in session.messages] == [
            "first request",
            "first steer",
            "second steer",
        ]
    finally:
        release_first.set()
        if not task.done():
            task.cancel()
            await asyncio.gather(task, return_exceptions=True)


@pytest.mark.asyncio
async def test_agent_loop_interrupt_preserves_active_steer_input(
    make_loop, temp_dir: Path, monkeypatch
):
    bus = MessageBus()
    provider = _SteerProvider()
    tools = _BlockingSteerRegistry()
    loop = make_loop(
        bus=bus,
        provider=provider,
        config=Config(storage_workspace=str(temp_dir)),
        max_iterations=3,
    )
    loop.tools = tools
    session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-interrupt-early")
    original_build_messages = ContextBuilder.build_messages

    async def recall_experience(self, *args, **kwargs):
        messages = await original_build_messages(self, *args, **kwargs)
        self.latest_recalled_exp_uris = ["viking://agent/experience/interrupted"]
        self.latest_recalled_exp_content = "interrupted experience"
        return messages

    monkeypatch.setattr(ContextBuilder, "build_messages", recall_experience)

    run_task = asyncio.create_task(loop.run())
    try:
        await bus.publish_inbound(
            InboundMessage(session_key=session_key, sender_id="user-1", content="first request")
        )
        await asyncio.wait_for(tools.started.wait(), timeout=2)
        assert (
            loop.steer(
                InboundMessage(session_key=session_key, sender_id="user-1", content="steer update")
            )
            is True
        )
        active = loop._active_turn
        assert active is not None
        steer_task = active.accepted_steers[0].task

        assert await loop.interrupt(session_key) is True

        interrupted = await _consume_outbound_event(bus, OutboundEventType.INTERRUPT)

        assert interrupted.content == ""
        session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
        assert [message["role"] for message in session.messages] == ["user", "user"]
        assert [message["content"] for message in session.messages] == [
            "first request",
            "steer update",
        ]
        assert "recalled_exp_uris" not in session.metadata
        assert bus.inbound_size == 0
        assert active.accepted_steers == []
        assert steer_task.done()
    finally:
        tools.release.set()
        run_task.cancel()
        await asyncio.gather(run_task, return_exceptions=True)


@pytest.mark.asyncio
async def test_agent_loop_error_preserves_accepted_steer_input(make_loop, monkeypatch):
    loop = make_loop()
    session_key = SessionKey(type="cli", channel_id="default", chat_id="steer-error")
    started = asyncio.Event()
    release = asyncio.Event()

    async def fail_message(_msg, active_turn=None):
        started.set()
        await release.wait()
        raise RuntimeError("processing failed")

    monkeypatch.setattr(loop, "_process_message", fail_message)
    task = asyncio.create_task(
        loop._process_message_as_active_turn(
            InboundMessage(session_key=session_key, sender_id="user-1", content="first request")
        )
    )
    try:
        await asyncio.wait_for(started.wait(), timeout=2)
        assert loop.steer(
            InboundMessage(session_key=session_key, sender_id="user-1", content="steer update")
        )
        active = loop._active_turn
        assert active is not None
        steer_task = active.accepted_steers[0].task

        release.set()
        with pytest.raises(RuntimeError, match="processing failed"):
            await task

        session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
        assert [message["content"] for message in session.messages] == [
            "first request",
            "steer update",
        ]
        assert active.accepted_steers == []
        assert steer_task.done()
    finally:
        release.set()
        if not task.done():
            task.cancel()
            await asyncio.gather(task, return_exceptions=True)


@pytest.mark.asyncio
async def test_agent_loop_makes_final_no_tool_call_when_iteration_limit_reached(
    make_loop, temp_dir: Path
):
    class _ToolLimitProvider(LLMProvider):
        def __init__(self):
            super().__init__()
            self.calls = []

        async def chat(self, messages, tools=None, **kwargs):
            self.calls.append(
                {
                    "messages": [dict(message) for message in messages],
                    "tools": list(tools or []),
                    "kwargs": kwargs,
                }
            )
            if len(self.calls) == 1:
                return LLMResponse(
                    content="Let me check these sources.",
                    tool_calls=[
                        ToolCallRequest(
                            id="call-1",
                            name="lookup_fact",
                            arguments={"query": "current facts 1"},
                            tokens=3,
                        ),
                        ToolCallRequest(
                            id="call-2",
                            name="lookup_fact",
                            arguments={"query": "current facts 2"},
                            tokens=3,
                        ),
                        ToolCallRequest(
                            id="call-3",
                            name="lookup_fact",
                            arguments={"query": "current facts 3"},
                            tokens=3,
                        ),
                    ],
                    usage={"prompt_tokens": 10, "completion_tokens": 2, "total_tokens": 12},
                )
            return LLMResponse(
                content="final answer from gathered tool results",
                usage={"prompt_tokens": 7, "completion_tokens": 5, "total_tokens": 12},
            )

        def get_default_model(self) -> str:
            return "fake-model"

    class _ToolRegistry:
        def __init__(self):
            self.execute_calls = []

        def get_definitions(self, **kwargs):
            return [
                {
                    "type": "function",
                    "function": {
                        "name": "lookup_fact",
                        "description": "Lookup fact",
                        "parameters": {"type": "object", "properties": {}},
                    },
                }
            ]

        async def execute(self, tool_name, arguments, **kwargs):
            self.execute_calls.append((tool_name, arguments, kwargs))
            return "tool result: useful context"

    provider = _ToolLimitProvider()
    tools = _ToolRegistry()
    bus = MessageBus()
    config = Config(storage_workspace=str(temp_dir))
    loop = make_loop(bus=bus, provider=provider, config=config, max_iterations=1)
    loop.tools = tools

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-limit")
    captured_turns = []
    final_content, _reasoning, tools_used, token_usage, iteration = await loop._run_agent_loop(
        messages=[{"role": "user", "content": "please answer with lookup"}],
        session_key=session_key,
        publish_events=False,
        captured_turns=captured_turns,
    )

    assert final_content == "final answer from gathered tool results"
    assert iteration == 1
    assert len(provider.calls) == 2
    assert provider.calls[0]["tools"]
    assert provider.calls[1]["tools"] == []
    assert provider.calls[1]["messages"][-1]["content"].startswith(
        "Tool-use iteration limit reached."
    )
    assert any(
        message.get("content") == "tool result: useful context"
        for message in provider.calls[1]["messages"]
    )
    assert len(tools.execute_calls) == 3
    assert tools.execute_calls[0][:2] == ("lookup_fact", {"query": "current facts 1"})
    assert [tool["tool_name"] for tool in tools_used] == [
        "lookup_fact",
        "lookup_fact",
        "lookup_fact",
    ]
    assert len(captured_turns) == 1
    assert captured_turns[0]["content"] == "Let me check these sources."
    assert [tool["tool_call_id"] for tool in captured_turns[0]["tool_calls"]] == [
        "call-1",
        "call-2",
        "call-3",
    ]
    assert [tool["result"] for tool in captured_turns[0]["tool_calls"]] == [
        "tool result: useful context",
        "tool result: useful context",
        "tool result: useful context",
    ]
    assert {
        key: token_usage[key] for key in ("prompt_tokens", "completion_tokens", "total_tokens")
    } == {"prompt_tokens": 17, "completion_tokens": 7, "total_tokens": 24}


@pytest.mark.asyncio
async def test_agent_loop_evaluates_previous_response_outcome_before_openviking_precommit_clear(
    make_loop, temp_dir: Path, monkeypatch
):
    async def fake_run_agent_loop(self, **kwargs):
        return "final answer", None, [], {"prompt_tokens": 1, "completion_tokens": 1}, 1

    fake_langfuse = _FakeLangfuseClient()
    monkeypatch.setattr(AgentLoop, "_run_agent_loop", fake_run_agent_loop)
    monkeypatch.setattr(
        "vikingbot.agent.loop.LangfuseClient.get_instance",
        staticmethod(lambda: fake_langfuse),
    )

    bus = MessageBus()
    config = Config(
        storage_workspace=str(temp_dir),
        agents={
            "session_context_enabled": True,
            "commit_token_threshold": 1,
            "commit_keep_recent_count": 0,
        },
    )
    loop = make_loop(bus=bus, config=config)

    async def fake_precommit(session, msg):
        session.clear()
        await loop.sessions.save(session)

    monkeypatch.setattr(loop, "_maybe_commit_openviking_before_turn", fake_precommit)

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-precommit-clear")
    session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
    session.add_message(
        "assistant",
        "hello",
        sender_id="user-1",
        response_id="resp-123",
        timestamp="2026-04-30T00:00:00",
    )
    await loop.sessions.save(session)

    await loop._process_message(
        InboundMessage(
            session_key=session_key,
            sender_id="user-1",
            content="that did not help",
            timestamp=datetime.fromisoformat("2026-04-30T00:05:00"),
        )
    )

    outcome_event = await bus.consume_outbound()
    assert outcome_event.event_type == OutboundEventType.RESPONSE_OUTCOME_EVALUATED
    assert outcome_event.response_id == "resp-123"
    persisted_session = SessionManager(config.bot_data_path).get_or_create(
        session_key, skip_heartbeat=True
    )
    assert persisted_session.metadata["response_outcomes"]["resp-123"]["outcome_label"] == "reasked"


@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_uses_ov_context_plus_unsynced_tail(
    make_loop, temp_dir: Path, monkeypatch
):
    fake_ov_client = _FakeOVClient(
        context_payload={
            "latest_archive_overview": "Earlier summary",
            "messages": [
                {"role": "user", "content": "OV user turn"},
                {"role": "assistant", "parts": [{"type": "text", "text": "OV assistant turn"}]},
            ],
        }
    )

    monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))

    bus = MessageBus()
    config = Config(
        storage_workspace=str(temp_dir),
        ov_server={"server_url": "http://127.0.0.1:1933"},
        agents={"session_context_enabled": True, "session_context_token_budget": 321},
    )
    loop = make_loop(bus=bus, config=config)

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-ov-history")
    session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
    session.add_message("user", "local synced user")
    session.add_message("assistant", "local synced assistant")
    session.add_message("user", "local unsynced user")
    session.add_message("assistant", "local unsynced assistant")
    session.metadata["openviking"] = {
        "session_id": "ov-session-1",
        "last_synced_local_index": 1,
    }

    history = await loop._build_prompt_history(session)

    assert fake_ov_client.context_calls == [("ov-session-1", 321)]
    assert [message["content"] for message in history] == [
        "[Earlier conversation summary]\nEarlier summary",
        "OV user turn",
        "OV assistant turn",
        "local unsynced user",
        "local unsynced assistant",
    ]


@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_falls_back_to_loaded_local_history_when_ov_empty(
    make_loop, temp_dir: Path, monkeypatch
):
    fake_ov_client = _FakeOVClient(context_payload={"messages": []})

    monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))

    config = Config(
        storage_workspace=str(temp_dir),
        ov_server={"server_url": "http://127.0.0.1:1933"},
        agents={"session_context_enabled": True, "session_context_token_budget": 321},
    )
    loop = make_loop(config=config)

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-local-fallback")
    session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
    session.add_message("user", "persisted user")
    session.add_message("assistant", "persisted assistant")
    session.add_message("user", "current question")
    session.metadata["openviking"] = {
        "session_id": "ov-session-missing-context",
        "last_synced_local_index": 1,
    }
    await loop.sessions.save(session)

    restarted_sessions = SessionManager(config.bot_data_path)
    loaded_session = restarted_sessions.get_or_create(session_key, skip_heartbeat=True)
    history = await loop._build_prompt_history(loaded_session)

    assert fake_ov_client.context_calls == [("ov-session-missing-context", 321)]
    assert [message["content"] for message in history] == [
        "persisted user",
        "persisted assistant",
        "current question",
    ]
    assert loaded_session.metadata["openviking"]["last_synced_local_index"] == 1


@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_skips_tail_when_sync_cursor_is_past_local_messages(
    make_loop, temp_dir: Path, monkeypatch
):
    fake_ov_client = _FakeOVClient(
        context_payload={"messages": [{"role": "user", "content": "OV user turn"}]}
    )

    monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))

    bus = MessageBus()
    config = Config(
        storage_workspace=str(temp_dir),
        ov_server={"server_url": "http://127.0.0.1:1933"},
        agents={"session_context_enabled": True, "session_context_token_budget": 321},
    )
    loop = make_loop(bus=bus, config=config)

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-ov-cursor-past")
    session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
    session.add_message("user", "local user")
    session.add_message("assistant", "local assistant")
    session.metadata["openviking"] = {
        "session_id": "ov-session-1",
        "last_synced_local_index": 20,
    }

    history = await loop._build_prompt_history(session)

    assert [message["content"] for message in history] == ["OV user turn"]


@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_enforces_token_budget_for_live_tool_outputs(
    make_loop, temp_dir: Path, monkeypatch
):
    tool_output = "x" * 10_000
    fake_ov_client = _FakeOVClient(
        context_payload={
            "messages": [
                {"role": "user", "parts": [{"type": "text", "text": "original query"}]},
                *[
                    {
                        "role": "assistant",
                        "parts": [
                            {"type": "text", "text": f"turn {index}"},
                            {"type": "tool", "tool_output": tool_output},
                        ],
                    }
                    for index in range(10)
                ],
                {"role": "assistant", "parts": [{"type": "text", "text": "final answer"}]},
            ]
        }
    )

    monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))

    loop = make_loop(
        config=Config(
            storage_workspace=str(temp_dir),
            ov_server={"server_url": "http://127.0.0.1:1933"},
            agents={"session_context_enabled": True, "session_context_token_budget": 3000},
        )
    )
    session = loop.sessions.get_or_create(
        SessionKey(type="cli", channel_id="default", chat_id="session-large-tools"),
        skip_heartbeat=True,
    )
    session.metadata["openviking"] = {
        "session_id": "ov-session-large-tools",
        "last_synced_local_index": -1,
    }

    history = await loop._build_prompt_history(session)

    assert fake_ov_client.context_calls == [("ov-session-large-tools", 3000)]
    assert sum(loop._history_message_tokens(message) for message in history) <= 3000
    assert history[-1]["content"] == "final answer"
    assert sum(str(message.get("content", "")).count("x") for message in history) < 100_000
    assert any("History truncated" in str(message.get("content", "")) for message in history)


@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_preserves_anchor_when_final_needs_truncation(
    make_loop, temp_dir: Path, monkeypatch
):
    fake_ov_client = _FakeOVClient(
        context_payload={
            "messages": [
                {"role": "user", "parts": [{"type": "text", "text": "u" * 400}]},
                {"role": "assistant", "parts": [{"type": "text", "text": "a" * 7600}]},
            ]
        }
    )

    monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))

    loop = make_loop(
        config=Config(
            storage_workspace=str(temp_dir),
            ov_server={"server_url": "http://127.0.0.1:1933"},
            agents={"session_context_enabled": True, "session_context_token_budget": 3000},
        )
    )
    session = loop.sessions.get_or_create(
        SessionKey(type="cli", channel_id="default", chat_id="session-long-final"),
        skip_heartbeat=True,
    )
    session.metadata["openviking"] = {
        "session_id": "ov-session-long-final",
        "last_synced_local_index": -1,
    }

    history = await loop._build_prompt_history(session)

    assert fake_ov_client.context_calls == [("ov-session-long-final", 3000)]
    assert [message["role"] for message in history] == ["user", "assistant"]
    assert history[0]["content"] == "u" * 400
    assert "History truncated" in history[1]["content"]
    assert sum(loop._history_message_tokens(message) for message in history) <= 3000


@pytest.mark.parametrize(
    "token_threshold, pending_tokens, memory_window",
    [pytest.param(100, 100, 50, id="token-budget"), pytest.param(1000, 0, 3, id="message-window")],
)
async def test_agent_loop_commits_before_model_at_context_limit(
    make_loop, temp_dir: Path, monkeypatch, token_threshold, pending_tokens, memory_window
):
    events = []

    async def fake_execute_hooks(context, **kwargs):
        events.append(
            (
                "hook",
                kwargs["force_commit"],
                kwargs.get("keep_recent_turn_count"),
                kwargs.get("commit_message_threshold"),
            )
        )
        session = kwargs["session"]
        state = session.metadata.setdefault("openviking", {})
        state["last_sync_status"] = "success"
        state["last_pending_tokens"] = 0
        state["last_commit_performed"] = bool(kwargs["force_commit"])
        state["last_commit_local_index"] = len(session.messages) - 1
        if not kwargs["force_commit"]:
            state["last_synced_local_index"] = len(session.messages) - 1
        return kwargs

    async def fake_get_ov_client(self, session_key, openviking_connection=None, actor_peer_id=None):
        del self, session_key, openviking_connection, actor_peer_id

        class _Client:
            async def get_session_context(self, session_id, token_budget):
                events.append(("context", session_id, token_budget))
                return {"messages": []}

            async def close(self):
                return None

        return _Client()

    async def fake_run_agent_loop(self, **kwargs):
        events.append(("model", [message.get("content") for message in kwargs["messages"]]))
        return "final answer", None, [], {"prompt_tokens": 1, "completion_tokens": 1}, 1

    fake_langfuse = _FakeLangfuseClient()
    monkeypatch.setattr(loop_module.hook_manager, "execute_hooks", fake_execute_hooks)
    monkeypatch.setattr(AgentLoop, "_get_ov_client", fake_get_ov_client)
    monkeypatch.setattr(AgentLoop, "_run_agent_loop", fake_run_agent_loop)
    monkeypatch.setattr(
        "vikingbot.agent.loop.LangfuseClient.get_instance",
        staticmethod(lambda: fake_langfuse),
    )

    bus = MessageBus()
    config = Config(
        storage_workspace=str(temp_dir),
        ov_server={"server_url": "http://127.0.0.1:1933"},
        agents={
            "session_context_enabled": True,
            "session_context_token_budget": 321,
            "commit_token_threshold": token_threshold,
            "commit_keep_recent_turn_count": 2,
        },
    )
    loop = make_loop(bus=bus, config=config, memory_window=memory_window)

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-precommit")
    session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
    session.add_message("user", "old user", sender_id="user-1")
    session.add_message("assistant", "old assistant", sender_id="user-1")
    session.metadata["openviking"] = {
        "session_id": session_key.safe_name(),
        "last_synced_local_index": 1,
        "last_pending_tokens": pending_tokens,
        "last_commit_local_index": -1,
    }
    await loop.sessions.save(session)

    response = await loop._process_message(
        InboundMessage(
            session_key=session_key,
            sender_id="user-1",
            content="new question",
            timestamp=datetime.fromisoformat("2026-04-30T00:05:00"),
        )
    )

    assert response is not None
    assert response.content == "final answer"
    assert events[0] == ("hook", True, 2, loop.memory_window)
    assert events[1] == ("context", "cli__default__session-precommit", 321)
    assert events[2][0] == "model"
    assert events[-1] == ("hook", False, None, loop.memory_window)
    persisted_session = SessionManager(config.bot_data_path).get_or_create(
        session_key, skip_heartbeat=True
    )
    assert [message["content"] for message in persisted_session.messages] == [
        "new question",
        "final answer",
    ]
    assert persisted_session.metadata["openviking"]["last_synced_local_index"] == 1


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "last_commit_index, expected_commits",
    [
        pytest.param(-1, [True], id="uncommitted-history"),
        pytest.param(1, [], id="already-committed"),
    ],
)
async def test_agent_loop_precommit_counts_messages_since_last_commit(
    make_loop, temp_dir: Path, monkeypatch, last_commit_index, expected_commits
):
    calls = []

    async def fake_execute_hooks(context, **kwargs):
        calls.append(kwargs)
        session = kwargs["session"]
        session.metadata.setdefault("openviking", {})["last_sync_status"] = "success"
        return kwargs

    monkeypatch.setattr(loop_module.hook_manager, "execute_hooks", fake_execute_hooks)

    bus = MessageBus()
    config = Config(
        storage_workspace=str(temp_dir),
        ov_server={"server_url": "http://127.0.0.1:1933"},
        agents={
            "session_context_enabled": True,
            "commit_token_threshold": 1000,
            "commit_keep_recent_turn_count": 2,
        },
    )
    loop = make_loop(bus=bus, config=config, memory_window=3)

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-window-no-repeat")
    session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
    session.add_message("user", "old user", sender_id="user-1")
    session.add_message("assistant", "old assistant", sender_id="user-1")
    session.metadata["openviking"] = {
        "session_id": session_key.safe_name(),
        "last_pending_tokens": 0,
        "last_commit_local_index": last_commit_index,
    }

    await loop._maybe_commit_openviking_before_turn(
        session,
        InboundMessage(
            session_key=session_key,
            sender_id="user-1",
            content="new question",
            timestamp=datetime.fromisoformat("2026-04-30T00:05:00"),
        ),
    )

    assert [call["force_commit"] for call in calls] == expected_commits


@pytest.mark.asyncio
async def test_agent_loop_post_turn_clears_local_session_after_openviking_commit(
    make_loop, temp_dir: Path, monkeypatch
):
    calls = []

    async def fake_execute_hooks(context, **kwargs):
        calls.append(kwargs)
        session = kwargs["session"]
        state = session.metadata.setdefault("openviking", {})
        state["last_sync_status"] = "success"
        state["last_commit_performed"] = True
        state["last_synced_local_index"] = len(session.messages) - 1
        state["last_commit_local_index"] = len(session.messages) - 1
        return kwargs

    async def fake_run_agent_loop(self, **kwargs):
        return "final answer", None, [], {"prompt_tokens": 1, "completion_tokens": 1}, 1

    fake_langfuse = _FakeLangfuseClient()
    monkeypatch.setattr(loop_module.hook_manager, "execute_hooks", fake_execute_hooks)
    monkeypatch.setattr(AgentLoop, "_run_agent_loop", fake_run_agent_loop)
    monkeypatch.setattr(
        "vikingbot.agent.loop.LangfuseClient.get_instance",
        staticmethod(lambda: fake_langfuse),
    )

    bus = MessageBus()
    config = Config(
        storage_workspace=str(temp_dir),
        ov_server={"server_url": "http://127.0.0.1:1933"},
        agents={"session_context_enabled": True},
    )
    loop = make_loop(bus=bus, config=config, memory_window=3)

    session_key = SessionKey(type="cli", channel_id="default", chat_id="session-post-clear")
    await loop._process_message(
        InboundMessage(
            session_key=session_key,
            sender_id="user-1",
            content="new question",
            timestamp=datetime.fromisoformat("2026-04-30T00:05:00"),
        )
    )

    persisted_session = SessionManager(config.bot_data_path).get_or_create(
        session_key, skip_heartbeat=True
    )
    assert calls[-1]["force_commit"] is False
    assert calls[-1]["commit_message_threshold"] == 3
    assert persisted_session.messages == []
    assert persisted_session.metadata["openviking"]["session_id"] == (
        make_openviking_storage_session_id(session_key.safe_name())
    )
    assert persisted_session.metadata["openviking"]["last_synced_local_index"] == -1
    assert persisted_session.metadata["openviking"]["last_commit_local_index"] == -1


@pytest.mark.asyncio
@pytest.mark.parametrize("confirmation", [True, False, None])
@pytest.mark.parametrize("manual", [True, False])
async def test_wm_compaction_only_clears_history_after_confirmation(
    make_loop, temp_dir, monkeypatch, confirmation, manual
):
    config = Config(
        storage_workspace=str(temp_dir),
        ov_server={"server_url": "http://ov.local"},
    )
    loop = make_loop(config=config)
    key = SessionKey(type="cli", channel_id="default", chat_id="wm-compaction")
    session = loop.sessions.get_or_create(key, skip_heartbeat=True)
    session.add_message("user", "Keep this constraint")
    session.add_message("assistant", "Recorded")
    await loop.sessions.save(session)
    original = copy.deepcopy(session.messages)
    commit_result = {"status": "accepted", "archived": True}
    if confirmation is not None:
        commit_result["effective_enable_working_memory"] = confirmation
    client = SimpleNamespace(
        session_owner_user_id=lambda: None,
        append_messages=AsyncMock(return_value={"added": 2}),
        get_session=AsyncMock(return_value={"pending_tokens": 0}),
        commit_session=AsyncMock(return_value=commit_result),
    )
    hook = OpenVikingCompactHook()
    monkeypatch.setattr(hook, "_get_client", AsyncMock(return_value=(client, False)))
    monkeypatch.setattr(loop_module.hook_manager, "execute_hooks", hook.execute)
    monkeypatch.setattr(loop, "_check_cmd_auth", lambda _: True)
    loop._summarize_compact_chunk = AsyncMock(side_effect=AssertionError("WM handles compaction"))
    if manual:
        response = await loop._process_message(
            InboundMessage(session_key=key, sender_id="user", content="/compact")
        )
        assert response is not None
        if confirmation is not True:
            assert "history was kept" in response.content
    else:
        success = await loop._submit_openviking_session_and_clear_if_committed(
            session, force_commit=True
        )
        assert success is (confirmation is True)
    assert client.commit_session.await_args.kwargs["enable_working_memory"] is True
    loop._summarize_compact_chunk.assert_not_awaited()
    persisted = loop.sessions._load(key)
    if confirmation is True:
        assert persisted.messages == []
    else:
        assert persisted.messages == original
        loop._get_ov_client = AsyncMock(side_effect=AssertionError("Keep the full local history"))
        history = await loop._build_prompt_history(session)
        assert [m["content"] for m in history] == [m["content"] for m in original]