# SPDX-License-Identifier: AGPL-3.0-or-later
"""Provider-completion parsing for the smoke runner.

The real client must turn a provider's text into a body the reducer accepts: a
catalogued verdict, a non-empty subject, and a non-empty rationale. These tests
pin the tolerant extraction and the strict validation.
"""
from __future__ import annotations

import http.server
import importlib.util
import json
import os
import sys
import threading
import time

import pytest

from orchestrator import glossary, secrets

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def _load_smoke_run():
    spec = importlib.util.spec_from_file_location(
        "orchestrator_smoke_run", os.path.join(ROOT, "scripts", "smoke_run.py")
    )
    module = importlib.util.module_from_spec(spec)
    # Register before executing: dataclasses resolve annotations through
    # sys.modules[cls.__module__].
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


@pytest.fixture(scope="module")
def smoke_run():
    return _load_smoke_run()


def test_instruction_uses_the_authoritative_verdict_catalogue(smoke_run):
    for verdict in glossary.semantic_verdicts():
        assert verdict in smoke_run._SYSTEM_INSTRUCTION


def test_valid_json_is_normalised(smoke_run):
    body = smoke_run.parse_provider_completion(
        json.dumps(
            {"subject_reference": "n1", "verdict": "PASS", "rationale": "looks correct"}
        )
    )
    assert body == {
        "subject_reference": "n1",
        "verdict": "PASS",
        "rationale": "looks correct",
    }


def test_fenced_json_with_prose_is_extracted(smoke_run):
    content = 'Sure.\n```json\n{"subject_reference": "n1", "verdict": "FAIL", "rationale": "r"}\n```\n'
    assert smoke_run.parse_provider_completion(content)["verdict"] == "FAIL"


def test_lowercase_verdict_is_upper_cased(smoke_run):
    content = '{"subject_reference": "n1", "verdict": "uncertain", "rationale": "r"}'
    assert smoke_run.parse_provider_completion(content)["verdict"] == "UNCERTAIN"


def test_uncatalogued_verdict_is_refused(smoke_run):
    content = '{"subject_reference": "n1", "verdict": "INCONCLUSIVE", "rationale": "r"}'
    with pytest.raises(ValueError):
        smoke_run.parse_provider_completion(content)


def test_empty_rationale_is_refused(smoke_run):
    content = '{"subject_reference": "n1", "verdict": "PASS", "rationale": "  "}'
    with pytest.raises(ValueError):
        smoke_run.parse_provider_completion(content)


def test_non_object_completion_is_refused(smoke_run):
    with pytest.raises(ValueError):
        smoke_run.parse_provider_completion("no json here")


def test_transcript_records_bounded_redacted_prompt_and_reply(smoke_run, tmp_path):
    from orchestrator import secrets

    credential = secrets.Credential(
        credential_id="c1", scope="MODEL_ENDPOINT", secret="sekret-value", expires_at=1e12
    )
    path = str(tmp_path / "debug" / "transcript.jsonl")
    client = smoke_run.OpenAICompatibleClient(
        smoke_run.experiment.ModelProfile(
            model_id="m", model_version="1", temperature=0.0, top_p=1.0,
            max_output_tokens=8, input_tokens_per_call=8, output_tokens_per_call=8,
            cost_per_call=1.0,
        ),
        base_url="http://example.invalid",
        path="/v1/chat/completions",
        credential=credential,
        units="USD",
        price_input_per_mtok=1.0,
        price_output_per_mtok=1.0,
        max_input_tokens=8,
        timeout=1.0,
        transcript_path=path,
        credentials=(credential,),
    )
    client._record(
        {"attempt_id": "a1", "node_id": "n1", "prompt_hash": "sha256:p"},
        "prompt with sekret-value",
        "reply with sekret-value",
    )
    with open(path) as handle:
        entry = json.loads(handle.readline())
    assert "sekret-value" not in json.dumps(entry)
    assert entry["attempt_id"] == "a1"
    assert os.stat(path).st_mode & 0o777 == 0o600


def test_transcript_is_disabled_by_default(smoke_run, tmp_path):
    assert smoke_run.transcript_write("", {"a": 1}) is None
    assert not list(tmp_path.iterdir())


class _FakeResponse:
    def __init__(self, blocks, delay=0.0):
        self._blocks = list(blocks)
        self._delay = delay

    def __enter__(self):
        return self

    def __exit__(self, *exc):
        return False

    def read(self, _size):
        if self._delay:
            time.sleep(self._delay)
        return self._blocks.pop(0) if self._blocks else b""


def test_reply_size_cap_refuses_an_oversized_reply(smoke_run, monkeypatch):
    blocks = [b"x" * 4096 for _ in range(20)]

    def fake_urlopen(request, timeout=None):
        return _FakeResponse(blocks)

    monkeypatch.setattr(smoke_run.urllib.request, "urlopen", fake_urlopen)
    client = smoke_run.OpenAICompatibleClient(
        smoke_run.experiment.ModelProfile(
            model_id="m", model_version="1", temperature=0.0, top_p=1.0,
            max_output_tokens=8, input_tokens_per_call=8, output_tokens_per_call=8,
            cost_per_call=1.0,
        ),
        base_url="http://example.invalid", path="/v1",
        credential=secrets.Credential(
            credential_id="c1", scope="MODEL_ENDPOINT", secret="s", expires_at=1e12
        ),
        units="USD", price_input_per_mtok=1.0, price_output_per_mtok=1.0,
        max_input_tokens=8, timeout=10.0, max_reply_bytes=8192,
    )
    with pytest.raises(RuntimeError):
        client._post({}, time.monotonic() + 10)


class _DripHandler(http.server.BaseHTTPRequestHandler):
    """A real HTTP server that promises a body and dribbles it slowly."""

    def do_POST(self):
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", "64")
        self.end_headers()
        try:
            for _ in range(64):
                self.wfile.write(b"{")
                self.wfile.flush()
                time.sleep(0.2)
        except (BrokenPipeError, ConnectionResetError):
            pass

    def log_message(self, *args):
        pass


@pytest.fixture
def dripping_server():
    server = http.server.HTTPServer(("127.0.0.1", 0), _DripHandler)
    thread = threading.Thread(target=server.serve_forever, daemon=True)
    thread.start()
    try:
        yield server.server_address[1]
    finally:
        server.shutdown()


def test_total_deadline_bounds_a_real_dripping_reply(smoke_run, dripping_server):
    """Faithful: a real socket that dribbles must not outlive the deadline. The
    previous fake response ignored the read size, so it never exercised the
    blocking read (reply-boundary review F2)."""
    client = smoke_run.OpenAICompatibleClient(
        smoke_run.experiment.ModelProfile(
            model_id="m", model_version="1", temperature=0.0, top_p=1.0,
            max_output_tokens=8, input_tokens_per_call=8, output_tokens_per_call=8,
            cost_per_call=1.0,
        ),
        base_url=f"http://127.0.0.1:{dripping_server}", path="/v1",
        credential=secrets.Credential(
            credential_id="c1", scope="MODEL_ENDPOINT", secret="s", expires_at=1e12
        ),
        units="USD", price_input_per_mtok=1.0, price_output_per_mtok=1.0,
        max_input_tokens=8, timeout=10.0,
    )
    started = time.monotonic()
    with pytest.raises(TimeoutError):
        client._post({}, time.monotonic() + 0.5)
    assert time.monotonic() - started < 3.0


def test_transcript_is_chmod_0600_even_when_pre_existing(smoke_run, tmp_path):
    path = tmp_path / "pre-existing.jsonl"
    path.write_text("")
    os.chmod(path, 0o644)
    smoke_run.transcript_write(str(path), {"a": 1})
    assert os.stat(path).st_mode & 0o777 == 0o600


def test_transcript_binds_the_real_call_id_and_key(smoke_run, tmp_path):
    path = str(tmp_path / "bound.jsonl")
    credential = secrets.Credential(
        credential_id="c1", scope="MODEL_ENDPOINT", secret="s", expires_at=1e12
    )
    client = smoke_run.OpenAICompatibleClient(
        smoke_run.experiment.ModelProfile(
            model_id="m", model_version="1", temperature=0.0, top_p=1.0,
            max_output_tokens=8, input_tokens_per_call=8, output_tokens_per_call=8,
            cost_per_call=1.0,
        ),
        base_url="http://example.invalid", path="/v1", credential=credential,
        units="USD", price_input_per_mtok=1.0, price_output_per_mtok=1.0,
        max_input_tokens=8, timeout=10.0, transcript_path=path,
    )
    client._record({"call_id": "a1", "call_key": "n1"}, "p", "r")
    entry = json.loads(open(path).readline())
    assert entry["attempt_id"] == "a1"
    assert entry["node_id"] == "n1"


