{
  "openapi": "3.1.0",
  "info": {
    "title": "Diff-Anything API (ANY-A02)",
    "version": "1.0.0",
    "summary": "Structured Differential & Overcharge Recovery Engine across cloud billing, freight surcharges, contract redlines, tariffs/pricing, and OpenAPI/DOM.",
    "description": "Polymorphic deterministic diffing engine with exact Decimal USD arithmetic (<= $0.01 precision), reproducible counterexamples, SHA-256 proofs, cross-tenant 7d-30d verified caching, and strict $0 billing on errors, HTTP 422 validation failures, or zero-finding diffs. Zero prompt or payload resale.",
    "contact": {
      "name": "Diff-Anything Operations",
      "url": "https://diff.fastmesh.dev",
      "email": "ops@fastmesh.dev"
    }
  },
  "servers": [
    {
      "url": "https://diff.fastmesh.dev",
      "description": "Production HTTPS gateway"
    },
    {
      "url": "http://127.0.0.1:8802",
      "description": "Local development upstream"
    }
  ],
  "tags": [
    { "name": "Health", "description": "Service liveness and supported domain metadata" },
    { "name": "Diff", "description": "Polymorphic single and batch structured diff endpoints" },
    { "name": "Billing", "description": "Prepaid pricing catalog, hash-chained ledger balances, and Creem webhooks" },
    { "name": "MCP", "description": "Native JSON-RPC 2.0 Remote Model Context Protocol endpoint" }
  ],
  "paths": {
    "/health": {
      "get": {
        "tags": ["Health"],
        "operationId": "getHealth",
        "summary": "Check service liveness and supported domains",
        "responses": {
          "200": {
            "description": "Service is healthy",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" }
              }
            }
          }
        }
      }
    },
    "/api/v1/diff": {
      "post": {
        "tags": ["Diff"],
        "operationId": "runPolymorphicDiff",
        "summary": "Run deterministic 5-domain structured diff and overcharge recovery analysis",
        "description": "Compares baseline/contracted/v1 data against actual/revised/v2 data across cloud_billing, freight_surcharge, contract_redline, tariff_pricing, or openapi_dom (or auto-detects domain). Enforces strict $0 billing (billed_micros = 0) on HTTP 422, errors, verification failures, or zero-finding diffs.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/DiffRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Verified DiffProofReport (billed_micros == 0 when zero findings or zero delta)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/DiffProofReport" }
              }
            }
          },
          "422": {
            "description": "Malformed, empty, corrupt, or adversarial input rejected with $0 billing",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ValidationErrorReport" }
              }
            }
          }
        }
      }
    },
    "/api/v1/diff/batch": {
      "post": {
        "tags": ["Diff"],
        "operationId": "runBatchDiff",
        "summary": "Run batch CSV/JSON structured diff across multiple pairs or large tabular datasets",
        "description": "Processes multi-item or large CSV/JSON tables with exact Decimal USD aggregate totals and per-item SHA-256 proofs. Strictly bills $0 on error, 422, or empty findings.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BatchDiffRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Aggregated batch DiffProofReport",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BatchDiffProofReport" }
              }
            }
          },
          "422": {
            "description": "Invalid batch payload rejected with $0 billing",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ValidationErrorReport" }
              }
            }
          }
        }
      }
    },
    "/api/v1/pricing": {
      "get": {
        "tags": ["Billing"],
        "operationId": "getPricingCatalog",
        "summary": "Retrieve Creem prepaid credit packs, per-domain micro-pricing, and $0 guarantee",
        "responses": {
          "200": {
            "description": "Full transparent pricing catalog",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PricingCatalog" }
              }
            }
          }
        }
      }
    },
    "/api/v1/ledger/balance": {
      "get": {
        "tags": ["Billing"],
        "operationId": "getLedgerBalance",
        "summary": "Inspect prepaid account balance and hash-chain ledger head",
        "parameters": [
          {
            "name": "account_id",
            "in": "query",
            "required": false,
            "schema": { "type": "string", "default": "anonymous" },
            "description": "Account identifier to query in the append-only billing ledger"
          }
        ],
        "responses": {
          "200": {
            "description": "Current prepaid account balance and usage summary",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/LedgerBalanceResponse" }
              }
            }
          }
        }
      }
    },
    "/mcp": {
      "post": {
        "tags": ["MCP"],
        "operationId": "handleMcpJsonRpc",
        "summary": "Native JSON-RPC 2.0 Remote MCP endpoint",
        "description": "Supports MCP methods: initialize, tools/list, and tools/call for diff_anything, diff_batch, and diff_estimate_recovery.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/McpJsonRpcRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "JSON-RPC 2.0 response envelope",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/McpJsonRpcResponse" }
              }
            }
          }
        }
      }
    },
    "/webhooks/creem": {
      "post": {
        "tags": ["Billing"],
        "operationId": "handleCreemWebhook",
        "summary": "Creem prepaid settlement webhook with HMAC-SHA256 verification and event_id replay protection",
        "parameters": [
          {
            "name": "creem-signature",
            "in": "header",
            "required": true,
            "schema": { "type": "string" },
            "description": "Hex-encoded HMAC-SHA256 digest of the raw request body computed with CREEM_WEBHOOK_SECRET"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CreemWebhookEvent" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Webhook verified and credited or idempotently acknowledged",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CreemWebhookResponse" }
              }
            }
          },
          "400": {
            "description": "Malformed JSON or missing event_id",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/WebhookErrorResponse" }
              }
            }
          },
          "401": {
            "description": "Missing or invalid HMAC-SHA256 signature",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/WebhookErrorResponse" }
              }
            }
          },
          "503": {
            "description": "CREEM_WEBHOOK_SECRET environment variable is not configured",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/WebhookErrorResponse" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "HealthResponse": {
        "type": "object",
        "required": ["status", "service", "domain", "port", "domains"],
        "properties": {
          "status": { "type": "string", "const": "ok" },
          "service": { "type": "string", "example": "a02_diff_anything" },
          "domain": { "type": "string", "example": "diff.fastmesh.dev" },
          "port": { "type": "integer", "example": 8802 },
          "domains": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "cloud_billing",
                "freight_surcharge",
                "contract_redline",
                "tariff_pricing",
                "openapi_dom"
              ]
            }
          }
        }
      },
      "DiffRequest": {
        "type": "object",
        "required": ["baseline", "actual"],
        "properties": {
          "domain": {
            "type": "string",
            "enum": [
              "auto",
              "cloud_billing",
              "freight_surcharge",
              "contract_redline",
              "tariff_pricing",
              "openapi_dom"
            ],
            "default": "auto",
            "description": "Explicit diff domain or 'auto' for deterministic payload classification"
          },
          "baseline": {
            "description": "Contracted rate card, v1 contract text, baseline HS schedule, OpenAPI v1 spec, or baseline HTML DOM (CSV string, JSON object/array, or raw text)"
          },
          "actual": {
            "description": "Actual invoice CSV/JSON, carrier billing sheet, v2 revised contract text, updated HS tariff/pricing table, OpenAPI v2 spec, or revised HTML DOM"
          },
          "tenant_id": {
            "type": "string",
            "description": "Optional tenant or account identifier for billing and 10% same-tenant cache replay discount"
          },
          "no_cache": {
            "type": "boolean",
            "default": false,
            "description": "Bypass 7d-30d verified archive cache lookup when true"
          },
          "options": {
            "type": "object",
            "additionalProperties": true,
            "description": "Optional domain-specific tolerances, contracted discount overrides, or DIM divisor settings"
          }
        }
      },
      "BatchDiffRequest": {
        "type": "object",
        "properties": {
          "domain": {
            "type": "string",
            "enum": [
              "auto",
              "cloud_billing",
              "freight_surcharge",
              "contract_redline",
              "tariff_pricing",
              "openapi_dom"
            ],
            "default": "auto"
          },
          "baseline": {
            "description": "Shared baseline schedule or large CSV/JSON table"
          },
          "actual": {
            "description": "Actual large CSV/JSON table to audit against baseline"
          },
          "items": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/DiffRequest" },
            "description": "Optional list of individual diff jobs to execute in a single batch call"
          },
          "tenant_id": { "type": "string" },
          "options": { "type": "object", "additionalProperties": true }
        }
      },
      "LineItemFinding": {
        "type": "object",
        "required": [
          "id",
          "category",
          "item",
          "baseline_value",
          "actual_value",
          "delta_usd",
          "severity",
          "evidence",
          "claim_action"
        ],
        "properties": {
          "id": { "type": "string" },
          "category": { "type": "string" },
          "item": { "type": "string" },
          "baseline_value": {},
          "actual_value": {},
          "delta_usd": { "type": "number" },
          "severity": {
            "type": "string",
            "enum": ["none", "low", "medium", "high", "critical"]
          },
          "evidence": { "type": "string" },
          "claim_action": { "type": "string" }
        }
      },
      "CounterexampleProof": {
        "type": "object",
        "required": ["id", "formula", "input", "expected", "observed", "delta_usd"],
        "properties": {
          "id": { "type": "string" },
          "formula": { "type": "string" },
          "input": { "type": "object", "additionalProperties": true },
          "expected": {},
          "observed": {},
          "delta_usd": { "type": "number" }
        }
      },
      "VerificationEnvelope": {
        "type": "object",
        "required": ["engine", "anti_fake_200_passed", "status_code", "receipt_sha256"],
        "properties": {
          "engine": { "type": "string", "example": "steel_core_4.composite_verify" },
          "anti_fake_200_passed": { "type": "boolean" },
          "fake_200_reason": { "type": ["string", "null"] },
          "status_code": { "type": "integer" },
          "cache_status": { "type": "string" },
          "is_repeat_tenant": { "type": "boolean" },
          "receipt_sha256": { "type": "string" }
        }
      },
      "DiffProofReport": {
        "type": "object",
        "required": [
          "ok",
          "domain",
          "summary",
          "total_delta_usd",
          "recoverable_overcharge_usd",
          "severity",
          "line_item_findings",
          "reproducible_counterexamples",
          "sha256_proof",
          "cache_hit",
          "verification",
          "billed_micros"
        ],
        "properties": {
          "ok": { "type": "boolean" },
          "domain": { "type": "string" },
          "summary": { "type": "string" },
          "total_delta_usd": { "type": "number" },
          "recoverable_overcharge_usd": { "type": "number" },
          "severity": {
            "type": "string",
            "enum": ["none", "low", "medium", "high", "critical"]
          },
          "line_item_findings": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/LineItemFinding" }
          },
          "reproducible_counterexamples": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CounterexampleProof" }
          },
          "sha256_proof": { "type": "string", "minLength": 64, "maxLength": 64 },
          "cache_hit": { "type": "boolean" },
          "verification": { "$ref": "#/components/schemas/VerificationEnvelope" },
          "billed_micros": { "type": "integer", "minimum": 0 },
          "metadata": { "type": "object", "additionalProperties": true }
        }
      },
      "BatchDiffProofReport": {
        "allOf": [
          { "$ref": "#/components/schemas/DiffProofReport" },
          {
            "type": "object",
            "properties": {
              "item_count": { "type": "integer" },
              "results": {
                "type": "array",
                "items": { "$ref": "#/components/schemas/DiffProofReport" }
              }
            }
          }
        ]
      },
      "ValidationErrorReport": {
        "type": "object",
        "required": ["ok", "domain", "summary", "billed_micros", "error_code"],
        "properties": {
          "ok": { "type": "boolean", "const": false },
          "domain": { "type": "string" },
          "summary": { "type": "string" },
          "total_delta_usd": { "type": "number", "const": 0 },
          "recoverable_overcharge_usd": { "type": "number", "const": 0 },
          "severity": { "type": "string", "const": "none" },
          "line_item_findings": { "type": "array", "maxItems": 0 },
          "reproducible_counterexamples": { "type": "array", "maxItems": 0 },
          "sha256_proof": { "type": "string" },
          "cache_hit": { "type": "boolean", "const": false },
          "billed_micros": { "type": "integer", "const": 0 },
          "error_code": { "type": "string" },
          "error_detail": { "type": "string" }
        }
      },
      "CreemPack": {
        "type": "object",
        "required": ["pack_key", "sku_id", "name", "price_usd", "credits_micros", "billing_type"],
        "properties": {
          "pack_key": { "type": "string" },
          "sku_id": { "type": "string" },
          "creem_product_id": { "type": "string" },
          "name": { "type": "string" },
          "tagline": { "type": "string" },
          "price_usd": { "type": "number" },
          "price_cents": { "type": "integer" },
          "credits_micros": { "type": "integer" },
          "bonus_pct": { "type": "integer" },
          "estimated_diffs": { "type": "integer" },
          "billing_type": { "type": "string" },
          "checkout_url": { "type": "string", "format": "uri" },
          "highlight": { "type": "boolean" }
        }
      },
      "PricingCatalog": {
        "type": "object",
        "required": ["ok", "currency", "micros_per_usd", "zero_billing_guarantee", "creem_packs", "domain_pricing"],
        "properties": {
          "ok": { "type": "boolean" },
          "service": { "type": "string" },
          "domain": { "type": "string" },
          "currency": { "type": "string" },
          "micros_per_usd": { "type": "integer" },
          "zero_billing_guarantee": { "type": "object", "additionalProperties": true },
          "creem_packs": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/CreemPack" }
          },
          "creem_product_ids": { "type": "object", "additionalProperties": true },
          "domain_pricing": { "type": "object", "additionalProperties": true },
          "batch_pricing": { "type": "object", "additionalProperties": true }
        }
      },
      "LedgerBalanceResponse": {
        "type": "object",
        "required": ["ok", "account_id", "balance_micros", "balance_usd", "last_entry_hash"],
        "properties": {
          "ok": { "type": "boolean" },
          "account_id": { "type": "string" },
          "currency": { "type": "string" },
          "balance_micros": { "type": "integer" },
          "balance_usd": { "type": "number" },
          "total_credited_micros": { "type": "integer" },
          "total_debited_micros": { "type": "integer" },
          "verified_diff_count": { "type": "integer" },
          "zero_billed_count": { "type": "integer" },
          "last_entry_seq": { "type": "integer" },
          "last_entry_hash": { "type": "string" }
        }
      },
      "McpJsonRpcRequest": {
        "type": "object",
        "required": ["jsonrpc", "method"],
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer", "null"] },
          "method": {
            "type": "string",
            "enum": ["initialize", "tools/list", "tools/call"]
          },
          "params": { "type": "object", "additionalProperties": true }
        }
      },
      "McpJsonRpcResponse": {
        "type": "object",
        "required": ["jsonrpc", "id"],
        "properties": {
          "jsonrpc": { "type": "string", "const": "2.0" },
          "id": { "type": ["string", "integer", "null"] },
          "result": { "type": "object", "additionalProperties": true },
          "error": {
            "type": "object",
            "properties": {
              "code": { "type": "integer" },
              "message": { "type": "string" }
            }
          }
        }
      },
      "CreemWebhookEvent": {
        "type": "object",
        "required": ["event_id", "event_type"],
        "properties": {
          "event_id": { "type": "string", "description": "Unique Creem event identifier used for replay/idempotency protection" },
          "event_type": { "type": "string", "example": "checkout.completed" },
          "account_id": { "type": "string" },
          "sku_id": { "type": "string", "example": "diff_pro_99" },
          "amount_usd": { "type": "number", "example": 99.0 },
          "object": { "type": "object", "additionalProperties": true }
        }
      },
      "CreemWebhookResponse": {
        "type": "object",
        "required": ["ok", "status_code", "duplicate", "event_id", "balance_micros"],
        "properties": {
          "ok": { "type": "boolean" },
          "status_code": { "type": "integer" },
          "duplicate": { "type": "boolean" },
          "idempotent": { "type": "boolean" },
          "event_id": { "type": "string" },
          "event_type": { "type": "string" },
          "account_id": { "type": "string" },
          "credited_micros": { "type": "integer" },
          "delta_micros": { "type": "integer" },
          "balance_micros": { "type": "integer" },
          "balance_usd": { "type": "number" },
          "entry_hash": { "type": "string" }
        }
      },
      "WebhookErrorResponse": {
        "type": "object",
        "required": ["ok", "status_code", "error", "billed_micros"],
        "properties": {
          "ok": { "type": "boolean", "const": false },
          "status_code": { "type": "integer" },
          "error": { "type": "string" },
          "billed_micros": { "type": "integer", "const": 0 }
        }
      }
    }
  }
}
