# SPDX-License-Identifier: AGPL-3.0-or-later
"""Reviewer probes for docs/research/phase5-run-entry-fixes-2-review-brief.md.

One case per new finding the review filed; every case is replayable on its own:

- ``test_review_f27_mark_infeasible_requires_no_failure_history`` proves the
  guard on `Controller.mark_infeasible` (finding F27) by driving it on a node
  with zero recorded failures; it asserts the enforced refusal
  (`pytest.raises(WorkflowError)`) with the node and run outcome unchanged, so
  the surface can no longer pass the "Deterministic infeasibility policy"
  guard on prose alone.
- ``test_review_f28_documented_commands_are_python3_and_run_as_written`` proves
  the `docs/run-entry.md` Commands-section form (finding F28): every documented
  head is the `python3 scripts/orchestrate.py …` form the clean-env pin
  executes, the system-`python3`-must-resolve-`yaml` prerequisite is named,
  no bare `scripts/orchestrate.py …` head is documented, and the documented
  form runs as written.
- ``test_review_settle_gap_adversarial_order_keeps_document_fresh`` is the
  executable record of the walked same-instant residual: a second process's
  complete `stop` committed between the settle's point-of-emission fresh read
  and its own emissions still leaves the returned document equal to the durable
  fresh outcome, with the attempt finally settled and a late node-level chain
  appended to the log.
- The four ``test_review_f30_*`` probes pin the Guarded-surface shape (finding
  F30): an accepted lineage settles exactly once through one
  `NODE_MARKED_INFEASIBLE`, a duplicate `mark_infeasible` dedupes against the
  shared `infeasible-{node_id}` key (history +0), a late call with a recorded
  failure on a terminal run is retained with no outcome rewrite, and the
  zero-fingerprint call the F27 guard refuses on a terminal run admits nothing
  and leaves node and outcome unchanged.

Every case is slow-marked: the probes build real repositories and really
dispatch sandboxed commands (the project oracle's fast gate deselects slow
tests, and this module is no exception).
"""

from __future__ import annotations

import os
import shutil
import subprocess
import sys
from pathlib import Path

import pytest

from orchestrator import run_entry
from orchestrator.controller import Controller, WorkflowError
from orchestrator.executor import ScriptedExecutor
from orchestrator.store import EventStore

pytestmark = pytest.mark.slow

_REPO_ROOT = Path(__file__).resolve().parents[1]
_SCRIPT = _REPO_ROOT / "scripts" / "orchestrate.py"

_GIT_ENV = {
    "GIT_AUTHOR_NAME": "operator",
    "GIT_AUTHOR_EMAIL": "operator@localhost",
    "GIT_COMMITTER_NAME": "operator",
    "GIT_COMMITTER_EMAIL": "operator@localhost",
}


def _make_repo(root: str) -> tuple[str, str]:
    """A small arbitrary repository with one derivable component."""
    repo = os.path.join(root, "target-repo")
    os.makedirs(os.path.join(repo, "src"))
    with open(os.path.join(repo, "src", "app.py"), "w") as handle:
        handle.write("def value():\n    raise NotImplementedError('value')\n")
    for args in (["init", "-q"], ["add", "-A"], ["commit", "-q", "-m", "base"]):
        subprocess.run(
            ["git", "-C", repo, *args],
            env={**os.environ, **_GIT_ENV},
            check=True,
            capture_output=True,
        )
    commit = subprocess.run(
        ["git", "-C", repo, "rev-parse", "HEAD"],
        capture_output=True,
        text=True,
        check=True,
    ).stdout.strip()
    return repo, commit


def _spec(repo: str, commit: str, run_id: str, command: str) -> dict:
    return {
        "run_id": run_id,
        "repository": repo,
        "base_commit": commit,
        "snapshot_id": f"TARGET-{run_id}",
        "task_statement": "Make the visible check pass.",
        "project_kernel": {"kernel_id": f"KERNEL-{run_id}"},
        "visible_commands": [command],
        "components": [["core", "src/app.py"]],
        "owned_scope": ["src/**"],
        "acceptance_criteria": ["AC-CORE"],
        "budgets": {"MODEL_COST": 10.0},
        "limits": {"actions": 4, "wall_clock_seconds": 60},
        "policy_version": 1,
        "evidence_policy": {"level": "MEDIUM", "version": 1},
        "executor_profile": "command-executor-v1",
    }


