"""Replay HTML renderer for OnCallEnv Red Shift episodes. Renders an agent episode as a vertically-scrolling incident timeline with: - Phase headers (Triage / Investigation / Diagnosis / Remediation / Resolution) - Per-step action cards color-coded by tool category - Reward breakdown bars at the end This is the storytelling centerpiece of the Space — judges spend most of their time reading these replays. Make every visual decision serve clarity. """ from __future__ import annotations import html as _html from typing import Any def _esc(text: Any) -> str: return _html.escape(str(text)) # --- Categorize actions for color coding ----------------------------------- READ_ACTIONS = { "kubectl_get_pods", "kubectl_describe_pod", "kubectl_logs", "kubectl_top", "promql_query", "logql_query", "jaeger_search", "istioctl_proxy_status", "istioctl_routes", "curl_service", "dns_lookup", "check_deploy_history", } MUTATE_ACTIONS = { "kubectl_rollout_undo", "kubectl_rollout_restart", "kubectl_scale", "feature_flag_toggle", "traffic_split_update", "kubectl_apply_config", } TERMINAL_ACTIONS = {"declare_resolved", "submit_rca", "post_status_update"} def _categorize(action_name: str) -> str: if action_name in READ_ACTIONS: return "read" if action_name in MUTATE_ACTIONS: return "mutate" if action_name in TERMINAL_ACTIONS: return "terminal" return "unknown" CATEGORY_STYLE = { "read": { "color": "#7aa7ff", "bg": "rgba(122, 167, 255, 0.06)", "icon": "▷", "label": "INVESTIGATE", }, "mutate": { "color": "#f59e0b", "bg": "rgba(245, 158, 11, 0.08)", "icon": "✎", "label": "REMEDIATE", }, "terminal": { "color": "#10b981", "bg": "rgba(16, 185, 129, 0.08)", "icon": "✓", "label": "RESOLVE", }, "unknown": { "color": "#7d8ba1", "bg": "rgba(125, 139, 161, 0.05)", "icon": "·", "label": "ACTION", }, } # --- Episode phase inference ---------------------------------------------- def _infer_phase(step_idx: int, total_steps: int, action: str) -> str: """Infer high-level phase from position and action category.""" cat = _categorize(action) if cat == "terminal": return "RESOLUTION" if cat == "mutate": return "REMEDIATION" if step_idx <= 1: return "TRIAGE" if step_idx <= total_steps // 2: return "INVESTIGATION" return "DIAGNOSIS" PHASE_STYLE = { "TRIAGE": {"color": "#ef4444", "icon": "⚠", "desc": "Alert received — assessing scope"}, "INVESTIGATION": {"color": "#7aa7ff", "icon": "◇", "desc": "Querying telemetry, narrowing hypothesis"}, "DIAGNOSIS": {"color": "#8b5cf6", "icon": "◆", "desc": "Root cause hypothesis converging"}, "REMEDIATION": {"color": "#f59e0b", "icon": "✎", "desc": "Applying corrective action"}, "RESOLUTION": {"color": "#10b981", "icon": "✓", "desc": "Validating recovery, writing RCA"}, } # --------------------------------------------------------------------------- def format_replay_html( log: list[dict], rewards: dict[str, float], scenario_id: str = "", fault_label: str = "", ) -> str: """Render an agent episode as an SRE-styled incident timeline. Args: log: list of step dicts with keys {step, action, result, reward, error} rewards: dict of reward components {recovery, rca_quality, blast_radius, safety, total} scenario_id: optional scenario identifier for the header fault_label: optional human-readable fault description """ if not log: return _empty_state() total_steps = len(log) parts: list[str] = [] parts.append(_outer_open(scenario_id, fault_label)) current_phase = None for idx, entry in enumerate(log): action = entry.get("action", "") action_name = action.split()[0] if action else "(empty)" phase = _infer_phase(idx, total_steps, action_name) if phase != current_phase: current_phase = phase parts.append(_phase_header(phase, idx + 1)) parts.append(_action_card(idx + 1, entry, action_name)) parts.append(_reward_panel(rewards)) parts.append(_outer_close()) return "".join(parts) def _empty_state() -> str: return """
{_esc(scenario_id)}")
if fault_label:
meta_parts.append(f"fault {_esc(fault_label)}")
meta_line = f"""