history.jsonl 跨会话注入导致上下文污染 Bug Description history.jsonl 跨会话注入导致上下文污染 问题描述 history.jsonl…
- User
history.jsonl跨会话注入导致上下文污染Bug Description
history.jsonl跨会话注入导致上下文污染问题描述
history.jsonl中的每条 entry 来源于 单个会话 的消息摘要(通过Consolidator.archive()生成),但ContextBuilder.build_system_prompt()在注入# Recent History时,未做会话隔离,会将所有会话尚未被 Dream 处理的 entry 全部混入当前会话的 system prompt。数据流分析
写入路径(
Consolidator)Consolidator.archive()被三个调用方触发,均传入 per-session 的消息块:# memory.py:578, 751, 824 chunk = session.messages[session.last_consolidated:end_idx] # 单会话切片 summary = await self.archive(chunk) self.store.append_history(summary, max_chars=_ARCHIVE_SUMMARY_MAX_CHARS)每条写入的 JSONL entry 格式为:
{"cursor": N, "timestamp": "2026-06-09 12:00", "content": "用户偏好深色主题..."}注意:entry 中没有
session_key或chat_id字段,无法追溯来源会话。注入路径(
ContextBuilder)# context.py:64-71 entries = self.memory.read_unprocessed_history(since_cursor=self.memory.get_last_dream_cursor()) if entries: capped = entries[-self._MAX_RECENT_HISTORY:] history_text = "\n".join( f"- [{e['timestamp']}] {e['content']}" for e in capped ) history_text = truncate_text(history_text, self._MAX_HISTORY_CHARS) parts.append("# Recent History\n\n" + history_text)read_unprocessed_history(since_cursor)读取的是 cursor > dream_cursor 的全部条目,不区分来源会话,无任何 session 隔离。问题场景
时序 事件 dream_cursorT1 微信会话 A 触发 Consolidator,写入 2 条摘要 entry 0 T2 Telegram 会话 B 触发 Consolidator,写入 1 条摘要 entry 0 T3 用户打开 Telegram 会话 B,调用 build_system_prompt读取 cursor>0 的 全部 3 条,混入会话 B 的 system prompt 此时会话 B 的
# Recent History中包含了微信会话 A 的摘要,而该摘要是从微信会话提炼的,可能包含:- 微信特定的事实或上下文
- 与 Telegram 会话完全无关的用户偏好
- 尚未被 Dream 消化/验证的原始摘要
影响评估
- 会话上下文污染 — 当前会话的 system prompt 被注入无关会话的事实,可能干扰 LLM 判断
- Assistant
我会沿着归档写入、prompt 注入、会话清理和 MCP 关闭四条路径逐一核对,直接实现并补齐针对跨会话隔离与 unified-session 规则的测试。先读取仓库约束和相关代码。
- Assistant
仓库没有安装
rg,我会用grep/find继续。当前约束强调 history 原子写不能被破坏;修复会保留现有持久化机制,只扩展 entry 元数据和读取过滤。 - Assistant
已确认污染点之外还有三处必须同步修复:文件上限触发的 raw archive、idle/replay consolidation、以及
/new的后台归档。另一个关闭竞态是close_mcp()只等待一次任务快照;我会改成可递归排空的任务集合,确保归档任务衍生出的后台任务也在关闭前完成。 - Assistant
核心隔离测试已经开始运行;静态检查只发现测试文件中的导入顺序和一个未使用变量。我先修正这两项,然后读取完整测试结果。
- Assistant
相关回归集现已全部通过(242 项),覆盖了会话过滤、unified-session、idle compaction、
/new和关闭排空。接下来跑完整 Python 测试与全包 lint,检查新增参数是否影响其他调用方。 - Assistant
完整测试目前运行到约 30%,已出现 1 个失败;其余仍在继续(其中有慢速超时类用例)。全仓 lint 同时暴露了 105 个既有、与本次改动无关的问题;本次涉及文件的 lint 已单独通过。测试结束后我会定位那个实际失败。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.