< Summary

Information
Class: AsiBackbone.Core.Audit.DecisionReceipt
Assembly: AsiBackbone.Core
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Core/Audit/DecisionReceipt.cs
Line coverage
100%
Covered lines: 291
Uncovered lines: 0
Coverable lines: 291
Total lines: 724
Line coverage: 100%
Branch coverage
100%
Covered branches: 64
Total branches: 64
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
get_HasReasonCodes()100%11100%
get_HasMetadata()100%11100%
WithCapabilityGrantId(...)100%11100%
Create(...)100%11100%
FromDecision(...)100%11100%
FromConstraint(...)100%11100%
CreateCore(...)100%44100%
NormalizeIdentifier(...)100%22100%
NormalizeDecisionReceiptId(...)100%22100%
NormalizeOptional(...)100%22100%
NormalizeNonNegative(...)100%22100%
NormalizeNonNegative(...)100%22100%
NormalizeRiskScore(...)100%88100%
NormalizeReasonCodes(...)100%1818100%
UseTrustedReasonCodes(...)100%22100%
AddNormalizedReasonCode(...)100%22100%
CreateReasonCodeCollection(...)100%44100%
NormalizeMetadata(...)100%1616100%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Core/Audit/DecisionReceipt.cs

#LineLine coverage
 1using System.Collections.ObjectModel;
 2using AsiBackbone.Core.Actors;
 3using AsiBackbone.Core.Constraints;
 4using AsiBackbone.Core.Decisions;
 5using AsiBackbone.Core.Serialization;
 6using AsiBackbone.Core.Signing;
 7
 8namespace AsiBackbone.Core.Audit;
 9
 10/// <summary>
 11/// Represents the framework-neutral decision receipt produced by an AsiBackbone operation.
 12/// </summary>
 13public sealed class DecisionReceipt : IDecisionReceipt
 14{
 515    private static readonly ReadOnlyCollection<string> EmptyReasonCodes =
 516        Array.AsReadOnly(Array.Empty<string>());
 17
 518    private static readonly IReadOnlyDictionary<string, string> EmptyMetadata =
 519        new ReadOnlyDictionary<string, string>(
 520            new Dictionary<string, string>(StringComparer.Ordinal));
 21
 20122    private DecisionReceipt(
 20123        string eventId,
 20124        string decisionReceiptId,
 20125        string schemaVersion,
 20126        DateTimeOffset occurredUtc,
 20127        string actorId,
 20128        GovernanceActorType actorType,
 20129        string? actorDisplayName,
 20130        string operationName,
 20131        string outcome,
 20132        IReadOnlyList<string> reasonCodes,
 20133        string? correlationId,
 20134        string? traceId,
 20135        string? spanId,
 20136        string? parentSpanId,
 20137        long? decisionLatencyMs,
 20138        string? constraintSetHash,
 20139        int? constraintCount,
 20140        double? riskScore,
 20141        string? policyScope,
 20142        string? tenantHash,
 20143        string? organizationHash,
 20144        string? emitterStatus,
 20145        string? emitterProvider,
 20146        long? outboxSequence,
 20147        string? gatewayExecutionId,
 20148        string? decisionStage,
 20149        string? policyVersion,
 20150        string? policyHash,
 20151        IReadOnlyDictionary<string, string> metadata,
 20152        string? capabilityGrantId)
 53    {
 20154        ArgumentException.ThrowIfNullOrWhiteSpace(eventId);
 20155        ArgumentException.ThrowIfNullOrWhiteSpace(decisionReceiptId);
 20156        ArgumentException.ThrowIfNullOrWhiteSpace(actorId);
 20157        ArgumentException.ThrowIfNullOrWhiteSpace(operationName);
 19858        ArgumentException.ThrowIfNullOrWhiteSpace(outcome);
 59
 19560        EventId = eventId.Trim();
 19561        DecisionReceiptId = decisionReceiptId.Trim();
 19562        SchemaVersion = GovernanceSchemaVersions.Normalize(schemaVersion);
 19563        OccurredUtc = occurredUtc.ToUniversalTime();
 19564        ActorId = actorId.Trim();
 19565        ActorType = actorType;
 19566        ActorDisplayName = NormalizeOptional(actorDisplayName);
 19567        OperationName = operationName.Trim();
 19568        Outcome = outcome.Trim();
 19569        ReasonCodes = reasonCodes;
 19570        CorrelationId = NormalizeOptional(correlationId);
 19571        TraceId = NormalizeOptional(traceId);
 19572        SpanId = NormalizeOptional(spanId);
 19573        ParentSpanId = NormalizeOptional(parentSpanId);
 19574        DecisionLatencyMs = NormalizeNonNegative(decisionLatencyMs, nameof(decisionLatencyMs));
 19275        ConstraintSetHash = NormalizeOptional(constraintSetHash);
 19276        ConstraintCount = NormalizeNonNegative(constraintCount, nameof(constraintCount));
 18977        RiskScore = NormalizeRiskScore(riskScore);
 18278        PolicyScope = NormalizeOptional(policyScope);
 18279        TenantHash = NormalizeOptional(tenantHash);
 18280        OrganizationHash = NormalizeOptional(organizationHash);
 18281        EmitterStatus = NormalizeOptional(emitterStatus);
 18282        EmitterProvider = NormalizeOptional(emitterProvider);
 18283        OutboxSequence = NormalizeNonNegative(outboxSequence, nameof(outboxSequence));
 17984        GatewayExecutionId = NormalizeOptional(gatewayExecutionId);
 17985        DecisionStage = NormalizeOptional(decisionStage);
 17986        PolicyVersion = NormalizeOptional(policyVersion);
 17987        PolicyHash = NormalizeOptional(policyHash);
 17988        Metadata = metadata;
 17989        CapabilityGrantId = NormalizeOptional(capabilityGrantId);
 17990    }
 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>
 15185    public bool HasReasonCodes => ReasonCodes.Count > 0;
 186
 187    /// <summary>
 188    /// Gets a value indicating whether this decision receipt contains metadata.
 189    /// </summary>
 10190    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    {
 9203        return new DecisionReceipt(
 9204            EventId,
 9205            DecisionReceiptId,
 9206            SchemaVersion,
 9207            OccurredUtc,
 9208            ActorId,
 9209            ActorType,
 9210            ActorDisplayName,
 9211            OperationName,
 9212            Outcome,
 9213            ReasonCodes,
 9214            CorrelationId,
 9215            TraceId,
 9216            SpanId,
 9217            ParentSpanId,
 9218            DecisionLatencyMs,
 9219            ConstraintSetHash,
 9220            ConstraintCount,
 9221            RiskScore,
 9222            PolicyScope,
 9223            TenantHash,
 9224            OrganizationHash,
 9225            EmitterStatus,
 9226            EmitterProvider,
 9227            OutboxSequence,
 9228            GatewayExecutionId,
 9229            DecisionStage,
 9230            PolicyVersion,
 9231            PolicyHash,
 9232            Metadata,
 9233            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    {
 150296        return CreateCore(
 150297            actor,
 150298            operationName,
 150299            outcome,
 150300            NormalizeReasonCodes(reasonCodes),
 150301            eventId,
 150302            occurredUtc,
 150303            correlationId,
 150304            traceId,
 150305            policyVersion,
 150306            policyHash,
 150307            metadata,
 150308            decisionReceiptId,
 150309            spanId,
 150310            parentSpanId,
 150311            decisionLatencyMs,
 150312            constraintSetHash,
 150313            constraintCount,
 150314            riskScore,
 150315            policyScope,
 150316            tenantHash,
 150317            organizationHash,
 150318            emitterStatus,
 150319            emitterProvider,
 150320            outboxSequence,
 150321            gatewayExecutionId,
 150322            decisionStage,
 150323            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    {
 34376        ArgumentNullException.ThrowIfNull(decision);
 377
 33378        return CreateCore(
 33379            actor,
 33380            operationName,
 33381            CanonicalEnumWireNames.ForDecisionOutcome(decision.Outcome),
 33382            UseTrustedReasonCodes(decision.ReasonCodes),
 33383            eventId,
 33384            occurredUtc,
 33385            decision.CorrelationId,
 33386            decision.TraceId,
 33387            decision.PolicyVersion,
 33388            decision.PolicyHash,
 33389            metadata,
 33390            decisionReceiptId,
 33391            spanId,
 33392            parentSpanId,
 33393            decisionLatencyMs,
 33394            constraintSetHash,
 33395            constraintCount,
 33396            riskScore,
 33397            policyScope,
 33398            tenantHash,
 33399            organizationHash,
 33400            emitterStatus,
 33401            emitterProvider,
 33402            outboxSequence,
 33403            gatewayExecutionId,
 33404            decisionStage,
 33405            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    {
 12466        ArgumentNullException.ThrowIfNull(constraintResult);
 467
 11468        return CreateCore(
 11469            actor,
 11470            operationName,
 11471            CanonicalEnumWireNames.ForConstraintOutcome(constraintResult.Outcome),
 11472            UseTrustedReasonCodes(constraintResult.ReasonCodes),
 11473            eventId,
 11474            occurredUtc,
 11475            correlationId,
 11476            traceId,
 11477            policyVersion,
 11478            policyHash,
 11479            metadata,
 11480            decisionReceiptId,
 11481            spanId,
 11482            parentSpanId,
 11483            decisionLatencyMs,
 11484            constraintSetHash,
 11485            constraintCount,
 11486            riskScore,
 11487            policyScope,
 11488            tenantHash,
 11489            organizationHash,
 11490            emitterStatus,
 11491            emitterProvider,
 11492            outboxSequence,
 11493            gatewayExecutionId,
 11494            decisionStage,
 11495            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    {
 194527        ArgumentNullException.ThrowIfNull(actor);
 528
 193529        string normalizedEventId = NormalizeIdentifier(eventId);
 530
 193531        return new DecisionReceipt(
 193532            normalizedEventId,
 193533            NormalizeDecisionReceiptId(decisionReceiptId, normalizedEventId),
 193534            schemaVersion ?? GovernanceSchemaVersions.StableArtifactsV1,
 193535            occurredUtc ?? DateTimeOffset.UtcNow,
 193536            actor.ActorId,
 193537            actor.ActorType,
 193538            actor.DisplayName,
 193539            operationName,
 193540            outcome,
 193541            reasonCodes,
 193542            correlationId,
 193543            traceId,
 193544            spanId,
 193545            parentSpanId,
 193546            decisionLatencyMs,
 193547            constraintSetHash,
 193548            constraintCount,
 193549            riskScore,
 193550            policyScope,
 193551            tenantHash,
 193552            organizationHash,
 193553            emitterStatus,
 193554            emitterProvider,
 193555            outboxSequence,
 193556            gatewayExecutionId,
 193557            decisionStage,
 193558            policyVersion,
 193559            policyHash,
 193560            NormalizeMetadata(metadata),
 193561            capabilityGrantId: null);
 562    }
 563
 564    private static string NormalizeIdentifier(string? identifier)
 565    {
 193566        return string.IsNullOrWhiteSpace(identifier)
 193567            ? Guid.NewGuid().ToString("N")
 193568            : identifier.Trim();
 569    }
 570
 571    private static string NormalizeDecisionReceiptId(string? decisionReceiptId, string eventId)
 572    {
 193573        return string.IsNullOrWhiteSpace(decisionReceiptId)
 193574            ? eventId
 193575            : decisionReceiptId.Trim();
 576    }
 577
 578    private static string? NormalizeOptional(string? value)
 579    {
 2972580        return string.IsNullOrWhiteSpace(value)
 2972581            ? null
 2972582            : value.Trim();
 583    }
 584
 585    private static long? NormalizeNonNegative(long? value, string parameterName)
 586    {
 377587        return value < 0
 377588            ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero."
 377589            : value;
 590    }
 591
 592    private static int? NormalizeNonNegative(int? value, string parameterName)
 593    {
 192594        return value < 0
 192595            ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero."
 192596            : value;
 597    }
 598
 599    private static double? NormalizeRiskScore(double? riskScore)
 600    {
 189601        return riskScore is null
 189602            ? null
 189603            : double.IsNaN(riskScore.Value) || double.IsInfinity(riskScore.Value) || riskScore.Value < 0
 189604            ? throw new ArgumentOutOfRangeException(nameof(riskScore), riskScore, "Risk score must be a finite value gre
 189605            : riskScore;
 606    }
 607
 608    private static ReadOnlyCollection<string> NormalizeReasonCodes(IEnumerable<string>? reasonCodes)
 609    {
 150610        if (reasonCodes is null)
 611        {
 69612            return EmptyReasonCodes;
 613        }
 614
 81615        if (reasonCodes is ICollection<string> collection)
 616        {
 79617            if (collection.Count == 0)
 618            {
 18619                return EmptyReasonCodes;
 620            }
 621
 61622            string[] normalizedReasonCodes = new string[collection.Count];
 61623            int normalizedCount = 0;
 624
 288625            foreach (string? reasonCode in collection)
 626            {
 83627                AddNormalizedReasonCode(reasonCode, normalizedReasonCodes, ref normalizedCount);
 628            }
 629
 61630            return CreateReasonCodeCollection(normalizedReasonCodes, normalizedCount);
 631        }
 632
 2633        List<string>? normalizedList = null;
 634
 16635        foreach (string? reasonCode in reasonCodes)
 636        {
 6637            if (string.IsNullOrWhiteSpace(reasonCode))
 638            {
 639                continue;
 640            }
 641
 2642            normalizedList ??= [];
 2643            normalizedList.Add(reasonCode.Trim());
 644        }
 645
 2646        return normalizedList is null || normalizedList.Count == 0
 2647            ? EmptyReasonCodes
 2648            : Array.AsReadOnly([.. normalizedList]);
 649    }
 650
 651    private static IReadOnlyList<string> UseTrustedReasonCodes(IReadOnlyList<string> reasonCodes)
 652    {
 44653        return reasonCodes.Count == 0
 44654            ? EmptyReasonCodes
 44655            : reasonCodes;
 656    }
 657
 658    private static void AddNormalizedReasonCode(
 659        string? reasonCode,
 660        string[] normalizedReasonCodes,
 661        ref int normalizedCount)
 662    {
 83663        if (string.IsNullOrWhiteSpace(reasonCode))
 664        {
 10665            return;
 666        }
 667
 73668        normalizedReasonCodes[normalizedCount] = reasonCode.Trim();
 73669        normalizedCount++;
 73670    }
 671
 672    private static ReadOnlyCollection<string> CreateReasonCodeCollection(
 673        string[] normalizedReasonCodes,
 674        int normalizedCount)
 675    {
 61676        if (normalizedCount == 0)
 677        {
 1678            return EmptyReasonCodes;
 679        }
 680
 60681        if (normalizedCount == normalizedReasonCodes.Length)
 682        {
 55683            return Array.AsReadOnly(normalizedReasonCodes);
 684        }
 685
 5686        string[] filteredReasonCodes = new string[normalizedCount];
 5687        Array.Copy(normalizedReasonCodes, filteredReasonCodes, normalizedCount);
 688
 5689        return Array.AsReadOnly(filteredReasonCodes);
 690    }
 691
 692    private static IReadOnlyDictionary<string, string> NormalizeMetadata(
 693        IReadOnlyDictionary<string, string>? metadata)
 694    {
 193695        if (metadata is null || metadata.Count == 0)
 696        {
 139697            return EmptyMetadata;
 698        }
 699
 54700        Dictionary<string, string> normalizedMetadata = new(metadata.Count, StringComparer.Ordinal);
 701
 363702        foreach (KeyValuePair<string, string> item in metadata)
 703        {
 128704            if (string.IsNullOrWhiteSpace(item.Key))
 705            {
 706                continue;
 707            }
 708
 121709            string normalizedKey = item.Key.Trim();
 121710            if (normalizedMetadata.ContainsKey(normalizedKey))
 711            {
 1712                throw new ArgumentException(
 1713                    $"Decision receipt metadata contains keys that collide after trimming: '{normalizedKey}'.",
 1714                    nameof(metadata));
 715            }
 716
 120717            normalizedMetadata[normalizedKey] = item.Value?.Trim() ?? string.Empty;
 718        }
 719
 53720        return normalizedMetadata.Count == 0
 53721            ? EmptyMetadata
 53722            : new ReadOnlyDictionary<string, string>(normalizedMetadata);
 723    }
 724}

Methods/Properties

.cctor()
.ctor(System.String,System.String,System.String,System.DateTimeOffset,System.String,AsiBackbone.Core.Actors.GovernanceActorType,System.String,System.String,System.String,System.Collections.Generic.IReadOnlyList`1<System.String>,System.String,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.Nullable`1<System.Int32>,System.Nullable`1<System.Double>,System.String,System.String,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.String,System.String,System.String,System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.String>,System.String)
get_HasReasonCodes()
get_HasMetadata()
WithCapabilityGrantId(System.String)
Create(AsiBackbone.Core.Actors.IGovernanceActorContext,System.String,System.String,System.Collections.Generic.IEnumerable`1<System.String>,System.String,System.Nullable`1<System.DateTimeOffset>,System.String,System.String,System.String,System.String,System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.String>,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.Nullable`1<System.Int32>,System.Nullable`1<System.Double>,System.String,System.String,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.String,System.String)
FromDecision(AsiBackbone.Core.Actors.IGovernanceActorContext,System.String,AsiBackbone.Core.Decisions.GovernanceDecision,System.String,System.Nullable`1<System.DateTimeOffset>,System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.String>,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.Nullable`1<System.Int32>,System.Nullable`1<System.Double>,System.String,System.String,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.String,System.String)
FromConstraint(AsiBackbone.Core.Actors.IGovernanceActorContext,System.String,AsiBackbone.Core.Constraints.ConstraintEvaluationResult,System.String,System.Nullable`1<System.DateTimeOffset>,System.String,System.String,System.String,System.String,System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.String>,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.Nullable`1<System.Int32>,System.Nullable`1<System.Double>,System.String,System.String,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.String,System.String)
CreateCore(AsiBackbone.Core.Actors.IGovernanceActorContext,System.String,System.String,System.Collections.Generic.IReadOnlyList`1<System.String>,System.String,System.Nullable`1<System.DateTimeOffset>,System.String,System.String,System.String,System.String,System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.String>,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.Nullable`1<System.Int32>,System.Nullable`1<System.Double>,System.String,System.String,System.String,System.String,System.String,System.Nullable`1<System.Int64>,System.String,System.String,System.String)
NormalizeIdentifier(System.String)
NormalizeDecisionReceiptId(System.String,System.String)
NormalizeOptional(System.String)
NormalizeNonNegative(System.Nullable`1<System.Int64>,System.String)
NormalizeNonNegative(System.Nullable`1<System.Int32>,System.String)
NormalizeRiskScore(System.Nullable`1<System.Double>)
NormalizeReasonCodes(System.Collections.Generic.IEnumerable`1<System.String>)
UseTrustedReasonCodes(System.Collections.Generic.IReadOnlyList`1<System.String>)
AddNormalizedReasonCode(System.String,System.String[],System.Int32&)
CreateReasonCodeCollection(System.String[],System.Int32)
NormalizeMetadata(System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.String>)