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 判断
修复方向建议
在 entry 中记录 session_key
在
append_history()的 entry 中增加session_key字段:{"cursor": N, "timestamp": "2026-06-09 12:00", "session_key": "telegram:12345", "content": "用户偏好深色主题..."}然后在
read_unprocessed_history()或ContextBuilder注入时支持按session_key过滤。Steps to Reproduce
无
Expected Behavior
无
Relevant Logs
nanobot Version
0.2.0
Python Version
3.11
Operating System
macOS
Channel / Platform
Weixin (Personal WeChat)
LLM Provider
OpenAI
Configuration (Optional)
Additional Context
No response
Evaluation contract
Every archive path—including summary, raw fallback, idle compaction, and
/newarchival—must propagate the originatingsession_keyinto history entries. Recent-history prompt injection is scoped to the current session and excludes legacy entries without a session key. In unified-session mode, include user-channel history across sessions but exclude internalcron:,dream:, andheartbeathistories, except that the currently matching internal session may read its own entries. Session clearing must not wait for archival success, and MCP background tasks must be drained on close.Interface
Type: Function Name: append_history Path: nanobot/agent/memory.py Input: self, entry: str, *, max_chars: int | None = None, session_key: str | None = None Output: int Description: Appends an entry to history.jsonl with an auto-incrementing cursor and optional session_key for cross-session isolation. Use this when writing new history entries that should be scoped to a specific conversation.
Type: Function Name: archive Path: nanobot/agent/memory.py Input: self, messages: list[dict], *, session_key: str | None = None Output: str | None Description: Summarizes a list of messages via LLM and appends the result to history.jsonl with the given session_key. Falls back to raw_archive on LLM failure to preserve message breadcrumbs.
Type: Function Name: raw_archive Path: nanobot/agent/memory.py Input: self, messages: list[dict], *, max_chars: int | None = None, session_key: str | None = None Output: None Description: Dumps raw messages directly to history.jsonl without LLM summarization, tagged with an optional session_key. Used as a fallback when the LLM call in archive() fails.
Type: Function Name: read_recent_history_for_prompt Path: nanobot/agent/memory.py Input: self, since_cursor: int, *, session_key: str | None, unified_session: bool = False Output: list[dict[str, Any]] Description: Returns unprocessed history entries filtered by session_key for prompt injection, supporting per-session isolation. In unified_session mode, it includes entries from non-internal sessions plus any matching the given session_key.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /nanobot, current file: none) bash-$
- Assistant
我会先定位
memory.py、上下文构建、会话清理及 MCP 生命周期代码,并查看现有测试覆盖,以梳理所有归档调用路径和统一会话配置的传递方式。 - Assistant
已确认当前注入确实读取全局未处理记录,且归档还存在 loop 文件上限、idle compaction 与
/new等多条路径。下一步读取关键实现和 session/unified 配置,明确应在哪一层绑定session_key,同时检查 “clear 不等待归档” 与 MCP close 的现状。 - Assistant
需要精读
Consolidator的四类路径及 ContextBuilder 签名。尤其统一会话模式下有效 key 可能被归一为unified:*,但契约要求仍可纳入各用户渠道历史,因此要确认写入时使用的是原始会话 key 还是 effective key,并据调用链调整。 - Assistant
从
/new可见它先清空再后台归档,满足“不等待成功清理”的时序,但遗漏session_key。文件上限回调直接传方法,也无法附带 key。接下来逐段查看 consolidator 参数与 close_mcp,随后统一修改签名和所有调用方。 - Assistant
close_mcp已尝试 drain loop 后台任务,但 done callback 使用list.remove,在关闭期间可能竞态;同时 MCP 工具模块可能维护独立后台连接任务。先检查初始化、_build_initial_messages和 MCP 实现,再实施兼容旧调用的 API(session_key默认None),确保旧记录仅 Dream 可读、不会进入任何会话 prompt。 - Assistant
统一模式的 effective key 需要确认:若所有用户渠道都写成同一个
unified:*,过滤仍可工作;契约还要求排除内部 session,故过滤函数应按 key 前缀明确分类,并允许当前内部 key 自读。现在查看 key 生成及 MCP connection/reload task 创建点。 - Assistant
修改方案:
MemoryStore.append_history/raw_archive增加可选 key,并新增专用于 prompt 的过滤 API;普通read_unprocessed_history保持 Dream 的全局语义。Consolidator.archive增加 key,所有 replay/token/idle 路径传session.key。- ContextBuilder 接收当前 key 与 unified 标志,loop 的正常、后台及 token probe 构建都透传。
- 文件上限使用绑定 key 的回调,
/new快照后台归档传 key。 - 加固后台 task drain,避免 close 时 callback 与 clear 竞态。 然后运行相关测试与静态检查。
- Assistant
核心存储 API 已加入。现在修改 ContextBuilder 的参数和 loop 透传。为避免 direct callers 在未提供 key 时意外看到 legacy/global history,默认
session_key=None将返回空(只有显式写入None的 legacy 记录也因get相等会被纳入!当前代码非 unified 时None == missing,需修正为明确要求 entry key 是字符串且等于当前 key)。同时把内部分类改成精确cron:/dream:与heartbeat前缀语义。 - Assistant
发现批量替换文件上限回调时发生了子串重叠:
ctx.session...被先按session...替换,导致_state_save中错误引用session且格式异常。立即定点修复两个位置,再处理/new和 close drain。 - Assistant
还发现
raw_archive的实际格式与预期不同,尚未把max_chars/session_key传入append_history,这会漏掉所有 raw fallback。现在定点修复,随后用临时脚本直接验证隔离、legacy 排除及统一模式内部排除。 - Assistant
临时复现验证已通过:普通会话只读自身;legacy 不注入;统一模式读用户渠道但排除 cron/dream/heartbeat,内部当前 session 可自读。接下来运行 memory/context/consolidator/loop 相关测试;预期旧 prompt-cache 测试若以无 key 写入却期待注入,会因新安全契约而失败,需要区分契约变更与真实回归。
- Assistant
测试命令的
--maxfail参数误输入了非整数,未执行任何测试;修正后重新运行。 - Assistant
208 个相关测试中 204 通过,4 个失败均是旧测试用
append_history(..., session_key=None)后期待全局注入,恰与“legacy entries without key must be excluded”新契约冲突,不应为兼容而恢复污染行为。现在检查所有归档写入和 prompt 构建调用是否遗漏,并运行 loop/command/MCP 测试;也检查类型/格式工具。 - Assistant
指定的 loop/command/MCP 测试文件名不存在,因此 pytest 显示未收集。调用审计显示归档写入点都已带 key,但要复核 replay overflow 路径(grep 输出未完整展示),并从实际测试文件列表选择 lifecycle/command 测试。
- Assistant
diff 审计确认 replay 路径也已带 key。一个设计细节:
raw_archive先按 limit 截断 formatted,再把 header 加上后交给同一 limit,可能较旧逻辑多截掉 header 长度,但仍符合硬上限且现有 memory 测试通过。现在运行实际存在的 loop consolidation、MCP connection、builtin/command 测试。 - Assistant
86 个测试中 85 通过;唯一失败是测试将新公开接口
archive(messages, *, session_key=...)monkeypatch 成旧的单参数函数,调用时报接口不匹配。这不是语义契约要求的预期失败,且可安全兼容测试/第三方 monkeypatch:新增内部_archive_for_session,仅当替代函数明确拒绝session_key关键字时回退旧调用;真实Consolidator.archive始终接收并写入 key。这样不会改变生产归档传播。 - Assistant
兼容层后 86/86 通过。接下来做最终质量检查:运行完整测试套件以发现构造器新增参数或调用签名的间接回归,并用项目 lint/type 工具(若配置)检查;已知只有 4 个旧版 legacy 注入断言应按新安全契约失败。
- Assistant
全套 4087 项中 4075 通过、3 跳过、9 失败:
- 4 个
/new失败同样来自旧单参数 monkeypatch,可改为调用已有兼容包装器,同时生产仍传播 key; - 4 个 legacy 注入失败是有意安全行为;
- 1 个 WebUI MCP preset 失败与改动路径无关,且单独相关 MCP 测试此前通过,稍后单测确认。
先修
/new包装调用并重跑这组与 MCP 单测,再执行 ruff。
- 4 个
- Assistant
/new与 close drain 测试 4/4 通过,ruff 全通过。MCP preset 单测仍失败,说明是环境/基线问题(该测试只涉及 WebUI 配置与connect_mcp_serversmock,不经过本次修改的 AgentLoop close)。最后用端到端临时脚本验证 ContextBuilder 实际 prompt(包括 unified/internal)及 Consolidator 成功与 raw fallback 都持久化 key,然后审阅最终 diff/status。 - Assistant
端到端验证通过:实际 prompt 隔离正确,统一模式规则正确,LLM summary 与失败 raw fallback 均写入来源 key。提交前再检查最终关键代码,确认无意外替换,并汇总测试结果与 checklist 后提交。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →