Skip to content

Commit 1333238

Browse files
authored
fix(cli): close standalone memory managers after commands (#118500)
1 parent d7ab20a commit 1333238

3 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎src/plugins/memory-runtime.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
resolveActiveMemoryBackendConfig,
3232
} from "./memory-runtime.js";
3333
import { resetStandaloneMemoryRegistrySlot } from "./memory-runtime.test-support.js";
34+
import { hasMemoryRuntime } from "./memory-state.js";
3435

3536
function createRuntime() {
3637
return {
@@ -95,6 +96,73 @@ describe("memory runtime handles", () => {
9596
});
9697
});
9798

99+
it("tracks standalone managers without activating config-only lookups and rearms reused handles", async () => {
100+
const { registry, runtime } = createRegistry();
101+
mocks.loadPluginRegistryHandle.mockReturnValue(registry);
102+
103+
expect(hasMemoryRuntime()).toBe(false);
104+
expect(resolveActiveMemoryBackendConfig({ cfg: memoryConfig, agentId: "main" })).toEqual({
105+
backend: "builtin",
106+
});
107+
expect(hasMemoryRuntime()).toBe(false);
108+
109+
await getActiveMemorySearchManager({ cfg: memoryConfig, agentId: "main" });
110+
expect(hasMemoryRuntime()).toBe(true);
111+
112+
await closeActiveMemorySearchManagers();
113+
expect(hasMemoryRuntime()).toBe(false);
114+
115+
await getActiveMemorySearchManager({ cfg: memoryConfig, agentId: "main" });
116+
expect(hasMemoryRuntime()).toBe(true);
117+
expect(mocks.loadPluginRegistryHandle).toHaveBeenCalledTimes(1);
118+
119+
await closeActiveMemorySearchManagers();
120+
expect(runtime.closeAllMemorySearchManagers).toHaveBeenCalledTimes(2);
121+
expect(hasMemoryRuntime()).toBe(false);
122+
});
123+
124+
it("retains standalone ownership across workspace replacement and per-agent cleanup", async () => {
125+
const main = createRegistry();
126+
const research = createRegistry();
127+
mocks.loadPluginRegistryHandle
128+
.mockReturnValueOnce(main.registry)
129+
.mockReturnValueOnce(research.registry);
130+
131+
await getActiveMemorySearchManager({ cfg: memoryConfig, agentId: "main" });
132+
await getActiveMemorySearchManager({ cfg: memoryConfig, agentId: "research" });
133+
expect(hasMemoryRuntime()).toBe(true);
134+
135+
await closeActiveMemorySearchManager({ cfg: memoryConfig, agentId: "main" });
136+
expect(hasMemoryRuntime()).toBe(true);
137+
138+
await closeActiveMemorySearchManagers();
139+
expect(main.runtime.closeAllMemorySearchManagers).toHaveBeenCalledTimes(1);
140+
expect(research.runtime.closeAllMemorySearchManagers).toHaveBeenCalledTimes(1);
141+
expect(hasMemoryRuntime()).toBe(false);
142+
});
143+
144+
it("retains standalone cleanup ownership when manager acquisition or teardown fails", async () => {
145+
const { registry, runtime } = createRegistry();
146+
mocks.loadPluginRegistryHandle.mockReturnValue(registry);
147+
runtime.getMemorySearchManager.mockRejectedValueOnce(
148+
new Error("manager initialization failed"),
149+
);
150+
151+
await expect(
152+
getActiveMemorySearchManager({ cfg: memoryConfig, agentId: "main" }),
153+
).rejects.toThrow("manager initialization failed");
154+
expect(hasMemoryRuntime()).toBe(true);
155+
156+
runtime.closeAllMemorySearchManagers.mockRejectedValueOnce(
157+
new Error("manager teardown failed"),
158+
);
159+
await expect(closeActiveMemorySearchManagers()).rejects.toThrow("manager teardown failed");
160+
expect(hasMemoryRuntime()).toBe(true);
161+
162+
await closeActiveMemorySearchManagers();
163+
expect(hasMemoryRuntime()).toBe(false);
164+
});
165+
98166
it("keys the single slot by the requesting agent workspace", () => {
99167
const main = createRegistry();
100168
const research = createRegistry();

‎src/plugins/memory-runtime.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
44
import { resolveUserPath } from "../utils.js";
55
import { normalizePluginsConfig } from "./config-state.js";
66
import { loadPluginRegistryHandle, resolvePluginRegistryLoadCacheKey } from "./loader.js";
7-
import { getMemoryRuntime, resolveMemoryCapabilityRegistration } from "./memory-state.js";
7+
import {
8+
getMemoryRuntime,
9+
resolveMemoryCapabilityRegistration,
10+
setStandaloneMemoryManagerActive,
11+
} from "./memory-state.js";
812
import type { PluginRegistry } from "./registry-types.js";
913
import { withPluginRuntimeRegistryScope } from "./runtime/gateway-request-scope.js";
1014

@@ -121,6 +125,9 @@ export async function getActiveMemorySearchManager(params: {
121125
if (!owner) {
122126
return { manager: null, error: "memory plugin unavailable" };
123127
}
128+
if (owner.registry) {
129+
setStandaloneMemoryManagerActive(true);
130+
}
124131
return await withMemoryRuntimeOwner(
125132
owner,
126133
async (runtime) => await runtime.getMemorySearchManager(params),
@@ -146,6 +153,7 @@ export async function closeActiveMemorySearchManagers(cfg?: OpenClawConfig): Pro
146153
),
147154
);
148155
standaloneMemoryRegistrySlot?.retiredRuntimes.clear();
156+
setStandaloneMemoryManagerActive(false);
149157
}
150158

151159
/** Closes the plugin-backed memory search manager for one agent. */
@@ -164,6 +172,7 @@ export async function closeActiveMemorySearchManager(params: {
164172

165173
function resetStandaloneMemoryRegistrySlot(): void {
166174
standaloneMemoryRegistrySlot = undefined;
175+
setStandaloneMemoryManagerActive(false);
167176
}
168177

169178
if (process.env.VITEST || process.env.NODE_ENV === "test") {

‎src/plugins/memory-state.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,15 @@ export function getMemoryRuntime(): MemoryPluginRuntime | undefined {
266266
return getMemoryCapability()?.capability.runtime;
267267
}
268268

269+
let standaloneMemoryManagerActive = false;
270+
271+
// Standalone managers are intentionally absent from the active plugin registry.
272+
export function setStandaloneMemoryManagerActive(active: boolean): void {
273+
standaloneMemoryManagerActive = active;
274+
}
275+
269276
export function hasMemoryRuntime(): boolean {
270-
return getMemoryRuntime() !== undefined;
277+
return standaloneMemoryManagerActive || getMemoryRuntime() !== undefined;
271278
}
272279

273280
function cloneMemoryPublicArtifact(

0 commit comments

Comments
 (0)