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.

32.4 KB
Raw Blame
"""Bounds for the pandoc conversion path (.rtf/.epub/.rst/.org/.odt).

Five upload formats convert via a ``pandoc`` subprocess before parsing:
unstructured's ``convert_file_to_html_text_using_pandoc`` (and
``partition_odt`` for .odt) -> ``pypandoc.convert_file`` ->
``Popen(...)`` + ``communicate()`` with **no timeout and no memory
cap**. A hostile or pathological file therefore pins an indexing worker
indefinitely or grows pandoc's heap without limit — the same class as
the unbounded soffice conversion (#6513), reachable for content up to
the upload size cap.

These contracts pin the bounded seam: the binary resolves the way
pypandoc resolves it (the shipped pypandoc-binary has no pandoc on
PATH), a wall-clock timeout that reaches the subprocess, a new session
plus a process-group kill on hang, a heap cap on the child, stdio sent
to /dev/null, an output-size ceiling checked before the output is read,
sandboxed pandoc argv, temp cleanup, and correct wiring (converted HTML
is dispatched through the ``.html`` loader; ``.epub``/``.odt``
containers still pass the zip guard first).
"""

from __future__ import annotations

import inspect
import io
import os
import signal
import subprocess
import sys
import time
import zipfile
from contextlib import contextmanager
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch

import pytest


def _conv():
    from local_deep_research.utilities import pandoc_conversion

    return pandoc_conversion


@contextmanager
def _pandoc_available(conv, rts=True):
    """Resolve the binary even on pandoc-less boxes, without probing."""
    with (
        patch.object(conv, "resolve_pandoc_binary", return_value="pandoc"),
        patch.object(conv, "_pandoc_accepts_rts_heap_cap", return_value=rts),
    ):
        yield


class _FakeProc:
    pid = 8484
    returncode = 0

    def __init__(self, on_communicate=None, command=None, kwargs=None):
        self._on_communicate = on_communicate
        self.communicate_timeout = None
        self.waited = False
        self.command = command
        self.kwargs = kwargs

    def communicate(self, timeout=None):
        self.communicate_timeout = timeout
        if self._on_communicate is not None:
            self._on_communicate()
        return b"", b""

    def wait(self):
        self.waited = True
        return 0


PANDOC_BACKED = [".rtf", ".epub", ".rst", ".org", ".odt"]


def _recording_popen(on_communicate=None, write_output=None):
    """Popen double that records argv + kwargs of every launch."""
    launched: list[_FakeProc] = []

    def fake_popen(command, **kwargs):
        def communicate():
            if write_output is not None:
                write_output(Path(command[command.index("-o") + 1]))
            if on_communicate is not None:
                on_communicate()

        proc = _FakeProc(
            on_communicate=communicate, command=command, kwargs=kwargs
        )
        launched.append(proc)
        return proc

    return fake_popen, launched


