File size: 11,643 Bytes
225e725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""Subtext Arena environment.

Episode flow:
  1. reset() picks a random MUStARD clip (Pivot Set oversampled 3x).
     Returns: clip_id + speaker + duration, no transcript yet — the agent
     must call get_transcript and/or audio tools to investigate.
  2. step(SubtextArenaAction) executes one tool call:
       - get_transcript            -> literal text + conversational context
       - get_prosody_features      -> pitch, energy, pause text summary
       - get_pitch_contour         -> ASCII contour
       - submit_belief             -> terminates episode with label + confidence
     Reward = per-step delta (small + for tool use, penalties for malformed
     actions) + final composite reward when submit_belief fires.
  3. After max_steps (default 6) without a submission, the episode is force-
     terminated with the no_submission penalty.

The trained policy is a TEXT LLM (Path A). Audio is processed by the env's
frozen prosody-feature pipeline; the agent only ever sees text. Audio is
load-bearing because the Pivot Set explicitly contains clips where the literal
transcript alone leads to the wrong answer — the agent must consult prosody
to score on those.
"""
from __future__ import annotations

import os
import random
from typing import Optional
from uuid import uuid4

from openenv.core.env_server.interfaces import Environment
from openenv.core.env_server.types import State

try:
    from ..models import SubtextArenaAction, SubtextArenaObservation
except ImportError:
    from models import SubtextArenaAction, SubtextArenaObservation  # type: ignore[no-redef]

try:
    from .scenarios import load_scenarios, sample_clip
    from .audio_tools import (
        render_transcript,
        render_prosody_features,
        render_pitch_contour,
    )
    from .grader import step_reward, final_reward
except ImportError:
    from server.scenarios import load_scenarios, sample_clip  # type: ignore[no-redef]
    from server.audio_tools import (  # type: ignore[no-redef]
        render_transcript,
        render_prosody_features,
        render_pitch_contour,
    )
    from server.grader import step_reward, final_reward  # type: ignore[no-redef]


VALID_TOOLS = {
    "get_transcript",
    "get_prosody_features",
    "get_pitch_contour",
    "submit_belief",
}
AUDIO_TOOLS = {"get_prosody_features", "get_pitch_contour"}


class SubtextArenaEnvironment(Environment):
    """OpenEnv environment for sarcasm-vs-sincere classification on MUStARD."""

    SUPPORTS_CONCURRENT_SESSIONS: bool = True

    def __init__(self, max_steps: int = 6, seed: Optional[int] = None):
        self._scenarios = load_scenarios()
        self._max_steps = max_steps
        self._rng = random.Random(seed if seed is not None else os.urandom(4))

        self._state = State(episode_id=str(uuid4()), step_count=0)
        self._current_clip_id: Optional[str] = None
        self._n_audio_calls = 0
        self._n_total_calls = 0
        self._terminated = False
        # When set (via FORCE_CLIP_ID env var), reset() picks this clip instead
        # of sampling. Used by eval_pivot_set.py to walk specific clips.
        self._force_next_clip_id: Optional[str] = None

    def force_next_reset(self, clip_id: str) -> None:
        """Force the next reset() to pick the given clip ID.

        Called by eval scripts that need to evaluate on specific clips
        (e.g. all 50 Prosody-Pivot clips) rather than random sampling.
        Auto-clears after one reset.
        """
        if clip_id not in self._scenarios:
            raise ValueError(
                f"Unknown clip_id {clip_id!r}; not in MUStARD scenarios."
            )
        self._force_next_clip_id = clip_id

    # ------------------------------------------------------------------
    # Reset
    # ------------------------------------------------------------------
    def reset(self) -> SubtextArenaObservation:
        if self._force_next_clip_id is not None:
            clip_id = self._force_next_clip_id
            self._force_next_clip_id = None
        else:
            # Honor FORCE_CLIP_ID env var as a fallback (works through HTTP too)
            forced = os.environ.get("FORCE_CLIP_ID", "").strip()
            if forced and forced in self._scenarios:
                clip_id = forced
            else:
                clip_id = sample_clip(self._scenarios, self._rng)
        clip = self._scenarios[clip_id]
        prosody = clip.get("prosody") or {}

        self._state = State(episode_id=str(uuid4()), step_count=0)
        self._current_clip_id = clip_id
        self._n_audio_calls = 0
        self._n_total_calls = 0
        self._terminated = False

        return SubtextArenaObservation(
            clip_id=clip_id,
            speaker=clip.get("speaker", ""),
            duration_s=float(prosody.get("duration_s", 0.0)),
            is_pivot=bool(clip.get("is_pivot", False)),
            tool_used="reset",
            tool_output=(
                f"Episode started. Clip {clip_id}, speaker {clip.get('speaker', '?')}, "
                f"duration {prosody.get('duration_s', 0.0):.2f}s. "
                f"You have {self._max_steps} tool calls before forced submission. "
                f"Available tools: get_transcript, get_prosody_features, get_pitch_contour, submit_belief."
            ),
            step=0,
            max_steps=self._max_steps,
            audio_calls_so_far=0,
            done=False,
            reward=0.0,
        )

    # ------------------------------------------------------------------
    # Step
    # ------------------------------------------------------------------
    def step(self, action: SubtextArenaAction) -> SubtextArenaObservation:  # type: ignore[override]
        if self._current_clip_id is None or self._terminated:
            # Episode ended — return done=True with no reward
            return SubtextArenaObservation(
                clip_id=self._current_clip_id or "",
                tool_used="",
                tool_output="Episode terminated. Call reset() to start a new episode.",
                step=self._state.step_count,
                max_steps=self._max_steps,
                audio_calls_so_far=self._n_audio_calls,
                done=True,
                reward=0.0,
                error="episode_terminated",
            )

        clip = self._scenarios[self._current_clip_id]
        prosody = clip.get("prosody") or {}

        self._state.step_count += 1
        self._n_total_calls += 1

        tool = (action.tool or "").strip()
        args = action.tool_args or {}
        error: Optional[str] = None
        tool_output: str = ""

        if tool not in VALID_TOOLS:
            error = f"unknown tool '{tool}'. Valid: {sorted(VALID_TOOLS)}"
            tool_output = f"[error] {error}"
            reward = step_reward(tool, error)
            return SubtextArenaObservation(
                clip_id=self._current_clip_id,
                speaker=clip.get("speaker", ""),
                duration_s=float(prosody.get("duration_s", 0.0)),
                is_pivot=bool(clip.get("is_pivot", False)),
                tool_used=tool,
                tool_output=tool_output,
                step=self._state.step_count,
                max_steps=self._max_steps,
                audio_calls_so_far=self._n_audio_calls,
                done=False,
                reward=reward,
                error=error,
            )

        if tool == "get_transcript":
            tool_output = render_transcript(self._current_clip_id, self._scenarios)
        elif tool == "get_prosody_features":
            tool_output = render_prosody_features(self._current_clip_id, prosody, args)
            self._n_audio_calls += 1
        elif tool == "get_pitch_contour":
            tool_output = render_pitch_contour(self._current_clip_id, prosody, args)
            self._n_audio_calls += 1
        elif tool == "submit_belief":
            return self._submit_and_terminate(args, clip, prosody)

        # Per-step delta for non-terminal actions
        per_step = step_reward(tool, error)
        forced_terminate = self._state.step_count >= self._max_steps
        if forced_terminate:
            # Force a submission with no label -> apply no_submission penalty
            return self._submit_and_terminate(
                {"label": None, "confidence": 0.0},
                clip,
                prosody,
                forced=True,
                preceding_reward=per_step,
            )

        return SubtextArenaObservation(
            clip_id=self._current_clip_id,
            speaker=clip.get("speaker", ""),
            duration_s=float(prosody.get("duration_s", 0.0)),
            is_pivot=bool(clip.get("is_pivot", False)),
            tool_used=tool,
            tool_output=tool_output,
            step=self._state.step_count,
            max_steps=self._max_steps,
            audio_calls_so_far=self._n_audio_calls,
            done=False,
            reward=per_step,
            error=error,
        )

    def _submit_and_terminate(
        self,
        args: dict,
        clip: dict,
        prosody: dict,
        forced: bool = False,
        preceding_reward: float = 0.0,
    ) -> SubtextArenaObservation:
        label = args.get("label")
        if isinstance(label, str):
            label = label.strip().lower()
            if label not in {"sarcastic", "sincere"}:
                label = None
        else:
            label = None
        confidence = float(args.get("confidence", 0.5) or 0.5)

        gold = "sarcastic" if clip.get("sarcasm") else "sincere"
        components = final_reward(
            submitted_label=label,
            submitted_confidence=confidence,
            gold_label=gold,
            is_pivot=bool(clip.get("is_pivot", False)),
            n_audio_calls=self._n_audio_calls,
            n_total_calls=self._n_total_calls,
        )
        total_reward = components["_total"] + preceding_reward
        if forced:
            tool_output = (
                f"[forced termination after {self._max_steps} steps without submit_belief]\n"
                f"Gold label: {gold}. Reward components: {components}"
            )
        else:
            verdict = "CORRECT" if (label == gold) else "WRONG"
            tool_output = (
                f"Submitted: label={label}, confidence={confidence:.2f}. "
                f"Gold: {gold}. {verdict}. Reward components: {components}"
            )

        self._terminated = True
        return SubtextArenaObservation(
            clip_id=self._current_clip_id or "",
            speaker=clip.get("speaker", ""),
            duration_s=float(prosody.get("duration_s", 0.0)),
            is_pivot=bool(clip.get("is_pivot", False)),
            tool_used="submit_belief",
            tool_output=tool_output,
            step=self._state.step_count,
            max_steps=self._max_steps,
            audio_calls_so_far=self._n_audio_calls,
            done=True,
            reward=round(total_reward, 4),
            metadata={
                "gold": gold,
                "submitted_label": label,
                "submitted_confidence": confidence,
                "n_audio_calls": self._n_audio_calls,
                "n_total_calls": self._n_total_calls,
                "is_pivot": bool(clip.get("is_pivot", False)),
                "reward_components": components,
            },
        )

    # ------------------------------------------------------------------
    # State
    # ------------------------------------------------------------------
    @property
    def state(self) -> State:
        return self._state