{
  "openapi": "3.1.0",
  "info": {
    "title": "Heal-Anything API",
    "version": "1.0.0",
    "summary": "Diagnostic and self-healing engine for fake-200 soft blocks, schema drift, DOM selector rot, 429/5xx circuit breakers, and malformed payloads with signed 24h SLA certificates.",
    "description": "Polymorphic anti-degradation and self-healing API across 5 fault classes: fake_200, schema_drift, selector_rot, circuit_breaker, and payload_sanitizer. Every heal and certificate issuance emits a SHA-256 attestation. Invalid/SSRF inputs (HTTP 422), errors, and empty results are strictly billed $0 (billed_micros = 0). Zero customer payload resale guarantee.",
    "contact": {
      "name": "Heal-Anything Operations",
      "email": "ops@fastmesh.dev",
      "url": "https://heal.fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://heal.fastmesh.dev",
      "description": "Public HTTPS Gateway Mount"
    },
    {
      "url": "http://127.0.0.1:8808",
      "description": "Local Runtime Server"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service liveness and capability probes" },
    { "name": "Diagnosis", "description": "Non-mutating fault diagnosis across 5 degradation classes" },
    { "name": "Healing", "description": "Diagnostic + executable healing patch generation and schema/payload restoration" },
    { "name": "SLA", "description": "Signed 24-hour SHA-256 SLA health certificate issuance and verification" },
    { "name": "MCP", "description": "Native flat JSON-RPC 2.0 Model Context Protocol endpoint" },
    { "name": "Billing", "description": "Creem HMAC-SHA256 prepaid credit webhook settlement" }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "tags": ["Health"],
        "summary": "Check Heal-Anything service health",
        "responses": {
          "200": {
            "description": "Service is healthy and ready",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" },
                "example": {
                  "status": "ok",
                  "service": "heal-anything",
                  "version": "1.0.0",
                  "public_host": "heal.fastmesh.dev",
                  "port": 8808,
                  "classes": [
                    "fake_200",
                    "schema_drift",
                    "selector_rot",
                    "circuit_breaker",
                    "payload_sanitizer"
                  ],
                  "fault_classes": [
                    "fake_200",
                    "schema_drift",
                    "selector_rot",
                    "circuit_breaker",
                    "payload_sanitizer"
                  ],
                  "pricing": {
                    "classes": {
                      "fake_200": 20000,
                      "schema_drift": 50000,
                      "selector_rot": 40000,
                      "circuit_breaker": 30000,
                      "payload_sanitizer": 30000
                    },
                    "diagnose_micros": 15000,
                    "sla_cert_micros": 20000,
                    "recheck_percent": 10,
                    "max_probe_cap_micros": 2000000
                  },
                  "cache_stats": {
                    "entries": 0,
                    "hits": 0
                  },
                  "ledger_enabled": true,
                  "mcp_enabled": true,
                  "timestamp_utc": "2026-12-01T20:15:00+00:00"
                }
              }
            }
          }
        }
      }
    },
    "/diagnose": {
      "post": {
        "operationId": "diagnoseFault",
        "tags": ["Diagnosis"],
        "summary": "Diagnose degradation across 5 fault classes without applying a patch",
        "description": "Identifies fake-200 soft blocks, JSON schema drift, HTML selector rot, 429/5xx circuit-breaker triggers, and malformed CSV/webhook encodings. Strictly bills $0 on HTTP 422 SSRF/invalid inputs, errors, or empty results.",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional tenant identifier for prepaid ledger accounting."
          },
          {
            "name": "X-API-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string" },
            "description": "Optional prepaid API key (omittable for Try-Now sandbox)."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/HealRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Diagnosis completed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealResponse" }
              }
            }
          },
          "422": {
            "description": "SSRF blocked or malformed input — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/heal": {
      "post": {
        "operationId": "healAnything",
        "tags": ["Healing"],
        "summary": "Diagnose degradation and emit an executable healing patch, healed output, and 24h SLA certificate",
        "description": "Generates deterministic executable patches across all 5 fault classes (remapping patches restoring 100% baseline schema, semantic DOM fallback selectors, exponential backoff + jitter breaker policies, and sanitized payloads) plus SHA-256 SLA certificate. Strictly bills $0 (`billed_micros = 0`) on 422 SSRF/invalid inputs, errors, or empty results.",
        "parameters": [
          {
            "name": "X-Tenant-Id",
            "in": "header",
            "required": false,
            "schema": { "type": "string" }
          },
          {
            "name": "X-API-Key",
            "in": "header",
            "required": false,
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/HealRequest" },
              "examples": {
                "fake_200_captcha": {
                  "summary": "Class 1: Fake-200 Soft-Block Diagnosis & Switchover Advice",
                  "value": {
                    "fault_class": "fake_200",
                    "target": "https://api.example.com/v1/feed",
                    "status_code": 200,
                    "headers": { "Content-Type": "text/html; charset=utf-8" },
                    "body": "<html><title>Just a moment...</title><div>Verify you are human - CAPTCHA</div></html>",
                    "expected_content_type": "application/json"
                  }
                },
                "schema_drift_remap": {
                  "summary": "Class 2: JSON Schema Drift Executable Remapping Patch",
                  "value": {
                    "fault_class": "schema_drift",
                    "target": "https://api.partner.example/v2/users",
                    "baseline_schema": {
                      "user_email": "string",
                      "account_id": "string",
                      "plan_tier": "string"
                    },
                    "payload": {
                      "contact": { "emailAddress": "ops@fastmesh.dev" },
                      "acctId": "acc_9042",
                      "tier": "pro"
                    }
                  }
                },
                "selector_rot_dom": {
                  "summary": "Class 3: HTML DOM Selector Rot Fallback Path",
                  "value": {
                    "fault_class": "selector_rot",
                    "target": "https://catalog.example.com/pricing",
                    "selector": "#legacy-price-v1 > span.cost",
                    "anchor_text": "$29/month",
                    "html": "<main><section data-testid='pricing-card'><h3>Pro Plan</h3><span class='amount' data-price='29'>$29/month</span></section></main>"
                  }
                },
                "circuit_breaker_policy": {
                  "summary": "Class 4: Upstream 429/5xx Circuit-Breaker & Backoff Policy",
                  "value": {
                    "fault_class": "circuit_breaker",
                    "target": "https://upstream.example.com/graphql",
                    "metrics": {
                      "status_429_rate": 0.18,
                      "status_5xx_rate": 0.07,
                      "p95_latency_ms": 3400,
                      "backup_providers": ["https://backup.example.com/graphql"]
                    }
                  }
                },
                "payload_sanitizer_csv": {
                  "summary": "Class 5: Dirty Encoding / Truncated CSV Sanitizer",
                  "value": {
                    "fault_class": "payload_sanitizer",
                    "target": "https://exports.example.com/daily.csv",
                    "body": "id,name,amount\r\n1,Alice,10.50\r\n2,\"Bob, Inc\",25.00\r\n3,\"Truncated"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Healing completed with executable patch and SLA certificate",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealResponse" }
              }
            }
          },
          "422": {
            "description": "SSRF blocked or invalid input — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" },
                "example": {
                  "ok": false,
                  "http_status": 422,
                  "receipt_id": "hr_0123456789abcdef",
                  "fault_class": "fake_200",
                  "target": "http://127.0.0.1:8080/internal-admin",
                  "verdict": "INVALID_INPUT",
                  "confidence": 0.0,
                  "error": "invalid_input_422",
                  "detail": "ssrf_blocked: private or loopback host '127.0.0.1' rejected",
                  "error_code": "invalid_input_422",
                  "error_detail": "ssrf_blocked: private or loopback host '127.0.0.1' rejected",
                  "billable_micros": 0,
                  "billed_micros": 0,
                  "base_price_micros": 20000,
                  "zero_bill_reason": "invalid_input_422",
                  "probe_cost_micros": 0
                }
              }
            }
          }
        }
      }
    },
    "/sla-cert": {
      "post": {
        "operationId": "issueSlaCert",
        "tags": ["SLA"],
        "summary": "Issue or verify a signed 24-hour SHA-256 SLA Health Certificate",
        "description": "Generates a deterministic SHA-256 attested 24h SLA certificate (or verifies an inline certificate when `sla_cert` is passed). Strictly $0 billed on 422/error/verification.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/SlaCertRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Signed 24h SLA Health Certificate envelope or verification status",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/SlaCertEnvelope" }
              }
            }
          },
          "422": {
            "description": "Invalid or SSRF target — strictly $0 billed",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/sla-cert/{cert_id}": {
      "get": {
        "operationId": "getSlaCert",
        "tags": ["SLA"],
        "summary": "Retrieve a stored SLA Health Certificate and verify its SHA-256 signature",
        "parameters": [
          {
            "name": "cert_id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Stored certificate with signature_valid flag",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "signature_valid": { "type": "boolean" },
                    "sla_cert": { "$ref": "#/components/schemas/SlaCertificate" }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Certificate not found",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ZeroBillErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/mcp": {
      "post": {
        "operationId": "mcpJsonRpc",
        "tags": ["MCP"],
        "summary": "Native flat JSON-RPC 2.0 MCP endpoint",
        "description": "Supports initialize, notifications/initialized, ping, tools/list, and tools/call for diagnose_fault, heal_anything, issue_sla_cert, and verify_sla_cert.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/McpJsonRpcRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC 2.0 response payload",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/McpJsonRpcResponse" }
              }
            }
          },
          "202": {
            "description": "Accepted notification (notifications/initialized without id)"
          }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "operationId": "creemWebhook",
        "tags": ["Billing"],
        "summary": "Creem HMAC-SHA256 payment webhook ingress",
        "description": "Verifies `creem-signature` HMAC-SHA256 using `CREEM_WEBHOOK_SECRET` from environment variables, deduplicates by `event_id`, and atomically settles prepaid healing credits into SQLite WAL + JSONL.",
        "parameters": [
          {
            "name": "creem-signature",
            "in": "header",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreemWebhookPayload" },
              "example": {
                "id": "evt_creem_heal_01",
                "eventType": "checkout.completed",
                "object": {
                  "product_id": "prod_heal_anything_growth_29",
                  "amount": 2900,
                  "currency": "USD",
                  "customer": { "email": "buyer@example.com" },
                  "metadata": { "tenant_id": "tenant_acme", "sku_id": "sku_heal_growth_29" }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Webhook verified and atomically settled (or deduplicated idempotently)"
          },
          "401": {
            "description": "Invalid or missing creem-signature HMAC-SHA256 digest"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "HealthResponse": {
        "type": "object",
        "required": ["status", "service"],
        "properties": {
          "status": { "type": "string", "enum": ["ok"] },
          "service": { "type": "string" },
          "version": { "type": "string" },
          "public_host": { "type": "string" },
          "port": { "type": "integer" },
          "classes": { "type": "array", "items": { "type": "string" } },
          "fault_classes": { "type": "array", "items": { "type": "string" } },
          "pricing": { "type": "object" },
          "cache_stats": { "type": "object" },
          "ledger_enabled": { "type": "boolean" },
          "mcp_enabled": { "type": "boolean" },
          "timestamp_utc": { "type": "string", "format": "date-time" }
        }
      },
      "HealRequest": {
        "type": "object",
        "properties": {
          "fault_class": {
            "type": "string",
            "enum": [
              "fake_200",
              "schema_drift",
              "selector_rot",
              "circuit_breaker",
              "payload_sanitizer"
            ]
          },
          "target": { "type": "string" },
          "url": { "type": "string" },
          "tenant_id": { "type": "string" },
          "status_code": { "type": "integer" },
          "headers": { "type": "object" },
          "body": { "type": "string" },
          "raw_payload": { "type": "string" },
          "csv_text": { "type": "string" },
          "expected_content_type": { "type": "string" },
          "required_keys": { "type": "array", "items": { "type": "string" } },
          "payload": { "type": ["object", "array"] },
          "drifted_payload": { "type": ["object", "array"] },
          "baseline_schema": { "type": "object" },
          "html": { "type": "string" },
          "selector": { "type": "string" },
          "broken_selector": { "type": "string" },
          "anchor_text": { "type": "string" },
          "metrics": { "type": "object" },
          "events": { "type": "array", "items": { "type": "object" } },
          "backup_providers": { "type": "array", "items": { "type": "string" } },
          "mode": { "type": "string" },
          "max_budget_micros": { "type": "integer", "minimum": 0 },
          "include_sla_cert": { "type": "boolean", "default": true },
          "options": { "type": "object" }
        }
      },
      "HealResponse": {
        "type": "object",
        "required": ["ok", "fault_class", "verdict", "billed_micros"],
        "properties": {
          "ok": { "type": "boolean" },
          "http_status": { "type": "integer" },
          "receipt_id": { "type": "string" },
          "service": { "type": "string" },
          "version": { "type": "string" },
          "public_host": { "type": "string" },
          "mode": { "type": "string" },
          "fault_class": { "type": "string" },
          "degradation_class": { "type": "string" },
          "target": { "type": "string" },
          "verdict": { "type": "string" },
          "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
          "diagnosis": { "type": "object" },
          "patch": { "type": ["object", "null"] },
          "healing_patch": { "type": ["object", "null"] },
          "healed_payload": {},
          "checks": { "type": "array", "items": { "type": "object" } },
          "cache_invalidation": { "type": "object" },
          "sla_cert": { "oneOf": [{ "$ref": "#/components/schemas/SlaCertificate" }, { "type": "null" }] },
          "sha256_attestation": { "type": "string" },
          "billable_micros": { "type": "integer", "minimum": 0 },
          "billed_micros": { "type": "integer", "minimum": 0 },
          "base_price_micros": { "type": "integer", "minimum": 0 },
          "zero_bill_reason": { "type": "string" },
          "probe_cost_micros": { "type": "integer", "minimum": 0 },
          "timestamp_utc": { "type": "string", "format": "date-time" }
        }
      },
      "SlaCertRequest": {
        "type": "object",
        "properties": {
          "target": { "type": "string" },
          "url": { "type": "string" },
          "fault_class": { "type": "string" },
          "tenant_id": { "type": "string" },
          "health_score": { "type": "number", "minimum": 0, "maximum": 100 },
          "availability_pct": { "type": "number", "minimum": 0, "maximum": 100 },
          "p95_latency_ms": { "type": "number", "minimum": 0 },
          "checks": { "type": "array", "items": { "type": "object" } },
          "diagnosis": { "type": "object" },
          "patch": { "type": "object" },
          "sla_cert": { "type": "object" },
          "valid_hours": { "type": "integer", "default": 24 },
          "ttl_hours": { "type": "integer", "default": 24 },
          "notes": { "type": "string" }
        }
      },
      "SlaCertEnvelope": {
        "type": "object",
        "required": ["ok", "cert_id", "signature_valid", "sla_cert", "billed_micros"],
        "properties": {
          "ok": { "type": "boolean" },
          "http_status": { "type": "integer" },
          "receipt_id": { "type": "string" },
          "cert_id": { "type": "string" },
          "fault_class": { "type": "string" },
          "target": { "type": "string" },
          "verdict": { "type": "string" },
          "signature_valid": { "type": "boolean" },
          "sha256_attestation": { "type": "string" },
          "sla_cert": { "$ref": "#/components/schemas/SlaCertificate" },
          "billable_micros": { "type": "integer", "minimum": 0 },
          "billed_micros": { "type": "integer", "minimum": 0 },
          "zero_bill_reason": { "type": "string" },
          "probe_cost_micros": { "type": "integer", "minimum": 0 }
        }
      },
      "SlaCertificate": {
        "type": "object",
        "required": ["cert_id", "target", "issued_at_utc", "expires_at_utc", "cert_sha256"],
        "properties": {
          "cert_id": { "type": "string" },
          "service": { "type": "string" },
          "target": { "type": "string" },
          "degradation_class": { "type": "string" },
          "fault_class": { "type": "string" },
          "sla_status": { "type": "string", "enum": ["PASS", "DEGRADED", "FAIL"] },
          "health_score": { "type": "number" },
          "availability_pct": { "type": "number" },
          "p95_latency_ms": { "type": "number" },
          "checks_passed": { "type": "integer" },
          "checks_total": { "type": "integer" },
          "checks": { "type": "array", "items": { "type": "object" } },
          "notes": { "type": "string" },
          "issued_at_utc": { "type": "string", "format": "date-time" },
          "expires_at_utc": { "type": "string", "format": "date-time" },
          "valid_hours": { "type": "integer" },
          "payload_sha256": { "type": "string" },
          "cert_sha256": { "type": "string" },
          "sha256_attestation": { "type": "string" }
        }
      },
      "ZeroBillErrorResponse": {
        "type": "object",
        "required": ["ok", "error_code", "billed_micros"],
        "properties": {
          "ok": { "type": "boolean", "enum": [false] },
          "http_status": { "type": "integer" },
          "receipt_id": { "type": "string" },
          "fault_class": { "type": "string" },
          "target": { "type": "string" },
          "verdict": { "type": "string" },
          "confidence": { "type": "number" },
          "error": { "type": "string" },
          "detail": { "type": "string" },
          "error_code": { "type": "string" },
          "error_detail": { "type": "string" },
          "billable_micros": { "type": "integer", "enum": [0] },
          "billed_micros": { "type": "integer", "enum": [0] },
          "base_price_micros": { "type": "integer" },
          "zero_bill_reason": { "type": "string" },
          "probe_cost_micros": { "type": "integer" },
          "sha256_attestation": { "type": "string" },
          "timestamp_utc": { "type": "string", "format": "date-time" }
        }
      },
      "McpJsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "method"],
        "properties": {
          "jsonrpc": { "type": "string", "enum": ["2.0"] },
          "id": { "type": ["string", "integer", "null"] },
          "method": { "type": "string" },
          "params": { "type": "object" }
        }
      },
      "McpJsonRpcResponse": {
        "type": "object",
        "required": ["jsonrpc"],
        "properties": {
          "jsonrpc": { "type": "string", "enum": ["2.0"] },
          "id": { "type": ["string", "integer", "null"] },
          "result": { "type": "object" },
          "error": { "type": "object" }
        }
      },
      "CreemWebhookPayload": {
        "type": "object",
        "required": ["id", "eventType", "object"],
        "properties": {
          "id": { "type": "string" },
          "eventType": { "type": "string" },
          "object": { "type": "object" }
        }
      }
    }
  }
}