class TestConversionBounds:
    def test_the_timeout_reaches_the_subprocess(self, tmp_path):
        conv = _conv()
        proc = _FakeProc()

        with (
            _pandoc_available(conv),
            patch.object(conv.subprocess, "Popen", return_value=proc),
        ):
            try:
                conv.pandoc_convert_bytes_to_html(
                    b"{\\rtf1}", ".rtf", work_dir=tmp_path
                )
            except conv.PandocConversionError:
                pass  # the fake writes no output; only timing matters

        assert (
            proc.communicate_timeout is not None
            and proc.communicate_timeout >= 30
        ), "pandoc runs without a real wall-clock timeout"

    def test_hanging_conversion_is_killed_by_group_and_fails_typed(
        self, tmp_path
    ):
        conv = _conv()

        def hang(timeout=None):
            raise subprocess.TimeoutExpired(cmd="pandoc", timeout=timeout)

        proc = _FakeProc(on_communicate=hang)
        kills: list = []

        with (
            _pandoc_available(conv),
            patch.object(conv.subprocess, "Popen", return_value=proc),
            patch.object(conv, "_kill_process_group", side_effect=kills.append),
        ):
            with pytest.raises(conv.PandocConversionError) as excinfo:
                conv.pandoc_convert_bytes_to_html(
                    b"{\\rtf1}", ".rtf", work_dir=tmp_path
                )

        assert "timed out" in str(excinfo.value).lower()
        assert kills == [proc.pid], (
            f"hung pandoc not killed by process group: {kills}"
        )
        assert proc.waited, "killed process was never reaped"

    def test_runaway_output_is_refused_before_reading(self, tmp_path):
        """Output over the ceiling is refused by size check, not read."""
        conv = _conv()

        def write_big(_proc):
            (tmp_path / "out.html").write_bytes(b"<html>" + b"x" * 100_000)

        with (
            _pandoc_available(conv),
            patch.object(
                conv.subprocess,
                "Popen",
                side_effect=lambda *a, **k: _FakeProc(
                    on_communicate=lambda: write_big(None)
                ),
            ),
        ):
            with pytest.raises(conv.PandocConversionError) as excinfo:
                conv.pandoc_convert_bytes_to_html(
                    b"{\\rtf1}",
                    ".rtf",
                    work_dir=tmp_path,
                    max_output_bytes=1000,
                )

        assert "larger than" in str(excinfo.value).lower()

    def test_pandoc_argv_is_sandboxed(self, tmp_path):
        """pandoc must run with --sandbox (restricted file access)."""
        conv = _conv()
        commands: list = []

        def fake_popen(command, **kwargs):
            commands.append(command)
            return _FakeProc()

        with (
            _pandoc_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            try:
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path
                )
            except conv.PandocConversionError:
                pass

        assert commands, "pandoc never launched"
        argv = commands[0]
        assert "--sandbox" in argv, f"pandoc argv not sandboxed: {argv}"
        assert "out.html" in " ".join(argv) or "-o" in argv, (
            f"pandoc does not write to a bounded output file: {argv}"
        )

    def test_temp_input_is_cleaned_up(self, tmp_path):
        conv = _conv()

        def check_input():
            docs = list(tmp_path.rglob("*.rst"))
            assert docs, "input missing while pandoc runs"

        with (
            _pandoc_available(conv),
            patch.object(
                conv.subprocess,
                "Popen",
                return_value=_FakeProc(on_communicate=check_input),
            ),
        ):
            with pytest.raises(conv.PandocConversionError):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path
                )

        leftovers = list(tmp_path.rglob("*"))
        assert not leftovers, f"conversion left files behind: {leftovers}"


