File size: 13,356 Bytes
4c76730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f75f0
4c76730
 
52f75f0
 
4c76730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f75f0
4c76730
 
 
 
 
 
 
 
 
 
 
 
 
 
52f75f0
4c76730
52f75f0
4c76730
 
 
 
 
 
 
 
 
 
 
 
 
52f75f0
4c76730
 
 
52f75f0
 
4c76730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1972eae
4c76730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
inference.py β€” Baseline Inference Script for LogTriageEnv
==========================================================
MANDATORY environment variables:
    API_BASE_URL   The API endpoint for the LLM
                   (default: https://router.huggingface.co/v1)
    MODEL_NAME     The model identifier to use for inference
    HF_TOKEN       Your Hugging Face / API key

Usage:
    # Set environment variables
    $env:API_BASE_URL="https://api.groq.com/openai/v1"   # or HF router
    $env:MODEL_NAME="llama-3.3-70b-versatile"             # or any model
    $env:HF_TOKEN="your-api-key-here"

    python inference.py

Runtime: < 20 minutes on vcpu=2, memory=8gb
"""
from __future__ import annotations
import os
import json
import time
import requests
from openai import OpenAI

# ─── MANDATORY ENV VARIABLES (as required by hackathon spec) ──────────────────

API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Llama-3.3-70B-Instruct")
API_KEY = os.getenv("HF_TOKEN") or os.getenv("GROQ_API_KEY")  # HF_TOKEN is primary

# ─── ENVIRONMENT CONFIG ───────────────────────────────────────────────────────

ENV_URL = os.getenv("ENV_URL", "http://localhost:7860")
TASKS = ["single_crash", "cascading_failure", "silent_degradation"]
MAX_STEPS_PER_TASK = {
    "single_crash": 8,
    "cascading_failure": 12,
    "silent_degradation": 15,
}
SEED = 42  # fixed seed for reproducibility

# ─── SYSTEM PROMPT ─────────────────────────────────────────────────────────────

SYSTEM_PROMPT = """You are an expert Site Reliability Engineer (SRE) performing incident triage.
You will receive log lines from a microservice cluster and must diagnose and resolve the incident.

Available services: api-gateway, auth-service, user-db, payment-service, payment-db, notification-service, email-queue
Available teams: sre-team, backend-team, dba-team, security-team

You must respond with ONLY a valid JSON object in this exact format:
{
  "action_type": "<one of: classify_severity, identify_root_cause, escalate, remediate, request_more_logs, resolve, ignore>",
  "value": "<depends on action_type>",
  "confidence": <float 0.0-1.0>,
  "reasoning": "<brief explanation>"
}

Value rules by action_type:
- classify_severity: value must be "P1", "P2", or "P3"
- identify_root_cause: value must be a service name from the list above
- escalate: value must be a team name from the list above
- remediate: value must be "restart:<service>", "rollback:<service>", "scale:<service>", "flush-cache:<service>", or "kill-query:<service>"
- request_more_logs: value must be a service name or "all"
- resolve: value must be "resolved"
- ignore: value must be "noise"

Severity classification rules:
- P1: service DOWN or error rate > 5% β€” immediate customer impact
- P2: degraded performance, trending toward P1 β€” no outage yet
- P3: warning only, no immediate impact

Strategy:
1. Read all log lines carefully β€” identify ERROR and FATAL lines first
2. Check system_state for each service (error_rate, latency_p99_ms, status)
3. Find the ROOT CAUSE service (where the problem STARTED, not where it SPREAD)
4. Classify severity based on actual current impact
5. Apply fix to ROOT CAUSE service, not symptom services
6. After classify + identify + remediate β€” call resolve

IMPORTANT: Respond with ONLY the JSON object. No explanation, no markdown, no backticks."""


def _build_user_prompt(obs: dict) -> str:
    """Convert observation dict into LLM prompt."""
    lines = []

    # System state β€” only show services with issues
    lines.append("=== SYSTEM STATE ===")
    shown_any = False
    for svc, status in obs.get("system_state", {}).items():
        if isinstance(status, dict):
            s = status.get("status", "unknown")
            er = status.get("error_rate", 0)
            lat = status.get("latency_p99_ms", 0)
            if s != "up" or er > 0.01 or lat > 200:
                lines.append(f"  {svc}: status={s} | error_rate={er:.1%} | latency_p99={lat}ms")
                shown_any = True
    if not shown_any:
        lines.append("  All services appear healthy")
    lines.append("")

    # Active alerts
    alerts = obs.get("active_alerts", [])
    if alerts:
        lines.append("=== ACTIVE ALERTS ===")
        for alert in alerts:
            lines.append(f"  ⚠ {alert}")
        lines.append("")

    # Log lines β€” show all of them
    lines.append("=== LOG LINES ===")
    for log in obs.get("logs", []):
        if isinstance(log, dict):
            ts = log.get("timestamp", "")[-8:]
            level = log.get("level", "INFO")
            svc = log.get("service", "unknown")
            msg = log.get("message", "")
            lines.append(f"  [{ts}] {level:<5} {svc:<25} {msg}")
    lines.append("")

    # Context
    step = obs.get("step_count", 0)
    task = obs.get("task_id", "")
    elapsed = obs.get("time_elapsed_seconds", 0)
    lines.append(f"Step: {step} | Task: {task} | Time elapsed: {elapsed}s")

    # Feedback from last action
    feedback = obs.get("last_action_feedback", "")
    if feedback and "Incident detected" not in feedback:
        lines.append(f"Last feedback: {feedback}")

    lines.append("")
    lines.append("Respond with JSON only.")
    return "\n".join(lines)


def _parse_action(response_text: str) -> dict | None:
    """Parse LLM response into action dict."""
    text = response_text.strip()

    # Strip markdown code blocks
    if text.startswith("```"):
        lines = text.split("\n")
        text = "\n".join(lines[1:-1] if lines[-1].strip() == "```" else lines[1:])

    try:
        action = json.loads(text)
        if "action_type" not in action or "value" not in action:
            return None
        action.setdefault("confidence", 0.8)
        action.setdefault("reasoning", "")
        return action
    except json.JSONDecodeError:
        import re
        match = re.search(r'\{[^{}]+\}', text, re.DOTALL)
        if match:
            try:
                return json.loads(match.group())
            except json.JSONDecodeError:
                return None
        return None


def _get_fallback_action(obs: dict, step: int, actions_taken: list) -> dict:
    """Fallback when LLM fails β€” use simple heuristics."""
    system_state = obs.get("system_state", {})

    # Find worst service
    worst_service = "payment-service"
    worst_error_rate = 0
    for svc, status in system_state.items():
        if isinstance(status, dict):
            er = status.get("error_rate", 0)
            if er > worst_error_rate:
                worst_error_rate = er
                worst_service = svc

    action_types_taken = [a.get("action_type") for a in actions_taken]

    if "classify_severity" not in action_types_taken:
        return {"action_type": "classify_severity", "value": "P1",
                "confidence": 0.5, "reasoning": "fallback"}
    elif "identify_root_cause" not in action_types_taken:
        return {"action_type": "identify_root_cause", "value": worst_service,
                "confidence": 0.5, "reasoning": "fallback"}
    elif "remediate" not in action_types_taken:
        return {"action_type": "remediate", "value": f"restart:{worst_service}",
                "confidence": 0.5, "reasoning": "fallback"}
    else:
        return {"action_type": "resolve", "value": "resolved",
                "confidence": 0.5, "reasoning": "fallback"}


def run_task(client: OpenAI, task_id: str, seed: int = 42) -> dict:
    """Run one complete episode for a task. Returns score + breakdown."""
    # Reset
    try:
        resp = requests.post(
            f"{ENV_URL}/reset",
            params={"task": task_id, "seed": seed},
            timeout=30
        )
        resp.raise_for_status()
        obs = resp.json()
    except Exception as e:
        print(f"[ERROR] reset task={task_id} error={e}", flush=True)
        return {"score": 0.0, "error": str(e), "task_id": task_id}

    print(f"[START] task={task_id}", flush=True)

    max_steps = MAX_STEPS_PER_TASK.get(task_id, 10)
    conversation_history = []
    actions_taken = []
    done = obs.get("done", False)
    steps_taken = 0

    while not done and steps_taken < max_steps:
        user_prompt = _build_user_prompt(obs)
        conversation_history.append({"role": "user", "content": user_prompt})

        # Keep conversation history bounded
        if len(conversation_history) > 8:
            conversation_history = conversation_history[-8:]

        # Call LLM
        try:
            response = client.chat.completions.create(
                model=MODEL_NAME,
                messages=[
                    {"role": "system", "content": SYSTEM_PROMPT},
                ] + conversation_history,
                max_tokens=200,
                temperature=0,
            )
            response_text = response.choices[0].message.content or ""
            conversation_history.append({"role": "assistant", "content": response_text})
            action = _parse_action(response_text)
            if action is None:
                action = _get_fallback_action(obs, steps_taken, actions_taken)
        except Exception as e:
            print(f"[ERROR] step={steps_taken + 1} llm_error={e}", flush=True)
            action = _get_fallback_action(obs, steps_taken, actions_taken)

        # Step environment
        try:
            step_resp = requests.post(
                f"{ENV_URL}/step",
                json=action,
                timeout=30
            )
            step_resp.raise_for_status()
            obs = step_resp.json()
            done = obs.get("done", False)
            reward = obs.get("reward", 0.0)
            actions_taken.append(action)
            print(f"[STEP] step={steps_taken + 1} reward={reward:.4f}", flush=True)
        except Exception as e:
            print(f"[ERROR] step={steps_taken + 1} env_error={e}", flush=True)
            break

        steps_taken += 1
        time.sleep(0.2)  # avoid rate limits

    # Get grader score
    try:
        grader_resp = requests.post(f"{ENV_URL}/grader", timeout=30)
        grader_resp.raise_for_status()
        grader_result = grader_resp.json()
        score = grader_result.get("score", 0.0)
        breakdown = grader_result.get("breakdown", {})
    except Exception as e:
        print(f"[ERROR] grader task={task_id} error={e}", flush=True)
        score = obs.get("cumulative_score", 0.0)
        breakdown = {}

    print(f"[INFO] Score: {score:.4f} ({steps_taken} steps)", flush=True)
    print(f"[END] task={task_id} score={score:.4f} steps={steps_taken}", flush=True)
    return {
        "task_id": task_id,
        "score": score,
        "steps_taken": steps_taken,
        "breakdown": breakdown,
    }


def main():
    """Run baseline agent on all 3 tasks and report scores."""

    # Validate env vars
    if not API_KEY:
        raise ValueError(
            "API key not found. Set HF_TOKEN environment variable:\n"
            "  PowerShell: $env:HF_TOKEN='your-key'\n"
            "  CMD:        set HF_TOKEN=your-key"
        )

    # Build client
    client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)

    print("=" * 60)
    print("LogTriageEnv β€” Baseline Inference Script")
    print("=" * 60)
    print(f"API_BASE_URL: {API_BASE_URL}")
    print(f"MODEL_NAME:   {MODEL_NAME}")
    print(f"ENV_URL:      {ENV_URL}")
    print(f"Seed:         {SEED}")
    print("=" * 60)

    # Verify environment
    try:
        health = requests.get(f"{ENV_URL}/health", timeout=10)
        health.raise_for_status()
        print("Environment: OK")
    except Exception as e:
        raise RuntimeError(
            f"Environment not responding at {ENV_URL}\n"
            f"Start with: python -m uvicorn server.app:app --port 7860\n"
            f"Error: {e}"
        )

    # Run all tasks
    results = []
    start_time = time.time()

    for task_id in TASKS:
        result = run_task(client, task_id, seed=SEED)
        results.append(result)

    elapsed = time.time() - start_time

    # Print report
    print("\n" + "=" * 60)
    print("BASELINE RESULTS")
    print("=" * 60)

    total = 0.0
    for result in results:
        task = result["task_id"]
        score = result["score"]
        steps = result["steps_taken"]
        total += score
        bar = "#" * int(score * 20) + "-" * (20 - int(score * 20))
        print(f"{task:<25} {score:.4f}  [{bar}]  ({steps} steps)")
        for k, v in result.get("breakdown", {}).items():
            print(f"  {k:<20} {v}")

    avg = total / len(TASKS)
    print("-" * 60)
    print(f"{'AVERAGE':<25} {avg:.4f}")
    print(f"{'RUNTIME':<25} {elapsed:.1f}s")
    print("=" * 60)

    # JSON output
    output = {
        "api_base_url": API_BASE_URL,
        "model_name": MODEL_NAME,
        "seed": SEED,
        "results": results,
        "average_score": round(avg, 4),
        "runtime_seconds": round(elapsed, 1),
    }
    print("\nJSON Output:")
    print(json.dumps(output, indent=2))
    return output


if __name__ == "__main__":
    main()