| """C2 dual-compile state machine + outcome (``docs/02`` §7). |
| |
| The C2 compiler runs two *independent* models (a proposer and a verifier) over |
| the same candidate and reconciles their outputs through a deterministic gate. |
| The gate has five terminal states, evaluated in priority order — the first |
| observed failure wins: |
| |
| * ``ALL_PASS`` — both models compiled a program; the two programs share a |
| canonical hash (§7.2), agree on the FULL-world answer, and agree on every |
| mutation in the §7.3 equivalence suite. |
| * ``PARSE_FAIL`` — at least one model's response did not yield a valid program |
| after the single allowed repair. That model's record is |
| ``compile_status="invalid_response"``. |
| * ``CANONICAL_HASH_DIFF`` — both programs parsed, but their canonical program |
| hashes differ (``REJECT_PROGRAM_COMPILE_DISAGREE``, §7.2). |
| * ``FULL_ANSWER_DIFF`` — the canonical hashes match yet the executor computes |
| different ``(status, answer)`` on the untransformed world (a canonicalizer |
| invariant guard). |
| * ``MUTATION_DIFF`` — hashes and the full answer agree, but some §7.3 mutation |
| makes the two programs diverge (a canonicalizer invariant guard). |
| |
| ``FULL_ANSWER_DIFF`` / ``MUTATION_DIFF`` are defense-in-depth: with a correct |
| canonicalizer, matching hashes imply identical programs, so the suite is a guard |
| that *validates* the canonicalizer's equivalence claim rather than an independent |
| reject path. The mutation suite still runs and is recorded whenever the earlier |
| checks pass, per §7.3 ("mutation suite is essential"). |
| |
| A C2-compiled program is **never** used in primary evaluation (§7); the records |
| produced here feed training-time augmentation only. |
| """ |
|
|
| from __future__ import annotations |
|
|
| from dataclasses import dataclass, field |
| from typing import Any, Literal |
|
|
| C2State = Literal[ |
| "ALL_PASS", |
| "PARSE_FAIL", |
| "CANONICAL_HASH_DIFF", |
| "FULL_ANSWER_DIFF", |
| "MUTATION_DIFF", |
| ] |
|
|
| |
| _STATE_PRIORITY: dict[C2State, int] = { |
| "PARSE_FAIL": 0, |
| "CANONICAL_HASH_DIFF": 1, |
| "FULL_ANSWER_DIFF": 2, |
| "MUTATION_DIFF": 3, |
| "ALL_PASS": 4, |
| } |
|
|
|
|
| @dataclass(frozen=True) |
| class MutationEvidence: |
| """One row of the §7.3 mutation equivalence suite. |
| |
| ``agree`` is ``True`` iff the proposer and verifier programs produced the |
| same ``(status, answer_canonical)`` on ``mutation.description``. |
| """ |
|
|
| leaf: str |
| kind: str |
| description: str |
| proposer_status: str |
| proposer_answer: str | int | bool | None |
| verifier_status: str |
| verifier_answer: str | int | bool | None |
| agree: bool |
|
|
|
|
| @dataclass(frozen=True) |
| class C2Outcome: |
| """The reconciled result of one C2 dual-compile. |
| |
| ``proposer_record`` / ``verifier_record`` are ``compiled_program``-schema |
| dicts (one per model). ``mutation_evidence`` is empty unless the suite ran |
| (parse + canonical hash both passed). ``state`` is the gate verdict. |
| """ |
|
|
| state: C2State |
| proposer_record: dict[str, Any] |
| verifier_record: dict[str, Any] |
| canonical_hash_proposer: str | None |
| canonical_hash_verifier: str | None |
| full_answer_proposer: tuple[str, str | int | bool | None] | None |
| full_answer_verifier: tuple[str, str | int | bool | None] | None |
| mutation_evidence: tuple[MutationEvidence, ...] = field(default_factory=tuple) |
| reason: str = "" |
|
|
| @property |
| def accepted(self) -> bool: |
| """``True`` iff the dual-compile passed every gate (§7 acceptance).""" |
| return self.state == "ALL_PASS" |
|
|
|
|
| def worst(*states: C2State) -> C2State: |
| """Return the highest-priority (most-severe) observed state. |
| |
| An empty input is ``ALL_PASS`` (nothing failed). Used when several checks run |
| unconditionally and the most-severe failure must be reported. |
| """ |
| if not states: |
| return "ALL_PASS" |
| return min(states, key=lambda s: _STATE_PRIORITY[s]) |
|
|
|
|
| __all__ = ["C2Outcome", "C2State", "MutationEvidence", "worst"] |
|
|