{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "SudoHex agent protocol v1",
  "description": "Actions an agent submits and the results/view it gets back. Served at /agents/schema.json. Human-readable spec: docs/agent-protocol.md. scripts/agent-check.ts fails if the codes, ops or view fields here differ from src/agent.",
  "definitions": {
    "Cell": {
      "description": "Row then column, one label each. SudoHex: one hex digit, \"D6\" is row D, column 6. Sudoku and Sudobi: one digit as on the board's axes, \"53\" is row 5, column 3.",
      "type": "string",
      "pattern": "^[0-9A-Fa-f]{2}$"
    },
    "Symbol": {
      "description": "One symbol. SudoHex: a hex digit, 0-9 or A-F. Sudoku: 1-9. Sudobi: 1-4.",
      "type": "string",
      "pattern": "^[0-9A-Fa-f]$"
    },
    "Action": {
      "oneOf": [
        {
          "title": "place",
          "type": "object",
          "required": ["op", "cell", "symbol"],
          "properties": {
            "op": { "const": "place" },
            "cell": { "$ref": "#/definitions/Cell" },
            "symbol": { "$ref": "#/definitions/Symbol" },
            "by": { "enum": ["naked_single", "hidden_single", "locked_candidates", "n_tuple", "x_wing", "short_chain", "xy_wing", "uniqueness", "fish", "aic", "als", "forcing_chain", "advanced"], "description": "Required in rated games: the technique that justifies this placement, checked by the solver. advanced = beyond the catalogue, needs note. Sudobi offers only naked_single and hidden_single." },
            "unit": { "type": "string", "pattern": "^[RCB][0-9A-Fa-f]$", "description": "With hidden_single: the row (R), column (C) or box (B, numbered row by row) where the symbol fits only this cell. The index is that unit's label on the board: 0-F on SudoHex, 1-9 on Sudoku, 1-4 on Sudobi (so B4 is the fourth box, boxes numbered row by row)." },
            "note": { "type": "string", "maxLength": 500, "description": "Required with advanced; optional with any other technique. Your reasoning in words, kept in transcript()." }
          },
          "additionalProperties": false
        },
        {
          "title": "hint",
          "type": "object",
          "required": ["op"],
          "properties": { "op": { "const": "hint" } },
          "additionalProperties": false
        },
        {
          "title": "candidates",
          "type": "object",
          "required": ["op"],
          "properties": { "op": { "const": "candidates" } },
          "additionalProperties": false
        }
      ]
    },
    "NewRequest": {
      "description": "The object form of new(): sudohexAgent.new({ mode, difficulty, seed }). new(\"beginner\", seed) is the same request for SudoHex. difficulty is required; mode defaults to sudohex; the argument is a plain object and the seed goes inside it (a second argument is bad_seed). Levels: SudoHex beginner to master; Sudoku beginner to expert; Sudobi beginner to adept. An unknown mode is a bad_mode Problem, a level the mode does not offer is bad_difficulty.",
      "type": "object",
      "required": ["difficulty"],
      "properties": {
        "mode": { "enum": ["sudohex", "sudoku", "sudobi"], "default": "sudohex" },
        "difficulty": { "enum": ["beginner", "apprentice", "adept", "expert", "master"] },
        "seed": { "description": "Leave it out for a rated game; any seed makes a non-rated practice game.", "type": "integer", "minimum": 1, "maximum": 2147483647 }
      }
    },
    "Problem": {
      "description": "A request that is not a move (new, view, transcript or act called wrongly). Nothing was played and nothing is charged.",
      "type": "object",
      "required": ["error", "msg"],
      "properties": {
        "error": { "enum": ["no_game", "bad_mode", "bad_difficulty", "bad_seed", "bad_actions"] },
        "msg": { "type": "string" }
      }
    },
    "Batch": {
      "description": "Ordered. Each action is judged against the state left by the ones before it. Partial: a failed action does not stop the rest.",
      "type": "array",
      "maxItems": 300,
      "items": { "$ref": "#/definitions/Action" }
    },
    "Result": {
      "description": "One per submitted action, in order, same index.",
      "type": "object",
      "required": ["i", "op", "ok"],
      "properties": {
        "i": { "type": "integer", "minimum": 0 },
        "op": { "type": "string" },
        "ok": { "type": "boolean" },
        "code": {
          "enum": ["bad_action", "bad_cell", "bad_symbol", "cell_given", "cell_filled", "bad_reason", "game_over", "hint_limit", "no_hint", "batch_too_long", "wrong_symbol"]
        },
        "charged": { "description": "true only for wrong_symbol: it was played and counted as a mistake.", "type": "boolean" },
        "msg": { "type": "string" },
        "cell": { "$ref": "#/definitions/Cell" },
        "symbol": { "$ref": "#/definitions/Symbol" },
        "method": { "description": "hint only: which technique found it, in words.", "type": "string" },
        "by": { "description": "hint only: the same technique as a machine-readable label, the vocabulary place.by uses.", "enum": ["naked_single", "hidden_single", "locked_candidates", "n_tuple", "x_wing", "short_chain", "xy_wing", "uniqueness", "fish", "aic", "als", "forcing_chain", "advanced"] },
        "cost": { "description": "hint only: score points charged. 0 when the same hint is repeated on an unchanged board.", "type": "integer", "minimum": 0 },
        "candidates": {
          "description": "candidates only: every empty cell mapped to the symbols the visible board still allows.",
          "type": "object",
          "additionalProperties": { "type": "string", "pattern": "^[0-9A-F]*$" }
        }
      }
    },
    "GameView": {
      "type": "object",
      "required": ["v", "status", "label", "difficulty", "seed", "board", "empty", "mistakes", "hints", "elapsed_s"],
      "properties": {
        "v": { "const": 1 },
        "status": { "enum": ["playing", "won", "lost"] },
        "label": { "enum": ["Non-rated / Offline", "Rated / Locally Protected"] },
        "mode": { "enum": ["sudoku", "sudobi"], "description": "Only Sudoku and Sudobi games carry it. A view without mode is a SudoHex game." },
        "receipt": { "type": "string", "description": "Only once the game is over: one plain line to report as your result." },
        "rank": {
          "type": "object",
          "description": "Only on rated games: your agent rank, kept in this browser and separate from the human rank.",
          "required": ["name", "index", "best_score", "streak"],
          "properties": {
            "name": { "enum": ["Beginner", "Apprentice", "Adept", "Expert", "Master"] },
            "index": { "type": "integer", "minimum": 0, "maximum": 4 },
            "best_score": { "type": "integer", "minimum": 0 },
            "streak": { "type": "integer", "minimum": 0 }
          },
          "additionalProperties": false
        },
        "difficulty": { "enum": ["beginner", "apprentice", "adept", "expert", "master"] },
        "seed": { "type": "integer", "minimum": 1 },
        "board": {
          "description": "The board's lines, as many strings as it has rows (SudoHex 16 of 16 characters, Sudoku 9 of 9, Sudobi 4 of 4). Line index = row, character index = column. A symbol, or \".\" for empty.",
          "type": "array",
          "minItems": 4,
          "maxItems": 16,
          "items": { "type": "string", "pattern": "^[0-9A-F.]{4,16}$" }
        },
        "empty": { "type": "integer", "minimum": 0, "maximum": 256 },
        "mistakes": {
          "type": "object",
          "required": ["made", "fatal_at"],
          "properties": { "made": { "type": "integer", "minimum": 0 }, "fatal_at": { "description": "The game is lost when made reaches this number.", "const": 4 } }
        },
        "hints": {
          "type": "object",
          "required": ["used", "left", "cost"],
          "properties": { "used": { "type": "integer" }, "left": { "type": "integer" }, "cost": { "type": "integer" } }
        },
        "elapsed_s": { "type": "integer", "minimum": 0 },
        "score": {
          "description": "Present once status is won.",
          "type": "object",
          "required": ["baseScore", "maxScore", "timePenalty", "errorPenalty", "undoPenalty", "hintPenalty", "finalScore"],
          "properties": {
            "baseScore": { "type": "integer" },
            "maxScore": { "type": "integer" },
            "timePenalty": { "type": "integer" },
            "errorPenalty": { "type": "integer" },
            "undoPenalty": { "type": "integer" },
            "hintPenalty": { "type": "integer" },
            "finalScore": { "type": "integer" }
          }
        }
      }
    }
  }
}
