# SPDX-License-Identifier: AGPL-3.0-or-later
"""The probe-oracle containment contract (Phase 2 review ruling + F2/F5/F6).

The ruling's shape: a probe is a data-only input driver running the candidate
in the sandbox, with expected values compared controller-side, wrapped by the
harness's per-check copy, tamper digest, and hard deadline. These tests pin
the hostile-candidate rows of the review's containment matrix and the
identity/binding rules.
"""

from __future__ import annotations

import dataclasses
import tempfile

import pytest

import types

from orchestrator import experiment_spec, oracle
from orchestrator.hidden_oracle import sandboxed_probe


def _check(probe, timeout=2.0, check_id="c"):
    return oracle.OracleCheck(
        check_id, ("AC",), 1.0, oracle.GRADE_PASS, timeout, True, probe=probe
    )


def _run(probe, tree, timeout=2.0):
    return oracle._run_check(_check(probe, timeout), tree, runner=None)


def test_expected_values_never_reach_the_candidate_process(tmp_path):
    """Review F2: the candidate's process sees inputs only. A candidate that
    dumps its own command line and environment cannot recover the expected
    comparison (it lives in the controller-side compare)."""
    tree = tmp_path / "tree"
    (tree / "pkg").mkdir(parents=True)
    (tree / "pkg" / "mod.py").write_text(
        "def leak():\n"
        "    return open('/proc/self/cmdline', 'rb').read().decode(errors='replace')\n"
    )
    seen = {}

    driver = (
        "import json, sys\n"
        "sys.path.insert(0, '/work')\n"
        "from pkg.mod import leak\n"
        "print('COLLECT:' + json.dumps({'cmdline': leak()}))\n"
    )

    def compare(payload):
        seen.update(payload)
        # The held-out expectation lives here, controller-side. A candidate
        # reading its own process sees the inputs (the driver) and never this
        # comparison.
        return (
            oracle.GRADE_PASS
            if "SENTINEL_EXPECTED" not in payload["cmdline"]
            else oracle.GRADE_FAIL
        )

    probe = sandboxed_probe(driver=driver, compare=compare, timeout_seconds=5.0)
    assert _run(probe, str(tree)) == oracle.GRADE_PASS
    # The held-out sentinel is the compare's own business: it is nowhere in
    # the candidate's process view (argv, driver source, environment).
    assert "SENTINEL_EXPECTED" not in seen["cmdline"]


def test_candidate_mutation_of_its_graded_copy_is_a_fail(tmp_path):
    """Review matrix row 'mutates its own graded tree mid-probe': the harness
    digest grades it FAIL, and each check's copy is private so contamination
    cannot cross checks."""
    tree = tmp_path / "tree"
    (tree / "pkg").mkdir(parents=True)
    (tree / "pkg" / "mod.py").write_text("VALUE = 1\n")

    mutate_driver = (
        "import sys\n"
        "sys.path.insert(0, '/work')\n"
        "open('/work/pkg/mod.py', 'a').write('VALUE = 2\\n')\n"
        "print('COLLECT:' + '{}')\n"
    )
    mutator = sandboxed_probe(
        driver=mutate_driver,
        compare=lambda payload: oracle.GRADE_PASS,
        timeout_seconds=5.0,
    )
    assert _run(mutator, str(tree)) == oracle.GRADE_FAIL

    # The original graded tree is untouched (the mutation hit only the copy).
    assert (tree / "pkg" / "mod.py").read_text() == "VALUE = 1\n"

    # A later check still sees the pristine tree (no cross-check contamination).
    observer = sandboxed_probe(
        driver=(
            "import json, sys\n"
            "sys.path.insert(0, '/work')\n"
            "print('COLLECT:' + json.dumps({'value': open('/work/pkg/mod.py').read()}))\n"
        ),
        compare=lambda payload: (
            oracle.GRADE_PASS
            if payload["value"] == "VALUE = 1\n"
            else oracle.GRADE_FAIL
        ),
        timeout_seconds=5.0,
    )
    assert _run(observer, str(tree)) == oracle.GRADE_PASS


