From 3cee7367a309d66ec34e325ec2770e332081a114 Mon Sep 17 00:00:00 2001 From: "Angnuo Li (from Dev Box)" Date: Wed, 18 Nov 2026 18:16:22 +0800 Subject: [PATCH] perf(models): speed up removal of active provider connections --- desktop/renderer/src/utils/model-provider.ts | 76 +---------------- desktop/src/model-config-application.test.ts | 73 +++++++++++++++- desktop/src/model-config-application.ts | 38 ++++----- desktop/src/provider-connection-config.ts | 84 +++++++++++++++++++ ...ovider-connection-save.integration.test.ts | 51 ++++++++++- ...rovider-api-key-save-latency-2026-11-17.md | 25 +++++- 6 files changed, 245 insertions(+), 102 deletions(-) create mode 100644 desktop/src/provider-connection-config.ts diff --git a/desktop/renderer/src/utils/model-provider.ts b/desktop/renderer/src/utils/model-provider.ts index 1e7a9ef..85b4066 100644 --- a/desktop/renderer/src/utils/model-provider.ts +++ b/desktop/renderer/src/utils/model-provider.ts @@ -1,3 +1,5 @@ +export { removeProviderConnectionConfig } from "../../../src/provider-connection-config"; + export type ModelApiFormat = "openai-chat" | "openai-responses" | "anthropic"; export type ModelInputCapability = "text" | "image"; export type ModelReasoningEffort = @@ -426,77 +428,3 @@ export function removeModelProviderConfig( }, }; } - -export function removeProviderConnectionConfig( - existing: JsonObject, - providerKey: string, -): JsonObject { - const models = asJsonObject(existing.models); - const providers = asJsonObject(models.providers); - if (!Object.hasOwn(providers, providerKey) && providerKey !== "llama-cpp") { - throw new Error(`Provider "${providerKey}" is not configured`); - } - const nextProviders = { ...providers }; - delete nextProviders[providerKey]; - - const belongsToProvider = (value: unknown) => - typeof value === "string" && value.startsWith(`${providerKey}/`); - const clearModelReferences = (value: unknown): unknown => { - if (!isJsonObject(value)) return value; - const scope = { ...value }; - if (belongsToProvider(scope.model)) { - delete scope.model; - } else if (isJsonObject(scope.model)) { - const selector = { ...scope.model }; - if (belongsToProvider(selector.primary)) delete selector.primary; - if (Array.isArray(selector.fallbacks)) { - const remaining = selector.fallbacks.filter((model) => !belongsToProvider(model)); - if (remaining.length !== selector.fallbacks.length) { - if (remaining.length) selector.fallbacks = remaining; - else delete selector.fallbacks; - } - } - if (Object.keys(selector).length) scope.model = selector; - else delete scope.model; - } - if (isJsonObject(scope.models)) { - const remaining = Object.fromEntries( - Object.entries(scope.models).filter(([modelRef]) => !belongsToProvider(modelRef)), - ); - if (Object.keys(remaining).length !== Object.keys(scope.models).length) { - if (Object.keys(remaining).length) scope.models = remaining; - else delete scope.models; - } - } - return scope; - }; - const agents = asJsonObject(existing.agents); - const nextAgents = { ...agents }; - if (Object.hasOwn(agents, "defaults")) { - nextAgents.defaults = clearModelReferences(agents.defaults); - } - if (Array.isArray(agents.list)) nextAgents.list = agents.list.map(clearModelReferences); - if (isJsonObject(agents.entries)) { - nextAgents.entries = Object.fromEntries( - Object.entries(agents.entries).map(([id, agent]) => [id, clearModelReferences(agent)]), - ); - } - const nextConfig = { - ...existing, - models: { ...models, providers: nextProviders }, - agents: nextAgents, - }; - if (providerKey !== "llama-cpp") return nextConfig; - const plugins = asJsonObject(existing.plugins); - const entries = asJsonObject(plugins.entries); - return { - ...nextConfig, - plugins: { - ...plugins, - entries: { - ...entries, - "llama-cpp": { ...asJsonObject(entries["llama-cpp"]), enabled: false }, - }, - }, - }; -} diff --git a/desktop/src/model-config-application.test.ts b/desktop/src/model-config-application.test.ts index 18f85ae..82275b5 100644 --- a/desktop/src/model-config-application.test.ts +++ b/desktop/src/model-config-application.test.ts @@ -5,6 +5,7 @@ import { type ModelConfigUpdate, } from "./model-config-application"; import { ensureGitHubCopilotProviderPlugin } from "./model-provider-plugins"; +import { removeProviderConnectionConfig } from "./provider-connection-config"; import { runWindowsNodeMxcModelUpdate } from "./windows-node-mxc-model-config"; function fixture() { @@ -227,9 +228,53 @@ describe("applyModelConfig", () => { expect(state.saved()).toEqual(state.input.config); }); + it.each(["string", "selector"])( + "removes a referenced provider through config.set and clears its %s default", + async (format) => { + const state = await removalFixture(); + const agents = { + defaults: { + workspace: "keep", + model: + format === "string" + ? "minimax/one" + : { primary: "minimax/one", fallbacks: ["minimax/two", "qwen/old"] }, + models: { "minimax/one": {}, "qwen/old": { alias: "keep" } }, + }, + }; + for (const config of [state.input.expectedConfig, state.saved()]) + (config as Record).agents = structuredClone(agents); + state.input.config = removeProviderConnectionConfig( + state.input.expectedConfig as Record, + "minimax", + ); + const hooks = { + readConfig: state.deps.readConfig, + pause: vi.fn(async () => {}), + apply: () => applyModelConfig(state.deps, state.input), + restartSavedConfig: vi.fn(async () => {}), + verify: vi.fn(async () => {}), + resume: vi.fn(), + failClosed: vi.fn(async () => {}), + }; + await runWindowsNodeMxcModelUpdate(hooks); + expect(state.gateway.request.mock.calls.map(([method]) => method)).toEqual([ + "config.get", + "config.set", + "config.get", + ]); + expect(state.saved()).toEqual(state.input.config); + expect(hooks.resume).toHaveBeenCalledOnce(); + expect(hooks.restartSavedConfig).not.toHaveBeenCalled(); + expect(hooks.failClosed).not.toHaveBeenCalled(); + expect((state.input.expectedConfig as Record).agents).toEqual(agents); + }, + ); + it.each([ "default-change", "agent-change", + "workspace-change", "other-provider", "plugin-change", "retained-primary", @@ -240,7 +285,7 @@ describe("applyModelConfig", () => { const next = state.input.config as { models: { providers: Record }; agents: { - defaults: { model: { primary: string } }; + defaults: { model: { primary: string }; workspace?: string }; entries?: Record; list?: unknown[]; }; @@ -248,6 +293,7 @@ describe("applyModelConfig", () => { }; if (reason === "default-change") next.agents.defaults.model.primary = "qwen/replacement"; if (reason === "agent-change") next.agents.entries = { writer: { model: "qwen/old" } }; + if (reason === "workspace-change") next.agents.defaults.workspace = "different"; if (reason === "other-provider") delete next.models.providers.qwen; if (reason === "plugin-change") next.plugins = { entries: { minimax: { enabled: false } } }; if (reason.startsWith("retained-")) { @@ -300,6 +346,31 @@ describe("applyModelConfig", () => { expect(state.saved()).toEqual(state.input.config); }); + it("rejects a removal receipt that retained the removed provider's model references", async () => { + const state = await removalFixture(); + for (const config of [state.input.expectedConfig, state.saved()]) + (config as Record).agents = { + defaults: { model: { primary: "minimax/one" } }, + }; + state.input.config = removeProviderConnectionConfig( + state.input.expectedConfig as Record, + "minimax", + ); + const request = state.gateway.request.getMockImplementation()!; + state.gateway.request.mockImplementation(async (method, params) => { + if (method === "config.set") { + const config = JSON.parse(String(params?.raw)); + config.agents = (state.input.expectedConfig as Record).agents; + return request(method, { ...params, raw: JSON.stringify(config) }); + } + return request(method, params); + }); + await expect(applyModelConfig(state.deps, state.input)).rejects.toMatchObject({ + cause: { message: "The saved model references do not match the requested removal" }, + }); + expect(state.deps.log).not.toHaveBeenCalledWith(expect.stringContaining("hot-applied")); + }); + it("applies Copilot activation through the MXC model transaction", async () => { const state = fixture(); const next = structuredClone(state.input.expectedConfig) as Record; diff --git a/desktop/src/model-config-application.ts b/desktop/src/model-config-application.ts index af60266..59ee377 100644 --- a/desktop/src/model-config-application.ts +++ b/desktop/src/model-config-application.ts @@ -4,6 +4,7 @@ import { assertConfigWriteAllowed } from "./config-write-policy"; import { GatewayRequestError } from "./gateway-client"; import { supportsLocalModelGatewayVersion } from "./local-model-service"; import { assertModelConfigUnchanged, ModelConfigRestartRequiredError } from "./model-setup"; +import { removeProviderConnectionConfig } from "./provider-connection-config"; type JsonObject = Record; @@ -40,7 +41,7 @@ function object(value: unknown): JsonObject { return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonObject) : {}; } -/** Connection edits/removal leave all other providers, model references and plugins alone. */ +/** Connection edits preserve models; removal may also clear only the removed provider's refs. */ function isManagedProviderConnectionUpdate(params: ModelConfigUpdate): boolean { if ( !["provider-save", "provider-remove"].includes(params.action) || @@ -56,27 +57,12 @@ function isManagedProviderConnectionUpdate(params: ModelConfigUpdate): boolean { Object.hasOwn(nextProviders, params.providerKey) ) return false; - const agents = object(object(params.config).agents); - const scopes = [ - agents.defaults, - ...Object.values(object(agents.entries)), - ...(Array.isArray(agents.list) ? agents.list : []), - ]; - const refersToProvider = (value: unknown) => - typeof value === "string" && value.startsWith(`${params.providerKey}/`); - if ( - scopes.some((scope) => { - const current = object(scope); - const selector = object(current.model); - return ( - refersToProvider(current.model) || - refersToProvider(selector.primary) || - (Array.isArray(selector.fallbacks) && selector.fallbacks.some(refersToProvider)) || - Object.keys(object(current.models)).some(refersToProvider) - ); - }) - ) - return false; + // Use the same removal as the UI so clearing the active default/fallbacks + // does not force config.apply's slower restart-receipt processing. + return isDeepStrictEqual( + removeProviderConnectionConfig(object(params.expectedConfig), params.providerKey), + params.config, + ); } else { const next = object(nextProviders[params.providerKey]); if ( @@ -241,7 +227,8 @@ export async function applyGatewayConfiguration( assertRequestedPluginsSaved(); if (useConnectionSet) { const providers = (config: unknown) => object(object(object(config).models).providers); - const savedProviders = providers(deps.readConfig()); + const savedConfig = deps.readConfig(); + const savedProviders = providers(savedConfig); const expectedProviders = providers(params.config); const providerKey = params.connectionProvider!; const saved = object(savedProviders[providerKey]); @@ -254,6 +241,11 @@ export async function applyGatewayConfiguration( ) ) throw new Error("The saved provider connection does not match the requested settings"); + if ( + !Object.hasOwn(expectedProviders, providerKey) && + !isDeepStrictEqual(object(savedConfig).agents, object(params.config).agents) + ) + throw new Error("The saved model references do not match the requested removal"); } deps.log(`${context} status=hot-applied`); return; diff --git a/desktop/src/provider-connection-config.ts b/desktop/src/provider-connection-config.ts new file mode 100644 index 0000000..c61b310 --- /dev/null +++ b/desktop/src/provider-connection-config.ts @@ -0,0 +1,84 @@ +type JsonObject = Record; + +function isJsonObject(value: unknown): value is JsonObject { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +function asJsonObject(value: unknown): JsonObject { + return isJsonObject(value) ? value : {}; +} + +/** Shared by the settings UI and the Gateway write guard. */ +export function removeProviderConnectionConfig( + existing: JsonObject, + providerKey: string, +): JsonObject { + const models = asJsonObject(existing.models); + const providers = asJsonObject(models.providers); + if (!Object.hasOwn(providers, providerKey) && providerKey !== "llama-cpp") { + throw new Error(`Provider "${providerKey}" is not configured`); + } + const nextProviders = { ...providers }; + delete nextProviders[providerKey]; + + const belongsToProvider = (value: unknown) => + typeof value === "string" && value.startsWith(`${providerKey}/`); + const clearModelReferences = (value: unknown): unknown => { + if (!isJsonObject(value)) return value; + const scope = { ...value }; + if (belongsToProvider(scope.model)) { + delete scope.model; + } else if (isJsonObject(scope.model)) { + const selector = { ...scope.model }; + if (belongsToProvider(selector.primary)) delete selector.primary; + if (Array.isArray(selector.fallbacks)) { + const remaining = selector.fallbacks.filter((model) => !belongsToProvider(model)); + if (remaining.length !== selector.fallbacks.length) { + if (remaining.length) selector.fallbacks = remaining; + else delete selector.fallbacks; + } + } + if (Object.keys(selector).length) scope.model = selector; + else delete scope.model; + } + if (isJsonObject(scope.models)) { + const remaining = Object.fromEntries( + Object.entries(scope.models).filter(([modelRef]) => !belongsToProvider(modelRef)), + ); + if (Object.keys(remaining).length !== Object.keys(scope.models).length) { + if (Object.keys(remaining).length) scope.models = remaining; + else delete scope.models; + } + } + return scope; + }; + const agents = asJsonObject(existing.agents); + const nextAgents = { ...agents }; + if (Object.hasOwn(agents, "defaults")) { + nextAgents.defaults = clearModelReferences(agents.defaults); + } + if (Array.isArray(agents.list)) nextAgents.list = agents.list.map(clearModelReferences); + if (isJsonObject(agents.entries)) { + nextAgents.entries = Object.fromEntries( + Object.entries(agents.entries).map(([id, agent]) => [id, clearModelReferences(agent)]), + ); + } + const nextConfig = { + ...existing, + models: { ...models, providers: nextProviders }, + agents: nextAgents, + }; + if (providerKey !== "llama-cpp") return nextConfig; + const plugins = asJsonObject(existing.plugins); + const entries = asJsonObject(plugins.entries); + return { + ...nextConfig, + plugins: { + ...plugins, + entries: { + ...entries, + "llama-cpp": { ...asJsonObject(entries["llama-cpp"]), enabled: false }, + }, + }, + }; +} diff --git a/desktop/src/provider-connection-save.integration.test.ts b/desktop/src/provider-connection-save.integration.test.ts index 8e696fa..27b3cfc 100644 --- a/desktop/src/provider-connection-save.integration.test.ts +++ b/desktop/src/provider-connection-save.integration.test.ts @@ -10,6 +10,7 @@ import { describe, expect, it, vi } from "vitest"; import * as deviceIdentity from "./device-identity"; import { GatewayClient } from "./gateway-client"; import { applyModelConfig } from "./model-config-application"; +import { removeProviderConnectionConfig } from "./provider-connection-config"; describe.skipIf( !process.env.MICROCLAW_OPENCLAW_TEST_PACKAGE_DIR || !process.env.MICROCLAW_GATEWAY_E2E_PARENT, @@ -208,7 +209,7 @@ describe.skipIf( expect(saved.models.providers.minimax?.apiKey).toBe( next.models.providers.minimax?.apiKey, ); - expect(readConfig().agents).toEqual(before.agents); + expect(readConfig().agents).toEqual(next.agents); expect(client.connectionGeneration).toBe(generation); expect(child.exitCode).toBeNull(); return sample; @@ -258,6 +259,53 @@ describe.skipIf( warmup: index < 2, }); } + const referencedRemovals: typeof samples = []; + for (const [index, method] of [ + "config.apply", + "config.set", + "config.set", + "config.apply", + ].entries()) { + // Prepare an active provider outside the measured interval. The UI's + // removal also clears this default, fallback and model allowlist. + const current = readConfig(); + const prepared = structuredClone(current); + prepared.models.providers.minimax = { + ...structuredClone(connection), + models: [{ id: "MiniMax-M3", name: "Fixture MiniMax" }], + }; + const agents = prepared.agents as Record; + agents.defaults = { + ...(agents.defaults as Record), + model: { primary: "minimax/MiniMax-M3", fallbacks: ["minimax/MiniMax-M3"] }, + models: { "minimax/MiniMax-M3": {} }, + }; + const snapshot = await client.request<{ hash: string }>("config.get", {}); + await client.request("config.set", { + baseHash: snapshot.hash, + raw: JSON.stringify(prepared), + }); + await vi.waitFor( + async () => { + const applied = await client.request<{ + configRevisionHash: string; + appliedConfigHash: string; + }>("config.get", {}); + expect(applied.configRevisionHash).toBeTruthy(); + expect(applied.appliedConfigHash).toBe(applied.configRevisionHash); + }, + { timeout: 30_000, interval: 100 }, + ); + const before = readConfig(); + const next = removeProviderConnectionConfig( + before as unknown as Record, + "minimax", + ) as unknown as FixtureConfig; + referencedRemovals.push({ + ...(await measure(method, "provider-remove", before, next)), + warmup: index < 2, + }); + } const report = { firstSaveMethod, gatewayVersion: client.serverVersion, @@ -265,6 +313,7 @@ describe.skipIf( "isolated Gateway configuration write and applied-revision confirmation; excludes API validation and MXC", samples, removals, + referencedRemovals, }; console.info(`[provider-save-benchmark] ${JSON.stringify(report)}`); const reportPath = process.env.MICROCLAW_PROVIDER_SAVE_REPORT; diff --git a/docs/reference/provider-api-key-save-latency-2026-11-17.md b/docs/reference/provider-api-key-save-latency-2026-11-17.md index 66602d5..55a70fa 100644 --- a/docs/reference/provider-api-key-save-latency-2026-11-17.md +++ b/docs/reference/provider-api-key-save-latency-2026-11-17.md @@ -19,7 +19,7 @@ 对于 OpenClaw 2026.10.10,在 Gateway 支持 `config.set` 且配置允许热更新时,千问、MiniMax 的纯连接参数保存、纯连接移除使用 `config.set`,随后检查目标配置哈希、运行时已应用版本和实际保存的连接字段或条目缺失。其余变更继续使用 `config.apply`。 -保存时仅允许修改指定提供商的 `apiKey`、`baseUrl` 和 `api`,以及首次添加空模型目录和默认 `models.mode=merge`。移除时必须只删除目标提供商条目,保持其他提供商、默认模型、智能体和插件配置不变,并且剩余的默认/智能体模型配置不能继续引用该提供商。需要清理模型引用、编辑模型目录或插件、修改其他配置的操作,以及旧版本和需要重启的模式,不使用该路径。仍保留乐观并发校验、API Key 验证和外层 MXC 事务;响应丢失时不重放请求。 +保存时仅允许修改指定提供商的 `apiKey`、`baseUrl` 和 `api`,以及首次添加空模型目录和默认 `models.mode=merge`。移除时允许删除目标提供商条目,并清理它在默认模型、fallback 和模型目录中的引用;提交内容必须与设置界面的共享移除函数生成的配置完全一致,保持其他提供商和无关设置不变。任意选择替代模型、修改其他模型目录或插件,以及旧版本和需要重启的模式,不使用该路径。仍保留乐观并发校验、API Key 验证和外层 MXC 事务;响应丢失时不重放请求。 与当前 `config.apply` 相比,`config.set` 不执行同一组重启通知/回执后处理。Gateway 的配置验证和持久化仍通过官方写入路径进行,客户端必须确认运行时版本已生效后才能完成保存。日志的 `method=config.set` 可用于确认新路径已启用。 @@ -51,7 +51,7 @@ 同日 12:38 的移除样本仍使用原 `config.apply` 路径,RPC 耗时 **6.804 秒**。日志中的热更新区间为 12:38:17.515–12:38:21.219,耗时 **3.704 秒**;到 12:38:22.837 返回 RPC 又经过 **1.618 秒**。返回成功和 `status=hot-applied` 表明移除已生效;途中其他插件的来源告警不能据此认定为移除失败。 -轻量移除还会检查目标提供商键确实不存在;保留空对象 `{}` 或仅移除 API Key 都不能通过成功校验。如果此次操作同时清理默认模型、fallback 或智能体模型引用,将继续走原有完整流程。 +11 月 17 日的轻量移除仅覆盖未被引用的连接:检查目标提供商键确实不存在,保留空对象 `{}` 或仅移除 API Key 都不能通过成功校验。当时同时清理默认模型、fallback 或智能体模型引用的操作仍走原有完整流程。11 月 18 日扩大了覆盖范围,见下一节。 真实 Gateway 集成测试在保存对照之后交替执行 `config.apply`/`config.set` 移除,每次用同一轻量保存方式恢复模拟连接,验证删除已经生效、默认模型未改变且 Gateway 没有重启。移除指标记录在报告的 `removals` 数组中,各轮前两次删除作为预热。 @@ -62,4 +62,23 @@ 每轮每条路径各保留一个非预热删除样本,原始数据在 `build/reports/provider-save/remove-connection.config.apply.json` 和 `build/reports/provider-save/remove-connection.config.set.json`。两轮新路径均更快,但绝对耗时波动明显,不能保证固定时延,也不能将这些隔离样本直接当作前述生产操作的复测。轻量写入完成之后仍需等待热更新与版本确认;移除不会因为写入 RPC 较快返回就提前报告完成。 -保存/移除逻辑、MXC 状态处理、模型目录及设置界面的 204 项相关测试,以及两个真实 Gateway 场景通过。涉及默认模型或智能体引用清理的移除仍保留原路径。 +11 月 17 日保存/移除逻辑、MXC 状态处理、模型目录及设置界面的 204 项相关测试,以及两个真实 Gateway 场景通过。 + +## 11 月 18 日:移除当前默认提供商 + +当天 17:20(UTC+8)的 MiniMax 移除仍使用 `config.apply`。桌面日志的 MXC 生命周期区间为 17:20:03.093–17:20:17.924,共 **14.831 秒**;配置应用区间为 17:20:03.126–17:20:14.873,共 **11.747 秒**,随后 MXC 校验和恢复约 **3.051 秒**。这些是后端日志区间,不含确认弹窗和界面绘制。 + +移除前的配置备份显示 MiniMax 是当前默认模型,因此不符合之前“其他模型引用完全不变”的快速路径条件。现将 `removeProviderConnectionConfig` 放入前后端共用的纯函数模块,后端只对与该函数结果完全一致的移除启用 `config.set`,允许删除目标连接及它自己的模型引用。任意修改其他默认模型、工作区、提供商或插件的请求仍不能进入此路径。 + +成功确认除了提供商条目确实消失,还会核对保存的 `agents` 配置与请求一致,防止残留默认模型或 fallback 引用。仍等待运行时配置版本生效,并经过原有 MXC 事务;不会在收到写入回执时提前报告移除成功。 + +集成测试增加 `referencedRemovals` 指标:在计时区间外准备一个已选为默认、带 fallback 和模型目录引用的模拟 MiniMax 连接,再交替执行两种移除路径,并确认实际引用清理、运行时版本一致和 Gateway 未重启。 + +| 隔离测试轮次(排除预热) | 完整路径移除当前默认提供商 | 轻量路径移除当前默认提供商 | +| --- | ---: | ---: | +| 第一轮 | 6.966 秒 | 3.619 秒 | +| 第二轮 | 6.025 秒 | 3.360 秒 | + +原始数据为 `build/reports/provider-save/active-removal-20261118.config.apply.json` 和 `build/reports/provider-save/active-removal-20261118.config.set.json` 的 `referencedRemovals`。每轮每条路径仅一个非预热样本;以上只覆盖隔离网关中的配置写入和运行时生效确认,不含 MXC 校验、生产插件和界面耗时,不能与当天生产日志直接作端到端前后对照。 + +164 项相关单元/界面测试和两个真实 Gateway 场景通过;主进程及界面的类型检查、相关文件 lint、格式检查和构建通过。