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