| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Actors; |
| | | 3 | | using AsiBackbone.Core.Decisions; |
| | | 4 | | using AsiBackbone.Core.Serialization; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.Core.Handshakes; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a framework-neutral liability or responsibility handshake request before consequential execution. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class LiabilityHandshakeRequest |
| | | 12 | | { |
| | 3 | 13 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 3 | 14 | | new ReadOnlyDictionary<string, string>( |
| | 3 | 15 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 16 | | |
| | 100 | 17 | | private LiabilityHandshakeRequest( |
| | 100 | 18 | | string handshakeId, |
| | 100 | 19 | | string? schemaVersion, |
| | 100 | 20 | | string actorId, |
| | 100 | 21 | | AsiBackboneActorType actorType, |
| | 100 | 22 | | string? actorDisplayName, |
| | 100 | 23 | | string operationName, |
| | 100 | 24 | | string reasonCode, |
| | 100 | 25 | | string message, |
| | 100 | 26 | | string requiredAcknowledgmentCode, |
| | 100 | 27 | | string requiredAcknowledgmentText, |
| | 100 | 28 | | LiabilityHandshakeRiskLevel riskLevel, |
| | 100 | 29 | | string? riskCategory, |
| | 100 | 30 | | string? correlationId, |
| | 100 | 31 | | string? traceId, |
| | 100 | 32 | | string? policyVersion, |
| | 100 | 33 | | string? policyHash, |
| | 100 | 34 | | IReadOnlyDictionary<string, string> metadata) |
| | | 35 | | { |
| | 100 | 36 | | ArgumentException.ThrowIfNullOrWhiteSpace(handshakeId); |
| | 100 | 37 | | ArgumentException.ThrowIfNullOrWhiteSpace(actorId); |
| | 100 | 38 | | ArgumentException.ThrowIfNullOrWhiteSpace(operationName); |
| | 98 | 39 | | ArgumentException.ThrowIfNullOrWhiteSpace(reasonCode); |
| | 96 | 40 | | ArgumentException.ThrowIfNullOrWhiteSpace(message); |
| | 96 | 41 | | ArgumentException.ThrowIfNullOrWhiteSpace(requiredAcknowledgmentCode); |
| | 94 | 42 | | ArgumentException.ThrowIfNullOrWhiteSpace(requiredAcknowledgmentText); |
| | | 43 | | |
| | 94 | 44 | | HandshakeId = handshakeId.Trim(); |
| | 94 | 45 | | SchemaVersion = AsiBackboneSchemaVersions.Normalize(schemaVersion); |
| | 94 | 46 | | ActorId = actorId.Trim(); |
| | 94 | 47 | | ActorType = actorType; |
| | 94 | 48 | | ActorDisplayName = NormalizeOptional(actorDisplayName); |
| | 94 | 49 | | OperationName = operationName.Trim(); |
| | 94 | 50 | | ReasonCode = reasonCode.Trim(); |
| | 94 | 51 | | Message = message.Trim(); |
| | 94 | 52 | | RequiredAcknowledgmentCode = requiredAcknowledgmentCode.Trim(); |
| | 94 | 53 | | RequiredAcknowledgmentText = requiredAcknowledgmentText.Trim(); |
| | 94 | 54 | | RiskLevel = riskLevel; |
| | 94 | 55 | | RiskCategory = NormalizeOptional(riskCategory); |
| | 94 | 56 | | CorrelationId = NormalizeOptional(correlationId); |
| | 94 | 57 | | TraceId = NormalizeOptional(traceId); |
| | 94 | 58 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 94 | 59 | | PolicyHash = NormalizeOptional(policyHash); |
| | 94 | 60 | | Metadata = metadata; |
| | 94 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets the stable handshake identifier. |
| | | 65 | | /// </summary> |
| | 88 | 66 | | public string HandshakeId { get; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets the schema version for the serialized handshake request shape. |
| | | 70 | | /// </summary> |
| | 4 | 71 | | public string SchemaVersion { get; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the stable actor identifier associated with the handshake. |
| | | 75 | | /// </summary> |
| | 4 | 76 | | public string ActorId { get; } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the actor type associated with the handshake. |
| | | 80 | | /// </summary> |
| | 3 | 81 | | public AsiBackboneActorType ActorType { get; } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the optional display name or label associated with the actor. |
| | | 85 | | /// </summary> |
| | 3 | 86 | | public string? ActorDisplayName { get; } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the operation name requiring acknowledgment. |
| | | 90 | | /// </summary> |
| | 58 | 91 | | public string OperationName { get; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the machine-readable reason code explaining why the handshake is required. |
| | | 95 | | /// </summary> |
| | 60 | 96 | | public string ReasonCode { get; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the human-readable message explaining why the handshake is required. |
| | | 100 | | /// </summary> |
| | 60 | 101 | | public string Message { get; } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets the required acknowledgment code the host may display or require before execution. |
| | | 105 | | /// </summary> |
| | 84 | 106 | | public string RequiredAcknowledgmentCode { get; } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets the required acknowledgment text the host may display before execution. |
| | | 110 | | /// </summary> |
| | 58 | 111 | | public string RequiredAcknowledgmentText { get; } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Gets the risk level associated with the handshake. |
| | | 115 | | /// </summary> |
| | 60 | 116 | | public LiabilityHandshakeRiskLevel RiskLevel { get; } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Gets the optional host-defined risk category associated with the handshake. |
| | | 120 | | /// </summary> |
| | 61 | 121 | | public string? RiskCategory { get; } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets the correlation identifier associated with the handshake, when supplied by the host. |
| | | 125 | | /// </summary> |
| | 88 | 126 | | public string? CorrelationId { get; } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets the trace identifier associated with the handshake, when supplied by the host. |
| | | 130 | | /// </summary> |
| | 50 | 131 | | public string? TraceId { get; } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Gets the policy version associated with the handshake, when supplied by the host. |
| | | 135 | | /// </summary> |
| | 24 | 136 | | public string? PolicyVersion { get; } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets the policy hash associated with the handshake, when supplied by the host. |
| | | 140 | | /// </summary> |
| | 24 | 141 | | public string? PolicyHash { get; } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets additional framework-neutral handshake metadata supplied by the host. |
| | | 145 | | /// </summary> |
| | 81 | 146 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Gets a value indicating whether this handshake contains metadata. |
| | | 150 | | /// </summary> |
| | 9 | 151 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Creates a liability or responsibility handshake request. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <param name="actor">The actor associated with the handshake.</param> |
| | | 157 | | /// <param name="operationName">The operation name requiring acknowledgment.</param> |
| | | 158 | | /// <param name="reasonCode">The machine-readable reason code.</param> |
| | | 159 | | /// <param name="message">The human-readable reason message.</param> |
| | | 160 | | /// <param name="requiredAcknowledgmentCode">The required acknowledgment code.</param> |
| | | 161 | | /// <param name="requiredAcknowledgmentText">The required acknowledgment text.</param> |
| | | 162 | | /// <param name="riskLevel">The risk level associated with the handshake.</param> |
| | | 163 | | /// <param name="riskCategory">Optional host-defined risk category.</param> |
| | | 164 | | /// <param name="handshakeId">Optional handshake identifier. When omitted, a new identifier is generated.</param> |
| | | 165 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 166 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 167 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 168 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 169 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | | 170 | | /// <param name="schemaVersion">Optional schema version for serialized or persisted handshake records.</param> |
| | | 171 | | /// <returns>A liability handshake request.</returns> |
| | | 172 | | public static LiabilityHandshakeRequest Create( |
| | | 173 | | IAsiBackboneActorContext actor, |
| | | 174 | | string operationName, |
| | | 175 | | string reasonCode, |
| | | 176 | | string message, |
| | | 177 | | string requiredAcknowledgmentCode, |
| | | 178 | | string requiredAcknowledgmentText, |
| | | 179 | | LiabilityHandshakeRiskLevel riskLevel = LiabilityHandshakeRiskLevel.Unspecified, |
| | | 180 | | string? riskCategory = null, |
| | | 181 | | string? handshakeId = null, |
| | | 182 | | string? correlationId = null, |
| | | 183 | | string? traceId = null, |
| | | 184 | | string? policyVersion = null, |
| | | 185 | | string? policyHash = null, |
| | | 186 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 187 | | string? schemaVersion = null) |
| | | 188 | | { |
| | 101 | 189 | | ArgumentNullException.ThrowIfNull(actor); |
| | | 190 | | |
| | 100 | 191 | | return new LiabilityHandshakeRequest( |
| | 100 | 192 | | NormalizeIdentifier(handshakeId), |
| | 100 | 193 | | schemaVersion, |
| | 100 | 194 | | actor.ActorId, |
| | 100 | 195 | | actor.ActorType, |
| | 100 | 196 | | actor.DisplayName, |
| | 100 | 197 | | operationName, |
| | 100 | 198 | | reasonCode, |
| | 100 | 199 | | message, |
| | 100 | 200 | | requiredAcknowledgmentCode, |
| | 100 | 201 | | requiredAcknowledgmentText, |
| | 100 | 202 | | riskLevel, |
| | 100 | 203 | | riskCategory, |
| | 100 | 204 | | correlationId, |
| | 100 | 205 | | traceId, |
| | 100 | 206 | | policyVersion, |
| | 100 | 207 | | policyHash, |
| | 100 | 208 | | NormalizeMetadata(metadata)); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Creates a liability or responsibility handshake request from a governance decision. |
| | | 213 | | /// </summary> |
| | | 214 | | /// <param name="actor">The actor associated with the handshake.</param> |
| | | 215 | | /// <param name="operationName">The operation name requiring acknowledgment.</param> |
| | | 216 | | /// <param name="decision">The governance decision requiring acknowledgment.</param> |
| | | 217 | | /// <param name="requiredAcknowledgmentCode">The required acknowledgment code.</param> |
| | | 218 | | /// <param name="requiredAcknowledgmentText">The required acknowledgment text.</param> |
| | | 219 | | /// <param name="riskLevel">The risk level associated with the handshake.</param> |
| | | 220 | | /// <param name="riskCategory">Optional host-defined risk category.</param> |
| | | 221 | | /// <param name="handshakeId">Optional handshake identifier. When omitted, a new identifier is generated.</param> |
| | | 222 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | | 223 | | /// <param name="schemaVersion">Optional schema version for serialized or persisted handshake records.</param> |
| | | 224 | | /// <returns>A liability handshake request.</returns> |
| | | 225 | | public static LiabilityHandshakeRequest FromDecision( |
| | | 226 | | IAsiBackboneActorContext actor, |
| | | 227 | | string operationName, |
| | | 228 | | GovernanceDecision decision, |
| | | 229 | | string requiredAcknowledgmentCode, |
| | | 230 | | string requiredAcknowledgmentText, |
| | | 231 | | LiabilityHandshakeRiskLevel riskLevel = LiabilityHandshakeRiskLevel.Unspecified, |
| | | 232 | | string? riskCategory = null, |
| | | 233 | | string? handshakeId = null, |
| | | 234 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 235 | | string? schemaVersion = null) |
| | | 236 | | { |
| | 49 | 237 | | ArgumentNullException.ThrowIfNull(decision); |
| | | 238 | | |
| | 49 | 239 | | string reasonCode = decision.ReasonCodes.Count > 0 |
| | 49 | 240 | | ? decision.ReasonCodes[0] |
| | 49 | 241 | | : "handshake.required"; |
| | | 242 | | |
| | 49 | 243 | | string message = decision.Reasons.Count > 0 |
| | 49 | 244 | | ? decision.Reasons[0].Message |
| | 49 | 245 | | : "Acknowledgment is required before proceeding."; |
| | | 246 | | |
| | 49 | 247 | | return Create( |
| | 49 | 248 | | actor, |
| | 49 | 249 | | operationName, |
| | 49 | 250 | | reasonCode, |
| | 49 | 251 | | message, |
| | 49 | 252 | | requiredAcknowledgmentCode, |
| | 49 | 253 | | requiredAcknowledgmentText, |
| | 49 | 254 | | riskLevel, |
| | 49 | 255 | | riskCategory, |
| | 49 | 256 | | handshakeId, |
| | 49 | 257 | | decision.CorrelationId, |
| | 49 | 258 | | decision.TraceId, |
| | 49 | 259 | | decision.PolicyVersion, |
| | 49 | 260 | | decision.PolicyHash, |
| | 49 | 261 | | metadata, |
| | 49 | 262 | | schemaVersion); |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | private static string NormalizeIdentifier(string? identifier) |
| | | 266 | | { |
| | 100 | 267 | | return string.IsNullOrWhiteSpace(identifier) |
| | 100 | 268 | | ? Guid.NewGuid().ToString("N") |
| | 100 | 269 | | : identifier.Trim(); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | private static string? NormalizeOptional(string? value) |
| | | 273 | | { |
| | 564 | 274 | | return string.IsNullOrWhiteSpace(value) |
| | 564 | 275 | | ? null |
| | 564 | 276 | | : value.Trim(); |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 280 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 281 | | { |
| | 100 | 282 | | if (metadata is null || metadata.Count == 0) |
| | | 283 | | { |
| | 91 | 284 | | return EmptyMetadata; |
| | | 285 | | } |
| | | 286 | | |
| | 9 | 287 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 288 | | |
| | 50 | 289 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 290 | | { |
| | 16 | 291 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 292 | | { |
| | | 293 | | continue; |
| | | 294 | | } |
| | | 295 | | |
| | 11 | 296 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 297 | | } |
| | | 298 | | |
| | 9 | 299 | | return normalizedMetadata.Count == 0 |
| | 9 | 300 | | ? EmptyMetadata |
| | 9 | 301 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 302 | | } |
| | | 303 | | } |