Spaces:
Sleeping
Sleeping
fix: add Pydantic field validators to clamp ALL numeric fields in Observation/State to (0,1) — nuclear approach to score validation
Browse files
models.py
CHANGED
|
@@ -3,7 +3,28 @@ from typing import Any, Dict, List, Literal, Optional
|
|
| 3 |
from openenv.core.env_server.types import Action as OpenEnvAction
|
| 4 |
from openenv.core.env_server.types import Observation as OpenEnvObservation
|
| 5 |
from openenv.core.env_server.types import State as OpenEnvState
|
| 6 |
-
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
class PolicyViolation(BaseModel):
|
|
@@ -29,7 +50,7 @@ class Observation(OpenEnvObservation):
|
|
| 29 |
task_id: str = ""
|
| 30 |
task_name: str = ""
|
| 31 |
step: int = 0
|
| 32 |
-
last_reward: float =
|
| 33 |
feedback: Optional[str] = None
|
| 34 |
rules_to_check: List[str] = Field(default_factory=list)
|
| 35 |
reward: Optional[float] = None
|
|
@@ -37,6 +58,28 @@ class Observation(OpenEnvObservation):
|
|
| 37 |
user_can_submit: bool = Field(default=True, description="Whether user can submit custom contracts")
|
| 38 |
partial_progress: Dict[str, float] = Field(default_factory=dict, description="Partial progress per rule category")
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
class State(OpenEnvState):
|
| 42 |
"""Full internal state (not fully exposed to agent)."""
|
|
@@ -48,7 +91,21 @@ class State(OpenEnvState):
|
|
| 48 |
agent_violations: List[PolicyViolation] = Field(default_factory=list)
|
| 49 |
step: int = 0
|
| 50 |
max_steps: int = 5
|
| 51 |
-
cumulative_reward: float =
|
| 52 |
done: bool = False
|
| 53 |
difficulty: str = "easy" # easy | medium | hard
|
| 54 |
task_metrics: Dict[str, float] = Field(default_factory=dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from openenv.core.env_server.types import Action as OpenEnvAction
|
| 4 |
from openenv.core.env_server.types import Observation as OpenEnvObservation
|
| 5 |
from openenv.core.env_server.types import State as OpenEnvState
|
| 6 |
+
from pydantic import BaseModel, Field, field_validator, model_validator
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# ── Score safety ──────────────────────────────────────────────────────────────
|
| 10 |
+
# The Scaler validator rejects any value that is exactly 0.0 or 1.0.
|
| 11 |
+
# We clamp every numeric field that could plausibly be read as a "score".
|
| 12 |
+
_EPSILON = 0.01
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _clamp_score(v: float) -> float:
|
| 16 |
+
"""Clamp a float strictly within (0, 1). None passes through."""
|
| 17 |
+
if v <= 0.0:
|
| 18 |
+
return _EPSILON
|
| 19 |
+
if v >= 1.0:
|
| 20 |
+
return 1.0 - _EPSILON
|
| 21 |
+
return v
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _clamp_optional(v: Optional[float]) -> Optional[float]:
|
| 25 |
+
if v is None:
|
| 26 |
+
return None
|
| 27 |
+
return _clamp_score(v)
|
| 28 |
|
| 29 |
|
| 30 |
class PolicyViolation(BaseModel):
|
|
|
|
| 50 |
task_id: str = ""
|
| 51 |
task_name: str = ""
|
| 52 |
step: int = 0
|
| 53 |
+
last_reward: float = _EPSILON
|
| 54 |
feedback: Optional[str] = None
|
| 55 |
rules_to_check: List[str] = Field(default_factory=list)
|
| 56 |
reward: Optional[float] = None
|
|
|
|
| 58 |
user_can_submit: bool = Field(default=True, description="Whether user can submit custom contracts")
|
| 59 |
partial_progress: Dict[str, float] = Field(default_factory=dict, description="Partial progress per rule category")
|
| 60 |
|
| 61 |
+
# Clamp all float fields that could be misread as task scores
|
| 62 |
+
@field_validator("last_reward", mode="before")
|
| 63 |
+
@classmethod
|
| 64 |
+
def _clamp_last_reward(cls, v: Any) -> float:
|
| 65 |
+
if v is None:
|
| 66 |
+
return _EPSILON
|
| 67 |
+
return _clamp_score(float(v))
|
| 68 |
+
|
| 69 |
+
@field_validator("reward", mode="before")
|
| 70 |
+
@classmethod
|
| 71 |
+
def _clamp_reward(cls, v: Any) -> Optional[float]:
|
| 72 |
+
if v is None:
|
| 73 |
+
return None
|
| 74 |
+
return _clamp_score(float(v))
|
| 75 |
+
|
| 76 |
+
@field_validator("partial_progress", mode="before")
|
| 77 |
+
@classmethod
|
| 78 |
+
def _clamp_partial_progress(cls, v: Any) -> Dict[str, float]:
|
| 79 |
+
if not isinstance(v, dict):
|
| 80 |
+
return v
|
| 81 |
+
return {k: _clamp_score(float(val)) for k, val in v.items()}
|
| 82 |
+
|
| 83 |
|
| 84 |
class State(OpenEnvState):
|
| 85 |
"""Full internal state (not fully exposed to agent)."""
|
|
|
|
| 91 |
agent_violations: List[PolicyViolation] = Field(default_factory=list)
|
| 92 |
step: int = 0
|
| 93 |
max_steps: int = 5
|
| 94 |
+
cumulative_reward: float = _EPSILON
|
| 95 |
done: bool = False
|
| 96 |
difficulty: str = "easy" # easy | medium | hard
|
| 97 |
task_metrics: Dict[str, float] = Field(default_factory=dict)
|
| 98 |
+
|
| 99 |
+
@field_validator("cumulative_reward", mode="before")
|
| 100 |
+
@classmethod
|
| 101 |
+
def _clamp_cumulative(cls, v: Any) -> float:
|
| 102 |
+
if v is None:
|
| 103 |
+
return _EPSILON
|
| 104 |
+
return _clamp_score(float(v))
|
| 105 |
+
|
| 106 |
+
@field_validator("task_metrics", mode="before")
|
| 107 |
+
@classmethod
|
| 108 |
+
def _clamp_metrics(cls, v: Any) -> Dict[str, float]:
|
| 109 |
+
if not isinstance(v, dict):
|
| 110 |
+
return v
|
| 111 |
+
return {k: _clamp_score(float(val)) for k, val in v.items()}
|