Sessions are managed by the SessionManager class in nanobot/session/manager.py:
class Session: """Represents a conversation session.""" key: str # "channel:chat_id" identifier messages: list[dict] # Full message history last_consolidated: int # Index of last consolidated message created_at: datetime updated_at: datetime def get_history(self, max_messages: int = 50) -> list[dict]: """Get recent messages for context building.""" return self.messages[-max_messages:]
lines = []for m in old_messages: if not m.get("content"): continue tools = f" [tools: {', '.join(m['tools_used'])}]" if m.get('tools_used') else "" lines.append(f"[{m.get('timestamp', '?')[:16]}] {m['role'].upper()}{tools}: {m['content']}")current_memory = self.read_long_term()prompt = f"""Process this conversation and call the save_memory tool with your consolidation.## Current Long-term Memory{current_memory or "(empty)"}## Conversation to Process{chr(10).join(lines)}"""
3
Call LLM with save_memory Tool
The LLM calls a special tool to save the consolidated memory:
_SAVE_MEMORY_TOOL = [ { "type": "function", "function": { "name": "save_memory", "description": "Save the memory consolidation result to persistent storage.", "parameters": { "type": "object", "properties": { "history_entry": { "type": "string", "description": "A paragraph (2-5 sentences) summarizing key events/decisions/topics. Start with [YYYY-MM-DD HH:MM]. Include detail useful for grep search.", }, "memory_update": { "type": "string", "description": "Full updated long-term memory as markdown. Include all existing facts plus new ones. Return unchanged if nothing new.", }, }, "required": ["history_entry", "memory_update"], }, }, }]
[2026-03-06 10:00] USER: What's my favorite color?[2026-03-06 10:01] ASSISTANT: I don't have that information yet.[2026-03-06 10:02] USER: It's blue. Remember that.[2026-03-06 10:03] ASSISTANT: Got it, I'll remember that your favorite color is blue.[2026-03-06 10:05] USER: What programming languages do I know?[2026-03-06 10:06] ASSISTANT: Could you tell me?[2026-03-06 10:07] USER: Python, JavaScript, and Go.[2026-03-06 10:08] ASSISTANT: Thanks, I've noted that you know Python, JavaScript, and Go.
Output (MEMORY.md):
# User Preferences- Favorite color: blue# Skills- Programming languages: Python, JavaScript, Go
Output (HISTORY.md):
[2026-03-06 10:00] User shared personal preferences and technical background. Confirmed favorite color is blue. Discussed programming experience with Python, JavaScript, and Go. No specific projects or tasks initiated during this session.
Cause: Session history may have been cleared or consolidated prematurely.Solution: Check {workspace}/memory/MEMORY.md — the information should be there. If not, it wasn’t properly consolidated. You can tell the agent the information again.
Consolidation fails
Cause: LLM may have refused to call the save_memory tool or returned invalid data.Solution: The agent loop logs consolidation failures. Check logs with: