diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..0da9ecb172e20f263226baf64d86e81f2b789bf8 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,18 @@ +.venv/ +__pycache__/ +*.pyc +.git/ +.pytest_cache/ +outputs/ +.omx/ +.codex/ +AGENTS.md +sre_env/ +*.egg-info/ +dist/ +build/ +.gemini/ +madhav_trial/ +*.png +*.npz +node_modules/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..87cc70342d9680e90f4a1c0b35b051fe0e62f9ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +.venv/ +__pycache__/ +.pytest_cache/ +*.pyc +learning_curve.png +.omx/ +.codex/ +outputs/ +AGENTS.md +.sisyphus/ +*.egg-info/ +uv.lock diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..994c2b1610251cb25c83d0aca4693fb5af5f5e2b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.11-slim + +WORKDIR /app + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + ENABLE_WEB_INTERFACE=true + +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + curl \ + && rm -rf /var/lib/apt/lists/* + +COPY . /app + +RUN pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir . + +EXPOSE 8000 + +CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..11b8f6f8b2256ca6ba692bd59effe167fdec910b --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +.PHONY: install dev test baseline walkthrough trainer-eval trainer-dataset trainer-session docker-build docker-run validate clean + +install: + python3 -m pip install -e ".[dev]" + @echo "Dependencies installed" + +dev: + ENABLE_WEB_INTERFACE=true uvicorn server.app:app --reload --host 0.0.0.0 --port 8000 + +test: + pytest unified_incident_env/tests -v --tb=short + +baseline: + python -m unified_incident_env.scripts.baseline_agent + +walkthrough: + python -m unified_incident_env.scripts.walkthrough --scenario easy_sqli_db_outage + +trainer-eval: + python -m unified_incident_env.trainer.eval_models --models qwen2.5:0.5b gemma2:2b qwen2.5:7b-instruct-q4_K_M --mode strict + +trainer-dataset: + python -m unified_incident_env.trainer.build_sft_dataset --source combined --output outputs/trainer/sft_dataset.jsonl + +trainer-session: + python -m unified_incident_env.trainer.run_session --model qwen2.5:0.5b --base-url http://127.0.0.1:8000 + +docker-build: + docker buildx build --platform linux/amd64 -t sre-env:latest . + +docker-run: + docker run -p 8000:8000 -e ENABLE_WEB_INTERFACE=true sre-env:latest + +validate: + openenv validate . + +clean: + rm -rf outputs __pycache__ .pytest_cache + find . -name "*.pyc" -delete diff --git a/README.md b/README.md index f497fa66bf5f9755ff83c84a44f020f2e59f1e31..a850ae51bfcb3e1108857058ab4df61db83aa926 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,117 @@ --- -title: Sre Gym -emoji: ๐Ÿ“š +title: SRE Gym +emoji: ๐Ÿšจ colorFrom: red -colorTo: pink +colorTo: yellow sdk: docker +app_port: 8000 pinned: false +license: apache-2.0 --- -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference +# sre-gym โ€” Fault-injecting SRE training env for OpenEnv + +Most SRE agent skills are runbooks and good intentions. **sre-gym** is the other half: a fault-injecting environment with deterministic grading where an agent diagnoses a real production-style incident, chooses a safe remediation, verifies recovery, and declares resolved. Every run is scored the same way twice. + +- Spec-compliant OpenEnv environment (typed Pydantic action / observation / state, `reset` / `step` / `state`, `openenv validate` green). +- 3 curriculum scenarios โ€” easy, medium, hard โ€” with decoy services and causal dependencies. +- 11 bounded actions. Honest state transitions. No hidden oracles. +- 21 tests passing. +- Ships a Claude Code skill + verified-runbook loop โ€” successful solves write markdown runbooks that the next run reads back. + +## 30-second demo + +```bash +./demo/run_demo.sh +``` + +Starts the env, solves each scenario cold, writes a runbook for each, re-solves to prove the loop. Full transcript takes ~10 seconds. + +## Curriculum + +| Difficulty | Scenario | Story | Decoy | Correct path | +|---|---|---|---|---| +| easy | `worker_deploy_cascade` | Bad worker deploy โ†’ DB crash-loop โ†’ login 502s | โ€” | rollback worker โ†’ restart db โ†’ verify โ†’ resolve | +| medium | `db_config_rollout` | DB config push shrank connection pool from 80โ†’12 | recent worker deploy | rollback **db** โ†’ restart db โ†’ verify โ†’ resolve | +| hard | `gateway_auth_rollout` | Gateway auth-middleware rollout rejects valid logins | recent worker deploy | rollback **gateway** โ†’ verify โ†’ resolve (no restart) | + +Rolling back the wrong service returns a negative reward and `failure_type="wrong_remediation_target"`. Restarting before the cause is removed re-inherits the bad state. `declare_resolved` is rejected until the scenario's resolution check passes against the actual world model. + +## Install + +```bash +# 1. Create a venv and install +python3 -m venv .venv && source .venv/bin/activate +pip install -e '.[dev]' + +# 2. Start the env +uvicorn server.app:app --host 127.0.0.1 --port 8000 + +# 3. Run the baseline inference against it +export HF_TOKEN="โ€ฆ"; export ENV_BASE_URL=http://127.0.0.1:8000 +python inference.py +``` + +## Install the Claude Code skill + +```bash +ln -s "$PWD/skill" "$HOME/.claude/skills/sre-gym" +``` + +Then, in Claude Code, ask: *"Solve the db_config_rollout scenario in sre-gym."* The skill will drive the env via `skill/tools/sre_gym_client.py`, load any existing runbook from `skill/verified-runbooks/`, and append a fresh runbook on any clean solve (score > 0.85). + +## Architecture + +``` +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” HTTP / WS โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ Claude Code โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถ โ”‚ OpenEnv server โ”‚ +โ”‚ (with sre-gym โ”‚ โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚ (FastAPI, uvicorn) โ”‚ +โ”‚ skill loaded) โ”‚ obs, reward โ”‚ unified_incident_env โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ โ–ฒ + โ–ผ on clean solve (score > 0.85) โ”‚ +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +โ”‚ verified-runbooks/ โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€ loaded at skill load โ”€โ”€โ”˜ +โ”‚ *.md โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +``` + +## Scoring + +Deterministic, 5 dimensions, sums to a public score in `[0.01, 0.99]`: + +- **Recovery** (0โ€“0.4): critical-path services healthy +- **Containment** (0โ€“0.3): root cause removed or offending service isolated +- **Verification** (0โ€“0.35): `database_recovery` + `end_to_end` checks passed +- **Impact** (0โ€“0.15): user-impact reduced +- **Efficiency** (0โ€“0.10): budget preserved, no wasteful repeats + +Target **> 0.85** for "clean solve." That's also the runbook-record threshold. + +## Repo layout + +``` +unified_incident_env/ # env core: models, environment, grader, challenge, tests +server/ # OpenEnv entrypoint wrapper +skill/ # Claude Code skill: SKILL.md, tools/, verified-runbooks/ +demo/ # run_demo.sh + pitch.md +inference.py # OpenAI-client baseline for OpenEnv hackathon submission +openenv.yaml # OpenEnv manifest +Dockerfile # HF Space deployment +``` + +## Verify + +```bash +pytest unified_incident_env/tests -q # 21 tests +python -m openenv.cli validate . # OpenEnv manifest check +docker build -t sre-engineer-llm:v2 . # HF Space image +``` + +## Roadmap โ€” v2 + +Distill the accumulated `verified-runbooks/` corpus into a local 3B reviewer via [OpenClaw-RL](https://github.com/Gen-Verse/OpenClaw-RL)'s async GRPO-on-next-state loop. Same reward contract (`run_check` passes / `failure_type` absent), same grader, but a compact policy that runs without a frontier API. + +## License + +Apache 2.0 diff --git a/demo/pitch.md b/demo/pitch.md new file mode 100644 index 0000000000000000000000000000000000000000..de86ece9400d6ca105597aaca62188b14fe1ee81 --- /dev/null +++ b/demo/pitch.md @@ -0,0 +1,49 @@ +# sre-gym โ€” 60-second pitch + +> You can't train SRE agents on production. We built the gym. + +## The story (00:00โ€“01:00) + +**[0:00โ€“0:10 ยท Hook]** "Most SRE agent skills are prompts โ€” a runbook and a good intention. We built the other half: a fault-injecting environment with deterministic grading, where every run is scored the same way twice." + +**[0:10โ€“0:25 ยท What it is]** +- OpenEnv-compliant. `openenv validate` passes. +- Three curriculum scenarios, easy โ†’ hard: + - **easy** `worker_deploy_cascade` โ€” bad worker deploy cascades to a DB crash. + - **medium** `db_config_rollout` โ€” DB config shrank the connection pool; a recent worker deploy is a decoy. + - **hard** `gateway_auth_rollout` โ€” bad auth-middleware rollout; two plausible suspects, one right answer. +- 11 bounded actions, honest state transitions (rolling back the wrong thing *fails*), deterministic grader across recovery / containment / verification / impact / efficiency. +- 21 tests passing. One public Space URL. + +**[0:25โ€“0:55 ยท Live demo]** `./demo/run_demo.sh` +- Env starts. Three scenarios visible in `/tasks`. +- Runbook dir cleared; demo starts cold. +- Each scenario solves end-to-end (score โ‰ˆ 0.99, 8โ€“10 steps). +- A markdown runbook is written per scenario from the successful trace. +- Re-solve the easy scenario โ€” this time the skill loads the runbook first. Same score, same path, zero wasted investigation. +- Point to `skill/verified-runbooks/` โ€” "Every clean solve makes the next one deterministic. No GRPO required for v1." + +**[0:55โ€“1:00 ยท Close]** "Install the skill by symlinking `skill/` into `~/.claude/skills/sre-gym`. Open source, Apache 2. v2 is the OpenClaw-RL loop โ€” distill this corpus of verified runbooks into a local 3B reviewer." + +## The one technical claim you should be ready to defend + +> "The env is honest." + +- No hidden oracles. Rolling back the wrong service returns a negative reward and `failure_type="wrong_remediation_target"` โ€” same observation contract as any other action. +- `declare_resolved` is rejected until the scenario's `resolution_check` passes, verified by actual service states in the world model, not a flag the grader peeks at. +- Rewards reward *effects*, not evidence-gathering โ€” you can't farm the env by spamming `query_logs`. +- `restart_service` on the database before the root cause is removed returns a negative reward. Always. Because in the real world, it would crash again. + +## Judge Q&A cheat sheet + +**"How is this different from running a real staging env?"** +Deterministic scoring. Every agent gets graded against the same signatures, same decoys, same tick budget. You can't do that on real infra. + +**"Why only three scenarios?"** +Three clears the hackathon DQ gate (`easy/medium/hard`). Each has a decoy + causal chain โ€” building another one is a data-entry exercise, not a design one. Adding scenarios #4โ€“#20 is the v2 data scaling lane. + +**"Why runbooks instead of GRPO?"** +For this submission, GRPO means 48 hours of training convergence risk on top of an env we just shipped. Markdown runbooks demonstrate the same loop (verified signal โ†’ persisted artefact โ†’ next run improves) in an auditable form. The GRPO wiring slots on top of the same traces when we're ready. + +**"What's the skill actually doing at runtime?"** +The skill lives in `skill/SKILL.md`. It directs Claude (or any agent) to read `verified-runbooks/{scenario}.md` before the first action, drive the env through `skill/tools/sre_gym_client.py`, and append a fresh runbook on any solve with `final_score > 0.85`. diff --git a/demo/run_demo.sh b/demo/run_demo.sh new file mode 100644 index 0000000000000000000000000000000000000000..bea1abf04fdb1774312f876fb383d7236b58bb14 --- /dev/null +++ b/demo/run_demo.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# sre-gym end-to-end demo. +# Spins up the env (or reuses a running one), solves each of the 3 scenarios +# with the baseline policy, records runbooks, shows the artefacts. +# +# Requires: python3.10+, docker (for the HF-Space-equivalent image) OR the +# repo's .venv. Defaults to .venv if present. + +set -euo pipefail +cd "$(dirname "$0")/.." + +PORT="${PORT:-8013}" +URL="http://127.0.0.1:${PORT}" +PY="${PYTHON:-.venv/bin/python}" +RUNBOOK_DIR="skill/verified-runbooks" + +banner() { printf '\n\033[1;36m== %s ==\033[0m\n' "$*"; } +ok() { printf '\033[0;32m โœ“ %s\033[0m\n' "$*"; } + +banner "0 / preflight" +if [[ ! -x "$PY" ]]; then + echo " note: $PY not found, falling back to system python3" >&2 + PY="python3" +fi +"$PY" -c "import unified_incident_env" 2>/dev/null || { + echo " error: unified_incident_env not importable; run 'pip install -e .' first" >&2 + exit 1 +} +ok "python + package ready" + +banner "1 / start env" +if curl -sf "$URL/health" > /dev/null 2>&1; then + ok "env already running on $URL" + SERVER_STARTED=0 +else + "$PY" -m uvicorn server.app:app --host 127.0.0.1 --port "$PORT" > /tmp/sre_gym_demo.log 2>&1 & + SERVER_PID=$! + SERVER_STARTED=1 + for _ in $(seq 1 20); do + if curl -sf "$URL/health" > /dev/null 2>&1; then break; fi + sleep 0.3 + done + curl -sf "$URL/health" > /dev/null || { echo " error: env failed to start" >&2; cat /tmp/sre_gym_demo.log >&2; exit 1; } + ok "env started on $URL (pid $SERVER_PID)" +fi +trap '[[ ${SERVER_STARTED:-0} -eq 1 ]] && kill ${SERVER_PID:-0} 2>/dev/null || true' EXIT + +banner "2 / available scenarios" +SRE_GYM_URL="$URL" "$PY" skill/tools/sre_gym_client.py list + +banner "3 / clear prior runbooks (demo starts cold)" +rm -f "$RUNBOOK_DIR"/*.md +ok "runbook directory cleared" + +for scenario in worker_deploy_cascade db_config_rollout gateway_auth_rollout; do + banner "4 / solve: $scenario" + SRE_GYM_URL="$URL" "$PY" skill/tools/sre_gym_client.py solve "$scenario" + SRE_GYM_URL="$URL" "$PY" skill/tools/sre_gym_client.py record-runbook "$scenario" +done + +banner "5 / verified runbooks now on disk" +ls -1 "$RUNBOOK_DIR"/*.md | sed 's|^| |' + +banner "6 / re-solve easy scenario โ€” runbook is loaded this time" +SRE_GYM_URL="$URL" "$PY" skill/tools/sre_gym_client.py solve worker_deploy_cascade | tail -4 + +banner "done" +echo " install the skill globally: ln -s \"$PWD/skill\" \"\$HOME/.claude/skills/sre-gym\"" +echo " env log: /tmp/sre_gym_demo.log" +echo " runbooks: $RUNBOOK_DIR/" diff --git a/deploy/push_to_hf.sh b/deploy/push_to_hf.sh new file mode 100644 index 0000000000000000000000000000000000000000..869f43ce01426b9adfa621db728de3fbc182ef13 --- /dev/null +++ b/deploy/push_to_hf.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# Deploy this repo to a Hugging Face Space (Docker SDK). +# +# Required: +# HF_TOKEN write-scoped HF access token +# HF_SPACE_ID e.g. yourname/sre-gym (create it at huggingface.co/new-space +# first, SDK=Docker, or let this script try to create it) +# +# Usage: +# HF_TOKEN=hf_xxx HF_SPACE_ID=yourname/sre-gym ./deploy/push_to_hf.sh +# +# After a successful push, verify from a different network: +# curl https://${space_subdomain}.hf.space/health +# curl https://${space_subdomain}.hf.space/tasks | jq '.scenarios[].difficulty' + +set -euo pipefail +cd "$(dirname "$0")/.." + +: "${HF_TOKEN:?HF_TOKEN is required}" +: "${HF_SPACE_ID:?HF_SPACE_ID is required, e.g. yourname/sre-gym}" + +if ! command -v huggingface-cli > /dev/null; then + echo "error: huggingface-cli not installed. pip install 'huggingface_hub[cli]'" >&2 + exit 1 +fi + +echo "== syncing openenv.yaml with HF_SPACE_ID ==" +python3 - < $HF_SPACE_ID") +PY + +echo "== ensuring the space exists (idempotent) ==" +huggingface-cli repo create "$HF_SPACE_ID" \ + --type space \ + --space_sdk docker \ + --token "$HF_TOKEN" \ + --yes 2>&1 | grep -v "already created" || true + +echo "== uploading repo ==" +huggingface-cli upload "$HF_SPACE_ID" . \ + --repo-type space \ + --token "$HF_TOKEN" \ + --commit-message "deploy sre-gym v2 (easy/medium/hard scenarios)" + +subdomain="$(echo "$HF_SPACE_ID" | tr '/' '-')" +echo +echo "== deployment kicked off ==" +echo " Logs: https://huggingface.co/spaces/$HF_SPACE_ID" +echo " Public: https://$subdomain.hf.space" +echo +echo "== verify from a different network (phone hotspot) ==" +echo " curl https://$subdomain.hf.space/health" +echo " curl https://$subdomain.hf.space/tasks | jq '.scenarios[].difficulty'" diff --git a/execution.md b/execution.md new file mode 100644 index 0000000000000000000000000000000000000000..6e264988115279e8e7efa836ce8e8770fb74cad7 --- /dev/null +++ b/execution.md @@ -0,0 +1,53 @@ +# How To Run (v2) + +## 1. Setup + +```bash +python3 -m venv .venv +source .venv/bin/activate +pip install -e '.[dev]' +``` + +## 2. Start the environment + +```bash +source .venv/bin/activate +uvicorn server.app:app --host 127.0.0.1 --port 8000 +``` + +## 3. Manual API smoke test + +```bash +curl -X POST http://127.0.0.1:8000/reset -H 'content-type: application/json' -d '{}' +curl -X POST http://127.0.0.1:8000/step -H 'content-type: application/json' -d '{"action":{"action_type":"query_deploys","service":"worker"}}' +``` + +## 4. Run inference + +```bash +source .venv/bin/activate + +export HF_TOKEN="your_hf_token" +export API_BASE_URL="https://router.huggingface.co/v1" +export MODEL_NAME="Qwen/Qwen2.5-72B-Instruct:novita" +export ENV_BASE_URL="http://127.0.0.1:8000" + +python inference.py +``` + +## 5. Verification + +```bash +source .venv/bin/activate +pytest unified_incident_env/tests -q +openenv validate . +``` + +## 6. Reward semantics + +- queries reveal evidence but do not directly mint positive breadcrumb reward +- remediation actions change the world state +- `run_check` verifies recovery explicitly +- `declare_resolved` succeeds only after objective checks pass + +Public benchmark score is deterministic and separate from the per-step training reward. diff --git a/inference.py b/inference.py new file mode 100644 index 0000000000000000000000000000000000000000..4ca933c049c3c78ff4592f3829e7268767297236 --- /dev/null +++ b/inference.py @@ -0,0 +1,264 @@ +#!/usr/bin/env python3 +"""Submission inference script for the honest narrow incident environment.""" + +from __future__ import annotations + +import json +import os +from typing import Any + +from openai import OpenAI + +from unified_incident_env.client import UnifiedIncidentEnv +from unified_incident_env.models import UnifiedIncidentAction, UnifiedIncidentObservation +from unified_incident_env.server.challenge import DEFAULT_SCENARIO_ID, SCENARIOS + +API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1") +MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct:novita") +HF_TOKEN = os.getenv("HF_TOKEN") +ENV_BASE_URL = os.getenv("ENV_BASE_URL") or UnifiedIncidentEnv.DEFAULT_BASE_URL +ENV_NAME = "unified-incident-env" +MAX_TOKENS = 260 + + +def create_client() -> OpenAI | None: + if not HF_TOKEN: + return None + return OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN) + + +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: str | None) -> None: + print( + f"[STEP] step={step} action={action} reward={reward:.2f} done={str(done).lower()} error={error or 'null'}", + flush=True, + ) + + +def log_end(*, success: bool, steps: int, score: float, rewards: list[float]) -> None: + rewards_text = ",".join(f"{reward:.2f}" for reward in rewards) + print(f"[END] success={str(success).lower()} steps={steps} score={score:.2f} rewards={rewards_text}", flush=True) + + +def _service_order(observation: UnifiedIncidentObservation) -> list[str]: + services = list(observation.service_health.items()) + services.sort(key=lambda item: (item[1].status != "crashed", item[1].status != "degraded", -item[1].error_rate_pct)) + return [name for name, _payload in services] + + +def _default_action_for_type(action_type: str, observation: UnifiedIncidentObservation) -> dict[str, Any]: + services = _service_order(observation) + service = services[0] if services else "database" + if action_type in {"query_logs", "query_dependencies", "query_deploys", "rollback_deploy", "restart_service", "isolate_service"}: + if action_type == "rollback_deploy": + service = "worker" + return {"action_type": action_type, "service": service} + if action_type == "query_metrics": + return {"action_type": action_type, "service": service, "metric": "cpu"} + if action_type == "run_check": + check_name = "database_recovery" + if observation.service_health.get("database") and observation.service_health["database"].status == "healthy": + check_name = "end_to_end" + return {"action_type": action_type, "check_name": check_name} + if action_type == "submit_hypothesis": + return { + "action_type": "submit_hypothesis", + "hypothesis": { + "root_cause": "bad_worker_deploy", + "affected_services": ["worker", "database"], + "confidence": 0.5, + "recommended_next_action": "query_deploys", + }, + } + return {"action_type": action_type} + + +def parse_action(raw: str, observation: UnifiedIncidentObservation) -> UnifiedIncidentAction | None: + text = raw.strip() + if not text: + return None + try: + data = json.loads(text) + except Exception: + return None + if not isinstance(data, dict): + return None + if "action" in data and "action_type" not in data and isinstance(data["action"], str): + data = {**data, "action_type": data["action"]} + data.pop("action", None) + action_type = data.get("action_type") + if action_type not in observation.allowed_actions: + return None + try: + return UnifiedIncidentAction(**data) + except Exception: + return None + + +def build_user_prompt(observation: UnifiedIncidentObservation) -> str: + required_lines = [] + for action, fields in observation.required_fields_by_action.items(): + required_lines.append(f"- {action}: {', '.join(fields) if fields else '(no extra fields)'}") + checks = "\n".join( + f"- {check.name}: {'passed' if check.passed else 'pending'} - {check.detail}" + for check in observation.checks + ) or "- none" + return ( + "Return exactly one JSON object representing the next action.\n" + f"Current stage: {observation.workflow_stage}\n" + f"Incident summary: {observation.incident_summary}\n" + f"Current score: {observation.final_score:.4f}\n" + f"Last action result: {observation.last_action_result or 'none'}\n" + f"Tool output: {observation.tool_output or 'none'}\n" + f"Failure: {observation.failure_type or 'none'}\n" + f"Why failed: {observation.why_failed or 'none'}\n" + f"User impact: {observation.user_impact:.2f}\n" + f"SLO burn rate: {observation.slo_burn_rate:.2f}\n" + "Allowed actions:\n" + + "\n".join(f"- {action}" for action in observation.allowed_actions) + + "\nRequired fields:\n" + + "\n".join(required_lines) + + "\nChecks:\n" + + checks + ) + + +def _schema(observation: UnifiedIncidentObservation) -> dict[str, Any]: + properties: dict[str, Any] = { + "action_type": {"type": "string", "enum": observation.allowed_actions}, + "service": {"type": "string", "enum": sorted(observation.service_health)}, + "metric": {"type": "string", "enum": ["cpu", "error_rate", "latency"]}, + "check_name": {"type": "string", "enum": ["database_recovery", "end_to_end"]}, + "hypothesis": { + "type": "object", + "properties": { + "root_cause": {"type": "string", "enum": ["bad_worker_deploy", "database_only_failure", "api_gateway_fault"]}, + "affected_services": { + "type": "array", + "items": {"type": "string", "enum": sorted(observation.service_health)}, + "minItems": 1, + }, + "confidence": {"type": "number", "minimum": 0.0, "maximum": 1.0}, + "recommended_next_action": { + "type": "string", + "enum": [ + "query_logs", + "query_metrics", + "query_dependencies", + "query_deploys", + "rollback_deploy", + "restart_service", + "run_check", + "isolate_service", + "escalate", + "declare_resolved", + ], + }, + }, + "required": ["root_cause", "affected_services", "confidence", "recommended_next_action"], + "additionalProperties": False, + }, + } + required = ["action_type"] + for action, fields in observation.required_fields_by_action.items(): + if action in observation.allowed_actions: + for field in fields: + if field not in required: + required.append(field) + return { + "type": "object", + "properties": properties, + "required": required, + "additionalProperties": False, + } + + +def request_action(client: OpenAI, observation: UnifiedIncidentObservation) -> str: + completion = client.chat.completions.create( + model=MODEL_NAME, + messages=[ + {"role": "system", "content": "You are an incident responder. Respond with JSON only."}, + {"role": "user", "content": build_user_prompt(observation)}, + ], + response_format={ + "type": "json_schema", + "json_schema": { + "name": "incident_action", + "strict": True, + "schema": _schema(observation), + }, + }, + max_tokens=MAX_TOKENS, + temperature=0.0, + ) + return (completion.choices[0].message.content or "").strip() + + +def build_fallback_action(observation: UnifiedIncidentObservation) -> UnifiedIncidentAction: + services = _service_order(observation) + if "query_deploys" in observation.allowed_actions and "worker" in observation.service_health: + return UnifiedIncidentAction(action_type="query_deploys", service="worker") + if "query_logs" in observation.allowed_actions: + return UnifiedIncidentAction(action_type="query_logs", service=services[0] if services else "database") + if "query_metrics" in observation.allowed_actions: + return UnifiedIncidentAction(action_type="query_metrics", service=services[0] if services else "database", metric="cpu") + action_type = observation.allowed_actions[0] + return UnifiedIncidentAction(**_default_action_for_type(action_type, observation)) + + +def get_model_action(client: OpenAI | None, observation: UnifiedIncidentObservation) -> tuple[UnifiedIncidentAction, str | None]: + if client is None: + return build_fallback_action(observation), "model_unavailable" + try: + parsed = parse_action(request_action(client, observation), observation) + if parsed is not None: + return parsed, None + except Exception: + pass + return build_fallback_action(observation), "fallback_used" + + +def run_scenario(client: OpenAI | None, scenario_id: str) -> dict[str, Any]: + with UnifiedIncidentEnv(base_url=ENV_BASE_URL).sync() as env: + observation = env.reset(scenario_id=scenario_id).observation + rewards: list[float] = [] + step = 0 + log_start(task=scenario_id, env=ENV_NAME, model=MODEL_NAME) + while not observation.done: + step += 1 + action, error = get_model_action(client, observation) + result = env.step(action) + observation = result.observation + rewards.append(float(result.reward)) + log_step( + step=step, + action=json.dumps(action.model_dump(exclude_none=True), separators=(",", ":")), + reward=float(result.reward), + done=bool(result.done), + error=error or observation.failure_type, + ) + log_end( + success=bool(observation.done and observation.incident_resolved), + steps=step, + score=observation.final_score, + rewards=rewards, + ) + return { + "success": bool(observation.done and observation.incident_resolved), + "score": observation.final_score, + "steps": step, + "rewards": rewards, + } + + +def main() -> None: + client = create_client() + for scenario_id in SCENARIOS: + run_scenario(client, scenario_id) + + +if __name__ == "__main__": + main() diff --git a/openenv.yaml b/openenv.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d4771c4be0b316a9cbb4ae369bd8c1822e8b9cdb --- /dev/null +++ b/openenv.yaml @@ -0,0 +1,21 @@ +name: sre-engineer-llm +version: 2.0.0 +description: > + Honest narrow OpenEnv benchmark for incident diagnosis and safe remediation. + Agents query evidence, choose bounded remediation actions, run explicit checks, + and declare resolution only after objective recovery succeeds. +author: Daksh Verma +license: MIT + +environment: + action_type: UnifiedIncidentAction + observation_type: UnifiedIncidentObservation + state_type: UnifiedIncidentState + max_steps: 12 + difficulties: [easy, medium, hard] + reward_type: dense + +huggingface: + space_id: dakshdoesdev/sre-gym + sdk: docker + hardware: cpu-basic diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000000000000000000000000000000000000..904647732b6ce957480e33183fffb9626a225a5f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,43 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "unified-incident-env" +version = "1.0.0" +description = "Unified OpenEnv benchmark for incident response with causally linked WebSec remediation" +readme = "README.md" +requires-python = ">=3.10" +dependencies = [ + "openenv-core>=0.2.1", + "fastapi>=0.115.0", + "uvicorn[standard]>=0.30.0", + "pydantic>=2.8.0", + "httpx>=0.27.0", + "openai>=1.0.0", + "websockets>=12.0", + "rich>=13.0.0", + "matplotlib>=3.9.0", + "numpy>=2.0.0" +] + +[project.optional-dependencies] +dev = [ + "pytest>=8.0.0", + "pytest-asyncio>=0.23.0" +] + +[project.scripts] +server = "server.app:main" +baseline = "unified_incident_env.scripts.baseline_agent:main" +walkthrough = "unified_incident_env.scripts.walkthrough:main" +trainer-run-episode = "unified_incident_env.trainer.run_episode:main" +trainer-build-dataset = "unified_incident_env.trainer.build_sft_dataset:main" +trainer-eval-models = "unified_incident_env.trainer.eval_models:main" +trainer-build-datasets = "unified_incident_env.trainer.build_datasets:main" +trainer-update-model = "unified_incident_env.trainer.update_model:main" +trainer-run-session = "unified_incident_env.trainer.run_session:main" +trainer-train-external = "unified_incident_env.trainer.train_external:main" + +[tool.hatch.build.targets.wheel] +packages = ["unified_incident_env", "server"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..d6e1198b1ab1f5a7f19c6f1fc2ba7338438cf718 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +-e . diff --git a/run_demo.py b/run_demo.py new file mode 100644 index 0000000000000000000000000000000000000000..912150506b0d09ff271e994ae4cb65639328b21c --- /dev/null +++ b/run_demo.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +"""Run a local end-to-end benchmark demo against the OpenEnv server.""" + +from __future__ import annotations + +import os +import signal +import subprocess +import sys +import time +from pathlib import Path + +import httpx + + +REPO_ROOT = Path(__file__).resolve().parent +BASE_URL = os.getenv("ENV_BASE_URL", "http://127.0.0.1:8000") +HEALTH_URL = f"{BASE_URL.rstrip('/')}/health" + + +def server_is_ready() -> bool: + try: + response = httpx.get(HEALTH_URL, timeout=2.0) + return response.status_code == 200 + except Exception: + return False + + +def start_server() -> subprocess.Popen[str]: + return subprocess.Popen( + [ + sys.executable, + "-m", + "uvicorn", + "server.app:app", + "--host", + "127.0.0.1", + "--port", + "8000", + ], + cwd=REPO_ROOT, + text=True, + ) + + +def wait_for_server(timeout_s: float = 20.0) -> None: + deadline = time.time() + timeout_s + while time.time() < deadline: + if server_is_ready(): + return + time.sleep(0.5) + raise RuntimeError(f"Server did not become ready at {HEALTH_URL}") + + +def stop_server(process: subprocess.Popen[str]) -> None: + if process.poll() is not None: + return + process.send_signal(signal.SIGTERM) + try: + process.wait(timeout=10) + except subprocess.TimeoutExpired: + process.kill() + + +def main() -> None: + server_process: subprocess.Popen[str] | None = None + try: + if not server_is_ready(): + server_process = start_server() + wait_for_server() + + env = os.environ.copy() + env.setdefault("ENV_BASE_URL", BASE_URL) + subprocess.run( + [sys.executable, "inference.py"], + cwd=REPO_ROOT, + env=env, + check=True, + ) + finally: + if server_process is not None: + stop_server(server_process) + + +if __name__ == "__main__": + main() diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..994c2b1610251cb25c83d0aca4693fb5af5f5e2b --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.11-slim + +WORKDIR /app + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + ENABLE_WEB_INTERFACE=true + +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + curl \ + && rm -rf /var/lib/apt/lists/* + +COPY . /app + +RUN pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir . + +EXPOSE 8000 + +CMD ["uvicorn", "server.app:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/server/__init__.py b/server/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e20e0b243fb66ca4525ae1341b7893597452e4e4 --- /dev/null +++ b/server/__init__.py @@ -0,0 +1 @@ +"""OpenEnv server wrapper package.""" diff --git a/server/app.py b/server/app.py new file mode 100644 index 0000000000000000000000000000000000000000..05e8dc3bf9d0ed1402aeb770339dfc685d736dba --- /dev/null +++ b/server/app.py @@ -0,0 +1,14 @@ +"""Top-level OpenEnv entrypoint wrapper.""" + +from unified_incident_env.server.app import app, serve +from unified_incident_env.server.app import main as _main + +__all__ = ["app", "main", "serve"] + + +def main() -> None: + _main() + + +if __name__ == "__main__": + main() diff --git a/server/requirements.txt b/server/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..f3bd1a305161caa87662c0ee1683673eadf0a8d9 --- /dev/null +++ b/server/requirements.txt @@ -0,0 +1,8 @@ +openenv-core>=0.2.1 +fastapi>=0.115.0 +uvicorn[standard]>=0.30.0 +pydantic>=2.8.0 +websockets>=12.0 +openai>=1.0.0 +matplotlib>=3.9.0 +numpy>=2.0.0 diff --git a/skill/SKILL.md b/skill/SKILL.md new file mode 100644 index 0000000000000000000000000000000000000000..6ec0adda2c7b11266e86ee67e9e15a94d6b646c4 --- /dev/null +++ b/skill/SKILL.md @@ -0,0 +1,100 @@ +--- +name: sre-gym +description: SRE incident-response training environment with fault injection and deterministic grading. Use when the user wants to practice SRE skills, solve an injected production incident, or run one of three scenarios (worker_deploy_cascade / db_config_rollout / gateway_auth_rollout) against the sre-gym HTTP server. Invokes scripts in skill/tools/ to query the env and records verified runbooks after clean solves. +--- + +# SRE Gym โ€” Incident Response Skill + +You are an SRE agent connected to a running sre-gym environment (HTTP, default `http://127.0.0.1:8000`). The env simulates production incidents with decoy services, deterministic grading, and explicit resolution checks. Your job is to diagnose from evidence, pick the correct remediation, verify recovery, then declare resolved. + +## When to use this skill + +- The user names a scenario (`worker_deploy_cascade`, `db_config_rollout`, `gateway_auth_rollout`) or says "solve an incident / run SRE scenario" +- The user asks you to practice, benchmark, or demo incident response +- The user points you at an sre-gym URL + +## Core rules (never break these) + +1. **Never guess at remediation.** Query evidence (`query_logs`, `query_deploys`, `query_metrics`) before `rollback_deploy` / `restart_service`. +2. **Root cause before restart.** Restarting a service before rolling back the triggering change re-inherits the bad state. +3. **Never call `declare_resolved` before the scenario's resolution check passes.** Each scenario specifies which check is required; read it from `observation.checks` and from any loaded runbook. +4. **Watch for decoys.** Each scenario has a plausible-looking wrong answer. Example: `db_config_rollout` has a recent worker deploy that is *not* the cause. Read logs before committing to a target. +5. **Repeating the same no-progress action wastes ticks.** The env emits `loop_warning` when you do this โ€” treat it as a hard signal to try a different evidence source. + +## Workflow + +### 1. Load prior knowledge + +Before your first action, check `skill/verified-runbooks/{scenario_id}.md`. If it exists, read it โ€” it's a log of previously-successful solves for this exact scenario, written by earlier runs of this skill. Use the winning path and the decoy list. + +### 2. Drive the env + +Use `skill/tools/sre_gym_client.py` to call the env: + +```bash +python skill/tools/sre_gym_client.py list # show available scenarios +python skill/tools/sre_gym_client.py reset # start an episode +python skill/tools/sre_gym_client.py step '' # take one action +python skill/tools/sre_gym_client.py status # current obs + grader +``` + +Action JSON matches the env's `UnifiedIncidentAction` model. Examples: +```json +{"action_type": "query_logs", "service": "database"} +{"action_type": "query_deploys", "service": "worker"} +{"action_type": "rollback_deploy", "service": "database"} +{"action_type": "run_check", "check_name": "end_to_end"} +{"action_type": "declare_resolved"} +``` + +### 3. Investigation loop (per tick) + +1. Read `observation.prompt_text` โ€” services, alerts, last result, failure_type, why_failed. +2. If `observation.failure_type` is set, your previous action was rejected โ€” **do not repeat it**, read `why_failed` and pick a different evidence source or remediation. +3. Form a hypothesis with `submit_hypothesis` once you have enough evidence (usually 2โ€“4 queries). Calibrate `confidence`: โ‰ฅ0.7 only if you're sure. +4. Remediate (`rollback_deploy` โ†’ `restart_service` if scenario requires โ†’ `run_check`). +5. `declare_resolved` only after the required check passes. + +### 4. Record the runbook + +If the episode finishes with `incident_resolved=true` and `final_score > 0.85`, run: + +```bash +python skill/tools/sre_gym_client.py record-runbook +``` + +This appends a new entry to `skill/verified-runbooks/{scenario_id}.md`. Future runs of this skill (yours or another Claude's) load it automatically. + +## Action reference (11 actions) + +| Action | Required fields | Purpose | +|---|---|---| +| `query_logs` | `service` | Read service-level error logs | +| `query_metrics` | `service`, `metric` (cpu/error_rate/latency) | Read quantitative signals | +| `query_dependencies` | `service` | Map upstream/downstream | +| `query_deploys` | `service` | Recent deploy history | +| `rollback_deploy` | `service` | Revert last deploy โ€” SCENARIO-SPECIFIC TARGET | +| `restart_service` | `service` | Reboot a service (usually after rollback) | +| `run_check` | `check_name` (`database_recovery` / `end_to_end`) | Objective recovery check | +| `isolate_service` | `service` | Containment only, does not resolve | +| `escalate` | โ€” | Record escalation note | +| `submit_hypothesis` | `hypothesis` object | Commit RCA with confidence calibration | +| `declare_resolved` | โ€” | Finalize; rejected if required check has not passed | + +## Scoring rubric (deterministic from the env) + +- **Recovery (0โ€“0.4):** services healthy on the critical path +- **Containment (0โ€“0.3):** root cause removed OR offending service isolated +- **Verification (0โ€“0.35):** both checks passed +- **Impact (0โ€“0.15):** user_impact reduced +- **Efficiency (0โ€“0.10):** budget preserved, no wasteful repeats + +Clean solve target: **> 0.85**. That's the runbook-record threshold. + +## Decoy knowledge (read before hypothesizing) + +- `worker_deploy_cascade`: the only true cause; no decoys. +- `db_config_rollout`: the recent worker deploy is a **decoy**. Rolling back worker yields `wrong_remediation_target`. +- `gateway_auth_rollout`: the recent worker deploy (`worker@...-hotfix` โ€” log-format tweak) is a **decoy**. The gateway auth rollout is the cause. + +If you take a wrong remediation, the env returns `failure_type="wrong_remediation_target"` and a negative reward โ€” **do not retry the same wrong target**, re-read the logs. diff --git a/skill/tools/sre_gym_client.py b/skill/tools/sre_gym_client.py new file mode 100644 index 0000000000000000000000000000000000000000..1e4aa5f1232a80ecdc5c611789a3e21cd0fb7262 --- /dev/null +++ b/skill/tools/sre_gym_client.py @@ -0,0 +1,238 @@ +#!/usr/bin/env python3 +"""CLI client for the sre-gym skill. + +Usage: + sre_gym_client.py list + sre_gym_client.py solve [--policy baseline] + sre_gym_client.py interactive # stdin: one JSON action per line + sre_gym_client.py record-runbook + +Because OpenEnv's HTTP /reset and /step handlers create a fresh environment per +call, episode state only persists within a single client session. This CLI wraps +one episode inside one Python process so the session is preserved. + +SRE_GYM_URL env var overrides the base URL (default http://127.0.0.1:8000). +""" + +from __future__ import annotations + +import datetime as _dt +import json +import os +import sys +from pathlib import Path +from typing import Any + +# Make the sibling package importable whether the script is invoked from the +# repo root or from the skill/ directory directly. +_REPO_ROOT = Path(__file__).resolve().parent.parent.parent +if str(_REPO_ROOT) not in sys.path: + sys.path.insert(0, str(_REPO_ROOT)) + +from unified_incident_env.client import UnifiedIncidentEnv # noqa: E402 +from unified_incident_env.models import UnifiedIncidentAction, UnifiedIncidentObservation # noqa: E402 +from unified_incident_env.server.challenge import SCENARIOS, list_baselines # noqa: E402 + +BASE_URL = os.environ.get("SRE_GYM_URL", "http://127.0.0.1:8000").rstrip("/") +RUNBOOK_DIR = Path(__file__).resolve().parent.parent / "verified-runbooks" +SCORE_THRESHOLD = 0.85 + + +def _clean_action(action: UnifiedIncidentAction) -> dict[str, Any]: + data = action.model_dump(exclude_none=True) + if data.get("metadata") == {}: + data.pop("metadata") + hypothesis = data.get("hypothesis") + if isinstance(hypothesis, dict) and hypothesis.get("metadata") == {}: + hypothesis.pop("metadata", None) + return data + + +def _summarize_obs(obs: UnifiedIncidentObservation) -> dict[str, Any]: + return { + "tick": obs.tick_count, + "workflow_stage": obs.workflow_stage, + "last_action_result": obs.last_action_result, + "tool_output": obs.tool_output, + "failure_type": obs.failure_type, + "why_failed": obs.why_failed, + "loop_warning": obs.loop_warning, + "checks": [{"name": c.name, "passed": c.passed} for c in obs.checks], + "final_score": obs.final_score, + "incident_resolved": obs.incident_resolved, + } + + +def _session_path(scenario_id: str) -> Path: + return Path(f"/tmp/sre_gym_session.{scenario_id}.json") + + +def cmd_list() -> None: + for scenario in SCENARIOS.values(): + print(f" {scenario['difficulty']:<6} {scenario['id']:<25} {scenario['name']}") + + +def cmd_solve(scenario_id: str, policy: str = "baseline") -> None: + """Run an entire episode end-to-end inside one process.""" + if scenario_id not in SCENARIOS: + print(f"error: unknown scenario {scenario_id!r}", file=sys.stderr) + sys.exit(2) + if policy != "baseline": + print(f"error: unknown policy {policy!r} (only 'baseline' available)", file=sys.stderr) + sys.exit(2) + + trace: list[dict[str, Any]] = [] + with UnifiedIncidentEnv(base_url=BASE_URL).sync() as env: + obs = env.reset(scenario_id=scenario_id).observation + print(f"[reset] scenario={scenario_id} difficulty={obs.difficulty}") + for step in list_baselines(scenario_id).baselines[0].actions: + result = env.step(step.action) + obs = result.observation + record = { + "step": obs.tick_count, + "action": _clean_action(step.action), + "rationale": step.rationale, + "reward": result.reward, + **_summarize_obs(obs), + } + trace.append(record) + action_repr = json.dumps(record["action"], separators=(",", ":")) + print(f"[step {obs.tick_count}] action={action_repr} reward={result.reward:+.2f} score={obs.final_score:.2f}") + if result.done: + break + final = _summarize_obs(obs) + + _session_path(scenario_id).write_text( + json.dumps({"scenario_id": scenario_id, "trace": trace, "final": final}, indent=2), + encoding="utf-8", + ) + print( + f"[done] resolved={final['incident_resolved']} score={final['final_score']:.2f} " + f"steps={final['tick']} session={_session_path(scenario_id)}" + ) + + +def cmd_interactive(scenario_id: str) -> None: + """One JSON action per stdin line. Preserves session for the whole process lifetime.""" + if scenario_id not in SCENARIOS: + print(f"error: unknown scenario {scenario_id!r}", file=sys.stderr) + sys.exit(2) + + trace: list[dict[str, Any]] = [] + with UnifiedIncidentEnv(base_url=BASE_URL).sync() as env: + obs = env.reset(scenario_id=scenario_id).observation + print(json.dumps({"event": "reset", "scenario_id": scenario_id, "obs": _summarize_obs(obs)}), flush=True) + for line in sys.stdin: + line = line.strip() + if not line: + continue + try: + data = json.loads(line) + action = UnifiedIncidentAction(**data) + except Exception as exc: + print(json.dumps({"event": "error", "detail": str(exc)}), flush=True) + continue + result = env.step(action) + obs = result.observation + record = {"step": obs.tick_count, "action": _clean_action(action), "reward": result.reward, **_summarize_obs(obs)} + trace.append(record) + print(json.dumps({"event": "step", **record}), flush=True) + if result.done: + print(json.dumps({"event": "done", "final": _summarize_obs(obs)}), flush=True) + break + + _session_path(scenario_id).write_text( + json.dumps({"scenario_id": scenario_id, "trace": trace, "final": _summarize_obs(obs)}, indent=2), + encoding="utf-8", + ) + + +def cmd_record_runbook(scenario_id: str, session_file: str | None = None) -> None: + """Append a new runbook entry if the referenced session cleared the threshold.""" + path = Path(session_file) if session_file else _session_path(scenario_id) + if not path.exists(): + print(f"error: no session file at {path}", file=sys.stderr) + sys.exit(2) + session = json.loads(path.read_text(encoding="utf-8")) + final = session.get("final", {}) + score = float(final.get("final_score", 0.0)) + + if not final.get("incident_resolved"): + print(f"skip: session not resolved (resolved={final.get('incident_resolved')})", file=sys.stderr) + sys.exit(1) + if score < SCORE_THRESHOLD: + print(f"skip: score {score:.2f} below runbook threshold {SCORE_THRESHOLD:.2f}", file=sys.stderr) + sys.exit(1) + + RUNBOOK_DIR.mkdir(parents=True, exist_ok=True) + runbook_path = RUNBOOK_DIR / f"{scenario_id}.md" + + timestamp = _dt.datetime.now(_dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") + steps = int(final.get("tick", 0)) + checks_passed = [c["name"] for c in final.get("checks", []) if c.get("passed")] + trace = session.get("trace", []) + + header = ( + f"# verified-runbooks/{scenario_id}.md\n\n" + "Runbook entries are written by the sre-gym skill after a successful solve " + f"(incident_resolved=true and final_score > {SCORE_THRESHOLD:.2f}).\n" + "Each entry is immutable evidence โ€” treat it as ground truth for the winning path.\n\n---\n" + ) + lines = [f"\n## Run {timestamp} โ€” Score {score:.2f}\n"] + lines.append(f"- Steps: **{steps}**") + lines.append(f"- Checks passed: {', '.join(checks_passed) or 'none'}") + lines.append("") + lines.append("**Winning path:**") + for entry in trace: + act = entry["action"] + action_type = act.get("action_type") + extras = ", ".join( + f"{k}={v if not isinstance(v, dict) else v.get('root_cause', v)}" + for k, v in act.items() + if k != "action_type" and v not in (None, {}) + ) + extra_str = f" ({extras})" if extras else "" + rationale = entry.get("rationale", "").rstrip(".") + lines.append(f"{entry['step']}. `{action_type}{extra_str}` โ€” {rationale}") + lines.append("") + entry_text = "\n".join(lines) + + if not runbook_path.exists(): + runbook_path.write_text(header + entry_text, encoding="utf-8") + else: + with runbook_path.open("a", encoding="utf-8") as f: + f.write(entry_text) + print(f"recorded runbook entry โ†’ {runbook_path} (score {score:.2f}, {steps} steps)") + + +def main() -> None: + argv = sys.argv[1:] + if not argv: + print(__doc__, file=sys.stderr) + sys.exit(2) + cmd, *rest = argv + if cmd == "list": + cmd_list() + elif cmd == "solve": + if not rest: + print("error: solve requires ", file=sys.stderr) + sys.exit(2) + cmd_solve(rest[0], rest[1] if len(rest) > 1 else "baseline") + elif cmd == "interactive": + if not rest: + print("error: interactive requires ", file=sys.stderr) + sys.exit(2) + cmd_interactive(rest[0]) + elif cmd == "record-runbook": + if not rest: + print("error: record-runbook requires ", file=sys.stderr) + sys.exit(2) + cmd_record_runbook(rest[0], rest[1] if len(rest) > 1 else None) + else: + print(f"error: unknown command {cmd!r}", file=sys.stderr) + print(__doc__, file=sys.stderr) + sys.exit(2) + + +if __name__ == "__main__": + main() diff --git a/skill/verified-runbooks/.gitkeep b/skill/verified-runbooks/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/skill/verified-runbooks/db_config_rollout.md b/skill/verified-runbooks/db_config_rollout.md new file mode 100644 index 0000000000000000000000000000000000000000..e12f7e50d2bbb951656270eb1470568b0af5d2be --- /dev/null +++ b/skill/verified-runbooks/db_config_rollout.md @@ -0,0 +1,23 @@ +# verified-runbooks/db_config_rollout.md + +Runbook entries are written by the sre-gym skill after a successful solve (incident_resolved=true and final_score > 0.85). +Each entry is immutable evidence โ€” treat it as ground truth for the winning path. + +--- + +## Run 2026-04-23T22:01:33Z โ€” Score 0.99 + +- Steps: **10** +- Checks passed: database_recovery, end_to_end + +**Winning path:** +1. `query_logs (service=database)` โ€” Database is the loudest alert; inspect logs for the actual error signature +2. `query_deploys (service=database)` โ€” Pool-acquire errors suggest a config change; check recent database rollouts +3. `query_metrics (service=database, metric=error_rate)` โ€” Confirm the error pattern is pool exhaustion rather than compute overload +4. `query_logs (service=worker)` โ€” Rule out the decoy worker deploy by reading worker logs directly +5. `submit_hypothesis (hypothesis=database_only_failure)` โ€” Localize the fault to the database config before remediating +6. `rollback_deploy (service=database)` โ€” Roll back the offending database config rollout +7. `restart_service (service=database)` โ€” Restart the database cleanly against the restored pool config +8. `run_check (check_name=database_recovery)` โ€” Verify database pool health and write latency are back within SLO +9. `run_check (check_name=end_to_end)` โ€” Verify gateway write-path traffic succeeds end-to-end +10. `declare_resolved` โ€” Declare resolved only after objective checks pass diff --git a/skill/verified-runbooks/gateway_auth_rollout.md b/skill/verified-runbooks/gateway_auth_rollout.md new file mode 100644 index 0000000000000000000000000000000000000000..adec4e39fdfdf745722284a296942ea3ed4a6361 --- /dev/null +++ b/skill/verified-runbooks/gateway_auth_rollout.md @@ -0,0 +1,21 @@ +# verified-runbooks/gateway_auth_rollout.md + +Runbook entries are written by the sre-gym skill after a successful solve (incident_resolved=true and final_score > 0.85). +Each entry is immutable evidence โ€” treat it as ground truth for the winning path. + +--- + +## Run 2026-04-23T22:01:37Z โ€” Score 0.99 + +- Steps: **8** +- Checks passed: database_recovery, end_to_end + +**Winning path:** +1. `query_logs (service=api-gateway)` โ€” Gateway is rejecting logins; read gateway logs to localize the rejection class +2. `query_deploys (service=api-gateway)` โ€” Login rejection aligns with a recent auth middleware rollout; confirm deploy timing +3. `query_deploys (service=worker)` โ€” Rule out the worker deploy explicitly rather than assuming +4. `submit_hypothesis (hypothesis=api_gateway_fault)` โ€” Commit a calibrated hypothesis localizing to the gateway auth rollout +5. `rollback_deploy (service=api-gateway)` โ€” Roll back the bad auth middleware rollout; no restart needed +6. `run_check (check_name=end_to_end)` โ€” Verify that gateway login traffic now succeeds end-to-end +7. `run_check (check_name=database_recovery)` โ€” Confirm the database is (and stayed) healthy throughout +8. `declare_resolved` โ€” Declare resolved only after objective checks pass diff --git a/skill/verified-runbooks/worker_deploy_cascade.md b/skill/verified-runbooks/worker_deploy_cascade.md new file mode 100644 index 0000000000000000000000000000000000000000..606ea1867511df43327b0887d30dcc09cb6436dc --- /dev/null +++ b/skill/verified-runbooks/worker_deploy_cascade.md @@ -0,0 +1,23 @@ +# verified-runbooks/worker_deploy_cascade.md + +Runbook entries are written by the sre-gym skill after a successful solve (incident_resolved=true and final_score > 0.85). +Each entry is immutable evidence โ€” treat it as ground truth for the winning path. + +--- + +## Run 2026-04-23T22:01:29Z โ€” Score 0.99 + +- Steps: **10** +- Checks passed: database_recovery, end_to_end + +**Winning path:** +1. `query_deploys (service=worker)` โ€” Check whether any recent deploy aligns with the incident start +2. `query_logs (service=worker)` โ€” Inspect worker logs because deploy timing and queue pressure suggest worker-originated harm +3. `query_metrics (service=database, metric=cpu)` โ€” Confirm that the database is overloaded as a downstream effect +4. `query_dependencies (service=api-gateway)` โ€” Verify the gateway depends on the worker and database path +5. `submit_hypothesis (hypothesis=bad_worker_deploy)` โ€” Commit a calibrated hypothesis before taking an invasive mitigation step +6. `rollback_deploy (service=worker)` โ€” Remove the triggering change before restarting downstream services +7. `restart_service (service=database)` โ€” Bring the database back cleanly after the root cause is removed +8. `run_check (check_name=database_recovery)` โ€” Verify the database is no longer crashing +9. `run_check (check_name=end_to_end)` โ€” Verify gateway traffic succeeds end-to-end +10. `declare_resolved` โ€” Declare resolved only after objective checks pass diff --git a/unified_incident_env/README.md b/unified_incident_env/README.md new file mode 100644 index 0000000000000000000000000000000000000000..679a40a0be0852556c8875959963fdf2cd51c21d --- /dev/null +++ b/unified_incident_env/README.md @@ -0,0 +1,10 @@ +# Unified Incident Env + +The runnable submission surface lives at the project root. This package contains the actual environment implementation: + +- typed models in `models.py` +- environment logic in `server/environment.py` +- scoring in `server/grader.py` +- scenario catalog in `server/challenge.py` + +Use the root `README.md` for run commands, scoring, and example interaction. diff --git a/unified_incident_env/__init__.py b/unified_incident_env/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..cea916e9b646a263d41000e2498158b79552a2a3 --- /dev/null +++ b/unified_incident_env/__init__.py @@ -0,0 +1,17 @@ +"""Unified incident-response OpenEnv package.""" + +from .interface import ( + UnifiedIncidentAction, + UnifiedIncidentEnv, + UnifiedIncidentEnvironment, + UnifiedIncidentObservation, + UnifiedIncidentState, +) + +__all__ = [ + "UnifiedIncidentAction", + "UnifiedIncidentEnv", + "UnifiedIncidentEnvironment", + "UnifiedIncidentObservation", + "UnifiedIncidentState", +] diff --git a/unified_incident_env/client.py b/unified_incident_env/client.py new file mode 100644 index 0000000000000000000000000000000000000000..09d936096cf503c55fa4e352fc18a25139f17f08 --- /dev/null +++ b/unified_incident_env/client.py @@ -0,0 +1,35 @@ +"""Typed OpenEnv client for the unified incident environment.""" + +from __future__ import annotations + +from typing import Any + +from openenv.core import EnvClient +from openenv.core.client_types import StepResult + +from .models import UnifiedIncidentAction, UnifiedIncidentObservation, UnifiedIncidentState + + +class UnifiedIncidentEnv( + EnvClient[UnifiedIncidentAction, UnifiedIncidentObservation, UnifiedIncidentState] +): + """Typed client wrapper around the OpenEnv HTTP API.""" + + DEFAULT_BASE_URL = "http://127.0.0.1:8000" + + def _step_payload(self, action: UnifiedIncidentAction) -> dict[str, Any]: + return action.model_dump(exclude_none=True) + + def _parse_result(self, payload: dict[str, Any]) -> StepResult[UnifiedIncidentObservation]: + observation_data = dict(payload.get("observation", {})) + observation_data.setdefault("reward", payload.get("reward", 0.0)) + observation_data.setdefault("done", payload.get("done", False)) + observation = UnifiedIncidentObservation.model_validate(observation_data) + return StepResult( + observation=observation, + reward=payload.get("reward", observation.reward), + done=payload.get("done", observation.done), + ) + + def _parse_state(self, payload: dict[str, Any]) -> UnifiedIncidentState: + return UnifiedIncidentState.model_validate(payload) diff --git a/unified_incident_env/interface.py b/unified_incident_env/interface.py new file mode 100644 index 0000000000000000000000000000000000000000..41a96e6881abd7b4bddfdb03c8db677eab7c8889 --- /dev/null +++ b/unified_incident_env/interface.py @@ -0,0 +1,17 @@ +"""Single public interface surface for the unified incident benchmark.""" + +from .client import UnifiedIncidentEnv +from .models import ( + UnifiedIncidentAction, + UnifiedIncidentObservation, + UnifiedIncidentState, +) +from .server.environment import UnifiedIncidentEnvironment + +__all__ = [ + "UnifiedIncidentAction", + "UnifiedIncidentEnv", + "UnifiedIncidentEnvironment", + "UnifiedIncidentObservation", + "UnifiedIncidentState", +] diff --git a/unified_incident_env/models.py b/unified_incident_env/models.py new file mode 100644 index 0000000000000000000000000000000000000000..747310ead1182fe22ab9a0a3b241485909fe4db2 --- /dev/null +++ b/unified_incident_env/models.py @@ -0,0 +1,332 @@ +"""Typed models for the honest narrow incident-remediation environment.""" + +from __future__ import annotations + +from typing import Any, Literal + +from openenv.core import Action, Observation, State +from pydantic import BaseModel, ConfigDict, Field, model_validator +from pydantic_core import PydanticCustomError + +ActionType = Literal[ + "query_logs", + "query_metrics", + "query_dependencies", + "query_deploys", + "rollback_deploy", + "restart_service", + "run_check", + "isolate_service", + "escalate", + "submit_hypothesis", + "declare_resolved", +] +Difficulty = Literal["easy", "medium", "hard"] +MetricName = Literal["cpu", "error_rate", "latency"] +ServiceName = Literal["api-gateway", "cache", "database", "worker"] +ServiceStatus = Literal["healthy", "degraded", "crashed", "isolated"] +WorkflowStage = Literal["triage", "mitigation", "validation", "resolved"] +CheckName = Literal["database_recovery", "end_to_end"] +RootCauseType = Literal[ + "bad_worker_deploy", + "database_only_failure", + "api_gateway_fault", +] +RecommendedActionType = Literal[ + "query_logs", + "query_metrics", + "query_dependencies", + "query_deploys", + "rollback_deploy", + "restart_service", + "run_check", + "isolate_service", + "escalate", + "declare_resolved", +] + + +class PostmortemPayload(BaseModel): + """Deprecated compatibility shell for the removed v1 postmortem action.""" + + model_config = ConfigDict(extra="forbid") + + root_cause: str = "" + attack_vector: str = "" + timeline: list[str] = Field(default_factory=list) + remediation_steps: list[str] = Field(default_factory=list) + prevention_steps: list[str] = Field(default_factory=list) + + +class SecurityContext(BaseModel): + """Deprecated compatibility shell for the removed v1 security subquest state.""" + + model_config = ConfigDict(extra="forbid") + + code_visible: bool = False + selected_vulnerability: str | None = None + selected_patch: str | None = None + exploit_blocked: bool | None = None + functionality_preserved: bool | None = None + + +class HypothesisPayload(BaseModel): + """Structured hypothesis submitted by the agent.""" + + model_config = ConfigDict(extra="forbid") + + root_cause: RootCauseType + affected_services: list[ServiceName] = Field(default_factory=list, min_length=1) + confidence: float = Field(ge=0.0, le=1.0) + recommended_next_action: RecommendedActionType + + +class ServiceHealth(BaseModel): + """Health snapshot for a service.""" + + model_config = ConfigDict(extra="forbid") + + name: ServiceName + status: ServiceStatus + cpu_pct: float = Field(ge=0.0, le=100.0) + memory_pct: float = Field(ge=0.0, le=100.0) + error_rate_pct: float = Field(ge=0.0, le=100.0) + latency_ms: float = Field(ge=0.0) + + +class Alert(BaseModel): + """Alert exposed to the agent.""" + + model_config = ConfigDict(extra="forbid") + + service: ServiceName + severity: Literal["warning", "critical"] + message: str + + +class CheckResult(BaseModel): + """Result of a verification check.""" + + model_config = ConfigDict(extra="forbid") + + name: CheckName + passed: bool + detail: str + + +class UnifiedIncidentAction(Action): + """One structured environment action.""" + + model_config = ConfigDict(extra="ignore") + + action_type: ActionType + service: ServiceName | None = None + metric: MetricName | None = None + check_name: CheckName | None = None + hypothesis: HypothesisPayload | None = None + + @model_validator(mode="after") + def _validate_payload(self) -> "UnifiedIncidentAction": + if self.action_type in { + "query_logs", + "query_dependencies", + "query_deploys", + "rollback_deploy", + "restart_service", + "isolate_service", + } and not self.service: + raise PydanticCustomError( + "missing_service", + "service is required for {action_type}", + {"action_type": self.action_type}, + ) + if self.action_type == "query_metrics": + if not self.service: + raise PydanticCustomError( + "missing_service", + "service is required for {action_type}", + {"action_type": self.action_type}, + ) + if not self.metric: + raise PydanticCustomError( + "missing_metric", + "metric is required for {action_type}", + {"action_type": self.action_type}, + ) + if self.action_type == "run_check" and not self.check_name: + raise PydanticCustomError( + "missing_check_name", + "check_name is required for {action_type}", + {"action_type": self.action_type}, + ) + if self.action_type == "submit_hypothesis" and self.hypothesis is None: + raise PydanticCustomError( + "missing_hypothesis", + "hypothesis is required for {action_type}", + {"action_type": self.action_type}, + ) + return self + + +class UnifiedIncidentObservation(Observation): + """Observation returned after reset and each step.""" + + model_config = ConfigDict(extra="forbid") + + prompt_text: str + incident_summary: str + tick_count: int + max_ticks: int + difficulty: Difficulty + workflow_stage: WorkflowStage + active_alerts: list[Alert] = Field(default_factory=list) + service_health: dict[str, ServiceHealth] = Field(default_factory=dict) + discovered_evidence: list[str] = Field(default_factory=list) + recent_deploys: list[str] = Field(default_factory=list) + checks: list[CheckResult] = Field(default_factory=list) + user_impact: float = Field(ge=0.0, le=1.0) + slo_burn_rate: float = Field(ge=0.0, le=1.0) + incident_resolved: bool = False + containment_applied: bool = False + last_action_result: str = "" + tool_output: str | None = None + failure_type: str | None = None + why_failed: str | None = None + allowed_actions: list[str] = Field(default_factory=list) + required_fields_by_action: dict[str, list[str]] = Field(default_factory=dict) + valid_action_example: dict[str, Any] | None = None + common_trap: str | None = None + loop_warning: str | None = None + blocked_until_security_complete: bool = False + security_unlock_reason: str | None = None + best_recovery_action_family: str | None = None + progress_flags: dict[str, bool] = Field(default_factory=dict) + security_subquest_status: str | None = None + security_context: dict[str, Any] = Field(default_factory=dict) + final_score: float = 0.0 + score_breakdown: dict[str, float] = Field(default_factory=dict) + reward: float = 0.0 + done: bool = False + + +class UnifiedIncidentState(State): + """Persistent episode state.""" + + model_config = ConfigDict(extra="forbid") + + episode_id: str + step_count: int + scenario_id: str + difficulty: Difficulty + current_tick: int + max_ticks: int + workflow_stage: WorkflowStage + active_alerts: list[Alert] = Field(default_factory=list) + service_health: dict[str, ServiceHealth] = Field(default_factory=dict) + discovered_evidence: list[str] = Field(default_factory=list) + recent_deploys: list[str] = Field(default_factory=list) + checks: list[CheckResult] = Field(default_factory=list) + user_impact: float = Field(ge=0.0, le=1.0) + slo_burn_rate: float = Field(ge=0.0, le=1.0) + incident_resolved: bool = False + containment_applied: bool = False + allowed_actions: list[str] = Field(default_factory=list) + required_fields_by_action: dict[str, list[str]] = Field(default_factory=dict) + valid_action_example: dict[str, Any] | None = None + progress_flags: dict[str, bool] = Field(default_factory=dict) + final_score: float = 0.0 + score_breakdown: dict[str, float] = Field(default_factory=dict) + cumulative_reward: float = 0.0 + wasteful_ticks: int = 0 + last_action_result: str = "" + failure_type: str | None = None + why_failed: str | None = None + + +class ScenarioSummary(BaseModel): + """Public scenario summary.""" + + model_config = ConfigDict(extra="forbid") + + id: str + difficulty: Difficulty + name: str + description: str + root_cause: str + optimal_ticks: int + + +class ScenarioCatalog(BaseModel): + """Public scenario catalog.""" + + model_config = ConfigDict(extra="forbid") + + environment: str = "unified_incident_env" + default_scenario_id: str + available_difficulties: list[Difficulty] + filtered_difficulty: Difficulty | None = None + scenarios: list[ScenarioSummary] + + +class BaselineStep(BaseModel): + """One baseline action.""" + + model_config = ConfigDict(extra="forbid") + + action: UnifiedIncidentAction + rationale: str = "" + + +class BaselineDefinition(BaseModel): + """One baseline trajectory.""" + + model_config = ConfigDict(extra="forbid") + + scenario_id: str + name: str + description: str + optimal_ticks: int + actions: list[BaselineStep] = Field(default_factory=list) + + +class BaselineCatalog(BaseModel): + """Public baseline catalog.""" + + model_config = ConfigDict(extra="forbid") + + environment: str = "unified_incident_env" + baselines: list[BaselineDefinition] + + +class GraderCheck(BaseModel): + """One normalized grader check.""" + + model_config = ConfigDict(extra="forbid") + + name: str + passed: bool + detail: str + weight: float + + +class GraderReport(BaseModel): + """Episode-grade report.""" + + model_config = ConfigDict(extra="forbid") + + scenario_id: str + passed: bool + score: float = Field(ge=0.0, le=1.0) + message: str + breakdown: dict[str, float] = Field(default_factory=dict) + checks: list[GraderCheck] = Field(default_factory=list) + + +class RuntimeStatus(BaseModel): + """Runtime status route payload.""" + + model_config = ConfigDict(extra="forbid") + + environment: str = "unified_incident_env" + progress: UnifiedIncidentState + grader: GraderReport diff --git a/unified_incident_env/scripts/__init__.py b/unified_incident_env/scripts/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..6e68186d779df59fb40818a02e26c4d173f7aa41 --- /dev/null +++ b/unified_incident_env/scripts/__init__.py @@ -0,0 +1 @@ +"""Scripts for the unified incident environment.""" diff --git a/unified_incident_env/scripts/baseline_agent.py b/unified_incident_env/scripts/baseline_agent.py new file mode 100644 index 0000000000000000000000000000000000000000..912390cfa660c74a0eae1837f57362bbfb74698b --- /dev/null +++ b/unified_incident_env/scripts/baseline_agent.py @@ -0,0 +1,43 @@ +"""Deterministic scripted baseline for the honest narrow incident environment.""" + +from __future__ import annotations + +import argparse +import json + +from ..client import UnifiedIncidentEnv +from ..server.challenge import DEFAULT_SCENARIO_ID, SCENARIOS, list_baselines + + +def plan_for_scenario(scenario_id: str): + catalog = list_baselines(scenario_id) + return [step.action for step in catalog.baselines[0].actions] + + +def run_scenario(base_url: str, scenario_id: str) -> dict[str, object]: + with UnifiedIncidentEnv(base_url=base_url).sync() as env: + env.reset(scenario_id=scenario_id) + final = None + for action in plan_for_scenario(scenario_id): + final = env.step(action).observation + assert final is not None + return { + "scenario_id": scenario_id, + "success": bool(final.done and final.incident_resolved), + "final_score": final.final_score, + "workflow_stage": final.workflow_stage, + } + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--base-url", default=UnifiedIncidentEnv.DEFAULT_BASE_URL) + parser.add_argument("--scenario", choices=sorted(SCENARIOS), default=DEFAULT_SCENARIO_ID) + args = parser.parse_args() + + results = [run_scenario(args.base_url, args.scenario)] + print(json.dumps(results, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/scripts/walkthrough.py b/unified_incident_env/scripts/walkthrough.py new file mode 100644 index 0000000000000000000000000000000000000000..e0a33cffa92e6a93fe63da618875ef3c5967e948 --- /dev/null +++ b/unified_incident_env/scripts/walkthrough.py @@ -0,0 +1,41 @@ +"""Simple walkthrough that prints a full episode interaction.""" + +from __future__ import annotations + +import argparse +import json + +from ..client import UnifiedIncidentEnv +from .baseline_agent import plan_for_scenario + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument( + "--base-url", + default=UnifiedIncidentEnv.DEFAULT_BASE_URL, + ) + parser.add_argument( + "--scenario", + default="easy_sqli_db_outage", + ) + args = parser.parse_args() + + with UnifiedIncidentEnv(base_url=args.base_url).sync() as env: + reset = env.reset(scenario_id=args.scenario).observation + print(json.dumps({"reset": reset.model_dump()}, indent=2)) + for action in plan_for_scenario(args.scenario): + step = env.step(action).observation + print( + json.dumps( + { + "action": action.model_dump(exclude_none=True), + "observation": step.model_dump(), + }, + indent=2, + ) + ) + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/server/__init__.py b/unified_incident_env/server/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..ed548ce6d44b429a864b55719f74a9e3a8bf4a18 --- /dev/null +++ b/unified_incident_env/server/__init__.py @@ -0,0 +1 @@ +"""Server package for the unified incident environment.""" diff --git a/unified_incident_env/server/app.py b/unified_incident_env/server/app.py new file mode 100644 index 0000000000000000000000000000000000000000..e964d59ec596bc863988215b1921af8f14895d61 --- /dev/null +++ b/unified_incident_env/server/app.py @@ -0,0 +1,148 @@ +"""FastAPI app and metadata routes for the honest narrow incident environment.""" + +from __future__ import annotations + +import argparse +import os +from typing import Any + +from fastapi import HTTPException +from fastapi.responses import HTMLResponse, RedirectResponse +from openenv.core.env_server.http_server import create_fastapi_app + +from ..models import ( + BaselineCatalog, + GraderReport, + RuntimeStatus, + ScenarioCatalog, + UnifiedIncidentAction, + UnifiedIncidentObservation, + UnifiedIncidentState, +) +from .challenge import current_runtime_progress, grade_episode, list_baselines, list_scenarios, set_runtime_progress +from .environment import UnifiedIncidentEnvironment + +_BOOTSTRAP_ENV = UnifiedIncidentEnvironment() +set_runtime_progress(_BOOTSTRAP_ENV.state.model_dump()) + +_SIMPLE_HTML = """ + + + + + Unified Incident Env + + + +

Unified Incident Env

+

This v2 environment exposes an honest bounded-action incident diagnosis and remediation task.

+ +

Core ideas

+
    +
  • Queries reveal evidence but do not directly mint positive reward.
  • +
  • Remediation actions change the world state.
  • +
  • run_check verifies recovery explicitly.
  • +
  • declare_resolved succeeds only after objective checks pass.
  • +
+

Manual example

+
curl -X POST http://127.0.0.1:8000/reset -H 'content-type: application/json' -d '{}'
+curl -X POST http://127.0.0.1:8000/step -H 'content-type: application/json' -d '{"action_type":"query_deploys","service":"worker"}'
+ + +""" + + +def create_compatible_app(): + env_factory = lambda: UnifiedIncidentEnvironment() + app = create_fastapi_app( + env_factory, + UnifiedIncidentAction, + UnifiedIncidentObservation, + max_concurrent_envs=1, + ) + + @app.get("/", include_in_schema=False) + async def web_root(): + return RedirectResponse(url="/simple") + + @app.get("/simple", include_in_schema=False) + async def simple_console(): + return HTMLResponse(_SIMPLE_HTML) + + _attach_metadata_routes(app) + + return app + + +def _attach_metadata_routes(app): + @app.get("/tasks", response_model=ScenarioCatalog, tags=["challenge"]) + def tasks(difficulty: str | None = None) -> ScenarioCatalog: + try: + return list_scenarios(difficulty=difficulty) + except ValueError as exc: + raise HTTPException(status_code=404, detail=str(exc)) from exc + + @app.get("/baseline", response_model=BaselineCatalog, tags=["challenge"]) + def baseline(scenario_id: str | None = None) -> BaselineCatalog: + try: + return list_baselines(scenario_id=scenario_id) + except ValueError as exc: + raise HTTPException(status_code=404, detail=str(exc)) from exc + + @app.get("/grader", response_model=GraderReport, tags=["challenge"]) + def grader(scenario_id: str | None = None) -> GraderReport: + progress = current_runtime_progress() + if scenario_id is not None: + progress["scenario_id"] = scenario_id + try: + return grade_episode(progress) + except ValueError as exc: + raise HTTPException(status_code=404, detail=str(exc)) from exc + + @app.get("/status", response_model=RuntimeStatus, tags=["challenge"]) + def status() -> RuntimeStatus: + progress = current_runtime_progress() + return RuntimeStatus( + progress=UnifiedIncidentState(**progress), + grader=grade_episode(progress), + ) + + @app.get("/health", tags=["challenge"]) + def health() -> dict[str, object]: + return { + "status": "ok", + "environment": "unified_incident_env", + "version": "2.0.0", + "stages": ["triage", "mitigation", "validation", "resolved"], + } + + +app = create_compatible_app() + + +def serve(host: str = "0.0.0.0", port: int = 8000) -> None: + import uvicorn + + uvicorn.run(app, host=host, port=port) + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--host", default=os.environ.get("HOST", "0.0.0.0")) + parser.add_argument("--port", type=int, default=int(os.environ.get("PORT", "8000"))) + args = parser.parse_args() + serve(host=args.host, port=args.port) + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/server/challenge.py b/unified_incident_env/server/challenge.py new file mode 100644 index 0000000000000000000000000000000000000000..aaadc6f9249d2ba59f18f68cb504344df25c1001 --- /dev/null +++ b/unified_incident_env/server/challenge.py @@ -0,0 +1,753 @@ +"""Scenario catalog, baselines, and runtime helpers for the honest v2 core.""" + +from __future__ import annotations + +from copy import deepcopy +from typing import Any + +from ..models import ( + BaselineCatalog, + BaselineDefinition, + BaselineStep, + ScenarioCatalog, + ScenarioSummary, + UnifiedIncidentAction, +) + +DEFAULT_SCENARIO_ID = "worker_deploy_cascade" + +SCENARIOS: dict[str, dict[str, Any]] = { + "worker_deploy_cascade": { + "id": "worker_deploy_cascade", + "difficulty": "easy", + "name": "Worker Deploy Cascade", + "description": ( + "A bad worker deploy causes sustained database overload and login 502s at the gateway. " + "The agent must diagnose from evidence, choose a safe remediation, verify recovery, and declare resolved only after checks pass." + ), + "root_cause": "A bad worker deploy is driving repeated database overload.", + "optimal_ticks": 10, + "max_ticks": 12, + "critical_service_weights": { + "worker": 0.4, + "database": 0.4, + "api-gateway": 0.2, + "cache": 0.0, + }, + "reward_config": { + "step_cost": 0.01, + "redundant_action_penalty": 0.02, + "unsafe_action_penalty": 0.08, + "premature_resolution_penalty": 0.2, + "successful_resolution_bonus": 0.25, + "hypothesis_bonus_scale": 0.12, + "forbidden_reward_sources": [ + "evidence_discovery", + "query_success", + "unlock_events", + "stage_advancement", + "patch_id_selection", + ], + }, + "initial_services": { + "api-gateway": { + "status": "degraded", + "cpu_pct": 61.0, + "memory_pct": 38.0, + "error_rate_pct": 24.0, + "latency_ms": 640.0, + }, + "cache": { + "status": "healthy", + "cpu_pct": 18.0, + "memory_pct": 24.0, + "error_rate_pct": 0.0, + "latency_ms": 14.0, + }, + "database": { + "status": "crashed", + "cpu_pct": 99.0, + "memory_pct": 97.0, + "error_rate_pct": 100.0, + "latency_ms": 0.0, + }, + "worker": { + "status": "degraded", + "cpu_pct": 88.0, + "memory_pct": 71.0, + "error_rate_pct": 19.0, + "latency_ms": 420.0, + }, + }, + "initial_alerts": [ + { + "service": "api-gateway", + "severity": "critical", + "message": "Login requests are returning sustained 502s.", + }, + { + "service": "database", + "severity": "critical", + "message": "Database process is crashing under repeated overload.", + }, + { + "service": "worker", + "severity": "warning", + "message": "Worker queue depth and retry volume spiked after a recent rollout.", + }, + ], + "logs": { + "api-gateway": ( + "Gateway upstream errors point to worker timeouts followed by database connection failures. " + "No recent gateway deploys are recorded." + ), + "cache": "Cache hit ratio is stable and cache upstream probes remain healthy.", + "database": ( + "Database logs show repeated bursts of expensive worker-originated writes immediately before each crash." + ), + "worker": ( + "Worker logs show request fanout amplification and elevated retries beginning right after rollout build worker@2026.04.23-bad." + ), + }, + "metrics": { + "api-gateway": { + "error_rate": "Gateway 502 rate is 24% and closely tracks worker timeout bursts.", + "latency": "Gateway p95 latency climbed to 640ms while waiting on downstream worker/database calls.", + }, + "database": { + "cpu": "Database CPU is pinned at 99% until the process exits.", + "latency": "Database latency spikes sharply before each crash loop.", + }, + "worker": { + "cpu": "Worker CPU is 88% with growing queue pressure.", + "error_rate": "Worker retry/error rate is elevated after rollout.", + }, + }, + "dependencies": { + "api-gateway": "api-gateway -> worker -> database", + "worker": "worker -> database", + "database": "database is a terminal dependency for write-heavy worker jobs", + }, + "deploy_history": { + "api-gateway": "No gateway deploys in the last 24h.", + "cache": "No cache deploys in the last 24h.", + "database": "No database deploys in the last 24h.", + "worker": "Rolled out worker@2026.04.23-bad 12 minutes ago.", + }, + "checks": { + "database_recovery": "Confirms the database is healthy and no longer crashing.", + "end_to_end": "Confirms login traffic succeeds without worker-induced overload.", + }, + "truth": { + "root_cause": "bad_worker_deploy", + "affected_services": ["worker", "database", "api-gateway"], + "best_next_action": "rollback_deploy", + }, + "remediation_recipe": { + "rollback_target": "worker", + "restart_target": "database", + "isolate_target": "worker", + "restart_requires_cause_removed": True, + "incident_driver": "worker", + "resolution_check": "end_to_end", + }, + "post_rollback_services": { + "worker": {"status": "healthy", "cpu_pct": 32.0, "memory_pct": 37.0, "error_rate_pct": 2.0, "latency_ms": 40.0}, + }, + "post_rollback_user_impact": 0.55, + "post_rollback_slo_burn": 0.58, + "post_restart_services": { + "database": {"status": "healthy", "cpu_pct": 34.0, "memory_pct": 39.0, "error_rate_pct": 0.0, "latency_ms": 22.0}, + "api-gateway": {"status": "healthy", "cpu_pct": 28.0, "memory_pct": 31.0, "error_rate_pct": 0.0, "latency_ms": 38.0}, + }, + "post_restart_user_impact": 0.14, + "post_restart_slo_burn": 0.18, + "post_isolate_services": { + "worker": {"status": "isolated", "cpu_pct": 8.0, "memory_pct": 18.0, "error_rate_pct": 0.0, "latency_ms": 0.0}, + "database": {"status": "healthy", "cpu_pct": 41.0, "memory_pct": 46.0, "error_rate_pct": 0.0, "latency_ms": 26.0}, + "api-gateway": {"status": "degraded", "cpu_pct": 34.0, "memory_pct": 33.0, "error_rate_pct": 7.0, "latency_ms": 91.0}, + }, + "post_isolate_user_impact": 0.45, + "post_isolate_slo_burn": 0.47, + "degraded_services": { + "worker": {"status": "degraded", "cpu_pct": 88.0, "memory_pct": 71.0, "error_rate_pct": 19.0, "latency_ms": 420.0}, + "database": {"status": "crashed", "cpu_pct": 99.0, "memory_pct": 97.0, "error_rate_pct": 100.0, "latency_ms": 0.0}, + "api-gateway": {"status": "degraded", "cpu_pct": 61.0, "memory_pct": 38.0, "error_rate_pct": 24.0, "latency_ms": 640.0}, + }, + "degraded_user_impact": 0.82, + "degraded_slo_burn": 0.91, + "failure_messages": { + "wrong_rollback_target": "Rolling back a service without a causal link wastes time and risk.", + "low_value_restart": "Restarting that service is not the safe next remediation step for this incident.", + "premature_restart": "Restarting before removing the trigger only causes another crash loop.", + "wrong_isolation_target": "Isolating that service does not contain the dominant failure path.", + }, + }, + "db_config_rollout": { + "id": "db_config_rollout", + "difficulty": "medium", + "name": "Database Config Rollout Regression", + "description": ( + "A database config push cut connection pool size and write requests now time out. " + "A separate worker deploy landed around the same time and looks suspicious but is not the cause. " + "The agent must avoid the decoy, roll back the database config, restart it, and verify recovery." + ), + "root_cause": "A bad database config rollout shrank the connection pool and is dropping writes.", + "optimal_ticks": 10, + "max_ticks": 12, + "critical_service_weights": { + "worker": 0.2, + "database": 0.5, + "api-gateway": 0.3, + "cache": 0.0, + }, + "reward_config": { + "step_cost": 0.01, + "redundant_action_penalty": 0.02, + "unsafe_action_penalty": 0.08, + "premature_resolution_penalty": 0.2, + "successful_resolution_bonus": 0.25, + "hypothesis_bonus_scale": 0.12, + "forbidden_reward_sources": [ + "evidence_discovery", + "query_success", + "unlock_events", + "stage_advancement", + "patch_id_selection", + ], + }, + "initial_services": { + "api-gateway": { + "status": "degraded", + "cpu_pct": 44.0, + "memory_pct": 36.0, + "error_rate_pct": 17.0, + "latency_ms": 520.0, + }, + "cache": { + "status": "healthy", + "cpu_pct": 20.0, + "memory_pct": 26.0, + "error_rate_pct": 0.0, + "latency_ms": 15.0, + }, + "database": { + "status": "degraded", + "cpu_pct": 62.0, + "memory_pct": 54.0, + "error_rate_pct": 48.0, + "latency_ms": 880.0, + }, + "worker": { + "status": "degraded", + "cpu_pct": 51.0, + "memory_pct": 44.0, + "error_rate_pct": 12.0, + "latency_ms": 310.0, + }, + }, + "initial_alerts": [ + { + "service": "database", + "severity": "critical", + "message": "Database connection acquire timeouts at 48% and climbing.", + }, + { + "service": "api-gateway", + "severity": "warning", + "message": "Write-path requests are returning sustained 5xx.", + }, + { + "service": "worker", + "severity": "warning", + "message": "Worker write latency is elevated; retries are climbing.", + }, + ], + "logs": { + "api-gateway": ( + "Gateway upstream errors are downstream-driven: writes to the worker path return pool-exhaustion " + "errors originating from the database. No gateway deploys recorded in the last 24h." + ), + "cache": "Cache reads are healthy and unrelated to the current write-path failures.", + "database": ( + "Database logs show 'could not acquire connection' errors immediately after config rollout " + "db@2026.04.24-cfg lowered max_connections from 80 to 12." + ), + "worker": ( + "Worker logs show retries driven by downstream database pool exhaustion, not local faults. " + "Worker code deploy worker@2026.04.24-refactor is unrelated to the pool error signature." + ), + }, + "metrics": { + "api-gateway": { + "error_rate": "Gateway 5xx rate is 17% and matches the database pool-exhaustion windows one-for-one.", + "latency": "Gateway p95 climbed to 520ms waiting on database connection acquire.", + }, + "database": { + "cpu": "Database CPU is moderate (~62%), so this is not a compute overload pattern.", + "error_rate": "Database error rate is 48% and dominated by 'connection acquire timeout'.", + "latency": "Database write latency jumped to 880ms after the config rollout.", + }, + "worker": { + "cpu": "Worker CPU is 51% โ€” no local overload; retries are reactive.", + "error_rate": "Worker errors are retries against the saturated database pool.", + }, + }, + "dependencies": { + "api-gateway": "api-gateway -> worker -> database", + "worker": "worker -> database", + "database": "database is the terminal dependency; pool exhaustion here starves all upstream writers", + }, + "deploy_history": { + "api-gateway": "No gateway deploys in the last 24h.", + "cache": "No cache deploys in the last 24h.", + "database": "Applied config db@2026.04.24-cfg 15 minutes ago (max_connections 80 -> 12).", + "worker": "Rolled out worker@2026.04.24-refactor 22 minutes ago (unrelated code cleanup).", + }, + "checks": { + "database_recovery": "Confirms database write latency and pool health are back within SLO.", + "end_to_end": "Confirms gateway write-path traffic succeeds end-to-end.", + }, + "truth": { + "root_cause": "database_only_failure", + "affected_services": ["database", "api-gateway", "worker"], + "best_next_action": "rollback_deploy", + }, + "remediation_recipe": { + "rollback_target": "database", + "restart_target": "database", + "isolate_target": None, + "restart_requires_cause_removed": True, + "incident_driver": "database", + "resolution_check": "end_to_end", + }, + "post_rollback_services": { + "database": {"status": "degraded", "cpu_pct": 48.0, "memory_pct": 42.0, "error_rate_pct": 6.0, "latency_ms": 120.0}, + }, + "post_rollback_user_impact": 0.40, + "post_rollback_slo_burn": 0.45, + "post_restart_services": { + "database": {"status": "healthy", "cpu_pct": 36.0, "memory_pct": 40.0, "error_rate_pct": 0.0, "latency_ms": 26.0}, + "api-gateway": {"status": "healthy", "cpu_pct": 29.0, "memory_pct": 30.0, "error_rate_pct": 0.0, "latency_ms": 44.0}, + "worker": {"status": "healthy", "cpu_pct": 33.0, "memory_pct": 36.0, "error_rate_pct": 1.0, "latency_ms": 48.0}, + }, + "post_restart_user_impact": 0.10, + "post_restart_slo_burn": 0.14, + "post_isolate_services": {}, + "post_isolate_user_impact": 0.70, + "post_isolate_slo_burn": 0.75, + "degraded_services": { + "database": {"status": "degraded", "cpu_pct": 62.0, "memory_pct": 54.0, "error_rate_pct": 48.0, "latency_ms": 880.0}, + "api-gateway": {"status": "degraded", "cpu_pct": 44.0, "memory_pct": 36.0, "error_rate_pct": 17.0, "latency_ms": 520.0}, + "worker": {"status": "degraded", "cpu_pct": 51.0, "memory_pct": 44.0, "error_rate_pct": 12.0, "latency_ms": 310.0}, + }, + "degraded_user_impact": 0.70, + "degraded_slo_burn": 0.78, + "failure_messages": { + "wrong_rollback_target": "The worker deploy is a decoy; worker errors are reactive to database pool exhaustion.", + "low_value_restart": "Restarting that service does not address a database-config regression.", + "premature_restart": "Restarting the database before rolling back the config will re-inherit the 12-connection pool and fail again.", + "wrong_isolation_target": "Isolation is not useful here: the cause is a config regression, not a runaway service.", + }, + }, + "gateway_auth_rollout": { + "id": "gateway_auth_rollout", + "difficulty": "hard", + "name": "Gateway Auth Rollout Regression", + "description": ( + "A new api-gateway auth-middleware rollout is rejecting ~40% of valid logins. " + "A recent worker deploy and elevated worker queue depth make the worker look like a plausible suspect. " + "The agent must localize to the gateway, roll back its deploy, and verify recovery without unnecessary restarts." + ), + "root_cause": "A bad api-gateway auth-middleware rollout is rejecting valid logins.", + "optimal_ticks": 8, + "max_ticks": 10, + "critical_service_weights": { + "worker": 0.15, + "database": 0.15, + "api-gateway": 0.70, + "cache": 0.0, + }, + "reward_config": { + "step_cost": 0.01, + "redundant_action_penalty": 0.02, + "unsafe_action_penalty": 0.12, + "premature_resolution_penalty": 0.3, + "successful_resolution_bonus": 0.3, + "hypothesis_bonus_scale": 0.12, + "forbidden_reward_sources": [ + "evidence_discovery", + "query_success", + "unlock_events", + "stage_advancement", + "patch_id_selection", + ], + }, + "initial_services": { + "api-gateway": { + "status": "degraded", + "cpu_pct": 38.0, + "memory_pct": 42.0, + "error_rate_pct": 41.0, + "latency_ms": 180.0, + }, + "cache": { + "status": "healthy", + "cpu_pct": 17.0, + "memory_pct": 23.0, + "error_rate_pct": 0.0, + "latency_ms": 12.0, + }, + "database": { + "status": "healthy", + "cpu_pct": 38.0, + "memory_pct": 41.0, + "error_rate_pct": 1.0, + "latency_ms": 28.0, + }, + "worker": { + "status": "degraded", + "cpu_pct": 63.0, + "memory_pct": 48.0, + "error_rate_pct": 4.0, + "latency_ms": 220.0, + }, + }, + "initial_alerts": [ + { + "service": "api-gateway", + "severity": "critical", + "message": "Gateway is returning 401 on ~40% of valid login attempts.", + }, + { + "service": "worker", + "severity": "warning", + "message": "Worker queue depth is elevated from the retry storm upstream.", + }, + ], + "logs": { + "api-gateway": ( + "Gateway logs show auth-middleware rejecting tokens with valid signatures. " + "Rejection rate started exactly at the gateway@2026.04.24-auth rollout boundary." + ), + "cache": "Cache hit ratio stable and unrelated.", + "database": "Database logs are clean; no increase in errors or latency.", + "worker": ( + "Worker logs show client-side retry storms triggered by upstream 401s, not local faults. " + "Worker deploy worker@2026.04.24-hotfix is a log-format tweak and does not touch auth." + ), + }, + "metrics": { + "api-gateway": { + "error_rate": "Gateway error rate is 41%, dominated by 401 responses (auth failures).", + "latency": "Gateway latency is normal โ€” errors are fast rejections, not timeouts.", + }, + "database": { + "cpu": "Database CPU is 38% (normal).", + "error_rate": "Database error rate is ~1% and flat.", + }, + "worker": { + "cpu": "Worker CPU is 63% from retry volume, not workload.", + "error_rate": "Worker errors are reactive retries, not primary failures.", + }, + }, + "dependencies": { + "api-gateway": "api-gateway -> (auth) -> worker -> database", + "worker": "worker -> database", + "database": "database is healthy; it is not on the fault path", + }, + "deploy_history": { + "api-gateway": "Rolled out gateway@2026.04.24-auth 9 minutes ago (auth middleware rewrite).", + "cache": "No cache deploys in the last 24h.", + "database": "No database deploys in the last 24h.", + "worker": "Rolled out worker@2026.04.24-hotfix 18 minutes ago (log-format tweak, no auth changes).", + }, + "checks": { + "database_recovery": "Confirms the database is healthy (always healthy in this scenario).", + "end_to_end": "Confirms gateway login traffic succeeds end-to-end.", + }, + "truth": { + "root_cause": "api_gateway_fault", + "affected_services": ["api-gateway", "worker"], + "best_next_action": "rollback_deploy", + }, + "remediation_recipe": { + "rollback_target": "api-gateway", + "restart_target": None, + "isolate_target": "api-gateway", + "restart_requires_cause_removed": True, + "incident_driver": "api-gateway", + "resolution_check": "end_to_end", + }, + "post_rollback_services": { + "api-gateway": {"status": "healthy", "cpu_pct": 30.0, "memory_pct": 34.0, "error_rate_pct": 1.0, "latency_ms": 38.0}, + "worker": {"status": "healthy", "cpu_pct": 34.0, "memory_pct": 36.0, "error_rate_pct": 1.0, "latency_ms": 52.0}, + }, + "post_rollback_user_impact": 0.12, + "post_rollback_slo_burn": 0.18, + "post_restart_services": {}, + "post_restart_user_impact": 0.12, + "post_restart_slo_burn": 0.18, + "post_isolate_services": { + "api-gateway": {"status": "isolated", "cpu_pct": 6.0, "memory_pct": 14.0, "error_rate_pct": 0.0, "latency_ms": 0.0}, + }, + "post_isolate_user_impact": 0.55, + "post_isolate_slo_burn": 0.60, + "degraded_services": { + "api-gateway": {"status": "degraded", "cpu_pct": 38.0, "memory_pct": 42.0, "error_rate_pct": 41.0, "latency_ms": 180.0}, + "worker": {"status": "degraded", "cpu_pct": 63.0, "memory_pct": 48.0, "error_rate_pct": 4.0, "latency_ms": 220.0}, + }, + "degraded_user_impact": 0.65, + "degraded_slo_burn": 0.72, + "failure_messages": { + "wrong_rollback_target": "The worker deploy is a log-format tweak and is not on the auth fault path.", + "low_value_restart": "Restarting a service does not fix a config/middleware regression rolled out as a deploy.", + "premature_restart": "Restarting before rolling back the gateway auth change just restarts the same bad middleware.", + "wrong_isolation_target": "Isolating workers or database cuts healthy traffic without fixing the gateway auth fault.", + }, + }, +} + +_RUNTIME_PROGRESS: dict[str, Any] | None = None + + +def get_scenario(scenario_id: str) -> dict[str, Any]: + if scenario_id not in SCENARIOS: + raise ValueError(f"Unknown scenario_id {scenario_id!r}") + return deepcopy(SCENARIOS[scenario_id]) + + +SUPPORTED_DIFFICULTIES: tuple[str, ...] = ("easy", "medium", "hard") + + +def scenario_for_difficulty(difficulty: str) -> dict[str, Any]: + for scenario in SCENARIOS.values(): + if scenario["difficulty"] == difficulty: + return deepcopy(scenario) + raise ValueError(f"Unknown difficulty {difficulty!r}") + + +def list_scenarios(difficulty: str | None = None) -> ScenarioCatalog: + if difficulty is not None and difficulty not in SUPPORTED_DIFFICULTIES: + raise ValueError(f"Unknown difficulty {difficulty!r}") + scenarios = [ + ScenarioSummary( + id=scenario["id"], + difficulty=scenario["difficulty"], + name=scenario["name"], + description=scenario["description"], + root_cause=scenario["root_cause"], + optimal_ticks=scenario["optimal_ticks"], + ) + for scenario in SCENARIOS.values() + if difficulty is None or scenario["difficulty"] == difficulty + ] + return ScenarioCatalog( + default_scenario_id=DEFAULT_SCENARIO_ID, + available_difficulties=list(SUPPORTED_DIFFICULTIES), + filtered_difficulty=difficulty, + scenarios=scenarios, + ) + + +def _worker_cascade_baseline() -> list[BaselineStep]: + return [ + BaselineStep( + action=UnifiedIncidentAction(action_type="query_deploys", service="worker"), + rationale="Check whether any recent deploy aligns with the incident start.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="query_logs", service="worker"), + rationale="Inspect worker logs because deploy timing and queue pressure suggest worker-originated harm.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="query_metrics", service="database", metric="cpu"), + rationale="Confirm that the database is overloaded as a downstream effect.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="query_dependencies", service="api-gateway"), + rationale="Verify the gateway depends on the worker and database path.", + ), + BaselineStep( + action=UnifiedIncidentAction( + action_type="submit_hypothesis", + hypothesis={ + "root_cause": "bad_worker_deploy", + "affected_services": ["worker", "database", "api-gateway"], + "confidence": 0.82, + "recommended_next_action": "rollback_deploy", + }, + ), + rationale="Commit a calibrated hypothesis before taking an invasive mitigation step.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="rollback_deploy", service="worker"), + rationale="Remove the triggering change before restarting downstream services.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="restart_service", service="database"), + rationale="Bring the database back cleanly after the root cause is removed.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="run_check", check_name="database_recovery"), + rationale="Verify the database is no longer crashing.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="run_check", check_name="end_to_end"), + rationale="Verify gateway traffic succeeds end-to-end.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="declare_resolved"), + rationale="Declare resolved only after objective checks pass.", + ), + ] + + +def _db_config_rollout_baseline() -> list[BaselineStep]: + return [ + BaselineStep( + action=UnifiedIncidentAction(action_type="query_logs", service="database"), + rationale="Database is the loudest alert; inspect logs for the actual error signature.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="query_deploys", service="database"), + rationale="Pool-acquire errors suggest a config change; check recent database rollouts.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="query_metrics", service="database", metric="error_rate"), + rationale="Confirm the error pattern is pool exhaustion rather than compute overload.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="query_logs", service="worker"), + rationale="Rule out the decoy worker deploy by reading worker logs directly.", + ), + BaselineStep( + action=UnifiedIncidentAction( + action_type="submit_hypothesis", + hypothesis={ + "root_cause": "database_only_failure", + "affected_services": ["database", "api-gateway", "worker"], + "confidence": 0.8, + "recommended_next_action": "rollback_deploy", + }, + ), + rationale="Localize the fault to the database config before remediating.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="rollback_deploy", service="database"), + rationale="Roll back the offending database config rollout.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="restart_service", service="database"), + rationale="Restart the database cleanly against the restored pool config.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="run_check", check_name="database_recovery"), + rationale="Verify database pool health and write latency are back within SLO.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="run_check", check_name="end_to_end"), + rationale="Verify gateway write-path traffic succeeds end-to-end.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="declare_resolved"), + rationale="Declare resolved only after objective checks pass.", + ), + ] + + +def _gateway_auth_rollout_baseline() -> list[BaselineStep]: + return [ + BaselineStep( + action=UnifiedIncidentAction(action_type="query_logs", service="api-gateway"), + rationale="Gateway is rejecting logins; read gateway logs to localize the rejection class.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="query_deploys", service="api-gateway"), + rationale="Login rejection aligns with a recent auth middleware rollout; confirm deploy timing.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="query_deploys", service="worker"), + rationale="Rule out the worker deploy explicitly rather than assuming.", + ), + BaselineStep( + action=UnifiedIncidentAction( + action_type="submit_hypothesis", + hypothesis={ + "root_cause": "api_gateway_fault", + "affected_services": ["api-gateway", "worker"], + "confidence": 0.85, + "recommended_next_action": "rollback_deploy", + }, + ), + rationale="Commit a calibrated hypothesis localizing to the gateway auth rollout.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="rollback_deploy", service="api-gateway"), + rationale="Roll back the bad auth middleware rollout; no restart needed.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="run_check", check_name="end_to_end"), + rationale="Verify that gateway login traffic now succeeds end-to-end.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="run_check", check_name="database_recovery"), + rationale="Confirm the database is (and stayed) healthy throughout.", + ), + BaselineStep( + action=UnifiedIncidentAction(action_type="declare_resolved"), + rationale="Declare resolved only after objective checks pass.", + ), + ] + + +_BASELINE_BUILDERS = { + "worker_deploy_cascade": _worker_cascade_baseline, + "db_config_rollout": _db_config_rollout_baseline, + "gateway_auth_rollout": _gateway_auth_rollout_baseline, +} + + +def _baseline_actions(scenario_id: str) -> list[BaselineStep]: + builder = _BASELINE_BUILDERS.get(scenario_id) + if builder is None: + raise ValueError(f"No baseline for scenario_id {scenario_id!r}") + return builder() + + +def list_baselines(scenario_id: str | None = None) -> BaselineCatalog: + if scenario_id is not None: + if scenario_id not in SCENARIOS: + raise ValueError(f"Unknown scenario_id {scenario_id!r}") + scenario_ids = [scenario_id] + else: + scenario_ids = list(SCENARIOS.keys()) + baselines = [ + BaselineDefinition( + scenario_id=current_id, + name="deterministic-remediation-baseline", + description=SCENARIOS[current_id]["description"], + optimal_ticks=SCENARIOS[current_id]["optimal_ticks"], + actions=_baseline_actions(current_id), + ) + for current_id in scenario_ids + ] + return BaselineCatalog(baselines=baselines) + + +def set_runtime_progress(progress: dict[str, Any]) -> None: + global _RUNTIME_PROGRESS + _RUNTIME_PROGRESS = deepcopy(progress) + + +def current_runtime_progress() -> dict[str, Any]: + if _RUNTIME_PROGRESS is None: + raise ValueError("Runtime progress is not initialized") + return deepcopy(_RUNTIME_PROGRESS) + + +def grade_episode(state: dict[str, Any]): + from .grader import UnifiedIncidentGrader + + scenario_id = state.get("scenario_id", DEFAULT_SCENARIO_ID) + return UnifiedIncidentGrader().build_report(state, get_scenario(scenario_id)) diff --git a/unified_incident_env/server/environment.py b/unified_incident_env/server/environment.py new file mode 100644 index 0000000000000000000000000000000000000000..8615d4bbf0a4aa0fcd6efce320bcc10c974502e8 --- /dev/null +++ b/unified_incident_env/server/environment.py @@ -0,0 +1,613 @@ +"""Honest narrow incident-remediation environment core.""" + +from __future__ import annotations + +import json +import uuid +from typing import Any + +from openenv.core.env_server import Environment +from openenv.core.env_server.types import EnvironmentMetadata + +from ..models import ( + Alert, + CheckResult, + ServiceHealth, + UnifiedIncidentAction, + UnifiedIncidentObservation, + UnifiedIncidentState, +) +from .challenge import DEFAULT_SCENARIO_ID, SCENARIOS, get_scenario, scenario_for_difficulty, set_runtime_progress +from .grader import UnifiedIncidentGrader + +SERVICE_ORDER = ("api-gateway", "cache", "database", "worker") +ALL_ACTIONS = [ + "query_logs", + "query_metrics", + "query_dependencies", + "query_deploys", + "rollback_deploy", + "restart_service", + "run_check", + "isolate_service", + "escalate", + "submit_hypothesis", + "declare_resolved", +] +REQUIRED_FIELDS_BY_ACTION: dict[str, list[str]] = { + "query_logs": ["service"], + "query_metrics": ["service", "metric"], + "query_dependencies": ["service"], + "query_deploys": ["service"], + "rollback_deploy": ["service"], + "restart_service": ["service"], + "run_check": ["check_name"], + "isolate_service": ["service"], + "escalate": [], + "submit_hypothesis": ["hypothesis"], + "declare_resolved": [], +} +STATUS_VALUES = { + "healthy": 1.0, + "degraded": 0.4, + "crashed": 0.0, + "isolated": 0.2, +} + + +class UnifiedIncidentEnvironment(Environment[UnifiedIncidentAction, UnifiedIncidentObservation, UnifiedIncidentState]): + """A bounded-action incident diagnosis and safe remediation environment.""" + + SUPPORTS_CONCURRENT_SESSIONS = False + + def __init__(self) -> None: + super().__init__() + self._grader = UnifiedIncidentGrader() + self._episode = self._make_episode(get_scenario(DEFAULT_SCENARIO_ID)) + set_runtime_progress(self._state_dict()) + + def get_metadata(self) -> EnvironmentMetadata: + return EnvironmentMetadata( + name="unified_incident_env", + description=( + "A narrow incident diagnosis and safe remediation environment with bounded actions, " + "world-state transitions, explicit checks, and effect-based rewards." + ), + version="2.0.0", + author="Daksh Verma", + ) + + def reset(self, seed: int | None = None, episode_id: str | None = None, **kwargs: Any) -> UnifiedIncidentObservation: + del seed + scenario_id = kwargs.get("scenario_id") + difficulty = kwargs.get("difficulty") + if scenario_id: + scenario = get_scenario(scenario_id) + elif difficulty: + scenario = scenario_for_difficulty(difficulty) + else: + scenario = get_scenario(DEFAULT_SCENARIO_ID) + self._episode = self._make_episode(scenario, episode_id=episode_id) + set_runtime_progress(self._state_dict()) + return self._build_observation( + last_action_result="Episode reset.", + tool_output=None, + reward=0.0, + done=False, + ) + + def step(self, action: UnifiedIncidentAction | dict[str, Any], timeout_s: float | None = None, **kwargs: Any) -> UnifiedIncidentObservation: + del timeout_s, kwargs + if isinstance(action, dict): + action = UnifiedIncidentAction(**action) + + if self._episode["done"]: + return self._build_observation( + last_action_result="Episode complete. Reset to start another run.", + tool_output=None, + reward=0.0, + done=True, + ) + + self._episode["tick"] += 1 + self._episode["step_count"] += 1 + before_potential = self._incident_health_potential() + base_step_cost = float(self._episode["scenario"]["reward_config"]["step_cost"]) + penalty = 0.0 + bonus = 0.0 + tool_output: str | None = None + state_changed = False + useful_observation = False + + self._episode["failure_type"] = None + self._episode["why_failed"] = None + self._episode["loop_warning"] = None + + if action.action_type == "query_logs": + tool_output = self._query_logs(action.service) + useful_observation = self._mark_evidence_once(f"logs:{action.service}", tool_output) + last_action_result = f"Queried logs for {action.service}." + elif action.action_type == "query_metrics": + tool_output = self._query_metrics(action.service, action.metric) + useful_observation = self._mark_evidence_once(f"metrics:{action.service}:{action.metric}", tool_output) + last_action_result = f"Queried {action.metric} for {action.service}." + elif action.action_type == "query_dependencies": + tool_output = self._query_dependencies(action.service) + useful_observation = self._mark_evidence_once(f"deps:{action.service}", tool_output) + last_action_result = f"Queried dependencies for {action.service}." + elif action.action_type == "query_deploys": + tool_output = self._query_deploys(action.service) + useful_observation = self._mark_evidence_once(f"deploys:{action.service}", tool_output) + last_action_result = f"Queried deploy history for {action.service}." + elif action.action_type == "submit_hypothesis": + bonus, useful_observation, last_action_result = self._submit_hypothesis(action) + elif action.action_type == "rollback_deploy": + state_changed, penalty, last_action_result = self._rollback_deploy(action.service) + elif action.action_type == "restart_service": + state_changed, penalty, last_action_result = self._restart_service(action.service) + elif action.action_type == "isolate_service": + state_changed, penalty, last_action_result = self._isolate_service(action.service) + elif action.action_type == "run_check": + tool_output, useful_observation, last_action_result = self._run_check(action.check_name) + elif action.action_type == "escalate": + useful_observation = self._mark_evidence_once( + f"escalate:{self._episode['tick']}", + "Escalation note recorded: expert attention requested while keeping the environment state unchanged.", + ) + last_action_result = "Escalated for human attention." + tool_output = "Escalation does not fix the incident, but records that expert attention was requested." + elif action.action_type == "declare_resolved": + resolved, penalty, bonus, last_action_result = self._declare_resolved() + state_changed = resolved + else: + last_action_result = f"Unsupported action {action.action_type!r}." + penalty += self._unsafe_penalty() + self._set_failure("unsupported_action", "That action is not part of this honest narrow environment.") + + self._advance_world() + self._refresh_alerts() + self._update_loop_feedback(action, useful_observation or state_changed) + after_potential = self._incident_health_potential() + + reward = -base_step_cost + (after_potential - before_potential) + bonus - penalty + if not useful_observation and not state_changed and bonus <= 0.0: + self._episode["wasteful_ticks"] += 1 + + if self._episode["tick"] >= self._episode["max_ticks"] and not self._episode["done"]: + self._episode["done"] = True + last_action_result = f"{last_action_result} Tick budget exhausted.".strip() + + self._episode["last_action_result"] = last_action_result + self._episode["workflow_stage"] = self._workflow_stage() + self._episode["score_breakdown"] = self._grader.compute_breakdown(self._state_dict(), self._episode["scenario"]) + self._episode["final_score"] = self._episode["score_breakdown"]["final_score"] + self._episode["cumulative_reward"] = round(self._episode["cumulative_reward"] + reward, 4) + + set_runtime_progress(self._state_dict()) + return self._build_observation( + last_action_result=last_action_result, + tool_output=tool_output, + reward=round(reward, 4), + done=self._episode["done"], + ) + + @property + def state(self) -> UnifiedIncidentState: + return UnifiedIncidentState(**self._state_dict()) + + def _make_episode(self, scenario: dict[str, Any], episode_id: str | None = None) -> dict[str, Any]: + services = { + name: ServiceHealth(name=name, **payload) + for name, payload in scenario["initial_services"].items() + } + checks = { + "database_recovery": CheckResult(name="database_recovery", passed=False, detail="Database recovery has not been verified yet."), + "end_to_end": CheckResult(name="end_to_end", passed=False, detail="End-to-end health has not been verified yet."), + } + recipe = scenario.get("remediation_recipe", {}) + rollback_target = recipe.get("rollback_target", "worker") + recent_deploy_service = rollback_target if rollback_target in scenario["deploy_history"] else "worker" + return { + "episode_id": episode_id or str(uuid.uuid4()), + "scenario": scenario, + "tick": 0, + "step_count": 0, + "max_ticks": scenario["max_ticks"], + "difficulty": scenario["difficulty"], + "services": services, + "alerts": [Alert(**payload) for payload in scenario["initial_alerts"]], + "discovered_evidence": [], + "evidence_seen": set(), + "recent_deploys": [scenario["deploy_history"].get(recent_deploy_service, "")], + "checks": checks, + "user_impact": scenario.get("degraded_user_impact", 0.82), + "slo_burn_rate": scenario.get("degraded_slo_burn", 0.91), + "containment_applied": False, + "cause_removed": False, + "isolated_service": None, + "hypothesis_seen": set(), + "failure_type": None, + "why_failed": None, + "loop_warning": None, + "last_action_key": None, + "repeat_count": 0, + "incident_resolved": False, + "workflow_stage": "triage", + "cumulative_reward": 0.0, + "wasteful_ticks": 0, + "score_breakdown": { + "recovery_score": 0.0, + "containment_score": 0.0, + "verification_score": 0.0, + "impact_score": 0.0, + "efficiency_score": 0.10, + "final_score": 0.10, + }, + "final_score": 0.10, + "last_action_result": "", + "done": False, + } + + def _query_logs(self, service: str | None) -> str: + assert service is not None + return self._episode["scenario"]["logs"][service] + + def _query_metrics(self, service: str | None, metric: str | None) -> str: + assert service is not None and metric is not None + return self._episode["scenario"]["metrics"][service][metric] + + def _query_dependencies(self, service: str | None) -> str: + assert service is not None + return self._episode["scenario"]["dependencies"][service] + + def _query_deploys(self, service: str | None) -> str: + assert service is not None + return self._episode["scenario"]["deploy_history"][service] + + def _submit_hypothesis(self, action: UnifiedIncidentAction) -> tuple[float, bool, str]: + assert action.hypothesis is not None + normalized = json.dumps(action.hypothesis.model_dump(), sort_keys=True) + if normalized in self._episode["hypothesis_seen"]: + return 0.0, False, "Repeated hypothesis recorded with no additional reward." + self._episode["hypothesis_seen"].add(normalized) + truth = self._episode["scenario"]["truth"] + payload = action.hypothesis + cause_match = 1.0 if payload.root_cause == truth["root_cause"] else 0.0 + service_match = len(set(payload.affected_services) & set(truth["affected_services"])) / len(set(truth["affected_services"])) + action_quality = 1.0 if payload.recommended_next_action == truth["best_next_action"] else -0.4 + if cause_match == 1.0: + calibration = 1.0 if payload.confidence >= 0.7 else 0.5 + else: + calibration = -1.0 if payload.confidence >= 0.7 else -0.2 + reward = (0.04 * cause_match) + (0.03 * service_match) + (0.03 * action_quality) + (0.02 * calibration) + return round(reward, 4), True, "Hypothesis recorded. Reward reflects root-cause accuracy, service localization, confidence calibration, and next-action quality." + + def _recipe(self) -> dict[str, Any]: + return self._episode["scenario"].get("remediation_recipe", {}) + + def _failure_message(self, key: str, default: str) -> str: + return self._episode["scenario"].get("failure_messages", {}).get(key, default) + + def _apply_service_updates(self, updates: dict[str, dict[str, Any]]) -> None: + for name, payload in updates.items(): + self._episode["services"][name] = ServiceHealth(name=name, **payload) + + def _rollback_deploy(self, service: str | None) -> tuple[bool, float, str]: + assert service is not None + recipe = self._recipe() + rollback_target = recipe.get("rollback_target") + if rollback_target is None or service != rollback_target: + self._set_failure( + "wrong_remediation_target", + self._failure_message("wrong_rollback_target", "Rolling back a service without a causal link wastes time and risk."), + ) + return False, self._unsafe_penalty(), f"Rollback on {service} did not address the incident." + if self._episode["cause_removed"]: + return False, 0.0, f"{rollback_target} deploy is already rolled back." + self._episode["cause_removed"] = True + self._episode["containment_applied"] = True + self._apply_service_updates(self._episode["scenario"].get("post_rollback_services", {})) + scenario = self._episode["scenario"] + self._episode["user_impact"] = min(self._episode["user_impact"], scenario.get("post_rollback_user_impact", self._episode["user_impact"])) + self._episode["slo_burn_rate"] = min(self._episode["slo_burn_rate"], scenario.get("post_rollback_slo_burn", self._episode["slo_burn_rate"])) + return True, 0.0, f"Rolled back the {rollback_target} deploy; the underlying cause is removed." + + def _restart_service(self, service: str | None) -> tuple[bool, float, str]: + assert service is not None + recipe = self._recipe() + restart_target = recipe.get("restart_target") + if restart_target is None or service != restart_target: + self._set_failure( + "low_value_restart", + self._failure_message("low_value_restart", f"Restarting {service} is not the safe next remediation step for this incident."), + ) + return False, self._unsafe_penalty() / 2, f"Restarting {service} had little or no positive effect." + if recipe.get("restart_requires_cause_removed", True) and not self._episode["cause_removed"]: + self._set_failure( + "premature_restart", + self._failure_message("premature_restart", f"Restarting {service} before removing the trigger only causes another failure."), + ) + return False, self._unsafe_penalty(), f"Restart of {service} failed because the triggering cause is still present." + self._apply_service_updates(self._episode["scenario"].get("post_restart_services", {})) + scenario = self._episode["scenario"] + self._episode["user_impact"] = scenario.get("post_restart_user_impact", self._episode["user_impact"]) + self._episode["slo_burn_rate"] = scenario.get("post_restart_slo_burn", self._episode["slo_burn_rate"]) + return True, 0.0, f"{service} restarted cleanly after the triggering cause was removed." + + def _isolate_service(self, service: str | None) -> tuple[bool, float, str]: + assert service is not None + recipe = self._recipe() + isolate_target = recipe.get("isolate_target") + if isolate_target is None or service != isolate_target: + self._set_failure( + "wrong_isolation_target", + self._failure_message("wrong_isolation_target", f"Isolating {service} does not contain the dominant failure path."), + ) + return False, self._unsafe_penalty() / 2, f"Isolation of {service} did not materially reduce blast radius." + if self._episode["isolated_service"] == isolate_target: + return False, 0.0, f"{isolate_target} is already isolated." + self._episode["isolated_service"] = isolate_target + self._episode["containment_applied"] = True + self._apply_service_updates(self._episode["scenario"].get("post_isolate_services", {})) + scenario = self._episode["scenario"] + self._episode["user_impact"] = scenario.get("post_isolate_user_impact", self._episode["user_impact"]) + self._episode["slo_burn_rate"] = scenario.get("post_isolate_slo_burn", self._episode["slo_burn_rate"]) + return True, 0.0, f"{isolate_target} isolated. Blast radius shrank, but full resolution still requires addressing the root cause." + + def _run_check(self, check_name: str | None) -> tuple[str, bool, str]: + assert check_name is not None + recipe = self._recipe() + isolated = self._episode["isolated_service"] + cause_removed = self._episode["cause_removed"] + services = self._episode["services"] + if check_name == "database_recovery": + db_healthy = services["database"].status == "healthy" + incident_driver = recipe.get("incident_driver") + if incident_driver in {"worker", "database"}: + passed = db_healthy and cause_removed + else: + passed = db_healthy + detail = ( + "Database is healthy and no longer failing." + if passed + else "Database is still unstable or the triggering cause is still present." + ) + else: + gateway_healthy = services["api-gateway"].status == "healthy" + db_healthy = services["database"].status == "healthy" + worker_healthy = services["worker"].status == "healthy" + passed = ( + gateway_healthy + and db_healthy + and worker_healthy + and cause_removed + and isolated is None + ) + detail = ( + "End-to-end login traffic is healthy." + if passed + else "End-to-end traffic still fails or remains degraded." + ) + self._episode["checks"][check_name] = CheckResult(name=check_name, passed=passed, detail=detail) + useful = self._mark_evidence_once(f"check:{check_name}:{passed}", detail) + return detail, useful, f"Ran {check_name} check." + + def _declare_resolved(self) -> tuple[bool, float, float, str]: + checks = self._episode["checks"] + resolution_check = self._recipe().get("resolution_check", "end_to_end") + safe_to_resolve = bool(checks.get(resolution_check) and checks[resolution_check].passed) + if not safe_to_resolve: + self._set_failure("premature_resolution", "The incident is not verified as resolved yet.") + return False, self._episode["scenario"]["reward_config"]["premature_resolution_penalty"], 0.0, "Resolution declaration rejected: required checks have not passed." + self._episode["incident_resolved"] = True + self._episode["done"] = True + return True, 0.0, self._episode["scenario"]["reward_config"]["successful_resolution_bonus"], "Incident declared resolved after passing objective checks." + + def _mark_evidence_once(self, key: str, detail: str) -> bool: + if key in self._episode["evidence_seen"]: + return False + self._episode["evidence_seen"].add(key) + self._episode["discovered_evidence"].append(detail) + return True + + def _unsafe_penalty(self) -> float: + return float(self._episode["scenario"]["reward_config"]["unsafe_action_penalty"]) + + def _set_failure(self, failure_type: str, why_failed: str) -> None: + self._episode["failure_type"] = failure_type + self._episode["why_failed"] = why_failed + + def _advance_world(self) -> None: + cause_removed = self._episode["cause_removed"] + isolated = self._episode["isolated_service"] + if not cause_removed and isolated is None: + self._apply_service_updates(self._episode["scenario"].get("degraded_services", {})) + scenario = self._episode["scenario"] + self._episode["user_impact"] = max(self._episode["user_impact"], scenario.get("degraded_user_impact", self._episode["user_impact"])) + self._episode["slo_burn_rate"] = max(self._episode["slo_burn_rate"], scenario.get("degraded_slo_burn", self._episode["slo_burn_rate"])) + if isolated is not None and not cause_removed: + self._episode["containment_applied"] = True + self._episode["workflow_stage"] = self._workflow_stage() + + def _refresh_alerts(self) -> None: + alerts: list[Alert] = [] + for service_name in SERVICE_ORDER: + service = self._episode["services"][service_name] + if service.status == "crashed": + alerts.append(Alert(service=service_name, severity="critical", message=f"{service_name} is unavailable.")) + elif service.status == "degraded": + alerts.append(Alert(service=service_name, severity="warning", message=f"{service_name} is degraded.")) + if self._episode["user_impact"] >= 0.3 and not any(alert.service == "api-gateway" for alert in alerts): + alerts.append(Alert(service="api-gateway", severity="warning", message="User-visible impact remains elevated.")) + self._episode["alerts"] = alerts + + def _update_loop_feedback(self, action: UnifiedIncidentAction, progressed: bool) -> None: + action_key = repr(action.model_dump(exclude_none=True)) + if progressed: + self._episode["last_action_key"] = action_key + self._episode["repeat_count"] = 0 + return + if self._episode["last_action_key"] == action_key: + self._episode["repeat_count"] += 1 + else: + self._episode["repeat_count"] = 1 + self._episode["last_action_key"] = action_key + if self._episode["repeat_count"] >= 2: + self._episode["loop_warning"] = "The same no-progress action has repeated; choose a different evidence source or remediation step." + + def _workflow_stage(self) -> str: + if self._episode["incident_resolved"]: + return "resolved" + checks = self._episode["checks"] + if checks["database_recovery"].passed or checks["end_to_end"].passed: + return "validation" + if self._episode["containment_applied"] or self._episode["cause_removed"] or self._episode["isolated_service"] is not None: + return "mitigation" + return "triage" + + def _allowed_actions(self) -> list[str]: + return list(ALL_ACTIONS) + + def _required_fields_by_action(self) -> dict[str, list[str]]: + return {action: REQUIRED_FIELDS_BY_ACTION[action] for action in self._allowed_actions()} + + def _progress_flags(self) -> dict[str, bool]: + checks = self._episode["checks"] + return { + "containment_applied": self._episode["containment_applied"], + "cause_removed": self._episode["cause_removed"], + "database_recovery": checks["database_recovery"].passed, + "end_to_end": checks["end_to_end"].passed, + "incident_resolved": self._episode["incident_resolved"], + "isolation_applied": self._episode["isolated_service"] is not None, + } + + def _incident_summary(self) -> str: + description = self._episode["scenario"].get("description") + if description: + return description + return ( + "An incident is degrading user traffic. Use evidence-gathering actions to diagnose, " + "then choose a safe remediation and verify with explicit checks." + ) + + def _prompt_text(self, tool_output: str | None) -> str: + lines = [ + f"TICK {self._episode['tick']}/{self._episode['max_ticks']}", + f"WORKFLOW_STAGE: {self._episode['workflow_stage']}", + "", + "INCIDENT_SUMMARY:", + self._incident_summary(), + "", + "ACTIVE_ALERTS:", + ] + if self._episode["alerts"]: + lines.extend(f"- [{alert.severity.upper()}] {alert.service}: {alert.message}" for alert in self._episode["alerts"]) + else: + lines.append("- none") + lines.extend([ + "", + "SERVICES:", + ]) + for service_name in SERVICE_ORDER: + health = self._episode["services"][service_name] + lines.append( + f"- {service_name}: {health.status} cpu={health.cpu_pct:.1f} mem={health.memory_pct:.1f} err={health.error_rate_pct:.1f} latency={health.latency_ms:.1f}" + ) + lines.extend([ + "", + f"USER_IMPACT: {self._episode['user_impact']:.2f}", + f"SLO_BURN_RATE: {self._episode['slo_burn_rate']:.2f}", + f"LAST_ACTION_RESULT: {self._episode['last_action_result'] or 'none'}", + f"TOOL_OUTPUT: {tool_output or 'none'}", + f"FAILURE_TYPE: {self._episode['failure_type'] or 'none'}", + f"WHY_FAILED: {self._episode['why_failed'] or 'none'}", + "", + "CHECKS:", + ]) + for check in self._episode["checks"].values(): + lines.append(f"- {check.name}: {'passed' if check.passed else 'pending'} - {check.detail}") + lines.extend([ + "", + "ALLOWED_ACTIONS:", + ]) + lines.extend(f"- {action}" for action in self._allowed_actions()) + return "\n".join(lines) + + def _incident_health_potential(self) -> float: + weights = self._episode["scenario"]["critical_service_weights"] + services = self._episode["services"] + operational = sum(weights.get(name, 0.0) * STATUS_VALUES[services[name].status] for name in weights) + impact_relief = 1.0 - self._episode["user_impact"] + burn_relief = 1.0 - self._episode["slo_burn_rate"] + containment = 1.0 if self._episode["containment_applied"] else 0.0 + return round((0.55 * operational) + (0.2 * impact_relief) + (0.15 * burn_relief) + (0.10 * containment), 4) + + def _state_dict(self) -> dict[str, Any]: + return { + "episode_id": self._episode["episode_id"], + "step_count": self._episode["step_count"], + "scenario_id": self._episode["scenario"]["id"], + "difficulty": self._episode["difficulty"], + "current_tick": self._episode["tick"], + "max_ticks": self._episode["max_ticks"], + "workflow_stage": self._episode["workflow_stage"], + "active_alerts": [alert.model_dump() for alert in self._episode["alerts"]], + "service_health": {name: service.model_dump() for name, service in self._episode["services"].items()}, + "discovered_evidence": list(self._episode["discovered_evidence"]), + "recent_deploys": list(self._episode["recent_deploys"]), + "checks": [check.model_dump() for check in self._episode["checks"].values()], + "user_impact": self._episode["user_impact"], + "slo_burn_rate": self._episode["slo_burn_rate"], + "incident_resolved": self._episode["incident_resolved"], + "containment_applied": self._episode["containment_applied"], + "allowed_actions": self._allowed_actions(), + "required_fields_by_action": self._required_fields_by_action(), + "valid_action_example": None, + "progress_flags": self._progress_flags(), + "final_score": self._episode["final_score"], + "score_breakdown": dict(self._episode["score_breakdown"]), + "cumulative_reward": self._episode["cumulative_reward"], + "wasteful_ticks": self._episode["wasteful_ticks"], + "last_action_result": self._episode["last_action_result"], + "failure_type": self._episode["failure_type"], + "why_failed": self._episode["why_failed"], + } + + def _build_observation(self, last_action_result: str, tool_output: str | None, reward: float, done: bool) -> UnifiedIncidentObservation: + return UnifiedIncidentObservation( + prompt_text=self._prompt_text(tool_output), + incident_summary=self._incident_summary(), + tick_count=self._episode["tick"], + max_ticks=self._episode["max_ticks"], + difficulty=self._episode["difficulty"], + workflow_stage=self._episode["workflow_stage"], + active_alerts=list(self._episode["alerts"]), + service_health=dict(self._episode["services"]), + discovered_evidence=list(self._episode["discovered_evidence"]), + recent_deploys=list(self._episode["recent_deploys"]), + checks=list(self._episode["checks"].values()), + user_impact=self._episode["user_impact"], + slo_burn_rate=self._episode["slo_burn_rate"], + incident_resolved=self._episode["incident_resolved"], + containment_applied=self._episode["containment_applied"], + last_action_result=last_action_result, + tool_output=tool_output, + failure_type=self._episode["failure_type"], + why_failed=self._episode["why_failed"], + allowed_actions=self._allowed_actions(), + required_fields_by_action=self._required_fields_by_action(), + valid_action_example=None, + common_trap=self._episode["scenario"].get("description"), + loop_warning=self._episode["loop_warning"], + blocked_until_security_complete=False, + security_unlock_reason=None, + best_recovery_action_family=None, + progress_flags=self._progress_flags(), + security_subquest_status=None, + security_context={}, + final_score=self._episode["final_score"], + score_breakdown=dict(self._episode["score_breakdown"]), + reward=round(reward, 4), + done=done, + ) diff --git a/unified_incident_env/server/grader.py b/unified_incident_env/server/grader.py new file mode 100644 index 0000000000000000000000000000000000000000..428514e9861eff58041bf106a8d8d46ac38333b7 --- /dev/null +++ b/unified_incident_env/server/grader.py @@ -0,0 +1,145 @@ +"""Deterministic public scoring for the honest narrow incident-remediation environment.""" + +from __future__ import annotations + +from typing import Any + +from ..models import GraderCheck, GraderReport + +MIN_PUBLIC_SCORE = 0.01 +MAX_PUBLIC_SCORE = 0.99 + + +def _strict_public_score(score: float) -> float: + return round(min(MAX_PUBLIC_SCORE, max(MIN_PUBLIC_SCORE, score)), 4) + + +def _service_score(status: str) -> float: + return { + "healthy": 1.0, + "degraded": 0.4, + "crashed": 0.0, + "isolated": 0.2, + }.get(status, 0.0) + + +class UnifiedIncidentGrader: + """Deterministic scorer focused on executed effects, not scripted clues.""" + + def compute_breakdown( + self, + state: dict[str, Any], + scenario: dict[str, Any], + ) -> dict[str, float]: + services = state.get("service_health", {}) + weights = scenario["critical_service_weights"] + recovery_score = round( + sum( + weights.get(service, 0.0) * _service_score((services.get(service) or {}).get("status", "crashed")) + for service in weights + ), + 4, + ) + + containment_score = 0.2 if state.get("containment_applied") else 0.0 + if state.get("containment_applied") and (services.get("worker") or {}).get("status") == "healthy": + containment_score = 0.3 + + checks = {item.get("name"): bool(item.get("passed")) for item in state.get("checks", [])} + verification_score = 0.0 + if checks.get("database_recovery"): + verification_score += 0.15 + if checks.get("end_to_end"): + verification_score += 0.2 + + user_impact = float(state.get("user_impact", 1.0)) + impact_score = round(max(0.0, 0.15 * (1.0 - user_impact)), 4) + + wasteful_ticks = int(state.get("wasteful_ticks", 0)) + efficiency_score = round(max(0.0, 0.10 - (0.01 * wasteful_ticks)), 4) + + final_score = _strict_public_score( + recovery_score + containment_score + verification_score + impact_score + efficiency_score + ) + + return { + "recovery_score": recovery_score, + "containment_score": round(containment_score, 4), + "verification_score": round(verification_score, 4), + "impact_score": impact_score, + "efficiency_score": efficiency_score, + "final_score": final_score, + } + + def build_report(self, state: dict[str, Any], scenario: dict[str, Any]) -> GraderReport: + breakdown = self.compute_breakdown(state, scenario) + checks = {item.get("name"): bool(item.get("passed")) for item in state.get("checks", [])} + passed = bool( + state.get("incident_resolved") + and checks.get("database_recovery") + and checks.get("end_to_end") + ) + report_checks = [ + GraderCheck( + name="root_cause_removed", + passed=bool(state.get("containment_applied")), + detail=( + "The root cause has been safely contained or removed." + if state.get("containment_applied") + else "The root cause is still active or only partially contained." + ), + weight=0.30, + ), + GraderCheck( + name="database_recovery", + passed=checks.get("database_recovery", False), + detail=( + "The database recovery check passed." + if checks.get("database_recovery") + else "The database recovery check has not passed yet." + ), + weight=0.20, + ), + GraderCheck( + name="end_to_end_check", + passed=checks.get("end_to_end", False), + detail=( + "The end-to-end service check passed." + if checks.get("end_to_end") + else "The end-to-end service check has not passed yet." + ), + weight=0.20, + ), + GraderCheck( + name="critical_services_recovered", + passed=breakdown["recovery_score"] >= 0.8, + detail=( + "Critical-path services are recovered." + if breakdown["recovery_score"] >= 0.8 + else "Critical-path services are still degraded or crashed." + ), + weight=0.20, + ), + GraderCheck( + name="declare_resolved", + passed=bool(state.get("incident_resolved")), + detail=( + "The agent declared the incident resolved after objective checks passed." + if state.get("incident_resolved") + else "The incident has not been safely declared resolved." + ), + weight=0.10, + ), + ] + return GraderReport( + scenario_id=scenario["id"], + passed=passed, + score=breakdown["final_score"], + message=( + "Incident diagnosed, remediated, and verified honestly." + if passed + else "Incident is not yet safely resolved." + ), + breakdown=breakdown, + checks=report_checks, + ) diff --git a/unified_incident_env/tests/__init__.py b/unified_incident_env/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..6ffff57bca34c64e34e03f88d6f6aa5b4f9904ac --- /dev/null +++ b/unified_incident_env/tests/__init__.py @@ -0,0 +1 @@ +"""Tests for the unified incident environment.""" diff --git a/unified_incident_env/tests/test_environment.py b/unified_incident_env/tests/test_environment.py new file mode 100644 index 0000000000000000000000000000000000000000..8358ed0ae73c88a00359c38ddaac38890ca5e865 --- /dev/null +++ b/unified_incident_env/tests/test_environment.py @@ -0,0 +1,192 @@ +"""Behavior and API tests for the honest narrow incident environment.""" + +from __future__ import annotations + +from fastapi.testclient import TestClient + +from unified_incident_env.models import HypothesisPayload, UnifiedIncidentAction +from unified_incident_env.server import app as app_module +from unified_incident_env.server.challenge import DEFAULT_SCENARIO_ID, list_baselines +from unified_incident_env.server.environment import UnifiedIncidentEnvironment + + +def _run_baseline(env: UnifiedIncidentEnvironment): + env.reset(scenario_id=DEFAULT_SCENARIO_ID) + last = None + for step in list_baselines(DEFAULT_SCENARIO_ID).baselines[0].actions: + last = env.step(step.action) + return last + + +def test_baseline_resolves_honestly() -> None: + env = UnifiedIncidentEnvironment() + obs = _run_baseline(env) + assert obs is not None + assert obs.done is True + assert obs.incident_resolved is True + checks = {check.name: check.passed for check in obs.checks} + assert checks["database_recovery"] is True + assert checks["end_to_end"] is True + assert obs.final_score > 0.7 + + +def test_query_deploys_reveals_evidence_but_not_positive_reward() -> None: + env = UnifiedIncidentEnvironment() + env.reset(scenario_id=DEFAULT_SCENARIO_ID) + obs = env.step(UnifiedIncidentAction(action_type="query_deploys", service="worker")) + assert obs.reward <= 0.0 + assert "worker@2026.04.23-bad" in (obs.tool_output or "") + assert obs.incident_resolved is False + + +def test_restart_database_before_rollback_is_negative() -> None: + env = UnifiedIncidentEnvironment() + env.reset(scenario_id=DEFAULT_SCENARIO_ID) + obs = env.step(UnifiedIncidentAction(action_type="restart_service", service="database")) + assert obs.reward < 0.0 + assert obs.failure_type == "premature_restart" + assert obs.incident_resolved is False + assert obs.service_health["database"].status == "crashed" + + +def test_duplicate_hypothesis_bonus_is_not_farmable() -> None: + env = UnifiedIncidentEnvironment() + env.reset(scenario_id=DEFAULT_SCENARIO_ID) + action = UnifiedIncidentAction( + action_type="submit_hypothesis", + hypothesis=HypothesisPayload( + root_cause="bad_worker_deploy", + affected_services=["worker", "database", "api-gateway"], + confidence=0.82, + recommended_next_action="rollback_deploy", + ), + ) + first = env.step(action) + second = env.step(action) + assert first.reward > second.reward + assert second.reward <= 0.0 + + +def test_isolating_worker_contains_but_does_not_resolve() -> None: + env = UnifiedIncidentEnvironment() + env.reset(scenario_id=DEFAULT_SCENARIO_ID) + isolated = env.step(UnifiedIncidentAction(action_type="isolate_service", service="worker")) + assert isolated.containment_applied is True + assert isolated.incident_resolved is False + checked = env.step(UnifiedIncidentAction(action_type="run_check", check_name="end_to_end")) + checks = {check.name: check.passed for check in checked.checks} + assert checks["end_to_end"] is False + + +def test_declare_resolved_requires_checks() -> None: + env = UnifiedIncidentEnvironment() + env.reset(scenario_id=DEFAULT_SCENARIO_ID) + obs = env.step(UnifiedIncidentAction(action_type="declare_resolved")) + assert obs.reward < 0.0 + assert obs.done is False + assert obs.failure_type == "premature_resolution" + + +def test_observation_exposes_bounded_actions_without_valid_example() -> None: + env = UnifiedIncidentEnvironment() + obs = env.reset(scenario_id=DEFAULT_SCENARIO_ID) + assert obs.allowed_actions == [ + "query_logs", + "query_metrics", + "query_dependencies", + "query_deploys", + "rollback_deploy", + "restart_service", + "run_check", + "isolate_service", + "escalate", + "submit_hypothesis", + "declare_resolved", + ] + assert obs.valid_action_example is None + + +def test_routes_expose_new_catalog_and_status(monkeypatch) -> None: + monkeypatch.setenv("ENABLE_WEB_INTERFACE", "false") + client = TestClient(app_module.create_compatible_app()) + + tasks = client.get("/tasks") + assert tasks.status_code == 200 + payload = tasks.json() + assert payload["default_scenario_id"] == DEFAULT_SCENARIO_ID + scenarios_by_difficulty = {scenario["difficulty"] for scenario in payload["scenarios"]} + assert {"easy", "medium", "hard"}.issubset(scenarios_by_difficulty) + assert {"easy", "medium", "hard"}.issubset(set(payload["available_difficulties"])) + + baseline = client.get("/baseline") + assert baseline.status_code == 200 + baseline_payload = baseline.json() + baseline_ids = {item["scenario_id"] for item in baseline_payload["baselines"]} + assert {"worker_deploy_cascade", "db_config_rollout", "gateway_auth_rollout"}.issubset(baseline_ids) + + health = client.get("/health") + assert health.status_code == 200 + assert health.json()["status"] in {"ok", "healthy"} + + status = client.get("/status") + assert status.status_code == 200 + status_payload = status.json() + assert status_payload["progress"]["scenario_id"] == DEFAULT_SCENARIO_ID + assert status_payload["grader"]["score"] > 0.0 + + +def _run_baseline_for_scenario(scenario_id: str): + env = UnifiedIncidentEnvironment() + env.reset(scenario_id=scenario_id) + last = None + for step in list_baselines(scenario_id).baselines[0].actions: + last = env.step(step.action) + return last + + +def test_medium_baseline_resolves_honestly() -> None: + obs = _run_baseline_for_scenario("db_config_rollout") + assert obs is not None + assert obs.done is True + assert obs.incident_resolved is True + checks = {check.name: check.passed for check in obs.checks} + assert checks["database_recovery"] is True + assert checks["end_to_end"] is True + assert obs.final_score > 0.7 + + +def test_hard_baseline_resolves_honestly() -> None: + obs = _run_baseline_for_scenario("gateway_auth_rollout") + assert obs is not None + assert obs.done is True + assert obs.incident_resolved is True + checks = {check.name: check.passed for check in obs.checks} + assert checks["end_to_end"] is True + assert obs.final_score > 0.7 + + +def test_medium_wrong_rollback_target_is_penalized() -> None: + env = UnifiedIncidentEnvironment() + env.reset(scenario_id="db_config_rollout") + obs = env.step(UnifiedIncidentAction(action_type="rollback_deploy", service="worker")) + assert obs.reward < 0.0 + assert obs.failure_type == "wrong_remediation_target" + assert obs.incident_resolved is False + + +def test_hard_wrong_rollback_target_is_penalized() -> None: + env = UnifiedIncidentEnvironment() + env.reset(scenario_id="gateway_auth_rollout") + obs = env.step(UnifiedIncidentAction(action_type="rollback_deploy", service="worker")) + assert obs.reward < 0.0 + assert obs.failure_type == "wrong_remediation_target" + + +def test_hard_does_not_require_database_recovery_check() -> None: + env = UnifiedIncidentEnvironment() + env.reset(scenario_id="gateway_auth_rollout") + env.step(UnifiedIncidentAction(action_type="rollback_deploy", service="api-gateway")) + end_to_end = env.step(UnifiedIncidentAction(action_type="run_check", check_name="end_to_end")) + assert any(check.name == "end_to_end" and check.passed for check in end_to_end.checks) + resolved = env.step(UnifiedIncidentAction(action_type="declare_resolved")) + assert resolved.incident_resolved is True diff --git a/unified_incident_env/tests/test_submission_inference.py b/unified_incident_env/tests/test_submission_inference.py new file mode 100644 index 0000000000000000000000000000000000000000..1bf0c76d2bf6aa5358e443fb0ef56cd9841705fc --- /dev/null +++ b/unified_incident_env/tests/test_submission_inference.py @@ -0,0 +1,119 @@ +from __future__ import annotations + +import inference +from unified_incident_env.models import Alert, CheckResult, ServiceHealth, UnifiedIncidentObservation + + +def make_observation(**overrides: object) -> UnifiedIncidentObservation: + defaults = { + "prompt_text": "Honest incident prompt", + "incident_summary": "Worker deploy is overloading the database.", + "tick_count": 0, + "max_ticks": 12, + "difficulty": "easy", + "workflow_stage": "triage", + "active_alerts": [ + Alert(service="database", severity="critical", message="database crashing"), + Alert(service="worker", severity="warning", message="worker retry volume elevated"), + ], + "service_health": { + "api-gateway": ServiceHealth(name="api-gateway", status="degraded", cpu_pct=61.0, memory_pct=38.0, error_rate_pct=24.0, latency_ms=640.0), + "cache": ServiceHealth(name="cache", status="healthy", cpu_pct=18.0, memory_pct=24.0, error_rate_pct=0.0, latency_ms=14.0), + "database": ServiceHealth(name="database", status="crashed", cpu_pct=99.0, memory_pct=97.0, error_rate_pct=100.0, latency_ms=0.0), + "worker": ServiceHealth(name="worker", status="degraded", cpu_pct=88.0, memory_pct=71.0, error_rate_pct=19.0, latency_ms=420.0), + }, + "discovered_evidence": [], + "recent_deploys": ["Rolled out worker@2026.04.23-bad 12 minutes ago."], + "checks": [ + CheckResult(name="database_recovery", passed=False, detail="Database recovery has not been verified yet."), + CheckResult(name="end_to_end", passed=False, detail="End-to-end health has not been verified yet."), + ], + "user_impact": 0.82, + "slo_burn_rate": 0.91, + "incident_resolved": False, + "containment_applied": False, + "last_action_result": "", + "tool_output": None, + "failure_type": None, + "why_failed": None, + "allowed_actions": [ + "query_logs", + "query_metrics", + "query_dependencies", + "query_deploys", + "rollback_deploy", + "restart_service", + "run_check", + "isolate_service", + "escalate", + "submit_hypothesis", + "declare_resolved", + ], + "required_fields_by_action": { + "query_logs": ["service"], + "query_metrics": ["service", "metric"], + "query_dependencies": ["service"], + "query_deploys": ["service"], + "rollback_deploy": ["service"], + "restart_service": ["service"], + "run_check": ["check_name"], + "isolate_service": ["service"], + "escalate": [], + "submit_hypothesis": ["hypothesis"], + "declare_resolved": [], + }, + "valid_action_example": None, + "common_trap": None, + "loop_warning": None, + "blocked_until_security_complete": False, + "security_unlock_reason": None, + "best_recovery_action_family": None, + "progress_flags": {}, + "security_subquest_status": None, + "security_context": {}, + "final_score": 0.1, + "score_breakdown": {"final_score": 0.1}, + "reward": 0.0, + "done": False, + } + defaults.update(overrides) + return UnifiedIncidentObservation(**defaults) + + +def test_log_helpers_match_required_format(capsys) -> None: + inference.log_start(task="worker_deploy_cascade", env="unified-incident-env", model="demo-model") + inference.log_step(step=2, action='{"action_type":"query_logs","service":"database"}', reward=-0.01, done=False, error=None) + inference.log_end(success=True, steps=2, score=0.37, rewards=[-0.01, 0.27]) + captured = capsys.readouterr().out.strip().splitlines() + assert captured == [ + "[START] task=worker_deploy_cascade env=unified-incident-env model=demo-model", + '[STEP] step=2 action={"action_type":"query_logs","service":"database"} reward=-0.01 done=false error=null', + "[END] success=true steps=2 score=0.37 rewards=-0.01,0.27", + ] + + +def test_parse_action_accepts_valid_json() -> None: + observation = make_observation() + action = inference.parse_action('{"action_type":"query_deploys","service":"worker"}', observation) + assert action == inference.UnifiedIncidentAction(action_type="query_deploys", service="worker") + + +def test_parse_action_rejects_incomplete_metric_query() -> None: + observation = make_observation() + assert inference.parse_action('{"action_type":"query_metrics","service":"database"}', observation) is None + + +def test_build_user_prompt_includes_public_state_without_examples() -> None: + observation = make_observation() + prompt = inference.build_user_prompt(observation) + assert "Incident summary:" in prompt + assert "Allowed actions:" in prompt + assert "Required fields:" in prompt + assert "Valid example" not in prompt + assert "worker@2026.04.23-bad" not in prompt + + +def test_build_fallback_action_prefers_public_deploy_query() -> None: + observation = make_observation() + action = inference.build_fallback_action(observation) + assert action == inference.UnifiedIncidentAction(action_type="query_deploys", service="worker") diff --git a/unified_incident_env/tests/test_trainer.py b/unified_incident_env/tests/test_trainer.py new file mode 100644 index 0000000000000000000000000000000000000000..1cf8d6b0f5678f4355d07d52bd750eec8625b9b3 --- /dev/null +++ b/unified_incident_env/tests/test_trainer.py @@ -0,0 +1,46 @@ +"""Smoke tests for reusable trainer-shell pieces after the v2 pivot.""" + +from __future__ import annotations + +from pathlib import Path + +from unified_incident_env.trainer.trajectory_memory import CorrectionMemory +from unified_incident_env.trainer.trajectory_store import TrajectoryStore +from unified_incident_env.trainer.types import EpisodeRecord, StepRecord + + +def test_correction_memory_empty_prompt_is_safe() -> None: + memory = CorrectionMemory() + addendum = memory.build_prompt_addendum("worker_deploy_cascade", "triage") + assert isinstance(addendum, str) + + +def test_trajectory_store_roundtrip(tmp_path: Path) -> None: + store = TrajectoryStore(tmp_path / "episodes.jsonl") + record = EpisodeRecord( + run_id="run-1", + scenario_id="worker_deploy_cascade", + difficulty="easy", + model_name="stub", + mode="strict", + success=False, + final_score=0.1, + steps=1, + elapsed_s=0.01, + step_records=[ + StepRecord( + step_index=1, + tick=1, + workflow_stage="triage", + observation={}, + prompt_text="prompt", + raw_model_output="{}", + parse_status="invalid_json", + reward=None, + ) + ], + ) + store.append_episode(record) + loaded = store.load_episodes() + assert len(loaded) == 1 + assert loaded[0].scenario_id == "worker_deploy_cascade" diff --git a/unified_incident_env/tests/test_trainer_session.py b/unified_incident_env/tests/test_trainer_session.py new file mode 100644 index 0000000000000000000000000000000000000000..bb210809d3f17670dfdabe9b5b951f0287fac980 --- /dev/null +++ b/unified_incident_env/tests/test_trainer_session.py @@ -0,0 +1,32 @@ +"""Smoke tests for session/report shells after the v2 pivot.""" + +from __future__ import annotations + +from unified_incident_env.trainer.reporting import build_phase_deltas +from unified_incident_env.trainer.types import SessionPhaseReport + + +def test_build_phase_deltas_handles_simple_progression() -> None: + phases = [ + SessionPhaseReport( + phase_name="probe", + episode_ids=[1, 2], + avg_score=0.2, + success_rate=0.0, + schema_failures=1, + loop_failures=1, + updates_applied=[], + ), + SessionPhaseReport( + phase_name="final_evaluation", + episode_ids=[3, 4], + avg_score=0.8, + success_rate=1.0, + schema_failures=0, + loop_failures=0, + updates_applied=[], + ), + ] + deltas = build_phase_deltas(phases) + assert deltas[1].phase_name == "final_evaluation" + assert deltas[1].score_delta == 0.6 diff --git a/unified_incident_env/trainer/__init__.py b/unified_incident_env/trainer/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..5ed3165caaa16a9ec1be908cc5a072c6f7e295ce --- /dev/null +++ b/unified_incident_env/trainer/__init__.py @@ -0,0 +1,8 @@ +"""Trainer package namespace. + +This package intentionally avoids eager importing of legacy trainer flows so the +honest v2 environment can reuse shell utilities without pulling in deprecated +benchmark-specific modules at import time. +""" + +__all__: list[str] = [] diff --git a/unified_incident_env/trainer/action_adapter.py b/unified_incident_env/trainer/action_adapter.py new file mode 100644 index 0000000000000000000000000000000000000000..618b7ea113ef89b607ae696f687ede81647d8cd5 --- /dev/null +++ b/unified_incident_env/trainer/action_adapter.py @@ -0,0 +1,204 @@ +"""Strict and lenient action parsers for training and eval.""" + +from __future__ import annotations + +import json +from typing import Any + +from ..models import ActionType, UnifiedIncidentAction +from .types import ParseResult + +_ALLOWED_KEYS = { + "action_type", + "service", + "metric", + "vulnerability_type", + "patch_id", + "postmortem", +} +_KNOWN_ACTIONS: set[str] = { + "query_logs", + "query_metrics", + "query_dependencies", + "restart_service", + "rollback_deploy", + "inspect_code", + "classify_vulnerability", + "apply_patch", + "verify_security_fix", + "submit_security_fix", + "submit_postmortem", +} + + +def _extract_json_text(raw_text: str) -> str: + text = raw_text.strip() + if "```" in text: + parts = text.split("```") + if len(parts) >= 2: + text = parts[1] + if text.startswith("json"): + text = text[4:] + start = text.find("{") + end = text.rfind("}") + if start != -1 and end != -1 and start < end: + text = text[start : end + 1] + return text.strip() + + +def _compact_action(action: UnifiedIncidentAction) -> dict[str, Any]: + payload = action.model_dump(exclude_none=True) + if payload.get("metadata") == {}: + payload.pop("metadata", None) + return payload + + +class StrictActionParser: + """Exact parser for judge-style evaluation.""" + + def parse(self, raw_text: str) -> ParseResult: + bare = raw_text.strip().strip('"').strip("'") + if bare in {"inspect_code", "verify_security_fix", "submit_security_fix"}: + action = UnifiedIncidentAction(action_type=bare) + return ParseResult( + parse_status="repaired", + cleaned_action=_compact_action(action), + repair_labels=["bare_action_wrapped"], + ) + + try: + data = json.loads(_extract_json_text(raw_text)) + except Exception as exc: + return ParseResult(parse_status="invalid_json", error=type(exc).__name__) + + if not isinstance(data, dict): + return ParseResult(parse_status="invalid_action", error="root must be object") + + repaired_labels: list[str] = [] + cleaned: dict[str, Any] = {k: v for k, v in data.items() if k in _ALLOWED_KEYS} + repaired = cleaned != data + if repaired: + repaired_labels.append("extra_keys_stripped") + + if "action_type" not in cleaned and isinstance(data.get("action"), str): + if data["action"] in _KNOWN_ACTIONS: + cleaned["action_type"] = data["action"] + repaired = True + repaired_labels.append("action_alias_normalized") + + if ( + "vulnerability_type" not in cleaned + and isinstance(data.get("vulnerability"), str) + ): + cleaned["vulnerability_type"] = data["vulnerability"] + repaired = True + repaired_labels.append("vulnerability_alias_normalized") + + metrics_value = data.get("metrics") + if "metric" not in cleaned and isinstance(metrics_value, list) and len(metrics_value) == 1: + cleaned["metric"] = metrics_value[0] + repaired = True + repaired_labels.append("metric_list_normalized") + + if "metrics" in data and ( + not isinstance(metrics_value, list) or len(metrics_value) != 1 + ): + return ParseResult( + parse_status="invalid_action", + error="metrics alias is ambiguous", + repair_labels=repaired_labels, + ) + + try: + action = UnifiedIncidentAction(**cleaned) + except Exception as exc: + return ParseResult( + parse_status="invalid_action", + error=str(exc), + repair_labels=repaired_labels, + ) + + return ParseResult( + parse_status="repaired" if repaired else "ok", + cleaned_action=_compact_action(action), + repair_labels=repaired_labels, + ) + + +class LenientActionAdapter: + """Training-time parser that repairs small schema mistakes only.""" + + def parse(self, raw_text: str) -> ParseResult: + bare = raw_text.strip().strip('"').strip("'") + if bare in _KNOWN_ACTIONS: + try: + action = UnifiedIncidentAction(action_type=bare) + except Exception as exc: + return ParseResult( + parse_status="invalid_action", + error=str(exc), + repair_labels=["bare_action_wrapped"], + ) + return ParseResult( + parse_status="repaired", + cleaned_action=_compact_action(action), + repair_labels=["bare_action_wrapped"], + ) + + try: + data = json.loads(_extract_json_text(raw_text)) + except Exception as exc: + return ParseResult(parse_status="invalid_json", error=type(exc).__name__) + + if not isinstance(data, dict): + return ParseResult(parse_status="invalid_action", error="root must be object") + + repaired_labels: list[str] = [] + cleaned: dict[str, Any] = {k: v for k, v in data.items() if k in _ALLOWED_KEYS} + repaired = cleaned != data + if repaired: + repaired_labels.append("extra_keys_stripped") + + if "action_type" not in cleaned and isinstance(data.get("action"), str): + if data["action"] in _KNOWN_ACTIONS: + cleaned["action_type"] = data["action"] + repaired = True + repaired_labels.append("action_alias_normalized") + + if ( + "vulnerability_type" not in cleaned + and isinstance(data.get("vulnerability"), str) + ): + cleaned["vulnerability_type"] = data["vulnerability"] + repaired = True + repaired_labels.append("vulnerability_alias_normalized") + + metrics_value = data.get("metrics") + if "metric" not in cleaned and isinstance(metrics_value, list) and len(metrics_value) == 1: + cleaned["metric"] = metrics_value[0] + repaired = True + repaired_labels.append("metric_list_normalized") + + if "metrics" in data and ( + not isinstance(metrics_value, list) or len(metrics_value) != 1 + ): + return ParseResult( + parse_status="invalid_action", + error="metrics alias is ambiguous", + repair_labels=repaired_labels, + ) + + try: + action = UnifiedIncidentAction(**cleaned) + except Exception as exc: + return ParseResult( + parse_status="invalid_action", + error=str(exc), + repair_labels=repaired_labels, + ) + + return ParseResult( + parse_status="repaired" if repaired else "ok", + cleaned_action=_compact_action(action), + repair_labels=repaired_labels, + ) diff --git a/unified_incident_env/trainer/analyze_failures.py b/unified_incident_env/trainer/analyze_failures.py new file mode 100644 index 0000000000000000000000000000000000000000..e0443c82bdf88e4b63bd47882553e3294a2b3d7c --- /dev/null +++ b/unified_incident_env/trainer/analyze_failures.py @@ -0,0 +1,251 @@ +"""Failure analysis for episode trajectories.""" + +from __future__ import annotations + +from collections import Counter + +from .types import EpisodeRecord, FailureAnalysisReport, FailureBucketEntry, StepRecord + +_INFRA_ACTIONS = {"restart_service", "rollback_deploy"} + + +def analyze_episode(record: EpisodeRecord) -> FailureAnalysisReport: + """Classify one episode into schema, policy, looping, and reasoning buckets.""" + entries: list[FailureBucketEntry] = [] + + for step in record.step_records: + entries.extend(_classify_step(record, step)) + + entries.extend(_classify_episode_level(record)) + + schema = sorted({entry.failure_type for entry in entries if entry.bucket == "schema"}) + policy = sorted({entry.failure_type for entry in entries if entry.bucket == "policy"}) + looping = sorted({entry.failure_type for entry in entries if entry.bucket == "looping"}) + reasoning = sorted({entry.failure_type for entry in entries if entry.bucket == "reasoning"}) + summary = Counter(entry.bucket for entry in entries) + + return FailureAnalysisReport( + episode_ids=[record.episode_id or 0], + scenario_ids=[record.scenario_id], + entries=entries, + schema_failures=schema, + policy_failures=policy, + looping_failures=looping, + reasoning_failures=reasoning, + summary={ + "schema": summary.get("schema", 0), + "policy": summary.get("policy", 0), + "looping": summary.get("looping", 0), + "reasoning": summary.get("reasoning", 0), + }, + ) + + +def analyze_block(records: list[EpisodeRecord]) -> FailureAnalysisReport: + """Combine multiple episode analyses into one block report.""" + analyses = [analyze_episode(record) for record in records] + entries = [entry for analysis in analyses for entry in analysis.entries] + summary = Counter(entry.bucket for entry in entries) + return FailureAnalysisReport( + episode_ids=[record.episode_id or 0 for record in records], + scenario_ids=[record.scenario_id for record in records], + entries=entries, + schema_failures=sorted({entry.failure_type for entry in entries if entry.bucket == "schema"}), + policy_failures=sorted({entry.failure_type for entry in entries if entry.bucket == "policy"}), + looping_failures=sorted({entry.failure_type for entry in entries if entry.bucket == "looping"}), + reasoning_failures=sorted({entry.failure_type for entry in entries if entry.bucket == "reasoning"}), + summary={ + "schema": summary.get("schema", 0), + "policy": summary.get("policy", 0), + "looping": summary.get("looping", 0), + "reasoning": summary.get("reasoning", 0), + }, + ) + + +def _classify_step(record: EpisodeRecord, step: StepRecord) -> list[FailureBucketEntry]: + entries: list[FailureBucketEntry] = [] + if step.parse_status in {"invalid_json", "invalid_action"}: + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="schema", + failure_type=_schema_failure_type(step), + detail=step.failure_reason or "schema failure", + ) + ) + return entries + + student = step.cleaned_action or {} + teacher = step.teacher_action or {} + if not teacher or not student or student == teacher: + return entries + + student_type = student.get("action_type") + teacher_type = teacher.get("action_type") + + if student_type == "classify_vulnerability": + failure_type = ( + "wrong_vulnerability" + if teacher_type == "classify_vulnerability" + else "fails_to_identify_real_vulnerability" + ) + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="reasoning", + failure_type=failure_type, + detail=f"student={student} teacher={teacher}", + ) + ) + return entries + + if student_type == "apply_patch" and teacher_type == "apply_patch": + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="policy", + failure_type="wrong_patch", + detail=f"student={student} teacher={teacher}", + ) + ) + return entries + + if student_type == "verify_security_fix" and teacher_type != "verify_security_fix": + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="policy", + failure_type="verify_too_early", + detail=f"student={student} teacher={teacher}", + ) + ) + return entries + + if student_type == "submit_security_fix" and teacher_type != "submit_security_fix": + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="policy", + failure_type="submit_too_early", + detail=f"student={student} teacher={teacher}", + ) + ) + return entries + + if student_type in _INFRA_ACTIONS and teacher_type not in _INFRA_ACTIONS: + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="policy", + failure_type="infra_before_security", + detail=f"student={student} teacher={teacher}", + ) + ) + return entries + + if student_type in _INFRA_ACTIONS and teacher_type in _INFRA_ACTIONS: + failure_type = "wrong_service" + if student_type == "restart_service": + failure_type = "wrong_restart" + elif student_type == "rollback_deploy": + failure_type = "wrong_rollback" + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="policy", + failure_type=failure_type, + detail=f"student={student} teacher={teacher}", + ) + ) + return entries + + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="policy", + failure_type="wrong_action_choice", + detail=f"student={student} teacher={teacher}", + ) + ) + return entries + + +def _classify_episode_level(record: EpisodeRecord) -> list[FailureBucketEntry]: + entries: list[FailureBucketEntry] = [] + previous = None + repeat_count = 0 + for step in record.step_records: + current = step.cleaned_action + if current and current == previous: + repeat_count += 1 + if repeat_count >= 1: + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=step.step_index, + bucket="looping", + failure_type="repeated_same_action", + detail=f"action={current}", + ) + ) + else: + repeat_count = 0 + previous = current + + stopped = record.stopped_reason or "" + if stopped in {"diagnosis", "root_cause_analysis"}: + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=None, + bucket="looping", + failure_type="stuck_in_diagnosis", + detail=f"stopped_reason={stopped}", + ) + ) + elif stopped == "security_subquest": + entries.append( + FailureBucketEntry( + episode_id=record.episode_id or 0, + scenario_id=record.scenario_id, + step_index=None, + bucket="looping", + failure_type="stuck_in_security_subquest", + detail=f"stopped_reason={stopped}", + ) + ) + + return entries + + +def _schema_failure_type(step: StepRecord) -> str: + raw = step.raw_model_output.lower() + error = (step.failure_reason or "").lower() + if '"reason"' in raw or '"details"' in raw or "extra_forbidden" in error: + return "extra_unsupported_fields" + if '"services"' in raw or '"metrics"' in raw or "field required" in error: + return "wrong_field_names" + if "required" in error or "missing" in error: + return "missing_required_fields" + if step.parse_status == "invalid_json": + return "invalid_json" + return "invalid_action" diff --git a/unified_incident_env/trainer/backend.py b/unified_incident_env/trainer/backend.py new file mode 100644 index 0000000000000000000000000000000000000000..60a98c42a9a55906d1f9950ea64e5d723e3141f9 --- /dev/null +++ b/unified_incident_env/trainer/backend.py @@ -0,0 +1,165 @@ +"""Backend interfaces for model calls.""" + +from __future__ import annotations + +import json +import time +from typing import Protocol +from urllib.parse import urlparse + +from openai import OpenAI + +from .types import ModelRequest, ModelResponse + + +class ModelBackend(Protocol): + """Minimal backend protocol for trainer use.""" + + def complete(self, request: ModelRequest) -> ModelResponse: + """Return raw model text and metadata for one request.""" + + +class OpenAICompatibleBackend: + """OpenAI-compatible backend, suitable for Ollama and similar servers.""" + + def __init__( + self, + *, + base_url: str, + api_key: str, + timeout_s: float = 90.0, + ) -> None: + self.base_url = base_url + self._client = OpenAI(api_key=api_key, base_url=base_url, timeout=timeout_s) + + def complete(self, request: ModelRequest) -> ModelResponse: + started = time.perf_counter() + create_kwargs = { + "model": request.model_name, + "temperature": request.temperature, + "max_tokens": request.max_tokens, + "messages": [ + {"role": "system", "content": request.system_prompt}, + {"role": "user", "content": request.user_prompt}, + ], + } + raw_text = "" + actual_mode = request.structured_mode + + if request.structured_mode == "backend_adaptive": + if self._is_ollama(): + actual_mode = "response_format_json" + else: + actual_mode = "tool_calling" + + try: + if actual_mode == "tool_calling": + tool_choice = request.tool_choice or { + "type": "function", + "function": {"name": "emit_action"}, + } + create_kwargs["tools"] = request.tools or [ + self._tool_from_response_format(request.response_format) + ] + create_kwargs["tool_choice"] = tool_choice + response = self._client.chat.completions.create(**create_kwargs) + raw_text = self._extract_tool_text(response) + elif actual_mode == "response_format_json": + if self._is_ollama(): + create_kwargs["extra_body"] = { + "format": self._ollama_format_payload(request.response_format) + } + else: + create_kwargs["response_format"] = request.response_format or { + "type": "json_object" + } + response = self._client.chat.completions.create(**create_kwargs) + raw_text = response.choices[0].message.content or "" + else: + response = self._client.chat.completions.create(**create_kwargs) + raw_text = response.choices[0].message.content or "" + except Exception: + if request.structured_mode == "backend_adaptive" and actual_mode == "tool_calling": + fallback_kwargs = dict(create_kwargs) + if "tools" in fallback_kwargs: + del fallback_kwargs["tools"] + if "tool_choice" in fallback_kwargs: + del fallback_kwargs["tool_choice"] + if self._is_ollama(): + fallback_kwargs["extra_body"] = { + "format": self._ollama_format_payload(request.response_format) + } + else: + fallback_kwargs["response_format"] = request.response_format or { + "type": "json_object" + } + response = self._client.chat.completions.create(**fallback_kwargs) + raw_text = response.choices[0].message.content or "" + actual_mode = "response_format_json" + else: + raise + + elapsed = time.perf_counter() - started + return ModelResponse( + raw_text=raw_text, + latency_s=round(elapsed, 4), + metadata={ + "model": request.model_name, + "structured_mode": actual_mode, + }, + ) + + def _is_ollama(self) -> bool: + parsed = urlparse(self.base_url) + host = parsed.netloc.lower() + return "11434" in host or "ollama" in host or "127.0.0.1" in host or "localhost" in host + + def _ollama_format_payload( + self, + response_format: dict[str, object] | None, + ) -> object: + if response_format and response_format.get("type") == "json_schema": + json_schema = response_format.get("json_schema", {}) + if isinstance(json_schema, dict): + return json_schema.get("schema", "json") + return "json" + + def _tool_from_response_format( + self, + response_format: dict[str, object] | None, + ) -> dict[str, object]: + schema = { + "type": "object", + "properties": {"action_type": {"type": "string"}}, + "required": ["action_type"], + "additionalProperties": False, + } + if response_format and response_format.get("type") == "json_schema": + json_schema = response_format.get("json_schema", {}) + schema = json_schema.get("schema", schema) # type: ignore[assignment] + return { + "type": "function", + "function": { + "name": "emit_action", + "description": "Emit exactly one structured environment action.", + "parameters": schema, + }, + } + + def _extract_tool_text(self, response) -> str: + message = response.choices[0].message + tool_calls = getattr(message, "tool_calls", None) or [] + if tool_calls: + function = getattr(tool_calls[0], "function", None) + if function is not None and getattr(function, "arguments", None): + return function.arguments + content = message.content or "" + if isinstance(content, list): + fragments = [] + for item in content: + if isinstance(item, dict) and item.get("type") == "text": + fragments.append(item.get("text", "")) + return "".join(fragments) + if isinstance(content, str): + return content + return json.dumps(content) diff --git a/unified_incident_env/trainer/build_datasets.py b/unified_incident_env/trainer/build_datasets.py new file mode 100644 index 0000000000000000000000000000000000000000..9c8ece9c40a0ac901027397abedf1db31b069ca6 --- /dev/null +++ b/unified_incident_env/trainer/build_datasets.py @@ -0,0 +1,258 @@ +"""Build correction datasets from trajectories and failure analyses.""" + +from __future__ import annotations + +import argparse +from pathlib import Path + +from .build_sft_dataset import build_baseline_records +from .trajectory_store import TrajectoryStore +from .types import EpisodeRecord, FailureAnalysisReport, SFTRecord + + +def build_schema_repair_records( + episodes: list[EpisodeRecord], + analyses: list[FailureAnalysisReport], +) -> list[SFTRecord]: + rows: list[SFTRecord] = [] + analysis_by_episode = { + analysis.episode_ids[0]: analysis for analysis in analyses if analysis.episode_ids + } + for episode in episodes: + analysis = analysis_by_episode.get(episode.episode_id or 0) + schema_types = set(analysis.schema_failures if analysis else []) + for step in episode.step_records: + if step.parse_status not in {"invalid_json", "invalid_action", "repaired", "teacher_override"}: + continue + if step.teacher_action is None: + continue + rows.append( + SFTRecord( + source="schema_repair", + scenario_id=episode.scenario_id, + tick=step.tick, + messages=[ + {"role": "system", "content": "Repair the action into strict JSON only."}, + { + "role": "user", + "content": ( + f"{step.prompt_text}\n\n" + f"Previous invalid output:\n{step.raw_model_output}" + ), + }, + ], + target_action=step.teacher_action, + student_action=step.cleaned_action, + parse_status=step.parse_status, + tags=sorted(schema_types) or [step.parse_status], + metadata={ + "episode_id": episode.episode_id, + "step_index": step.step_index, + "repair_retry_used": step.repair_retry_used, + "teacher_override_used": step.teacher_override_used, + "normalization_applied": step.normalization_applied, + "failure_type": step.observation.get("failure_type"), + "why_failed": step.observation.get("why_failed"), + "loop_warning": step.observation.get("loop_warning"), + "blocked_until_security_complete": step.observation.get("blocked_until_security_complete"), + "security_unlock_reason": step.observation.get("security_unlock_reason"), + "progress_flags": step.observation.get("progress_flags"), + }, + ) + ) + return rows + + +def build_next_action_records( + episodes: list[EpisodeRecord], + analyses: list[FailureAnalysisReport], +) -> list[SFTRecord]: + rows: list[SFTRecord] = [] + episode_entries = { + analysis.episode_ids[0]: analysis.entries + for analysis in analyses + if analysis.episode_ids + } + allowed = {"policy", "reasoning", "looping"} + for episode in episodes: + entries = episode_entries.get(episode.episode_id or 0, []) + step_indices = { + entry.step_index + for entry in entries + if entry.bucket in allowed and entry.step_index is not None + } + for step in episode.step_records: + if step.step_index not in step_indices: + continue + if step.teacher_action is None: + continue + tags = [ + entry.failure_type + for entry in entries + if entry.step_index == step.step_index and entry.bucket in allowed + ] + rows.append( + SFTRecord( + source="next_action", + scenario_id=episode.scenario_id, + tick=step.tick, + messages=[ + {"role": "system", "content": "Choose the best next action as strict JSON only."}, + {"role": "user", "content": step.prompt_text}, + ], + target_action=step.teacher_action, + student_action=step.cleaned_action, + parse_status=step.parse_status, + tags=sorted(set(tags)) or ["next_action"], + metadata={ + "episode_id": episode.episode_id, + "step_index": step.step_index, + "workflow_stage": step.workflow_stage, + "teacher_override_used": step.teacher_override_used, + "failure_type": step.observation.get("failure_type"), + "why_failed": step.observation.get("why_failed"), + "loop_warning": step.observation.get("loop_warning"), + "progress_flags": step.observation.get("progress_flags"), + }, + ) + ) + return rows + + +def build_recovery_records( + episodes: list[EpisodeRecord], + analyses: list[FailureAnalysisReport], +) -> list[SFTRecord]: + rows: list[SFTRecord] = [] + episode_entries = { + analysis.episode_ids[0]: analysis.entries + for analysis in analyses + if analysis.episode_ids + } + recovery_failures = { + "wrong_restart", + "wrong_rollback", + "wrong_service", + "wrong_patch", + "wrong_vulnerability", + "verify_too_early", + "submit_too_early", + "infra_before_security", + "repeated_same_action", + } + for episode in episodes: + entries = episode_entries.get(episode.episode_id or 0, []) + step_indices = { + entry.step_index + for entry in entries + if entry.failure_type in recovery_failures and entry.step_index is not None + } + for step in episode.step_records: + if step.step_index not in step_indices: + continue + if step.teacher_action is None or not step.next_prompt_text: + continue + tags = [ + entry.failure_type + for entry in entries + if entry.step_index == step.step_index + and entry.failure_type in recovery_failures + ] + rows.append( + SFTRecord( + source="recovery", + scenario_id=episode.scenario_id, + tick=step.tick, + messages=[ + {"role": "system", "content": "Recover from the previous mistake. Return the best next strict JSON action only."}, + { + "role": "user", + "content": ( + f"{step.next_prompt_text}\n\n" + f"Previous wrong action: {step.cleaned_action}\n" + f"Penalty or result: reward={step.reward}" + ), + }, + ], + target_action=step.teacher_action, + student_action=step.cleaned_action, + parse_status=step.parse_status, + tags=sorted(set(tags)) or ["recovery"], + metadata={ + "episode_id": episode.episode_id, + "step_index": step.step_index, + "teacher_override_used": step.teacher_override_used, + "failure_type": step.observation.get("failure_type"), + "why_failed": step.observation.get("why_failed"), + "loop_warning": step.observation.get("loop_warning"), + "best_recovery_action_family": step.observation.get("best_recovery_action_family"), + }, + ) + ) + return rows + + +def combine_sft_records( + *, + baseline_records: list[SFTRecord], + schema_records: list[SFTRecord], + next_action_records: list[SFTRecord], + recovery_records: list[SFTRecord], +) -> list[SFTRecord]: + return [ + *baseline_records, + *schema_records, + *next_action_records, + *recovery_records, + ] + + +def write_jsonl(records: list[SFTRecord], path: Path) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("w", encoding="utf-8") as handle: + for record in records: + handle.write(record.model_dump_json()) + handle.write("\n") + + +def load_episodes(path: Path) -> list[EpisodeRecord]: + return TrajectoryStore(path).load_episodes() + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--episodes", default="outputs/trainer/episodes.jsonl") + parser.add_argument("--output-dir", required=True) + args = parser.parse_args() + + output_dir = Path(args.output_dir) + episodes = load_episodes(Path(args.episodes)) + + from .analyze_failures import analyze_episode + + analyses = [analyze_episode(episode) for episode in episodes] + baseline_records = build_baseline_records() + schema_records = build_schema_repair_records(episodes, analyses) + next_action_records = build_next_action_records(episodes, analyses) + recovery_records = build_recovery_records(episodes, analyses) + combined_records = combine_sft_records( + baseline_records=baseline_records, + schema_records=schema_records, + next_action_records=next_action_records, + recovery_records=recovery_records, + ) + + write_jsonl(baseline_records, output_dir / "baseline_teacher_dataset.jsonl") + write_jsonl(schema_records, output_dir / "schema_repair.jsonl") + write_jsonl(next_action_records, output_dir / "next_action.jsonl") + write_jsonl(recovery_records, output_dir / "recovery.jsonl") + write_jsonl(combined_records, output_dir / "sft_dataset.jsonl") + print( + f"wrote baseline={len(baseline_records)} schema={len(schema_records)} " + f"next_action={len(next_action_records)} recovery={len(recovery_records)} " + f"combined={len(combined_records)} to {output_dir}" + ) + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/trainer/build_sft_dataset.py b/unified_incident_env/trainer/build_sft_dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..9468a04ead2466e3aa5761d4fc0733318e09c4e4 --- /dev/null +++ b/unified_incident_env/trainer/build_sft_dataset.py @@ -0,0 +1,101 @@ +"""Build supervised JSONL datasets from baseline and replay trajectories.""" + +from __future__ import annotations + +import argparse +from pathlib import Path + +from ..scripts.baseline_agent import plan_for_scenario +from ..server.challenge import SCENARIOS +from ..server.environment import UnifiedIncidentEnvironment +from .prompts import TRAINING_SYSTEM_PROMPT +from .trajectory_store import TrajectoryStore +from .types import SFTRecord + + +def build_baseline_records() -> list[SFTRecord]: + rows: list[SFTRecord] = [] + for scenario_id in SCENARIOS: + env = UnifiedIncidentEnvironment() + obs = env.reset(scenario_id=scenario_id) + for step_index, action in enumerate(plan_for_scenario(scenario_id), start=1): + rows.append( + SFTRecord( + source="baseline", + scenario_id=scenario_id, + tick=obs.tick_count, + messages=[ + {"role": "system", "content": TRAINING_SYSTEM_PROMPT}, + {"role": "user", "content": obs.prompt_text}, + ], + target_action=action.model_dump(exclude_none=True), + tags=["teacher", f"step_{step_index}"], + ) + ) + obs = env.step(action) + return rows + + +def build_replay_records(episodes_path: Path) -> list[SFTRecord]: + rows: list[SFTRecord] = [] + for episode in TrajectoryStore(episodes_path).load_episodes(): + for step in episode.step_records: + if step.teacher_action is None: + continue + tags = [episode.mode, step.parse_status] + if step.failure_reason: + tags.append("failure") + rows.append( + SFTRecord( + source="replay", + scenario_id=episode.scenario_id, + tick=step.tick, + messages=[ + {"role": "system", "content": TRAINING_SYSTEM_PROMPT}, + {"role": "user", "content": step.prompt_text}, + ], + target_action=step.teacher_action, + student_action=step.cleaned_action, + parse_status=step.parse_status, + tags=tags, + ) + ) + return rows + + +def write_jsonl(records: list[SFTRecord], output_path: Path) -> None: + output_path.parent.mkdir(parents=True, exist_ok=True) + with output_path.open("w", encoding="utf-8") as handle: + for record in records: + handle.write(record.model_dump_json()) + handle.write("\n") + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument( + "--source", + choices=["baseline", "replay", "combined"], + default="combined", + ) + parser.add_argument( + "--episodes", + default="outputs/trainer/episodes.jsonl", + ) + parser.add_argument( + "--output", + required=True, + ) + args = parser.parse_args() + + records: list[SFTRecord] = [] + if args.source in {"baseline", "combined"}: + records.extend(build_baseline_records()) + if args.source in {"replay", "combined"}: + records.extend(build_replay_records(Path(args.episodes))) + write_jsonl(records, Path(args.output)) + print(f"wrote {len(records)} rows to {args.output}") + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/trainer/collect_trajectory.py b/unified_incident_env/trainer/collect_trajectory.py new file mode 100644 index 0000000000000000000000000000000000000000..7bede9d261aa35b86810a7c6cbde444c94e4f45c --- /dev/null +++ b/unified_incident_env/trainer/collect_trajectory.py @@ -0,0 +1,53 @@ +"""Collection wrapper that turns one episode into trajectory + analysis + summary.""" + +from __future__ import annotations + +from .analyze_failures import analyze_episode +from .types import EpisodeRecord, EpisodeSummaryRecord + + +def collect_episode( + *, + runner, + scenario_id: str, + episode_id: int, + mode: str, + model_version: str, +) -> tuple[EpisodeRecord, EpisodeSummaryRecord, object]: + """Run, analyze, and summarize one episode.""" + record = runner.run( + scenario_id=scenario_id, + mode=mode, + episode_id=episode_id, + model_version=model_version, + ) + analysis = analyze_episode(record) + record.schema_failures = analysis.summary.get("schema", 0) + record.policy_failures = analysis.policy_failures + record.looping_failures = analysis.looping_failures + record.reasoning_failures = analysis.reasoning_failures + summary = EpisodeSummaryRecord( + episode_id=episode_id, + run_id=record.run_id, + scenario_id=record.scenario_id, + difficulty=record.difficulty, + model_name=record.model_name, + model_version=record.model_version, + mode=record.mode, + steps=record.steps, + success=record.success, + final_score=record.final_score, + schema_failures=analysis.summary.get("schema", 0), + json_valid_steps=record.json_valid_steps, + strict_schema_valid_steps=record.strict_schema_valid_steps, + teacher_override_count=record.teacher_override_count, + repair_retry_count=record.repair_retry_count, + policy_failures=analysis.policy_failures, + looping_failures=analysis.looping_failures, + reasoning_failures=analysis.reasoning_failures, + security_subquest_completed=record.security_subquest_completed, + postmortem_completed=record.postmortem_completed, + stopped_reason=record.stopped_reason, + elapsed_s=record.elapsed_s, + ) + return record, summary, analysis diff --git a/unified_incident_env/trainer/eval_models.py b/unified_incident_env/trainer/eval_models.py new file mode 100644 index 0000000000000000000000000000000000000000..09f2ec3966fa3795b66d49d9cf21f40a63fa79d0 --- /dev/null +++ b/unified_incident_env/trainer/eval_models.py @@ -0,0 +1,145 @@ +"""Batch evaluation for one or more models.""" + +from __future__ import annotations + +import argparse +import json +import os +from pathlib import Path + +from ..server.challenge import SCENARIOS +from .action_adapter import LenientActionAdapter, StrictActionParser +from .backend import OpenAICompatibleBackend +from .run_episode import EpisodeRunner +from .trajectory_store import TrajectoryStore +from .types import EvalScenarioResult, EvalSummary + + +def summarize(results: list[EvalScenarioResult], mode: str) -> EvalSummary: + success_rate = ( + sum(1 for result in results if result.success) / len(results) if results else 0.0 + ) + avg_score = ( + sum(result.final_score for result in results) / len(results) if results else 0.0 + ) + schema_failure_rate = ( + sum(1 for result in results if result.schema_failure) / len(results) + if results + else 0.0 + ) + + by_model: dict[str, dict[str, float]] = {} + by_scenario: dict[str, dict[str, float]] = {} + for result in results: + model_bucket = by_model.setdefault( + result.model_name, + {"runs": 0.0, "successes": 0.0, "score_sum": 0.0, "schema_failures": 0.0}, + ) + model_bucket["runs"] += 1 + model_bucket["successes"] += 1.0 if result.success else 0.0 + model_bucket["score_sum"] += result.final_score + model_bucket["schema_failures"] += 1.0 if result.schema_failure else 0.0 + + scenario_bucket = by_scenario.setdefault( + result.scenario_id, + {"runs": 0.0, "successes": 0.0, "score_sum": 0.0}, + ) + scenario_bucket["runs"] += 1 + scenario_bucket["successes"] += 1.0 if result.success else 0.0 + scenario_bucket["score_sum"] += result.final_score + + for bucket in by_model.values(): + runs = bucket["runs"] or 1.0 + bucket["success_rate"] = round(bucket["successes"] / runs, 4) + bucket["avg_score"] = round(bucket["score_sum"] / runs, 4) + bucket["schema_failure_rate"] = round(bucket["schema_failures"] / runs, 4) + del bucket["score_sum"] + del bucket["successes"] + del bucket["schema_failures"] + + for bucket in by_scenario.values(): + runs = bucket["runs"] or 1.0 + bucket["success_rate"] = round(bucket["successes"] / runs, 4) + bucket["avg_score"] = round(bucket["score_sum"] / runs, 4) + del bucket["score_sum"] + del bucket["successes"] + + return EvalSummary( + mode=mode, + results=results, + success_rate=round(success_rate, 4), + avg_score=round(avg_score, 4), + schema_failure_rate=round(schema_failure_rate, 4), + by_model=by_model, + by_scenario=by_scenario, + ) + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--models", nargs="+", required=True) + parser.add_argument("--mode", choices=["strict", "lenient"], default="strict") + parser.add_argument("--base-url", default="http://127.0.0.1:8000") + parser.add_argument( + "--api-base-url", + default=os.environ.get("API_BASE_URL", "http://127.0.0.1:11434/v1"), + ) + parser.add_argument( + "--api-key", + default=os.environ.get("OPENAI_API_KEY") or os.environ.get("HF_TOKEN") or "local", + ) + parser.add_argument( + "--output", + default=None, + ) + parser.add_argument( + "--episodes-output", + default="outputs/trainer/episodes.jsonl", + ) + args = parser.parse_args() + + backend = OpenAICompatibleBackend( + base_url=args.api_base_url, + api_key=args.api_key, + ) + parser_impl = StrictActionParser() if args.mode == "strict" else LenientActionAdapter() + episode_store = TrajectoryStore(Path(args.episodes_output)) + + results: list[EvalScenarioResult] = [] + for model_name in args.models: + runner = EpisodeRunner( + backend=backend, + parser=parser_impl, + model_name=model_name, + base_url=args.base_url, + ) + for scenario_id in SCENARIOS: + episode = runner.run(scenario_id=scenario_id, mode=args.mode) + episode_store.append_episode(episode) + results.append( + EvalScenarioResult( + model_name=model_name, + scenario_id=scenario_id, + success=episode.success, + final_score=episode.final_score, + failure_reason=episode.failure_reason, + schema_failure=bool( + episode.failure_reason + and episode.failure_reason.startswith("parse_failure") + ), + elapsed_s=episode.elapsed_s, + ) + ) + + summary = summarize(results, mode=args.mode) + output_path = Path( + args.output + or f"outputs/trainer/{args.mode}_eval_summary.json" + ) + output_path.parent.mkdir(parents=True, exist_ok=True) + output_path.write_text(summary.model_dump_json(indent=2), encoding="utf-8") + print(summary.model_dump_json(indent=2)) + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/trainer/parser.py b/unified_incident_env/trainer/parser.py new file mode 100644 index 0000000000000000000000000000000000000000..a46318ea9d92dec1c829524b7fcd8d5e700d841b --- /dev/null +++ b/unified_incident_env/trainer/parser.py @@ -0,0 +1,5 @@ +"""Parser surface alias for trainer action normalization.""" + +from .action_adapter import LenientActionAdapter, StrictActionParser + +__all__ = ["LenientActionAdapter", "StrictActionParser"] diff --git a/unified_incident_env/trainer/policy_adapter.py b/unified_incident_env/trainer/policy_adapter.py new file mode 100644 index 0000000000000000000000000000000000000000..2090f70cd33115bcaa78b34672717a87e8d39ef4 --- /dev/null +++ b/unified_incident_env/trainer/policy_adapter.py @@ -0,0 +1,64 @@ +"""Policy adapter artifacts produced by the external trainer wrapper.""" + +from __future__ import annotations + +import json +from dataclasses import asdict, dataclass, field +from pathlib import Path + + +@dataclass +class PolicyAdapter: + """Serializable prompt-side policy adapter for later phases.""" + + base_model: str + phase_name: str + schema_examples: list[dict] = field(default_factory=list) + next_action_examples: list[dict] = field(default_factory=list) + recovery_examples: list[dict] = field(default_factory=list) + + def build_prompt_addendum(self, scenario_id: str, stage: str) -> str: + """Build an addendum for the given scenario and stage.""" + lines: list[str] = [] + schema = [ + item for item in self.schema_examples + if item.get("scenario_id") == scenario_id + ][:2] + next_action = [ + item for item in self.next_action_examples + if item.get("scenario_id") == scenario_id + ][:2] + recovery = [ + item for item in self.recovery_examples + if item.get("scenario_id") == scenario_id + ][:2] + + if schema: + lines.append("Adapter schema reminders:") + for item in schema: + lines.append( + f"- invalid -> valid: {json.dumps(item['target_action'], separators=(',', ':'))}" + ) + if next_action: + lines.append("Adapter next-action reminders:") + for item in next_action: + lines.append( + f"- stage {item.get('workflow_stage', stage)} -> {json.dumps(item['target_action'], separators=(',', ':'))}" + ) + if recovery: + lines.append("Adapter recovery reminders:") + for item in recovery: + lines.append( + f"- recover with {json.dumps(item['target_action'], separators=(',', ':'))}" + ) + return "\n".join(lines) + + def save(self, path: Path) -> None: + """Write adapter JSON to disk.""" + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(asdict(self), indent=2), encoding="utf-8") + + @classmethod + def load(cls, path: Path) -> "PolicyAdapter": + """Load adapter JSON from disk.""" + return cls(**json.loads(path.read_text(encoding="utf-8"))) diff --git a/unified_incident_env/trainer/prompts.py b/unified_incident_env/trainer/prompts.py new file mode 100644 index 0000000000000000000000000000000000000000..5165201eaf4af7f504b1dbfacc646163950acf1e --- /dev/null +++ b/unified_incident_env/trainer/prompts.py @@ -0,0 +1,435 @@ +"""Prompt builders for training, repair, and strict evaluation.""" + +from __future__ import annotations + +import json +from typing import Any + +from ..models import UnifiedIncidentObservation + +ALL_ACTIONS = [ + "query_logs", + "query_metrics", + "query_dependencies", + "restart_service", + "rollback_deploy", + "inspect_code", + "classify_vulnerability", + "apply_patch", + "verify_security_fix", + "submit_security_fix", + "submit_postmortem", +] + +TRAINING_SYSTEM_PROMPT = ( + "You are solving a deterministic incident-response benchmark.\n\n" + "Return exactly one JSON object and nothing else.\n\n" + "Rules:\n" + "- Choose only from the allowed action types shown in the user message.\n" + "- Use only the required fields for the chosen action.\n" + "- Do not include explanation text.\n" + "- Do not include markdown.\n" + "- Do not include code fences.\n" + "- Do not repeat an action that already failed or made no progress.\n" + "- If patching is required, use only one of the listed patch IDs.\n" +) +STAGE_GOALS = { + "diagnosis": "find the most relevant next investigation step", + "root_cause_analysis": "confirm the root-cause evidence and avoid unnecessary recovery", + "security_subquest": "complete the security fix before infrastructure recovery", + "remediation": "recover services in the correct order", + "verification": "verify that recovery and security remediation are complete", + "postmortem": "submit the final incident summary", + "done": "complete the benchmark", +} + +STAGE_ACTIONS = { + "diagnosis": ["query_logs", "query_metrics", "query_dependencies"], + "root_cause_analysis": ["query_logs", "query_metrics", "query_dependencies"], + "security_subquest": [ + "inspect_code", + "classify_vulnerability", + "apply_patch", + "verify_security_fix", + "submit_security_fix", + ], + "remediation": ["restart_service", "rollback_deploy"], + "verification": ["restart_service", "rollback_deploy"], + "postmortem": ["submit_postmortem"], + "done": [], +} + +REQUIRED_FIELDS = { + "query_logs": ["service"], + "query_metrics": ["service", "metric"], + "query_dependencies": ["service"], + "restart_service": ["service"], + "rollback_deploy": ["service"], + "inspect_code": [], + "classify_vulnerability": ["vulnerability_type"], + "apply_patch": ["patch_id"], + "verify_security_fix": [], + "submit_security_fix": [], + "submit_postmortem": ["postmortem"], +} + +USER_PROMPT_TEMPLATE = """Current stage: {stage} + +Current goal: {goal} + +Allowed actions: +{allowed_actions_block} + +Required fields: +{required_fields_block} + +{patch_ids_block}{transition_block}{negative_reward_block}Current environment state: +{state_block} + +Valid example: +{valid_example} + +Return exactly one JSON object. +""" + + +def build_runtime_request( + observation: UnifiedIncidentObservation, + *, + teacher_action: dict[str, Any] | None = None, + correction_memory_text: str = "", + strict: bool = False, +) -> tuple[str, str, dict[str, Any]]: + """Build staged prompts and JSON schema guidance for one step.""" + stage = observation.workflow_stage + allowed_actions = _narrow_allowed_actions( + observation.allowed_actions or STAGE_ACTIONS.get(stage, ALL_ACTIONS), + observation, + ) + required_fields = observation.required_fields_by_action or { + action: REQUIRED_FIELDS[action] for action in allowed_actions + } + del teacher_action + example = observation.valid_action_example or _default_example(stage, observation) + if example.get("action_type") not in allowed_actions: + example = _default_example(stage, observation) + user_prompt = _build_user_prompt( + observation, + allowed_actions, + required_fields, + example=example, + correction_hint=None if strict else correction_memory_text.strip() or None, + ) + + response_format = _response_format_for_actions(observation, allowed_actions) + return TRAINING_SYSTEM_PROMPT, user_prompt, response_format + + +def build_repair_request( + observation: UnifiedIncidentObservation, + *, + raw_bad_output: str, + parse_error: str, + teacher_action: dict[str, Any] | None = None, +) -> tuple[str, str]: + """Build one-shot repair prompt after a schema failure.""" + stage = observation.workflow_stage + example = teacher_action or observation.valid_action_example or _default_example(stage, observation) + system = ( + "Your previous response was invalid.\n" + "Return exactly one valid JSON object.\n" + "No explanation. No extra keys." + ) + user = ( + f"Stage: {stage}\n" + f"Error: {parse_error}\n" + f"Bad output: {raw_bad_output}\n" + f"Example: {json.dumps(example, separators=(',', ':'))}" + ) + return system, user + + +def _default_example( + stage: str, + observation: UnifiedIncidentObservation, +) -> dict[str, Any]: + if stage in {"diagnosis", "root_cause_analysis"}: + unhealthy = [ + service + for service, payload in observation.service_health.items() + if payload.status != "healthy" + ] + service = unhealthy[0] if unhealthy else "database" + if stage == "diagnosis": + if service == "database": + return {"action_type": "query_logs", "service": "database"} + return {"action_type": "query_metrics", "service": service, "metric": "cpu"} + return {"action_type": "query_dependencies", "service": "api-gateway"} + if stage == "security_subquest": + context = observation.security_context + if not context.code_visible: + return {"action_type": "inspect_code"} + if context.selected_vulnerability is None: + return { + "action_type": "classify_vulnerability", + "vulnerability_type": "sql_injection", + } + if context.selected_patch is None: + return {"action_type": "apply_patch", "patch_id": "parameterized_query"} + if context.exploit_blocked is not True: + return {"action_type": "verify_security_fix"} + return {"action_type": "submit_security_fix"} + if stage in {"remediation", "verification"}: + unhealthy = [ + service + for service, payload in observation.service_health.items() + if payload.status != "healthy" + ] + service = unhealthy[0] if unhealthy else "database" + return {"action_type": "restart_service", "service": service} + if stage == "postmortem": + return { + "action_type": "submit_postmortem", + "postmortem": { + "root_cause": "The security issue caused the outage.", + "attack_vector": "The vulnerable path was abused.", + "timeline": ["Investigated", "Patched", "Recovered"], + "remediation_steps": ["Patch", "Restart"], + "prevention_steps": ["Detection", "Hardening"], + }, + } + return {"action_type": "query_logs", "service": "database"} + + +def _response_format_for_actions( + observation: UnifiedIncidentObservation, + allowed_actions: list[str], +) -> dict[str, Any]: + properties: dict[str, Any] = { + "action_type": { + "type": "string", + "enum": allowed_actions, + } + } + if any(action in allowed_actions for action in {"query_logs", "query_metrics", "query_dependencies", "restart_service", "rollback_deploy"}): + services = sorted(observation.service_health.keys()) + properties["service"] = {"type": "string", "enum": services} + if "query_metrics" in allowed_actions: + properties["metric"] = {"type": "string", "enum": ["cpu", "memory", "latency", "error_rate", "throughput"]} + if "classify_vulnerability" in allowed_actions: + properties["vulnerability_type"] = { + "type": "string", + "enum": ["sql_injection", "broken_access_control", "command_injection"], + } + if "apply_patch" in allowed_actions: + patch_options = _patch_options(observation) + properties["patch_id"] = { + "type": "string", + "enum": patch_options or ["parameterized_query", "enforce_admin_role", "avoid_shell"], + } + if "submit_postmortem" in allowed_actions: + properties["postmortem"] = {"type": "object"} + + required = ["action_type"] + example = observation.valid_action_example or {} + for field in ("service", "metric", "vulnerability_type", "patch_id", "postmortem"): + if field in properties and field in example: + required.append(field) + + schema = { + "type": "object", + "properties": properties, + "required": required, + "additionalProperties": False, + } + return { + "type": "json_schema", + "json_schema": { + "name": "unified_incident_action", + "strict": True, + "schema": schema, + }, + } + + +def _patch_options(observation: UnifiedIncidentObservation) -> list[str]: + sources = [observation.tool_output or "", observation.prompt_text] + for source in sources: + marker = "Patch options:" + if marker not in source: + continue + suffix = source.split(marker, 1)[1].splitlines()[0] + return [item.strip() for item in suffix.split(",") if item.strip()] + return [] + + +def _build_required_fields_block( + required_fields: dict[str, list[str]], + allowed_actions: list[str], +) -> str: + lines = [] + for action in allowed_actions: + fields = required_fields.get(action, []) + if fields: + lines.append(f"- {action} -> {', '.join(fields)}") + else: + lines.append(f"- {action} -> none") + return "\n".join(lines) or "- none" + + +def _build_patch_ids_block(patch_ids: list[str]) -> str: + if not patch_ids: + return "" + lines = ["Available patch IDs:"] + lines.extend(f"- {patch_id}" for patch_id in patch_ids) + lines.append("") + return "\n".join(lines) + + +def _build_transition_block(observation: UnifiedIncidentObservation) -> str: + transition_hint = _transition_hint(observation) + if not transition_hint: + return "" + return f"Important transition hint:\n- {transition_hint}\n\n" + + +def _build_negative_reward_block(correction_hint: str | None) -> str: + if not correction_hint: + return "" + compact = correction_hint.splitlines()[0].strip() + return f"Previous action correction:\n- {compact}\n\n" + + +def _bool_text(value: bool | None) -> str: + if value is None: + return "unknown" + return str(value).lower() + + +def _render_tool_output(observation: UnifiedIncidentObservation) -> str: + if not observation.tool_output: + return "" + + if observation.workflow_stage in {"security_subquest", "verification"}: + lines = [line.rstrip() for line in observation.tool_output.splitlines() if line.strip()] + return "\n".join(lines[:6]) + + return observation.tool_output.splitlines()[0] + + +def _build_state_block(observation: UnifiedIncidentObservation) -> str: + lines: list[str] = [] + if observation.active_alerts: + lines.append("Active alerts:") + for alert in observation.active_alerts[:3]: + lines.append(f"- {alert.service}: {alert.severity} - {alert.message}") + + lines.append(f"Final score: {observation.final_score:.4f}") + + if observation.last_action_result: + lines.append(f"Last action result: {observation.last_action_result}") + + if observation.tool_output: + rendered_tool_output = _render_tool_output(observation) + if "\n" in rendered_tool_output: + lines.append("Tool output:") + lines.extend(rendered_tool_output.splitlines()) + else: + lines.append(f"Tool output: {rendered_tool_output}") + + security = observation.security_context + if observation.workflow_stage in {"security_subquest", "verification"}: + lines.append( + "Security status: " + f"code visible = {str(security.code_visible).lower()}, " + f"vulnerability classified = {str(security.selected_vulnerability is not None).lower()}, " + f"patch applied = {str(security.selected_patch is not None).lower()}, " + f"exploit blocked = {_bool_text(security.exploit_blocked)}, " + f"functionality preserved = {_bool_text(security.functionality_preserved)}" + ) + + if observation.security_unlock_reason: + lines.append(f"Security unlock reason: {observation.security_unlock_reason}") + + if observation.blocked_until_security_complete: + lines.append("Recovery gate: security must be completed before recovery.") + + return "\n".join(lines) or "- none" + + +def _transition_hint(observation: UnifiedIncidentObservation) -> str | None: + if observation.blocked_until_security_complete: + return "security must be completed before recovery" + stage = observation.workflow_stage + security = observation.security_context + if stage == "root_cause_analysis": + return "confirm the root cause with one more relevant investigation step" + if stage == "security_subquest": + if not security.code_visible: + return "inspection must happen before classification or patching" + if security.selected_vulnerability is None: + return "classification is required before patching" + if security.selected_patch is None: + return "classification is complete; next required action family is patching" + if security.exploit_blocked is not True or security.functionality_preserved is not True: + return "patching is complete; verification is required before submission" + return "security is fixed; submit the security fix before recovery" + if stage == "remediation": + return "recover infrastructure only after security is completed" + if stage == "verification": + return "verify both security remediation and infrastructure recovery" + if stage == "postmortem": + return "submit the final incident summary" + return None + + +def _build_user_prompt( + observation: UnifiedIncidentObservation, + allowed_actions: list[str], + required_fields: dict[str, list[str]], + *, + example: dict[str, Any], + correction_hint: str | None = None, +) -> str: + return USER_PROMPT_TEMPLATE.format( + stage=observation.workflow_stage, + goal=STAGE_GOALS.get(observation.workflow_stage, "take the best next action"), + allowed_actions_block="\n".join(f"- {action}" for action in allowed_actions) or "- none", + required_fields_block=_build_required_fields_block(required_fields, allowed_actions), + patch_ids_block=_build_patch_ids_block( + _patch_options(observation) if "apply_patch" in allowed_actions else [] + ), + transition_block=_build_transition_block(observation), + negative_reward_block=_build_negative_reward_block(correction_hint), + state_block=_build_state_block(observation), + valid_example=json.dumps(example, separators=(",", ":")), + ) + + +def _narrow_allowed_actions( + allowed_actions: list[str], + observation: UnifiedIncidentObservation, +) -> list[str]: + if observation.workflow_stage not in {"security_subquest", "verification"}: + return allowed_actions + + context = observation.security_context + if not context.code_visible and "inspect_code" in allowed_actions: + return ["inspect_code"] + if context.code_visible and context.selected_vulnerability is None and "classify_vulnerability" in allowed_actions: + return ["classify_vulnerability"] + if context.selected_vulnerability is not None and context.selected_patch is None and "apply_patch" in allowed_actions: + return ["apply_patch"] + if ( + context.selected_patch is not None + and (context.exploit_blocked is not True or context.functionality_preserved is not True) + and "verify_security_fix" in allowed_actions + ): + return ["verify_security_fix"] + if ( + context.exploit_blocked is True + and context.functionality_preserved is True + and "submit_security_fix" in allowed_actions + ): + return ["submit_security_fix"] + return allowed_actions diff --git a/unified_incident_env/trainer/reporting.py b/unified_incident_env/trainer/reporting.py new file mode 100644 index 0000000000000000000000000000000000000000..12f19222d7bb5fb5bdfa3b2740cb1624af04af0b --- /dev/null +++ b/unified_incident_env/trainer/reporting.py @@ -0,0 +1,227 @@ +"""Session reporting and artifact writing helpers.""" + +from __future__ import annotations + +import json +from pathlib import Path + +from .types import EpisodeRecord, PhaseDeltaRecord, SessionPhaseReport, SessionReport + + +def build_phase_deltas(phase_reports: list[SessionPhaseReport]) -> list[PhaseDeltaRecord]: + """Compute deltas from one phase to the next.""" + deltas: list[PhaseDeltaRecord] = [] + previous = None + for report in phase_reports: + if previous is None: + delta = PhaseDeltaRecord( + phase_name=report.phase_name, + score_delta=0.0, + schema_failure_delta=0.0, + loop_failure_delta=0.0, + success_delta=0.0, + ) + else: + delta = PhaseDeltaRecord( + phase_name=report.phase_name, + score_delta=round(report.avg_score - previous.avg_score, 4), + schema_failure_delta=round( + (report.schema_failures / len(report.episode_ids)) + - (previous.schema_failures / len(previous.episode_ids)), + 4, + ), + loop_failure_delta=round( + _avg_loop_failures(report) - _avg_loop_failures(previous), + 4, + ), + success_delta=round(report.success_rate - previous.success_rate, 4), + ) + deltas.append(delta) + previous = report + return deltas + + +def _avg_loop_failures(report: SessionPhaseReport) -> float: + if not report.episode_ids: + return 0.0 + return float(report.loop_failures) / len(report.episode_ids) + + +def redact_episode_prompts( + records: list[EpisodeRecord], + *, + include_prompts: bool, +) -> list[EpisodeRecord]: + """Redact prompt text fields when compact artifacts are desired.""" + if include_prompts: + return records + redacted: list[EpisodeRecord] = [] + for record in records: + step_records = [] + for step in record.step_records: + observation = dict(step.observation) + if "prompt_text" in observation: + observation["prompt_text"] = "" + step_records.append( + step.model_copy( + update={ + "prompt_text": "", + "next_prompt_text": None, + "observation": observation, + } + ) + ) + redacted.append(record.model_copy(update={"step_records": step_records})) + return redacted + + +def write_jsonl_episodes(path: Path, records: list[EpisodeRecord]) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("w", encoding="utf-8") as handle: + for record in records: + handle.write(record.model_dump_json()) + handle.write("\n") + + +def build_final_report(report: SessionReport) -> dict: + """Build the high-level report JSON shown at the end of a session.""" + summaries = report.episode_summaries + final_report = { + "session_id": report.session_id, + "model_name": report.model_name, + "runtime_mode": report.runtime_mode, + "initial_avg_score": 0.0, + "final_avg_score": 0.0, + "success_count": 0, + "schema_failure_rate": 0.0, + "json_valid_rate": 0.0, + "strict_schema_valid_rate": 0.0, + "average_repeated_action_count": 0.0, + "teacher_override_rate": 0.0, + "repair_retry_success_rate": 0.0, + "schema_to_policy_progression_rate": 0.0, + "security_unlock_rate": 0.0, + "security_completion_rate": 0.0, + "full_task_completion_rate": 0.0, + "average_steps_to_termination": 0.0, + "average_score_per_scenario": {}, + "phase_to_phase_score_delta": { + delta.phase_name: delta.score_delta for delta in report.phase_deltas + }, + "improvement_metrics": report.improvement_metrics, + "correction_memory_stats": report.correction_memory_stats, + } + if summaries: + initial = [item for item in summaries if item.episode_id in {1, 2}] + final = [item for item in summaries if item.episode_id in {9, 10}] + final_report["initial_avg_score"] = round( + sum(item.final_score for item in initial) / len(initial), 4 + ) if initial else 0.0 + final_report["final_avg_score"] = round( + sum(item.final_score for item in final) / len(final), 4 + ) if final else 0.0 + final_report["success_count"] = sum(1 for item in summaries if item.success) + final_report["schema_failure_rate"] = round( + sum(item.schema_failures for item in summaries) / len(summaries), + 4, + ) + final_report["json_valid_rate"] = round( + sum(1 for item in summaries if item.schema_failures == 0) / len(summaries), + 4, + ) + final_report["strict_schema_valid_rate"] = round( + sum( + 1 + for item in summaries + if item.mode == "strict" and item.schema_failures == 0 + ) + / max(1, sum(1 for item in summaries if item.mode == "strict")), + 4, + ) + final_report["average_repeated_action_count"] = round( + sum(len(item.looping_failures) for item in summaries) / len(summaries), + 4, + ) + final_report["teacher_override_rate"] = round( + sum(item.teacher_override_count for item in summaries) + / max(1, sum(item.steps for item in summaries)), + 4, + ) + retry_steps = sum(item.repair_retry_count for item in summaries) + repaired_steps = sum( + 1 + for item in summaries + if item.repair_retry_count > 0 and item.schema_failures == 0 + ) + final_report["repair_retry_success_rate"] = round( + repaired_steps / max(1, retry_steps), + 4, + ) + policy_after_schema = sum( + 1 + for item in summaries + if item.schema_failures == 0 and item.policy_failures + ) + final_report["schema_to_policy_progression_rate"] = round( + policy_after_schema / len(summaries), + 4, + ) + final_report["security_unlock_rate"] = round( + sum( + 1 + for item in summaries + if item.stopped_reason not in {"diagnosis", "root_cause_analysis"} + ) + / len(summaries), + 4, + ) + final_report["security_completion_rate"] = round( + sum( + 1 + for item in summaries + if "infra_before_security" not in item.policy_failures + and item.stopped_reason not in {"security_subquest"} + ) + / len(summaries), + 4, + ) + final_report["full_task_completion_rate"] = round( + sum(1 for item in summaries if item.success) / len(summaries), + 4, + ) + final_report["average_steps_to_termination"] = round( + sum(item.steps for item in summaries) / len(summaries), + 4, + ) + by_scenario: dict[str, list[float]] = {} + for item in summaries: + by_scenario.setdefault(item.scenario_id, []).append(item.final_score) + final_report["average_score_per_scenario"] = { + scenario_id: round(sum(scores) / len(scores), 4) + for scenario_id, scores in by_scenario.items() + } + return final_report + + +def write_session_outputs( + *, + output_dir: Path, + report: SessionReport, + records: list[EpisodeRecord], + include_prompts: bool, +) -> None: + """Write all top-level session artifacts.""" + redacted_records = redact_episode_prompts(records, include_prompts=include_prompts) + write_jsonl_episodes(output_dir / "episodes.jsonl", redacted_records) + (output_dir / "summaries.json").write_text( + report.model_dump_json(indent=2), + encoding="utf-8", + ) + (output_dir / "phase_delta.json").write_text( + json.dumps([item.model_dump() for item in report.phase_deltas], indent=2), + encoding="utf-8", + ) + (output_dir / "final_report.json").write_text( + json.dumps(build_final_report(report), indent=2), + encoding="utf-8", + ) diff --git a/unified_incident_env/trainer/run_episode.py b/unified_incident_env/trainer/run_episode.py new file mode 100644 index 0000000000000000000000000000000000000000..d5b8ce974e9242dbd71e12fe38849428606196c1 --- /dev/null +++ b/unified_incident_env/trainer/run_episode.py @@ -0,0 +1,354 @@ +"""Episode runner for strict and lenient training/eval modes.""" + +from __future__ import annotations + +import argparse +import json +import os +import time +import uuid +from pathlib import Path +from typing import Any + +from ..client import UnifiedIncidentEnv +from ..models import UnifiedIncidentAction +from ..scripts.baseline_agent import plan_for_scenario +from ..server.challenge import SCENARIOS +from ..server.environment import UnifiedIncidentEnvironment +from .action_adapter import LenientActionAdapter, StrictActionParser +from .backend import OpenAICompatibleBackend +from .prompts import build_repair_request, build_runtime_request +from .trajectory_memory import CorrectionMemory +from .trajectory_store import TrajectoryStore +from .types import EpisodeRecord, ModelRequest, StepRecord + + +class EpisodeRunner: + """Runs one model through one scenario and records a full trajectory.""" + + def __init__( + self, + *, + backend, + parser, + model_name: str, + base_url: str | None = None, + env_factory=None, + correction_memory: CorrectionMemory | None = None, + ) -> None: + self.backend = backend + self.parser = parser + self.model_name = model_name + self.base_url = base_url + self.env_factory = env_factory + self.correction_memory = correction_memory or CorrectionMemory() + + def run( + self, + scenario_id: str, + mode: str, + *, + episode_id: int | None = None, + model_version: str | None = None, + ) -> EpisodeRecord: + started = time.perf_counter() + scenario = SCENARIOS[scenario_id] + teacher_plan = plan_for_scenario(scenario_id) + step_records: list[StepRecord] = [] + failure_reason: str | None = None + final_score = 0.0 + success = False + steps_taken = 0 + previous_action: dict[str, Any] | None = None + repeated_no_progress = 0 + + with self._env_context() as env: + observation = self._unwrap_observation(env.reset(scenario_id=scenario_id)) + while not observation.done: + teacher_action = ( + teacher_plan[steps_taken].model_dump(exclude_none=True) + if steps_taken < len(teacher_plan) + else None + ) + correction_text = self.correction_memory.build_prompt_addendum( + scenario_id=scenario_id, + stage=observation.workflow_stage, + ) + system_prompt, user_prompt, response_format = build_runtime_request( + observation, + teacher_action=teacher_action, + correction_memory_text=correction_text, + strict=(mode == "strict"), + ) + structured_mode = "backend_adaptive" + raw_model_output = "" + parse_result = None + repair_retry_used = False + teacher_override_used = False + + if ( + mode == "lenient" + and repeated_no_progress >= 2 + and teacher_action is not None + ): + parse_result = self._teacher_override_parse_result(teacher_action) + raw_model_output = json.dumps(teacher_action) + teacher_override_used = True + repeated_no_progress = 0 + else: + response = self.backend.complete( + ModelRequest( + model_name=self.model_name, + system_prompt=system_prompt, + user_prompt=user_prompt, + structured_mode=structured_mode, + response_format=response_format, + ) + ) + raw_model_output = response.raw_text + parse_result = self.parser.parse(response.raw_text) + + if ( + mode == "lenient" + and parse_result.cleaned_action is None + ): + repair_retry_used = True + repair_system, repair_user = build_repair_request( + observation, + raw_bad_output=response.raw_text, + parse_error=parse_result.error or parse_result.parse_status, + teacher_action=teacher_action, + ) + repair_response = self.backend.complete( + ModelRequest( + model_name=self.model_name, + system_prompt=repair_system, + user_prompt=repair_user, + structured_mode=structured_mode, + response_format=response_format, + temperature=0.0, + ) + ) + raw_model_output = repair_response.raw_text + parse_result = self.parser.parse(repair_response.raw_text) + + if ( + mode == "lenient" + and parse_result.cleaned_action is None + and teacher_action is not None + ): + parse_result = self._teacher_override_parse_result(teacher_action) + raw_model_output = json.dumps(teacher_action) + teacher_override_used = True + + assert parse_result is not None + if parse_result.cleaned_action is None: + failure_reason = f"parse_failure:{parse_result.parse_status}" + observation_payload = observation.model_dump() + step_records.append( + StepRecord( + episode_id=episode_id, + scenario_id=scenario_id, + step_index=steps_taken + 1, + tick=observation.tick_count, + workflow_stage=observation.workflow_stage, + observation=observation_payload, + prompt_text=observation.prompt_text, + raw_model_output=raw_model_output, + parse_status=parse_result.parse_status, + normalization_applied=parse_result.repair_labels, + cleaned_action=None, + teacher_action=teacher_action, + reward=None, + cumulative_score=observation.final_score, + final_score_after_step=observation.final_score, + done=False, + next_prompt_text=None, + structured_mode_used=structured_mode, + repair_retry_used=repair_retry_used, + teacher_override_used=teacher_override_used, + failure_reason=parse_result.error, + failure_type=parse_result.parse_status, + ) + ) + break + + action = UnifiedIncidentAction(**parse_result.cleaned_action) + step = self._unwrap_observation(env.step(action)) + observation_payload = observation.model_dump() + step_records.append( + StepRecord( + episode_id=episode_id, + scenario_id=scenario_id, + step_index=steps_taken + 1, + tick=observation.tick_count, + workflow_stage=observation.workflow_stage, + observation=observation_payload, + prompt_text=observation.prompt_text, + raw_model_output=raw_model_output, + parse_status=parse_result.parse_status, + normalization_applied=parse_result.repair_labels, + cleaned_action=parse_result.cleaned_action, + teacher_action=teacher_action, + reward=step.reward, + cumulative_score=step.final_score, + final_score_after_step=step.final_score, + done=step.done, + next_prompt_text=step.prompt_text, + structured_mode_used=structured_mode, + repair_retry_used=repair_retry_used, + teacher_override_used=teacher_override_used, + failure_reason=None, + failure_type=None, + ) + ) + if step.reward is not None and step.reward <= 0 and parse_result.cleaned_action == previous_action: + repeated_no_progress += 1 + else: + repeated_no_progress = 0 + previous_action = parse_result.cleaned_action + observation = step + steps_taken += 1 + + if failure_reason is None: + success = bool(observation.done and observation.incident_resolved) + final_score = observation.final_score + if not success: + failure_reason = f"stopped:{observation.workflow_stage}" + else: + final_score = observation.final_score + + elapsed_s = round(time.perf_counter() - started, 4) + return EpisodeRecord( + run_id=str(uuid.uuid4()), + episode_id=episode_id, + scenario_id=scenario_id, + difficulty=scenario["difficulty"], + model_name=self.model_name, + model_version=model_version, + mode=mode, + success=success, + final_score=final_score, + steps=steps_taken, + failure_reason=failure_reason, + elapsed_s=elapsed_s, + json_valid_steps=sum( + 1 for step in step_records if step.parse_status in {"ok", "repaired", "teacher_override"} + ), + strict_schema_valid_steps=sum( + 1 for step in step_records if step.parse_status == "ok" + ), + teacher_override_count=sum(1 for step in step_records if step.teacher_override_used), + repair_retry_count=sum(1 for step in step_records if step.repair_retry_used), + security_subquest_completed=bool( + step_records and any( + step.cleaned_action and step.cleaned_action.get("action_type") == "submit_security_fix" + for step in step_records + ) + ), + postmortem_completed=bool( + step_records and any( + step.cleaned_action and step.cleaned_action.get("action_type") == "submit_postmortem" + for step in step_records + ) + ), + stopped_reason=( + failure_reason.split(":", 1)[1] + if failure_reason and failure_reason.startswith("stopped:") + else failure_reason + ), + step_records=step_records, + ) + + def _env_context(self): + if self.base_url is not None: + return UnifiedIncidentEnv(base_url=self.base_url).sync() + return _LocalEnvironmentContext(self.env_factory or UnifiedIncidentEnvironment) + + def _unwrap_observation(self, value): + return value.observation if hasattr(value, "observation") else value + + def _teacher_override_parse_result(self, teacher_action: dict[str, Any]): + from .types import ParseResult + + return ParseResult( + parse_status="teacher_override", + cleaned_action=teacher_action, + repair_labels=["teacher_override"], + ) + + +class _LocalEnvironmentContext: + """Simple context wrapper for direct environment instances.""" + + def __init__(self, factory) -> None: + self.factory = factory + self.instance = None + + def __enter__(self): + self.instance = self.factory() + return self.instance + + def __exit__(self, exc_type, exc, tb): + self.instance = None + return False + + +def run_episode( + *, + model_name: str, + scenario_id: str, + mode: str, + base_url: str | None, + api_base_url: str, + api_key: str, + output_path: Path | None = None, +) -> EpisodeRecord: + parser = StrictActionParser() if mode == "strict" else LenientActionAdapter() + backend = OpenAICompatibleBackend(base_url=api_base_url, api_key=api_key) + runner = EpisodeRunner( + backend=backend, + parser=parser, + model_name=model_name, + base_url=base_url, + ) + record = runner.run(scenario_id=scenario_id, mode=mode) + if output_path is not None: + TrajectoryStore(output_path).append_episode(record) + return record + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--model", required=True) + parser.add_argument("--scenario", required=True, choices=sorted(SCENARIOS)) + parser.add_argument("--mode", choices=["strict", "lenient"], default="strict") + parser.add_argument("--base-url", default=UnifiedIncidentEnv.DEFAULT_BASE_URL) + parser.add_argument( + "--api-base-url", + default=os.environ.get("API_BASE_URL", "http://127.0.0.1:11434/v1"), + ) + parser.add_argument( + "--api-key", + default=os.environ.get("OPENAI_API_KEY") or os.environ.get("HF_TOKEN") or "local", + ) + parser.add_argument( + "--output", + default="outputs/trainer/episodes.jsonl", + ) + args = parser.parse_args() + + record = run_episode( + model_name=args.model, + scenario_id=args.scenario, + mode=args.mode, + base_url=args.base_url, + api_base_url=args.api_base_url, + api_key=args.api_key, + output_path=Path(args.output), + ) + print(record.model_dump_json(indent=2)) + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/trainer/run_session.py b/unified_incident_env/trainer/run_session.py new file mode 100644 index 0000000000000000000000000000000000000000..31abaf5c4270baa306e607d5588f114cfe4e97b3 --- /dev/null +++ b/unified_incident_env/trainer/run_session.py @@ -0,0 +1,337 @@ +"""Ten-episode session loop with failure analysis and pluggable updates.""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path + +from .action_adapter import LenientActionAdapter, StrictActionParser +from .backend import OpenAICompatibleBackend +from .build_datasets import ( + build_baseline_records, + build_next_action_records, + build_recovery_records, + build_schema_repair_records, + combine_sft_records, + write_jsonl, +) +from .collect_trajectory import collect_episode +from .reporting import build_phase_deltas, write_session_outputs +from .run_episode import EpisodeRunner +from .session_config import make_session_config_from_env +from .policy_adapter import PolicyAdapter +from .trajectory_memory import CorrectionMemory +from .types import SessionPhaseReport, SessionReport, UpdateRequest, UpdateResult +from .update_model import build_updater + + +def _extract_policy_adapter_path(model_after: str) -> str | None: + prefix = "policy_adapter::" + if not model_after.startswith(prefix): + return None + parts = model_after.split("::", 2) + if len(parts) != 3: + return None + return parts[2] + + +def _base_model_from_version(model_version: str) -> str: + prefix = "policy_adapter::" + if model_version.startswith(prefix): + parts = model_version.split("::", 2) + if len(parts) == 3: + return parts[1] + return model_version + + +def _memory_from_policy_adapter(path: Path) -> CorrectionMemory: + adapter = PolicyAdapter.load(path) + memory = CorrectionMemory() + from .trajectory_memory import MemoryExample + + for row in adapter.schema_examples: + memory.schema_examples.setdefault(row["scenario_id"], []).append( + MemoryExample( + scenario_id=row["scenario_id"], + stage=row.get("metadata", {}).get("workflow_stage", "diagnosis"), + prompt_text=row["messages"][-1]["content"], + raw_output=json.dumps(row.get("student_action", {}) or {}), + corrected_action=row["target_action"], + tags=row.get("tags", []), + failure_type=(row.get("tags") or [None])[0], + action_family=None, + mistake=row.get("metadata", {}).get("failure_reason", ""), + correction=f"Use {row['target_action'].get('action_type', 'the valid action')} instead.", + ) + ) + for row in adapter.next_action_examples: + memory.next_action_examples.setdefault(row["scenario_id"], []).append( + MemoryExample( + scenario_id=row["scenario_id"], + stage=row.get("metadata", {}).get("workflow_stage", "diagnosis"), + prompt_text=row["messages"][-1]["content"], + raw_output=json.dumps(row.get("student_action", {}) or {}), + corrected_action=row["target_action"], + tags=row.get("tags", []), + failure_type=(row.get("tags") or [None])[0], + action_family=None, + mistake=row.get("metadata", {}).get("failure_reason", ""), + correction=f"Use {row['target_action'].get('action_type', 'the valid action')} instead.", + ) + ) + for row in adapter.recovery_examples: + memory.recovery_examples.setdefault(row["scenario_id"], []).append( + MemoryExample( + scenario_id=row["scenario_id"], + stage=row.get("metadata", {}).get("workflow_stage", "diagnosis"), + prompt_text=row["messages"][-1]["content"], + raw_output=json.dumps(row.get("student_action", {}) or {}), + corrected_action=row["target_action"], + tags=row.get("tags", []), + failure_type=(row.get("tags") or [None])[0], + action_family=None, + mistake=row.get("metadata", {}).get("failure_reason", ""), + correction=f"Recover with {row['target_action'].get('action_type', 'the valid action')}.", + ) + ) + return memory + + +def run_session(config, *, backend=None, runner_factory=EpisodeRunner, updater=None): + output_dir = Path(config.output_root) + output_dir.mkdir(parents=True, exist_ok=True) + checkpoints_dir = output_dir / "checkpoints" + checkpoints_dir.mkdir(parents=True, exist_ok=True) + + if backend is None: + backend = OpenAICompatibleBackend( + base_url=config.api_base_url, + api_key=config.api_key, + ) + if updater is None: + updater = build_updater( + config.updater_backend, + runtime_mode=config.runtime_mode, + command_template=config.updater_command_template, + openai_base_url=config.api_base_url, + api_key=config.api_key, + ) + phase_reports: list[SessionPhaseReport] = [] + + baseline_records = build_baseline_records() + write_jsonl(baseline_records, output_dir / "baseline_teacher_dataset.jsonl") + memory_path = output_dir / "correction_memory.json" + correction_memory = CorrectionMemory.load(memory_path) + + all_records = [] + all_summaries = [] + all_updates = [] + all_schema_records = [] + all_next_action_records = [] + all_recovery_records = [] + + current_model_version = config.initial_model_version + + for phase in config.phases: + mode = ( + config.final_eval_mode + if phase.phase_name == "final_evaluation" + else config.collection_mode + ) + parser_impl = StrictActionParser() if mode == "strict" else LenientActionAdapter() + runner = runner_factory( + backend=backend, + parser=parser_impl, + model_name=config.model_name, + base_url=config.base_url, + correction_memory=correction_memory, + ) + phase_records = [] + phase_summaries = [] + phase_analyses = [] + + for episode_id in phase.episode_ids: + scenario_id = config.scenario_schedule[episode_id - 1] + record, summary, analysis = collect_episode( + runner=runner, + scenario_id=scenario_id, + episode_id=episode_id, + mode=mode, + model_version=current_model_version, + ) + phase_records.append(record) + phase_summaries.append(summary) + phase_analyses.append(analysis) + all_records.append(record) + all_summaries.append(summary) + correction_memory.add_episode_examples(record, analysis) + correction_memory.save(memory_path) + + update_ids = [] + if phase.update_after and phase.update_index is not None: + update_dir = checkpoints_dir / f"update_{phase.update_index:02d}" + schema_records = build_schema_repair_records(phase_records, phase_analyses) + next_action_records = build_next_action_records(phase_records, phase_analyses) + recovery_records = build_recovery_records(phase_records, phase_analyses) + combined_records = combine_sft_records( + baseline_records=[], + schema_records=schema_records, + next_action_records=next_action_records, + recovery_records=recovery_records, + ) + write_jsonl(schema_records, update_dir / "schema_repair.jsonl") + write_jsonl(next_action_records, update_dir / "next_action.jsonl") + write_jsonl(recovery_records, update_dir / "recovery.jsonl") + write_jsonl(combined_records, update_dir / "sft_dataset.jsonl") + + all_schema_records.extend(schema_records) + all_next_action_records.extend(next_action_records) + all_recovery_records.extend(recovery_records) + + update_request = UpdateRequest( + update_index=phase.update_index, + phase_name=phase.phase_name, + episodes_used=phase.episode_ids, + datasets_used=[ + str(update_dir / "schema_repair.jsonl"), + str(update_dir / "next_action.jsonl"), + str(update_dir / "recovery.jsonl"), + str(update_dir / "sft_dataset.jsonl"), + ], + model_before=_base_model_from_version(current_model_version), + output_dir=str(update_dir.resolve()), + runtime_mode=config.runtime_mode, + command_template=config.updater_command_template, + training_file=str((update_dir / "sft_dataset.jsonl").resolve()), + suffix=f"{phase.phase_name[:10]}-{phase.update_index:02d}", + ) + update_result = updater.update(update_request) + if not isinstance(update_result, UpdateResult): + update_result = UpdateResult.model_validate( + update_result, + from_attributes=True, + ) + all_updates.append(update_result) + if update_result.status == "completed": + current_model_version = update_result.model_after + adapter_path = _extract_policy_adapter_path(update_result.model_after) + if adapter_path is not None and Path(adapter_path).exists(): + correction_memory.merge( + _memory_from_policy_adapter(Path(adapter_path)) + ) + correction_memory.save(memory_path) + update_ids.append(update_result.update_index) + + phase_report = SessionPhaseReport( + phase_name=phase.phase_name, + episode_ids=phase.episode_ids, + avg_score=round( + sum(summary.final_score for summary in phase_summaries) / len(phase_summaries), + 4, + ), + success_rate=round( + sum(1 for summary in phase_summaries if summary.success) / len(phase_summaries), + 4, + ), + schema_failures=sum(summary.schema_failures for summary in phase_summaries), + loop_failures=sum(len(summary.looping_failures) for summary in phase_summaries), + updates_applied=update_ids, + ) + phase_reports.append(phase_report) + + write_jsonl(all_schema_records, output_dir / "schema_repair.jsonl") + write_jsonl(all_next_action_records, output_dir / "next_action.jsonl") + write_jsonl(all_recovery_records, output_dir / "recovery.jsonl") + write_jsonl( + combine_sft_records( + baseline_records=baseline_records, + schema_records=all_schema_records, + next_action_records=all_next_action_records, + recovery_records=all_recovery_records, + ), + output_dir / "sft_dataset.jsonl", + ) + + initial_phase = [summary for summary in all_summaries if summary.episode_id in {1, 2}] + final_phase = [summary for summary in all_summaries if summary.episode_id in {9, 10}] + improvement_metrics = { + "score_delta_ep1_2_to_ep9_10": round( + ( + sum(item.final_score for item in final_phase) / len(final_phase) + if final_phase + else 0.0 + ) + - ( + sum(item.final_score for item in initial_phase) / len(initial_phase) + if initial_phase + else 0.0 + ), + 4, + ), + "schema_failure_delta_ep1_2_to_ep9_10": round( + ( + sum(item.schema_failures for item in initial_phase) / len(initial_phase) + if initial_phase + else 0.0 + ) + - ( + sum(item.schema_failures for item in final_phase) / len(final_phase) + if final_phase + else 0.0 + ), + 4, + ), + } + + report = SessionReport( + session_id=config.session_id, + model_name=config.model_name, + runtime_mode=config.runtime_mode, + output_dir=str(output_dir), + episode_summaries=all_summaries, + updates=all_updates, + phase_reports=phase_reports, + phase_deltas=build_phase_deltas(phase_reports), + improvement_metrics=improvement_metrics, + correction_memory_stats=correction_memory.stats(), + ) + write_session_outputs( + output_dir=output_dir, + report=report, + records=all_records, + include_prompts=config.log_rendered_prompts, + ) + return report + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--model", default="qwen2.5:1.5b") + parser.add_argument("--base-url", default="http://127.0.0.1:8000") + parser.add_argument("--output-root", default="outputs/trainer") + parser.add_argument("--runtime-mode", choices=["competition", "research"], default="competition") + parser.add_argument("--collection-mode", choices=["strict", "lenient"], default="lenient") + parser.add_argument("--final-eval-mode", choices=["strict", "lenient"], default="strict") + parser.add_argument("--updater-backend", choices=["noop", "external_command", "openai_finetune"], default="external_command") + parser.add_argument("--updater-command-template", default=None) + parser.add_argument("--log-rendered-prompts", action="store_true") + args = parser.parse_args() + + config = make_session_config_from_env( + model_name=args.model, + output_root=args.output_root, + base_url=args.base_url, + runtime_mode=args.runtime_mode, + collection_mode=args.collection_mode, + final_eval_mode=args.final_eval_mode, + updater_backend=args.updater_backend, + updater_command_template=args.updater_command_template, + log_rendered_prompts=args.log_rendered_prompts if args.log_rendered_prompts else None, + ) + report = run_session(config) + print(report.model_dump_json(indent=2)) + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/trainer/session_config.py b/unified_incident_env/trainer/session_config.py new file mode 100644 index 0000000000000000000000000000000000000000..519cad0bc39933d788b205398654d23274945556 --- /dev/null +++ b/unified_incident_env/trainer/session_config.py @@ -0,0 +1,125 @@ +"""Configuration helpers for the ten-episode session loop.""" + +from __future__ import annotations + +import os +import uuid +from pathlib import Path + +from ..server.challenge import SCENARIOS +from .types import RuntimeMode, SessionConfig, SessionPhaseConfig + +DEFAULT_UPDATE_EPISODES = (2, 5, 8) + + +def default_scenario_schedule() -> list[str]: + """Return a deterministic ten-episode round-robin schedule.""" + ordered = list(SCENARIOS) + schedule: list[str] = [] + while len(schedule) < 10: + schedule.extend(ordered) + return schedule[:10] + + +def default_phases() -> list[SessionPhaseConfig]: + """Return the fixed four-phase ten-episode schedule.""" + return [ + SessionPhaseConfig( + phase_name="probe", + episode_ids=[1, 2], + update_after=True, + update_index=1, + ), + SessionPhaseConfig( + phase_name="first_correction", + episode_ids=[3, 4, 5], + update_after=True, + update_index=2, + ), + SessionPhaseConfig( + phase_name="workflow_correction", + episode_ids=[6, 7, 8], + update_after=True, + update_index=3, + ), + SessionPhaseConfig( + phase_name="final_evaluation", + episode_ids=[9, 10], + update_after=False, + update_index=None, + ), + ] + + +def make_session_config( + *, + model_name: str, + output_root: str | Path, + base_url: str | None, + api_base_url: str, + api_key: str, + runtime_mode: RuntimeMode = "competition", + collection_mode: str = "lenient", + final_eval_mode: str = "strict", + updater_backend: str = "external_command", + updater_command_template: str | None = None, + scenario_schedule: list[str] | None = None, + phases: list[SessionPhaseConfig] | None = None, + log_rendered_prompts: bool | None = None, +) -> SessionConfig: + """Create one deterministic session config.""" + session_id = uuid.uuid4().hex[:12] + output_root_path = Path(output_root) + output_dir = output_root_path / f"session-{session_id}" + schedule = scenario_schedule or default_scenario_schedule() + if log_rendered_prompts is None: + log_rendered_prompts = runtime_mode == "research" + return SessionConfig( + session_id=session_id, + model_name=model_name, + initial_model_version=model_name, + runtime_mode=runtime_mode, + collection_mode=collection_mode, + final_eval_mode=final_eval_mode, + log_rendered_prompts=log_rendered_prompts, + base_url=base_url, + api_base_url=api_base_url, + api_key=api_key, + output_root=str(output_dir), + scenario_schedule=schedule, + phases=phases or default_phases(), + updater_backend=updater_backend, + updater_command_template=updater_command_template, + ) + + +def make_session_config_from_env( + *, + model_name: str = "qwen2.5:1.5b", + output_root: str | Path = "outputs/trainer", + base_url: str | None = None, + runtime_mode: RuntimeMode = "competition", + collection_mode: str = "lenient", + final_eval_mode: str = "strict", + updater_backend: str = "external_command", + updater_command_template: str | None = None, + phases: list[SessionPhaseConfig] | None = None, + log_rendered_prompts: bool | None = None, +) -> SessionConfig: + """Create a session config using the common environment variables.""" + return make_session_config( + model_name=model_name, + output_root=output_root, + base_url=base_url, + runtime_mode=runtime_mode, + api_base_url=os.environ.get("API_BASE_URL", "http://127.0.0.1:11434/v1"), + api_key=os.environ.get("OPENAI_API_KEY") + or os.environ.get("HF_TOKEN") + or "local", + collection_mode=collection_mode, + final_eval_mode=final_eval_mode, + updater_backend=updater_backend, + updater_command_template=updater_command_template, + phases=phases, + log_rendered_prompts=log_rendered_prompts, + ) diff --git a/unified_incident_env/trainer/train_external.py b/unified_incident_env/trainer/train_external.py new file mode 100644 index 0000000000000000000000000000000000000000..6a682185053f84994bb41aba9ad3d91c48c232b1 --- /dev/null +++ b/unified_incident_env/trainer/train_external.py @@ -0,0 +1,89 @@ +"""Default lightweight external trainer wrapper for session updates.""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path + +from .policy_adapter import PolicyAdapter +from .types import UpdateRequest + + +def _load_jsonl(path: Path) -> list[dict]: + if not path.exists(): + return [] + rows = [] + with path.open(encoding="utf-8") as handle: + for line in handle: + line = line.strip() + if line: + rows.append(json.loads(line)) + return rows + + +def build_policy_adapter(request: UpdateRequest) -> PolicyAdapter: + """Build a lightweight policy adapter from phase datasets.""" + schema_rows = _load_jsonl(Path(request.output_dir) / "schema_repair.jsonl") + next_action_rows = _load_jsonl(Path(request.output_dir) / "next_action.jsonl") + recovery_rows = _load_jsonl(Path(request.output_dir) / "recovery.jsonl") + + if request.phase_name == "probe": + schema_rows = schema_rows[:12] + next_action_rows = next_action_rows[:4] + recovery_rows = [] + elif request.phase_name == "first_correction": + schema_rows = schema_rows[:12] + next_action_rows = next_action_rows[:12] + recovery_rows = [] + else: + schema_rows = schema_rows[:8] + next_action_rows = next_action_rows[:8] + recovery_rows = recovery_rows[:12] + + return PolicyAdapter( + base_model=_base_model_from_version(request.model_before), + phase_name=request.phase_name, + schema_examples=schema_rows, + next_action_examples=next_action_rows, + recovery_examples=recovery_rows, + ) + + +def _base_model_from_version(model_version: str) -> str: + prefix = "policy_adapter::" + if model_version.startswith(prefix): + parts = model_version.split("::", 2) + if len(parts) == 3: + return parts[1] + return model_version + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--request", required=True) + args = parser.parse_args() + + request = UpdateRequest.model_validate_json(Path(args.request).read_text(encoding="utf-8")) + output_dir = Path(request.output_dir) + output_dir.mkdir(parents=True, exist_ok=True) + adapter = build_policy_adapter(request) + adapter_path = output_dir / "policy_adapter.json" + adapter.save(adapter_path) + result = { + "status": "completed", + "model_after": f"policy_adapter::{request.model_before}::{adapter_path}", + "notes": ( + f"built adapter with schema={len(adapter.schema_examples)} " + f"next_action={len(adapter.next_action_examples)} " + f"recovery={len(adapter.recovery_examples)}" + ), + "artifact_paths": [str(adapter_path)], + } + result_path = output_dir / f"update_{request.update_index:02d}_external_result.json" + result_path.write_text(json.dumps(result, indent=2), encoding="utf-8") + print(json.dumps(result, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/unified_incident_env/trainer/trajectory_memory.py b/unified_incident_env/trainer/trajectory_memory.py new file mode 100644 index 0000000000000000000000000000000000000000..9fd69467936a8b759a20b0a542a0d70792f2e26c --- /dev/null +++ b/unified_incident_env/trainer/trajectory_memory.py @@ -0,0 +1,351 @@ +"""Compact policy-card memory for in-session improvement without weight updates.""" + +from __future__ import annotations + +import json +from dataclasses import dataclass, field +from pathlib import Path + +from .types import EpisodeRecord, FailureAnalysisReport + +SCHEMA_RULES = [ + "Return JSON only.", + "Use action_type.", + "Use only allowed actions.", + "Do not add explanation text.", +] +MAX_POLICY_WORDS = 300 + + +@dataclass +class MemoryExample: + """One stored correction example.""" + + scenario_id: str + stage: str + prompt_text: str + raw_output: str + corrected_action: dict + tags: list[str] = field(default_factory=list) + bucket: str = "next_action" + failure_type: str | None = None + action_family: str | None = None + mistake: str = "" + correction: str = "" + + +@dataclass +class CorrectionMemory: + """Stores compact policy lessons and examples across episodes.""" + + max_examples_per_bucket: int = 6 + schema_examples: dict[str, list[MemoryExample]] = field(default_factory=dict) + next_action_examples: dict[str, list[MemoryExample]] = field(default_factory=dict) + recovery_examples: dict[str, list[MemoryExample]] = field(default_factory=dict) + + def add_phase_examples( + self, + episodes: list[EpisodeRecord], + analyses: list[FailureAnalysisReport], + ) -> None: + analysis_map = { + analysis.episode_ids[0]: analysis for analysis in analyses if analysis.episode_ids + } + for episode in episodes: + analysis = analysis_map.get(episode.episode_id or 0) + if analysis is not None: + self.add_episode_examples(episode, analysis) + + def add_episode_examples( + self, + episode: EpisodeRecord, + analysis: FailureAnalysisReport, + ) -> None: + """Update memory immediately after one episode.""" + entries_by_step: dict[int, list] = {} + for entry in analysis.entries: + if entry.step_index is None: + continue + entries_by_step.setdefault(entry.step_index, []).append(entry) + + for step in episode.step_records: + if step.teacher_action is None: + continue + step_entries = entries_by_step.get(step.step_index, []) + if not step_entries and step.parse_status not in { + "invalid_json", + "invalid_action", + "repaired", + "teacher_override", + }: + continue + + primary = step_entries[0] if step_entries else None + failure_type = primary.failure_type if primary else _schema_failure_type(step.parse_status) + bucket = primary.bucket if primary else "schema" + mistake = _mistake_text(step, primary) + correction = _correction_text(step) + example = MemoryExample( + scenario_id=episode.scenario_id, + stage=step.workflow_stage, + prompt_text=step.prompt_text, + raw_output=step.raw_model_output, + corrected_action=step.teacher_action, + tags=list({entry.failure_type for entry in step_entries} or ([failure_type] if failure_type else [])), + bucket=bucket, + failure_type=failure_type, + action_family=_action_family(step.teacher_action), + mistake=mistake, + correction=correction, + ) + + if bucket == "schema" or step.parse_status in { + "invalid_json", + "invalid_action", + "repaired", + "teacher_override", + }: + self._append(self.schema_examples, episode.scenario_id, example) + if bucket in {"policy", "reasoning"}: + self._append(self.next_action_examples, episode.scenario_id, example) + if bucket == "looping": + self._append(self.recovery_examples, episode.scenario_id, example) + + def build_prompt_addendum(self, scenario_id: str, stage: str) -> str: + """Return a compact rolling policy card for the current scenario and stage.""" + schema = self._select_examples(self.schema_examples, scenario_id, stage, limit=1) + failures = self._select_examples(self.next_action_examples, scenario_id, stage, limit=1) + recoveries = self._select_examples(self.recovery_examples, scenario_id, stage, limit=1) + examples = self._select_examples( + { + **self.schema_examples, + **{ + key: self.next_action_examples.get(key, []) + self.recovery_examples.get(key, []) + for key in set(self.next_action_examples) | set(self.recovery_examples) + }, + }, + scenario_id, + stage, + limit=1, + ) + + lines = ["Schema rules:"] + lines.extend(f"- {rule}" for rule in SCHEMA_RULES) + + lesson_lines: list[str] = [] + for item in schema + failures + recoveries: + lesson_lines.append(f"- Mistake: {item.mistake}") + lesson_lines.append(f"- Correction: {item.correction}") + + if lesson_lines: + lines.append("") + lines.append("Episode lessons:") + lines.extend(lesson_lines[:6]) + + if failures: + item = failures[0] + lines.append("") + lines.append("Relevant past failure:") + lines.append(f"- {item.failure_type or 'wrong_action'}: {item.mistake}") + + if recoveries: + item = recoveries[0] + lines.append("") + lines.append("Relevant recovery:") + lines.append( + f"- recover with {json.dumps(item.corrected_action, separators=(',', ':'))}" + ) + + if examples: + item = examples[0] + lines.append("") + lines.append("Valid example:") + lines.append( + f"- {json.dumps(item.corrected_action, separators=(',', ':'))}" + ) + + rendered = "\n".join(lines) + return _limit_words(rendered, MAX_POLICY_WORDS) + + def stats(self) -> dict[str, int]: + """Return compact memory counts.""" + return { + "schema_examples": sum(len(v) for v in self.schema_examples.values()), + "next_action_examples": sum(len(v) for v in self.next_action_examples.values()), + "recovery_examples": sum(len(v) for v in self.recovery_examples.values()), + } + + def merge(self, other: "CorrectionMemory") -> None: + """Merge another memory store into this one.""" + for bucket_name in ("schema_examples", "next_action_examples", "recovery_examples"): + current = getattr(self, bucket_name) + incoming = getattr(other, bucket_name) + for key, values in incoming.items(): + current.setdefault(key, []) + current[key].extend(values) + current[key] = current[key][-self.max_examples_per_bucket :] + + def save(self, path: Path) -> None: + """Persist memory to disk.""" + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text( + json.dumps( + { + "schema_examples": self._dump(self.schema_examples), + "next_action_examples": self._dump(self.next_action_examples), + "recovery_examples": self._dump(self.recovery_examples), + }, + indent=2, + ), + encoding="utf-8", + ) + + @classmethod + def load(cls, path: Path) -> "CorrectionMemory": + """Load memory from disk.""" + if not path.exists(): + return cls() + data = json.loads(path.read_text(encoding="utf-8")) + memory = cls() + memory.schema_examples = memory._load_bucket(data.get("schema_examples", {})) + memory.next_action_examples = memory._load_bucket(data.get("next_action_examples", {})) + memory.recovery_examples = memory._load_bucket(data.get("recovery_examples", {})) + return memory + + def _append(self, bucket: dict[str, list[MemoryExample]], key: str, example: MemoryExample) -> None: + bucket.setdefault(key, []).append(example) + bucket[key] = bucket[key][-self.max_examples_per_bucket :] + + def _select_examples( + self, + bucket: dict[str, list[MemoryExample]], + scenario_id: str, + stage: str, + *, + limit: int, + ) -> list[MemoryExample]: + all_examples = [ + item + for items in bucket.values() + for item in items + ] + ranked = sorted( + all_examples, + key=lambda item: self._score(item, scenario_id, stage), + reverse=True, + ) + selected: list[MemoryExample] = [] + seen = set() + for item in ranked: + key = ( + item.scenario_id, + item.stage, + item.failure_type, + json.dumps(item.corrected_action, sort_keys=True), + ) + if key in seen: + continue + seen.add(key) + selected.append(item) + if len(selected) >= limit: + break + return selected + + def _score(self, item: MemoryExample, scenario_id: str, stage: str) -> int: + score = 0 + if item.scenario_id == scenario_id: + score += 100 + if item.stage == stage: + score += 60 + if item.failure_type: + score += 10 + if item.action_family: + score += 5 + return score + + def _dump(self, bucket: dict[str, list[MemoryExample]]) -> dict: + return { + key: [ + { + "scenario_id": item.scenario_id, + "stage": item.stage, + "prompt_text": item.prompt_text, + "raw_output": item.raw_output, + "corrected_action": item.corrected_action, + "tags": item.tags, + "bucket": item.bucket, + "failure_type": item.failure_type, + "action_family": item.action_family, + "mistake": item.mistake, + "correction": item.correction, + } + for item in value + ] + for key, value in bucket.items() + } + + def _load_bucket(self, bucket: dict) -> dict[str, list[MemoryExample]]: + return { + key: [MemoryExample(**item) for item in value] + for key, value in bucket.items() + } + + +def _schema_failure_type(parse_status: str) -> str: + if parse_status == "invalid_json": + return "invalid_json" + return "invalid_action" + + +def _action_family(action: dict | None) -> str | None: + action_type = (action or {}).get("action_type") + if action_type in {"query_logs", "query_metrics", "query_dependencies"}: + return "investigate" + if action_type in { + "inspect_code", + "classify_vulnerability", + "apply_patch", + "verify_security_fix", + "submit_security_fix", + }: + return "security" + if action_type in {"restart_service", "rollback_deploy"}: + return "recovery" + if action_type == "submit_postmortem": + return "postmortem" + return None + + +def _mistake_text(step, entry) -> str: + if step.failure_type and step.observation.get("why_failed"): + return step.observation["why_failed"] + if entry is not None: + return entry.detail + return "The previous action did not follow the stage rules." + + +def _correction_text(step) -> str: + teacher_action = step.teacher_action or {} + action_type = teacher_action.get("action_type") + if action_type is None: + return "Return one valid action object for the current stage." + if action_type in {"query_logs", "query_metrics", "query_dependencies"}: + return f"If you are still diagnosing, prefer {action_type} instead of recovery actions." + if action_type in { + "inspect_code", + "classify_vulnerability", + "apply_patch", + "verify_security_fix", + "submit_security_fix", + }: + return f"If security is not completed, prefer {action_type} before infrastructure recovery." + if action_type in {"restart_service", "rollback_deploy"}: + return f"After security completion, use {action_type} for recovery." + return f"The next valid move is {action_type}." + + +def _limit_words(text: str, limit: int) -> str: + words = text.split() + if len(words) <= limit: + return text + return " ".join(words[:limit]).strip() + " ..." diff --git a/unified_incident_env/trainer/trajectory_store.py b/unified_incident_env/trainer/trajectory_store.py new file mode 100644 index 0000000000000000000000000000000000000000..e252d81806d5fab712903327a7501064a1fa8bbd --- /dev/null +++ b/unified_incident_env/trainer/trajectory_store.py @@ -0,0 +1,33 @@ +"""JSONL trajectory storage and summary helpers.""" + +from __future__ import annotations + +import json +from pathlib import Path + +from .types import EpisodeRecord + + +class TrajectoryStore: + """Append-only JSONL store for episode trajectories.""" + + def __init__(self, path: Path) -> None: + self.path = path + self.path.parent.mkdir(parents=True, exist_ok=True) + + def append_episode(self, record: EpisodeRecord) -> None: + with self.path.open("a", encoding="utf-8") as handle: + handle.write(record.model_dump_json()) + handle.write("\n") + + def load_episodes(self) -> list[EpisodeRecord]: + if not self.path.exists(): + return [] + records: list[EpisodeRecord] = [] + with self.path.open(encoding="utf-8") as handle: + for line in handle: + line = line.strip() + if not line: + continue + records.append(EpisodeRecord.model_validate(json.loads(line))) + return records diff --git a/unified_incident_env/trainer/types.py b/unified_incident_env/trainer/types.py new file mode 100644 index 0000000000000000000000000000000000000000..4ded4947dd0ef56f097095684e37b61d5f5bbedf --- /dev/null +++ b/unified_incident_env/trainer/types.py @@ -0,0 +1,336 @@ +"""Typed records for trainer requests, trajectories, datasets, and sessions.""" + +from __future__ import annotations + +from typing import Any, Literal + +from pydantic import BaseModel, ConfigDict, Field + +FailureBucketName = Literal["schema", "policy", "looping", "reasoning"] +ParseStatus = Literal["ok", "repaired", "teacher_override", "invalid_json", "invalid_action"] +UpdateBackendName = Literal["noop", "external_command", "openai_finetune"] +UpdateStatus = Literal["completed", "noop", "failed"] +SessionPhaseName = Literal["probe", "first_correction", "workflow_correction", "final_evaluation"] +TrainerMode = Literal["strict", "lenient"] +RuntimeMode = Literal["competition", "research"] +StructuredMode = Literal[ + "plain_json", + "response_format_json", + "tool_calling", + "backend_adaptive", +] +SFTSource = Literal[ + "baseline", + "replay", + "schema_repair", + "next_action", + "recovery", +] + + +class ModelRequest(BaseModel): + """One backend completion request.""" + + model_config = ConfigDict(extra="forbid") + + model_name: str + system_prompt: str + user_prompt: str + structured_mode: StructuredMode = "backend_adaptive" + response_format: dict[str, Any] | None = None + tools: list[dict[str, Any]] | None = None + tool_choice: dict[str, Any] | None = None + temperature: float = 0.0 + max_tokens: int = 220 + + +class ModelResponse(BaseModel): + """One backend completion response.""" + + model_config = ConfigDict(extra="forbid") + + raw_text: str + latency_s: float + metadata: dict[str, Any] = Field(default_factory=dict) + + +class ParseResult(BaseModel): + """Result of converting raw model text to an action.""" + + model_config = ConfigDict(extra="forbid") + + parse_status: ParseStatus + cleaned_action: dict[str, Any] | None = None + error: str | None = None + repair_labels: list[str] = Field(default_factory=list) + + +class StepRecord(BaseModel): + """One recorded environment step or parse failure.""" + + model_config = ConfigDict(extra="forbid") + + episode_id: int | None = None + scenario_id: str | None = None + step_index: int + tick: int + workflow_stage: str + observation: dict[str, Any] = Field(default_factory=dict) + prompt_text: str + raw_model_output: str + parse_status: ParseStatus + normalization_applied: list[str] = Field(default_factory=list) + cleaned_action: dict[str, Any] | None = None + teacher_action: dict[str, Any] | None = None + reward: float | None = None + cumulative_score: float | None = None + final_score_after_step: float | None = None + done: bool = False + next_prompt_text: str | None = None + structured_mode_used: StructuredMode = "plain_json" + repair_retry_used: bool = False + teacher_override_used: bool = False + failure_reason: str | None = None + failure_type: str | None = None + + +class FailureBucketEntry(BaseModel): + """One classified failure instance.""" + + model_config = ConfigDict(extra="forbid") + + episode_id: int + scenario_id: str + step_index: int | None = None + bucket: FailureBucketName + failure_type: str + detail: str + + +class FailureAnalysisReport(BaseModel): + """Failure analysis for one episode or one block.""" + + model_config = ConfigDict(extra="forbid") + + episode_ids: list[int] = Field(default_factory=list) + scenario_ids: list[str] = Field(default_factory=list) + entries: list[FailureBucketEntry] = Field(default_factory=list) + schema_failures: list[str] = Field(default_factory=list) + policy_failures: list[str] = Field(default_factory=list) + looping_failures: list[str] = Field(default_factory=list) + reasoning_failures: list[str] = Field(default_factory=list) + summary: dict[str, int] = Field(default_factory=dict) + + +class EpisodeSummaryRecord(BaseModel): + """Compact episode-level summary used in session reports.""" + + model_config = ConfigDict(extra="forbid") + + episode_id: int + run_id: str + scenario_id: str + difficulty: str + model_name: str + model_version: str | None = None + mode: TrainerMode + steps: int + success: bool + final_score: float + schema_failures: int = 0 + json_valid_steps: int = 0 + strict_schema_valid_steps: int = 0 + teacher_override_count: int = 0 + repair_retry_count: int = 0 + policy_failures: list[str] = Field(default_factory=list) + looping_failures: list[str] = Field(default_factory=list) + reasoning_failures: list[str] = Field(default_factory=list) + security_subquest_completed: bool = False + postmortem_completed: bool = False + stopped_reason: str | None = None + elapsed_s: float + + +class EpisodeRecord(BaseModel): + """One full episode trajectory.""" + + model_config = ConfigDict(extra="forbid") + + run_id: str + episode_id: int | None = None + scenario_id: str + difficulty: str + model_name: str + model_version: str | None = None + mode: TrainerMode + success: bool + final_score: float + steps: int + failure_reason: str | None = None + elapsed_s: float + schema_failures: int = 0 + json_valid_steps: int = 0 + strict_schema_valid_steps: int = 0 + teacher_override_count: int = 0 + repair_retry_count: int = 0 + policy_failures: list[str] = Field(default_factory=list) + looping_failures: list[str] = Field(default_factory=list) + reasoning_failures: list[str] = Field(default_factory=list) + security_subquest_completed: bool = False + postmortem_completed: bool = False + stopped_reason: str | None = None + step_records: list[StepRecord] = Field(default_factory=list) + + +class EvalScenarioResult(BaseModel): + """Summary for one model/scenario pair.""" + + model_config = ConfigDict(extra="forbid") + + model_name: str + scenario_id: str + success: bool + final_score: float + failure_reason: str | None = None + schema_failure: bool = False + elapsed_s: float + + +class EvalSummary(BaseModel): + """Aggregate evaluation report.""" + + model_config = ConfigDict(extra="forbid") + + mode: TrainerMode + results: list[EvalScenarioResult] = Field(default_factory=list) + success_rate: float = 0.0 + avg_score: float = 0.0 + schema_failure_rate: float = 0.0 + by_model: dict[str, dict[str, float]] = Field(default_factory=dict) + by_scenario: dict[str, dict[str, float]] = Field(default_factory=dict) + + +class SFTRecord(BaseModel): + """One supervised fine-tuning row.""" + + model_config = ConfigDict(extra="forbid") + + source: SFTSource + scenario_id: str + tick: int + messages: list[dict[str, str]] + target_action: dict[str, Any] + student_action: dict[str, Any] | None = None + parse_status: str | None = None + tags: list[str] = Field(default_factory=list) + metadata: dict[str, Any] = Field(default_factory=dict) + + +class UpdateRequest(BaseModel): + """One request to update a model between episode blocks.""" + + model_config = ConfigDict(extra="forbid") + + update_index: int + phase_name: SessionPhaseName + episodes_used: list[int] = Field(default_factory=list) + datasets_used: list[str] = Field(default_factory=list) + model_before: str + output_dir: str + runtime_mode: RuntimeMode = "competition" + command_template: str | None = None + training_file: str | None = None + suffix: str | None = None + + +class UpdateResult(BaseModel): + """Result of one update stage.""" + + model_config = ConfigDict(extra="forbid") + + update_index: int + phase_name: SessionPhaseName + updater_backend: UpdateBackendName + model_before: str + model_after: str + status: UpdateStatus + episodes_used: list[int] = Field(default_factory=list) + datasets_used: list[str] = Field(default_factory=list) + artifact_paths: list[str] = Field(default_factory=list) + notes: str | None = None + + +class SessionPhaseConfig(BaseModel): + """One configured phase in the ten-episode schedule.""" + + model_config = ConfigDict(extra="forbid") + + phase_name: SessionPhaseName + episode_ids: list[int] + update_after: bool = False + update_index: int | None = None + + +class SessionConfig(BaseModel): + """Configuration for one ten-episode session.""" + + model_config = ConfigDict(extra="forbid") + + session_id: str + model_name: str + initial_model_version: str + runtime_mode: RuntimeMode = "competition" + collection_mode: TrainerMode = "lenient" + final_eval_mode: TrainerMode = "strict" + log_rendered_prompts: bool = False + base_url: str | None = None + api_base_url: str + api_key: str + output_root: str + scenario_schedule: list[str] + phases: list[SessionPhaseConfig] + updater_backend: UpdateBackendName = "noop" + updater_command_template: str | None = None + + +class SessionPhaseReport(BaseModel): + """Aggregate report for one phase.""" + + model_config = ConfigDict(extra="forbid") + + phase_name: SessionPhaseName + episode_ids: list[int] + avg_score: float + success_rate: float + schema_failures: int + loop_failures: int = 0 + updates_applied: list[int] = Field(default_factory=list) + + +class PhaseDeltaRecord(BaseModel): + """Delta from the previous phase for key session metrics.""" + + model_config = ConfigDict(extra="forbid") + + phase_name: SessionPhaseName + score_delta: float + schema_failure_delta: float + loop_failure_delta: float + success_delta: float + + +class SessionReport(BaseModel): + """Full ten-episode training/eval session report.""" + + model_config = ConfigDict(extra="forbid") + + session_id: str + model_name: str + runtime_mode: RuntimeMode + output_dir: str + episode_summaries: list[EpisodeSummaryRecord] = Field(default_factory=list) + updates: list[UpdateResult] = Field(default_factory=list) + phase_reports: list[SessionPhaseReport] = Field(default_factory=list) + phase_deltas: list[PhaseDeltaRecord] = Field(default_factory=list) + improvement_metrics: dict[str, float] = Field(default_factory=dict) + correction_memory_stats: dict[str, Any] = Field(default_factory=dict) diff --git a/unified_incident_env/trainer/update_model.py b/unified_incident_env/trainer/update_model.py new file mode 100644 index 0000000000000000000000000000000000000000..115a069ddf83de8ca47c98c60ac7b635e522410d --- /dev/null +++ b/unified_incident_env/trainer/update_model.py @@ -0,0 +1,267 @@ +"""Pluggable model updater backends for the session loop.""" + +from __future__ import annotations + +import argparse +import json +import shlex +import subprocess +import sys +import time +from pathlib import Path + +from openai import OpenAI + +from .types import UpdateRequest, UpdateResult + + +class NoOpUpdater: + """Updater that records an update request without changing weights.""" + + backend_name = "noop" + + def update(self, request: UpdateRequest) -> UpdateResult: + output_dir = Path(request.output_dir).resolve() + output_dir.mkdir(parents=True, exist_ok=True) + request_path = output_dir / f"update_{request.update_index:02d}_request.json" + result_path = output_dir / f"update_{request.update_index:02d}_result.json" + request_path.write_text(request.model_dump_json(indent=2), encoding="utf-8") + + result = UpdateResult( + update_index=request.update_index, + phase_name=request.phase_name, + updater_backend="noop", + model_before=request.model_before, + model_after=request.model_before, + status="noop", + episodes_used=request.episodes_used, + datasets_used=request.datasets_used, + artifact_paths=[str(request_path), str(result_path)], + notes="NoOpUpdater recorded the update request but did not change weights.", + ) + result_path.write_text(result.model_dump_json(indent=2), encoding="utf-8") + return result + + +class ExternalCommandUpdater: + """Updater that delegates weight updates to an external command.""" + + backend_name = "external_command" + + def __init__(self, command_template: str) -> None: + self.command_template = command_template + + def _command_for_current_python(self) -> str: + executable = shlex.quote(sys.executable) + if self.command_template.startswith("python "): + return executable + self.command_template[len("python") :] + if self.command_template.startswith("python3 "): + return executable + self.command_template[len("python3") :] + return self.command_template + + def update(self, request: UpdateRequest) -> UpdateResult: + output_dir = Path(request.output_dir).resolve() + output_dir.mkdir(parents=True, exist_ok=True) + request_path = output_dir / f"update_{request.update_index:02d}_request.json" + result_path = output_dir / f"update_{request.update_index:02d}_result.json" + request_path.write_text(request.model_dump_json(indent=2), encoding="utf-8") + + command = self._command_for_current_python().format( + output_dir=str(output_dir), + request_path=str(request_path), + model_before=request.model_before, + ) + completed = subprocess.run( + command, + shell=True, + cwd=output_dir, + capture_output=True, + text=True, + ) + external_result_path = output_dir / f"update_{request.update_index:02d}_external_result.json" + status = "completed" if completed.returncode == 0 else "failed" + model_after = str(output_dir) if completed.returncode == 0 else request.model_before + notes = ( + f"stdout={completed.stdout.strip()} stderr={completed.stderr.strip()}" + ).strip() + artifact_paths = [str(request_path), str(result_path)] + + if completed.returncode == 0 and external_result_path.exists(): + payload = json.loads(external_result_path.read_text(encoding="utf-8")) + status = payload.get("status", status) + model_after = payload.get("model_after", model_after) + notes = payload.get("notes", notes) + artifact_paths.extend(payload.get("artifact_paths", [])) + artifact_paths.append(str(external_result_path)) + elif completed.returncode == 0: + stdout_text = completed.stdout.strip() + if stdout_text.startswith("{") and stdout_text.endswith("}"): + try: + payload = json.loads(stdout_text) + status = payload.get("status", status) + model_after = payload.get("model_after", model_after) + notes = payload.get("notes", notes) + artifact_paths.extend(payload.get("artifact_paths", [])) + except Exception: + pass + + result = UpdateResult( + update_index=request.update_index, + phase_name=request.phase_name, + updater_backend="external_command", + model_before=request.model_before, + model_after=model_after if status == "completed" else request.model_before, + status=status, + episodes_used=request.episodes_used, + datasets_used=request.datasets_used, + artifact_paths=artifact_paths, + notes=notes, + ) + result_path.write_text(result.model_dump_json(indent=2), encoding="utf-8") + return result + + +class OpenAIFineTuneUpdater: + """Optional hosted fine-tune backend for research mode only.""" + + backend_name = "openai_finetune" + + def __init__( + self, + *, + base_url: str, + api_key: str, + client: OpenAI | None = None, + poll_interval_s: float = 5.0, + timeout_s: float = 1800.0, + ) -> None: + self.client = client or OpenAI( + api_key=api_key, + base_url=base_url, + timeout=120.0, + ) + self.poll_interval_s = poll_interval_s + self.timeout_s = timeout_s + + def update(self, request: UpdateRequest) -> UpdateResult: + output_dir = Path(request.output_dir) + output_dir.mkdir(parents=True, exist_ok=True) + request_path = output_dir / f"update_{request.update_index:02d}_request.json" + result_path = output_dir / f"update_{request.update_index:02d}_result.json" + request_path.write_text(request.model_dump_json(indent=2), encoding="utf-8") + + if not request.training_file: + result = UpdateResult( + update_index=request.update_index, + phase_name=request.phase_name, + updater_backend="openai_finetune", + model_before=request.model_before, + model_after=request.model_before, + status="failed", + episodes_used=request.episodes_used, + datasets_used=request.datasets_used, + artifact_paths=[str(request_path), str(result_path)], + notes="training_file is required for openai_finetune updater", + ) + result_path.write_text(result.model_dump_json(indent=2), encoding="utf-8") + return result + + training_path = Path(request.training_file) + training_file = None + try: + with training_path.open("rb") as handle: + training_file = self.client.files.create( + file=handle, + purpose="fine-tune", + ) + job = self.client.fine_tuning.jobs.create( + training_file=training_file.id, + model=request.model_before, + suffix=request.suffix, + ) + started = time.monotonic() + status = getattr(job, "status", "queued") + final_job = job + while status not in {"succeeded", "failed", "cancelled"}: + if time.monotonic() - started > self.timeout_s: + status = "failed" + break + time.sleep(self.poll_interval_s) + final_job = self.client.fine_tuning.jobs.retrieve(job.id) + status = getattr(final_job, "status", "queued") + + model_after = getattr(final_job, "fine_tuned_model", None) or request.model_before + result_status = "completed" if status == "succeeded" else "failed" + notes = f"job_id={job.id} status={status}" + except Exception as exc: + model_after = request.model_before + result_status = "failed" + notes = f"{type(exc).__name__}: {exc}" + + result = UpdateResult( + update_index=request.update_index, + phase_name=request.phase_name, + updater_backend="openai_finetune", + model_before=request.model_before, + model_after=model_after, + status=result_status, + episodes_used=request.episodes_used, + datasets_used=request.datasets_used, + artifact_paths=[str(request_path), str(result_path)], + notes=notes, + ) + result_path.write_text(result.model_dump_json(indent=2), encoding="utf-8") + return result + + +def build_updater( + backend_name: str, + *, + runtime_mode: str = "competition", + command_template: str | None = None, + openai_base_url: str | None = None, + api_key: str | None = None, +): + """Return the configured updater backend.""" + if backend_name == "noop": + return NoOpUpdater() + if backend_name == "external_command": + if not command_template: + command_template = ( + "python -m unified_incident_env.trainer.train_external " + "--request {request_path}" + ) + return ExternalCommandUpdater(command_template) + if backend_name == "openai_finetune": + if runtime_mode != "research": + raise ValueError("openai_finetune updater is allowed only in research mode") + if not openai_base_url or not api_key: + raise ValueError("openai_base_url and api_key are required for openai_finetune") + return OpenAIFineTuneUpdater(base_url=openai_base_url, api_key=api_key) + raise ValueError(f"Unknown updater backend {backend_name!r}") + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--request", required=True) + parser.add_argument("--backend", choices=["noop", "external_command", "openai_finetune"], default="noop") + parser.add_argument("--runtime-mode", choices=["competition", "research"], default="competition") + parser.add_argument("--command-template") + parser.add_argument("--openai-base-url") + parser.add_argument("--api-key") + args = parser.parse_args() + + request = UpdateRequest.model_validate_json(Path(args.request).read_text(encoding="utf-8")) + updater = build_updater( + args.backend, + runtime_mode=args.runtime_mode, + command_template=args.command_template, + openai_base_url=args.openai_base_url, + api_key=args.api_key, + ) + result = updater.update(request) + print(result.model_dump_json(indent=2)) + + +if __name__ == "__main__": + main()