fix: share context engine registry across bundled chunks (#40115)

Merged via squash.

Prepared head SHA: 6af4820b7d0ea64d96f2f894ef3b0e5750b776aa
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
This commit is contained in:
Josh Lehman
2026-03-08 11:56:01 -07:00
committed by GitHub
parent 9914b48c57
commit 4bfa800cc7
3 changed files with 37 additions and 5 deletions

View File

@@ -28,6 +28,7 @@ Docs: https://docs.openclaw.ai
- Sessions/model switch: clear stale cached `contextTokens` when a session changes models so status and runtime paths recompute against the active model window. (#38044) thanks @yuweuii.
- ACP/session history: persist transcripts for successful ACP child runs, preserve exact transcript text, record ACP spawned-session lineage, and keep spawn-time transcript-path persistence best-effort so history storage failures do not block execution. (#40137) thanks @mbelinky.
- Browser/CDP: normalize loopback direct WebSocket CDP URLs back to HTTP(S) for `/json/*` tab operations so local `ws://` / `wss://` profiles can still list, focus, open, and close tabs after the new direct-WS support lands. (#31085) Thanks @shrey150.
- Context engine registry/bundled builds: share the registry state through a `globalThis` singleton so duplicated bundled module copies can resolve engines registered by each other at runtime, with regression coverage for duplicate-module imports. (#40115) thanks @jalehman.
## 2026.3.7

View File

@@ -198,6 +198,19 @@ describe("Registry tests", () => {
expect(getContextEngineFactory("reg-overwrite")).toBe(factory2);
expect(getContextEngineFactory("reg-overwrite")).not.toBe(factory1);
});
it("shares registered engines across duplicate module copies", async () => {
const registryUrl = new URL("./registry.ts", import.meta.url).href;
const suffix = Date.now().toString(36);
const first = await import(/* @vite-ignore */ `${registryUrl}?copy=${suffix}-a`);
const second = await import(/* @vite-ignore */ `${registryUrl}?copy=${suffix}-b`);
const engineId = `dup-copy-${suffix}`;
const factory = () => new MockContextEngine();
first.registerContextEngine(engineId, factory);
expect(second.getContextEngineFactory(engineId)).toBe(factory);
});
});
// ═══════════════════════════════════════════════════════════════════════════

View File

@@ -12,27 +12,45 @@ export type ContextEngineFactory = () => ContextEngine | Promise<ContextEngine>;
// Registry (module-level singleton)
// ---------------------------------------------------------------------------
const _engines = new Map<string, ContextEngineFactory>();
const CONTEXT_ENGINE_REGISTRY_STATE = Symbol.for("openclaw.contextEngineRegistryState");
type ContextEngineRegistryState = {
engines: Map<string, ContextEngineFactory>;
};
// Keep context-engine registrations process-global so duplicated dist chunks
// still share one registry map at runtime.
function getContextEngineRegistryState(): ContextEngineRegistryState {
const globalState = globalThis as typeof globalThis & {
[CONTEXT_ENGINE_REGISTRY_STATE]?: ContextEngineRegistryState;
};
if (!globalState[CONTEXT_ENGINE_REGISTRY_STATE]) {
globalState[CONTEXT_ENGINE_REGISTRY_STATE] = {
engines: new Map<string, ContextEngineFactory>(),
};
}
return globalState[CONTEXT_ENGINE_REGISTRY_STATE];
}
/**
* Register a context engine implementation under the given id.
*/
export function registerContextEngine(id: string, factory: ContextEngineFactory): void {
_engines.set(id, factory);
getContextEngineRegistryState().engines.set(id, factory);
}
/**
* Return the factory for a registered engine, or undefined.
*/
export function getContextEngineFactory(id: string): ContextEngineFactory | undefined {
return _engines.get(id);
return getContextEngineRegistryState().engines.get(id);
}
/**
* List all registered engine ids.
*/
export function listContextEngineIds(): string[] {
return [..._engines.keys()];
return [...getContextEngineRegistryState().engines.keys()];
}
// ---------------------------------------------------------------------------
@@ -55,7 +73,7 @@ export async function resolveContextEngine(config?: OpenClawConfig): Promise<Con
? slotValue.trim()
: defaultSlotIdForKey("contextEngine");
const factory = _engines.get(engineId);
const factory = getContextEngineRegistryState().engines.get(engineId);
if (!factory) {
throw new Error(
`Context engine "${engineId}" is not registered. ` +