{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://genesiscode.dev/schemas/gate-manifest-v0.1.json",
  "title": "GenesisCode Gate Manifest v0.1",
  "type": "object",
  "additionalProperties": false,
  "required": ["kind", "version", "inventory", "platforms", "profiles", "inputSets", "outputSets", "gates"],
  "properties": {
    "kind": {"const": "genesis/gate-manifest-v0.1"},
    "version": {"const": "0.1"},
    "inventory": {
      "type": "object",
      "additionalProperties": false,
      "required": ["checkGlob", "gateCount", "generatedFrom", "governanceBudget"],
      "properties": {
        "checkGlob": {"const": "scripts/check_*.sh"},
        "gateCount": {"type": "integer", "minimum": 1},
        "generatedFrom": {
          "type": "array",
          "minItems": 3,
          "maxItems": 3,
          "items": {"$ref": "#/$defs/sourceIdentity"}
        },
        "governanceBudget": {"$ref": "#/$defs/governanceBudget"}
      }
    },
    "platforms": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/platform"}
    },
    "profiles": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/profile"}
    },
    "inputSets": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/inputSet"}
    },
    "outputSets": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/outputSet"}
    },
    "gates": {
      "type": "array",
      "minItems": 1,
      "items": {"$ref": "#/$defs/gate"}
    }
  },
  "$defs": {
    "sourceIdentity": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "sha256"],
      "properties": {
        "path": {"$ref": "#/$defs/repoPath"},
        "sha256": {"$ref": "#/$defs/sha256"}
      }
    },
    "governanceBudget": {
      "type": "object",
      "additionalProperties": false,
      "required": ["baseline", "ceilings", "current", "delta", "retiredAliases", "rule", "scheduledEnvelopeSavings"],
      "properties": {
        "baseline": {"$ref": "#/$defs/governanceSnapshot"},
        "ceilings": {"$ref": "#/$defs/entrypointCeilings"},
        "current": {"$ref": "#/$defs/governanceSnapshot"},
        "delta": {"$ref": "#/$defs/governanceDelta"},
        "retiredAliases": {
          "type": "array",
          "minItems": 1,
          "items": {"$ref": "#/$defs/retiredAlias"}
        },
        "rule": {"const": "one-in-one-out-distinct-trust-boundary"},
        "scheduledEnvelopeSavings": {
          "type": "object",
          "additionalProperties": false,
          "required": ["declaredDurationSeconds", "declaredDiskMiB"],
          "properties": {
            "declaredDurationSeconds": {"type": "integer", "minimum": 0},
            "declaredDiskMiB": {"type": "integer", "minimum": 0}
          }
        }
      }
    },
    "governanceSnapshot": {
      "type": "object",
      "additionalProperties": false,
      "required": ["checkEntrypoints", "updateEntrypoints", "renderEntrypoints", "declaredDurationSeconds", "declaredDiskMiB"],
      "properties": {
        "checkEntrypoints": {"type": "integer", "minimum": 0},
        "updateEntrypoints": {"type": "integer", "minimum": 0},
        "renderEntrypoints": {"type": "integer", "minimum": 0},
        "declaredDurationSeconds": {"type": "integer", "minimum": 0},
        "declaredDiskMiB": {"type": "integer", "minimum": 0}
      }
    },
    "entrypointCeilings": {
      "type": "object",
      "additionalProperties": false,
      "required": ["checkEntrypoints", "updateEntrypoints", "renderEntrypoints"],
      "properties": {
        "checkEntrypoints": {"type": "integer", "minimum": 0},
        "updateEntrypoints": {"type": "integer", "minimum": 0},
        "renderEntrypoints": {"type": "integer", "minimum": 0}
      }
    },
    "governanceDelta": {
      "type": "object",
      "additionalProperties": false,
      "required": ["checkEntrypoints", "updateEntrypoints", "renderEntrypoints", "declaredDurationSeconds", "declaredDiskMiB"],
      "properties": {
        "checkEntrypoints": {"type": "integer", "maximum": 0},
        "updateEntrypoints": {"type": "integer", "maximum": 0},
        "renderEntrypoints": {"type": "integer", "maximum": 0},
        "declaredDurationSeconds": {"type": "integer", "maximum": 0},
        "declaredDiskMiB": {"type": "integer", "maximum": 0}
      }
    },
    "retiredAlias": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "replacement", "surface", "declaredDurationSeconds", "declaredDiskMiB"],
      "properties": {
        "path": {"$ref": "#/$defs/repoPath"},
        "replacement": {"$ref": "#/$defs/repoPath"},
        "surface": {"enum": ["check", "update", "render"]},
        "declaredDurationSeconds": {"type": "integer", "minimum": 0},
        "declaredDiskMiB": {"type": "integer", "minimum": 0}
      }
    },
    "platform": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "tier"],
      "properties": {
        "id": {"type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$"},
        "tier": {"type": "integer", "minimum": 1, "maximum": 2}
      }
    },
    "profile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "rank"],
      "properties": {
        "id": {"type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$"},
        "rank": {"type": "integer", "minimum": 0}
      }
    },
    "inputSet": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "globs"],
      "properties": {
        "id": {"type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$"},
        "globs": {
          "type": "array",
          "minItems": 1,
          "items": {"type": "string", "minLength": 1}
        }
      }
    },
    "outputSet": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "class", "paths"],
      "properties": {
        "id": {"type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$"},
        "class": {"enum": ["temporary", "rebuildable-cache", "retained-evidence"]},
        "paths": {
          "type": "array",
          "minItems": 1,
          "items": {"type": "string", "minLength": 1}
        }
      }
    },
    "gate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "entrypoint", "sourceSha256", "executionIdentitySha256", "inputIdentitySha256", "boundaryClass", "kind", "inputs", "outputs", "dependencies", "profile", "expectedDurationSeconds", "diskBudgetMiB", "network", "platforms", "tools", "sharding", "compilation", "readOnly"],
      "properties": {
        "id": {"type": "string", "pattern": "^gate/[a-z0-9][a-z0-9-]*$"},
        "entrypoint": {"$ref": "#/$defs/repoPath"},
        "sourceSha256": {"$ref": "#/$defs/sha256"},
        "executionIdentitySha256": {"$ref": "#/$defs/sha256"},
        "inputIdentitySha256": {"$ref": "#/$defs/sha256"},
        "boundaryClass": {"enum": ["static", "build-runtime", "benchmark", "aggregate"]},
        "kind": {"enum": ["static", "build", "test", "benchmark", "proof", "release-only"]},
        "inputs": {
          "type": "object",
          "additionalProperties": false,
          "required": ["sets", "paths"],
          "properties": {
            "sets": {"$ref": "#/$defs/stringList"},
            "paths": {"$ref": "#/$defs/pathList"}
          }
        },
        "outputs": {
          "type": "object",
          "additionalProperties": false,
          "required": ["sets", "retainedPaths", "repositoryWrites"],
          "properties": {
            "sets": {"$ref": "#/$defs/stringList"},
            "retainedPaths": {"$ref": "#/$defs/pathList"},
            "repositoryWrites": {"const": false}
          }
        },
        "dependencies": {"$ref": "#/$defs/pathList"},
        "profile": {"type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$"},
        "expectedDurationSeconds": {"type": "integer", "minimum": 1},
        "diskBudgetMiB": {"type": "integer", "minimum": 1},
        "network": {
          "type": "object",
          "additionalProperties": false,
          "required": ["mode", "declaredInputs"],
          "properties": {
            "mode": {"enum": ["deny", "loopback-only", "external-optional", "external-required"]},
            "declaredInputs": {"$ref": "#/$defs/stringList"}
          }
        },
        "platforms": {"$ref": "#/$defs/stringList"},
        "tools": {"$ref": "#/$defs/stringList"},
        "sharding": {
          "type": "object",
          "additionalProperties": false,
          "required": ["mode", "maxShards", "isolation"],
          "properties": {
            "mode": {"enum": ["none", "gate-level", "dependency-dag", "workspace-crate"]},
            "maxShards": {"type": "integer", "minimum": 0},
            "isolation": {"enum": ["shared-read-only", "shared-cargo-cache", "isolated-worktree-and-cache"]}
          }
        },
        "compilation": {"type": "boolean"},
        "readOnly": {"const": true}
      }
    },
    "repoPath": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)+$"
    },
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    },
    "stringList": {
      "type": "array",
      "items": {"type": "string", "minLength": 1}
    },
    "pathList": {
      "type": "array",
      "items": {"$ref": "#/$defs/repoPath"}
    }
  }
}
