From fdd7468c202ca389ca1d6e7dcf33199869049e98 Mon Sep 17 00:00:00 2001 From: Josh Lehman Date: Sun, 8 Mar 2026 10:25:44 -0700 Subject: [PATCH] fix: share context engine registry across bundled chunks --- src/context-engine/context-engine.test.ts | 13 +++++++++++ src/context-engine/registry.ts | 28 +++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/context-engine/context-engine.test.ts b/src/context-engine/context-engine.test.ts index e6788d2f86c..91b9ffac524 100644 --- a/src/context-engine/context-engine.test.ts +++ b/src/context-engine/context-engine.test.ts @@ -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); + }); }); // ═══════════════════════════════════════════════════════════════════════════ diff --git a/src/context-engine/registry.ts b/src/context-engine/registry.ts index 49bf34bfbb3..d73266c62de 100644 --- a/src/context-engine/registry.ts +++ b/src/context-engine/registry.ts @@ -12,27 +12,45 @@ export type ContextEngineFactory = () => ContextEngine | Promise; // Registry (module-level singleton) // --------------------------------------------------------------------------- -const _engines = new Map(); +const CONTEXT_ENGINE_REGISTRY_STATE = Symbol.for("openclaw.contextEngineRegistryState"); + +type ContextEngineRegistryState = { + engines: Map; +}; + +// 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(), + }; + } + 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