|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.Json.Serialization; |
| 5 | + |
| 6 | +namespace OpenClaw.Shared; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Pure, transport-neutral projection of the facts that determine whether an |
| 10 | +/// agent can use a Windows-node capability. The owners of settings, Windows |
| 11 | +/// permissions, gateway lifecycle, approval, and runtime probes remain |
| 12 | +/// unchanged; this type only makes their combined result inspectable. |
| 13 | +/// </summary> |
| 14 | +public static class CapabilityTruthProjection |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// The declaration gate shared with the projection. Runtime and approval |
| 18 | + /// facts may block usability, but they never override an explicit local |
| 19 | + /// Settings opt-out by advertising the command anyway. |
| 20 | + /// </summary> |
| 21 | + public static bool ShouldDeclare(bool settingsEnabled) => settingsEnabled; |
| 22 | + |
| 23 | + public enum WindowsPermissionKind |
| 24 | + { |
| 25 | + NotRequired, |
| 26 | + Unknown, |
| 27 | + Allowed, |
| 28 | + Denied, |
| 29 | + } |
| 30 | + |
| 31 | + public enum ApprovalKind |
| 32 | + { |
| 33 | + NotConnected, |
| 34 | + Unknown, |
| 35 | + Approved, |
| 36 | + Pending, |
| 37 | + Rejected, |
| 38 | + } |
| 39 | + |
| 40 | + public enum RuntimeKind |
| 41 | + { |
| 42 | + Unknown, |
| 43 | + Ready, |
| 44 | + Blocked, |
| 45 | + } |
| 46 | + |
| 47 | + public sealed record Input( |
| 48 | + string Id, |
| 49 | + string DisplayName, |
| 50 | + IReadOnlyList<string> Commands, |
| 51 | + bool SettingsEnabled, |
| 52 | + WindowsPermissionKind WindowsPermission, |
| 53 | + bool GatewaySessionLive, |
| 54 | + ApprovalKind Approval, |
| 55 | + IReadOnlyCollection<string> EffectiveCommands, |
| 56 | + IReadOnlyCollection<string> PendingCommands, |
| 57 | + IReadOnlyDictionary<string, bool> GatewayPermissions, |
| 58 | + bool LocalMcpExposed, |
| 59 | + RuntimeKind Runtime, |
| 60 | + string? RuntimeDetail = null, |
| 61 | + string? RuntimeRepair = null); |
| 62 | + |
| 63 | + public sealed record State( |
| 64 | + [property: JsonPropertyName("id")] string Id, |
| 65 | + [property: JsonPropertyName("displayName")] string DisplayName, |
| 66 | + [property: JsonPropertyName("commands")] IReadOnlyList<string> Commands, |
| 67 | + [property: JsonPropertyName("settingsEnabled")] bool SettingsEnabled, |
| 68 | + [property: JsonPropertyName("windowsPermission")] string WindowsPermission, |
| 69 | + [property: JsonPropertyName("gatewayDeclaration")] string GatewayDeclaration, |
| 70 | + [property: JsonPropertyName("approval")] string Approval, |
| 71 | + [property: JsonPropertyName("gatewayPathState")] string GatewayPathState, |
| 72 | + [property: JsonPropertyName("gatewayRepair")] string GatewayRepair, |
| 73 | + [property: JsonPropertyName("localMcpExposed")] bool LocalMcpExposed, |
| 74 | + [property: JsonPropertyName("runtimeReadiness")] string RuntimeReadiness, |
| 75 | + [property: JsonPropertyName("overallState")] string OverallState, |
| 76 | + [property: JsonPropertyName("summary")] string Summary, |
| 77 | + [property: JsonPropertyName("repair")] string Repair); |
| 78 | + |
| 79 | + public static State Project(Input input) |
| 80 | + { |
| 81 | + ArgumentNullException.ThrowIfNull(input); |
| 82 | + |
| 83 | + var gatewayEffective = input.GatewaySessionLive && input.Commands.All(command => |
| 84 | + input.EffectiveCommands.Contains(command, StringComparer.OrdinalIgnoreCase)); |
| 85 | + var pending = input.Commands.Any(command => |
| 86 | + input.PendingCommands.Contains(command, StringComparer.OrdinalIgnoreCase)); |
| 87 | + var gatewayPermissionBlocked = input.Commands.Any(command => |
| 88 | + CommandCenterDiagnostics.TryGetCommandPermission(input.GatewayPermissions, command, out var allowed) && !allowed); |
| 89 | + |
| 90 | + var declaration = gatewayEffective |
| 91 | + ? "effective" |
| 92 | + : pending |
| 93 | + ? "pending-approval" |
| 94 | + : !input.GatewaySessionLive |
| 95 | + ? "not-connected" |
| 96 | + : "not-declared"; |
| 97 | + |
| 98 | + if (!input.SettingsEnabled) |
| 99 | + { |
| 100 | + return Build(input, declaration, "disabled", |
| 101 | + $"{input.DisplayName} is disabled in Windows Hub Settings.", |
| 102 | + $"Enable {input.DisplayName} on the Permissions page, save, and reconnect the Windows node."); |
| 103 | + } |
| 104 | + |
| 105 | + if (input.WindowsPermission == WindowsPermissionKind.Denied) |
| 106 | + { |
| 107 | + return Build(input, declaration, "blocked", |
| 108 | + $"{input.DisplayName} is enabled, but Windows permission is denied.", |
| 109 | + input.RuntimeRepair ?? "Open Windows privacy settings, grant the required desktop-app permission, and retry."); |
| 110 | + } |
| 111 | + |
| 112 | + if (input.Runtime == RuntimeKind.Blocked) |
| 113 | + { |
| 114 | + return Build(input, declaration, "blocked", |
| 115 | + input.RuntimeDetail ?? $"{input.DisplayName} is not runtime-ready.", |
| 116 | + input.RuntimeRepair ?? "Repair the runtime prerequisite and retry."); |
| 117 | + } |
| 118 | + |
| 119 | + if (input.LocalMcpExposed) |
| 120 | + { |
| 121 | + var runtime = input.Runtime == RuntimeKind.Unknown ? "unchecked" : "ready"; |
| 122 | + var summary = input.RuntimeDetail ?? |
| 123 | + $"{input.DisplayName} is available through local MCP independently of the Gateway declaration."; |
| 124 | + var repair = input.Runtime == RuntimeKind.Unknown |
| 125 | + ? input.RuntimeRepair ?? "Invoke a read-only or safe command to verify the Windows runtime prerequisite." |
| 126 | + : "No repair is required."; |
| 127 | + return Build(input, declaration, "ready", summary, repair, runtime); |
| 128 | + } |
| 129 | + |
| 130 | + if (!gatewayEffective && (pending || input.Approval == ApprovalKind.Rejected)) |
| 131 | + { |
| 132 | + return Build(input, declaration, "pending-approval", |
| 133 | + $"{input.DisplayName} is waiting for gateway node command approval.", |
| 134 | + "Approve the pending Windows node declaration on the Gateway, then reconnect the node."); |
| 135 | + } |
| 136 | + |
| 137 | + if (gatewayPermissionBlocked) |
| 138 | + { |
| 139 | + return Build(input, declaration, "blocked", |
| 140 | + $"{input.DisplayName} is declared, but the Gateway policy blocks one or more commands.", |
| 141 | + "Review gateway.nodes allow/deny policy for the listed commands, approve the change, and reconnect the node."); |
| 142 | + } |
| 143 | + |
| 144 | + if (gatewayEffective) |
| 145 | + { |
| 146 | + var runtime = input.Runtime == RuntimeKind.Unknown ? "unchecked" : "ready"; |
| 147 | + var summary = input.RuntimeDetail ?? |
| 148 | + $"{input.DisplayName} is effective for the connected Gateway node."; |
| 149 | + var repair = input.Runtime == RuntimeKind.Unknown |
| 150 | + ? input.RuntimeRepair ?? "Invoke a read-only or safe command to verify the Windows runtime prerequisite." |
| 151 | + : "No repair is required."; |
| 152 | + return Build(input, declaration, "ready", summary, repair, runtime); |
| 153 | + } |
| 154 | + |
| 155 | + if (input.GatewaySessionLive) |
| 156 | + { |
| 157 | + return Build(input, declaration, "blocked", |
| 158 | + $"{input.DisplayName} is enabled locally but is not in the Gateway's effective Windows node declaration.", |
| 159 | + "Reconnect the Windows node. If the declaration changed, approve the pending command set on the Gateway."); |
| 160 | + } |
| 161 | + |
| 162 | + return Build(input, declaration, "unavailable", |
| 163 | + $"{input.DisplayName} is enabled, but neither local MCP nor a Gateway declaration currently exposes it.", |
| 164 | + "Enable Local MCP Server or connect and approve the Windows node, then retry."); |
| 165 | + } |
| 166 | + |
| 167 | + private static State Build( |
| 168 | + Input input, |
| 169 | + string declaration, |
| 170 | + string overall, |
| 171 | + string summary, |
| 172 | + string repair, |
| 173 | + string? runtime = null) |
| 174 | + { |
| 175 | + var gatewayEffective = input.GatewaySessionLive && input.Commands.All(command => |
| 176 | + input.EffectiveCommands.Contains(command, StringComparer.OrdinalIgnoreCase)); |
| 177 | + var pending = input.Commands.Any(command => |
| 178 | + input.PendingCommands.Contains(command, StringComparer.OrdinalIgnoreCase)); |
| 179 | + var gatewayPermissionBlocked = input.Commands.Any(command => |
| 180 | + CommandCenterDiagnostics.TryGetCommandPermission(input.GatewayPermissions, command, out var allowed) && !allowed); |
| 181 | + var gatewayPathState = !input.GatewaySessionLive |
| 182 | + ? pending ? "pending-approval" : "not-connected" |
| 183 | + : gatewayPermissionBlocked |
| 184 | + ? "blocked" |
| 185 | + : gatewayEffective |
| 186 | + ? "ready" |
| 187 | + : pending |
| 188 | + ? "pending-approval" |
| 189 | + : "not-declared"; |
| 190 | + var gatewayRepair = gatewayPathState switch |
| 191 | + { |
| 192 | + "ready" when pending => "The current declaration remains effective. Approve the pending Windows node changes, then reconnect to activate them.", |
| 193 | + "ready" => "No Gateway repair is required.", |
| 194 | + "blocked" => "Review gateway.nodes allow/deny policy for the listed commands, approve the change, and reconnect the node.", |
| 195 | + "pending-approval" => "Approve the pending Windows node declaration on the Gateway, then reconnect the node.", |
| 196 | + "not-declared" => "Reconnect the Windows node. If the declaration changed, approve the pending command set on the Gateway.", |
| 197 | + _ => "Connect the Windows node to inspect its Gateway declaration." |
| 198 | + }; |
| 199 | + |
| 200 | + return new( |
| 201 | + input.Id, |
| 202 | + input.DisplayName, |
| 203 | + input.Commands, |
| 204 | + input.SettingsEnabled, |
| 205 | + ToKebabCase(input.WindowsPermission.ToString()), |
| 206 | + declaration, |
| 207 | + ToKebabCase(input.Approval.ToString()), |
| 208 | + gatewayPathState, |
| 209 | + gatewayRepair, |
| 210 | + input.LocalMcpExposed, |
| 211 | + runtime ?? ToKebabCase(input.Runtime.ToString()), |
| 212 | + overall, |
| 213 | + summary, |
| 214 | + repair); |
| 215 | + } |
| 216 | + |
| 217 | + private static string ToKebabCase(string value) => |
| 218 | + string.Concat(value.Select((character, index) => |
| 219 | + index > 0 && char.IsUpper(character) ? $"-{char.ToLowerInvariant(character)}" : char.ToLowerInvariant(character).ToString())); |
| 220 | +} |
0 commit comments