{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aisa.ablera.com/contracts/runtime-state.schema.json",
  "title": "Restorable task state v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "task_id",
    "next_turn_no",
    "phase",
    "pins",
    "context_paper_id",
    "model_call_id",
    "pending_call_ids",
    "pending_gate_ids",
    "pending_message_ids",
    "paper_heads",
    "completed_step_ids",
    "handle_scope_case_id"
  ],
  "properties": {
    "schema_version": {
      "const": 1
    },
    "task_id": {
      "type": "string",
      "minLength": 1
    },
    "next_turn_no": {
      "type": "integer",
      "minimum": 1
    },
    "phase": {
      "enum": [
        "ready",
        "action_pending",
        "waiting",
        "complete"
      ]
    },
    "pins": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "manifest_policy_id",
        "manifest_hash",
        "case_type_id",
        "profile_id",
        "prompt_id",
        "route_id",
        "framework_version",
        "runtime_version"
      ],
      "properties": {
        "manifest_policy_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "manifest_hash": {
          "type": [
            "string",
            "null"
          ]
        },
        "case_type_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "profile_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "prompt_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "route_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "framework_version": {
          "type": [
            "string",
            "null"
          ]
        },
        "runtime_version": {
          "type": [
            "string",
            "null"
          ]
        }
      }
    },
    "context_paper_id": {
      "type": [
        "string",
        "null"
      ]
    },
    "model_call_id": {
      "type": [
        "string",
        "null"
      ]
    },
    "pending_call_ids": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true
    },
    "pending_gate_ids": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true
    },
    "pending_message_ids": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true
    },
    "paper_heads": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "kind",
          "paper_id",
          "hash"
        ],
        "properties": {
          "kind": {
            "type": "string"
          },
          "paper_id": {
            "type": "string"
          },
          "hash": {
            "type": "string",
            "pattern": "^[a-f0-9]{64}$"
          }
        }
      }
    },
    "completed_step_ids": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true
    },
    "handle_scope_case_id": {
      "type": "string",
      "minLength": 1
    }
  }
}
