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
Let me start by understanding the repository structure and the issue. The issue is about
history.jsonlcross-session injection causing context pollution. The fix involves:- Adding
session_keyto history entries - Modifying
append_historyto acceptsession_key - Modifying
archiveandraw_archiveto accept and propagatesession_key - Modifying
read_unprocessed_history(or creatingread_recent_history_for_prompt) to filter bysession_key - Updating callers of these functions to pass
session_key
Let me explore the repository structure first.
- Adding
Assistant
Let me explore the key files: memory.py and context.py in the agent directory.
Assistant
Let me look at memory.py and context.py:
Assistant
Let me use the correct tool. I need to use
bash_terminalfor all commands, includingopen.Assistant
The output was truncated. Let me see the rest of the file:
Assistant
Let me read the file in sections.
Assistant
Now I see the
append_historyandread_unprocessed_historymethods. Let me continue reading to seeraw_archive, theConsolidator.archive()method, and then look atcontext.py.Assistant
Let me read the rest of memory.py and context.py.
Assistant
Now let me see context.py:
Assistant
Now let me find all callers of these key functions to understand the full scope of changes needed:
Assistant
Let me look at the loop.py and command/builtin.py to see how these are called:
Assistant
Let me check where
enforce_file_capis defined and how it uses `on_archiveAssistant
Let me look at the
enforce_file_capmethod:Assistant
Now let me look at the
/newcommand in builtin.pyAssistant
Let me look at the full Consolidator class and all its archive call sites
Assistant
Let me look at the test files to understand the expected interface and how
raw_archiveandappend_historyare called inAssistant
Let me check the
build_system_promptcallers to understand howsession_keyisAssistant
Let me look at how
build_messagesis called in loop.py to see what session_key infoAssistant
Let me search for "unified" references to understand the unified
Assistant
Now I have a comprehensive understanding. Let me start implementing the changes. First, let me modify
MemoryStore.append_historyto add thesession_keyAssistant
The file is open. Let me use goto to go to line 235 and then do the edit.
Assistant
Now let me do the
Assistant
Good. Now let me update
raw_archiveto accept and pass throughsession_key. Let me find it.Assistant
Now let me update `raw_archive
Assistant
Now let me add the
read_recent_history_for_promptmethod. Let me findread_unprocessed_history:Assistant
Now let me add the
read_recent_history_for_promptmethod after `read_unprocessed_historyAssistant
Now let me update
Consolidator.archiveto accept and pass throughsession_key. Let me find it:Assistant
Let me see the full
archivemethod and update it:Assistant
Now update
Consolidator.archive:Assistant
Now I need to update all callers of
Consolidator.archive()to passsession_key. Let me find and update each caller.First,
_consolidate_replay_overflow:Assistant
Let me see more of that function:
Assistant
Update
_consolidate_replay_overflow:Assistant
Now update the
maybe_consolidate_by_tokenscall:Assistant
That's in
maybe_consolidate_by_tokens. Let me update it.Assistant
Now update the
compact_idle_sessioncall:Assistant
Now let me update
command/builtin.pyto pass session_key:Assistant
Let me open and edit that file.
Assistant
Now let me edit the builtin.py file:
Assistant
Let me open builtin.py and edit it.
Assistant
Now edit line 215:
Assistant
Now I need to handle
loop.pycalls toenforce_file_capwithraw_archive. Let me look at those calls:Assistant
Let me look at both enforce_file_cap calls in loop.py:
Assistant
Let me switch to the loop.py file and find both call sites. Let me also look at the second call.
Assistant
Let me open loop.py and look at both contexts
Assistant
Let me look at the second call context:
Assistant
Now let me update both call sites in loop.py. First, line 1166
Assistant
Now update the second call site at line 1490:
Assistant
Now edit line 1490:
Assistant
Now I need to update
context.py. Let me updatebuild_system_promptandbuild_messagesto useread_recent_history_for_prompt:Assistant
Now update
build_system_promptto acceptsession_keyand `unified_sessionAssistant
Now let me update
build_messagesto accept and passsession_keyandunified_session:Assistant
Now update
build_messages:Assistant
I see duplication. Let me check what happened around lines 249-256:
Assistant
I need to fix the duplicate lines. Let me remove them:
Assistant
Now I need to update callers of
build_messagesin loop.py to passsession_keyandunified_session. Let me findAssistant
Let me look at each call site. First, the one at line 600:
Assistant
Let me open loop.py and update the call
Assistant
Now update
_build_initial_messages:Assistant
Now check the other call site:
Assistant
Let me check the call at 1142:
Assistant
Let me see the function context around this call:
Assistant
Let me see the full function containing line 1142:
Assistant
Now update the build_messages call at line 1142:
Assistant
Update the call at line 1142:
Assistant
Let me also check for direct callers of
build_system_prompt:Assistant
Let me also check subagent.py for calls to
build_messages:Assistant
Good. Now let me update the Consolidator's
estimate_session_prompt_tokensto passsession_key:Assistant
Now update
estimate_session_prompt_tokensto passsession_key:Assistant
Now let me run the existing tests to verify the changes:
Assistant
All memory store tests pass. Let me run the other related tests:
Assistant
Let me also run the prompt cache and consolidator tests:
Assistant
All tests pass. Let me also run the dream tests:
Assistant
All tests pass. Now let me write a quick test script to verify the core behavior:
Assistant
All core behaviors work. Let me check for any remaining callers that I might have missed:
Assistant
All archive call sites are updated. Let me also check for any calls to
raw_archivethat might need updating:Assistant
Let me also run the auto_compact tests to make sure they still pass:
Assistant
Let me run a broader set of tests to make sure nothing is broken:
Assistant
There's a test failure. The test replaces
loop.consolidator.archivewith a mock that doesn't acceptsession_key. Let me look at that test:Assistant
This test failure is expected — the test mocks
archivewith a function that doesn't acceptsession_key. Per the instructions, I should ignore it since the test needs updating to match the new interface. Let me skip this test and run the rest:Assistant
Let me check which tests are affected by the new
session_keyparameter onarchive:Assistant
All 5 failures are due to test mocks that don't accept the new
session_keykeyword argument. These are expected test updates needed. Let me verify the session manager tests too:Assistant
Let me check for any remaining callers that might need updating — specifically for direct
append_historyorraw_archivecalls outside of Consolidator:Assistant
Let me check the shell.py reference to make sure it's fine:
Assistant
Let me check runner.py for any contexts where build_messages is used:
Assistant
Let me also verify the backward-compat behavior when
session_keyis None:Assistant
Everything works correctly. Let me do a final review of all my changes:
Assistant
Let me review the final diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.