def test_run_limits_use_aggregate_ceilings_not_per_call_caps(smoke_run, monkeypatch):
    """The per-call declared cap bounds the client; the run aggregate ceiling
    bounds the trial. Conflating them starves the run (first real-run finding)."""
    monkeypatch.setenv("ORCHESTRATOR_MAX_OUTPUT_TOKENS", "100")
    monkeypatch.setenv("ORCHESTRATOR_RUN_MAX_OUTPUT_TOKENS", "9000")
    monkeypatch.setenv("ORCHESTRATOR_WORKDIR", "/tmp/unused")
    settings = smoke_run.load_settings()
    limits = smoke_run.experiment_limits(settings)
    assert settings.max_output_tokens == 100
    assert limits.root_model_output_tokens == 9000


def test_json_mode_routes_narration_to_stderr_and_emits_one_document(smoke_run, capsys):
    real_stdout = sys.stdout
    smoke_run._JSON_MODE = True
    smoke_run._REAL_STDOUT = real_stdout
    sys.stdout = sys.stderr
    try:
        smoke_run.log("hello")
        smoke_run.emit_json({"a": 1})
    finally:
        sys.stdout = real_stdout
        smoke_run._JSON_MODE = False
        smoke_run._REAL_STDOUT = None
    out, err = capsys.readouterr()
    assert "[01] hello" in err
    assert json.loads(out.strip()) == {"a": 1}


def test_json_mode_is_off_by_default(smoke_run, capsys):
    smoke_run.emit_json({"a": 1})
    smoke_run.log("hello")
    out, err = capsys.readouterr()
    assert "hello" in out
    assert err == ""


class _OnceResponse:
    def __init__(self, payload):
        self._payload = payload

    def __enter__(self):
        return self

    def __exit__(self, *exc):
        return False

    def read1(self, _size):
        payload, self._payload = self._payload, b""
        return payload


def test_client_sends_the_per_trial_sampling_seed(smoke_run, monkeypatch):
    captured = {}
    reply = {
        "choices": [
            {
                "message": {
                    "content": json.dumps(
                        {"subject_reference": "n", "verdict": "PASS", "rationale": "r"}
                    )
                },
                "finish_reason": "stop",
            }
        ],
        "usage": {"prompt_tokens": 1, "completion_tokens": 1},
    }

    def fake_urlopen(request, timeout=None):
        captured["body"] = json.loads(request.data)
        return _OnceResponse(json.dumps(reply).encode())

    monkeypatch.setattr(smoke_run.urllib.request, "urlopen", fake_urlopen)
    client = smoke_run.OpenAICompatibleClient(
        smoke_run.experiment.ModelProfile(
            model_id="m", model_version="1", temperature=0.0, top_p=1.0,
            max_output_tokens=8, input_tokens_per_call=8, output_tokens_per_call=8,
            cost_per_call=1.0,
        ),
        base_url="http://example.invalid", path="/v1",
        credential=secrets.Credential(
            credential_id="c1", scope="MODEL_ENDPOINT", secret="s", expires_at=1e12
        ),
        units="USD", price_input_per_mtok=1.0, price_output_per_mtok=1.0,
        max_input_tokens=8, timeout=10.0, sampling_seed=1003,
    )
    completion = client.complete({
        "prompt": "p", "prompt_hash": "sha256:x",
        "attempt_id": "a1", "node_id": "n1", "call_id": "a1", "call_key": "n1",
    })
    assert captured["body"]["seed"] == 1003
    assert completion.outputs[0].output_type == "SEMANTIC_EVALUATION"


def test_multi_seed_selection_scales_the_cap(smoke_run, monkeypatch):
    monkeypatch.setenv("ORCHESTRATOR_SEEDS", "1001,1002")
    monkeypatch.setenv("ORCHESTRATOR_OWNER", "o")
    monkeypatch.setenv("ORCHESTRATOR_CAP", "1000")
    monkeypatch.setenv("ORCHESTRATOR_WORKDIR", "/tmp/unused")
    settings = smoke_run.load_settings()
    assert settings.seeds == (1001, 1002)
    worksheet = smoke_run.cap_worksheet(settings)
    assert worksheet["trials"] == len(smoke_run.experiment.ARM_ORDER) * 2
    assert worksheet["required_cap"] == 2 * len(smoke_run.experiment.ARM_ORDER) * (
        settings.model_cost + settings.per_attempt_commitment
    )