def test_review_f27_mark_infeasible_requires_no_failure_history(tmp_path):
    """Finding F27: a public surface that admits a terminal decision while its
    only guard ("Deterministic infeasibility policy passes") was prose. The
    guard is now enforced inside the surface: a zero-failure drive must be
    refused with a `WorkflowError` and admit no event, leaving the node and the
    run outcome untouched."""
    repo, commit = _make_repo(str(tmp_path))
    spec = run_entry.validate_run_spec(_spec(repo, commit, "RUN-RV-F27", "true"))
    store = EventStore(os.path.join(str(tmp_path), "f27.db"))
    try:
        controller, _target, run_id, root = run_entry.build_run(
            spec, store=store, workdir=str(tmp_path)
        )
        state = store.state(run_id)
        assert not state.failure_fingerprints.get(root)
        assert state.nodes[root].state != "INFEASIBLE"
        history_before = len(store.history(run_id))
        # The enforced guard refuses this emission: no failure was ever
        # recorded on the lineage, so the deterministic-infeasibility policy
        # did not pass. The assertion names the finding.
        with pytest.raises(WorkflowError):
            controller.mark_infeasible(run_id, root)
        state = store.state(run_id)
        assert state.nodes[root].state != "INFEASIBLE", (
            "F27: mark_infeasible admitted a terminal decision for a lineage "
            "with zero recorded failures, driving the run outcome to "
            f"{state.outcome!r} from the prose guard alone"
        )
        assert state.outcome is None
        assert len(store.history(run_id)) == history_before
    finally:
        store.close()


def _commands_section_heads() -> list[str]:
    """The non-blank command heads of `docs/run-entry.md`'s Commands block."""
    document = (_REPO_ROOT / "docs" / "run-entry.md").read_text()
    section = document.split("## Commands", 1)[1]
    block = section.split("```text", 1)[1].split("```", 1)[0]
    return [line.strip() for line in block.splitlines() if line.strip()]


def _documented_interpreter() -> str:
    """The interpreter the documented CLI form names: the `python3` on PATH
    when it can import `yaml`, else the interpreter running this suite (the
    same substitution ``test_clean_env_cli_shape`` records)."""
    candidate = shutil.which("python3") or "python3"
    probe = subprocess.run([candidate, "-c", "import yaml"], capture_output=True)
    if probe.returncode == 0:
        return candidate
    return sys.executable


def _commands_section_prose() -> str:
    """The Commands section's prose, with the fenced command block removed.

    The documented heads and their argument shapes live inside the `text`
    block; the prerequisite claim lives in the surrounding sentences. Reading
    the prose apart from the block keeps the assertion from being satisfied by
    the argument shapes themselves (`RUN_SPEC.yaml`, the `python3` interpreter)
    — row I's vacuous-truth class (F32)."""
    document = (_REPO_ROOT / "docs" / "run-entry.md").read_text()
    section = document.split("## Commands", 1)[1]
    before, _rest = section.split("```text", 1)
    _block, after = _rest.split("```", 1)
    return before + after


def test_review_f28_documented_commands_are_python3_and_run_as_written():
    """Finding F28: docs/run-entry.md's Commands section named the bare form
    `scripts/orchestrate.py ...`, which the delivered 100644 script cannot run
    as written. The section is now the `python3` form the clean-env pin
    executes, the system-`python3`-must-resolve-`yaml` prerequisite is named,
    no bare head is documented, and the documented form runs as written."""
    heads = _commands_section_heads()
    assert heads, "docs/run-entry.md's Commands section names no commands"
    for head in heads:
        assert head.startswith("python3 scripts/orchestrate.py "), (
            "F28: docs/run-entry.md's Commands section documents "
            f"{head!r}; every documented head must be the `python3 "
            "scripts/orchestrate.py ...` form the clean-env pin executes"
        )
        assert not head.startswith("scripts/orchestrate.py "), (
            "F28: docs/run-entry.md's Commands section documents the bare "
            f"{head!r} form as runnable"
        )
    prose = _commands_section_prose()
    assert "yaml" in prose and "python3" in prose, (
        "F28: docs/run-entry.md's Commands section does not name the "
        "system-`python3`-must-resolve-`yaml` prerequisite beside its heads"
    )
    completed = subprocess.run(
        [_documented_interpreter(), str(_SCRIPT), "--help"],
        capture_output=True,
        text=True,
        cwd=str(_REPO_ROOT),
        env={key: value for key, value in os.environ.items() if key != "PYTHONPATH"},
    )
    assert completed.returncode == 0, completed.stderr
    assert "ModuleNotFoundError" not in completed.stderr


