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 的消息块:python# 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 格式为:
json{"cursor": N, "timestamp": "2026-06-09 12:00", "content": "用户偏好深色主题..."}注意:entry 中没有
session_key或chat_id字段,无法追溯来源会话。注入路径(
ContextBuilder)python# 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 - Assistant
I'll start by exploring the repository structure and the relevant memory/context code to understand how history is written and injected.
- Assistant
I'll inspect the memory and context modules plus related tests to map the current history flow.
- Assistant
rg is not available, let me use grep instead.
- Assistant
Let me open the core memory and context modules.
- Assistant
Let me read more of memory.py focusing on append_history, archive, raw_archive, and Consolidator.
- Assistant
Let me continue reading the Consolidator and related methods.
- Assistant
Let me read the Consolidator class fully.
- Assistant
Continue reading Consolidator.
- Assistant
Continue reading the archive methods
- Assistant
Continue with archive and related methods.
- Assistant
Continue with rest of consolidator
- Assistant
Continue to end of file
- Assistant
Next I'll check context.py and all archive/history call sites.
- Assistant
The grep failed because of directory? Let me use find properly.
- Assistant
I'll inspect
/newarchival, Autocompact, MCP close drain, and the remaining archive call sites. - Assistant
I'll inspect how system prompt/messages are built and how archive paths receive the session key.
- Assistant
I'll inspect
enforce_file_capand related history-session tests next. - Assistant
I'll look for internal session-key patterns and related context-builder tests.
- Assistant
Checking how consolidator calls archive and whether heartbeat history keys exist.
- Assistant
Let me also read the _process_message path where build messages is done and the consolidator maybe_consolidate path. Also file cap on_archive - calling
raw_archivewith just the chunk means it's a b...I need to implement session-key isolation for history entries to prevent cross-session pollution in the recent-history prompt injection. The core change is to propagatesession_keythrough the entire archive pipeline so thatread_recent_history_for_promptcan filter entries correctly.Let me start by updating the memory interface and implementation to support session keys everywhere.
First, I'll modify the append_history function to accept and store session_key. Then I'll update all call sites to pass it through.
Let me check the current memory.py file to see the existing implementation. I should read the relevant sections. Let me use the file system to explore the code. I need to 1) update the append_history signature and implementation, 2) update archive and raw_archive to accept and pass session_key, 3) update read_recent_history_for_prompt to implement the filtering logic, and 4) update ContextBuilder to pass session_key through the call stack.
Let me start by examining the memory.
- Assistant
I'll further inspect consolidator init and estimate paths to ensure token probing stays accurate.
- Assistant
Implementing the core memory changes now.
- Assistant
I need to navigate to the target line before editing.
- Assistant
Now I'll apply the edit.
- Assistant
Next I'll add the filtered history reader and update raw_archive/archive.
- Assistant
I'll update raw_archive and Consolidator.archive next.
- Assistant
Updating Consolidator next to accept unified_session and thread session_key through archive paths.
- Assistant
I'll update the archive method and its call sites now.
- Assistant
Let me update all of these.
- Assistant
Continue with estimate, archive, and other call sites.
- Assistant
Update archive method.
- Assistant
Update the remaining two archive calls.
- Assistant
Need to goto first
- Assistant
Update compact_idle_session archive call.
- Assistant
Now update context.py for filtering recent history by session.
- Assistant
Now update build_messages in context.py.
- Assistant
Need to pass session_key and unified_session to build_system_prompt call.
- Assistant
Updating loop.py and cmd_new to propagate session_key.
- Assistant
Update _build_initial_messages
- Assistant
Now update process_direct build_messages and enforce_file_cap call sites.
- Assistant
Update both enforce_file_cap calls.
- Assistant
Find the other enforce_file_cap call.
- Assistant
Update the second one.
- Assistant
Updating
/newarchival to pass broadband session_key. - Assistant
I'll scan remaining archive/history write paths and any Consolidator construction sites.
- Assistant
I'll run a quick verification script for the isolation logic and a few related unit tests.
- Assistant
I'll run a few existing unit tests related to memory and consolidation.
- Assistant
The test failures come only from outdated mocks that ignore
session_key; that's expected. I'll verify/newarchival with the real consolidator path next. - Assistant
Also verify cmd_new archives with session key via background:
- Assistant
Let me also check if there's a lambda late-binding issue with session - when enforce_file_cap is called synchronously it's fine.
- Assistant
All those passed. Good - dream's build_dream_prompt still uses Unprocessed history without session filter, and Recent History injection is skipped for ephemeral.
- Assistant
I'll do a final review of the key changed sections.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.