def test_probe_deadline_and_base_exception_are_contained(tmp_path):
    """Review F5: a probe without its own budget cannot hang grading (the
    harness deadline fires), and a bare SystemExit is contained and mapped to
    infrastructure — never escapes grading."""
    tree = tmp_path / "tree"
    tree.mkdir()

    def hang(_tree):
        import time

        while True:
            time.sleep(0.05)

    assert _run(hang, str(tree), timeout=0.5) == oracle.GRADE_CANDIDATE_TIMEOUT

    def exits(_tree):
        raise SystemExit(1)

    assert _run(exits, str(tree)) == oracle.GRADE_INFRASTRUCTURE_ERROR

    def interrupts(_tree):
        raise KeyboardInterrupt()

    assert _run(interrupts, str(tree)) == oracle.GRADE_INFRASTRUCTURE_ERROR


def _probe_tree_names(root):
    import os

    return {
        name
        for name in os.listdir(root)
        if name.startswith(("orchestrator-probe-", "orchestrator-probe-exec-"))
    }


@pytest.mark.timeout(120)
def test_killed_probe_grade_reaps_every_intermediate_tree(tmp_path, monkeypatch):
    """F15: the kill path the settled ``finally`` never sees. A probe that
    raises ``KeyboardInterrupt`` mid-grade (the contained kill path; a SIGKILL
    kills the runner itself) and whose grade has built its sandbox root beside
    the execution copy (``hidden_oracle.run_sandboxed_probe``'s
    ``new_root=f"{tree_path}-root"``) leaves no ``orchestrator-probe-*`` tree
    behind: the janitor reaps every tree the run registered, the sandbox root
    included.

    The window is this run's own tmp root (F2): the harness builds its
    intermediate trees in ``tempfile.gettempdir()``, so the pin redirects that
    to ``tmp_path`` and lists only that root. Scanning the whole shared
    ``gettempdir()`` would couple the member to every concurrent run's trees,
    which the battery's parallel interleave reddens with no defect present."""
    import os
    import tempfile

    monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmp_path))
    root = str(tmp_path)
    tree = tmp_path / "tree"
    tree.mkdir()
    before = _probe_tree_names(root)

    def interrupts_mid_grade(execution_tree):
        # The sanctioned probe's sandbox root sits beside the execution copy.
        os.mkdir(f"{execution_tree}-root")
        raise KeyboardInterrupt()

    assert _run(interrupts_mid_grade, str(tree)) == oracle.GRADE_INFRASTRUCTURE_ERROR
    assert _probe_tree_names(root) - before == set()


def test_probe_identity_binds_behaviour_not_qualname():
    """Review F6: factory-produced probes share a qualname; the identity must
    distinguish behaviour (closure parameters) and stay reproducible."""

    def factory(flag):
        def compare(payload):
            return oracle.GRADE_PASS if payload.get("v") == flag else oracle.GRADE_FAIL

        def probe(tree_path):
            return compare({"v": 1})

        return probe

    one_a, one_b, two = factory(1), factory(1), factory(2)
    assert one_a.__qualname__ == two.__qualname__
    assert oracle.probe_identity(one_a) == oracle.probe_identity(one_b)
    assert oracle.probe_identity(one_a) != oracle.probe_identity(two)

    # The freeze hash binds the identity: swapping the probe changes it.
    manifest = oracle.OracleManifest(
        "v",
        (oracle.OracleCriterion("AC", mandatory=True, weight=1.0),),
        (_check(one_a),),
        target_binding="",
    )
    swapped = dataclasses.replace(manifest, checks=(_check(two),))
    assert oracle.probe_identity(manifest.checks[0].probe) != oracle.probe_identity(
        swapped.checks[0].probe
    )


