Skip to content

"Vibe-Trading: Your Personal Trading Agent"

36.0 KB
Raw Blame
"""Contracts for the IM channel runtime wiring."""

from __future__ import annotations

import asyncio
import base64
from pathlib import Path
from typing import Any

import pytest

from src.channels.bus.events import InboundMessage, OutboundMessage
from src.channels.bus.queue import MessageBus
from src.channels.manager import ChannelManager
from src.channels.registry import discover_channel_names, inspect_channels
from src.config.schema import ChannelsConfig
from src.channelsui.cli_apps_api import normalize_cli_app_mentions
from src.channelsui.gateway_services import build_gateway_services
from src.channelsui.mcp_presets_api import normalize_mcp_preset_mentions
from src.channelsui.transcription_ws import webui_transcription_event
from src.session.goal_state import goal_state_ws_blob
from src.session.models import Message, Session
from src.session.service import SessionBusyError
from src.session.webui_turns import (
    clear_websocket_turn_started,
    mark_websocket_turn_started,
    websocket_turn_wall_started_at,
)
from src.utils.media_decode import FileSizeExceeded, save_base64_data_url


class FakeSessionService:
    """Small SessionService stand-in for channel runtime tests."""

    def __init__(self) -> None:
        self.created: list[Session] = []
        self.sent: list[tuple[str, str]] = []
        self.messages: dict[str, list[Message]] = {}

    def create_session(self, title: str = "", config: dict[str, Any] | None = None) -> Session:
        session = Session(session_id=f"session-{len(self.created) + 1}", title=title, config=config or {})
        self.created.append(session)
        self.messages[session.session_id] = []
        return session

    def get_session(self, session_id: str) -> Session | None:
        return next((session for session in self.created if session.session_id == session_id), None)

    async def send_message(
        self,
        session_id: str,
        content: str,
        *,
        include_shell_tools: bool = False,
        parent_attempt_id: str | None = None,
    ) -> dict[str, str]:
        del include_shell_tools, parent_attempt_id
        self.sent.append((session_id, content))
        attempt_id = f"attempt-{len(self.sent)}"
        self.messages[session_id].append(
            Message(
                session_id=session_id,
                role="assistant",
                content=f"agent reply: {content}",
                linked_attempt_id=attempt_id,
            )
        )
        return {"message_id": "msg-1", "attempt_id": attempt_id}

    def get_messages(self, session_id: str, limit: int = 100) -> list[Message]:
        del limit
        return list(self.messages.get(session_id, []))


def test_channel_manager_can_construct_websocket_with_default_gateway() -> None:
    bus = MessageBus()
    manager = ChannelManager(
        {"websocket": {"enabled": True, "host": "127.0.0.1", "port": 0, "allow_from": ["*"]}},
        bus,
    )

    assert "websocket" in manager.channels
    assert manager.channels["websocket"].name == "websocket"


def test_should_suppress_outbound_rejects_duplicate_reply_to_same_message() -> None:
    """A second byte-identical reply carrying the same inbound message_id
    must be suppressed, matching the log line's documented contract."""
    bus = MessageBus()
    manager = ChannelManager(
        {"websocket": {"enabled": True, "host": "127.0.0.1", "port": 0, "allow_from": ["*"]}},
        bus,
    )
    msg1 = OutboundMessage(
        channel="telegram",
        chat_id="123",
        content="Here is your answer.",
        metadata={"message_id": "inbound-42"},
    )
    msg2 = OutboundMessage(
        channel="telegram",
        chat_id="123",
        content="Here is your answer.",
        metadata={"message_id": "inbound-42"},
    )

    assert manager._should_suppress_outbound(msg1) is False
    assert manager._should_suppress_outbound(msg2) is True


def _dedup_manager() -> ChannelManager:
    return ChannelManager(
        {"websocket": {"enabled": True, "host": "127.0.0.1", "port": 0, "allow_from": ["*"]}},
        MessageBus(),
    )


