Skip to content

Commit f2b7a98

Browse files
committed
fix(memory): keep post-ready RPCs responsive
Punchcard-Session: coral-workshop-workshop-3f
1 parent e69c3df commit f2b7a98

3 files changed

Lines changed: 4 additions & 299 deletions

File tree

‎extensions/memory-core/index.test.ts‎

Lines changed: 3 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
33
import type { OpenClawPluginApi, OpenClawPluginCommandDefinition } from "openclaw/plugin-sdk/core";
44
import type { MemoryPluginRuntime } from "openclaw/plugin-sdk/memory-core-host-runtime-core";
55
import { 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";
77
import { buildMemoryFlushPlan } from "./src/flush-plan.js";
88
import type { MemoryCoreRuntimeHost } from "./src/memory/runtime-host.js";
99
import { 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

‎extensions/memory-core/index.ts‎

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
22
// Memory Core plugin entrypoint registers its OpenClaw integration.
33
import {
44
jsonResult,
5-
listAgentIds,
65
resolveMemorySearchConfig,
76
resolveSessionAgentIds,
87
type MemoryPluginRuntime,
98
type OpenClawConfig,
109
} from "openclaw/plugin-sdk/memory-core-host-runtime-core";
1110
import { resolveMemoryBackendConfig } from "openclaw/plugin-sdk/memory-core-host-runtime-files";
12-
import { normalizePluginsConfig } from "openclaw/plugin-sdk/plugin-config-runtime";
1311
import {
1412
definePluginEntry,
1513
type AnyAgentTool,
16-
type OpenClawPluginApi,
1714
type OpenClawPluginToolContext,
1815
} from "openclaw/plugin-sdk/plugin-entry";
1916
import type {
@@ -57,8 +54,6 @@ const loadRuntimeProviderModule = createLazyRuntimeModule(
5754
() => import("./src/runtime-provider.js"),
5855
);
5956

60-
const MEMORY_MANAGER_WARMUP_DELAY_MS = 5_000;
61-
6257
function getToolConfig(options: MemoryToolOptions): OpenClawConfig | undefined {
6358
return options.getConfig?.() ?? options.config;
6459
}
@@ -277,72 +272,6 @@ function createLazyMemoryRuntime(host: MemoryCoreRuntimeHost): MemoryPluginRunti
277272
};
278273
}
279274

280-
function registerMemoryManagerWarmup(
281-
api: OpenClawPluginApi,
282-
memoryRuntime: MemoryPluginRuntime,
283-
): void {
284-
let lifecycleGeneration = 0;
285-
let warmupTimer: ReturnType<typeof setTimeout> | null = null;
286-
287-
const cancelWarmup = () => {
288-
lifecycleGeneration += 1;
289-
if (warmupTimer) {
290-
clearTimeout(warmupTimer);
291-
warmupTimer = null;
292-
}
293-
};
294-
295-
api.on("gateway_start", (_event, ctx) => {
296-
cancelWarmup();
297-
const generation = lifecycleGeneration;
298-
const config = (api.runtime.config?.current?.() ?? ctx.config ?? api.config) as OpenClawConfig;
299-
if (normalizePluginsConfig(config.plugins).slots.memory !== "memory-core") {
300-
return;
301-
}
302-
303-
// Leave a bounded idle window for initial post-ready RPCs before loading the manager.
304-
const timer = setTimeout(() => {
305-
if (warmupTimer !== timer || generation !== lifecycleGeneration) {
306-
return;
307-
}
308-
warmupTimer = null;
309-
for (const agentId of listAgentIds(config)) {
310-
const backend = memoryRuntime.resolveMemoryBackendConfig({ cfg: config, agentId });
311-
void memoryRuntime
312-
.getMemorySearchManager({ cfg: config, agentId })
313-
.then(async ({ manager, error }) => {
314-
if (generation !== lifecycleGeneration) {
315-
return;
316-
}
317-
if (!manager) {
318-
if (error) {
319-
api.logger.debug?.(`memory-core: startup index warmup unavailable: ${error}`);
320-
}
321-
return;
322-
}
323-
if (backend.backend === "builtin") {
324-
await manager.sync?.({ reason: "startup-warmup" });
325-
}
326-
})
327-
.catch((error: unknown) => {
328-
if (generation !== lifecycleGeneration) {
329-
return;
330-
}
331-
api.logger.debug?.(
332-
`memory-core: startup index warmup failed for ${agentId}: ${
333-
error instanceof Error ? error.message : String(error)
334-
}`,
335-
);
336-
});
337-
}
338-
}, MEMORY_MANAGER_WARMUP_DELAY_MS);
339-
warmupTimer = timer;
340-
warmupTimer.unref?.();
341-
});
342-
343-
api.on("gateway_stop", cancelWarmup);
344-
}
345-
346275
export default definePluginEntry({
347276
id: "memory-core",
348277
name: "OpenClaw Memory",
@@ -358,7 +287,6 @@ export default definePluginEntry({
358287
const memoryRuntime = createLazyMemoryRuntime(host);
359288
registerShortTermPromotionDreaming(api);
360289
registerSessionBackfillGatewayMethods(api);
361-
registerMemoryManagerWarmup(api, memoryRuntime);
362290
api.registerMemoryCapability({
363291
promptBuilder: buildPromptSection,
364292
flushPlanResolver: buildMemoryFlushPlan,

‎src/plugins/contracts/boundary-invariants.test.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ const BUNDLED_TYPED_HOOK_REGISTRATION_GUARDS = {
4242
"extensions/feishu/subagent-hooks-api.ts": ["subagent_delivery_target", "subagent_ended"],
4343
"extensions/matrix/subagent-hooks-api.ts": ["subagent_delivery_target", "subagent_ended"],
4444
"extensions/memory-core/src/dreaming.ts": ["before_agent_reply", "gateway_start", "gateway_stop"],
45-
"extensions/memory-core/index.ts": [
46-
"before_agent_reply",
47-
"before_prompt_build",
48-
"gateway_start",
49-
"gateway_stop",
50-
],
45+
"extensions/memory-core/index.ts": ["before_agent_reply", "before_prompt_build"],
5146
"extensions/memory-lancedb/index.ts": ["agent_end", "before_prompt_build", "session_end"],
5247
"extensions/onepassword/index.ts": ["before_tool_call", "tool_result_persist"],
5348
"extensions/thread-ownership/index.ts": ["message_received", "message_sending"],

0 commit comments

Comments
 (0)