Spaces:
Configuration error

File size: 9,180 Bytes
f898792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Core OpenEnv environment for Financial Statement Anomaly Detection.
Implements step(), reset(), state() per the OpenEnv specification.
"""

from __future__ import annotations

import copy
import json
import random
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple

from .anomaly_injector import inject_anomalies
from .generator import generate_company_data
from .grader import grade
from .models import (
    Action,
    ActionType,
    AnomalyFlag,
    CompanyMetadata,
    GroundTruthAnomaly,
    Info,
    Observation,
    QuarterData,
    Reward,
    State,
)


TASKS_DIR = Path(__file__).parent.parent / "tasks"


class FinancialAnomalyEnv:
    """
    Financial Statement Anomaly Detection environment.

    The agent receives quarterly financial statements and must identify
    injected anomalies by flagging them with type, location, severity,
    and explanation.
    """

    def __init__(self, task_id: str = "easy", max_steps: int = 20):
        self.task_id = task_id
        self.max_steps = max_steps

        # Episode state
        self._metadata: Optional[CompanyMetadata] = None
        self._quarters: List[QuarterData] = []
        self._ground_truth: List[GroundTruthAnomaly] = []
        self._flags: List[AnomalyFlag] = []
        self._step_number: int = 0
        self._cumulative_reward: float = 0.0
        self._done: bool = False
        self._task_config: Dict[str, Any] = {}

        # Load task config
        self._load_task(task_id)

    def _load_task(self, task_id: str) -> None:
        """Load task configuration from JSON file."""
        task_file = TASKS_DIR / f"task_{task_id}.json"
        if task_file.exists():
            with open(task_file, "r") as f:
                self._task_config = json.load(f)
        else:
            # Default configs
            defaults = {
                "easy": {"seed": 42, "num_quarters": 4, "difficulty": "easy"},
                "medium": {"seed": 137, "num_quarters": 6, "difficulty": "medium"},
                "hard": {"seed": 256, "num_quarters": 8, "difficulty": "hard"},
            }
            self._task_config = defaults.get(task_id, defaults["easy"])

    def reset(self) -> Observation:
        """
        Generate a fresh company with anomalies and return the initial observation.
        Clears all episode state.
        """
        seed = self._task_config.get("seed", 42)
        num_q = self._task_config.get("num_quarters", 6)
        difficulty = self._task_config.get("difficulty", "easy")

        # Generate clean data
        self._metadata, clean_quarters = generate_company_data(
            seed=seed,
            num_quarters=num_q,
        )

        # Inject anomalies
        self._quarters, self._ground_truth = inject_anomalies(
            quarters=clean_quarters,
            difficulty=difficulty,
            seed=seed + 1000,  # separate seed for injection
        )

        # Reset episode state
        self._flags = []
        self._step_number = 0
        self._cumulative_reward = 0.0
        self._done = False

        return self._build_observation()

    def step(self, action: Action) -> Tuple[Observation, Reward, bool, Info]:
        """
        Process one agent action.

        Args:
            action: The agent's action (flag_anomaly, request_detail, or submit_report).

        Returns:
            Tuple of (observation, reward, done, info).
        """
        if self._done:
            return (
                self._build_observation(),
                Reward(step_reward=0.0, cumulative_reward=self._cumulative_reward),
                True,
                Info(message="Episode already finished.", done_reason="already_done"),
            )

        self._step_number += 1
        step_reward = 0.0
        info_msg = ""
        done_reason = None
        detail_response = None

        if action.action_type == ActionType.FLAG_ANOMALY and action.flag:
            flag = action.flag
            self._flags.append(flag)

            # Compute immediate reward
            matched = False
            matched_gt = None
            for gt in self._ground_truth:
                loc_match = (
                    flag.line_item == gt.line_item
                    and (flag.quarter == gt.quarter or gt.quarter == "ALL")
                )
                if loc_match:
                    if flag.anomaly_type.value == gt.anomaly_type.value:
                        step_reward = 1.0
                        matched = True
                        matched_gt = gt
                    else:
                        step_reward = 0.5
                        matched = True
                        matched_gt = gt
                    break

            if not matched:
                step_reward = -0.3  # false positive

            info_msg = f"Flag recorded: {flag.line_item} @ {flag.quarter}"

        elif action.action_type == ActionType.REQUEST_DETAIL:
            step_reward = -0.1  # exploration cost
            # Return a synthetic sub-ledger breakdown
            if action.detail_line_item and action.detail_quarter:
                detail_response = self._generate_detail(
                    action.detail_line_item, action.detail_quarter
                )
                info_msg = f"Detail returned for {action.detail_line_item} @ {action.detail_quarter}"
            else:
                info_msg = "request_detail requires detail_line_item and detail_quarter"

        elif action.action_type == ActionType.SUBMIT_REPORT:
            self._done = True
            done_reason = "submitted"
            # Final grading
            grade_result = grade(self._flags, self._ground_truth)
            step_reward = 0.0
            info_msg = f"Report submitted. Final score: {grade_result['score']}"

        # Check max steps
        if self._step_number >= self.max_steps and not self._done:
            self._done = True
            done_reason = "max_steps"
            step_reward -= 0.5
            info_msg = "Max steps reached. Episode ended with penalty."

        self._cumulative_reward += step_reward

        obs = self._build_observation(detail_response=detail_response)
        reward = Reward(
            step_reward=round(step_reward, 4),
            cumulative_reward=round(self._cumulative_reward, 4),
            breakdown={"step": round(step_reward, 4)},
        )
        info = Info(
            message=info_msg,
            flags_so_far=len(self._flags),
            steps_remaining=self.max_steps - self._step_number,
            done_reason=done_reason,
        )

        return obs, reward, self._done, info

    def state(self) -> State:
        """Return the current internal state of the episode."""
        return State(
            step_number=self._step_number,
            max_steps=self.max_steps,
            flags_submitted=list(self._flags),
            cumulative_reward=round(self._cumulative_reward, 4),
            done=self._done,
            task_id=self.task_id,
        )

    def get_final_score(self) -> Dict:
        """Run the grader on all submitted flags and return the full result."""
        return grade(self._flags, self._ground_truth)

    def get_ground_truth(self) -> List[GroundTruthAnomaly]:
        """Return ground truth anomalies (for testing/debugging only)."""
        return self._ground_truth

    def _build_observation(
        self, detail_response: Optional[Dict] = None
    ) -> Observation:
        """Build the current observation for the agent."""
        return Observation(
            company=self._metadata,
            quarters=self._quarters,
            footnotes=self._task_config.get("footnotes", []),
            prior_flags=list(self._flags),
            detail_response=detail_response,
            step_number=self._step_number,
            max_steps=self.max_steps,
        )

    def _generate_detail(self, line_item: str, quarter: str) -> Dict:
        """Generate a synthetic sub-ledger breakdown for a line item."""
        rng = random.Random(hash(f"{line_item}_{quarter}"))
        target_q = None
        for q in self._quarters:
            if q.quarter_label == quarter:
                target_q = q
                break
        if target_q is None:
            return {"error": f"Quarter {quarter} not found"}

        total = getattr(target_q, line_item, None)
        if total is None:
            return {"error": f"Line item {line_item} not found"}

        # Generate 3-5 sub-components
        n_subs = rng.randint(3, 5)
        sub_labels = [f"{line_item}_component_{i+1}" for i in range(n_subs)]
        weights = [rng.random() for _ in range(n_subs)]
        weight_sum = sum(weights)
        components = {}
        running = 0
        for i, label in enumerate(sub_labels):
            if i == n_subs - 1:
                components[label] = total - running
            else:
                val = int(total * weights[i] / weight_sum)
                components[label] = val
                running += val

        return {
            "line_item": line_item,
            "quarter": quarter,
            "total": total,
            "components": components,
        }