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 cross-session contamination in
history.jsonl. The fix involves:- Adding
session_keyparameter toappend_history,archive,raw_archive, andread_recent_history_for_promptmethods - Ensuring all archive paths propagate the originating
session_keyinto history entries - Recent-history prompt injection should be scoped to the current session
- In unified-session mode, include user-channel history across sessions but exclude internal
cron:,dream:, andheartbeathistories
Let me explore the repository first.
- Adding
Assistant
Let me explore the key files mentioned in the issue:
memory.pyandcontext.py.Assistant
Let me look at the memory.py and context.py files.
Assistant
Let me read the full files. The output was truncated. Let me use
cat -nto see all line numbersAssistant
Let me open both files to understand the full
Assistant
Let me use the
opencommand correctly.Assistant
Let me scroll down to see the rest of
append_historyand the other methods.Assistant
I need to use the bash_terminal to call scroll_down.
Assistant
Let me continue reading memory.py and also look at context.py.
Assistant
Let me search for
raw_archiveand theConsolidatorclassAssistant
Let me look at
raw_archive, theConsolidatorclass, and itsarchivemethod.Assistant
Now let me look at the Consolidator's
archivemethod and the callers:Assistant
Let me see all the places where
archiveis called within Consolidator:Assistant
Let me look at the context around all three
archive()call sites and the consolidation path:Assistant
Now let me look at the context around line 861 and 933:
Assistant
Let me finish reading
compact_idle_session:Assistant
Now let me look at context.py to understand the read path:
Assistant
Now I have a good understanding of the issue. Let me look at who calls
build_system_promptandbuild_messages, and also examine the Session class:Assistant
Let me check loop.py to see what parameters are available at the call sites:
Assistant
Let me look at the key sections in loop.py to understand what parameters flow through:
Assistant
Let me look at the build_messages calls in loop.py:
Assistant
Let me look at line 1140 for the other build_messages call:
Assistant
Let me also look at the
_effective_session_keyandprepare_sessionmethods:Assistant
Now let me look for where Consolidator.archive is called from auto_compact or elsewhere with the session key available
Assistant
Let me check the
/newcommand that callsloop.consolidator.archive(snapshot)Assistant
Let me check what
ctx.keyis:Assistant
Let me look at the command context:
Assistant
Let me check the CommandContext:
Assistant
Let me look at the turn messages call site and the
_unified_sessionusage:Assistant
Let me look at a few more things
Assistant
Let me check for existing tests:
Assistant
Let me look at the test files to understand the expected interface:
Assistant
Let me now look at the context builder test:
Assistant
Let me check the consolidator test:
Assistant
Let me check how
unified_sessionreaches the context builder and look at the_build_initial_messagescallerAssistant
Now let me look at where
_build_initial_messagesis called and whatsession_keyis availableAssistant
Let me look at the
_build_initial_messagescaller at line 141Assistant
Let me look at the call around line 1140
Assistant
Now I have a comprehensive picture. Let me also check the Consolidator's
_consolidate_replay_overflowto confirm the session.key availabilityAssistant
Now let me also check the
UNIFIED_SESSION_KEYand any internal session key patterns likecron:,dream:, `heartbeatAssistant
Now I have a thorough understanding. Let me start implementing the changes. I'll work through the files methodically.
First, let me modify
MemoryStore.append_history()to accept and persistsession_keyAssistant
Now let me update
raw_archiveto accept and pass `session_keyAssistant
Now let me add the
read_recent_history_for_promptmethod. I'll place it afterread_unprocessed_history:Assistant
I see there's a problem with the edit - the docstring for
compact_historygot merged in. Let me look at the surrounding area:Assistant
I see the issue - the
compact_historymethod signature was lost. Let me fix that:Assistant
Now update
Consolidator.archive()to accept and propagate `session_keyAssistant
Now update
Consolidator.archive()withsession_key:Assistant
Now update the call sites for
Consolidator.archive(). First,_consolidate_replay_overflow:Assistant
Now update
_consolidate_replay_overflowto passsession.key:Assistant
Now update the archive call in
maybe_consolidate_by_tokens:Assistant
Let me update both calls. First the
maybe_consolidate_by_tokensone:Assistant
Now update the
compact_idle_sessionarchive call:Assistant
Now update the
/newcommand inbuiltin.pyto passsession_key:Assistant
Update the
/newcommand to passctx.key:Assistant
Now let me check the
raw_archivecalls in loop.py:Assistant
Let me check the
enforce_file_capinterface:Assistant
Let me check
enforce_file_cap:Assistant
Now let me update
context.pyto acceptsession_keyand use the new `read_recent_history_for_pAssistant
Now update
build_system_promptto acceptsession_keyand useread_recent_history_for_prompt:Assistant
Now update
build_messagesto accept and pass throughsession_keyandunified_session:Assistant
Now update
build_messagesto acceptsession_keyandunified_sessionAssistant
Now update the callers in
loop.py. First, let me look at_build_initial_messages:Assistant
Let me update
_build_initial_messagesto passsession_key:Assistant
Now update the other
build_messagescall:Assistant
Now update the second
build_messagescall:Assistant
Now update
enforce_file_capinsession/manager.pyto passsession_key:Assistant
Now update
enforce_file_capto passsession_key:Assistant
Let me check the other
enforce_file_capcall at line 1490 in loop.py to make sure it's also covered (it uses the sameself.context.memory.raw_archivecallback which now acceptssession_keyAssistant
Let me check the
_build_messagescall in the Consolidator at line 751:Assistant
Good. The
_build_messagescall in token estimation doesn't passsession_key, but that's fine since the new params are optional with defaults.Let me now run the existing tests to verify my changes work:
Assistant
Memory store tests pass. Now let's run the context builder tests:
Assistant
All context builder tests pass. Now let's run the consolidator tests:
Assistant
All tests pass. Let me also run additional related tests:
Assistant
There's a test that mocks
archivewhich doesn't accept the newsession_keykwarg. Let me look at the test:Assistant
Let me run a broader set of tests to see the overall impact:
Assistant
Let me also make sure I haven't missed any other callers:
Assistant
Let me verify the changes work with a quick script:
Assistant
All my functional tests pass. Now let me run the broader test suite briefly to see the extent of test impact (focusing on non-mock failures):
Assistant
All non-mock-related tests pass. Let me now look at the git diff to verify everything is correct:
Assistant
Let me see the full diff:
Assistant
The diff looks correct and complete. Let me now verify the checklist and also run one more comprehensive test to make sure nothing else broke:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.