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.
Root cause
history.jsonl entries were written without any record of their originating session, and ContextBuilder.build_system_prompt() injected all un-Dream-processed entries into every session's # Recent History — so a WeChat session's archive summaries leaked into a Telegram session's system prompt.
Changes
nanobot/agent/memory.py
MemoryStore.append_history(..., session_key=None) — tags each JSONL record with "session_key" when provided (memory.py:285).
New MemoryStore.read_recent_history_for_prompt(since_cursor, *, session_key, unified_session=False) (memory.py:346) — prompt-injection reads are now scoped:
only entries matching the current session_key;
legacy/untagged entries are excluded (origin unknown — Dream still consumes them via the untouched read_unprocessed_history);
in unifiedSession mode, entries from any non-internal session are shared, while cron: / dream: / heartbeat histories stay isolated — except an internal session reading its own entries.
MemoryStore.raw_archive(...) and Consolidator.archive(...) accept and propagate session_key, including on the raw-dump fallback when the LLM call fails.
All Consolidator archive paths pass session.key: replay-overflow consolidation, the token-budget loop, and idle-session compaction; the token-estimation probe also passes session_key so estimates match the scoped injection.
nanobot/agent/context.py — build_system_prompt / build_messages take session_key + unified_session and use the new scoped reader.
nanobot/agent/loop.py — both build_messages call sites pass the session key and unified flag; both enforce_file_cap archive callbacks wrap raw_archive in partial(..., session_key=...).
nanobot/command/builtin.py — /new archival tags the snapshot with the session key; clearing remains synchronous and archival stays fire-and-forget in the background (drained by close_mcp).
Tests & docs — new tests for record tagging, per-session/unified/internal filtering, legacy exclusion, Dream still seeing everything, and archive/raw-fallback propagation; existing recent-history tests updated to use tagged entries; archive stubs in two test files updated to accept the new kwarg; docs/memory.md documents the session_key field and scoping.
Verification
pytest tests — 4100 passed, 3 skipped; the single failure (tests/webui/test_mcp_presets_api.py) also fails on the clean tree, so it's pre-existing and unrelated.
ruff check on changed files reports only the two pre-existing issues that exist on the baseline; no new lint errors.