| | | 1 | | using AsiBackbone.Core.Actors; |
| | | 2 | | using AsiBackbone.Core.Constraints; |
| | | 3 | | using AsiBackbone.Core.Decisions; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Core.Audit; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides a fluent construction path for complex <see cref="AuditResidue" /> values. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// The builder is intended for ergonomic object creation in host code, samples, and tests. It preserves the immutable |
| | | 12 | | /// <see cref="AuditResidue" /> value model by producing a new residue when <see cref="Build" /> is called. |
| | | 13 | | /// </remarks> |
| | | 14 | | public sealed class AuditResidueBuilder |
| | | 15 | | { |
| | | 16 | | private readonly IAsiBackboneActorContext actor; |
| | | 17 | | private readonly string operationName; |
| | | 18 | | private readonly string outcome; |
| | 4 | 19 | | private readonly List<string> reasonCodes = []; |
| | 4 | 20 | | private readonly Dictionary<string, string> metadata = new(StringComparer.Ordinal); |
| | | 21 | | |
| | | 22 | | private string? eventId; |
| | | 23 | | private DateTimeOffset? occurredUtc; |
| | | 24 | | private string? correlationId; |
| | | 25 | | private string? traceId; |
| | | 26 | | private string? policyVersion; |
| | | 27 | | private string? policyHash; |
| | | 28 | | private string? auditResidueId; |
| | | 29 | | private string? spanId; |
| | | 30 | | private string? parentSpanId; |
| | | 31 | | private long? decisionLatencyMs; |
| | | 32 | | private string? constraintSetHash; |
| | | 33 | | private int? constraintCount; |
| | | 34 | | private double? riskScore; |
| | | 35 | | private string? policyScope; |
| | | 36 | | private string? tenantHash; |
| | | 37 | | private string? organizationHash; |
| | | 38 | | private string? emitterStatus; |
| | | 39 | | private string? emitterProvider; |
| | | 40 | | private long? outboxSequence; |
| | | 41 | | private string? gatewayExecutionId; |
| | | 42 | | private string? decisionStage; |
| | | 43 | | private string? schemaVersion; |
| | | 44 | | |
| | 4 | 45 | | private AuditResidueBuilder( |
| | 4 | 46 | | IAsiBackboneActorContext actor, |
| | 4 | 47 | | string operationName, |
| | 4 | 48 | | string outcome, |
| | 4 | 49 | | IEnumerable<string>? reasonCodes = null) |
| | | 50 | | { |
| | 4 | 51 | | ArgumentNullException.ThrowIfNull(actor); |
| | | 52 | | |
| | 4 | 53 | | this.actor = actor; |
| | 4 | 54 | | this.operationName = operationName; |
| | 4 | 55 | | this.outcome = outcome; |
| | 4 | 56 | | _ = WithReasonCodes(reasonCodes); |
| | 4 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Creates a builder for a host-defined audit outcome. |
| | | 61 | | /// </summary> |
| | | 62 | | public static AuditResidueBuilder Create( |
| | | 63 | | IAsiBackboneActorContext actor, |
| | | 64 | | string operationName, |
| | | 65 | | string outcome) |
| | | 66 | | { |
| | 2 | 67 | | return new AuditResidueBuilder(actor, operationName, outcome); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Creates a builder initialized from a governance decision. |
| | | 72 | | /// </summary> |
| | | 73 | | public static AuditResidueBuilder FromDecision( |
| | | 74 | | IAsiBackboneActorContext actor, |
| | | 75 | | string operationName, |
| | | 76 | | GovernanceDecision decision) |
| | | 77 | | { |
| | 2 | 78 | | ArgumentNullException.ThrowIfNull(decision); |
| | | 79 | | |
| | 1 | 80 | | return new AuditResidueBuilder( |
| | 1 | 81 | | actor, |
| | 1 | 82 | | operationName, |
| | 1 | 83 | | decision.Outcome.ToString(), |
| | 1 | 84 | | decision.ReasonCodes) |
| | 1 | 85 | | .WithCorrelationId(decision.CorrelationId) |
| | 1 | 86 | | .WithTraceId(decision.TraceId) |
| | 1 | 87 | | .WithPolicyVersion(decision.PolicyVersion) |
| | 1 | 88 | | .WithPolicyHash(decision.PolicyHash); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Creates a builder initialized from a constraint evaluation result. |
| | | 93 | | /// </summary> |
| | | 94 | | public static AuditResidueBuilder FromConstraint( |
| | | 95 | | IAsiBackboneActorContext actor, |
| | | 96 | | string operationName, |
| | | 97 | | ConstraintEvaluationResult constraintResult) |
| | | 98 | | { |
| | 2 | 99 | | ArgumentNullException.ThrowIfNull(constraintResult); |
| | | 100 | | |
| | 1 | 101 | | return new AuditResidueBuilder( |
| | 1 | 102 | | actor, |
| | 1 | 103 | | operationName, |
| | 1 | 104 | | constraintResult.Outcome.ToString(), |
| | 1 | 105 | | constraintResult.ReasonCodes); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Sets the audit event identifier. |
| | | 110 | | /// </summary> |
| | | 111 | | public AuditResidueBuilder WithEventId(string? value) |
| | | 112 | | { |
| | 3 | 113 | | eventId = value; |
| | 3 | 114 | | return this; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Sets the event occurrence timestamp. |
| | | 119 | | /// </summary> |
| | | 120 | | public AuditResidueBuilder WithOccurredUtc(DateTimeOffset? value) |
| | | 121 | | { |
| | 3 | 122 | | occurredUtc = value; |
| | 3 | 123 | | return this; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Replaces the reason-code collection. |
| | | 128 | | /// </summary> |
| | | 129 | | public AuditResidueBuilder WithReasonCodes(IEnumerable<string>? values) |
| | | 130 | | { |
| | 4 | 131 | | reasonCodes.Clear(); |
| | | 132 | | |
| | 4 | 133 | | if (values is not null) |
| | | 134 | | { |
| | 2 | 135 | | reasonCodes.AddRange(values); |
| | | 136 | | } |
| | | 137 | | |
| | 4 | 138 | | return this; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Adds one reason code to the builder. |
| | | 143 | | /// </summary> |
| | | 144 | | public AuditResidueBuilder AddReasonCode(string value) |
| | | 145 | | { |
| | 4 | 146 | | reasonCodes.Add(value); |
| | 4 | 147 | | return this; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Sets the correlation identifier. |
| | | 152 | | /// </summary> |
| | | 153 | | public AuditResidueBuilder WithCorrelationId(string? value) |
| | | 154 | | { |
| | 3 | 155 | | correlationId = value; |
| | 3 | 156 | | return this; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Sets the trace identifier. |
| | | 161 | | /// </summary> |
| | | 162 | | public AuditResidueBuilder WithTraceId(string? value) |
| | | 163 | | { |
| | 3 | 164 | | traceId = value; |
| | 3 | 165 | | return this; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Sets the policy version. |
| | | 170 | | /// </summary> |
| | | 171 | | public AuditResidueBuilder WithPolicyVersion(string? value) |
| | | 172 | | { |
| | 3 | 173 | | policyVersion = value; |
| | 3 | 174 | | return this; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Sets the policy hash. |
| | | 179 | | /// </summary> |
| | | 180 | | public AuditResidueBuilder WithPolicyHash(string? value) |
| | | 181 | | { |
| | 3 | 182 | | policyHash = value; |
| | 3 | 183 | | return this; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Replaces the metadata collection. |
| | | 188 | | /// </summary> |
| | | 189 | | public AuditResidueBuilder WithMetadata(IReadOnlyDictionary<string, string>? values) |
| | | 190 | | { |
| | 2 | 191 | | metadata.Clear(); |
| | | 192 | | |
| | 2 | 193 | | if (values is not null) |
| | | 194 | | { |
| | 10 | 195 | | foreach (KeyValuePair<string, string> item in values) |
| | | 196 | | { |
| | 3 | 197 | | metadata[item.Key] = item.Value; |
| | | 198 | | } |
| | | 199 | | } |
| | | 200 | | |
| | 2 | 201 | | return this; |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Adds or replaces one metadata value. |
| | | 206 | | /// </summary> |
| | | 207 | | public AuditResidueBuilder AddMetadata(string key, string value) |
| | | 208 | | { |
| | 3 | 209 | | metadata[key] = value; |
| | 3 | 210 | | return this; |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Sets the stable audit residue identifier. |
| | | 215 | | /// </summary> |
| | | 216 | | public AuditResidueBuilder WithAuditResidueId(string? value) |
| | | 217 | | { |
| | 2 | 218 | | auditResidueId = value; |
| | 2 | 219 | | return this; |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Sets the span identifier. |
| | | 224 | | /// </summary> |
| | | 225 | | public AuditResidueBuilder WithSpanId(string? value) |
| | | 226 | | { |
| | 2 | 227 | | spanId = value; |
| | 2 | 228 | | return this; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | /// <summary> |
| | | 232 | | /// Sets the parent span identifier. |
| | | 233 | | /// </summary> |
| | | 234 | | public AuditResidueBuilder WithParentSpanId(string? value) |
| | | 235 | | { |
| | 2 | 236 | | parentSpanId = value; |
| | 2 | 237 | | return this; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Sets the decision latency in milliseconds. |
| | | 242 | | /// </summary> |
| | | 243 | | public AuditResidueBuilder WithDecisionLatencyMs(long? value) |
| | | 244 | | { |
| | 2 | 245 | | decisionLatencyMs = value; |
| | 2 | 246 | | return this; |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | /// <summary> |
| | | 250 | | /// Sets the evaluated constraint-set hash. |
| | | 251 | | /// </summary> |
| | | 252 | | public AuditResidueBuilder WithConstraintSetHash(string? value) |
| | | 253 | | { |
| | 2 | 254 | | constraintSetHash = value; |
| | 2 | 255 | | return this; |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Sets the evaluated constraint count. |
| | | 260 | | /// </summary> |
| | | 261 | | public AuditResidueBuilder WithConstraintCount(int? value) |
| | | 262 | | { |
| | 2 | 263 | | constraintCount = value; |
| | 2 | 264 | | return this; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Sets the host-defined risk score. |
| | | 269 | | /// </summary> |
| | | 270 | | public AuditResidueBuilder WithRiskScore(double? value) |
| | | 271 | | { |
| | 2 | 272 | | riskScore = value; |
| | 2 | 273 | | return this; |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | /// <summary> |
| | | 277 | | /// Sets the host-defined policy scope. |
| | | 278 | | /// </summary> |
| | | 279 | | public AuditResidueBuilder WithPolicyScope(string? value) |
| | | 280 | | { |
| | 2 | 281 | | policyScope = value; |
| | 2 | 282 | | return this; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Sets the privacy-preserving tenant hash. |
| | | 287 | | /// </summary> |
| | | 288 | | public AuditResidueBuilder WithTenantHash(string? value) |
| | | 289 | | { |
| | 2 | 290 | | tenantHash = value; |
| | 2 | 291 | | return this; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <summary> |
| | | 295 | | /// Sets the privacy-preserving organization hash. |
| | | 296 | | /// </summary> |
| | | 297 | | public AuditResidueBuilder WithOrganizationHash(string? value) |
| | | 298 | | { |
| | 2 | 299 | | organizationHash = value; |
| | 2 | 300 | | return this; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Sets the provider-neutral emitter status. |
| | | 305 | | /// </summary> |
| | | 306 | | public AuditResidueBuilder WithEmitterStatus(string? value) |
| | | 307 | | { |
| | 2 | 308 | | emitterStatus = value; |
| | 2 | 309 | | return this; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Sets the provider-neutral emitter provider name. |
| | | 314 | | /// </summary> |
| | | 315 | | public AuditResidueBuilder WithEmitterProvider(string? value) |
| | | 316 | | { |
| | 2 | 317 | | emitterProvider = value; |
| | 2 | 318 | | return this; |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Sets the outbox sequence. |
| | | 323 | | /// </summary> |
| | | 324 | | public AuditResidueBuilder WithOutboxSequence(long? value) |
| | | 325 | | { |
| | 2 | 326 | | outboxSequence = value; |
| | 2 | 327 | | return this; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Sets the gateway execution identifier. |
| | | 332 | | /// </summary> |
| | | 333 | | public AuditResidueBuilder WithGatewayExecutionId(string? value) |
| | | 334 | | { |
| | 2 | 335 | | gatewayExecutionId = value; |
| | 2 | 336 | | return this; |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | /// <summary> |
| | | 340 | | /// Sets the provider-neutral decision stage. |
| | | 341 | | /// </summary> |
| | | 342 | | public AuditResidueBuilder WithDecisionStage(string? value) |
| | | 343 | | { |
| | 2 | 344 | | decisionStage = value; |
| | 2 | 345 | | return this; |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | /// <summary> |
| | | 349 | | /// Sets the audit residue schema version. |
| | | 350 | | /// </summary> |
| | | 351 | | public AuditResidueBuilder WithSchemaVersion(string? value) |
| | | 352 | | { |
| | 2 | 353 | | schemaVersion = value; |
| | 2 | 354 | | return this; |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// Builds an immutable audit residue value. |
| | | 359 | | /// </summary> |
| | | 360 | | public AuditResidue Build() |
| | | 361 | | { |
| | 5 | 362 | | return AuditResidue.Create( |
| | 5 | 363 | | actor, |
| | 5 | 364 | | operationName, |
| | 5 | 365 | | outcome, |
| | 5 | 366 | | reasonCodes, |
| | 5 | 367 | | eventId, |
| | 5 | 368 | | occurredUtc, |
| | 5 | 369 | | correlationId, |
| | 5 | 370 | | traceId, |
| | 5 | 371 | | policyVersion, |
| | 5 | 372 | | policyHash, |
| | 5 | 373 | | metadata, |
| | 5 | 374 | | auditResidueId, |
| | 5 | 375 | | spanId, |
| | 5 | 376 | | parentSpanId, |
| | 5 | 377 | | decisionLatencyMs, |
| | 5 | 378 | | constraintSetHash, |
| | 5 | 379 | | constraintCount, |
| | 5 | 380 | | riskScore, |
| | 5 | 381 | | policyScope, |
| | 5 | 382 | | tenantHash, |
| | 5 | 383 | | organizationHash, |
| | 5 | 384 | | emitterStatus, |
| | 5 | 385 | | emitterProvider, |
| | 5 | 386 | | outboxSequence, |
| | 5 | 387 | | gatewayExecutionId, |
| | 5 | 388 | | decisionStage, |
| | 5 | 389 | | schemaVersion); |
| | | 390 | | } |
| | | 391 | | } |