{
  "name": "Incident Response Automation",
  "version": "1.0.0",
  "description": "Auto-triage alerts, analyze root cause with AI, and remediate known issues - demonstrates router control flow, trycatch error handling, and AI analysis",
  "input_schema": {
    "type": "object",
    "properties": {
      "alert_id": {
        "type": "string",
        "description": "Alert/incident ID"
      },
      "service": {
        "type": "string",
        "description": "Affected service name"
      },
      "severity": {
        "type": "string",
        "enum": ["low", "medium", "high", "critical"]
      },
      "summary": {
        "type": "string",
        "description": "Alert summary"
      },
      "triggered_at": {
        "type": "string",
        "format": "date-time"
      }
    },
    "required": ["alert_id", "service", "severity", "summary"]
  },
  "output_schema": {
    "type": "object",
    "properties": {
      "resolution": { "type": "object" },
      "analysis": { "type": "object" },
      "notifications": { "type": "object" }
    }
  },
  "stages": [
    {
      "id": "receive-alert",
      "name": "Parse Alert",
      "component_type": "json_transform",
      "config": {
        "data": "{{input}}",
        "jmespath_expression": "{ alert_id: alert_id, service: service, severity: severity, summary: summary, triggered_at: triggered_at }"
      },
      "depends_on": [],
      "position": { "x": 100, "y": 100 }
    },
    {
      "id": "fetch-logs",
      "name": "Fetch Recent Logs",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.LOGGING_API}}/query",
        "method": "POST",
        "headers": {
          "Authorization": "Bearer {{secrets.LOGGING_TOKEN}}"
        },
        "body": {
          "query": "service:{{input.service}} level:error",
          "from": "-30m",
          "limit": 100
        },
        "timeout": 15
      },
      "depends_on": ["receive-alert"],
      "position": { "x": 350, "y": 100 }
    },
    {
      "id": "fetch-metrics",
      "name": "Fetch Metrics",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.METRICS_API}}/query_range",
        "method": "POST",
        "body": {
          "query": "up{service=\"{{input.service}}\"}",
          "start": "-30m",
          "end": "now",
          "step": "60"
        },
        "timeout": 10
      },
      "depends_on": ["receive-alert"],
      "position": { "x": 350, "y": 200 }
    },
    {
      "id": "fetch-recent-deploys",
      "name": "Fetch Recent Deploys",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.DEPLOY_API}}/deployments",
        "method": "GET",
        "params": {
          "service": "{{input.service}}",
          "since": "-24h",
          "limit": "5"
        },
        "timeout": 10
      },
      "depends_on": ["receive-alert"],
      "position": { "x": 350, "y": 300 }
    },
    {
      "id": "analyze-incident",
      "name": "AI Root Cause Analysis",
      "component_type": "generator",
      "config": {
        "model": "gpt-4",
        "temperature": 0.2,
        "system_prompt": "You are an expert SRE performing incident triage. Analyze the data and determine:\n1. Root cause (be specific)\n2. Severity (critical/high/medium/low)\n3. Is this a known issue pattern? (OOM, connection pool, rate limit, etc.)\n4. Recommended immediate action\n5. Confidence level (high/medium/low)\n\nFormat as JSON with keys: root_cause, severity, issue_type, recommended_action, confidence",
        "prompt": "Incident triage for {{input.service}}:\n\nAlert: {{input.summary}}\nSeverity: {{input.severity}}\n\nRecent Logs:\n{{upstream.fetch-logs.body}}\n\nMetrics:\n{{upstream.fetch-metrics.body}}\n\nRecent Deployments:\n{{upstream.fetch-recent-deploys.body}}"
      },
      "depends_on": ["fetch-logs", "fetch-metrics", "fetch-recent-deploys"],
      "position": { "x": 600, "y": 200 }
    },
    {
      "id": "determine-action",
      "name": "Route by Issue Type",
      "component_type": "router",
      "config": {
        "routes": [
          {
            "name": "oom-restart",
            "condition": "{{upstream.analyze-incident.result.issue_type == 'OOM' && upstream.analyze-incident.result.confidence == 'high'}}",
            "stages": ["auto-restart-service"]
          },
          {
            "name": "recent-deploy-rollback",
            "condition": "{{upstream.analyze-incident.result.issue_type == 'bad_deploy'}}",
            "stages": ["auto-rollback"]
          },
          {
            "name": "escalate",
            "condition": "true",
            "stages": ["escalate-to-human"]
          }
        ]
      },
      "depends_on": ["analyze-incident"],
      "position": { "x": 600, "y": 350 }
    },
    {
      "id": "auto-restart-service",
      "name": "Auto Restart Service",
      "component_type": "trycatch",
      "config": {
        "try_stages": ["restart-pods", "verify-health"],
        "catch_stages": ["escalate-restart-failed"],
        "finally_stages": ["log-restart-attempt"]
      },
      "depends_on": ["determine-action"],
      "position": { "x": 350, "y": 450 }
    },
    {
      "id": "restart-pods",
      "name": "Restart Service Pods",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.K8S_API}}/apis/apps/v1/namespaces/production/deployments/{{input.service}}",
        "method": "PATCH",
        "headers": {
          "Authorization": "Bearer {{secrets.K8S_TOKEN}}",
          "Content-Type": "application/strategic-merge-patch+json"
        },
        "body": {
          "spec": {
            "template": {
              "metadata": {
                "annotations": {
                  "kubectl.kubernetes.io/restartedAt": "{{now()}}"
                }
              }
            }
          }
        },
        "timeout": 30
      },
      "depends_on": ["auto-restart-service"],
      "position": { "x": 350, "y": 550 }
    },
    {
      "id": "verify-health",
      "name": "Verify Service Health",
      "component_type": "http_request",
      "config": {
        "url": "https://{{input.service}}.internal/health",
        "method": "GET",
        "timeout": 10
      },
      "depends_on": ["restart-pods"],
      "position": { "x": 350, "y": 650 }
    },
    {
      "id": "auto-rollback",
      "name": "Auto Rollback Deploy",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.K8S_API}}/apis/apps/v1/namespaces/production/deployments/{{input.service}}/rollback",
        "method": "POST",
        "headers": {
          "Authorization": "Bearer {{secrets.K8S_TOKEN}}"
        },
        "timeout": 60
      },
      "depends_on": ["determine-action"],
      "position": { "x": 600, "y": 450 }
    },
    {
      "id": "escalate-to-human",
      "name": "Escalate to Human",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.PAGERDUTY_EVENTS_URL}}",
        "method": "POST",
        "body": {
          "routing_key": "{{secrets.PAGERDUTY_KEY}}",
          "event_action": "trigger",
          "payload": {
            "summary": "[NEEDS HUMAN] {{input.summary}}",
            "severity": "{{input.severity}}",
            "source": "flowmason-incident-response",
            "custom_details": {
              "ai_analysis": "{{upstream.analyze-incident.result.root_cause}}",
              "recommended_action": "{{upstream.analyze-incident.result.recommended_action}}",
              "confidence": "{{upstream.analyze-incident.result.confidence}}",
              "why_not_auto": "Issue type not in auto-remediation playbook"
            }
          }
        }
      },
      "depends_on": ["determine-action"],
      "position": { "x": 850, "y": 450 }
    },
    {
      "id": "escalate-restart-failed",
      "name": "Escalate Restart Failed",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.PAGERDUTY_EVENTS_URL}}",
        "method": "POST",
        "body": {
          "routing_key": "{{secrets.PAGERDUTY_KEY}}",
          "event_action": "trigger",
          "payload": {
            "summary": "[AUTO-REMEDIATION FAILED] {{input.summary}}",
            "severity": "critical",
            "custom_details": {
              "attempted_action": "service_restart",
              "failure_reason": "Health check failed after restart"
            }
          }
        }
      },
      "depends_on": ["auto-restart-service"],
      "position": { "x": 100, "y": 550 }
    },
    {
      "id": "log-restart-attempt",
      "name": "Log Restart Attempt",
      "component_type": "logger",
      "config": {
        "message": "Auto-restart attempted for {{input.service}}",
        "level": "info",
        "data": {
          "service": "{{input.service}}",
          "alert_id": "{{input.alert_id}}",
          "action": "restart"
        }
      },
      "depends_on": ["auto-restart-service"],
      "position": { "x": 100, "y": 650 }
    },
    {
      "id": "update-status",
      "name": "Update Status Page",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.STATUSPAGE_API}}/incidents",
        "method": "POST",
        "headers": {
          "Authorization": "OAuth {{secrets.STATUSPAGE_TOKEN}}"
        },
        "body": {
          "incident": {
            "name": "{{input.service}} - {{upstream.analyze-incident.result.issue_type}}",
            "status": "investigating",
            "body": "Auto-remediation in progress. Root cause: {{upstream.analyze-incident.result.root_cause}}"
          }
        }
      },
      "depends_on": ["analyze-incident"],
      "position": { "x": 850, "y": 200 }
    },
    {
      "id": "notify-team",
      "name": "Notify Team",
      "component_type": "http_request",
      "config": {
        "url": "{{secrets.SLACK_WEBHOOK}}",
        "method": "POST",
        "body": {
          "blocks": [
            {
              "type": "header",
              "text": {
                "type": "plain_text",
                "text": "Incident Auto-Handled"
              }
            },
            {
              "type": "section",
              "text": {
                "type": "mrkdwn",
                "text": "*Service:* {{input.service}}\n*Issue:* {{upstream.analyze-incident.result.issue_type}}\n*Root Cause:* {{upstream.analyze-incident.result.root_cause}}"
              }
            }
          ]
        }
      },
      "depends_on": ["verify-health"],
      "position": { "x": 600, "y": 650 }
    },
    {
      "id": "create-postmortem",
      "name": "Generate Postmortem Draft",
      "component_type": "generator",
      "config": {
        "model": "gpt-4",
        "temperature": 0.3,
        "system_prompt": "Generate a concise incident postmortem draft with: Summary, Timeline, Root Cause, Resolution, Action Items. Be factual and specific.",
        "prompt": "Create postmortem for:\n\nService: {{input.service}}\nIncident: {{input.summary}}\nRoot Cause: {{upstream.analyze-incident.result.root_cause}}\nTriggered: {{input.triggered_at}}\nAction Taken: {{upstream.analyze-incident.result.recommended_action}}"
      },
      "depends_on": ["notify-team"],
      "position": { "x": 850, "y": 650 }
    }
  ],
  "output_stage_id": "analyze-incident"
}
