fix(browser): hot-reload profiles added after gateway start (#4841) (#8816)

* fix(browser): hot-reload profiles added after gateway start (#4841)

* style: format files with oxfmt

* Fix hot-reload stale config fields bug in forProfile

* Fix test order-dependency in hot-reload profiles test

* Fix mock reset order to prevent stale cfgProfiles

* Fix config cache blocking hot-reload by clearing cache before loadConfig

* test: improve hot-reload test to properly exercise config cache

- Add simulated cache behavior in mock
- Prime cache before mutating config
- Verify stale value without clearConfigCache
- Verify fresh value after hot-reload

Addresses review comment about test not exercising cache

* test: add hot-reload tests for browser profiles in server context.

* fix(browser): optimize profile hot-reload to avoid global cache clear

* fix(browser): remove unused loadConfig import

* fix(test): execute resetModules before test setup

* feat: implement browser server context with profile hot-reloading and tab management.

* fix(browser): harden profile hot-reload and shutdown cleanup

* test(browser): use toSorted in known-profile names test

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
Jessy LANGE
2026-02-14 00:44:04 +01:00
committed by GitHub
parent d5e25e0ad8
commit 3bda3df729
8 changed files with 331 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ import fs from "node:fs";
import type { ResolvedBrowserProfile } from "./config.js";
import type { PwAiModule } from "./pw-ai-module.js";
import type {
BrowserServerState,
BrowserRouteContext,
BrowserTab,
ContextOptions,
@@ -9,6 +10,7 @@ import type {
ProfileRuntimeState,
ProfileStatus,
} from "./server-context.types.js";
import { createConfigIO, loadConfig } from "../config/config.js";
import { appendCdpPath, createTargetViaCdp, getHeadersWithAuth, normalizeCdpWsUrl } from "./cdp.js";
import {
isChromeCdpReady,
@@ -17,7 +19,7 @@ import {
resolveOpenClawUserDataDir,
stopOpenClawChrome,
} from "./chrome.js";
import { resolveProfile } from "./config.js";
import { resolveBrowserConfig, resolveProfile } from "./config.js";
import {
ensureChromeExtensionRelayServer,
stopChromeExtensionRelayServer,
@@ -35,6 +37,14 @@ export type {
ProfileStatus,
} from "./server-context.types.js";
export function listKnownProfileNames(state: BrowserServerState): string[] {
const names = new Set(Object.keys(state.resolved.profiles));
for (const name of state.profiles.keys()) {
names.add(name);
}
return [...names];
}
/**
* Normalize a CDP WebSocket URL to use the correct base URL.
*/
@@ -559,6 +569,8 @@ function createProfileContext(
}
export function createBrowserRouteContext(opts: ContextOptions): BrowserRouteContext {
const refreshConfigFromDisk = opts.refreshConfigFromDisk === true;
const state = () => {
const current = opts.getState();
if (!current) {
@@ -567,10 +579,53 @@ export function createBrowserRouteContext(opts: ContextOptions): BrowserRouteCon
return current;
};
const applyResolvedConfig = (
current: BrowserServerState,
freshResolved: BrowserServerState["resolved"],
) => {
current.resolved = freshResolved;
for (const [name, runtime] of current.profiles) {
const nextProfile = resolveProfile(freshResolved, name);
if (nextProfile) {
runtime.profile = nextProfile;
continue;
}
if (!runtime.running) {
current.profiles.delete(name);
}
}
};
const refreshResolvedConfig = (current: BrowserServerState) => {
if (!refreshConfigFromDisk) {
return;
}
const cfg = loadConfig();
const freshResolved = resolveBrowserConfig(cfg.browser, cfg);
applyResolvedConfig(current, freshResolved);
};
const refreshResolvedConfigFresh = (current: BrowserServerState) => {
if (!refreshConfigFromDisk) {
return;
}
const freshCfg = createConfigIO().loadConfig();
const freshResolved = resolveBrowserConfig(freshCfg.browser, freshCfg);
applyResolvedConfig(current, freshResolved);
};
const forProfile = (profileName?: string): ProfileContext => {
const current = state();
refreshResolvedConfig(current);
const name = profileName ?? current.resolved.defaultProfile;
const profile = resolveProfile(current.resolved, name);
let profile = resolveProfile(current.resolved, name);
// Hot-reload: try fresh config if profile not found
if (!profile) {
refreshResolvedConfigFresh(current);
profile = resolveProfile(current.resolved, name);
}
if (!profile) {
const available = Object.keys(current.resolved.profiles).join(", ");
throw new Error(`Profile "${name}" not found. Available profiles: ${available || "(none)"}`);
@@ -580,6 +635,7 @@ export function createBrowserRouteContext(opts: ContextOptions): BrowserRouteCon
const listProfiles = async (): Promise<ProfileStatus[]> => {
const current = state();
refreshResolvedConfig(current);
const result: ProfileStatus[] = [];
for (const name of Object.keys(current.resolved.profiles)) {