def test_open_defects_driver_accepts_a_parameterized_pin_node():
    """P2-4: the actual registry metrics driver normalizes pytest's
    `test_fn[case]` node id to its registered test function for the
    unregistered-pin comparison."""
    from orchestrator.hashing import TOOLCHAIN_PATHS
    from orchestrator.project_oracle import _OPEN_DEFECTS_DRIVER, _bind_registry
    import os
    import yaml

    tree = tempfile.mkdtemp()
    os.makedirs(os.path.join(tree, "tests"))
    with open(os.path.join(tree, "tests", "test_bug_example.py"), "w") as handle:
        handle.write(
            "import pytest\n"
            "@pytest.mark.parametrize('value', [1])\n"
            "def test_bug_example(value):\n"
            "    assert value == 1\n"
        )
    registry = {
        "min_fixed": 1,
        "min_open": 0,
        "defects": {
            "example": {
                "status": "fixed",
                "tests": ["tests/test_bug_example.py::test_bug_example[1]"],
            }
        },
    }
    with open(os.path.join(tree, "tests", "open_defects.yaml"), "w") as handle:
        yaml.safe_dump(registry, handle)

    payload = {}

    def compare(result):
        payload.update(result)
        return (
            oracle.GRADE_PASS
            if result.get("unregistered") == [] and result.get("vacuous") == []
            else oracle.GRADE_FAIL
        )

    probe = sandboxed_probe(
        driver=_bind_registry(_OPEN_DEFECTS_DRIVER, "tests/open_defects.yaml"),
        compare=compare,
        timeout_seconds=10.0,
        ro_paths=((os.path.abspath(".venv"), "/work/.venv"),),
        interpreter="/work/.venv/bin/python",
    )
    assert _run(probe, tree, timeout=10.0) == oracle.GRADE_PASS
    assert payload["unregistered"] == []
    assert payload["vacuous"] == []


def test_flooding_candidate_is_bounded_and_graded(tmp_path):
    """Review matrix row 'floods stdout/stderr': the sandbox capture is
    bounded — the controller never buffers the flood. A payload that arrives
    within the bound is graded; a payload drowned by a flood is missing
    evidence and fails closed, never passing."""
    tree = tmp_path / "tree"
    tree.mkdir()

    payload_first = sandboxed_probe(
        driver=(
            "import sys\n"
            "print('COLLECT:' + '{}')\n"
            "sys.stdout.write('x' * 5_000_000)\n"
            "sys.stdout.flush()\n"
        ),
        compare=lambda payload: oracle.GRADE_PASS if payload == {} else oracle.GRADE_FAIL,
        timeout_seconds=10.0,
    )
    assert _run(payload_first, str(tree), timeout=15.0) == oracle.GRADE_PASS

    flood_first = sandboxed_probe(
        driver=(
            "import sys\n"
            "sys.stdout.write('x' * 5_000_000)\n"
            "sys.stdout.flush()\n"
            "print('COLLECT:' + '{}')\n"
        ),
        compare=lambda payload: oracle.GRADE_PASS if payload == {} else oracle.GRADE_FAIL,
        timeout_seconds=10.0,
    )
    assert _run(flood_first, str(tree), timeout=15.0) == oracle.GRADE_FAIL


def test_harness_deadline_beats_the_probe_budget_review_n3(tmp_path):
    """Re-review N3: a harness-deadline overrun raised inside the sanctioned
    probe is CANDIDATE_TIMEOUT, never a mislabelled INFRASTRUCTURE_ERROR. The
    probe's own sandbox budget is deliberately longer than the check's."""
    tree = tmp_path / "tree"
    tree.mkdir()
    probe = sandboxed_probe(
        driver="import time\ntime.sleep(30)\n",
        compare=lambda payload: oracle.GRADE_PASS,
        timeout_seconds=30.0,
    )
    assert _run(probe, str(tree), timeout=0.5) == oracle.GRADE_CANDIDATE_TIMEOUT


def test_probe_identity_binds_referenced_globals_review_n4():
    """Re-review N4: a probe reading a module global keeps one identity while
    its behaviour flips — the identity must bind the referenced globals."""
    module = types.ModuleType("probe_under_test")
    exec(compile("def probe(tree):\n    return MODE\n", "probe_under_test", "exec"), module.__dict__)
    module.MODE = "PASS"
    first = oracle.probe_identity(module.probe)
    module.MODE = "FAIL"
    second = oracle.probe_identity(module.probe)
    assert first != second
    module.MODE = "PASS"
    assert oracle.probe_identity(module.probe) == first


