From 21dedb3616bd35f6dcdcd91c7d0c8e37834c773d Mon Sep 17 00:00:00 2001 From: autodev Date: Tue, 30 Jun 2026 11:37:08 +0800 Subject: [PATCH] feat(memos-local-openclaw): add recall.autoRecallMinQueryLength config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Auto-recall previously fired on every prompt down to 2 characters, injecting unrelated history when users typed one-word confirmations like "好的", "可以", "运行", "继续", "?", "👍". Reporter (#1347) documented the noise injection, token waste, and behavioural drift this caused, and could only disable the symptom by switching off the whole auto-recall path via `allowPromptInjection=false`. Add a configurable threshold (`recall.autoRecallMinQueryLength`, default 4) that the `before_prompt_build` hook checks against the normalised query length before searching. Default of 4 filters every reporter-listed example phrase while keeping legitimate short queries ("fix bug" / "what is X") usable. Users wanting the more aggressive suggestion (10) can opt in; setting 0 restores pre-feature behaviour. Refs: #1347 Co-Authored-By: Claude Opus 4.7 (1M context) --- apps/memos-local-openclaw/README.md | 3 ++- apps/memos-local-openclaw/index.ts | 5 ++-- apps/memos-local-openclaw/src/config.ts | 1 + apps/memos-local-openclaw/src/types.ts | 22 ++++++++++++++++ .../memos-local-openclaw/tests/config.test.ts | 25 +++++++++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/apps/memos-local-openclaw/README.md b/apps/memos-local-openclaw/README.md index f2a9df64f..ce660e035 100644 --- a/apps/memos-local-openclaw/README.md +++ b/apps/memos-local-openclaw/README.md @@ -519,7 +519,8 @@ All optional — shown with defaults: "rrfK": 60, // RRF fusion constant "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 + "vectorSearchMaxChunks": 0, // 0 = search all (default). Set 200000–300000 only if search is slow on huge DBs + "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": { "similarityThreshold": 0.75, // Cosine similarity for smart-dedup candidates (Top-5) diff --git a/apps/memos-local-openclaw/index.ts b/apps/memos-local-openclaw/index.ts index 2f46052ee..9c2ce7e1f 100644 --- a/apps/memos-local-openclaw/index.ts +++ b/apps/memos-local-openclaw/index.ts @@ -1882,8 +1882,9 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`, const query = normalizeAutoRecallQuery(rawPrompt); recallQuery = query; - if (query.length < 2) { - ctx.log.debug("auto-recall: extracted query too short, skipping"); + const minQueryLength = ctx.config.recall?.autoRecallMinQueryLength ?? DEFAULTS.autoRecallMinQueryLength; + if (query.length < minQueryLength) { + ctx.log.debug(`auto-recall: query too short (len=${query.length} < minQueryLength=${minQueryLength}), skipping`); return; } ctx.log.debug(`auto-recall: query="${query.slice(0, 80)}"`); diff --git a/apps/memos-local-openclaw/src/config.ts b/apps/memos-local-openclaw/src/config.ts index 150b09cc4..6e588f10c 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, + autoRecallMinQueryLength: cfg.recall?.autoRecallMinQueryLength ?? DEFAULTS.autoRecallMinQueryLength, }, 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 cb08eb1cf..78160eb78 100644 --- a/apps/memos-local-openclaw/src/types.ts +++ b/apps/memos-local-openclaw/src/types.ts @@ -312,6 +312,20 @@ 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; + /** + * Minimum length (in UTF-16 code units, i.e. `string.length`) of the + * normalised auto-recall query. When the user prompt is shorter than + * this threshold (e.g. one-word confirmations like "好的", "可以", + * "运行", "继续", "?"), the `before_prompt_build` hook skips + * auto-recall to avoid injecting noise into the agent context. + * + * Only affects the *automatic* recall path. Explicit `memory_search` + * tool calls are unaffected — the agent / user opted in. + * + * Default: 4. Set to 0 to disable the guard (run auto-recall on + * every prompt regardless of length). + */ + autoRecallMinQueryLength?: number; }; dedup?: { similarityThreshold?: number; @@ -337,6 +351,14 @@ export const DEFAULTS = { mmrLambda: 0.7, recencyHalfLifeDays: 14, vectorSearchMaxChunks: 0, + /** + * Default minimum length of the normalised auto-recall query before + * the `before_prompt_build` hook will run a memory search. Filters + * out one-word language tokens (e.g. "好的", "可以", "运行", "继续", + * "?", "👍") that would otherwise inject unrelated history into the + * agent context. See `MemosLocalConfig.recall.autoRecallMinQueryLength`. + */ + autoRecallMinQueryLength: 4, dedupSimilarityThreshold: 0.80, evidenceWrapperTag: "STORED_MEMORY", excerptMinChars: 200, diff --git a/apps/memos-local-openclaw/tests/config.test.ts b/apps/memos-local-openclaw/tests/config.test.ts index 072728b9d..a035770f0 100644 --- a/apps/memos-local-openclaw/tests/config.test.ts +++ b/apps/memos-local-openclaw/tests/config.test.ts @@ -1,5 +1,30 @@ import { describe, expect, it } from "vitest"; import { resolveConfig } from "../src/config"; +import { DEFAULTS } from "../src/types"; + +describe("resolveConfig recall.autoRecallMinQueryLength", () => { + it("defaults to DEFAULTS.autoRecallMinQueryLength when not set", () => { + const resolved = resolveConfig({}, "/tmp/memos-config-min-query-default"); + expect(resolved.recall?.autoRecallMinQueryLength).toBe(DEFAULTS.autoRecallMinQueryLength); + expect(resolved.recall?.autoRecallMinQueryLength).toBe(4); + }); + + it("preserves an explicit override", () => { + const resolved = resolveConfig( + { recall: { autoRecallMinQueryLength: 10 } }, + "/tmp/memos-config-min-query-override", + ); + expect(resolved.recall?.autoRecallMinQueryLength).toBe(10); + }); + + it("preserves 0 explicitly (disables the short-query skip)", () => { + const resolved = resolveConfig( + { recall: { autoRecallMinQueryLength: 0 } }, + "/tmp/memos-config-min-query-zero", + ); + expect(resolved.recall?.autoRecallMinQueryLength).toBe(0); + }); +}); describe("resolveConfig", () => { it("injects openclaw providers into existing blocks when host capabilities are enabled", () => {