Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.6 KB
Raw Blame
"""Preserve literal Markdown while removing citations outside its boundaries."""

import pytest

from local_deep_research.chat.citation_stripping import (
    strip_prior_turn_citations,
)
from local_deep_research.text_optimization.citation_formatter import (
    CitationFormatter,
    CitationMode,
)


@pytest.mark.parametrize(
    "text,expected",
    [
        (r"Literal \[1] and fact [2].", r"Literal \[1] and fact."),
        (
            r"Escaped \`tick [1] and `code [2]` fact [3].",
            r"Escaped \`tick and `code [2]` fact.",
        ),
        ("[1][manual]\n\n[manual]: /guide", "[1][manual]\n\n[manual]: /guide"),
        ("[1][2]\n\n[2]: /guide", "[1][2]\n\n[2]: /guide"),
        ("Fact [1][2].", "Fact."),
        (
            "- ```\n  [1]\n  ```\n\nFact [2].",
            "- ```\n  [1]\n  ```\n\nFact.",
        ),
        (
            "- item\n\n  ```python\n  values = [1]\n  ```\n\nFact [2].",
            "- item\n\n  ```python\n  values = [1]\n  ```\n\nFact.",
        ),
        (
            "10. ```\n    code [1]\n    ```\n\nFact [2].",
            "10. ```\n    code [1]\n    ```\n\nFact.",
        ),
        (
            "> ```\n> code [1]\n\nFact [2].",
            "> ```\n> code [1]\n\nFact.",
        ),
    ],
)
def test_literals_and_container_boundaries(text, expected):
    """Catches stripping before escape/reference protection or across code boundaries."""
    assert strip_prior_turn_citations(text) == expected


@pytest.mark.parametrize(
    "url",
    [
        "http://example.com:8080/a",
        "http://10.0.0.1/a",
        "http://10.0.0.1:8080/a",
        "http://[::1]/a",
        "http://[::1]:8080/a",
    ],
)
@pytest.mark.parametrize(
    "mode",
    [
        CitationMode.DOMAIN_ID_HYPERLINKS,
        CitationMode.DOMAIN_ID_ALWAYS_HYPERLINKS,
    ],
)
def test_formatter_authority_ids_are_removed(url, mode):
    """Two same-authority sources make the conditional formatter emit ID suffixes."""
    sources = [
        {"index": 1, "title": "First", "url": url},
        {"index": 2, "title": "Second", "url": url + "b"},
    ]
    formatted = CitationFormatter(mode).apply_inline_hyperlinks(
        "See [1, 2] now.", sources
    )
    assert formatted != "See [1, 2] now."
    assert strip_prior_turn_citations(formatted) == "See now."


@pytest.mark.parametrize("ticks", [2, 3, 4])
def test_escaped_tick_leaves_shorter_code_opener(ticks):
    """An escape consumes one backtick, not the entire run before inline code."""
    literal = "\\" + "`" * ticks + "code [1]" + "`" * (ticks - 1)
    assert (
        strip_prior_turn_citations(literal + " fact [2].") == literal + " fact."
    )


@pytest.mark.parametrize(
    "example",
    ["```\n[2]: /url\n```", "<!--\n[2]: /url\n-->", "<pre>\n[2]: /url\n</pre>"],
)
def test_code_reference_definitions_do_not_change_prose_citations(example):
    """Reference-looking code is not a real definition for an adjacent marker run."""
    assert (
        strip_prior_turn_citations(example + "\nFact [1][2].")
        == example + "\nFact."
    )


@pytest.mark.parametrize(
    "text,expected",
    [
        # The heading / list item ends the paragraph, so the first
        # backtick never closes and ``[1]`` is prose. It ends its line, so
        # the space before it goes with it (no trailing whitespace is left
        # behind; see the module docstring's separator rule).
        (
            "`literal [1]\n# heading `code [2]` fact [3].",
            "`literal\n# heading `code [2]` fact.",
        ),
        (
            "`literal [1]\n- item `code [2]` fact [3].",
            "`literal\n- item `code [2]` fact.",
        ),
        (
            "- > ```\n  > values = [1]\n  > ```\n\nFact [2].",
            "- > ```\n  > values = [1]\n  > ```\n\nFact.",
        ),
        (
            "> - > ```\n>   > values = [1]\n>   > ```\n\nFact [2].",
            "> - > ```\n>   > values = [1]\n>   > ```\n\nFact.",
        ),
        ("<!--\n```\n-->\n\nClaim [1].", "<!--\n```\n-->\n\nClaim."),
        ("```\n<!--\n```\n\nClaim [1].", "```\n<!--\n```\n\nClaim."),
        ("`<!--`\nClaim [1].", "`<!--`\nClaim."),
        (
            '<span title="<pre>">literal</span>\n\nFact [[1]](u).\n\nMore [2].',
            '<span title="<pre>">literal</span>\n\nFact.\n\nMore.',
        ),
        (
            '<code title="a > b">literal [1]</code> Fact [2].',
            '<code title="a > b">literal [1]</code> Fact.',
        ),
        ("Read [1] now.\n\n[1]: /manual", "Read [1] now.\n\n[1]: /manual"),
        (
            'Read [1](/manual "the guide") now.',
            'Read [1](/manual "the guide") now.',
        ),
        ("Read [1](<manual page>) now.", "Read [1](<manual page>) now."),
        (
            "Fact [local-1][local-2][local-3] and code `x`.",
            "Fact and code `x`.",
        ),
    ],
)
def test_composed_markdown_boundaries(text, expected):
    """Catches cross-block delimiters, nested containers, and literal HTML tags."""
    assert strip_prior_turn_citations(text) == expected


@pytest.mark.parametrize("suffix", ["`code`", "<span>value</span>"])
def test_successful_runs_have_bounded_matcher_work(monkeypatch, suffix):
    """Revisiting each recognized suffix makes the number of lookups quadratic."""
    from local_deep_research.chat import citation_stripping as scanner

    original = scanner._match_citation_token
    calls = 0

    def counted(text, position):
        nonlocal calls
        calls += 1
        return original(text, position)

    monkeypatch.setattr(scanner, "_match_citation_token", counted)
    markers = 600
    result = scanner.strip_prior_turn_citations("[1] " * markers + suffix)
    assert "[1]" not in result
    assert suffix in result
    assert calls <= 10 * markers