{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rodgui.github.io/ai-agent-governance-framework/schemas/agent-registry.schema.json",
  "title": "AI Agent Registry Record 2.0",
  "description": "Registro canônico de discovery, lifecycle, estado operacional, ownership, risco, admissibilidade e evidências de um agente.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "agentId",
    "name",
    "purpose",
    "ownership",
    "discovery",
    "lifecycle",
    "risk",
    "currentBlueprint",
    "platforms",
    "lastReviewed"
  ],
  "properties": {
    "schemaVersion": {
      "const": "2.0"
    },
    "agentId": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9._-]{2,63}$"
    },
    "name": {
      "type": "string",
      "minLength": 3,
      "maxLength": 160
    },
    "description": {
      "type": "string",
      "maxLength": 2000
    },
    "purpose": {
      "type": "string",
      "minLength": 10,
      "maxLength": 1000
    },
    "intendedUsers": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "string",
        "minLength": 2
      },
      "uniqueItems": true
    },
    "prohibitedUses": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 3
      },
      "uniqueItems": true
    },
    "ownership": {
      "$ref": "#/$defs/ownership"
    },
    "lifecycle": {
      "$ref": "#/$defs/lifecycle"
    },
    "risk": {
      "$ref": "#/$defs/risk"
    },
    "currentBlueprint": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "version",
        "path"
      ],
      "properties": {
        "version": {
          "type": "string",
          "pattern": "^[0-9]+\\.[0-9]+(?:\\.[0-9]+)?$"
        },
        "path": {
          "type": "string",
          "minLength": 3
        },
        "sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      }
    },
    "platforms": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "name",
          "environment"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 2
          },
          "environment": {
            "enum": [
              "development",
              "test",
              "staging",
              "production"
            ]
          },
          "tenant": {
            "type": "string"
          },
          "region": {
            "type": "string"
          },
          "externalId": {
            "type": "string"
          }
        }
      }
    },
    "capabilities": {
      "type": "array",
      "items": {
        "enum": [
          "observe",
          "create",
          "modify",
          "execute",
          "approve",
          "delete",
          "delegate"
        ]
      },
      "uniqueItems": true
    },
    "evidenceLinks": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/evidenceLink"
      }
    },
    "attestation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "expiresAt"
      ],
      "properties": {
        "status": {
          "enum": [
            "valid",
            "conditional",
            "expired",
            "not-required"
          ]
        },
        "attestedAt": {
          "type": "string",
          "format": "date"
        },
        "expiresAt": {
          "type": "string",
          "format": "date"
        },
        "decisionRef": {
          "type": "string"
        }
      }
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string",
        "pattern": "^[a-z0-9][a-z0-9-]{0,39}$"
      },
      "uniqueItems": true
    },
    "lastReviewed": {
      "type": "string",
      "format": "date"
    },
    "discovery": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "confidence",
        "firstSeenAt",
        "lastSeenAt",
        "signals"
      ],
      "properties": {
        "status": {
          "enum": [
            "confirmed",
            "probable",
            "suspected"
          ]
        },
        "confidence": {
          "enum": [
            "low",
            "medium",
            "high"
          ]
        },
        "firstSeenAt": {
          "type": "string",
          "format": "date-time"
        },
        "lastSeenAt": {
          "type": "string",
          "format": "date-time"
        },
        "signals": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "system",
              "signalType",
              "observedAt",
              "evidenceRef"
            ],
            "properties": {
              "system": {
                "type": "string",
                "minLength": 2
              },
              "signalType": {
                "enum": [
                  "control-plane",
                  "identity",
                  "network",
                  "repository",
                  "procurement",
                  "declaration",
                  "other"
                ]
              },
              "observedAt": {
                "type": "string",
                "format": "date-time"
              },
              "evidenceRef": {
                "type": "string",
                "minLength": 3
              }
            }
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "anyOf": [
          {
            "properties": {
              "lifecycle": {
                "properties": {
                  "stage": {
                    "enum": [
                      "approved",
                      "production",
                      "retirement-review"
                    ]
                  }
                },
                "required": [
                  "stage"
                ]
              }
            }
          },
          {
            "properties": {
              "platforms": {
                "contains": {
                  "properties": {
                    "environment": {
                      "const": "production"
                    }
                  },
                  "required": [
                    "environment"
                  ]
                }
              }
            }
          }
        ]
      },
      "then": {
        "required": [
          "attestation",
          "evidenceLinks"
        ],
        "properties": {
          "evidenceLinks": {
            "minItems": 1
          },
          "attestation": {
            "required": [
              "status",
              "attestedAt",
              "expiresAt",
              "decisionRef"
            ],
            "properties": {
              "status": {
                "enum": [
                  "valid",
                  "conditional"
                ]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "lifecycle": {
            "properties": {
              "stage": {
                "enum": [
                  "approved",
                  "production",
                  "retirement-review"
                ]
              }
            },
            "required": [
              "stage"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "lifecycle": {
            "required": [
              "approvedAt"
            ]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "lifecycle": {
            "properties": {
              "stage": {
                "const": "production"
              }
            },
            "required": [
              "stage"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "lifecycle": {
            "properties": {
              "operationalState": {
                "enum": [
                  "enabled",
                  "suspended",
                  "quarantined"
                ]
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "lifecycle": {
            "properties": {
              "stage": {
                "enum": [
                  "retired",
                  "archived"
                ]
              }
            },
            "required": [
              "stage"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "lifecycle": {
            "properties": {
              "operationalState": {
                "const": "disabled"
              }
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "platforms": {
            "contains": {
              "properties": {
                "environment": {
                  "const": "production"
                }
              },
              "required": [
                "environment"
              ]
            }
          }
        }
      },
      "then": {
        "properties": {
          "risk": {
            "properties": {
              "admissibility": {
                "enum": [
                  "permitted",
                  "conditional",
                  "restricted"
                ]
              }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "owner": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "role"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 2
        },
        "role": {
          "type": "string",
          "minLength": 2
        },
        "contact": {
          "type": "string",
          "maxLength": 200
        },
        "organizationUnit": {
          "type": "string"
        }
      }
    },
    "ownership": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "businessOwner",
        "technicalOwner"
      ],
      "properties": {
        "businessOwner": {
          "$ref": "#/$defs/owner"
        },
        "technicalOwner": {
          "$ref": "#/$defs/owner"
        },
        "runOwner": {
          "$ref": "#/$defs/owner"
        },
        "dataOwners": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/owner"
          }
        }
      }
    },
    "lifecycle": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "stage",
        "operationalState",
        "createdAt",
        "enteredStageAt",
        "statusReason",
        "transitionHistory"
      ],
      "properties": {
        "stage": {
          "enum": [
            "discovered",
            "draft",
            "under-review",
            "approved",
            "production",
            "retirement-review",
            "retired",
            "archived"
          ]
        },
        "operationalState": {
          "enum": [
            "not-deployed",
            "enabled",
            "suspended",
            "quarantined",
            "disabled"
          ]
        },
        "createdAt": {
          "type": "string",
          "format": "date"
        },
        "enteredStageAt": {
          "type": "string",
          "format": "date-time"
        },
        "approvedAt": {
          "type": "string",
          "format": "date"
        },
        "sunsetBy": {
          "type": "string",
          "format": "date"
        },
        "statusReason": {
          "type": "string",
          "minLength": 5,
          "maxLength": 1000
        },
        "transitionHistory": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/lifecycleTransition"
          }
        }
      }
    },
    "risk": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "tier",
        "admissibility",
        "admissibilityRationale",
        "assessmentRef",
        "classifiedAt"
      ],
      "properties": {
        "tier": {
          "enum": [
            "T1",
            "T2",
            "T3",
            "T4"
          ]
        },
        "admissibility": {
          "enum": [
            "permitted",
            "conditional",
            "restricted",
            "prohibited"
          ]
        },
        "admissibilityRationale": {
          "type": "string",
          "minLength": 10,
          "maxLength": 1000
        },
        "assessmentRef": {
          "type": "string",
          "minLength": 3
        },
        "classifiedAt": {
          "type": "string",
          "format": "date"
        },
        "redFlags": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 3
          },
          "uniqueItems": true
        },
        "residualRisk": {
          "enum": [
            "low",
            "moderate",
            "high",
            "critical",
            "unknown"
          ]
        },
        "conditionRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "minLength": 3
          },
          "uniqueItems": true
        },
        "exceptionRef": {
          "type": "string",
          "minLength": 3
        },
        "exceptionExpiresAt": {
          "type": "string",
          "format": "date"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "admissibility": {
                "const": "conditional"
              }
            },
            "required": [
              "admissibility"
            ]
          },
          "then": {
            "required": [
              "conditionRefs"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "admissibility": {
                "const": "restricted"
              }
            },
            "required": [
              "admissibility"
            ]
          },
          "then": {
            "required": [
              "exceptionRef",
              "exceptionExpiresAt"
            ]
          }
        }
      ]
    },
    "evidenceLink": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "type",
        "path"
      ],
      "properties": {
        "type": {
          "enum": [
            "assessment",
            "control",
            "evaluation",
            "approval",
            "incident",
            "attestation",
            "sunset"
          ]
        },
        "path": {
          "type": "string",
          "minLength": 3
        },
        "sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        }
      }
    },
    "lifecycleTransition": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "transitionType",
        "fromStage",
        "toStage",
        "fromOperationalState",
        "toOperationalState",
        "occurredAt",
        "authority",
        "reason",
        "evidenceRef"
      ],
      "properties": {
        "transitionType": {
          "enum": [
            "stage",
            "operational",
            "both"
          ]
        },
        "fromStage": {
          "enum": [
            null,
            "discovered",
            "draft",
            "under-review",
            "approved",
            "production",
            "retirement-review",
            "retired",
            "archived"
          ]
        },
        "toStage": {
          "enum": [
            "discovered",
            "draft",
            "under-review",
            "approved",
            "production",
            "retirement-review",
            "retired",
            "archived"
          ]
        },
        "fromOperationalState": {
          "enum": [
            null,
            "not-deployed",
            "enabled",
            "suspended",
            "quarantined",
            "disabled"
          ]
        },
        "toOperationalState": {
          "enum": [
            "not-deployed",
            "enabled",
            "suspended",
            "quarantined",
            "disabled"
          ]
        },
        "occurredAt": {
          "type": "string",
          "format": "date-time"
        },
        "authority": {
          "type": "string",
          "minLength": 3
        },
        "reason": {
          "type": "string",
          "minLength": 5,
          "maxLength": 1000
        },
        "evidenceRef": {
          "type": "string",
          "minLength": 3
        }
      }
    }
  }
}
