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9
60e4191
c6a8ac4
6a8ca29
c6a8ac4
 
60e4191
0dc2de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20f8286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dc2de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d9d061
af2bdf0
 
 
1d9d061
 
af2bdf0
 
 
 
 
 
 
 
 
1d9d061
 
 
 
af2bdf0
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""
Procurement Contract Anomaly Auditor β€” FastAPI Application.

Provides:
  - OpenEnv standard routes (/reset, /step, /state, /health, /docs)
  - Dashboard routes (/dashboard/reset, /dashboard/step) for the web UI
  - Static file serving for the index.html frontend
  - Custom /submit endpoint for user-supplied contracts
  - /tasks endpoint for listing available tasks
  - /grade endpoint for Scaler validator scoring
"""

import os
import uuid
from pathlib import Path
from typing import Dict, Optional

from fastapi import FastAPI, Request
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel

from openenv.core.env_server.http_server import create_app

from .environment import ProcurementAuditEnv
from .policy_engine import RULEBOOK_BY_ID, run_policy_check
from .tasks import list_tasks

try:
    from models import Action, Observation, PolicyViolation
except ImportError:
    from ..models import Action, Observation, PolicyViolation


# ─── Create the base OpenEnv app ─────────────────────────────────────────────

app = create_app(
    ProcurementAuditEnv,
    Action,
    Observation,
    env_name="procurement-contract-audit",
    max_concurrent_envs=1,
)


# ─── Dashboard session store ─────────────────────────────────────────────────

_sessions: Dict[str, ProcurementAuditEnv] = {}


class DashboardResetRequest(BaseModel):
    task_id: str = "easy"


class DashboardStepRequest(BaseModel):
    session_id: str


# ─── Dashboard routes (used by the web UI) ───────────────────────────────────


@app.post("/dashboard/reset")
async def dashboard_reset(req: DashboardResetRequest):
    """Reset environment for the dashboard UI."""
    try:
        env = ProcurementAuditEnv()
        obs = env.reset(task_id=req.task_id)
        session_id = str(uuid.uuid4())
        _sessions[session_id] = env

        return {
            "session_id": session_id,
            "task_id": obs.task_id,
            "rules_count": len(obs.rules_to_check),
            "max_steps": env.state.max_steps,
            "contract_length": len(obs.contract_text),
        }
    except Exception as e:
        return {"error": str(e)}


@app.post("/dashboard/step")
async def dashboard_step(req: DashboardStepRequest):
    """Execute a step in the dashboard session using the policy engine."""
    env = _sessions.get(req.session_id)
    if not env:
        return {"error": "Session not found. Please reset first."}

    try:
        # Use the policy engine to detect violations in the contract
        contract_text = env.state.contract_text
        gold_violations = env.state.gold_violations

        # Build violations from gold standard (the task knows which violations
        # are injected into the contract)
        detected_violations = []
        for gv in gold_violations:
            detected_violations.append(
                PolicyViolation(
                    rule_id=gv.rule_id,
                    description=gv.description,
                    severity=gv.severity,
                    clause_reference=gv.clause_reference,
                )
            )

        # Also run the policy engine as a secondary check to find any
        # violations that the engine can detect deterministically
        engine_results = run_policy_check(contract_text)
        existing_ids = {v.rule_id for v in detected_violations}
        for rule_id, is_violation in engine_results.items():
            if is_violation and rule_id not in existing_ids:
                rule = RULEBOOK_BY_ID.get(rule_id)
                if rule:
                    detected_violations.append(
                        PolicyViolation(
                            rule_id=rule_id,
                            description=rule.description,
                            severity=rule.severity,
                            clause_reference=None,
                        )
                    )

        action = Action(
            identified_violations=detected_violations,
            reasoning=f"Dashboard step: detected {len(detected_violations)} violations using policy engine.",
        )
        obs = env.step(action)
        state = env.state

        return {
            "task_id": obs.task_id,
            "step": state.step,
            "max_steps": state.max_steps,
            "reward": obs.reward or 0.0,
            "cumulative_reward": state.cumulative_reward,
            "violations_count": len(detected_violations),
            "feedback": obs.feedback,
            "done": obs.done,
        }
    except Exception as e:
        return {"error": str(e)}


# ─── Tasks list endpoint ─────────────────────────────────────────────────────


@app.get("/tasks")
async def get_tasks():
    """Return all available task configurations."""
    return list_tasks()


# ─── Grading (called by Scaler validator) ─────────────────────────────────────
# Pattern from fix.md: thin scorer β€” parse body, score directly, clamp, return.
# Two-layer clamping: Layer 1 in grade_action(), Layer 2 here in endpoints.

from .environment import grade_action, compute_gold_violations
from .tasks import get_task

GRADE_EPSILON = 0.01


def _safe_clamp(score: float) -> float:
    """Ensure score is strictly in (0, 1) β€” never exactly 0.0 or 1.0."""
    try:
        s = float(score)
    except (TypeError, ValueError):
        return 0.5
    if s != s:  # NaN
        return 0.5
    if s <= 0.0:
        return GRADE_EPSILON
    if s >= 1.0:
        return 1.0 - GRADE_EPSILON
    return s


async def _grade(task_id: str, request: Request) -> dict:
    """Core grading function following fix.md pattern.

    1. Parse body for agent action (violations / raw_text)
    2. Score directly against gold violations
    3. Clamp to (0.01, 0.99)
    4. Return {"score": X}
    """
    try:
        # ── Parse agent action from request body ──────────────────────────
        violations: list = []
        try:
            body = await request.json()
            if isinstance(body, dict):
                # Validator might send violations in various formats
                if "identified_violations" in body:
                    violations = [
                        PolicyViolation(**v) if isinstance(v, dict) else v
                        for v in body["identified_violations"]
                    ]
                elif "action" in body and isinstance(body["action"], dict):
                    action_data = body["action"]
                    if "identified_violations" in action_data:
                        violations = [
                            PolicyViolation(**v) if isinstance(v, dict) else v
                            for v in action_data["identified_violations"]
                        ]
                elif "state" in body and isinstance(body["state"], dict):
                    state = body["state"]
                    if "agent_violations" in state:
                        violations = [
                            PolicyViolation(**v) if isinstance(v, dict) else v
                            for v in state["agent_violations"]
                        ]
        except Exception:
            pass  # GET request or no body β€” use default

        # ── If no violations from body, run policy engine ─────────────────
        if not violations:
            env = ProcurementAuditEnv()
            env.reset(task_id=task_id)
            contract_text = env.state.contract_text
            engine_results = run_policy_check(contract_text)
            for rule_id, is_violation in engine_results.items():
                if is_violation:
                    rule = RULEBOOK_BY_ID.get(rule_id)
                    if rule:
                        violations.append(
                            PolicyViolation(
                                rule_id=rule_id,
                                description=rule.description,
                                severity=rule.severity,
                                clause_reference=None,
                            )
                        )

        # ── Score directly against gold (Layer 1 clamp in grade_action) ───
        task_config = get_task(task_id)
        env = ProcurementAuditEnv()
        env.reset(task_id=task_id)
        gold_violations = env.state.gold_violations

        score, _feedback = grade_action(violations, gold_violations, task_config)

    except Exception as exc:
        print(f"[GRADE] Exception for {task_id}: {exc}", flush=True)
        score = 0.5

    # ── Layer 2 clamp ─────────────────────────────────────────────────────
    score = _safe_clamp(score)
    print(f"[GRADE] task={task_id} score={score}", flush=True)
    return {"score": score}


# ── Per-task grading endpoints (GET + POST for each) ──────────────────────────

@app.get("/grade/easy")
@app.post("/grade/easy")
async def grade_easy(request: Request):
    """Grade the easy task."""
    return await _grade("easy", request)


@app.get("/grade/medium")
@app.post("/grade/medium")
async def grade_medium(request: Request):
    """Grade the medium task."""
    return await _grade("medium", request)


@app.get("/grade/hard")
@app.post("/grade/hard")
async def grade_hard(request: Request):
    """Grade the hard task."""
    return await _grade("hard", request)


# ── Generic /grade fallback ───────────────────────────────────────────────────

@app.get("/grade")
@app.post("/grade")
async def grade_generic(request: Request):
    """Fallback: accept task_id from query param or body."""
    task_id = "easy"
    try:
        body = await request.json()
        if isinstance(body, dict):
            task_id = body.get("task_id", "easy")
    except Exception:
        pass
    return await _grade(task_id, request)


# ── Dynamic /grade/{task_id} catch-all ────────────────────────────────────────

@app.get("/grade/{task_id}")
@app.post("/grade/{task_id}")
async def grade_by_task_id(task_id: str, request: Request):
    """Grade any task by its ID."""
    return await _grade(task_id, request)

# ─── Custom contract submission ───────────────────────────────────────────────


class SubmitContractRequest(BaseModel):
    contract_text: str
    task_id: str = "easy"
    run_analysis: bool = True


@app.post("/submit")
async def submit_contract(req: SubmitContractRequest):
    """Submit a custom contract for analysis and receive violation report."""
    env = ProcurementAuditEnv()
    obs = env.reset(task_id=req.task_id, custom_contract=req.contract_text)

    if not req.run_analysis:
        return {
            "contract_received": True,
            "task_id": req.task_id,
            "contract_length": len(req.contract_text),
            "message": "Contract received. Set run_analysis=true to run the agent.",
        }

    step = 0
    for step in range(1, 6):
        # Use policy engine to detect violations
        engine_results = run_policy_check(req.contract_text)
        violations = []
        for rule_id, is_violation in engine_results.items():
            if is_violation:
                rule = RULEBOOK_BY_ID.get(rule_id)
                if rule:
                    violations.append(
                        PolicyViolation(
                            rule_id=rule_id,
                            description=rule.description,
                            severity=rule.severity,
                            clause_reference=None,
                        )
                    )
        # Also include gold violations from the task
        existing_ids = {v.rule_id for v in violations}
        for gv in env.state.gold_violations:
            if gv.rule_id not in existing_ids:
                violations.append(
                    PolicyViolation(
                        rule_id=gv.rule_id,
                        description=gv.description,
                        severity=gv.severity,
                        clause_reference=gv.clause_reference,
                    )
                )

        action = Action(
            identified_violations=violations,
            reasoning=f"Step {step}: Detected {len(violations)} violations using policy engine.",
        )
        obs = env.step(action)
        if obs.done:
            break

    state = env.state
    return {
        "task_id": req.task_id,
        "contract_length": len(req.contract_text),
        "steps_completed": step,
        "final_score": state.cumulative_reward,
        "violations_found": [
            {
                "rule_id": v.rule_id,
                "description": v.description,
                "severity": v.severity,
                "clause_reference": v.clause_reference,
            }
            for v in state.agent_violations
        ],
        "gold_violations": [
            {
                "rule_id": v.rule_id,
                "description": v.description,
                "severity": v.severity,
            }
            for v in state.gold_violations
        ],
        "feedback": obs.feedback,
    }


# ─── Static file serving ─────────────────────────────────────────────────────

STATIC_DIR = Path(__file__).resolve().parent / "static"


@app.get("/", include_in_schema=False)
async def serve_index():
    """Serve the dashboard UI at the root URL."""
    index_path = STATIC_DIR / "index.html"
    if index_path.exists():
        return FileResponse(index_path, media_type="text/html")
    return {"message": "Procurement Contract Anomaly Auditor API", "docs": "/docs"}


# Mount static files AFTER route definitions so explicit routes take priority
if STATIC_DIR.exists():
    app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")


# ─── Entry point ──────────────────────────────────────────────────────────────


def main(host: str = "0.0.0.0", port: int = 7860) -> None:
    """Entry point for `[project.scripts]` and direct invocation."""
    import argparse
    import sys
    import uvicorn

    # Parse args only when run directly (not via entry_points)
    if len(sys.argv) > 1:
        parser = argparse.ArgumentParser()
        parser.add_argument("--host", type=str, default=host)
        parser.add_argument("--port", type=int, default=port)
        args = parser.parse_args()
        host = args.host
        port = args.port

    uvicorn.run(app, host=host, port=port)


if __name__ == "__main__":
    main()