def test_integer_message_ids_dedupe_like_strings() -> None:
    """Telegram and NapCat carry int message ids; they were never deduped (#1476)."""
    manager = _dedup_manager()
    reply = lambda: OutboundMessage(  # noqa: E731
        channel="telegram", chat_id="123", content="Here is your answer.", metadata={"message_id": 42}
    )

    assert manager._should_suppress_outbound(reply()) is False
    assert manager._should_suppress_outbound(reply()) is True


def test_a_different_reply_to_the_same_message_is_sent() -> None:
    manager = _dedup_manager()
    first = OutboundMessage(channel="telegram", chat_id="1", content="first", metadata={"message_id": "m"})
    second = OutboundMessage(channel="telegram", chat_id="1", content="second", metadata={"message_id": "m"})

    assert manager._should_suppress_outbound(first) is False
    assert manager._should_suppress_outbound(second) is False


def test_reply_fingerprints_are_bounded(monkeypatch) -> None:
    import src.channels.manager as manager_module

    monkeypatch.setattr(manager_module, "_MAX_REPLY_FINGERPRINTS", 3)
    manager = _dedup_manager()
    for i in range(5):
        manager._should_suppress_outbound(
            OutboundMessage(channel="telegram", chat_id="1", content="same", metadata={"message_id": f"m{i}"})
        )

    assert list(manager._origin_reply_fingerprints) == [
        ("telegram", "1", "m2"),
        ("telegram", "1", "m3"),
        ("telegram", "1", "m4"),
    ]


def test_registry_reports_all_built_in_channels_with_dependency_recovery() -> None:
    expected = {
        "dingtalk",
        "discord",
        "email",
        "feishu",
        "matrix",
        "mochat",
        "msteams",
        "napcat",
        "qq",
        "signal",
        "slack",
        "telegram",
        "websocket",
        "wecom",
        "weixin",
        "whatsapp",
    }

    assert expected.issubset(set(discover_channel_names()))
    assert {"config", "runtime"}.isdisjoint(set(discover_channel_names()))

    statuses = inspect_channels({"telegram": {"enabled": True}, "slack": {"enabled": True}})

    for name in expected:
        assert name in statuses
        assert statuses[name]["name"] == name
        assert "install_hint" in statuses[name]
        assert "available" in statuses[name]

    missing = [item for item in statuses.values() if not item["available"]]
    assert all(item["install_hint"].startswith("pip install") for item in missing)


def test_registry_ignores_global_channel_settings() -> None:
    statuses = inspect_channels(
        ChannelsConfig.model_validate(
            {
                "replyTimeoutS": 120,
                "sendMaxRetries": 1,
                "telegram": {"enabled": True},
            }
        )
    )

    assert "telegram" in statuses
    assert "reply_timeout_s" not in statuses
    assert "send_max_retries" not in statuses
    assert "model_dump" not in statuses


def test_telegram_status_suggests_numeric_authorized_private_chats() -> None:
    from src.channels.registry import inspect_channel

    if not inspect_channel("telegram").available:
        pytest.skip("telegram optional dependency not installed")

    manager = ChannelManager(
        {
            "telegram": {
                "enabled": True,
                "token": "test-token",
                "allow_from": ["1381234567", "alice", "*", "1381234567|alice", "123456"],
            }
        },
        MessageBus(),
    )

    status = manager.get_status()["telegram"]

    assert status["delivery_target_suggestions"] == [
        {"kind": "private_chat", "target": "1381234567"}
    ]


