{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/cache/cache-update-transaction.v1.schema.json",
  "title": "RootBlocks Cache Update Transaction v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "transactionId",
    "startedAt",
    "finishedAt",
    "operation",
    "enforcementMode",
    "targetBundle",
    "source",
    "previousValidCache",
    "staging",
    "verification",
    "lock",
    "replacement",
    "recovery",
    "result",
    "reasonCodes",
    "requiredActions",
    "evidence"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.cache-update-transaction/v1"
    },
    "transactionId": {
      "type": "string",
      "pattern": "^cache-update-[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "startedAt": {
      "type": "string",
      "format": "date-time"
    },
    "finishedAt": {
      "type": "string",
      "format": "date-time"
    },
    "operation": {
      "$ref": "#/$defs/operation"
    },
    "enforcementMode": {
      "enum": ["advisory", "enforced"]
    },
    "targetBundle": {
      "$ref": "#/$defs/bundleRef"
    },
    "source": {
      "$ref": "#/$defs/updateSource"
    },
    "previousValidCache": {
      "anyOf": [
        { "$ref": "#/$defs/previousCache" },
        { "type": "null" }
      ]
    },
    "staging": {
      "$ref": "#/$defs/staging"
    },
    "verification": {
      "$ref": "#/$defs/verification"
    },
    "lock": {
      "$ref": "#/$defs/lock"
    },
    "replacement": {
      "$ref": "#/$defs/replacement"
    },
    "quarantine": {
      "$ref": "#/$defs/quarantine"
    },
    "recovery": {
      "$ref": "#/$defs/recovery"
    },
    "result": {
      "enum": ["committed", "restored-last-valid", "quarantined", "blocked", "wait-for-lock"]
    },
    "reasonCodes": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": [
          "UPDATE_COMMITTED",
          "TEMP_VERIFY_FAILED",
          "SIGNATURE_INVALID",
          "ASSET_HASH_MISMATCH",
          "COMPATIBILITY_BLOCK",
          "LOCK_HELD",
          "PARTIAL_DOWNLOAD",
          "METADATA_WRITE_FAILED",
          "RESTORED_LAST_VALID",
          "QUARANTINED_CURRENT",
          "OFFLINE_BLOCKED"
        ]
      },
      "uniqueItems": true
    },
    "requiredActions": {
      "type": "array",
      "minItems": 1,
      "items": {
        "enum": [
          "download-temp",
          "verify-temp",
          "acquire-lock",
          "atomic-replace",
          "emit-cache-metadata",
          "preserve-last-valid",
          "quarantine-current",
          "restore-last-valid",
          "block-operation",
          "wait-for-lock"
        ]
      },
      "uniqueItems": true
    },
    "evidence": {
      "$ref": "#/$defs/evidence"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "result": {
            "const": "committed"
          }
        },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "staging": {
            "properties": {
              "downloadComplete": {
                "const": true
              }
            },
            "required": ["downloadComplete"]
          },
          "verification": {
            "properties": {
              "result": {
                "const": "pass"
              }
            },
            "required": ["result"]
          },
          "lock": {
            "properties": {
              "acquired": {
                "const": true
              }
            },
            "required": ["acquired"]
          },
          "replacement": {
            "properties": {
              "state": {
                "const": "committed"
              },
              "preserveLastValid": {
                "const": true
              },
              "atomicReplace": {
                "const": true
              },
              "metadataWrittenAfterReplace": {
                "const": true
              }
            },
            "required": ["state", "preserveLastValid", "atomicReplace", "metadataWrittenAfterReplace"]
          },
          "requiredActions": {
            "allOf": [
              { "contains": { "const": "download-temp" } },
              { "contains": { "const": "verify-temp" } },
              { "contains": { "const": "acquire-lock" } },
              { "contains": { "const": "atomic-replace" } },
              { "contains": { "const": "emit-cache-metadata" } },
              { "contains": { "const": "preserve-last-valid" } }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "verification": {
            "properties": {
              "result": {
                "const": "fail"
              }
            },
            "required": ["result"]
          }
        },
        "required": ["verification"]
      },
      "then": {
        "properties": {
          "result": {
            "enum": ["blocked", "quarantined", "restored-last-valid"]
          },
          "requiredActions": {
            "contains": {
              "const": "block-operation"
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "result": {
            "const": "quarantined"
          }
        },
        "required": ["result"]
      },
      "then": {
        "required": ["quarantine"],
        "properties": {
          "quarantine": {
            "required": ["quarantineId", "state", "reasonCodes", "quarantinedAt", "quarantinePath"],
            "properties": {
              "state": {
                "const": "quarantined"
              }
            }
          },
          "recovery": {
            "properties": {
              "action": {
                "const": "quarantine-current"
              }
            },
            "required": ["action"]
          },
          "reasonCodes": {
            "contains": {
              "const": "QUARANTINED_CURRENT"
            }
          },
          "requiredActions": {
            "allOf": [
              { "contains": { "const": "quarantine-current" } },
              { "contains": { "const": "block-operation" } }
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "result": {
            "const": "restored-last-valid"
          }
        },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "previousValidCache": {
            "$ref": "#/$defs/previousCache"
          },
          "recovery": {
            "properties": {
              "action": {
                "const": "restore-last-valid"
              }
            },
            "required": ["action"]
          },
          "reasonCodes": {
            "contains": {
              "const": "RESTORED_LAST_VALID"
            }
          },
          "requiredActions": {
            "contains": {
              "const": "restore-last-valid"
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "result": {
            "const": "wait-for-lock"
          }
        },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "lock": {
            "properties": {
              "acquired": {
                "const": false
              }
            },
            "required": ["acquired"]
          },
          "reasonCodes": {
            "contains": {
              "const": "LOCK_HELD"
            }
          },
          "requiredActions": {
            "contains": {
              "const": "wait-for-lock"
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "result": {
            "const": "blocked"
          }
        },
        "required": ["result"]
      },
      "then": {
        "properties": {
          "requiredActions": {
            "contains": {
              "const": "block-operation"
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "hash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64,128}$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@-]+$"
    },
    "uri": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500
    },
    "operation": {
      "enum": ["generate", "validate", "compile", "run", "debug", "package", "sign", "release"]
    },
    "bundleKind": {
      "enum": [
        "rootblocks-global-standard",
        "organization-component-extension",
        "project-component-extension",
        "personal-component-extension"
      ]
    },
    "bundleRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "bundleKind", "version", "manifestHash", "bundleHash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "bundleKind": {
          "$ref": "#/$defs/bundleKind"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        },
        "bundleHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "updateSource": {
      "type": "object",
      "additionalProperties": false,
      "required": ["distributionMode", "sourceRef", "downloadManifestPath", "localVerificationRequired"],
      "properties": {
        "distributionMode": {
          "enum": ["service", "git-bootstrap", "service-with-git-fallback"]
        },
        "sourceRef": {
          "$ref": "#/$defs/uri"
        },
        "downloadManifestPath": {
          "$ref": "#/$defs/path"
        },
        "localVerificationRequired": {
          "const": true
        }
      }
    },
    "previousCache": {
      "type": "object",
      "additionalProperties": false,
      "required": ["metadataPath", "bundlePath", "manifestHash", "bundleHash", "state"],
      "properties": {
        "metadataPath": {
          "$ref": "#/$defs/path"
        },
        "bundlePath": {
          "$ref": "#/$defs/path"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        },
        "bundleHash": {
          "$ref": "#/$defs/hash"
        },
        "state": {
          "enum": ["current", "stale-warning"]
        }
      }
    },
    "staging": {
      "type": "object",
      "additionalProperties": false,
      "required": ["tempPath", "downloadComplete", "downloadHash", "sizeBytes"],
      "properties": {
        "tempPath": {
          "$ref": "#/$defs/path"
        },
        "downloadComplete": {
          "type": "boolean"
        },
        "downloadHash": {
          "$ref": "#/$defs/hash"
        },
        "sizeBytes": {
          "type": "integer",
          "minimum": 1
        }
      }
    },
    "verification": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schema",
        "manifestHash",
        "bundleHash",
        "assetHashes",
        "signature",
        "compatibility",
        "result"
      ],
      "properties": {
        "schema": {
          "$ref": "#/$defs/checkResult"
        },
        "manifestHash": {
          "$ref": "#/$defs/checkResult"
        },
        "bundleHash": {
          "$ref": "#/$defs/checkResult"
        },
        "assetHashes": {
          "$ref": "#/$defs/checkResult"
        },
        "signature": {
          "$ref": "#/$defs/checkResult"
        },
        "compatibility": {
          "$ref": "#/$defs/checkResult"
        },
        "result": {
          "enum": ["pass", "fail"]
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "result": {
                "const": "pass"
              }
            },
            "required": ["result"]
          },
          "then": {
            "properties": {
              "schema": { "properties": { "status": { "const": "pass" } } },
              "manifestHash": { "properties": { "status": { "const": "pass" } } },
              "bundleHash": { "properties": { "status": { "const": "pass" } } },
              "assetHashes": { "properties": { "status": { "const": "pass" } } },
              "signature": { "properties": { "status": { "const": "pass" } } },
              "compatibility": { "properties": { "status": { "enum": ["pass", "warn"] } } }
            }
          }
        }
      ]
    },
    "checkResult": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status"],
      "properties": {
        "status": {
          "enum": ["pass", "warn", "fail", "not-run"]
        },
        "path": {
          "$ref": "#/$defs/path"
        },
        "message": {
          "type": "string",
          "minLength": 1,
          "maxLength": 500
        }
      }
    },
    "lock": {
      "type": "object",
      "additionalProperties": false,
      "required": ["lockId", "acquired", "lockPath"],
      "properties": {
        "lockId": {
          "$ref": "#/$defs/id"
        },
        "acquired": {
          "type": "boolean"
        },
        "lockPath": {
          "$ref": "#/$defs/path"
        },
        "acquiredAt": {
          "type": "string",
          "format": "date-time"
        },
        "staleLockAction": {
          "enum": ["none", "wait", "break-with-evidence"]
        }
      }
    },
    "replacement": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "state",
        "preserveLastValid",
        "atomicReplace",
        "metadataWrittenAfterReplace",
        "activeCachePath",
        "metadataPath"
      ],
      "properties": {
        "state": {
          "enum": ["not-started", "skipped", "committed", "rolled-back"]
        },
        "preserveLastValid": {
          "type": "boolean"
        },
        "atomicReplace": {
          "type": "boolean"
        },
        "metadataWrittenAfterReplace": {
          "type": "boolean"
        },
        "activeCachePath": {
          "$ref": "#/$defs/path"
        },
        "metadataPath": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "quarantine": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "quarantineId": {
          "$ref": "#/$defs/id"
        },
        "state": {
          "const": "quarantined"
        },
        "reasonCodes": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["SIGNATURE_INVALID", "ASSET_HASH_MISMATCH", "MANIFEST_HASH_MISMATCH", "SCHEMA_INVALID", "GENERIC_EXTERNAL_ASSET_WRITE"]
          },
          "uniqueItems": true
        },
        "quarantinedAt": {
          "type": "string",
          "format": "date-time"
        },
        "quarantinePath": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "recovery": {
      "type": "object",
      "additionalProperties": false,
      "required": ["action"],
      "properties": {
        "action": {
          "enum": ["none", "restore-last-valid", "quarantine-current", "block-operation", "wait-for-lock"]
        },
        "restoredFromPath": {
          "$ref": "#/$defs/path"
        },
        "evidenceRef": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "evidenceRef", "recordsVerification", "recordsLock", "recordsReplacement"],
      "properties": {
        "required": {
          "const": true
        },
        "evidenceRef": {
          "$ref": "#/$defs/path"
        },
        "recordsVerification": {
          "const": true
        },
        "recordsLock": {
          "const": true
        },
        "recordsReplacement": {
          "const": true
        }
      }
    }
  }
}