class TestWiring:
    @pytest.mark.parametrize("ext", PANDOC_BACKED)
    def test_pandoc_backed_uploads_dispatch_through_the_converter(
        self, tmp_path, ext
    ):
        """Each pandoc-backed upload converts first, then loads as HTML."""
        import local_deep_research.document_loaders.bytes_loader as bl

        conv = _conv()
        dispatched: list = []
        guard_calls: list = []

        from local_deep_research.document_loaders import zip_container_guard

        def fake_popen(command, **kwargs):
            out = Path(command[command.index("-o") + 1])
            out.write_text(
                "<html><body>converted</body></html>", encoding="utf-8"
            )
            return _FakeProc()

        def recording_guard(content, extension, **kwargs):
            guard_calls.append(extension)
            # Behave like the real guard: containers validated, the
            # rest pass through untouched.
            if extension in zip_container_guard.ZIP_CONTAINER_EXTENSIONS:
                zip_container_guard.validate_zip_container(content, extension)

        def fake_get_loader(extension):
            dispatched.append(extension)
            return  # load_from_bytes then raises "No loader found"

        def payload_for(extension):
            if extension in (".epub", ".odt"):
                buf = io.BytesIO()
                with zipfile.ZipFile(buf, "w") as zf:
                    zf.writestr("META-INF/container.xml", "<r/>")
                return buf.getvalue()
            return b"payload"

        with (
            _pandoc_available(conv),
            patch.object(bl, "is_extension_supported", return_value=True),
            patch.object(
                bl,
                "get_loader_class_for_extension",
                side_effect=fake_get_loader,
            ),
            patch.object(bl, "validate_zip_container", recording_guard),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            # The fake loader registry entry is None, so the (already
            # converted) dispatch ends in the expected ValueError.
            with pytest.raises(ValueError, match="No loader found"):
                bl.load_from_bytes(payload_for(ext), ext, f"hostile{ext}")

        # The converted content was dispatched for HTML loading...
        assert dispatched == [".html"], dispatched
        # ...and the container guard ran on the ORIGINAL extension
        # (notably .epub, before the conversion replaced it).
        assert guard_calls == [ext], guard_calls

    def test_live_round_trip(self, tmp_path):
        """Control: a real minimal rst converts to HTML with the binary
        the app actually ships (pypandoc-binary, not PATH)."""
        conv = _conv()
        if conv.resolve_pandoc_binary() is None:
            pytest.skip("no pandoc via pypandoc or PATH")

        html = conv.pandoc_convert_bytes_to_html(
            b"Hello *pandoc*\n=====\n\nbody text", ".rst", work_dir=tmp_path
        )

        assert b"body text" in html.lower()

    def test_live_odt_round_trip(self, tmp_path):
        """Control: a genuine ODT converts through the bounded wrapper."""
        pypandoc = pytest.importorskip("pypandoc")
        conv = _conv()
        if conv.resolve_pandoc_binary() is None:
            pytest.skip("no pandoc via pypandoc or PATH")
        odt_path = tmp_path / "src.odt"
        pypandoc.convert_text(
            "Otters build dams.", "odt", format="md", outputfile=str(odt_path)
        )

        work = tmp_path / "work"
        work.mkdir()

        html = conv.pandoc_convert_bytes_to_html(
            odt_path.read_bytes(), ".odt", work_dir=work
        )

        assert b"Otters build dams." in html

    def test_live_binary_honours_the_rts_heap_cap(self):
        """The shipped binary accepts ``+RTS -M``, so the preferred
        (thread-safe) memory cap is the one actually in force."""
        conv = _conv()
        pandoc = conv.resolve_pandoc_binary()
        if pandoc is None or "pypandoc" not in pandoc:
            pytest.skip("bundled pypandoc-binary pandoc not in use")
        with patch.dict(conv._rts_probe_cache, clear=True):
            assert conv._pandoc_accepts_rts_heap_cap(pandoc) is True


class TestBinaryResolution:
    """The shipped install is pypandoc-binary: pandoc lives at
    ``pypandoc/files/pandoc`` and nothing named ``pandoc`` is on PATH.
    A ``shutil.which``-only resolver fails every pandoc-backed upload."""

    def test_resolver_uses_pypandoc_when_path_has_no_pandoc(self):
        pypandoc = pytest.importorskip("pypandoc")
        conv = _conv()
        bundled = "/opt/site-packages/pypandoc/files/pandoc"

        with (
            patch.object(conv.shutil, "which", return_value=None),
            patch.object(pypandoc, "get_pandoc_path", return_value=bundled),
        ):
            assert conv.resolve_pandoc_binary() == bundled

    def test_conversion_launches_the_pypandoc_binary(self, tmp_path):
        pypandoc = pytest.importorskip("pypandoc")
        conv = _conv()
        bundled = "/opt/site-packages/pypandoc/files/pandoc"
        fake_popen, launched = _recording_popen(
            write_output=lambda out: out.write_text(
                "<p>ok</p>", encoding="utf-8"
            )
        )

        with (
            patch.object(conv.shutil, "which", return_value=None),
            patch.object(pypandoc, "get_pandoc_path", return_value=bundled),
            patch.object(
                conv, "_pandoc_accepts_rts_heap_cap", return_value=True
            ),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            html = conv.pandoc_convert_bytes_to_html(
                b"x", ".rst", work_dir=tmp_path
            )

        assert html == b"<p>ok</p>"
        assert launched[0].command[0] == bundled

    def test_resolver_falls_back_to_path_without_pypandoc_pandoc(self):
        pypandoc = pytest.importorskip("pypandoc")
        conv = _conv()

        with (
            patch.object(conv.shutil, "which", return_value="/usr/bin/pandoc"),
            patch.object(
                pypandoc, "get_pandoc_path", side_effect=OSError("none")
            ),
        ):
            assert conv.resolve_pandoc_binary() == "/usr/bin/pandoc"

    def test_no_binary_anywhere_fails_typed(self, tmp_path):
        conv = _conv()

        with patch.object(conv, "resolve_pandoc_binary", return_value=None):
            with pytest.raises(
                conv.PandocConversionError, match="not available"
            ):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path
                )


class TestProcessIsolation:
    """A hang must be killable as a tree, and nothing may be buffered."""

    def _launch(self, conv, tmp_path, **popen_opts):
        fake_popen, launched = _recording_popen(**popen_opts)
        with (
            _pandoc_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            try:
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path
                )
            except conv.PandocConversionError:
                pass
        assert launched, "pandoc never launched"
        return launched[0]

    def test_pandoc_runs_in_its_own_session(self, tmp_path):
        """Without a new session, proc.pid is not a process-group id:
        the group kill misses and ``proc.wait()`` blocks forever."""
        conv = _conv()
        kwargs = self._launch(conv, tmp_path).kwargs

        assert (
            kwargs.get("start_new_session") is True
            or kwargs.get("process_group") == 0
        ), f"pandoc not started in its own process group: {kwargs}"

    def test_stdio_goes_to_devnull_not_a_pipe(self, tmp_path):
        """``communicate()`` on a PIPE buffers every byte pandoc writes
        (tens of MB of warnings were observed) in the worker."""
        conv = _conv()
        kwargs = self._launch(conv, tmp_path).kwargs

        for stream in ("stdout", "stderr"):
            assert kwargs.get(stream) == subprocess.DEVNULL, (
                f"pandoc {stream} is buffered: {kwargs.get(stream)!r}"
            )

    @pytest.mark.skipif(
        not hasattr(os, "killpg"), reason="no process groups here"
    )
    def test_timeout_kill_targets_the_process_group(self, tmp_path):
        """The real kill helper must signal the GROUP, not just the
        child: grandchildren would otherwise survive the timeout."""
        conv = _conv()

        def hang():
            raise subprocess.TimeoutExpired(cmd="pandoc", timeout=1)

        fake_popen, launched = _recording_popen(on_communicate=hang)
        group_kills: list = []
        pid_kills: list = []

        with (
            _pandoc_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
            patch.object(
                conv.os, "killpg", side_effect=lambda *a: group_kills.append(a)
            ),
            patch.object(
                conv.os, "kill", side_effect=lambda *a: pid_kills.append(a)
            ),
        ):
            with pytest.raises(conv.PandocConversionError, match="timed out"):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path
                )

        assert group_kills == [(launched[0].pid, signal.SIGKILL)], (
            f"hung pandoc not killed as a group: killpg={group_kills} "
            f"kill={pid_kills}"
        )
        assert launched[0].waited, "killed process was never reaped"