def test_channel_manager_status_includes_every_configured_adapter() -> None:
    bus = MessageBus()
    manager = ChannelManager(
        {
            "send_max_retries": 1,
            "reply_timeout_s": 600.0,
            "websocket": {"enabled": True, "host": "127.0.0.1", "port": 0, "allow_from": ["*"]},
            "telegram": {"enabled": False},
            "slack": {"enabled": True},
        },
        bus,
    )

    status = manager.get_status()

    assert "send_max_retries" not in status
    assert "reply_timeout_s" not in status
    assert status["websocket"]["loaded"] is True
    assert status["websocket"]["enabled"] is True
    assert status["telegram"]["configured"] is True
    assert status["telegram"]["enabled"] is False
    assert status["slack"]["configured"] is True
    assert status["slack"]["enabled"] is True
    assert "available" in status["slack"]
    if not status["slack"]["loaded"]:
        assert status["slack"]["error"]
        assert status["slack"]["install_hint"].startswith("pip install")


def test_registry_marks_lazy_sdk_adapter_unavailable(monkeypatch: pytest.MonkeyPatch) -> None:
    import src.channels.discord as discord_channel

    from src.channels.registry import inspect_channel

    monkeypatch.setattr(discord_channel, "DISCORD_AVAILABLE", False)

    status = inspect_channel("discord").to_dict()

    assert status["available"] is False
    assert status["install_hint"] == "pip install 'vibe-trading-ai[discord]'"


def test_channel_manager_skips_enabled_adapter_when_lazy_sdk_missing(monkeypatch: pytest.MonkeyPatch) -> None:
    import src.channels.discord as discord_channel

    monkeypatch.setattr(discord_channel, "DISCORD_AVAILABLE", False)

    manager = ChannelManager({"discord": {"enabled": True, "token": "x"}}, MessageBus())
    status = manager.get_status()["discord"]

    assert "discord" not in manager.channels
    assert status["enabled"] is True
    assert status["available"] is False
    assert status["loaded"] is False
    assert status["install_hint"] == "pip install 'vibe-trading-ai[discord]'"


def test_websocket_turn_wall_accepts_optional_chat_id() -> None:
    assert websocket_turn_wall_started_at("chat-1") is None
    mark_websocket_turn_started("chat-1", 123.0)
    assert websocket_turn_wall_started_at("chat-1") == 123.0
    clear_websocket_turn_started("chat-1")
    assert websocket_turn_wall_started_at("chat-1") is None


def test_websocket_compatibility_helpers_have_structured_behavior() -> None:
    assert goal_state_ws_blob({"active_goal": {"goal_id": "g1"}}) == {
        "active": True,
        "active_goals": [{"goal_id": "g1"}],
        "completed_goals": [],
    }
    assert normalize_cli_app_mentions("@codex, terminal") == ["codex", "terminal"]
    assert normalize_mcp_preset_mentions(["@research", "market"]) == ["research", "market"]


def test_gateway_services_adapt_session_service_for_hydration() -> None:
    service = FakeSessionService()
    session = service.create_session(title="chat", config={"metadata": {"active_goal": {"goal_id": "g1"}}})
    gateway = build_gateway_services(session_manager=service)

    row = gateway.session_manager.read_session_file(session.session_id)

    assert row["metadata"] == {"active_goal": {"goal_id": "g1"}}


def test_transcription_event_returns_structured_error() -> None:
    async def scenario() -> None:
        event, payload = await webui_transcription_event({"type": "transcribe_audio"})
        assert event == "error"
        assert "not configured" in payload["detail"]

    asyncio.run(scenario())


def test_save_base64_data_url_decodes_and_limits_size(tmp_path: Path) -> None:
    payload = base64.b64encode(b"hello").decode("ascii")

    saved = save_base64_data_url(f"data:text/plain;base64,{payload}", tmp_path, max_bytes=10)

    assert saved.read_text(encoding="utf-8") == "hello"
    assert saved.suffix == ".txt"

    with pytest.raises(FileSizeExceeded):
        save_base64_data_url(f"data:text/plain;base64,{payload}", tmp_path, max_bytes=4)

    with pytest.raises(ValueError):
        save_base64_data_url("not-a-data-url", tmp_path, max_bytes=10)


