Spaces:
Sleeping
Sleeping
File size: 8,408 Bytes
26bf1c9 | 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | """
Deterministic Auditor policy backed by the rule-based Track A / Track B graders.
The Auditor is intentionally **deterministic by design**. It consumes
the full audit-phase observation, runs every rule-based audit in
``graders/auditor_track_a.py`` (rationale citation, calibration,
cross-ad consistency, bias, rationale↔verdict coherence) and
``graders/auditor_track_b.py`` (intrinsic, grounding, real-world,
signal-realism, novelty plausibility), queues one
``flag_investigator`` / ``flag_fraudster`` action per flag, and
submits an audit report containing the flag payloads and aggregated
audit scores.
Why deterministic
-----------------
The Auditor's flag stream is the **reward source** for both the
Investigator (Track A flags drive the rationale-quality penalty in
``multi_agent_rewards.investigator_reward``) and the Fraudster (Track B
plausibility drives the survival credit in
``multi_agent_rewards.fraudster_reward``). Keeping it deterministic
means the reward function is interpretable, inspectable, and free of
LLM noise / cost / latency. It also mirrors how real ad-policy review
teams operate: rule-based scorecards layered on top of model verdicts.
Future scope
------------
Replacing this with an LLM-backed Auditor is a clean drop-in: the
``run_full_audit`` orchestrator in
:mod:`counterfeint.graders.auditor_pipeline` returns a typed
``FullAuditResult`` that any LLM policy could synthesise in one shot.
That swap is **out of scope for the hackathon submission** — we keep
the deterministic path so the reward signal is reproducible.
"""
from __future__ import annotations
from typing import Any, Dict, List, Optional
from ..graders.auditor_pipeline import run_full_audit
from ..graders.base_grader import (
EpisodeRecord,
LinkResult,
VerdictResult,
)
from ..models import AuditorAction
from ._base import PolicyBase
class HeuristicAuditor(PolicyBase):
"""Scripted Auditor — runs rule-based graders and submits a final report."""
def __init__(self) -> None:
self._queued: List[AuditorAction] = []
self._submitted: bool = False
self._report: Optional[Dict[str, Any]] = None
def reset(self) -> None:
self._queued = []
self._submitted = False
self._report = None
def act(self, observation: Dict[str, Any]) -> AuditorAction:
if observation.get("phase") == "done" or self._submitted:
return AuditorAction(
action_type="submit_audit_report",
audit_report=self._report or {},
note="HeuristicAuditor: episode already done.",
)
if not self._queued:
self._queued, self._report = self._plan(observation)
if self._queued:
action = self._queued.pop(0)
if action.action_type == "submit_audit_report":
self._submitted = True
return action
self._submitted = True
return AuditorAction(
action_type="submit_audit_report",
audit_report=self._report or {},
note="HeuristicAuditor: queue empty, submitting final report.",
)
# ------------------------------------------------------------------
# Internal planning
# ------------------------------------------------------------------
def _plan(
self, observation: Dict[str, Any]
) -> tuple[List[AuditorAction], Dict[str, Any]]:
record = self._build_record(observation)
investigator_actions = observation.get("investigator_actions", []) or []
fraudster_proposals = observation.get("fraudster_proposals", []) or []
investigation_data_seen = observation.get("investigation_data_seen", {}) or {}
audit = run_full_audit(
record=record,
investigator_action_log=investigator_actions,
investigation_data_seen=investigation_data_seen,
fraudster_proposal_log=fraudster_proposals,
)
actions: List[AuditorAction] = []
for f in audit.track_b_flags:
actions.append(
AuditorAction(
action_type="flag_fraudster",
target_ad_id=f.target_ad_id,
flag_type=f.flag_type,
severity=f.severity,
note=f.note,
)
)
for f in audit.track_a_flags:
actions.append(
AuditorAction(
action_type="flag_investigator",
target_ad_id=f.target_ad_id,
flag_type=f.flag_type,
severity=f.severity,
note=f.note,
)
)
report: Dict[str, Any] = {
"track_a_flags": [f.model_dump() for f in audit.track_a_flags],
"track_b_flags": [f.model_dump() for f in audit.track_b_flags],
"investigator_audit_score": round(audit.investigator_audit_score, 4),
"fraudster_plausibility_score": round(audit.fraudster_plausibility_score, 4),
"per_ad_plausibility": {
k: round(v, 4) for k, v in audit.per_ad_plausibility.items()
},
"notes": (
f"HeuristicAuditor: {len(audit.track_a_flags)} Track A flag(s), "
f"{len(audit.track_b_flags)} Track B flag(s); "
f"inv_audit={audit.investigator_audit_score:.2f}, "
f"fraud_plaus={audit.fraudster_plausibility_score:.2f}."
),
}
actions.append(
AuditorAction(
action_type="submit_audit_report",
audit_report=report,
note="HeuristicAuditor: submitting derived report.",
)
)
return actions, report
# ------------------------------------------------------------------
# EpisodeRecord reconstruction from the auditor observation
# ------------------------------------------------------------------
def _build_record(self, observation: Dict[str, Any]) -> Optional[EpisodeRecord]:
record_payload = observation.get("full_episode_record") or {}
if not record_payload:
return None
verdict_entries = record_payload.get("verdicts") or []
link_entries = record_payload.get("links") or []
ad_entries = record_payload.get("ads") or []
verdicts: List[VerdictResult] = []
for v in verdict_entries:
if "verdict" not in v:
continue
verdicts.append(
VerdictResult(
ad_id=v.get("ad_id", ""),
verdict=v.get("verdict", "approve"),
confidence=float(v.get("confidence", 0.5) or 0.5),
ground_truth=v.get("ground_truth", "legit"),
auto_approved=bool(v.get("auto_approved", False)),
)
)
links: List[LinkResult] = [
LinkResult(
ad_id_1=l.get("ad_id_1", ""),
ad_id_2=l.get("ad_id_2", ""),
correct=bool(l.get("correct", False)),
)
for l in link_entries
if l.get("ad_id_1") and l.get("ad_id_2")
]
ads_metadata: List[Dict[str, Any]] = []
for ad in ad_entries:
if not ad.get("ad_id"):
continue
ads_metadata.append(
{
"ad_id": ad.get("ad_id"),
"ground_truth": ad.get("ground_truth", "legit"),
"severity": float(ad.get("severity", 0.5) or 0.5),
"fraud_type": ad.get("fraud_type", ""),
"category": ad.get("category", ""),
"country": ad.get("country", ""),
}
)
return EpisodeRecord(
task_id=record_payload.get("task_id", ""),
total_steps=int(record_payload.get("total_steps", 0) or 0),
action_budget=int(record_payload.get("action_budget", 0) or 0),
verdicts=verdicts,
links=links,
ads_metadata=ads_metadata,
)
__all__ = ["HeuristicAuditor"]
|