def test_review_settle_gap_adversarial_order_keeps_document_fresh(tmp_path, monkeypatch):
    """The walked same-instant residual of the settle-claim discipline: the
    second process's complete `stop` committed in the sub-step gap between the
    settle's point-of-emission fresh read and its own emissions. The returned
    document must still name the durable fresh outcome, the attempt's accounting
    must stay finally settled, and the gap appends the late node-level chain to
    the log (the shape docs/research/phase5-run-entry-fixes-2-implementer.md's
    SHAPES section names)."""
    from orchestrator import run_entry as run_entry_module

    repo, commit = _make_repo(str(tmp_path))
    run_id = "RUN-RV-GAP"
    spec = run_entry.validate_run_spec(
        _spec(repo, commit, run_id, "python3 -c \"import time; time.sleep(30)\"")
    )
    spec["limits"] = {"actions": 4, "wall_clock_seconds": 1}
    store_path = os.path.join(str(tmp_path), "gap.db")
    store = EventStore(store_path)
    real_replay = store.replay_fresh
    rules = {"settle_reads": 0}

    def interleaved_replay_fresh(rid):
        result = real_replay(rid)
        # The settle phase's reads are the ones whose projection shows the
        # attempt terminal (TIMED_OUT) while the run is not: the decision
        # read, the point-of-emission read, and the post-admission re-read.
        if any(a.state == "TIMED_OUT" for a in result.attempts.values()):
            rules["settle_reads"] += 1
            if rules["settle_reads"] == 2:
                # The point-of-emission read returned None; the second process
                # now completes its whole stop inside the gap, before the
                # settle's own emissions.
                second = EventStore(store_path)
                try:
                    run_entry_module.stop_run(second, run_id)
                finally:
                    second.close()
        return result

    monkeypatch.setattr(store, "replay_fresh", interleaved_replay_fresh)
    try:
        result = run_entry.start_run(spec, store=store, workdir=str(tmp_path))
        assert result.accepted is False
        # The counter must be read before this test's own inspection replay.
        settle_reads = rules["settle_reads"]
        fresh = store.replay_fresh(run_id)
        # The document equals the durable fresh outcome even though the stop
        # landed inside the gap.
        assert result.outcome == fresh.outcome == "CANCELLED"
        attempt = fresh.attempts[result.attempt_id]
        assert attempt.state == "TIMED_OUT"
        assert attempt.accounting_state == "FINALLY_SETTLED"
        assert fresh.nodes[result.root_node_id].state == "CANCELLED"
        # The walked residual's late node-level chain is present in the log.
        tail = [event.event_type for event in store.history(run_id)]
        cancelled_at = len(tail) - 1 - tail[::-1].index("NODE_CANCELLED")
        assert tail[cancelled_at + 1 :] == ["FAILURE_RECORDED", "NODE_MARKED_INFEASIBLE"]
        # Three settle-phase fresh reads: decision, point of emission,
        # post-admission re-read.
        assert settle_reads == 3
    finally:
        store.close()


# -- Guarded-surface shape (F30) ---------------------------------------------


def test_review_f30_mark_infeasible_settles_an_accepted_lineage_once(tmp_path):
    """F30 facet 1: a lineage with a recorded failure settles exactly once —
    one NODE_MARKED_INFEASIBLE, READY -> INFEASIBLE through any_non_terminal."""
    repo, commit = _make_repo(str(tmp_path))
    spec = run_entry.validate_run_spec(_spec(repo, commit, "RUN-RV-F30A", "true"))
    store = EventStore(os.path.join(str(tmp_path), "f30a.db"))
    try:
        controller, _target, run_id, root = run_entry.build_run(
            spec, store=store, workdir=str(tmp_path)
        )
        controller.record_failure(
            run_id,
            root,
            action_type="EXECUTE",
            diagnostic_category="VISIBLE_VALIDATION_FAILED",
        )
        assert store.state(run_id).nodes[root].state == "READY"
        controller.mark_infeasible(run_id, root)
        state = store.state(run_id)
        assert state.nodes[root].state == "INFEASIBLE"
        assert state.outcome == "INFEASIBLE"
        types = [event.event_type for event in store.history(run_id)]
        assert types.count("NODE_MARKED_INFEASIBLE") == 1
    finally:
        store.close()


