{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:agent-reliability:schema:incident:1.0.0",
  "title": "Vendor-neutral reliability incident",
  "description": "A normalized mismatch between intended, apparent, and actual outcomes, including recurrence and recovery burden.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "object_type",
    "incident_id",
    "status",
    "intended_outcome",
    "apparent_outcome",
    "actual_outcome",
    "escape_behavior",
    "failure_class",
    "selector",
    "detection",
    "recurrence",
    "recovery",
    "affected",
    "evidence_ids",
    "provenance"
  ],
  "properties": {
    "schema_version": { "const": "1.0.0" },
    "object_type": { "const": "incident" },
    "incident_id": { "type": "string", "minLength": 1 },
    "status": { "enum": ["open", "resolved", "recurring", "unknown"] },
    "intended_outcome": { "type": "string", "minLength": 1 },
    "apparent_outcome": { "type": "string", "minLength": 1 },
    "actual_outcome": { "type": "string", "minLength": 1 },
    "escape_behavior": { "type": "string", "minLength": 1 },
    "failure_class": { "type": "string", "minLength": 1 },
    "selector": {
      "type": "object",
      "additionalProperties": false,
      "required": ["selector_id", "selection_basis"],
      "properties": {
        "selector_id": { "type": "string", "minLength": 1 },
        "selection_basis": { "type": "string", "minLength": 1 }
      }
    },
    "detection": {
      "type": "object",
      "additionalProperties": false,
      "required": ["detected_at", "source", "evidence_ids"],
      "properties": {
        "detected_at": { "type": "string", "format": "date-time" },
        "source": { "type": "string", "minLength": 1 },
        "evidence_ids": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "recurrence": {
      "type": "object",
      "additionalProperties": false,
      "required": ["count", "first_seen_at", "last_seen_at", "occurrence_ids"],
      "properties": {
        "count": { "type": "integer", "minimum": 1 },
        "first_seen_at": { "type": "string", "format": "date-time" },
        "last_seen_at": { "type": "string", "format": "date-time" },
        "occurrence_ids": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": { "type": "string", "minLength": 1 }
        },
        "basis": { "type": "string", "minLength": 1 }
      }
    },
    "recovery": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "action", "burden"],
      "properties": {
        "status": { "enum": ["not_started", "planned", "in_progress", "recovered", "unknown"] },
        "action": { "type": "string", "minLength": 1 },
        "recovered_at": { "type": ["string", "null"], "format": "date-time" },
        "burden": { "$ref": "#/$defs/burden" }
      },
      "allOf": [
        {
          "if": { "properties": { "status": { "const": "recovered" } }, "required": ["status"] },
          "then": {
            "required": ["recovered_at"],
            "properties": { "recovered_at": { "type": "string" } }
          }
        }
      ]
    },
    "affected": {
      "type": "object",
      "additionalProperties": false,
      "required": ["repositories", "harness_adapters", "models", "environments"],
      "properties": {
        "repositories": { "type": "array", "uniqueItems": true, "items": { "type": "string", "minLength": 1 } },
        "harness_adapters": { "type": "array", "uniqueItems": true, "items": { "type": "string", "minLength": 1 } },
        "models": { "type": "array", "uniqueItems": true, "items": { "type": "string", "minLength": 1 } },
        "environments": { "type": "array", "uniqueItems": true, "items": { "type": "string", "minLength": 1 } }
      }
    },
    "evidence_ids": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": { "type": "string", "minLength": 1 }
    },
    "provenance": { "$ref": "#/$defs/provenance" }
  },
  "$defs": {
    "measurement": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["availability", "value", "unit"],
          "properties": {
            "availability": { "const": "measured" },
            "value": { "type": "number" },
            "unit": { "type": "string", "minLength": 1 }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["availability", "unit", "reason"],
          "properties": {
            "availability": { "enum": ["unavailable", "not_applicable", "insufficient_data"] },
            "unit": { "type": "string", "minLength": 1 },
            "reason": { "type": "string", "minLength": 1 }
          },
          "not": { "required": ["value"] }
        }
      ]
    },
    "namedMeasurement": {
      "type": "object",
      "additionalProperties": false,
      "required": ["metric", "measurement"],
      "properties": {
        "metric": { "type": "string", "minLength": 1 },
        "measurement": { "$ref": "#/$defs/measurement" }
      }
    },
    "burden": {
      "type": "object",
      "additionalProperties": false,
      "required": ["measurements"],
      "properties": {
        "measurements": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/namedMeasurement" } }
      }
    },
    "sourceArtifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["uri", "digest"],
      "properties": {
        "uri": { "type": "string", "minLength": 1 },
        "digest": {
          "type": "string",
          "pattern": "^sha256:[a-f0-9]{64}$",
          "description": "SHA-256 of the exact source bytes addressed by uri and revision, without local line-ending or encoding normalization."
        },
        "revision": { "type": "string", "minLength": 1 },
        "media_type": { "type": "string", "minLength": 1 },
        "record_locator": { "type": "string", "minLength": 1 }
      }
    },
    "provenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_system", "source_record_id", "source_class", "source_artifacts"],
      "properties": {
        "source_system": { "type": "string", "minLength": 1 },
        "source_record_id": { "type": "string", "minLength": 1 },
        "source_class": { "enum": ["production", "historical", "synthetic", "fixture", "unknown"] },
        "mapped_at": { "type": "string", "format": "date-time" },
        "source_artifacts": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/sourceArtifact" } },
        "external_ids": { "type": "object", "additionalProperties": { "type": "string", "minLength": 1 } }
      }
    }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "const": "recurring" } }, "required": ["status"] },
      "then": { "properties": { "recurrence": { "properties": { "count": { "minimum": 2 } } } } }
    },
    {
      "if": { "properties": { "status": { "const": "resolved" } }, "required": ["status"] },
      "then": {
        "properties": {
          "recovery": {
            "required": ["recovered_at"],
            "properties": {
              "status": { "const": "recovered" },
              "recovered_at": { "type": "string" }
            }
          }
        }
      }
    }
  ]
}
