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 start by exploring the repository structure and the relevant memory/context code to understand how history is written and injected.
- Assistant
Next I'll inspect
memory.pyandcontext.py, plus call sites that archive history. - Assistant
rg is not available, let me use grep instead.
- Assistant
Let me read the relevant sections of memory.py and context.py in detail.
- Assistant
I'll keep reading the history/archive APIs and how ContextBuilder injects recent history.
- Assistant
Let me read the archive, raw_archive, and consolidator methods and their callers.
- Assistant
I'll examine Consolidator.archive and every archive call path next.
- Assistant
Let me read the rest of Consolidator methods and the command/builtin.py for /new, as well as autocompact and context build_messages.
- Assistant
I'll inspect archive callers and how build_messages receives session context.
- Assistant
I'll continue reading consolidate, compact_idle_session, and the close path.
- Assistant
I'll finish reading compact_idle_session and search for any related internal-session conventions.
- Assistant
I'll check how heartbeat/cron session keys are formed and review the context builder tests.
- Assistant
I'll check how Consolidator is wired and how command new archival is tested.
- Assistant
Checking evaluation case docs and any prior design notes.
- Assistant
I'll locate every call site for
build_messagesandbuild_system_promptso session scoping is threaded through. - Assistant
I'll inspect the remaining build_messages call sites in loop.py.
- Assistant
I have a clear plan: add
session_keythrough the archive path, filter recent history per session (with unified-mode rules), and propagate keys from all call sites. - Assistant
Adding the prompt-scoped history reader and threading
session_keyintoraw_archive. - Assistant
Now update raw_archive and Consolidator.
- Assistant
Let me update all these sections.
- Assistant
Note that
_RAW_ARCHIVE_MAX_CHARSis defined afterraw_archiveuses it. Looking at original code - raw_archive uses_RAW_ARCHIVE_MAX_CHARSwhich is defined later at module level. That's fine in P... - Assistant
Now update Consolidator.init to accept unified_session, update archive, and all call sites.
- Assistant
I accidentally duplicated some lines — fixing that now.
- Assistant
There's a duplicate
def set_provider(line — fixing it. - Assistant
The last edit still left a bad
set_providersignature — rewriting that method cleanly. - Assistant
Good. Now I'll update the archive method and its call sites to pass session_key.
- Assistant
I'll apply several replacements:
- Assistant
Continue updating estimate_session_prompt_tokens, archive, and other archive calls.
- Assistant
Now update archive method itself.
- Assistant
Next I'll update the remaining archive calls in maybe_consolidate_by_tokens and compact_idle_session.
- Assistant
Need to update lines 933 and 1005.
- Assistant
Update both archive calls.
- Assistant
Update line 1005
- Assistant
Next I'll update
context.pyto filter recent history by session. - Assistant
Update build_system_prompt and build_messages
- Assistant
Now update build_messages
- Assistant
Update build_messages signature and call to build_system_prompt
- Assistant
I'll wire session_key and unified_session through loop.py call sites.
- Assistant
Update consolidator init and build_messages paths.
- Assistant
Now update
_build_initial_messages, system messagebuild_messages, andenforce_file_cap. - Assistant
Need to pass session_key and unified_session in
_build_initial_messages, and update its callers. - Assistant
Updating callers of
_build_initial_messagesand the system-message path next. - Assistant
Let me look at those line numbers.
- Assistant
sed needs commas not colons
- Assistant
I'll use replace for these sections.
- Assistant
Need to open/goto before replace for the currently open file.
- Assistant
Now update enforce_file_cap calls and the
_build_initial_messagescaller. - Assistant
Update the other enforce_file_cap and _build_initial_messages
- Assistant
Update the second enforce_file_cap
- Assistant
Now update
cmd_newin builtin.py. - Assistant
I'll keep /new clearing immediately and pass session_key into background archive.
- Assistant
Checking remaining write paths and verifying the Python modules still compile.
- Assistant
I'll write a temporary script to verify isolation behavior.
- Assistant
Isolation looks good — now I'll run the related existing tests.
- Assistant
Those failures come from outdated test mocks not accepting
session_key; I'll leave those alone and review the full diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.