def test_channel_runtime_routes_inbound_to_session_and_outbound(tmp_path: Path) -> None:
    async def scenario() -> None:
        from src.channels.runtime import ChannelRuntime

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(
                    channel="websocket",
                    sender_id="user-1",
                    chat_id="chat-1",
                    content="hello from IM",
                    metadata={"message_id": "orig-msg-42"},
                )
            )

            outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert service.sent == [("session-1", "hello from IM")]
        assert outbound == OutboundMessage(
            channel="websocket",
            chat_id="chat-1",
            content="agent reply: hello from IM",
            metadata={
                "_channel_runtime": True,
                "attempt_id": "attempt-1",
                "session_id": "session-1",
                # The originating message id is threaded through so the QQ
                # adapter can send the reply as a passive message (msg_id)
                # instead of an active message QQ rejects for non-privileged bots.
                "message_id": "orig-msg-42",
            },
        )

    asyncio.run(scenario())


def test_channel_runtime_handles_pairing_commands_without_agent(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
    async def scenario() -> None:
        from src.channels.pairing import store as pairing_store
        from src.channels.runtime import ChannelRuntime

        monkeypatch.setattr(pairing_store, "_store_path", lambda: tmp_path / "pairing.json")
        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
            operators=["owner"],
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="owner",
                    chat_id="chat-1",
                    content="/PAIRING LIST",
                    metadata={"message_id": "pairing-msg-1"},
                )
            )
            outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert service.sent == []
        assert outbound.channel == "telegram"
        assert outbound.chat_id == "chat-1"
        assert "No pending pairing requests" in outbound.content
        assert outbound.metadata["_pairing_command"] is True
        assert outbound.metadata["message_id"] == "pairing-msg-1"

    asyncio.run(scenario())


def test_channel_runtime_rejects_pairing_from_non_operator(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """A non-operator /pairing command is refused without touching the store (GHSA-fwpw)."""

    async def scenario() -> None:
        from src.channels import pairing
        from src.channels.pairing import store as pairing_store
        from src.channels.runtime import ChannelRuntime

        monkeypatch.setattr(pairing_store, "_store_path", lambda: tmp_path / "pairing.json")
        # A pending code exists on another channel; a non-operator must not be
        # able to approve it or even learn it exists.
        code = pairing.generate_code("signal", "victim-sender")

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
            operators=["owner"],
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="stranger",
                    chat_id="chat-1",
                    content=f"/pairing approve {code}",
                    metadata={"message_id": "pairing-msg-2"},
                )
            )
            outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert service.sent == []
        assert outbound.metadata.get("unauthorized") is True
        assert outbound.metadata["message_id"] == "pairing-msg-2"
        assert "Not authorized" in outbound.content
        # The code must NOT have been consumed by the rejected attempt.
        assert pairing.is_approved("signal", "victim-sender") is False
        assert any(item["code"] == code for item in pairing.list_pending())

    asyncio.run(scenario())


def test_channel_runtime_operator_can_pair(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """A configured operator can approve a pending pairing code end to end."""

    async def scenario() -> None:
        from src.channels import pairing
        from src.channels.pairing import store as pairing_store
        from src.channels.runtime import ChannelRuntime

        monkeypatch.setattr(pairing_store, "_store_path", lambda: tmp_path / "pairing.json")
        code = pairing.generate_code("telegram", "new-user")

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
            operators=["owner"],
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="owner",
                    chat_id="chat-1",
                    content=f"/pairing approve {code}",
                )
            )
            outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert outbound.metadata.get("unauthorized") is not True
        assert "Approved pairing code" in outbound.content
        assert pairing.is_approved("telegram", "new-user") is True

    asyncio.run(scenario())


