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..47def99632 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; @@ -25,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); @@ -41,7 +49,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 BuildLegacyProviderJson(install); + 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 +90,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 +142,33 @@ 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); + + /// + /// 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); _ = 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 +188,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. @@ -176,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 75149de042..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() }, @@ -59,8 +60,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 +122,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 +130,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); @@ -201,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)) { @@ -254,9 +269,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!))) @@ -266,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)) @@ -367,9 +382,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!)); @@ -383,7 +397,7 @@ private static async Task ReconcileFailedRecoveryRestoreAsync( observed.ProviderExisted && observed.PrimaryModelExisted && ctx.LocalAiResolvedInstall is { } replacement && - LocalAiGatewayProviderDefinition.MatchesProviderJson( + LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson( observed.ProviderJson!, replacement) && JsonEquals( @@ -444,7 +458,7 @@ fallbackModel is not null && } if ((current.ProviderExisted && - !LocalAiGatewayProviderDefinition.MatchesProviderJson(current.ProviderJson!, install)) || + !LocalAiGatewayProviderDefinition.MatchesCurrentOrLegacyProviderJson(current.ProviderJson!, install)) || (current.PrimaryModelExisted && !currentPrimaryIsManaged && !currentPrimaryIsFallback && @@ -537,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}} @@ -554,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.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..dde357a236 100644 --- a/src/OpenClaw.Tray.WinUI/App.xaml.cs +++ b/src/OpenClaw.Tray.WinUI/App.xaml.cs @@ -925,10 +925,11 @@ _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/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 56d655b563..70ca289204 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 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())) + { + _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; } @@ -873,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 895e20b4e3..da19e64208 100644 --- a/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs +++ b/src/OpenClaw.Tray.WinUI/Services/LocalAiGatewayProviderCoordinator.cs @@ -19,41 +19,55 @@ 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( + public Task QuiesceAsync( LocalAiResolvedInstall install, LocalAiQuiesceReason reason = LocalAiQuiesceReason.Teardown, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(install); + 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."); @@ -66,8 +80,9 @@ public async Task QuiesceAsync( install.Manifest.GatewayFallbackModel); if (current.ProviderExists) { - _ = BuildProvider(install); - if (!MatchesProvider(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."); } @@ -88,6 +103,8 @@ public async Task QuiesceAsync( } 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 (_atomicConfiguration is not null) { @@ -162,10 +179,16 @@ public async Task PublishAsync( CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(install); - string batch; + return await PublishCoreAsync(install, cancellationToken).ConfigureAwait(false); + } + + private async Task PublishCoreAsync( + LocalAiResolvedInstall install, + CancellationToken cancellationToken) + { try { - batch = LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install); + _ = LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install); } catch (Exception ex) when (ex is InvalidDataException or InvalidOperationException) { @@ -174,8 +197,38 @@ 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; + // 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, ProviderShape shape) = await DecideShapeAsync(install, cancellationToken) + .ConfigureAwait(false); + if (!decided.Success) + return decided; + if (current.ProviderExists) + { + // 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. + 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 QuiesceCoreAsync( + 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) { @@ -196,14 +249,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!); @@ -227,16 +282,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)) { @@ -262,7 +317,8 @@ private LocalAiEndpointLifecycleResult CheckPublication(GatewayCapture current, string managedPrimary = LocalAiGatewayProviderDefinition.BuildPrimaryModel(install); if (current.ProviderExists) { - return MatchesProvider(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() @@ -283,6 +339,38 @@ 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); + 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; + 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, ProviderShape.Current) && + 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,18 +493,100 @@ 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); 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) => - LocalAiGatewayProviderDefinition.MatchesProviderJson(json, install, _getApiKey?.Invoke()); + private bool MatchesProvider(string json, LocalAiResolvedInstall install, ProviderShape shape) => + shape == ProviderShape.Legacy + ? LocalAiGatewayProviderDefinition.MatchesLegacyProviderJson(json, install, _getApiKey?.Invoke()) + : LocalAiGatewayProviderDefinition.MatchesProviderJson(json, install, _getApiKey?.Invoke()); + + // 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) + { + 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()), default); + } + } + else + { + // A dry-run validates against the installed schema without writing. + // 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) + { + var legacy = await ValidateAsync( + LocalAiGatewayProviderDefinition.BuildProviderBatchJson(install, legacy: true), ct) + .ConfigureAwait(false); + 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) + { + _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."); + } + 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, so nothing was written. Reconnect the Gateway and retry."); private async Task CaptureAtomicAsync(CancellationToken ct) { @@ -477,6 +647,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 df771a24e3..fa65c346ad 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw @@ -10178,4 +10178,7 @@ 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 saved Local AI route could not be verified. Preserve the endpoint and reconcile the original Gateway. + 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 08ac904554..45df134d07 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw @@ -10107,4 +10107,7 @@ 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. + 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 ; 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 0517f84327..270c5de8d5 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw @@ -10108,4 +10108,7 @@ 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 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; 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 9ea7711296..3fdaffc9cb 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/pt-br/Resources.resw @@ -10160,4 +10160,7 @@ 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 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; 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 aec0a0cfb5..963bd1e71c 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw @@ -10107,4 +10107,7 @@ 跟踪此对话中委派的工作。 此会话发布的小组件和选项卡。 此对话暂无可用的面板。 + 无法验证已保存的本地 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 d0f0cb3c2e..2e4581d165 100644 --- a/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw +++ b/src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw @@ -10107,4 +10107,7 @@ 追蹤此對話中委派的工作。 此工作階段發布的小工具和索引標籤。 此對話尚無可用的面板。 + 無法驗證已儲存的本機 AI 路由。請保留端點並協調原始 Gateway 的設定。 + Gateway 拒絕了本機 AI 提供者設定,未寫入任何內容。請更新 OpenClaw Gateway 後再試一次。 + Gateway 無法確認它接受哪種本機 AI 設定,未寫入任何內容。請重新連線 Gateway 後再試一次。 diff --git a/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs b/tests/OpenClaw.Connection.Tests/LocalAiPortLifecycleTests.cs index eba07840f7..55e6964307 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() { @@ -3590,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.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs b/tests/OpenClaw.SetupEngine.Tests/LocalAiGatewayUninstallTests.cs index 1b7f9423b6..55cda117bd 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; @@ -132,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); @@ -162,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() { @@ -182,6 +215,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 +374,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 +433,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 +836,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 } }; @@ -809,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( @@ -858,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.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..6282598158 100644 --- a/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs +++ b/tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs @@ -35,6 +35,353 @@ 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 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() + { + var install = Install(28_765); + 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 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.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); + 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 batch = DecodeBatch(commands.StandardInputs[publish]!); + 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_LegacyWithdrawalFailureWritesNoNewProvider() + { + var install = Install(28_765); + 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(-1, commands.FirstApplyIndex); + 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_CurrentRouteIsAcceptedWithoutChanges() + { + var install = Install(28_765); + var commands = new FakeWslCommandRunner(LocalAiGatewayProviderDefinition.BuildProviderJson(install, "llama-local"), + LocalAiGatewayProviderDefinition.BuildPrimaryModel(install)); + + var result = await CreateCoordinator(commands).PublishAsync(install); + + Assert.True(result.Success, result.Detail); + Assert.DoesNotContain(commands.Calls, call => call.Contains("restart")); + 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 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(3, commands.ValidatedBatches.Count); + Assert.Contains("thinkingLevelMap", commands.ValidatedBatches[0], 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); + 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); + // Withdrawal never waits on the shape check. + Assert.Empty(commands.ValidatedBatches); + 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); + // 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 WslPublish_GatewayRejectingBothShapesWritesNothing() + { + 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 published = await coordinator.PublishAsync(install); + + Assert.False(published.Success); + Assert.Equal("localized:LocalAi_GatewayProviderRejected", published.Detail); + 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); + Assert.Equal(legacy, commands.ProviderJson); + Assert.Equal(legacy, commands.ActiveProviderJson); + 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)] + 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 RpcEndpointCycle_WithdrawsWithoutSchemaCheckAndPublishWritesNothingWhenSchemaCannotBeRead() + { + var install = Install(28_765, "cloud/prior"); + var transport = new AtomicTransport("{}") { FailSchema = true }; + transport.SetManaged(install); + StripEffortMetadata((JsonObject)transport.Config["models"]!["providers"]!["llamacpp"]!["models"]![0]!); + + // 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.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); + + Assert.False(published.Success); + Assert.Equal("localized:LocalAi_GatewayProviderCheckFailed", published.Detail); + Assert.DoesNotContain("sensitive", published.Detail); + Assert.Equal(1, transport.SchemaChecks); + Assert.Equal(patches, transport.Patches.Count); + Assert.Equal(withdrawn, transport.Config.ToJsonString()); + } + + /// 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)] @@ -132,8 +479,21 @@ 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; } + /// 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(); @@ -162,6 +522,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; } @@ -238,6 +603,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] @@ -604,12 +973,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); @@ -618,7 +992,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); @@ -1035,6 +1409,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\"", @@ -1050,13 +1432,26 @@ 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; } + /// 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; } 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, @@ -1075,6 +1470,31 @@ 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); + 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) + 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",