def test_freeze_refuses_raw_probes_unless_hashed_opt_in():
    """Re-review N5: the ruling's shape is enforced at freeze. A RAW probe
    freezes only under an explicit allowance that enters the content hash."""

    def raw(tree_path):
        return oracle.GRADE_PASS

    manifest = oracle.OracleManifest(
        "v",
        (oracle.OracleCriterion("AC", mandatory=True, weight=1.0),),
        (_check(raw),),
        target_binding="",
    )
    target = _target_stub()
    bound = dataclasses.replace(
        manifest, target_binding=experiment_spec.oracle_target_binding(target, manifest)
    )
    cases = experiment_spec.CalibrationReport(cases=_five_cases())
    with pytest.raises(experiment_spec.ExperimentError):
        experiment_spec.freeze_manifest(
            target=target,
            oracle_manifest=bound,
            limits=experiment_spec.placeholder_limits(),
            calibration=cases,
        )
    frozen = experiment_spec.freeze_manifest(
        target=target,
        oracle_manifest=bound,
        limits=experiment_spec.placeholder_limits(),
        calibration=cases,
        allow_unverified_probes=True,
    )
    assert frozen.unverified_probes_allowed is True
    # The allowance enters the experiment's identity.
    assert frozen.content_hash() != dataclasses.replace(
        frozen, unverified_probes_allowed=False
    ).content_hash()
    # The sanctioned shape freezes with no allowance at all.
    good = oracle.OracleManifest(
        "v",
        (oracle.OracleCriterion("AC", mandatory=True, weight=1.0),),
        (_check(sandboxed_probe(
            driver="print('COLLECT:' + '{}')",
            compare=lambda payload: oracle.GRADE_PASS,
            timeout_seconds=5.0,
        )),),
        target_binding="",
    )
    good_bound = dataclasses.replace(
        good, target_binding=experiment_spec.oracle_target_binding(target, good)
    )
    ok = experiment_spec.freeze_manifest(
        target=target,
        oracle_manifest=good_bound,
        limits=experiment_spec.placeholder_limits(),
        calibration=cases,
    )
    assert ok.unverified_probes_allowed is False


def test_missing_graded_tree_is_infrastructure_review_n6(tmp_path):
    """Re-review N6: an oracle-side I/O failure (an unreadable graded tree)
    is INFRASTRUCTURE_ERROR, never a raw exception escaping grading."""
    probe = sandboxed_probe(
        driver="print('COLLECT:' + '{}')",
        compare=lambda payload: oracle.GRADE_PASS,
        timeout_seconds=5.0,
    )
    assert _run(probe, str(tmp_path / "absent")) == oracle.GRADE_INFRASTRUCTURE_ERROR


def test_probe_copy_rejects_symlink_and_special_file_before_execution(tmp_path):
    """Re-review P1-3: the per-check copier must not follow a candidate
    symlink to /dev/zero (unbounded grader-side copy) or copy a FIFO/device.
    Both are candidate failures before the probe runs, under the harness
    deadline."""
    import os
    import time

    tree = tmp_path / "tree"
    tree.mkdir()
    (tree / "gate").symlink_to("/dev/zero")
    invoked = []
    probe = _check(lambda candidate: invoked.append(True) or oracle.GRADE_PASS)
    started = time.monotonic()
    assert _run(probe, str(tree), timeout=1.0) == oracle.GRADE_FAIL
    assert not invoked
    assert time.monotonic() - started < 2.0

    (tree / "gate").unlink()
    os.mkfifo(tree / "gate")
    started = time.monotonic()
    assert _run(probe, str(tree), timeout=1.0) == oracle.GRADE_FAIL
    assert not invoked
    assert time.monotonic() - started < 2.0


