Skip to content

Commit 04a2fda

Browse files
committed
fix(memory): enforce live revocation
1 parent 248966d commit 04a2fda

4 files changed

Lines changed: 88 additions & 11 deletions

File tree

‎extensions/memory-core/src/tools.citations.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,35 @@ describe("memory tools", () => {
236236
expect(getMemorySearchManagerMockCalls()).toBe(0);
237237
});
238238

239+
it("revokes retained memory tools when live config disables memory", async () => {
240+
const startupConfig = asOpenClawConfig({
241+
agents: { list: [{ id: "main", default: true }] },
242+
});
243+
let liveConfig = startupConfig;
244+
const getConfig = () => liveConfig;
245+
const searchTool = createMemorySearchTool({ config: startupConfig, getConfig });
246+
const getTool = createMemoryGetTool({ config: startupConfig, getConfig });
247+
if (!searchTool || !getTool) {
248+
throw new Error("memory tools missing");
249+
}
250+
251+
liveConfig = asOpenClawConfig({
252+
agents: {
253+
list: [{ id: "main", default: true, memory: { search: { enabled: false } } }],
254+
},
255+
});
256+
const disabledMessage =
257+
"Memory is disabled for this agent. Enable memory search for this agent, then retry.";
258+
await expect(
259+
searchTool.execute("revoked-search", { query: "private preference" }),
260+
).rejects.toThrow(disabledMessage);
261+
await expect(getTool.execute("revoked-get", { path: "MEMORY.md" })).rejects.toThrow(
262+
disabledMessage,
263+
);
264+
expect(getMemorySearchManagerMockCalls()).toBe(0);
265+
expect(getReadAgentMemoryFileMockCalls()).toBe(0);
266+
});
267+
239268
it("rejects fractional memory_get ranges before reading files", async () => {
240269
const tool = createMemoryGetToolOrThrow();
241270

‎extensions/memory-core/src/tools.shared.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const MemoryGetSchema = Type.Object({
4242
});
4343

4444
function resolveMemoryToolContext(options: MemoryToolOptions) {
45-
const cfg = options.getConfig?.() ?? options.config;
45+
const cfg = options.getConfig ? options.getConfig() : options.config;
4646
if (!cfg) {
4747
return null;
4848
}
@@ -109,7 +109,14 @@ export function createMemoryTool(params: {
109109
description: params.description,
110110
parameters: params.parameters,
111111
execute: async (toolCallId, toolParams, signal, onUpdate) => {
112-
const latestCtx = resolveMemoryToolContext(params.options) ?? ctx;
112+
const latestCtx = params.options.getConfig ? resolveMemoryToolContext(params.options) : ctx;
113+
// A live getter makes missing or disabled current config a revocation.
114+
// The captured context is valid only for fixed-snapshot callers.
115+
if (!latestCtx) {
116+
throw new Error(
117+
"Memory is disabled for this agent. Enable memory search for this agent, then retry.",
118+
);
119+
}
113120
return await params.execute(latestCtx)(toolCallId, toolParams, signal, onUpdate);
114121
},
115122
};

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,7 @@ describe("memory plugin e2e", () => {
18541854
}));
18551855
const ensureGlobalUndiciEnvProxyDispatcher = vi.fn();
18561856
const add = vi.fn(async () => undefined);
1857+
const deleteRows = vi.fn(async () => ({ numDeletedRows: 1 }));
18571858
const loadLanceDbModule = vi.fn(async () => ({
18581859
connect: vi.fn(async () => ({
18591860
tableNames: vi.fn(async () => ["memories"]),
@@ -1864,7 +1865,7 @@ describe("memory plugin e2e", () => {
18641865
),
18651866
countRows: vi.fn(async () => 0),
18661867
add,
1867-
delete: vi.fn(async () => undefined),
1868+
delete: deleteRows,
18681869
})),
18691870
})),
18701871
}));
@@ -1925,14 +1926,13 @@ describe("memory plugin e2e", () => {
19251926
}),
19261927
),
19271928
).toEqual([null, null, null]);
1928-
expect(
1929-
registeredToolFactories.map(({ toolOrFactory }) =>
1930-
materializeRegisteredTool(toolOrFactory, {
1931-
agentId: "main",
1932-
getRuntimeConfig: () => configFile,
1933-
}),
1934-
),
1935-
).toMatchObject([
1929+
const enabledTools = registeredToolFactories.map(({ toolOrFactory }) =>
1930+
materializeRegisteredTool(toolOrFactory, {
1931+
agentId: "main",
1932+
getRuntimeConfig: () => configFile,
1933+
}),
1934+
);
1935+
expect(enabledTools).toMatchObject([
19361936
{ name: "memory_recall" },
19371937
{ name: "memory_store" },
19381938
{ name: "memory_forget" },
@@ -1985,6 +1985,29 @@ describe("memory plugin e2e", () => {
19851985

19861986
agents: { defaults: {} },
19871987
};
1988+
const [recallTool, storeTool, forgetTool] = enabledTools;
1989+
embeddingsCreate.mockClear();
1990+
loadLanceDbModule.mockClear();
1991+
add.mockClear();
1992+
deleteRows.mockClear();
1993+
const disabledMessage =
1994+
"Memory is disabled for this agent. Enable memory search for this agent, then retry.";
1995+
await expect(
1996+
recallTool.execute("revoked-recall", { query: "private preference" }),
1997+
).rejects.toThrow(disabledMessage);
1998+
await expect(
1999+
storeTool.execute("revoked-store", { text: "The user prefers Helix." }),
2000+
).rejects.toThrow(disabledMessage);
2001+
await expect(
2002+
forgetTool.execute("revoked-forget", {
2003+
memoryId: "11111111-1111-4111-8111-111111111111",
2004+
}),
2005+
).rejects.toThrow(disabledMessage);
2006+
expect(embeddingsCreate).not.toHaveBeenCalled();
2007+
expect(loadLanceDbModule).not.toHaveBeenCalled();
2008+
expect(add).not.toHaveBeenCalled();
2009+
expect(deleteRows).not.toHaveBeenCalled();
2010+
19882011
const recallDefaultDisabled = await beforePromptBuild?.(recallEvent, {
19892012
agentId: "unlisted",
19902013
});

‎extensions/memory-lancedb/index.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ export default definePluginEntry({
136136
const enabled = overrides?.enabled ?? runtimeConfig.memory?.search?.enabled ?? true;
137137
return enabled ? agentId : undefined;
138138
};
139+
const assertRetainedToolEnabled = (
140+
agentId: string,
141+
getRuntimeConfig: (() => OpenClawConfig | undefined) | undefined,
142+
): void => {
143+
if (!getRuntimeConfig) {
144+
return;
145+
}
146+
const runtimeConfig = getRuntimeConfig();
147+
if (!runtimeConfig || !resolveEnabledAgentId(agentId, runtimeConfig)) {
148+
throw new Error(
149+
"Memory is disabled for this agent. Enable memory search for this agent, then retry.",
150+
);
151+
}
152+
};
139153
const resolveCliAgentId = (rawAgentId: unknown): string => {
140154
if (typeof rawAgentId === "string" && rawAgentId.trim()) {
141155
return normalizeAgentId(rawAgentId);
@@ -221,6 +235,8 @@ export default definePluginEntry({
221235
limit: optionalPositiveIntegerSchema({ description: "Max results (default: 5)" }),
222236
}),
223237
async execute(_toolCallId, params) {
238+
// Tool definitions outlive hot config reloads; revalidate before memory I/O.
239+
assertRetainedToolEnabled(agentId, ctx.getRuntimeConfig);
224240
const rawParams = params as Record<string, unknown>;
225241
const query = rawParams.query as string;
226242
const limit = readPositiveIntegerParam(rawParams, "limit") ?? 5;
@@ -343,6 +359,7 @@ export default definePluginEntry({
343359
category: Type.Optional(Type.Enum(MEMORY_CATEGORIES, { type: "string" })),
344360
}),
345361
async execute(_toolCallId, params) {
362+
assertRetainedToolEnabled(agentId, ctx.getRuntimeConfig);
346363
if (isIncognitoSessionKey(ctx.sessionKey)) {
347364
return {
348365
content: [
@@ -438,6 +455,7 @@ export default definePluginEntry({
438455
memoryId: Type.Optional(Type.String({ description: "Specific memory ID" })),
439456
}),
440457
async execute(_toolCallId, params) {
458+
assertRetainedToolEnabled(agentId, ctx.getRuntimeConfig);
441459
const { query, memoryId } = params as { query?: string; memoryId?: string };
442460

443461
if (memoryId) {

0 commit comments

Comments
 (0)