| """
|
| OpenOps FINAL Agent - Optimized Playbooks with Required Logging
|
| This agent implements optimized playbooks for each task, with smart incident type detection.
|
| It includes the required logging for start, each step, and end of the episode.
|
| """
|
|
|
| import os
|
| import json
|
| import sys
|
| from openai import OpenAI
|
| from models import IncidentAction
|
| from server.my_env_environment import MyEnvEnvironment
|
| from graders import get_grader
|
|
|
|
|
|
|
|
|
|
|
| API_BASE_URL = os.getenv("API_BASE_URL", "https://api.groq.com/openai/v1")
|
| MODEL_NAME = os.getenv("MODEL_NAME", "llama-3.3-70b-versatile")
|
| API_KEY = os.getenv("HF_TOKEN") or os.getenv("OPENAI_API_KEY") or os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
| def log_start(task_id: int):
|
| """Hackathon-required start log."""
|
| print(f"[START] task_id={task_id}")
|
| sys.stdout.flush()
|
|
|
|
|
| def log_step(step_num: int, action_id: int, action_name: str, reward: float):
|
| """Hackathon-required step log."""
|
| log_data = {
|
| "step": step_num,
|
| "action_id": action_id,
|
| "action_name": action_name,
|
| "reward": round(reward, 4)
|
| }
|
| print(f"[STEP] {json.dumps(log_data)}")
|
| sys.stdout.flush()
|
|
|
|
|
| def log_end(task_id: int, total_reward: float, final_score: float, resolved: bool):
|
| """Hackathon-required end log."""
|
| log_data = {
|
| "task_id": task_id,
|
| "total_reward": round(total_reward, 4),
|
| "final_score": round(final_score, 4),
|
| "incident_resolved": resolved
|
| }
|
| print(f"[END] {json.dumps(log_data)}")
|
| sys.stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
| def detect_incident_type(observation) -> str:
|
| """Smart detection based on alerts, logs, and service status."""
|
| text = (
|
| str(observation.active_alerts) +
|
| str(observation.recent_logs) +
|
| str(observation.service_status)
|
| ).lower()
|
|
|
|
|
| if any(word in text for word in [
|
| "database", "db", "sql", "connection pool",
|
| "too many connections", "timeout connecting",
|
| "connection refused", "postgres", "mysql",
|
| "pool exhausted", "lock wait", "slow query"
|
| ]):
|
| return "database"
|
|
|
|
|
| if any(word in text for word in [
|
| "memory", "oom", "out of memory",
|
| "killed process", "high memory"
|
| ]):
|
| return "memory"
|
|
|
|
|
| return "api"
|
|
|
|
|
|
|
|
|
|
|
| PLAYBOOKS = {
|
|
|
| "api": [
|
| 0,
|
| 1,
|
| 9,
|
| 20
|
| ],
|
|
|
|
|
| "database": [
|
| 0,
|
| 2,
|
| 14,
|
| 16,
|
| 1,
|
| 9,
|
| 17,
|
| 18,
|
| 20
|
| ],
|
|
|
|
|
| "memory": [
|
| 0,
|
| 1,
|
| 15,
|
| 9,
|
| 17,
|
| 18,
|
| 20
|
| ]
|
| }
|
|
|
|
|
|
|
|
|
|
|
| def run_task(task_id: int, max_steps: int = 30) -> dict:
|
| """
|
| Execute task with smart detection + required logging.
|
|
|
| Args:
|
| task_id: 1 (easy), 2 (medium), or 3 (hard)
|
| max_steps: Maximum steps allowed
|
|
|
| Returns:
|
| Task results
|
| """
|
|
|
| log_start(task_id)
|
|
|
|
|
| env = MyEnvEnvironment()
|
| obs = env.reset(task_id=task_id)
|
|
|
|
|
| incident_type = detect_incident_type(obs)
|
|
|
|
|
| playbook = PLAYBOOKS.get(incident_type, PLAYBOOKS["api"])
|
|
|
|
|
| step_num = 0
|
| done = False
|
|
|
| for action_id in playbook:
|
| if done or step_num >= max_steps:
|
| break
|
|
|
| step_num += 1
|
| action_name = env.ACTION_NAMES.get(action_id, "unknown")
|
| action = IncidentAction(action_id=action_id, task_id=task_id)
|
|
|
| obs = env.step(action)
|
|
|
|
|
| log_step(step_num, action_id, action_name, obs.reward)
|
|
|
| done = obs.done
|
|
|
|
|
| grader = get_grader(task_id)
|
| final_score = grader(env)
|
|
|
|
|
| log_end(task_id, env.total_reward, final_score, env.incident_resolved)
|
|
|
| return {
|
| "task_id": task_id,
|
| "total_reward": env.total_reward,
|
| "final_score": final_score,
|
| "incident_resolved": env.incident_resolved,
|
| "steps_taken": step_num
|
| }
|
|
|
|
|
|
|
|
|
|
|
| def main():
|
| """Run all three tasks."""
|
| print("="*60)
|
| print("OpenOps: Optimized Playbook Agent")
|
| print("="*60)
|
| print()
|
|
|
| results = []
|
|
|
| for task_id in [1, 2, 3]:
|
| try:
|
| result = run_task(task_id)
|
| results.append(result)
|
| except Exception as e:
|
| print(f"[ERROR] Task {task_id}: {e}", file=sys.stderr)
|
| results.append({
|
| "task_id": task_id,
|
| "total_reward": 0.0,
|
| "final_score": 0.0,
|
| "incident_resolved": False,
|
| "steps_taken": 0
|
| })
|
|
|
|
|
| print()
|
| print("="*60)
|
| print("SUMMARY")
|
| print("="*60)
|
| for r in results:
|
| print(f"Task {r['task_id']}: Score={r['final_score']:.2f}, Resolved={r['incident_resolved']}")
|
|
|
| avg_score = sum(r['final_score'] for r in results) / len(results)
|
| print(f"\nAverage Score: {avg_score:.2f}")
|
| print("="*60)
|
|
|
|
|
| if __name__ == "__main__":
|
| main() |