fix(plugins): restore memory prompt builder on cached plugin loads

This commit is contained in:
Jari Mustonen 2026-03-09 15:42:51 +02:00 committed by Josh Lehman
parent 586326158e
commit df5f2cb77f
No known key found for this signature in database
GPG Key ID: D141B425AC7F876B
3 changed files with 32 additions and 9 deletions

View File

@ -23,6 +23,16 @@ export function buildMemoryPromptSection(params: {
return _builder?.(params) ?? [];
}
/** Return the current builder (used by the plugin cache to snapshot state). */
export function getMemoryPromptSectionBuilder(): MemoryPromptSectionBuilder | undefined {
return _builder;
}
/** Restore a previously-snapshotted builder (used on plugin cache hits). */
export function restoreMemoryPromptSection(builder: MemoryPromptSectionBuilder | undefined): void {
_builder = builder;
}
/** Clear the registered builder (called on plugin reload and in tests). */
export function clearMemoryPromptSection(): void {
_builder = undefined;

View File

@ -9,8 +9,12 @@ import type { PluginInstallRecord } from "../config/types.plugins.js";
import type { GatewayRequestHandler } from "../gateway/server-methods/types.js";
import { openBoundaryFileSync } from "../infra/boundary-file-read.js";
import { createSubsystemLogger } from "../logging/subsystem.js";
import {
clearMemoryPromptSection,
getMemoryPromptSectionBuilder,
restoreMemoryPromptSection,
} from "../memory/prompt-section.js";
import { resolveUserPath } from "../utils.js";
import { clearMemoryPromptSection } from "../memory/prompt-section.js";
import { inspectBundleMcpRuntimeSupport } from "./bundle-mcp.js";
import { clearPluginCommands } from "./commands.js";
import {
@ -91,8 +95,13 @@ export class PluginLoadFailureError extends Error {
}
}
type CachedPluginState = {
registry: PluginRegistry;
memoryPromptBuilder: ReturnType<typeof getMemoryPromptSectionBuilder>;
};
const MAX_PLUGIN_REGISTRY_CACHE_ENTRIES = 128;
const registryCache = new Map<string, PluginRegistry>();
const registryCache = new Map<string, CachedPluginState>();
const openAllowlistWarningCache = new Set<string>();
const LAZY_RUNTIME_REFLECTION_KEYS = [
"version",
@ -180,7 +189,7 @@ export const __testing = {
maxPluginRegistryCacheEntries: MAX_PLUGIN_REGISTRY_CACHE_ENTRIES,
};
function getCachedPluginRegistry(cacheKey: string): PluginRegistry | undefined {
function getCachedPluginRegistry(cacheKey: string): CachedPluginState | undefined {
const cached = registryCache.get(cacheKey);
if (!cached) {
return undefined;
@ -191,11 +200,11 @@ function getCachedPluginRegistry(cacheKey: string): PluginRegistry | undefined {
return cached;
}
function setCachedPluginRegistry(cacheKey: string, registry: PluginRegistry): void {
function setCachedPluginRegistry(cacheKey: string, state: CachedPluginState): void {
if (registryCache.has(cacheKey)) {
registryCache.delete(cacheKey);
}
registryCache.set(cacheKey, registry);
registryCache.set(cacheKey, state);
while (registryCache.size > MAX_PLUGIN_REGISTRY_CACHE_ENTRIES) {
const oldestKey = registryCache.keys().next().value;
if (!oldestKey) {
@ -734,10 +743,11 @@ export function loadOpenClawPlugins(options: PluginLoadOptions = {}): PluginRegi
if (cacheEnabled) {
const cached = getCachedPluginRegistry(cacheKey);
if (cached) {
restoreMemoryPromptSection(cached.memoryPromptBuilder);
if (shouldActivate) {
activatePluginRegistry(cached, cacheKey);
activatePluginRegistry(cached.registry, cacheKey);
}
return cached;
return cached.registry;
}
}
@ -1289,7 +1299,10 @@ export function loadOpenClawPlugins(options: PluginLoadOptions = {}): PluginRegi
maybeThrowOnPluginLoadError(registry, options.throwOnLoadError);
if (cacheEnabled) {
setCachedPluginRegistry(cacheKey, registry);
setCachedPluginRegistry(cacheKey, {
registry,
memoryPromptBuilder: getMemoryPromptSectionBuilder(),
});
}
if (shouldActivate) {
activatePluginRegistry(registry, cacheKey);

View File

@ -2,13 +2,13 @@ import path from "node:path";
import type { AnyAgentTool } from "../agents/tools/common.js";
import type { ChannelPlugin } from "../channels/plugins/types.js";
import { registerContextEngineForOwner } from "../context-engine/registry.js";
import { registerMemoryPromptSection } from "../memory/prompt-section.js";
import type {
GatewayRequestHandler,
GatewayRequestHandlers,
} from "../gateway/server-methods/types.js";
import { registerInternalHook } from "../hooks/internal-hooks.js";
import type { HookEntry } from "../hooks/types.js";
import { registerMemoryPromptSection } from "../memory/prompt-section.js";
import { resolveUserPath } from "../utils.js";
import { registerPluginCommand, validatePluginCommandDefinition } from "./commands.js";
import { normalizePluginHttpPath } from "./http-path.js";