From 9c9bbd3303facf475356e190eebef95d218b811c Mon Sep 17 00:00:00 2001 From: Alberto Delgado <44581951+turutupa@users.noreply.github.com> Date: Tue, 10 Nov 2026 17:00:31 -0800 Subject: [PATCH 1/5] fix: honor Effort Off for qualified Qwen local AI models (#1976) Effort Off still produced reasoning for managed Qwen models because the llama-server preset forced `reasoning = on` and the Gateway provider had no effort vocabulary to map Off onto. - Drop `reasoning = on` from the router preset so llama-server's default auto mode lets each request turn reasoning off. - Tag the qualified Qwen catalog entries with a reasoning profile (Qwen38 / QwenOnOff) and publish effort metadata for those templates only (supportsReasoningEffort, supportedReasoningEfforts, reasoningEffortMap off->none, thinkingLevelMap). Other templates keep the previous provider shape. - Recognize the exact pre-upgrade provider shape in ownership, recovery, rollback and teardown (SetupEngine step, native lifecycle, coordinator) so an unchanged legacy route is not treated as externally edited. - Migrate an exact legacy provider by replacing only its owned `models` array: guarded config.patch on native, dry-run then --expect-current-json write then restart on WSL. The API key is never replayed from a redacted readback. - Native endpoint recovery keeps its early return for a current-shape route and continues through the guarded coordinator only for the exact legacy shape. - Canvas actions no longer send `thinking: "low"`, so they inherit the session's effort. No Gateway version gate: every field written has been in the Gateway config schema since 2026.6.5. Gateways 2026.6.5 to 2026.9.7 accept it and keep today's behavior (Off still reasons); older Gateways reject the write through the existing guarded paths with no config change. Co-Authored-By: Claude Opus 5.5 --- docs/a2ui/data-and-actions.md | 4 +- .../LocalAi/LlamaServerRouterConfiguration.cs | 2 +- .../LocalAiGatewayProviderDefinition.cs | 87 ++++++++++++- .../LocalAiGatewayConfiguration.cs | 21 +-- .../Inference/Catalog/LocalModelCatalog.cs | 27 ++-- .../A2UI/Actions/GatewayActionTransport.cs | 5 +- src/OpenClaw.Tray.WinUI/App.xaml.cs | 4 +- .../Services/LocalAiGatewayLifecycle.cs | 29 +++-- .../LocalAiGatewayProviderCoordinator.cs | 98 +++++++++++++- .../Services/WindowManager.cs | 3 +- .../Strings/en-us/Resources.resw | 4 + .../Strings/fr-fr/Resources.resw | 4 + .../Strings/nl-nl/Resources.resw | 4 + .../Strings/pt-br/Resources.resw | 4 + .../Strings/zh-cn/Resources.resw | 4 + .../Strings/zh-tw/Resources.resw | 4 + .../LocalAiPortLifecycleTests.cs | 38 ++++++ .../LocalAiGatewayUninstallTests.cs | 108 ++++++++++++++++ .../NativeLocalAiLifecycleTests.cs | 89 +++++++++++++ .../GatewayActionTransportTests.cs | 10 +- .../LocalAiGatewayProviderCoordinatorTests.cs | 120 ++++++++++++++++++ 21 files changed, 617 insertions(+), 52 deletions(-) diff --git a/docs/a2ui/data-and-actions.md b/docs/a2ui/data-and-actions.md index 33d4455220..f63ad9cb2e 100644 --- a/docs/a2ui/data-and-actions.md +++ b/docs/a2ui/data-and-actions.md @@ -167,12 +167,14 @@ an `agent.request` node event: { "message": "CANVAS_A2UI action=submit session=main surface=main component=btn-1 host=… instance=… ctx=… default=update_canvas", "sessionKey": "main", - "thinking": "low", "deliver": false, "key": "" } ``` +The event carries no `thinking` override, so a canvas action runs with the +session's own effort setting. + `AgentMessageFormatter` is a deliberate byte-for-byte port of the Android node's formatter - the gateway parses tags identically across platforms. diff --git a/src/OpenClaw.Connection/LocalAi/LlamaServerRouterConfiguration.cs b/src/OpenClaw.Connection/LocalAi/LlamaServerRouterConfiguration.cs index f64f5acfcb..00ae127efd 100644 --- a/src/OpenClaw.Connection/LocalAi/LlamaServerRouterConfiguration.cs +++ b/src/OpenClaw.Connection/LocalAi/LlamaServerRouterConfiguration.cs @@ -294,7 +294,7 @@ internal static string BuildPreset( preset.Append("repeat-penalty = ").AppendLine(Invariant(sampling.RepetitionPenalty)); preset.Append("presence-penalty = ").AppendLine(Invariant(sampling.PresencePenalty)); preset.AppendLine("jinja = true"); - preset.AppendLine("reasoning = on"); + // Keep llama-server at its default auto mode so each request can turn reasoning off. preset.AppendLine("reasoning-format = deepseek"); preset.AppendLine("context-shift = true"); return preset.ToString(); diff --git a/src/OpenClaw.Connection/LocalAi/LocalAiGatewayProviderDefinition.cs b/src/OpenClaw.Connection/LocalAi/LocalAiGatewayProviderDefinition.cs index c190072c84..81047a8164 100644 --- a/src/OpenClaw.Connection/LocalAi/LocalAiGatewayProviderDefinition.cs +++ b/src/OpenClaw.Connection/LocalAi/LocalAiGatewayProviderDefinition.cs @@ -11,6 +11,7 @@ // bool matches = LocalAiGatewayProviderDefinition.MatchesProviderJson(cliOutputJson, install); using OpenClaw.Shared.Inference.Catalog; using System.Text.Json; +using System.Text.Json.Nodes; namespace OpenClaw.Connection.LocalAi; @@ -41,7 +42,40 @@ public static string BuildProviderJson(LocalAiResolvedInstall install) /// so the API key may be either its written value or the documented /// redaction marker; every routing and model field must still match. /// - public static bool MatchesProviderJson(string providerJson, LocalAiResolvedInstall install, string? apiKey = null) + public static bool MatchesProviderJson(string providerJson, LocalAiResolvedInstall install, string? apiKey = null) => + MatchesProviderJson(providerJson, install, apiKey, legacy: false); + + /// Recognizes an exact current or pre-effort-metadata managed provider for ownership recovery. + public static bool MatchesCurrentOrLegacyProviderJson( + string providerJson, LocalAiResolvedInstall install, string? apiKey = null) => + MatchesProviderJson(providerJson, install, apiKey) || + MatchesLegacyProviderJson(providerJson, install, apiKey); + + /// Rebuilds a trusted provider for rollback in the exact observed managed shape. + public static string BuildProviderJsonForRollback(string priorProviderJson, LocalAiResolvedInstall install) + { + if (MatchesProviderJson(priorProviderJson, install)) + return BuildProviderJson(install); + if (MatchesLegacyProviderJson(priorProviderJson, install)) + return BuildProviderJson(install, "llama-local", legacy: true); + throw new InvalidDataException("The original Local AI provider is not an exact managed configuration."); + } + + /// Checks that recovery restored the same managed model-metadata shape. + public static bool MatchesSameManagedProviderShape( + string priorProviderJson, string restoredProviderJson, LocalAiResolvedInstall install) + { + if (MatchesProviderJson(priorProviderJson, install)) + return MatchesProviderJson(restoredProviderJson, install); + return MatchesLegacyProviderJson(priorProviderJson, install) && + MatchesLegacyProviderJson(restoredProviderJson, install); + } + + /// Recognizes only the exact provider shape published before effort metadata was added. + public static bool MatchesLegacyProviderJson(string providerJson, LocalAiResolvedInstall install, string? apiKey = null) => + MatchesProviderJson(providerJson, install, apiKey, legacy: true); + + private static bool MatchesProviderJson(string providerJson, LocalAiResolvedInstall install, string? apiKey, bool legacy) { ArgumentException.ThrowIfNullOrWhiteSpace(providerJson); ArgumentNullException.ThrowIfNull(install); @@ -49,12 +83,12 @@ public static bool MatchesProviderJson(string providerJson, LocalAiResolvedInsta try { using JsonDocument actual = JsonDocument.Parse(providerJson); - using JsonDocument expected = JsonDocument.Parse(BuildProviderJson(install, apiKey ?? "llama-local")); + using JsonDocument expected = JsonDocument.Parse(BuildProviderJson(install, apiKey ?? "llama-local", legacy)); if (JsonEquals(actual.RootElement, expected.RootElement)) return true; using JsonDocument redacted = JsonDocument.Parse( - BuildProviderJson(install, CliRedactedApiKey)); + BuildProviderJson(install, CliRedactedApiKey, legacy)); return JsonEquals(actual.RootElement, redacted.RootElement); } catch (JsonException) @@ -101,13 +135,26 @@ private static bool JsonEquals(JsonElement left, JsonElement right) } /// Builds the managed provider JSON for a resolved install and explicit local API credential. - public static string BuildProviderJson(LocalAiResolvedInstall install, string apiKey) + public static string BuildProviderJson(LocalAiResolvedInstall install, string apiKey) => + BuildProviderJson(install, apiKey, legacy: false); + + private static string BuildProviderJson(LocalAiResolvedInstall install, string apiKey, bool legacy) { ArgumentNullException.ThrowIfNull(install); _ = LocalAiApiCredentialStore.RequireApiKey(apiKey); LocalModelInfo model = GetQualifiedModel(install); Uri endpoint = install.Endpoint ?? throw new InvalidOperationException("The verified Local AI endpoint is required."); + string[]? efforts = legacy ? null : model.ReasoningProfile switch + { + LocalModelReasoningProfile.Qwen38 => ["none", "low", "medium", "high"], + LocalModelReasoningProfile.QwenOnOff => ["none", "medium"], + _ => null, + }; + object? thinkingLevelMap = efforts is null ? null : + model.ReasoningProfile == LocalModelReasoningProfile.QwenOnOff + ? new { minimal = (string?)null, high = (string?)null } + : new { minimal = (string?)null }; var value = new { @@ -127,12 +174,40 @@ public static string BuildProviderJson(LocalAiResolvedInstall install, string ap contextWindow = install.Manifest.ContextLength, contextTokens = install.Manifest.ContextLength, maxTokens = GetMaximumOutputTokens(install.Manifest.ContextLength), - compat = new { supportsTools = true, supportsUsageInStreaming = true }, + compat = new + { + supportsTools = true, + supportsUsageInStreaming = true, + supportsReasoningEffort = efforts is null ? (bool?)null : true, + supportedReasoningEfforts = efforts, + reasoningEffortMap = efforts is null ? null : new { off = "none" }, + }, + thinkingLevelMap, api = ApiType, }, }, }; - return JsonSerializer.Serialize(value); + JsonObject provider = (JsonObject)JsonSerializer.SerializeToNode(value)!; + if (efforts is null) + { + // Keep the old shape for templates whose reasoning vocabulary has + // not been verified. Null map entries on qualified templates are + // meaningful and must remain in the serialized configuration. + JsonObject configuredModel = (JsonObject)((JsonArray)provider["models"]!)[0]!; + JsonObject compat = (JsonObject)configuredModel["compat"]!; + compat.Remove("supportsReasoningEffort"); + compat.Remove("supportedReasoningEfforts"); + compat.Remove("reasoningEffortMap"); + configuredModel.Remove("thinkingLevelMap"); + } + return provider.ToJsonString(); + } + + /// Builds the exact model array for a guarded legacy-provider migration. + public static string BuildProviderModelsJson(LocalAiResolvedInstall install) + { + using JsonDocument provider = JsonDocument.Parse(BuildProviderJson(install)); + return provider.RootElement.GetProperty("models").GetRawText(); } /// Builds the Gateway primary-model identifier for a resolved managed install. diff --git a/src/OpenClaw.SetupEngine/LocalAiGatewayConfiguration.cs b/src/OpenClaw.SetupEngine/LocalAiGatewayConfiguration.cs index 75149de042..37df7e80f4 100644 --- a/src/OpenClaw.SetupEngine/LocalAiGatewayConfiguration.cs +++ b/src/OpenClaw.SetupEngine/LocalAiGatewayConfiguration.cs @@ -59,8 +59,11 @@ public static string BuildRecoveryRestoreBatchJson( { ArgumentNullException.ThrowIfNull(prior); ArgumentNullException.ThrowIfNull(originalInstall); + // Recreate the exact pre-recovery model shape with the known local + // credential; never replay a CLI-redacted API key from the snapshot. using JsonDocument provider = JsonDocument.Parse( - LocalAiGatewayProviderDefinition.BuildProviderJson(originalInstall)); + LocalAiGatewayProviderDefinition.BuildProviderJsonForRollback( + prior.ProviderJson!, originalInstall)); using JsonDocument primary = JsonDocument.Parse(prior.PrimaryModelJson!); object[] operations = [ @@ -118,7 +121,7 @@ public override async Task ExecuteAsync(SetupContext ctx, Cancellati if (prior.ProviderExisted) { bool matchesCurrentInstall = install.Endpoint is not null && - LocalAiGatewayProviderDefinition.MatchesProviderJson(prior.ProviderJson!, install); + LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson(prior.ProviderJson!, install); bool matchesRecoveryInstall = false; if (!matchesCurrentInstall && ctx.LocalAiRecoveryOriginalInstall is { Endpoint: not null } originalInstall) @@ -126,7 +129,7 @@ public override async Task ExecuteAsync(SetupContext ctx, Cancellati string originalPrimary = JsonSerializer.Serialize( LocalAiGatewayProviderDefinition.BuildPrimaryModel(originalInstall)); matchesRecoveryInstall = - LocalAiGatewayProviderDefinition.MatchesProviderJson( + LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson( prior.ProviderJson!, originalInstall) && JsonEquals(originalPrimary, expectedPrimary); @@ -254,9 +257,8 @@ public override async Task RollbackAsync(SetupContext ctx, CancellationToken ct) if (recoveryOriginal is not null && current.ProviderExisted == prior.ProviderExisted && (!current.ProviderExisted || - LocalAiGatewayProviderDefinition.MatchesProviderJson( - current.ProviderJson!, - recoveryOriginal)) && + LocalAiGatewayProviderDefinition.MatchesSameManagedProviderShape( + prior.ProviderJson!, current.ProviderJson!, recoveryOriginal)) && current.PrimaryModelExisted == prior.PrimaryModelExisted && (!current.PrimaryModelExisted || JsonEquals(current.PrimaryModelJson!, prior.PrimaryModelJson!))) @@ -367,9 +369,8 @@ private static async Task ReconcileFailedRecoveryRestoreAsync( bool originalRestored = observed.ProviderExisted == prior.ProviderExisted && (!observed.ProviderExisted || - LocalAiGatewayProviderDefinition.MatchesProviderJson( - observed.ProviderJson!, - recoveryOriginal)) && + LocalAiGatewayProviderDefinition.MatchesSameManagedProviderShape( + prior.ProviderJson!, observed.ProviderJson!, recoveryOriginal)) && observed.PrimaryModelExisted == prior.PrimaryModelExisted && (!observed.PrimaryModelExisted || JsonEquals(observed.PrimaryModelJson!, prior.PrimaryModelJson!)); @@ -444,7 +445,7 @@ fallbackModel is not null && } if ((current.ProviderExisted && - !LocalAiGatewayProviderDefinition.MatchesProviderJson(current.ProviderJson!, install)) || + !LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson(current.ProviderJson!, install)) || (current.PrimaryModelExisted && !currentPrimaryIsManaged && !currentPrimaryIsFallback && diff --git a/src/OpenClaw.Shared/Inference/Catalog/LocalModelCatalog.cs b/src/OpenClaw.Shared/Inference/Catalog/LocalModelCatalog.cs index 8d97a0ffb7..d052bfef4e 100644 --- a/src/OpenClaw.Shared/Inference/Catalog/LocalModelCatalog.cs +++ b/src/OpenClaw.Shared/Inference/Catalog/LocalModelCatalog.cs @@ -135,6 +135,9 @@ public LocalModelRunRecipe( public PinnedArtifact? VisionProjector { get; } } +/// The reasoning effort vocabulary verified for a managed model template. +public enum LocalModelReasoningProfile { Unspecified, Qwen38, QwenOnOff } + /// A downloadable GGUF model and its deterministic llama-server recipe. public sealed record LocalModelInfo( string Id, @@ -150,7 +153,8 @@ public sealed record LocalModelInfo( string RuntimeFamily = "llama.cpp", long MinimumSystemMemoryBytes = 0, long MinimumFreeDiskBytes = 0, - LocalCustomModelRecipe? CustomRecipe = null); + LocalCustomModelRecipe? CustomRecipe = null, + LocalModelReasoningProfile ReasoningProfile = LocalModelReasoningProfile.Unspecified); /// The capacity-sensitive settings selected for one model launch. public sealed record LocalInferenceRunProfile @@ -270,7 +274,8 @@ public static partial class LocalModelCatalog IsDefault: true, IsExplicitAlternative: false, SupportsVision: false, - RecommendationPriority: 400), + RecommendationPriority: 400, + ReasoningProfile: LocalModelReasoningProfile.Qwen38), new LocalModelInfo( Qwen35BModelId, "Qwen3.6 35B-A3B (UD-Q4_K_M)", @@ -290,7 +295,8 @@ public static partial class LocalModelCatalog IsDefault: false, IsExplicitAlternative: true, SupportsVision: false, - RecommendationPriority: 300), + RecommendationPriority: 300, + ReasoningProfile: LocalModelReasoningProfile.QwenOnOff), new LocalModelInfo( Qwen27BModelId, "Qwen3.6 27B (Q4_K_M)", @@ -309,7 +315,8 @@ public static partial class LocalModelCatalog IsDefault: false, IsExplicitAlternative: true, SupportsVision: false, - RecommendationPriority: 200), + RecommendationPriority: 200, + ReasoningProfile: LocalModelReasoningProfile.QwenOnOff), // RTX Spark SKU recipes below, offered by RtxSparkInferenceSelector // keyed off the detected Spark unified-memory SKU. RecommendationPriority: 0 // alone does not exclude a model from the generic dGPU default pick -- @@ -337,7 +344,8 @@ public static partial class LocalModelCatalog IsDefault: false, IsExplicitAlternative: false, SupportsVision: false, - RecommendationPriority: 0), + RecommendationPriority: 0, + ReasoningProfile: LocalModelReasoningProfile.QwenOnOff), new LocalModelInfo( Qwen38_27B_DFlashModelId, "Qwen3.8 27B (UD-Q4_K_M, DFlash)", @@ -366,7 +374,8 @@ public static partial class LocalModelCatalog IsDefault: false, IsExplicitAlternative: false, SupportsVision: false, - RecommendationPriority: 0), + RecommendationPriority: 0, + ReasoningProfile: LocalModelReasoningProfile.Qwen38), new LocalModelInfo( Bonsai2ModelId, "Bonsai 2 27B (PTQ1_0, experimental)", @@ -422,7 +431,8 @@ public static partial class LocalModelCatalog IsDefault: false, IsExplicitAlternative: false, SupportsVision: false, - RecommendationPriority: 0), + RecommendationPriority: 0, + ReasoningProfile: LocalModelReasoningProfile.QwenOnOff), new LocalModelInfo( Qwen35B_IQ4XSModelId, "Qwen3.6 35B-A3B (UD-IQ4_XS)", @@ -442,7 +452,8 @@ public static partial class LocalModelCatalog IsDefault: false, IsExplicitAlternative: false, SupportsVision: false, - RecommendationPriority: 0), + RecommendationPriority: 0, + ReasoningProfile: LocalModelReasoningProfile.QwenOnOff), }); private static readonly IReadOnlyDictionary> diff --git a/src/OpenClaw.Tray.WinUI/A2UI/Actions/GatewayActionTransport.cs b/src/OpenClaw.Tray.WinUI/A2UI/Actions/GatewayActionTransport.cs index b7d02dba5f..003d2e3dd6 100644 --- a/src/OpenClaw.Tray.WinUI/A2UI/Actions/GatewayActionTransport.cs +++ b/src/OpenClaw.Tray.WinUI/A2UI/Actions/GatewayActionTransport.cs @@ -113,13 +113,12 @@ public static JsonObject BuildAgentRequestPayload(Protocol.A2UIAction action, IG contextJson: contextJson); // deliver=false keeps the raw CANVAS_A2UI line out of the visible - // chat; only the model's response is shown to the user. thinking=low - // matches the Android budget hint for a quick agentic step. + // chat; only the model's response is shown to the user. Inherit the + // session's effort so canvas actions do not change the user's choice. return new JsonObject { ["message"] = message, ["sessionKey"] = sessionKey, - ["thinking"] = "low", ["deliver"] = false, ["key"] = action.Id, }; diff --git a/src/OpenClaw.Tray.WinUI/App.xaml.cs b/src/OpenClaw.Tray.WinUI/App.xaml.cs index 3640bbc713..71b2858c5e 100644 --- a/src/OpenClaw.Tray.WinUI/App.xaml.cs +++ b/src/OpenClaw.Tray.WinUI/App.xaml.cs @@ -925,10 +925,10 @@ _dispatcherQueue is null // Model recovery also covers the local runtime's health check. new WslExeCommandRunner(localAiLogger, TimeSpan.FromSeconds(420)), new LocalAiGatewayDistroResolver(_gatewayRegistry), - localAiLogger); + localAiLogger, LocalizationHelper.GetString); _localAiGatewayLifecycle = new LocalAiGatewayLifecycle(localAiPaths, SettingsManager.SettingsDirectoryPath, () => _gatewayRegistry, () => _connectionManager, - localAiEndpointLifecycle, localAiLogger); + localAiEndpointLifecycle, localAiLogger, LocalizationHelper.GetString); _localAiRuntime = new LlamaServerRuntimeService( new LlamaServerRuntimeOptions { diff --git a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayLifecycle.cs b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayLifecycle.cs index 56d655b563..c5a4447e3a 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayLifecycle.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayLifecycle.cs @@ -64,7 +64,7 @@ namespace OpenClawTray.Services; internal sealed class LocalAiGatewayLifecycle( LocalAiPaths paths, string dataDirectory, Func getRegistry, Func getManager, ILocalAiEndpointLifecycle wsl, - IOpenClawLogger logger) : ILocalAiEndpointLifecycle + IOpenClawLogger logger, Func? getString = null) : ILocalAiEndpointLifecycle { private const string UnconfirmedGatewayChangeDetail = "Local AI has an unconfirmed Gateway change. Reconnect the original Gateway and review Local AI first."; @@ -382,7 +382,7 @@ public async Task ReconcileVerifiedAsync(GatewayRecord record, IGatewayAiSetupTr var provider = config["models"]?["providers"]?["llamacpp"]; if (binding.ModelRef != LocalAiGatewayProviderDefinition.BuildPrimaryModel(install) || config["agents"]?["defaults"]?["model"]?["primary"]?.GetValue() != binding.ModelRef || - provider is null || !LocalAiGatewayProviderDefinition.MatchesProviderJson( + provider is null || !LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson( provider.ToJsonString(), install, _credentials.GetOrCreate())) throw new InvalidOperationException("The saved Local AI provider or primary model was changed. No ownership was adopted."); // A redacted credential cannot establish ownership. Explicit verification @@ -622,7 +622,7 @@ private async Task PrepareCoreAsync(LocalAiResolvedInstall install, NativeLocalA } else if (provider is not null && primary == binding.ModelRef && install.Endpoint is { } endpoint && binding.ModelRef == LocalAiGatewayProviderDefinition.BuildPrimaryModel(install) && - LocalAiGatewayProviderDefinition.MatchesProviderJson( + LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson( provider.ToJsonString(), install, _credentials.GetOrCreate())) { // Explicit Use may restore only the same authenticated listener. @@ -693,6 +693,7 @@ private async Task RunNativeAsync( var current = await rpc.CaptureAsync(ct).ConfigureAwait(false); var config = JsonNode.Parse(current.Config.GetRawText())!; binding = SettleUnconfirmedPrimary(binding, config); + bool verifiedRecoveryPublication = false; if (reason == LocalAiQuiesceReason.Teardown && binding.AddedAllowlistEntry && config["agents"]?["defaults"]?["models"]?[binding.ModelRef] is { } entry && entry is not JsonObject { Count: 0 }) @@ -713,7 +714,7 @@ private async Task RunNativeAsync( } else if (reason == LocalAiQuiesceReason.Teardown && !binding.Pending && provider is not null && primary == binding.ModelRef && - LocalAiGatewayProviderDefinition.MatchesProviderJson( + LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson( provider.ToJsonString(), install, _credentials.GetOrCreate())) { // Redacted keys alone cannot authorize deletion after an external edit. @@ -747,9 +748,19 @@ private async Task RunNativeAsync( var after = await rpc.CaptureAsync(ct).ConfigureAwait(false); if (!verification.Ok || after.Hash != current.Hash) throw new InvalidOperationException("The recovered endpoint could not be verified through the original Gateway."); - _store.Save(binding with { Pending = false, ConfigHash = after.Hash }); - _endpointRecovery = null; - return LocalAiEndpointLifecycleResult.Ok(); + // Inference proves the old route belongs to this listener. Only an + // exact pre-effort provider continues through the guarded + // coordinator to receive its effort metadata; any other verified + // route is already published. + binding = binding with { Pending = false, ConfigHash = after.Hash }; + if (provider is null || !LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson( + provider.ToJsonString(), install, _credentials.GetOrCreate())) + { + _store.Save(binding); + _endpointRecovery = null; + return LocalAiEndpointLifecycleResult.Ok(); + } + verifiedRecoveryPublication = true; } else { @@ -760,7 +771,7 @@ private async Task RunNativeAsync( _store.Save(binding); var guarded = new JournaledTransport(new(target, transport), _store, binding, reason == LocalAiQuiesceReason.Teardown); - var coordinator = new LocalAiGatewayProviderCoordinator(guarded, _credentials.GetOrCreate, logger); + var coordinator = new LocalAiGatewayProviderCoordinator(guarded, _credentials.GetOrCreate, logger, getString); var ownedInstall = install with { Manifest = install.Manifest with { GatewayFallbackModel = binding.PreviousPrimary } }; ct.ThrowIfCancellationRequested(); if (reason is null) progress?.Report(LocalAiRuntimeStartStage.PublishingProvider); @@ -769,6 +780,8 @@ private async Task RunNativeAsync( : await coordinator.PublishAsync(ownedInstall, ct).ConfigureAwait(false); if (result.Success && reason == LocalAiQuiesceReason.Teardown) await guarded.CompleteTeardownAsync(ct).ConfigureAwait(false); + if (result.Success && verifiedRecoveryPublication) + _endpointRecovery = null; return result; } catch (OperationCanceledException) when (ct.IsCancellationRequested) { throw; } diff --git a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs index 895e20b4e3..5903c28e9e 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs @@ -19,33 +19,39 @@ internal sealed class LocalAiGatewayProviderCoordinator : ILocalAiEndpointLifecy private readonly ILocalAiGatewayAtomicConfigurationTransport? _atomicConfiguration; private readonly Func? _getApiKey; private readonly IOpenClawLogger _logger; + private readonly Func? _getString; public LocalAiGatewayProviderCoordinator( IWslCommandRunner commands, ILocalAiGatewayDistroResolver distroResolver, - IOpenClawLogger logger) + IOpenClawLogger logger, + Func? getString = null) : this(new WslLocalAiGatewayConfigurationTransport( commands ?? throw new ArgumentNullException(nameof(commands)), - distroResolver ?? throw new ArgumentNullException(nameof(distroResolver))), logger) + distroResolver ?? throw new ArgumentNullException(nameof(distroResolver))), logger, getString) { } public LocalAiGatewayProviderCoordinator( ILocalAiGatewayConfigurationTransport configuration, - IOpenClawLogger logger) + IOpenClawLogger logger, + Func? getString = null) { _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _getString = getString; } public LocalAiGatewayProviderCoordinator( ILocalAiGatewayAtomicConfigurationTransport configuration, Func getApiKey, - IOpenClawLogger logger) + IOpenClawLogger logger, + Func? getString = null) { _atomicConfiguration = configuration ?? throw new ArgumentNullException(nameof(configuration)); _getApiKey = getApiKey ?? throw new ArgumentNullException(nameof(getApiKey)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _getString = getString; } public async Task QuiesceAsync( @@ -67,7 +73,8 @@ public async Task QuiesceAsync( if (current.ProviderExists) { _ = BuildProvider(install); - if (!MatchesProvider(current.ProviderJson!, install)) + if (!MatchesProvider(current.ProviderJson!, install) && + !MatchesLegacyProvider(current.ProviderJson!, install)) { return Failed("The llamacpp provider was changed outside the companion; preserving it and refusing to cycle the managed endpoint."); } @@ -174,8 +181,16 @@ public async Task PublishAsync( GatewayCapture current = await CaptureGatewayAsync(cancellationToken).ConfigureAwait(false); LocalAiEndpointLifecycleResult admission = CheckPublication(current, install); - if (!admission.Success || current.ProviderExists) + if (!admission.Success) return admission; + if (current.ProviderExists) + { + // Admission accepted the current shape or the exact pre-effort shape; + // only the latter needs its owned model array upgraded. + return MatchesProvider(current.ProviderJson!, install) + ? admission + : await MigrateLegacyProviderAsync(current, install, cancellationToken).ConfigureAwait(false); + } if (_atomicConfiguration is not null) { @@ -262,7 +277,8 @@ private LocalAiEndpointLifecycleResult CheckPublication(GatewayCapture current, string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); if (current.ProviderExists) { - return MatchesProvider(current.ProviderJson!, install) && + return (MatchesProvider(current.ProviderJson!, install) || + MatchesLegacyProvider(current.ProviderJson!, install)) && current.PrimaryExists && string.Equals(current.PrimaryModel, managedPrimary, StringComparison.Ordinal) ? LocalAiEndpointLifecycleResult.Ok() @@ -283,6 +299,65 @@ private LocalAiEndpointLifecycleResult CheckPublication(GatewayCapture current, return LocalAiEndpointLifecycleResult.Ok(); } + private async Task MigrateLegacyProviderAsync( + GatewayCapture current, LocalAiResolvedInstall install, CancellationToken ct) + { + // Change the owned model array only. The provider readback may redact + // the API key, so replaying the entire provider would destroy a secret. + string models = LocalAiGatewayProviderDefinition.BuildProviderModelsJson(install); + if (_atomicConfiguration is not null) + { + var patch = new JsonObject { ["models"] = new JsonObject + { + ["providers"] = new JsonObject { ["llamacpp"] = new JsonObject + { + ["models"] = JsonNode.Parse(models), + } }, + } }; + LocalAiEndpointLifecycleResult applied = await ApplyAtomicAsync(current, patch, ct).ConfigureAwait(false); + if (!applied.Success) return applied; + } + else + { + using JsonDocument oldProvider = ParseBounded(current.ProviderJson!); + string oldModels = oldProvider.RootElement.GetProperty("models").GetRawText(); + string[] args = ["config", "set", LocalAiGatewayProviderDefinition.ProviderModelsPath, + models, "--strict-json", "--replace", "--expect-current-json", oldModels]; + // The CLI does not allow conditional expectations on dry runs. + // Validate the proposed array first; the actual write remains guarded. + LocalAiGatewayCommandOutcome dryRun = await _configuration!.RunAsync( + ["config", "set", LocalAiGatewayProviderDefinition.ProviderModelsPath, + models, "--strict-json", "--replace", "--dry-run"], ct).ConfigureAwait(false); + if (!dryRun.Routed || !dryRun.Result!.Success) + return Failed(Localized("LocalAi_GatewayLegacyMigrationValidation", "The legacy Local AI model settings could not be validated for migration. No Gateway configuration was changed.")); + LocalAiGatewayCommandOutcome applied = await _configuration.RunAsync(args, ct).ConfigureAwait(false); + if (!applied.Routed || !applied.Result!.Success) + return Failed(Localized("LocalAi_GatewayLegacyMigrationChanged", "The legacy Local AI model settings changed before migration could be confirmed. Reconnect the original Gateway to reconcile.")); + // The conditional write has landed; drain the restart despite + // cancellation so the running Gateway can adopt this revision. + LocalAiGatewayCommandOutcome restarted = await _configuration.RunAsync( + ["gateway", "restart"], CancellationToken.None).ConfigureAwait(false); + if (!restarted.Routed || !restarted.Result!.Success) + return Failed(Localized("LocalAi_GatewayLegacyMigrationRestart", "The Local AI model settings were saved, but the WSL Gateway restart was not confirmed. Reconcile the original Gateway before retrying.")); + } + + return await VerifyMigratedProviderAsync(install).ConfigureAwait(false); + } + + private async Task VerifyMigratedProviderAsync(LocalAiResolvedInstall install) + { + // The migration write has already landed. Read it back without the + // caller's token, and never tear down the route that was just upgraded. + GatewayCapture verified = await CaptureGatewayAsync(CancellationToken.None).ConfigureAwait(false); + return verified.Success && verified.ProviderExists && + MatchesProvider(verified.ProviderJson!, install) && + verified.PrimaryExists && + string.Equals(verified.PrimaryModel, + LocalAiGatewayProviderDefinition.BuildPrimaryModel(install), StringComparison.Ordinal) + ? LocalAiEndpointLifecycleResult.Ok() + : Failed(Localized("LocalAi_GatewaySavedRouteVerification", "The saved Local AI route could not be verified. Preserve the endpoint and reconcile the original Gateway.")); + } + private LocalAiEndpointLifecycleResult PublicationFailed( string detail, LocalAiEndpointLifecycleResult cleanup) => cleanup.Success @@ -405,6 +480,12 @@ private static JsonDocument ParseBounded(string value) return JsonDocument.Parse(value, new JsonDocumentOptions { MaxDepth = 32 }); } + private string Localized(string key, string fallback) + { + string? value = _getString?.Invoke(key); + return string.IsNullOrWhiteSpace(value) || value == key ? fallback : value; + } + private LocalAiEndpointLifecycleResult Failed(string detail) { _logger.Warn(detail); @@ -418,6 +499,9 @@ private string BuildProvider(LocalAiResolvedInstall install) => _getApiKey is nu private bool MatchesProvider(string json, LocalAiResolvedInstall install) => LocalAiGatewayProviderDefinition.MatchesProviderJson(json, install, _getApiKey?.Invoke()); + private bool MatchesLegacyProvider(string json, LocalAiResolvedInstall install) => + LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(json, install, _getApiKey?.Invoke()); + private async Task CaptureAtomicAsync(CancellationToken ct) { try diff --git a/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs b/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs index 64f01148f6..a155a82259 100644 --- a/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs +++ b/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs @@ -728,7 +728,8 @@ private async Task ResolveLocalAiSetupRouteAsync() LocalAiInstallationObservation.InspectAsync, ct => Task.Run(() => new OpenClaw.Shared.Inference.CudaHostHardwareProbe().Probe(), ct).WaitAsync(ct), () => new LocalAiGatewayProviderCoordinator(new WslExeCommandRunner(new AppLogger()), - new LocalAiGatewayDistroResolver(_callbacks.GetGatewayRegistry()), new AppLogger()), + new LocalAiGatewayDistroResolver(_callbacks.GetGatewayRegistry()), new AppLogger(), + LocalizationHelper.GetString), ReconcileSetupConnectionAsync, NotifySetupRegistryRecovery, _callbacks.GetLocalAiGatewayLifecycle?.Invoke()); diff --git a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw index df771a24e3..60ab009a47 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw @@ -10178,4 +10178,8 @@ After enrolling, return to Settings > Windows Update and check for updates. I Follow delegated work from this conversation. Widgets and tabs this session published. No panels are available for this conversation yet. + The legacy Local AI model settings could not be validated for migration. No Gateway configuration was changed. + The legacy Local AI model settings changed before migration could be confirmed. Reconnect the original Gateway to reconcile. + The Local AI model settings were saved, but the WSL Gateway restart was not confirmed. Reconcile the original Gateway before retrying. + The saved Local AI route could not be verified. Preserve the endpoint and reconcile the original Gateway. diff --git a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw index 08ac904554..281ac8a17b 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw @@ -10107,4 +10107,8 @@ Après votre inscription, revenez dans Paramètres > Windows Update et recher Suivez le travail délégué depuis cette conversation. Widgets et onglets publiés par cette session. Aucun panneau n'est encore disponible pour cette conversation. + Les anciens réglages du modèle d’IA locale n’ont pas pu être validés pour la migration. La configuration du Gateway n’a pas été modifiée. + Les anciens réglages du modèle d’IA locale ont changé avant la confirmation de la migration. Reconnectez le Gateway d’origine pour les réconcilier. + Les réglages du modèle d’IA locale ont été enregistrés, mais le redémarrage du Gateway WSL n’a pas été confirmé. Réconciliez le Gateway d’origine avant de réessayer. + La route d’IA locale enregistrée n’a pas pu être vérifiée. Conservez le point de terminaison et réconciliez le Gateway d’origine. diff --git a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw index 0517f84327..33d141516b 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw @@ -10108,4 +10108,8 @@ Ga na inschrijving terug naar Instellingen > Windows Update en controleer op Volg gedelegeerd werk vanuit dit gesprek. Widgets en tabbladen die deze sessie heeft gepubliceerd. Er zijn nog geen panelen beschikbaar voor dit gesprek. + De oude modelinstellingen voor Lokale AI konden niet voor migratie worden gevalideerd. De Gateway-configuratie is niet gewijzigd. + De oude modelinstellingen voor Lokale AI zijn gewijzigd voordat de migratie kon worden bevestigd. Maak opnieuw verbinding met de oorspronkelijke Gateway om dit te herstellen. + De modelinstellingen voor Lokale AI zijn opgeslagen, maar het opnieuw starten van de WSL Gateway is niet bevestigd. Herstel de oorspronkelijke Gateway voordat je het opnieuw probeert. + De opgeslagen route voor Lokale AI kon niet worden geverifieerd. Behoud het eindpunt en herstel de oorspronkelijke Gateway. diff --git a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw index 9ea7711296..585c6af909 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw @@ -10160,4 +10160,8 @@ Após a inscrição, volte para Configurações > Windows Update e verifique Acompanhe o trabalho delegado desta conversa. Widgets e guias publicados por esta sessão. Ainda não há painéis disponíveis para esta conversa. + Não foi possível validar as configurações antigas do modelo de IA local para migração. A configuração do Gateway não foi alterada. + As configurações antigas do modelo de IA local mudaram antes da confirmação da migração. Reconecte o Gateway original para reconciliá-las. + As configurações do modelo de IA local foram salvas, mas a reinicialização do Gateway WSL não foi confirmada. Reconcilie o Gateway original antes de tentar novamente. + Não foi possível verificar a rota de IA local salva. Preserve o endpoint e reconcilie o Gateway original. diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw index aec0a0cfb5..c1a56acd38 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw @@ -10107,4 +10107,8 @@ 跟踪此对话中委派的工作。 此会话发布的小组件和选项卡。 此对话暂无可用的面板。 + 无法验证旧版本地 AI 模型设置以进行迁移。Gateway 配置未更改。 + 旧版本地 AI 模型设置在迁移确认前发生了变化。请重新连接原 Gateway 以协调配置。 + 本地 AI 模型设置已保存,但无法确认 WSL Gateway 已重启。重试前请协调原 Gateway 的配置。 + 无法验证已保存的本地 AI 路由。请保留端点并协调原 Gateway 的配置。 diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw index d0f0cb3c2e..cdb5b52a3d 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw @@ -10107,4 +10107,8 @@ 追蹤此對話中委派的工作。 此工作階段發布的小工具和索引標籤。 此對話尚無可用的面板。 + 無法驗證舊版本機 AI 模型設定以進行遷移。Gateway 設定未變更。 + 舊版本機 AI 模型設定在確認遷移前發生變更。請重新連線原始 Gateway 以協調設定。 + 本機 AI 模型設定已儲存,但無法確認 WSL Gateway 已重新啟動。重試前請協調原始 Gateway 的設定。 + 無法驗證已儲存的本機 AI 路由。請保留端點並協調原始 Gateway 的設定。 diff --git a/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs b/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs index eba07840f7..08df50f9a5 100644 --- a/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs +++ b/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs @@ -253,12 +253,50 @@ public async Task RouterPreset_BoundsOmittedGenerationAtGatewayMaximum() $"n-predict = {gatewayMaximum}", launch.PresetContent.Split(Environment.NewLine)); Assert.Contains("ctx-size = 262144", launch.PresetContent.Split(Environment.NewLine)); + Assert.DoesNotContain("reasoning =", launch.PresetContent); + Assert.DoesNotContain("reasoning = on", launch.PresetContent.Split(Environment.NewLine)); + JsonElement model = provider.RootElement.GetProperty("models")[0]; + JsonElement compat = model.GetProperty("compat"); + Assert.Equal(new[] { "none", "medium" }, compat.GetProperty("supportedReasoningEfforts") + .EnumerateArray().Select(item => item.GetString()).ToArray()); + Assert.Equal("none", compat.GetProperty("reasoningEffortMap").GetProperty("off").GetString()); + Assert.Equal(JsonValueKind.Null, model.GetProperty("thinkingLevelMap").GetProperty("minimal").ValueKind); + Assert.Equal(JsonValueKind.Null, model.GetProperty("thinkingLevelMap").GetProperty("high").ValueKind); Assert.Contains("cache-type-k = q8_0", launch.PresetContent.Split(Environment.NewLine)); Assert.Contains("cache-type-v = q8_0", launch.PresetContent.Split(Environment.NewLine)); Assert.Contains("cache-type-k-draft = q8_0", launch.PresetContent.Split(Environment.NewLine)); Assert.Contains("cache-type-v-draft = q8_0", launch.PresetContent.Split(Environment.NewLine)); } + [Fact] + public void Qwen38Provider_HidesUnsupportedMinimalButKeepsFourSupportedEfforts() + { + LocalModelInfo model = LocalModelCatalog.Find(LocalModelCatalog.Qwen38_27BModelId)!; + var manifest = ValidManifest() with + { + ModelCatalogId = model.Id, + ModelAlias = model.Id, + ModelAsset = new LocalAiAssetReceipt + { + FileName = Path.GetFileName(model.Weights.RelativePath), + SourceUrl = model.Weights.DownloadUri.AbsoluteUri, + SizeBytes = model.Weights.SizeBytes, + Sha256 = model.Weights.Sha256.Value, + }, + }; + var install = new LocalAiResolvedInstall(manifest, "llama-server", "model.gguf", + new Uri("http://127.0.0.1:28765/v1")); + + using JsonDocument provider = JsonDocument.Parse(LocalAiGatewayProviderDefinition.BuildProviderJson(install)); + JsonElement configured = provider.RootElement.GetProperty("models")[0]; + Assert.Equal(new[] { "none", "low", "medium", "high" }, + configured.GetProperty("compat").GetProperty("supportedReasoningEfforts") + .EnumerateArray().Select(value => value.GetString()).ToArray()); + Assert.Equal(JsonValueKind.Null, + configured.GetProperty("thinkingLevelMap").GetProperty("minimal").ValueKind); + Assert.False(configured.GetProperty("thinkingLevelMap").TryGetProperty("high", out _)); + } + [Fact] public async Task Manifest_OmitsLegacyHardwareProfileIdFromNewWrites() { diff --git a/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs b/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs index 1b7f9423b6..3a622ee179 100644 --- a/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs +++ b/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs @@ -5,6 +5,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.Json; +using System.Text.Json.Nodes; namespace OpenClaw.SetupEngine.Tests; @@ -182,6 +183,46 @@ public async Task FreshProcessUninstall_RemovesExactManagedProviderAndPrimary() command.Contains("LOCAL_AI_GATEWAY_UNSET", StringComparison.Ordinal)); } + [Fact] + public async Task FreshProcessUninstall_RemovesExactLegacyQwenProvider() + { + using var temp = new TempDirectory("local-ai-gateway-uninstall-"); + LocalAiResolvedInstall install = await SaveManifestAsync(temp.Path); + string provider = LegacyProviderJson(install); + string primary = JsonSerializer.Serialize(LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)); + var commands = new GatewayStateCommandRunner(provider, primary); + SetupContext context = CreateContext(temp.Path, commands); + context.IsUninstalling = true; + + Assert.True(LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson(provider, install)); + Assert.False(LocalAiGatewayProviderDefinition.MatchesProviderJson(provider, install)); + await new ConfigureLocalAiGatewayStep().RollbackAsync(context, CancellationToken.None); + + Assert.Null(commands.ProviderJson); + Assert.Null(commands.PrimaryJson); + } + + [Fact] + public async Task FreshProcessUninstall_PreservesEditedLegacyQwenProvider() + { + using var temp = new TempDirectory("local-ai-gateway-uninstall-"); + LocalAiResolvedInstall install = await SaveManifestAsync(temp.Path); + var edited = JsonNode.Parse(LegacyProviderJson(install))!.AsObject(); + edited["baseUrl"] = "http://127.0.0.1:49999/v1"; + string provider = edited.ToJsonString(); + string primary = JsonSerializer.Serialize(LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)); + var commands = new GatewayStateCommandRunner(provider, primary); + SetupContext context = CreateContext(temp.Path, commands); + context.IsUninstalling = true; + + Assert.False(LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson(provider, install)); + await Assert.ThrowsAsync(() => + new ConfigureLocalAiGatewayStep().RollbackAsync(context, CancellationToken.None)); + + Assert.Equal(provider, commands.ProviderJson); + Assert.Equal(primary, commands.PrimaryJson); + } + [Fact] public async Task FreshProcessUninstall_AcceptsCliRedactedManagedApiKey() { @@ -301,6 +342,31 @@ public async Task Recovery_ReplacesExactManagedProviderAfterAutomaticPortChanges Assert.Equal(primary, commands.PrimaryJson); } + [Fact] + public async Task Recovery_ExactLegacyQwenProviderCanTransitionToReplacement() + { + using var temp = new TempDirectory("local-ai-gateway-recovery-"); + LocalAiResolvedInstall original = await SaveManifestAsync(temp.Path, "openai/gpt-5"); + string legacy = LegacyProviderJson(original); + string primary = JsonSerializer.Serialize(LocalAiGatewayProviderDefinition.BuildPrimaryModel(original)); + var commands = new GatewayStateCommandRunner(legacy, primary); + SetupContext context = CreateRecoveryContext(temp.Path, commands); + context.LocalAiRecoveryOriginalInstall = original; + context.LocalAiRecoveryReceiptRollbackAllowed = true; + var replacementManifest = original.Manifest with { Endpoint = "http://127.0.0.1:39876/v1" }; + var store = new LocalAiManifestStore(new LocalAiPaths(temp.Path)); + await store.SaveAsync(replacementManifest); + context.LocalAiResolvedInstall = store.ResolveAndValidate(replacementManifest); + + StepResult result = await new ConfigureLocalAiGatewayStep().ExecuteAsync(context, CancellationToken.None); + + Assert.Equal(StepOutcome.Success, result.Outcome); + Assert.True(context.LocalAiRecoveryProviderTransition); + Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson( + commands.ProviderJson!, context.LocalAiResolvedInstall)); + Assert.Equal(primary, commands.PrimaryJson); + } + [Fact] public async Task Recovery_PreservesProviderThatMatchesNeitherEndpoint() { @@ -335,6 +401,36 @@ public async Task Recovery_PreservesProviderThatMatchesNeitherEndpoint() Assert.Equal(primary, commands.PrimaryJson); } + [Fact] + public async Task Recovery_RollbackRestoresExactLegacyShapeWithoutReplayingRedactedCredential() + { + using var temp = new TempDirectory("local-ai-gateway-recovery-"); + LocalAiResolvedInstall original = await SaveManifestAsync(temp.Path, "openai/gpt-5"); + var prior = JsonNode.Parse(LegacyProviderJson(original))!.AsObject(); + prior["apiKey"] = LocalAiGatewayProviderDefinition.CliRedactedApiKey; + string originalProvider = prior.ToJsonString(); + string primary = JsonSerializer.Serialize(LocalAiGatewayProviderDefinition.BuildPrimaryModel(original)); + var commands = new GatewayStateCommandRunner(originalProvider, primary); + SetupContext context = CreateRecoveryContext(temp.Path, commands); + context.LocalAiRecoveryOriginalInstall = original; + context.LocalAiRecoveryReceiptRollbackAllowed = true; + var replacementManifest = original.Manifest with { Endpoint = "http://127.0.0.1:39876/v1" }; + var store = new LocalAiManifestStore(new LocalAiPaths(temp.Path)); + await store.SaveAsync(replacementManifest); + context.LocalAiResolvedInstall = store.ResolveAndValidate(replacementManifest); + var step = new ConfigureLocalAiGatewayStep(); + + StepResult configured = await step.ExecuteAsync(context, CancellationToken.None); + Assert.Equal(StepOutcome.Success, configured.Outcome); + await step.RollbackAsync(context, CancellationToken.None); + + Assert.True(context.LocalAiRecoveryReceiptRollbackAllowed); + Assert.True(LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(commands.ProviderJson!, original)); + Assert.False(LocalAiGatewayProviderDefinition.MatchesProviderJson(commands.ProviderJson!, original)); + Assert.Equal("llama-local", JsonNode.Parse(commands.ProviderJson!)!["apiKey"]!.GetValue()); + Assert.Equal(primary, commands.PrimaryJson); + } + [Fact] public async Task Recovery_RollbackRestoresOriginalProviderAndReceipt() { @@ -708,6 +804,18 @@ public async Task Recovery_ProviderCreationRollbackCancellationKeepsReplacementR Assert.False(context.LocalAiRecoveryReceiptRollbackAllowed); } + private static string LegacyProviderJson(LocalAiResolvedInstall install) + { + var provider = JsonNode.Parse(LocalAiGatewayProviderDefinition.BuildProviderJson(install))!.AsObject(); + var model = ((JsonArray)provider["models"]!)[0]!.AsObject(); + var compat = model["compat"]!.AsObject(); + compat.Remove("supportsReasoningEffort"); + compat.Remove("supportedReasoningEfforts"); + compat.Remove("reasoningEffortMap"); + model.Remove("thinkingLevelMap"); + return provider.ToJsonString(); + } + private static SetupContext CreateContext(string localDataDirectory, ICommandRunner commands) { var config = new SetupConfig { LocalAi = new LocalAiConfig { Enabled = true } }; diff --git a/tests/OpenClaw.SetupEngine.Tests/NativeLocalAiLifecycleTests.cs b/tests/OpenClaw.SetupEngine.Tests/NativeLocalAiLifecycleTests.cs index 38e20a9ebc..1ec379b663 100644 --- a/tests/OpenClaw.SetupEngine.Tests/NativeLocalAiLifecycleTests.cs +++ b/tests/OpenClaw.SetupEngine.Tests/NativeLocalAiLifecycleTests.cs @@ -922,6 +922,39 @@ public async Task EndpointRecoveryReportsVerificationBeforeProbeWithoutAnotherPu Assert.False(fixture.Store.Load()!.Pending); } + [Fact] + public async Task EndpointRecoveryMigratesExactLegacyProviderOnceAfterVerification() + { + using var fixture = new Fixture(); + await fixture.Lifecycle.PrepareAsync(fixture.Install, default); + fixture.Rpc.LoseReply = true; + Assert.False((await fixture.Lifecycle.PublishAsync(fixture.Install)).Success); + fixture.Rpc.LoseReply = false; + // The unconfirmed route is the exact shape an earlier release published. + var provider = fixture.Rpc.Config["models"]!["providers"]!["llamacpp"]!.AsObject(); + RemoveEffortMetadata(provider); + fixture.Rpc.Revision++; + string credential = fixture.Lifecycle.GetApiKey()!; + Assert.False(LocalAiGatewayProviderDefinition.MatchesProviderJson(provider.ToJsonString(), fixture.Install, credential)); + var recovered = fixture.CreateLifecycle(); + recovered.Register(Record, fixture.Rpc); + await recovered.PrepareAsync(fixture.Install, default); + var stages = new List(); + + var result = await recovered.CompleteStartAsync(fixture.Install, default, + new SynchronousProgress(stages.Add)); + + Assert.True(result.Success, result.Detail); + Assert.Equal(1, fixture.Rpc.Verifications); + int verifying = stages.IndexOf(LocalAiRuntimeStartStage.VerifyingEndpoint); + Assert.InRange(verifying, 0, stages.IndexOf(LocalAiRuntimeStartStage.PublishingProvider) - 1); + Assert.Equal(2, fixture.Rpc.Writes); + Assert.False(fixture.Store.Load()!.Pending); + var migrated = fixture.Rpc.Config["models"]!["providers"]!["llamacpp"]!; + Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson(migrated.ToJsonString(), fixture.Install, credential)); + Assert.Equal(credential, migrated["apiKey"]!.GetValue()); + } + [Theory] [InlineData("none")] [InlineData("gateway")] @@ -1946,6 +1979,62 @@ await Assert.ThrowsAsync(() => Assert.Equal(0, fixture.Rpc.Writes); } + [Fact] + public async Task NativeRecoveryMigratesExactLegacyQwenProviderAfterInference() + { + using var fixture = new Fixture(); + await SeedLegacyPublishedProviderAsync(fixture); + string credential = fixture.Lifecycle.GetApiKey()!; + + await fixture.Lifecycle.PrepareStartAsync(fixture.Install, default); + Assert.Equal(fixture.Install.Endpoint!.Port, fixture.Lifecycle.GetRecoveryPort(fixture.Install)); + var published = await fixture.Lifecycle.PublishAsync(fixture.Install); + + Assert.True(published.Success, published.Detail); + Assert.False(fixture.Store.Load()!.Pending); + Assert.Equal(1, fixture.Rpc.Verifications); + Assert.Equal(1, fixture.Rpc.Writes); + Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson( + fixture.Rpc.Config["models"]!["providers"]!["llamacpp"]!.ToJsonString(), + fixture.Install, credential)); + Assert.Equal(credential, + fixture.Rpc.Config["models"]!["providers"]!["llamacpp"]!["apiKey"]!.GetValue()); + } + + [Fact] + public async Task NativeTeardownAcceptsExactLegacyQwenProvider() + { + using var fixture = new Fixture(); + await SeedLegacyPublishedProviderAsync(fixture); + + var result = await fixture.Lifecycle.QuiesceAsync(fixture.Install, LocalAiQuiesceReason.Teardown); + + Assert.True(result.Success, result.Detail); + Assert.Null(fixture.Rpc.Config["models"]!["providers"]!["llamacpp"]); + } + + private static async Task SeedLegacyPublishedProviderAsync(Fixture fixture) + { + await fixture.Lifecycle.PrepareAsync(fixture.Install, default); + var provider = JsonNode.Parse(LocalAiGatewayProviderDefinition.BuildProviderJson( + fixture.Install, fixture.Lifecycle.GetApiKey()!))!.AsObject(); + RemoveEffortMetadata(provider); + fixture.Rpc.Config["models"]!["providers"]!["llamacpp"] = provider; + fixture.Rpc.Config["agents"]!["defaults"]!["model"]!["primary"] = fixture.Model; + fixture.Rpc.Config["agents"]!["defaults"]!["models"]![fixture.Model] = new JsonObject(); + fixture.Rpc.Revision++; + } + + private static void RemoveEffortMetadata(JsonObject provider) + { + var model = ((JsonArray)provider["models"]!)[0]!.AsObject(); + var compat = model["compat"]!.AsObject(); + compat.Remove("supportsReasoningEffort"); + compat.Remove("supportedReasoningEfforts"); + compat.Remove("reasoningEffortMap"); + model.Remove("thinkingLevelMap"); + } + private sealed class Fixture : IDisposable { private readonly TempDirectory _directory = new(); diff --git a/tests/OpenClaw.Tray.Tests/GatewayActionTransportTests.cs b/tests/OpenClaw.Tray.Tests/GatewayActionTransportTests.cs index 4554f19083..9d86a4bce9 100644 --- a/tests/OpenClaw.Tray.Tests/GatewayActionTransportTests.cs +++ b/tests/OpenClaw.Tray.Tests/GatewayActionTransportTests.cs @@ -14,27 +14,27 @@ private sealed class FakeContext : IGatewayActionContext } [Fact] - public void BuildAgentRequestPayload_HasFiveTopLevelFields() + public void BuildAgentRequestPayload_InheritsSessionEffort() { var action = new A2UIAction { Name = "submit", SurfaceId = "form", SourceComponentId = "btnSave" }; var payload = GatewayActionTransport.BuildAgentRequestPayload(action, new FakeContext()); - Assert.Equal(5, payload.Count); + Assert.Equal(4, payload.Count); Assert.Contains("message", payload); Assert.Contains("sessionKey", payload); - Assert.Contains("thinking", payload); + Assert.DoesNotContain("thinking", payload); Assert.Contains("deliver", payload); Assert.Contains("key", payload); } [Fact] - public void BuildAgentRequestPayload_DeliverIsFalse_ThinkingIsLow() + public void BuildAgentRequestPayload_DeliverIsFalse_WithoutChangingEffort() { var action = new A2UIAction { Name = "x", SurfaceId = "s", SourceComponentId = "c" }; var payload = GatewayActionTransport.BuildAgentRequestPayload(action, new FakeContext()); Assert.False(payload["deliver"]!.GetValue()); - Assert.Equal("low", payload["thinking"]!.GetValue()); + Assert.False(payload.ContainsKey("thinking")); } [Fact] diff --git a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs index e66b49ef98..89b7549251 100644 --- a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs +++ b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs @@ -35,6 +35,105 @@ public async Task RpcPublish_UsesOneGuardedPatchAndPreservesOtherProvidersAndAll Assert.Empty(Assert.Single(transport.ReplacementPaths)); } + [Fact] + public async Task RpcPublish_MigratesOnlyLegacyModelArrayAndPreservesCredential() + { + var install = Install(28_765, "cloud/prior"); + var transport = new AtomicTransport("{}"); + transport.SetManaged(install); + StripEffortMetadata((JsonObject)transport.Config["models"]!["providers"]!["llamacpp"]!["models"]![0]!); + var oldProvider = transport.Config["models"]!["providers"]!["llamacpp"]!.ToJsonString(); + Assert.True(LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(oldProvider, install, "actual-local-secret")); + Assert.False(LocalAiGatewayProviderDefinition.MatchesProviderJson(oldProvider, install, "actual-local-secret")); + var coordinator = new LocalAiGatewayProviderCoordinator(transport, () => "actual-local-secret", NullLogger.Instance); + + var result = await coordinator.PublishAsync(install); + + Assert.True(result.Success, result.Detail); + Assert.Single(transport.Patches); + Assert.Equal([LocalAiGatewayProviderDefinition.ProviderModelsPath], Assert.Single(transport.ReplacementPaths)); + Assert.False(transport.Patches[0].GetProperty("models").GetProperty("providers") + .GetProperty("llamacpp").TryGetProperty("apiKey", out _)); + Assert.Equal("actual-local-secret", (string?)transport.Config["models"]?["providers"]?["llamacpp"]?["apiKey"]); + Assert.Equal("cloud/prior", install.Manifest.GatewayFallbackModel); + Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson( + transport.Config["models"]!["providers"]!["llamacpp"]!.ToJsonString(), install, "actual-local-secret")); + } + + [Fact] + public async Task WslPublish_MigratesLegacyModelArrayWithoutOverwritingCredential() + { + var install = Install(28_765); + var commands = new FakeWslCommandRunner(LegacyProviderJson(install, "llama-local"), + LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)); + var coordinator = CreateCoordinator(commands); + + var result = await coordinator.PublishAsync(install); + + Assert.True(result.Success, result.Detail); + Assert.Equal("llama-local", JsonNode.Parse(commands.ProviderJson!)!["apiKey"]!.GetValue()); + Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson(commands.ProviderJson!, install, "llama-local")); + Assert.Contains(commands.Calls, call => call.Contains("--expect-current-json")); + Assert.Contains(commands.Calls, call => call.Contains("gateway") && call.Contains("restart")); + } + + [Fact] + public async Task WslPublish_GatewayThatRejectsMigrationKeepsLegacyRouteUnchanged() + { + var install = Install(28_765); + var original = LegacyProviderJson(install); + var commands = new FakeWslCommandRunner(original, + LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)) { RejectModelsWrite = true }; + + var result = await CreateCoordinator(commands).PublishAsync(install); + + Assert.False(result.Success); + Assert.Equal(original, commands.ProviderJson); + Assert.DoesNotContain(commands.Calls, call => call.Contains("--expect-current-json")); + Assert.DoesNotContain(commands.Calls, call => call.Contains("restart")); + } + + [Fact] + public async Task WslPublish_FinishesRestartAfterConfirmedWriteWhenCallerCancels() + { + var install = Install(28_765); + using var cancellation = new CancellationTokenSource(); + FakeWslCommandRunner? observed = null; + var commands = new FakeWslCommandRunner(LegacyProviderJson(install), + LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)) + { + CommandObserved = _ => + { + if (observed!.Calls[^1].Contains("--expect-current-json")) + cancellation.Cancel(); + }, + }; + observed = commands; + + var result = await CreateCoordinator(commands).PublishAsync(install, cancellation.Token); + + Assert.True(cancellation.IsCancellationRequested); + Assert.True(result.Success, result.Detail); + Assert.Single(commands.Calls, call => call.Contains("restart")); + } + + /// The exact provider shape published before effort metadata was added. + private static string LegacyProviderJson(LocalAiResolvedInstall install, string apiKey = "llama-local") + { + var provider = JsonNode.Parse(LocalAiGatewayProviderDefinition.BuildProviderJson(install, apiKey))!.AsObject(); + StripEffortMetadata((JsonObject)provider["models"]![0]!); + return provider.ToJsonString(); + } + + private static void StripEffortMetadata(JsonObject model) + { + var compat = (JsonObject)model["compat"]!; + compat.Remove("supportsReasoningEffort"); + compat.Remove("supportedReasoningEfforts"); + compat.Remove("reasoningEffortMap"); + model.Remove("thinkingLevelMap"); + } + [Theory] [InlineData(false)] [InlineData(true)] @@ -1046,6 +1145,7 @@ private sealed class FakeWslCommandRunner(string? providerJson, string? primaryM public string? ActiveProviderJson { get; private set; } = providerJson; public string? PrimaryModel { get; private set; } = primaryModel; public string? ProviderAfterApply { get; init; } + public bool RejectModelsWrite { get; init; } public string? PrimaryAfterApply { get; init; } public bool FailReads { get; init; } public HashSet FailedReadCalls { get; init; } = []; @@ -1073,6 +1173,26 @@ public Task RunInDistroAsync( command.Contains(LocalAiGatewayProviderDefinition.ProviderPath); if (providerRead && (FailReads || FailedReadCalls.Contains(++_readCalls))) return Result(1, string.Empty, "wsl.exe failed"); + if (command.Contains("set") && command.Contains(LocalAiGatewayProviderDefinition.ProviderModelsPath)) + { + int guard = command.ToList().IndexOf("--expect-current-json"); + bool dryRun = command.Contains("--dry-run"); + // An older Gateway schema rejects the new fields at validation. + if (RejectModelsWrite || ProviderJson is null || dryRun && guard >= 0 || !dryRun && guard < 0) + return Result(1, string.Empty); + var provider = JsonNode.Parse(ProviderJson)!.AsObject(); + if (!dryRun) + { + using var current = JsonDocument.Parse(provider["models"]!.ToJsonString()); + using var expected = JsonDocument.Parse(command[guard + 1]); + if (!JsonElement.DeepEquals(current.RootElement, expected.RootElement)) + return Result(1, string.Empty); + int path = command.ToList().IndexOf(LocalAiGatewayProviderDefinition.ProviderModelsPath); + provider["models"] = JsonNode.Parse(command[path + 1]); + ProviderJson = provider.ToJsonString(); + } + return Result(0, string.Empty); + } if (command.Contains("/bin/sh")) { if (ProviderAfterApply is not null) From 34f80858968f336432b69f8375f3c1b8451e336a Mon Sep 17 00:00:00 2001 From: Alberto Delgado <44581951+turutupa@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:47:17 -0800 Subject: [PATCH 2/5] fix: republish a legacy WSL Local AI provider; keep native in-place migration A provider in the exact pre-effort shape needs its model metadata upgraded. WSL: the companion never edits it in place. Every runtime start (and every non-ready refresh) quiesces the route first: the provider is unset, the Gateway restarted and the removal read back before any publication, and the llama-server router is in a kill-on-close job object, so it never outlives the app. If a publication still finds the legacy shape, it is withdrawn the same way and published from scratch (API key from the credential store, never from a redacted readback); a failed withdrawal fails the publication and writes nothing new. Native: endpoint recovery deliberately keeps a verified live route and passes the legacy provider to publication, so only the owned model array is patched atomically and read back, as before. Co-Authored-By: Claude Opus 5.5 --- src/OpenClaw.Tray.WinUI/App.xaml.cs | 2 +- .../LocalAiGatewayProviderCoordinator.cs | 74 +++++------- .../Services/WindowManager.cs | 3 +- .../Strings/en-us/Resources.resw | 3 - .../Strings/fr-fr/Resources.resw | 3 - .../Strings/nl-nl/Resources.resw | 3 - .../Strings/pt-br/Resources.resw | 3 - .../Strings/zh-cn/Resources.resw | 3 - .../Strings/zh-tw/Resources.resw | 3 - .../LocalAiGatewayProviderCoordinatorTests.cs | 112 +++++++++--------- 10 files changed, 90 insertions(+), 119 deletions(-) diff --git a/src/OpenClaw.Tray.WinUI/App.xaml.cs b/src/OpenClaw.Tray.WinUI/App.xaml.cs index 71b2858c5e..c9ea547687 100644 --- a/src/OpenClaw.Tray.WinUI/App.xaml.cs +++ b/src/OpenClaw.Tray.WinUI/App.xaml.cs @@ -925,7 +925,7 @@ _dispatcherQueue is null // Model recovery also covers the local runtime's health check. new WslExeCommandRunner(localAiLogger, TimeSpan.FromSeconds(420)), new LocalAiGatewayDistroResolver(_gatewayRegistry), - localAiLogger, LocalizationHelper.GetString); + localAiLogger); _localAiGatewayLifecycle = new LocalAiGatewayLifecycle(localAiPaths, SettingsManager.SettingsDirectoryPath, () => _gatewayRegistry, () => _connectionManager, localAiEndpointLifecycle, localAiLogger, LocalizationHelper.GetString); diff --git a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs index 5903c28e9e..bddfba7309 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs @@ -24,22 +24,19 @@ internal sealed class LocalAiGatewayProviderCoordinator : ILocalAiEndpointLifecy public LocalAiGatewayProviderCoordinator( IWslCommandRunner commands, ILocalAiGatewayDistroResolver distroResolver, - IOpenClawLogger logger, - Func? getString = null) + IOpenClawLogger logger) : this(new WslLocalAiGatewayConfigurationTransport( commands ?? throw new ArgumentNullException(nameof(commands)), - distroResolver ?? throw new ArgumentNullException(nameof(distroResolver))), logger, getString) + distroResolver ?? throw new ArgumentNullException(nameof(distroResolver))), logger) { } public LocalAiGatewayProviderCoordinator( ILocalAiGatewayConfigurationTransport configuration, - IOpenClawLogger logger, - Func? getString = null) + IOpenClawLogger logger) { _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _getString = getString; } public LocalAiGatewayProviderCoordinator( @@ -185,11 +182,25 @@ public async Task PublishAsync( return admission; if (current.ProviderExists) { - // Admission accepted the current shape or the exact pre-effort shape; - // only the latter needs its owned model array upgraded. - return MatchesProvider(current.ProviderJson!, install) - ? admission - : await MigrateLegacyProviderAsync(current, install, cancellationToken).ConfigureAwait(false); + // Admission accepted the current shape or the exact pre-effort shape. + if (MatchesProvider(current.ProviderJson!, install)) + return admission; + // Native endpoint recovery keeps a verified live route, so only the + // owned model array of a pre-effort provider is upgraded in place. + if (_atomicConfiguration is not null) + return await MigrateLegacyProviderAsync(current, install, cancellationToken).ConfigureAwait(false); + // WSL always quiesces before publishing, so a pre-effort provider is + // withdrawn like an endpoint cycle and published from scratch. + LocalAiEndpointLifecycleResult withdrawn = await QuiesceAsync( + install, LocalAiQuiesceReason.EndpointCycle, cancellationToken).ConfigureAwait(false); + if (!withdrawn.Success) + return withdrawn; + current = await CaptureGatewayAsync(cancellationToken).ConfigureAwait(false); + admission = CheckPublication(current, install); + if (!admission.Success) + return admission; + if (current.ProviderExists) + return Failed("The Local AI gateway route changed outside the companion; preserving it instead of publishing the managed endpoint."); } if (_atomicConfiguration is not null) @@ -305,42 +316,15 @@ private async Task MigrateLegacyProviderAsync( // Change the owned model array only. The provider readback may redact // the API key, so replaying the entire provider would destroy a secret. string models = LocalAiGatewayProviderDefinition.BuildProviderModelsJson(install); - if (_atomicConfiguration is not null) + var patch = new JsonObject { ["models"] = new JsonObject { - var patch = new JsonObject { ["models"] = new JsonObject + ["providers"] = new JsonObject { ["llamacpp"] = new JsonObject { - ["providers"] = new JsonObject { ["llamacpp"] = new JsonObject - { - ["models"] = JsonNode.Parse(models), - } }, - } }; - LocalAiEndpointLifecycleResult applied = await ApplyAtomicAsync(current, patch, ct).ConfigureAwait(false); - if (!applied.Success) return applied; - } - else - { - using JsonDocument oldProvider = ParseBounded(current.ProviderJson!); - string oldModels = oldProvider.RootElement.GetProperty("models").GetRawText(); - string[] args = ["config", "set", LocalAiGatewayProviderDefinition.ProviderModelsPath, - models, "--strict-json", "--replace", "--expect-current-json", oldModels]; - // The CLI does not allow conditional expectations on dry runs. - // Validate the proposed array first; the actual write remains guarded. - LocalAiGatewayCommandOutcome dryRun = await _configuration!.RunAsync( - ["config", "set", LocalAiGatewayProviderDefinition.ProviderModelsPath, - models, "--strict-json", "--replace", "--dry-run"], ct).ConfigureAwait(false); - if (!dryRun.Routed || !dryRun.Result!.Success) - return Failed(Localized("LocalAi_GatewayLegacyMigrationValidation", "The legacy Local AI model settings could not be validated for migration. No Gateway configuration was changed.")); - LocalAiGatewayCommandOutcome applied = await _configuration.RunAsync(args, ct).ConfigureAwait(false); - if (!applied.Routed || !applied.Result!.Success) - return Failed(Localized("LocalAi_GatewayLegacyMigrationChanged", "The legacy Local AI model settings changed before migration could be confirmed. Reconnect the original Gateway to reconcile.")); - // The conditional write has landed; drain the restart despite - // cancellation so the running Gateway can adopt this revision. - LocalAiGatewayCommandOutcome restarted = await _configuration.RunAsync( - ["gateway", "restart"], CancellationToken.None).ConfigureAwait(false); - if (!restarted.Routed || !restarted.Result!.Success) - return Failed(Localized("LocalAi_GatewayLegacyMigrationRestart", "The Local AI model settings were saved, but the WSL Gateway restart was not confirmed. Reconcile the original Gateway before retrying.")); - } - + ["models"] = JsonNode.Parse(models), + } }, + } }; + LocalAiEndpointLifecycleResult applied = await ApplyAtomicAsync(current, patch, ct).ConfigureAwait(false); + if (!applied.Success) return applied; return await VerifyMigratedProviderAsync(install).ConfigureAwait(false); } diff --git a/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs b/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs index a155a82259..64f01148f6 100644 --- a/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs +++ b/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs @@ -728,8 +728,7 @@ private async Task ResolveLocalAiSetupRouteAsync() LocalAiInstallationObservation.InspectAsync, ct => Task.Run(() => new OpenClaw.Shared.Inference.CudaHostHardwareProbe().Probe(), ct).WaitAsync(ct), () => new LocalAiGatewayProviderCoordinator(new WslExeCommandRunner(new AppLogger()), - new LocalAiGatewayDistroResolver(_callbacks.GetGatewayRegistry()), new AppLogger(), - LocalizationHelper.GetString), + new LocalAiGatewayDistroResolver(_callbacks.GetGatewayRegistry()), new AppLogger()), ReconcileSetupConnectionAsync, NotifySetupRegistryRecovery, _callbacks.GetLocalAiGatewayLifecycle?.Invoke()); diff --git a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw index 60ab009a47..b9cc4595a5 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw @@ -10178,8 +10178,5 @@ After enrolling, return to Settings > Windows Update and check for updates. I Follow delegated work from this conversation. Widgets and tabs this session published. No panels are available for this conversation yet. - The legacy Local AI model settings could not be validated for migration. No Gateway configuration was changed. - The legacy Local AI model settings changed before migration could be confirmed. Reconnect the original Gateway to reconcile. - The Local AI model settings were saved, but the WSL Gateway restart was not confirmed. Reconcile the original Gateway before retrying. The saved Local AI route could not be verified. Preserve the endpoint and reconcile the original Gateway. diff --git a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw index 281ac8a17b..e130c4597d 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw @@ -10107,8 +10107,5 @@ Après votre inscription, revenez dans Paramètres > Windows Update et recher Suivez le travail délégué depuis cette conversation. Widgets et onglets publiés par cette session. Aucun panneau n'est encore disponible pour cette conversation. - Les anciens réglages du modèle d’IA locale n’ont pas pu être validés pour la migration. La configuration du Gateway n’a pas été modifiée. - Les anciens réglages du modèle d’IA locale ont changé avant la confirmation de la migration. Reconnectez le Gateway d’origine pour les réconcilier. - Les réglages du modèle d’IA locale ont été enregistrés, mais le redémarrage du Gateway WSL n’a pas été confirmé. Réconciliez le Gateway d’origine avant de réessayer. La route d’IA locale enregistrée n’a pas pu être vérifiée. Conservez le point de terminaison et réconciliez le Gateway d’origine. diff --git a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw index 33d141516b..0c61055ff0 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw @@ -10108,8 +10108,5 @@ Ga na inschrijving terug naar Instellingen > Windows Update en controleer op Volg gedelegeerd werk vanuit dit gesprek. Widgets en tabbladen die deze sessie heeft gepubliceerd. Er zijn nog geen panelen beschikbaar voor dit gesprek. - De oude modelinstellingen voor Lokale AI konden niet voor migratie worden gevalideerd. De Gateway-configuratie is niet gewijzigd. - De oude modelinstellingen voor Lokale AI zijn gewijzigd voordat de migratie kon worden bevestigd. Maak opnieuw verbinding met de oorspronkelijke Gateway om dit te herstellen. - De modelinstellingen voor Lokale AI zijn opgeslagen, maar het opnieuw starten van de WSL Gateway is niet bevestigd. Herstel de oorspronkelijke Gateway voordat je het opnieuw probeert. De opgeslagen route voor Lokale AI kon niet worden geverifieerd. Behoud het eindpunt en herstel de oorspronkelijke Gateway. diff --git a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw index 585c6af909..440fc7e96f 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw @@ -10160,8 +10160,5 @@ Após a inscrição, volte para Configurações > Windows Update e verifique Acompanhe o trabalho delegado desta conversa. Widgets e guias publicados por esta sessão. Ainda não há painéis disponíveis para esta conversa. - Não foi possível validar as configurações antigas do modelo de IA local para migração. A configuração do Gateway não foi alterada. - As configurações antigas do modelo de IA local mudaram antes da confirmação da migração. Reconecte o Gateway original para reconciliá-las. - As configurações do modelo de IA local foram salvas, mas a reinicialização do Gateway WSL não foi confirmada. Reconcilie o Gateway original antes de tentar novamente. Não foi possível verificar a rota de IA local salva. Preserve o endpoint e reconcilie o Gateway original. diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw index c1a56acd38..87d626bc91 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw @@ -10107,8 +10107,5 @@ 跟踪此对话中委派的工作。 此会话发布的小组件和选项卡。 此对话暂无可用的面板。 - 无法验证旧版本地 AI 模型设置以进行迁移。Gateway 配置未更改。 - 旧版本地 AI 模型设置在迁移确认前发生了变化。请重新连接原 Gateway 以协调配置。 - 本地 AI 模型设置已保存,但无法确认 WSL Gateway 已重启。重试前请协调原 Gateway 的配置。 无法验证已保存的本地 AI 路由。请保留端点并协调原 Gateway 的配置。 diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw index cdb5b52a3d..8472bc4f58 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw @@ -10107,8 +10107,5 @@ 追蹤此對話中委派的工作。 此工作階段發布的小工具和索引標籤。 此對話尚無可用的面板。 - 無法驗證舊版本機 AI 模型設定以進行遷移。Gateway 設定未變更。 - 舊版本機 AI 模型設定在確認遷移前發生變更。請重新連線原始 Gateway 以協調設定。 - 本機 AI 模型設定已儲存,但無法確認 WSL Gateway 已重新啟動。重試前請協調原始 Gateway 的設定。 無法驗證已儲存的本機 AI 路由。請保留端點並協調原始 Gateway 的設定。 diff --git a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs index 89b7549251..e7d439256a 100644 --- a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs +++ b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs @@ -61,60 +61,87 @@ public async Task RpcPublish_MigratesOnlyLegacyModelArrayAndPreservesCredential( } [Fact] - public async Task WslPublish_MigratesLegacyModelArrayWithoutOverwritingCredential() + public async Task WslPublish_WithdrawsLegacyProviderThenPublishesCurrentShape() { var install = Install(28_765); - var commands = new FakeWslCommandRunner(LegacyProviderJson(install, "llama-local"), - LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)); - var coordinator = CreateCoordinator(commands); + string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); + string legacy = RedactApiKey(LegacyProviderJson(install)); + Assert.Contains(LocalAiGatewayProviderDefinition.CliRedactedApiKey, legacy); + var commands = new FakeWslCommandRunner(legacy, managedPrimary) + { + ProviderAfterApply = RedactApiKey(LocalAiGatewayProviderDefinition.BuildProviderJson(install)), + PrimaryAfterApply = managedPrimary, + }; - var result = await coordinator.PublishAsync(install); + var result = await CreateCoordinator(commands).PublishAsync(install); Assert.True(result.Success, result.Detail); - Assert.Equal("llama-local", JsonNode.Parse(commands.ProviderJson!)!["apiKey"]!.GetValue()); - Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson(commands.ProviderJson!, install, "llama-local")); - Assert.Contains(commands.Calls, call => call.Contains("--expect-current-json")); - Assert.Contains(commands.Calls, call => call.Contains("gateway") && call.Contains("restart")); + int unset = commands.Calls.FindIndex(call => + call.Contains("unset") && call.Contains(LocalAiGatewayProviderDefinition.ProviderPath)); + int publish = commands.Calls.FindIndex(call => call.Contains("/bin/sh")); + int[] restarts = commands.Calls.Select((call, index) => (call, index)) + .Where(item => item.call.Contains("gateway") && item.call.Contains("restart")) + .Select(item => item.index).ToArray(); + Assert.InRange(unset, 0, publish - 1); + Assert.Equal(2, restarts.Length); + Assert.InRange(restarts[0], unset + 1, publish - 1); + Assert.True(restarts[1] > publish, "The Gateway must activate the republished Local AI route."); + Assert.DoesNotContain(commands.Calls, call => call.Contains(LocalAiGatewayProviderDefinition.PrimaryModelPath) && + !call.Contains("get")); + Assert.DoesNotContain(commands.Calls, call => call.Contains(LocalAiGatewayProviderDefinition.ProviderModelsPath)); + + // The fresh batch carries the canonical credential, never the redacted readback. + string script = commands.StandardInputs[publish]!; + const string prefix = "printf '%s' '"; + int start = script.IndexOf(prefix, StringComparison.Ordinal) + prefix.Length; + string batch = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String( + script[start..script.IndexOf('\'', start)])); + var written = JsonNode.Parse(batch)!.AsArray(); + JsonNode provider = written.Single(entry => + (string?)entry!["path"] == LocalAiGatewayProviderDefinition.ProviderPath)!["value"]!; + Assert.Equal("llama-local", (string?)provider["apiKey"]); + Assert.DoesNotContain(LocalAiGatewayProviderDefinition.CliRedactedApiKey, batch); + Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson(provider.ToJsonString(), install)); + Assert.NotNull(provider["models"]![0]!["compat"]!["supportedReasoningEfforts"]); + Assert.Equal(managedPrimary, commands.PrimaryModel); + Assert.Equal(commands.ProviderJson, commands.ActiveProviderJson); } [Fact] - public async Task WslPublish_GatewayThatRejectsMigrationKeepsLegacyRouteUnchanged() + public async Task WslPublish_LegacyWithdrawalFailureWritesNoNewProvider() { var install = Install(28_765); - var original = LegacyProviderJson(install); - var commands = new FakeWslCommandRunner(original, - LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)) { RejectModelsWrite = true }; + string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); + string legacy = LegacyProviderJson(install); + var commands = new FakeWslCommandRunner(legacy, managedPrimary) + { + IndeterminateRestartCalls = [1], + ProviderAfterApply = LocalAiGatewayProviderDefinition.BuildProviderJson(install), + PrimaryAfterApply = managedPrimary, + }; var result = await CreateCoordinator(commands).PublishAsync(install); Assert.False(result.Success); - Assert.Equal(original, commands.ProviderJson); - Assert.DoesNotContain(commands.Calls, call => call.Contains("--expect-current-json")); - Assert.DoesNotContain(commands.Calls, call => call.Contains("restart")); + Assert.DoesNotContain(commands.Calls, call => call.Contains("/bin/sh")); + Assert.Single(commands.Calls, call => call.Contains("gateway") && call.Contains("restart")); + Assert.Null(commands.ProviderJson); + Assert.Equal(legacy, commands.ActiveProviderJson); + Assert.Equal(managedPrimary, commands.PrimaryModel); } [Fact] - public async Task WslPublish_FinishesRestartAfterConfirmedWriteWhenCallerCancels() + public async Task WslPublish_CurrentRouteIsAcceptedWithoutChanges() { var install = Install(28_765); - using var cancellation = new CancellationTokenSource(); - FakeWslCommandRunner? observed = null; - var commands = new FakeWslCommandRunner(LegacyProviderJson(install), - LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)) - { - CommandObserved = _ => - { - if (observed!.Calls[^1].Contains("--expect-current-json")) - cancellation.Cancel(); - }, - }; - observed = commands; + var commands = new FakeWslCommandRunner(LocalAiGatewayProviderDefinition.BuildProviderJson(install, "llama-local"), + LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)); - var result = await CreateCoordinator(commands).PublishAsync(install, cancellation.Token); + var result = await CreateCoordinator(commands).PublishAsync(install); - Assert.True(cancellation.IsCancellationRequested); Assert.True(result.Success, result.Detail); - Assert.Single(commands.Calls, call => call.Contains("restart")); + Assert.DoesNotContain(commands.Calls, call => call.Contains("restart")); + Assert.DoesNotContain(commands.Calls, call => call.Contains("set") || call.Contains("unset")); } /// The exact provider shape published before effort metadata was added. @@ -1145,7 +1172,6 @@ private sealed class FakeWslCommandRunner(string? providerJson, string? primaryM public string? ActiveProviderJson { get; private set; } = providerJson; public string? PrimaryModel { get; private set; } = primaryModel; public string? ProviderAfterApply { get; init; } - public bool RejectModelsWrite { get; init; } public string? PrimaryAfterApply { get; init; } public bool FailReads { get; init; } public HashSet FailedReadCalls { get; init; } = []; @@ -1173,26 +1199,6 @@ public Task RunInDistroAsync( command.Contains(LocalAiGatewayProviderDefinition.ProviderPath); if (providerRead && (FailReads || FailedReadCalls.Contains(++_readCalls))) return Result(1, string.Empty, "wsl.exe failed"); - if (command.Contains("set") && command.Contains(LocalAiGatewayProviderDefinition.ProviderModelsPath)) - { - int guard = command.ToList().IndexOf("--expect-current-json"); - bool dryRun = command.Contains("--dry-run"); - // An older Gateway schema rejects the new fields at validation. - if (RejectModelsWrite || ProviderJson is null || dryRun && guard >= 0 || !dryRun && guard < 0) - return Result(1, string.Empty); - var provider = JsonNode.Parse(ProviderJson)!.AsObject(); - if (!dryRun) - { - using var current = JsonDocument.Parse(provider["models"]!.ToJsonString()); - using var expected = JsonDocument.Parse(command[guard + 1]); - if (!JsonElement.DeepEquals(current.RootElement, expected.RootElement)) - return Result(1, string.Empty); - int path = command.ToList().IndexOf(LocalAiGatewayProviderDefinition.ProviderModelsPath); - provider["models"] = JsonNode.Parse(command[path + 1]); - ProviderJson = provider.ToJsonString(); - } - return Result(0, string.Empty); - } if (command.Contains("/bin/sh")) { if (ProviderAfterApply is not null) From f096746cd8bfd9307d6f104e9783d4fab849631c Mon Sep 17 00:00:00 2001 From: Alberto Delgado <44581951+turutupa@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:51:42 -0800 Subject: [PATCH 3/5] test: cover a failed migration readback and the preset for models without effort metadata From an independent review of the previous head: - A native legacy migration whose write lands but whose readback does not match (here, a Gateway that drops thinkingLevelMap) now has a test. It reports the localized LocalAi_GatewaySavedRouteVerification failure, makes no second write, and keeps the upgraded route, its credential and the managed primary in place. - The Bonsai receipt test now asserts what removing the forced `reasoning = on` means for a model without a reasoning profile: the preset uses llama-server's auto mode, and the provider entry carries no effort vocabulary, so it is unchanged from the pre-upgrade shape and the Gateway never requests "none" for it. Co-Authored-By: Claude Opus 5.5 --- .../LocalAiPortLifecycleTests.cs | 11 +++++++ .../LocalAiGatewayProviderCoordinatorTests.cs | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs b/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs index 08df50f9a5..55e6964307 100644 --- a/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs +++ b/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs @@ -3628,6 +3628,17 @@ public async Task BonsaiReceipt_UsesPrismRuntimeAndBoundsOutputToSmallContext() Assert.DoesNotContain("spec-type", launch.PresetContent); using var provider = JsonDocument.Parse(LocalAiGatewayProviderDefinition.BuildProviderJson(install)); Assert.Equal(2048, provider.RootElement.GetProperty("models")[0].GetProperty("maxTokens").GetInt32()); + // A model without a reasoning profile gets no effort vocabulary, so the Gateway never asks it for "none". + // llama-server's auto mode then keeps a thinking template reasoning, as the old forced preset did, and + // the provider entry is unchanged from the pre-upgrade shape. + JsonElement bonsaiModel = provider.RootElement.GetProperty("models")[0]; + Assert.DoesNotContain("reasoning =", launch.PresetContent); + Assert.Contains("reasoning-format = deepseek", launch.PresetContent.Split(Environment.NewLine)); + Assert.False(bonsaiModel.GetProperty("compat").TryGetProperty("supportedReasoningEfforts", out _)); + Assert.False(bonsaiModel.GetProperty("compat").TryGetProperty("reasoningEffortMap", out _)); + Assert.False(bonsaiModel.TryGetProperty("thinkingLevelMap", out _)); + Assert.True(LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson( + provider.RootElement.GetRawText(), install)); // Even a structurally valid stock-runtime receipt cannot launch ternary weights. var stock = LlamaRuntimeCatalog.Find(System.Runtime.InteropServices.Architecture.X64)!; diff --git a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs index e7d439256a..9bf45c7a14 100644 --- a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs +++ b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs @@ -60,6 +60,29 @@ public async Task RpcPublish_MigratesOnlyLegacyModelArrayAndPreservesCredential( transport.Config["models"]!["providers"]!["llamacpp"]!.ToJsonString(), install, "actual-local-secret")); } + [Fact] + public async Task RpcPublish_LegacyMigrationThatFailsReadbackKeepsTheUpgradedRouteAndReportsLocalizedFailure() + { + var install = Install(28_765, "cloud/prior"); + var transport = new AtomicTransport("{}") { DropThinkingLevelMapOnApply = true }; + transport.SetManaged(install); + StripEffortMetadata((JsonObject)transport.Config["models"]!["providers"]!["llamacpp"]!["models"]![0]!); + var coordinator = new LocalAiGatewayProviderCoordinator( + transport, () => "actual-local-secret", NullLogger.Instance, key => $"localized:{key}"); + + var result = await coordinator.PublishAsync(install); + + // The write landed but its readback does not match: report it, never retry or withdraw the route. + Assert.False(result.Success); + Assert.Equal("localized:LocalAi_GatewaySavedRouteVerification", result.Detail); + Assert.Single(transport.Patches); + Assert.Equal([LocalAiGatewayProviderDefinition.ProviderModelsPath], Assert.Single(transport.ReplacementPaths)); + Assert.Equal("actual-local-secret", (string?)transport.Config["models"]?["providers"]?["llamacpp"]?["apiKey"]); + Assert.NotNull(transport.Config["models"]?["providers"]?["llamacpp"]?["models"]?[0]?["compat"]?["supportedReasoningEfforts"]); + Assert.Equal(LocalAiGatewayProviderDefinition.BuildPrimaryModel(install), + (string?)transport.Config["agents"]?["defaults"]?["model"]?["primary"]); + } + [Fact] public async Task WslPublish_WithdrawsLegacyProviderThenPublishesCurrentShape() { @@ -258,6 +281,7 @@ private sealed class AtomicTransport(string config) : ILocalAiGatewayAtomicConfi public bool DriftOnApply { get; init; } public bool FailRead { get; init; } public bool FailAfterApply { get; init; } + public bool DropThinkingLevelMapOnApply { get; init; } private int _version; public Task CaptureAsync(CancellationToken ct) @@ -288,6 +312,11 @@ public Task ApplyAsync(LocalAiGatewayConfigurationSnapshot expected, JsonElement throw new InvalidOperationException("Destructive model array change requires its exact replacePaths entry."); Merge(Config, JsonNode.Parse(patch.GetRawText())!.AsObject()); _version++; + if (DropThinkingLevelMapOnApply && + Config["models"]?["providers"]?["llamacpp"]?["models"] is JsonArray written) + { + foreach (var model in written.OfType()) model.Remove("thinkingLevelMap"); + } if (FailAfterApply) throw new IOException("UNAVAILABLE: configuration persisted but restart did not complete."); return Task.CompletedTask; } From 7bdbfe9d6c714cacbe9cf96b72a5e8c01e562c5c Mon Sep 17 00:00:00 2001 From: Alberto Delgado <44581951+turutupa@users.noreply.github.com> Date: Thu, 19 Nov 2026 14:49:49 -0800 Subject: [PATCH 4/5] fix: keep the legacy Local AI route on Gateways that reject effort metadata The companion admits any protocol-4 Gateway, but the effort metadata this PR publishes needs newer model schemas: supportsReasoningEffort arrived in 2026.1.14, supportedReasoningEfforts and reasoningEffortMap in 2026.4.26, and thinkingLevelMap only in 2026.6.5. Gateway 2026.6.1-2026.6.4 therefore rejects the new provider ("Unrecognized key"). PublishAsync withdrew a pre-effort WSL provider (config unset + Gateway restart) before the replacement was dry-run, and normal startup also withdraws on its endpoint cycle before publishing (native: config.patch, same order). On those Gateways the working route was removed, publication failed, and every retry generated the same rejected config. Decide the provider shape before anything is withdrawn or written: - WSL: dry-run the full batch (new ValidateBatchAsync, dry-run only); if it is rejected, dry-run the exact pre-effort batch. - Native: read-only config.schema check (SupportsEffortMetadataAsync); a Gateway that does not list config.schema is newer than 2026.6.5. - Full shape when accepted; otherwise the pre-effort shape, as on main, with one warning that Effort Off cannot disable reasoning on that Gateway. A pre-effort provider there counts as current: no withdraw/republish churn and no in-place migration. - If neither shape validates or the check fails, nothing is withdrawn and the operation fails with a localized message, keeping the current route. An endpoint-cycle quiesce decides the shape and the following publication reuses it; teardown needs no check. Publication, verification and native migration use the decided shape. Setup's WSL step already dry-runs before applying; it now falls back to the pre-effort batch when a dry-run confirms the Gateway rejects the full one, and its rollback recognizes that shape. Co-Authored-By: Claude Opus 5.5 --- .../LocalAiGatewayProviderDefinition.cs | 25 +- .../LocalAiGatewayConfiguration.cs | 38 ++- src/OpenClaw.Tray.WinUI/App.xaml.cs | 3 +- .../LocalAiGatewayConfigurationTransport.cs | 19 +- .../Services/LocalAiGatewayLifecycle.cs | 7 +- .../LocalAiGatewayProviderCoordinator.cs | 165 ++++++++++-- ...LocalAiGatewayRpcConfigurationTransport.cs | 19 ++ .../Services/WindowManager.cs | 3 +- .../Strings/en-us/Resources.resw | 2 + .../Strings/fr-fr/Resources.resw | 2 + .../Strings/nl-nl/Resources.resw | 2 + .../Strings/pt-br/Resources.resw | 2 + .../Strings/zh-cn/Resources.resw | 2 + .../Strings/zh-tw/Resources.resw | 2 + .../LocalAiGatewayUninstallTests.cs | 50 ++++ .../LocalAiGatewayProviderCoordinatorTests.cs | 234 +++++++++++++++++- ...AiGatewayRpcConfigurationTransportTests.cs | 37 +++ 17 files changed, 557 insertions(+), 55 deletions(-) diff --git a/src/OpenClaw.Connection/LocalAi/LocalAiGatewayProviderDefinition.cs b/src/OpenClaw.Connection/LocalAi/LocalAiGatewayProviderDefinition.cs index 81047a8164..47def99632 100644 --- a/src/OpenClaw.Connection/LocalAi/LocalAiGatewayProviderDefinition.cs +++ b/src/OpenClaw.Connection/LocalAi/LocalAiGatewayProviderDefinition.cs @@ -26,6 +26,13 @@ public static class LocalAiGatewayProviderDefinition public const int ProviderTimeoutSeconds = 300; public const int MaximumOutputTokens = 8_192; + /// + /// Model keys the effort metadata needs. A protocol-4 Gateway can predate + /// them (thinkingLevelMap arrived in 2026.6.5) and rejects them as unknown. + /// + public static readonly IReadOnlyList EffortMetadataKeys = + ["thinkingLevelMap", "supportedReasoningEfforts", "reasoningEffortMap"]; + /// Caps generated Gateway output tokens to one quarter of the selected context window. public static int GetMaximumOutputTokens(int contextTokens) => Math.Min(MaximumOutputTokens, contextTokens / 4); @@ -57,7 +64,7 @@ public static string BuildProviderJsonForRollback(string priorProviderJson, Loca if (MatchesProviderJson(priorProviderJson, install)) return BuildProviderJson(install); if (MatchesLegacyProviderJson(priorProviderJson, install)) - return BuildProviderJson(install, "llama-local", legacy: true); + return BuildLegacyProviderJson(install); throw new InvalidDataException("The original Local AI provider is not an exact managed configuration."); } @@ -138,6 +145,13 @@ private static bool JsonEquals(JsonElement left, JsonElement right) public static string BuildProviderJson(LocalAiResolvedInstall install, string apiKey) => BuildProviderJson(install, apiKey, legacy: false); + /// + /// Builds the exact pre-effort provider for Gateways whose model schema + /// rejects effort metadata. Effort Off cannot disable reasoning there. + /// + public static string BuildLegacyProviderJson(LocalAiResolvedInstall install, string apiKey = "llama-local") => + BuildProviderJson(install, apiKey, legacy: true); + private static string BuildProviderJson(LocalAiResolvedInstall install, string apiKey, bool legacy) { ArgumentNullException.ThrowIfNull(install); @@ -251,10 +265,13 @@ public static bool TryReadPrimaryModelJson(string json, out string? model) } } - /// Builds an atomic Gateway batch that writes both provider definition and primary model. - public static string BuildProviderBatchJson(LocalAiResolvedInstall install) + /// + /// Builds an atomic Gateway batch that writes both provider definition and + /// primary model, in the pre-effort shape when is set. + /// + public static string BuildProviderBatchJson(LocalAiResolvedInstall install, bool legacy = false) { - using JsonDocument provider = JsonDocument.Parse(BuildProviderJson(install)); + using JsonDocument provider = JsonDocument.Parse(BuildProviderJson(install, "llama-local", legacy)); return JsonSerializer.Serialize(new[] { new { path = ProviderPath, value = (object)provider.RootElement.Clone() }, diff --git a/src/OpenClaw.SetupEngine/LocalAiGatewayConfiguration.cs b/src/OpenClaw.SetupEngine/LocalAiGatewayConfiguration.cs index 37df7e80f4..bbf975fdf8 100644 --- a/src/OpenClaw.SetupEngine/LocalAiGatewayConfiguration.cs +++ b/src/OpenClaw.SetupEngine/LocalAiGatewayConfiguration.cs @@ -16,15 +16,16 @@ internal static class LocalAiGatewayConfigBuilder internal const string ProviderPath = LocalAiGatewayProviderDefinition.ProviderPath; internal const string PrimaryModelPath = LocalAiGatewayProviderDefinition.PrimaryModelPath; - public static string BuildBatchJson(SetupContext context) + public static string BuildBatchJson(SetupContext context, bool legacy = false) { ArgumentNullException.ThrowIfNull(context); var install = context.LocalAiResolvedInstall ?? throw new InvalidOperationException("The Local AI install receipt is required."); _ = context.LocalAiEligibility?.Plan ?? throw new InvalidOperationException("The qualified Local AI plan is required."); - using JsonDocument provider = JsonDocument.Parse( - LocalAiGatewayProviderDefinition.BuildProviderJson(install)); + using JsonDocument provider = JsonDocument.Parse(legacy + ? LocalAiGatewayProviderDefinition.BuildLegacyProviderJson(install) + : LocalAiGatewayProviderDefinition.BuildProviderJson(install)); object[] operations = [ new { path = ProviderPath, value = (object)provider.RootElement.Clone() }, @@ -204,6 +205,17 @@ public override async Task ExecuteAsync(SetupContext ctx, Cancellati string batchJson = LocalAiGatewayConfigBuilder.BuildBatchJson(ctx); CommandResult result = await ApplyBatchAsync(ctx, batchJson, "LOCAL_AI_GATEWAY_CONFIGURED", ct); + if (result.ExitCode != 0 && !result.TimedOut && + await RejectsBatchAsync(ctx, batchJson, ct).ConfigureAwait(false)) + { + // Effort metadata needs Gateway 2026.6.5 or later; protocol-4 Gateways + // before it reject the model keys. The dry-run wrote nothing, so publish + // the exact pre-effort provider instead of failing setup. + ctx.Logger.Warn("The Gateway does not accept Local AI effort metadata (it needs OpenClaw Gateway 2026.6.5 or later). " + + "Publishing the earlier provider shape, so Effort Off cannot turn reasoning off on this Gateway."); + result = await ApplyBatchAsync( + ctx, LocalAiGatewayConfigBuilder.BuildBatchJson(ctx, legacy: true), "LOCAL_AI_GATEWAY_CONFIGURED", ct); + } if (result.ExitCode != 0 || result.TimedOut || !result.Stdout.Contains("LOCAL_AI_GATEWAY_CONFIGURED", StringComparison.Ordinal)) { @@ -268,8 +280,9 @@ public override async Task RollbackAsync(SetupContext ctx, CancellationToken ct) } string expectedPrimary = JsonSerializer.Serialize(LocalAiGatewayConfigBuilder.ExpectedPrimaryModel(ctx)); + // Setup writes the pre-effort shape to Gateways that reject effort metadata. if (!current.ProviderExisted || !current.PrimaryModelExisted || - !LocalAiGatewayProviderDefinition.MatchesProviderJson( + !LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson( current.ProviderJson!, ctx.LocalAiResolvedInstall!) || !JsonEquals(current.PrimaryModelJson!, expectedPrimary)) @@ -384,7 +397,7 @@ private static async Task ReconcileFailedRecoveryRestoreAsync( observed.ProviderExisted && observed.PrimaryModelExisted && ctx.LocalAiResolvedInstall is { } replacement && - LocalAiGatewayProviderDefinition.MatchesProviderJson( + LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson( observed.ProviderJson!, replacement) && JsonEquals( @@ -538,16 +551,27 @@ private static Task CaptureStateAsync(SetupContext ctx, Cancellat user: ctx.Config.Wsl.User, inputViaStdin: true); } + /// True only when a dry-run of the batch completed and the Gateway rejected it. + private static async Task RejectsBatchAsync(SetupContext ctx, string batchJson, CancellationToken ct) + { + CommandResult validated = await ApplyBatchAsync( + ctx, batchJson, "LOCAL_AI_GATEWAY_VALIDATED", ct, apply: false).ConfigureAwait(false); + return validated.ExitCode != 0 && !validated.TimedOut; + } + private static Task ApplyBatchAsync( SetupContext ctx, string batchJson, string successMarker, - CancellationToken ct) + CancellationToken ct, + bool apply = true) { var environment = new Dictionary { [BatchVariable] = Convert.ToBase64String(Encoding.UTF8.GetBytes(batchJson)), }; + // A validation-only run stops after the dry-run (":" is the shell no-op). + string write = apply ? "openclaw config set --batch-file \"$batch_file\"" : ":"; string script = $$""" set -e {{ctx.WslPathPrefix}} @@ -555,7 +579,7 @@ private static Task ApplyBatchAsync( trap 'rm -f "$batch_file"' EXIT printf '%s' "$OPENCLAW_LOCAL_AI_BATCH_B64" | base64 -d > "$batch_file" openclaw config set --batch-file "$batch_file" --dry-run - openclaw config set --batch-file "$batch_file" + {{write}} echo {{successMarker}} """; return ctx.Commands.RunInWslAsync( diff --git a/src/OpenClaw.Tray.WinUI/App.xaml.cs b/src/OpenClaw.Tray.WinUI/App.xaml.cs index c9ea547687..dde357a236 100644 --- a/src/OpenClaw.Tray.WinUI/App.xaml.cs +++ b/src/OpenClaw.Tray.WinUI/App.xaml.cs @@ -925,7 +925,8 @@ _dispatcherQueue is null // Model recovery also covers the local runtime's health check. new WslExeCommandRunner(localAiLogger, TimeSpan.FromSeconds(420)), new LocalAiGatewayDistroResolver(_gatewayRegistry), - localAiLogger); + localAiLogger, + LocalizationHelper.GetString); _localAiGatewayLifecycle = new LocalAiGatewayLifecycle(localAiPaths, SettingsManager.SettingsDirectoryPath, () => _gatewayRegistry, () => _connectionManager, localAiEndpointLifecycle, localAiLogger, LocalizationHelper.GetString); diff --git a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayConfigurationTransport.cs b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayConfigurationTransport.cs index b0750ea4bc..bd7ed8d03c 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayConfigurationTransport.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayConfigurationTransport.cs @@ -20,6 +20,10 @@ // LocalAiGatewayCommandOutcome applied = await transport.ApplyBatchAsync( // batchJson, cancellationToken); // +// // Schema check only: the same dry-run, with no write. +// LocalAiGatewayCommandOutcome accepted = await transport.ValidateBatchAsync( +// batchJson, cancellationToken); +// // Routed is false only when the pinned distro is missing, ambiguous, or // drifted, so the command never started and Detail holds the resolver's // reason. Routed true means the CLI ran; Result carries ExitCode, @@ -56,6 +60,9 @@ Task RunAsync( IReadOnlyList arguments, CancellationToken cancellationToken); Task ApplyBatchAsync( string batch, CancellationToken cancellationToken); + /// Validates a batch against the installed Gateway schema without writing it. + Task ValidateBatchAsync( + string batch, CancellationToken cancellationToken); } internal sealed class WslLocalAiGatewayConfigurationTransport( @@ -78,7 +85,15 @@ public Task RunAsync( } public Task ApplyBatchAsync( - string batch, CancellationToken cancellationToken) + string batch, CancellationToken cancellationToken) => + RunBatchAsync(batch, apply: true, cancellationToken); + + public Task ValidateBatchAsync( + string batch, CancellationToken cancellationToken) => + RunBatchAsync(batch, apply: false, cancellationToken); + + private Task RunBatchAsync( + string batch, bool apply, CancellationToken cancellationToken) { string encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(batch)); // stdin prevents wsl.exe from expanding the Linux temporary-file variable. @@ -88,7 +103,7 @@ public Task ApplyBatchAsync( "trap 'rm -f \"$batch_file\"' EXIT\n" + $"printf '%s' '{encoded}' | base64 -d > \"$batch_file\"\n" + "openclaw config set --batch-file \"$batch_file\" --dry-run\n" + - "openclaw config set --batch-file \"$batch_file\"\n"; + (apply ? "openclaw config set --batch-file \"$batch_file\"\n" : string.Empty); return RunInManagedDistroAsync( ["/usr/bin/env", $"PATH={FixedPath}", "/bin/sh", "-s"], cancellationToken, script); diff --git a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayLifecycle.cs b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayLifecycle.cs index c5a4447e3a..70ca289204 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayLifecycle.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayLifecycle.cs @@ -750,8 +750,8 @@ private async Task RunNativeAsync( throw new InvalidOperationException("The recovered endpoint could not be verified through the original Gateway."); // Inference proves the old route belongs to this listener. Only an // exact pre-effort provider continues through the guarded - // coordinator to receive its effort metadata; any other verified - // route is already published. + // coordinator to receive its effort metadata when the Gateway + // accepts it; any other verified route is already published. binding = binding with { Pending = false, ConfigHash = after.Hash }; if (provider is null || !LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson( provider.ToJsonString(), install, _credentials.GetOrCreate())) @@ -886,6 +886,9 @@ public async Task CaptureAsync(Cancellation return snapshot; } + public Task SupportsEffortMetadataAsync(CancellationToken ct) => + rpc.SupportsEffortMetadataAsync(ct); + public async Task ApplyAsync(LocalAiGatewayConfigurationSnapshot expected, JsonElement patch, CancellationToken ct, IReadOnlyList? replacePaths = null) { diff --git a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs index bddfba7309..9035ed5621 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs @@ -21,22 +21,29 @@ internal sealed class LocalAiGatewayProviderCoordinator : ILocalAiEndpointLifecy private readonly IOpenClawLogger _logger; private readonly Func? _getString; + // The provider shape the installed Gateway accepts, decided before an + // endpoint-cycle withdrawal and consumed by the publication that follows. + private ProviderShape? _decidedShape; + public LocalAiGatewayProviderCoordinator( IWslCommandRunner commands, ILocalAiGatewayDistroResolver distroResolver, - IOpenClawLogger logger) + IOpenClawLogger logger, + Func? getString = null) : this(new WslLocalAiGatewayConfigurationTransport( commands ?? throw new ArgumentNullException(nameof(commands)), - distroResolver ?? throw new ArgumentNullException(nameof(distroResolver))), logger) + distroResolver ?? throw new ArgumentNullException(nameof(distroResolver))), logger, getString) { } public LocalAiGatewayProviderCoordinator( ILocalAiGatewayConfigurationTransport configuration, - IOpenClawLogger logger) + IOpenClawLogger logger, + Func? getString = null) { _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _getString = getString; } public LocalAiGatewayProviderCoordinator( @@ -51,12 +58,22 @@ public LocalAiGatewayProviderCoordinator( _getString = getString; } - public async Task QuiesceAsync( + public Task QuiesceAsync( LocalAiResolvedInstall install, LocalAiQuiesceReason reason = LocalAiQuiesceReason.Teardown, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(install); + // Every external withdrawal starts a fresh decision; the Gateway may have changed. + _decidedShape = null; + return QuiesceCoreAsync(install, reason, cancellationToken); + } + + private async Task QuiesceCoreAsync( + LocalAiResolvedInstall install, + LocalAiQuiesceReason reason, + CancellationToken cancellationToken) + { GatewayCapture current = await CaptureGatewayAsync(cancellationToken).ConfigureAwait(false); if (!current.Success) return Failed(current.Detail ?? "The managed Local AI gateway route could not be inspected."); @@ -69,9 +86,9 @@ public async Task QuiesceAsync( install.Manifest.GatewayFallbackModel); if (current.ProviderExists) { - _ = BuildProvider(install); - if (!MatchesProvider(current.ProviderJson!, install) && - !MatchesLegacyProvider(current.ProviderJson!, install)) + _ = BuildProvider(install, ProviderShape.Current); + if (!MatchesProvider(current.ProviderJson!, install, ProviderShape.Current) && + !MatchesProvider(current.ProviderJson!, install, ProviderShape.Legacy)) { return Failed("The llamacpp provider was changed outside the companion; preserving it and refusing to cycle the managed endpoint."); } @@ -93,6 +110,15 @@ public async Task QuiesceAsync( string? expectedPrimary = current.PrimaryModel; bool retainManagedPrimary = reason == LocalAiQuiesceReason.EndpointCycle; + if (retainManagedPrimary && current.ProviderExists) + { + // The route is republished right after an endpoint cycle. Withdraw + // it only once the installed Gateway is known to accept a replacement. + LocalAiEndpointLifecycleResult decided = await EnsureShapeAsync(install, cancellationToken) + .ConfigureAwait(false); + if (!decided.Success) + return decided; + } if (_atomicConfiguration is not null) { if (primaryIsManaged && !retainManagedPrimary) @@ -166,10 +192,23 @@ public async Task PublishAsync( CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(install); - string batch; try { - batch = LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install); + return await PublishCoreAsync(install, cancellationToken).ConfigureAwait(false); + } + finally + { + _decidedShape = null; + } + } + + private async Task PublishCoreAsync( + LocalAiResolvedInstall install, + CancellationToken cancellationToken) + { + try + { + _ = LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install); } catch (Exception ex) when (ex is InvalidDataException or InvalidOperationException) { @@ -180,10 +219,19 @@ public async Task PublishAsync( LocalAiEndpointLifecycleResult admission = CheckPublication(current, install); if (!admission.Success) return admission; + // Admission accepted the current shape or the exact pre-effort shape. + if (current.ProviderExists && MatchesProvider(current.ProviderJson!, install, ProviderShape.Current)) + return admission; + // Decide what the installed Gateway accepts before anything is written or withdrawn. + LocalAiEndpointLifecycleResult decided = await EnsureShapeAsync(install, cancellationToken) + .ConfigureAwait(false); + if (!decided.Success) + return decided; + ProviderShape shape = _decidedShape!.Value; if (current.ProviderExists) { - // Admission accepted the current shape or the exact pre-effort shape. - if (MatchesProvider(current.ProviderJson!, install)) + // A Gateway that rejects effort metadata keeps its pre-effort route as is. + if (shape == ProviderShape.Legacy) return admission; // Native endpoint recovery keeps a verified live route, so only the // owned model array of a pre-effort provider is upgraded in place. @@ -191,7 +239,7 @@ public async Task PublishAsync( return await MigrateLegacyProviderAsync(current, install, cancellationToken).ConfigureAwait(false); // WSL always quiesces before publishing, so a pre-effort provider is // withdrawn like an endpoint cycle and published from scratch. - LocalAiEndpointLifecycleResult withdrawn = await QuiesceAsync( + LocalAiEndpointLifecycleResult withdrawn = await QuiesceCoreAsync( install, LocalAiQuiesceReason.EndpointCycle, cancellationToken).ConfigureAwait(false); if (!withdrawn.Success) return withdrawn; @@ -222,14 +270,16 @@ public async Task PublishAsync( var patch = new JsonObject { ["models"] = new JsonObject { ["providers"] = new JsonObject - { ["llamacpp"] = JsonNode.Parse(BuildProvider(install)) } }, + { ["llamacpp"] = JsonNode.Parse(BuildProvider(install, shape)) } }, ["agents"] = new JsonObject { ["defaults"] = defaults }, }; var result = await ApplyAtomicAsync(current, patch, cancellationToken).ConfigureAwait(false); if (!result.Success) return result; - return await VerifyPublishedAsync(install, CancellationToken.None).ConfigureAwait(false); + return await VerifyPublishedAsync(install, shape, CancellationToken.None).ConfigureAwait(false); } + string batch = LocalAiGatewayProviderDefinition.BuildProviderBatchJson( + install, legacy: shape == ProviderShape.Legacy); LocalAiGatewayCommandOutcome applied = await _configuration!.ApplyBatchAsync(batch, cancellationToken).ConfigureAwait(false); if (!applied.Routed) return Failed(applied.Detail!); @@ -253,16 +303,16 @@ public async Task PublishAsync( return Failed("The Local AI route was saved, but the app-owned Gateway did not restart. Check the WSL Gateway before retrying Local AI."); } - return await VerifyPublishedAsync(install, cancellationToken).ConfigureAwait(false); + return await VerifyPublishedAsync(install, shape, cancellationToken).ConfigureAwait(false); } private async Task VerifyPublishedAsync( - LocalAiResolvedInstall install, CancellationToken cancellationToken) + LocalAiResolvedInstall install, ProviderShape shape, CancellationToken cancellationToken) { string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); GatewayCapture verified = await CaptureGatewayAsync(cancellationToken).ConfigureAwait(false); if (!verified.Success || !verified.ProviderExists || - !MatchesProvider(verified.ProviderJson!, install) || + !MatchesProvider(verified.ProviderJson!, install, shape) || !verified.PrimaryExists || !string.Equals(verified.PrimaryModel, managedPrimary, StringComparison.Ordinal)) { @@ -288,8 +338,8 @@ private LocalAiEndpointLifecycleResult CheckPublication(GatewayCapture current, string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); if (current.ProviderExists) { - return (MatchesProvider(current.ProviderJson!, install) || - MatchesLegacyProvider(current.ProviderJson!, install)) && + return (MatchesProvider(current.ProviderJson!, install, ProviderShape.Current) || + MatchesProvider(current.ProviderJson!, install, ProviderShape.Legacy)) && current.PrimaryExists && string.Equals(current.PrimaryModel, managedPrimary, StringComparison.Ordinal) ? LocalAiEndpointLifecycleResult.Ok() @@ -334,7 +384,7 @@ private async Task VerifyMigratedProviderAsync(L // caller's token, and never tear down the route that was just upgraded. GatewayCapture verified = await CaptureGatewayAsync(CancellationToken.None).ConfigureAwait(false); return verified.Success && verified.ProviderExists && - MatchesProvider(verified.ProviderJson!, install) && + MatchesProvider(verified.ProviderJson!, install, ProviderShape.Current) && verified.PrimaryExists && string.Equals(verified.PrimaryModel, LocalAiGatewayProviderDefinition.BuildPrimaryModel(install), StringComparison.Ordinal) @@ -476,15 +526,73 @@ private LocalAiEndpointLifecycleResult Failed(string detail) return LocalAiEndpointLifecycleResult.Failed(detail); } - private string BuildProvider(LocalAiResolvedInstall install) => _getApiKey is null - ? LocalAiGatewayProviderDefinition.BuildProviderJson(install) - : LocalAiGatewayProviderDefinition.BuildProviderJson(install, _getApiKey()); + private string BuildProvider(LocalAiResolvedInstall install, ProviderShape shape) + { + string apiKey = _getApiKey?.Invoke() ?? "llama-local"; + return shape == ProviderShape.Legacy + ? LocalAiGatewayProviderDefinition.BuildLegacyProviderJson(install, apiKey) + : LocalAiGatewayProviderDefinition.BuildProviderJson(install, apiKey); + } + + private bool MatchesProvider(string json, LocalAiResolvedInstall install, ProviderShape shape) => + shape == ProviderShape.Legacy + ? LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(json, install, _getApiKey?.Invoke()) + : LocalAiGatewayProviderDefinition.MatchesProviderJson(json, install, _getApiKey?.Invoke()); - private bool MatchesProvider(string json, LocalAiResolvedInstall install) => - LocalAiGatewayProviderDefinition.MatchesProviderJson(json, install, _getApiKey?.Invoke()); + private async Task EnsureShapeAsync( + LocalAiResolvedInstall install, CancellationToken ct) + { + if (_decidedShape is not null) + return LocalAiEndpointLifecycleResult.Ok(); + bool effortMetadata; + if (_atomicConfiguration is not null) + { + try + { + effortMetadata = await _atomicConfiguration.SupportsEffortMetadataAsync(ct).ConfigureAwait(false); + } + catch (OperationCanceledException) when (ct.IsCancellationRequested) { throw; } + catch (Exception) + { + // RPC errors can include authored config. Never log their payload. + return Failed(ShapeCheckFailed()); + } + } + else + { + // A dry-run validates against the installed schema without writing. + LocalAiGatewayCommandOutcome full = await _configuration!.ValidateBatchAsync( + LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install), ct).ConfigureAwait(false); + if (!full.Routed) + return Failed(full.Detail!); + if (full.Result!.TimedOut || full.Result.OutcomeIndeterminate) + return Failed(ShapeCheckFailed()); + effortMetadata = full.Result.Success; + if (!effortMetadata) + { + LocalAiGatewayCommandOutcome legacy = await _configuration.ValidateBatchAsync( + LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install, legacy: true), ct) + .ConfigureAwait(false); + if (!legacy.Routed) + return Failed(legacy.Detail!); + if (legacy.Result!.TimedOut || legacy.Result.OutcomeIndeterminate) + return Failed(ShapeCheckFailed()); + if (!legacy.Result.Success) + return Failed(Localized("LocalAi_GatewayProviderRejected", + "The Gateway rejected the Local AI provider configuration. The current Local AI route was kept; update the OpenClaw Gateway and retry.")); + } + } + if (!effortMetadata) + { + _logger.Warn("The Gateway does not accept Local AI effort metadata (it needs OpenClaw Gateway 2026.6.5 or later). " + + "Publishing the earlier provider shape, so Effort Off cannot turn reasoning off on this Gateway."); + } + _decidedShape = effortMetadata ? ProviderShape.Current : ProviderShape.Legacy; + return LocalAiEndpointLifecycleResult.Ok(); + } - private bool MatchesLegacyProvider(string json, LocalAiResolvedInstall install) => - LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(json, install, _getApiKey?.Invoke()); + private string ShapeCheckFailed() => Localized("LocalAi_GatewayProviderCheckFailed", + "The Gateway could not confirm which Local AI configuration it accepts. The current Local AI route was kept; reconnect the Gateway and retry."); private async Task CaptureAtomicAsync(CancellationToken ct) { @@ -545,6 +653,9 @@ private static bool TryGetPath(JsonElement root, string[] path, out JsonElement return true; } + /// Current carries effort metadata; Legacy is the exact pre-effort provider. + private enum ProviderShape { Current, Legacy } + private sealed record SettingCapture(bool Success, bool Exists, string? Json, string? Detail); private sealed record GatewayCapture( bool Success, diff --git a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayRpcConfigurationTransport.cs b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayRpcConfigurationTransport.cs index 70c34678c0..1bd2ce2964 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayRpcConfigurationTransport.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayRpcConfigurationTransport.cs @@ -13,6 +13,8 @@ internal interface ILocalAiGatewayAtomicConfigurationTransport Task CaptureAsync(CancellationToken ct); Task ApplyAsync(LocalAiGatewayConfigurationSnapshot expected, JsonElement patch, CancellationToken ct, IReadOnlyList? replacePaths = null); + /// Reports, without writing, whether the Gateway model schema accepts effort metadata. + Task SupportsEffortMetadataAsync(CancellationToken ct); } /// @@ -86,6 +88,23 @@ public async Task ApplyAsync(LocalAiGatewayConfigurationSnapshot expected, JsonE RequireOwner(); } + public async Task SupportsEffortMetadataAsync(CancellationToken ct) + { + RequireOwner(); + // config.patch has no dry-run. Every protocol-4 Gateway that predates + // the effort keys serves config.schema, so one that does not list it is newer. + if (!_transport.Methods.Contains("config.schema", StringComparer.Ordinal)) + return true; + var response = await _transport.RequestAsync("config.schema", new { }, 15_000, ct).ConfigureAwait(false); + RequireOwner(); + if (response.ValueKind != JsonValueKind.Object) + throw new InvalidDataException("The Gateway did not return a configuration schema."); + // Never log the schema: only whether it names every effort key. + string schema = response.GetRawText(); + return LocalAiGatewayProviderDefinition.EffortMetadataKeys.All( + key => schema.Contains(key, StringComparison.Ordinal)); + } + private void RequireOwner() { if (!_transport.IsConnected || _transport.Route != _owner || _transport.Generation != _generation) diff --git a/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs b/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs index 64f01148f6..a155a82259 100644 --- a/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs +++ b/src/OpenClaw.Tray.WinUI/Services/WindowManager.cs @@ -728,7 +728,8 @@ private async Task ResolveLocalAiSetupRouteAsync() LocalAiInstallationObservation.InspectAsync, ct => Task.Run(() => new OpenClaw.Shared.Inference.CudaHostHardwareProbe().Probe(), ct).WaitAsync(ct), () => new LocalAiGatewayProviderCoordinator(new WslExeCommandRunner(new AppLogger()), - new LocalAiGatewayDistroResolver(_callbacks.GetGatewayRegistry()), new AppLogger()), + new LocalAiGatewayDistroResolver(_callbacks.GetGatewayRegistry()), new AppLogger(), + LocalizationHelper.GetString), ReconcileSetupConnectionAsync, NotifySetupRegistryRecovery, _callbacks.GetLocalAiGatewayLifecycle?.Invoke()); diff --git a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw index b9cc4595a5..f6c4d9f7be 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw @@ -10179,4 +10179,6 @@ After enrolling, return to Settings > Windows Update and check for updates. I Widgets and tabs this session published. No panels are available for this conversation yet. The saved Local AI route could not be verified. Preserve the endpoint and reconcile the original Gateway. + The Gateway rejected the Local AI provider configuration. The current Local AI route was kept; update the OpenClaw Gateway and retry. + The Gateway could not confirm which Local AI configuration it accepts. The current Local AI route was kept; reconnect the Gateway and retry. diff --git a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw index e130c4597d..b508b256d1 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw @@ -10108,4 +10108,6 @@ Après votre inscription, revenez dans Paramètres > Windows Update et recher Widgets et onglets publiés par cette session. Aucun panneau n'est encore disponible pour cette conversation. La route d’IA locale enregistrée n’a pas pu être vérifiée. Conservez le point de terminaison et réconciliez le Gateway d’origine. + Le Gateway a refusé la configuration du fournisseur d’IA locale. La route d’IA locale actuelle a été conservée ; mettez à jour le Gateway OpenClaw et réessayez. + Le Gateway n’a pas pu confirmer quelle configuration d’IA locale il accepte. La route d’IA locale actuelle a été conservée ; reconnectez le Gateway et réessayez. diff --git a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw index 0c61055ff0..77005a1743 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw @@ -10109,4 +10109,6 @@ Ga na inschrijving terug naar Instellingen > Windows Update en controleer op Widgets en tabbladen die deze sessie heeft gepubliceerd. Er zijn nog geen panelen beschikbaar voor dit gesprek. De opgeslagen route voor Lokale AI kon niet worden geverifieerd. Behoud het eindpunt en herstel de oorspronkelijke Gateway. + De Gateway heeft de providerconfiguratie voor Lokale AI geweigerd. De huidige route voor Lokale AI is behouden; werk de OpenClaw Gateway bij en probeer het opnieuw. + De Gateway kon niet bevestigen welke configuratie voor Lokale AI hij accepteert. De huidige route voor Lokale AI is behouden; maak opnieuw verbinding met de Gateway en probeer het opnieuw. diff --git a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw index 440fc7e96f..065a92998a 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw @@ -10161,4 +10161,6 @@ Após a inscrição, volte para Configurações > Windows Update e verifique Widgets e guias publicados por esta sessão. Ainda não há painéis disponíveis para esta conversa. Não foi possível verificar a rota de IA local salva. Preserve o endpoint e reconcilie o Gateway original. + O Gateway rejeitou a configuração do provedor de IA local. A rota de IA local atual foi mantida; atualize o OpenClaw Gateway e tente novamente. + O Gateway não conseguiu confirmar qual configuração de IA local aceita. A rota de IA local atual foi mantida; reconecte o Gateway e tente novamente. diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw index 87d626bc91..30feeb0343 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw @@ -10108,4 +10108,6 @@ 此会话发布的小组件和选项卡。 此对话暂无可用的面板。 无法验证已保存的本地 AI 路由。请保留端点并协调原 Gateway 的配置。 + Gateway 拒绝了本地 AI 提供程序配置。已保留当前的本地 AI 路由;请更新 OpenClaw Gateway 后重试。 + Gateway 无法确认它接受哪种本地 AI 配置。已保留当前的本地 AI 路由;请重新连接 Gateway 后重试。 diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw index 8472bc4f58..7658863b8d 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw @@ -10108,4 +10108,6 @@ 此工作階段發布的小工具和索引標籤。 此對話尚無可用的面板。 無法驗證已儲存的本機 AI 路由。請保留端點並協調原始 Gateway 的設定。 + Gateway 拒絕了本機 AI 提供者設定。已保留目前的本機 AI 路由;請更新 OpenClaw Gateway 後再試一次。 + Gateway 無法確認它接受哪種本機 AI 設定。已保留目前的本機 AI 路由;請重新連線 Gateway 後再試一次。 diff --git a/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs b/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs index 3a622ee179..55cda117bd 100644 --- a/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs +++ b/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs @@ -133,6 +133,7 @@ public async Task Repair_RollbackRestoresFallbackAfterRetainedEndpointCycle() Assert.Equal(StepOutcome.Success, result.Outcome); Assert.Equal(LocalAiGatewayProviderDefinition.BuildProviderJson(install), commands.ProviderJson); Assert.Equal(managedPrimary, commands.PrimaryJson); + Assert.Empty(commands.ValidatedBatches); LocalAiResolvedInstall repaired = (await new LocalAiManifestStore(new LocalAiPaths(temp.Path)).LoadAsync())!; Assert.Equal("openai/gpt-5", repaired.Manifest.GatewayFallbackModel); @@ -163,6 +164,37 @@ public async Task Repair_RollbackUnsetsPrimaryAfterRetainedEndpointCycleWithoutF Assert.Null(commands.PrimaryJson); } + [Fact] + public async Task Configure_GatewayRejectingEffortMetadataPublishesAndRollsBackLegacyShape() + { + using var temp = new TempDirectory("local-ai-gateway-legacy-"); + LocalAiResolvedInstall install = await SaveManifestAsync(temp.Path, "openai/gpt-5"); + string managedPrimary = JsonSerializer.Serialize( + LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)); + var commands = new GatewayStateCommandRunner(providerJson: null, managedPrimary) + { + RejectEffortMetadata = true, + }; + SetupContext context = CreateContext(temp.Path, commands); + context.LocalAiResolvedInstall = install; + context.LocalAiEligibility = LocalInferenceEligibility.Evaluate(CreateSparkHardware()); + var step = new ConfigureLocalAiGatewayStep(); + + StepResult result = await step.ExecuteAsync(context, CancellationToken.None); + + // Gateway 2026.6.1-2026.6.4 rejects thinkingLevelMap: setup keeps the pre-effort route. + Assert.Equal(StepOutcome.Success, result.Outcome); + Assert.Contains("thinkingLevelMap", Assert.Single(commands.ValidatedBatches), StringComparison.Ordinal); + Assert.True(LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(commands.ProviderJson!, install)); + Assert.False(LocalAiGatewayProviderDefinition.MatchesProviderJson(commands.ProviderJson!, install)); + Assert.Equal(managedPrimary, commands.PrimaryJson); + + await step.RollbackAsync(context, CancellationToken.None); + + Assert.Null(commands.ProviderJson); + Assert.Equal(JsonSerializer.Serialize("openai/gpt-5"), commands.PrimaryJson); + } + [Fact] public async Task FreshProcessUninstall_RemovesExactManagedProviderAndPrimary() { @@ -917,6 +949,9 @@ private sealed class GatewayStateCommandRunner( public bool FailRestoreBatchOnce { get; set; } public bool LoseRestoreAcknowledgementOnce { get; set; } public bool ThrowOnNextCapture { get; set; } + /// Models Gateway 2026.6.1-2026.6.4, whose model schema has no thinkingLevelMap. + public bool RejectEffortMetadata { get; init; } + public List ValidatedBatches { get; } = []; public List WslCalls { get; } = []; public Task RunAsync( @@ -966,6 +1001,21 @@ public Task RunInWslAsync( } string encoded = Assert.Single(environment).Value; string batch = Encoding.UTF8.GetString(Convert.FromBase64String(encoded)); + bool rejected = RejectEffortMetadata && + batch.Contains("thinkingLevelMap", StringComparison.Ordinal); + if (command.Contains("LOCAL_AI_GATEWAY_VALIDATED", StringComparison.Ordinal)) + { + ValidatedBatches.Add(batch); + return Task.FromResult(rejected + ? new CommandResult(1, "", "Unrecognized key: \"thinkingLevelMap\"", TimeSpan.Zero, TimedOut: false) + : new CommandResult(0, "LOCAL_AI_GATEWAY_VALIDATED", "", TimeSpan.Zero, TimedOut: false)); + } + if (rejected) + { + // The dry-run before the write fails, so nothing is written. + return Task.FromResult(new CommandResult( + 1, "", "Unrecognized key: \"thinkingLevelMap\"", TimeSpan.Zero, TimedOut: false)); + } using JsonDocument document = JsonDocument.Parse(batch); foreach (JsonElement operation in document.RootElement.EnumerateArray()) { diff --git a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs index 9bf45c7a14..75662305dc 100644 --- a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs +++ b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs @@ -99,12 +99,16 @@ public async Task WslPublish_WithdrawsLegacyProviderThenPublishesCurrentShape() var result = await CreateCoordinator(commands).PublishAsync(install); Assert.True(result.Success, result.Detail); + int validation = commands.Calls.FindIndex(call => call.Contains("/bin/sh")); int unset = commands.Calls.FindIndex(call => call.Contains("unset") && call.Contains(LocalAiGatewayProviderDefinition.ProviderPath)); - int publish = commands.Calls.FindIndex(call => call.Contains("/bin/sh")); + int publish = commands.FirstApplyIndex; int[] restarts = commands.Calls.Select((call, index) => (call, index)) .Where(item => item.call.Contains("gateway") && item.call.Contains("restart")) .Select(item => item.index).ToArray(); + // The Gateway accepted the replacement before the working route was withdrawn. + Assert.Contains("thinkingLevelMap", Assert.Single(commands.ValidatedBatches), StringComparison.Ordinal); + Assert.InRange(validation, 0, unset - 1); Assert.InRange(unset, 0, publish - 1); Assert.Equal(2, restarts.Length); Assert.InRange(restarts[0], unset + 1, publish - 1); @@ -114,11 +118,7 @@ public async Task WslPublish_WithdrawsLegacyProviderThenPublishesCurrentShape() Assert.DoesNotContain(commands.Calls, call => call.Contains(LocalAiGatewayProviderDefinition.ProviderModelsPath)); // The fresh batch carries the canonical credential, never the redacted readback. - string script = commands.StandardInputs[publish]!; - const string prefix = "printf '%s' '"; - int start = script.IndexOf(prefix, StringComparison.Ordinal) + prefix.Length; - string batch = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String( - script[start..script.IndexOf('\'', start)])); + string batch = DecodeBatch(commands.StandardInputs[publish]!); var written = JsonNode.Parse(batch)!.AsArray(); JsonNode provider = written.Single(entry => (string?)entry!["path"] == LocalAiGatewayProviderDefinition.ProviderPath)!["value"]!; @@ -146,7 +146,7 @@ public async Task WslPublish_LegacyWithdrawalFailureWritesNoNewProvider() var result = await CreateCoordinator(commands).PublishAsync(install); Assert.False(result.Success); - Assert.DoesNotContain(commands.Calls, call => call.Contains("/bin/sh")); + Assert.Equal(-1, commands.FirstApplyIndex); Assert.Single(commands.Calls, call => call.Contains("gateway") && call.Contains("restart")); Assert.Null(commands.ProviderJson); Assert.Equal(legacy, commands.ActiveProviderJson); @@ -167,6 +167,155 @@ public async Task WslPublish_CurrentRouteIsAcceptedWithoutChanges() Assert.DoesNotContain(commands.Calls, call => call.Contains("set") || call.Contains("unset")); } + [Fact] + public async Task WslPublish_GatewayRejectingEffortMetadataKeepsLegacyRouteUntouched() + { + var install = Install(28_765); + string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); + string legacy = RedactApiKey(LegacyProviderJson(install)); + var commands = new FakeWslCommandRunner(legacy, managedPrimary) { RejectEffortMetadata = true }; + + var result = await CreateCoordinator(commands).PublishAsync(install); + + // Gateway 2026.6.1-2026.6.4 admits protocol 4 but rejects thinkingLevelMap: + // the working pre-effort route is current there, so nothing is withdrawn. + Assert.True(result.Success, result.Detail); + Assert.Equal(2, commands.ValidatedBatches.Count); + Assert.Contains("thinkingLevelMap", commands.ValidatedBatches[0], StringComparison.Ordinal); + Assert.DoesNotContain("thinkingLevelMap", commands.ValidatedBatches[1], StringComparison.Ordinal); + Assert.DoesNotContain(commands.Calls, call => call.Contains("unset")); + Assert.DoesNotContain(commands.Calls, call => call.Contains("restart")); + Assert.Equal(-1, commands.FirstApplyIndex); + Assert.Equal(legacy, commands.ProviderJson); + Assert.Equal(legacy, commands.ActiveProviderJson); + Assert.Equal(managedPrimary, commands.PrimaryModel); + } + + [Fact] + public async Task WslEndpointCycle_GatewayRejectingEffortMetadataRepublishesLegacyRoute() + { + var install = Install(28_765); + var restarted = Install(28_766); + string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); + var commands = new FakeWslCommandRunner(RedactApiKey(LegacyProviderJson(install)), managedPrimary) + { + RejectEffortMetadata = true, + ApplyWritesBatch = true, + }; + var coordinator = CreateCoordinator(commands); + + // Normal runtime startup: withdraw the old endpoint, then publish the new one. + var withdrawn = await coordinator.QuiesceAsync(install, LocalAiQuiesceReason.EndpointCycle); + Assert.True(withdrawn.Success, withdrawn.Detail); + Assert.Null(commands.ProviderJson); + var published = await coordinator.PublishAsync(restarted); + + Assert.True(published.Success, published.Detail); + Assert.True(LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(commands.ProviderJson!, restarted)); + Assert.False(LocalAiGatewayProviderDefinition.MatchesProviderJson(commands.ProviderJson!, restarted)); + Assert.Equal(commands.ProviderJson, commands.ActiveProviderJson); + Assert.Equal(managedPrimary, commands.PrimaryModel); + // The withdrawal decided the shape; publication reuses it instead of probing again. + Assert.Equal(2, commands.ValidatedBatches.Count); + int unset = commands.Calls.FindIndex(call => call.Contains("unset")); + Assert.All(commands.ValidationCallIndexes, index => Assert.InRange(index, 0, unset - 1)); + } + + [Fact] + public async Task WslEndpointCycle_GatewayRejectingBothShapesNeverWithdrawsRoute() + { + var install = Install(28_765); + string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); + string legacy = LegacyProviderJson(install); + var commands = new FakeWslCommandRunner(legacy, managedPrimary) { RejectEveryProvider = true }; + var coordinator = new LocalAiGatewayProviderCoordinator(commands, + new LocalAiGatewayDistroResolver(CreateRegistry(ManagedRecord("managed", "OpenClawGateway"))), + NullLogger.Instance, key => $"localized:{key}"); + + var withdrawn = await coordinator.QuiesceAsync(install, LocalAiQuiesceReason.EndpointCycle); + var published = await coordinator.PublishAsync(install); + + Assert.False(withdrawn.Success); + Assert.Equal("localized:LocalAi_GatewayProviderRejected", withdrawn.Detail); + Assert.False(published.Success); + Assert.Equal("localized:LocalAi_GatewayProviderRejected", published.Detail); + Assert.Equal(4, commands.ValidatedBatches.Count); + Assert.DoesNotContain(commands.Calls, call => call.Contains("unset")); + Assert.DoesNotContain(commands.Calls, call => call.Contains("restart")); + Assert.Equal(-1, commands.FirstApplyIndex); + Assert.Equal(legacy, commands.ProviderJson); + Assert.Equal(legacy, commands.ActiveProviderJson); + Assert.Equal(managedPrimary, commands.PrimaryModel); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task RpcPublish_WritesTheShapeTheGatewaySchemaAccepts(bool supportsEffort) + { + var install = Install(28_765, "cloud/prior"); + var transport = new AtomicTransport("""{"agents":{"defaults":{"model":{"primary":"cloud/prior"}}}}""") + { + SupportsEffortMetadata = supportsEffort, + }; + var coordinator = new LocalAiGatewayProviderCoordinator(transport, () => "actual-local-secret", NullLogger.Instance); + + var result = await coordinator.PublishAsync(install); + + Assert.True(result.Success, result.Detail); + Assert.Equal(1, transport.SchemaChecks); + Assert.Single(transport.Patches); + string provider = transport.Config["models"]!["providers"]!["llamacpp"]!.ToJsonString(); + Assert.Equal(supportsEffort, + LocalAiGatewayProviderDefinition.MatchesProviderJson(provider, install, "actual-local-secret")); + Assert.Equal(!supportsEffort, + LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(provider, install, "actual-local-secret")); + Assert.Equal(supportsEffort, transport.Patches[0].GetRawText().Contains("thinkingLevelMap", StringComparison.Ordinal)); + } + + [Fact] + public async Task RpcPublish_GatewayWithoutEffortSchemaKeepsLegacyProviderWithoutMigration() + { + var install = Install(28_765, "cloud/prior"); + var transport = new AtomicTransport("{}") { SupportsEffortMetadata = false }; + transport.SetManaged(install); + StripEffortMetadata((JsonObject)transport.Config["models"]!["providers"]!["llamacpp"]!["models"]![0]!); + string legacy = transport.Config.ToJsonString(); + var coordinator = new LocalAiGatewayProviderCoordinator(transport, () => "actual-local-secret", NullLogger.Instance); + + var result = await coordinator.PublishAsync(install); + + Assert.True(result.Success, result.Detail); + Assert.Equal(1, transport.SchemaChecks); + Assert.Empty(transport.Patches); + Assert.Equal(legacy, transport.Config.ToJsonString()); + } + + [Fact] + public async Task RpcQuiesce_EndpointCycleKeepsRouteWhenSchemaCannotBeRead() + { + var install = Install(28_765, "cloud/prior"); + var transport = new AtomicTransport("{}") { FailSchema = true }; + transport.SetManaged(install); + string before = transport.Config.ToJsonString(); + var coordinator = new LocalAiGatewayProviderCoordinator( + transport, () => "actual-local-secret", NullLogger.Instance, key => $"localized:{key}"); + + var cycle = await coordinator.QuiesceAsync(install, LocalAiQuiesceReason.EndpointCycle); + + Assert.False(cycle.Success); + Assert.Equal("localized:LocalAi_GatewayProviderCheckFailed", cycle.Detail); + Assert.DoesNotContain("sensitive", cycle.Detail); + Assert.Empty(transport.Patches); + Assert.Equal(before, transport.Config.ToJsonString()); + + // Teardown publishes nothing afterwards, so it needs no schema decision. + var teardown = await coordinator.QuiesceAsync(install, LocalAiQuiesceReason.Teardown); + Assert.True(teardown.Success, teardown.Detail); + Assert.Equal(1, transport.SchemaChecks); + Assert.Null(transport.Config["models"]?["providers"]?["llamacpp"]); + } + /// The exact provider shape published before effort metadata was added. private static string LegacyProviderJson(LocalAiResolvedInstall install, string apiKey = "llama-local") { @@ -282,8 +431,20 @@ private sealed class AtomicTransport(string config) : ILocalAiGatewayAtomicConfi public bool FailRead { get; init; } public bool FailAfterApply { get; init; } public bool DropThinkingLevelMapOnApply { get; init; } + /// False models a config.schema without thinkingLevelMap (Gateway 2026.6.1-2026.6.4). + public bool SupportsEffortMetadata { get; init; } = true; + public bool FailSchema { get; init; } + public int SchemaChecks { get; private set; } private int _version; + public Task SupportsEffortMetadataAsync(CancellationToken ct) + { + ct.ThrowIfCancellationRequested(); + SchemaChecks++; + if (FailSchema) throw new IOException("sensitive RPC response"); + return Task.FromResult(SupportsEffortMetadata); + } + public Task CaptureAsync(CancellationToken ct) { ct.ThrowIfCancellationRequested(); @@ -393,6 +554,10 @@ public Task ApplyBatchAsync( Mutations++; return Task.FromResult(new LocalAiGatewayCommandOutcome(new(0, "", ""), null)); } + + public Task ValidateBatchAsync( + string batch, CancellationToken cancellationToken) => + Task.FromResult(new LocalAiGatewayCommandOutcome(new(0, "", "", timedOut, indeterminate), null)); } [Fact] @@ -759,12 +924,17 @@ public async Task Publish_UsesVerifiedEndpointAndNonDefaultManagedDistro() Assert.True(result.Success); Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson(commands.ProviderJson!, install)); Assert.Equal(LocalAiGatewayProviderDefinition.BuildPrimaryModel(install), commands.PrimaryModel); - IReadOnlyList apply = Assert.Single(commands.Calls, call => call.Contains("/bin/sh")); + // One dry-run-only schema check, then one dry-run-and-apply batch. + Assert.Equal(2, commands.Calls.Count(call => call.Contains("/bin/sh"))); + string validation = Assert.IsType(commands.StandardInputs[ + commands.Calls.FindIndex(call => call.Contains("/bin/sh"))]); + Assert.EndsWith("openclaw config set --batch-file \"$batch_file\" --dry-run\n", validation, StringComparison.Ordinal); + Assert.Contains("thinkingLevelMap", Assert.Single(commands.ValidatedBatches), StringComparison.Ordinal); + IReadOnlyList apply = commands.Calls[commands.FirstApplyIndex]; Assert.Equal("-s", apply[^1]); Assert.DoesNotContain("-c", apply); Assert.DoesNotContain(apply, argument => argument.Contains("base64", StringComparison.Ordinal)); - string script = Assert.IsType(commands.StandardInputs[ - commands.Calls.FindIndex(call => call.Contains("/bin/sh"))]); + string script = Assert.IsType(commands.StandardInputs[commands.FirstApplyIndex]); Assert.StartsWith("set -e\n", script, StringComparison.Ordinal); Assert.Contains("batch_file=\"$(mktemp)\"\n", script, StringComparison.Ordinal); Assert.Contains("trap 'rm -f \"$batch_file\"' EXIT\n", script, StringComparison.Ordinal); @@ -773,7 +943,7 @@ public async Task Publish_UsesVerifiedEndpointAndNonDefaultManagedDistro() "openclaw config set --batch-file \"$batch_file\" --dry-run\n" + "openclaw config set --batch-file \"$batch_file\"\n", script, StringComparison.Ordinal); - int publishIndex = commands.Calls.FindIndex(call => call.Contains("/bin/sh")); + int publishIndex = commands.FirstApplyIndex; int restartIndex = commands.Calls.FindIndex(call => call.Contains("gateway") && call.Contains("restart")); Assert.True(restartIndex > publishIndex, "The Gateway must activate the newly published Local AI route."); Assert.DoesNotContain("/dev/stdin", script, StringComparison.Ordinal); @@ -1190,6 +1360,14 @@ private static LocalAiResolvedInstall InstallWithoutEndpoint( }; } + private static string DecodeBatch(string script) + { + const string prefix = "printf '%s' '"; + int start = script.IndexOf(prefix, StringComparison.Ordinal) + prefix.Length; + return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String( + script[start..script.IndexOf('\'', start)])); + } + private static string RedactApiKey(string value) => value.Replace( "\"api\":\"openai-completions\",\"apiKey\":\"llama-local\"", $"\"apiKey\":\"{LocalAiGatewayProviderDefinition.CliRedactedApiKey}\",\"api\":\"openai-completions\"", @@ -1205,13 +1383,24 @@ private sealed class FakeWslCommandRunner(string? providerJson, string? primaryM public bool FailReads { get; init; } public HashSet FailedReadCalls { get; init; } = []; public HashSet IndeterminateRestartCalls { get; init; } = []; + /// Models Gateway 2026.6.1-2026.6.4: protocol 4, but no thinkingLevelMap in its model schema. + public bool RejectEffortMetadata { get; init; } + public bool RejectEveryProvider { get; init; } + /// Persists the provider and primary from an applied batch instead of the preset values. + public bool ApplyWritesBatch { get; init; } public Action? CommandObserved { get; init; } public List> Calls { get; } = []; public List StandardInputs { get; } = []; public List Distros { get; } = []; + public List ValidatedBatches { get; } = []; + public List ValidationCallIndexes { get; } = []; + public int FirstApplyIndex => StandardInputs.FindIndex(IsApplyScript); private int _readCalls; private int _restartCalls; + private static bool IsApplyScript(string? script) => + script?.Contains("--batch-file \"$batch_file\"\n", StringComparison.Ordinal) == true; + public Task RunInDistroAsync( string name, IReadOnlyList command, @@ -1230,6 +1419,29 @@ public Task RunInDistroAsync( return Result(1, string.Empty, "wsl.exe failed"); if (command.Contains("/bin/sh")) { + string batch = DecodeBatch(standardInput!); + bool rejected = batch.Contains(LocalAiGatewayProviderDefinition.ProviderPath, StringComparison.Ordinal) && + (RejectEveryProvider || + RejectEffortMetadata && batch.Contains("thinkingLevelMap", StringComparison.Ordinal)); + if (!IsApplyScript(standardInput)) + { + ValidatedBatches.Add(batch); + ValidationCallIndexes.Add(Calls.Count - 1); + return rejected ? Result(1, string.Empty, "Unrecognized key: \"thinkingLevelMap\"") : Result(0, string.Empty); + } + if (rejected) + return Result(1, string.Empty, "Unrecognized key: \"thinkingLevelMap\""); + if (ApplyWritesBatch) + { + foreach (JsonNode? entry in JsonNode.Parse(batch)!.AsArray()) + { + if ((string?)entry!["path"] == LocalAiGatewayProviderDefinition.ProviderPath) + ProviderJson = entry["value"]!.ToJsonString(); + else if ((string?)entry["path"] == LocalAiGatewayProviderDefinition.PrimaryModelPath) + PrimaryModel = (string?)entry["value"]; + } + return Result(0, string.Empty); + } if (ProviderAfterApply is not null) ProviderJson = ProviderAfterApply; PrimaryModel = PrimaryAfterApply; diff --git a/tests/OpenClaw.Tray.Tests/LocalAiGatewayRpcConfigurationTransportTests.cs b/tests/OpenClaw.Tray.Tests/LocalAiGatewayRpcConfigurationTransportTests.cs index 28add5e831..182bef3bc5 100644 --- a/tests/OpenClaw.Tray.Tests/LocalAiGatewayRpcConfigurationTransportTests.cs +++ b/tests/OpenClaw.Tray.Tests/LocalAiGatewayRpcConfigurationTransportTests.cs @@ -196,6 +196,43 @@ public void MissingCapabilityOrWriteScopeFailsBeforeRequest(bool patchAvailable, Assert.Empty(rpc.Calls); } + [Fact] + public async Task GatewayWithoutConfigSchemaSupportsEffortMetadataWithoutRequest() + { + var rpc = new Transport(); + + Assert.True(await Create(rpc).SupportsEffortMetadataAsync(CancellationToken.None)); + Assert.Empty(rpc.Calls); + } + + [Theory] + [InlineData("""{"schema":{"properties":{"compat":{"properties":{"supportsReasoningEffort":{}}}}}}""", false)] + [InlineData("""{"schema":{"properties":{"compat":{"properties":{"supportedReasoningEfforts":{},"reasoningEffortMap":{}}}}}}""", false)] + [InlineData("""{"schema":{"properties":{"thinkingLevelMap":{},"compat":{"properties":{"supportedReasoningEfforts":{},"reasoningEffortMap":{}}}}}}""", true)] + public async Task ConfigSchemaDecidesEffortMetadataSupport(string schema, bool expected) + { + var rpc = new Transport + { + Methods = ["config.get", "config.patch", "config.schema"], + Response = JsonDocument.Parse(schema).RootElement.Clone(), + }; + + Assert.Equal(expected, await Create(rpc).SupportsEffortMetadataAsync(CancellationToken.None)); + Assert.Equal("config.schema", Assert.Single(rpc.Calls).Method); + } + + [Fact] + public async Task ConfigSchemaCheckRequiresTheBoundOwner() + { + var rpc = new Transport { Methods = ["config.get", "config.patch", "config.schema"] }; + var configuration = Create(rpc); + rpc.Generation++; + + await Assert.ThrowsAsync(() => + configuration.SupportsEffortMetadataAsync(CancellationToken.None)); + Assert.Empty(rpc.Calls); + } + private static readonly GatewayRecord NativeRecord = new() { Id = "native", From 4c902b0b3310fcb8b30d07fecb59feef9212abdb Mon Sep 17 00:00:00 2001 From: Alberto Delgado <44581951+turutupa@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:49:18 -0800 Subject: [PATCH 5/5] fix: never gate route withdrawal on the provider-shape check An independent review of 7bdbfe9d found that endpoint-cycle quiesce ran the provider-shape check before withdrawing. The runtime also withdraws through an endpoint cycle to take the route off an untrusted or conflicting listener (LlamaServerRuntimeService ownership-incomplete and conflict paths), so a failed check (config.schema timeout, dry-run indeterminate) left the Gateway routing prompts to that listener. - Quiesce withdraws unconditionally again, as on main. The shape is decided only in PublishAsync, once per publication, before anything is written. This also removes the cached decision, so native quiesce and publish on separate coordinator instances no longer re-check after a withdrawal, and a failed quiesce cannot leave a stale shape behind. - WSL treats the effort shape as rejected only when its dry-run fails twice and the pre-effort shape validates, so one failed CLI run cannot downgrade a working route. - The rejected and check-failed messages now say nothing was written instead of claiming the current route was kept (all six languages). Tests: endpoint-cycle withdrawal never waits on the check (WSL and native, native publish through a separate coordinator), publication rejecting both shapes writes nothing, and a one-off dry-run failure keeps the current shape. Co-Authored-By: Claude Opus 5.5 --- .../LocalAiGatewayProviderCoordinator.cs | 88 +++++++-------- .../Strings/en-us/Resources.resw | 4 +- .../Strings/fr-fr/Resources.resw | 4 +- .../Strings/nl-nl/Resources.resw | 4 +- .../Strings/pt-br/Resources.resw | 4 +- .../Strings/zh-cn/Resources.resw | 4 +- .../Strings/zh-tw/Resources.resw | 4 +- .../LocalAiGatewayProviderCoordinatorTests.cs | 105 +++++++++++++----- 8 files changed, 132 insertions(+), 85 deletions(-) diff --git a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs index 9035ed5621..da19e64208 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs @@ -21,10 +21,6 @@ internal sealed class LocalAiGatewayProviderCoordinator : ILocalAiEndpointLifecy private readonly IOpenClawLogger _logger; private readonly Func? _getString; - // The provider shape the installed Gateway accepts, decided before an - // endpoint-cycle withdrawal and consumed by the publication that follows. - private ProviderShape? _decidedShape; - public LocalAiGatewayProviderCoordinator( IWslCommandRunner commands, ILocalAiGatewayDistroResolver distroResolver, @@ -64,8 +60,6 @@ public Task QuiesceAsync( CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(install); - // Every external withdrawal starts a fresh decision; the Gateway may have changed. - _decidedShape = null; return QuiesceCoreAsync(install, reason, cancellationToken); } @@ -109,16 +103,9 @@ private async Task QuiesceCoreAsync( } string? expectedPrimary = current.PrimaryModel; + // Withdrawal never waits on the provider-shape check: the runtime also + // withdraws here to take the route off an untrusted or conflicting listener. bool retainManagedPrimary = reason == LocalAiQuiesceReason.EndpointCycle; - if (retainManagedPrimary && current.ProviderExists) - { - // The route is republished right after an endpoint cycle. Withdraw - // it only once the installed Gateway is known to accept a replacement. - LocalAiEndpointLifecycleResult decided = await EnsureShapeAsync(install, cancellationToken) - .ConfigureAwait(false); - if (!decided.Success) - return decided; - } if (_atomicConfiguration is not null) { if (primaryIsManaged && !retainManagedPrimary) @@ -192,14 +179,7 @@ public async Task PublishAsync( CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(install); - try - { - return await PublishCoreAsync(install, cancellationToken).ConfigureAwait(false); - } - finally - { - _decidedShape = null; - } + return await PublishCoreAsync(install, cancellationToken).ConfigureAwait(false); } private async Task PublishCoreAsync( @@ -223,11 +203,10 @@ private async Task PublishCoreAsync( if (current.ProviderExists && MatchesProvider(current.ProviderJson!, install, ProviderShape.Current)) return admission; // Decide what the installed Gateway accepts before anything is written or withdrawn. - LocalAiEndpointLifecycleResult decided = await EnsureShapeAsync(install, cancellationToken) + (LocalAiEndpointLifecycleResult decided, ProviderShape shape) = await DecideShapeAsync(install, cancellationToken) .ConfigureAwait(false); if (!decided.Success) return decided; - ProviderShape shape = _decidedShape!.Value; if (current.ProviderExists) { // A Gateway that rejects effort metadata keeps its pre-effort route as is. @@ -539,11 +518,12 @@ private bool MatchesProvider(string json, LocalAiResolvedInstall install, Provid ? LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(json, install, _getApiKey?.Invoke()) : LocalAiGatewayProviderDefinition.MatchesProviderJson(json, install, _getApiKey?.Invoke()); - private async Task EnsureShapeAsync( + // Decides, once per publication and before anything is written or withdrawn, + // which provider shape the installed Gateway accepts. Nothing is cached: the + // Gateway may be upgraded or replaced between publications. + private async Task<(LocalAiEndpointLifecycleResult Result, ProviderShape Shape)> DecideShapeAsync( LocalAiResolvedInstall install, CancellationToken ct) { - if (_decidedShape is not null) - return LocalAiEndpointLifecycleResult.Ok(); bool effortMetadata; if (_atomicConfiguration is not null) { @@ -555,31 +535,35 @@ private async Task EnsureShapeAsync( catch (Exception) { // RPC errors can include authored config. Never log their payload. - return Failed(ShapeCheckFailed()); + return (Failed(ShapeCheckFailed()), default); } } else { // A dry-run validates against the installed schema without writing. - LocalAiGatewayCommandOutcome full = await _configuration!.ValidateBatchAsync( - LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install), ct).ConfigureAwait(false); - if (!full.Routed) - return Failed(full.Detail!); - if (full.Result!.TimedOut || full.Result.OutcomeIndeterminate) - return Failed(ShapeCheckFailed()); - effortMetadata = full.Result.Success; + // A rejection counts only when it repeats and the pre-effort entry + // validates, so one failed CLI run cannot downgrade a working route. + string fullBatch = LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install); + var full = await ValidateAsync(fullBatch, ct).ConfigureAwait(false); + if (full.Failure is not null) + return (full.Failure, default); + if (!full.Accepted) + { + full = await ValidateAsync(fullBatch, ct).ConfigureAwait(false); + if (full.Failure is not null) + return (full.Failure, default); + } + effortMetadata = full.Accepted; if (!effortMetadata) { - LocalAiGatewayCommandOutcome legacy = await _configuration.ValidateBatchAsync( + var legacy = await ValidateAsync( LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install, legacy: true), ct) .ConfigureAwait(false); - if (!legacy.Routed) - return Failed(legacy.Detail!); - if (legacy.Result!.TimedOut || legacy.Result.OutcomeIndeterminate) - return Failed(ShapeCheckFailed()); - if (!legacy.Result.Success) - return Failed(Localized("LocalAi_GatewayProviderRejected", - "The Gateway rejected the Local AI provider configuration. The current Local AI route was kept; update the OpenClaw Gateway and retry.")); + if (legacy.Failure is not null) + return (legacy.Failure, default); + if (!legacy.Accepted) + return (Failed(Localized("LocalAi_GatewayProviderRejected", + "The Gateway rejected the Local AI provider configuration, so nothing was written. Update the OpenClaw Gateway and retry.")), default); } } if (!effortMetadata) @@ -587,12 +571,22 @@ private async Task EnsureShapeAsync( _logger.Warn("The Gateway does not accept Local AI effort metadata (it needs OpenClaw Gateway 2026.6.5 or later). " + "Publishing the earlier provider shape, so Effort Off cannot turn reasoning off on this Gateway."); } - _decidedShape = effortMetadata ? ProviderShape.Current : ProviderShape.Legacy; - return LocalAiEndpointLifecycleResult.Ok(); + return (LocalAiEndpointLifecycleResult.Ok(), effortMetadata ? ProviderShape.Current : ProviderShape.Legacy); + } + + private async Task<(bool Accepted, LocalAiEndpointLifecycleResult? Failure)> ValidateAsync( + string batch, CancellationToken ct) + { + LocalAiGatewayCommandOutcome outcome = await _configuration!.ValidateBatchAsync(batch, ct).ConfigureAwait(false); + if (!outcome.Routed) + return (false, Failed(outcome.Detail!)); + if (outcome.Result!.TimedOut || outcome.Result.OutcomeIndeterminate) + return (false, Failed(ShapeCheckFailed())); + return (outcome.Result.Success, null); } private string ShapeCheckFailed() => Localized("LocalAi_GatewayProviderCheckFailed", - "The Gateway could not confirm which Local AI configuration it accepts. The current Local AI route was kept; reconnect the Gateway and retry."); + "The Gateway could not confirm which Local AI configuration it accepts, so nothing was written. Reconnect the Gateway and retry."); private async Task CaptureAtomicAsync(CancellationToken ct) { diff --git a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw index f6c4d9f7be..fa65c346ad 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw @@ -10179,6 +10179,6 @@ After enrolling, return to Settings > Windows Update and check for updates. I Widgets and tabs this session published. No panels are available for this conversation yet. The saved Local AI route could not be verified. Preserve the endpoint and reconcile the original Gateway. - The Gateway rejected the Local AI provider configuration. The current Local AI route was kept; update the OpenClaw Gateway and retry. - The Gateway could not confirm which Local AI configuration it accepts. The current Local AI route was kept; reconnect the Gateway and retry. + The Gateway rejected the Local AI provider configuration, so nothing was written. Update the OpenClaw Gateway and retry. + The Gateway could not confirm which Local AI configuration it accepts, so nothing was written. Reconnect the Gateway and retry. diff --git a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw index b508b256d1..45df134d07 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw @@ -10108,6 +10108,6 @@ Après votre inscription, revenez dans Paramètres > Windows Update et recher Widgets et onglets publiés par cette session. Aucun panneau n'est encore disponible pour cette conversation. La route d’IA locale enregistrée n’a pas pu être vérifiée. Conservez le point de terminaison et réconciliez le Gateway d’origine. - Le Gateway a refusé la configuration du fournisseur d’IA locale. La route d’IA locale actuelle a été conservée ; mettez à jour le Gateway OpenClaw et réessayez. - Le Gateway n’a pas pu confirmer quelle configuration d’IA locale il accepte. La route d’IA locale actuelle a été conservée ; reconnectez le Gateway et réessayez. + Le Gateway a refusé la configuration du fournisseur d’IA locale ; rien n’a été écrit. Mettez à jour le Gateway OpenClaw et réessayez. + Le Gateway n’a pas pu confirmer quelle configuration d’IA locale il accepte ; rien n’a été écrit. Reconnectez le Gateway et réessayez. diff --git a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw index 77005a1743..270c5de8d5 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw @@ -10109,6 +10109,6 @@ Ga na inschrijving terug naar Instellingen > Windows Update en controleer op Widgets en tabbladen die deze sessie heeft gepubliceerd. Er zijn nog geen panelen beschikbaar voor dit gesprek. De opgeslagen route voor Lokale AI kon niet worden geverifieerd. Behoud het eindpunt en herstel de oorspronkelijke Gateway. - De Gateway heeft de providerconfiguratie voor Lokale AI geweigerd. De huidige route voor Lokale AI is behouden; werk de OpenClaw Gateway bij en probeer het opnieuw. - De Gateway kon niet bevestigen welke configuratie voor Lokale AI hij accepteert. De huidige route voor Lokale AI is behouden; maak opnieuw verbinding met de Gateway en probeer het opnieuw. + De Gateway heeft de providerconfiguratie voor Lokale AI geweigerd; er is niets weggeschreven. Werk de OpenClaw Gateway bij en probeer het opnieuw. + De Gateway kon niet bevestigen welke configuratie voor Lokale AI hij accepteert; er is niets weggeschreven. Maak opnieuw verbinding met de Gateway en probeer het opnieuw. diff --git a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw index 065a92998a..3fdaffc9cb 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw @@ -10161,6 +10161,6 @@ Após a inscrição, volte para Configurações > Windows Update e verifique Widgets e guias publicados por esta sessão. Ainda não há painéis disponíveis para esta conversa. Não foi possível verificar a rota de IA local salva. Preserve o endpoint e reconcilie o Gateway original. - O Gateway rejeitou a configuração do provedor de IA local. A rota de IA local atual foi mantida; atualize o OpenClaw Gateway e tente novamente. - O Gateway não conseguiu confirmar qual configuração de IA local aceita. A rota de IA local atual foi mantida; reconecte o Gateway e tente novamente. + O Gateway rejeitou a configuração do provedor de IA local; nada foi gravado. Atualize o OpenClaw Gateway e tente novamente. + O Gateway não conseguiu confirmar qual configuração de IA local aceita; nada foi gravado. Reconecte o Gateway e tente novamente. diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw index 30feeb0343..963bd1e71c 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw @@ -10108,6 +10108,6 @@ 此会话发布的小组件和选项卡。 此对话暂无可用的面板。 无法验证已保存的本地 AI 路由。请保留端点并协调原 Gateway 的配置。 - Gateway 拒绝了本地 AI 提供程序配置。已保留当前的本地 AI 路由;请更新 OpenClaw Gateway 后重试。 - Gateway 无法确认它接受哪种本地 AI 配置。已保留当前的本地 AI 路由;请重新连接 Gateway 后重试。 + Gateway 拒绝了本地 AI 提供程序配置,未写入任何内容。请更新 OpenClaw Gateway 后重试。 + Gateway 无法确认它接受哪种本地 AI 配置,未写入任何内容。请重新连接 Gateway 后重试。 diff --git a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw index 7658863b8d..2e4581d165 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw @@ -10108,6 +10108,6 @@ 此工作階段發布的小工具和索引標籤。 此對話尚無可用的面板。 無法驗證已儲存的本機 AI 路由。請保留端點並協調原始 Gateway 的設定。 - Gateway 拒絕了本機 AI 提供者設定。已保留目前的本機 AI 路由;請更新 OpenClaw Gateway 後再試一次。 - Gateway 無法確認它接受哪種本機 AI 設定。已保留目前的本機 AI 路由;請重新連線 Gateway 後再試一次。 + Gateway 拒絕了本機 AI 提供者設定,未寫入任何內容。請更新 OpenClaw Gateway 後再試一次。 + Gateway 無法確認它接受哪種本機 AI 設定,未寫入任何內容。請重新連線 Gateway 後再試一次。 diff --git a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs index 75662305dc..6282598158 100644 --- a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs +++ b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs @@ -177,12 +177,14 @@ public async Task WslPublish_GatewayRejectingEffortMetadataKeepsLegacyRouteUntou var result = await CreateCoordinator(commands).PublishAsync(install); - // Gateway 2026.6.1-2026.6.4 admits protocol 4 but rejects thinkingLevelMap: + // Gateway 2026.6.1 admits protocol 4 but rejects thinkingLevelMap: // the working pre-effort route is current there, so nothing is withdrawn. + // The rejection is confirmed by a second dry-run before the fallback. Assert.True(result.Success, result.Detail); - Assert.Equal(2, commands.ValidatedBatches.Count); + Assert.Equal(3, commands.ValidatedBatches.Count); Assert.Contains("thinkingLevelMap", commands.ValidatedBatches[0], StringComparison.Ordinal); - Assert.DoesNotContain("thinkingLevelMap", commands.ValidatedBatches[1], StringComparison.Ordinal); + Assert.Contains("thinkingLevelMap", commands.ValidatedBatches[1], StringComparison.Ordinal); + Assert.DoesNotContain("thinkingLevelMap", commands.ValidatedBatches[2], StringComparison.Ordinal); Assert.DoesNotContain(commands.Calls, call => call.Contains("unset")); Assert.DoesNotContain(commands.Calls, call => call.Contains("restart")); Assert.Equal(-1, commands.FirstApplyIndex); @@ -208,6 +210,8 @@ public async Task WslEndpointCycle_GatewayRejectingEffortMetadataRepublishesLega var withdrawn = await coordinator.QuiesceAsync(install, LocalAiQuiesceReason.EndpointCycle); Assert.True(withdrawn.Success, withdrawn.Detail); Assert.Null(commands.ProviderJson); + // Withdrawal never waits on the shape check. + Assert.Empty(commands.ValidatedBatches); var published = await coordinator.PublishAsync(restarted); Assert.True(published.Success, published.Detail); @@ -215,14 +219,32 @@ public async Task WslEndpointCycle_GatewayRejectingEffortMetadataRepublishesLega Assert.False(LocalAiGatewayProviderDefinition.MatchesProviderJson(commands.ProviderJson!, restarted)); Assert.Equal(commands.ProviderJson, commands.ActiveProviderJson); Assert.Equal(managedPrimary, commands.PrimaryModel); - // The withdrawal decided the shape; publication reuses it instead of probing again. - Assert.Equal(2, commands.ValidatedBatches.Count); - int unset = commands.Calls.FindIndex(call => call.Contains("unset")); - Assert.All(commands.ValidationCallIndexes, index => Assert.InRange(index, 0, unset - 1)); + // Publication decided the shape before its write. + Assert.Equal(3, commands.ValidatedBatches.Count); + Assert.All(commands.ValidationCallIndexes, + index => Assert.InRange(index, 0, commands.FirstApplyIndex - 1)); + } + + [Fact] + public async Task WslEndpointCycle_WithdrawalNeverWaitsOnTheShapeCheck() + { + var install = Install(28_765); + string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); + // Even a Gateway that would reject every provider: the runtime also + // withdraws through an endpoint cycle to take the route off an untrusted listener. + var commands = new FakeWslCommandRunner(LegacyProviderJson(install), managedPrimary) { RejectEveryProvider = true }; + + var withdrawn = await CreateCoordinator(commands).QuiesceAsync(install, LocalAiQuiesceReason.EndpointCycle); + + Assert.True(withdrawn.Success, withdrawn.Detail); + Assert.Empty(commands.ValidatedBatches); + Assert.Null(commands.ProviderJson); + Assert.Null(commands.ActiveProviderJson); + Assert.Equal(managedPrimary, commands.PrimaryModel); } [Fact] - public async Task WslEndpointCycle_GatewayRejectingBothShapesNeverWithdrawsRoute() + public async Task WslPublish_GatewayRejectingBothShapesWritesNothing() { var install = Install(28_765); string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); @@ -232,14 +254,11 @@ public async Task WslEndpointCycle_GatewayRejectingBothShapesNeverWithdrawsRoute new LocalAiGatewayDistroResolver(CreateRegistry(ManagedRecord("managed", "OpenClawGateway"))), NullLogger.Instance, key => $"localized:{key}"); - var withdrawn = await coordinator.QuiesceAsync(install, LocalAiQuiesceReason.EndpointCycle); var published = await coordinator.PublishAsync(install); - Assert.False(withdrawn.Success); - Assert.Equal("localized:LocalAi_GatewayProviderRejected", withdrawn.Detail); Assert.False(published.Success); Assert.Equal("localized:LocalAi_GatewayProviderRejected", published.Detail); - Assert.Equal(4, commands.ValidatedBatches.Count); + Assert.Equal(3, commands.ValidatedBatches.Count); Assert.DoesNotContain(commands.Calls, call => call.Contains("unset")); Assert.DoesNotContain(commands.Calls, call => call.Contains("restart")); Assert.Equal(-1, commands.FirstApplyIndex); @@ -248,6 +267,29 @@ public async Task WslEndpointCycle_GatewayRejectingBothShapesNeverWithdrawsRoute Assert.Equal(managedPrimary, commands.PrimaryModel); } + [Fact] + public async Task WslPublish_OneFailedDryRunDoesNotDowngradeToTheLegacyShape() + { + var install = Install(28_765); + string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); + var commands = new FakeWslCommandRunner(RedactApiKey(LegacyProviderJson(install)), managedPrimary) + { + ApplyWritesBatch = true, + FailedValidations = [0], + }; + + var result = await CreateCoordinator(commands).PublishAsync(install); + + // A Gateway that accepts effort metadata, with one unrelated CLI failure: + // the repeated dry-run passes and the current shape is published. + Assert.True(result.Success, result.Detail); + Assert.Equal(2, commands.ValidatedBatches.Count); + Assert.All(commands.ValidatedBatches, + batch => Assert.Contains("thinkingLevelMap", batch, StringComparison.Ordinal)); + Assert.True(LocalAiGatewayProviderDefinition.MatchesProviderJson(commands.ProviderJson!, install)); + Assert.Equal(commands.ProviderJson, commands.ActiveProviderJson); + } + [Theory] [InlineData(true)] [InlineData(false)] @@ -292,28 +334,35 @@ public async Task RpcPublish_GatewayWithoutEffortSchemaKeepsLegacyProviderWithou } [Fact] - public async Task RpcQuiesce_EndpointCycleKeepsRouteWhenSchemaCannotBeRead() + public async Task RpcEndpointCycle_WithdrawsWithoutSchemaCheckAndPublishWritesNothingWhenSchemaCannotBeRead() { var install = Install(28_765, "cloud/prior"); var transport = new AtomicTransport("{}") { FailSchema = true }; transport.SetManaged(install); - string before = transport.Config.ToJsonString(); - var coordinator = new LocalAiGatewayProviderCoordinator( - transport, () => "actual-local-secret", NullLogger.Instance, key => $"localized:{key}"); + StripEffortMetadata((JsonObject)transport.Config["models"]!["providers"]!["llamacpp"]!["models"]![0]!); - var cycle = await coordinator.QuiesceAsync(install, LocalAiQuiesceReason.EndpointCycle); + // The runtime withdraws through an endpoint cycle and, as in production, + // publishes through a separate coordinator instance. + var cycle = await new LocalAiGatewayProviderCoordinator( + transport, () => "actual-local-secret", NullLogger.Instance, key => $"localized:{key}") + .QuiesceAsync(install, LocalAiQuiesceReason.EndpointCycle); - Assert.False(cycle.Success); - Assert.Equal("localized:LocalAi_GatewayProviderCheckFailed", cycle.Detail); - Assert.DoesNotContain("sensitive", cycle.Detail); - Assert.Empty(transport.Patches); - Assert.Equal(before, transport.Config.ToJsonString()); + Assert.True(cycle.Success, cycle.Detail); + Assert.Equal(0, transport.SchemaChecks); + Assert.Null(transport.Config["models"]?["providers"]?["llamacpp"]); + string withdrawn = transport.Config.ToJsonString(); + int patches = transport.Patches.Count; + + var published = await new LocalAiGatewayProviderCoordinator( + transport, () => "actual-local-secret", NullLogger.Instance, key => $"localized:{key}") + .PublishAsync(install); - // Teardown publishes nothing afterwards, so it needs no schema decision. - var teardown = await coordinator.QuiesceAsync(install, LocalAiQuiesceReason.Teardown); - Assert.True(teardown.Success, teardown.Detail); + Assert.False(published.Success); + Assert.Equal("localized:LocalAi_GatewayProviderCheckFailed", published.Detail); + Assert.DoesNotContain("sensitive", published.Detail); Assert.Equal(1, transport.SchemaChecks); - Assert.Null(transport.Config["models"]?["providers"]?["llamacpp"]); + Assert.Equal(patches, transport.Patches.Count); + Assert.Equal(withdrawn, transport.Config.ToJsonString()); } /// The exact provider shape published before effort metadata was added. @@ -1386,6 +1435,8 @@ private sealed class FakeWslCommandRunner(string? providerJson, string? primaryM /// Models Gateway 2026.6.1-2026.6.4: protocol 4, but no thinkingLevelMap in its model schema. public bool RejectEffortMetadata { get; init; } public bool RejectEveryProvider { get; init; } + /// Zero-based dry-run ordinals that fail once for a reason unrelated to the schema. + public HashSet FailedValidations { get; init; } = []; /// Persists the provider and primary from an applied batch instead of the preset values. public bool ApplyWritesBatch { get; init; } public Action? CommandObserved { get; init; } @@ -1427,6 +1478,8 @@ public Task RunInDistroAsync( { ValidatedBatches.Add(batch); ValidationCallIndexes.Add(Calls.Count - 1); + if (FailedValidations.Contains(ValidatedBatches.Count - 1)) + return Result(1, string.Empty, "config file is locked"); return rejected ? Result(1, string.Empty, "Unrecognized key: \"thinkingLevelMap\"") : Result(0, string.Empty); } if (rejected)