class TestMemoryCap:
    """pandoc's zip reader inflates entries regardless of a lying
    declared size, so the child needs its own memory ceiling."""

    def test_rts_heap_cap_is_on_the_argv(self, tmp_path):
        conv = _conv()
        fake_popen, launched = _recording_popen()

        with (
            _pandoc_available(conv, rts=True),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            with pytest.raises(conv.PandocConversionError):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path
                )

        argv = launched[0].command
        cap = f"-M{conv.DEFAULT_MAX_HEAP_MB}m"
        assert argv[1:4] == ["+RTS", cap, "-RTS"], (
            f"pandoc runs without a heap cap: {argv}"
        )

    def test_default_heap_cap_is_bounded(self):
        conv = _conv()
        default = (
            inspect.signature(conv.pandoc_convert_bytes_to_html)
            .parameters["max_heap_mb"]
            .default
        )

        assert default == conv.DEFAULT_MAX_HEAP_MB
        assert 512 <= conv.DEFAULT_MAX_HEAP_MB <= 4096

    @pytest.mark.skipif(
        not sys.platform.startswith("linux"), reason="RLIMIT_AS is Linux-only"
    )
    def test_rlimit_as_fallback_without_rts_options(self, tmp_path):
        """A pandoc build without ``-rtsopts`` rejects ``+RTS``; the
        cap then moves to RLIMIT_AS in the child instead of vanishing."""
        import resource

        conv = _conv()
        fake_popen, launched = _recording_popen()

        with (
            _pandoc_available(conv, rts=False),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            with pytest.raises(conv.PandocConversionError):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path, max_heap_mb=1000
                )

        proc = launched[0]
        assert "+RTS" not in proc.command
        preexec = proc.kwargs.get("preexec_fn")
        assert preexec is not None, "no memory cap without RTS options"

        applied: list = []
        with patch.object(
            resource, "setrlimit", side_effect=lambda *a: applied.append(a)
        ):
            preexec()
        expected = (1000 + conv._RLIMIT_AS_HEADROOM_MB) * 1024 * 1024
        assert applied == [(resource.RLIMIT_AS, (expected, expected))]

    def test_rlimit_headroom_is_sane(self):
        """Headroom covers code, RTS bookkeeping and mmaps; a huge value
        would quietly turn the fallback cap into no cap."""
        conv = _conv()

        assert 256 <= conv._RLIMIT_AS_HEADROOM_MB <= 1024

    @pytest.mark.parametrize(
        ("rts", "expected"),
        [
            (True, "2048 MiB heap cap"),
            (False, "2560 MiB address-space limit"),
        ],
    )
    def test_heap_exhaustion_fails_typed_with_the_effective_limit(
        self, tmp_path, rts, expected
    ):
        """The failure names the limit actually in force: the heap cap,
        or (fallback) heap + headroom as an address-space limit."""
        conv = _conv()
        if not rts and not sys.platform.startswith("linux"):
            pytest.skip("RLIMIT_AS fallback is Linux-only")

        class _Exhausted(_FakeProc):
            returncode = 251

        with (
            _pandoc_available(conv, rts=rts),
            patch.object(conv.subprocess, "Popen", return_value=_Exhausted()),
        ):
            with pytest.raises(conv.PandocConversionError) as excinfo:
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path, max_heap_mb=2048
                )

        message = str(excinfo.value)
        assert "out of memory" in message
        assert expected in message, message


