Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.

Commit dd5e3ed

Browse files
fix(hsg): sanitise raw API content blocks before memory storage
Add sanitise_content() called at the top of add_hsg_memory() to strip {"type": "thinking"}, {"type": "tool_use"}, and {"type": "tool_result"} JSON blocks before they reach the DB. Memories that reduce to fewer than 20 chars after sanitisation are rejected with a clear error. Prevents Claude's internal extended thinking monologue from accumulating as noise in the memory store when /remember or similar skills are run. Co-Authored-By: Claude <noreply@anthropic.com> AI-Generated: true
1 parent 5537e0b commit dd5e3ed

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

  • packages/openmemory-js/src/memory

packages/openmemory-js/src/memory/hsg.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,22 @@ async function ensure_user_exists(user_id: string): Promise<void> {
10491049
}
10501050
}
10511051

1052+
/**
1053+
* Strip raw Claude API content blocks from memory content before storage.
1054+
* Thinking blocks, tool_use blocks, and tool_result blocks are internal
1055+
* artefacts that should never be persisted verbatim.
1056+
*/
1057+
function sanitise_content(raw: string): string {
1058+
// Match JSON objects with "type": "<block_type>" — handles whitespace variants
1059+
const block_types = ["thinking", "tool_use", "tool_result"];
1060+
let out = raw;
1061+
for (const t of block_types) {
1062+
// Non-greedy match of { ... } containing the type discriminant
1063+
out = out.replace(new RegExp(`\\{[^{}]*"type"\\s*:\\s*"${t}"[\\s\\S]*?\\}`, "g"), "");
1064+
}
1065+
return out.trim();
1066+
}
1067+
10521068
export async function add_hsg_memory(
10531069
content: string,
10541070
tags?: string,
@@ -1063,6 +1079,12 @@ export async function add_hsg_memory(
10631079
deduplicated?: boolean;
10641080
upserted?: boolean;
10651081
}> {
1082+
// Strip raw API content blocks (thinking, tool_use, tool_result) before storage
1083+
content = sanitise_content(content);
1084+
if (content.length < 20) {
1085+
throw new Error("memory_content_empty_after_sanitise");
1086+
}
1087+
10661088
// Named upsert: if upsert_key matches an existing memory, update in-place
10671089
if (upsert_key) {
10681090
const existing_uk = await q.get_mem_by_upsert_key.get(upsert_key);

0 commit comments

Comments
 (0)