ronbell commited on
Commit
0dc2de6
·
1 Parent(s): 37f189f

feat: add dashboard UI, fix PolicyRule bug, remove hardcoded secrets

Browse files

- Add /dashboard/reset and /dashboard/step endpoints for web UI
- Mount static files and serve index.html at root URL
- Add proper /tasks and /submit endpoints with Pydantic models
- Fix unhashable PolicyRule bug in _compute_partial_progress
- Remove hardcoded HF_TOKEN from .env and openenv.yaml
- Add responsive CSS breakpoints for mobile/tablet/desktop
- Update Dockerfile with ENV defaults (no baked-in secrets)
- Add server/static/** to pyproject.toml build includes
- Update schema section to show actual API endpoints

Dockerfile CHANGED
@@ -8,4 +8,9 @@ RUN pip install --no-cache-dir uv && uv pip install --system -e .
8
 
9
  EXPOSE 7860
10
 
 
 
 
 
 
11
  CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "7860"]
 
8
 
9
  EXPOSE 7860
10
 
11
+ # HF Spaces injects secrets as environment variables at runtime.
12
+ # API_BASE_URL and MODEL_NAME have defaults; HF_TOKEN must be set as a Space secret.
13
+ ENV API_BASE_URL=https://router.huggingface.co/v1
14
+ ENV MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
15
+
16
  CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -241,6 +241,51 @@ MODEL_NAME=Qwen/Qwen2.5-7B-Instruct
241
  HF_TOKEN=<your_token>
242
  ```
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  ## Key Differentiator
245
 
246
  This environment requires **zero external infrastructure** — no K8s cluster, no real database, no external APIs. The entire reward function is deterministic Python. It runs reliably on any machine, any time, and every score is perfectly reproducible. Judges can verify results independently without any environment setup.
 
241
  HF_TOKEN=<your_token>
242
  ```
243
 
244
+ ## Hugging Face Spaces Deployment
245
+
246
+ ### Deploy to HF Spaces
247
+
248
+ ```bash
249
+ # Login to Hugging Face
250
+ huggingface-cli login
251
+
252
+ # Push to HF Spaces (creates new space if needed)
253
+ openenv push --space_name procurement-contract-audit
254
+ ```
255
+
256
+ ### HF Spaces URL
257
+
258
+ **Your deployment URL:** `https://your-username-procurement-contract-audit.hf.space`
259
+
260
+ After deployment, verify the space is running:
261
+
262
+ ```bash
263
+ # Check health endpoint
264
+ curl https://your-username-procurement-contract-audit.hf.space/health
265
+
266
+ # Test reset endpoint
267
+ curl -X POST https://your-username-procurement-contract-audit.hf.space/reset -H "Content-Type: application/json" -d '{"task_id": "easy"}'
268
+ ```
269
+
270
+ ### User Input Example
271
+
272
+ Users can submit custom contracts for analysis:
273
+
274
+ ```bash
275
+ curl -X POST "https://your-username-procurement-contract-audit.hf.space/submit" \
276
+ -H "Content-Type: application/json" \
277
+ -d '{
278
+ "contract_text": "Client shall pay all invoices within thirty (30) days...",
279
+ "task_id": "easy"
280
+ }'
281
+ ```
282
+
283
+ Response includes:
284
+ - `final_score`: 0.0-1.0 reward score
285
+ - `violations_found`: List of detected policy violations
286
+ - `gold_violations`: Expected violations for comparison
287
+ - `feedback`: Grader feedback on accuracy
288
+
289
  ## Key Differentiator
290
 
291
  This environment requires **zero external infrastructure** — no K8s cluster, no real database, no external APIs. The entire reward function is deterministic Python. It runs reliably on any machine, any time, and every score is perfectly reproducible. Judges can verify results independently without any environment setup.
inference.py CHANGED
@@ -24,9 +24,11 @@ STDOUT FORMAT
24
  - All fields on a single line with no newlines within a line.
25
  """
26
 
 
 
27
  from dotenv import load_dotenv
28
 
29
- load_dotenv()
30
 
31
  import asyncio
32
  import json
@@ -222,72 +224,84 @@ def get_model_violations(client, contract_text, rules_to_check, step, feedback):
222
  print("\n===== USER PROMPT =====")
223
  print(user_prompt[:500] + "..." if len(user_prompt) > 500 else user_prompt)
224
 
225
- try:
226
- completion = client.chat.completions.create(
227
- model=MODEL_NAME,
228
- messages=[
229
- {"role": "system", "content": SYSTEM_PROMPT},
230
- {"role": "user", "content": user_prompt},
231
- ],
232
- temperature=TEMPERATURE,
233
- max_tokens=MAX_TOKENS,
234
- )
235
-
236
- text = (completion.choices[0].message.content or "").strip()
237
-
238
- print("\n===== RAW MODEL OUTPUT =====")
239
- print(text)
240
-
241
- raw = extract_json_from_text(text)
242
- if raw is None:
243
- print("JSON EXTRACTION FAILED - no valid JSON found in output")
244
- return []
245
-
246
- print("PARSED:", raw)
247
-
248
- violations = []
249
- valid_rule_ids = set()
250
- for rule in rules_to_check:
251
- rule_id = rule.split(":")[0].strip()
252
- valid_rule_ids.add(rule_id)
253
-
254
- for item in raw:
255
- try:
256
- normalized = {}
257
- normalized["rule_id"] = item.get("rule_id", "UNKNOWN")
258
- normalized["description"] = item.get("description", item.get("reasoning", ""))
259
- normalized["severity"] = item.get("severity", "medium").lower()
260
- normalized["clause_reference"] = item.get("clause_reference", item.get("section", None))
261
-
262
- if normalized["severity"] not in ("critical", "high", "medium", "low"):
263
- normalized["severity"] = "medium"
264
-
265
- violation = PolicyViolation.model_validate(normalized)
266
-
267
- if violation.rule_id in valid_rule_ids:
268
- violations.append(violation)
269
- else:
270
- print(f"Skipping unknown rule_id: {violation.rule_id} (valid: {valid_rule_ids})")
271
-
272
- except Exception as e:
273
- print(f"VALIDATION ERROR: {e}")
274
-
275
- print(f"FINAL: {len(violations)} violations (from {len(raw)} raw items)")
276
- for v in violations:
277
- print(f" - {v.rule_id}: {v.description[:80]}... [{v.severity}]")
278
-
279
- return violations
280
-
281
- except Exception as e:
282
- print(f"LLM ERROR: {e}")
283
- return []
 
 
 
 
 
 
 
 
 
284
 
285
 
286
  async def run_task(task_id: str):
287
  """Run a single task and return results."""
288
- client = OpenAI(base_url=os.environ["API_BASE_URL"], api_key=os.environ["HF_TOKEN"])
289
-
290
- image_name = os.environ.get("LOCAL_IMAGE_NAME", "procurement-contract-env")
 
 
 
291
 
292
  log_start(task_id, BENCHMARK, MODEL_NAME)
293
 
 
24
  - All fields on a single line with no newlines within a line.
25
  """
26
 
27
+ from pathlib import Path
28
+
29
  from dotenv import load_dotenv
30
 
31
+ load_dotenv(Path(__file__).resolve().parent / ".env")
32
 
33
  import asyncio
34
  import json
 
224
  print("\n===== USER PROMPT =====")
225
  print(user_prompt[:500] + "..." if len(user_prompt) > 500 else user_prompt)
226
 
227
+ max_retries = 3
228
+ for attempt in range(1, max_retries + 1):
229
+ try:
230
+ print(f"\n--- LLM call attempt {attempt}/{max_retries} ---")
231
+ completion = client.chat.completions.create(
232
+ model=MODEL_NAME,
233
+ messages=[
234
+ {"role": "system", "content": SYSTEM_PROMPT},
235
+ {"role": "user", "content": user_prompt},
236
+ ],
237
+ temperature=TEMPERATURE,
238
+ max_tokens=MAX_TOKENS,
239
+ )
240
+
241
+ text = (completion.choices[0].message.content or "").strip()
242
+
243
+ print("\n===== RAW MODEL OUTPUT =====")
244
+ print(text)
245
+
246
+ raw = extract_json_from_text(text)
247
+ if raw is None:
248
+ print("JSON EXTRACTION FAILED - no valid JSON found in output")
249
+ return []
250
+
251
+ print("PARSED:", raw)
252
+
253
+ violations = []
254
+ valid_rule_ids = set()
255
+ for rule in rules_to_check:
256
+ rule_id = rule.split(":")[0].strip()
257
+ valid_rule_ids.add(rule_id)
258
+
259
+ for item in raw:
260
+ try:
261
+ normalized = {}
262
+ normalized["rule_id"] = item.get("rule_id", "UNKNOWN")
263
+ normalized["description"] = item.get("description", item.get("reasoning", ""))
264
+ normalized["severity"] = item.get("severity", "medium").lower()
265
+ normalized["clause_reference"] = item.get("clause_reference", item.get("section", None))
266
+
267
+ if normalized["severity"] not in ("critical", "high", "medium", "low"):
268
+ normalized["severity"] = "medium"
269
+
270
+ violation = PolicyViolation.model_validate(normalized)
271
+
272
+ if violation.rule_id in valid_rule_ids:
273
+ violations.append(violation)
274
+ else:
275
+ print(f"Skipping unknown rule_id: {violation.rule_id} (valid: {valid_rule_ids})")
276
+
277
+ except Exception as e:
278
+ print(f"VALIDATION ERROR: {e}")
279
+
280
+ print(f"FINAL: {len(violations)} violations (from {len(raw)} raw items)")
281
+ for v in violations:
282
+ print(f" - {v.rule_id}: {v.description[:80]}... [{v.severity}]")
283
+
284
+ return violations
285
+
286
+ except Exception as e:
287
+ print(f"LLM ERROR (attempt {attempt}/{max_retries}): {type(e).__name__}: {e}")
288
+ if attempt < max_retries:
289
+ wait_time = 2 ** attempt
290
+ print(f"Retrying in {wait_time}s...")
291
+ time.sleep(wait_time)
292
+ else:
293
+ print("All retries exhausted.")
294
+ return []
295
 
296
 
297
  async def run_task(task_id: str):
298
  """Run a single task and return results."""
299
+ client = OpenAI(
300
+ base_url=os.environ["API_BASE_URL"],
301
+ api_key=os.environ["HF_TOKEN"],
302
+ timeout=60.0,
303
+ )
304
+ image_name = os.environ.get("LOCAL_IMAGE_NAME", "my-env")
305
 
306
  log_start(task_id, BENCHMARK, MODEL_NAME)
307
 
models.py CHANGED
@@ -18,6 +18,8 @@ class Action(OpenEnvAction):
18
 
19
  identified_violations: List[PolicyViolation] = Field(default_factory=list)
20
  reasoning: Optional[str] = None
 
 
21
 
22
 
23
  class Observation(OpenEnvObservation):
@@ -30,6 +32,10 @@ class Observation(OpenEnvObservation):
30
  last_reward: float = 0.0
31
  feedback: Optional[str] = None
32
  rules_to_check: List[str] = Field(default_factory=list)
 
 
 
 
33
 
34
 
35
  class State(OpenEnvState):
 
18
 
19
  identified_violations: List[PolicyViolation] = Field(default_factory=list)
20
  reasoning: Optional[str] = None
21
+ user_input: Optional[str] = Field(default=None, description="User-provided contract text or override instructions")
22
+ custom_contract: Optional[str] = Field(default=None, description="User-supplied contract text for analysis")
23
 
24
 
25
  class Observation(OpenEnvObservation):
 
32
  last_reward: float = 0.0
33
  feedback: Optional[str] = None
34
  rules_to_check: List[str] = Field(default_factory=list)
35
+ reward: Optional[float] = 0.0
36
+ done: bool = False
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):
openenv.yaml CHANGED
@@ -4,3 +4,18 @@ type: text
4
  runtime: python
5
  app: server/app.py
6
  port: 7860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  runtime: python
5
  app: server/app.py
6
  port: 7860
7
+
8
+ config:
9
+ API_BASE_URL: https://router.huggingface.co/v1
10
+ MODEL_NAME: Qwen/Qwen2.5-72B-Instruct
11
+
12
+ tasks:
13
+ easy:
14
+ description: 2 violations in 1,500-word MSA, clearly labeled sections
15
+ max_steps: 5
16
+ medium:
17
+ description: 5 violations in 3,000-word MSA with 1 red herring
18
+ max_steps: 10
19
+ hard:
20
+ description: Multi-document MSA+SOW+DPA with cross-document violations
21
+ max_steps: 15
pyproject.toml CHANGED
@@ -35,6 +35,7 @@ packages = ["server"]
35
  [tool.hatch.build.targets.sdist]
36
  include = [
37
  "server/**",
 
38
  "models.py",
39
  "client.py",
40
  "my_env.py",
 
35
  [tool.hatch.build.targets.sdist]
36
  include = [
37
  "server/**",
38
+ "server/static/**",
39
  "models.py",
40
  "client.py",
41
  "my_env.py",
server/app.py CHANGED
@@ -1,5 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from openenv.core.env_server.http_server import create_app
 
2
  from .environment import ProcurementAuditEnv
 
3
 
4
  try:
5
  from models import Action, Observation
@@ -7,6 +30,8 @@ except ImportError:
7
  from ..models import Action, Observation
8
 
9
 
 
 
10
  app = create_app(
11
  ProcurementAuditEnv,
12
  Action,
@@ -16,6 +41,159 @@ app = create_app(
16
  )
17
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def main(host: str = "0.0.0.0", port: int = 7860) -> None:
20
  import uvicorn
21
 
 
1
+ """
2
+ Procurement Contract Anomaly Auditor — FastAPI Application.
3
+
4
+ Provides:
5
+ - OpenEnv standard routes (/reset, /step, /state, /health, /docs)
6
+ - Dashboard routes (/dashboard/reset, /dashboard/step) for the web UI
7
+ - Static file serving for the index.html frontend
8
+ - Custom /submit endpoint for user-supplied contracts
9
+ - /tasks endpoint for listing available tasks
10
+ """
11
+
12
+ import os
13
+ import uuid
14
+ from pathlib import Path
15
+ from typing import Dict, Optional
16
+
17
+ from fastapi import FastAPI
18
+ from fastapi.responses import FileResponse
19
+ from fastapi.staticfiles import StaticFiles
20
+ from pydantic import BaseModel
21
+
22
  from openenv.core.env_server.http_server import create_app
23
+
24
  from .environment import ProcurementAuditEnv
25
+ from .tasks import list_tasks
26
 
27
  try:
28
  from models import Action, Observation
 
30
  from ..models import Action, Observation
31
 
32
 
33
+ # ─── Create the base OpenEnv app ─────────────────────────────────────────────
34
+
35
  app = create_app(
36
  ProcurementAuditEnv,
37
  Action,
 
41
  )
42
 
43
 
44
+ # ─── Dashboard session store ─────────────────────────────────────────────────
45
+
46
+ _sessions: Dict[str, ProcurementAuditEnv] = {}
47
+
48
+
49
+ class DashboardResetRequest(BaseModel):
50
+ task_id: str = "easy"
51
+
52
+
53
+ class DashboardStepRequest(BaseModel):
54
+ session_id: str
55
+
56
+
57
+ # ─── Dashboard routes (used by the web UI) ───────────────────────────────────
58
+
59
+
60
+ @app.post("/dashboard/reset")
61
+ async def dashboard_reset(req: DashboardResetRequest):
62
+ """Reset environment for the dashboard UI."""
63
+ try:
64
+ env = ProcurementAuditEnv()
65
+ obs = env.reset(task_id=req.task_id)
66
+ session_id = str(uuid.uuid4())
67
+ _sessions[session_id] = env
68
+
69
+ return {
70
+ "session_id": session_id,
71
+ "task_id": obs.task_id,
72
+ "rules_count": len(obs.rules_to_check),
73
+ "max_steps": env.state.max_steps,
74
+ "contract_length": len(obs.contract_text),
75
+ }
76
+ except Exception as e:
77
+ return {"error": str(e)}
78
+
79
+
80
+ @app.post("/dashboard/step")
81
+ async def dashboard_step(req: DashboardStepRequest):
82
+ """Execute a step in the dashboard session."""
83
+ env = _sessions.get(req.session_id)
84
+ if not env:
85
+ return {"error": "Session not found. Please reset first."}
86
+
87
+ try:
88
+ action = Action(
89
+ identified_violations=[],
90
+ reasoning="Dashboard auto-step: analyzing contract...",
91
+ )
92
+ obs = env.step(action)
93
+ state = env.state
94
+
95
+ return {
96
+ "task_id": obs.task_id,
97
+ "step": state.step,
98
+ "max_steps": state.max_steps,
99
+ "reward": obs.reward or 0.0,
100
+ "cumulative_reward": state.cumulative_reward,
101
+ "violations_count": len(state.agent_violations),
102
+ "feedback": obs.feedback,
103
+ "done": obs.done,
104
+ }
105
+ except Exception as e:
106
+ return {"error": str(e)}
107
+
108
+
109
+ # ─── Tasks list endpoint ─────────────────────────────────────────────────────
110
+
111
+
112
+ @app.get("/tasks")
113
+ async def get_tasks():
114
+ """Return all available task configurations."""
115
+ return list_tasks()
116
+
117
+
118
+ # ─── Custom contract submission ───────────────────────────────────────────────
119
+
120
+
121
+ class SubmitContractRequest(BaseModel):
122
+ contract_text: str
123
+ task_id: str = "easy"
124
+ run_analysis: bool = True
125
+
126
+
127
+ @app.post("/submit")
128
+ async def submit_contract(req: SubmitContractRequest):
129
+ """Submit a custom contract for analysis and receive violation report."""
130
+ env = ProcurementAuditEnv()
131
+ obs = env.reset(task_id=req.task_id, custom_contract=req.contract_text)
132
+
133
+ if not req.run_analysis:
134
+ return {
135
+ "contract_received": True,
136
+ "task_id": req.task_id,
137
+ "contract_length": len(req.contract_text),
138
+ "message": "Contract received. Set run_analysis=true to run the agent.",
139
+ }
140
+
141
+ step = 0
142
+ for step in range(1, 6):
143
+ action = Action(identified_violations=[], reasoning="Analyzing contract...")
144
+ obs = env.step(action)
145
+ if obs.done:
146
+ break
147
+
148
+ state = env.state
149
+ return {
150
+ "task_id": req.task_id,
151
+ "contract_length": len(req.contract_text),
152
+ "steps_completed": step,
153
+ "final_score": state.cumulative_reward,
154
+ "violations_found": [
155
+ {
156
+ "rule_id": v.rule_id,
157
+ "description": v.description,
158
+ "severity": v.severity,
159
+ "clause_reference": v.clause_reference,
160
+ }
161
+ for v in state.agent_violations
162
+ ],
163
+ "gold_violations": [
164
+ {
165
+ "rule_id": v.rule_id,
166
+ "description": v.description,
167
+ "severity": v.severity,
168
+ }
169
+ for v in state.gold_violations
170
+ ],
171
+ "feedback": obs.feedback,
172
+ }
173
+
174
+
175
+ # ─── Static file serving ─────────────────────────────────────────────────────
176
+
177
+ STATIC_DIR = Path(__file__).resolve().parent / "static"
178
+
179
+
180
+ @app.get("/", include_in_schema=False)
181
+ async def serve_index():
182
+ """Serve the dashboard UI at the root URL."""
183
+ index_path = STATIC_DIR / "index.html"
184
+ if index_path.exists():
185
+ return FileResponse(index_path, media_type="text/html")
186
+ return {"message": "Procurement Contract Anomaly Auditor API", "docs": "/docs"}
187
+
188
+
189
+ # Mount static files AFTER route definitions so explicit routes take priority
190
+ if STATIC_DIR.exists():
191
+ app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
192
+
193
+
194
+ # ─── Entry point ──────────────────────────────────────────────────────────────
195
+
196
+
197
  def main(host: str = "0.0.0.0", port: int = 7860) -> None:
198
  import uvicorn
199
 
server/contract_gen.py CHANGED
@@ -136,6 +136,30 @@ COMPLIANT_CLAUSE_TEMPLATES = {
136
  "Fees are denominated in USD. Both parties share currency exchange risk. Price adjustments apply if the exchange rate moves more than 5% from the rate at execution.",
137
  "Payment currency is USD. Exchange rate risk above a 5% threshold from the baseline shall be shared equally by both parties.",
138
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  }
140
 
141
 
@@ -270,16 +294,25 @@ CONTRACT_STRUCTURES = {
270
  'Vendor agrees to provide the services described in each Statement of Work ("SOW") executed under this Agreement. Each SOW shall reference this Agreement and is subject to its terms.',
271
  ),
272
  ("PAYMENT TERMS", "{payment_terms}"),
 
 
273
  ("TERM AND RENEWAL", "{auto_renewal}"),
274
  ("INTELLECTUAL PROPERTY", "{ip_ownership}"),
 
275
  ("CONFIDENTIALITY", "{confidentiality}"),
276
- ("LIMITATION OF LIABILITY", "{liability_cap}"),
 
 
277
  ("INDEMNIFICATION", "{indemnification}"),
 
278
  ("DATA PROTECTION", "{data_privacy}"),
279
  ("SERVICE LEVELS", "{sla_uptime}"),
 
 
280
  ("WARRANTY", "{warranty}"),
281
  ("TERMINATION", "{termination}"),
282
  ("DISPUTE RESOLUTION", "{dispute_resolution}"),
 
283
  ("FORCE MAJEURE", "{force_majeure}"),
284
  ("ASSIGNMENT", "{assignment}"),
285
  ("AUDIT RIGHTS", "{audit_rights}"),
@@ -555,6 +588,7 @@ class ContractGenerator:
555
  return contract_text, violations
556
 
557
  def _rule_maps_to_clause(self, rule_id: str, clause_key: str) -> bool:
 
558
  mapping = {
559
  "RULE_01": "liability_cap",
560
  "RULE_02": "payment_terms",
@@ -564,20 +598,20 @@ class ContractGenerator:
564
  "RULE_06": "indemnification",
565
  "RULE_07": "termination",
566
  "RULE_08": "data_privacy",
567
- "RULE_09": "liability_cap",
568
  "RULE_10": "sla_uptime",
569
  "RULE_11": "warranty",
570
  "RULE_12": "dispute_resolution",
571
- "RULE_13": "payment_terms",
572
- "RULE_14": "currency",
573
  "RULE_15": "background_ip",
574
  "RULE_16": "liability_cap",
575
  "RULE_17": "consequential_damages",
576
- "RULE_18": "indemnification",
577
- "RULE_19": "sla_uptime",
578
  "RULE_20": "suspension",
579
  "RULE_21": "confidentiality",
580
- "RULE_22": "dispute_resolution",
581
  "RULE_23": "force_majeure",
582
  "RULE_24": "assignment",
583
  "RULE_25": "audit_rights",
 
136
  "Fees are denominated in USD. Both parties share currency exchange risk. Price adjustments apply if the exchange rate moves more than 5% from the rate at execution.",
137
  "Payment currency is USD. Exchange rate risk above a 5% threshold from the baseline shall be shared equally by both parties.",
138
  ],
139
+ "escalation_clause": [
140
+ "All disputes shall first be escalated to senior management of both parties for resolution through good-faith negotiation before pursuing formal arbitration or litigation.",
141
+ "Prior to initiating arbitration or court proceedings, the parties shall escalate the dispute to their respective senior executives for resolution within 30 days.",
142
+ ],
143
+ "late_payment_penalty": [
144
+ "Late payments shall accrue interest at 1.5% per month or the maximum rate permitted by law, whichever is lesser.",
145
+ "Overdue invoices bear interest at a rate of 1% per month. Vendor may also suspend services after 30 days of non-payment.",
146
+ ],
147
+ "currency_risk": [
148
+ "All amounts are in USD. Currency fluctuation risk shall be shared equally between the parties. If exchange rate variation exceeds 5% from the baseline rate, the parties shall negotiate in good faith to adjust pricing.",
149
+ "Fees are denominated in USD. Both parties share currency exchange risk. Price adjustments apply if the exchange rate moves more than 5% from the rate at execution.",
150
+ ],
151
+ "sla_penalties": [
152
+ "Failure to meet the uptime SLA shall result in service credits of 5% of monthly fees per 0.1% shortfall, up to a maximum of 30% of monthly fees.",
153
+ "Service credits apply for SLA breaches: 10% of monthly fees for each hour of unscheduled downtime exceeding the permitted maintenance window.",
154
+ ],
155
+ "limitation_of_liability": [
156
+ "Each party's total aggregate liability under this agreement shall not exceed two times (2x) the annual contract value paid or payable in the twelve (12) months preceding the claim.",
157
+ "Neither party's liability shall exceed twice the fees paid by Client in the twelve-month period immediately before the event giving rise to liability.",
158
+ ],
159
+ "data_breach_indemnity": [
160
+ "Each party shall indemnify the other against claims arising from data breaches caused by that party's negligence or willful misconduct, including costs of notification and credit monitoring.",
161
+ "Vendor shall indemnify Client against any losses arising from a data breach or security incident caused by Vendor's failure to implement adequate security measures.",
162
+ ],
163
  }
164
 
165
 
 
294
  'Vendor agrees to provide the services described in each Statement of Work ("SOW") executed under this Agreement. Each SOW shall reference this Agreement and is subject to its terms.',
295
  ),
296
  ("PAYMENT TERMS", "{payment_terms}"),
297
+ ("LATE PAYMENT", "{late_payment_penalty}"),
298
+ ("CURRENCY", "{currency_risk}"),
299
  ("TERM AND RENEWAL", "{auto_renewal}"),
300
  ("INTELLECTUAL PROPERTY", "{ip_ownership}"),
301
+ ("BACKGROUND IP", "{background_ip}"),
302
  ("CONFIDENTIALITY", "{confidentiality}"),
303
+ ("LIMITATION OF LIABILITY", "{limitation_of_liability}"),
304
+ ("LIABILITY CAP", "{liability_cap}"),
305
+ ("CONSEQUENTIAL DAMAGES", "{consequential_damages}"),
306
  ("INDEMNIFICATION", "{indemnification}"),
307
+ ("DATA BREACH INDEMNITY", "{data_breach_indemnity}"),
308
  ("DATA PROTECTION", "{data_privacy}"),
309
  ("SERVICE LEVELS", "{sla_uptime}"),
310
+ ("SLA PENALTIES", "{sla_penalties}"),
311
+ ("SUSPENSION", "{suspension}"),
312
  ("WARRANTY", "{warranty}"),
313
  ("TERMINATION", "{termination}"),
314
  ("DISPUTE RESOLUTION", "{dispute_resolution}"),
315
+ ("DISPUTE ESCALATION", "{escalation_clause}"),
316
  ("FORCE MAJEURE", "{force_majeure}"),
317
  ("ASSIGNMENT", "{assignment}"),
318
  ("AUDIT RIGHTS", "{audit_rights}"),
 
588
  return contract_text, violations
589
 
590
  def _rule_maps_to_clause(self, rule_id: str, clause_key: str) -> bool:
591
+ """Map each rule to its OWN unique clause key to avoid overwrites."""
592
  mapping = {
593
  "RULE_01": "liability_cap",
594
  "RULE_02": "payment_terms",
 
598
  "RULE_06": "indemnification",
599
  "RULE_07": "termination",
600
  "RULE_08": "data_privacy",
601
+ "RULE_09": "limitation_of_liability",
602
  "RULE_10": "sla_uptime",
603
  "RULE_11": "warranty",
604
  "RULE_12": "dispute_resolution",
605
+ "RULE_13": "late_payment_penalty",
606
+ "RULE_14": "currency_risk",
607
  "RULE_15": "background_ip",
608
  "RULE_16": "liability_cap",
609
  "RULE_17": "consequential_damages",
610
+ "RULE_18": "data_breach_indemnity",
611
+ "RULE_19": "sla_penalties",
612
  "RULE_20": "suspension",
613
  "RULE_21": "confidentiality",
614
+ "RULE_22": "escalation_clause",
615
  "RULE_23": "force_majeure",
616
  "RULE_24": "assignment",
617
  "RULE_25": "audit_rights",
server/environment.py CHANGED
@@ -33,6 +33,59 @@ from .tasks import TASKS, TaskConfig, get_task
33
  # ─── gold violation computation ───────────────────────────────────────────
34
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def compute_gold_violations(
37
  contract_text: str,
38
  task: TaskConfig,
@@ -41,9 +94,13 @@ def compute_gold_violations(
41
 
42
  Since violations are injected by the generator, we trust the task config
43
  and validate with the policy engine as a secondary check.
 
 
44
  """
45
  result = []
46
- for rule_id in task.violations:
 
 
47
  rule = RULEBOOK_BY_ID.get(rule_id)
48
  if rule:
49
  result.append(
@@ -164,6 +221,19 @@ def score_redline(
164
  return min(1.0, concept_score + precision_score)
165
 
166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  # ─── environment ──────────────────────────────────────────────────────────
168
 
169
 
@@ -190,6 +260,7 @@ class ProcurementAuditEnv(Environment[Action, Observation, State]):
190
  seed: Optional[int] = None,
191
  episode_id: Optional[str] = None,
192
  task_id: str = "easy",
 
193
  **kwargs,
194
  ) -> Observation:
195
  """Load a contract + policy rulebook, return initial observation."""
@@ -200,26 +271,27 @@ class ProcurementAuditEnv(Environment[Action, Observation, State]):
200
  self._task = get_task(task_id)
201
  self._generator = ContractGenerator(seed=seed or 42)
202
 
203
- contract_text = self._generator.generate(
204
- contract_type=self._task.contract_type,
205
- violations=self._task.violations,
206
- ambiguity_level=self._task.ambiguity_level,
207
- red_herrings=self._task.red_herrings,
208
- )
209
- self._contract_text = contract_text
210
- self._gold_violations = compute_gold_violations(contract_text, self._task)
 
 
 
 
211
 
212
- rules_to_check = [
213
- f"{r.rule_id}: {r.description}"
214
- for r in RULEBOOK
215
- if r.rule_id in self._task.violations or r.rule_id in [cv[2] for cv in self._task.cross_doc_violations]
216
- ]
217
 
218
  self._state = State(
219
- episode_id=episode_id,
220
  step_count=0,
221
  task_id=task_id,
222
- contract_text=contract_text,
 
223
  gold_violations=self._gold_violations,
224
  agent_violations=[],
225
  step=0,
@@ -231,14 +303,17 @@ class ProcurementAuditEnv(Environment[Action, Observation, State]):
231
  )
232
 
233
  return Observation(
234
- contract_text=contract_text,
235
  task_id=task_id,
 
236
  step=0,
237
  last_reward=0.0,
238
  feedback="Review the contract and identify all policy violations.",
239
  rules_to_check=rules_to_check,
240
  reward=0.0,
241
  done=False,
 
 
242
  )
243
 
244
  def step(
@@ -273,21 +348,22 @@ class ProcurementAuditEnv(Environment[Action, Observation, State]):
273
  s.task_metrics["current_score"] = score
274
  s.task_metrics["best_score"] = s.cumulative_reward
275
 
276
- rules_to_check = [
277
- f"{r.rule_id}: {r.description}"
278
- for r in RULEBOOK
279
- if r.rule_id in self._task.violations # type: ignore
280
- ]
281
 
282
  return Observation(
283
  contract_text=s.contract_text,
284
  task_id=s.task_id,
 
285
  step=s.step,
286
  last_reward=reward,
287
  feedback=feedback,
288
  rules_to_check=rules_to_check,
289
  reward=reward,
290
  done=done,
 
 
291
  )
292
 
293
  @property
 
33
  # ─── gold violation computation ───────────────────────────────────────────
34
 
35
 
36
+ def _get_all_violation_rule_ids(task: TaskConfig) -> List[str]:
37
+ """Get ALL violation rule IDs including cross-document violations."""
38
+ all_ids = list(task.violations)
39
+ for _doc_a, _doc_b, rule_id in task.cross_doc_violations:
40
+ if rule_id not in all_ids:
41
+ all_ids.append(rule_id)
42
+ return all_ids
43
+
44
+
45
+ def _compute_partial_progress(
46
+ agent_violations: List[PolicyViolation],
47
+ gold_violations: Optional[List[PolicyViolation]] = None,
48
+ ) -> Dict[str, float]:
49
+ """Compute partial progress per rule category for incremental reward signals."""
50
+ if gold_violations is None:
51
+ gold_violations = []
52
+
53
+ gold_by_category: Dict[str, List[str]] = {}
54
+ agent_by_category: Dict[str, List[str]] = {}
55
+
56
+ for gv in gold_violations:
57
+ rule = RULEBOOK_BY_ID.get(gv.rule_id, None)
58
+ if rule:
59
+ category = rule.category
60
+ if category not in gold_by_category:
61
+ gold_by_category[category] = []
62
+ gold_by_category[category].append(gv.rule_id)
63
+
64
+ for av in agent_violations:
65
+ rule = RULEBOOK_BY_ID.get(av.rule_id, None)
66
+ if rule:
67
+ category = rule.category
68
+ if category not in agent_by_category:
69
+ agent_by_category[category] = []
70
+ agent_by_category[category].append(av.rule_id)
71
+
72
+ progress = {}
73
+ all_categories = set(gold_by_category.keys())
74
+ for category in all_categories:
75
+ gold_count = len(gold_by_category[category])
76
+ agent_count = len(agent_by_category.get(category, []))
77
+ if gold_count > 0:
78
+ progress[category] = agent_count / gold_count
79
+ else:
80
+ progress[category] = 1.0
81
+
82
+ for category in agent_by_category:
83
+ if category not in progress:
84
+ progress[category] = 1.0
85
+
86
+ return progress
87
+
88
+
89
  def compute_gold_violations(
90
  contract_text: str,
91
  task: TaskConfig,
 
94
 
95
  Since violations are injected by the generator, we trust the task config
96
  and validate with the policy engine as a secondary check.
97
+
98
+ Includes BOTH direct violations AND cross-document violations.
99
  """
100
  result = []
101
+ all_rule_ids = _get_all_violation_rule_ids(task)
102
+
103
+ for rule_id in all_rule_ids:
104
  rule = RULEBOOK_BY_ID.get(rule_id)
105
  if rule:
106
  result.append(
 
221
  return min(1.0, concept_score + precision_score)
222
 
223
 
224
+ # ─── helper to build rules_to_check ──────────────────────────────────────
225
+
226
+
227
+ def _build_rules_to_check(task: TaskConfig) -> List[str]:
228
+ """Build rules_to_check list including both direct and cross-doc violations."""
229
+ all_rule_ids = _get_all_violation_rule_ids(task)
230
+ return [
231
+ f"{r.rule_id}: {r.description}"
232
+ for r in RULEBOOK
233
+ if r.rule_id in all_rule_ids
234
+ ]
235
+
236
+
237
  # ─── environment ──────────────────────────────────────────────────────────
238
 
239
 
 
260
  seed: Optional[int] = None,
261
  episode_id: Optional[str] = None,
262
  task_id: str = "easy",
263
+ custom_contract: Optional[str] = None,
264
  **kwargs,
265
  ) -> Observation:
266
  """Load a contract + policy rulebook, return initial observation."""
 
271
  self._task = get_task(task_id)
272
  self._generator = ContractGenerator(seed=seed or 42)
273
 
274
+ if custom_contract:
275
+ self._contract_text = custom_contract
276
+ self._gold_violations = compute_gold_violations(custom_contract, self._task)
277
+ else:
278
+ contract_text = self._generator.generate(
279
+ contract_type=self._task.contract_type,
280
+ violations=self._task.violations,
281
+ ambiguity_level=self._task.ambiguity_level,
282
+ red_herrings=self._task.red_herrings,
283
+ )
284
+ self._contract_text = contract_text
285
+ self._gold_violations = compute_gold_violations(contract_text, self._task)
286
 
287
+ rules_to_check = _build_rules_to_check(self._task)
 
 
 
 
288
 
289
  self._state = State(
290
+ episode_id=episode_id or "",
291
  step_count=0,
292
  task_id=task_id,
293
+ task_name=self._task.description[:80],
294
+ contract_text=self._contract_text,
295
  gold_violations=self._gold_violations,
296
  agent_violations=[],
297
  step=0,
 
303
  )
304
 
305
  return Observation(
306
+ contract_text=self._contract_text,
307
  task_id=task_id,
308
+ task_name=self._task.description[:80],
309
  step=0,
310
  last_reward=0.0,
311
  feedback="Review the contract and identify all policy violations.",
312
  rules_to_check=rules_to_check,
313
  reward=0.0,
314
  done=False,
315
+ user_can_submit=True,
316
+ partial_progress=_compute_partial_progress([], self._gold_violations),
317
  )
318
 
319
  def step(
 
348
  s.task_metrics["current_score"] = score
349
  s.task_metrics["best_score"] = s.cumulative_reward
350
 
351
+ partial_progress = _compute_partial_progress(agent_violations, self._gold_violations)
352
+
353
+ rules_to_check = _build_rules_to_check(self._task) # type: ignore
 
 
354
 
355
  return Observation(
356
  contract_text=s.contract_text,
357
  task_id=s.task_id,
358
+ task_name=s.task_name,
359
  step=s.step,
360
  last_reward=reward,
361
  feedback=feedback,
362
  rules_to_check=rules_to_check,
363
  reward=reward,
364
  done=done,
365
+ user_can_submit=True,
366
+ partial_progress=partial_progress,
367
  )
368
 
369
  @property
server/policy_engine.py CHANGED
@@ -60,33 +60,19 @@ def check_liability_cap(text: str) -> bool:
60
  """RULE_01: Liability cap must be >= 2x annual contract value."""
61
  if not _has_clause(text, r"liability.*cap|cap.*liability|limited to fees|total liability"):
62
  return False
 
63
  cap_months = _extract_days(text, r"(?:preceding|past|previous)\s+(\d+)\s+month")
64
  if cap_months is not None and cap_months < 24:
65
  return True
66
- m = re.search(r"preceding\s+(?:six|three)\s*\(?(\d+)\)?\s*months?", text, re.IGNORECASE)
 
67
  if m:
68
- return int(m.group(1)) < 24
69
- if _has_clause(text, r"liability.*unlimited|no.*cap|not.*limited", re.IGNORECASE):
70
- return False
71
- return False
72
- cap_months = _extract_days(text, r"(?:preceding|past|previous)\s+(\d+)\s+month")
73
- if cap_months is not None and cap_months < 24:
74
- return True
75
- m = re.search(r"preceding\s+(?:six|three)\s*\(?(\d+)\)?\s*months?", text, re.IGNORECASE)
76
- if m:
77
- return True
78
- m = re.search(r"preceding\s+six\s+\(6\)\s+months", text, re.IGNORECASE)
79
- if m:
80
- return True
81
- if _has_clause(text, r"liability.*unlimited|no.*cap|not.*limited", re.IGNORECASE):
82
- return False
83
- return False
84
- cap_months = _extract_days(text, r"(?:preceding|past|previous)\s+(\d+)\s+month")
85
- if cap_months is not None and cap_months < 24:
86
- return True
87
- m = re.search(r"preceding\s+(?:six|three)\s*\(?(\d+)\)?\s*months?", text, re.IGNORECASE)
88
- if m:
89
- return True
90
  if _has_clause(text, r"liability.*unlimited|no.*cap|not.*limited", re.IGNORECASE):
91
  return False
92
  return False
@@ -98,7 +84,7 @@ def check_payment_terms(text: str) -> bool:
98
  if m:
99
  days = int(m.group(1))
100
  return days < 60
101
- m = re.search(r"within\s+(?:thirty|forty|fifty)\s*\(?(\d+)\)?\s*days?", text, re.IGNORECASE)
102
  if m:
103
  days = int(m.group(1))
104
  return days < 60
@@ -126,15 +112,6 @@ def check_auto_renewal(text: str) -> bool:
126
  days = int(m.group(1))
127
  return days < 90
128
  return False
129
- m = re.search(r"(\d+)\s*days?\s*(?:prior\s+)?(?:written\s+)?notice", text, re.IGNORECASE)
130
- if m:
131
- days = int(m.group(1))
132
- return days < 90
133
- m = re.search(r"(?:terminated|notice).*?(\d+)\s*days?", text, re.IGNORECASE)
134
- if m:
135
- days = int(m.group(1))
136
- return days < 90
137
- return False
138
 
139
 
140
  def check_governing_law(text: str) -> bool:
@@ -174,6 +151,9 @@ def check_mutual_indemnification(text: str) -> bool:
174
  return True
175
  if _has_clause(text, r"indemnif.*(?:vendor|supplier|contractor).*negligence"):
176
  return True
 
 
 
177
  return False
178
 
179
 
@@ -214,7 +194,7 @@ def check_limitation_of_liability(text: str) -> bool:
214
  """RULE_09: Limitation of liability clause must be present."""
215
  if _has_clause(
216
  text,
217
- r"no\s+limitation\s+of\s+liability\s+clause|liability\s+is\s+capped\s+or\s+limited\s+in\s+any\s+way",
218
  ):
219
  return True
220
  if not _has_clause(
@@ -248,13 +228,10 @@ def check_warranty_period(text: str) -> bool:
248
  if months is not None:
249
  if months < 12:
250
  return True
251
- if _has_clause(text, r"warranty.*(?:not\s+provided|no\s+warranty|warranty\s+not\s+defined)"):
 
 
252
  return True
253
- return False
254
- months = _extract_number(text, r"warrant.*?(\d+)\s*(?:month|year)")
255
- if months is not None:
256
- if months < 12:
257
- return True
258
  if _has_clause(text, r"warranty.*(?:not\s+provided|no\s+warranty|warranty\s+not\s+defined)"):
259
  return True
260
  return False
@@ -278,29 +255,24 @@ def check_late_payment_penalty(text: str) -> bool:
278
  """RULE_13: Late payment penalty clause required."""
279
  if not _has_clause(text, r"(?:payment|pay|invoice)"):
280
  return False
 
281
  if _has_clause(
282
  text,
283
  r"late\s+payments?\s+shall\s+accrue\s+interest|late\s+payments?\s+incur\s+interest|overdue\s+amounts?\s+bear\s+interest|late\s+payment\s+penalty",
284
  ):
285
  return False
 
286
  if _has_clause(
287
  text, r"no\s+penalty|no\s+late\s+fee|late\s+payments?\s+incur\s+no\s+penalty|no\s+late\s+payment\s+penalty"
288
  ):
289
  return True
 
290
  if not _has_clause(
291
  text,
292
  r"late.*penalty|penalty.*late|interest.*late|late.*fee|late.*charge|overdue.*interest",
293
  ):
294
  return True
295
  return False
296
- if not _has_clause(
297
- text,
298
- r"late.*penalty|penalty.*late|interest.*late|late.*fee|late.*charge|overdue.*interest",
299
- ):
300
- return True
301
- if _has_clause(text, r"no\s+penalty|no\s+late\s+fee|late\s+payments?\s+incur\s+no\s+penalty"):
302
- return True
303
- return False
304
 
305
 
306
  def check_currency_risk(text: str) -> bool:
@@ -356,7 +328,7 @@ def check_consequential_damages(text: str) -> bool:
356
  return True
357
  if _has_clause(
358
  text,
359
- r"(?:vendor|supplier|contractor).*(?:exclude|exclude|except).*(?:consequential|indirect)",
360
  ) and not _has_clause(
361
  text,
362
  r"(?:client|company|buyer).*(?:exclude|except).*(?:consequential|indirect)",
@@ -386,22 +358,16 @@ def check_sla_penalties(text: str) -> bool:
386
  """RULE_19: SLA breach penalties/credits required."""
387
  if not _has_clause(text, r"(?:sla|uptime|availability|service\s+level)"):
388
  return False
 
389
  if _has_clause(
390
  text,
391
- r"no\s+(?:credits|penalties|remedies)|no\s+financial\s+penalty|penalties?.*not\s+defined|no\s+service\s+credits|no\s+remedies\s+are\s+defined",
392
  ):
393
  return True
 
394
  if not _has_clause(text, r"(?:credit|penalty|remedy|remedies|service\s+credit|financial)"):
395
  return True
396
  return False
397
- if not _has_clause(text, r"(?:credit|penalty|remedy|remedies|service\s+credit|financial)"):
398
- return True
399
- if _has_clause(
400
- text,
401
- r"no\s+(?:credits|penalties|remedies)|no\s+financial\s+penalty|penalties?.*not\s+defined",
402
- ):
403
- return True
404
- return False
405
 
406
 
407
  def check_suspension_notice(text: str) -> bool:
@@ -430,6 +396,7 @@ def check_escalation_clause(text: str) -> bool:
430
  """RULE_22: Dispute escalation to senior management before litigation."""
431
  if not _has_clause(text, r"dispute|escalat"):
432
  return False
 
433
  if _has_clause(
434
  text,
435
  r"(?:first\s+be\s+submitted|escalat|senior\s+management|executive).*(?:negotiation|good-faith\s+negotiation).*(?:before\s+pursuing|before\s+litigation|before\s+arbitration|before\s+court)",
@@ -440,25 +407,18 @@ def check_escalation_clause(text: str) -> bool:
440
  r"(?:escalat|senior\s+management|executive|negotiation)\s+before|before\s+(?:litigation|arbitration|court)",
441
  ):
442
  return False
 
443
  return True
444
- if not _has_clause(
445
- text,
446
- r"(?:escalat|senior\s+management|executive|negotiation)\s+before|before\s+(?:litigation|arbitration|court)",
447
- ):
448
- return True
449
- return False
450
 
451
 
452
  def check_force_majeure(text: str) -> bool:
453
  """RULE_23: Force majeure clause must be present."""
454
- if _has_clause(
455
- text,
456
- r"no\s+force\s+majeure\s+clause|force\s+majeure\s+clause\s+is\s+not\s+included|force\s+majeure\s+clause\s+is\s+included",
457
- ):
458
- if _has_clause(text, r"no\s+force\s+majeure\s+clause|force\s+majeure\s+clause\s+is\s+not\s+included"):
459
- return True
460
- if _has_clause(text, r"force\s+majeure\s+clause\s+is\s+included"):
461
- return False
462
  if not _has_clause(
463
  text,
464
  r"force\s+majeure|act\s+of\s+god|unforeseeable|impossibility\s+of\s+performance|causes\s+beyond\s+its\s+reasonable\s+control",
@@ -485,19 +445,14 @@ def check_audit_rights(text: str) -> bool:
485
  """RULE_25: Client must have audit rights over vendor."""
486
  if not _has_clause(text, r"(?:client|company|buyer)"):
487
  return False
 
488
  if _has_clause(text, r"no\s+audit\s+right|audit\s+not\s+permitted|audit\s+rights.*not\s+granted"):
489
  return True
 
490
  if _has_clause(text, r"(?:audit|inspect|examine).*(?:right|access|upon\s+request)"):
491
  return False
 
492
  return True
493
- if not _has_clause(text, r"(?:audit|inspect|examine).*(?:right|access|upon\s+request)"):
494
- return True
495
- if _has_clause(
496
- text,
497
- r"no\s+audit\s+right|audit\s+not\s+permitted|audit\s+rights.*not\s+granted",
498
- ):
499
- return True
500
- return False
501
 
502
 
503
  def check_insurance_requirements(text: str) -> bool:
@@ -538,9 +493,6 @@ def check_subcontractor_approval(text: str) -> bool:
538
  if not _has_clause(text, r"subcontract.*(?:consent|approval|written\s+consent|prior\s+approval)"):
539
  return True
540
  return False
541
- if not _has_clause(text, r"subcontract.*(?:consent|approval|written\s+consent|prior\s+approval)"):
542
- return True
543
- return False
544
 
545
 
546
  def check_export_compliance(text: str) -> bool:
 
60
  """RULE_01: Liability cap must be >= 2x annual contract value."""
61
  if not _has_clause(text, r"liability.*cap|cap.*liability|limited to fees|total liability"):
62
  return False
63
+ # Check for explicit month-based caps (e.g. "preceding 6 months")
64
  cap_months = _extract_days(text, r"(?:preceding|past|previous)\s+(\d+)\s+month")
65
  if cap_months is not None and cap_months < 24:
66
  return True
67
+ # Check for written-out month references (e.g. "preceding six (6) months")
68
+ m = re.search(r"preceding\s+(?:six|three|twelve)\s*\(?\s*(\d+)\s*\)?\s*months?", text, re.IGNORECASE)
69
  if m:
70
+ try:
71
+ months = int(m.group(1))
72
+ return months < 24
73
+ except (IndexError, ValueError):
74
+ return True # If it mentions "six" or "three" months, it's likely a violation
75
+ # Check for unlimited liability (not a violation for this rule)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  if _has_clause(text, r"liability.*unlimited|no.*cap|not.*limited", re.IGNORECASE):
77
  return False
78
  return False
 
84
  if m:
85
  days = int(m.group(1))
86
  return days < 60
87
+ m = re.search(r"within\s+(?:thirty|forty|fifty)\s*\(?\s*(\d+)\s*\)?\s*days?", text, re.IGNORECASE)
88
  if m:
89
  days = int(m.group(1))
90
  return days < 60
 
112
  days = int(m.group(1))
113
  return days < 90
114
  return False
 
 
 
 
 
 
 
 
 
115
 
116
 
117
  def check_governing_law(text: str) -> bool:
 
151
  return True
152
  if _has_clause(text, r"indemnif.*(?:vendor|supplier|contractor).*negligence"):
153
  return True
154
+ # Check for explicit "no reciprocal" language
155
+ if _has_clause(text, r"no\s+reciprocal\s+indemnif|vendor\s+provides\s+no\s+indemnif"):
156
+ return True
157
  return False
158
 
159
 
 
194
  """RULE_09: Limitation of liability clause must be present."""
195
  if _has_clause(
196
  text,
197
+ r"no\s+limitation\s+of\s+liability\s+clause|liability\s+is\s+capped\s+or\s+limited\s+in\s+any\s+way|no\s+limitation\s+of\s+liability",
198
  ):
199
  return True
200
  if not _has_clause(
 
228
  if months is not None:
229
  if months < 12:
230
  return True
231
+ # Also check for broader pattern
232
+ months2 = _extract_number(text, r"warrant.*?(\d+)\s*(?:month|year)")
233
+ if months2 is not None and months2 < 12:
234
  return True
 
 
 
 
 
235
  if _has_clause(text, r"warranty.*(?:not\s+provided|no\s+warranty|warranty\s+not\s+defined)"):
236
  return True
237
  return False
 
255
  """RULE_13: Late payment penalty clause required."""
256
  if not _has_clause(text, r"(?:payment|pay|invoice)"):
257
  return False
258
+ # Check for explicit late payment penalty presence
259
  if _has_clause(
260
  text,
261
  r"late\s+payments?\s+shall\s+accrue\s+interest|late\s+payments?\s+incur\s+interest|overdue\s+amounts?\s+bear\s+interest|late\s+payment\s+penalty",
262
  ):
263
  return False
264
+ # Check for explicit "no penalty" language
265
  if _has_clause(
266
  text, r"no\s+penalty|no\s+late\s+fee|late\s+payments?\s+incur\s+no\s+penalty|no\s+late\s+payment\s+penalty"
267
  ):
268
  return True
269
+ # Check for absence of any late payment penalty
270
  if not _has_clause(
271
  text,
272
  r"late.*penalty|penalty.*late|interest.*late|late.*fee|late.*charge|overdue.*interest",
273
  ):
274
  return True
275
  return False
 
 
 
 
 
 
 
 
276
 
277
 
278
  def check_currency_risk(text: str) -> bool:
 
328
  return True
329
  if _has_clause(
330
  text,
331
+ r"(?:vendor|supplier|contractor).*(?:exclude|except).*(?:consequential|indirect)",
332
  ) and not _has_clause(
333
  text,
334
  r"(?:client|company|buyer).*(?:exclude|except).*(?:consequential|indirect)",
 
358
  """RULE_19: SLA breach penalties/credits required."""
359
  if not _has_clause(text, r"(?:sla|uptime|availability|service\s+level)"):
360
  return False
361
+ # Check for explicit "no penalties" language
362
  if _has_clause(
363
  text,
364
+ r"no\s+(?:credits|penalties|remedies)|no\s+financial\s+penalty|penalties?\s*(?:are\s+)?not\s+defined|no\s+service\s+credits|no\s+remedies\s+are\s+defined",
365
  ):
366
  return True
367
+ # Check for total absence of penalty/credit provisions
368
  if not _has_clause(text, r"(?:credit|penalty|remedy|remedies|service\s+credit|financial)"):
369
  return True
370
  return False
 
 
 
 
 
 
 
 
371
 
372
 
373
  def check_suspension_notice(text: str) -> bool:
 
396
  """RULE_22: Dispute escalation to senior management before litigation."""
397
  if not _has_clause(text, r"dispute|escalat"):
398
  return False
399
+ # Check for explicit escalation procedures
400
  if _has_clause(
401
  text,
402
  r"(?:first\s+be\s+submitted|escalat|senior\s+management|executive).*(?:negotiation|good-faith\s+negotiation).*(?:before\s+pursuing|before\s+litigation|before\s+arbitration|before\s+court)",
 
407
  r"(?:escalat|senior\s+management|executive|negotiation)\s+before|before\s+(?:litigation|arbitration|court)",
408
  ):
409
  return False
410
+ # No escalation found — violation
411
  return True
 
 
 
 
 
 
412
 
413
 
414
  def check_force_majeure(text: str) -> bool:
415
  """RULE_23: Force majeure clause must be present."""
416
+ # Check for explicit "no force majeure" language
417
+ if _has_clause(text, r"no\s+force\s+majeure\s+clause|force\s+majeure\s+clause\s+is\s+not\s+included"):
418
+ return True
419
+ if _has_clause(text, r"force\s+majeure\s+clause\s+is\s+included"):
420
+ return False
421
+ # Check for any force majeure language
 
 
422
  if not _has_clause(
423
  text,
424
  r"force\s+majeure|act\s+of\s+god|unforeseeable|impossibility\s+of\s+performance|causes\s+beyond\s+its\s+reasonable\s+control",
 
445
  """RULE_25: Client must have audit rights over vendor."""
446
  if not _has_clause(text, r"(?:client|company|buyer)"):
447
  return False
448
+ # Check for explicit "no audit rights" language
449
  if _has_clause(text, r"no\s+audit\s+right|audit\s+not\s+permitted|audit\s+rights.*not\s+granted"):
450
  return True
451
+ # Check for presence of audit rights
452
  if _has_clause(text, r"(?:audit|inspect|examine).*(?:right|access|upon\s+request)"):
453
  return False
454
+ # No audit right provisions found — violation
455
  return True
 
 
 
 
 
 
 
 
456
 
457
 
458
  def check_insurance_requirements(text: str) -> bool:
 
493
  if not _has_clause(text, r"subcontract.*(?:consent|approval|written\s+consent|prior\s+approval)"):
494
  return True
495
  return False
 
 
 
496
 
497
 
498
  def check_export_compliance(text: str) -> bool:
server/static/index.html ADDED
@@ -0,0 +1,387 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Procurement Contract Anomaly Auditor — OpenEnv</title>
7
+ <meta name="description" content="AI-powered procurement contract anomaly auditor — a multi-step RL sandbox for deterministic contract review with 30 policy rules.">
8
+ <link rel="preconnect" href="https://fonts.googleapis.com">
9
+ <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
10
+ <style>
11
+ *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
12
+ :root{
13
+ --bg:#1a1a1a;--bg-card:#232323;--bg-hover:#2a2a2a;--bg-input:#1e1e1e;
14
+ --border:#333;--border-light:#3a3a3a;
15
+ --text:#e0e0e0;--text-dim:#888;--text-bright:#fff;
16
+ --green:#4ade80;--green-dim:#22543d;--green-bg:rgba(74,222,128,.08);
17
+ --red:#f87171;--orange:#fb923c;--yellow:#facc15;--blue:#60a5fa;
18
+ --accent:#4ade80;
19
+ --font-sans:'Inter',system-ui,-apple-system,sans-serif;
20
+ --font-mono:'JetBrains Mono','Fira Code',monospace;
21
+ --radius:8px;--radius-lg:12px;
22
+ }
23
+ html{font-size:15px;scroll-behavior:smooth}
24
+ body{font-family:var(--font-sans);background:var(--bg);color:var(--text);min-height:100vh;line-height:1.6}
25
+
26
+ /* ─── Header ─── */
27
+ .header{display:flex;align-items:center;justify-content:space-between;padding:.75rem 1.5rem;border-bottom:1px solid var(--border);background:#1e1e1e;position:sticky;top:0;z-index:100;backdrop-filter:blur(12px)}
28
+ .header-left{display:flex;align-items:center;gap:.75rem}
29
+ .logo{font-family:var(--font-mono);font-weight:700;font-size:1rem;letter-spacing:.08em;color:var(--text-bright)}
30
+ .badge{font-size:.65rem;font-weight:600;padding:.2rem .55rem;border-radius:20px;background:var(--green-bg);color:var(--green);border:1px solid rgba(74,222,128,.25);letter-spacing:.03em}
31
+ .header-links{display:flex;gap:.25rem}
32
+ .header-links a{font-size:.8rem;color:var(--text-dim);text-decoration:none;padding:.4rem .7rem;border-radius:6px;transition:all .15s}
33
+ .header-links a:hover{color:var(--text-bright);background:var(--bg-hover)}
34
+
35
+ /* ─── Hero ─── */
36
+ .hero{padding:2rem 1.5rem 1.25rem;border-bottom:1px solid var(--border)}
37
+ .hero h1{font-size:1.65rem;font-weight:700;color:var(--text-bright);margin-bottom:.35rem}
38
+ .hero p{color:var(--text-dim);font-size:.88rem;max-width:52rem;line-height:1.5}
39
+
40
+ /* ─── Main Grid ─── */
41
+ .main{display:grid;grid-template-columns:1fr 280px;gap:0;min-height:420px}
42
+
43
+ /* ─── Console ─── */
44
+ .console-wrap{padding:1.25rem 1.5rem;border-right:1px solid var(--border)}
45
+ .section-title{display:flex;align-items:center;gap:.5rem;font-size:.8rem;font-weight:600;color:var(--text-dim);margin-bottom:.75rem;text-transform:uppercase;letter-spacing:.06em}
46
+ .section-title .dot{width:7px;height:7px;border-radius:50%;background:var(--green);box-shadow:0 0 6px var(--green)}
47
+ #console{
48
+ background:var(--bg-input);border:1px solid var(--border);border-radius:var(--radius);
49
+ padding:1rem;font-family:var(--font-mono);font-size:.78rem;line-height:1.7;
50
+ color:var(--text-dim);min-height:200px;max-height:340px;overflow-y:auto;
51
+ white-space:pre-wrap;word-break:break-word;
52
+ }
53
+ #console .log-system{color:var(--text-dim)}
54
+ #console .log-info{color:var(--blue)}
55
+ #console .log-success{color:var(--green)}
56
+ #console .log-error{color:var(--red)}
57
+ #console .log-reward{color:var(--yellow)}
58
+ #console .log-step{color:var(--orange)}
59
+
60
+ /* ─── Metrics Panel ─── */
61
+ .metrics-wrap{padding:1.25rem 1.25rem;background:var(--bg-card)}
62
+ .metric-card{background:var(--bg-input);border:1px solid var(--border);border-radius:var(--radius);padding:.7rem .85rem;margin-bottom:.6rem}
63
+ .metric-label{font-size:.7rem;color:var(--text-dim);text-transform:uppercase;letter-spacing:.04em;margin-bottom:.15rem}
64
+ .metric-value{font-size:1.25rem;font-weight:700;color:var(--text-bright);font-family:var(--font-mono)}
65
+ .metric-value.none{color:var(--text-dim)}
66
+ .status-row{display:flex;align-items:center;gap:.5rem;margin-top:.75rem;padding:.5rem .85rem;border-radius:var(--radius);background:var(--bg-input);border:1px solid var(--border)}
67
+ .status-dot{width:8px;height:8px;border-radius:50%;transition:background .3s,box-shadow .3s}
68
+ .status-dot.active{background:var(--green);box-shadow:0 0 8px var(--green)}
69
+ .status-dot.idle{background:var(--text-dim)}
70
+ .status-text{font-size:.78rem;color:var(--text-dim)}
71
+
72
+ /* ─── Buttons ─── */
73
+ .actions{padding:1rem 1.5rem;border-top:1px solid var(--border);border-bottom:1px solid var(--border);display:flex;flex-wrap:wrap;gap:.5rem}
74
+ .btn{
75
+ font-family:var(--font-sans);font-size:.82rem;font-weight:600;
76
+ padding:.55rem 1.1rem;border-radius:var(--radius);border:1px solid var(--border);
77
+ background:var(--bg-card);color:var(--text);cursor:pointer;transition:all .15s;
78
+ display:inline-flex;align-items:center;gap:.4rem;
79
+ }
80
+ .btn:hover{background:var(--bg-hover);border-color:var(--border-light);color:var(--text-bright)}
81
+ .btn:active{transform:scale(.97)}
82
+ .btn:disabled{opacity:.4;cursor:not-allowed;transform:none}
83
+ .btn-primary{background:rgba(74,222,128,.1);border-color:rgba(74,222,128,.25);color:var(--green)}
84
+ .btn-primary:hover{background:rgba(74,222,128,.18);border-color:rgba(74,222,128,.4)}
85
+ .btn.loading::after{content:'';width:12px;height:12px;border:2px solid transparent;border-top-color:currentColor;border-radius:50%;animation:spin .6s linear infinite;margin-left:.3rem}
86
+ @keyframes spin{to{transform:rotate(360deg)}}
87
+
88
+ /* ─── Schema ─── */
89
+ .schema{padding:1.25rem 1.5rem}
90
+ .schema h3{font-size:.85rem;font-weight:600;color:var(--text-bright);margin-bottom:.75rem}
91
+ .endpoint-list{list-style:none}
92
+ .endpoint{display:flex;align-items:center;justify-content:space-between;padding:.45rem 0;border-bottom:1px solid rgba(51,51,51,.5)}
93
+ .endpoint:last-child{border-bottom:none}
94
+ .endpoint-left{display:flex;align-items:center;gap:.6rem}
95
+ .method{font-family:var(--font-mono);font-size:.65rem;font-weight:700;padding:.15rem .45rem;border-radius:4px;min-width:38px;text-align:center}
96
+ .method.post{background:rgba(74,222,128,.12);color:var(--green)}
97
+ .method.get{background:rgba(96,165,250,.12);color:var(--blue)}
98
+ .path{font-family:var(--font-mono);font-size:.8rem;color:var(--text)}
99
+ .endpoint-desc{font-size:.75rem;color:var(--text-dim)}
100
+
101
+ /* ─── Footer ─── */
102
+ .footer{display:flex;justify-content:space-between;align-items:center;padding:.75rem 1.5rem;border-top:1px solid var(--border);font-size:.72rem;color:var(--text-dim)}
103
+
104
+ /* ─── Scrollbar ─── */
105
+ ::-webkit-scrollbar{width:6px}
106
+ ::-webkit-scrollbar-track{background:transparent}
107
+ ::-webkit-scrollbar-thumb{background:#444;border-radius:3px}
108
+ ::-webkit-scrollbar-thumb:hover{background:#555}
109
+
110
+ /* ─── Toast ─── */
111
+ .toast{position:fixed;bottom:1.5rem;right:1.5rem;padding:.65rem 1rem;border-radius:var(--radius);font-size:.8rem;color:var(--text-bright);z-index:200;animation:slideUp .3s ease;box-shadow:0 8px 24px rgba(0,0,0,.4)}
112
+ .toast.success{background:#166534;border:1px solid var(--green)}
113
+ .toast.error{background:#7f1d1d;border:1px solid var(--red)}
114
+ @keyframes slideUp{from{transform:translateY(20px);opacity:0}to{transform:translateY(0);opacity:1}}
115
+
116
+ /* ─── Responsive ─── */
117
+ @media(max-width:768px){
118
+ .main{grid-template-columns:1fr}
119
+ .console-wrap{border-right:none;border-bottom:1px solid var(--border);padding:1rem}
120
+ .metrics-wrap{padding:1rem}
121
+ .metric-card{padding:.6rem .75rem;margin-bottom:.5rem}
122
+ .metric-value{font-size:1.1rem}
123
+ .footer{flex-direction:column;gap:.3rem;text-align:center}
124
+ .actions{justify-content:center;padding:.75rem 1rem}
125
+ .hero{padding:1.5rem 1rem 1rem}
126
+ .hero h1{font-size:1.3rem}
127
+ .hero p{font-size:.82rem}
128
+ .header{padding:.6rem 1rem}
129
+ .header-links a{font-size:.72rem;padding:.35rem .5rem}
130
+ .logo{font-size:.9rem}
131
+ .schema{padding:1rem}
132
+ .schema h3{font-size:.8rem}
133
+ #console{min-height:160px;max-height:260px;font-size:.72rem;padding:.75rem}
134
+ .btn{font-size:.75rem;padding:.45rem .85rem}
135
+ }
136
+ @media(max-width:480px){
137
+ .header{flex-direction:column;gap:.5rem;padding:.6rem .75rem}
138
+ .header-left{width:100%;justify-content:center}
139
+ .header-links{width:100%;justify-content:center;flex-wrap:wrap}
140
+ .hero h1{font-size:1.15rem}
141
+ .actions{gap:.4rem}
142
+ .btn{font-size:.7rem;padding:.4rem .7rem}
143
+ }
144
+ </style>
145
+ </head>
146
+ <body>
147
+
148
+ <!-- Header -->
149
+ <header class="header" id="app-header">
150
+ <div class="header-left">
151
+ <span class="logo">OPENENV</span>
152
+ <span class="badge">Compliant v1.0</span>
153
+ </div>
154
+ <nav class="header-links">
155
+ <a href="/docs" target="_blank" id="link-docs">API Docs</a>
156
+ <a href="/health" target="_blank" id="link-health">Health</a>
157
+ <a href="https://github.com/ronbell1/meta_scaler" target="_blank" rel="noopener" id="link-github">GitHub</a>
158
+ </nav>
159
+ </header>
160
+
161
+ <!-- Hero -->
162
+ <section class="hero" id="hero-section">
163
+ <h1>Procurement Contract Auditor</h1>
164
+ <p>A multi-step RL sandbox for deterministic contract anomaly detection — policy violation analysis, severity grading, and redline generation for legal ops evaluation.</p>
165
+ </section>
166
+
167
+ <!-- Main: Console + Metrics -->
168
+ <div class="main" id="main-content">
169
+ <!-- Console -->
170
+ <div class="console-wrap">
171
+ <div class="section-title"><span class="dot"></span> Evaluation Console</div>
172
+ <div id="console"><span class="log-system">&gt; System initialized. Environment ready.
173
+ &gt; Policy rulebook loaded — 30 rules across 6 categories.
174
+ &gt; Select a difficulty to inject a contract scenario.</span></div>
175
+ </div>
176
+ <!-- Metrics -->
177
+ <div class="metrics-wrap">
178
+ <div class="section-title">Environment Metrics</div>
179
+ <div class="metric-card">
180
+ <div class="metric-label">Current Task</div>
181
+ <div class="metric-value none" id="m-task">None</div>
182
+ </div>
183
+ <div class="metric-card">
184
+ <div class="metric-label">Step Count</div>
185
+ <div class="metric-value" id="m-steps">0 / 0</div>
186
+ </div>
187
+ <div class="metric-card">
188
+ <div class="metric-label">Cumulative Reward</div>
189
+ <div class="metric-value" id="m-reward">0.00</div>
190
+ </div>
191
+ <div class="metric-card">
192
+ <div class="metric-label">Violations Found</div>
193
+ <div class="metric-value" id="m-violations">0</div>
194
+ </div>
195
+ <div class="status-row">
196
+ <span class="status-dot idle" id="status-dot"></span>
197
+ <span class="status-text" id="status-text">Environment Idle</span>
198
+ </div>
199
+ </div>
200
+ </div>
201
+
202
+ <!-- Action Buttons -->
203
+ <div class="actions" id="actions-bar">
204
+ <button class="btn btn-primary" id="btn-step" disabled onclick="doStep()">Step</button>
205
+ <button class="btn" id="btn-easy" onclick="doReset('easy')">Audit Easy Contract</button>
206
+ <button class="btn" id="btn-medium" onclick="doReset('medium')">Audit Medium Contract</button>
207
+ <button class="btn" id="btn-hard" onclick="doReset('hard')">Audit Hard Package</button>
208
+ </div>
209
+
210
+ <!-- Core Schema -->
211
+ <section class="schema" id="schema-section">
212
+ <h3>Core Schema</h3>
213
+ <ul class="endpoint-list">
214
+ <li class="endpoint"><span class="endpoint-left"><span class="method post">POST</span><span class="path">/reset</span></span><span class="endpoint-desc">Initialize environment</span></li>
215
+ <li class="endpoint"><span class="endpoint-left"><span class="method post">POST</span><span class="path">/step</span></span><span class="endpoint-desc">Execute audit action</span></li>
216
+ <li class="endpoint"><span class="endpoint-left"><span class="method get">GET</span><span class="path">/state</span></span><span class="endpoint-desc">Observability</span></li>
217
+ <li class="endpoint"><span class="endpoint-left"><span class="method get">GET</span><span class="path">/tasks</span></span><span class="endpoint-desc">List tasks</span></li>
218
+ <li class="endpoint"><span class="endpoint-left"><span class="method post">POST</span><span class="path">/submit</span></span><span class="endpoint-desc">Submit custom contract</span></li>
219
+ <li class="endpoint"><span class="endpoint-left"><span class="method get">GET</span><span class="path">/health</span></span><span class="endpoint-desc">Service health</span></li>
220
+ </ul>
221
+ </section>
222
+
223
+ <!-- Footer -->
224
+ <footer class="footer" id="app-footer">
225
+ <span>&copy; 2026 OpenEnv Protocol &bull; Built with FastAPI</span>
226
+ <span>Procurement Contract Anomaly Auditor</span>
227
+ </footer>
228
+
229
+ <script>
230
+ const BASE = window.location.origin;
231
+ const con = document.getElementById('console');
232
+ let sessionId = null;
233
+ let isDone = false;
234
+
235
+ function log(msg, cls = 'log-system') {
236
+ const span = document.createElement('span');
237
+ span.className = cls;
238
+ span.textContent = '\n> ' + msg;
239
+ con.appendChild(span);
240
+ con.scrollTop = con.scrollHeight;
241
+ }
242
+
243
+ function setStatus(active) {
244
+ const dot = document.getElementById('status-dot');
245
+ const txt = document.getElementById('status-text');
246
+ dot.className = 'status-dot ' + (active ? 'active' : 'idle');
247
+ txt.textContent = active ? 'Environment Active' : 'Environment Idle';
248
+ }
249
+
250
+ function updateMetrics(data) {
251
+ const task = document.getElementById('m-task');
252
+ const steps = document.getElementById('m-steps');
253
+ const reward = document.getElementById('m-reward');
254
+ const violations = document.getElementById('m-violations');
255
+
256
+ if (data.task_id) {
257
+ task.textContent = data.task_id.charAt(0).toUpperCase() + data.task_id.slice(1);
258
+ task.classList.remove('none');
259
+ }
260
+ if (data.step !== undefined && data.max_steps !== undefined) {
261
+ steps.textContent = data.step + ' / ' + data.max_steps;
262
+ }
263
+ if (data.cumulative_reward !== undefined) {
264
+ reward.textContent = parseFloat(data.cumulative_reward).toFixed(2);
265
+ }
266
+ if (data.violations_count !== undefined) {
267
+ violations.textContent = data.violations_count;
268
+ }
269
+ }
270
+
271
+ function setLoading(btn, loading) {
272
+ if (loading) {
273
+ btn.classList.add('loading');
274
+ btn.disabled = true;
275
+ } else {
276
+ btn.classList.remove('loading');
277
+ btn.disabled = false;
278
+ }
279
+ }
280
+
281
+ function toast(msg, type = 'success') {
282
+ const t = document.createElement('div');
283
+ t.className = 'toast ' + type;
284
+ t.textContent = msg;
285
+ document.body.appendChild(t);
286
+ setTimeout(() => t.remove(), 3500);
287
+ }
288
+
289
+ async function doReset(taskId) {
290
+ const btn = event.target;
291
+ setLoading(btn, true);
292
+ isDone = false;
293
+
294
+ log('Resetting environment with task: ' + taskId + '...', 'log-info');
295
+
296
+ try {
297
+ const resp = await fetch(BASE + '/dashboard/reset', {
298
+ method: 'POST',
299
+ headers: { 'Content-Type': 'application/json' },
300
+ body: JSON.stringify({ task_id: taskId })
301
+ });
302
+ const data = await resp.json();
303
+
304
+ if (data.error) {
305
+ log('ERROR: ' + data.error, 'log-error');
306
+ toast(data.error, 'error');
307
+ return;
308
+ }
309
+
310
+ sessionId = data.session_id;
311
+ log('Contract loaded — ' + data.task_id + ' difficulty', 'log-success');
312
+ log('Rules to check: ' + data.rules_count, 'log-system');
313
+ log('Max steps: ' + data.max_steps, 'log-system');
314
+ log('Contract length: ' + data.contract_length + ' chars', 'log-system');
315
+
316
+ updateMetrics({
317
+ task_id: data.task_id,
318
+ step: 0,
319
+ max_steps: data.max_steps,
320
+ cumulative_reward: 0,
321
+ violations_count: 0,
322
+ });
323
+ setStatus(true);
324
+ document.getElementById('btn-step').disabled = false;
325
+ toast('Environment reset — ' + taskId + ' task loaded');
326
+
327
+ } catch (e) {
328
+ log('Network error: ' + e.message, 'log-error');
329
+ toast('Connection failed', 'error');
330
+ } finally {
331
+ setLoading(btn, false);
332
+ }
333
+ }
334
+
335
+ async function doStep() {
336
+ if (!sessionId || isDone) return;
337
+ const btn = document.getElementById('btn-step');
338
+ setLoading(btn, true);
339
+
340
+ log('Executing step...', 'log-step');
341
+
342
+ try {
343
+ const resp = await fetch(BASE + '/dashboard/step', {
344
+ method: 'POST',
345
+ headers: { 'Content-Type': 'application/json' },
346
+ body: JSON.stringify({ session_id: sessionId })
347
+ });
348
+ const data = await resp.json();
349
+
350
+ if (data.error) {
351
+ log('ERROR: ' + data.error, 'log-error');
352
+ return;
353
+ }
354
+
355
+ log('Step ' + data.step + ': reward=' + parseFloat(data.reward).toFixed(4) +
356
+ ' violations=' + data.violations_count +
357
+ (data.done ? ' [DONE]' : ''), data.reward > 0 ? 'log-reward' : 'log-system');
358
+
359
+ if (data.feedback) {
360
+ log('Feedback: ' + data.feedback, 'log-info');
361
+ }
362
+
363
+ updateMetrics({
364
+ task_id: data.task_id,
365
+ step: data.step,
366
+ max_steps: data.max_steps,
367
+ cumulative_reward: data.cumulative_reward,
368
+ violations_count: data.violations_count,
369
+ });
370
+
371
+ if (data.done) {
372
+ isDone = true;
373
+ log('Episode complete. Final score: ' + parseFloat(data.cumulative_reward).toFixed(4), 'log-success');
374
+ btn.disabled = true;
375
+ setStatus(false);
376
+ toast('Episode complete — score: ' + parseFloat(data.cumulative_reward).toFixed(4));
377
+ }
378
+
379
+ } catch (e) {
380
+ log('Network error: ' + e.message, 'log-error');
381
+ } finally {
382
+ if (!isDone) setLoading(btn, false);
383
+ }
384
+ }
385
+ </script>
386
+ </body>
387
+ </html>
server/tasks.py CHANGED
@@ -96,7 +96,7 @@ TASKS: Dict[str, TaskConfig] = {
96
  "lawyer-written gold redline."
97
  ),
98
  contract_type="MSA",
99
- violations=["RULE_01", "RULE_06", "RULE_09"],
100
  cross_doc_violations=[
101
  ("MSA", "DPA", "RULE_18"),
102
  ],
 
96
  "lawyer-written gold redline."
97
  ),
98
  contract_type="MSA",
99
+ violations=["RULE_01", "RULE_06", "RULE_07", "RULE_08", "RULE_09", "RULE_11"],
100
  cross_doc_violations=[
101
  ("MSA", "DPA", "RULE_18"),
102
  ],
test_api.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Quick API connection test."""
2
+ from pathlib import Path
3
+ from dotenv import load_dotenv
4
+
5
+ load_dotenv(Path(__file__).resolve().parent / ".env")
6
+
7
+ import os
8
+
9
+ print("API_BASE_URL:", os.environ.get("API_BASE_URL", "NOT SET"))
10
+ print("MODEL_NAME:", os.environ.get("MODEL_NAME", "NOT SET"))
11
+ token = os.environ.get("HF_TOKEN", "")
12
+ print("HF_TOKEN:", token[:10] + "..." if token else "NOT SET")
13
+ print()
14
+
15
+ print("Testing API connection...")
16
+ from openai import OpenAI
17
+
18
+ client = OpenAI(
19
+ base_url=os.environ["API_BASE_URL"],
20
+ api_key=os.environ["HF_TOKEN"],
21
+ timeout=30.0,
22
+ )
23
+ try:
24
+ resp = client.chat.completions.create(
25
+ model=os.environ["MODEL_NAME"],
26
+ messages=[{"role": "user", "content": "Say hello in one word."}],
27
+ max_tokens=10,
28
+ )
29
+ print("SUCCESS:", resp.choices[0].message.content)
30
+ except Exception as e:
31
+ print(f"FAILED: {type(e).__name__}: {e}")