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 to understand the codebase and locate relevant files. Let's first check the current directory and list files.
- Assistant
Let me explore the repository structure more broadly.
- Assistant
Let me first read the main files mentioned in the issue to understand the current code structure.
- Assistant
Let me read the memory.py file to understand the current implementation of append_history, archive, raw_archive, and the existing read methods.
- Assistant
Let me scroll down to find the append_history method and other relevant methods.
- Assistant
Let me read the append_history method and surrounding code.
- Assistant
Let me continue reading to see the read_unprocessed_history and related methods.
- Assistant
Let me read the read_unprocessed_history method.
- Assistant
Let me continue reading to see the Consolidator class and its archive method.
- Assistant
Let me read the Consolidator class and its archive method.
- Assistant
Let me continue reading to find the archive method.
- Assistant
Let me read the archive method and surrounding code.
- Assistant
Now let me find the raw_archive method and see where
append_historyis called. - Assistant
Let me read the raw_archive method.
- Assistant
Let me read more of the Consolidator code to see the callers of
archive. - Assistant
Let me read lines around 861 and 933 to see the other archive callers.
- Assistant
Let me continue reading to see lines 920-955.
- Assistant
Let me now read the context.py file to see how history is injected into the system prompt.
- Assistant
Let me continue reading the rest of context.py.
- Assistant
Now let me understand all callers of
append_history,archive, andraw_archivethroughout the codebase. - Assistant
Let me now look at the callers of
build_system_promptto see ifsession_keyis available there. - Assistant
Let me look at the loop.py to understand how session_key is available in the consolidation flow.
- Assistant
Let me see how
build_system_promptis called in context.py:229: - Assistant
Let me also look at the
/newcommand in builtin.py: - Assistant
Let me also look at the Session class's
enforce_file_capmethod: - Assistant
Let me look at that method to understand what
on_archivecallback expects. - Assistant
Now let me check how
Session.keyis available and howenforce_file_capis called: - Assistant
Let me read the Session class to understand its structure.
- Assistant
Let me check the existing tests to understand the test structure and then look at the
enforce_file_capcallback signature more carefully. - Assistant
Let me look at the
enforce_file_capmore carefully to understand the callback signature: - Assistant
Let me read the other read method at line 413 in memory.py:
- Assistant
Let me look at existing tests to understand the test patterns:
- Assistant
Let me continue reading the test file to understand the test patterns:
- Assistant
Let me also look at the context_builder tests to understand the test patterns:
- Assistant
Let me also look at how
build_messagesis called in loop.py to see ifsession_keyis available: - Assistant
Let me look at the callers of
build_messagesin loop.py: - Assistant
Let me also look at line 600 in loop.py to see the other build_messages call:
- Assistant
Let me check how the
_unified_sessionflag andUNIFIED_SESSION_KEYare used: - Assistant
Let me check the
session_key_overrideandunified_sessionusage: - Assistant
Let me look at how the effective session key is determined in the main flow:
- Assistant
Let me look at the main message processing flow to understand how session_key is available:
- Assistant
Let me look at the
_process_user_turnmethod to understand the full flow: - Assistant
Let me find
_process_user_turnto see the main user message flow: - Assistant
Let me look at how
build_messagesis called in the main user turn flow: - Assistant
Let me look at line 1490 to see the other enforce_file_cap call:
- Assistant
Let me look at line 300-320 in loop.py to see where
build_messagesis passed to the consolidator: - Assistant
Now I have a comprehensive understanding of the codebase. Let me run the existing tests to make sure they pass before I start making changes:
- Assistant
Let me also run all tests to establish a baseline:
- Assistant
Good, there's one pre-existing failing test. Let me check if it's related to our changes or pre-existing:
- Assistant
This test failure is pre-existing and not related to our changes. Let me now start implementing the fix. Here's my plan:
Implementation Plan
MemoryStore.append_history— Addsession_keyparameter and store it in JSONL entryMemoryStore.raw_archive— Addsession_keyparameter and pass toappend_historyMemoryStore.read_recent_history_for_prompt— New method with session filteringConsolidator.archive— Addsession_keyparameter and propagate to store callsConsolidator._consolidate_replay_overflow— Passsession.keytoarchiveConsolidator.maybe_consolidate_by_tokens— Passsession.keytoarchiveConsolidator.compact_idle_session— Passsession_keytoarchiveContextBuilder.build_system_prompt— Add and parameters, use
- Assistant
Now I'll start implementing the changes. Let me begin with
MemoryStore.append_history: - Assistant
Good. Now let me update
raw_archiveto accept and passsession_key: - Assistant
Now let me update
raw_archiveto accept and passsession_key: - Assistant
Good. Now let me add the
read_recent_history_for_promptmethod. Let me find whereread_unprocessed_historyis: - Assistant
Now let me add the
read_recent_history_for_promptmethod afterread_unprocessed_history: - Assistant
Good. Now let me update
Consolidator.archiveto accept and passsession_key: - Assistant
Let me read the archive method to update it.
- Assistant
Now let me update
Consolidator.archiveto accept and passsession_key: - Assistant
Good. Now let me update
_consolidate_replay_overflowto passsession.keytoarchive: - Assistant
Now let me update
_consolidate_replay_overflowto passsession.keytoarchive: - Assistant
Good. Now let me update the
maybe_consolidate_by_tokensmethod to passsession.keyto itsarchivecalls: - Assistant
Now let me update line 936 to pass
session.keytoarchive: - Assistant
Good. Now let me update
compact_idle_sessionto passsession_keytoarchive: - Assistant
Good. Now let me update the
ContextBuilderclass. First, let me updatebuild_system_prompt: - Assistant
Now let me update
build_system_promptto acceptsession_keyandunified_sessionparameters: - Assistant
Good. Now let me update
build_messagesto accept and passsession_keyandunified_session: - Assistant
Now let me update
build_messagesto accept and passsession_keyandunified_session: - Assistant
Good. Now let me update the callers in
loop.py. Let me start with the_build_initial_messagesmethod: - Assistant
Let me find where
_build_initial_messagesis called to understand what session_key is available: - Assistant
Let me read the caller at line 1413:
- Assistant
Good. I can see that
ctx.session_keyis available. Now let me update_build_initial_messagesto accept and pass session_key: - Assistant
Good. Now let me update the caller of
_build_initial_messagesat line 1413 to pass session_key: - Assistant
It looks like there's a duplicate section in the file now. Let me check what happened:
- Assistant
I see there's a duplicate section. Let me fix this by removing the duplicate lines:
- Assistant
Good, the duplicate is fixed. Now let me update the other
build_messagescall in_process_system_messageat line 1140: - Assistant
Now let me update the
build_messagescall at line 1144 and theenforce_file_capcall at line 1170: - Assistant
Good. Now let me update the other
enforce_file_capcall at line 1494 (in_state_save): - Assistant
Now let me update line 1499 to pass session_key to raw_archive:
- Assistant
Good. Now let me update the
builtin.py/newcommand to passsession_keytoarchive: - Assistant
Now let me update the
cmd_newfunction to passsession_keytoarchive: - Assistant
Good. Now let me check if there are other callers of
build_messagesthat I might have missed. Let me also check the_build_messagesreference in the Consolidator constructor: - Assistant
Let me check how
_build_messagesis used in the Consolidator'sestimate_session_prompt_tokens: - Assistant
Good. The
_build_messagesinestimate_session_prompt_tokensdoesn't passsession_key- this is fine for token estimation as it gives a conservative (upper-bound) estimate. Let me now run the tests to verify everything works: - Assistant
All 84 tests pass. Let me also run the consolidation-related tests:
- Assistant
The test failure is expected because the test's mock doesn't accept the new
session_keykeyword argument. The instructions say: "If existing tests break because they need to be updated to reflect the changes you made, just ignore it." Let me continue checking for other test failures: - Assistant
Let me check what this test does:
- Assistant
This is the same issue - tests monkey-patching
archivewith functions that don't acceptsession_key. Let me check how many tests have this issue: - Assistant
Let me check if the
AsyncMocktests pass: - Assistant
Good, the
AsyncMocktests pass. The issue is only with custom function mocks that don't acceptsession_key. Let me check the other test files: - Assistant
Let me also run the full test suite to see which tests break:
- Assistant
Let me check this test:
- Assistant
Let me check all tests that break:
- Assistant
All 5 failing tests are in
test_consolidate_offset.pyandtest_loop_consolidation_tokens.py, all due to monkey-patchingarchivewith functions that don't accept the newsession_keykeyword argument. The instructions say: "If existing tests break because they need to be updated to reflect the changes you made, just ignore it. Evaluation system will not take it into account."Now let me create a verification script to demonstrate the fix works:
- Assistant
All verification tests pass. Let me also verify the ContextBuilder integration:
- Assistant
All tests pass. Let me clean up the temporary test files:
- Assistant
Now let me do a final review of all the changes I made:
- Assistant
Let me verify the full diff:
- Assistant
The diff looks clean and correct. Let me now run the final verification to make sure the existing tests still pass:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.