@@ -3,6 +3,7 @@ import { AsyncLocalStorage } from "node:async_hooks";
33import { filterStringEntries } from "@openclaw/normalization-core/string-normalization";
44import type { OpenClawConfig } from "../config/types.openclaw.js";
55import { createSubsystemLogger } from "../logging/subsystem.js";
6+ import { normalizePluginsConfig, resolveEffectivePluginActivationState } from "./config-state.js";
67import type {
78 MemoryCorpusSupplement,
89 MemoryCorpusSupplementRegistration,
@@ -18,6 +19,7 @@ import type {
1819 MemoryPromptSupplementRegistration,
1920 PreparedMemoryPromptSection,
2021} from "./registry-contribution-types.js";
22+ import type { PluginRegistry } from "./registry-types.js";
2123import { requireActivePluginRegistry, resolveDirectPluginRegistrationOwner } from "./runtime.js";
2224
2325const log = createSubsystemLogger("plugins/memory-state");
@@ -100,6 +102,79 @@ export function getMemoryCapabilityRegistration(): MemoryPluginCapabilityRegistr
100102export function listMemoryCorpusSupplements(): MemoryCorpusSupplementRegistration[] {
101103 return [...requireActivePluginRegistry().memoryCorpusSupplements];
102104}
105+
106+ function adoptEligibleRuntimeMemoryRegistrations<T extends { pluginId: string }>(
107+ target: T[],
108+ runtime: readonly T[],
109+ canAdopt: (pluginId: string) => boolean,
110+ ): T[] {
111+ const pluginIds = new Set(target.map((registration) => registration.pluginId));
112+ let adopted: T[] | undefined;
113+ for (const registration of runtime) {
114+ if (pluginIds.has(registration.pluginId) || !canAdopt(registration.pluginId)) {
115+ continue;
116+ }
117+ (adopted ??= [...target]).push(registration);
118+ pluginIds.add(registration.pluginId);
119+ }
120+ return adopted ?? target;
121+ }
122+
123+ /**
124+ * Discovery scopes cannot safely rerun full memory plugin setup.
125+ * Reuse exact root sidecars only while activation and source ownership still match.
126+ */
127+ export function adoptRuntimeMemoryRegistrations(
128+ targetRegistry: PluginRegistry,
129+ runtimeRegistry: PluginRegistry,
130+ config: OpenClawConfig,
131+ ): PluginRegistry {
132+ const normalizedConfig = normalizePluginsConfig(config.plugins);
133+ const canAdopt = (pluginId: string) => {
134+ const targetOwner = targetRegistry.plugins.find((plugin) => plugin.id === pluginId);
135+ const runtimeOwner = runtimeRegistry.plugins.find((plugin) => plugin.id === pluginId);
136+ if (
137+ runtimeOwner?.status !== "loaded" ||
138+ !resolveEffectivePluginActivationState({
139+ id: runtimeOwner.id,
140+ origin: runtimeOwner.origin,
141+ config: normalizedConfig,
142+ rootConfig: config,
143+ enabledByDefault: runtimeOwner.activationSource === "default",
144+ }).enabled ||
145+ (targetOwner &&
146+ (targetOwner.status !== "loaded" || targetOwner.source !== runtimeOwner.source))
147+ ) {
148+ return false;
149+ }
150+ return true;
151+ };
152+ const memoryCorpusSupplements = adoptEligibleRuntimeMemoryRegistrations(
153+ targetRegistry.memoryCorpusSupplements,
154+ runtimeRegistry.memoryCorpusSupplements,
155+ canAdopt,
156+ );
157+ const memoryPromptPreparations = adoptEligibleRuntimeMemoryRegistrations(
158+ targetRegistry.memoryPromptPreparations,
159+ runtimeRegistry.memoryPromptPreparations,
160+ canAdopt,
161+ );
162+ const memoryPromptSupplements = adoptEligibleRuntimeMemoryRegistrations(
163+ targetRegistry.memoryPromptSupplements,
164+ runtimeRegistry.memoryPromptSupplements,
165+ canAdopt,
166+ );
167+ return memoryCorpusSupplements === targetRegistry.memoryCorpusSupplements &&
168+ memoryPromptPreparations === targetRegistry.memoryPromptPreparations &&
169+ memoryPromptSupplements === targetRegistry.memoryPromptSupplements
170+ ? targetRegistry
171+ : {
172+ ...targetRegistry,
173+ memoryCorpusSupplements,
174+ memoryPromptPreparations,
175+ memoryPromptSupplements,
176+ };
177+ }
103178export function registerMemoryPromptSupplement(
104179 requestedPluginId: string,
105180 builder: MemoryPromptSectionBuilder,
0 commit comments