{
  "openapi": "3.1.0",
  "info": {
    "title": "score-centering hosted API",
    "version": "1.0.0",
    "description": "Hosted training observability for policy gradients. Runs expose only five scalar aggregate metrics; tensors, logprobs, token IDs, prompts, and completions are never collected or returned. All /v1 endpoints, including billing, authenticate with a bearer API token. The /app/billing/* browser forms use a session cookie, an exact-match Origin header, and an owner role, then redirect to Stripe. The Stripe webhook is provider-only and signature-authenticated."
  },
  "servers": [
    {
      "url": "https://scorecentering.com"
    },
    {
      "url": "https://www.scorecentering.com"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "tags": [
    {
      "name": "Runs",
      "description": "Create, list, and inspect training runs."
    },
    {
      "name": "Tokens",
      "description": "Issue API tokens for an organization."
    },
    {
      "name": "Billing",
      "description": "Stripe-hosted Checkout and Customer Portal sessions."
    },
    {
      "name": "Provider",
      "description": "Provider-only callbacks, not for API consumers."
    },
    {
      "name": "Web",
      "description": "Session-authenticated browser forms that redirect to Stripe-hosted pages."
    }
  ],
  "paths": {
    "/v1/runs": {
      "post": {
        "tags": [
          "Runs"
        ],
        "summary": "Create a run",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateRunRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Run created.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string",
                      "format": "uuid",
                      "examples": [
                        "00000000-0000-4000-8000-000000000001"
                      ]
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "402": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          }
        }
      },
      "get": {
        "tags": [
          "Runs"
        ],
        "summary": "List runs in the authenticated organization",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Runs owned by the organization.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "runs": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Run"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/tokens": {
      "post": {
        "tags": [
          "Tokens"
        ],
        "summary": "Create an API token",
        "description": "The plaintext token is returned once; store it securely.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateTokenRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Token created.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Token"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "402": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/ingest": {
      "post": {
        "tags": [
          "Runs"
        ],
        "summary": "Ingest scalar metrics for a run",
        "description": "Accepts 1..=1024 finite scalar samples per batch. No tensors, logprobs, token IDs, prompts, or completions are accepted.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/IngestRequest"
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Samples accepted."
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "402": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/runs/{id}/summary": {
      "get": {
        "tags": [
          "Runs"
        ],
        "summary": "Read a run summary",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/RunId"
          }
        ],
        "responses": {
          "200": {
            "description": "Run metadata, latest sample, series, and alerts.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/RunSummary"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/runs/{id}/metrics": {
      "get": {
        "tags": [
          "Runs"
        ],
        "summary": "Read a run's scalar metric series",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/RunId"
          },
          {
            "name": "from",
            "in": "query",
            "required": false,
            "description": "Inclusive lower bound on step.",
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          },
          {
            "name": "to",
            "in": "query",
            "required": false,
            "description": "Inclusive upper bound on step.",
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "description": "Maximum number of samples returned. Defaults to 1000 and is clamped to 1..=10000.",
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Scalar metric samples in step order.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "samples"
                  ],
                  "properties": {
                    "samples": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/IngestSample"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "404": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/billing/checkout": {
      "post": {
        "tags": [
          "Billing"
        ],
        "summary": "Create a Stripe-hosted Checkout session",
        "description": "Bearer-authenticated. Tier and cycle are accepted; Stripe identifiers always come from server configuration.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CheckoutRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Checkout URL.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HostedUrl"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "422": {
            "$ref": "#/components/responses/Error"
          },
          "502": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/billing/portal": {
      "post": {
        "tags": [
          "Billing"
        ],
        "summary": "Create a Stripe-hosted Customer Portal session",
        "description": "Bearer-authenticated. Requires a completed Checkout; no Stripe identifiers are accepted from the client.",
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Portal URL.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HostedUrl"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "409": {
            "$ref": "#/components/responses/Error"
          },
          "502": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/v1/stripe/webhook": {
      "post": {
        "tags": [
          "Provider"
        ],
        "summary": "Stripe provider webhook",
        "description": "Provider-only callback sent by Stripe to update subscription state. This endpoint is signature-authenticated with the Stripe-Signature header; it does not accept bearer tokens or session cookies and is not for API consumers. The production path is configured by STRIPE_WEBHOOK_PATH (currently /v1/stripe/webhook).",
        "security": [
          {
            "stripeSignature": []
          }
        ],
        "parameters": [
          {
            "name": "stripe-signature",
            "in": "header",
            "required": true,
            "description": "Stripe signature, verified server-side before the event is trusted.",
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "description": "Stripe event envelope; verified by signature, never by a client token.",
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Event acknowledged."
          },
          "400": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          },
          "502": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/app/billing/checkout": {
      "post": {
        "tags": [
          "Web"
        ],
        "summary": "Start Stripe Checkout from the browser workspace",
        "description": "Session-authenticated HTML form requiring an exact-match Origin header and an owner role. Redirects (303) to the Stripe-hosted Checkout URL. An unauthenticated session receives a 303 redirect to /login; invalid form values or a missing billing account return a 200 HTML error view.",
        "security": [
          {
            "sessionCookie": []
          }
        ],
        "parameters": [
          {
            "name": "Origin",
            "in": "header",
            "required": true,
            "description": "Must exactly match PUBLIC_BASE_URL; cross-origin form posts are rejected.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "org",
            "in": "query",
            "required": false,
            "description": "Optional organization id when the account belongs to more than one org.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "$ref": "#/components/schemas/BrowserCheckoutRequest"
              }
            }
          }
        },
        "responses": {
          "303": {
            "description": "Redirect to Stripe-hosted Checkout, or to /login when signed out."
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          },
          "502": {
            "$ref": "#/components/responses/Error"
          },
          "200": {
            "description": "HTML error view (for example an invalid tier/cycle or a missing billing account).",
            "content": {
              "text/html": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },
    "/app/billing/portal": {
      "post": {
        "tags": [
          "Web"
        ],
        "summary": "Open the Stripe Customer Portal from the browser workspace",
        "description": "Session-authenticated HTML form requiring an exact-match Origin header and an owner role. Redirects (303) to the Stripe-hosted Customer Portal. An unauthenticated session receives a 303 redirect to /login; invalid form values or a missing billing account return a 200 HTML error view.",
        "security": [
          {
            "sessionCookie": []
          }
        ],
        "parameters": [
          {
            "name": "Origin",
            "in": "header",
            "required": true,
            "description": "Must exactly match PUBLIC_BASE_URL; cross-origin form posts are rejected.",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "org",
            "in": "query",
            "required": false,
            "description": "Optional organization id when the account belongs to more than one org.",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          }
        ],
        "responses": {
          "303": {
            "description": "Redirect to Stripe-hosted Customer Portal, or to /login when signed out."
          },
          "403": {
            "$ref": "#/components/responses/Error"
          },
          "500": {
            "$ref": "#/components/responses/Error"
          },
          "502": {
            "$ref": "#/components/responses/Error"
          },
          "200": {
            "description": "HTML error view (for example an invalid tier/cycle or a missing billing account).",
            "content": {
              "text/html": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "API token for every /v1 route, including billing, supplied as `Authorization: Bearer <token>`."
      },
      "sessionCookie": {
        "type": "apiKey",
        "in": "cookie",
        "name": "sc_session",
        "description": "Browser session cookie for /app/* forms; bearer tokens are not accepted there."
      },
      "stripeSignature": {
        "type": "apiKey",
        "in": "header",
        "name": "stripe-signature",
        "description": "Provider-only Stripe signature. Not a bearer token and not usable by API consumers."
      }
    },
    "parameters": {
      "RunId": {
        "name": "id",
        "in": "path",
        "required": true,
        "description": "Run identifier (placeholder value shown).",
        "schema": {
          "type": "string",
          "format": "uuid",
          "examples": [
            "00000000-0000-4000-8000-000000000001"
          ]
        }
      }
    },
    "responses": {
      "Error": {
        "description": "Client-safe error: code plus optional detail, never provider internals.",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code"
            ],
            "properties": {
              "code": {
                "type": "string",
                "enum": [
                  "unauthorized",
                  "forbidden",
                  "not_found",
                  "invalid_payload",
                  "payment_required",
                  "billing_unavailable",
                  "no_billing_account",
                  "invalid_signature",
                  "internal"
                ]
              },
              "detail": {
                "type": "string",
                "description": "Fixed, client-safe explanation; omitted when not applicable."
              }
            }
          }
        }
      },
      "CreateRunRequest": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200,
            "examples": [
              "example-run"
            ],
            "description": "Whitespace is trimmed before validation; must be 1..=200 bytes after trimming."
          },
          "config_json": {
            "type": [
              "string",
              "null"
            ],
            "description": "Opaque JSON object serialized as a string; never contains model outputs."
          },
          "tags_json": {
            "type": [
              "string",
              "null"
            ],
            "description": "Opaque JSON tags serialized as a string."
          }
        }
      },
      "CreateTokenRequest": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200,
            "examples": [
              "ci-token"
            ],
            "description": "Whitespace is trimmed before validation; must be 1..=200 bytes after trimming."
          }
        }
      },
      "IngestRequest": {
        "type": "object",
        "required": [
          "run_id",
          "samples"
        ],
        "properties": {
          "run_id": {
            "type": "string",
            "format": "uuid",
            "examples": [
              "00000000-0000-4000-8000-000000000001"
            ]
          },
          "samples": {
            "type": "array",
            "minItems": 1,
            "maxItems": 1024,
            "items": {
              "$ref": "#/components/schemas/IngestSample"
            }
          }
        }
      },
      "IngestSample": {
        "type": "object",
        "required": [
          "step",
          "mean_q_head_mass",
          "mean_p_head_mass",
          "mean_rho",
          "mean_correction",
          "epsilon_floor_rate"
        ],
        "description": "Five finite scalar aggregates at a training step. No tensors, logprobs, token IDs, prompts, or completions.",
        "properties": {
          "step": {
            "type": "integer",
            "format": "int64",
            "examples": [
              100
            ]
          },
          "mean_q_head_mass": {
            "type": "number",
            "format": "float",
            "examples": [
              0.9
            ]
          },
          "mean_p_head_mass": {
            "type": "number",
            "format": "float",
            "examples": [
              0.8
            ]
          },
          "mean_rho": {
            "type": "number",
            "format": "float",
            "examples": [
              1.0
            ]
          },
          "mean_correction": {
            "type": "number",
            "format": "float",
            "examples": [
              0.01
            ]
          },
          "epsilon_floor_rate": {
            "type": "number",
            "format": "float",
            "examples": [
              0.0
            ]
          }
        }
      },
      "Run": {
        "type": "object",
        "description": "Tenant-scoped run metadata.",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "org_id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "config_json": {
            "type": [
              "string",
              "null"
            ]
          },
          "tags_json": {
            "type": [
              "string",
              "null"
            ]
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "Alert": {
        "type": "object",
        "description": "Alert raised from the scalar metric series.",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "run_id": {
            "type": "string",
            "format": "uuid"
          },
          "step": {
            "type": "integer",
            "format": "int64"
          },
          "kind": {
            "type": "string",
            "enum": [
              "high_epsilon_floor",
              "rho_drift",
              "correction_spike"
            ]
          },
          "message": {
            "type": "string"
          },
          "created_at": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "RunSummary": {
        "type": "object",
        "description": "Run metadata, optional latest sample, bounded series, and alerts.",
        "properties": {
          "run": {
            "$ref": "#/components/schemas/Run"
          },
          "latest": {
            "anyOf": [
              {
                "$ref": "#/components/schemas/IngestSample"
              },
              {
                "type": "null"
              }
            ]
          },
          "series": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/IngestSample"
            }
          },
          "alerts": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Alert"
            }
          }
        }
      },
      "Token": {
        "type": "object",
        "description": "The plaintext token appears only in the creation response.",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "token": {
            "type": "string",
            "examples": [
              "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
            ]
          }
        }
      },
      "CheckoutRequest": {
        "type": "object",
        "required": [
          "tier",
          "cycle"
        ],
        "properties": {
          "tier": {
            "type": "string",
            "enum": [
              "starter",
              "pro",
              "team"
            ]
          },
          "cycle": {
            "type": "string",
            "enum": [
              "monthly",
              "annual"
            ]
          }
        }
      },
      "HostedUrl": {
        "type": "object",
        "required": [
          "url"
        ],
        "properties": {
          "url": {
            "type": "string",
            "format": "uri",
            "description": "Stripe-hosted session URL (example is fictional).",
            "examples": [
              "https://checkout.stripe.com/c/pay/example"
            ]
          }
        }
      },
      "BrowserCheckoutRequest": {
        "type": "object",
        "required": [
          "tier",
          "cycle"
        ],
        "properties": {
          "tier": {
            "type": "string",
            "enum": [
              "starter",
              "pro",
              "team"
            ]
          },
          "cycle": {
            "type": "string",
            "enum": [
              "monthly",
              "annual"
            ]
          }
        }
      }
    }
  }
}
