"""The warden's outcome vocabulary — full supervisor fidelity, one table (M2-P1 §1.6).

The supervisor (``mgf-test-supervisor``) speaks seven outcomes in ``run.jsonl``
(``records.OUTCOMES``; mirrored by ``mgf.common.testing.Outcome``): ``passed`` /
``failed`` / ``error`` / ``crashed`` / ``hung`` / ``skipped`` / ``quarantined``.
The warden used to flatten that to four (``crashed``/``hung`` → ``error``,
``quarantined`` → ``skipped``) — a hang and an assertion error are *different
facts* and the console must not blur them. This module is now the single place
the vocabulary is mapped, in two explicit layers:

``OUTCOME_TO_RESULT``
    supervisor outcome → warden result, **fidelity-preserving** (``hung`` stays
    ``hung``). The warden result is the model + live-wire vocabulary: it flows
    through ``broker.result()``, the ``test_end``/``assert`` live NDJSON events
    (whose ``result`` field is a free string by schema), and the console chips.
    An outcome *not* in this table maps to ``"unknown"`` — **loudly** (the
    runner emits an ``error`` event naming the unmapped outcome); it is never
    silently reported as ``error``. A new upstream outcome (e.g. a future
    supervisor release extending the vocabulary) therefore renders honestly as
    ``unknown`` at runtime, and the contract pin test — which iterates the
    *installed* package's ``OUTCOMES`` — goes red until this table gains an
    explicit row. Feature-detection, not a version pin.

``RESULT_TO_RECORD_STATUS``
    warden result → the durable run-record's ``result``. The fedres 0.1
    ``test_status_value`` enum (``pass``/``fail``/``error``/``skipped``/
    ``unknown``) predates the fidelity work and has no ``hung``/``crashed``/
    ``quarantined`` — so the durable field carries the nearest honest verdict
    (``hung``/``crashed`` → ``error``; ``quarantined`` → ``skipped``) and the
    recorder preserves the *verbatim* result on the record's audit trail
    (``{"event": "outcome", "note": "hung"}``), which is schema-legal and what
    the History view renders. Widening the enum itself is mgf-fedres's call
    (route via FEEDBACK.md, never a local schema edit).
"""

from __future__ import annotations

#: Supervisor outcome (verbatim, from ``run.jsonl``) → warden result.
#: FULL FIDELITY — ``hung``/``crashed``/``quarantined`` stay distinct.
OUTCOME_TO_RESULT: dict[str, str] = {
    "passed": "pass",
    "failed": "fail",
    "error": "error",
    "crashed": "crashed",
    "hung": "hung",
    "skipped": "skipped",
    "quarantined": "quarantined",
}

#: The loud fallback for an outcome this build has no row for.
UNKNOWN_RESULT: str = "unknown"

#: Everything ``broker.result()`` can utter: the mapped results, plus
#: ``cancelled`` (operator intent, authored by the broker itself — not a
#: supervisor outcome) and the loud ``unknown`` fallback.
RESULT_VALUES: frozenset[str] = frozenset(OUTCOME_TO_RESULT.values()) | {
    "cancelled",
    UNKNOWN_RESULT,
}

#: The console's display/filter ordering of the outcome vocabulary (the chip
#: language; ``cancelled`` is a lease-audit fact, not an outcome chip).
DISPLAY_ORDER: tuple[str, ...] = (
    "pass",
    "fail",
    "error",
    "crashed",
    "hung",
    "skipped",
    "quarantined",
    UNKNOWN_RESULT,
)

#: Warden result → fedres 0.1 ``test_status_value`` for the durable run-record.
#: Explicit and total over ``RESULT_VALUES``; anything else projects to
#: ``unknown`` (honest), never silently to ``error``.
RESULT_TO_RECORD_STATUS: dict[str, str] = {
    "pass": "pass",
    "fail": "fail",
    "error": "error",
    "crashed": "error",
    "hung": "error",
    "skipped": "skipped",
    "quarantined": "skipped",
    "cancelled": "error",
    UNKNOWN_RESULT: UNKNOWN_RESULT,
}


def record_status(result: str) -> str:
    """Project a warden result onto the fedres 0.1 run-record enum (total)."""
    return RESULT_TO_RECORD_STATUS.get(result, UNKNOWN_RESULT)
