From 57aaafd52a5ea556503bf7e252296fd10ff9b23b Mon Sep 17 00:00:00 2001 From: Johnny Date: Sun, 29 Mar 2026 23:00:54 +0800 Subject: [PATCH 1/2] feat(openclaw-plugin): add topic-judge pre-filter to skip auto-recall on topic continuation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a lightweight LLM-based pre-filter before auto-recall search to avoid unnecessary embedding + vector search + LLM filter calls when the user is continuing the current conversation. - Extract topicJudgePreFilter() as a standalone function returning 'skip'|'proceed' - Configurable via recall.topicJudgeRounds (default: 4, set 0 to disable) - Uses existing Summarizer.judgeNewTopic() (already implemented in all providers) - Graceful fallback: too-few-lines → skip; LLM error → proceed with recall - Only 1 small LLM call (max_tokens=10) vs full search pipeline saved on SAME --- apps/memos-local-openclaw/index.ts | 108 ++++++++++++++++++++++++ apps/memos-local-openclaw/src/config.ts | 1 + apps/memos-local-openclaw/src/types.ts | 2 + 3 files changed, 111 insertions(+) diff --git a/apps/memos-local-openclaw/index.ts b/apps/memos-local-openclaw/index.ts index bcb2c0870..eadb974f1 100644 --- a/apps/memos-local-openclaw/index.ts +++ b/apps/memos-local-openclaw/index.ts @@ -1807,6 +1807,104 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`, // ─── Auto-recall: inject relevant memories before agent starts ─── + /** + * Pre-filter: use LLM to check if the new message continues the current topic. + * Returns "skip" to skip recall (same topic), "proceed" to run recall (new topic or disabled/error). + * + * Config: + * topicJudgeRounds = 0 → disabled, always returns "proceed" + * topicJudgeRounds > 0 → enabled, uses that many conversation rounds for context + */ + async function topicJudgePreFilter(opts: { + messages: unknown[] | undefined; + query: string; + topicJudgeRounds: number; + summarizer: Summarizer; + log: { info: (...args: any[]) => void; warn: (...args: any[]) => void; debug: (...args: any[]) => void }; + }): Promise<"skip" | "proceed"> { + const { messages, query, topicJudgeRounds, summarizer, log } = opts; + + if (topicJudgeRounds <= 0) return "proceed"; + if (!Array.isArray(messages) || messages.length < 3) return "proceed"; + + try { + const msgs = messages as Array>; + const rawMsgs = msgs.slice(-20); + const merged: Array<{ role: string; text: string }> = []; + for (const m of rawMsgs) { + const role = m.role as string; + if (role === "tool") continue; + let text = ""; + if (typeof m.content === "string") { + text = m.content; + } else if (Array.isArray(m.content)) { + for (const block of m.content as Array>) { + if (block.type === "text" && typeof block.text === "string") text += block.text + " "; + } + } + text = text.trim(); + if (!text) continue; + if (merged.length > 0 && merged[merged.length - 1].role === role) { + merged[merged.length - 1].text += "\n\n" + text; + } else { + merged.push({ role, text }); + } + } + if (merged.length > 0) merged.pop(); + + const sliceLen = topicJudgeRounds * 2; + const lastN = merged.length > sliceLen ? merged.slice(-sliceLen) : merged; + if (lastN.length > 0 && lastN[0].role !== "user") lastN.shift(); + if (lastN.length > 0 && lastN[lastN.length - 1].role !== "assistant") lastN.pop(); + + const MAX_CONTEXT_LEN = 500; + const HEAD_TAIL = 150; + const contextLines = lastN.map((m) => { + const role = (m.role === "user") ? "USER" : "ASSISTANT"; + let text = m.text; + if (role === "USER") { + const senderIdx = text.lastIndexOf("Sender (untrusted metadata):"); + if (senderIdx > 0) text = text.slice(senderIdx); + const fenceStart = text.indexOf("```json"); + const fenceEnd = fenceStart >= 0 ? text.indexOf("```\n", fenceStart + 7) : -1; + if (fenceEnd > 0) text = text.slice(fenceEnd + 4).replace(/^\s*\n/, "").trim(); + if (senderIdx < 0) { + const injectEnd = text.indexOf("rephrased query to find more.\n\n"); + if (injectEnd !== -1) { + text = text.slice(injectEnd + "rephrased query to find more.\n\n".length).trim(); + } else { + const injectEnd2 = text.indexOf("Do NOT skip this step. Do NOT answer without searching first.\n\n"); + if (injectEnd2 !== -1) { + text = text.slice(injectEnd2 + "Do NOT skip this step. Do NOT answer without searching first.\n\n".length).trim(); + } + } + } + } + text = text.replace(/^\[.*?\]\s*/, "").trim(); + if (text.length > MAX_CONTEXT_LEN) { + text = text.slice(0, HEAD_TAIL) + "..." + text.slice(-HEAD_TAIL); + } + return `${role}: ${text.trim()}`; + }).filter((l) => l.split(": ")[1]?.length > 0); + + if (contextLines.length < 2) { + log.info(`[auto-recall] topic-judge: too-few-lines (${contextLines.length}), skip recall`); + return "skip"; + } + + const currentContext = contextLines.join("\n"); + log.info(`[auto-recall] topic-judge: lines=${contextLines.length}, query="${query.slice(0, 60)}"`); + const isNew = await summarizer.judgeNewTopic(currentContext, query); + const topicResult = isNew === true ? "NEW" : isNew === false ? "SAME" : `ERROR(${isNew})`; + log.info(`[auto-recall] topic-judge: result=${topicResult}`); + + return isNew === false ? "skip" : "proceed"; + } catch (judgeErr) { + log.warn(`[auto-recall] topic-judge error="${judgeErr}", fallback=proceed`); + return "proceed"; + } + } + api.on("before_prompt_build", async (event: { prompt?: string; messages?: unknown[] }, hookCtx?: { agentId?: string; sessionKey?: string }) => { if (!allowPromptInjection) return {}; if (!event.prompt || event.prompt.length < 3) return; @@ -1849,6 +1947,16 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`, } ctx.log.debug(`auto-recall: query="${query.slice(0, 80)}"`); + // ─── Pre-filter: topic-judge to skip recall on topic continuation ─── + const shouldSkipRecall = await topicJudgePreFilter({ + messages: event.messages, + query, + topicJudgeRounds: ctx.config.recall?.topicJudgeRounds ?? 4, + summarizer, + log: ctx.log, + }); + if (shouldSkipRecall === "skip") return; + const result = await engine.search({ query, maxResults: 10, minScore: 0.45, ownerFilter: recallOwnerFilter }); // Hub fallback helper: search team shared memories when local search has no relevant results diff --git a/apps/memos-local-openclaw/src/config.ts b/apps/memos-local-openclaw/src/config.ts index 150b09cc4..e88965d62 100644 --- a/apps/memos-local-openclaw/src/config.ts +++ b/apps/memos-local-openclaw/src/config.ts @@ -66,6 +66,7 @@ export function resolveConfig(raw: Partial | undefined, stateD mmrLambda: cfg.recall?.mmrLambda ?? DEFAULTS.mmrLambda, recencyHalfLifeDays: cfg.recall?.recencyHalfLifeDays ?? DEFAULTS.recencyHalfLifeDays, vectorSearchMaxChunks: cfg.recall?.vectorSearchMaxChunks ?? DEFAULTS.vectorSearchMaxChunks, + topicJudgeRounds: cfg.recall?.topicJudgeRounds ?? 4, }, dedup: { similarityThreshold: cfg.dedup?.similarityThreshold ?? DEFAULTS.dedupSimilarityThreshold, diff --git a/apps/memos-local-openclaw/src/types.ts b/apps/memos-local-openclaw/src/types.ts index 88a853f09..b0d4603ac 100644 --- a/apps/memos-local-openclaw/src/types.ts +++ b/apps/memos-local-openclaw/src/types.ts @@ -312,6 +312,8 @@ export interface MemosLocalConfig { recencyHalfLifeDays?: number; /** Cap vector search to this many most recent chunks. 0 = no cap (search all; may get slower with 200k+ chunks). If you set a cap for performance, use a large value (e.g. 200000–300000) so older memories are still in the window; FTS always searches all. */ vectorSearchMaxChunks?: number; + /** Number of conversation rounds (user+assistant pairs) used by topic-judge to determine if recall should run. 0 = disabled (always recall). Default: 4. */ + topicJudgeRounds?: number; }; dedup?: { similarityThreshold?: number; From 4ceed826884b97d8991d14b12990080e0b0f089f Mon Sep 17 00:00:00 2001 From: jiachengzhen Date: Thu, 6 Aug 2026 17:02:48 +0800 Subject: [PATCH 2/2] fix(openclaw-plugin): fail open in topic judge --- apps/memos-local-openclaw/index.ts | 1 + .../src/recall/topic-judge.ts | 19 ++++-- .../tests/topic-judge-pre-filter.test.ts | 59 ++++++++++++++++++- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/apps/memos-local-openclaw/index.ts b/apps/memos-local-openclaw/index.ts index 7ed7505c1..2522ade84 100644 --- a/apps/memos-local-openclaw/index.ts +++ b/apps/memos-local-openclaw/index.ts @@ -1851,6 +1851,7 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`, messages: event.messages, query, topicJudgeRounds: ctx.config.recall?.topicJudgeRounds ?? DEFAULTS.topicJudgeRounds, + isNewSession, summarizer, log: ctx.log, }); diff --git a/apps/memos-local-openclaw/src/recall/topic-judge.ts b/apps/memos-local-openclaw/src/recall/topic-judge.ts index 3d2f00241..09c8563cd 100644 --- a/apps/memos-local-openclaw/src/recall/topic-judge.ts +++ b/apps/memos-local-openclaw/src/recall/topic-judge.ts @@ -11,6 +11,7 @@ interface TopicJudgeOptions { messages: unknown[] | undefined; query: string; topicJudgeRounds: number; + isNewSession?: boolean; summarizer: TopicJudgeSummarizer; log: Logger; } @@ -72,7 +73,8 @@ export function buildTopicJudgeContext( } } - // OpenClaw commonly includes the current prompt as the final user message. + // before_prompt_build passes the current prompt separately. A trailing user + // message therefore represents an incomplete historical round. if (merged.at(-1)?.role === "user") merged.pop(); const maxLines = Math.floor(topicJudgeRounds) * 2; @@ -105,18 +107,23 @@ export async function topicJudgePreFilter({ messages, query, topicJudgeRounds, + isNewSession, summarizer, log, }: TopicJudgeOptions): Promise { - if (topicJudgeRounds <= 0) return "proceed"; - - const currentContext = buildTopicJudgeContext(messages, topicJudgeRounds); - if (!currentContext) { - log.debug("auto-recall: topic-judge has insufficient completed context; proceeding"); + if (!Number.isFinite(topicJudgeRounds) || topicJudgeRounds <= 0) return "proceed"; + if (isNewSession) { + log.debug("auto-recall: new session detected; bypassing topic-judge"); return "proceed"; } try { + const currentContext = buildTopicJudgeContext(messages, topicJudgeRounds); + if (!currentContext) { + log.debug("auto-recall: topic-judge has insufficient completed context; proceeding"); + return "proceed"; + } + log.debug(`auto-recall: topic-judge evaluating ${currentContext.split("\n").length} context lines`); const isNewTopic = await summarizer.judgeNewTopic(currentContext, query); if (isNewTopic === false) return "skip"; diff --git a/apps/memos-local-openclaw/tests/topic-judge-pre-filter.test.ts b/apps/memos-local-openclaw/tests/topic-judge-pre-filter.test.ts index ac568d978..bfaed4ad4 100644 --- a/apps/memos-local-openclaw/tests/topic-judge-pre-filter.test.ts +++ b/apps/memos-local-openclaw/tests/topic-judge-pre-filter.test.ts @@ -12,7 +12,6 @@ const log: Logger = { const conversation = [ { role: "user", content: "How do I configure retries?" }, { role: "assistant", content: "Set maxRetries in the client config." }, - { role: "user", content: "Can you show an example?" }, ]; function makeSummarizer(result: boolean | null) { @@ -33,6 +32,33 @@ describe("topicJudgePreFilter", () => { expect(summarizer.judgeNewTopic).not.toHaveBeenCalled(); }); + it("treats non-finite round configuration as disabled", async () => { + const summarizer = makeSummarizer(false); + + await expect(topicJudgePreFilter({ + messages: conversation, + query: "Can you show an example?", + topicJudgeRounds: Number.NaN, + summarizer, + log, + })).resolves.toBe("proceed"); + expect(summarizer.judgeNewTopic).not.toHaveBeenCalled(); + }); + + it("bypasses topic classification for a new session", async () => { + const summarizer = makeSummarizer(false); + + await expect(topicJudgePreFilter({ + messages: conversation, + query: "Can you show an example?", + topicJudgeRounds: 4, + isNewSession: true, + summarizer, + log, + })).resolves.toBe("proceed"); + expect(summarizer.judgeNewTopic).not.toHaveBeenCalled(); + }); + it("skips recall for a SAME-topic decision", async () => { const summarizer = makeSummarizer(false); @@ -83,6 +109,24 @@ describe("topicJudgePreFilter", () => { })).resolves.toBe("proceed"); }); + it("fails open when context construction throws", async () => { + const malformedMessage = Object.defineProperty({}, "role", { + get() { + throw new Error("invalid message"); + }, + }); + const summarizer = makeSummarizer(false); + + await expect(topicJudgePreFilter({ + messages: [malformedMessage], + query: "Can you show an example?", + topicJudgeRounds: 4, + summarizer, + log, + })).resolves.toBe("proceed"); + expect(summarizer.judgeNewTopic).not.toHaveBeenCalled(); + }); + it("fails open when there is no completed conversation round", async () => { const summarizer = makeSummarizer(false); @@ -111,7 +155,6 @@ describe("buildTopicJudgeContext", () => { }, { role: "assistant", content: "Set maxRetries in the client config." }, { role: "tool", content: "internal tool output" }, - { role: "user", content: "Can you show an example?" }, ], 4); expect(context).toContain("USER: How do I configure retries?"); @@ -119,6 +162,16 @@ describe("buildTopicJudgeContext", () => { expect(context).not.toContain("untrusted metadata"); expect(context).not.toContain("user-1"); expect(context).not.toContain("internal tool output"); - expect(context).not.toContain("Can you show an example?"); + }); + + it("excludes a trailing user message because it is not a completed round", () => { + const context = buildTopicJudgeContext([ + ...conversation, + { role: "user", content: "Interrupted historical prompt" }, + ], 4); + + expect(context).toContain("USER: How do I configure retries?"); + expect(context).toContain("ASSISTANT: Set maxRetries in the client config."); + expect(context).not.toContain("Interrupted historical prompt"); }); });