File size: 10,798 Bytes
61f1214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
AI Interview Simulator β€” OpenEnv Environment
============================================

Implements the full OpenEnv interface:
  reset()  β†’ InterviewObservation
  step()   β†’ StepResult(observation, reward, done, info)
  state()  β†’ InterviewState

The environment simulates a real job interview: the agent receives one
question at a time and must provide a text response. After every answer
a deterministic grader scores it (0.0–1.0) and issues partial reward.
The episode ends when all questions have been answered.

Usage:
    env = InterviewEnv(task_id="junior_behavioral")
    obs = env.reset()
    while True:
        action = InterviewAction(response="My answer …")
        result = env.step(action)
        if result.done:
            break
"""
from __future__ import annotations

import time
from typing import Any, Dict, Optional

from interview_env.graders import grade_response
from interview_env.models import (
    DifficultyLevel,
    InterviewAction,
    InterviewObservation,
    InterviewStage,
    InterviewState,
    QAPair,
    StepResult,
)
from interview_env.tasks import TASK_REGISTRY, TaskDefinition, get_task


class InterviewEnv:
    """
    OpenEnv-compliant AI Interview Simulator environment.

    Parameters
    ----------
    task_id : str
        One of: "junior_behavioral", "mid_technical", "senior_system_design"
    max_response_length : int
        Characters β€” responses exceeding this are truncated (soft limit).
    penalty_per_truncation : float
        Reward deducted when the agent's response is truncated.
    """

    VALID_TASKS = list(TASK_REGISTRY.keys())
    DEFAULT_TASK = "junior_behavioral"

    def __init__(
        self,
        task_id: str = DEFAULT_TASK,
        max_response_length: int = 2000,
        penalty_per_truncation: float = 0.05,
    ) -> None:
        if task_id not in TASK_REGISTRY:
            raise ValueError(
                f"Unknown task_id '{task_id}'. "
                f"Choose from: {self.VALID_TASKS}"
            )
        self._task_id              = task_id
        self._max_response_length  = max_response_length
        self._penalty_per_truncation = penalty_per_truncation

        # Internal state (initialised by reset)
        self._task: TaskDefinition        = get_task(task_id)
        self._current_index: int          = 0
        self._qa_history: list[QAPair]    = []
        self._per_q_scores: list[float]   = []
        self._done: bool                  = False
        self._total_steps: int            = 0
        self._start_time: float           = time.time()
        self._cumulative_reward: float    = 0.0

    # ──────────────────────────────────────────
    # OpenEnv Interface
    # ──────────────────────────────────────────

    def reset(self) -> InterviewObservation:
        """
        Reset the environment to the beginning of the interview.
        Returns the first question as an observation.
        """
        self._task            = get_task(self._task_id)
        self._current_index   = 0
        self._qa_history      = []
        self._per_q_scores    = []
        self._done            = False
        self._total_steps     = 0
        self._start_time      = time.time()
        self._cumulative_reward = 0.0

        return self._build_observation()

    def step(self, action: InterviewAction) -> StepResult:
        """
        Submit the agent's answer to the current interview question.

        Returns
        -------
        StepResult with:
          observation  β€” next question (or completion message if done)
          reward       β€” per-step score in [–0.05, 1.0]
          done         β€” True when all questions are answered
          info         β€” detailed grader breakdown
        """
        if self._done:
            # No-op after episode ends
            return StepResult(
                observation=self._build_observation(),
                reward=0.0,
                done=True,
                info={"warning": "Episode already complete. Call reset()."},
            )

        self._total_steps += 1
        current_q = self._task.questions[self._current_index]

        # Soft-truncate response and penalise
        response    = action.response
        truncated   = False
        reward_adj  = 0.0
        if len(response) > self._max_response_length:
            response  = response[: self._max_response_length]
            truncated = True
            reward_adj = -self._penalty_per_truncation

        # Grade the response
        reward_obj = grade_response(response, current_q)
        step_reward = reward_obj.total_score + reward_adj
        step_reward = max(0.0, min(1.0, step_reward))

        # Record in history
        qa = QAPair(
            question        = current_q.text,
            answer          = response,
            stage           = current_q.stage,
            question_score  = reward_obj.total_score,
        )
        self._qa_history.append(qa)
        self._per_q_scores.append(reward_obj.total_score)
        self._cumulative_reward += step_reward

        # Advance to next question
        self._current_index += 1
        if self._current_index >= len(self._task.questions):
            self._done = True

        obs  = self._build_observation()
        info: Dict[str, Any] = {
            "question_answered": current_q.text,
            "grader_breakdown": {
                "relevance":       reward_obj.relevance,
                "structure":       reward_obj.structure,
                "depth":           reward_obj.depth,
                "professionalism": reward_obj.professionalism,
                "total_score":     reward_obj.total_score,
            },
            "feedback":       reward_obj.feedback,
            "truncated":      truncated,
            "question_number": self._current_index,   # 1-indexed (just answered)
            "total_questions": len(self._task.questions),
        }

        if self._done:
            final_score = self._final_score()
            info["episode_summary"] = {
                "final_score":       final_score,
                "per_question_scores": self._per_q_scores,
                "passed":            final_score >= 0.60,
                "pass_threshold":    0.60,
            }

        return StepResult(
            observation = obs,
            reward      = round(step_reward, 4),
            done        = self._done,
            info        = info,
        )

    def state(self) -> InterviewState:
        """
        Return a serialisable snapshot of the full internal state.
        Useful for debugging, checkpointing, and OpenEnv validation.
        """
        return InterviewState(
            task_id          = self._task_id,
            difficulty       = self._task.difficulty,
            job_title        = self._task.job_title,
            company_context  = self._task.company_context,
            questions        = [q.text for q in self._task.questions],
            stages           = [q.stage for q in self._task.questions],
            current_index    = self._current_index,
            qa_history       = self._qa_history,
            per_question_scores = self._per_q_scores,
            episode_done     = self._done,
            total_steps      = self._total_steps,
            start_time       = self._start_time,
            cumulative_reward = self._cumulative_reward,
        )

    # ──────────────────────────────────────────
    # Properties
    # ──────────────────────────────────────────

    @property
    def task_id(self) -> str:
        return self._task_id

    @property
    def available_tasks(self) -> list[str]:
        return self.VALID_TASKS

    @property
    def action_space_description(self) -> str:
        return (
            "InterviewAction(response: str) β€” "
            "A free-text string (1–2000 chars) representing the candidate's spoken answer."
        )

    @property
    def observation_space_description(self) -> str:
        return (
            "InterviewObservation with fields: "
            "task_id, job_title, company_context, difficulty, "
            "current_question, question_number, total_questions, stage, "
            "previous_qa (list of QAPair), cumulative_score, time_elapsed, interview_complete."
        )

    # ──────────────────────────────────────────
    # Private helpers
    # ──────────────────────────────────────────

    def _build_observation(self) -> InterviewObservation:
        if self._done or self._current_index >= len(self._task.questions):
            current_question   = (
                "Thank you for completing the interview. "
                "We will be in touch shortly."
            )
            stage              = InterviewStage.COMPLETE
            question_number    = len(self._task.questions)
            interview_complete = True
        else:
            q                  = self._task.questions[self._current_index]
            current_question   = q.text
            stage              = q.stage
            question_number    = self._current_index + 1
            interview_complete = False

        return InterviewObservation(
            task_id            = self._task_id,
            job_title          = self._task.job_title,
            company_context    = self._task.company_context,
            difficulty         = self._task.difficulty,
            current_question   = current_question,
            question_number    = question_number,
            total_questions    = len(self._task.questions),
            stage              = stage,
            previous_qa        = list(self._qa_history),
            cumulative_score   = round(self._cumulative_reward, 4),
            time_elapsed       = round(time.time() - self._start_time, 2),
            interview_complete = interview_complete,
        )

    def _final_score(self) -> float:
        """Weighted average over all per-question scores."""
        if not self._per_q_scores:
            return 0.0
        questions = self._task.questions
        weighted  = sum(
            s * q.weight
            for s, q in zip(self._per_q_scores, questions)
        )
        total_w = sum(q.weight for q in questions)
        return round(weighted / total_w, 4) if total_w > 0 else 0.0