Skip to content

添加 excludeSessionKey 选项,避免召回当前会话内容 #1362

Closed

Description

@zwcf5200

问题描述

使用 memory_search 时,当前会话的 chunks 也会被召回,导致:

  1. Token 浪费 - 重复内容被喂给 LLM
  2. 干扰对话 - 当前对话内容污染历史记忆检索
  3. 降低相关性 - 用户需要的是历史记忆,而不是刚刚说过的话

示例:

用户问:"MemOS 是什么?"
LLM 召回:[5 分钟前当前会话中的 MemOS 介绍]
这是冗余的 - 上下文已经在对话中了!

当前行为

所有搜索方法都缺少会话过滤:

// engine.ts - RecallOptions
interface RecallOptions {
  query?: string;
  maxResults?: number;
  minScore?: number;
  role?: string;
  ownerFilter?: string[];
  // 缺少: excludeSessionKey?: string;
}

// sqlite.ts - 所有搜索方法
ftsSearch(query, limit, ownerFilter?)       // 无会话过滤
vectorSearch(store, queryVec, maxChunks, ownerFilter)  // 无会话过滤
patternSearch(patterns, { role?, limit? })  // 无会话过滤

建议方案

添加 excludeSessionKey 参数:

interface RecallOptions {
  // ... 现有字段
  excludeSessionKey?: string;  // 排除该会话的 chunks
}

// 在 sqlite.ts 中
ftsSearch(query, limit, ownerFilter?, excludeSessionKey?) {
  let sql = `... WHERE chunks_fts MATCH ? AND c.dedup_status = 'active'`;
  if (excludeSessionKey) {
    sql += ` AND c.session_key != ?`;
  }
}

使用场景

OpenClaw 调用 memory_search 时,传入当前会话 key 进行排除:

const results = await memory_search({
  query: "MemOS 架构",
  excludeSessionKey: context.sessionKey  // 不召回当前对话
});

收益

  • 节省 tokens - 无当前会话的重复内容
  • 更好的相关性 - 聚焦于历史记忆
  • 更干净的上下文 - LLM 获得有用的历史上下文,而不是回声

数据参考

实际测试数据:

  • 8721 条 chunks,其中 7274 条 (77%) 是 assistant 回复
  • 每次召回可能包含刚刚生成的回复
  • 造成 tokens 浪费和上下文污染

Activity

  1. added
    types:enhancementNew feature or improvement | 新功能或改进
    area:coreMOS 编排层 / 框架底座 / 跨模块问题
    on Jun 1, 2026
  2. added
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    on Jun 29, 2026
  3. self-assigned this
    on Jun 30, 2026
  4. Memtensor-AI commented on Jun 30, 2026

    @Memtensor-AI
    Collaborator

    🤖 AutoDev has picked up this issue and started working on it.

    Task ID: 673e879094851408
    Working branch: feature/autodev-1362
    Target branch: latest dev* branch
    Workflow: opsp (analysis → coding → testing → PR)

    I will post the PR link here once done. If I need more information, I will ask in the comments.

  5. Memtensor-AI commented on Jun 30, 2026

    @Memtensor-AI
    Collaborator

    ✅ AutoDev task 673e879094851408 completed.

    Summary: Adds an optional excludeSessionKey parameter to the memory_search recall pipeline in apps/memos-local-openclaw so callers (e.g. OpenClaw) can suppress recall of chunks belonging to the current conversation session. This avoids redundant tokens and echoed content in LLM context, and surfaces only historical memories.

    Surface changes: RecallOptions.excludeSessionKey on src/recall/engine.ts; new optional argument plumbed through SqliteStore.ftsSearch, SqliteStore.patternSearch, SqliteStore.getAllEmbeddings, SqliteStore.getRecentEmbeddings, and the vectorSearch helper in src/storage/vector.ts; new excludeSessionKey input on the memory_search tool schema and handler in src/tools/memory-search.ts. The filter is implemented at the SQL layer as AND c.session_key != ?, applied only when the caller supplies the field — existing call sites are byte-for-byte equivalent. Hub-memory hits are intentionally not filtered because they carry a synthetic sessionKey (hub-shared:<userId>) and represent cross-user shared knowledge.

    Tests: 11 new vitest cases across tests/storage.test.ts, tests/memory-search-tool.test.ts, and tests/recall-engine-exclude-session.test.ts. All 37 tests across the touched suites pass; tsc --noEmit is clean. The full vitest run shows 233 passing and 5 pre-existing failures (accuracy.test.ts × 2 — local ONNX embedding model not available in the sandbox; skill-auto-install / task-processor / update-install — unrelated to recall/storage, verified by re-running them against the stashed baseline before this change).

    Confidence at Phase 1 was 0.85 so no clarification was requested; assumptions and Phase progress are logged in the opsp task file. The opsp artifacts (.ai-tasks/2026-06-29-1362-…md, openspec/changes/2026-06-29-1362-…/tasks.md + test-cases.md) stay out of the MemOS PR (via .git/info/exclude) and have been archived to the sibling memos-autodev-specs repo on main.

    Base branch: main
    Branch: feature/autodev-1362
    Commit: 9f405aa
    PR: #1978
    Assigned to: @MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller
    Reviewers: @MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller

  6. added
    ai:testingAI agent is running tests | AI 正在运行测试
    ai:pr-readyAI-created PR is ready for review | AI 生成的 PR 已等待评审
    and removed
    ai:codingAI agent is coding | AI 正在编码
    ai:testingAI agent is running tests | AI 正在运行测试
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    on Jun 30, 2026
  7. added a commit that references this issue on Jul 1, 2026
    3ee48ff
  8. added and removed
    ai:pr-readyAI-created PR is ready for review | AI 生成的 PR 已等待评审
    on Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:coreMOS 编排层 / 框架底座 / 跨模块问题types:enhancementNew feature or improvement | 新功能或改进

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions