@@ -103,25 +103,36 @@ export function listMemoryCorpusSupplements(): MemoryCorpusSupplementRegistratio
103103 return [ ...requireActivePluginRegistry ( ) . memoryCorpusSupplements ] ;
104104}
105105
106- /** Copies root-owned corpus supplements into a same-workspace scoped registry. */
107- export function adoptRuntimeMemoryCorpusSupplementRegistrations (
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 (
108128 targetRegistry : PluginRegistry ,
109129 runtimeRegistry : PluginRegistry ,
110130 config : OpenClawConfig ,
111131) : PluginRegistry {
112132 const normalizedConfig = normalizePluginsConfig ( config . plugins ) ;
113- const supplements = [ ...targetRegistry . memoryCorpusSupplements ] ;
114- let changed = false ;
115- for ( const registration of runtimeRegistry . memoryCorpusSupplements ) {
116- if ( supplements . some ( ( candidate ) => candidate . pluginId === registration . pluginId ) ) {
117- continue ;
118- }
119- const targetOwner = targetRegistry . plugins . find (
120- ( plugin ) => plugin . id === registration . pluginId ,
121- ) ;
122- const runtimeOwner = runtimeRegistry . plugins . find (
123- ( plugin ) => plugin . id === registration . pluginId ,
124- ) ;
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 ) ;
125136 if (
126137 runtimeOwner ?. status !== "loaded" ||
127138 ! resolveEffectivePluginActivationState ( {
@@ -134,12 +145,35 @@ export function adoptRuntimeMemoryCorpusSupplementRegistrations(
134145 ( targetOwner &&
135146 ( targetOwner . status !== "loaded" || targetOwner . source !== runtimeOwner . source ) )
136147 ) {
137- continue ;
148+ return false ;
138149 }
139- supplements . push ( registration ) ;
140- changed = true ;
141- }
142- return changed ? { ...targetRegistry , memoryCorpusSupplements : supplements } : targetRegistry ;
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+ } ;
143177}
144178export function registerMemoryPromptSupplement (
145179 requestedPluginId : string ,
0 commit comments