{
  "openapi": "3.1.0",
  "info": {
    "title": "Rill 源流 API",
    "version": "0.1.0",
    "description": "MVP 设计契约。路径相对未来的 API 服务地址；当前仅发布规范。"
  },
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "schemas": {
      "Reset": {
        "type": "object",
        "properties": {
          "task": {
            "const": "order-exception@1"
          },
          "seed": {
            "type": "integer",
            "minimum": 0,
            "maximum": 2147483647
          }
        },
        "required": [
          "task",
          "seed"
        ],
        "additionalProperties": false
      },
      "Step": {
        "type": "object",
        "properties": {
          "state_token": {
            "type": "string",
            "minLength": 1
          },
          "action": {
            "type": "string",
            "enum": [
              "check_payment",
              "check_stock",
              "ship",
              "hold"
            ]
          }
        },
        "required": [
          "state_token",
          "action"
        ],
        "additionalProperties": false
      },
      "Transition": {
        "type": "object",
        "properties": {
          "state_token": {
            "type": "string",
            "minLength": 1
          },
          "observation": {
            "type": "object",
            "properties": {
              "payment": {
                "type": "string",
                "enum": [
                  "unknown",
                  "paid",
                  "unpaid"
                ]
              },
              "stock": {
                "type": "string",
                "enum": [
                  "unknown",
                  "available",
                  "empty"
                ]
              },
              "order": {
                "type": "string",
                "enum": [
                  "pending",
                  "shipped",
                  "held"
                ]
              }
            },
            "required": [
              "payment",
              "stock",
              "order"
            ],
            "additionalProperties": false
          },
          "step": {
            "type": "integer",
            "minimum": 0
          },
          "reward": {
            "type": "integer",
            "enum": [
              0,
              1
            ]
          },
          "done": {
            "type": "boolean"
          }
        },
        "required": [
          "state_token",
          "observation",
          "step",
          "reward",
          "done"
        ],
        "additionalProperties": false
      },
      "Manifest": {
        "type": "object",
        "properties": {
          "version": {
            "type": "string",
            "minLength": 1
          },
          "recipe": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "source_version": {
                  "type": "string",
                  "minLength": 1
                },
                "weight_bps": {
                  "type": "integer",
                  "minimum": 1,
                  "maximum": 10000
                },
                "license_ref": {
                  "type": "string",
                  "minLength": 1
                }
              },
              "required": [
                "source_version",
                "weight_bps",
                "license_ref"
              ],
              "additionalProperties": false
            }
          },
          "exclusion_version": {
            "type": "string",
            "minLength": 1
          },
          "sample_count": {
            "type": "integer",
            "minimum": 0
          },
          "sequence_sha256": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          }
        },
        "required": [
          "version",
          "recipe",
          "exclusion_version",
          "sample_count",
          "sequence_sha256"
        ],
        "additionalProperties": false
      },
      "Sample": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "text": {
            "type": "string",
            "minLength": 1
          },
          "source_version": {
            "type": "string",
            "minLength": 1
          },
          "license_ref": {
            "type": "string",
            "minLength": 1
          },
          "next_cursor": {
            "type": [
              "string",
              "null"
            ]
          }
        },
        "required": [
          "id",
          "text",
          "source_version",
          "license_ref",
          "next_cursor"
        ],
        "additionalProperties": false
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string",
                "minLength": 1
              },
              "message": {
                "type": "string",
                "minLength": 1
              }
            },
            "required": [
              "code",
              "message"
            ],
            "additionalProperties": false
          },
          "request_id": {
            "type": "string",
            "minLength": 1
          }
        },
        "required": [
          "error",
          "request_id"
        ],
        "additionalProperties": false
      }
    }
  },
  "paths": {
    "/v1/env/reset": {
      "post": {
        "operationId": "reset",
        "summary": "按任务版本和种子开始；固定版本固定起点",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Reset"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Transition"
                }
              }
            }
          },
          "400": {
            "description": "invalid_argument / invalid_cursor / cursor_version_mismatch",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "unauthenticated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "forbidden",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "重试前等待的秒数",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          },
          "503": {
            "description": "temporarily_unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "重试前等待的秒数",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          }
        }
      }
    },
    "/v1/env/step": {
      "post": {
        "operationId": "step",
        "summary": "按不可变状态推进一步；同一状态可分叉",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Step"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Transition"
                }
              }
            }
          },
          "400": {
            "description": "invalid_argument / invalid_cursor / cursor_version_mismatch",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "unauthenticated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "forbidden",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "409": {
            "description": "episode_done / invalid_state",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "410": {
            "description": "state_expired",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "重试前等待的秒数",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          },
          "503": {
            "description": "temporarily_unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "重试前等待的秒数",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          }
        }
      }
    },
    "/v1/data/{version}/manifest": {
      "get": {
        "operationId": "getManifest",
        "summary": "读取不可变版本的配方、许可及序列摘要",
        "parameters": [
          {
            "name": "version",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "成功",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Manifest"
                }
              }
            }
          },
          "401": {
            "description": "unauthenticated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "forbidden",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "version_not_found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "重试前等待的秒数",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          },
          "503": {
            "description": "temporarily_unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "重试前等待的秒数",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          }
        }
      }
    },
    "/v1/data/{version}/samples": {
      "get": {
        "operationId": "getSamples",
        "summary": "按固定顺序流式读取；每行一个 Sample，游标指向该行之后",
        "parameters": [
          {
            "name": "version",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string",
              "minLength": 1
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 1000,
              "default": 100
            }
          }
        ],
        "responses": {
          "200": {
            "description": "UTF-8 NDJSON；最后一条样本的 next_cursor 为 null。空版本返回空流。",
            "content": {
              "application/x-ndjson": {
                "schema": {
                  "type": "string"
                },
                "x-line-schema": {
                  "$ref": "#/components/schemas/Sample"
                }
              }
            }
          },
          "400": {
            "description": "invalid_argument / invalid_cursor / cursor_version_mismatch",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "unauthenticated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "forbidden",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "version_not_found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "description": "rate_limited",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "重试前等待的秒数",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          },
          "503": {
            "description": "temporarily_unavailable",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            },
            "headers": {
              "Retry-After": {
                "description": "重试前等待的秒数",
                "schema": {
                  "type": "integer",
                  "minimum": 0
                }
              }
            }
          }
        }
      }
    }
  }
}