def test_channel_runtime_channel_operator_cannot_act_cross_channel(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """A per-channel (non-global) operator cannot approve codes on other channels."""

    async def scenario() -> None:
        from src.channels import pairing
        from src.channels.pairing import store as pairing_store
        from src.channels.runtime import ChannelRuntime

        monkeypatch.setattr(pairing_store, "_store_path", lambda: tmp_path / "pairing.json")
        # Pending code lives on signal; the operator is scoped to telegram only.
        code = pairing.generate_code("signal", "signal-victim")

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
            channel_operators={"telegram": ["tg-op"]},
        )
        await runtime.start(start_manager=False)
        try:
            # 1) Approve attempt against another channel's code is refused and
            #    does not consume the code (no cross-channel oracle).
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="tg-op",
                    chat_id="chat-1",
                    content=f"/pairing approve {code}",
                )
            )
            approve_reply = await asyncio.wait_for(bus.consume_outbound(), timeout=1)

            # 2) list must not leak the other channel's pending request.
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="tg-op",
                    chat_id="chat-1",
                    content="/pairing list",
                )
            )
            list_reply = await asyncio.wait_for(bus.consume_outbound(), timeout=1)

            # 3) explicit cross-channel revoke is refused.
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="tg-op",
                    chat_id="chat-1",
                    content="/pairing revoke signal signal-victim",
                )
            )
            revoke_reply = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert "Invalid or expired pairing code" in approve_reply.content
        assert pairing.is_approved("signal", "signal-victim") is False
        assert any(item["code"] == code for item in pairing.list_pending())

        assert code not in list_reply.content
        assert "signal-victim" not in list_reply.content

        assert "Not authorized" in revoke_reply.content

    asyncio.run(scenario())


def test_channel_runtime_pairing_fail_closed_when_no_operators_configured(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """With no operators configured, ALL in-chat /pairing is rejected (fail-closed)."""

    async def scenario() -> None:
        from src.channels import pairing
        from src.channels.pairing import store as pairing_store
        from src.channels.runtime import ChannelRuntime

        monkeypatch.setattr(pairing_store, "_store_path", lambda: tmp_path / "pairing.json")
        code = pairing.generate_code("telegram", "new-user")

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
            # operators / channel_operators both unset -> fail closed.
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="anyone",
                    chat_id="chat-1",
                    content=f"/pairing approve {code}",
                )
            )
            outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert outbound.metadata.get("unauthorized") is True
        assert "Not authorized" in outbound.content
        # Nothing was approved; the pending code survives untouched.
        assert pairing.is_approved("telegram", "new-user") is False
        assert any(item["code"] == code for item in pairing.list_pending())

    asyncio.run(scenario())


def test_channel_runtime_non_operator_list_does_not_leak_codes(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """A non-operator's rejected /pairing list must not disclose codes or sender ids."""

    async def scenario() -> None:
        from src.channels import pairing
        from src.channels.pairing import store as pairing_store
        from src.channels.runtime import ChannelRuntime

        monkeypatch.setattr(pairing_store, "_store_path", lambda: tmp_path / "pairing.json")
        code = pairing.generate_code("signal", "victim-sender")

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
            operators=["owner"],
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="stranger",
                    chat_id="chat-1",
                    content="/pairing list",
                )
            )
            outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert outbound.metadata.get("unauthorized") is True
        assert "Not authorized" in outbound.content
        assert code not in outbound.content
        assert "victim-sender" not in outbound.content

    asyncio.run(scenario())


def test_channel_runtime_new_command_resets_session_and_creates_fresh_one(tmp_path: Path) -> None:
    async def scenario() -> None:
        from src.channels.runtime import ChannelRuntime

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(channel="feishu", sender_id="u1", chat_id="c1", content="hello")
            )
            reply1 = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
            assert reply1.metadata["session_id"] == "session-1"

            await bus.publish_inbound(
                InboundMessage(channel="feishu", sender_id="u1", chat_id="c1", content="/new")
            )
            reset_reply = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
            assert "Session reset" in reset_reply.content
            assert reset_reply.metadata.get("session_reset") is True

            await bus.publish_inbound(
                InboundMessage(channel="feishu", sender_id="u1", chat_id="c1", content="after reset")
            )
            reply2 = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
            assert reply2.metadata["session_id"] == "session-2"
        finally:
            await runtime.stop()

        assert service.sent == [("session-1", "hello"), ("session-2", "after reset")]

    asyncio.run(scenario())


