Files
openclaw/src/plugins/hook-runner-global.ts
w33d dd149f01ba fix: stateful plugin hooks and tools stay aligned after scoped loads (#108110)
* fix(plugins): align hook and tool registrations

Reuse gateway-owned plugin registrations for matching hooks and tools while loading only missing tool owners from narrower runtime scopes.\n\nCo-authored-by: w33d <w33d@steadholme.local>

* test(plugins): cover mixed registry tool owners

Verify gateway-pinned and compatible active registrations compose without another plugin load.

Co-authored-by: w33d <w33d@steadholme.local>

* refactor(plugins): keep pinned registry lookup internal

Reuse the existing runtime-state contract so hook and tool ownership does not expand the Plugin SDK surface or treat unpinned active registries as Gateway owners.

* test(plugins): complete hook context fixture

Supply the required tool name in the stateful hook ownership regression context.

* chore: leave contributor release note in PR

Normal contributor PRs do not modify the release-owned changelog; the PR body retains the release note and attribution.

* fix(plugins): align partial registry owners

* fix(plugins): preserve scoped tool diagnostics

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
Co-authored-by: w33d <w33d@steadholme.local>
2026-07-15 07:12:43 -07:00

114 lines
3.9 KiB
TypeScript

/**
* Global Plugin Hook Runner
*
* Singleton hook runner that's initialized when plugins are loaded
* and can be called from anywhere in the codebase.
*
* The runner is created once and resolves hooks live on every dispatch from a
* composed view of the registries that are currently live: an explicitly
* initialized SDK registry, the pinned channel registry, the active registry,
* and other pinned surfaces. Freezing one registry caused scoped mid-run activations (harness
* and memory ensures) to rebind the runner to a narrow registry and silently
* drop other plugins' tool-call hooks (#91918). Composing live also preserves
* the older contract that hooks pushed into a registry after initialization
* (e.g. the SDK `addTestHook` helper) dispatch immediately.
*/
import { createSubsystemLogger } from "../logging/subsystem.js";
import type { GlobalHookRunnerRegistry } from "./hook-registry.types.js";
import {
createComposedHookRegistryFacade,
getHookRunnerGlobalState,
} from "./hook-runner-global-state.js";
import type { PluginHookGatewayContext, PluginHookGatewayStopEvent } from "./hook-types.js";
import { createHookRunner, type HookRunner } from "./hooks.js";
const getLog = () => createSubsystemLogger("plugins");
/**
* Initialize the global hook runner with a plugin registry.
* Called on every plugin registry activation and by SDK consumers. The runner
* instance stays stable so references captured mid-run keep seeing current
* hooks. An isolated SDK registry stays authoritative; runtime registries use
* the gateway surface precedence shared by plugin tool resolution.
*/
export function initializeGlobalHookRunner(registry: GlobalHookRunnerRegistry): void {
const state = getHookRunnerGlobalState();
const log = getLog();
state.registry = registry;
if (!state.hookRunner) {
state.hookRunner = createHookRunner(createComposedHookRegistryFacade(state), {
logger: {
debug: (msg) => log.debug(msg),
warn: (msg) => log.warn(msg),
error: (msg) => log.error(msg),
},
catchErrors: true,
failurePolicyByHook: {
before_agent_run: "fail-closed",
before_install: "fail-closed",
before_tool_call: "fail-closed",
},
});
}
const hookCount = registry.hooks.length;
if (hookCount > 0) {
log.debug(`hook runner initialized with ${hookCount} registered hooks`);
}
}
/**
* Get the global hook runner.
* Returns null if plugins haven't been loaded yet.
*/
export function getGlobalHookRunner(): HookRunner | null {
return getHookRunnerGlobalState().hookRunner;
}
/**
* Get the registry from the most recent activation or explicit initialization.
* Returns null if plugins haven't been loaded yet. Hook dispatch does not use
* this single registry; the runner resolves hooks from the live composed view.
*/
export function getGlobalPluginRegistry(): GlobalHookRunnerRegistry | null {
return getHookRunnerGlobalState().registry;
}
/**
* Check if any hooks are registered for a given hook name.
*/
export function hasGlobalHooks(hookName: Parameters<HookRunner["hasHooks"]>[0]): boolean {
return getHookRunnerGlobalState().hookRunner?.hasHooks(hookName) ?? false;
}
export async function runGlobalGatewayStopSafely(params: {
event: PluginHookGatewayStopEvent;
ctx: PluginHookGatewayContext;
onError?: (err: unknown) => void;
}): Promise<void> {
const log = getLog();
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("gateway_stop")) {
return;
}
try {
await hookRunner.runGatewayStop(params.event, params.ctx);
} catch (err) {
if (params.onError) {
params.onError(err);
return;
}
log.warn(`gateway_stop hook failed: ${String(err)}`);
}
}
/**
* Reset the global hook runner (for testing).
*/
export function resetGlobalHookRunner(): void {
const state = getHookRunnerGlobalState();
state.hookRunner = null;
state.registry = null;
}