class TestRtsProbeCache:
    """A successful probe is remembered; a failed one is retried, but
    at most once per ``_RTS_PROBE_FAILURE_TTL_S``."""

    @staticmethod
    def _run(conv, results, times):
        probes: list = []

        def probe(pandoc):
            probes.append(pandoc)
            return results[min(len(probes), len(results)) - 1]

        answers = []
        clock = iter(times)
        with (
            patch.dict(conv._rts_probe_cache, clear=True),
            patch.object(conv, "_probe_rts_heap_cap", side_effect=probe),
            patch.object(
                conv, "time", SimpleNamespace(monotonic=lambda: next(clock))
            ),
        ):
            for _ in times:
                answers.append(conv._pandoc_accepts_rts_heap_cap("pandoc"))
        return answers, probes

    def test_success_is_cached(self):
        conv = _conv()

        answers, probes = self._run(conv, [True], [0.0, 1.0, 10_000.0])

        assert answers == [True, True, True]
        assert len(probes) == 1

    def test_failure_is_retried_after_the_ttl_only(self):
        conv = _conv()
        ttl = conv._RTS_PROBE_FAILURE_TTL_S

        answers, probes = self._run(
            conv, [False, True], [0.0, ttl / 2, ttl + 1.0, ttl + 2.0]
        )

        # Cached failure inside the window, re-probed after it, and the
        # later success sticks.
        assert answers == [False, False, True, True]
        assert len(probes) == 2

    def test_failure_ttl_is_bounded(self):
        conv = _conv()

        assert 30 <= conv._RTS_PROBE_FAILURE_TTL_S <= 3600

    def test_probe_timeout_counts_as_failure(self):
        conv = _conv()

        def slow(*args, **kwargs):
            raise subprocess.TimeoutExpired(cmd="pandoc", timeout=30)

        with patch.object(conv.subprocess, "run", side_effect=slow):
            assert conv._probe_rts_heap_cap("pandoc") is False


