sungguk's picture
Release visual answerability benchmark v1.0.0
e1ced61 verified
Raw
History Blame Contribute Delete
10.1 kB
"""The C2 dual-compile orchestrator (``docs/02`` §7).
:class:`C2Compiler` runs the proposer (Qwen3.5-9B) and verifier (Gemma-4-12B-it)
*independently* over one :class:`C2Candidate` and reconciles their outputs through
the §7 gate:
1. build the §7.1 request (shared, hides intervention / sibling output /
transformed image);
2. each model compiles → parse to a typed program with one allowed repair;
3. §7.2 canonical-program exact-match (``REJECT_PROGRAM_COMPILE_DISAGREE``);
4. full-world answer agreement via the deterministic P3 executor;
5. §7.3 mutation equivalence suite (``≥8`` alternatives per dependency leaf).
The first observed failure fixes the :class:`C2State`; only ``ALL_PASS`` is
accepted (§7 acceptance). A C2 program is never used in primary evaluation —
the records feed training-time augmentation only. Model calls go through the
injected :class:`ModelClient`; nothing here fakes a response.
"""
from __future__ import annotations
from dataclasses import dataclass
from ..dsl.ast import Dsl, Program
from ..executors import get_executor
from ..executors.base import ExecutionResult
from ..sources.base import World
from .client import ModelClient
from .mutations import deterministic_mutations
from .parse import ParsedProgram, parse_program_response
from .record import ParseStatus, build_compiled_program
from .request import C2Candidate, CompileRequest, build_compile_request
from .state import C2Outcome, MutationEvidence
# Parse-status literals matched to ``compiled_program.schema.json``.
_FIRST: ParseStatus = "valid_first_response"
_REPAIR: ParseStatus = "valid_after_one_repair"
_INVALID: ParseStatus = "invalid"
@dataclass(frozen=True)
class C2Compiler:
"""Inject the two independent model clients and compile candidates."""
proposer: ModelClient
verifier: ModelClient
def compile(self, candidate: C2Candidate, *, seed: int = 0) -> C2Outcome:
request = build_compile_request(candidate)
world = candidate.world
prop_parsed, prop_response_sha, prop_status = self._compile_one(
self.proposer, request, candidate
)
ver_parsed, ver_response_sha, ver_status = self._compile_one(
self.verifier, request, candidate
)
proposer_record = build_compiled_program(
base_id=candidate.base_id,
question_sha256=candidate.question_sha256,
choices_sha256=candidate.choices_sha256,
compiler_role="program_proposer",
model_repo_id=self.proposer.model_repo_id,
model_revision=self.proposer.model_revision,
request_sha256=request.request_sha256,
response_sha256=prop_response_sha,
parse_status=prop_status,
parsed=prop_parsed if prop_parsed.ok else None,
)
verifier_record = build_compiled_program(
base_id=candidate.base_id,
question_sha256=candidate.question_sha256,
choices_sha256=candidate.choices_sha256,
compiler_role="independent_program_verifier",
model_repo_id=self.verifier.model_repo_id,
model_revision=self.verifier.model_revision,
request_sha256=request.request_sha256,
response_sha256=ver_response_sha,
parse_status=ver_status,
parsed=ver_parsed if ver_parsed.ok else None,
)
# Gate 1: both models must produce a valid program.
if not (prop_parsed.ok and ver_parsed.ok):
return C2Outcome(
state="PARSE_FAIL",
proposer_record=proposer_record,
verifier_record=verifier_record,
canonical_hash_proposer=prop_parsed.canonical_program_sha256,
canonical_hash_verifier=ver_parsed.canonical_program_sha256,
full_answer_proposer=None,
full_answer_verifier=None,
reason="one or both models failed to compile a valid program",
)
# Gate 2: §7.2 canonical-program exact-match.
hash_p = prop_parsed.canonical_program_sha256
hash_v = ver_parsed.canonical_program_sha256
if hash_p != hash_v:
return C2Outcome(
state="CANONICAL_HASH_DIFF",
proposer_record=proposer_record,
verifier_record=verifier_record,
canonical_hash_proposer=hash_p,
canonical_hash_verifier=hash_v,
full_answer_proposer=None,
full_answer_verifier=None,
reason="REJECT_PROGRAM_COMPILE_DISAGREE: canonical hashes differ",
)
# Gate 3: full-world answer agreement (canonicalizer-invariant guard).
assert prop_parsed.program is not None and ver_parsed.program is not None
full_p, full_v, full_agree = check_full_answer(
prop_parsed.program, ver_parsed.program, world, dsl=candidate.dsl
)
if not full_agree:
return C2Outcome(
state="FULL_ANSWER_DIFF",
proposer_record=proposer_record,
verifier_record=verifier_record,
canonical_hash_proposer=hash_p,
canonical_hash_verifier=hash_v,
full_answer_proposer=full_p,
full_answer_verifier=full_v,
reason="programs disagree on the FULL-world answer",
)
# Gate 4: §7.3 mutation equivalence suite.
evidence, diverged = run_mutation_suite(
prop_parsed.program, ver_parsed.program, world, dsl=candidate.dsl, seed=seed
)
if diverged:
return C2Outcome(
state="MUTATION_DIFF",
proposer_record=proposer_record,
verifier_record=verifier_record,
canonical_hash_proposer=hash_p,
canonical_hash_verifier=hash_v,
full_answer_proposer=full_p,
full_answer_verifier=full_v,
mutation_evidence=tuple(evidence),
reason="programs diverge on a §7.3 mutation",
)
return C2Outcome(
state="ALL_PASS",
proposer_record=proposer_record,
verifier_record=verifier_record,
canonical_hash_proposer=hash_p,
canonical_hash_verifier=hash_v,
full_answer_proposer=full_p,
full_answer_verifier=full_v,
mutation_evidence=tuple(evidence),
reason="dual-compile accepted (canonical match + full + mutations)",
)
# --- one model's compile-with-one-repair -------------------------------
def _compile_one(
self, client: ModelClient, request: CompileRequest, candidate: C2Candidate
) -> tuple[ParsedProgram, str, ParseStatus]:
response = client.compile(request)
parsed = parse_program_response(
response.text,
dsl=candidate.dsl,
world=candidate.world,
base_id=candidate.base_id,
question_sha256=candidate.question_sha256,
choices_sha256=candidate.choices_sha256,
)
if parsed.ok:
return parsed, response.response_sha256, _FIRST
# The single allowed repair (§7): re-ask with the first-response errors.
repaired = client.repair(request, list(parsed.errors))
again = parse_program_response(
repaired.text,
dsl=candidate.dsl,
world=candidate.world,
base_id=candidate.base_id,
question_sha256=candidate.question_sha256,
choices_sha256=candidate.choices_sha256,
)
if again.ok:
return again, repaired.response_sha256, _REPAIR
return parsed, response.response_sha256, _INVALID
def _answer_key(result: ExecutionResult) -> tuple[str, str | int | bool | None]:
"""The comparison key for answer agreement (status + canonical answer)."""
return (result.status, result.answer_canonical)
def check_full_answer(
proposer: Program, verifier: Program, world: World, *, dsl: Dsl
) -> tuple[tuple[str, str | int | bool | None], tuple[str, str | int | bool | None], bool]:
"""Execute both programs on the FULL world; return ``(key_p, key_v, agree)``.
Extracted so the gate-3 branch is unit-testable with crafted programs (the
pipeline otherwise short-circuits at the §7.2 canonical-hash diff).
"""
executor = get_executor(dsl)
exec_p = executor.execute(proposer, world=world)
exec_v = executor.execute(verifier, world=world)
key_p, key_v = _answer_key(exec_p), _answer_key(exec_v)
return key_p, key_v, key_p == key_v
def run_mutation_suite(
proposer: Program,
verifier: Program,
world: World,
*,
dsl: Dsl,
seed: int,
) -> tuple[list[MutationEvidence], bool]:
"""Run the §7.3 mutation suite; return ``(evidence, diverged)``.
Extracted so the gate-4 branch is unit-testable with crafted programs.
"""
executor = get_executor(dsl)
exec_p = executor.execute(proposer, world=world)
leaves = exec_p.dependency_node_ids
mutations = deterministic_mutations(world, leaves, dsl=dsl, seed=seed)
evidence: list[MutationEvidence] = []
diverged = False
for mut in mutations:
e_p = executor.execute(proposer, world=mut.world)
e_v = executor.execute(verifier, world=mut.world)
kp, kv = _answer_key(e_p), _answer_key(e_v)
agree = kp == kv
evidence.append(
MutationEvidence(
leaf=mut.leaf,
kind=mut.kind,
description=mut.description,
proposer_status=e_p.status,
proposer_answer=e_p.answer_canonical,
verifier_status=e_v.status,
verifier_answer=e_v.answer_canonical,
agree=agree,
)
)
if not agree:
diverged = True
return evidence, diverged
__all__ = ["C2Compiler", "check_full_answer", "run_mutation_suite"]