| | | 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 | | using AsiBackbone.Core.Signing; |
| | | 7 | | |
| | | 8 | | namespace AsiBackbone.Core.Audit; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Represents the framework-neutral decision receipt produced by an AsiBackbone operation. |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class DecisionReceipt : IDecisionReceipt |
| | | 14 | | { |
| | 5 | 15 | | private static readonly ReadOnlyCollection<string> EmptyReasonCodes = |
| | 5 | 16 | | Array.AsReadOnly(Array.Empty<string>()); |
| | | 17 | | |
| | 5 | 18 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 5 | 19 | | new ReadOnlyDictionary<string, string>( |
| | 5 | 20 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 21 | | |
| | 176 | 22 | | private DecisionReceipt( |
| | 176 | 23 | | string eventId, |
| | 176 | 24 | | string decisionReceiptId, |
| | 176 | 25 | | string schemaVersion, |
| | 176 | 26 | | DateTimeOffset occurredUtc, |
| | 176 | 27 | | string actorId, |
| | 176 | 28 | | GovernanceActorType actorType, |
| | 176 | 29 | | string? actorDisplayName, |
| | 176 | 30 | | string operationName, |
| | 176 | 31 | | string outcome, |
| | 176 | 32 | | IReadOnlyList<string> reasonCodes, |
| | 176 | 33 | | string? correlationId, |
| | 176 | 34 | | string? traceId, |
| | 176 | 35 | | string? spanId, |
| | 176 | 36 | | string? parentSpanId, |
| | 176 | 37 | | long? decisionLatencyMs, |
| | 176 | 38 | | string? constraintSetHash, |
| | 176 | 39 | | int? constraintCount, |
| | 176 | 40 | | double? riskScore, |
| | 176 | 41 | | string? policyScope, |
| | 176 | 42 | | string? tenantHash, |
| | 176 | 43 | | string? organizationHash, |
| | 176 | 44 | | string? emitterStatus, |
| | 176 | 45 | | string? emitterProvider, |
| | 176 | 46 | | long? outboxSequence, |
| | 176 | 47 | | string? gatewayExecutionId, |
| | 176 | 48 | | string? decisionStage, |
| | 176 | 49 | | string? policyVersion, |
| | 176 | 50 | | string? policyHash, |
| | 176 | 51 | | IReadOnlyDictionary<string, string> metadata) |
| | | 52 | | { |
| | 176 | 53 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventId); |
| | 176 | 54 | | ArgumentException.ThrowIfNullOrWhiteSpace(decisionReceiptId); |
| | 176 | 55 | | ArgumentException.ThrowIfNullOrWhiteSpace(actorId); |
| | 176 | 56 | | ArgumentException.ThrowIfNullOrWhiteSpace(operationName); |
| | 173 | 57 | | ArgumentException.ThrowIfNullOrWhiteSpace(outcome); |
| | | 58 | | |
| | 170 | 59 | | EventId = eventId.Trim(); |
| | 170 | 60 | | DecisionReceiptId = decisionReceiptId.Trim(); |
| | 170 | 61 | | SchemaVersion = GovernanceSchemaVersions.Normalize(schemaVersion); |
| | 170 | 62 | | OccurredUtc = occurredUtc.ToUniversalTime(); |
| | 170 | 63 | | ActorId = actorId.Trim(); |
| | 170 | 64 | | ActorType = actorType; |
| | 170 | 65 | | ActorDisplayName = NormalizeOptional(actorDisplayName); |
| | 170 | 66 | | OperationName = operationName.Trim(); |
| | 170 | 67 | | Outcome = outcome.Trim(); |
| | 170 | 68 | | ReasonCodes = reasonCodes; |
| | 170 | 69 | | CorrelationId = NormalizeOptional(correlationId); |
| | 170 | 70 | | TraceId = NormalizeOptional(traceId); |
| | 170 | 71 | | SpanId = NormalizeOptional(spanId); |
| | 170 | 72 | | ParentSpanId = NormalizeOptional(parentSpanId); |
| | 170 | 73 | | DecisionLatencyMs = NormalizeNonNegative(decisionLatencyMs, nameof(decisionLatencyMs)); |
| | 167 | 74 | | ConstraintSetHash = NormalizeOptional(constraintSetHash); |
| | 167 | 75 | | ConstraintCount = NormalizeNonNegative(constraintCount, nameof(constraintCount)); |
| | 164 | 76 | | RiskScore = NormalizeRiskScore(riskScore); |
| | 157 | 77 | | PolicyScope = NormalizeOptional(policyScope); |
| | 157 | 78 | | TenantHash = NormalizeOptional(tenantHash); |
| | 157 | 79 | | OrganizationHash = NormalizeOptional(organizationHash); |
| | 157 | 80 | | EmitterStatus = NormalizeOptional(emitterStatus); |
| | 157 | 81 | | EmitterProvider = NormalizeOptional(emitterProvider); |
| | 157 | 82 | | OutboxSequence = NormalizeNonNegative(outboxSequence, nameof(outboxSequence)); |
| | 154 | 83 | | GatewayExecutionId = NormalizeOptional(gatewayExecutionId); |
| | 154 | 84 | | DecisionStage = NormalizeOptional(decisionStage); |
| | 154 | 85 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 154 | 86 | | PolicyHash = NormalizeOptional(policyHash); |
| | 154 | 87 | | Metadata = metadata; |
| | 154 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc /> |
| | | 91 | | public string EventId { get; } |
| | | 92 | | |
| | | 93 | | /// <inheritdoc /> |
| | | 94 | | public string DecisionReceiptId { get; } |
| | | 95 | | |
| | | 96 | | /// <inheritdoc /> |
| | | 97 | | public string SchemaVersion { get; } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | | 100 | | public DateTimeOffset OccurredUtc { get; } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc /> |
| | | 103 | | public string ActorId { get; } |
| | | 104 | | |
| | | 105 | | /// <inheritdoc /> |
| | | 106 | | public GovernanceActorType ActorType { get; } |
| | | 107 | | |
| | | 108 | | /// <inheritdoc /> |
| | | 109 | | public string? ActorDisplayName { get; } |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | public string OperationName { get; } |
| | | 113 | | |
| | | 114 | | /// <inheritdoc /> |
| | | 115 | | public string Outcome { get; } |
| | | 116 | | |
| | | 117 | | /// <inheritdoc /> |
| | | 118 | | public IReadOnlyList<string> ReasonCodes { get; } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc /> |
| | | 121 | | public string? CorrelationId { get; } |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | | 124 | | public string? TraceId { get; } |
| | | 125 | | |
| | | 126 | | /// <inheritdoc /> |
| | | 127 | | public string? SpanId { get; } |
| | | 128 | | |
| | | 129 | | /// <inheritdoc /> |
| | | 130 | | public string? ParentSpanId { get; } |
| | | 131 | | |
| | | 132 | | /// <inheritdoc /> |
| | | 133 | | public long? DecisionLatencyMs { get; } |
| | | 134 | | |
| | | 135 | | /// <inheritdoc /> |
| | | 136 | | public string? ConstraintSetHash { get; } |
| | | 137 | | |
| | | 138 | | /// <inheritdoc /> |
| | | 139 | | public int? ConstraintCount { get; } |
| | | 140 | | |
| | | 141 | | /// <inheritdoc /> |
| | | 142 | | public double? RiskScore { get; } |
| | | 143 | | |
| | | 144 | | /// <inheritdoc /> |
| | | 145 | | public string? PolicyScope { get; } |
| | | 146 | | |
| | | 147 | | /// <inheritdoc /> |
| | | 148 | | public string? TenantHash { get; } |
| | | 149 | | |
| | | 150 | | /// <inheritdoc /> |
| | | 151 | | public string? OrganizationHash { get; } |
| | | 152 | | |
| | | 153 | | /// <inheritdoc /> |
| | | 154 | | public string? EmitterStatus { get; } |
| | | 155 | | |
| | | 156 | | /// <inheritdoc /> |
| | | 157 | | public string? EmitterProvider { get; } |
| | | 158 | | |
| | | 159 | | /// <inheritdoc /> |
| | | 160 | | public long? OutboxSequence { get; } |
| | | 161 | | |
| | | 162 | | /// <inheritdoc /> |
| | | 163 | | public string? GatewayExecutionId { get; } |
| | | 164 | | |
| | | 165 | | /// <inheritdoc /> |
| | | 166 | | public string? DecisionStage { get; } |
| | | 167 | | |
| | | 168 | | /// <inheritdoc /> |
| | | 169 | | public string? PolicyVersion { get; } |
| | | 170 | | |
| | | 171 | | /// <inheritdoc /> |
| | | 172 | | public string? PolicyHash { get; } |
| | | 173 | | |
| | | 174 | | /// <inheritdoc /> |
| | | 175 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Gets a value indicating whether this decision receipt contains reason codes. |
| | | 179 | | /// </summary> |
| | 15 | 180 | | public bool HasReasonCodes => ReasonCodes.Count > 0; |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Gets a value indicating whether this decision receipt contains metadata. |
| | | 184 | | /// </summary> |
| | 10 | 185 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Creates decision receipt from a host-defined operation outcome. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 191 | | /// <param name="operationName">The operation name.</param> |
| | | 192 | | /// <param name="outcome">The governance, constraint, or host-defined outcome.</param> |
| | | 193 | | /// <param name="reasonCodes">Optional machine-readable reason codes.</param> |
| | | 194 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 195 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 196 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 197 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 198 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 199 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 200 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 201 | | /// <param name="decisionReceiptId">Optional decision receipt identifier. When omitted, the normalized event identif |
| | | 202 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 203 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 204 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 205 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 206 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 207 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 208 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 209 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 210 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 211 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 212 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 213 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 214 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 215 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 216 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 217 | | /// <returns>An decision receipt value.</returns> |
| | | 218 | | public static DecisionReceipt Create( |
| | | 219 | | IGovernanceActorContext actor, |
| | | 220 | | string operationName, |
| | | 221 | | string outcome, |
| | | 222 | | IEnumerable<string>? reasonCodes = null, |
| | | 223 | | string? eventId = null, |
| | | 224 | | DateTimeOffset? occurredUtc = null, |
| | | 225 | | string? correlationId = null, |
| | | 226 | | string? traceId = null, |
| | | 227 | | string? policyVersion = null, |
| | | 228 | | string? policyHash = null, |
| | | 229 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 230 | | string? decisionReceiptId = null, |
| | | 231 | | string? spanId = null, |
| | | 232 | | string? parentSpanId = null, |
| | | 233 | | long? decisionLatencyMs = null, |
| | | 234 | | string? constraintSetHash = null, |
| | | 235 | | int? constraintCount = null, |
| | | 236 | | double? riskScore = null, |
| | | 237 | | string? policyScope = null, |
| | | 238 | | string? tenantHash = null, |
| | | 239 | | string? organizationHash = null, |
| | | 240 | | string? emitterStatus = null, |
| | | 241 | | string? emitterProvider = null, |
| | | 242 | | long? outboxSequence = null, |
| | | 243 | | string? gatewayExecutionId = null, |
| | | 244 | | string? decisionStage = null, |
| | | 245 | | string? schemaVersion = null) |
| | | 246 | | { |
| | 148 | 247 | | return CreateCore( |
| | 148 | 248 | | actor, |
| | 148 | 249 | | operationName, |
| | 148 | 250 | | outcome, |
| | 148 | 251 | | NormalizeReasonCodes(reasonCodes), |
| | 148 | 252 | | eventId, |
| | 148 | 253 | | occurredUtc, |
| | 148 | 254 | | correlationId, |
| | 148 | 255 | | traceId, |
| | 148 | 256 | | policyVersion, |
| | 148 | 257 | | policyHash, |
| | 148 | 258 | | metadata, |
| | 148 | 259 | | decisionReceiptId, |
| | 148 | 260 | | spanId, |
| | 148 | 261 | | parentSpanId, |
| | 148 | 262 | | decisionLatencyMs, |
| | 148 | 263 | | constraintSetHash, |
| | 148 | 264 | | constraintCount, |
| | 148 | 265 | | riskScore, |
| | 148 | 266 | | policyScope, |
| | 148 | 267 | | tenantHash, |
| | 148 | 268 | | organizationHash, |
| | 148 | 269 | | emitterStatus, |
| | 148 | 270 | | emitterProvider, |
| | 148 | 271 | | outboxSequence, |
| | 148 | 272 | | gatewayExecutionId, |
| | 148 | 273 | | decisionStage, |
| | 148 | 274 | | schemaVersion); |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Creates decision receipt from a governance decision. |
| | | 279 | | /// </summary> |
| | | 280 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 281 | | /// <param name="operationName">The operation name.</param> |
| | | 282 | | /// <param name="decision">The governance decision to audit.</param> |
| | | 283 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 284 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 285 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 286 | | /// <param name="decisionReceiptId">Optional decision receipt identifier. When omitted, the normalized event identif |
| | | 287 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 288 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 289 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 290 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 291 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 292 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 293 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 294 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 295 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 296 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 297 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 298 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 299 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 300 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 301 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 302 | | /// <returns>An decision receipt value.</returns> |
| | | 303 | | public static DecisionReceipt FromDecision( |
| | | 304 | | IGovernanceActorContext actor, |
| | | 305 | | string operationName, |
| | | 306 | | GovernanceDecision decision, |
| | | 307 | | string? eventId = null, |
| | | 308 | | DateTimeOffset? occurredUtc = null, |
| | | 309 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 310 | | string? decisionReceiptId = null, |
| | | 311 | | string? spanId = null, |
| | | 312 | | string? parentSpanId = null, |
| | | 313 | | long? decisionLatencyMs = null, |
| | | 314 | | string? constraintSetHash = null, |
| | | 315 | | int? constraintCount = null, |
| | | 316 | | double? riskScore = null, |
| | | 317 | | string? policyScope = null, |
| | | 318 | | string? tenantHash = null, |
| | | 319 | | string? organizationHash = null, |
| | | 320 | | string? emitterStatus = null, |
| | | 321 | | string? emitterProvider = null, |
| | | 322 | | long? outboxSequence = null, |
| | | 323 | | string? gatewayExecutionId = null, |
| | | 324 | | string? decisionStage = null, |
| | | 325 | | string? schemaVersion = null) |
| | | 326 | | { |
| | 20 | 327 | | ArgumentNullException.ThrowIfNull(decision); |
| | | 328 | | |
| | 19 | 329 | | return CreateCore( |
| | 19 | 330 | | actor, |
| | 19 | 331 | | operationName, |
| | 19 | 332 | | CanonicalEnumWireNames.ForDecisionOutcome(decision.Outcome), |
| | 19 | 333 | | UseTrustedReasonCodes(decision.ReasonCodes), |
| | 19 | 334 | | eventId, |
| | 19 | 335 | | occurredUtc, |
| | 19 | 336 | | decision.CorrelationId, |
| | 19 | 337 | | decision.TraceId, |
| | 19 | 338 | | decision.PolicyVersion, |
| | 19 | 339 | | decision.PolicyHash, |
| | 19 | 340 | | metadata, |
| | 19 | 341 | | decisionReceiptId, |
| | 19 | 342 | | spanId, |
| | 19 | 343 | | parentSpanId, |
| | 19 | 344 | | decisionLatencyMs, |
| | 19 | 345 | | constraintSetHash, |
| | 19 | 346 | | constraintCount, |
| | 19 | 347 | | riskScore, |
| | 19 | 348 | | policyScope, |
| | 19 | 349 | | tenantHash, |
| | 19 | 350 | | organizationHash, |
| | 19 | 351 | | emitterStatus, |
| | 19 | 352 | | emitterProvider, |
| | 19 | 353 | | outboxSequence, |
| | 19 | 354 | | gatewayExecutionId, |
| | 19 | 355 | | decisionStage, |
| | 19 | 356 | | schemaVersion); |
| | | 357 | | } |
| | | 358 | | |
| | | 359 | | /// <summary> |
| | | 360 | | /// Creates decision receipt from a constraint evaluation result. |
| | | 361 | | /// </summary> |
| | | 362 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 363 | | /// <param name="operationName">The operation name.</param> |
| | | 364 | | /// <param name="constraintResult">The constraint evaluation result to audit.</param> |
| | | 365 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 366 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 367 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 368 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 369 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 370 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 371 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 372 | | /// <param name="decisionReceiptId">Optional decision receipt identifier. When omitted, the normalized event identif |
| | | 373 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 374 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 375 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 376 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 377 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 378 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 379 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 380 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 381 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 382 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 383 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 384 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 385 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 386 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 387 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 388 | | /// <returns>An decision receipt value.</returns> |
| | | 389 | | public static DecisionReceipt FromConstraint( |
| | | 390 | | IGovernanceActorContext actor, |
| | | 391 | | string operationName, |
| | | 392 | | ConstraintEvaluationResult constraintResult, |
| | | 393 | | string? eventId = null, |
| | | 394 | | DateTimeOffset? occurredUtc = null, |
| | | 395 | | string? correlationId = null, |
| | | 396 | | string? traceId = null, |
| | | 397 | | string? policyVersion = null, |
| | | 398 | | string? policyHash = null, |
| | | 399 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 400 | | string? decisionReceiptId = null, |
| | | 401 | | string? spanId = null, |
| | | 402 | | string? parentSpanId = null, |
| | | 403 | | long? decisionLatencyMs = null, |
| | | 404 | | string? constraintSetHash = null, |
| | | 405 | | int? constraintCount = null, |
| | | 406 | | double? riskScore = null, |
| | | 407 | | string? policyScope = null, |
| | | 408 | | string? tenantHash = null, |
| | | 409 | | string? organizationHash = null, |
| | | 410 | | string? emitterStatus = null, |
| | | 411 | | string? emitterProvider = null, |
| | | 412 | | long? outboxSequence = null, |
| | | 413 | | string? gatewayExecutionId = null, |
| | | 414 | | string? decisionStage = null, |
| | | 415 | | string? schemaVersion = null) |
| | | 416 | | { |
| | 12 | 417 | | ArgumentNullException.ThrowIfNull(constraintResult); |
| | | 418 | | |
| | 11 | 419 | | return CreateCore( |
| | 11 | 420 | | actor, |
| | 11 | 421 | | operationName, |
| | 11 | 422 | | CanonicalEnumWireNames.ForConstraintOutcome(constraintResult.Outcome), |
| | 11 | 423 | | UseTrustedReasonCodes(constraintResult.ReasonCodes), |
| | 11 | 424 | | eventId, |
| | 11 | 425 | | occurredUtc, |
| | 11 | 426 | | correlationId, |
| | 11 | 427 | | traceId, |
| | 11 | 428 | | policyVersion, |
| | 11 | 429 | | policyHash, |
| | 11 | 430 | | metadata, |
| | 11 | 431 | | decisionReceiptId, |
| | 11 | 432 | | spanId, |
| | 11 | 433 | | parentSpanId, |
| | 11 | 434 | | decisionLatencyMs, |
| | 11 | 435 | | constraintSetHash, |
| | 11 | 436 | | constraintCount, |
| | 11 | 437 | | riskScore, |
| | 11 | 438 | | policyScope, |
| | 11 | 439 | | tenantHash, |
| | 11 | 440 | | organizationHash, |
| | 11 | 441 | | emitterStatus, |
| | 11 | 442 | | emitterProvider, |
| | 11 | 443 | | outboxSequence, |
| | 11 | 444 | | gatewayExecutionId, |
| | 11 | 445 | | decisionStage, |
| | 11 | 446 | | schemaVersion); |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | private static DecisionReceipt CreateCore( |
| | | 450 | | IGovernanceActorContext actor, |
| | | 451 | | string operationName, |
| | | 452 | | string outcome, |
| | | 453 | | IReadOnlyList<string> reasonCodes, |
| | | 454 | | string? eventId, |
| | | 455 | | DateTimeOffset? occurredUtc, |
| | | 456 | | string? correlationId, |
| | | 457 | | string? traceId, |
| | | 458 | | string? policyVersion, |
| | | 459 | | string? policyHash, |
| | | 460 | | IReadOnlyDictionary<string, string>? metadata, |
| | | 461 | | string? decisionReceiptId, |
| | | 462 | | string? spanId, |
| | | 463 | | string? parentSpanId, |
| | | 464 | | long? decisionLatencyMs, |
| | | 465 | | string? constraintSetHash, |
| | | 466 | | int? constraintCount, |
| | | 467 | | double? riskScore, |
| | | 468 | | string? policyScope, |
| | | 469 | | string? tenantHash, |
| | | 470 | | string? organizationHash, |
| | | 471 | | string? emitterStatus, |
| | | 472 | | string? emitterProvider, |
| | | 473 | | long? outboxSequence, |
| | | 474 | | string? gatewayExecutionId, |
| | | 475 | | string? decisionStage, |
| | | 476 | | string? schemaVersion) |
| | | 477 | | { |
| | 178 | 478 | | ArgumentNullException.ThrowIfNull(actor); |
| | | 479 | | |
| | 177 | 480 | | string normalizedEventId = NormalizeIdentifier(eventId); |
| | | 481 | | |
| | 177 | 482 | | return new DecisionReceipt( |
| | 177 | 483 | | normalizedEventId, |
| | 177 | 484 | | NormalizeDecisionReceiptId(decisionReceiptId, normalizedEventId), |
| | 177 | 485 | | schemaVersion ?? GovernanceSchemaVersions.StableArtifactsV1, |
| | 177 | 486 | | occurredUtc ?? DateTimeOffset.UtcNow, |
| | 177 | 487 | | actor.ActorId, |
| | 177 | 488 | | actor.ActorType, |
| | 177 | 489 | | actor.DisplayName, |
| | 177 | 490 | | operationName, |
| | 177 | 491 | | outcome, |
| | 177 | 492 | | reasonCodes, |
| | 177 | 493 | | correlationId, |
| | 177 | 494 | | traceId, |
| | 177 | 495 | | spanId, |
| | 177 | 496 | | parentSpanId, |
| | 177 | 497 | | decisionLatencyMs, |
| | 177 | 498 | | constraintSetHash, |
| | 177 | 499 | | constraintCount, |
| | 177 | 500 | | riskScore, |
| | 177 | 501 | | policyScope, |
| | 177 | 502 | | tenantHash, |
| | 177 | 503 | | organizationHash, |
| | 177 | 504 | | emitterStatus, |
| | 177 | 505 | | emitterProvider, |
| | 177 | 506 | | outboxSequence, |
| | 177 | 507 | | gatewayExecutionId, |
| | 177 | 508 | | decisionStage, |
| | 177 | 509 | | policyVersion, |
| | 177 | 510 | | policyHash, |
| | 177 | 511 | | NormalizeMetadata(metadata)); |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | private static string NormalizeIdentifier(string? identifier) |
| | | 515 | | { |
| | 177 | 516 | | return string.IsNullOrWhiteSpace(identifier) |
| | 177 | 517 | | ? Guid.NewGuid().ToString("N") |
| | 177 | 518 | | : identifier.Trim(); |
| | | 519 | | } |
| | | 520 | | |
| | | 521 | | private static string NormalizeDecisionReceiptId(string? decisionReceiptId, string eventId) |
| | | 522 | | { |
| | 177 | 523 | | return string.IsNullOrWhiteSpace(decisionReceiptId) |
| | 177 | 524 | | ? eventId |
| | 177 | 525 | | : decisionReceiptId.Trim(); |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | private static string? NormalizeOptional(string? value) |
| | | 529 | | { |
| | 2418 | 530 | | return string.IsNullOrWhiteSpace(value) |
| | 2418 | 531 | | ? null |
| | 2418 | 532 | | : value.Trim(); |
| | | 533 | | } |
| | | 534 | | |
| | | 535 | | private static long? NormalizeNonNegative(long? value, string parameterName) |
| | | 536 | | { |
| | 327 | 537 | | return value < 0 |
| | 327 | 538 | | ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero." |
| | 327 | 539 | | : value; |
| | | 540 | | } |
| | | 541 | | |
| | | 542 | | private static int? NormalizeNonNegative(int? value, string parameterName) |
| | | 543 | | { |
| | 167 | 544 | | return value < 0 |
| | 167 | 545 | | ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero." |
| | 167 | 546 | | : value; |
| | | 547 | | } |
| | | 548 | | |
| | | 549 | | private static double? NormalizeRiskScore(double? riskScore) |
| | | 550 | | { |
| | 164 | 551 | | return riskScore is null |
| | 164 | 552 | | ? null |
| | 164 | 553 | | : double.IsNaN(riskScore.Value) || double.IsInfinity(riskScore.Value) || riskScore.Value < 0 |
| | 164 | 554 | | ? throw new ArgumentOutOfRangeException(nameof(riskScore), riskScore, "Risk score must be a finite value gre |
| | 164 | 555 | | : riskScore; |
| | | 556 | | } |
| | | 557 | | |
| | | 558 | | private static ReadOnlyCollection<string> NormalizeReasonCodes(IEnumerable<string>? reasonCodes) |
| | | 559 | | { |
| | 148 | 560 | | if (reasonCodes is null) |
| | | 561 | | { |
| | 69 | 562 | | return EmptyReasonCodes; |
| | | 563 | | } |
| | | 564 | | |
| | 79 | 565 | | if (reasonCodes is ICollection<string> collection) |
| | | 566 | | { |
| | 77 | 567 | | if (collection.Count == 0) |
| | | 568 | | { |
| | 16 | 569 | | return EmptyReasonCodes; |
| | | 570 | | } |
| | | 571 | | |
| | 61 | 572 | | string[] normalizedReasonCodes = new string[collection.Count]; |
| | 61 | 573 | | int normalizedCount = 0; |
| | | 574 | | |
| | 288 | 575 | | foreach (string? reasonCode in collection) |
| | | 576 | | { |
| | 83 | 577 | | AddNormalizedReasonCode(reasonCode, normalizedReasonCodes, ref normalizedCount); |
| | | 578 | | } |
| | | 579 | | |
| | 61 | 580 | | return CreateReasonCodeCollection(normalizedReasonCodes, normalizedCount); |
| | | 581 | | } |
| | | 582 | | |
| | 2 | 583 | | List<string>? normalizedList = null; |
| | | 584 | | |
| | 16 | 585 | | foreach (string? reasonCode in reasonCodes) |
| | | 586 | | { |
| | 6 | 587 | | if (string.IsNullOrWhiteSpace(reasonCode)) |
| | | 588 | | { |
| | | 589 | | continue; |
| | | 590 | | } |
| | | 591 | | |
| | 2 | 592 | | normalizedList ??= []; |
| | 2 | 593 | | normalizedList.Add(reasonCode.Trim()); |
| | | 594 | | } |
| | | 595 | | |
| | 2 | 596 | | return normalizedList is null || normalizedList.Count == 0 |
| | 2 | 597 | | ? EmptyReasonCodes |
| | 2 | 598 | | : Array.AsReadOnly([.. normalizedList]); |
| | | 599 | | } |
| | | 600 | | |
| | | 601 | | private static IReadOnlyList<string> UseTrustedReasonCodes(IReadOnlyList<string> reasonCodes) |
| | | 602 | | { |
| | 30 | 603 | | return reasonCodes.Count == 0 |
| | 30 | 604 | | ? EmptyReasonCodes |
| | 30 | 605 | | : reasonCodes; |
| | | 606 | | } |
| | | 607 | | |
| | | 608 | | private static void AddNormalizedReasonCode( |
| | | 609 | | string? reasonCode, |
| | | 610 | | string[] normalizedReasonCodes, |
| | | 611 | | ref int normalizedCount) |
| | | 612 | | { |
| | 83 | 613 | | if (string.IsNullOrWhiteSpace(reasonCode)) |
| | | 614 | | { |
| | 10 | 615 | | return; |
| | | 616 | | } |
| | | 617 | | |
| | 73 | 618 | | normalizedReasonCodes[normalizedCount] = reasonCode.Trim(); |
| | 73 | 619 | | normalizedCount++; |
| | 73 | 620 | | } |
| | | 621 | | |
| | | 622 | | private static ReadOnlyCollection<string> CreateReasonCodeCollection( |
| | | 623 | | string[] normalizedReasonCodes, |
| | | 624 | | int normalizedCount) |
| | | 625 | | { |
| | 61 | 626 | | if (normalizedCount == 0) |
| | | 627 | | { |
| | 1 | 628 | | return EmptyReasonCodes; |
| | | 629 | | } |
| | | 630 | | |
| | 60 | 631 | | if (normalizedCount == normalizedReasonCodes.Length) |
| | | 632 | | { |
| | 55 | 633 | | return Array.AsReadOnly(normalizedReasonCodes); |
| | | 634 | | } |
| | | 635 | | |
| | 5 | 636 | | string[] filteredReasonCodes = new string[normalizedCount]; |
| | 5 | 637 | | Array.Copy(normalizedReasonCodes, filteredReasonCodes, normalizedCount); |
| | | 638 | | |
| | 5 | 639 | | return Array.AsReadOnly(filteredReasonCodes); |
| | | 640 | | } |
| | | 641 | | |
| | | 642 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 643 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 644 | | { |
| | 177 | 645 | | if (metadata is null || metadata.Count == 0) |
| | | 646 | | { |
| | 137 | 647 | | return EmptyMetadata; |
| | | 648 | | } |
| | | 649 | | |
| | 40 | 650 | | Dictionary<string, string> normalizedMetadata = new(metadata.Count, StringComparer.Ordinal); |
| | | 651 | | |
| | 213 | 652 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 653 | | { |
| | 67 | 654 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 655 | | { |
| | | 656 | | continue; |
| | | 657 | | } |
| | | 658 | | |
| | 60 | 659 | | string normalizedKey = item.Key.Trim(); |
| | 60 | 660 | | if (normalizedMetadata.ContainsKey(normalizedKey)) |
| | | 661 | | { |
| | 1 | 662 | | throw new ArgumentException( |
| | 1 | 663 | | $"Decision receipt metadata contains keys that collide after trimming: '{normalizedKey}'.", |
| | 1 | 664 | | nameof(metadata)); |
| | | 665 | | } |
| | | 666 | | |
| | 59 | 667 | | normalizedMetadata[normalizedKey] = item.Value?.Trim() ?? string.Empty; |
| | | 668 | | } |
| | | 669 | | |
| | 39 | 670 | | return normalizedMetadata.Count == 0 |
| | 39 | 671 | | ? EmptyMetadata |
| | 39 | 672 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 673 | | } |
| | | 674 | | } |