File size: 5,067 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Certificate-derived and controlled-baseline target mapping."""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from .answers import UNANSWERABLE_TOKEN

ANSWERABLE_STATES = frozenset({"FULL", "A_SAME", "A_CHANGED"})
UNANSWERABLE_STATES = frozenset({"U_MISSING", "U_INVALID"})
TARGET_ROLES = frozenset({"TARGET", "TARGET_EVIDENCE", "TARGET_REFERENT", "SUBSTITUTE"})


class TargetMappingError(ValueError):
    """Raised when state, role, and certificate target disagree."""


def target_text(value: Any) -> str:
    if isinstance(value, bool):
        return "true" if value else "false"
    return str(value)


def ours_target(view: Mapping[str, Any], full_answer: Any) -> str:
    """Map a view state to the intervention-GRPO/EVI certificate target."""

    state = str(view.get("state", ""))
    candidate = view.get("target_canonical")
    full_text = target_text(full_answer)

    if state in {"FULL", "A_SAME"}:
        if candidate is not None and target_text(candidate) != full_text:
            raise TargetMappingError(f"{state} target differs from FULL")
        return full_text
    if state == "A_CHANGED":
        if candidate is None:
            raise TargetMappingError("A_CHANGED requires target_canonical")
        changed = target_text(candidate)
        if changed == full_text:
            raise TargetMappingError("A_CHANGED target equals FULL")
        return changed
    if state in UNANSWERABLE_STATES:
        if candidate is not None and target_text(candidate) != UNANSWERABLE_TOKEN:
            raise TargetMappingError(f"{state} target is not {UNANSWERABLE_TOKEN}")
        return UNANSWERABLE_TOKEN
    raise TargetMappingError(f"unknown intervention state: {state!r}")


def defacto_target(comparison_role: str, full_answer: Any) -> str:
    """Map controlled comparison roles to DeFacto's blanket-unknown target."""

    if comparison_role in {"POSITIVE", "CONTROL"}:
        return target_text(full_answer)
    if comparison_role in TARGET_ROLES:
        return UNANSWERABLE_TOKEN
    raise TargetMappingError(f"unknown comparison role: {comparison_role!r}")


def no_recompute_target(view: Mapping[str, Any], full_answer: Any) -> str:
    """Certificate-derived target with the recompute branch dropped.

    Identical to :func:`ours_target` except an ``A_CHANGED`` view is answered
    with the FULL answer (maintain) rather than the certificate's recomputed
    target, while abstention on ``U_MISSING``/``U_INVALID`` is preserved. This
    is the "intervention minus recompute" target -- equivalently "DeFacto plus
    certificate-derived abstention, no recompute" -- so ablation arms D and J
    share it: a single run answers both the recompute-isolation and the
    defacto-deconfound claims.
    """

    state = str(view.get("state", ""))
    candidate = view.get("target_canonical")
    full_text = target_text(full_answer)
    if state in {"FULL", "A_SAME"}:
        if candidate is not None and target_text(candidate) != full_text:
            raise TargetMappingError(f"{state} target differs from FULL")
        return full_text
    if state == "A_CHANGED":
        # Drop the recompute branch: maintain the FULL answer despite changed
        # evidence. The certificate's recomputed target is intentionally ignored.
        return full_text
    if state in UNANSWERABLE_STATES:
        if candidate is not None and target_text(candidate) != UNANSWERABLE_TOKEN:
            raise TargetMappingError(f"{state} target is not {UNANSWERABLE_TOKEN}")
        return UNANSWERABLE_TOKEN
    raise TargetMappingError(f"unknown intervention state: {state!r}")


def answerable_only_target(view: Mapping[str, Any], full_answer: Any) -> str:
    """Certificate-derived target with the abstention branch dropped.

    Identical to :func:`ours_target` except ``U_MISSING``/``U_INVALID`` views
    are answered with the FULL answer rather than abstaining, while the
    ``A_CHANGED`` recompute branch is preserved. This is the "intervention
    minus abstention" target (ablation arm C): it isolates the contribution of
    certificate-derived abstention by removing it.
    """

    state = str(view.get("state", ""))
    candidate = view.get("target_canonical")
    full_text = target_text(full_answer)
    if state in {"FULL", "A_SAME"}:
        if candidate is not None and target_text(candidate) != full_text:
            raise TargetMappingError(f"{state} target differs from FULL")
        return full_text
    if state == "A_CHANGED":
        if candidate is None:
            raise TargetMappingError("A_CHANGED requires target_canonical")
        changed = target_text(candidate)
        if changed == full_text:
            raise TargetMappingError("A_CHANGED target equals FULL")
        return changed
    if state in UNANSWERABLE_STATES:
        # Drop the abstention branch: answer the FULL answer even when the
        # certificate marks the evidence missing/invalid.
        return full_text
    raise TargetMappingError(f"unknown intervention state: {state!r}")