{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://hogarmas.net/agent-drift/schema.json",
  "title": "H/M Agent Drift export packet",
  "type": "object",
  "additionalProperties": false,
  "required": ["schema", "ruleset", "generatedAt", "input", "comparison", "limitations"],
  "properties": {
    "schema": { "const": "hm.agent-drift.v1" },
    "ruleset": { "type": "string" },
    "generatedAt": { "type": "string", "format": "date-time" },
    "input": {
      "type": "object",
      "additionalProperties": false,
      "required": ["baselineSha256", "candidateSha256", "baselineSchema", "candidateSchema", "sample", "rawReportsIncluded", "sourcePayloadsIncluded", "sourceFileNamesIncluded", "toolNamesIncluded"],
      "properties": {
        "baselineSha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "candidateSha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "baselineSchema": { "enum": ["hm.agent-scope.v1", "hm.agent-risk-atlas.v1"] },
        "candidateSchema": { "enum": ["hm.agent-scope.v1", "hm.agent-risk-atlas.v1"] },
        "sample": { "type": "boolean" },
        "rawReportsIncluded": { "const": false },
        "sourcePayloadsIncluded": { "const": false },
        "sourceFileNamesIncluded": { "const": false },
        "toolNamesIncluded": { "const": false }
      }
    },
    "comparison": { "$ref": "#/$defs/comparison" },
    "limitations": { "$ref": "#/$defs/limitations" }
  },
  "$defs": {
    "comparison": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "ruleset", "decision", "measurement", "summary", "changes", "gates", "limitations"],
      "properties": {
        "schema": { "const": "hm.agent-drift.comparison.v1" },
        "ruleset": { "type": "string" },
        "decision": { "enum": ["review-required", "change-observed", "no-observed-drift"] },
        "measurement": {
          "type": "object",
          "additionalProperties": false,
          "required": ["baseline", "candidate", "sourceSchemaChanged", "sourceMethodChanged", "sourceRulesetChanged", "comparableAuthorityModel"],
          "properties": {
            "baseline": { "$ref": "#/$defs/source" },
            "candidate": { "$ref": "#/$defs/source" },
            "sourceSchemaChanged": { "type": "boolean" },
            "sourceMethodChanged": { "type": "boolean" },
            "sourceRulesetChanged": { "type": "boolean" },
            "comparableAuthorityModel": { "type": "boolean" }
          }
        },
        "summary": {
          "type": "object",
          "additionalProperties": false,
          "required": ["changeCount", "violatedGates", "reviewPriorityPoints", "addedCapabilities", "removedCapabilities", "newPaths", "resolvedPaths", "newCriticalPaths", "removedControls", "coverageRegressions", "baselineSeverity", "candidateSeverity", "observedSeverityTrend", "toolCountDelta", "isolateToolsDelta", "criticalToolsDelta", "highToolsDelta"],
          "properties": {
            "changeCount": { "type": "integer", "minimum": 0 },
            "violatedGates": { "type": "integer", "minimum": 0 },
            "reviewPriorityPoints": { "type": "integer", "minimum": 0, "description": "A triage ordering heuristic, not a risk score or security rating." },
            "addedCapabilities": { "type": "integer", "minimum": 0 },
            "removedCapabilities": { "type": "integer", "minimum": 0 },
            "newPaths": { "type": "integer", "minimum": 0 },
            "resolvedPaths": { "type": "integer", "minimum": 0 },
            "newCriticalPaths": { "type": "integer", "minimum": 0 },
            "removedControls": { "type": "integer", "minimum": 0 },
            "coverageRegressions": { "type": "integer", "minimum": 0 },
            "baselineSeverity": { "$ref": "#/$defs/severity" },
            "candidateSeverity": { "$ref": "#/$defs/severity" },
            "observedSeverityTrend": { "enum": ["increased", "decreased", "unchanged"] },
            "toolCountDelta": { "$ref": "#/$defs/nullableInteger" },
            "isolateToolsDelta": { "$ref": "#/$defs/nullableInteger" },
            "criticalToolsDelta": { "$ref": "#/$defs/nullableInteger" },
            "highToolsDelta": { "$ref": "#/$defs/nullableInteger" }
          }
        },
        "changes": {
          "type": "object",
          "additionalProperties": false,
          "required": ["addedCapabilities", "removedCapabilities", "addedSensitiveCapabilities", "addedControls", "removedControls", "newPaths", "resolvedPaths", "coverageRegressions", "coverageImprovements"],
          "properties": {
            "addedCapabilities": { "$ref": "#/$defs/idList" },
            "removedCapabilities": { "$ref": "#/$defs/idList" },
            "addedSensitiveCapabilities": { "$ref": "#/$defs/idList" },
            "addedControls": { "$ref": "#/$defs/idList" },
            "removedControls": { "$ref": "#/$defs/idList" },
            "newPaths": { "type": "array", "items": { "$ref": "#/$defs/path" } },
            "resolvedPaths": { "type": "array", "items": { "$ref": "#/$defs/path" } },
            "coverageRegressions": { "type": "array", "items": { "$ref": "#/$defs/coverageChange" } },
            "coverageImprovements": { "type": "array", "items": { "$ref": "#/$defs/coverageChange" } }
          }
        },
        "gates": { "type": "array", "items": { "$ref": "#/$defs/gate" } },
        "limitations": { "$ref": "#/$defs/limitations" }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema", "ruleset", "method", "sample", "toolCount", "isolateTools"],
      "properties": {
        "schema": { "enum": ["hm.agent-scope.v1", "hm.agent-risk-atlas.v1"] },
        "ruleset": { "type": "string" },
        "method": { "enum": ["declared-risk-atlas-profile", "inferred-from-agent-scope"] },
        "sample": { "type": "boolean" },
        "toolCount": { "$ref": "#/$defs/nullableCount" },
        "isolateTools": { "$ref": "#/$defs/nullableCount" }
      }
    },
    "path": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "severity", "controlState", "missingControls"],
      "properties": {
        "id": { "type": "string" },
        "severity": { "$ref": "#/$defs/severity" },
        "controlState": { "enum": ["uncovered", "partial", "declared-covered"] },
        "missingControls": { "$ref": "#/$defs/idList" }
      }
    },
    "coverageChange": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "severity", "from", "to", "missingControlsAdded", "missingControlsRemoved"],
      "properties": {
        "id": { "type": "string" },
        "severity": { "$ref": "#/$defs/severity" },
        "from": { "enum": ["uncovered", "partial", "declared-covered"] },
        "to": { "enum": ["uncovered", "partial", "declared-covered"] },
        "missingControlsAdded": { "$ref": "#/$defs/idList" },
        "missingControlsRemoved": { "$ref": "#/$defs/idList" }
      }
    },
    "gate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "status", "evidenceCount"],
      "properties": {
        "id": { "enum": ["no-new-critical-paths", "no-sensitive-authority-expansion", "no-control-removal", "no-coverage-regression", "no-isolation-growth", "same-measurement-method"] },
        "status": { "enum": ["violated", "not-observed", "not-evaluable"] },
        "evidenceCount": { "type": "integer", "minimum": 0 }
      }
    },
    "limitations": {
      "type": "object",
      "additionalProperties": false,
      "required": ["runtimeObserved", "controlsVerified", "sourceIntegrityVerified", "deploymentIdentityVerified", "securityVerdictProduced", "completenessGuaranteed", "sourceDataUploaded", "toolExecutionPerformed"],
      "properties": {
        "runtimeObserved": { "const": false },
        "controlsVerified": { "const": false },
        "sourceIntegrityVerified": { "const": false },
        "deploymentIdentityVerified": { "const": false },
        "securityVerdictProduced": { "const": false },
        "completenessGuaranteed": { "const": false },
        "sourceDataUploaded": { "const": false },
        "toolExecutionPerformed": { "const": false }
      }
    },
    "severity": { "enum": ["critical", "high", "elevated", "watch", "no-known-path"] },
    "idList": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "nullableInteger": { "type": ["integer", "null"] },
    "nullableCount": { "type": ["integer", "null"], "minimum": 0 }
  }
}
