@@ -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 { beforeEach , describe , expect , it , vi } from "vitest" ;
6+ import { afterEach , 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,6 +112,10 @@ describe("memory-core plugin runtime registration", () => {
112112 vi . clearAllMocks ( ) ;
113113 } ) ;
114114
115+ afterEach ( ( ) => {
116+ vi . useRealTimers ( ) ;
117+ } ) ;
118+
115119 it ( "registers the dreaming runtime slash command" , ( ) => {
116120 let command : OpenClawPluginCommandDefinition | undefined ;
117121 plugin . register (
@@ -196,6 +200,7 @@ describe("memory-core plugin runtime registration", () => {
196200 } ) ;
197201
198202 it ( "warms each configured memory manager at gateway start and logs failures at debug" , async ( ) => {
203+ vi . useFakeTimers ( ) ;
199204 const gatewayStartHandlers : Array < ( event : unknown , ctx : { config : OpenClawConfig } ) => void > =
200205 [ ] ;
201206 const syncMain = vi . fn ( async ( ) => { } ) ;
@@ -229,6 +234,8 @@ describe("memory-core plugin runtime registration", () => {
229234 }
230235 warmup ( { } , { config } ) ;
231236
237+ expect ( getMemorySearchManagerMock ) . not . toHaveBeenCalled ( ) ;
238+ await vi . runOnlyPendingTimersAsync ( ) ;
232239 await vi . waitFor ( ( ) => {
233240 expect ( getMemorySearchManagerMock ) . toHaveBeenCalledTimes ( 2 ) ;
234241 expect ( syncMain ) . toHaveBeenCalledWith ( { reason : "startup-warmup" } ) ;
@@ -239,7 +246,123 @@ describe("memory-core plugin runtime registration", () => {
239246 } ) ;
240247 } ) ;
241248
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 ;
255+ plugin . register (
256+ createTestPluginApi ( {
257+ config,
258+ 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+ } ,
266+ } ) ,
267+ ) ;
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+
242364 it ( "leaves QMD startup synchronization to the backend boot policy" , async ( ) => {
365+ vi . useFakeTimers ( ) ;
243366 const gatewayStartHandlers : Array < ( event : unknown , ctx : { config : OpenClawConfig } ) => void > =
244367 [ ] ;
245368 const sync = vi . fn ( async ( ) => { } ) ;
@@ -267,6 +390,7 @@ describe("memory-core plugin runtime registration", () => {
267390
268391 warmup ( { } , { config } ) ;
269392
393+ await vi . runOnlyPendingTimersAsync ( ) ;
270394 await vi . waitFor ( ( ) => expect ( getMemorySearchManagerMock ) . toHaveBeenCalledTimes ( 1 ) ) ;
271395 expect ( sync ) . not . toHaveBeenCalled ( ) ;
272396 } ) ;
0 commit comments