-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathGatewayReleasePolicy.cs
More file actions
504 lines (447 loc) · 20 KB
/
Copy pathGatewayReleasePolicy.cs
File metadata and controls
504 lines (447 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
using System.Text.RegularExpressions;
using OpenClaw.Shared;
namespace OpenClaw.SetupEngine;
public enum GatewayReleaseSelectionMode
{
Recommended,
Fallback,
Exact,
}
public enum GatewayReleaseStatus
{
Validated,
Candidate,
Rejected,
}
public enum GatewayCompatibilityFailureKind
{
InvalidPolicy,
MissingFallback,
BelowSecurityFloor,
UnattestedRelease,
InstalledVersionMismatch,
InstalledRuntimeMismatch,
ProtocolMismatch,
ServerVersionMismatch,
}
public sealed class GatewayCompatibilityException : Exception
{
public GatewayCompatibilityException(GatewayCompatibilityFailureKind kind, string message)
: base(message)
{
Kind = kind;
}
public GatewayCompatibilityFailureKind Kind { get; }
}
public sealed record GatewayReleaseEvidence(
string Version,
GatewayReleaseStatus Status,
int ProtocolGeneration,
string NpmIntegrity,
string ReleaseUrl,
string ValidationEvidence,
string? RejectionReason = null);
public sealed record GatewayReleaseResolution(
GatewayReleaseSelectionMode Mode,
string Version,
int ProtocolGeneration,
bool IsCustomInstaller,
GatewayReleaseEvidence? Evidence);
public readonly record struct GatewayReleaseVersion(int Year, int Month, int Patch, int Correction)
: IComparable<GatewayReleaseVersion>
{
private static readonly Regex s_pattern = new(
@"^(?<year>\d{4})\.(?<month>\d{1,2})\.(?<patch>\d+)(?:-(?<correction>\d+))?$",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
private static readonly Regex s_outputPattern = new(
@"(?<![0-9A-Za-z.-])(?<version>\d{4}\.\d{1,2}\.\d+(?:-\d+)?)(?![0-9A-Za-z.-])",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
public static bool TryParse(string? value, out GatewayReleaseVersion version)
{
version = default;
if (string.IsNullOrWhiteSpace(value))
return false;
var match = s_pattern.Match(value.Trim());
if (!match.Success ||
!int.TryParse(match.Groups["year"].Value, out var year) ||
!int.TryParse(match.Groups["month"].Value, out var month) ||
!int.TryParse(match.Groups["patch"].Value, out var patch) ||
month is < 1 or > 12)
{
return false;
}
var correction = 0;
if (match.Groups["correction"].Success &&
!int.TryParse(match.Groups["correction"].Value, out correction))
{
return false;
}
version = new GatewayReleaseVersion(year, month, patch, correction);
return true;
}
public static bool TryExtract(string? output, out string version)
{
version = "";
if (string.IsNullOrWhiteSpace(output))
return false;
var match = s_outputPattern.Match(output);
if (!match.Success || !TryParse(match.Groups["version"].Value, out _))
return false;
version = match.Groups["version"].Value;
return true;
}
public int CompareTo(GatewayReleaseVersion other)
{
var year = Year.CompareTo(other.Year);
if (year != 0) return year;
var month = Month.CompareTo(other.Month);
if (month != 0) return month;
var patch = Patch.CompareTo(other.Patch);
return patch != 0 ? patch : Correction.CompareTo(other.Correction);
}
}
/// <summary>
/// Embedded Windows release policy. npm dist-tags discover candidates; they never
/// select the version installed by the product.
/// </summary>
public static class GatewayReleasePolicy
{
public const string DefaultInstallUrl = "https://openclaw.ai/install-cli.sh";
public const int ProtocolGeneration = 4;
public const string NodeVersion = "24.19.0";
public const string SecurityFloor = "2026.6.11";
public const string RecommendedVersion = "2026.6.34";
public const string RuntimeRejectedVersion = "2026.7.1";
public const string EvidenceRejectedVersion = "2026.7.1-2";
private static readonly IReadOnlyDictionary<string, GatewayReleaseEvidence> s_releases =
new Dictionary<string, GatewayReleaseEvidence>(StringComparer.Ordinal)
{
["2026.6.11"] = new(
"2026.6.11",
GatewayReleaseStatus.Validated,
ProtocolGeneration,
"sha512-T+P/g19IheeT1ckXMoPN61dYuE8vBF4MderI+kWkvpuFYxPkJxn8AXLpu9IXCnN9g36Acpm9+mMD/V+lsvOkyA==",
"https://github.com/openclaw/openclaw/releases/tag/v2026.6.11",
"Validated fallback: exact Windows clean setup, protocol-v4 pairing, and connectivity proof on 2026-08-06"),
[RecommendedVersion] = new(
RecommendedVersion,
GatewayReleaseStatus.Validated,
ProtocolGeneration,
"sha512-Rm4khBrWn9HYqE99NBryCFgjwlsIuwBqK5jIANn2773CGXJ1JIZkDn5twEHB+8SVFdh0FPNPHRVgZepzNJDfHg==",
"https://github.com/openclaw/openclaw/releases/tag/v2026.6.34",
"Promoted 2026-08-06 after exact Windows clean setup, protocol-v4 handshake, operator/node pairing, restart, network recovery, revocation recovery, and Gateway node.invoke proof"),
[RuntimeRejectedVersion] = new(
RuntimeRejectedVersion,
GatewayReleaseStatus.Rejected,
ProtocolGeneration,
"sha512-ge/Xss99CHAjPL/ikmH/UFoiOrjcxDB4sW3y9mhyCD+dYW3wzV7TKbAVdkrXFgAG2d2BjpJofP97zUZ+umxo8g==",
"https://github.com/openclaw/openclaw/releases/tag/v2026.7.1",
"Exact Windows candidate run on 2026-08-06",
"clean setup failed when the expanded Gateway wizard restarted the service and the fail-closed reconnect could not re-establish trusted endpoint ownership"),
[EvidenceRejectedVersion] = new(
EvidenceRejectedVersion,
GatewayReleaseStatus.Rejected,
ProtocolGeneration,
"sha512-ycF3yPcbjN6bUPeaUx6Mh6vze1hQWoD3CT/wWcmD7a8xaHHHRUaAlaq+lFxMHf1ssEgODVAwjlzYqp2twkYZ7g==",
"https://github.com/openclaw/openclaw/releases/tag/v2026.7.1-2",
"Candidate preflight on 2026-08-06",
"npm provenance attestation and a stable release-validation manifest were not published"),
};
public static string? FallbackVersion => "2026.6.11";
public static IReadOnlyDictionary<string, GatewayReleaseEvidence> Releases => s_releases;
public static bool IsOfficialInstallerUrl(string? installUrl) =>
string.IsNullOrWhiteSpace(installUrl) ||
string.Equals(installUrl, DefaultInstallUrl, StringComparison.OrdinalIgnoreCase);
public static IReadOnlyList<string> ValidateEmbeddedPolicy()
{
var errors = new List<string>();
if (!Version.TryParse(NodeVersion, out _))
errors.Add($"Node runtime '{NodeVersion}' is not an exact numeric version.");
if (!GatewayReleaseVersion.TryParse(SecurityFloor, out var floor))
errors.Add($"Security floor '{SecurityFloor}' is not an exact stable release.");
foreach (var (key, release) in s_releases)
{
if (!string.Equals(key, release.Version, StringComparison.Ordinal))
errors.Add($"Release key '{key}' does not match evidence version '{release.Version}'.");
if (!GatewayReleaseVersion.TryParse(release.Version, out _))
errors.Add($"Release '{release.Version}' is not an exact stable version.");
if (release.ProtocolGeneration != ProtocolGeneration)
errors.Add($"Release '{release.Version}' does not declare protocol v{ProtocolGeneration}.");
if (!release.NpmIntegrity.StartsWith("sha512-", StringComparison.Ordinal))
errors.Add($"Release '{release.Version}' does not have SHA-512 npm integrity.");
if (!Uri.TryCreate(release.ReleaseUrl, UriKind.Absolute, out var releaseUri) ||
!string.Equals(releaseUri.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
{
errors.Add($"Release '{release.Version}' does not have an HTTPS evidence URL.");
}
if (release.Status == GatewayReleaseStatus.Rejected &&
string.IsNullOrWhiteSpace(release.RejectionReason))
{
errors.Add($"Rejected release '{release.Version}' does not record a rejection reason.");
}
}
ValidateSelectedPolicyEntry("recommended", RecommendedVersion, floor, errors);
if (FallbackVersion is { } fallback)
{
if (string.Equals(fallback, RecommendedVersion, StringComparison.Ordinal))
errors.Add("Fallback release must be distinct from the recommendation.");
ValidateSelectedPolicyEntry("fallback", fallback, floor, errors);
}
return errors;
}
internal static bool IsReleaseEligible(
GatewayReleaseEvidence? evidence,
bool allowCandidate) =>
evidence is not null &&
evidence.ProtocolGeneration == ProtocolGeneration &&
evidence.Status != GatewayReleaseStatus.Rejected &&
(evidence.Status != GatewayReleaseStatus.Candidate || allowCandidate);
public static GatewayReleaseResolution ResolveAndApply(
SetupConfig config,
bool allowCandidate = false)
{
ArgumentNullException.ThrowIfNull(config);
var policyErrors = ValidateEmbeddedPolicy();
if (policyErrors.Count != 0)
{
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
$"Embedded Gateway release policy is invalid: {string.Join(" ", policyErrors)}");
}
var gateway = config.Gateway;
var customInstaller = !IsOfficialInstallerUrl(gateway.InstallUrl);
var requestedVersion = gateway.Version?.Trim();
if (customInstaller)
{
if (string.IsNullOrWhiteSpace(requestedVersion))
{
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
"Custom Gateway installer URLs require an exact stable Gateway.Version.");
}
ValidateStableFloor(requestedVersion);
var customResolution = new GatewayReleaseResolution(
GatewayReleaseSelectionMode.Exact,
requestedVersion,
ProtocolGeneration,
IsCustomInstaller: true,
Evidence: null);
gateway.Selection = "exact";
gateway.Version = requestedVersion;
gateway.ResolvedRelease = customResolution;
return customResolution;
}
var mode = ParseMode(gateway.Selection, requestedVersion);
string selectedVersion;
switch (mode)
{
case GatewayReleaseSelectionMode.Recommended:
selectedVersion = RecommendedVersion;
break;
case GatewayReleaseSelectionMode.Fallback:
selectedVersion = FallbackVersion ?? throw Failure(
GatewayCompatibilityFailureKind.MissingFallback,
$"No distinct validated Gateway fallback is available at or above security floor {SecurityFloor}.");
break;
case GatewayReleaseSelectionMode.Exact:
if (string.IsNullOrWhiteSpace(requestedVersion))
{
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
"Gateway selection 'exact' requires Gateway.Version.");
}
selectedVersion = requestedVersion;
break;
default:
throw new InvalidOperationException($"Unsupported Gateway release selection mode: {mode}");
}
ValidateStableFloor(selectedVersion);
_ = s_releases.TryGetValue(selectedVersion, out var evidence);
if (!IsReleaseEligible(evidence, allowCandidate))
{
var rejection = evidence?.RejectionReason;
var detail = string.IsNullOrWhiteSpace(rejection) ? "" : $" {rejection}.";
throw Failure(
GatewayCompatibilityFailureKind.UnattestedRelease,
$"Gateway {selectedVersion} is not an eligible protocol-v{ProtocolGeneration} Windows release.{detail}");
}
var resolution = new GatewayReleaseResolution(
mode,
selectedVersion,
ProtocolGeneration,
IsCustomInstaller: false,
evidence);
gateway.Selection = mode.ToString().ToLowerInvariant();
gateway.Version = selectedVersion;
gateway.ResolvedRelease = resolution;
return resolution;
}
internal static GatewayReleaseResolution ResolveAndApplyValidationPackage(SetupConfig config)
{
ArgumentNullException.ThrowIfNull(config);
var policyErrors = ValidateEmbeddedPolicy();
if (policyErrors.Count != 0)
{
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
$"Embedded Gateway release policy is invalid: {string.Join(" ", policyErrors)}");
}
if (!IsOfficialInstallerUrl(config.Gateway.InstallUrl))
{
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
"Gateway candidate package validation requires the official installer.");
}
var selectedVersion = config.Gateway.Version?.Trim();
if (string.IsNullOrWhiteSpace(selectedVersion))
{
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
"Gateway candidate package validation requires an exact Gateway.Version.");
}
ValidateStableFloor(selectedVersion);
_ = s_releases.TryGetValue(selectedVersion, out var evidence);
if (evidence?.Status == GatewayReleaseStatus.Rejected)
{
var detail = string.IsNullOrWhiteSpace(evidence.RejectionReason)
? ""
: $" {evidence.RejectionReason}.";
throw Failure(
GatewayCompatibilityFailureKind.UnattestedRelease,
$"Gateway {selectedVersion} is rejected for protocol-v{ProtocolGeneration} Windows setup.{detail}");
}
var resolution = new GatewayReleaseResolution(
GatewayReleaseSelectionMode.Exact,
selectedVersion,
ProtocolGeneration,
IsCustomInstaller: false,
evidence);
config.Gateway.Selection = "exact";
config.Gateway.Version = selectedVersion;
config.Gateway.ResolvedRelease = resolution;
return resolution;
}
public static GatewayCompatibilityException? ValidateHandshake(
SetupConfig config,
GatewaySelfInfo? gatewaySelf)
{
var selectedVersion = config.Gateway.ResolvedRelease?.Version ?? config.Gateway.Version;
if (string.IsNullOrWhiteSpace(selectedVersion))
{
return Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
"Gateway release policy was not resolved before the compatibility handshake.");
}
if (gatewaySelf?.Protocol != ProtocolGeneration)
{
var actual = gatewaySelf?.Protocol?.ToString() ?? "missing";
return Failure(
GatewayCompatibilityFailureKind.ProtocolMismatch,
$"Gateway compatibility check failed: expected protocol v{ProtocolGeneration}, received {actual}.");
}
if (!string.Equals(gatewaySelf.ServerVersion, selectedVersion, StringComparison.Ordinal))
{
var actual = string.IsNullOrWhiteSpace(gatewaySelf.ServerVersion) ? "missing" : gatewaySelf.ServerVersion;
return Failure(
GatewayCompatibilityFailureKind.ServerVersionMismatch,
$"Gateway compatibility check failed: selected version {selectedVersion}, server reported {actual}.");
}
return null;
}
public static bool TryApplyFallback(SetupConfig config, out string? error)
{
if (!CanRetryWithFallback(config))
{
error = !IsOfficialInstallerUrl(config.Gateway.InstallUrl)
? "Custom Gateway installers do not use the product's validated fallback."
: FallbackVersion is null
? $"No validated Gateway fallback is available at or above security floor {SecurityFloor}."
: $"Gateway {FallbackVersion} is already selected; no older validated fallback will be tried.";
return false;
}
config.Gateway.Selection = "fallback";
config.Gateway.Version = null;
config.Gateway.ResolvedRelease = null;
try
{
ResolveAndApply(config);
error = null;
return true;
}
catch (GatewayCompatibilityException ex)
{
error = ex.Message;
return false;
}
}
public static bool CanRetryWithFallback(SetupConfig config) =>
FallbackVersion is { } fallback &&
IsOfficialInstallerUrl(config.Gateway.InstallUrl) &&
!string.Equals(config.Gateway.Version, fallback, StringComparison.Ordinal);
public static bool CanRetryWithFallback(
SetupConfig config,
GatewayCompatibilityFailureKind failureKind) =>
failureKind is GatewayCompatibilityFailureKind.InstalledVersionMismatch
or GatewayCompatibilityFailureKind.ProtocolMismatch
or GatewayCompatibilityFailureKind.ServerVersionMismatch &&
CanRetryWithFallback(config);
private static GatewayReleaseSelectionMode ParseMode(string? selection, string? requestedVersion)
{
if (!string.IsNullOrWhiteSpace(requestedVersion) &&
(string.IsNullOrWhiteSpace(selection) ||
selection.Equals("recommended", StringComparison.OrdinalIgnoreCase)))
{
return GatewayReleaseSelectionMode.Exact;
}
if (string.IsNullOrWhiteSpace(selection))
return GatewayReleaseSelectionMode.Recommended;
if (Enum.TryParse<GatewayReleaseSelectionMode>(selection, ignoreCase: true, out var mode))
return mode;
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
$"Invalid Gateway.Selection '{selection}'. Use recommended, fallback, or exact.");
}
private static void ValidateStableFloor(string version)
{
if (!GatewayReleaseVersion.TryParse(version, out var parsed))
{
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
$"Gateway version '{version}' is not an exact stable release. Prerelease channels are not eligible.");
}
if (!GatewayReleaseVersion.TryParse(SecurityFloor, out var floor))
{
throw Failure(
GatewayCompatibilityFailureKind.InvalidPolicy,
$"Gateway security floor '{SecurityFloor}' is not an exact stable release.");
}
if (parsed.CompareTo(floor) < 0)
{
throw Failure(
GatewayCompatibilityFailureKind.BelowSecurityFloor,
$"Gateway {version} is below the Windows security floor {SecurityFloor}.");
}
}
private static GatewayCompatibilityException Failure(
GatewayCompatibilityFailureKind kind,
string message) => new(kind, message);
private static void ValidateSelectedPolicyEntry(
string role,
string version,
GatewayReleaseVersion floor,
List<string> errors)
{
if (!s_releases.TryGetValue(version, out var evidence))
{
errors.Add($"The {role} release '{version}' has no evidence entry.");
return;
}
if (evidence.Status != GatewayReleaseStatus.Validated)
errors.Add($"The {role} release '{version}' is not validated.");
if (GatewayReleaseVersion.TryParse(version, out var parsed) &&
parsed.CompareTo(floor) < 0)
{
errors.Add($"The {role} release '{version}' is below security floor {SecurityFloor}.");
}
}
}