{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/audit/standard-audit-event.v1.schema.json",
  "title": "RootBlocks Standard Audit Event v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "auditEventId",
    "operationId",
    "occurredAt",
    "category",
    "operation",
    "phase",
    "result",
    "severity",
    "enforcementMode",
    "actor",
    "tool",
    "standard",
    "auditMode",
    "correlation",
    "redaction"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.standard-audit-event/v1"
    },
    "auditEventId": {
      "type": "string",
      "pattern": "^audit-event-[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "operationId": {
      "type": "string",
      "pattern": "^op-[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "occurredAt": {
      "type": "string",
      "format": "date-time"
    },
    "category": {
      "enum": ["validation", "packaging", "signing", "release", "distribution", "cache", "consumer", "organization", "compatibility"]
    },
    "operation": {
      "enum": [
        "validate",
        "package",
        "sign",
        "release",
        "publish",
        "download",
        "verify",
        "cache-refresh",
        "cache-update",
        "consumer-use",
        "organization-sync",
        "latest-compatible",
        "compat-check"
      ]
    },
    "phase": {
      "enum": ["started", "completed", "failed", "blocked", "cancelled"]
    },
    "result": {
      "enum": ["started", "passed", "failed", "blocked", "cancelled", "warn"]
    },
    "severity": {
      "enum": ["info", "warning", "error", "critical"]
    },
    "enforcementMode": {
      "enum": ["advisory", "enforced"]
    },
    "actor": {
      "$ref": "#/$defs/actor"
    },
    "tool": {
      "$ref": "#/$defs/tool"
    },
    "standard": {
      "anyOf": [
        { "$ref": "#/$defs/standardRef" },
        { "type": "null" }
      ]
    },
    "consumer": {
      "$ref": "#/$defs/consumer"
    },
    "target": {
      "$ref": "#/$defs/target"
    },
    "auditMode": {
      "$ref": "#/$defs/auditMode"
    },
    "correlation": {
      "$ref": "#/$defs/correlation"
    },
    "summary": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500
    },
    "error": {
      "$ref": "#/$defs/error"
    },
    "redaction": {
      "$ref": "#/$defs/redaction"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "phase": {
            "const": "started"
          }
        },
        "required": ["phase"]
      },
      "then": {
        "properties": {
          "result": {
            "const": "started"
          },
          "severity": {
            "enum": ["info", "warning"]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "phase": {
            "const": "completed"
          }
        },
        "required": ["phase"]
      },
      "then": {
        "properties": {
          "result": {
            "enum": ["passed", "warn"]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "phase": {
            "enum": ["failed", "blocked"]
          }
        },
        "required": ["phase"]
      },
      "then": {
        "required": ["error"],
        "properties": {
          "result": {
            "enum": ["failed", "blocked"]
          },
          "severity": {
            "enum": ["error", "critical"]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "phase": {
            "const": "cancelled"
          }
        },
        "required": ["phase"]
      },
      "then": {
        "properties": {
          "result": {
            "const": "cancelled"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "enforcementMode": {
            "const": "enforced"
          },
          "operation": {
            "enum": ["sign", "release", "publish", "download", "verify", "cache-refresh", "cache-update", "consumer-use"]
          }
        },
        "required": ["enforcementMode", "operation"]
      },
      "then": {
        "properties": {
          "auditMode": {
            "properties": {
              "mandatory": {
                "const": true
              },
              "failureBehavior": {
                "const": "block-operation"
              }
            },
            "required": ["mandatory", "failureBehavior"]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "redaction": {
            "properties": {
              "status": {
                "const": "failed"
              }
            },
            "required": ["status"]
          }
        },
        "required": ["redaction"]
      },
      "then": {
        "properties": {
          "result": {
            "enum": ["failed", "blocked"]
          }
        }
      }
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "hash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64,128}$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@-]+$"
    },
    "actor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "id"],
      "properties": {
        "type": {
          "enum": ["human", "agent", "tool", "system"]
        },
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 160
        }
      }
    },
    "tool": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "version"],
      "properties": {
        "name": {
          "enum": ["rbstd-validate", "standard-golden", "standard-packager", "standard-signer", "standard-compat", "rootblocks-console", "builderx", "agent", "custom"]
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "runId": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "standardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "bundleKind", "version", "manifestHash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "bundleKind": {
          "enum": [
            "rootblocks-global-standard",
            "organization-component-extension",
            "project-component-extension",
            "personal-component-extension"
          ]
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestHash": {
          "$ref": "#/$defs/hash"
        },
        "bundleHash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "consumer": {
      "type": "object",
      "additionalProperties": false,
      "required": ["consumerName", "version"],
      "properties": {
        "consumerName": {
          "enum": ["rootblocks-console", "darkfactory", "builderx", "rbstd", "agent", "custom"]
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "operationMode": {
          "enum": ["generate", "validate", "compile", "run", "debug", "package", "sign", "release"]
        }
      }
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": ["kind", "id"],
      "properties": {
        "kind": {
          "enum": ["standard-bundle", "component-extension", "organization-registry", "cache-entry", "release-candidate", "release-evidence", "consumer-operation", "distribution-request"]
        },
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "path": {
          "$ref": "#/$defs/path"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "auditMode": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mandatory", "failureBehavior"],
      "properties": {
        "mandatory": {
          "type": "boolean"
        },
        "failureBehavior": {
          "enum": ["block-operation", "warn", "record-only"]
        }
      }
    },
    "correlation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["evidenceRefs"],
      "properties": {
        "evidenceRefs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/path"
          }
        },
        "releaseEvidenceRef": {
          "$ref": "#/$defs/path"
        },
        "cacheTransactionId": {
          "type": "string",
          "pattern": "^cache-update-[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "distributionRequestId": {
          "type": "string",
          "pattern": "^(dist-request|download|component-sync)-[a-z0-9][a-z0-9.-]{2,127}$"
        },
        "traceId": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "error": {
      "type": "object",
      "additionalProperties": false,
      "required": ["code", "message"],
      "properties": {
        "code": {
          "enum": [
            "VALIDATION_FAILED",
            "SIGNATURE_INVALID",
            "HASH_MISMATCH",
            "COMPATIBILITY_BLOCK",
            "CACHE_QUARANTINED",
            "DISTRIBUTION_UNAVAILABLE",
            "AUDIT_EMIT_FAILED",
            "REDACTION_FAILED",
            "OPERATION_CANCELLED",
            "UNKNOWN"
          ]
        },
        "message": {
          "type": "string",
          "minLength": 1,
          "maxLength": 500
        }
      }
    },
    "redaction": {
      "type": "object",
      "additionalProperties": false,
      "required": ["status", "rules", "rawPayloadIncluded", "secretsIncluded", "sensitiveContentIncluded"],
      "properties": {
        "status": {
          "enum": ["applied", "not-needed", "failed"]
        },
        "rules": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["no-secrets-in-standards", "evidence-redaction-gate-rules", "trace-redaction-v1", "private-component-content-redaction", "not-needed"]
          },
          "uniqueItems": true
        },
        "rawPayloadIncluded": {
          "const": false
        },
        "secretsIncluded": {
          "const": false
        },
        "sensitiveContentIncluded": {
          "const": false
        }
      }
    }
  }
}
