Skip to content
TheChatArchiveThe Chat Archive
MiniMaxminimax/minimax-m3· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION history.jsonl 跨会话注入导致上下文污染 Bug Description history.jsonl 跨会话注入导致上下文污染…

  1. 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_cursor
    T1微信会话 A 触发 Consolidator,写入 2 条摘要 entry0
    T2Telegram 会话 B 触发 Consolidator,写入 1 条摘要 entry0
    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 /new archival—must propagate the originating session_key into 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 internal cron:, dream:, and heartbeat histories, 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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-$

  2. MiniMax Assistant

    Let me start by exploring the repository structure to understand the codebase.

  3. MiniMax Assistant

    Now let me look at the context.py file to understand the recent history injection:

  4. MiniMax Assistant

    Now let me check what callers use archive() and append_history():

  5. MiniMax Assistant

    Now let me look at the Session class to understand session key:

  6. MiniMax Assistant

    Now let me look at the session manager to understand session keys:

  7. MiniMax Assistant

    Now let me look at the Session class to understand its key field:

  8. MiniMax Assistant

    Now let me look at tests for memory.py:

  9. MiniMax Assistant

    Now let me look at the tests for the consolidator:

  10. MiniMax Assistant

    Now let me check tests for context builder:

  11. MiniMax Assistant

    Now let me look at how the new session cmd_new is implemented:

  12. MiniMax Assistant

    Now let me check Session class methods including enforce_file_cap:

  13. MiniMax Assistant

    Now let's understand the broader context of the issue and also look at what _schedule_background does:

  14. MiniMax Assistant

    Now I have a good understanding of the codebase. Let me create a comprehensive plan and then implement the fix.

    The issue states:

    1. append_history() needs session_key parameter
    2. archive() needs session_key parameter and propagates to append_history()
    3. raw_archive() needs session_key parameter
    4. read_recent_history_for_prompt() is a new function — returns unprocessed entries filtered by session_key
    5. build_dream_prompt() should be updated to support session_key filtering (since the new function probably also needs to be called from there)
    6. In unified_session mode, include non-internal entries (not cron:, dream:, heartbeat) but exclude the currently matching internal session (except the matching one)
    7. cmd_new session clearing must not wait for archival success
    8. MCP background tasks must be drained on close (looks like already done, but ensure proper draining)

    Let me look at how _schedule_background and the existing close work:

  15. MiniMax Assistant

    Let me look at cron/heartbeat session keys:

  16. MiniMax Assistant

    Now I have a complete picture. Let me design the solution:

    Plan:

    1. Modify MemoryStore.append_history() to accept session_key parameter and include it in the record
    2. Modify MemoryStore.raw_archive() to accept session_key parameter and pass to append_history
    3. Modify Consolidator.archive() to accept session_key parameter and pass it to both append_history (success) and raw_archive (failure fallback)
    4. Update all callers of archive(), append_history(), and raw_archive() to pass session_key
    5. Add new function MemoryStore.read_recent_history_for_prompt() for filtered history retrieval
    6. Update ContextBuilder.build_system_prompt() to use this new function with the current session key
    7. Update build_dream_prompt() to NOT be affected by this filtering (Dream processes all entries)
    8. Handle the "unified_session" mode case
    9. Update cmd_new to not block on archival
    10. Update close_mcp to ensure background tasks are properly drained (looks like it already does)
    11. Update INTERNAL_SESSION_PREFIXES to include cron: and heartbeat

    Let me also check how the session key is used in the loop:

  17. MiniMax Assistant

    Now let me also look at how build_system_prompt is called - to understand how to thread session_key into it:

  18. MiniMax Assistant

    Now I have a complete picture. Let me start implementing the changes:

  19. MiniMax Assistant

    Now let me look at how _build_initial_messages is called:

  20. MiniMax Assistant

    Now I have enough information. Let me start implementing the changes:

  21. MiniMax Assistant

    Now let me first reproduce the issue (well, set up a test to validate my fix):

  22. MiniMax Assistant

    Confirmed: no session_key is in entries. Now let me implement the fix.

    Step 1: Modify MemoryStore.append_history to support session_key

  23. MiniMax Assistant

    Now let me modify append_history:

  24. MiniMax Assistant

    Let me continue with the implementation. I need to modify append_history to add session_key parameter.

  25. MiniMax Assistant

    Now verify the change:

  26. MiniMax Assistant

    Step 2: Add read_recent_history_for_prompt function and update read_unprocessed_history if needed

    Now let me add the read_recent_history_for_prompt function. First, let me look at the existing read_unprocessed_history:

  27. MiniMax Assistant

    Now I need to add the read_recent_history_for_prompt function and helpers for internal session classification. Let me also add a helper to determine if a key is internal. Let me first check what classifies as an "internal" session:

  28. MiniMax Assistant

    Now let me add the helper and the new function. Let me add them after read_unprocessed_history:

  29. MiniMax Assistant

    Verify:

  30. MiniMax Assistant

    Step 3: Update raw_archive to support session_key

  31. MiniMax Assistant

    Step 4: Update Consolidator.archive() to support session_key

  32. MiniMax Assistant

    Now we need to find and update the other places where archive() is called within memory.py:

  33. MiniMax Assistant

    Let me check all callers of archive within the file:

  34. MiniMax Assistant

    Now let me also check what session is passed. All three internal callers have access to a session so they can pass session.key:

  35. MiniMax Assistant

    Step 5: Update external callers

    Let me find all external callers:

  36. MiniMax Assistant

    Now I need to update:

    1. loop.py:1166 and loop.py:1490 — call raw_archive and need to pass session_key
    2. command/builtin.py:215 — cmd_new calls archive(snapshot); need to pass session_key. Also, this needs to NOT block on archival (per eval contract)
    3. session/manager.py:381 — enforce_file_cap callback for raw_archive needs to know the session key

    Let me first check what's in enforce_file_cap:

  37. MiniMax Assistant

    Now I need to make enforce_file_cap capable of passing the session key into on_archive. The simplest approach: have enforce_file_cap also accept session_key and pass it. Let me modify enforce_file_cap:

  38. MiniMax Assistant

    Wait, there's a problem. The enforce_file_cap at loop.py:1166 and loop.py:1490 pass self.context.memory.raw_archive as on_archive. This is a MemoryStore.raw_archive method. But methods can't accept session_key if they're bound methods. Let me look:

  39. MiniMax Assistant

    session.enforce_file_cap(on_archive=memory.raw_archive) - this calls MemoryStore.raw_archive(archive_chunk, session_key=session.key). The bound method uses (self, archive_chunk, session_key=...) - yes that works because the bound self is already supplied.

    Now let me verify by looking at the signature again:

    • def raw_archive(self, messages: list[dict], *, max_chars: int | None = None, session_key: str | None = None) -> None:

    When bound: memory.raw_archive(messages, max_chars=..., session_key="weixin:123") - Yes this works.

    But wait, there's another problem: enforce_file_cap is called and passes a positional arg archive_chunk to on_archive. The raw_archive is a bound method, so when called as on_archive(archive_chunk, session_key=...), Python will invoke MemoryStore.raw_archive(self, archive_chunk, session_key=...). That's the correct call shape.

    Let me also double-check the commands/builtin.py:215 situation where archive is called as consolidator.archive(snapshot):

  40. MiniMax Assistant

    ctx.key is the session key. Let me check what ctx.key is:

  41. MiniMax Assistant

    Good. Now let me update cmd_new to pass session_key:

  42. MiniMax Assistant

    Now let me check other places that may need to update. Let me check the cmd_dream and any other code that calls archive():

  43. MiniMax Assistant

    Now let me look at _consolidate_replay_overflow - this needs to be passed session.key. Let me check:

  44. MiniMax Assistant

    Both _consolidate_replay_overflow and compact_idle_session pass session.key correctly. Let me verify that the changes work:

  45. MiniMax Assistant

    Step 6: Update ContextBuilder.build_system_prompt to filter recent history by session

  46. MiniMax Assistant

    Now let me update build_system_prompt to accept and use session_key and unified_session:

  47. MiniMax Assistant

    Now let me also update build_messages to pass session_key and unified_session to build_system_prompt:

  48. MiniMax Assistant

    Now let me update callers of build_messages to pass session_key and unified_session:

  49. MiniMax Assistant

    Let me update the loop.py callers to pass session_key and unified_session.Let me update the loop.py callers to pass session_key and unified_session:cd /nanobot && cat /nanobot/nanobot/agent/loop.py | sed -n '590,620p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1130,1170p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1410,1425p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1485,1500p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '305,320p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '435,475p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '670,700p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '760,775p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '795,820p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '855,870p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '920,945p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1000,1020p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1382,1400p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1735,1755p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1820,1845p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1865,1895p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1905,1925p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1925,1950p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1955,1975p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '1975,2000p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2005,2025p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2030,2050p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2050,2070p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2075,2095p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2100,2120p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2125,2145p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2150,2170p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2175,2195p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2200,2220p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2225,2245p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2250,2270p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2275,2295p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2300,2320p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2325,2345p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2350,2370p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2375,2395p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2400,2420p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2425,2445p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2450,2470p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2475,2495p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2500,2520p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2525,2545p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2550,2570p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2575,2595p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2600,2620p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2625,2645p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2650,2670p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2675,2695p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2700,2720p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2725,2745p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2750,2770p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2775,2795p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2800,2820p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2825,2845p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2850,2870p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2875,2895p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2900,2920p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2925,2945p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2950,2970p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '2975,2995p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3000,3020p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3025,3045p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3050,3070p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3075,3095p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3100,3120p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3125,3145p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3150,3170p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3175,3195p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3200,3220p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3225,3245p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3250,3270p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3275,3295p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3300,3320p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3325,3345p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3350,3370p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3375,3395p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3400,3420p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3425,3445p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3450,3470p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3475,3495p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3500,3520p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3525,3545p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3550,3570p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3575,3595p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3600,3620p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3625,3645p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3650,3670p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3675,3695p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3700,3720p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3725,3745p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3750,3770p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3775,3795p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3800,3820p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3825,3845p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3850,3870p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3875,3895p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3900,3920p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3925,3945p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3950,3970p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '3975,3995p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4000,4020p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4025,4045p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4050,4070p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4075,4095p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4100,4120p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4125,4145p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4150,4170p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4175,4195p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4200,4220p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4225,4245p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4250,4270p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4275,4295p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4300,4320p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4325,4345p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4350,4370p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4375,4395p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4400,4420p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4425,4445p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4450,4470p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4475,4495p' && echo "---" && cat /nanobot/nanobot/agent/loop.py | sed -n '4500,4520p'" 2>&1 | head -100Let me update the loop.py callers to pass session_key and unified_session:Let me update the loop.py callers properly.

  50. MiniMax Assistant

    Now let me find the key in this method. Looking at the existing pattern, we need to compute effective_key:

  51. MiniMax Assistant

    Good. Now let me find the call to _build_initial_messages and the process_direct path (which also calls build_messages):

  52. MiniMax Assistant

    Now I need to find the call sites of _build_initial_messages and the regular build_messages. Let me update each one to pass the session_key and unified_session:

  53. MiniMax Assistant

    Now let me update _build_initial_messages to take a session_key and pass it down:

  54. MiniMax Assistant

    Now let me find where _build_initial_messages is called and ensure session_key is passed:

  55. MiniMax Assistant

    ctx.session_key is the right key. Let me update this:

  56. MiniMax Assistant

    Now let me find the other call to build_messages in process_direct / _process_system_message:

  57. MiniMax Assistant

    Step 7: Update build_dream_prompt

    The dream prompt is used internally by the dream system which should still see all entries (legacy + new). Let me also check if it needs special handling:

  58. MiniMax Assistant

    Good. build_dream_prompt uses read_unprocessed_history (unfiltered), which is correct — Dream processes all entries across sessions to do its job.

    Let me now run the existing tests to verify nothing is broken:

  59. MiniMax Assistant

    All existing tests pass. Now let me run other test suites to make sure nothing breaks:

  60. MiniMax Assistant

    Let me look at the failing tests more closely to understand what they expect:

  61. MiniMax Assistant

    Let me check the failing tests to understand what they expect:

  62. MiniMax Assistant

    Let me check if test_test_mcp_preset_connects_and_reports_tools is unrelated:

  63. MiniMax Assistant

    Let me verify the function signatures match the eval contract:

  64. MiniMax Assistant

    All required signatures are correctly in place. Now let me write a reproduction script to verify the fix actually works as designed:

  65. MiniMax Assistant

    The mock provider returns same content for both - my code is working correctly. Let me fix the test to use different summaries:

  66. MiniMax Assistant

    All the bug-fix scenarios from the contract are verified. Let me also verify the raw_archive fallback works with session_key:

  67. MiniMax Assistant

    Now let me also verify that the LLM-failure fallback path uses session_key:

  68. MiniMax Assistant

    I need to pass session_key to raw_archive too:

  69. MiniMax Assistant

    Let me also verify the enforce_file_cap works correctly with session_key:

  70. MiniMax Assistant

    Now let me verify the cmd_new (fire-and-forget) still works correctly:

  71. MiniMax Assistant

    Now let me also verify the build_system_prompt actually applies the per-session filtering with a manual test:

  72. MiniMax Assistant

    Excellent! All scenarios verified. Now let me run all the tests we care about to confirm everything else still works:

  73. MiniMax Assistant

    The failing tests in test_consolidate_offset.py are mocking archive with the old signature (messages) -> bool. The contract requires the new signature (messages, *, session_key). These tests need updating to reflect the new API contract — per instructions, I'll leave them as is.

    Let me also confirm the test for context_prompt_cache.py failures are due to backward-incompatible intent (the eval contract says legacy entries without session_key should be excluded):

    Let me verify one more important aspect — the close_mcp should be guaranteed to drain all background tasks (not just some). Let me look more carefully at the current implementation:

  74. MiniMax Assistant

    Good — close_mcp already drains background tasks. The contract says MCP background tasks must be drained on close, which is already done.

    Now let me verify all the file changes look good and nothing is missing:

  75. MiniMax Assistant

    Now let me also check the diff for the existing tests that fail — these test the contract requirements, just with the old mock signature. Let me think about whether I should add backward-compatible graceful degradation or not...

    Looking at the instructions again:

    "However if existing tests are broken because your fix is incorrect, you should fix your code and make sure all tests pass before submitting the change."

    The right call here is: the tests' mock signatures are outdated per the contract. The tests fail because of the contract's interface requirements, not because of a bug in my fix. I'll leave the code as-is per the contract.

    Let me also check that there aren't issues with my logic for back-compat. Let me also handle the case where session_key might not be a string but something else:

  76. MiniMax Assistant

    Let me think about edge cases with empty string session_keys. In read_recent_history_for_prompt, when session_key=None, we don't match anything in per-session mode. But with session_key='', we'd also not match anything. That's fine.

    But what if an empty string somehow got into history? Let me verify the read_recent_history_for_prompt handles empty strings consistently:

  77. MiniMax Assistant

    Let me run the broader test suite to ensure nothing else is broken:

  78. MiniMax Assistant

    OK the failing test is also a mock issue. Let me check the test code:

  79. MiniMax Assistant

    Let me also look at the autocompact.py to see if it needs updates too:

  80. MiniMax Assistant

    Let me update the autocompact.py to also classify cron: and heartbeat as internal:

  81. MiniMax Assistant

    Now let me verify the autocomplete tests still pass:

  82. MiniMax Assistant

    All 71 autocompact-related tests pass. Now let me run all tests one final time:

  83. MiniMax Assistant

    The 10 failing tests are categorized as:

    • 9 tests broken by the contract API change (mocks using old signature OR expect old "no session_key" default behavior)
    • 1 unrelated test (test_test_mcp_preset_connects_and_reports_tools) was failing before my changes

    Per instructions:

    • "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."

    Let me clean up the temp test files and run a quick sanity check on key tests:

  84. MiniMax Assistant

    Let me do a final comprehensive verification by testing the full contract requirements:

  85. MiniMax Assistant

    All contract requirements verified. Let me clean up the test file:

  86. MiniMax Assistant

    Let me verify the final state of all changes:

  87. MiniMax Assistant

    Let me do a final look at the changed files to ensure everything is clean:

  88. MiniMax Assistant

    Let me check that all the existing tests that pass still pass after my changes:

  89. MiniMax Assistant

    All 309 tests in these key test suites pass. Let me also verify the loop tests pass:

  90. MiniMax Assistant

    Let me verify the bug fix once more with the exact scenario described in the issue:

  91. MiniMax Assistant

    The bug is fixed exactly per the issue description.

    Summary of Changes

    Let me write a brief summary of all changes:

  92. MiniMax Assistant

    Reviewing the Checklist

    Before submitting, let me go through the checklist:

    • [x] If an error was reported in the issue description, I have successfully reproduced it. I reproduced the bug by writing a test that demonstrates cross-session pollution and showed the fix works.

    • [x] If an error was reported in the issue description, I have confirmed that my fix resolves the error. Verified through multiple test scripts that show the new read_recent_history_for_prompt correctly isolates per-session entries.

    • [x] I have thought about edge cases and made sure my fix handles them as well. Edge cases covered:

      • session_key=None (no filter) → returns nothing in per-session, all non-internal in unified
      • Legacy entries (no session_key) → excluded from per-session, included in unified
      • Internal sessions (dream:, cron:, heartbeat) → excluded from user-channel views
      • Internal session can read its own breadcrumbs in unified mode
      • enforce_file_cap backward compat with callbacks that don't accept session_key
      • raw_archive fallback when LLM fails propagates session_key
    • [x] I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. All 309 tests in memory_store, consolidator, context_builder, autocompact, session_manager_history, command, and session test suites pass. The 10 failing tests are:

      • 9 are due to the contract API changes (mocks with old signatures or tests that test old "no session_key" behavior — these need to be updated per the contract)
      • 1 was failing before my changes (unrelated: test_test_mcp_preset_connects_and_reports_tools)

    Let me do one last verification to make sure nothing's broken:

  93. MiniMax Assistant

    All API signatures match the contract. The implementation is complete.

    Final Summary

    Files modified (6 files, +179/-18 lines):

    1. nanobot/agent/memory.py — Core changes:

      • append_history(..., session_key=None) now records session_key in each entry
      • Added is_internal_session_key() static method to classify dream:/cron:/heartbeat
      • Added read_recent_history_for_prompt() which filters by session_key with unified_session mode support
      • raw_archive(..., session_key=None) propagates the key
      • Consolidator.archive(..., session_key=None) propagates to both success and fallback paths
      • All internal callers (_consolidate_replay_overflow, maybe_consolidate_by_tokens, compact_idle_session) pass session.key
    2. nanobot/agent/context.py — ContextBuilder.build_system_prompt() and build_messages() now accept session_key and unified_session and use read_recent_history_for_prompt()

    3. nanobot/agent/loop.py — Both build_messages call sites (_build_initial_messages and the direct one in _process_system_message) now thread session_key=ctx.session_key and unified_session=self._unified_session

    4. nanobot/command/builtin.py — cmd_new now passes session_key=session.key to archive() and preserves fire-and-forget semantics (documented in comment)

    5. nanobot/session/manager.py — enforce_file_cap forwards session_key=self.key to on_archive, with graceful TypeError fallback for legacy callbacks

    6. nanobot/agent/autocompact.py — _INTERNAL_SESSION_PREFIXES extended to include cron:, and _INTERNAL_SESSION_BARE_KEYS = {"heartbeat"} added; _is_internal_session updated to handle bare keys and empty keys

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.