Skip to content

Commit 8eacffd

Browse files
committed
fix(plugins): preserve hook context mutations
1 parent 8a68ea0 commit 8eacffd

3 files changed

Lines changed: 91 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Docs: https://docs.openclaw.ai
103103

104104
### Fixes
105105

106+
- Plugins/hooks: preserve plugin hook context mutations while keeping `pluginConfig` scoped to the active handler, so `agent:bootstrap` plugins can add bootstrap files without leaking config. Fixes #75245. Thanks @xuruiray.
106107
- Providers/xAI: stop sending OpenAI-style reasoning effort controls to native Grok Responses models, so `xai/grok-4.3` no longer fails live Docker/Gateway runs with `Invalid reasoning effort`.
107108
- Providers/xAI: clamp the bundled xAI thinking profile to `off` so live Gateway runs cannot send unsupported reasoning levels to native Grok Responses models.
108109
- Discord/gateway: measure heartbeat ACK timeouts from the actual heartbeat send, preventing late initial heartbeats from triggering false reconnect loops while the channel is still awaiting readiness. Fixes #77668. (#78087) Thanks @bryce-d-greybeard and @NikolaFC.

‎src/plugins/loader.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "node:fs";
22
import path from "node:path";
33
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
44
import { listAgentHarnessIds } from "../agents/harness/registry.js";
5+
import type { WorkspaceBootstrapFile } from "../agents/workspace.js";
56
import { applyPluginAutoEnable } from "../config/plugin-auto-enable.js";
67
import {
78
clearRuntimeConfigSnapshot,
@@ -11,6 +12,7 @@ import { getContextEngineFactory, listContextEngineIds } from "../context-engine
1112
import {
1213
clearInternalHooks,
1314
createInternalHookEvent,
15+
type AgentBootstrapHookContext,
1416
getRegisteredEventKeys,
1517
triggerInternalHook,
1618
} from "../hooks/internal-hooks.js";
@@ -2185,6 +2187,81 @@ module.exports = { id: "throws-after-import", register() {} };`,
21852187
clearInternalHooks();
21862188
});
21872189

2190+
it("preserves plugin hook context mutations for agent bootstrap hooks", async () => {
2191+
useNoBundledPlugins();
2192+
const plugin = writePlugin({
2193+
id: "bootstrap-context-mutator",
2194+
filename: "bootstrap-context-mutator.cjs",
2195+
body: `module.exports = {
2196+
id: "bootstrap-context-mutator",
2197+
register(api) {
2198+
api.registerHook(
2199+
"agent:bootstrap",
2200+
(event) => {
2201+
const marker = event.context.pluginConfig?.marker;
2202+
if (marker !== "plugin-config-visible") {
2203+
throw new Error("plugin config missing from hook context");
2204+
}
2205+
event.context.bootstrapFiles = [
2206+
...event.context.bootstrapFiles,
2207+
{
2208+
name: "PLUGIN.md",
2209+
path: event.context.workspaceDir + "/PLUGIN.md",
2210+
content: marker,
2211+
missing: false,
2212+
},
2213+
];
2214+
},
2215+
{ name: "bootstrap-context-mutator" },
2216+
);
2217+
},
2218+
};`,
2219+
});
2220+
updatePluginManifest(plugin, {
2221+
configSchema: { type: "object" },
2222+
});
2223+
2224+
clearInternalHooks();
2225+
2226+
loadOpenClawPlugins({
2227+
cache: false,
2228+
workspaceDir: plugin.dir,
2229+
config: {
2230+
plugins: {
2231+
load: { paths: [plugin.file] },
2232+
allow: ["bootstrap-context-mutator"],
2233+
entries: {
2234+
"bootstrap-context-mutator": {
2235+
config: {
2236+
marker: "plugin-config-visible",
2237+
},
2238+
},
2239+
},
2240+
},
2241+
},
2242+
onlyPluginIds: ["bootstrap-context-mutator"],
2243+
});
2244+
2245+
const baseFile: WorkspaceBootstrapFile = {
2246+
name: "AGENTS.md",
2247+
path: path.join(plugin.dir, "AGENTS.md"),
2248+
content: "base",
2249+
missing: false,
2250+
};
2251+
const event = createInternalHookEvent("agent", "bootstrap", "agent:main:test:dm:peer", {
2252+
workspaceDir: plugin.dir,
2253+
bootstrapFiles: [baseFile],
2254+
});
2255+
await triggerInternalHook(event);
2256+
2257+
const context = event.context as AgentBootstrapHookContext & { pluginConfig?: unknown };
2258+
expect(context.bootstrapFiles.map((file) => file.name)).toEqual(["AGENTS.md", "PLUGIN.md"]);
2259+
expect(context.bootstrapFiles[1]?.content).toBe("plugin-config-visible");
2260+
expect("pluginConfig" in context).toBe(false);
2261+
2262+
clearInternalHooks();
2263+
});
2264+
21882265
it("rolls back global side effects when registration fails", async () => {
21892266
useNoBundledPlugins();
21902267
const plugin = writePlugin({

‎src/plugins/registry.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,19 @@ export function createPluginRegistry(registryParams: PluginRegistryParams) {
657657
}> = [];
658658
for (const event of normalizedEvents) {
659659
const wrappedHandler: typeof handler = async (evt) => {
660-
// Shallow-copy to avoid mutating the shared event object
661-
// passed to all handlers sequentially by triggerInternalHook
662-
return handler({ ...evt, context: { ...evt.context, pluginConfig } });
660+
const { context } = evt;
661+
const hadPluginConfig = Object.prototype.hasOwnProperty.call(context, "pluginConfig");
662+
const previousPluginConfig = context.pluginConfig;
663+
context.pluginConfig = pluginConfig;
664+
try {
665+
return await handler({ ...evt, context });
666+
} finally {
667+
if (hadPluginConfig) {
668+
context.pluginConfig = previousPluginConfig;
669+
} else {
670+
delete context.pluginConfig;
671+
}
672+
}
663673
};
664674
registerInternalHook(event, wrappedHandler);
665675
nextRegistrations.push({ event, handler: wrappedHandler });

0 commit comments

Comments
 (0)