class TestDefaultsAndHygiene:
    def test_default_timeout_is_bounded_and_reaches_the_subprocess(
        self, tmp_path
    ):
        conv = _conv()
        fake_popen, launched = _recording_popen()

        with (
            _pandoc_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            with pytest.raises(conv.PandocConversionError):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path
                )

        assert launched[0].communicate_timeout == (
            conv.DEFAULT_CONVERSION_TIMEOUT_S
        )
        assert 30 <= conv.DEFAULT_CONVERSION_TIMEOUT_S <= 300

    def test_default_output_ceiling_refuses_one_byte_over(self, tmp_path):
        """The default ceiling is 64 MiB and applies without kwargs; a
        sparse file keeps the check cheap."""
        conv = _conv()
        assert conv.DEFAULT_MAX_OUTPUT_BYTES == 64 * 1024 * 1024

        def sparse(out):
            with out.open("wb") as fh:
                fh.truncate(conv.DEFAULT_MAX_OUTPUT_BYTES + 1)

        fake_popen, _ = _recording_popen(write_output=sparse)
        with (
            _pandoc_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            with pytest.raises(conv.PandocConversionError, match="larger"):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path
                )

    def test_oversized_output_is_never_read(self, tmp_path):
        """The size check must come BEFORE the read: reading first
        materializes the whole runaway output in the worker."""
        conv = _conv()
        reads: list = []
        real_read_bytes = Path.read_bytes

        def spy(self):
            reads.append(self.name)
            return real_read_bytes(self)

        fake_popen, _ = _recording_popen(
            write_output=lambda out: out.write_bytes(b"x" * 5000)
        )
        with (
            _pandoc_available(conv),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
            patch.object(Path, "read_bytes", spy),
        ):
            with pytest.raises(conv.PandocConversionError, match="larger"):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=tmp_path, max_output_bytes=100
                )

        assert "out.html" not in reads, "oversized output was read"

    @pytest.mark.parametrize("succeed", [True, False])
    def test_owned_work_dir_is_removed(self, tmp_path, succeed):
        """With no work_dir the wrapper owns a mkdtemp dir; it must be
        gone afterwards on success and on failure."""
        conv = _conv()
        created: list[Path] = []
        real_mkdtemp = conv.tempfile.mkdtemp

        def recording_mkdtemp(*args, **kwargs):
            path = real_mkdtemp(*args, dir=tmp_path, **kwargs)
            created.append(Path(path))
            return path

        fake_popen, _ = _recording_popen(
            write_output=(
                (lambda out: out.write_text("<p>ok</p>", encoding="utf-8"))
                if succeed
                else None
            )
        )
        with (
            _pandoc_available(conv),
            patch.object(conv.tempfile, "mkdtemp", recording_mkdtemp),
            patch.object(conv.subprocess, "Popen", side_effect=fake_popen),
        ):
            if succeed:
                assert conv.pandoc_convert_bytes_to_html(b"x", ".rst")
            else:
                with pytest.raises(conv.PandocConversionError):
                    conv.pandoc_convert_bytes_to_html(b"x", ".rst")

        assert len(created) == 1, created
        assert not created[0].exists(), "owned work dir left behind"


class TestOdtIsPandocBacked:
    def test_every_pandoc_loader_extension_is_routed(self):
        """Every registered loader that shells out to pandoc must be
        in the bounded set — .odt included (partition_odt ->
        pypandoc.convert_file, unbounded)."""
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        conv = _conv()
        pandoc_loaders = {
            "UnstructuredODTLoader",
            "UnstructuredEPubLoader",
            "UnstructuredRTFLoader",
            "UnstructuredRSTLoader",
            "UnstructuredOrgModeLoader",
        }
        registered = {
            ext
            for ext, entry in LOADER_REGISTRY.items()
            if entry["loader_class"].__name__ in pandoc_loaders
        }

        assert ".odt" in conv.PANDOC_BACKED_EXTENSIONS
        assert registered <= conv.PANDOC_BACKED_EXTENSIONS, (
            registered - conv.PANDOC_BACKED_EXTENSIONS
        )


_FAKE_BINARY = """#!/bin/sh
sleep 20 &
echo $! > "{pidfile}"
wait
"""

#: A pandoc without -rtsopts: rejects ``+RTS``, otherwise writes -o.
_NO_RTSOPTS_BINARY = """#!/bin/sh
if [ "$1" = "+RTS" ]; then
  echo "Most RTS options are disabled" >&2
  exit 1
fi
out=""
while [ $# -gt 0 ]; do
  if [ "$1" = "-o" ]; then out="$2"; fi
  shift
done
echo "<p>converted without rtsopts</p>" > "$out"
"""


def _pid_alive(pid: int) -> bool:
    try:
        with open(f"/proc/{pid}/stat", encoding="utf-8") as fh:
            state = fh.read().rsplit(")", 1)[1].split()[0]
    except (FileNotFoundError, ProcessLookupError, IndexError):
        return False
    return state not in ("Z", "X")


def _script(tmp_path: Path, name: str, body: str) -> Path:
    """Write an executable script, skipping on a noexec tmp_path."""
    path = tmp_path / name
    path.write_text(body, encoding="utf-8")
    path.chmod(0o755)
    probe = tmp_path / "exec-probe"
    probe.write_text("#!/bin/sh\nexit 0\n", encoding="utf-8")
    probe.chmod(0o755)
    try:
        subprocess.run([str(probe)], check=True, timeout=10)
    except (OSError, subprocess.SubprocessError):
        pytest.skip("tmp_path does not allow executing scripts")
    return path