def test_channel_runtime_new_command_with_no_existing_session(tmp_path: Path) -> None:
    async def scenario() -> None:
        from src.channels.runtime import ChannelRuntime

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(
                    channel="telegram",
                    sender_id="u1",
                    chat_id="c1",
                    content="/new",
                    metadata={"message_id": "reset-msg-1"},
                )
            )
            reply = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert "No active session to reset" in reply.content
        assert reply.metadata.get("session_reset") is True
        assert reply.metadata["message_id"] == "reset-msg-1"
        assert service.sent == []
        assert len(service.created) == 0

    asyncio.run(scenario())


@pytest.mark.parametrize(
    ("failure", "flag"),
    [
        (SessionBusyError("busy"), "busy"),
        (RuntimeError("boom"), "error"),
    ],
)
def test_channel_runtime_preserves_message_id_for_failure_replies(
    tmp_path: Path,
    failure: Exception,
    flag: str,
) -> None:
    async def scenario() -> None:
        from src.channels.runtime import ChannelRuntime

        bus = MessageBus()
        service = FakeSessionService()

        async def fail_send(*args: Any, **kwargs: Any) -> dict[str, str]:
            del args, kwargs
            raise failure

        service.send_message = fail_send  # type: ignore[method-assign]
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(
                    channel="qq",
                    sender_id="u1",
                    chat_id="c1",
                    content="hello",
                    metadata={"message_id": "origin-msg-1"},
                )
            )
            reply = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
        finally:
            await runtime.stop()

        assert reply.metadata[flag] is True
        assert reply.metadata["message_id"] == "origin-msg-1"

    asyncio.run(scenario())


def test_channel_runtime_reset_and_newsession_aliases_work(tmp_path: Path) -> None:
    async def scenario() -> None:
        from src.channels.runtime import ChannelRuntime

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(channel="discord", sender_id="u1", chat_id="c1", content="hi")
            )
            await asyncio.wait_for(bus.consume_outbound(), timeout=1)

            await bus.publish_inbound(
                InboundMessage(channel="discord", sender_id="u1", chat_id="c1", content="/reset")
            )
            reply = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
            assert "Session reset" in reply.content

            await bus.publish_inbound(
                InboundMessage(channel="discord", sender_id="u1", chat_id="c1", content="hi again")
            )
            await asyncio.wait_for(bus.consume_outbound(), timeout=1)

            await bus.publish_inbound(
                InboundMessage(channel="discord", sender_id="u1", chat_id="c1", content="/newsession")
            )
            reply2 = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
            assert "Session reset" in reply2.content
        finally:
            await runtime.stop()

    asyncio.run(scenario())


def test_channel_runtime_regular_messages_not_intercepted_as_new_session(tmp_path: Path) -> None:
    async def scenario() -> None:
        from src.channels.runtime import ChannelRuntime

        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=tmp_path / "channel_sessions.json",
            reply_timeout_s=1,
            poll_interval_s=0.01,
        )
        await runtime.start(start_manager=False)
        try:
            for text in ["hello /new world", "/new stuff", "/NEW YORK", "type /new to reset"]:
                await bus.publish_inbound(
                    InboundMessage(channel="slack", sender_id="u1", chat_id="c1", content=text)
                )
                reply = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
                assert reply.metadata.get("session_reset") is not True
                assert "agent reply:" in reply.content
        finally:
            await runtime.stop()

        assert len(service.sent) == 4

    asyncio.run(scenario())


