{
  "openapi": "3.1.0",
  "info": {
    "title": "Qerexa public decision API",
    "version": "1.0.0",
    "description": "Read-only public contract for the core decision and review-lookup path. This is a supported subset, not a complete inventory of tenant, provider, or internal routes. An API response is not a substitute for enforcement by the caller."
  },
  "servers": [
    {
      "url": "https://api.qerexa.com",
      "description": "Qerexa API; the tenant key determines its environment mode"
    }
  ],
  "tags": [
    {
      "name": "Decision flow",
      "description": "Evaluate a proposed action and look up a pending review."
    }
  ],
  "paths": {
    "/api/v1/gates/evaluate": {
      "post": {
        "tags": ["Decision flow"],
        "operationId": "evaluateAction",
        "summary": "Evaluate an action before it runs",
        "description": "Returns allowed, blocked, or approval_required. A 202 creates a durable review flow and never authorizes execution. The caller must enforce the response and the exact action binding before any downstream effect. The API accepts at most 1 MiB (1048576 bytes) of body data and allows five seconds to read it; these limits apply with or without an Idempotency-Key.",
        "security": [{ "BearerApiKey": [] }],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "Use one stable, unique value for retries of the same logical request. Do not reuse it for a changed action or target.",
            "schema": { "type": "string" }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/EvaluateRequest" },
              "example": {
                "action": "deploy:payments-service",
                "actor": "deployment-service",
                "reasoning": "Reviewed release change",
                "scope": { "target": "payments-service" }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Allowed for the submitted action, subject to response expiry and exact binding.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/GateDecision" } }
            }
          },
          "202": {
            "description": "Approval required. Stop execution and retain flow_id for review lookup.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/GateDecision" } }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": {
            "description": "Blocked by a control or forbidden for this caller. Never execute.",
            "content": {
              "application/json": { "schema": { "type": "object", "additionalProperties": true } }
            }
          },
          "408": { "$ref": "#/components/responses/RequestTimeout" },
          "413": { "$ref": "#/components/responses/BodyTooLarge" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/Unavailable" }
        }
      }
    },
    "/api/v1/trust-flows/{id}": {
      "get": {
        "tags": ["Decision flow"],
        "operationId": "getReviewFlow",
        "summary": "Get the current status of a review flow",
        "description": "Looks up a flow in the API key's tenant and mode. A status value alone is not an execution capability; the protected integration must enforce its action-specific authorization contract.",
        "security": [{ "HeaderApiKey": [] }],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "description": "The flow_id returned by an approval_required evaluation.",
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "Current flow state.",
            "content": {
              "application/json": { "schema": { "$ref": "#/components/schemas/ReviewFlow" } }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerApiKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "Tenant API key in Authorization: Bearer <key>. Use a key with gates:evaluate for review creation. Keep it server-side."
      },
      "HeaderApiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Tenant API key with flows:read (or the compatible keys:read scope) for flow lookup. Keep it server-side."
      }
    },
    "schemas": {
      "EvaluateRequest": {
        "type": "object",
        "description": "The canonical action shape covered by this public quickstart. Other structured intent shapes require the current signed-in customer documentation.",
        "required": ["action", "actor", "reasoning"],
        "properties": {
          "action": {
            "type": "string",
            "description": "Action name when no structured intent is supplied."
          },
          "actor": {
            "type": "string",
            "description": "Identifier of the actor requesting this action."
          },
          "reasoning": {
            "type": "string",
            "description": "Reason for the action. Avoid unnecessary sensitive data."
          },
          "scope": {
            "type": "object",
            "additionalProperties": true,
            "description": "Optional target and other contextual scope."
          }
        }
      },
      "GateDecision": {
        "type": "object",
        "required": [
          "decision",
          "allowed",
          "decision_id",
          "risk_score",
          "risk_band",
          "intent",
          "gates_checked",
          "proof_receipt",
          "expires_at"
        ],
        "properties": {
          "decision": { "type": "string", "enum": ["allowed", "blocked", "approval_required"] },
          "allowed": { "type": "boolean" },
          "decision_id": { "type": "string", "format": "uuid" },
          "reason": { "type": "string" },
          "flow_id": {
            "type": "string",
            "format": "uuid",
            "description": "Present when approval is required."
          },
          "review_deadline": { "type": "string", "format": "date-time" },
          "risk_score": { "type": "number" },
          "risk_band": { "type": "string", "enum": ["low", "medium", "high", "critical"] },
          "intent": { "type": "object", "additionalProperties": true },
          "gates_checked": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "gate": { "type": "string" },
                "status": { "type": "string", "enum": ["passed", "failed", "pending"] },
                "reason": { "type": "string" }
              }
            }
          },
          "proof_receipt": {
            "type": "object",
            "properties": {
              "status": { "type": "string", "enum": ["pending_approval", "not_applicable"] }
            }
          },
          "audit_receipt": {
            "type": "object",
            "properties": {
              "hash": { "type": "string" },
              "previous_hash": { "type": "string" },
              "recorded_at": { "type": "string", "format": "date-time" }
            }
          },
          "expires_at": { "type": "string", "format": "date-time" },
          "metadata": { "type": "object", "additionalProperties": true }
        }
      },
      "ReviewFlow": {
        "type": "object",
        "required": ["id", "status", "mode", "approvals"],
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "status": {
            "type": "string",
            "enum": ["pending", "approved", "rejected", "expired", "cancelled", "escalated"]
          },
          "action": { "type": ["string", "null"] },
          "mode": { "type": "string", "enum": ["live", "test", "demo"] },
          "min_approvals": { "type": "integer" },
          "max_approvals": { "type": ["integer", "null"] },
          "review_deadline": { "type": ["string", "null"], "format": "date-time" },
          "expires_at": { "type": ["string", "null"], "format": "date-time" },
          "approvals": {
            "type": "array",
            "items": { "type": "object", "additionalProperties": true }
          },
          "intent": { "type": ["object", "null"], "additionalProperties": true }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" },
          "message": { "type": "string" }
        }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Invalid request or intent.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "Unauthorized": {
        "description": "Missing, invalid, or revoked API key.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "Forbidden": {
        "description": "Insufficient scope or policy denial.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "NotFound": {
        "description": "No flow in this tenant and mode matches the id.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "RequestTimeout": {
        "description": "Request body did not finish within the API's five-second read deadline. No gate decision was created; do not execute the action.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "BodyTooLarge": {
        "description": "Request body exceeds 1 MiB, or its declared length is invalid. No gate decision was created; reduce the request before retrying.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "RateLimited": {
        "description": "Rate limit exceeded. Observe Retry-After when present.",
        "headers": {
          "Retry-After": {
            "description": "Seconds before retry is permitted.",
            "schema": { "type": "integer" }
          }
        },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      },
      "Unavailable": {
        "description": "Required evaluation is unavailable; do not execute.",
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } }
      }
    }
  }
}
