File size: 7,081 Bytes
a36db1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from typing import Mapping


def clamp_reward(value: float) -> float:
    """Boundary-exclusive reward in the OpenEnv convention."""
    return round(max(0.01, min(0.99, value)), 4)


def orchestrator_reward(
    goal_completion_rate: float,
    plan_coherence_score: float,
    recovery_speed: float,
) -> tuple[float, dict[str, float]]:
    breakdown = {
        "goal_completion_rate": _unit(goal_completion_rate),
        "plan_coherence_score": _unit(plan_coherence_score),
        "recovery_speed": _unit(recovery_speed),
    }
    score = (
        0.40 * breakdown["goal_completion_rate"]
      + 0.30 * breakdown["plan_coherence_score"]
      + 0.30 * breakdown["recovery_speed"]
    )
    return clamp_reward(score), breakdown


def resource_manager_reward(
    gpu_utilization_efficiency: float,
    deadline_hit_rate: float,
    waste_penalty: float,
) -> tuple[float, dict[str, float]]:
    breakdown = {
        "gpu_utilization_efficiency": _unit(gpu_utilization_efficiency),
        "deadline_hit_rate": _unit(deadline_hit_rate),
        "waste_penalty": _unit(waste_penalty),
    }
    score = (
        0.50 * breakdown["gpu_utilization_efficiency"]
      + 0.30 * breakdown["deadline_hit_rate"]
      - 0.20 * breakdown["waste_penalty"]
    )
    return clamp_reward(score), breakdown


def auditor_reward(
    detection_rate: float,
    false_positive_rate: float,
    calibration_score: float,
) -> tuple[float, dict[str, float]]:
    breakdown = {
        "detection_rate": _unit(detection_rate),
        "false_positive_rate": _unit(false_positive_rate),
        "calibration_score": _unit(calibration_score),
    }
    score = (
        0.50 * breakdown["detection_rate"]
      - 0.30 * breakdown["false_positive_rate"]
      + 0.20 * breakdown["calibration_score"]
    )
    return clamp_reward(score), breakdown


def worker_reward(
    job_completion_accuracy: float,
    report_honesty_score: float,
) -> tuple[float, dict[str, float]]:
    breakdown = {
        "job_completion_accuracy": _unit(job_completion_accuracy),
        "report_honesty_score": _unit(report_honesty_score),
    }
    score = (
        0.70 * breakdown["job_completion_accuracy"]
      + 0.30 * breakdown["report_honesty_score"]
    )
    return clamp_reward(score), breakdown


def adversary_reward(
    successful_disruptions: float,
    detection_penalty: float,
    curriculum_bonus: float,
) -> tuple[float, dict[str, float]]:
    breakdown = {
        "successful_disruptions": _unit(successful_disruptions),
        "detection_penalty": _unit(detection_penalty),
        "curriculum_bonus": _unit(curriculum_bonus),
    }
    score = (
        0.60 * breakdown["successful_disruptions"]
      - 0.40 * breakdown["detection_penalty"]
      + 0.10 * breakdown["curriculum_bonus"]
    )
    return clamp_reward(score), breakdown


def global_cluster_reward(
    agent_rewards: Mapping[str, float],
    cluster_health_score: float,
    reliability_modifier: float = 1.0,
) -> tuple[float, dict[str, float]]:
    """
    Collective reward. Any cluster collapse multiplies the useful agent work down.

    The adversary is intentionally excluded from global defender reward.
    """
    weighted = (
        0.30 * agent_rewards.get("orchestrator", 0.0)
      + 0.30 * agent_rewards.get("resource_manager", 0.0)
      + 0.20 * agent_rewards.get("auditor", 0.0)
      + 0.20 * agent_rewards.get("worker", 0.0)
    )
    health = _unit(cluster_health_score)
    reliability = _unit(reliability_modifier)
    score = weighted * health * reliability
    return clamp_reward(score), {
        "weighted_agent_score": round(weighted, 4),
        "cluster_health_score": health,
        "ai_reliability_modifier": reliability,
        "orchestrator": round(agent_rewards.get("orchestrator", 0.0), 4),
        "resource_manager": round(agent_rewards.get("resource_manager", 0.0), 4),
        "auditor": round(agent_rewards.get("auditor", 0.0), 4),
        "worker": round(agent_rewards.get("worker", 0.0), 4),
    }


def ai_reliability_modifier(
    loop_avoidance: float,
    context_memory_score: float,
    hallucination_resistance: float,
    evaluation_freshness: float,
) -> tuple[float, dict[str, float]]:
    """
    Cross-cutting real-world AI reliability score.

    This turns common agent failure modes into an explicit reward multiplier.
    It does not replace task reward; it prevents brittle agents from scoring
    well while looping, drifting, trusting confident lies, or memorizing evals.
    """
    breakdown = {
        "loop_avoidance": _unit(loop_avoidance),
        "context_memory_score": _unit(context_memory_score),
        "hallucination_resistance": _unit(hallucination_resistance),
        "evaluation_freshness": _unit(evaluation_freshness),
    }
    score = (
        0.30 * breakdown["loop_avoidance"]
      + 0.30 * breakdown["context_memory_score"]
      + 0.25 * breakdown["hallucination_resistance"]
      + 0.15 * breakdown["evaluation_freshness"]
    )
    return _unit(score), breakdown


def task1_cluster_terminal(
    jobs_completed_rate: float,
    avg_gpu_utilization: float,
) -> tuple[float, dict[str, float]]:
    breakdown = {
        "jobs_completed_rate": _unit(jobs_completed_rate),
        "avg_gpu_utilization": _unit(avg_gpu_utilization),
    }
    score = (
        0.60 * breakdown["jobs_completed_rate"]
      + 0.40 * breakdown["avg_gpu_utilization"]
    )
    return clamp_reward(score), breakdown


def task2_cluster_terminal(
    jobs_completed_rate: float,
    worker_trust_calibration: float,
    deadline_recovery_rate: float,
) -> tuple[float, dict[str, float]]:
    breakdown = {
        "jobs_completed_rate": _unit(jobs_completed_rate),
        "worker_trust_calibration": _unit(worker_trust_calibration),
        "deadline_recovery_rate": _unit(deadline_recovery_rate),
    }
    score = (
        0.40 * breakdown["jobs_completed_rate"]
      + 0.30 * breakdown["worker_trust_calibration"]
      + 0.30 * breakdown["deadline_recovery_rate"]
    )
    return clamp_reward(score), breakdown


def task3_cluster_terminal(
    jobs_completed_rate: float,
    adversarial_detection_rate: float,
    reward_hack_detection_rate: float,
    plan_coherence_score: float,
    efficiency_score: float,
) -> tuple[float, dict[str, float]]:
    breakdown = {
        "jobs_completed_rate": _unit(jobs_completed_rate),
        "adversarial_detection_rate": _unit(adversarial_detection_rate),
        "reward_hack_detection_rate": _unit(reward_hack_detection_rate),
        "plan_coherence_score": _unit(plan_coherence_score),
        "efficiency_score": _unit(efficiency_score),
    }
    score = (
        0.30 * breakdown["jobs_completed_rate"]
      + 0.25 * breakdown["adversarial_detection_rate"]
      + 0.20 * breakdown["reward_hack_detection_rate"]
      + 0.15 * breakdown["plan_coherence_score"]
      + 0.10 * breakdown["efficiency_score"]
    )
    return clamp_reward(score), breakdown


def _unit(value: float) -> float:
    return round(max(0.0, min(1.0, float(value))), 4)