File size: 4,207 Bytes
ff293b1
 
 
 
 
 
 
 
 
ee21104
 
ff293b1
 
 
 
 
 
ee21104
 
 
 
 
 
 
ff293b1
 
 
 
ee21104
 
ff293b1
 
ee21104
 
 
 
 
 
 
 
 
ff293b1
ee21104
 
 
 
 
ff293b1
 
ee21104
 
 
 
 
ff293b1
ee21104
 
 
 
 
ff293b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Public trajectory graders for OpenEnv Phase 2 / HF deep validation.



These are **episode-level** scores (strictly inside (0, 1)), separate from per-step

rewards in `server/reward.py`. The hackathon validator reads `openenv.yaml`

`tasks[].grader` and calls these functions with trajectory dicts.

"""
from __future__ import annotations

import math
from typing import List

STRICT_MIN = 0.01
STRICT_MAX = 0.99


def _bounded(value: float) -> float:
    try:
        v = round(float(value), 4)
    except (TypeError, ValueError):
        return 0.5
    if not math.isfinite(v):
        return 0.5
    return min(max(v, STRICT_MIN), STRICT_MAX)


def _as_reward_list(trajectory: dict | None) -> List[float]:
    payload = trajectory or {}
    if not isinstance(payload, dict):
        return []
    rewards = payload.get("rewards")
    if isinstance(rewards, list) and rewards:
        out: List[float] = []
        for r in rewards:
            try:
                rv = float(r)
            except (TypeError, ValueError):
                continue
            if math.isfinite(rv):
                out.append(rv)
        return out
    if "score" in payload:
        try:
            v = float(payload["score"])
            return [v] if math.isfinite(v) else []
        except (TypeError, ValueError):
            return []
    reward = payload.get("reward")
    if isinstance(reward, dict) and "total" in reward:
        try:
            v = float(reward["total"])
            return [v] if math.isfinite(v) else []
        except (TypeError, ValueError):
            return []
    if reward is not None:
        try:
            v = float(reward)
            return [v] if math.isfinite(v) else []
        except (TypeError, ValueError):
            return []
    return []


def _profile(reward: float) -> str:
    if reward <= 0.05:
        return "unsafe_miss"
    if reward <= 0.20:
        return "bad_call"
    if reward < 0.50:
        return "weak"
    if reward < 0.80:
        return "workable"
    if reward < 0.95:
        return "strong"
    return "expert"


def _score_episode(

    rewards: List[float],

    *,

    miss_cost: float,

    overcall_cost: float,

    stability_gain: float,

    expertise_gain: float,

) -> float:
    if not rewards:
        return _bounded(0.5)
    labels = [_profile(r) for r in rewards]
    mean_r = sum(rewards) / len(rewards)
    n = len(rewards)
    miss = labels.count("unsafe_miss")
    bad = labels.count("bad_call")
    weak = labels.count("weak")
    strong = labels.count("strong") + labels.count("expert")
    expert = labels.count("expert")

    downward = (
        min(miss * miss_cost, 0.35)
        + min(bad * overcall_cost, 0.15)
        + min(weak * 0.015, 0.06)
    )
    upward = 0.0
    if strong / n >= 0.80:
        upward += stability_gain
    if expert / n >= 0.60:
        upward += expertise_gain

    return _bounded(mean_r - downward + upward)


def phase2_core_grader(trajectory: dict | None = None) -> float:
    """Easy tier — dense default inbox (scenarios/phase2_core.json)."""
    return _score_episode(
        _as_reward_list(trajectory),
        miss_cost=0.12,
        overcall_cost=0.03,
        stability_gain=0.05,
        expertise_gain=0.01,
    )


def monday_morning_grader(trajectory: dict | None = None) -> float:
    """Medium tier — stacked Monday conflicts (scenarios/monday_morning.json)."""
    return _score_episode(
        _as_reward_list(trajectory),
        miss_cost=0.09,
        overcall_cost=0.04,
        stability_gain=0.03,
        expertise_gain=0.02,
    )


def dinner_disaster_grader(trajectory: dict | None = None) -> float:
    """Hard tier — personal/professional collision (scenarios/dinner_disaster.json)."""
    return _score_episode(
        _as_reward_list(trajectory),
        miss_cost=0.07,
        overcall_cost=0.03,
        stability_gain=0.02,
        expertise_gain=0.04,
    )


__all__ = [
    "phase2_core_grader",
    "monday_morning_grader",
    "dinner_disaster_grader",
    "STRICT_MIN",
    "STRICT_MAX",
]