# SPDX-License-Identifier: AGPL-3.0-or-later
"""Review probe for the phase6 grading-seed step.

Filed by the review at `docs/research/phase6-grading-seed-review-brief.md`.
One shape the delivered module does not build, green at FINAL
`b852fd43d381627b7045411e382ebc803002441d`:

``test_broken_source_grades_identically_across_consecutive_runs`` — five
consecutive ``gate-check-tests`` grades of the neutralisation test's
broken-source candidate (the near-deadline, failing-and-shrinking shape the
lottery actually flips), each recorded with its wall. The delivered
determinism pin grades only the known-good candidate, which never crosses
the fixture's 60.0s deadline in this environment; this probe extends the
verdict-identity claim to the unstable half of the surface and records
whether the seeded gate grades it identically across consecutive runs.

Measured on byte-verified scratch copies at the fixture's own 60.0s
deadline: unseeded at REV ``FAIL`` x5 (44.43s-48.29s per grade), seeded at
FINAL ``FAIL`` x5 (44.43s-46.31s per grade). Measured in the workspace under
this machine's background load (the review session's own CLI plus the
user's browser sessions, load average ~4-5): the seeded grade at the same
60.0s deadline flipped mid-run once (``FAIL``, ``FAIL``, then
``CANDIDATE_TIMEOUT`` x3 at 57.16s-60.10s) and held at ``FAIL`` x5 once more
(56.28s-59.29s) — the 60.0s deadline sits ~1.3x above this candidate's
unloaded wall, so the wall class survives the seed. This probe therefore
builds its manifest with the oracle's own default
``gate_timeout_seconds=300.0`` and asserts the draw class alone: five
identical ``FAIL`` verdicts with the deadline out of the way. The load
class is carried as a finding (F16) beside the review report.

This is a review probe, not a delivered pin: it rides red until absorbed.
A module that runs sandboxed suites must be slow-marked so the nested fast
gate (``-m "not slow"``) deselects it."""

from __future__ import annotations

import os
import shutil
import subprocess
import time

import pytest

from orchestrator import experiment, oracle, project_oracle


pytestmark = pytest.mark.slow


_TREE_IGNORE = shutil.ignore_patterns(
    ".git", ".venv", "evidence", "__pycache__", ".pytest_cache", ".hypothesis", ".ruff_cache"
)


def _repo_root() -> str:
    return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def _snapshot_repo(root: str) -> str:
    source = _repo_root()
    repo = os.path.join(root, "orchestrator-target")
    shutil.copytree(
        source,
        repo,
        ignore=shutil.ignore_patterns(
            ".git", ".venv", "evidence", "__pycache__",
            ".pytest_cache", ".hypothesis", ".ruff_cache", "node_modules",
        ),
    )
    for argv in (
        ("git", "init", "-q", repo),
        ("git", "-C", repo, "config", "user.email", "phase3@example.invalid"),
        ("git", "-C", repo, "config", "user.name", "Phase 3 test snapshot"),
        ("git", "-C", repo, "add", "-A"),
        ("git", "-C", repo, "commit", "-q", "-m", "phase3 oracle test snapshot"),
    ):
        subprocess.run(argv, check=True, capture_output=True)
    return repo


def _case_tree(root: str, repo: str, name: str) -> str:
    tree = os.path.join(root, name)
    shutil.copytree(repo, tree, ignore=_TREE_IGNORE)
    os.makedirs(os.path.join(tree, ".venv"), exist_ok=True)
    return tree


def _break_candidate_source(tree: str) -> None:
    """The neutralisation test's broken-source shape (`src/orchestrator/reducer.py`)."""
    path = os.path.join(tree, "src", "orchestrator", "reducer.py")
    with open(path, encoding="utf-8") as handle:
        text = handle.read()
    needle = "NODE_TABLE = tables.node_table()"
    assert needle in text
    text = text.replace(
        needle, needle + "\nNODE_TABLE = {}  # candidate regression", 1
    )
    with open(path, "w", encoding="utf-8") as handle:
        handle.write(text)


def _build_self_target(repo: str):
    commit = subprocess.run(
        ("git", "-C", repo, "rev-parse", "HEAD"),
        check=True, capture_output=True, text=True,
    ).stdout.strip()
    return experiment.build_target(
        repo, commit, snapshot_id="TARGET-SELF-PROBE-1",
        task_statement="Satisfy the repository's own acceptance gates.",
        project_kernel={
            "kernel_id": "KERNEL-SELF-PROBE-1",
            "purpose": "The orchestrator repository as its own acceptance target.",
            "target_users": "Self-host rehearsals.",
            "desired_outcomes": ["The repository's gates stay green under its frozen floors."],
            "current_scope": ["**"],
            "current_non_goals": ["Candidate production"],
        },
        visible_commands=("true",),
        components=(("core", "**"),), owned_scope=("**",),
        acceptance_criteria=project_oracle.CRITERIA,
    )


@pytest.fixture(scope="module")
def probe_context(tmp_path_factory):
    """Build the trusted manifest with the oracle's own default gate deadline
    (300.0s) rather than the fixture's 60.0s, so the probe measures the draw
    class (seeded determinism) with the wall class out of the way; the
    fixture's 60.0s shape is load-fragile for this candidate and is carried
    as finding F16 beside the review report."""
    repo = _snapshot_repo(str(tmp_path_factory.mktemp("phase3-probe-target")))
    target = _build_self_target(repo)
    manifest = project_oracle.build_project_oracle(
        target,
        repo_root=repo,
        venv_path=os.path.join(_repo_root(), ".venv"),
        gate_timeout_seconds=300.0,
    )
    return target, manifest


@pytest.mark.timeout(1200)
def test_broken_source_grades_identically_across_consecutive_runs(
    tmp_path, probe_context
):
    """Five consecutive gate-check-tests grades of the broken-source tree
    (the lottery's flipping shape), each recorded with its wall."""
    target, manifest = probe_context
    repo = target.repository
    check = next(c for c in manifest.checks if c.check_id == "gate-check-tests")

    def grade_one(tree: str):
        one = oracle.OracleManifest(
            oracle_version=manifest.oracle_version,
            criteria=manifest.criteria,
            checks=(check,),
            target_binding="",
        )
        start = time.monotonic()
        outcome = oracle.grade_manifest(one, tree, runner=None).outcomes["gate-check-tests"]
        return outcome, time.monotonic() - start

    tree = _case_tree(str(tmp_path), repo, "tree-broken-probe")
    _break_candidate_source(tree)

    runs = 5
    results = [grade_one(tree) for _ in range(runs)]
    for index, (verdict, wall) in enumerate(results, start=1):
        print(f"PROBE broken-source grade {index}/{runs}: verdict={verdict} wall={wall:.2f}s")
    assert all(
        verdict == oracle.GRADE_FAIL for verdict, _wall in results
    ), f"the broken-source candidate graded other than FAIL: {results}"