def _wait_for_pid(pidfile: Path, timeout_s: float = 5.0) -> int:
    deadline = time.monotonic() + timeout_s
    while time.monotonic() < deadline:
        try:
            text = pidfile.read_text(encoding="utf-8").strip()
        except FileNotFoundError:
            text = ""
        if text:
            return int(text)
        time.sleep(0.05)
    pytest.skip("child never reached its spawn point (loaded host)")


@pytest.mark.skipif(
    not sys.platform.startswith("linux"), reason="uses /proc and /bin/sh"
)
class TestRealProcessTree:
    """A real child that spawns a sleeping grandchild: the timeout must
    return promptly AND take the grandchild down with it."""

    def test_timeout_kills_the_whole_tree(self, tmp_path):
        conv = _conv()
        pidfile = tmp_path / "grandchild.pid"
        binary = _script(
            tmp_path, "fake-pandoc", _FAKE_BINARY.format(pidfile=pidfile)
        )
        work = tmp_path / "work"
        work.mkdir()

        grandchild = None
        try:
            with (
                patch.object(
                    conv, "resolve_pandoc_binary", return_value=str(binary)
                ),
                patch.object(
                    conv, "_pandoc_accepts_rts_heap_cap", return_value=True
                ),
            ):
                started = time.monotonic()
                with pytest.raises(conv.PandocConversionError, match="timed"):
                    conv.pandoc_convert_bytes_to_html(
                        b"x", ".rst", work_dir=work, timeout_s=2
                    )
                elapsed = time.monotonic() - started

            assert elapsed < 12, f"timeout took {elapsed:.1f}s to return"
            grandchild = _wait_for_pid(pidfile)
            deadline = time.monotonic() + 5
            while _pid_alive(grandchild) and time.monotonic() < deadline:
                time.sleep(0.05)
            assert not _pid_alive(grandchild), "grandchild survived the kill"
        finally:
            if grandchild is None and pidfile.exists():
                text = pidfile.read_text(encoding="utf-8").strip()
                grandchild = int(text) if text else None
            if grandchild is not None:
                try:
                    os.kill(grandchild, signal.SIGKILL)
                except (ProcessLookupError, PermissionError):
                    pass


@pytest.mark.skipif(
    not sys.platform.startswith("linux"),
    reason="RLIMIT_AS fallback is Linux-only; uses /bin/sh",
)
class TestBinaryWithoutRtsopts:
    """A pandoc built without ``-rtsopts`` against the real probe."""

    def test_real_probe_falls_back_to_rlimit_and_converts(self, tmp_path):
        conv = _conv()
        binary = _script(tmp_path, "pandoc-no-rts", _NO_RTSOPTS_BINARY)
        work = tmp_path / "work"
        work.mkdir()
        launched: list = []
        real_popen = subprocess.Popen

        def spy(command, **kwargs):
            launched.append((command, kwargs))
            return real_popen(command, **kwargs)

        with (
            patch.dict(conv._rts_probe_cache, clear=True),
            patch.object(
                conv, "resolve_pandoc_binary", return_value=str(binary)
            ),
            patch.object(conv.subprocess, "Popen", side_effect=spy),
        ):
            html = conv.pandoc_convert_bytes_to_html(
                b"x", ".rst", work_dir=work, timeout_s=20
            )

        assert b"converted without rtsopts" in html
        command, kwargs = launched[-1]
        assert "+RTS" not in command
        assert kwargs.get("preexec_fn") is not None, "fallback cap missing"

    def test_wrong_probe_costs_availability_not_the_cap(self, tmp_path):
        """If the probe wrongly says yes, the binary rejects ``+RTS``
        and the conversion fails typed — pandoc never runs uncapped."""
        conv = _conv()
        binary = _script(tmp_path, "pandoc-no-rts", _NO_RTSOPTS_BINARY)
        work = tmp_path / "work"
        work.mkdir()

        with (
            patch.object(
                conv, "resolve_pandoc_binary", return_value=str(binary)
            ),
            patch.object(
                conv, "_pandoc_accepts_rts_heap_cap", return_value=True
            ),
        ):
            with pytest.raises(conv.PandocConversionError, match="exit 1"):
                conv.pandoc_convert_bytes_to_html(
                    b"x", ".rst", work_dir=work, timeout_s=20
                )