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
Now I have the full picture. Let me implement the fix.
1.
memory.py—append_historywithsession_key: - Assistant
2. Add
read_recent_history_for_promptwith session isolation: - Assistant
4. Update all
archive()call sites insideConsolidator: - Assistant
5.
context.py— ContextBuilder session-scoped injection: - Assistant
6.
loop.py— wire session keys through: - Assistant
Now the two
enforce_file_capraw-archive callbacks: - Assistant
7.
/newarchival inbuiltin.py: - Assistant
Now run lint and the relevant test suites:
- Assistant
These tests encode the old unscoped behavior — update them to the new session-scoped contract:
- Assistant
All agent tests pass. Now run the full test suite while I add regression tests for the new behavior:
- Assistant
Now add regression tests to
test_memory_store.py: - Assistant
Now add a Consolidator-level test verifying archive paths tag entries:
- Assistant
The diff covers every contract item. Now waiting on the full suite result:
- Assistant
While waiting, run a quick end-to-end simulation of the exact bug scenario from the report:
- Assistant
The exact scenario from the bug report is fixed. Now I'll wait for the full test suite to complete before summarizing.
- Assistant
Waiting on the suite — the monitor will fire on completion.
- Assistant
That failure reproduces on a clean checkout of HEAD () with my changes stashed — it's pre-existing and unrelated (a webui MCP-preset test, likely environment-dependent). Everything else passes: , including all agent/memory/context/consolidator tests and the new regression tests.
- Assistant
That's just the leftover duplicate monitor timing out — the suite result was already captured (4083 passed, 3 skipped, 1 pre-existing unrelated failure). No action needed; the fix is complete and verified.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.