@@ -3,7 +3,7 @@ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
33import type { OpenClawPluginApi , OpenClawPluginCommandDefinition } from "openclaw/plugin-sdk/core" ;
44import type { MemoryPluginRuntime } from "openclaw/plugin-sdk/memory-core-host-runtime-core" ;
55import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api" ;
6- import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
6+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
77import { buildMemoryFlushPlan } from "./src/flush-plan.js" ;
88import type { MemoryCoreRuntimeHost } from "./src/memory/runtime-host.js" ;
99import { buildPromptSection } from "./src/prompt-section.js" ;
@@ -112,10 +112,6 @@ describe("memory-core plugin runtime registration", () => {
112112 vi . clearAllMocks ( ) ;
113113 } ) ;
114114
115- afterEach ( ( ) => {
116- vi . useRealTimers ( ) ;
117- } ) ;
118-
119115 it ( "registers the dreaming runtime slash command" , ( ) => {
120116 let command : OpenClawPluginCommandDefinition | undefined ;
121117 plugin . register (
@@ -199,228 +195,14 @@ describe("memory-core plugin runtime registration", () => {
199195 expect ( intentFactory ( { config : { } , senderIsOwner : true } ) ) . toMatchObject ( { name : "intent" } ) ;
200196 } ) ;
201197
202- it ( "warms each configured memory manager at gateway start and logs failures at debug" , async ( ) => {
203- vi . useFakeTimers ( ) ;
204- const gatewayStartHandlers : Array < ( event : unknown , ctx : { config : OpenClawConfig } ) => void > =
205- [ ] ;
206- const syncMain = vi . fn ( async ( ) => { } ) ;
207- const syncWork = vi . fn ( async ( ) => {
208- throw new Error ( "warmup failed" ) ;
209- } ) ;
210- const debug = vi . fn ( ) ;
211- getMemorySearchManagerMock
212- . mockResolvedValueOnce ( { manager : { sync : syncMain } } as never )
213- . mockResolvedValueOnce ( { manager : { sync : syncWork } } as never ) ;
214- const config = {
215- agents : { entries : { main : { default : true } , work : { } } } ,
216- } as OpenClawConfig ;
217- const testApi = createTestPluginApi ( {
218- config,
219- logger : { debug, info : vi . fn ( ) , warn : vi . fn ( ) , error : vi . fn ( ) } ,
220- runtime : hostRuntime ,
221- on ( hookName , handler ) {
222- if ( hookName === "gateway_start" ) {
223- gatewayStartHandlers . push (
224- handler as unknown as ( event : unknown , ctx : { config : OpenClawConfig } ) => void ,
225- ) ;
226- }
227- } ,
228- } ) ;
229-
230- plugin . register ( testApi ) ;
231- const warmup = gatewayStartHandlers . at ( - 1 ) ;
232- if ( ! warmup ) {
233- throw new Error ( "expected memory warmup gateway_start hook" ) ;
234- }
235- warmup ( { } , { config } ) ;
236-
237- expect ( getMemorySearchManagerMock ) . not . toHaveBeenCalled ( ) ;
238- await vi . runOnlyPendingTimersAsync ( ) ;
239- await vi . waitFor ( ( ) => {
240- expect ( getMemorySearchManagerMock ) . toHaveBeenCalledTimes ( 2 ) ;
241- expect ( syncMain ) . toHaveBeenCalledWith ( { reason : "startup-warmup" } ) ;
242- expect ( syncWork ) . toHaveBeenCalledWith ( { reason : "startup-warmup" } ) ;
243- expect ( debug ) . toHaveBeenCalledWith (
244- "memory-core: startup index warmup failed for work: warmup failed" ,
245- ) ;
246- } ) ;
247- } ) ;
248-
249- it ( "defers memory manager initialization until after the gateway start turn" , async ( ) => {
250- vi . useFakeTimers ( ) ;
251- const gatewayStartHandlers : Array < ( event : unknown , ctx : { config : OpenClawConfig } ) => void > =
252- [ ] ;
253- getMemorySearchManagerMock . mockResolvedValue ( { manager : null } as never ) ;
254- const config = { } as OpenClawConfig ;
198+ it ( "keeps memory manager initialization demand-driven" , ( ) => {
255199 plugin . register (
256200 createTestPluginApi ( {
257- config,
258201 runtime : hostRuntime ,
259- on ( hookName , handler ) {
260- if ( hookName === "gateway_start" ) {
261- gatewayStartHandlers . push (
262- handler as unknown as ( event : unknown , ctx : { config : OpenClawConfig } ) => void ,
263- ) ;
264- }
265- } ,
266202 } ) ,
267203 ) ;
268- const warmup = gatewayStartHandlers . at ( - 1 ) ;
269- if ( ! warmup ) {
270- throw new Error ( "expected memory warmup gateway_start hook" ) ;
271- }
272-
273- warmup ( { } , { config } ) ;
274-
275- expect ( getMemorySearchManagerMock ) . not . toHaveBeenCalled ( ) ;
276- await vi . runOnlyPendingTimersAsync ( ) ;
277- await vi . waitFor ( ( ) => expect ( getMemorySearchManagerMock ) . toHaveBeenCalledTimes ( 1 ) ) ;
278- } ) ;
279-
280- it ( "cancels deferred memory warmup when the gateway stops" , async ( ) => {
281- vi . useFakeTimers ( ) ;
282- const gatewayStartHandlers : Array < ( event : unknown , ctx : { config : OpenClawConfig } ) => void > =
283- [ ] ;
284- const gatewayStopHandlers : Array < ( ) => void > = [ ] ;
285- const config = { } as OpenClawConfig ;
286- plugin . register (
287- createTestPluginApi ( {
288- config,
289- runtime : hostRuntime ,
290- on ( hookName , handler ) {
291- if ( hookName === "gateway_start" ) {
292- gatewayStartHandlers . push (
293- handler as unknown as ( event : unknown , ctx : { config : OpenClawConfig } ) => void ,
294- ) ;
295- }
296- if ( hookName === "gateway_stop" ) {
297- gatewayStopHandlers . push ( handler as unknown as ( ) => void ) ;
298- }
299- } ,
300- } ) ,
301- ) ;
302- const warmup = gatewayStartHandlers . at ( - 1 ) ;
303- const stop = gatewayStopHandlers . at ( - 1 ) ;
304- if ( ! warmup || ! stop ) {
305- throw new Error ( "expected memory warmup lifecycle hooks" ) ;
306- }
307-
308- warmup ( { } , { config } ) ;
309- stop ( ) ;
310- await vi . runOnlyPendingTimersAsync ( ) ;
311-
312- expect ( getMemorySearchManagerMock ) . not . toHaveBeenCalled ( ) ;
313- } ) ;
314-
315- it ( "prevents a restarted gateway generation from syncing a stale manager" , async ( ) => {
316- vi . useFakeTimers ( ) ;
317- const gatewayStartHandlers : Array < ( event : unknown , ctx : { config : OpenClawConfig } ) => void > =
318- [ ] ;
319- let resolveStaleManager :
320- | ( ( value : { manager : { sync : ( ) => Promise < void > } } ) => void )
321- | undefined ;
322- const staleManagerResult = new Promise < { manager : { sync : ( ) => Promise < void > } } > (
323- ( resolve ) => {
324- resolveStaleManager = resolve ;
325- } ,
326- ) ;
327- const staleSync = vi . fn ( async ( ) => { } ) ;
328- const currentSync = vi . fn ( async ( ) => { } ) ;
329- getMemorySearchManagerMock
330- . mockReturnValueOnce ( staleManagerResult as never )
331- . mockResolvedValueOnce ( { manager : { sync : currentSync } } as never ) ;
332- const config = { } as OpenClawConfig ;
333- plugin . register (
334- createTestPluginApi ( {
335- config,
336- runtime : hostRuntime ,
337- on ( hookName , handler ) {
338- if ( hookName === "gateway_start" ) {
339- gatewayStartHandlers . push (
340- handler as unknown as ( event : unknown , ctx : { config : OpenClawConfig } ) => void ,
341- ) ;
342- }
343- } ,
344- } ) ,
345- ) ;
346- const warmup = gatewayStartHandlers . at ( - 1 ) ;
347- if ( ! warmup ) {
348- throw new Error ( "expected memory warmup gateway_start hook" ) ;
349- }
350-
351- warmup ( { } , { config } ) ;
352- await vi . runOnlyPendingTimersAsync ( ) ;
353- await vi . waitFor ( ( ) => expect ( getMemorySearchManagerMock ) . toHaveBeenCalledTimes ( 1 ) ) ;
354-
355- warmup ( { } , { config } ) ;
356- await vi . runOnlyPendingTimersAsync ( ) ;
357- await vi . waitFor ( ( ) => expect ( currentSync ) . toHaveBeenCalledWith ( { reason : "startup-warmup" } ) ) ;
358-
359- resolveStaleManager ?.( { manager : { sync : staleSync } } ) ;
360- await vi . waitFor ( ( ) => expect ( getMemorySearchManagerMock ) . toHaveBeenCalledTimes ( 2 ) ) ;
361- expect ( staleSync ) . not . toHaveBeenCalled ( ) ;
362- } ) ;
363-
364- it ( "leaves QMD startup synchronization to the backend boot policy" , async ( ) => {
365- vi . useFakeTimers ( ) ;
366- const gatewayStartHandlers : Array < ( event : unknown , ctx : { config : OpenClawConfig } ) => void > =
367- [ ] ;
368- const sync = vi . fn ( async ( ) => { } ) ;
369- getMemorySearchManagerMock . mockResolvedValueOnce ( { manager : { sync } } as never ) ;
370- const config = {
371- memory : { backend : "qmd" , qmd : { update : { onBoot : false } } } ,
372- } as OpenClawConfig ;
373- plugin . register (
374- createTestPluginApi ( {
375- config,
376- runtime : hostRuntime ,
377- on ( hookName , handler ) {
378- if ( hookName === "gateway_start" ) {
379- gatewayStartHandlers . push (
380- handler as unknown as ( event : unknown , ctx : { config : OpenClawConfig } ) => void ,
381- ) ;
382- }
383- } ,
384- } ) ,
385- ) ;
386- const warmup = gatewayStartHandlers . at ( - 1 ) ;
387- if ( ! warmup ) {
388- throw new Error ( "expected memory warmup gateway_start hook" ) ;
389- }
390-
391- warmup ( { } , { config } ) ;
392-
393- await vi . runOnlyPendingTimersAsync ( ) ;
394- await vi . waitFor ( ( ) => expect ( getMemorySearchManagerMock ) . toHaveBeenCalledTimes ( 1 ) ) ;
395- expect ( sync ) . not . toHaveBeenCalled ( ) ;
396- } ) ;
397-
398- it ( "does not warm memory-core when another plugin owns the memory slot" , async ( ) => {
399- const gatewayStartHandlers : Array < ( event : unknown , ctx : { config : OpenClawConfig } ) => void > =
400- [ ] ;
401- const config = {
402- plugins : { slots : { memory : "memory-lancedb" } } ,
403- } as OpenClawConfig ;
404- plugin . register (
405- createTestPluginApi ( {
406- config,
407- runtime : hostRuntime ,
408- on ( hookName , handler ) {
409- if ( hookName === "gateway_start" ) {
410- gatewayStartHandlers . push (
411- handler as unknown as ( event : unknown , ctx : { config : OpenClawConfig } ) => void ,
412- ) ;
413- }
414- } ,
415- } ) ,
416- ) ;
417- const warmup = gatewayStartHandlers . at ( - 1 ) ;
418- if ( ! warmup ) {
419- throw new Error ( "expected memory warmup gateway_start hook" ) ;
420- }
421-
422- warmup ( { } , { config } ) ;
423204
205+ expect ( createMemoryRuntimeMock ) . not . toHaveBeenCalled ( ) ;
424206 expect ( getMemorySearchManagerMock ) . not . toHaveBeenCalled ( ) ;
425207 } ) ;
426208
0 commit comments