{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "provenancecode.mistake.v1",
  "title": "ProvenanceCode Mistake Record (MR v1.0)",
  "description": "Schema for MR (Mistake Record) artifacts — documents a mistake, root cause, fix, and derived prevention rule. Closes the formal schema gap for provenancecode.mistake@1.0.",
  "type": "object",
  "required": [
    "schema",
    "mr_id",
    "repo",
    "ref",
    "title",
    "status",
    "created_at",
    "severity",
    "failure_type",
    "scope",
    "trigger",
    "impact",
    "root_cause",
    "fix",
    "prevent_rule",
    "links"
  ],
  "additionalProperties": true,
  "properties": {
    "schema": {
      "type": "string",
      "enum": ["provenancecode.mistake@1.0", "provenancecode.mistake.v1"]
    },
    "mr_id": {
      "type": "string",
      "pattern": "^MR-([A-Z0-9]{2,8}(-[A-Z0-9]{2,8})?-[0-9]{6}|[0-9]{6})$"
    },
    "repo": {
      "type": "object",
      "required": ["org", "name"],
      "additionalProperties": true,
      "properties": {
        "org": { "type": "string" },
        "name": { "type": "string" }
      }
    },
    "ref": {
      "type": "string",
      "pattern": "^pc:\\/\\/"
    },
    "title": {
      "type": "string",
      "minLength": 8,
      "maxLength": 200
    },
    "status": {
      "type": "string",
      "enum": ["open", "resolved", "wont_fix", "duplicate"]
    },
    "created_at": {
      "type": "string",
      "format": "date-time"
    },
    "updated_at": {
      "type": ["string", "null"],
      "format": "date-time"
    },
    "severity": {
      "type": "string",
      "enum": ["low", "medium", "high", "critical"]
    },
    "failure_type": {
      "type": "string",
      "minLength": 1,
      "description": "Category of mistake (e.g. schema-drift, missing-validation, security-gap, data-loss)"
    },
    "scope": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "paths": {
          "type": "array",
          "items": { "type": "string" }
        },
        "components": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "trigger": {
      "type": "string",
      "minLength": 1,
      "description": "What caused the mistake to occur or be discovered"
    },
    "impact": {
      "type": "string",
      "minLength": 1,
      "description": "Observed or potential impact"
    },
    "root_cause": {
      "type": "string",
      "minLength": 1
    },
    "fix": {
      "type": "object",
      "required": ["description"],
      "additionalProperties": true,
      "properties": {
        "description": { "type": "string", "minLength": 1 },
        "pr_url": { "type": ["string", "null"] },
        "commit_sha": { "type": ["string", "null"] }
      }
    },
    "prevent_rule": {
      "type": "object",
      "required": ["rule_id", "statement"],
      "additionalProperties": true,
      "properties": {
        "rule_id": {
          "type": "string",
          "minLength": 1,
          "description": "Stable rule ID derived from MR ID (e.g. PRV-MR-000001-require-mistake-guardrail-links)"
        },
        "statement": {
          "type": "string",
          "minLength": 10
        },
        "enforcement": {
          "type": "string",
          "enum": ["ci", "pr-gate", "ai-hint", "policy-rule", "none"]
        }
      }
    },
    "enforcement": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "enforced_in_ci": { "type": "boolean" },
        "ai_hints": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "links": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "decisions": {
          "type": "array",
          "items": { "type": "string", "pattern": "^DEC-" }
        },
        "specs": {
          "type": "array",
          "items": { "type": "string", "pattern": "^SPEC-" }
        },
        "risks": {
          "type": "array",
          "items": { "type": "string", "pattern": "^RA-" }
        },
        "issues": {
          "type": "array",
          "items": { "type": "string" }
        },
        "pull_requests": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    }
  }
}