def test_review_f30_duplicate_mark_infeasible_dedupes_the_shared_key(tmp_path):
    """F30 facet 2: a duplicate mark_infeasible dedupes against the shared
    `infeasible-{node_id}` idempotency key: history +0, outcome unchanged."""
    repo, commit = _make_repo(str(tmp_path))
    spec = run_entry.validate_run_spec(_spec(repo, commit, "RUN-RV-F30B", "true"))
    store = EventStore(os.path.join(str(tmp_path), "f30b.db"))
    try:
        controller, _target, run_id, root = run_entry.build_run(
            spec, store=store, workdir=str(tmp_path)
        )
        controller.record_failure(
            run_id,
            root,
            action_type="EXECUTE",
            diagnostic_category="VISIBLE_VALIDATION_FAILED",
        )
        controller.mark_infeasible(run_id, root)
        history_before = len(store.history(run_id))
        controller.mark_infeasible(run_id, root)
        assert len(store.history(run_id)) == history_before, (
            "F30: the duplicate mark_infeasible did not dedupe against the "
            "shared infeasible-{node_id} key"
        )
        assert store.state(run_id).outcome == "INFEASIBLE"
        assert (
            [event.event_type for event in store.history(run_id)].count(
                "NODE_MARKED_INFEASIBLE"
            )
            == 1
        )
    finally:
        store.close()


def test_review_f30_late_mark_infeasible_on_terminal_run_is_retained(tmp_path):
    """F30 facet 3: a late mark_infeasible with a recorded failure on a terminal
    run is admitted as a retained no-op: the outcome is not rewritten and the
    node stays terminal."""
    repo, commit = _make_repo(str(tmp_path))
    spec = run_entry.validate_run_spec(_spec(repo, commit, "RUN-RV-F30C", "true"))
    store = EventStore(os.path.join(str(tmp_path), "f30c.db"))
    try:
        result = run_entry.start_run(spec, store=store, workdir=str(tmp_path))
        assert result.outcome == "ACCEPTANCE_CLAIMED"
        controller = Controller(store, ScriptedExecutor({}))
        controller.record_failure(
            result.run_id,
            result.root_node_id,
            action_type="EXECUTE",
            diagnostic_category="LATE_ON_TERMINAL",
        )
        controller.mark_infeasible(result.run_id, result.root_node_id)
        state = store.state(result.run_id)
        assert state.nodes[result.root_node_id].state == "ACCEPTED"
        assert state.outcome == "ACCEPTANCE_CLAIMED"
        assert "NODE_MARKED_INFEASIBLE" in [
            event.event_type for event in store.history(result.run_id)
        ], "the late terminal stamp must be admitted (retained), not refused"
    finally:
        store.close()


def test_review_f30_zero_fingerprint_terminal_run_refuses(tmp_path):
    """F27/F30: a zero-fingerprint mark_infeasible on a terminal run refuses
    with WorkflowError before any event; node and outcome unchanged."""
    repo, commit = _make_repo(str(tmp_path))
    spec = run_entry.validate_run_spec(_spec(repo, commit, "RUN-RV-F30D", "true"))
    store = EventStore(os.path.join(str(tmp_path), "f30d.db"))
    try:
        result = run_entry.start_run(spec, store=store, workdir=str(tmp_path))
        assert result.outcome == "ACCEPTANCE_CLAIMED"
        controller = Controller(store, ScriptedExecutor({}))
        history_before = len(store.history(result.run_id))
        with pytest.raises(WorkflowError):
            controller.mark_infeasible(result.run_id, result.root_node_id)
        state = store.state(result.run_id)
        assert state.nodes[result.root_node_id].state == "ACCEPTED"
        assert state.outcome == "ACCEPTANCE_CLAIMED"
        assert len(store.history(result.run_id)) == history_before
    finally:
        store.close()
