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.

87.2 KB
Raw Blame
import gc
import os
import sys
import time
import timeit
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch

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

import core.reply_enhancer as reply_enhancer
from core.controller import Controller
from core.message_dispatcher import ConsolidatedMessageDispatcher
from core.reply_enhancer import process_reply, strip_silent_blocks
from core.system_prompt_injection import build_system_prompt_injection
from modules.agents.base import AgentRequest, BaseAgent
from modules.im import MessageContext



# Each input grows by this factor between the two timed runs.
_GROWTH_STEP = 8
# Linear work grows about ``_GROWTH_STEP`` times across the step and quadratic
# work about its square (64); the bound sits between them with room on both sides.
_LINEAR_GROWTH_BOUND = 32


def _growth(make, count: int):
    """How many times longer ``process_reply`` takes on an input ``_GROWTH_STEP`` times larger.

    The parser is measured against itself, both sizes on the same runner moments
    apart, so the runner's speed cancels out: an absolute wall-clock bound
    measured the runner as much as the parser (#2636). The garbage collector is
    off while timing, as in ``timeit``; the smaller input, whose noise would
    inflate the growth, is timed best of three. The larger size must be one at
    which a quadratic term outweighs the parser's linear cost. Returns the growth
    and the larger input's reply.
    """

    small = make(count)
    small_seconds = min(timeit.repeat(lambda: process_reply(small), repeat=3, number=1))
    large = make(count * _GROWTH_STEP)
    collecting = gc.isenabled()
    gc.disable()
    try:
        started = time.perf_counter()
        reply = process_reply(large)
        large_seconds = time.perf_counter() - started
    finally:
        if collecting:
            gc.enable()
    return large_seconds / small_seconds, reply


class _StubSettingsManager:
    @staticmethod
    def _canonicalize_message_type(message_type: str) -> str:
        return message_type

    @staticmethod
    def is_message_type_hidden(settings_key: str, message_type: str) -> bool:
        return False


class _StubIMClient:
    def __init__(self):
        self.sent_messages = []
        self.sent_button_messages = []
        self.uploaded_markdowns = []
        self._next_id = 1

    @staticmethod
    def should_use_thread_for_reply() -> bool:
        return False

    async def send_message(self, context, text, parse_mode=None, reply_to=None):
        self.sent_messages.append((context.channel_id, text, parse_mode))
        message_id = f"msg-{self._next_id}"
        self._next_id += 1
        return message_id

    async def send_message_with_buttons(self, context, text, keyboard, parse_mode=None):
        self.sent_button_messages.append((context.channel_id, text, parse_mode, keyboard))
        message_id = f"btn-{self._next_id}"
        self._next_id += 1
        return message_id

    async def upload_markdown(self, context, title, content, filetype="markdown"):
        self.uploaded_markdowns.append((context.channel_id, title, content, filetype))
        return "file-1"


class _StubController:
    def __init__(self, platform: str, progress_style: str = "verbose"):
        self.config = type(
            "Config",
            (),
            {"platform": platform, "reply_enhancements": True},
        )()
        self.settings_manager = _StubSettingsManager()
        self.im_client = _StubIMClient()
        # Process/assistant messages are gated by the progress style on
        # status-bubble platforms (Slack/Discord/Lark). These content-transform
        # tests exercise the verbose log-message delivery path, so default to
        # "verbose" (lark's historical effective behavior before it gained the
        # status-bubble capability).
        self._progress_style = progress_style

    def get_progress_style_for_context(self, context=None) -> str:
        return self._progress_style

    @staticmethod
    def _get_settings_key(context: MessageContext) -> str:
        return f"{context.channel_id}:{context.user_id}"

    @staticmethod
    def _get_session_key(context: MessageContext) -> str:
        return f"{getattr(context, 'platform', None) or 'test'}::{context.channel_id}:{context.user_id}"

    def get_settings_manager_for_context(self, context=None):
        return self.settings_manager


class _StubAgent(BaseAgent):
    name = "stub"

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


def _resolved_core_skills(*, manual_only: bool = False) -> list[SimpleNamespace]:
    return [
        SimpleNamespace(
            name=name,
            description=f"{name} workflow",
            directory=Path("/tmp") / name,
            disable_model_invocation=manual_only,
        )
        for name in ("use-show-pages", "use-avibe-vault", "use-avibe-harness")
    ]


