fix(memory): add minimum-length filter for auto-save messages
Every user message was auto-saved to memory regardless of length, flooding the store with trivial entries like "ok", "thanks", "hi". These noise entries competed with real memories during recall, degrading relevance — especially with keyword-only search. Skip auto-saving messages shorter than 20 characters. Applied to both the channel path (channels/mod.rs) and CLI agent path (agent/loop_.rs). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
63a59e3735
commit
ea2ff7c53b
2 changed files with 15 additions and 5 deletions
|
|
@ -26,6 +26,10 @@ const STREAM_CHUNK_MIN_CHARS: usize = 80;
|
||||||
/// Used as a safe fallback when `max_tool_iterations` is unset or configured as zero.
|
/// Used as a safe fallback when `max_tool_iterations` is unset or configured as zero.
|
||||||
const DEFAULT_MAX_TOOL_ITERATIONS: usize = 10;
|
const DEFAULT_MAX_TOOL_ITERATIONS: usize = 10;
|
||||||
|
|
||||||
|
/// Minimum user-message length (in chars) for auto-save to memory.
|
||||||
|
/// Matches the channel-side constant in `channels/mod.rs`.
|
||||||
|
const AUTOSAVE_MIN_MESSAGE_CHARS: usize = 20;
|
||||||
|
|
||||||
static SENSITIVE_KEY_PATTERNS: LazyLock<RegexSet> = LazyLock::new(|| {
|
static SENSITIVE_KEY_PATTERNS: LazyLock<RegexSet> = LazyLock::new(|| {
|
||||||
RegexSet::new([
|
RegexSet::new([
|
||||||
r"(?i)token",
|
r"(?i)token",
|
||||||
|
|
@ -1475,8 +1479,8 @@ pub async fn run(
|
||||||
let mut final_output = String::new();
|
let mut final_output = String::new();
|
||||||
|
|
||||||
if let Some(msg) = message {
|
if let Some(msg) = message {
|
||||||
// Auto-save user message to memory
|
// Auto-save user message to memory (skip short/trivial messages)
|
||||||
if config.memory.auto_save {
|
if config.memory.auto_save && msg.chars().count() >= AUTOSAVE_MIN_MESSAGE_CHARS {
|
||||||
let user_key = autosave_memory_key("user_msg");
|
let user_key = autosave_memory_key("user_msg");
|
||||||
let _ = mem
|
let _ = mem
|
||||||
.store(&user_key, &msg, MemoryCategory::Conversation, None)
|
.store(&user_key, &msg, MemoryCategory::Conversation, None)
|
||||||
|
|
@ -1597,8 +1601,10 @@ pub async fn run(
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto-save conversation turns
|
// Auto-save conversation turns (skip short/trivial messages)
|
||||||
if config.memory.auto_save {
|
if config.memory.auto_save
|
||||||
|
&& user_input.chars().count() >= AUTOSAVE_MIN_MESSAGE_CHARS
|
||||||
|
{
|
||||||
let user_key = autosave_memory_key("user_msg");
|
let user_key = autosave_memory_key("user_msg");
|
||||||
let _ = mem
|
let _ = mem
|
||||||
.store(&user_key, &user_input, MemoryCategory::Conversation, None)
|
.store(&user_key, &user_input, MemoryCategory::Conversation, None)
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,10 @@ use tokio_util::sync::CancellationToken;
|
||||||
type ConversationHistoryMap = Arc<Mutex<HashMap<String, Vec<ChatMessage>>>>;
|
type ConversationHistoryMap = Arc<Mutex<HashMap<String, Vec<ChatMessage>>>>;
|
||||||
/// Maximum history messages to keep per sender.
|
/// Maximum history messages to keep per sender.
|
||||||
const MAX_CHANNEL_HISTORY: usize = 50;
|
const MAX_CHANNEL_HISTORY: usize = 50;
|
||||||
|
/// Minimum user-message length (in chars) for auto-save to memory.
|
||||||
|
/// Messages shorter than this (e.g. "ok", "thanks") are not stored,
|
||||||
|
/// reducing noise in memory recall.
|
||||||
|
const AUTOSAVE_MIN_MESSAGE_CHARS: usize = 20;
|
||||||
|
|
||||||
/// Maximum characters per injected workspace file (matches `OpenClaw` default).
|
/// Maximum characters per injected workspace file (matches `OpenClaw` default).
|
||||||
const BOOTSTRAP_MAX_CHARS: usize = 20_000;
|
const BOOTSTRAP_MAX_CHARS: usize = 20_000;
|
||||||
|
|
@ -808,7 +812,7 @@ async fn process_channel_message(
|
||||||
let memory_context =
|
let memory_context =
|
||||||
build_memory_context(ctx.memory.as_ref(), &msg.content, ctx.min_relevance_score).await;
|
build_memory_context(ctx.memory.as_ref(), &msg.content, ctx.min_relevance_score).await;
|
||||||
|
|
||||||
if ctx.auto_save_memory {
|
if ctx.auto_save_memory && msg.content.chars().count() >= AUTOSAVE_MIN_MESSAGE_CHARS {
|
||||||
let autosave_key = conversation_memory_key(&msg);
|
let autosave_key = conversation_memory_key(&msg);
|
||||||
let _ = ctx
|
let _ = ctx
|
||||||
.memory
|
.memory
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue