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.

4.4 KB
Raw Blame
"""YAML front-matter title contracts for the Quarto exporter.

The report title derives from web-influenced content (the first heading
of the report, or a caller-supplied title) and is interpolated into the
generated ``.qmd`` front matter. The front matter must therefore always
parse back to one scalar: a ``"`` that breaks out of the quoted scalar
turns the export into a document whose YAML header fails parse (a
render-time denial of service), and an embedded newline can inject
additional front-matter keys. Pandoc reads that scalar as Markdown, so it
holds the title with a backslash before every ASCII punctuation character
and with runs of spaces, tabs and line breaks collapsed to one space;
Pandoc reads it back as the title text.

The quote-breakout and key-injection scenarios are pinned end-to-end
by the promoted contracts in
``tests/web/services/test_export_generation_contracts.py``; this file
pins only the edges those suites do not cover: backslash titles,
control characters, and the plain-title common path.
"""

from __future__ import annotations

import re

import yaml

from local_deep_research.text_optimization.citation_formatter import (
    QuartoExporter,
)


def _parse_front_matter(qmd: str) -> dict:
    """Parse the YAML front matter block of a rendered .qmd document."""
    assert qmd.startswith("---\n"), "document must open with a YAML block"
    end = qmd.index("\n---\n", 3)
    block = qmd[: end + 1]
    return yaml.safe_load(block[4:])


def _markdown_text(value: str) -> str:
    """The text Pandoc reads from *value*: a backslash before an ASCII
    punctuation character stands for that character."""
    return re.sub(r"\\([!-/:-@\[-`{-~])", r"\1", value)


class TestDerivedTitleStaysASingleScalar:
    """Titles pulled from the report heading must survive YAML parsing."""

    def test_backslash_in_derived_title_parses_back_exactly(self):
        exporter = QuartoExporter()
        content = "# A \\ LaTeX \\ backslash\n\nBody text.\n"
        result = exporter.export_to_quarto(content)

        front = _parse_front_matter(result)
        assert front["title"] == "A \\\\ LaTeX \\\\ backslash"
        assert _markdown_text(front["title"]) == "A \\ LaTeX \\ backslash"


class TestExplicitTitleCannotInjectFrontMatter:
    """Caller-supplied titles must not escape the title scalar."""

    def test_control_characters_are_escaped(self):
        exporter = QuartoExporter()
        result = exporter.export_to_quarto(
            "# Heading\n\nBody.\n", title="tab\there\x00nul"
        )

        front = _parse_front_matter(result)
        # A tab is whitespace to Pandoc, read as one space either way.
        assert front["title"] == "tab here\x00nul"


class TestC1AndLineSeparatorTitlesRoundTrip:
    """C1 controls and the Unicode line separators must round-trip too.

    ``_yaml_double_quote`` escapes the same range ``is_line_breaking_char``
    defines elsewhere in this module: C0, DEL, the C1 block
    U+0080-U+009F, and U+2028/U+2029. YAML's printable production
    excludes C1 outright and folds U+0085 (NEL) into a space, so either
    one breaks the "parses back exactly" guarantee if left unescaped.
    """

    def test_c1_nel_and_line_separator_titles_parse_back_exactly(self):
        exporter = QuartoExporter()

        c1_front = _parse_front_matter(
            exporter.export_to_quarto(
                "# Heading\n\nBody.\n", title="Smart\x93quote heading"
            )
        )
        assert c1_front["title"] == "Smart\x93quote heading"

        nel_front = _parse_front_matter(
            exporter.export_to_quarto(
                "# Heading\n\nBody.\n", title="Before\x85after"
            )
        )
        assert nel_front["title"] == "Before\x85after"

        ls_front = _parse_front_matter(
            exporter.export_to_quarto(
                "# Heading\n\nBody.\n", title="Before\u2028after"
            )
        )
        assert ls_front["title"] == "Before\u2028after"


class TestPlainTitleStillRenders:
    """The common case must keep working unchanged."""

    def test_plain_title_parses_with_expected_keys(self):
        exporter = QuartoExporter()
        result = exporter.export_to_quarto(
            "# Heading\n\nBody.\n", title="A plain title"
        )

        front = _parse_front_matter(result)
        assert front["title"] == "A plain title"
        assert front["author"] == "Local Deep Research"
        assert front["bibliography"] == "references.bib"