def test_channel_runtime_session_map_persisted_after_reset(tmp_path: Path) -> None:
    import json

    async def scenario() -> None:
        from src.channels.runtime import ChannelRuntime

        map_path = tmp_path / "channel_sessions.json"
        bus = MessageBus()
        service = FakeSessionService()
        runtime = ChannelRuntime(
            bus=bus,
            session_service=service,
            manager=None,
            session_map_path=map_path,
            reply_timeout_s=1,
            poll_interval_s=0.01,
        )
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(
                InboundMessage(channel="feishu", sender_id="u1", chat_id="c1", content="hi")
            )
            await asyncio.wait_for(bus.consume_outbound(), timeout=1)

            data = json.loads(map_path.read_text(encoding="utf-8"))
            assert data["feishu:c1"] == "session-1"

            await bus.publish_inbound(
                InboundMessage(channel="feishu", sender_id="u1", chat_id="c1", content="/new")
            )
            await asyncio.wait_for(bus.consume_outbound(), timeout=1)

            data = json.loads(map_path.read_text(encoding="utf-8"))
            assert "feishu:c1" not in data
        finally:
            await runtime.stop()

    asyncio.run(scenario())


def test_signal_group_command_requires_per_sender_authorization() -> None:
    """A slash-command in an allowed group needs per-sender auth (GHSA-fwpw).

    Group membership alone must not authorize control-plane commands: an
    unauthorized member's ``/pairing`` is dropped, while an allowlisted
    sender's command passes even without a mention.
    """
    from src.channels.signal import SignalChannel, SignalConfig, SignalDMConfig, SignalGroupConfig

    config = SignalConfig(
        enabled=True,
        phone_number="+15550000",
        dm=SignalDMConfig(enabled=True, allow_from=["+15550001"]),
        group=SignalGroupConfig(
            enabled=True,
            policy="allowlist",
            allow_from=["group-1"],
            require_mention=True,
        ),
    )
    channel = SignalChannel(config, MessageBus())

    denied, denied_chat = channel._check_inbound_policy(
        sender_id="+15559999",
        sender_number="+15559999",
        group_id="group-1",
        is_group_message=True,
        message_text="/pairing list",
        mentions=[],
        sender_name="Mallory",
        timestamp=None,
    )
    assert denied is False
    assert denied_chat == "group-1"

    allowed, allowed_chat = channel._check_inbound_policy(
        sender_id="+15550001",
        sender_number="+15550001",
        group_id="group-1",
        is_group_message=True,
        message_text="/pairing list",
        mentions=[],
        sender_name="Owner",
        timestamp=None,
    )
    assert allowed is True
    assert allowed_chat == "group-1"


@pytest.mark.parametrize("fmt,label", [("html", "HTML"), ("pdf", "PDF"), (None, "plain text")])
def test_channel_confirmation_displays_email_format(tmp_path, monkeypatch, fmt, label):
    from src.scheduled_research import proposals

    monkeypatch.setattr(proposals, "latest_pending_for_session", lambda sid: {
        "operation": "create", "job": {
            "title": "Research", "schedule": {"expression": "60000"},
            "delivery": {"channel": "email", "target_label": "Research", "format": fmt},
        },
    })

    async def scenario():
        from src.channels.runtime import ChannelRuntime

        bus = MessageBus()
        runtime = ChannelRuntime(bus=bus, session_service=FakeSessionService(), manager=None,
                                 session_map_path=tmp_path / "sessions.json", reply_timeout_s=1,
                                 poll_interval_s=0.01)
        await runtime.start(start_manager=False)
        try:
            await bus.publish_inbound(InboundMessage(channel="websocket", sender_id="u", chat_id="c", content="schedule research"))
            outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
            assert f"Email format: {label}" in outbound.content
            assert 'Reply exactly "confirm"' in outbound.content
        finally:
            await runtime.stop()

    asyncio.run(scenario())