def test_probe_copy_refuses_symlink_to_infinite_stream_before_copy_review_p1_3(tmp_path):
    """Review P1-3: a candidate symlink to /dev/zero is never followed by the
    controller's pre-deadline copy. The tree is refused promptly as a
    candidate failure, not copied without bound and not mislabelled as
    infrastructure."""
    tree = tmp_path / "tree"
    (tree / "scripts").mkdir(parents=True)
    (tree / "scripts" / "check-static").symlink_to("/dev/zero")
    probe = _check(lambda candidate: open(candidate + "/scripts/check-static", "rb").read(1))
    import time

    started = time.monotonic()
    result = _run(probe, str(tree), timeout=2.0)
    assert result == oracle.GRADE_FAIL
    assert time.monotonic() - started < 2.0


def test_probe_copy_refuses_special_files_before_runner(tmp_path):
    """FIFOs/devices/sockets are not regular candidate inputs and are refused
    before a probe can block opening them."""
    import os
    import time

    tree = tmp_path / "tree"
    tree.mkdir()
    os.mkfifo(tree / "blocked")
    probe = _check(lambda candidate: open(candidate + "/blocked", "rb").read(1))
    started = time.monotonic()
    assert _run(probe, str(tree), timeout=2.0) == oracle.GRADE_FAIL
    assert time.monotonic() - started < 2.0


def test_probe_copy_refuses_internal_symlink_to_special_file(tmp_path):
    """An in-tree relative link whose target is a FIFO/device is refused too;
    containment must classify the target type, not only the link path."""
    import os
    import time

    tree = tmp_path / "tree"
    tree.mkdir()
    os.mkfifo(tree / "fifo")
    (tree / "alias").symlink_to("fifo")
    probe = _check(lambda candidate: oracle.GRADE_PASS)
    started = time.monotonic()
    assert _run(probe, str(tree), timeout=2.0) == oracle.GRADE_FAIL
    assert time.monotonic() - started < 2.0


def test_probe_copy_budget_and_deadline_cover_pre_run_copy(tmp_path):
    """Review P1-3: the wall-clock budget starts before the copy, and total
    copied bytes are capped. A huge regular file or a copier that overruns its
    budget cannot consume unbounded controller time/disk before the probe is
    called."""
    import time

    from orchestrator import oracle as oracle_module

    tree = tmp_path / "tree"
    tree.mkdir()
    # Oversize is refused before a probe runs (without allocating the file's
    # full contents in memory).
    huge = tree / "huge.bin"
    with open(huge, "wb") as handle:
        handle.truncate(oracle_module._MAX_PROBE_COPY_BYTES + 1)
    probe_called = []
    check = _check(lambda candidate: probe_called.append(True) or oracle.GRADE_PASS)
    started = time.monotonic()
    assert oracle_module._run_probe(check, str(tree)) == oracle.GRADE_FAIL
    assert time.monotonic() - started < 3.0
    assert not probe_called


def _target_stub():
    from orchestrator.experiment_target import TargetSnapshot

    return TargetSnapshot(
        snapshot_id="S",
        repository="/nonexistent",
        commit="0" * 40,
        task_statement="t",
        contract_hash="sha256:x",
        project_kernel={"kernel_id": "K"},
        project_kernel_hash="sha256:y",
        visible_commands=("true",),
        environment_image_digest="v",
        allowed_tools=(),
        components=(("c", "c/**"),),
    )


def _five_cases():
    def case(case_id):
        return experiment_spec.CalibrationOutcome(
            case_id=case_id,
            description=case_id,
            expected_classification="CORRECT_ACCEPTANCE",
            outcomes=(("c", "PASS"),),
            effective_outcomes=(("c", "PASS"),),
            verified_progress=1.0,
            criterion_verified_progress=1.0,
            classification="CORRECT_ACCEPTANCE",
            rerun_eligible=(),
            rerun_spent=False,
        )

    return tuple(
        case(name)
        for name in ("known-good", "unchanged", "known-broken", "known-timeout", "infrastructure")
    )
