| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using System.Globalization; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Core.HostIntegration; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a framework-neutral completion receipt for one governed host execution attempt. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// The receipt binds governance lifecycle evidence to host-owned persistence without copying raw |
| | | 11 | | /// original or current application values into AsiBackbone. Mutation details remain authoritative |
| | | 12 | | /// in the host audit store; this receipt carries only opaque identifiers, counts, hashes, and |
| | | 13 | | /// minimized metadata. |
| | | 14 | | /// </remarks> |
| | | 15 | | public sealed class GovernedOperationExecutionReceipt |
| | | 16 | | { |
| | | 17 | | private const int MaximumIdentifierLength = 256; |
| | | 18 | | private const int MaximumProviderLength = 256; |
| | | 19 | | private const int MaximumHashLength = 1024; |
| | | 20 | | private const int MaximumAlgorithmLength = 128; |
| | | 21 | | private const int MaximumMetadataKeyLength = 128; |
| | | 22 | | private const int MaximumMetadataValueLength = 2048; |
| | | 23 | | |
| | 4 | 24 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 4 | 25 | | new ReadOnlyDictionary<string, string>(new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 26 | | |
| | 32 | 27 | | private GovernedOperationExecutionReceipt( |
| | 32 | 28 | | string operationExecutionId, |
| | 32 | 29 | | string? executionAttemptId, |
| | 32 | 30 | | GovernedOperationPersistenceOutcome persistenceOutcome, |
| | 32 | 31 | | string? mutationBatchId, |
| | 32 | 32 | | int mutationRecordCount, |
| | 32 | 33 | | string? mutationManifestHash, |
| | 32 | 34 | | string? mutationManifestAlgorithm, |
| | 32 | 35 | | DateTimeOffset completedUtc, |
| | 32 | 36 | | string? persistenceProvider, |
| | 32 | 37 | | string? decisionAuditRecordId, |
| | 32 | 38 | | IReadOnlyDictionary<string, string> metadata) |
| | | 39 | | { |
| | 32 | 40 | | OperationExecutionId = operationExecutionId; |
| | 32 | 41 | | ExecutionAttemptId = executionAttemptId; |
| | 32 | 42 | | PersistenceOutcome = persistenceOutcome; |
| | 32 | 43 | | MutationBatchId = mutationBatchId; |
| | 32 | 44 | | MutationRecordCount = mutationRecordCount; |
| | 32 | 45 | | MutationManifestHash = mutationManifestHash; |
| | 32 | 46 | | MutationManifestAlgorithm = mutationManifestAlgorithm; |
| | 32 | 47 | | CompletedUtc = completedUtc; |
| | 32 | 48 | | PersistenceProvider = persistenceProvider; |
| | 32 | 49 | | DecisionAuditRecordId = decisionAuditRecordId; |
| | 32 | 50 | | Metadata = metadata; |
| | 32 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary>Gets the logical execution identifier shared across retries.</summary> |
| | 36 | 54 | | public string OperationExecutionId { get; } |
| | | 55 | | |
| | | 56 | | /// <summary>Gets the identifier for this execution attempt, when supplied.</summary> |
| | 40 | 57 | | public string? ExecutionAttemptId { get; } |
| | | 58 | | |
| | | 59 | | /// <summary>Gets the host persistence outcome.</summary> |
| | 106 | 60 | | public GovernedOperationPersistenceOutcome PersistenceOutcome { get; } |
| | | 61 | | |
| | | 62 | | /// <summary>Gets the opaque host mutation batch identifier, when persistence committed.</summary> |
| | 32 | 63 | | public string? MutationBatchId { get; } |
| | | 64 | | |
| | | 65 | | /// <summary>Gets the number of host-owned mutation records represented by the batch.</summary> |
| | 32 | 66 | | public int MutationRecordCount { get; } |
| | | 67 | | |
| | | 68 | | /// <summary>Gets the canonical privacy-safe mutation manifest hash, when persistence committed.</summary> |
| | 30 | 69 | | public string? MutationManifestHash { get; } |
| | | 70 | | |
| | | 71 | | /// <summary>Gets the mutation manifest hash algorithm, when persistence committed.</summary> |
| | 26 | 72 | | public string? MutationManifestAlgorithm { get; } |
| | | 73 | | |
| | | 74 | | /// <summary>Gets the UTC timestamp when execution completed.</summary> |
| | 24 | 75 | | public DateTimeOffset CompletedUtc { get; } |
| | | 76 | | |
| | | 77 | | /// <summary>Gets the host persistence provider or provider family, when supplied.</summary> |
| | 26 | 78 | | public string? PersistenceProvider { get; } |
| | | 79 | | |
| | | 80 | | /// <summary>Gets the persisted AsiBackbone decision audit record identifier, when supplied.</summary> |
| | 26 | 81 | | public string? DecisionAuditRecordId { get; } |
| | | 82 | | |
| | | 83 | | /// <summary>Gets minimized host metadata associated with the receipt.</summary> |
| | 26 | 84 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 85 | | |
| | | 86 | | /// <summary>Gets a value indicating whether execution completed without persisted mutation.</summary> |
| | 30 | 87 | | public bool CompletedWithoutMutation => PersistenceOutcome == GovernedOperationPersistenceOutcome.CompletedWithoutMu |
| | | 88 | | |
| | | 89 | | /// <summary>Gets a value indicating whether the receipt contains a committed mutation binding.</summary> |
| | 16 | 90 | | public bool HasCommittedMutation => PersistenceOutcome == GovernedOperationPersistenceOutcome.Committed; |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Creates a validated execution completion receipt. |
| | | 94 | | /// </summary> |
| | | 95 | | public static GovernedOperationExecutionReceipt Create( |
| | | 96 | | string operationExecutionId, |
| | | 97 | | GovernedOperationPersistenceOutcome persistenceOutcome, |
| | | 98 | | string? executionAttemptId = null, |
| | | 99 | | string? mutationBatchId = null, |
| | | 100 | | int mutationRecordCount = 0, |
| | | 101 | | string? mutationManifestHash = null, |
| | | 102 | | string? mutationManifestAlgorithm = null, |
| | | 103 | | DateTimeOffset? completedUtc = null, |
| | | 104 | | string? persistenceProvider = null, |
| | | 105 | | string? decisionAuditRecordId = null, |
| | | 106 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 107 | | { |
| | 36 | 108 | | if (!Enum.IsDefined(persistenceOutcome)) |
| | | 109 | | { |
| | 0 | 110 | | throw new ArgumentOutOfRangeException(nameof(persistenceOutcome), persistenceOutcome, "Persistence outcome m |
| | | 111 | | } |
| | | 112 | | |
| | 36 | 113 | | if (mutationRecordCount < 0) |
| | | 114 | | { |
| | 0 | 115 | | throw new ArgumentOutOfRangeException(nameof(mutationRecordCount), mutationRecordCount, "Mutation record cou |
| | | 116 | | } |
| | | 117 | | |
| | 36 | 118 | | string normalizedOperationExecutionId = NormalizeRequired(operationExecutionId, nameof(operationExecutionId), Ma |
| | 36 | 119 | | string? normalizedExecutionAttemptId = NormalizeOptional(executionAttemptId, nameof(executionAttemptId), Maximum |
| | 36 | 120 | | string? normalizedMutationBatchId = NormalizeOptional(mutationBatchId, nameof(mutationBatchId), MaximumIdentifie |
| | 36 | 121 | | string? normalizedManifestHash = NormalizeOptional(mutationManifestHash, nameof(mutationManifestHash), MaximumHa |
| | 36 | 122 | | string? normalizedManifestAlgorithm = NormalizeOptional(mutationManifestAlgorithm, nameof(mutationManifestAlgori |
| | 36 | 123 | | string? normalizedPersistenceProvider = NormalizeOptional(persistenceProvider, nameof(persistenceProvider), Maxi |
| | 36 | 124 | | string? normalizedDecisionAuditRecordId = NormalizeOptional(decisionAuditRecordId, nameof(decisionAuditRecordId) |
| | | 125 | | |
| | 36 | 126 | | ValidateMutationBinding( |
| | 36 | 127 | | persistenceOutcome, |
| | 36 | 128 | | mutationRecordCount, |
| | 36 | 129 | | normalizedMutationBatchId, |
| | 36 | 130 | | normalizedManifestHash, |
| | 36 | 131 | | normalizedManifestAlgorithm); |
| | | 132 | | |
| | 32 | 133 | | return new GovernedOperationExecutionReceipt( |
| | 32 | 134 | | normalizedOperationExecutionId, |
| | 32 | 135 | | normalizedExecutionAttemptId, |
| | 32 | 136 | | persistenceOutcome, |
| | 32 | 137 | | normalizedMutationBatchId, |
| | 32 | 138 | | mutationRecordCount, |
| | 32 | 139 | | normalizedManifestHash, |
| | 32 | 140 | | normalizedManifestAlgorithm, |
| | 32 | 141 | | (completedUtc ?? DateTimeOffset.UtcNow).ToUniversalTime(), |
| | 32 | 142 | | normalizedPersistenceProvider, |
| | 32 | 143 | | normalizedDecisionAuditRecordId, |
| | 32 | 144 | | NormalizeMetadata(metadata)); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Builds lifecycle metadata from the typed receipt and optional minimized host metadata. |
| | | 149 | | /// </summary> |
| | | 150 | | public IReadOnlyDictionary<string, string> ToLifecycleMetadata(IReadOnlyDictionary<string, string>? metadata = null) |
| | | 151 | | { |
| | 22 | 152 | | SortedDictionary<string, string> values = new(StringComparer.Ordinal); |
| | | 153 | | |
| | 22 | 154 | | AddMetadata(values, Metadata); |
| | 22 | 155 | | AddMetadata(values, metadata); |
| | | 156 | | |
| | 22 | 157 | | values[HostAccountabilityMetadataKeys.OperationExecutionId] = OperationExecutionId; |
| | 22 | 158 | | values[HostAccountabilityMetadataKeys.MutationRecordCount] = MutationRecordCount.ToString(CultureInfo.InvariantC |
| | 22 | 159 | | values[HostAccountabilityMetadataKeys.PersistenceOutcome] = PersistenceOutcome.ToString(); |
| | 22 | 160 | | values[HostAccountabilityMetadataKeys.CompletedWithoutMutation] = CompletedWithoutMutation ? "true" : "false"; |
| | | 161 | | |
| | 22 | 162 | | AddOptional(values, HostAccountabilityMetadataKeys.ExecutionAttemptId, ExecutionAttemptId); |
| | 22 | 163 | | AddOptional(values, HostAccountabilityMetadataKeys.MutationBatchId, MutationBatchId); |
| | 22 | 164 | | AddOptional(values, HostAccountabilityMetadataKeys.MutationManifestHash, MutationManifestHash); |
| | 22 | 165 | | AddOptional(values, HostAccountabilityMetadataKeys.MutationManifestAlgorithm, MutationManifestAlgorithm); |
| | 22 | 166 | | AddOptional(values, HostAccountabilityMetadataKeys.PersistenceProvider, PersistenceProvider); |
| | 22 | 167 | | AddOptional(values, HostAccountabilityMetadataKeys.DecisionAuditRecordId, DecisionAuditRecordId); |
| | | 168 | | |
| | 22 | 169 | | return new ReadOnlyDictionary<string, string>(values); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | private static void ValidateMutationBinding( |
| | | 173 | | GovernedOperationPersistenceOutcome outcome, |
| | | 174 | | int mutationRecordCount, |
| | | 175 | | string? mutationBatchId, |
| | | 176 | | string? mutationManifestHash, |
| | | 177 | | string? mutationManifestAlgorithm) |
| | | 178 | | { |
| | 36 | 179 | | bool hasAnyBinding = mutationBatchId is not null || mutationManifestHash is not null || mutationManifestAlgorith |
| | | 180 | | |
| | 36 | 181 | | if (outcome == GovernedOperationPersistenceOutcome.Committed) |
| | | 182 | | { |
| | 20 | 183 | | if (mutationRecordCount < 1) |
| | | 184 | | { |
| | 0 | 185 | | throw new ArgumentException("A committed persistence outcome requires at least one mutation record.", na |
| | | 186 | | } |
| | | 187 | | |
| | 20 | 188 | | if (mutationBatchId is null || mutationManifestHash is null || mutationManifestAlgorithm is null) |
| | | 189 | | { |
| | 2 | 190 | | throw new ArgumentException("A committed persistence outcome requires a mutation batch identifier, manif |
| | | 191 | | } |
| | | 192 | | |
| | 18 | 193 | | return; |
| | | 194 | | } |
| | | 195 | | |
| | 16 | 196 | | if (mutationRecordCount != 0 || hasAnyBinding) |
| | | 197 | | { |
| | 2 | 198 | | throw new ArgumentException("Failed, rolled-back, and no-mutation outcomes cannot claim a committed mutation |
| | | 199 | | } |
| | 14 | 200 | | } |
| | | 201 | | |
| | | 202 | | private static string NormalizeRequired(string value, string parameterName, int maximumLength) |
| | | 203 | | { |
| | 80 | 204 | | ArgumentException.ThrowIfNullOrWhiteSpace(value, parameterName); |
| | 80 | 205 | | return Normalize(value, parameterName, maximumLength); |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | private static string? NormalizeOptional(string? value, string parameterName, int maximumLength) |
| | | 209 | | { |
| | 216 | 210 | | return string.IsNullOrWhiteSpace(value) ? null : Normalize(value, parameterName, maximumLength); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | private static string Normalize(string value, string parameterName, int maximumLength) |
| | | 214 | | { |
| | 274 | 215 | | string normalized = value.Trim(); |
| | 274 | 216 | | return normalized.Length > maximumLength |
| | 274 | 217 | | ? throw new ArgumentOutOfRangeException(parameterName, value, $"Value cannot exceed {maximumLength} characte |
| | 274 | 218 | | : normalized.Any(char.IsControl) |
| | 274 | 219 | | ? throw new ArgumentException("Value cannot contain control characters.", parameterName) |
| | 274 | 220 | | : normalized; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata(IReadOnlyDictionary<string, string>? metadata) |
| | | 224 | | { |
| | 76 | 225 | | if (metadata is null || metadata.Count == 0) |
| | | 226 | | { |
| | 54 | 227 | | return EmptyMetadata; |
| | | 228 | | } |
| | | 229 | | |
| | 22 | 230 | | SortedDictionary<string, string> normalized = new(StringComparer.Ordinal); |
| | 132 | 231 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 232 | | { |
| | 44 | 233 | | string key = NormalizeRequired(item.Key, nameof(metadata), MaximumMetadataKeyLength); |
| | 44 | 234 | | string value = Normalize(item.Value ?? string.Empty, nameof(metadata), MaximumMetadataValueLength); |
| | 44 | 235 | | normalized[key] = value; |
| | | 236 | | } |
| | | 237 | | |
| | 22 | 238 | | return new ReadOnlyDictionary<string, string>(normalized); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | private static void AddMetadata(SortedDictionary<string, string> destination, IReadOnlyDictionary<string, string>? m |
| | | 242 | | { |
| | 44 | 243 | | IReadOnlyDictionary<string, string> normalized = NormalizeMetadata(metadata); |
| | 160 | 244 | | foreach (KeyValuePair<string, string> item in normalized) |
| | | 245 | | { |
| | 36 | 246 | | destination[item.Key] = item.Value; |
| | | 247 | | } |
| | 44 | 248 | | } |
| | | 249 | | |
| | | 250 | | private static void AddOptional(SortedDictionary<string, string> destination, string key, string? value) |
| | | 251 | | { |
| | 132 | 252 | | if (value is not null) |
| | | 253 | | { |
| | 106 | 254 | | destination[key] = value; |
| | | 255 | | } |
| | 132 | 256 | | } |
| | | 257 | | } |