ISSUE DESCRIPTION history.jsonl 跨会话注入导致上下文污染 Bug Description history.jsonl 跨会话注入导致上下文污染…
- User
ISSUE DESCRIPTION
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
I'll explore the repository structure and locate the memory/context code paths involved in the history.jsonl cross-session pollution issue.
- Assistant
Next I'll inspect
memory.py,context.py, and related tests for history archival and prompt injection. - Assistant
rg is not found, let me use grep instead.
- Assistant
Let me read the relevant sections of memory.py and context.py in detail.
- Assistant
Let me read more of memory.py - particularly archive, raw_archive, consolidator methods, and also search for session clear and MCP drained.
- Assistant
I'll read consolidator archive paths,
/newhandling, close_mcp, and related tests next. - Assistant
Let me read more of the consolidator and relevant tests more carefully.
- Assistant
Let me read the rest of the consolidator code and related tests about history and /new
- Assistant
I'll inspect archive call sites and how context/loop build the system prompt so session_key can be threaded through.
- Assistant
Let me also look at the rest of memory.py for maybe_consolidate and archive_unconsolidated / compact methods, and see enforce_file_cap, close_mcp and build_messages signature in the loop.
- Assistant
I'll check
compact_idle_session, related docs, and the evaluation case folder for expected behavior. - Assistant
Checking how heartbeat/cron session keys are formed and reviewing design notes.
- Assistant
Inspecting how the loop wires
build_messagesand Dream history reads. - Assistant
Searching for all call sites that write to or filter history entries.
- Assistant
I have enough context to implement the changes. Updating
memory.pyfirst. - Assistant
Implementing core memory store and consolidator changes.
- Assistant
I'll navigate to the right line first with goto.
- Assistant
Let me try the edit command again now that we're at the right place.
- Assistant
Now I'll add
read_recent_history_for_promptand updateraw_archive. - Assistant
I'll add read_recent_history_for_prompt after read_unprocessed_history, and also define a helper for internal session keys.
- Assistant
I'll fix the missing blank line and update
raw_archive. - Assistant
Updating
raw_archiveand the consolidator archive paths next. - Assistant
Let me read those sections and update them.
- Assistant
I'll update raw_archive, Consolidator.init, archive method and all call sites.
- Assistant
Updating Consolidator to accept and propagate
session_key/unified_session. - Assistant
Removing the duplicate
__init__body lines. - Assistant
Updating archive call sites and the archive method itself.
- Assistant
Let me update the relevant parts.
- Assistant
Continue with estimate, archive, and other call sites.
- Assistant
Now update archive method and its call sites in maybe_consolidate and compact_idle.
- Assistant
Now update maybe_consolidate archive call and compact_idle_session.
- Assistant
Need to update those two remaining calls.
- Assistant
Update both archive calls.
- Assistant
One more archive call in compact_idle_session
- Assistant
Updating ContextBuilder to scope recent history by session.
- Assistant
Update build_system_prompt and build_messages.
- Assistant
Now update build_messages.
- Assistant
Update build_messages signature and the call to build_system_prompt.
- Assistant
There's a duplicate
},on line 247 that needs to be removed. - Assistant
Updating loop and command call sites to pass
session_keyandunified_session. - Assistant
I need to pass unified_session to Consolidator. Note that self._unified_session is set before this - let me check.
- Assistant
Good, self._unified_session is set before consolidator.
- Assistant
Now update
_build_initial_messages, otherbuild_messagescall sites, andenforce_file_cap. - Assistant
Update _build_initial_messages
- Assistant
Now find the system message build_messages and enforce_file_cap.
- Assistant
Update those three places.
- Assistant
Update system message path
- Assistant
Update the second enforce_file_cap at line around 1493 (plus offset).
- Assistant
Update line 1499.
- Assistant
Update enforce_file_cap in _state_save
- Assistant
Updating
/newarchival to includesession_key. - Assistant
Need to capture session key before clear/invalidate. The key is session.key - after clear session still has key. Let's save key before archive.
- Assistant
Verifying syntax and running a quick isolation test for the history changes.
- Assistant
Running related unit tests to ensure nothing regressed.
- Assistant
Checking the consolidator end-to-end path and whether any other call sites need updates.
- Assistant
Checking other callers of
build_system_prompt/build_messagesfor missing session_key. - Assistant
Reviewing the full diff before submission.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.