Now let me check tests referencing "Recent History" and how the context builder is tested:
Assistant
Let me check remaining append_history callers and the consolidator test fixtures:
Assistant
Now I have the full picture. Let me implement the changes, starting with memory.py:
Assistant
Now add the internal-session helper and read_recent_history_for_prompt after read_unprocessed_history:
Assistant
Now raw_archive and Consolidator.archive:
Assistant
Now the three internal archive() call sites in Consolidator:
Assistant
Now update ContextBuilder in context.py:
Assistant
Now update loop.py. First look at the Consolidator construction:
Assistant
Add the partial import to loop.py:
Assistant
Now let's update the existing tests that rely on unscoped history injection. Let me view the affected test files:
Assistant
Now run the relevant tests:
Assistant
This one looks unrelated. Let me verify it fails on pristine HEAD:
Assistant
Pre-existing failure, unrelated. Now let me add dedicated unit tests for the new store API:
Assistant
All lint findings are pre-existing (they exist on pristine HEAD too). Final full-suite run:
Assistant
4097 passed; the single remaining failure (test_mcp_presets_api) fails identically on pristine HEAD — unrelated to this fix. Final diff review:
Assistant
The fix is complete and verified. Summary of the changes:
Root cause
history.jsonl entries were written per-session by but carried no origin marker, and injected unprocessed entries into every session's — leaking, e.g., WeChat conversation summaries into a Telegram session's prompt.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
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.
Consolidator.archive()
ContextBuilder.build_system_prompt()
all
# Recent History
Changes
nanobot/agent/memory.py
MemoryStore.append_history() — new session_key kwarg; persisted in each JSONL record when present.
MemoryStore.raw_archive() — new session_key kwarg, propagated to append_history.
New MemoryStore.read_recent_history_for_prompt(since_cursor, *, session_key, unified_session=False) — session-scoped read for prompt injection:
legacy entries without a session_key are excluded (unattributable);
normal mode: only entries matching the current session;
unified mode: user-channel entries shared across sessions, but internal sessions (cron:, dream:, heartbeat) stay private — only the exact matching internal session reads its own entries.
Consolidator.archive(messages, *, session_key=None) — tags summaries and the raw-archive fallback; all internal call paths (replay-overflow consolidation, token-budget rounds, compact_idle_session) pass session.key. The token-estimation probe also passes session_key so estimates match the real scoped prompt.
nanobot/agent/context.py — build_system_prompt()/build_messages() accept session_key/unified_session and use read_recent_history_for_prompt instead of the unfiltered read.
nanobot/agent/loop.py — threads session.key + self._unified_session into both build_messages call sites, and tags enforce_file_cap's raw-archive callback with the session key via functools.partial. close_mcp() continues to drain background archive tasks before closing.
nanobot/command/builtin.py — /new tags its background archive with session.key; clearing still completes before (and never waits on) archival.
Tests — new coverage for session-key persistence, per-session filtering, legacy-entry exclusion, unified-mode sharing/internal-session privacy, and archive-path tagging (summary, raw fallback, idle compaction, /new); existing stubs/tests updated for the new signatures.
Verification
Full suite: 4097 passed, 3 skipped; the only failure (tests/webui/test_mcp_presets_api.py) fails identically on pristine HEAD — pre-existing and unrelated.
ruff check reports no new findings on touched files (all remaining findings exist on HEAD).