class ReplyEnhancerPlatformTests(unittest.IsolatedAsyncioTestCase):
    def test_prompt_can_exclude_quick_replies(self):
        prompt = build_system_prompt_injection(include_quick_replies=False)

        self.assertIn("## Silent replies", prompt)
        self.assertIn("<silent>reason not shown to the user</silent>", prompt)
        # One positive trigger, so its complement does the excluding and there is
        # no carve-out list to erode: `logs`/`runtime` keep read-only inspection
        # gated (that is where agents learn to read logs through the API and treat
        # internal state as opaque), and "does not cover" gates every Avibe
        # explanation this prompt cannot answer. Phrasing it as gate-plus-carve-out
        # cost two review rounds re-adding cases each compression shaved off.
        self.assertIn(
            "Consult the `use-avibe` playbook to operate Avibe "
            "(config, state, service, logs, runtime) or answer anything about it "
            "this prompt does not cover",
            prompt,
        )
        self.assertNotIn("configuration, repair, explanation, and operations", prompt)
        self.assertNotIn("when it is not installed locally", prompt)
        self.assertNotIn("new user turn", prompt)
        self.assertNotIn("active Agent Session context", prompt)
        self.assertNotIn("context compaction removed the guidance", prompt)
        self.assertIn("## Send files", prompt)
        self.assertIn("Avibe provides optional capabilities:", prompt)
        self.assertNotIn("If you generate an image with Codex", prompt)
        self.assertNotIn("## Quick-reply buttons", prompt)
        self.assertIn("## Project Context and History", prompt)
        self.assertIn("`AGENTS.md` is an index, not a log.", prompt)
        self.assertIn("make good use of `vibe data query`", prompt)
        # The shared preferences file is retired: the prompt neither names it nor routes notes to it.
        self.assertNotIn("user_preferences.md", prompt)
        self.assertNotIn("preferences file", prompt)

    def test_prompt_can_include_codex_generated_image_instructions(self):
        with patch.dict(os.environ, {"CODEX_HOME": "/Users/test/.codex"}):
            prompt = build_system_prompt_injection(
                include_quick_replies=False,
                include_codex_generated_images=True,
            )

        self.assertIn("## Codex-generated images", prompt)
        self.assertIn("If you generate an image with Codex", prompt)
        self.assertIn("file:///Users/test/.codex/generated_images/thread-id/image-file.png", prompt)
        self.assertIn("Never emit variables, placeholder paths, or sandbox paths like `/mnt/data/...`", prompt)

    def test_prompt_routes_vault_work_to_builtin_skill(self):
        with patch("core.managed_skills.resolve_skills", return_value=_resolved_core_skills()):
            prompt = build_system_prompt_injection(
                include_quick_replies=False,
                skills_cwd=Path("/tmp/project"),
            )

        self.assertIn("## Vault", prompt)
        self.assertIn("load the `use-avibe-vault` Skill", prompt)
        self.assertNotIn("vibe vault request OPENAI_API_KEY", prompt)
        self.assertNotIn("$<OPENAI_API_KEY>", prompt)

        skill = (Path(__file__).resolve().parents[1] / "skills" / "use-avibe-vault" / "SKILL.md").read_text()
        self.assertIn("vibe vault request OPENAI_API_KEY", skill)
        self.assertIn("$<OPENAI_API_KEY>", skill)
        self.assertIn("Do not rerun `sign`", skill)
        self.assertIn("the child process receives static secrets as environment variables", skill)

    def test_vault_routing_prompt_is_platform_independent(self):
        contexts = [
            MessageContext(
                user_id="U1",
                channel_id="C1",
                platform=platform,
                platform_specific={"agent_session_id": "sesk8m4q2p7x"},
            )
            for platform in ("avibe", "slack")
        ]

        with patch("core.managed_skills.resolve_skills", return_value=_resolved_core_skills()):
            prompts = [
                build_system_prompt_injection(
                    include_quick_replies=False,
                    context=context,
                    skills_cwd=Path("/tmp/project"),
                )
                for context in contexts
            ]

        self.assertEqual(
            prompts[0].split("## Vault", 1)[1].split("## Harness", 1)[0],
            prompts[1].split("## Vault", 1)[1].split("## Harness", 1)[0],
        )
        self.assertNotIn("$<OPENAI_API_KEY>", prompts[0])

    def test_required_skill_routes_do_not_depend_on_catalog_discovery(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="avibe",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )
        with patch("core.managed_skills.resolve_skills", return_value=[]):
            prompt = build_system_prompt_injection(
                include_quick_replies=False,
                context=context,
                skills_cwd=Path("/tmp/project"),
            )

        self.assertNotIn("## Vault", prompt)
        self.assertNotIn("load the `use-avibe-vault` Skill", prompt)
        self.assertIn("load the `use-show-pages` Skill", prompt)
        self.assertIn("load the `use-avibe-harness` Skill", prompt)
        self.assertNotIn("No enabled Agents", prompt)

    def test_show_pages_remain_when_quick_replies_are_disabled(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )

        with patch("core.managed_skills.resolve_skills", return_value=_resolved_core_skills()):
            prompt = build_system_prompt_injection(
                include_quick_replies=False,
                context=context,
                skills_cwd=Path("/tmp/project"),
            )

        self.assertIn("## Show Pages", prompt)
        self.assertNotIn("## Quick-reply buttons", prompt)
        self.assertIn("## Harness", prompt)
        self.assertNotIn("## Scheduled tasks, watches, and hooks", prompt)
        self.assertIn("Current session id: `sesk8m4q2p7x`", prompt)

    def test_prompt_can_exclude_context_guidance(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )

        prompt = build_system_prompt_injection(
            include_quick_replies=False,
            include_context_guidance=False,
            context=context,
        )

        self.assertIn("Current session id: `sesk8m4q2p7x`", prompt)
        self.assertNotIn("## Project Context and History", prompt)


    def test_process_reply_strips_silent_blocks_before_enhancements(self):
        reply = process_reply(
            "Visible\n<silent>skip [secret](file:///tmp/secret.txt)\n---\n[Hidden]</silent>\nDone"
        )

        self.assertEqual(reply.text, "Visible\n\nDone")
        self.assertEqual(reply.files, [])
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_silent_file_link_literal_inside_code(self):
        specimens = [
            "Example `<silent>[file](file:///tmp/secret.txt)</silent>` remains.",
            "```markdown\n<silent>[file](file:///tmp/secret.txt)</silent>\n```",
            "```markdown\n> <silent>[file](file:///tmp/secret.txt)</silent>\n```",
        ]

        for text in specimens:
            with self.subTest(text=text):
                reply = process_reply(text)
                self.assertEqual(reply.text, text)
                self.assertEqual(reply.files, [])

    def test_process_reply_only_extracts_file_links_outside_markdown_code(self):
        text = (
            "Attach [report](file:///tmp/report.txt); preserve "
            "`<silent>[example](file:///tmp/secret.txt)</silent>`."
        )

        reply = process_reply(text)

        self.assertEqual(
            reply.text,
            "Attach report; preserve "
            "`<silent>[example](file:///tmp/secret.txt)</silent>`.",
        )
        self.assertEqual([file.path for file in reply.files], ["/tmp/report.txt"])

    def test_process_reply_recovers_attachment_label_from_original_text(self):
        reply = process_reply("[see `report`](file:///tmp/report.txt)")

        self.assertEqual(reply.text, "see `report`")
        self.assertEqual([file.label for file in reply.files], ["see `report`"])

    def test_process_reply_uses_shared_code_mask_for_secret_requests(self):
        text = (
            "````markdown\n"
            "text ``` inner\n"
            "<silent>$<OPENAI_KEY></silent>\n"
            "````"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.secret_requests, [])

    def test_process_reply_uses_shared_code_mask_for_quick_replies(self):
        text = (
            "```markdown\n"
            "<silent>literal</silent>\n"
            "---\n"
            "[Yes] | [No]"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_unseparated_button_row_in_code(self):
        text = "```markdown\n[Yes] | [No]\n```"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_unseparated_button_row_in_raw_html(self):
        text = "<div>\n[A] | [B]"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_accepts_row_after_closed_raw_html_block(self):
        reply = process_reply("<script>\ncontent\n</script>\n[A] | [B]")

        self.assertEqual(reply.text, "<script>\ncontent\n</script>")
        self.assertEqual([button.text for button in reply.buttons], ["A", "B"])

    def test_process_reply_preserves_oversized_separator_free_row(self):
        text = "Grades:\n[A] | [B] | [C] | [D] | [E] | [F]"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_ignores_indented_code_when_scanning_table_context(self):
        text = "    Head | Status\n    --- | ---\n[A] | [B]"

        reply = process_reply(text)

        self.assertEqual(reply.text, "    Head | Status\n    --- | ---")
        self.assertEqual([button.text for button in reply.buttons], ["A", "B"])

    def test_silent_parser_preserves_inline_code_and_trailing_report_byte_for_byte(self):
        trailing_report = "\n".join(
            [
                "2. Keep queue and cancellation work in their existing lanes.",
                "3. Persist the complete terminal message and callback payload.",
                "4. Re-run the exact-head review and CI gates.",
            ]
        )
        text = (
            "Intermediate assistant text must not leave its Session; "
            "the literal directive is `<silent>`.\n\n"
            f"{trailing_report}"
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_preserves_fenced_code_byte_for_byte(self):
        text = (
            "Examples:\n\n"
            "```markdown\n"
            "<silent>complete example</silent>\n"
            "<silent>\n"
            "substantial text after the unmatched literal\n"
            "```\n\n"
            "~~~text\n"
            "<SILENT data-example=\"true\">\n"
            "more literal text\n"
            "~~~\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_preserves_container_fences_byte_for_byte(self):
        text = (
            "- ```text\n"
            "  <silent>list literal</silent>\n"
            "  ```\n\n"
            "> ~~~text\n"
            "> <silent>quote literal</silent>\n"
            "> ~~~\n\n"
            "> - ````markdown\n"
            ">   ```nested```\n"
            ">   <silent>nested literal</silent>\n"
            ">   ````\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_preserves_tab_indented_list_fence(self):
        text = (
            "-\t```text\n"
            "\t<silent>tab-indented literal</silent>\n"
            "\t```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_preserves_continuation_line_list_fence(self):
        text = (
            "10. Example\n"
            "    ```text\n"
            "    <silent>continuation literal</silent>\n"
            "    ```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_retains_list_state_across_lazy_continuation(self):
        text = (
            "- item\n"
            "lazy continuation\n"
            "    ```text\n"
            "    <silent>lazy list literal</silent>\n"
            "    ```"
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_tracks_nested_list_context_across_lines(self):
        text = (
            "- Outer\n"
            "  1. Inner\n"
            "     ```text\n"
            "     <silent>nested continuation literal</silent>\n"
            "     ```\n"
            "  ```text\n"
            "  > <silent>outer continuation literal</silent>\n"
            "  ```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_tracks_empty_ordered_list_item(self):
        text = (
            "10.\n"
            "    ```text\n"
            "    <silent>empty-item continuation literal</silent>\n"
            "    ```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_tracks_quote_nested_inside_list(self):
        text = (
            "- > ```text\n"
            "  > <silent>list quote literal</silent>\n"
            "  > ```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_enforces_list_indent_before_nested_quote(self):
        text = (
            "- > ```text\n"
            "> <silent>remove outside code</silent>\n"
            "> ```"
        )

        self.assertEqual(strip_silent_blocks(text), "- > ```text\n> \n> ```")

    def test_silent_parser_tracks_alternating_quote_and_list_containers(self):
        text = (
            "> - > - ```text\n"
            ">   >   <silent>alternating container literal</silent>\n"
            ">   >   ```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_preserves_inline_code_after_non_one_list_text(self):
        text = (
            "Paragraph\n"
            "10. Not a list interruption\n"
            "    ```text\n"
            "    <silent>paragraph code literal</silent>\n"
            "    ```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_rejects_empty_list_interrupting_paragraph(self):
        text = (
            "Paragraph\n"
            "-\n"
            "    ```text\n"
            "  <silent>remove outside code</silent>\n"
            "    ```\n"
            "Tail remains."
        )
        expected = (
            "Paragraph\n"
            "-\n"
            "    ```text\n"
            "  \n"
            "    ```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_uses_list_relative_indented_code_threshold(self):
        text = (
            "- item\n"
            "\n"
            "    <silent>remove list paragraph content</silent>\n"
            "\n"
            "      <silent>preserve list indented code</silent>"
        )
        expected = (
            "- item\n"
            "\n"
            "    \n"
            "\n"
            "      <silent>preserve list indented code</silent>"
        )

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_uses_nested_list_indented_code_threshold(self):
        text = (
            "- outer\n"
            "  - middle\n"
            "    - inner\n"
            "\n"
            "        <silent>remove inner list content</silent>"
        )

        self.assertEqual(
            strip_silent_blocks(text),
            "- outer\n  - middle\n    - inner",
        )

    def test_silent_parser_preserves_alternating_container_indented_code(self):
        text = "- >     <silent>alternating indented literal</silent>"

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_resets_paragraph_state_after_heading(self):
        text = (
            "# Heading\n"
            "10. item\n"
            "    ```text\n"
            "    <silent>heading list literal</silent>\n"
            "    ```\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_preserves_inline_code_after_orphan_setext_marker(self):
        text = (
            "===\n"
            "10. item\n"
            "    ```text\n"
            "    <silent>paragraph code literal</silent>\n"
            "    ```"
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_keeps_invalid_fence_line_in_paragraph_state(self):
        text = (
            "``` foo`bar\n"
            "10. item\n"
            "    ```text\n"
            "    <silent>remove outside code</silent>\n"
            "    ```"
        )

        self.assertEqual(
            strip_silent_blocks(text),
            "``` foo`bar\n10. item\n    ```text\n    \n    ```",
        )

    def test_silent_parser_allows_list_after_link_reference_definition(self):
        text = (
            "[ref]: /url\n"
            "10. item\n"
            "    ```text\n"
            "    <silent>link definition literal</silent>\n"
            "    ```"
        )

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_stops_unclosed_fence_at_container_boundary(self):
        text = (
            "- ```text\n"
            "  <silent>list literal</silent>\n"
            "List ended.\n"
            "<silent>remove after list</silent>\n\n"
            "> ~~~text\n"
            "> <silent>quote literal</silent>\n"
            "Quote ended.\n"
            "<silent>remove after quote</silent>\n"
            "Tail remains."
        )
        expected = (
            "- ```text\n"
            "  <silent>list literal</silent>\n"
            "List ended.\n"
            "\n\n"
            "> ~~~text\n"
            "> <silent>quote literal</silent>\n"
            "Quote ended.\n"
            "\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_removes_real_blocks_while_preserving_code_examples(self):
        text = (
            "Start\n"
            "<silent>remove one</silent>\n"
            "Inline `<silent>literal one</silent>` stays.\n"
            "<SILENT reason=\"hidden\">remove two</silent >\n"
            "````markdown\n"
            "```nested fence```\n"
            "<silent>literal two</silent>\n"
            "````\n"
            "End"
        )
        expected = (
            "Start\n"
            "\n"
            "Inline `<silent>literal one</silent>` stays.\n"
            "\n"
            "````markdown\n"
            "```nested fence```\n"
            "<silent>literal two</silent>\n"
            "````\n"
            "End"
        )

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_handles_backtick_and_escape_edges(self):
        protected = "Double ``literal `<silent>` marker`` remains."
        escaped = r"Before \`<silent>remove me</silent>\` after"
        partial_escape = "\\``<silent>literal</silent>`"
        escaped_closer = r"`<silent>literal</silent>\`"

        self.assertEqual(strip_silent_blocks(protected), protected)
        self.assertEqual(strip_silent_blocks(escaped), r"Before \`\` after")
        self.assertEqual(strip_silent_blocks(partial_escape), partial_escape)
        self.assertEqual(strip_silent_blocks(escaped_closer), escaped_closer)

    def test_silent_parser_preserves_multiline_inline_code_span(self):
        text = "`foo\n<silent>multiline literal</silent>\nbar`\nTail remains."

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_ignores_backticks_inside_raw_html_tags(self):
        text = '<a title="`">x</a> <silent>remove me</silent> `tail'

        self.assertEqual(
            strip_silent_blocks(text),
            '<a title="`">x</a>  `tail',
        )

    def test_silent_parser_rejects_invalid_raw_html_tag_grammar(self):
        text = '<a? title="`"> [literal](file:///tmp/secret.txt) `'

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.files, [])

    def test_silent_parser_honors_escape_before_raw_html_candidate(self):
        text = r'\<a title="`"> [literal](file:///tmp/secret.txt) `'

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.files, [])

    def test_silent_parser_rejects_unicode_space_in_raw_html_tag(self):
        text = '<a\u00a0title="`"> [literal](file:///tmp/secret.txt) `'

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.files, [])

    def test_silent_parser_rejects_attributes_on_raw_html_closing_tags(self):
        text = '</a title="`"> [literal](file:///tmp/secret.txt) `'

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.files, [])

    def test_silent_parser_parses_html_as_text_inside_open_code_span(self):
        text = (
            '`prefix <a title="`"> '
            "<silent>remove outside code</silent> "
            "`tail`"
        )

        self.assertEqual(
            strip_silent_blocks(text),
            '`prefix <a title="`">  `tail`',
        )

    def test_silent_parser_ignores_backticks_inside_uri_autolinks(self):
        text = (
            "<https://example.com/`> "
            "<silent>remove outside code</silent> "
            "`tail"
        )

        self.assertEqual(
            strip_silent_blocks(text),
            "<https://example.com/`>  `tail",
        )

    def test_silent_parser_strips_quote_prefixes_before_inline_html(self):
        text = (
            "> text <!A\n"
            "> `> <silent>remove outside code</silent> `tail"
        )

        self.assertEqual(
            strip_silent_blocks(text),
            "> text <!A\n> `>  `tail",
        )

    def test_silent_parser_maps_normalized_nul_to_original_source(self):
        text = (
            "`before\x00"
            "[literal](file:///tmp/secret.txt) "
            "<silent>literal</silent>"
            "after`"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.files, [])

    def test_silent_parser_maps_tab_expanded_list_continuation(self):
        text = (
            "- item\n"
            "\t`<silent>[literal](file:///tmp/secret.txt)</silent>`"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.files, [])

    def test_silent_parser_handles_many_unmatched_backtick_runs(self):
        unmatched_runs = " ".join("`" * length for length in range(2, 502))
        text = f"Literal `<silent>` remains.\n{unmatched_runs}\nTail remains."

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_does_not_pair_inline_spans_across_lines(self):
        text = "    ```\n<silent>remove outside code</silent>\n    ```\nTail remains."

        self.assertEqual(
            strip_silent_blocks(text),
            "    ```\n\n    ```\nTail remains.",
        )

    def test_silent_parser_ends_quoted_fence_at_unmarked_blank_line(self):
        text = (
            "> ```text\n"
            "\n"
            "> <silent>remove outside code</silent>\n"
            "> ```"
        )

        self.assertEqual(strip_silent_blocks(text), "> ```text\n\n> \n> ```")

    def test_silent_parser_preserves_indented_code_literal(self):
        text = "Example:\n\n    <silent>indented literal</silent>\nTail remains."

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_preserves_quoted_indented_code_literal(self):
        text = ">     <silent>quoted indented literal</silent>"

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_ignores_fences_inside_raw_html_blocks(self):
        text = (
            "<pre>\n"
            "```\n"
            "<silent>remove real directive</silent>\n"
            "```\n"
            "</pre>\n"
            "Tail remains."
        )
        expected = "<pre>\n```\n\n```\n</pre>\nTail remains."

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_does_not_mask_indented_content_in_raw_html_block(self):
        text = (
            "<pre>\n"
            "\n"
            "    <silent>remove real directive</silent>\n"
            "</pre>"
        )

        self.assertEqual(strip_silent_blocks(text), "<pre>\n\n    \n</pre>")

    def test_silent_parser_ignores_fences_inside_html_comment_blocks(self):
        text = (
            "<!--\n"
            "```\n"
            "-->\n"
            "<silent>remove real directive</silent>\n"
            "```"
        )

        self.assertEqual(strip_silent_blocks(text), "<!--\n```\n-->\n\n```")

    def test_silent_parser_does_not_parse_code_spans_in_unterminated_html_blocks(self):
        for opener in ("<!--", "<?target", "<![CDATA[", "<!DOCTYPE"):
            with self.subTest(opener=opener):
                text = f"{opener}\n`<silent>hidden</silent>`"

                self.assertEqual(strip_silent_blocks(text), f"{opener}\n``")

    def test_process_reply_keeps_fence_newline_before_quick_replies(self):
        text = "```text\nexample\n```\n---\n[Yes] | [No]"

        reply = process_reply(text)

        self.assertEqual(reply.text, "```text\nexample\n```")
        self.assertEqual([button.text for button in reply.buttons], ["Yes", "No"])

    def test_silent_parser_removes_many_control_blocks_in_one_pass(self):
        blocks = "".join(
            f"visible-{index}<silent>hidden-{index}</silent>"
            for index in range(2_000)
        )
        expected = "".join(f"visible-{index}" for index in range(2_000))

        self.assertEqual(strip_silent_blocks(blocks), expected)

    def test_silent_parser_parses_progressively_exposed_blocks_once(self):
        lengths = range(1, 201)
        blocks = "".join(
            f"<silent>{'`' * length}</silent>" for length in lengths
        )
        closers = " ".join("`" * length for length in lengths)
        text = f"prefix {blocks} {closers}"

        with patch.object(
            reply_enhancer._BLOCK_MARKDOWN,
            "parse",
            wraps=reply_enhancer._BLOCK_MARKDOWN.parse,
        ) as parse:
            result = strip_silent_blocks(text)

        self.assertEqual(result, f"prefix  {closers}")
        self.assertEqual(parse.call_count, 1)

    def test_silent_parser_handles_many_malformed_html_prefixes_linearly(self):
        text = "<a" * 8_000 + " `<silent>literal</silent>`"

        self.assertEqual(strip_silent_blocks(text), text)

    def test_silent_parser_strips_many_malformed_html_comments_linearly(self):
        text = "<!--" * 8_000 + " `<silent>literal</silent>`"

        self.assertEqual(strip_silent_blocks(text), "<!--" * 8_000 + " ``")

    def test_reply_enhancer_treats_unicode_digits_as_plain_text(self):
        text = "². item\n①. item"

        self.assertEqual(process_reply(text).text, text)

    def test_silent_parser_does_not_pair_inline_spans_across_cr_lines(self):
        text = "    ```\r<silent>remove outside code</silent>\r    ```\rTail remains."

        self.assertEqual(
            strip_silent_blocks(text),
            "    ```\r\r    ```\rTail remains.",
        )

    def test_silent_parser_closes_real_block_before_parsing_hidden_markdown(self):
        text = "Before\n<silent>\n```\nhidden\n</silent>\nTail remains."

        self.assertEqual(strip_silent_blocks(text), "Before\n\nTail remains.")

    def test_silent_parser_keeps_hidden_fences_from_masking_later_blocks(self):
        text = (
            "Before\n"
            "<silent>\n```\nhidden\n</silent>\n"
            "<silent>second secret</silent>\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), "Before\n\n\nTail remains.")

    def test_silent_parser_restores_code_literals_after_hidden_fence(self):
        text = (
            "Before\n"
            "<silent>\n````\nhidden\n</silent>\n"
            "```text\n"
            "<silent>literal\n```\n</silent>\n"
            "<silent>remove two</silent>\n"
            "Tail remains."
        )
        expected = (
            "Before\n\n"
            "```text\n"
            "<silent>literal\n```\n</silent>\n\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_process_reply_reparses_after_multiline_control_html_block(self):
        text = (
            "<silent>\ninternal\n</silent>\n"
            "`<silent>inline literal</silent>`\n"
            "```text\n"
            "<silent>[literal](file:///tmp/secret.txt)</silent>\n"
            "```\n"
            "Tail remains."
        )
        expected = (
            "`<silent>inline literal</silent>`\n"
            "```text\n"
            "<silent>[literal](file:///tmp/secret.txt)</silent>\n"
            "```\n"
            "Tail remains."
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, expected)
        self.assertEqual(reply.files, [])

    def test_silent_parser_reparses_unmatched_literal_after_html_block(self):
        tail = "\n".join(f"substantial trailing line {index}" for index in range(50))
        text = (
            "<silent>\ninternal\n</silent>\n"
            "`<silent>unmatched literal opener`\n"
            f"{tail}"
        )
        expected = "`<silent>unmatched literal opener`\n" + tail

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_does_not_cross_pair_provisional_code_literal(self):
        text = (
            "<silent>\ninternal\n</silent>\n"
            "`<silent>unmatched literal opener`\n"
            "<silent>remove later control</silent>\n"
            "Tail remains."
        )
        expected = (
            "`<silent>unmatched literal opener`\n\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_reparses_html_block_inside_quote(self):
        text = (
            "> <silent>\n"
            "> internal\n"
            "> </silent>\n"
            "> `<silent>literal</silent>`\n"
            "> Tail remains."
        )
        expected = (
            "> \n"
            "> `<silent>literal</silent>`\n"
            "> Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_does_not_pair_code_opener_with_real_closer(self):
        text = (
            "```text\n"
            "<silent>unterminated literal\n"
            "```\n"
            "<silent>remove real block</silent>\n"
            "Tail remains."
        )
        expected = (
            "```text\n"
            "<silent>unterminated literal\n"
            "```\n\n"
            "Tail remains."
        )

        self.assertEqual(strip_silent_blocks(text), expected)

    def test_silent_parser_bounds_hidden_unmatched_fence_refinement(self):
        blocks = "".join(
            f"<silent>\n{'`' * length}\nhidden\n</silent>\n"
            for length in range(205, 4, -1)
        )

        with patch.object(
            reply_enhancer._BLOCK_MARKDOWN,
            "parse",
            wraps=reply_enhancer._BLOCK_MARKDOWN.parse,
        ) as parse:
            result = strip_silent_blocks(f"Before\n{blocks}Tail remains.")

        self.assertNotIn("<silent", result)
        self.assertTrue(result.startswith("Before\n"))
        self.assertTrue(result.endswith("Tail remains."))
        self.assertLessEqual(parse.call_count, 4)

    def test_silent_parser_partitions_sorted_ranges_in_one_pass(self):
        class CountingPosition:
            comparisons = 0

            def __init__(self, value):
                self.value = value

            def __le__(self, other):
                type(self).comparisons += 1
                return self.value <= other.value

            def __lt__(self, other):
                type(self).comparisons += 1
                return self.value < other.value

        count = 8_000
        containers = [
            (CountingPosition(index * 4), CountingPosition(index * 4 + 1))
            for index in range(count)
        ]
        ranges = [
            source_range
            for index in range(count)
            for source_range in (
                (CountingPosition(index * 4), CountingPosition(index * 4)),
                (
                    CountingPosition(index * 4 + 2),
                    CountingPosition(index * 4 + 2),
                ),
            )
        ]

        outside, inside = reply_enhancer._partition_ranges_by_start(
            ranges,
            containers,
        )

        self.assertEqual(len(outside), count)
        self.assertEqual(len(inside), count)
        self.assertLess(
            CountingPosition.comparisons,
            8 * (len(ranges) + len(containers)),
        )

    def test_silent_parser_scans_malformed_openers_without_unbounded_regex(self):
        text = "<silent" * 16_000 + "\nTail remains."

        with patch.object(
            reply_enhancer,
            "_SILENT_OPEN_RE",
            None,
            create=True,
        ):
            self.assertEqual(strip_silent_blocks(text), text)

    def test_process_reply_keeps_original_enhancement_eligibility(self):
        text = (
            "`<silent>hidden</silent>``\n"
            "[report](file:///tmp/report.txt)\n"
            "$<REPORT_TOKEN>\n"
            "---\n"
            "[Continue] | [Stop]"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, "```\nreport\n$<REPORT_TOKEN>")
        self.assertEqual([file.path for file in reply.files], ["/tmp/report.txt"])
        self.assertEqual(
            [request.name for request in reply.secret_requests],
            ["REPORT_TOKEN"],
        )
        self.assertEqual(
            [button.text for button in reply.buttons],
            ["Continue", "Stop"],
        )

    async def test_base_agent_result_keeps_attachment_eligibility_after_silent_removal(self):
        class _DispatchingController(_StubController):
            def __init__(self):
                super().__init__("slack")
                self.config.show_duration = False
                self.dispatcher = ConsolidatedMessageDispatcher(self)

            async def emit_agent_message(self, context, message_type, text, **kwargs):
                return await self.dispatcher.emit_agent_message(
                    context,
                    message_type,
                    text,
                    **kwargs,
                )

        controller = _DispatchingController()
        upload_file_links = AsyncMock()
        controller.dispatcher._upload_file_links = upload_file_links
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
        )
        source = (
            "`<silent>hidden</silent>``\n"
            "[report](file:///tmp/report.txt)"
        )

        await _StubAgent(controller).emit_result_message(
            context,
            source,
            duration_ms=0,
        )

        uploaded_files = upload_file_links.await_args.args[2]
        self.assertEqual(
            [(file.label, file.path) for file in uploaded_files],
            [("report", "/tmp/report.txt")],
        )
        self.assertEqual(
            controller.im_client.sent_messages,
            [("C1", "```\nreport", "markdown")],
        )

    def test_silent_parser_keeps_unterminated_recovery_outside_code(self):
        text = "Visible result\n<silent>unfinished hidden diagnostic\nmust not leak"

        self.assertEqual(strip_silent_blocks(text), "Visible result")

    def test_silent_parser_keeps_all_silent_response_empty(self):
        self.assertEqual(strip_silent_blocks("<silent>internal only</silent>"), "")

    def test_silent_parser_preserves_boundary_indented_code(self):
        text = (
            "<silent>remove real directive</silent>\n\n"
            "    <silent>[literal](file:///tmp/secret.txt)</silent>"
        )

        reply = process_reply(text)

        self.assertEqual(
            reply.text,
            "    <silent>[literal](file:///tmp/secret.txt)</silent>",
        )
        self.assertEqual(reply.files, [])

    def test_silent_parser_does_not_reparse_synthesized_openers(self):
        text = (
            "Visible <sil"
            "<silent>hidden</silent>"
            "ent>KEEP</silent> tail"
        )

        self.assertEqual(
            strip_silent_blocks(text),
            "Visible <silent>KEEP</silent> tail",
        )

    def test_process_reply_can_disable_quick_reply_button_parsing_only(self):
        reply = process_reply(
            "Report [file](file:///tmp/report.txt)\n\n---\n[Continue] | [Stop]",
            include_quick_replies=False,
        )

        self.assertEqual(reply.text, "Report file\n\n---\n[Continue] | [Stop]")
        self.assertEqual([file.path for file in reply.files], ["/tmp/report.txt"])
        self.assertEqual(reply.buttons, [])

    def test_process_reply_can_disable_only_separator_free_parsing(self):
        reply = process_reply(
            "Done.\n[A] | [B]",
            allow_unseparated_quick_replies=False,
        )

        self.assertEqual(reply.text, "Done.\n[A] | [B]")
        self.assertEqual(reply.buttons, [])

        explicit = process_reply(
            "Done.\n---\n[A] | [B]",
            allow_unseparated_quick_replies=False,
        )
        self.assertEqual(explicit.text, "Done.")
        self.assertEqual([button.text for button in explicit.buttons], ["A", "B"])

    def test_process_reply_accepts_markdown_link_style_quick_reply_button(self):
        reply = process_reply(
            "Done.\n\n---\n"
            "[:eyes: 看 PR](<https://github.com/avibe-bot/avibe/pull/298>) | "
            "[:rocket: 等评审完合并] | [:test_tube: 先回归测一遍]"
        )

        self.assertEqual(reply.text, "Done.")
        self.assertEqual(
            [button.text for button in reply.buttons],
            [":eyes: 看 PR", ":rocket: 等评审完合并", ":test_tube: 先回归测一遍"],
        )

    def test_process_reply_preserves_bodyless_pipe_row_without_rule(self):
        text = "[A] | [B]"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_whitespace_only_body_without_rule(self):
        text = "\n[A] | [B]"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_final_row_of_markdown_table(self):
        text = (
            "Option | Status\n"
            "--- | ---\n"
            "[Docs](https://example.com) | [Open]\n"
            "[Issue](https://example.com/1) | [Closed]"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_short_markdown_table_delimiter(self):
        text = "Option | Status\n- | -\n[A] | [B]"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_accepts_slack_angle_link_style_quick_reply_button(self):
        reply = process_reply(
            "Done.\n\n---\n"
            "<https://github.com/avibe-bot/avibe/pull/298|:eyes: 看 PR> | "
            "[:rocket: 等评审完合并] | [:test_tube: 先回归测一遍]"
        )

        self.assertEqual(reply.text, "Done.")
        self.assertEqual(
            [button.text for button in reply.buttons],
            [":eyes: 看 PR", ":rocket: 等评审完合并", ":test_tube: 先回归测一遍"],
        )

    def test_process_reply_accepts_pipe_separated_buttons_without_rule(self):
        reply = process_reply("Done.\n[查看冲突] | [继续修复]")

        self.assertEqual(reply.text, "Done.")
        self.assertEqual(
            [button.text for button in reply.buttons],
            ["查看冲突", "继续修复"],
        )

    def test_process_reply_accepts_buttons_after_blank_line(self):
        reply = process_reply("Table | Value\n\n[A] | [B]")

        self.assertEqual(reply.text, "Table | Value")
        self.assertEqual([button.text for button in reply.buttons], ["A", "B"])

    def test_process_reply_preserves_lazy_markdown_container_continuations(self):
        for text in (
            "> Compare these states:\n[A] | [B]",
            "- Compare these states:\n[A] | [B]",
            "1. Compare these states:\n[A] | [B]",
        ):
            with self.subTest(text=text):
                reply = process_reply(text)

                self.assertEqual(reply.text, text)
                self.assertEqual(reply.buttons, [])

    def test_process_reply_accepts_fullwidth_pipe_without_rule(self):
        reply = process_reply("Done.\n[A] | [B]")

        self.assertEqual(reply.text, "Done.")
        self.assertEqual([button.text for button in reply.buttons], ["A", "B"])

    def test_process_reply_accepts_trailing_pipe_without_rule(self):
        for text in ("Done.\n[A] | [B] |", "Done.\n[A] | [B] |"):
            with self.subTest(text=text):
                reply = process_reply(text)

                self.assertEqual(reply.text, "Done.")
                self.assertEqual([button.text for button in reply.buttons], ["A", "B"])

    def test_process_reply_preserves_single_bracket_line_without_rule(self):
        text = "Use this value:\n[example]"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_unseparated_row_with_blank_button_label(self):
        text = "Checkbox states:\n[ ] | [Checked]"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_plain_link_without_rule(self):
        text = "Done.\n[Release notes](https://example.com)"

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_separator_free_markdown_link_list(self):
        text = (
            "Links\n"
            "[Documentation](https://example.com/docs) | "
            "[Issues](https://example.com/issues)"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_separator_free_slack_link_list(self):
        text = (
            "Links\n"
            "[Documentation](<https://example.com/docs>) | "
            "<https://example.com/issues|Issues>"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_markdown_table_last_row(self):
        text = (
            "Option | Status\n"
            "--- | ---\n"
            "[Docs](https://example.com) | [Issue](https://example.com/1)"
        )

        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_accepts_unseparated_buttons_with_crlf(self):
        reply = process_reply("Done\r\n[A] | [B]\r\n")

        self.assertEqual(reply.text, "Done")
        self.assertEqual([button.text for button in reply.buttons], ["A", "B"])

    def test_process_reply_ignores_bare_angle_link_as_quick_reply_button(self):
        text = "Done.\n\n---\n<https://github.com/avibe-bot/avibe/pull/298>"
        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_plain_markdown_reference_link_block(self):
        text = "Done.\n\n---\n[Release notes](https://example.com)"
        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_accepts_plain_markdown_link_button_within_group(self):
        # Regression: a plain ``[label](https://…)`` token used to drop EVERY
        # button in the group (its trailing ``(url)`` broke the end-anchored
        # block match). A link inside a ``|`` group must render as a button, with
        # the label as the payload and the URL discarded.
        reply = process_reply(
            "Done.\n\n---\n"
            "[👀 我先确认] | [🔗 看 PR](https://github.com/avibe-bot/avibe/pull/451) | [✅ 直接合并]"
        )

        self.assertEqual(reply.text, "Done.")
        self.assertEqual(
            [button.text for button in reply.buttons],
            ["👀 我先确认", "🔗 看 PR", "✅ 直接合并"],
        )

    def test_process_reply_accepts_plain_markdown_link_button_as_last_token(self):
        reply = process_reply("Done.\n\n---\n[A] | [docs](https://example.com)")

        self.assertEqual(reply.text, "Done.")
        self.assertEqual([button.text for button in reply.buttons], ["A", "docs"])

    def test_process_reply_preserves_lone_plain_link_with_pipe_in_url(self):
        # A lone reference link whose URL contains ``|`` must still be preserved
        # as text: the lone-link disambiguation matches the whole block rather than
        # scanning for a stray ``|`` (which a URL may legitimately hold).
        text = "Done.\n\n---\n[chart](https://example.com/a?b=1|2)"
        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_preserves_multiple_plain_reference_links_without_separator(self):
        # Several plain Markdown links after ``---`` with no ``|`` separator are a
        # reference-link section, not a button group — they must stay as text.
        text = "Done.\n\n---\n[Release notes](https://example.com/r)\n[Changelog](https://example.com/c)"
        reply = process_reply(text)

        self.assertEqual(reply.text, text)
        self.assertEqual(reply.buttons, [])

    def test_process_reply_accepts_plain_link_button_with_balanced_parens_in_url(self):
        # A plain-link button whose URL contains balanced parentheses (e.g. a
        # Wikipedia ``A_(B)`` target) must not truncate at the first ``)`` and drop
        # the group.
        reply = process_reply("Done.\n\n---\n[Wiki](https://en.wikipedia.org/wiki/A_(B)) | [Done]")

        self.assertEqual(reply.text, "Done.")
        self.assertEqual([button.text for button in reply.buttons], ["Wiki", "Done"])

    def test_process_reply_accepts_placeholder_link_buttons(self):
        # Agents sometimes write every button as a ``[label](#)`` link. A
        # placeholder target points nowhere in a chat message, so the row must
        # render as buttons instead of a line of dead links.
        reply = process_reply(
            "你定一下先做哪个。\n\n---\n"
            "[✅ 三个都做](#) | [📝 先改提示词](#) | [🔁 先做重启续跑](#) | [⏳ 等2230合并再说](#)"
        )

        self.assertEqual(reply.text, "你定一下先做哪个。")
        self.assertEqual(
            [button.text for button in reply.buttons],
            ["✅ 三个都做", "📝 先改提示词", "🔁 先做重启续跑", "⏳ 等2230合并再说"],
        )

    def test_process_reply_reads_placeholder_link_like_bare_button(self):
        cases = {
            "Done.\n\n---\n[A](#)": ["A"],
            "Done.\n\n---\n[A]() | [B](#next) | [C]": ["A", "B", "C"],
            "Done.\n[A](#) | [B](#b)": ["A", "B"],
        }
        for text, labels in cases.items():
            with self.subTest(text=text):
                reply = process_reply(text)

                self.assertEqual(reply.text, "Done.")
                self.assertEqual([button.text for button in reply.buttons], labels)

    def test_process_reply_preserves_relative_link_row(self):
        for text in (
            "Done.\n\n---\n[Guide](docs/guide.md) | [Notes](notes.md)",
            "Done.\n[Guide](docs/guide.md) | [Notes](notes.md)",
        ):
            with self.subTest(text=text):
                reply = process_reply(text)

                self.assertEqual(reply.text, text)
                self.assertEqual(reply.buttons, [])

    def test_prompt_keeps_harness_routing_and_moves_operational_detail_to_skill(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )
        enabled_agents = [
            SimpleNamespace(
                name="codex",
                normalized_name="codex",
                backend="codex",
                description="Codex compatibility Agent for existing sessions",
            ),
            SimpleNamespace(
                name="Release Auditor",
                normalized_name="release-auditor",
                backend="claude",
                description="Review releases | verify follow-up risk",
            ),
            SimpleNamespace(
                name="--Review Bot",
                normalized_name="",
                backend="codex",
                description="Name needs prompt-safe normalization",
            ),
        ]

        with patch("core.managed_skills.resolve_skills", return_value=_resolved_core_skills()):
            prompt = build_system_prompt_injection(
                include_quick_replies=True,
                context=context,
                enabled_agents=enabled_agents,
                skills_cwd=Path("/tmp/project"),
            )

        self.assertIn("## Show Pages", prompt)
        self.assertIn("load the `use-show-pages` Skill", prompt)
        self.assertNotIn("`vibe show status`", prompt)
        self.assertIn("## Harness", prompt)
        self.assertIn("load the `use-avibe-harness` Skill", prompt)
        self.assertNotIn("Backend-native background work", prompt)
        self.assertNotIn("tool-layer", prompt)
        self.assertNotIn("### Mental model", prompt)
        self.assertNotIn("Watch waiter contract", prompt)
        self.assertEqual(prompt.count("Current session id: `sesk8m4q2p7x`"), 1)
        self.assertIn("| Agent Name | Backend | Agent Description |", prompt)
        self.assertIn("| codex | codex | Codex compatibility Agent for existing sessions |", prompt)
        self.assertIn(r"| release-auditor | claude | Review releases \| verify follow-up risk |", prompt)
        self.assertIn("| review-bot | codex | Name needs prompt-safe normalization |", prompt)
        self.assertLess(prompt.index("| codex |"), prompt.index("| release-auditor |"))
        self.assertLess(prompt.index("| release-auditor |"), prompt.index("| review-bot |"))
        self.assertIn("## Project Context and History", prompt)

        skill = (Path(__file__).resolve().parents[1] / "skills" / "use-avibe-harness" / "SKILL.md").read_text()
        self.assertIn("Avibe Harness turns user intent into durable Agent work", skill)
        self.assertIn("Backend-native work is process-local", skill)
        self.assertIn("anything that may outlive the turn through Harness", skill)
        self.assertIn("### Mental model", skill)
        self.assertIn("Watch waiter contract", skill)
        self.assertIn("vibe harness status", skill)
        self.assertIn("vibe watch add", skill)

    def test_prompt_is_byte_stable_when_agent_input_order_changes(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )
        enabled_agents = [
            SimpleNamespace(name="zeta", normalized_name="zeta", backend="codex", description="Last"),
            SimpleNamespace(name="Alpha", normalized_name="alpha", backend="claude", description="First"),
            SimpleNamespace(name="beta", normalized_name="beta", backend="opencode", description="Middle"),
        ]

        forward = build_system_prompt_injection(
            include_quick_replies=False,
            context=context,
            enabled_agents=enabled_agents,
        )
        reverse = build_system_prompt_injection(
            include_quick_replies=False,
            context=context,
            enabled_agents=reversed(enabled_agents),
        )

        self.assertEqual(forward, reverse)
        self.assertLess(forward.index("| alpha |"), forward.index("| beta |"))
        self.assertLess(forward.index("| beta |"), forward.index("| zeta |"))


    def test_show_page_history_is_skill_scoped_and_does_not_change_prompt(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )

        with patch("core.show_git.show_git_checkpointing_active", return_value=False):
            unavailable = build_system_prompt_injection(include_quick_replies=True, context=context)
        with (
            patch("core.show_git.show_git_checkpointing_active", return_value=True),
            patch("core.show_git._workspace_is_self_managed", return_value=False),
        ):
            managed = build_system_prompt_injection(include_quick_replies=True, context=context)
        with (
            patch("core.show_git.show_git_checkpointing_active", return_value=True),
            patch("core.show_git._workspace_is_self_managed", return_value=True),
        ):
            self_managed = build_system_prompt_injection(include_quick_replies=True, context=context)

        self.assertEqual(managed, self_managed)
        self.assertEqual(managed, unavailable)
        self.assertNotIn("History contract:", unavailable)
        self.assertIn("Before creating, updating, or restoring a Show Page", managed)
        skill = (Path(__file__).resolve().parents[1] / "skills" / "use-show-pages" / "SKILL.md").read_text()
        self.assertIn("### Show Page workspace history", skill)
        self.assertIn("These rules apply only to this Session's Show Page workspace", skill)
        self.assertIn("`history.mode` is `managed`", skill)
        self.assertIn("`history.mode` is `self-managed`", skill)
        self.assertIn("Separately entrusted repository work follows the user's mandate", skill)
        self.assertNotIn("Follow the History contract in the current System Prompt", skill)
        self.assertIn("`vibe show status`", skill)

    def test_prompt_does_not_render_empty_agents_as_invokable_table_row(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )

        missing_store_prompt = build_system_prompt_injection(
            include_quick_replies=False,
            context=context,
            enabled_agents=None,
        )
        empty_store_prompt = build_system_prompt_injection(
            include_quick_replies=False,
            context=context,
            enabled_agents=[],
        )

        self.assertNotIn("### Agents", missing_store_prompt)
        self.assertNotIn("### Agents", empty_store_prompt)
        self.assertNotIn("No enabled Agents", missing_store_prompt)
        self.assertNotIn("No Agents are currently enabled", empty_store_prompt)
        self.assertNotIn("| (none) |", missing_store_prompt)
        self.assertNotIn("| (none) |", empty_store_prompt)

    def test_show_page_prompt_describes_only_the_available_capability(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )

        with patch("core.managed_skills.resolve_skills", return_value=_resolved_core_skills()):
            prompt = build_system_prompt_injection(
                include_quick_replies=False,
                context=context,
                skills_cwd=Path("/tmp/project"),
            )

        self.assertIn("load the `use-show-pages` Skill", prompt)
        self.assertNotIn("`vibe show path`", prompt)
        self.assertNotIn("Avibe Cloud is not connected", prompt)

        skill = (Path(__file__).resolve().parents[1] / "skills" / "use-show-pages" / "SKILL.md").read_text()
        self.assertIn("`vibe show path`", skill)
        self.assertIn("`Accept: text/markdown`", skill)
        self.assertIn("export async function GET(request)", skill)
        self.assertIn("[show-annotation]", skill)
        self.assertIn("vibe show mark", skill)
        self.assertIn("They include Show Page motion for changed text", skill)
        self.assertIn("`vibe show status`", skill)
        self.assertIn("command output as authoritative", skill)
        self.assertNotIn("injects the current Cloud-availability guidance", skill)
        self.assertNotIn("Avibe Cloud is not connected", skill)

    def test_show_pages_guidance_and_skill_catalog_are_advertised_together(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="slack",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )

        skills = [
            SimpleNamespace(
                name="use-show-pages",
                description="Show Page workflow",
                directory=Path("/tmp/show-pages"),
                disable_model_invocation=False,
            ),
            SimpleNamespace(
                name="use-avibe-vault",
                description="Vault workflow",
                directory=Path("/tmp/vault"),
                disable_model_invocation=False,
            ),
        ]
        with patch("core.managed_skills.resolve_skills", return_value=skills):
            prompt = build_system_prompt_injection(
                include_quick_replies=False,
                context=context,
                skills_cwd=Path("/tmp/project"),
            )

        self.assertIn("load the `use-show-pages` Skill", prompt)
        self.assertIn("- use-show-pages:", prompt)
        self.assertIn("- use-avibe-vault:", prompt)

    def test_required_skill_routes_remain_when_catalog_entries_are_manual_only(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="avibe",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )
        with (
            patch(
                "core.managed_skills.resolve_skills",
                return_value=_resolved_core_skills(manual_only=True),
            ),
        ):
            prompt = build_system_prompt_injection(
                include_quick_replies=False,
                context=context,
                skills_cwd=Path("/tmp/project"),
            )

        self.assertIn("load the `use-show-pages` Skill", prompt)
        self.assertNotIn("load the `use-avibe-vault` Skill", prompt)
        self.assertIn("load the `use-avibe-harness` Skill", prompt)
        self.assertNotIn("- use-show-pages:", prompt)
        self.assertNotIn("- use-avibe-vault:", prompt)
        self.assertNotIn("- use-avibe-harness:", prompt)
        self.assertNotIn("### Agents", prompt)

    def test_required_skill_routes_do_not_depend_on_a_catalog_binding(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform="avibe",
            platform_specific={"agent_session_id": "sesk8m4q2p7x"},
        )
        prompt = build_system_prompt_injection(
            include_quick_replies=False,
            context=context,
            skills_cwd=None,
        )

        self.assertIn("load the `use-show-pages` Skill", prompt)
        self.assertNotIn("load the `use-avibe-vault` Skill", prompt)
        self.assertIn("load the `use-avibe-harness` Skill", prompt)
        self.assertNotIn("### Agents", prompt)

    def test_prompt_uses_fallback_platform_for_unannotated_context(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            thread_id="171717.123",
            platform_specific={"is_dm": False},
        )

        with self.assertRaisesRegex(ValueError, "agent_session_id is required"):
            build_system_prompt_injection(
                include_quick_replies=True,
                context=context,
                fallback_platform="slack",
            )

    def test_prompt_handles_missing_platform_specific(self):
        context = MessageContext(
            user_id="U1",
            channel_id="C1",
            platform=None,
            platform_specific=None,
        )

        with self.assertRaisesRegex(ValueError, "agent_session_id is required"):
            build_system_prompt_injection(
                include_quick_replies=True,
                context=context,
                fallback_platform="slack",
            )

    def test_file_links_with_parentheses_are_preserved(self):
        enhanced = process_reply("![video](file:///Users/test/SaveTwitter.Net_GABV3XNWYAARAZz(gif).mp4)")

        self.assertEqual(len(enhanced.files), 1)
        self.assertEqual(
            enhanced.files[0].path,
            "/Users/test/SaveTwitter.Net_GABV3XNWYAARAZz(gif).mp4",
        )

    def test_angle_wrapped_file_links_accept_commonmark_destinations(self):
        enhanced = process_reply(
            "[下载报告](<file:///tmp/My Report (最终).md>) and "
            "![图片](<file:///tmp/图片 文件.png>)"
        )

        self.assertEqual(enhanced.text, "下载报告 and 图片")
        self.assertEqual(
            [(file.label, file.path, file.is_image) for file in enhanced.files],
            [
                ("下载报告", "/tmp/My Report (最终).md", False),
                ("图片", "/tmp/图片 文件.png", True),
            ],
        )

    def test_legacy_bare_file_links_accept_ascii_spaces(self):
        enhanced = process_reply(
            "[download](file:///tmp/My Report.md) and "
            '![image](file:///tmp/图片 文件.png "preview")'
        )

        self.assertEqual(enhanced.text, "download and image")
        self.assertEqual(
            [(file.path, file.is_image) for file in enhanced.files],
            [
                ("/tmp/My Report.md", False),
                ("/tmp/图片 文件.png", True),
            ],
        )

    def test_legacy_bare_file_link_extension_rejects_near_neighbors(self):
        specimens = [
            "[newline](file:///tmp/My\nReport.md)",
            "[tab](file:///tmp/My\tReport.md)",
            "[unclosed](file:///tmp/My Report.md",
            "[upper](FILE:///tmp/My Report.md)",
            "`[code](file:///tmp/My Report.md)`",
            '<span title="[html](file:///tmp/My Report.md)">visible</span>',
            '[outer](https://example.com "[inner](file:///tmp/My Report.md)")',
            r"\[escaped](file:///tmp/My Report.md)",
        ]

        for text in specimens:
            with self.subTest(text=text):
                enhanced = process_reply(text)
                self.assertEqual(enhanced.text, text)
                self.assertEqual(enhanced.files, [])

    def test_legacy_bare_file_link_recovers_after_malformed_prefix(self):
        text = (
            "[bad](file:///tmp/My Report "
            "[ok](file:///tmp/Good Report.md)"
        )

        enhanced = process_reply(text)

        self.assertEqual(enhanced.text, "[bad](file:///tmp/My Report ok")
        self.assertEqual(
            [(file.label, file.path) for file in enhanced.files],
            [("ok", "/tmp/Good Report.md")],
        )

    def test_legacy_bare_file_link_recovers_after_multiple_malformed_prefixes(self):
        text = (
            "[bad1](file:///tmp/One Report "
            "[bad2](file:///tmp/Two Report "
            "[ok](file:///tmp/Good Report.md)"
        )

        enhanced = process_reply(text)

        self.assertEqual(
            enhanced.text,
            "[bad1](file:///tmp/One Report [bad2](file:///tmp/Two Report ok",
        )
        self.assertEqual([file.path for file in enhanced.files], ["/tmp/Good Report.md"])

    def test_legacy_bare_file_link_keeps_malformed_source_without_valid_tail(self):
        text = "[bad](file:///tmp/My Report"

        enhanced = process_reply(text)

        self.assertEqual(enhanced.text, text)
        self.assertEqual(enhanced.files, [])

    def test_angle_wrapped_file_links_unescape_angle_brackets_in_paths(self):
        enhanced = process_reply(r"[report](<file:///tmp/a\>b.md>)")

        self.assertEqual(enhanced.text, "report")
        self.assertEqual([file.path for file in enhanced.files], ["/tmp/a>b.md"])

    def test_angle_wrapped_file_links_allow_unbalanced_parentheses(self):
        enhanced = process_reply(r"[draft](<file:///tmp/draft (v1.txt>)")

        self.assertEqual(enhanced.text, "draft")
        self.assertEqual([file.path for file in enhanced.files], ["/tmp/draft (v1.txt"])

    def test_angle_wrapped_file_links_unescape_before_percent_decoding(self):
        enhanced = process_reply(r"[literal](<file:///tmp/a%5C%3Eb.txt>)")

        self.assertEqual(enhanced.text, "literal")
        self.assertEqual([file.path for file in enhanced.files], [r"/tmp/a\>b.txt"])

    def test_angle_wrapped_file_links_unescape_all_commonmark_punctuation(self):
        enhanced = process_reply(r"[report](<file:///tmp/a\(b\)\[c\]\#d.md> 'download')")

        self.assertEqual(enhanced.text, "report")
        self.assertEqual([file.path for file in enhanced.files], ["/tmp/a(b)[c]#d.md"])

    def test_angle_wrapped_file_links_commonmark_source_matrix(self):
        cases = {
            r"[a\]b](<file:///tmp/report.md>)": "/tmp/report.md",
            r"[double](<file:///tmp/a\\(b.txt>)": r"/tmp/a\(b.txt",
            "[refs](<file:///tmp/a&amp;b.txt>)": "/tmp/a&b.txt",
            "[upper](<FILE:///tmp/upper.txt>)": "/tmp/upper.txt",
        }
        for text, expected_path in cases.items():
            with self.subTest(text=text):
                enhanced = process_reply(text)
                self.assertEqual(len(enhanced.files), 1)
                self.assertEqual(enhanced.files[0].path, expected_path)

    def test_file_link_parser_uses_commonmark_link_ownership(self):
        nested = process_reply("[outer [inner](<file:///tmp/inner.txt>)]")
        titled = process_reply(
            '[outer](<file:///tmp/outer.txt> "fake [inner](<file:///tmp/inner.txt>)")'
        )
        image_label = process_reply(
            "[outer ![inner](<file:///tmp/inner.png>)](<file:///tmp/outer.txt>)"
        )

        self.assertEqual(nested.text, "[outer inner]")
        self.assertEqual([file.path for file in nested.files], ["/tmp/inner.txt"])
        self.assertEqual(titled.text, "outer")
        self.assertEqual([file.path for file in titled.files], ["/tmp/outer.txt"])
        self.assertEqual(
            image_label.text,
            "outer ![inner](<file:///tmp/inner.png>)",
        )
        self.assertEqual(
            [file.path for file in image_label.files],
            ["/tmp/outer.txt"],
        )

    def test_file_link_parser_respects_commonmark_html_block_ownership(self):
        html_block = "<script>\n[hidden](<file:///tmp/hidden.txt>)\n</script>"
        escaped_tag = r"\<span>[visible](<file:///tmp/visible.txt>)</span>"

        blocked = process_reply(html_block)
        visible = process_reply(escaped_tag)

        self.assertEqual(blocked.text, html_block)
        self.assertEqual(blocked.files, [])
        self.assertEqual(visible.text, r"\<span>visible</span>")
        self.assertEqual([file.path for file in visible.files], ["/tmp/visible.txt"])

    def test_file_link_parser_normalizes_strict_entities_before_acceptance(self):
        valid = process_reply("[report](<f&#105;le:///tmp/a&amp;b.txt>)")
        semicolonless = process_reply("[literal](<file:///tmp/a&amp.txt>)")

        self.assertEqual([file.path for file in valid.files], ["/tmp/a&b.txt"])
        self.assertEqual(
            [file.path for file in semicolonless.files],
            ["/tmp/a&amp.txt"],
        )

    def test_file_link_parser_keeps_rejected_relative_links_verbatim(self):
        text = "[draft](<file:relative/report.md>)"

        enhanced = process_reply(text)

        self.assertEqual(enhanced.text, text)
        self.assertEqual(enhanced.files, [])

    def test_file_link_parser_keeps_malformed_authority_verbatim(self):
        text = "[bad](<file://[bad/path>)"

        enhanced = process_reply(text)

        self.assertEqual(enhanced.text, text)
        self.assertEqual(enhanced.files, [])

    def test_file_link_parser_skips_offset_maps_without_captures(self):
        with patch.object(
            reply_enhancer,
            "_inline_source_offsets",
            wraps=reply_enhancer._inline_source_offsets,
        ) as source_offsets:
            enhanced = process_reply("ordinary reply " + "x" * 100000)

        self.assertEqual(enhanced.files, [])
        source_offsets.assert_not_called()

    def test_file_link_parser_maps_captured_inline_source(self):
        with patch.object(
            reply_enhancer,
            "_inline_source_offsets",
            wraps=reply_enhancer._inline_source_offsets,
        ) as source_offsets:
            enhanced = process_reply("> [report](<file:///tmp/report.md>)")

        self.assertEqual(enhanced.text, "> report")
        self.assertEqual([file.path for file in enhanced.files], ["/tmp/report.md"])
        self.assertGreaterEqual(source_offsets.call_count, 1)

    def test_angle_wrapped_file_links_require_whitespace_before_title(self):
        text = '[report](<file:///tmp/report.md>"download")'

        enhanced = process_reply(text)

        self.assertEqual(enhanced.text, text)
        self.assertEqual(enhanced.files, [])

    def test_angle_wrapped_file_links_support_titles_and_reject_bad_titles(self):
        valid = [
            '[double](<file:///tmp/report.md> "download")',
            r"[single](<file:///tmp/report.md> 'download')",
            r"[paren](<file:///tmp/report.md> (download))",
            '[line](<file:///tmp/report.md>\n "download")',
        ]
        for text in valid:
            with self.subTest(text=text):
                self.assertEqual(len(process_reply(text).files), 1)

        invalid = [
            '[unclosed](<file:///tmp/report.md> "download)',
            r"[nested](<file:///tmp/report.md> (download (copy)))",
            '[marker](<file:///tmp/report.md> `download`)',
            '[no-space](<file:///tmp/report.md>"download")',
            '[blank](<file:///tmp/report.md>\n\n"download")',
        ]
        for text in invalid:
            with self.subTest(text=text):
                enhanced = process_reply(text)
                self.assertEqual(enhanced.text, text)
                self.assertEqual(enhanced.files, [])

    def test_angle_wrapped_file_links_ignore_escaped_openers(self):
        escaped = process_reply(r"\[example](<file:///tmp/report.txt>)")
        even_escaped = process_reply(r"\\[example](<file:///tmp/report.txt>)")

        self.assertEqual(escaped.text, r"\[example](<file:///tmp/report.txt>)")
        self.assertEqual(escaped.files, [])
        self.assertEqual(even_escaped.text, r"\\example")
        self.assertEqual([file.path for file in even_escaped.files], ["/tmp/report.txt"])

        escaped_image = process_reply(r"\![preview](<file:///tmp/preview.png>)")
        even_escaped_image = process_reply(r"\\![preview](<file:///tmp/preview.png>)")
        self.assertEqual(escaped_image.text, r"\!preview")
        self.assertEqual([file.is_image for file in escaped_image.files], [False])
        self.assertEqual(even_escaped_image.text, r"\\preview")
        self.assertEqual([file.is_image for file in even_escaped_image.files], [True])

    def test_angle_wrapped_file_links_ignore_raw_html_attributes(self):
        text = '<span title="[hidden](<file:///tmp/hidden.txt>)">visible</span>'

        enhanced = process_reply(text)

        self.assertEqual(enhanced.text, text)
        self.assertEqual(enhanced.files, [])

    def test_angle_wrapped_file_links_reject_malformed_markdown_and_code(self):
        specimens = [
            "[missing close](<file:///tmp/report.md)",
            "[extra angle](<file:///tmp/report>copy.md>)",
            "[extra open](<<file:///tmp/report.md>>)",
            "[missing label close(<file:///tmp/report.md>)",
            "[missing close](<file://" + "a" * 100000,
            "`[inline](<file:///tmp/report.md>)`",
            "```markdown\n[fenced](<file:///tmp/report.md>)\n```",
        ]

        for text in specimens:
            with self.subTest(text=text):
                enhanced = process_reply(text)
                self.assertEqual(enhanced.text, text)
                self.assertEqual(enhanced.files, [])

    def test_block_parser_preserves_commonmark_structure_without_inline_children(self):
        specimens = [
            "> - [report](<file:///tmp/report.md>) and `code`\n>   continuation\n",
            "heading\n=======\n\n~~~markdown\n[file](file:///tmp/code.md)\n~~~\n",
            "    indented code\n\n<div>\nraw HTML\n</div>\n\nvisible\n",
            "[reference]: /target \"title\"\n\n[reference] and ![image](/image.png)\n",
            "1. first\r\n   - second\r\n\r\n---\r\n[one] | [two]\r\n",
            "[" * 1000 + "[report](<file:///tmp/report.md>)",
        ]
        reference = reply_enhancer.MarkdownIt("commonmark")
        for text in specimens:
            with self.subTest(text=text[:80]):
                expected = reference.parse(text)
                actual = reply_enhancer._BLOCK_MARKDOWN.parse(text)
                self.assertEqual(len(actual), len(expected))
                for actual_token, expected_token in zip(actual, expected):
                    actual_fields = actual_token.as_dict()
                    expected_fields = expected_token.as_dict()
                    actual_fields.pop("children")
                    expected_fields.pop("children")
                    self.assertEqual(actual_fields, expected_fields)
                    self.assertFalse(actual_token.children)

    def test_block_range_consumers_do_not_run_discarded_inline_parsing(self):
        text = "> [report](<file:///tmp/report.md>)\n\n```\ncode\n```\n"
        with patch.object(
            reply_enhancer._BLOCK_MARKDOWN.inline,
            "parse",
            side_effect=AssertionError("block-only consumers must not parse inline children"),
        ):
            code, inline, blocking = reply_enhancer._markdown_block_ranges(text)
            level = reply_enhancer._markdown_container_level_at(
                "> body\n> [one] | [two]", len("> body")
            )
            enhanced = process_reply(text)
            stripped = reply_enhancer.strip_file_links(text)
            replaced = reply_enhancer._replace_file_links(text, lambda match: "/media/report")

        self.assertEqual(code, [(text.index("```"), len(text))])
        self.assertEqual(blocking, code)
        self.assertEqual(inline, [(0, text.index("\n") + 1, "[report](<file:///tmp/report.md>)")])
        self.assertEqual(level, 2)
        self.assertEqual([file.path for file in enhanced.files], ["/tmp/report.md"])
        self.assertEqual(enhanced.text, "> report\n\n```\ncode\n```")
        self.assertEqual(stripped, "> report\n\n```\ncode\n```\n")
        self.assertEqual(replaced, "> [report](</media/report>)\n\n```\ncode\n```\n")

    def test_file_link_parser_handles_many_openers_in_linear_time(self):
        growth, enhanced = _growth(lambda count: "[" * count + "[report](<file:///tmp/report.md>)", 12_500)

        self.assertEqual([file.path for file in enhanced.files], ["/tmp/report.md"])
        self.assertLess(growth, _LINEAR_GROWTH_BOUND)

    def test_legacy_bare_file_link_scans_malformed_destination_in_linear_time(self):
        def destination(count):
            return "[missing](file:///tmp/" + "a " * count + "tail"

        growth, enhanced = _growth(destination, 25_000)

        self.assertEqual(enhanced.text, destination(25_000 * _GROWTH_STEP))
        self.assertEqual(enhanced.files, [])
        self.assertLess(growth, _LINEAR_GROWTH_BOUND)

    def test_legacy_bare_file_link_scans_many_malformed_candidates_linearly(self):
        growth, enhanced = _growth(
            lambda count: "[bad](file:///tmp/My Report " * count + "[ok](file:///tmp/Good Report.md)",
            250,
        )

        self.assertEqual([file.path for file in enhanced.files], ["/tmp/Good Report.md"])
        self.assertLess(growth, _LINEAR_GROWTH_BOUND)

    def test_unwrapped_file_link_parser_keeps_existing_destination_behavior(self):
        enhanced = process_reply("[report](file:///tmp/report>draft.md)")

        self.assertEqual(enhanced.text, "report")
        self.assertEqual(
            [file.path for file in enhanced.files],
            ["/tmp/report>draft.md"],
        )

    def test_windows_file_uri_is_normalized_before_absolute_check(self):
        with patch("core.reply_enhancer.os.name", "nt"), patch("core.reply_enhancer.os.path.isabs") as isabs:
            isabs.side_effect = lambda value: value == r"C:\Users\test\generated image.png"
            enhanced = process_reply("![generated image](file:///C:/Users/test/generated%20image.png)")

        self.assertEqual(len(enhanced.files), 1)
        self.assertEqual(enhanced.files[0].path, r"C:\Users\test\generated image.png")

    async def test_wechat_result_ignores_quick_reply_buttons(self):
        controller = _StubController("wechat")
        dispatcher = ConsolidatedMessageDispatcher(controller)
        context = MessageContext(user_id="U1", channel_id="C1")

        await dispatcher.emit_agent_message(
            context,
            "result",
            "Done.\n---\n[继续] | [提交PR]",
        )

        self.assertEqual(controller.im_client.sent_button_messages, [])
        self.assertEqual(
            controller.im_client.sent_messages,
            [("C1", "Done.", "markdown")],
        )

    async def test_lark_quick_reply_buttons_use_horizontal_layout(self):
        controller = _StubController("lark")
        dispatcher = ConsolidatedMessageDispatcher(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="lark")

        await dispatcher.emit_agent_message(
            context,
            "result",
            "Done.\n---\n[继续] | [提交PR]",
        )

        self.assertEqual(len(controller.im_client.sent_button_messages), 1)
        keyboard = controller.im_client.sent_button_messages[0][3]
        # Lark quick replies are now multi-column (cap 3/row), so two buttons
        # share a single row instead of stacking vertically.
        self.assertEqual([[button.text for button in row] for row in keyboard.buttons], [["继续", "提交PR"]])

    async def test_markdown_link_style_quick_reply_dispatches_label_callbacks(self):
        controller = _StubController("slack")
        dispatcher = ConsolidatedMessageDispatcher(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="slack")

        await dispatcher.emit_agent_message(
            context,
            "result",
            "Done.\n---\n"
            "[:eyes: 看 PR](<https://github.com/avibe-bot/avibe/pull/298>) | "
            "[:rocket: 等评审完合并]",
        )

        self.assertEqual(len(controller.im_client.sent_button_messages), 1)
        keyboard = controller.im_client.sent_button_messages[0][3]
        buttons = keyboard.buttons[0]
        self.assertEqual([button.text for button in buttons], [":eyes: 看 PR", ":rocket: 等评审完合并"])
        self.assertEqual(
            [button.callback_data for button in buttons],
            ["quick_reply::eyes: 看 PR", "quick_reply::rocket: 等评审完合并"],
        )

    async def test_lark_log_message_strips_file_links_before_sending(self):
        controller = _StubController("lark")
        dispatcher = ConsolidatedMessageDispatcher(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="lark")

        await dispatcher.emit_agent_message(
            context,
            "assistant",
            "Preview ready\n\n![screen](file:///tmp/screen-room.png)",
        )

        self.assertEqual(
            controller.im_client.sent_messages,
            [("C1", "Preview ready\n\nscreen", "markdown")],
        )

    async def test_lark_log_message_preserves_button_like_markdown_blocks(self):
        controller = _StubController("lark")
        dispatcher = ConsolidatedMessageDispatcher(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="lark")

        await dispatcher.emit_agent_message(
            context,
            "assistant",
            "Runbook\n---\n[step one] | [step two]",
        )

        self.assertEqual(
            controller.im_client.sent_messages,
            [("C1", "Runbook\n---\n[step one] | [step two]", "markdown")],
        )

    async def test_telegram_quick_reply_buttons_use_vertical_layout(self):
        controller = _StubController("telegram")
        dispatcher = ConsolidatedMessageDispatcher(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="telegram")

        await dispatcher.emit_agent_message(
            context,
            "result",
            "Done.\n---\n[继续] | [提交PR]",
        )

        self.assertEqual(len(controller.im_client.sent_button_messages), 1)
        keyboard = controller.im_client.sent_button_messages[0][3]
        self.assertEqual([[button.text for button in row] for row in keyboard.buttons], [["继续"], ["提交PR"]])

    async def test_discord_long_result_splits_into_multiple_messages_without_markdown_attachment(self):
        controller = _StubController("discord")
        dispatcher = ConsolidatedMessageDispatcher(controller)
        context = MessageContext(user_id="U1", channel_id="C1", platform="discord")
        long_text = " ".join(["Alpha"] * 320) + "\n\n" + " ".join(["Beta"] * 120)

        message_id = await dispatcher.emit_agent_message(context, "result", long_text)

        self.assertEqual(message_id, "msg-1")
        self.assertGreater(len(controller.im_client.sent_messages), 1)
        self.assertEqual(
            "".join(text for _, text, _ in controller.im_client.sent_messages),
            long_text,
        )
        self.assertEqual(controller.im_client.uploaded_markdowns, [])


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