Skip to content

armgit / avibe

Public Imported from GitHub
0

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

9.9 KB
Raw Blame
from __future__ import annotations

import sys
import unittest
from pathlib import Path
from types import SimpleNamespace

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

from core.agent_output import DeliveryReceipt
from core.citations import CitationSource, register_citations
from modules.agents.base import AgentRequest, BaseAgent
from modules.im import MessageContext
from modules.im.formatters.slack_formatter import SlackFormatter


class _StubController:
    def __init__(self, token_field: str = ""):
        self.config = SimpleNamespace(show_duration=True)
        self.im_client = SimpleNamespace(formatter=SlackFormatter())
        self.settings_manager = SimpleNamespace(sessions=None)
        self.messages = []
        self.result_footers = []
        self._token_field = token_field

    def session_token_field(self, context):
        return self._token_field

    async def emit_agent_message(
        self,
        context,
        message_type,
        text,
        parse_mode="markdown",
        *,
        is_error=False,
        level="normal",
        result_footer=None,
        output=None,
    ):
        self.messages.append((message_type, text, parse_mode))
        self.result_footers.append(result_footer)
        return "message-id"


class _StubAgent(BaseAgent):
    name = "stub"

    async def handle_message(self, request: AgentRequest) -> None:
        return None


class AgentSilentResultTests(unittest.IsolatedAsyncioTestCase):
    async def test_silent_only_result_suppresses_duration_wrapper(self):
        controller = _StubController()
        agent = _StubAgent(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")

        message_id = await agent.emit_result_message(
            context,
            "<silent>not relevant</silent>",
            subtype="success",
            duration_ms=1234,
        )

        self.assertEqual(controller.messages, [("result", "", "markdown")])
        self.assertEqual(message_id, DeliveryReceipt("message-id"))

    async def test_no_visible_result_with_duration_hidden_settles_via_outbound(self):
        # show_duration off + empty result/suffix is still a TERMINAL turn: it is
        # settled through the OUTBOUND status chokepoint — an empty terminal result
        # emit (→ dot idle/failed + releases the web-Chat stream) instead of being
        # left to hang to the 600s timeout (Codex P2).
        controller = _StubController()
        controller.config = SimpleNamespace(show_duration=False)
        agent = _StubAgent(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="avibe")

        await agent.emit_result_message(context, "", subtype="success", duration_ms=0)

        # An empty terminal result is emitted (no visible text); the dispatcher's
        # result path settles the dot + releases the stream.
        self.assertEqual(controller.messages, [("result", "", "markdown")])

    async def test_a_footer_only_end_is_content_free(self):
        """MESSAGE-DELIVERY-335: show_duration on and nothing visible to say. A footer is
        never a reply, so the end is content-free: an empty body, the footer only as
        ``result_footer`` (the dispatcher settles the Turn with its last reply)."""
        controller = _StubController(token_field="1.2k tok")
        agent = _StubAgent(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")

        await agent.emit_result_message(context, "", subtype="success", duration_ms=5000)

        self.assertEqual(controller.messages, [("result", "", "markdown")])
        self.assertEqual(controller.result_footers, ["✅ ⏱️ 5s · 🪙 1.2k tok"])

    async def test_an_uncited_result_does_not_require_the_citation_keyword(self):
        """The controller is substitutable, so an empty sidecar stays unsent.

        ``_StubController`` above deliberately keeps the pre-citation signature:
        an ordinary uncited turn must reach any stand-in that never heard of
        citations, on both the duration-on and duration-off paths.
        """
        controller = _StubController()
        agent = _StubAgent(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")

        for show_duration in (True, False):
            controller.config.show_duration = show_duration
            message_id = await agent.emit_result_message(context, "Plain answer.")

            self.assertEqual(message_id, DeliveryReceipt("message-id"))
        self.assertEqual([m[0] for m in controller.messages], ["result", "result"])

    async def test_a_cited_result_forwards_its_sidecar(self):
        """The keyword is withheld only when there is nothing to carry."""
        seen: list[object] = []

        class _CitationAwareController(_StubController):
            async def emit_agent_message(self, *args, citations=None, **kwargs):
                seen.append(citations)
                return await super().emit_agent_message(*args, **kwargs)

        controller = _CitationAwareController()
        agent = _StubAgent(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")
        registered, bundle = register_citations(
            "Cited answer.\ue200cite\ue202turn0view0\ue201",
            {
                "turn0view0": CitationSource(
                    ref_id="turn0view0", title="Example", url="https://example.com/x"
                )
            },
            unresolved_label="(source unavailable)",
        )

        await agent.emit_result_message(context, registered, citations=bundle)

        # The bundle is forwarded, not a copy of it: the tokens in the text are
        # only meaningful against the one that minted them.
        self.assertEqual(seen, [bundle])
        self.assertIs(seen[0], bundle)


class AgentSessionIdContextTests(unittest.TestCase):
    def test_bind_agent_session_id_attaches_returned_public_session_id(self):
        controller = _StubController()
        controller.sessions = SimpleNamespace(
            bind_agent_session=lambda session_key, agent_name, anchor, native_id, **kwargs: "sesk8m4q2p7x"
        )
        agent = _StubAgent(controller)
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"platform": "slack"},
        )
        request = AgentRequest(
            context=context,
            message="hello",
            user_message="hello",
            working_path="/tmp/work",
            base_session_id="slack_171717.123",
            composite_session_id="slack_171717.123:/tmp/work",
            session_key="slack::C1",
        )

        session_id = agent.bind_agent_session_id(request, "thread-native-1")

        self.assertEqual(session_id, "sesk8m4q2p7x")
        self.assertEqual(request.context.platform_specific["agent_session_id"], "sesk8m4q2p7x")

    def test_bind_agent_session_id_passes_resolved_target_workdir(self):
        calls = {}

        def bind_agent_session(session_key, agent_name, anchor, native_id, **kwargs):
            calls.update(
                session_key=session_key,
                agent_name=agent_name,
                anchor=anchor,
                native_id=native_id,
                kwargs=kwargs,
            )
            return "sesk8m4q2p7x"

        controller = _StubController()
        controller.sessions = SimpleNamespace(bind_agent_session=bind_agent_session)
        agent = _StubAgent(controller)
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={
                "platform": "slack",
                "agent_run_target": {"workdir": "/repo/original"},
            },
        )
        request = AgentRequest(
            context=context,
            message="hello",
            user_message="hello",
            working_path="/tmp/test",
            base_session_id="slack_171717.123",
            composite_session_id="slack_171717.123:/repo/original",
            session_key="slack::C1",
        )

        session_id = agent.bind_agent_session_id(request, "thread-native-1")

        self.assertEqual(session_id, "sesk8m4q2p7x")
        self.assertEqual(calls["kwargs"]["workdir"], "/repo/original")

    def test_target_subagent_native_is_stored_under_namespaced_anchor(self):
        calls = {}

        def bind_agent_session(session_key, agent_name, anchor, native_id, **kwargs):
            calls.update(
                session_key=session_key,
                agent_name=agent_name,
                anchor=anchor,
                native_id=native_id,
                kwargs=kwargs,
            )
            return "ses_subagent"

        controller = _StubController()
        controller.sessions = SimpleNamespace(
            bind_agent_session=bind_agent_session,
            bind_agent_session_by_id=lambda *a, **k: "ses_main",
        )
        agent = _StubAgent(controller)
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="avibe",
            platform_specific={
                "agent_session_target": {"id": "ses_main"},
                "routing_subagent": "reviewer",
            },
        )
        request = AgentRequest(
            context=context,
            message="hello",
            user_message="hello",
            working_path="/tmp/work",
            base_session_id="chat-1:reviewer",
            composite_session_id="chat-1:reviewer:/tmp/work",
            session_key="avibe::project::p1",
            subagent_name="reviewer",
        )

        session_id = agent.bind_agent_session_id(request, "thread-native-reviewer")

        self.assertEqual(session_id, "ses_subagent")
        self.assertEqual(calls["anchor"], "chat-1:reviewer")
        self.assertEqual(calls["native_id"], "thread-native-reviewer")
        self.assertEqual(request.context.platform_specific["agent_session_id"], "ses_main")


if __name__ == "__main__":
    unittest.main()