{
  "openapi": "3.1.0",
  "info": {
    "title": "KernelSprint API",
    "version": "1.0.0"
  },
  "paths": {
    "/api/v1/bounties": {
      "get": {
        "responses": {
          "200": {
            "description": "Public bounties",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BountyList"
                }
              }
            }
          }
        }
      },
      "post": {
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BountyInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BountyCreated"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/api/v1/bounties/{id}": {
      "parameters": [
        {
          "$ref": "#/components/parameters/id"
        }
      ],
      "get": {
        "responses": {
          "200": {
            "description": "Bounty and solutions",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BountyDetail"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/Error"
          }
        }
      },
      "patch": {
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["status"],
                "properties": {
                  "status": {
                    "const": "archived"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Archived"
          },
          "401": {
            "$ref": "#/components/responses/Error"
          },
          "403": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/api/v1/bounties/{id}/submissions": {
      "parameters": [
        {
          "$ref": "#/components/parameters/id"
        }
      ],
      "get": {
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Owner submissions",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["submissions"],
                  "properties": {
                    "submissions": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/PrivateSubmission"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/api/v1/bounties/{id}/solutions": {
      "parameters": [
        {
          "$ref": "#/components/parameters/id"
        }
      ],
      "post": {
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SolutionInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Submitted",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SolutionCreated"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/api/v1/solutions/{id}/source": {
      "parameters": [
        {
          "$ref": "#/components/parameters/id"
        }
      ],
      "get": {
        "security": [
          {
            "bearerAuth": []
          }
        ],
        "responses": {
          "200": {
            "description": "Kernel source",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              },
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/api/v1/bounties/{id}/contract": {
      "parameters": [{ "$ref": "#/components/parameters/id" }],
      "get": {
        "description": "Download the canonical public bounty contract. Legacy bounties without an evaluation manifest return 409.",
        "responses": {
          "200": {
            "description": "Canonical contract JSON",
            "headers": {
              "X-Contract-SHA256": {
                "schema": { "type": "string", "pattern": "^[0-9a-f]{64}$" }
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/Contract" }
              }
            }
          },
          "404": { "$ref": "#/components/responses/Error" },
          "409": { "$ref": "#/components/responses/Error" }
        }
      }
    }
  },
  "components": {
    "parameters": {
      "id": {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string",
          "format": "uuid"
        }
      }
    },
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "responses": {
      "Error": {
        "description": "Error",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "Evaluation": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "schemaVersion",
          "environment",
          "workload",
          "correctness",
          "measurement",
          "integration",
          "resources",
          "award",
          "artifact"
        ],
        "properties": {
          "schemaVersion": { "const": 1 },
          "environment": {
            "type": "string",
            "minLength": 1,
            "maxLength": 2000
          },
          "workload": { "type": "string", "minLength": 1, "maxLength": 4000 },
          "correctness": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4000
          },
          "measurement": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4000
          },
          "integration": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4000
          },
          "resources": { "type": "string", "minLength": 1, "maxLength": 2000 },
          "award": { "type": "string", "minLength": 1, "maxLength": 4000 },
          "artifact": { "type": "string", "minLength": 1, "maxLength": 2000 }
        }
      },
      "Contract": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "title",
          "description",
          "gpu",
          "baselineUrl",
          "benchmark",
          "prizeUsd",
          "deadline",
          "sponsor",
          "license",
          "evaluation"
        ],
        "properties": {
          "title": { "type": "string" },
          "description": { "type": "string" },
          "gpu": { "type": "string" },
          "baselineUrl": { "type": "string", "format": "uri" },
          "benchmark": { "type": "string" },
          "prizeUsd": { "type": "number" },
          "deadline": { "type": "string", "format": "date-time" },
          "sponsor": { "type": "string" },
          "license": { "type": "string" },
          "evaluation": { "$ref": "#/components/schemas/Evaluation" }
        }
      },
      "BountyInput": {
        "type": "object",
        "required": [
          "title",
          "description",
          "gpu",
          "baselineUrl",
          "benchmark",
          "prizeUsd",
          "deadline",
          "email",
          "sponsor",
          "license",
          "evaluation"
        ],
        "properties": {
          "title": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "gpu": {
            "type": "string"
          },
          "baselineUrl": {
            "type": "string",
            "format": "uri"
          },
          "benchmark": {
            "type": "string"
          },
          "prizeUsd": {
            "type": "number",
            "exclusiveMinimum": 0
          },
          "deadline": {
            "type": "string",
            "format": "date-time"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "sponsor": {
            "type": "string"
          },
          "license": {
            "type": "string"
          },
          "evaluation": {
            "$ref": "#/components/schemas/Evaluation"
          }
        }
      },
      "Bounty": {
        "type": "object",
        "required": [
          "id",
          "title",
          "description",
          "gpu",
          "baselineUrl",
          "benchmark",
          "prizeUsd",
          "deadline",
          "sponsor",
          "license",
          "status",
          "fundingStatus",
          "createdAt",
          "submissionCount",
          "acceptingSubmissions",
          "evaluation",
          "contractSha256"
        ],
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "title": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "gpu": {
            "type": "string"
          },
          "baselineUrl": {
            "type": "string",
            "format": "uri"
          },
          "benchmark": {
            "type": "string"
          },
          "prizeUsd": {
            "type": "number"
          },
          "deadline": {
            "type": "string",
            "format": "date-time"
          },
          "sponsor": {
            "type": "string"
          },
          "license": {
            "type": "string"
          },
          "status": {
            "enum": ["open", "archived"]
          },
          "fundingStatus": {
            "const": "pledged"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "submissionCount": {
            "type": "integer",
            "minimum": 0
          },
          "acceptingSubmissions": {
            "type": "boolean",
            "description": "Whether the bounty is open and before its deadline, evaluated by the database clock."
          },
          "evaluation": {
            "oneOf": [
              { "$ref": "#/components/schemas/Evaluation" },
              { "type": "null" }
            ]
          },
          "contractSha256": {
            "oneOf": [
              { "type": "string", "pattern": "^[0-9a-f]{64}$" },
              { "type": "null" }
            ]
          }
        }
      },
      "PublicSolution": {
        "type": "object",
        "required": ["id", "name", "createdAt", "status", "sha256", "verdicts"],
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "name": {
            "type": "string"
          },
          "createdAt": {
            "type": "string",
            "format": "date-time"
          },
          "status": {
            "const": "submitted"
          },
          "sha256": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          },
          "verdicts": {
            "$ref": "#/components/schemas/Verdicts"
          }
        }
      },
      "PrivateSubmission": {
        "allOf": [
          {
            "$ref": "#/components/schemas/PublicSolution"
          },
          {
            "type": "object",
            "required": ["email", "notes", "filename", "publicCredit"],
            "properties": {
              "email": {
                "type": "string",
                "format": "email"
              },
              "notes": {
                "type": "string"
              },
              "filename": {
                "type": "string"
              },
              "publicCredit": {
                "type": "boolean"
              }
            }
          }
        ]
      },
      "Verdicts": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "correctness",
          "performance",
          "integration",
          "deployability"
        ],
        "properties": {
          "correctness": { "const": "not_assessed" },
          "performance": { "const": "not_assessed" },
          "integration": { "const": "not_assessed" },
          "deployability": { "const": "not_assessed" }
        }
      },
      "BountyList": {
        "type": "object",
        "required": ["bounties"],
        "properties": {
          "bounties": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Bounty"
            }
          }
        }
      },
      "BountyCreated": {
        "type": "object",
        "required": ["bounty", "manageToken"],
        "properties": {
          "bounty": {
            "$ref": "#/components/schemas/Bounty"
          },
          "manageToken": {
            "type": "string"
          }
        }
      },
      "BountyDetail": {
        "type": "object",
        "required": ["bounty", "solutions"],
        "properties": {
          "bounty": {
            "$ref": "#/components/schemas/Bounty"
          },
          "solutions": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/PublicSolution"
            }
          }
        }
      },
      "SolutionInput": {
        "type": "object",
        "required": [
          "name",
          "email",
          "notes",
          "filename",
          "content",
          "licenseAccepted",
          "publicCredit"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "email": {
            "type": "string",
            "format": "email"
          },
          "notes": {
            "type": "string"
          },
          "filename": {
            "type": "string"
          },
          "content": {
            "type": "string"
          },
          "licenseAccepted": {
            "const": true
          },
          "publicCredit": {
            "type": "boolean"
          }
        }
      },
      "SolutionCreated": {
        "type": "object",
        "required": ["solution", "receiptToken"],
        "properties": {
          "solution": {
            "$ref": "#/components/schemas/PublicSolution"
          },
          "receiptToken": {
            "type": "string"
          }
        }
      },
      "Error": {
        "type": "object",
        "required": ["error"],
        "properties": {
          "error": {
            "type": "string"
          }
        }
      }
    }
  }
}
