Skip to content

feat(memo, runtime, web): 引入 run 边界记忆提取与语义去重,修复 Web 端 slash memo 指令#604

Merged
phantom5099 merged 6 commits into1024XEngineer:mainfrom
Yumiue:html_progress
May 10, 2026
Merged

feat(memo, runtime, web): 引入 run 边界记忆提取与语义去重,修复 Web 端 slash memo 指令#604
phantom5099 merged 6 commits into1024XEngineer:mainfrom
Yumiue:html_progress

Conversation

@Yumiue
Copy link
Copy Markdown
Collaborator

@Yumiue Yumiue commented May 10, 2026

背景

当前自动记忆提取存在两个问题:

  1. 提取窗口边界不够清晰
    触发提取时可能混入历史会话消息,导致“本次 run 的事实”与“旧上下文”交叉,影响提取稳定性与可解释性。

  2. 去重能力偏弱
    仅依赖近似“新增去重”,缺少对“已有自动提取记忆”的语义更新能力,容易重复沉淀或错过增量修正。

另外,Web 端 /memo/remember/forget 在未建立 session 时交互较硬,并且对工具返回字段大小写兼容不完整(content vs Content)。


目标

  • 将自动记忆提取严格收敛到当前 run 边界
  • 为自动提取引入语义决策协议:create / update / skip
  • 仅允许更新自动提取来源(extractor_auto)的记忆,避免覆盖用户手动记忆。
  • 修复 Web 端 slash memo 指令可用性与返回解析兼容性。

主要变更

1) Runtime:按 run 边界收集提取消息

  • 新增 run 内消息收集能力(只记录本次 run 成功写入 transcript 的消息)。
  • 触发自动记忆提取时,不再直接使用整个 session 消息,而是使用 run 边界消息切片。
  • 在 compact 等路径下也保证提取窗口不跨 run。

对应影响:

  • internal/runtime/memo.go
  • internal/runtime/run.go
  • internal/runtime/session_mutation.go
  • internal/runtime/state.go
  • internal/runtime/runtime.go

2) Context Projection:新增 memo 提取专用窗口构建

  • 新增 BuildMemoExtractionMessagesForModel(...),用于构造“完整 run + provider-safe”的提取上下文。
  • 保留合法 assistant-tool span,过滤孤立/无效 tool 片段,确保发给模型的上下文结构稳定。

对应影响:

  • internal/context/projection.go

3) Memo:语义去重与受控更新

  • 新增提取决策模型:
    • ExtractionAction: create | update | skip
    • ExtractionCandidate: 既有记忆快照(含 ref/scope/source/type/title/content)
    • ExtractionDecision: 模型返回决策结构
  • LLMExtractor 新增 ExtractDecisions(...),在 prompt 中注入 existing candidates,支持语义层面的合并/跳过判断。
  • AutoExtractor 优先走 DecisionExtractor 路径,按决策执行 create/update/skip。
  • Service 增加:
    • autoExtractionCandidates(...):加载既有记忆候选
    • updateAutoExtractIfAllowed(...):仅允许更新 source=extractor_auto 的记忆
    • parseScopedTopicKey(...) 等辅助逻辑

对应影响:

  • internal/memo/types.go
  • internal/memo/llm_extractor.go
  • internal/memo/auto_extractor.go
  • internal/memo/service.go
  • internal/memo/auto_extractor.go(决策执行路径)

4) Web:slash memo 指令修复

  • 去掉 /memo/remember/forget 对“先有 session”的硬依赖,提升可用性。
  • 新增统一解析函数,兼容系统工具返回的 payload.contentpayload.Content
  • 保留 /remember/forget 参数校验,避免空参数误调用。

对应影响:

  • web/src/components/chat/ChatInput.tsx

测试

本 PR 增补并调整了关键测试,覆盖以下场景:

  1. Runtime 边界
  • 自动提取仅使用当前 run 的 user + assistant 消息,不跨历史消息。
  • compact 后仍保持 run 边界正确。
  1. Context Projection
  • memo 专用窗口保留完整合法 span,过滤无效 tool 噪声。
  1. Memo 语义去重
  • ExtractDecisions 能读取 existing candidates 并返回 create/update/skip。
  • update 仅作用于自动提取记忆,不覆盖手动记忆。
  • create 路径仍保留本地精确去重保护。
  1. Web slash 指令
  • /memo/remember/forget 在无 session id 时可正常执行。
  • payload.Content 回退读取生效。
  • 参数缺失时保持校验与错误提示。

涉及测试文件(节选):

  • internal/runtime/runtime_test.go
  • internal/context/projection_test.go
  • internal/memo/auto_extractor_test.go
  • internal/memo/llm_extractor_test.go
  • internal/app/bootstrap_test.go
  • web/src/components/chat/ChatInput.test.tsx

兼容性与风险评估

  • 行为变化:
    • 自动记忆提取窗口从“会话级”收敛为“run 级”。
    • 模型提取结果新增 update/skip 语义分支(兼容旧格式默认 create)。
  • 风险控制:
    • 通过 source 限制,防止自动流程覆盖手动记忆。
    • 保留精确去重兜底,降低重复写入概率。
    • 通过 runtime/context/memo/web 多层单测覆盖回归风险。

变更总结

该 PR 把记忆提取从“可用”提升到“边界清晰 + 可语义维护”的状态:

  • 提取输入更干净:只看当前 run
  • 提取决策更可靠:支持 create/update/skip
  • 数据安全更可控:不碰手动记忆
  • 前端交互更顺滑:slash memo 系列可直接用、返回解析更稳

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

Copy link
Copy Markdown

@fennoai fennoai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found 3 noteworthy issues in the changed memo extraction path. No additional security-specific findings stood out beyond these.

Comment thread internal/context/projection.go
Comment thread internal/memo/llm_extractor.go
Comment thread internal/memo/service.go
@phantom5099 phantom5099 merged commit 2974bb9 into 1024XEngineer:main May 10, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants