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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
431
"""
Inference Script for AdAudit
===================================
MANDATORY
- Before submitting, ensure the following variables are defined in your environment configuration:
    API_BASE_URL   The API endpoint for the LLM.
    MODEL_NAME     The model identifier to use for inference.
    HF_TOKEN       Your Hugging Face / API key.
    LOCAL_IMAGE_NAME The name of the local image to use for the environment if you are using from_docker_image()
                     method

- Defaults are set only for API_BASE_URL and MODEL_NAME
    (and should reflect your active inference setup):
    API_BASE_URL = os.getenv("API_BASE_URL", "<your-active-endpoint>")
    MODEL_NAME = os.getenv("MODEL_NAME", "<your-active-model>")

- The inference script must be named `inference.py` and placed in the root directory of the project
- Participants must use OpenAI Client for all LLM calls using above variables

STDOUT FORMAT
- The script must emit exactly three line types to stdout, in this order:

    [START] task=<task_name> env=<benchmark> model=<model_name>
    [STEP]  step=<n> action=<action_str> reward=<0.00> done=<true|false> error=<msg|null>
    [END]   success=<true|false> steps=<n> score=<score> rewards=<r1,r2,...,rn>

  Rules:
    - One [START] line at episode begin.
    - One [STEP] line per step, immediately after env.step() returns.
    - One [END] line after env.close(), always emitted (even on exception).
    - reward and rewards are formatted to 2 decimal places.
    - done and success are lowercase booleans: true or false.
    - error is the raw last_action_error string, or null if none.
    - All fields on a single line with no newlines within a line.
    - Each tasks should return score in [0, 1]

  Example:
    [START] task=click-test env=miniwob model=Qwen3-VL-30B
    [STEP] step=1 action=click('123') reward=0.00 done=false error=null
    [STEP] step=2 action=fill('456','text') reward=0.00 done=false error=null
    [STEP] step=3 action=click('789') reward=1.00 done=true error=null
    [END] success=true steps=3 score=1.00 rewards=0.00,0.00,1.00
"""

import json
import os
import re
from typing import Any, Dict, List, Optional, Tuple

from openai import OpenAI

from server.Ad_Audit_environment import AdAuditEnv, EPISODE_DAYS
from models import AdAuditAction

# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------

IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME")
API_KEY = os.getenv("HF_TOKEN") or os.getenv("API_KEY")

API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")

_VALID_TASKS = {"easy", "medium", "hard"}
_task_env = os.getenv("ADAUDIT_TASK", "").strip().lower()
TASK_NAME = _task_env if _task_env in _VALID_TASKS else "medium"
BENCHMARK = os.getenv("ADAUDIT_BENCHMARK", "adaudit")
TEMPERATURE = 0.0
MAX_TOKENS = 2048
HISTORY_WINDOW = 5
BASELINE_DAYS = 3
SUCCESS_SCORE_THRESHOLD = 0.4

# Rule-based investigation tools per fraud type
TOOLS_FOR = {
    "bot_traffic": ["click_timestamps", "ip_distribution"],
    "click_injection": ["conversion_quality", "click_timestamps"],
    "domain_spoofing": ["referral_urls", "viewability_scores"],
}

SYSTEM_PROMPT = """\
You are an ad fraud analyst monitoring a 14-day campaign with multiple publishers.
Each day you see metrics (impressions, clicks, conversions, CTR, CVR) and take ONE action.

## ACTIONS

Use publisher IDs (pub_001, pub_002, etc.), never publisher names.

1. Monitor — observe metrics, take no action:
   {"action_type": "monitor"}
2. Investigate — run one tool on one publisher (costs 1 budget):
   {"action_type": "investigate_publisher", "publisher_id": "pub_001", "tool": "click_timestamps"}
3. Flag fraud — flag a publisher as fraudulent (irreversible):
   {"action_type": "flag_fraud", "publisher_id": "pub_001", "fraud_type": "bot_traffic", "evidence": ["click_timestamps", "ip_distribution"]}
4. Submit report — end the episode:
   {"action_type": "submit_report", "summary": "..."}

Valid tools: click_timestamps, ip_distribution, device_fingerprints, \
referral_urls, viewability_scores, conversion_quality
Valid fraud types: bot_traffic, domain_spoofing, click_injection

## STRATEGY
1. Monitor days 1-3 to establish baselines. Then compare each publisher's CTR/CVR against its own baseline. Only publishers with BIG shifts (CTR doubled, CVR crashed, impressions tripled) are suspicious. Stable metrics = clean.
2. Investigate suspicious publishers with 2 tools before flagging. bot_traffic = CTR spikes + CVR near zero. click_injection = CVR abnormally high (5%+). domain_spoofing = impressions surge + CVR drops.
3. False positives are heavily penalized. When in doubt, monitor. 
4. Do not submit report until you are confident you have found and flagged ALL fraudsters.

Respond with ONLY a JSON action in ```json``` markers.
"""


# ---------------------------------------------------------------------------
# Logging helpers
# ---------------------------------------------------------------------------

def log_start(task: str, env: str, model: str) -> None:
    print(f"[START] task={task} env={env} model={model}", flush=True)


def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
    error_val = error if error else "null"
    done_val = str(done).lower()
    print(f"[STEP] step={step} action={action} reward={reward:.2f} done={done_val} error={error_val}", flush=True)


def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
    rewards_str = ",".join(f"{r:.2f}" for r in rewards)
    print(f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_str}", flush=True)


# ---------------------------------------------------------------------------
# Observation formatting (for LLM)
# ---------------------------------------------------------------------------

def format_observation(obs_dict: Dict[str, Any], action_log: List[str] = None) -> str:
    parts: List[str] = []
    parts.append(f"=== DAY {obs_dict['day']} of {obs_dict.get('campaign_day_total', 14)} ===\n")

    if action_log:
        parts.append("Past actions: " + " | ".join(action_log))
        parts.append("")

    if obs_dict.get("daily_metrics"):
        parts.append("Metrics:")
        parts.append(f"{'ID':<10} {'Publisher':<22} {'Impressions':>12} {'Clicks':>8} {'Conversions':>12} {'Spend ($)':>10} {'CTR':>7} {'CVR':>7}")
        for m in obs_dict["daily_metrics"]:
            parts.append(
                f"{m['publisher_id']:<10} {m['name']:<22} {m['impressions']:>12,} {m['clicks']:>8,} "
                f"{m['conversions']:>12,} {m['spend']:>10,.2f} {m['ctr']:>6.2%} {m['cvr']:>6.2%}"
            )
        parts.append("")

    if obs_dict.get("investigation_results"):
        inv = obs_dict["investigation_results"]
        if isinstance(inv, dict):
            if "error" in inv:
                parts.append(f"Investigation ERROR: {inv['error']}")
            else:
                parts.append(f"Investigation ({inv.get('tool', '?')}) for {inv.get('publisher_id', '?')}:")
                for k, v in inv.items():
                    if k not in ("tool", "publisher_id"):
                        parts.append(f"  {k}: {v}")
        parts.append("")

    pub_status = obs_dict.get("publisher_status", {})
    if pub_status:
        flagged_pubs = [pid for pid, s in pub_status.items() if s == "flagged"]
        active = [pid for pid, s in pub_status.items() if s == "active"]
        if flagged_pubs:
            parts.append(f"FLAGGED: {', '.join(flagged_pubs)}")
        parts.append(f"Active publishers: {', '.join(active)}")

    b = obs_dict.get("budget_status", {})
    if b:
        parts.append(
            f"Budget: ${b.get('remaining',0):,.0f} remaining | "
            f"Investigations left: {b.get('investigation_budget_remaining', 0)}"
        )
    return "\n".join(parts)


# ---------------------------------------------------------------------------
# Action parsing (for LLM)
# ---------------------------------------------------------------------------

def parse_action(text: str) -> Dict[str, Any]:
    match = re.search(r"```json\s*(\{.*?\})\s*```", text, re.DOTALL)
    if match:
        try:
            return json.loads(match.group(1))
        except json.JSONDecodeError:
            pass
    for line in reversed(text.strip().split("\n")):
        line = line.strip()
        if line.startswith("{") and line.endswith("}"):
            try:
                return json.loads(line)
            except json.JSONDecodeError:
                continue
    return {"action_type": "monitor"}


# ---------------------------------------------------------------------------
# LLM action
# ---------------------------------------------------------------------------

def get_llm_action(
    client: OpenAI,
    obs_text: str,
    history: List[Dict[str, str]],
) -> Optional[Dict[str, Any]]:
    """Try to get an action from the LLM. Returns None on failure."""
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    messages.extend(history)
    messages.append({"role": "user", "content": obs_text})

    try:
        response = client.chat.completions.create(
            model=MODEL_NAME,
            messages=messages,
            temperature=TEMPERATURE,
            max_tokens=MAX_TOKENS,
        )
        text = response.choices[0].message.content or ""
        return parse_action(text)
    except Exception:
        return None


# ---------------------------------------------------------------------------
# Rule-based fallback
# ---------------------------------------------------------------------------

def score_publishers(metrics_history: Dict[str, List[Dict]]) -> List[Tuple[str, float, str]]:
    """Score each publisher's suspicion based on accumulated metrics."""
    results = []
    for pid, hist in metrics_history.items():
        if len(hist) < 2:
            continue
        recent = hist[-3:]
        avg_ctr = sum(h["ctr"] for h in recent) / len(recent)
        avg_cvr = sum(h["cvr"] for h in recent) / len(recent)
        baseline = hist[:2]
        base_ctr = sum(h["ctr"] for h in baseline) / len(baseline)
        base_cvr = sum(h["cvr"] for h in baseline) / len(baseline)

        score = 0.0
        fraud_type = "bot_traffic"

        if base_ctr > 0:
            ctr_rise = avg_ctr / base_ctr
            if ctr_rise > 1.5:
                score += 0.4
            if ctr_rise > 2.0:
                score += 0.3
        if base_cvr > 0:
            cvr_drop = avg_cvr / base_cvr
            if cvr_drop < 0.5:
                score += 0.3
            if cvr_drop < 0.2:
                score += 0.3
        if avg_cvr > 0.04:
            score += 0.5
            fraud_type = "click_injection"
        if base_ctr > 0 and base_cvr > 0:
            ctr_rise = avg_ctr / base_ctr
            cvr_drop = avg_cvr / base_cvr
            if 1.2 < ctr_rise < 2.0 and 0.2 < cvr_drop < 0.6:
                if fraud_type == "bot_traffic" and score < 0.5:
                    fraud_type = "domain_spoofing"

        if score > 0.2:
            results.append((pid, score, fraud_type))

    results.sort(key=lambda x: -x[1])
    return results


def get_rule_action(
    obs_dict: Dict[str, Any],
    metrics_history: Dict[str, List[Dict]],
    investigated: Dict[str, List[str]],
    flagged: set,
) -> Dict[str, Any]:
    """Deterministic rule-based action selection."""
    day = obs_dict["day"]
    budget_left = obs_dict.get("budget_status", {}).get("investigation_budget_remaining", 0)

    if day <= BASELINE_DAYS:
        return {"action_type": "monitor"}

    suspects = score_publishers(metrics_history)
    suspects = [(pid, sc, ft) for pid, sc, ft in suspects if pid not in flagged]

    for pid, _, ft in suspects:
        tools_done = investigated.get(pid, [])
        if len(tools_done) >= 2:
            flagged.add(pid)
            return {
                "action_type": "flag_fraud",
                "publisher_id": pid,
                "fraud_type": ft,
                "evidence": tools_done,
            }

    if budget_left > 0 and suspects:
        for pid, _, ft in suspects:
            if pid in flagged:
                continue
            tools_done = investigated.get(pid, [])
            tools_to_try = TOOLS_FOR.get(ft, TOOLS_FOR["bot_traffic"])
            for tool in tools_to_try:
                if tool not in tools_done:
                    investigated.setdefault(pid, []).append(tool)
                    return {
                        "action_type": "investigate_publisher",
                        "publisher_id": pid,
                        "tool": tool,
                    }

    return {"action_type": "monitor"}


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

def run_episode(task_name: str, llm_client: Optional[OpenAI]) -> None:
    use_rules = llm_client is None

    env = AdAuditEnv()

    rewards: List[float] = []
    steps_taken = 0
    score = 0.0
    success = False

    # LLM state
    history: List[Dict[str, str]] = []
    action_log: List[str] = []

    # Rule-based state
    metrics_history: Dict[str, List[Dict]] = {}
    investigated: Dict[str, List[str]] = {}
    flagged: set = set()

    log_start(task=task_name, env=BENCHMARK, model=MODEL_NAME if not use_rules else "rule-based")

    try:
        obs = env.reset(episode_id=task_name)
        obs_dict = obs.model_dump()

        while not obs_dict.get("done", False) and steps_taken < EPISODE_DAYS:
            # Track metrics for rule-based fallback
            for m in obs_dict.get("daily_metrics", []):
                metrics_history.setdefault(m["publisher_id"], []).append(m)

            action = None

            if not use_rules:
                obs_text = format_observation(obs_dict, action_log)
                action = get_llm_action(llm_client, obs_text, history[-HISTORY_WINDOW * 2:])
                if action is None:
                    use_rules = True

            if action is None:
                action = get_rule_action(obs_dict, metrics_history, investigated, flagged)

            # Validate action
            try:
                action_obj = AdAuditAction(**action)
            except Exception:
                action_obj = AdAuditAction(action_type="invalid")

            # Build action log entry
            log_entry = f"D{obs_dict['day']}:{action_obj.action_type}"
            if action_obj.publisher_id:
                log_entry += f"({action_obj.publisher_id}"
                if action_obj.tool:
                    log_entry += f",{action_obj.tool}"
                if action_obj.fraud_type:
                    log_entry += f",{action_obj.fraud_type}"
                log_entry += ")"
            action_log.append(log_entry)

            # Update LLM history
            if not use_rules:
                history.append({"role": "user", "content": obs_text})
                history.append({"role": "assistant", "content": json.dumps(action)})

            # Step environment
            obs = env.step(action_obj)
            obs_dict = obs.model_dump()
            steps_taken += 1

            reward = obs_dict.get("reward", 0.0)
            done = obs_dict.get("done", False)
            error = None
            rewards.append(reward)

            action_str = json.dumps(action, separators=(",", ":"))
            log_step(step=steps_taken, action=action_str, reward=reward, done=done, error=error)

        # Final grading
        state = env.state
        grader = state.grader_inputs
        score = grader.get("final_score", 0.0)
        score = min(max(score, 0.0), 1.0)
        success = score >= SUCCESS_SCORE_THRESHOLD

    finally:
        log_end(success=success, steps=steps_taken, score=score, rewards=rewards)


def main() -> None:
    # Try to init LLM client; fall back to rule-based if it fails
    llm_client: Optional[OpenAI] = None
    try:
        llm_client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
        llm_client.models.list()
    except Exception:
        llm_client = None

    for task in sorted(_VALID_TASKS):
        run_episode(task, llm_client)


if __name__ == "__main__":
    main()