Skip to content

Commit b82e3c1

Browse files
fix(gateway): hot-reload browser profile config (#93827)
* fix(gateway): hot-reload browser profile config * refactor(gateway): cache reload rule specificity * fix(gateway): pin reload policy to gateway registry --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 30e5dfc commit b82e3c1

5 files changed

Lines changed: 99 additions & 11 deletions

File tree

‎extensions/browser/index.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ function registerBrowserAutoEnableProbe(): BrowserAutoEnableProbe {
107107

108108
describe("browser plugin", () => {
109109
it("exposes static browser metadata on the plugin definition", () => {
110-
expect(browserPluginReload).toEqual({ restartPrefixes: ["browser"] });
110+
expect(browserPluginReload).toEqual({
111+
restartPrefixes: ["browser"],
112+
hotPrefixes: ["browser.profiles"],
113+
});
111114
expect(browserPluginNodeHostCommands).toHaveLength(1);
112115
expect(browserPluginNodeHostCommands[0]?.command).toBe("browser.proxy");
113116
expect(browserPluginNodeHostCommands[0]?.cap).toBe("browser");

‎extensions/browser/plugin-registration.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ function createBrowserToolOptions(ctx: OpenClawPluginToolContext): {
141141
}
142142

143143
/** Browser plugin reload policy. */
144-
export const browserPluginReload = { restartPrefixes: ["browser"] };
144+
export const browserPluginReload = {
145+
restartPrefixes: ["browser"],
146+
hotPrefixes: ["browser.profiles"],
147+
};
145148

146149
/** Node-host command descriptors exposed by the Browser plugin. */
147150
export const browserPluginNodeHostCommands: OpenClawPluginNodeHostCommand[] = [

‎src/gateway/config-reload-plan.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import { type ChannelId, listChannelPlugins } from "../channels/plugins/index.js";
44
import {
55
getActivePluginChannelRegistryVersion,
6-
getActivePluginRegistry,
7-
getActivePluginRegistryVersion,
6+
getActivePluginHttpRouteRegistry,
7+
getActivePluginHttpRouteRegistryVersion,
88
} from "../plugins/runtime.js";
99
import { isPlainObject } from "../utils.js";
1010

@@ -146,24 +146,26 @@ const BASE_RELOAD_RULES_TAIL: ReloadRule[] = [
146146
];
147147

148148
let cachedReloadRules: ReloadRule[] | null = null;
149-
let cachedRegistry: ReturnType<typeof getActivePluginRegistry> | null = null;
150-
let cachedActiveRegistryVersion = -1;
149+
let cachedRegistry: ReturnType<typeof getActivePluginHttpRouteRegistry> | null = null;
150+
let cachedGatewayRegistryVersion = -1;
151151
let cachedChannelRegistryVersion = -1;
152152

153153
function listReloadRules(): ReloadRule[] {
154-
const registry = getActivePluginRegistry();
155-
const activeRegistryVersion = getActivePluginRegistryVersion();
154+
// Reload metadata is Gateway policy. Agent-scoped registry activation must
155+
// not replace the pinned Gateway surface and silently change restart rules.
156+
const registry = getActivePluginHttpRouteRegistry();
157+
const gatewayRegistryVersion = getActivePluginHttpRouteRegistryVersion();
156158
const channelRegistryVersion = getActivePluginChannelRegistryVersion();
157159
// Plugin/channel reload rules are process-stable until the active registry
158160
// version changes; cache them to keep every config diff cheap.
159161
if (
160162
registry !== cachedRegistry ||
161-
activeRegistryVersion !== cachedActiveRegistryVersion ||
163+
gatewayRegistryVersion !== cachedGatewayRegistryVersion ||
162164
channelRegistryVersion !== cachedChannelRegistryVersion
163165
) {
164166
cachedReloadRules = null;
165167
cachedRegistry = registry;
166-
cachedActiveRegistryVersion = activeRegistryVersion;
168+
cachedGatewayRegistryVersion = gatewayRegistryVersion;
167169
cachedChannelRegistryVersion = channelRegistryVersion;
168170
}
169171
if (cachedReloadRules) {
@@ -229,6 +231,9 @@ function listReloadRules(): ReloadRule[] {
229231
...channelPluginStateRules,
230232
...BASE_RELOAD_RULES_TAIL,
231233
];
234+
// Narrow config contracts must override broad owner fallbacks. Sort once per
235+
// registry snapshot so the hot path can retain first-match semantics.
236+
rules.sort((a, b) => b.prefix.length - a.prefix.length);
232237
cachedReloadRules = rules;
233238
return rules;
234239
}

‎src/gateway/config-reload.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
import type { PluginInstallRecord } from "../config/types.plugins.js";
1313
import {
1414
pinActivePluginChannelRegistry,
15+
pinActivePluginHttpRouteRegistry,
1516
resetPluginRuntimeStateForTest,
1617
setActivePluginRegistry,
1718
} from "../plugins/runtime.js";
@@ -172,7 +173,7 @@ describe("buildGatewayReloadPlan", () => {
172173
{
173174
pluginId: "browser",
174175
pluginName: "Browser",
175-
registration: { restartPrefixes: ["browser"] },
176+
registration: { restartPrefixes: ["browser"], hotPrefixes: ["browser.profiles"] },
176177
source: "test",
177178
},
178179
{
@@ -214,6 +215,28 @@ describe("buildGatewayReloadPlan", () => {
214215
expect(plan.hotReasons).toStrictEqual([]);
215216
});
216217

218+
it("hot-reloads browser profile config under the broad browser restart rule", () => {
219+
const path = "browser.profiles.sandbox.cdpUrl";
220+
const plan = buildGatewayReloadPlan([path]);
221+
222+
expect(plan.restartGateway).toBe(false);
223+
expect(plan.restartReasons).toStrictEqual([]);
224+
expect(plan.hotReasons).toEqual([path]);
225+
expect(plan.noopPaths).toStrictEqual([]);
226+
expect(resolveConfigReloadMetadata(path).kind).toBe("hot");
227+
});
228+
229+
it("keeps Gateway reload policy when an agent activates a scoped registry", () => {
230+
pinActivePluginHttpRouteRegistry(registry);
231+
setActivePluginRegistry(emptyRegistry);
232+
233+
const path = "browser.profiles.sandbox.cdpUrl";
234+
expect(buildGatewayReloadPlan([path])).toMatchObject({
235+
restartGateway: false,
236+
hotReasons: [path],
237+
});
238+
});
239+
217240
it("restarts the Gmail watcher for hooks.gmail changes", () => {
218241
const plan = buildGatewayReloadPlan(["hooks.gmail.account"]);
219242
expect(plan.restartGateway).toBe(false);
@@ -513,6 +536,16 @@ describe("buildGatewayReloadPlan", () => {
513536
expectRestartGateway: true,
514537
expectRestartReason: "browser.enabled",
515538
},
539+
{
540+
path: "browser.defaultProfile",
541+
expectRestartGateway: true,
542+
expectRestartReason: "browser.defaultProfile",
543+
},
544+
{
545+
path: "browser.profiles.sandbox.cdpUrl",
546+
expectRestartGateway: false,
547+
expectHotPath: "browser.profiles.sandbox.cdpUrl",
548+
},
516549
{
517550
path: "gateway.channelHealthCheckMinutes",
518551
expectRestartGateway: false,

‎src/gateway/server-methods/config.shared-auth.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
55
import type { OpenClawConfig } from "../../config/types.openclaw.js";
66
import type { RestartSentinelPayload } from "../../infra/restart-sentinel.js";
7+
import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../../plugins/runtime.js";
8+
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
79
import {
810
createConfigHandlerHarness,
911
createConfigWriteSnapshot,
@@ -141,6 +143,19 @@ function hotReloadConfig(): OpenClawConfig {
141143
};
142144
}
143145

146+
function installBrowserReloadRegistry(): void {
147+
const registry = createTestRegistry([]);
148+
registry.reloads = [
149+
{
150+
pluginId: "browser",
151+
pluginName: "Browser",
152+
registration: { restartPrefixes: ["browser"], hotPrefixes: ["browser.profiles"] },
153+
source: "test",
154+
},
155+
];
156+
setActivePluginRegistry(registry);
157+
}
158+
144159
function mockPreviousConfig(config: OpenClawConfig): void {
145160
readConfigFileSnapshotForWriteMock.mockResolvedValue(createConfigWriteSnapshot(config));
146161
}
@@ -171,6 +186,7 @@ function expectNoDirectRestart(): void {
171186

172187
afterEach(() => {
173188
vi.clearAllMocks();
189+
resetPluginRuntimeStateForTest();
174190
});
175191

176192
beforeEach(() => {
@@ -382,6 +398,34 @@ describe("config shared auth disconnects", () => {
382398
expect(payload?.stats?.requiresRestart).toBe(false);
383399
});
384400

401+
it("does not schedule a direct restart for hot-mode browser profile config.patch writes", async () => {
402+
installBrowserReloadRegistry();
403+
mockPreviousConfig({
404+
...hotReloadConfig(),
405+
browser: {
406+
profiles: {
407+
sandbox: {
408+
cdpUrl: "http://127.0.0.1:9222",
409+
color: "#0066CC",
410+
},
411+
},
412+
},
413+
});
414+
415+
await runConfigPatch({
416+
browser: {
417+
profiles: {
418+
sandbox: {
419+
cdpUrl: "http://127.0.0.1:9223",
420+
color: "#0066CC",
421+
},
422+
},
423+
},
424+
});
425+
426+
expectNoDirectRestart();
427+
});
428+
385429
it("does not add an agent continuation from generic control-plane sessionKey params", async () => {
386430
mockPreviousConfig(hotReloadConfig());
387431

0 commit comments

Comments
 (0)