Skip to content

Commit bcc6f21

Browse files
fix(memory): fail zero-row delete receipts
1 parent 1d97299 commit bcc6f21

2 files changed

Lines changed: 76 additions & 7 deletions

File tree

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import fs from "node:fs/promises";
1313
import path from "node:path";
1414
import { expectDefined } from "@openclaw/normalization-core";
1515
import { Command } from "commander";
16+
import { isToolResultError } from "openclaw/plugin-sdk/agent-harness-runtime";
17+
import {
18+
buildContractReplyPayloads,
19+
createContractToolTerminalObserver,
20+
} from "openclaw/plugin-sdk/agent-runtime-test-contracts";
1621
import {
1722
clearMemoryPluginState,
1823
getMemoryCapabilityRegistration,
@@ -3620,19 +3625,82 @@ describe("memory plugin e2e", () => {
36203625
expectToolExecute(forgetTool, "memory_forget");
36213626

36223627
const directAbsent = await forgetTool.execute("forget-direct-absent", { memoryId });
3623-
expect(directAbsent.details).toEqual({ action: "not_found", id: memoryId });
3628+
const notDeletedError = `Memory ${memoryId} was not deleted because it was not found.`;
3629+
expect(directAbsent.details).toEqual({
3630+
action: "not_found",
3631+
error: notDeletedError,
3632+
id: memoryId,
3633+
status: "error",
3634+
});
3635+
expect(directAbsent.content?.[0]?.text).toBe(notDeletedError);
36243636

36253637
const queryAbsent = await forgetTool.execute("forget-query-absent", {
36263638
query: "concise replies",
36273639
});
3628-
expect(queryAbsent.details).toEqual({ action: "not_found", id: memoryId });
3640+
expect(queryAbsent.details).toEqual({
3641+
action: "not_found",
3642+
error: notDeletedError,
3643+
id: memoryId,
3644+
status: "error",
3645+
});
3646+
expect(queryAbsent.content?.[0]?.text).toBe(notDeletedError);
36293647
expect(queryAbsent.content?.[0]?.text).not.toContain("Forgotten");
36303648

3649+
for (const [label, args, result] of [
3650+
["direct", { memoryId }, directAbsent],
3651+
["query", { query: "concise replies" }, queryAbsent],
3652+
] as const) {
3653+
expect(isToolResultError(result), `${label} zero-row receipt`).toBe(true);
3654+
const terminal = createContractToolTerminalObserver(`forget-${label}-zero`)({
3655+
toolName: "memory_forget",
3656+
arguments: args,
3657+
outcome: "failure",
3658+
failure: { error: result.content?.[0]?.text },
3659+
ownerMutation: { ownerKey: '["memory-lancedb","memory_forget"]' },
3660+
});
3661+
const payloads = buildContractReplyPayloads({
3662+
assistantText: "Done — I forgot that memory.",
3663+
lastToolError: terminal.lastToolError,
3664+
});
3665+
expect(payloads).toEqual([
3666+
expect.objectContaining({ text: "Done — I forgot that memory." }),
3667+
expect.objectContaining({ isError: true }),
3668+
]);
3669+
expect(JSON.stringify(payloads)).not.toContain("memory-lancedb");
3670+
}
3671+
36313672
const queryDeleted = await forgetTool.execute("forget-query-deleted", {
36323673
query: "concise replies",
36333674
});
36343675
expect(queryDeleted.details).toEqual({ action: "deleted", id: memoryId });
36353676
expect(queryDeleted.content?.[0]?.text).toContain("Forgotten");
3677+
expect(isToolResultError(queryDeleted)).toBe(false);
3678+
const successTerminal = createContractToolTerminalObserver("forget-query-positive")({
3679+
toolName: "memory_forget",
3680+
arguments: { query: "concise replies" },
3681+
outcome: "success",
3682+
ownerMutation: { ownerKey: '["memory-lancedb","memory_forget"]' },
3683+
});
3684+
expect(
3685+
buildContractReplyPayloads({
3686+
assistantText: "Done — I forgot that memory.",
3687+
lastToolError: successTerminal.lastToolError,
3688+
}),
3689+
).toEqual([expect.objectContaining({ text: "Done — I forgot that memory." })]);
3690+
3691+
const unrelatedNotFound = { details: { action: "not_found" } };
3692+
expect(isToolResultError(unrelatedNotFound)).toBe(false);
3693+
const recallTerminal = createContractToolTerminalObserver("recall-not-found")({
3694+
toolName: "memory_recall",
3695+
arguments: { query: "concise replies" },
3696+
outcome: "success",
3697+
});
3698+
expect(
3699+
buildContractReplyPayloads({
3700+
assistantText: "No matching memory was found.",
3701+
lastToolError: recallTerminal.lastToolError,
3702+
}),
3703+
).toEqual([expect.objectContaining({ text: "No matching memory was found." })]);
36363704
expect(deleteRows).toHaveBeenCalledTimes(3);
36373705
},
36383706
});

‎extensions/memory-lancedb/index.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ export {
8282
shouldCapture,
8383
} from "./memory-policy.js";
8484

85-
function memoryNotFoundResult(id: string) {
85+
function memoryDeleteFailureResult(id: string) {
86+
const error = `Memory ${id} was not deleted because it was not found.`;
8687
return {
87-
content: [{ type: "text" as const, text: `Memory ${id} was not found.` }],
88-
details: { action: "not_found", id },
88+
content: [{ type: "text" as const, text: error }],
89+
details: { action: "not_found", status: "error", error, id },
8990
};
9091
}
9192

@@ -442,7 +443,7 @@ export default definePluginEntry({
442443
if (memoryId) {
443444
const deleted = await db.delete(agentId, memoryId);
444445
if (!deleted) {
445-
return memoryNotFoundResult(memoryId);
446+
return memoryDeleteFailureResult(memoryId);
446447
}
447448
return {
448449
content: [{ type: "text", text: `Memory ${memoryId} forgotten.` }],
@@ -469,7 +470,7 @@ export default definePluginEntry({
469470
if (singleResult && singleResult.score > 0.9) {
470471
const deleted = await db.delete(agentId, singleResult.entry.id);
471472
if (!deleted) {
472-
return memoryNotFoundResult(singleResult.entry.id);
473+
return memoryDeleteFailureResult(singleResult.entry.id);
473474
}
474475
return {
475476
content: [{ type: "text", text: `Forgotten: "${singleResult.entry.text}"` }],

0 commit comments

Comments
 (0)