| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Actors; |
| | | 3 | | using AsiBackbone.Core.Constraints; |
| | | 4 | | using AsiBackbone.Core.Decisions; |
| | | 5 | | using AsiBackbone.Core.Serialization; |
| | | 6 | | |
| | | 7 | | namespace AsiBackbone.Core.Audit; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents the framework-neutral audit residue produced by an AsiBackbone operation. |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class AuditResidue : IAsiBackboneAuditResidue |
| | | 13 | | { |
| | 7 | 14 | | private static readonly ReadOnlyCollection<string> EmptyReasonCodes = |
| | 7 | 15 | | Array.AsReadOnly(Array.Empty<string>()); |
| | | 16 | | |
| | 7 | 17 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 7 | 18 | | new ReadOnlyDictionary<string, string>( |
| | 7 | 19 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 20 | | |
| | 101 | 21 | | private AuditResidue( |
| | 101 | 22 | | string eventId, |
| | 101 | 23 | | string auditResidueId, |
| | 101 | 24 | | string schemaVersion, |
| | 101 | 25 | | DateTimeOffset occurredUtc, |
| | 101 | 26 | | string actorId, |
| | 101 | 27 | | AsiBackboneActorType actorType, |
| | 101 | 28 | | string? actorDisplayName, |
| | 101 | 29 | | string operationName, |
| | 101 | 30 | | string outcome, |
| | 101 | 31 | | IReadOnlyList<string> reasonCodes, |
| | 101 | 32 | | string? correlationId, |
| | 101 | 33 | | string? traceId, |
| | 101 | 34 | | string? spanId, |
| | 101 | 35 | | string? parentSpanId, |
| | 101 | 36 | | long? decisionLatencyMs, |
| | 101 | 37 | | string? constraintSetHash, |
| | 101 | 38 | | int? constraintCount, |
| | 101 | 39 | | double? riskScore, |
| | 101 | 40 | | string? policyScope, |
| | 101 | 41 | | string? tenantHash, |
| | 101 | 42 | | string? organizationHash, |
| | 101 | 43 | | string? emitterStatus, |
| | 101 | 44 | | string? emitterProvider, |
| | 101 | 45 | | long? outboxSequence, |
| | 101 | 46 | | string? gatewayExecutionId, |
| | 101 | 47 | | string? decisionStage, |
| | 101 | 48 | | string? policyVersion, |
| | 101 | 49 | | string? policyHash, |
| | 101 | 50 | | IReadOnlyDictionary<string, string> metadata) |
| | | 51 | | { |
| | 101 | 52 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventId); |
| | 101 | 53 | | ArgumentException.ThrowIfNullOrWhiteSpace(auditResidueId); |
| | 101 | 54 | | ArgumentException.ThrowIfNullOrWhiteSpace(actorId); |
| | 101 | 55 | | ArgumentException.ThrowIfNullOrWhiteSpace(operationName); |
| | 98 | 56 | | ArgumentException.ThrowIfNullOrWhiteSpace(outcome); |
| | | 57 | | |
| | 95 | 58 | | EventId = eventId.Trim(); |
| | 95 | 59 | | AuditResidueId = auditResidueId.Trim(); |
| | 95 | 60 | | SchemaVersion = AsiBackboneSchemaVersions.Normalize(schemaVersion); |
| | 95 | 61 | | OccurredUtc = occurredUtc.ToUniversalTime(); |
| | 95 | 62 | | ActorId = actorId.Trim(); |
| | 95 | 63 | | ActorType = actorType; |
| | 95 | 64 | | ActorDisplayName = NormalizeOptional(actorDisplayName); |
| | 95 | 65 | | OperationName = operationName.Trim(); |
| | 95 | 66 | | Outcome = outcome.Trim(); |
| | 95 | 67 | | ReasonCodes = reasonCodes; |
| | 95 | 68 | | CorrelationId = NormalizeOptional(correlationId); |
| | 95 | 69 | | TraceId = NormalizeOptional(traceId); |
| | 95 | 70 | | SpanId = NormalizeOptional(spanId); |
| | 95 | 71 | | ParentSpanId = NormalizeOptional(parentSpanId); |
| | 95 | 72 | | DecisionLatencyMs = NormalizeNonNegative(decisionLatencyMs, nameof(decisionLatencyMs)); |
| | 93 | 73 | | ConstraintSetHash = NormalizeOptional(constraintSetHash); |
| | 93 | 74 | | ConstraintCount = NormalizeNonNegative(constraintCount, nameof(constraintCount)); |
| | 91 | 75 | | RiskScore = NormalizeRiskScore(riskScore); |
| | 87 | 76 | | PolicyScope = NormalizeOptional(policyScope); |
| | 87 | 77 | | TenantHash = NormalizeOptional(tenantHash); |
| | 87 | 78 | | OrganizationHash = NormalizeOptional(organizationHash); |
| | 87 | 79 | | EmitterStatus = NormalizeOptional(emitterStatus); |
| | 87 | 80 | | EmitterProvider = NormalizeOptional(emitterProvider); |
| | 87 | 81 | | OutboxSequence = NormalizeNonNegative(outboxSequence, nameof(outboxSequence)); |
| | 85 | 82 | | GatewayExecutionId = NormalizeOptional(gatewayExecutionId); |
| | 85 | 83 | | DecisionStage = NormalizeOptional(decisionStage); |
| | 85 | 84 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 85 | 85 | | PolicyHash = NormalizeOptional(policyHash); |
| | 85 | 86 | | Metadata = metadata; |
| | 85 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc /> |
| | 67 | 90 | | public string EventId { get; } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | 56 | 93 | | public string AuditResidueId { get; } |
| | | 94 | | |
| | | 95 | | /// <inheritdoc /> |
| | 52 | 96 | | public string SchemaVersion { get; } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc /> |
| | 56 | 99 | | public DateTimeOffset OccurredUtc { get; } |
| | | 100 | | |
| | | 101 | | /// <inheritdoc /> |
| | 54 | 102 | | public string ActorId { get; } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc /> |
| | 51 | 105 | | public AsiBackboneActorType ActorType { get; } |
| | | 106 | | |
| | | 107 | | /// <inheritdoc /> |
| | 52 | 108 | | public string? ActorDisplayName { get; } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc /> |
| | 58 | 111 | | public string OperationName { get; } |
| | | 112 | | |
| | | 113 | | /// <inheritdoc /> |
| | 62 | 114 | | public string Outcome { get; } |
| | | 115 | | |
| | | 116 | | /// <inheritdoc /> |
| | 69 | 117 | | public IReadOnlyList<string> ReasonCodes { get; } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | 68 | 120 | | public string? CorrelationId { get; } |
| | | 121 | | |
| | | 122 | | /// <inheritdoc /> |
| | 65 | 123 | | public string? TraceId { get; } |
| | | 124 | | |
| | | 125 | | /// <inheritdoc /> |
| | 54 | 126 | | public string? SpanId { get; } |
| | | 127 | | |
| | | 128 | | /// <inheritdoc /> |
| | 54 | 129 | | public string? ParentSpanId { get; } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | 52 | 132 | | public long? DecisionLatencyMs { get; } |
| | | 133 | | |
| | | 134 | | /// <inheritdoc /> |
| | 52 | 135 | | public string? ConstraintSetHash { get; } |
| | | 136 | | |
| | | 137 | | /// <inheritdoc /> |
| | 52 | 138 | | public int? ConstraintCount { get; } |
| | | 139 | | |
| | | 140 | | /// <inheritdoc /> |
| | 52 | 141 | | public double? RiskScore { get; } |
| | | 142 | | |
| | | 143 | | /// <inheritdoc /> |
| | 52 | 144 | | public string? PolicyScope { get; } |
| | | 145 | | |
| | | 146 | | /// <inheritdoc /> |
| | 52 | 147 | | public string? TenantHash { get; } |
| | | 148 | | |
| | | 149 | | /// <inheritdoc /> |
| | 52 | 150 | | public string? OrganizationHash { get; } |
| | | 151 | | |
| | | 152 | | /// <inheritdoc /> |
| | 54 | 153 | | public string? EmitterStatus { get; } |
| | | 154 | | |
| | | 155 | | /// <inheritdoc /> |
| | 54 | 156 | | public string? EmitterProvider { get; } |
| | | 157 | | |
| | | 158 | | /// <inheritdoc /> |
| | 54 | 159 | | public long? OutboxSequence { get; } |
| | | 160 | | |
| | | 161 | | /// <inheritdoc /> |
| | 54 | 162 | | public string? GatewayExecutionId { get; } |
| | | 163 | | |
| | | 164 | | /// <inheritdoc /> |
| | 54 | 165 | | public string? DecisionStage { get; } |
| | | 166 | | |
| | | 167 | | /// <inheritdoc /> |
| | 60 | 168 | | public string? PolicyVersion { get; } |
| | | 169 | | |
| | | 170 | | /// <inheritdoc /> |
| | 59 | 171 | | public string? PolicyHash { get; } |
| | | 172 | | |
| | | 173 | | /// <inheritdoc /> |
| | 92 | 174 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Gets a value indicating whether this audit residue contains reason codes. |
| | | 178 | | /// </summary> |
| | 6 | 179 | | public bool HasReasonCodes => ReasonCodes.Count > 0; |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Gets a value indicating whether this audit residue contains metadata. |
| | | 183 | | /// </summary> |
| | 8 | 184 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Creates audit residue from a host-defined operation outcome. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 190 | | /// <param name="operationName">The operation name.</param> |
| | | 191 | | /// <param name="outcome">The governance, constraint, or host-defined outcome.</param> |
| | | 192 | | /// <param name="reasonCodes">Optional machine-readable reason codes.</param> |
| | | 193 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 194 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 195 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 196 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 197 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 198 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 199 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 200 | | /// <param name="auditResidueId">Optional audit residue identifier. When omitted, the normalized event identifier is |
| | | 201 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 202 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 203 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 204 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 205 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 206 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 207 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 208 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 209 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 210 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 211 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 212 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 213 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 214 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 215 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 216 | | /// <returns>An audit residue value.</returns> |
| | | 217 | | public static AuditResidue Create( |
| | | 218 | | IAsiBackboneActorContext actor, |
| | | 219 | | string operationName, |
| | | 220 | | string outcome, |
| | | 221 | | IEnumerable<string>? reasonCodes = null, |
| | | 222 | | string? eventId = null, |
| | | 223 | | DateTimeOffset? occurredUtc = null, |
| | | 224 | | string? correlationId = null, |
| | | 225 | | string? traceId = null, |
| | | 226 | | string? policyVersion = null, |
| | | 227 | | string? policyHash = null, |
| | | 228 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 229 | | string? auditResidueId = null, |
| | | 230 | | string? spanId = null, |
| | | 231 | | string? parentSpanId = null, |
| | | 232 | | long? decisionLatencyMs = null, |
| | | 233 | | string? constraintSetHash = null, |
| | | 234 | | int? constraintCount = null, |
| | | 235 | | double? riskScore = null, |
| | | 236 | | string? policyScope = null, |
| | | 237 | | string? tenantHash = null, |
| | | 238 | | string? organizationHash = null, |
| | | 239 | | string? emitterStatus = null, |
| | | 240 | | string? emitterProvider = null, |
| | | 241 | | long? outboxSequence = null, |
| | | 242 | | string? gatewayExecutionId = null, |
| | | 243 | | string? decisionStage = null, |
| | | 244 | | string? schemaVersion = null) |
| | | 245 | | { |
| | 102 | 246 | | ArgumentNullException.ThrowIfNull(actor); |
| | | 247 | | |
| | 101 | 248 | | string normalizedEventId = NormalizeIdentifier(eventId); |
| | | 249 | | |
| | 101 | 250 | | return new AuditResidue( |
| | 101 | 251 | | normalizedEventId, |
| | 101 | 252 | | NormalizeAuditResidueId(auditResidueId, normalizedEventId), |
| | 101 | 253 | | schemaVersion ?? AsiBackboneSchemaVersions.StableArtifactsV1, |
| | 101 | 254 | | occurredUtc ?? DateTimeOffset.UtcNow, |
| | 101 | 255 | | actor.ActorId, |
| | 101 | 256 | | actor.ActorType, |
| | 101 | 257 | | actor.DisplayName, |
| | 101 | 258 | | operationName, |
| | 101 | 259 | | outcome, |
| | 101 | 260 | | NormalizeReasonCodes(reasonCodes), |
| | 101 | 261 | | correlationId, |
| | 101 | 262 | | traceId, |
| | 101 | 263 | | spanId, |
| | 101 | 264 | | parentSpanId, |
| | 101 | 265 | | decisionLatencyMs, |
| | 101 | 266 | | constraintSetHash, |
| | 101 | 267 | | constraintCount, |
| | 101 | 268 | | riskScore, |
| | 101 | 269 | | policyScope, |
| | 101 | 270 | | tenantHash, |
| | 101 | 271 | | organizationHash, |
| | 101 | 272 | | emitterStatus, |
| | 101 | 273 | | emitterProvider, |
| | 101 | 274 | | outboxSequence, |
| | 101 | 275 | | gatewayExecutionId, |
| | 101 | 276 | | decisionStage, |
| | 101 | 277 | | policyVersion, |
| | 101 | 278 | | policyHash, |
| | 101 | 279 | | NormalizeMetadata(metadata)); |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | /// <summary> |
| | | 283 | | /// Creates audit residue from a governance decision. |
| | | 284 | | /// </summary> |
| | | 285 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 286 | | /// <param name="operationName">The operation name.</param> |
| | | 287 | | /// <param name="decision">The governance decision to audit.</param> |
| | | 288 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 289 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 290 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 291 | | /// <param name="auditResidueId">Optional audit residue identifier. When omitted, the normalized event identifier is |
| | | 292 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 293 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 294 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 295 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 296 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 297 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 298 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 299 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 300 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 301 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 302 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 303 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 304 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 305 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 306 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 307 | | /// <returns>An audit residue value.</returns> |
| | | 308 | | public static AuditResidue FromDecision( |
| | | 309 | | IAsiBackboneActorContext actor, |
| | | 310 | | string operationName, |
| | | 311 | | GovernanceDecision decision, |
| | | 312 | | string? eventId = null, |
| | | 313 | | DateTimeOffset? occurredUtc = null, |
| | | 314 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 315 | | string? auditResidueId = null, |
| | | 316 | | string? spanId = null, |
| | | 317 | | string? parentSpanId = null, |
| | | 318 | | long? decisionLatencyMs = null, |
| | | 319 | | string? constraintSetHash = null, |
| | | 320 | | int? constraintCount = null, |
| | | 321 | | double? riskScore = null, |
| | | 322 | | string? policyScope = null, |
| | | 323 | | string? tenantHash = null, |
| | | 324 | | string? organizationHash = null, |
| | | 325 | | string? emitterStatus = null, |
| | | 326 | | string? emitterProvider = null, |
| | | 327 | | long? outboxSequence = null, |
| | | 328 | | string? gatewayExecutionId = null, |
| | | 329 | | string? decisionStage = null, |
| | | 330 | | string? schemaVersion = null) |
| | | 331 | | { |
| | 6 | 332 | | ArgumentNullException.ThrowIfNull(decision); |
| | | 333 | | |
| | 5 | 334 | | return Create( |
| | 5 | 335 | | actor, |
| | 5 | 336 | | operationName, |
| | 5 | 337 | | decision.Outcome.ToString(), |
| | 5 | 338 | | decision.ReasonCodes, |
| | 5 | 339 | | eventId, |
| | 5 | 340 | | occurredUtc, |
| | 5 | 341 | | decision.CorrelationId, |
| | 5 | 342 | | decision.TraceId, |
| | 5 | 343 | | decision.PolicyVersion, |
| | 5 | 344 | | decision.PolicyHash, |
| | 5 | 345 | | metadata, |
| | 5 | 346 | | auditResidueId, |
| | 5 | 347 | | spanId, |
| | 5 | 348 | | parentSpanId, |
| | 5 | 349 | | decisionLatencyMs, |
| | 5 | 350 | | constraintSetHash, |
| | 5 | 351 | | constraintCount, |
| | 5 | 352 | | riskScore, |
| | 5 | 353 | | policyScope, |
| | 5 | 354 | | tenantHash, |
| | 5 | 355 | | organizationHash, |
| | 5 | 356 | | emitterStatus, |
| | 5 | 357 | | emitterProvider, |
| | 5 | 358 | | outboxSequence, |
| | 5 | 359 | | gatewayExecutionId, |
| | 5 | 360 | | decisionStage, |
| | 5 | 361 | | schemaVersion); |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | /// <summary> |
| | | 365 | | /// Creates audit residue from a constraint evaluation result. |
| | | 366 | | /// </summary> |
| | | 367 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 368 | | /// <param name="operationName">The operation name.</param> |
| | | 369 | | /// <param name="constraintResult">The constraint evaluation result to audit.</param> |
| | | 370 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 371 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 372 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 373 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 374 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 375 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 376 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 377 | | /// <param name="auditResidueId">Optional audit residue identifier. When omitted, the normalized event identifier is |
| | | 378 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 379 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 380 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 381 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 382 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 383 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 384 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 385 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 386 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 387 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 388 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 389 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 390 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 391 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 392 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 393 | | /// <returns>An audit residue value.</returns> |
| | | 394 | | public static AuditResidue FromConstraint( |
| | | 395 | | IAsiBackboneActorContext actor, |
| | | 396 | | string operationName, |
| | | 397 | | ConstraintEvaluationResult constraintResult, |
| | | 398 | | string? eventId = null, |
| | | 399 | | DateTimeOffset? occurredUtc = null, |
| | | 400 | | string? correlationId = null, |
| | | 401 | | string? traceId = null, |
| | | 402 | | string? policyVersion = null, |
| | | 403 | | string? policyHash = null, |
| | | 404 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 405 | | string? auditResidueId = null, |
| | | 406 | | string? spanId = null, |
| | | 407 | | string? parentSpanId = null, |
| | | 408 | | long? decisionLatencyMs = null, |
| | | 409 | | string? constraintSetHash = null, |
| | | 410 | | int? constraintCount = null, |
| | | 411 | | double? riskScore = null, |
| | | 412 | | string? policyScope = null, |
| | | 413 | | string? tenantHash = null, |
| | | 414 | | string? organizationHash = null, |
| | | 415 | | string? emitterStatus = null, |
| | | 416 | | string? emitterProvider = null, |
| | | 417 | | long? outboxSequence = null, |
| | | 418 | | string? gatewayExecutionId = null, |
| | | 419 | | string? decisionStage = null, |
| | | 420 | | string? schemaVersion = null) |
| | | 421 | | { |
| | 4 | 422 | | ArgumentNullException.ThrowIfNull(constraintResult); |
| | | 423 | | |
| | 3 | 424 | | return Create( |
| | 3 | 425 | | actor, |
| | 3 | 426 | | operationName, |
| | 3 | 427 | | constraintResult.Outcome.ToString(), |
| | 3 | 428 | | constraintResult.ReasonCodes, |
| | 3 | 429 | | eventId, |
| | 3 | 430 | | occurredUtc, |
| | 3 | 431 | | correlationId, |
| | 3 | 432 | | traceId, |
| | 3 | 433 | | policyVersion, |
| | 3 | 434 | | policyHash, |
| | 3 | 435 | | metadata, |
| | 3 | 436 | | auditResidueId, |
| | 3 | 437 | | spanId, |
| | 3 | 438 | | parentSpanId, |
| | 3 | 439 | | decisionLatencyMs, |
| | 3 | 440 | | constraintSetHash, |
| | 3 | 441 | | constraintCount, |
| | 3 | 442 | | riskScore, |
| | 3 | 443 | | policyScope, |
| | 3 | 444 | | tenantHash, |
| | 3 | 445 | | organizationHash, |
| | 3 | 446 | | emitterStatus, |
| | 3 | 447 | | emitterProvider, |
| | 3 | 448 | | outboxSequence, |
| | 3 | 449 | | gatewayExecutionId, |
| | 3 | 450 | | decisionStage, |
| | 3 | 451 | | schemaVersion); |
| | | 452 | | } |
| | | 453 | | |
| | | 454 | | private static string NormalizeIdentifier(string? identifier) |
| | | 455 | | { |
| | 101 | 456 | | return string.IsNullOrWhiteSpace(identifier) |
| | 101 | 457 | | ? Guid.NewGuid().ToString("N") |
| | 101 | 458 | | : identifier.Trim(); |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | private static string NormalizeAuditResidueId(string? auditResidueId, string eventId) |
| | | 462 | | { |
| | 101 | 463 | | return string.IsNullOrWhiteSpace(auditResidueId) |
| | 101 | 464 | | ? eventId |
| | 101 | 465 | | : auditResidueId.Trim(); |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | private static string? NormalizeOptional(string? value) |
| | | 469 | | { |
| | 1343 | 470 | | return string.IsNullOrWhiteSpace(value) |
| | 1343 | 471 | | ? null |
| | 1343 | 472 | | : value.Trim(); |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | private static long? NormalizeNonNegative(long? value, string parameterName) |
| | | 476 | | { |
| | 182 | 477 | | return value < 0 |
| | 182 | 478 | | ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero." |
| | 182 | 479 | | : value; |
| | | 480 | | } |
| | | 481 | | |
| | | 482 | | private static int? NormalizeNonNegative(int? value, string parameterName) |
| | | 483 | | { |
| | 93 | 484 | | return value < 0 |
| | 93 | 485 | | ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero." |
| | 93 | 486 | | : value; |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | private static double? NormalizeRiskScore(double? riskScore) |
| | | 490 | | { |
| | 91 | 491 | | return riskScore is null |
| | 91 | 492 | | ? null |
| | 91 | 493 | | : double.IsNaN(riskScore.Value) || double.IsInfinity(riskScore.Value) || riskScore.Value < 0 |
| | 91 | 494 | | ? throw new ArgumentOutOfRangeException(nameof(riskScore), riskScore, "Risk score must be a finite value gre |
| | 91 | 495 | | : riskScore; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | private static ReadOnlyCollection<string> NormalizeReasonCodes(IEnumerable<string>? reasonCodes) |
| | | 499 | | { |
| | 101 | 500 | | string[] normalizedReasonCodes = reasonCodes? |
| | 58 | 501 | | .Where(reasonCode => !string.IsNullOrWhiteSpace(reasonCode)) |
| | 52 | 502 | | .Select(reasonCode => reasonCode.Trim()) |
| | 101 | 503 | | .ToArray() ?? []; |
| | | 504 | | |
| | 101 | 505 | | return normalizedReasonCodes.Length == 0 |
| | 101 | 506 | | ? EmptyReasonCodes |
| | 101 | 507 | | : Array.AsReadOnly(normalizedReasonCodes); |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 511 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 512 | | { |
| | 101 | 513 | | if (metadata is null || metadata.Count == 0) |
| | | 514 | | { |
| | 64 | 515 | | return EmptyMetadata; |
| | | 516 | | } |
| | | 517 | | |
| | 37 | 518 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 519 | | |
| | 198 | 520 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 521 | | { |
| | 62 | 522 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 523 | | { |
| | | 524 | | continue; |
| | | 525 | | } |
| | | 526 | | |
| | 58 | 527 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 528 | | } |
| | | 529 | | |
| | 37 | 530 | | return normalizedMetadata.Count == 0 |
| | 37 | 531 | | ? EmptyMetadata |
| | 37 | 532 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 533 | | } |
| | | 534 | | } |