diff --git a/src/hooks/internal-hooks.ts b/src/hooks/internal-hooks.ts index 4a29acb8b..08a642a17 100644 --- a/src/hooks/internal-hooks.ts +++ b/src/hooks/internal-hooks.ts @@ -200,8 +200,23 @@ export interface InternalHookEvent { export type InternalHookHandler = (event: InternalHookEvent) => Promise | void; -/** Registry of hook handlers by event key */ -const handlers = new Map(); +/** + * Registry of hook handlers by event key. + * + * Uses a globalThis singleton so that registerInternalHook and + * triggerInternalHook always share the same Map even when the bundler + * emits multiple copies of this module into separate chunks (bundle + * splitting). Without the singleton, handlers registered in one chunk + * are invisible to triggerInternalHook in another chunk, causing hooks + * to silently fire with zero handlers. + */ +const _g = globalThis as typeof globalThis & { + __openclaw_internal_hook_handlers__?: Map; +}; +if (!_g.__openclaw_internal_hook_handlers__) { + _g.__openclaw_internal_hook_handlers__ = new Map(); +} +const handlers = _g.__openclaw_internal_hook_handlers__; const log = createSubsystemLogger("internal-hooks"); /**