{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://aslice.sh/schematics/json/recovery-plan.schema.json",
  "title": "Recovery plan",
  "description": "Version 1 structural contract. Normative field semantics: docs/STATE-AND-RECOVERY.md section 10.2 and docs/DATABASE.md section 9. Cryptography, canonical encoding, chain continuity, ordering, and filesystem safety require semantic validation.",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "format": {
      "const": 1
    },
    "action": {
      "enum": [
        "continue",
        "manual",
        "salvage",
        "activate"
      ]
    },
    "instance_id": {
      "type": "string",
      "pattern": "^[0-9a-f]{32}$"
    },
    "interrupted_operation": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[0-9a-f]{32}$"
        },
        {
          "type": "null"
        }
      ]
    },
    "base_head": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "base_generation": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        {
          "type": "null"
        }
      ]
    },
    "replacement_path": {
      "anyOf": [
        {
          "type": "string",
          "minLength": 1
        },
        {
          "type": "null"
        }
      ]
    },
    "evidence": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "digest": {
            "type": "string",
            "pattern": "^sha256:[0-9a-f]{64}$"
          },
          "size": {
            "type": "integer",
            "minimum": 0,
            "maximum": 9007199254740991
          }
        },
        "required": [
          "digest",
          "size"
        ]
      },
      "uniqueItems": true
    },
    "artifacts": {
      "type": "array",
      "items": {
        "type": "string",
        "pattern": "^sha256:[0-9a-f]{64}$"
      },
      "uniqueItems": true
    },
    "omissions": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    },
    "required_consents": {
      "type": "array",
      "items": {
        "enum": [
          "system-changes",
          "grafts"
        ]
      },
      "uniqueItems": true
    },
    "effects": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "owner": {
            "enum": [
              "user",
              "protected"
            ]
          },
          "resource": {
            "type": "string",
            "minLength": 1
          },
          "operation": {
            "enum": [
              "restore",
              "retain",
              "replace",
              "register",
              "unregister",
              "relocate",
              "select"
            ]
          },
          "before": {
            "anyOf": [
              {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "digest": {
                    "type": "string",
                    "pattern": "^sha256:[0-9a-f]{64}$"
                  },
                  "size": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 9007199254740991
                  }
                },
                "required": [
                  "digest",
                  "size"
                ]
              },
              {
                "type": "null"
              }
            ]
          },
          "after": {
            "anyOf": [
              {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "digest": {
                    "type": "string",
                    "pattern": "^sha256:[0-9a-f]{64}$"
                  },
                  "size": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 9007199254740991
                  }
                },
                "required": [
                  "digest",
                  "size"
                ]
              },
              {
                "type": "null"
              }
            ]
          },
          "backup": {
            "anyOf": [
              {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "digest": {
                    "type": "string",
                    "pattern": "^sha256:[0-9a-f]{64}$"
                  },
                  "size": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 9007199254740991
                  }
                },
                "required": [
                  "digest",
                  "size"
                ]
              },
              {
                "type": "null"
              }
            ]
          }
        },
        "required": [
          "owner",
          "resource",
          "operation",
          "before",
          "after",
          "backup"
        ]
      },
      "uniqueItems": true
    },
    "choices": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "group": {
            "type": "string",
            "minLength": 1
          },
          "group_digest": {
            "type": "string",
            "pattern": "^sha256:[0-9a-f]{64}$"
          },
          "choice": {
            "enum": [
              "restore",
              "keep",
              "manual"
            ]
          },
          "evidence": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "digest": {
                  "type": "string",
                  "pattern": "^sha256:[0-9a-f]{64}$"
                },
                "size": {
                  "type": "integer",
                  "minimum": 0,
                  "maximum": 9007199254740991
                }
              },
              "required": [
                "digest",
                "size"
              ]
            },
            "uniqueItems": true
          }
        },
        "required": [
          "group",
          "group_digest",
          "choice",
          "evidence"
        ]
      },
      "uniqueItems": true
    }
  },
  "required": [
    "format",
    "action",
    "instance_id",
    "interrupted_operation",
    "base_head",
    "base_generation",
    "replacement_path",
    "evidence",
    "artifacts",
    "omissions",
    "required_consents",
    "effects",
    "choices"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "action": {
            "const": "continue"
          }
        }
      },
      "then": {
        "properties": {
          "interrupted_operation": {
            "type": "string",
            "pattern": "^[0-9a-f]{32}$"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "action": {
            "const": "activate"
          }
        }
      },
      "then": {
        "properties": {
          "replacement_path": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    }
  ]
}
