fix(hooks): preserve plugin-registered hooks across gateway startup reload#26242
Closed
Sid-Qin wants to merge 2 commits intoopenclaw:mainfrom
Closed
fix(hooks): preserve plugin-registered hooks across gateway startup reload#26242Sid-Qin wants to merge 2 commits intoopenclaw:mainfrom
Sid-Qin wants to merge 2 commits intoopenclaw:mainfrom
Conversation
…eload Plugins register internal hooks during loadGatewayPlugins(), but startGatewaySidecars() later calls clearInternalHooks() to reload file-based hook definitions, which wiped plugin handlers. Hooks registered via api.registerHook() appeared in `openclaw hooks list` but never fired. Introduce registerPluginHook() that tracks handlers separately. clearInternalHooks() now restores plugin handlers after clearing, so they survive the gateway startup reload cycle. Closes openclaw#25859 Co-authored-by: Cursor <[email protected]>
Contributor
Additional Comments (1)
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/hooks/internal-hooks.ts
Line: 163-178
Comment:
`unregisterInternalHook` doesn't remove handlers from the `pluginHandlers` map. If a plugin hook is unregistered, it will be removed from `handlers` but restored on the next `clearInternalHooks()` call.
```suggestion
export function unregisterInternalHook(eventKey: string, handler: InternalHookHandler): void {
const eventHandlers = handlers.get(eventKey);
if (!eventHandlers) {
return;
}
const index = eventHandlers.indexOf(handler);
if (index !== -1) {
eventHandlers.splice(index, 1);
}
// Also remove from pluginHandlers if present
const pluginEventHandlers = pluginHandlers.get(eventKey);
if (pluginEventHandlers) {
const pluginIndex = pluginEventHandlers.indexOf(handler);
if (pluginIndex !== -1) {
pluginEventHandlers.splice(pluginIndex, 1);
}
if (pluginEventHandlers.length === 0) {
pluginHandlers.delete(eventKey);
}
}
// Clean up empty handler arrays
if (eventHandlers.length === 0) {
handlers.delete(eventKey);
}
}
```
How can I resolve this? If you propose a fix, please make it concise. |
Co-authored-by: Cursor <[email protected]>
This was referenced Mar 1, 2026
Contributor
|
Closing as duplicate of #29515. Same startup hook-preservation fix path; consolidating review and merge discussion into one PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
api.registerHook("message:received", handler)appear inopenclaw hooks listbut never fire. The handler is registered during plugin load, then wiped byclearInternalHooks()during gateway sidecar startup.api.registerHook()for internal hooks (e.g.,message:received,session:new) is silently broken — the hook appears registered but never triggers.registerPluginHook()that tracks plugin-sourced handlers separately.clearInternalHooks()now restores plugin handlers after clearing, so they survive the gateway startup reload cycle.api.on()(typed plugin hooks) is unaffected — it uses a separate registry. File-based HOOK.md hooks are still cleared and reloaded as before.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
User-visible / Behavior Changes
Plugin hooks registered via
api.registerHook()now correctly fire on matching events. Previously they were silently cleared during gateway startup.Security Impact (required)
NoNoNoNoNoRepro + Verification
Environment
Steps
api.registerHook("message:received", handler)Expected
Actual (before fix)
openclaw hooks listbut handler never firesEvidence
"should preserve plugin-registered handlers"ininternal-hooks.test.tsHuman Verification (required)
clearInternalHooks(); regular hooks are still cleared; plugin hooks fire after gateway startup reloadCompatibility / Migration
YesNoNoFailure Recovery (if this breaks)
src/hooks/internal-hooks.ts,src/plugins/registry.tsRisks and Mitigations
clearInternalHooksis called without a full process restart.loadGatewayPlugins()at process start. Hot-reload scenarios re-run the full startup sequence including plugin loading.Greptile Summary
Fixes plugin hooks being silently cleared during gateway startup by introducing separate tracking for plugin-registered handlers.
Key changes:
pluginHandlersmap to track plugin-sourced hook handlers separately from config-based hooksregisterPluginHook()registers handlers in bothhandlersandpluginHandlersmapsclearInternalHooks()now preserves plugin handlers by restoring them after clearingregisterPluginHook()instead ofregisterInternalHook()Issue found:
unregisterInternalHook()doesn't clean up thepluginHandlersmap, which could cause unregistered plugin hooks to be incorrectly restored on the nextclearInternalHooks()callConfidence Score: 4/5
unregisterInternalHook()doesn't clean up thepluginHandlersmap, which could cause unregistered plugin hooks to be incorrectly restored. This is unlikely to be hit in practice since plugins are loaded once and not dynamically unloaded, but should be fixed for correctness.src/hooks/internal-hooks.ts- theunregisterInternalHookfunction needs to also clean up thepluginHandlersmapLast reviewed commit: ee2530d