#!/usr/bin/env python3 """R_B: the FROZEN, pre-registered baseline tier adapter. The fixed-surface baseline (upgrade-risk-be authority-graph CLI) does not emit a native T1/T2/T3. R_B deterministically derives an `R_B-derived upgrade-path tier` (never called a native baseline tier) from the baseline's OWN emitted signals only: controlModel, graph (nodes/edges), effectiveDelay, and resolutions.upgrade_authority. It is aligned with control-plane-resolution/classify.py tier definitions. Frozen: this file must NOT be modified after ground truth is created. It is the exact logic that produced sealed/baseline_normalized.json (verified by reproduce_check()). Mechanism-based, not tuned to outcomes. """ import json, os ACTOR = {'EOA', 'GNOSIS_SAFE', 'TIMELOCK', 'GOVERNOR'} OPAQUE_T = {'CONTRACT', 'ROLE', 'PROXY_ADMIN'} def r_b(run): """Return the normalized baseline prediction dict for one baseline raw run record.""" r = {"protocol_slug": run['protocol_slug'], "chain": run['chain'], "address": run['address'], "raw_output_ref": "sealed/baseline_raw.json#" + run['protocol_slug'] + "__" + run['chain']} if not run.get('ok'): r.update(dependency_status="INFRASTRUCTURE_ERROR", predicted_dependency=None, terminal_status="INFRASTRUCTURE_ERROR", predicted_terminal=None, predicted_terminal_type=None, predicted_upgrade_path_tier=None, authority_opaque=False, abstention=True, abstention_reason=run.get('exec_error'), confidence=None, control_model=None, status="INFRASTRUCTURE_ERROR") return r res = run['result'] or {} cm = res.get('controlModel') g = res.get('graph') or {} nodes = {n['id']: n for n in g.get('nodes', [])} ed = res.get('effectiveDelay') or {} path = ed.get('path') or [] d = ed.get('totalDelaySec') or 0 ptypes = [nodes.get(a, {}).get('nodeType') for a in path] term_addr = path[-1] if path else None term_type = ptypes[-1] if ptypes else None has_gov = 'GOVERNOR' in ptypes has_tl = ('TIMELOCK' in ptypes) or (d and d > 0) ua = (res.get('resolutions') or {}).get('upgrade_authority') or {} ua_term = ua.get('terminal') conf = ed.get('confidence') or ua.get('confidence') if cm in ('IMMUTABLE_BY_RENUNCIATION', 'IMMUTABLE_NO_GOVERNANCE_FOUND') or ua_term == 'RENOUNCED': tier = 'IMMUTABLE'; status = 'RESOLVED'; dep = 'RESOLVED'; tstat = 'RESOLVED'; opaque = False; abst = False; areason = None else: # UPGRADEABLE_PROXY or IMMUTABLE_CORE_WITH_GOVERNANCE -> classify by fastest-path terminal if has_gov: tier = 'T3'; status = 'RESOLVED'; dep = 'RESOLVED'; tstat = 'RESOLVED'; opaque = False; abst = False; areason = None elif has_tl: tier = 'T2'; status = 'RESOLVED'; dep = 'RESOLVED'; tstat = 'RESOLVED'; opaque = False; abst = False; areason = None elif term_type in ('EOA', 'GNOSIS_SAFE') or ua_term in ('EOA', 'MULTISIG'): tier = 'T1'; status = 'RESOLVED'; dep = 'RESOLVED'; tstat = 'RESOLVED'; opaque = False; abst = False; areason = None elif (term_type in OPAQUE_T) or ua_term == 'CONTRACT_OTHER': tier = None; status = 'AUTHORITY_OPAQUE'; dep = 'RESOLVED'; tstat = 'PARTIAL'; opaque = True; abst = False areason = 'authority resolved to opaque contract/role; holder not enumerable' else: tier = None; status = 'ABSTAIN'; dep = 'ABSTAIN'; tstat = 'ABSTAIN'; opaque = False; abst = True areason = (ua.get('reason') or 'no authority path resolved') r.update(dependency_status=dep, predicted_dependency=ua.get('source'), terminal_status=tstat, predicted_terminal=term_addr, predicted_terminal_type=(term_type or ua_term), predicted_upgrade_path_tier=tier, authority_opaque=opaque, abstention=abst, abstention_reason=areason, confidence=conf, control_model=cm, effective_delay_sec=(d or None), status=status) return r def reproduce_check(hold): """Verify R_B reproduces the sealed baseline_normalized.json exactly.""" bl = json.load(open(hold + "/sealed/baseline_raw.json"))['runs'] got = [r_b(x) for x in bl] got.sort(key=lambda r: (r['protocol_slug'], r['chain'])) sealed = json.load(open(hold + "/sealed/baseline_normalized.json"))['predictions'] return got == sealed, len(got) if __name__ == '__main__': hold = os.path.dirname(os.path.abspath(__file__)) ok, n = reproduce_check(hold) print("R_B reproduces sealed baseline_normalized.json:", ok, "| n=", n)