diff --git a/apps/memos-local-openclaw/README.md b/apps/memos-local-openclaw/README.md index f851316b8..851ebf024 100644 --- a/apps/memos-local-openclaw/README.md +++ b/apps/memos-local-openclaw/README.md @@ -521,6 +521,7 @@ All optional — shown with defaults: "mmrLambda": 0.7, // MMR relevance vs diversity (0-1) "recencyHalfLifeDays": 14, // Time decay half-life "vectorSearchMaxChunks": 0, // 0 = search all (default). Set 200000–300000 only if search is slow on huge DBs + "topicJudgeRounds": 0, // Optional LLM pre-filter. Positive values skip auto-recall when the prompt continues the current topic. 0 disables it. "autoRecallMinQueryLength": 4 // Skip auto-recall when the normalised user prompt is shorter than this many chars (filters "好的"/"可以"/"?"). 0 disables the guard. Has no effect on explicit memory_search tool calls. }, "dedup": { diff --git a/apps/memos-local-openclaw/index.ts b/apps/memos-local-openclaw/index.ts index 89152f2e6..2522ade84 100644 --- a/apps/memos-local-openclaw/index.ts +++ b/apps/memos-local-openclaw/index.ts @@ -18,6 +18,7 @@ import { Embedder } from "./src/embedding"; import { IngestWorker } from "./src/ingest/worker"; import { RecallEngine } from "./src/recall/engine"; import { shouldSkipAutoRecallForSession } from "./src/recall/session-policy"; +import { topicJudgePreFilter } from "./src/recall/topic-judge"; import { captureMessages, stripInboundMetadata } from "./src/capture"; import { DEFAULTS } from "./src/types"; import type { SearchHit } from "./src/types"; @@ -1846,6 +1847,19 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`, } ctx.log.debug(`auto-recall: query="${query.slice(0, 80)}"`); + const topicDecision = await topicJudgePreFilter({ + messages: event.messages, + query, + topicJudgeRounds: ctx.config.recall?.topicJudgeRounds ?? DEFAULTS.topicJudgeRounds, + isNewSession, + summarizer, + log: ctx.log, + }); + if (topicDecision === "skip") { + ctx.log.debug("auto-recall: topic continuation detected, skipping recall"); + return; + } + // ── Phase 1: Local search ∥ Hub search (parallel) ── // Issue #1514: previously hardcoded maxResults: 10 here, which made // `recall.maxResultsDefault` and the new `recall.autoRecallMaxResults` diff --git a/apps/memos-local-openclaw/src/config.ts b/apps/memos-local-openclaw/src/config.ts index 1691ec482..28115be01 100644 --- a/apps/memos-local-openclaw/src/config.ts +++ b/apps/memos-local-openclaw/src/config.ts @@ -74,6 +74,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 ?? DEFAULTS.topicJudgeRounds, autoRecallMinQueryLength: cfg.recall?.autoRecallMinQueryLength ?? DEFAULTS.autoRecallMinQueryLength, }, dedup: { diff --git a/apps/memos-local-openclaw/src/recall/topic-judge.ts b/apps/memos-local-openclaw/src/recall/topic-judge.ts new file mode 100644 index 000000000..09c8563cd --- /dev/null +++ b/apps/memos-local-openclaw/src/recall/topic-judge.ts @@ -0,0 +1,138 @@ +import { stripInboundMetadata } from "../capture"; +import type { Logger } from "../types"; + +export type TopicJudgeDecision = "skip" | "proceed"; + +interface TopicJudgeSummarizer { + judgeNewTopic(currentContext: string, newMessage: string): Promise; +} + +interface TopicJudgeOptions { + messages: unknown[] | undefined; + query: string; + topicJudgeRounds: number; + isNewSession?: boolean; + summarizer: TopicJudgeSummarizer; + log: Logger; +} + +interface ConversationLine { + role: "user" | "assistant"; + text: string; +} + +const MAX_MESSAGES_TO_INSPECT = 20; +const MAX_CONTEXT_LINE_CHARS = 500; +const CONTEXT_EDGE_CHARS = 150; + +function extractText(content: unknown): string { + if (typeof content === "string") return content.trim(); + if (!Array.isArray(content)) return ""; + + return content + .filter( + (block): block is { type: "text"; text: string } => + typeof block === "object" + && block !== null + && (block as { type?: unknown }).type === "text" + && typeof (block as { text?: unknown }).text === "string", + ) + .map((block) => block.text) + .join(" ") + .trim(); +} + +function truncateContextLine(text: string): string { + if (text.length <= MAX_CONTEXT_LINE_CHARS) return text; + return `${text.slice(0, CONTEXT_EDGE_CHARS)}...${text.slice(-CONTEXT_EDGE_CHARS)}`; +} + +/** Build completed user/assistant rounds without the current user prompt. */ +export function buildTopicJudgeContext( + messages: unknown[] | undefined, + topicJudgeRounds: number, +): string | null { + if (!Array.isArray(messages) || !Number.isFinite(topicJudgeRounds) || topicJudgeRounds <= 0) { + return null; + } + + const merged: ConversationLine[] = []; + for (const value of messages.slice(-MAX_MESSAGES_TO_INSPECT)) { + if (typeof value !== "object" || value === null) continue; + const message = value as { role?: unknown; content?: unknown }; + if (message.role !== "user" && message.role !== "assistant") continue; + + const text = extractText(message.content); + if (!text) continue; + + const previous = merged.at(-1); + if (previous?.role === message.role) { + previous.text += `\n\n${text}`; + } else { + merged.push({ role: message.role, text }); + } + } + + // 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; + const completed = merged.slice(-maxLines); + while (completed.length > 0 && completed[0].role !== "user") completed.shift(); + while (completed.length > 0 && completed.at(-1)?.role !== "assistant") completed.pop(); + + const contextLines = completed.flatMap(({ role, text }) => { + const cleaned = role === "user" ? stripInboundMetadata(text) : text.trim(); + if (!cleaned) return []; + return [`${role === "user" ? "USER" : "ASSISTANT"}: ${truncateContextLine(cleaned)}`]; + }); + + if ( + contextLines.length < 2 + || !contextLines.some((line) => line.startsWith("USER:")) + || !contextLines.some((line) => line.startsWith("ASSISTANT:")) + ) { + return null; + } + + return contextLines.join("\n"); +} + +/** + * Skip auto-recall only when topic classification conclusively returns SAME. + * Missing context, parse failures, and provider errors all fail open. + */ +export async function topicJudgePreFilter({ + messages, + query, + topicJudgeRounds, + isNewSession, + summarizer, + log, +}: TopicJudgeOptions): Promise { + 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"; + if (isNewTopic === true) return "proceed"; + + log.warn("auto-recall: topic-judge returned no parseable decision; proceeding"); + return "proceed"; + } catch (error) { + log.warn(`auto-recall: topic-judge failed (${String(error)}); proceeding`); + return "proceed"; + } +} diff --git a/apps/memos-local-openclaw/src/types.ts b/apps/memos-local-openclaw/src/types.ts index 0eec78cbe..de522ff22 100644 --- a/apps/memos-local-openclaw/src/types.ts +++ b/apps/memos-local-openclaw/src/types.ts @@ -345,6 +345,12 @@ 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 completed conversation rounds used to decide whether the + * current prompt continues the same topic. Same-topic prompts skip + * auto-recall. Default: 0 (disabled); set a positive value to opt in. + */ + topicJudgeRounds?: number; /** * Minimum length (in UTF-16 code units, i.e. `string.length`) of the * normalised auto-recall query. When the user prompt is shorter than @@ -384,6 +390,8 @@ export const DEFAULTS = { mmrLambda: 0.7, recencyHalfLifeDays: 14, vectorSearchMaxChunks: 0, + /** Topic judging is opt-in because it adds an LLM call before auto-recall. */ + topicJudgeRounds: 0, /** * Default minimum length of the normalised auto-recall query before * the `before_prompt_build` hook will run a memory search. Filters diff --git a/apps/memos-local-openclaw/tests/config.test.ts b/apps/memos-local-openclaw/tests/config.test.ts index 2835902c6..3e1b3c1bb 100644 --- a/apps/memos-local-openclaw/tests/config.test.ts +++ b/apps/memos-local-openclaw/tests/config.test.ts @@ -2,6 +2,22 @@ import { describe, expect, it } from "vitest"; import { resolveConfig } from "../src/config"; import { DEFAULTS } from "../src/types"; +describe("resolveConfig recall.topicJudgeRounds", () => { + it("defaults to disabled", () => { + const resolved = resolveConfig({}, "/tmp/memos-config-topic-judge-default"); + expect(resolved.recall?.topicJudgeRounds).toBe(DEFAULTS.topicJudgeRounds); + expect(resolved.recall?.topicJudgeRounds).toBe(0); + }); + + it("preserves an explicit opt-in value", () => { + const resolved = resolveConfig( + { recall: { topicJudgeRounds: 4 } }, + "/tmp/memos-config-topic-judge-override", + ); + expect(resolved.recall?.topicJudgeRounds).toBe(4); + }); +}); + describe("resolveConfig recall.autoRecallMinQueryLength", () => { it("defaults to DEFAULTS.autoRecallMinQueryLength when not set", () => { const resolved = resolveConfig({}, "/tmp/memos-config-min-query-default"); 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 new file mode 100644 index 000000000..bfaed4ad4 --- /dev/null +++ b/apps/memos-local-openclaw/tests/topic-judge-pre-filter.test.ts @@ -0,0 +1,177 @@ +import { describe, expect, it, vi } from "vitest"; +import { buildTopicJudgeContext, topicJudgePreFilter } from "../src/recall/topic-judge"; +import type { Logger } from "../src/types"; + +const log: Logger = { + debug: vi.fn(), + info: vi.fn(), + warn: vi.fn(), + error: vi.fn(), +}; + +const conversation = [ + { role: "user", content: "How do I configure retries?" }, + { role: "assistant", content: "Set maxRetries in the client config." }, +]; + +function makeSummarizer(result: boolean | null) { + return { judgeNewTopic: vi.fn().mockResolvedValue(result) }; +} + +describe("topicJudgePreFilter", () => { + it("is disabled at zero rounds", async () => { + const summarizer = makeSummarizer(false); + + await expect(topicJudgePreFilter({ + messages: conversation, + query: "Can you show an example?", + topicJudgeRounds: 0, + summarizer, + log, + })).resolves.toBe("proceed"); + 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); + + await expect(topicJudgePreFilter({ + messages: conversation, + query: "Can you show an example?", + topicJudgeRounds: 4, + summarizer, + log, + })).resolves.toBe("skip"); + }); + + it("proceeds with recall for a NEW-topic decision", async () => { + const summarizer = makeSummarizer(true); + + await expect(topicJudgePreFilter({ + messages: conversation, + query: "What is the deployment process?", + topicJudgeRounds: 4, + summarizer, + log, + })).resolves.toBe("proceed"); + }); + + it("fails open when the classifier response cannot be parsed", async () => { + const summarizer = makeSummarizer(null); + + await expect(topicJudgePreFilter({ + messages: conversation, + query: "Can you show an example?", + topicJudgeRounds: 4, + summarizer, + log, + })).resolves.toBe("proceed"); + }); + + it("fails open when topic classification throws", async () => { + const summarizer = { + judgeNewTopic: vi.fn().mockRejectedValue(new Error("provider unavailable")), + }; + + await expect(topicJudgePreFilter({ + messages: conversation, + query: "Can you show an example?", + topicJudgeRounds: 4, + summarizer, + log, + })).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); + + await expect(topicJudgePreFilter({ + messages: [{ role: "user", content: "Current prompt" }], + query: "Current prompt", + topicJudgeRounds: 4, + summarizer, + log, + })).resolves.toBe("proceed"); + expect(summarizer.judgeNewTopic).not.toHaveBeenCalled(); + }); +}); + +describe("buildTopicJudgeContext", () => { + it("strips OpenClaw inbound metadata using the shared capture helper", () => { + const context = buildTopicJudgeContext([ + { + role: "user", + content: [ + { + type: "text", + text: "Sender (untrusted metadata):\n```json\n{\"id\":\"user-1\"}\n```\n\nHow do I configure retries?", + }, + ], + }, + { role: "assistant", content: "Set maxRetries in the client config." }, + { role: "tool", content: "internal tool output" }, + ], 4); + + expect(context).toContain("USER: How do I configure retries?"); + expect(context).toContain("ASSISTANT: Set maxRetries in the client config."); + expect(context).not.toContain("untrusted metadata"); + expect(context).not.toContain("user-1"); + expect(context).not.toContain("internal tool output"); + }); + + 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"); + }); +});